一、JNI 与 NDK
1、JNI定义
JNI : Java Native Interface的缩写
,从字面意思理解即Java的本地接口,Java 与 Native(即C/C++)层通信的桥梁。
2、NDK的定义
NDK:Native Development Kit的缩写
,即Android的工具开发包。作用即将C/C++代码编译到原生库中。
3、Java,JNI 与NDK三者之间的关系
1、NDK将C/C++编译到原生库,然后使用IDE的集成编译系统将原生库打包到APK中。
2、JNI编写对应的接口函数,函数的内部实现就是由C/C++来完成的。
3、Java代码通过调用JNI编写好的接口函数,调用底层C/C++实现的功能函数。
关于编译流程可参考 :https://developer.android.com/studio/build/
二、NDK下载
下载路径:https://developer.android.com/ndk/downloads
当然也可使用Android Studio 进行下载,只需要通过SDK Manager进行下载
注意:在下载过程中,我们需要下载两个构建工具:
- CMake:一个跨平台的编译构建工具
- LLDB:C/C++的调试工具
三、编写JNI程序
1.AndroidStudio会自动生成一个native-lib.cpp以及CmakeLists.txt文件
2.MainActivity的分析
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
}
Java 调用本地方法,使用的是native
关键字,而这个本地方法的实现在native-lib这个动态库中实现的,如果说使用动态库,就必须先加载这个动态库,那么就有了这个System.loadLibrary(“native-lib”)方法,这个是Java规定的。
3、native-lib.cpp的具体实现
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_com_android_jni_demo_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
那么MainActivity中native方法的具体实现就是由native-lib.cpp的Java_com_android_jni_demo_MainActivity_stringFromJNI()来完成;
该方法使用的是静态注册的方式,即Java_包名_类名_方法名的形式。
4、调用流程解析
1、Gradle调用外部构建脚本CmakeLists.txt
2、CMake按照构建脚本中的命令将C++源文件native-lib.cpp编译到共享库中,并命名为libnative-lib.so,Gradle编译系统将该文件打包到APK中。
3、运行时,应用的MainActivity会调用静态代码块System.loadLibrary()进行原生库的加载,那么应用就可以使用案例中原生库的stringFromJNI()方法。
4、通过调用stringFromJNI() 返回"Hello from C++"字符串,也就完成了Java层调用原生库中的函数返回的数据。
四、Gradle脚本的相关解析
要配置cmakelists.txt的路径,gradle在进行编译的时候,就会加载cmakelists.txt文件,通过cmake编译工具,执行文件中所编写的指令生成相应的动态库。通过配置externalNativeBuild
,
并且需要使用cmake{} 进行配置CMakeLists.txt 文件的路径,用于cmake构建构建工具执行相关编译指令。
android {
.....
defaultConfig{...}
externalNativeBuild{
cmake{
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
}
五、CmakeLists.txt的相关解析
1、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.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
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})
- 1、指定cmake的最小编译版本 :cmake_minimum_required
- 2、添加需要生成的原生库信息:add_library 其中包含原生库的名称,原生库的源文件路径,以及指定是生成共享库(SHARED - .so文件)还是静态库(STATIC - .a文件)
- 3、查找原生库所需要的第三方库或者是系统库:find_library 其中指定的库大多是系统的公共库,cmake默认搜索的路径也就是公共库的路径,在这个find_library( var path name) 其中name是存在的公共库的名称,var path 是一个变量用来存储name库的path路径。
- 4、最后一步就是将生成的原生库和共享库进行连接,即target_link_libraries(); 该指令里面会指定3步中var path变量,通过${path)的方式找到这个公共库,还有就是一个native-lib是该项目所要生成的原生库的名称。
可以参照我对于camkelists.txt文件的一些理解,如果有理解不对的地方,欢迎留言讨论:
##指定cmake最小版本
cmake_minimum_required(VERSION 3.4.1)
## 指定需要生成库的名称,生成库的源文件路径,以及指定生成的库是动态库还是静态库
add_library(
##指定生成的库的名称比如名字叫做hash
hash
##指定是动态库还是静态库 比如说生成一个动态库(SHARED), 静态库则是(STATIC)
SHARED
##指定生成库的源文件路径比如说是工程下的src/main/cpp/hash.cpp
src/main/cpp/hash.cpp
)
## 查找生成库可能要依赖的其他库 比如NDK 的 log 库
## 该命令是指的是查找指定的库文件; find_library(VAR name path) 查找到指定的预编译库,并将它的路径存储在变量中。
## 默认的搜索路径为cmake包含的系统库(解释: 也就是说你你指定一个log库,该库是位于NDK的公共库中,属于系统库,那么这个时候指定
库的名称即可,这个时候只需要指定该库的一个变量名称,方便在调用target_link_libraries 链接该库的时候使用其声明的一个变量名即可,
该变量名存储的是该系统库的路径。)
find_library(
## 首先指定该库对应的变量名 比如说 log-lib
log-lib
##
log
)
## 关联库 原则 目标库需要使用第三方库或者系统库,需要先进行find_library操作(1,找到这些库的路径,2,将这些库的路径保存到相应的
变量中,方便target_link_libraries调用)
target_link_libraries(
## 目标库
hash
## 需要关联的第三方或者系统库
${log_lib}
)
此处也参考了许多前辈的总结经验,如有侵犯,请告知。还有如果有需要转载请注明。