1.JNI
JNI(Java Native Interface):java 本地接口,是java自带的。用于打通Java层与Native层的桥梁。由于有些在Java上存在性能、安全问题或者跨平台,这样就JNI就起到纽带的作用。

2.NDK
NDK(Native Development Kit):本地开发工具集,是Android特有的,便于快速开发动态库。
3.NDK下载
3.1.google官网下载:
https://developer.android.google.cn/ndk/downloads?hl=zh_cn
3.2.AS中下载:

NDK:NDK开发工具
cmake:跨平台的构建系统
LLDB:C/C++调试工具
3.3.通过Project Structure打开如下对话框进行配置NDK路径(如是官网下载需要配置,AS下载不用)。

4.实例
4.1 新建


4.2 创建工程结构

开发步骤:
4.2.1 配置cmake构建脚本文件
# 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})
cmake_minimum_required(VERSION 3.4.1):指定cmake最低版本号
add_library( ):如上,将native-lib.cpp文件编译成名为native-lib的动态库。
find_library():添加log-lib动态库
target_link_libraries():关联native-lib,log-lib动态库
cmake语法会在后面详细和大家分享。
4.2.2 Gradle配置外部构建脚本路径

配置externalNativeBuild路径。
4.2.3 加载动态库声明native方法
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();
}
4.2.4 利用javah生成.h头文件
/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/javah -classpath . -jni -d /Users/zhouwen/Desktop/JNITest/app/src/main/jni zw.chowen.com.jnitest.MainActivity
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class zw_chowen_com_jnitest_MainActivity */
#ifndef _Included_zw_chowen_com_jnitest_MainActivity
#define _Included_zw_chowen_com_jnitest_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: zw_chowen_com_jnitest_MainActivity
* Method: stringFromJNI
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_zw_chowen_com_jnitest_MainActivity_stringFromJNI
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
对应c++里的函数如下,这里稍微提下方法规则(Java_包名_函数名)后面会详细讲到:
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_zw_chowen_com_jnitest_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
5.总结
5.1.cmake里配置好相关动态库的文件。
5.2.gradle配置好外部编译的cmake脚本文件路径。
5.3.申明Java层native方法,调用本地方法(public native String stringFromJNI();)。
5.4.javah生成.h头文件。
5.5.copy头文件生成的函数编写c++函数体。
网友评论