准备工作:NKD环境搭建
- 使用Android studio 打开Project structure,在设置页面找到SDK location,可以设置NDK的路径,如果目前还没有NDK开发包,studio会提示你下载,下载完成后设置路径即可
- 检查local.properties文件里面有没有NDK路径:
开始创建JNI接口
- 新建一个类myJNI,然后添加JNI接口定义,如下所示,
public static native String sayHello();
JNI接口需要用native关键字修饰,我们会看到方法名报红,没关系,我们继续,图片中的方法名未报红是因为我们已经编译生成了.so文件。
2. build一下工程,检查myJNI.java编译后有没有生成class文件,在这个位置下:
app\build\intermediates\classes\debug\com\study\view\studytest\myJNI.class
3. 使用javah生成.h头文件,具体如下:
打开Terminal,输入命令进入到debug目录下,命令如下:
cd app/build/intermediates/classes/debug
然后使用javah+包名+文件路径来生成头文件,命令如下:
javah com.study.view.studytest.myJNI
然后我们会发现在app\build\intermediates\classes\debug\com\study\view\studytes目录下会有一个.h的文件生成
4. 有个.h头文件后,我们去实现.h文件里的方法,我们在main下新建一个jni文件夹,如图:
把生成的.h文件拷贝到jni文件夹下,在jni文件夹下,新建一个.c(c语言)或者.cpp(c++)的文件,来实现.h文件里声明的方法:把.h文件里面声明的方法拷贝到新建的c++文件里面,然后在文件里面引入.h文件:
引入.h文件#include “com_study_view_studytest_myJNI.h”,完成的.c文件如图
5. 在需要调用JNI接口的地方先加载动态库
static {
System.loadLibrary("jni_test");
}
生成so文件
- gradle3.0以前生成方式:
在根目录gradle.properties下面加上: android.useDeprecatedNdk=true意思就是允许使用低版本的NDK ;
在module下面的build.gradle下面加上ndk节点如下图:
ndk {
moduleName “jni_test”//定义自己Ndklibrary的名字
ldLibs “log” //添加log库
// 设置支持的SO库架构
abiFilters ‘armeabi’
}
build项目后发现报错:
Error: Flag android.useDeprecatedNdk is no longer supported and will be removed in the next version of Android Studio. Please switch to a supported build system.
Consider using CMake or ndk-build integration. For more information, go to:
https://d.android.com/r/studio-ui/add-native-code.html#ndkCompile
To get started, you can use the sample ndk-build script the Android
plugin generated for you at:
/Users/apple/Desktop/AndroidJNITest/app/build/intermediates/ndk/debug/Android.mk
Alternatively, you can use the experimental plugin:
https://developer.android.com/r/tools/experimental-plugin.html
To continue using the deprecated NDK compile for another 60 days, set
android.deprecatedNdkCompileLease=1512283120054 in gradle.properties
android.useDeprecatedNdk 不再支持了
让使用CMake or ndk-build
2. gradle3.0 生成so的方式:
先通过SDKManager下载:CMake和LLDB
在build.gradle的defaultConfig节点下加入:
// 使用Cmake工具
externalNativeBuild {
cmake {
cppFlags ""
//生成多个版本的so文件
abiFilters 'arm64-v8a','armeabi-v7a','x86'
}
}
在build.gradle的android节点下加入:
// 配置CMakeLists.txt路径
externalNativeBuild {
cmake {
path "CMakeLists.txt" // 设置所要编写的c源码位置,以及编译后so文件的名字
}
}
添加CMakeLists.txt文件到build.gradle文件同级目录下,具体内容如下:
# 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.
#CMakeLists.txt
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文件名称.
jni_test
# Sets the library as a shared library.
SHARED
# 设置这个so文件为共享.
# Provides a relative path to your source file(s).
# 设置这个so文件为共享.
src/main/jni/com_study_view_studytest_myJNI.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.
# 制定目标库.
jni_test
# Links the target library to the log library
# included in the NDK.
${log-lib} )
再重新build一下工程,即可生成so文件,生成的so文件位置:
app\build\intermediates\cmake\debug\obj
例如:app\build\intermediates\cmake\debug\obj\x86\libjni_test.so
最后调用JNI方法
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myJNI jni = new myJNI();
Log.d("jni", "onCreate: " + jni.sayHello());
}
}
运行结果: