MainActivity static { System.loadLibrary("xxx"); // 库名 } // 加载so编译后 真实文件名字 libxxx.so
public native String stringFromJNI(); // 函数实现在so层 声明函数
buildgradle // cmake 使用cpp 标准 externalNativeBuild { cmake { cppFlags '-std=c++11' } } }
// CMakeLists 指导编译 目录 版本 externalNativeBuild { cmake { path file('src/main/cpp/CMakeLists.txt') version '3.18.1' } }
externalNativeBuild { cmake { cppFlags '-std=c++11' } ndk { abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64' } } } // abi 过滤 编译指定so 省略ndk过滤默认编译全部
project("xxx") 工程名 # 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. xxx // 添加库 名字 文件 # Sets the library as a shared library. SHARED // 指明库类型 共享 so # Provides a relative path to your source file(s). xxx.cpp) // 指明源文件 // cmakeclists 指明需要编译的文件名
// 链接相关 target_link_libraries( # Specifies the target library. xxx 库名 # Links the target library to the log library # included in the NDK. ${log-lib})