myjni.java
package com.sample.jnitest;
public class myjni {
public static native int SetString(String msg);
public static native String GetString();
//load library
static
{
System.loadLibrary("native_jni_test");
}
}
点击Make Project
会自动生成.class 文件:
C:\Users\alex\AndroidStudioProjects\jni_test\app\build\intermediates\javac\debug\classes\com\sample\jnitest\myjni.class
创建jni文件夹
使用Android Stutio的Terminal 切换到jni目录
使用命令自动生成头文件:
javah -d . -classpath C:\Users\alex\AndroidStudioProjects\jnitest\app\build\intermediates\javac\debug\classes com.sample.jnitest.myjni
生成的头文件如下:
com_sample_jnitest_myjni.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class com_sample_jnitest_myjni */
#ifndef _Included_com_sample_jnitest_myjni
#define _Included_com_sample_jnitest_myjni
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_sample_jnitest_myjni
* Method: SetString
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_sample_jnitest_myjni_SetString
(JNIEnv *, jclass, jstring);
/*
* Class: com_sample_jnitest_myjni
* Method: GetString
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_sample_jnitest_myjni_GetString
(JNIEnv *, jclass, jstring);
#ifdef __cplusplus
}
#endif
#endif
在jni目录下新建thirdlibs\libs
thirdlibs\include 目录。
将(1)中生成的第三方库so库文件(包含ABI文件夹)复制到libs目录
将(1)中的sample.h文件(包含ABI文件夹)复制到libs目录
创建com_sample_jnitest_myjni.c
实现头文件定义的函数即可。
JNI代码实现方式二:采用RegisterNatives的方式注册到JVM
//
// Created by alex on 8/27/2019.
//
#include
#include
#include
#include
#include
#include
#include
#define JNI_CLASSIC
#ifdef JNI_CLASSIC
#include "com_sample_jnitest_myjni.h"
static jstring g_msg;
/*
* Class: com_sample_jnitest_myjni
* Method: SetString
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_sample_jnitest_myjni_SetString
(JNIEnv *env, jclass clazz, jstring msg)
{
__android_log_print(ANDROID_LOG_DEBUG,"myjni","Java_com_sample_jni_1test_myjni_SetString...");
__android_log_print(ANDROID_LOG_DEBUG,"myjni","%s ",msg);
g_msg = msg;
return 0;
}
/*
* Class: com_sample_jnitest_myjni
* Method: GetString
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_sample_jnitest_myjni_GetString
(JNIEnv *env, jclass clazz, jstring msg)
{
__android_log_print(ANDROID_LOG_DEBUG,"myjni","Java_com_sample_jni_1test_myjni_GetString...");
msg = g_msg;
return 0;
}
#else
static jstring g_msg;
/*
* Class: com_sample_jnitest_myjni
* Method: SetString
* Signature: (Ljava/lang/String;)I
*/
jint Java_com_sample_jnitest_myjni_SetString
(JNIEnv *env, jclass clazz, jstring msg)
{
__android_log_print(ANDROID_LOG_DEBUG,"myjni","Java_com_sample_jni_1test_myjni_SetString...");
__android_log_print(ANDROID_LOG_DEBUG,"myjni","%s ",msg);
g_msg = msg;
return 0;
}
/*
* Class: com_sample_jnitest_myjni
* Method: GetString
* Signature: (Ljava/lang/String;)I
*/
jint Java_com_sample_jnitest_myjni_GetString
(JNIEnv *env, jclass clazz, jstring msg)
{
__android_log_print(ANDROID_LOG_DEBUG,"myjni","Java_com_sample_jni_1test_myjni_GetString...");
msg = g_msg;
return 0;
}
static const JNINativeMethod methods[]={
{"Java_com_sample_jnitest_myjni_SetString","(Ljava/lang/String;)I",(void *)Java_com_sample_jnitest_myjni_SetString},
{"Java_com_sample_jnitest_myjni_GetString","(Ljava/lang/String;)I",(void *)Java_com_sample_jnitest_myjni_GetString},
};
JNIEXPORT jint JNICALL JNIOnLoad
(JavaVM *jvm, void *reserved)
{
__android_log_print(ANDROID_LOG_DEBUG,"JNITag","enter jni_onload");
JNIEnv* env = NULL;
jclass cls;
jint result=-1;
if( (*jvm)->GetEnv(jvm,(void**)&env , JNI_VERSION_1_6) != JNI_OK)
return JNI_ERR;
cls=(*env)->FindClass(env,"com/sample/jnitest/myjni");
if(cls == NULL)
return JNI_ERR;
if((*env)->RegisterNatives(env,cls,methods,sizeof(methods)/sizeof(methods[0]))<0){
return JNI_ERR;
}
return JNI_VERSION_1_6;
}
#endif
在jni目录创建 Android.mk 和 Application.mk
创建Android.mk
LOCAL_PATH := $(call my-dir)
local_c_includes := \
$(LOCAL_PATH)
local_src_files:= \
com_sample_jnitest_myjni.c
include $(CLEAR_VARS)
LOCAL_LDLIBS +=-llog -lz
LOCAL_MODULE :=native_jni_test
LOCAL_SRC_FILES += $(local_src_files)
LOCAL_C_INCLUDES += $(local_c_includes)
LOCAL_MODULE_TAGS := optional
include $(BUILD_SHARED_LIBRARY)
创建 Application.mk
LOCAL_PATH := $(call my-dir)
APP_STL :=c++_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI :=all
APP_PLATFORM :=android-28
APP_BUILD_SCRIPT := $(LOCAL_PATH)/Android.mk
在Android Studio使用编译命令:
ndk-build NDK_APPLICATION_MK=./Application.mk NDK_PROJECT_PATH=./
即可生产各硬件版本的库文件:
[arm64-v8a] Compile : native_jni_test <= com_sample_jnitest_myjni.c
[arm64-v8a] SharedLibrary : libnative_jni_test.so
[arm64-v8a] Install : libnative_jni_test.so => libs/arm64-v8a/libnative_jni_test.so
[x86_64] Compile : native_jni_test <= com_sample_jnitest_myjni.c
[x86_64] SharedLibrary : libnative_jni_test.so
[x86_64] Install : libnative_jni_test.so => libs/x86_64/libnative_jni_test.so
[armeabi-v7a] Compile thumb : native_jni_test <= com_sample_jnitest_myjni.c
[armeabi-v7a] SharedLibrary : libnative_jni_test.so
[armeabi-v7a] Install : libnative_jni_test.so => libs/armeabi-v7a/libnative_jni_test.so
[x86] Compile : native_jni_test <= com_sample_jnitest_myjni.c
[x86] SharedLibrary : libnative_jni_test.so
[x86] Install : libnative_jni_test.so => libs/x86/libnative_jni_test.so