May you return with a young heart after years of fighting.
愿你出走半生,归来仍是少年。
1.创建项目与NDK配置
第一步:创建项目
项目名:JNIDemo
包名:com.pfj.jnidemo
第二步:配置NDK环境
下载ndk需要的工具:打开SDKManager 然后选择SDK Tools
需要下载LLDB、CMake、NDK如图。
第三步
下载完成以后打开 File – Project Structure – SDK Location,使用SDK Tools下载成功的NDK。
2.JNI开发
2.1创建一个 类HelloNDK.java
public class HelloNDK {
// 动态导入 so 库
static {
System.loadLibrary("HelloNDK");
}
//创建一个 native 方法,交给C实现
public native static String helloWorld();
}
2.2创建 C 语言文件,创建 so 库
在命令行中执行如下两个命令:
cd app/src/main/java
javah com.pfj.jnidemo.HelloNDK(即:javah HelloNDK全类名)
注意:先把HelloNDK的中文注释删掉,再执行命令,否则会报错。
生成了一个com_pfj_jnidemo_HelloNDK.h 文件
然后在main目录下创建cpp目录,并将上面生成的.h文件移动到该目录
com_pfj_jnidemo_HelloNDK.h代码如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_pfj_jnidemo_HelloNDK */
#ifndef _Included_com_pfj_jnidemo_HelloNDK
#define _Included_com_pfj_jnidemo_HelloNDK
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_pfj_jnidemo_HelloNDK
* Method: helloWorld
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_pfj_jnidemo_HelloNDK_helloWorld
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
2.3编写 c++ 实现方法
在cpp目录下创建HelloNDK.h(可为空文件)和HelloNDK.cpp文件,并在HelloNDK.cpp编写如下代码:
#include "HelloNDK.h"
#include "com_pfj_jnidemo_HelloNDK.h"
JNIEXPORT jstring JNICALL Java_com_pfj_jnidemo_HelloNDK_helloWorld
(JNIEnv *env, jclass jclass1){
return env->NewStringUTF("你得到了Native返回数据");
}
2.4创建一个 CMakeLists.txt 文件
在app目录下创建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.
HelloNDK
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/HelloNDK.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.
HelloNDK
# Links the target library to the log library
# included in the NDK.
${log-lib} )
2.5引用 CMakeLists.txt 文件
在app目录下的build.gradle中加上如下代码:
..
android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "com.pfj.jnidemo"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
//jni配置
externalNativeBuild{
cmake{
abiFilters 'armeabi-v7a'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
//jni配置
externalNativeBuild{
cmake{
path 'CMakeLists.txt'
}
}
}
..
2.6生成so文件
2.7运行程序
得到了C文件返回数据。