NDK是Google为Android实现JNI调用而提供的工具
原生库的构建方法包括旧版的ndk-build,及新版的CMake
参考Google文档ndk-build
ndk-build构建方法
构建脚本:
语法与AOSP的Android.mk文件类似,其内定义若干个模块
LOCAL_PATH := $(call my-dir) #文件所在目录
include $(CLEAR_VARS) #清除各类LOCAL_XXX变量(不包括LOCAL_PATH)
LOCAL_MODULE := hello-jni #模块名(生成libhello-jni.so)
LOCAL_SRC_FILES := hello-jni.c #源文件
include $(BUILD_SHARED_LIBRARY) #连接以上内容。BUILD_SHARED_LIBRARY指向GNU Makefile脚本,确定构建内容及方法
描述应用需要的原生模块
CMake构建方法
参考Google文档add c & c++ code
构建脚本:
- CMakeList.txt
# 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从NDK现有库中查找到android/log库,并赋值给log-lib变量;
#再通过target_link_libraries把log-lib链接到native-lib库;
#基于此,native-lib中的.cpp文件可#include <android/log.h>引入对应库,并使用__android_log_print方法记录log
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} )
Gradle关联到原生库
- 在模块build.gradle中关联原生库的脚本文件:
android {
...
defaultConfig {...}
buildTypes {...}
// Encapsulates your external native build configurations.
externalNativeBuild {
// Encapsulates your CMake build configurations.
cmake {
// Provides a relative path to your CMake build script.
path "CMakeLists.txt"
}
}
}
如果需关联ndk-build项目,则使用ndkBuild { }块替换cmake { },关联Android.mk文件。
- Gradle可选配置:
在模块build.gradle的defaultConfig{ }(可在productFlavors中使用不同配置)中配置CMake或ndk-build的可选参数和标志。
android {
...
defaultConfig {
...
// This block is different from the one you use to link Gradle
// to your CMake or ndk-build script.
externalNativeBuild {
// For ndk-build, instead use ndkBuild {}
cmake {
// Passes optional arguments to CMake.
arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang"
// Sets optional flags for the C compiler.
cFlags "-D_EXAMPLE_C_FLAG1", "-D_EXAMPLE_C_FLAG2"
// Sets a flag to enable format macro constants for the C++ compiler.
cppFlags "-D__STDC_FORMAT_MACROS"
}
}
}
buildTypes {...}
productFlavors {
...
demo {
...
externalNativeBuild {
cmake {
...
// Specifies which native libraries to build and package for this
// product flavor. If you don't configure this property, Gradle
// builds and packages all shared object libraries that you define
// in your CMake or ndk-build project.
targets "native-lib-demo"
}
}
}
paid {
...
externalNativeBuild {
cmake {
...
targets "native-lib-paid"
}
}
}
}
// Use this block to link Gradle to your CMake or ndk-build script.
externalNativeBuild {
cmake {...}
// or ndkBuild {...}
}
}