Android Studio环境下实现JNI调用
- 环境准备:
(1).NDKl环境, 首先需要下载NDK。
翻墙:http://developer.android.com/intl/zh-cn/ndk/downloads/index.html
不翻墙:http://www.androiddevtools.cn
MAC版本下载完成后,直接就是解压完成的包,放到/Users/longcheng/Documents/Android/android-ndk-r14b下面。
(2).Android设置NDK路径设置
local.properties里面添加:
ndk.dir=/Users/longcheng/Documents/Android/android-ndk-r14b
(3).grade.properties里面添加:
android.useDeprecatedNdk=true
(4).app/build.grade里面添加生成so的名称
ndk {
moduleName"hello-jni"// 生成库的名称 默认前面会加 lib ( libhello-jni.so )
}
(5). src目录下面建立jni文件夹, 通过右键->New->Folder->JNI Folder创建
- 环境准备完毕,现在写一个HelloJni类:
static{
System.loadLibrary("hello-jni");}
public nativeString stringFromJni();```
3. Build->make project, 在app/build/intermediates/classes/debug下面能够找到HelloJni.class类,在app/build/intermediates/classes/debug下面用命令行执行
javah -d /Users/longcheng/Documents/AndroidCode/DemoCode/JNIDemo/app/src/main/jni(jni的目录) jrdcom.com.jnidemo.HelloJni
4. 在jni目录下面将会生成jrdcom_com_jnidemo_HelloJni.h头文件,如下图所示:
/* DO NOT EDIT THIS FILE - it is machine generated */
include <jni.h>
/* Header for class jrdcom_com_jnidemo_HelloJni */
ifndef _Included_jrdcom_com_jnidemo_HelloJni
define _Included_jrdcom_com_jnidemo_HelloJni
ifdef __cplusplus
extern "C" {
endif
/*
- Class: jrdcom_com_jnidemo_HelloJni
- Method: stringFromJni
- Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_jrdcom_com_jnidemo_HelloJni_stringFromJni
(JNIEnv *, jobject);
ifdef __cplusplus
}
endif
endif
5. jni文件中定义HelloJni.c文件
//
// Created by longcheng on 2017/6/16.
//
//
// Created by longcheng on 2017/6/16.
//
include <string.h>
include <assert.h>
include "jrdcom_com_jnidemo_HelloJni.h"
JNIEXPORT jstring JNICALL Java_jrdcom_com_jnidemo_HelloJni_stringFromJni
(JNIEnv *env, jobject thiz) {
return (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI ");
}
//参数映射表
static JNINativeMethod methods[] = {
{"stringFromJni", "()Ljava/lang/String;", (void*)getCLanguageString},
};
//自定义函数,为某一个类注册本地方法,调运JNI注册方法
static int registerNativeMethods(JNIEnv* env , const char* className , JNINativeMethod* gMethods, int numMethods)
{
jclass clazz;
clazz = (*env)->FindClass(env, className);
if (clazz == NULL) {
return JNI_FALSE;
}
if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) {
return JNI_FALSE;
}
return JNI_TRUE;
}
static int registerNatives(JNIEnv* env)
{
const char* kClassName = "com/jared/emjnistudy/NdkJniUtils";//指定要注册的类
return registerNativeMethods(env, kClassName, methods, sizeof(methods) / sizeof(methods[0]));
}
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env = NULL;
jint result = -1;
if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
return -1;
}
assert(env != NULL);
//动态注册,自定义函数
if (!registerNatives(env)) {
return -1;
}
return JNI_VERSION_1_4;
}
6. 编译一下,就可以在app/build/intermediates/ndk/debug下面找到生成的so文件:
![image.png](http://upload-images.jianshu.io/upload_images/4929838-da95681726980f07.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)7. Java代码中就可以使用这个接口了:
HelloJni helloJni =newHelloJni();
Toast.makeText(this, helloJni.stringFromJni(), Toast.LENGTH_SHORT).show();