一. JNI开发方式一
-
1.在类中申明本地方法
public native String hello();
-
2.创建JNI目录,创建c语言源文件

- 3.编写C语言代码(以返回一个简单字符串为例)
#include <jni.h>
//jstring: 返回值
//方法命名: Java_包名_类名_方法名(JNIEnv * env, jobject obj)
jstring Java_jni_itheima_com_jni33_MainActivity_hello_FromC(JNIEnv *env,jobject obj){
return (*env)->NewStringUTF(env,"hello");
}
- 4.使用书写android.mk application.mk 配置文件

- android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello #生成的so库名 lib库名.so
LOCAL_SRC_FILES := hello.c #编译的C语言源文件
include $(BUILD_SHARED_LIBRARY) #指定生成动态库
-
application.mk
#指定生成的库的平台 APP_ABI := armeabi armeabi-v7a x86
-
5.cd到jni目录下执行 ndk-build 命令生成so库(使用前自行配置环境变量)
-
6.更改so库的默认生成路径
打开项目下的build.gralde 文件加入中增加sourceSets闭包android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "jni.huyingzi.com.jni" minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } //默认指向目录(可以根据自己的喜好如果不想更改可以不增加这一步) sourceSets{ main{ jniLibs.srcDirs=['libs'] } }
-
7.在类中使用静态代码块加载so库
static{ System.loadLibrary("hello"); }
正确完成以上7步 就可以在java代码中正常调用自己申明的本地方法了。
二. JNI开发方式二
- 1.申明本地方法
- 2.在AS中配置好ndk路径

- 3.在模块处右键New - Folder - JNI Folder 他会在main目录下自动创建jni目录


- 4.在生成的jni下面创建C语言源文件

-
5.编写源代码
#include <jni.h> //jstring: 返回值 //方法命名: Java_包名_类名_方法名(JNIEnv * env, jobject obj) jstring Java_jni_itheima_com_jni33_MainActivity_hello_FromC(JNIEnv *env,jobject obj){ return (*env)->NewStringUTF(env,"hello"); }
-
6.打开项目下的buidl.gradle文件 在defaultConfig闭包中配置ndk闭包
android {
compileSdkVersion 24
buildToolsVersion "25.0.2"defaultConfig { applicationId "com.itheima.hellob" minSdkVersion 15 targetSdkVersion 24 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" ndk{ moduleName "hello" //指定生成的库名 abiFilters "armeabi","armeabi-v7a","x86" //指定生成的平台 ldLibs "log" //引用log库 } }
-
7.在类中使用静态代码块加载so库
static{ System.loadLibrary("hello"); }
注: 如果使用的低版本的ndk 可以回报错,需要在 gradle.progerties中添加如下配置:
#使用过时的ndk
android.useDeprecatesNdk=true
- 8.生成的so库的位置: build - intermediates - ndk - debug - lib
三 Cmake 实现跨平台开发JNI三
- 1.创建项目的时候选择支持C/C++开发

- 2.在如下位置创建cpp目录 并创建c语言源文件

-
3.编写C语言源代码
#include <jni.h> //jstring: 返回值 //方法命名: Java_包名_类名_方法名(JNIEnv * env, jobject obj) jstring Java__huyingzi_com_jni_MainActivity_hello(JNIEnv *env,jobject obj){ return (*env)->NewStringUTF(env,"hello"); }
-
4.在模块目录下配置CmakeLists.txt 文件

cmake_minimum_required(VERSION 3.4.1)
add_library( # 指定生成的库名
hello
# 指定生成动态库
SHARED
# 指定编译的C语言源文件 有多个源文件需要将全路径依次排开
src/main/cpp/hello.c )
#找jni提供库
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 )
# 链接库
target_link_libraries( # 链接自己生成的so库
hello
${log-lib} )
- 5.关联配置文件 右键模块 - Link C++ Project with Gradle

- 6.选择Cmake方式 Path处关联CmakeLists.txt 的路径

-
7.在build.gradle下指定生成的so库平台
android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.itheima.cmake" minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" //制定生成的库平台 externalNativeBuild { cmake { abiFilters "armeabi","armeabi-v7a","x86" } } }
-
8.在类中使用静态代码块加载so库
static{ System.loadLibrary("hello"); }
-
9.so库的生成目录如下

网友评论