用户可以新建个工程,对C++ include 打勾 就是C++支持的工程了,我们这里要对build.gradle里的相关配置做了解
externalNativeBuild {
cmake {
cppFlags ""
path "CMakeLists.txt"
}
}
这个离cppFlags 是针对C++的在android开发中的配置例如:
cppFlags “-std=c++14 -frtti -fexceptions”
-std=c++14:这个是C++的版本配置,为14
-frtti:这个是是NDK对C++ RTTI的支持,在NDK R5的时候就支持了Rtti,只不过为了通用性的原因,默认是不支持的,需要用户手动开启
那么什么是RTTI呢,RTTI 全英文,Runtime Type Information,是运行时能够识别类或者变量的类型特写,类似java中的反射,
-fexceptions:这个是支持C++的异常控制,这个是也NDKR5 就支持的特写,为了通用性原因,默认不支持,需要手动打开
path “CMakeLists.txt”:这个就是CMakeLists的路径配置了,现在这个就是根目录下的文件
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
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 )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
上面这个是我拷贝的默认配置
cmake_minimum_required(VERSION 3.4.1)
这个是CMake的配置版本
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
这个是添加自己导入或者编写的C++源文件的作用
native-lib:这个是编译成so库的名称,生成的so会在前面加lib,比如这个最后生成的so:libnative-lib.so
这个要和java源文件对应的要一致
static {
System.loadLibrary("native-lib");
}
SHARED:这个代表是共享库,.so是共享库,如果不加SHARED默认是静态库,生成.a
src/main/cpp/native-lib.cpp:这个代表参加编译的C++源文件的路径,如果是多个源文件就依次添加多个就可以了
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 )
这个是搜索NDK默认支持的库,这里面就搜索android中LOG日志库,并且设置变量为log-lib
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
这个是要链接的库的目录,
native-lib:这个就是之前我们自己的写的库的变量
${log-lib} :这个是前面系统的log的变量
CMakeList中的配置还有很多,暂时就说这么多了,