1.Android NDK环境配置:
project---- local.properties下面可以直接添加ndk地址:
## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Mon Mar 18 13:40:13 CST 2019
ndk.dir=E\:\\SDK\\ndk-bundle
sdk.dir=E\:\\SDK
也可以选择:
File------Project Structure------SDK Location
添加CMake工具 AS下操作:
2.开发步骤:
2.1:创建本地的Javanative方法方便c代码实现:
package test.com.zh.testcmakedemo;
类名:MainActivity
public native String stringFromJNI();
2.2:在app/src/main 下创建一个文件例如 cpp;里面存放要加载执行的C代码:
C代码:
#include <jni.h>
#include<stdio.h>
#include <stdlib.h>
JNIEXPORT jstring
//test.com.zh.testcmakedemo
JNICALL
Java_test_com_zh_testcmakedemo_MainActivity_stringFromJNI(
JNIEnv *env,
jobject obj) {
return (*env)->NewStringUTF(env,"Hello from C");
}
2.3用CMake工具执行生成.so文件,需要先创建一个CMakeLists.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.
// 将要生成的.so文件的名字
Hello
# Sets the library as a shared library.
SHARED
// c代码的本路径
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.c )
# 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.
Hello
# Links the target library to the log library
# included in the NDK.
${log-lib} )
3.用CMake执行CMakeLists.txt,方法两种:
1: app的gradle添加:
externalNativeBuild {
cmake {
path file('CMakeLists.txt')
}
}
添加后的样子:
android {
compileSdkVersion 28
defaultConfig {
...
}
buildTypes {
}
externalNativeBuild {
cmake {
path file('CMakeLists.txt')
}
}
}
2.可视化执行的方法:
Project 下 点击app右键--------->link C++Project With Gradle 1:
2.选择CMake模式指定上面创建好的CMakeLists.txt文件:
指定好后:点击OK:
4.加载库文件执行本地方法:
static {
System.loadLibrary("Hello");
}
Toast.makeText(MainActivity.this,stringFromJNI(),Toast.LENGTH_LONG).show();
最后看下生成的目录结构:
这个是多生成文件:
整个目录路径应该是这样的:
CMake工具方式下libs不要copy.so文件;app gradle下无需指定JNI的libs。
打包生成的apk文件自动添加.so文件;如下图
生成的apk里面的.so可以看下:
arm64-v8a:机型为 MI Note3
好了以上就是CMake工具下的JNI开发,原理和ndk-build下的一样,规范都是JNI.h要求的。不同点就是执行.c生成二进制代码的方式不同而已。