将Android.mk转换成Cmake使用

Android studio 2.2之后就引入了Cmake 编译Native code。我们可以通过gradle+cmakelists 配置脚本自动构建native code 生产so库。

gradle(app/build.gradle)配置:

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
      ......
        externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

defaultConfig.externalNativeBuild块是可自定义配置,可以重定义 android.externalNativeBuild块中内容。详细

对于这样一个Android.mk文件

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := TwoDG1
LOCAL_SRC_FILES := Triangle.cpp Square.cpp TwoDG1.cpp

LOCAL_LDLIBS := -lGLESv1_CM -llog

include $(BUILD_SHARED_LIBRARY)

我们改成CMakeLists脚本编译

add_library( # Sets the name of the library.
             TwoDG1

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp
             src/main/cpp/Triangle.cpp
             src/main/cpp/Square.cpp)

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log)

#add lib dependencies
target_link_libraries( # Specifies the target library.
                       TwoDG1

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib}
                       GLESv1_CM)

你可能感兴趣的:(android-ndk开发)