android jni
By Long Luo
静态注册
1 | cmake_minimum_required(VERSION 3.18.1) |
hello-jni/app/src/main/cpp/hello-jni.cpp1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
JNIEXPORT jstring JNICALL
Java_me_longluo_hellojni_HelloJniActivity_stringFromJNI(JNIEnv *env, jobject /* this */) {
std::string hello = "Hello from JNI.";
return env->NewStringUTF(hello.c_str());
}
JNIEXPORT jint JNICALL
Java_me_longluo_hellojni_HelloJniActivity_add(JNIEnv *env, jobject /* this */, jint a, jint b) {
int result = a + b;
return result;
}
hello-jni/app/src/main/cpp/hello_jni.h1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
extern "C" {
JNIEXPORT jstring JNICALL
Java_me_longluo_hellojni_HelloJniActivity_stringFromJNI(JNIEnv *env, jobject /* this */);
JNIEXPORT jint JNICALL
Java_me_longluo_hellojni_HelloJniActivity_add(JNIEnv *env, jobject /* this */, jint a, jint b);
}1
2
3
4
5
6
7ndkVersion '25.1.8937393'
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}
动态注册
hello-jni-dynamic/app/src/main/cpp/CMakeLists.txt1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50cmake_minimum_required(VERSION 3.18.1)
# Declares and names the project.
project("hello-jni-dynamic")
# Automatically all files in a directory to a target
file (GLOB_RECURSE HELLOJNI_SRCS CONFIGURE_DEPENDS
"src/*.cpp"
"src/*.h"
)
# 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.
hello-jni
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${HELLOJNI_SRCS})
# 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.
hello-jni
# Links the target library to the log library
# included in the NDK.
${log-lib}
)
hello-jni-dynamic/app/src/main/cpp/src/hello-jni.cpp1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
jint JNI_OnLoad(JavaVM *vm, void *reserved){
UnionJNIEnvToVoid uenv;
uenv.venv = nullptr;
jint result = -1;
JNIEnv* env;
ALOGI("JNI_OnLoad");
if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_6) != JNI_OK) {
ALOGE("ERROR: GetEnv failed");
goto bail;
}
env = uenv.env;
if (registerNatives(env) != JNI_TRUE) {
ALOGE("ERROR: registerNatives failed");
goto bail;
}
result = JNI_VERSION_1_6;
bail:
return result;
}
hello-jni-dynamic/app/src/main/cpp/src/hello-jni.h1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
typedef union {
JNIEnv *env;
void *venv;
} UnionJNIEnvToVoid;
/*
* Register several native methods for one class.
*/
static int registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *gMethods,
int numMethods) {
jclass clazz;
clazz = env->FindClass(className);
if (clazz == NULL) {
ALOGE("Native registration unable to find class '%s'", className);
return JNI_FALSE;
}
if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
ALOGE("RegisterNatives failed for '%s'", className);
return JNI_FALSE;
}
return JNI_TRUE;
}
/*
* Register native methods for all classes we know about.
*
* returns JNI_TRUE on success.
*/
static int registerNatives(JNIEnv *env) {
if (!registerNativeMethods(env, HelloJNI::constants,
HelloJNI::constants_methods,
SIZE(HelloJNI::constants_methods))
) {
return JNI_FALSE;
}
return JNI_TRUE;
}
/*
* This is called by the VM when the shared library is first loaded.
*/
jint JNI_OnLoad(JavaVM *vm, void *reserved);
hello-jni-dynamic/app/src/main/cpp/src/constants.h1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
namespace HelloJNI{
static jfloat JniVersion(JNIEnv *env, jobject object) {
return 1.6;
}
static jstring JniDynamicRegister(JNIEnv *env, jobject object){
return env->NewStringUTF("Hello Jni Dynamic Register");
}
static const char *constants = "me/longluo/hellojni/JniLibrary";
static JNINativeMethod constants_methods[] = {
{"nativeGetJniVersion", "()F", (void *) JniVersion},
{"nativeGetJniDynamicRegister", "()Ljava/lang/String;", (void *) JniDynamicRegister}
};
static std::string jString2String(JNIEnv *env, jstring jStr) {
if (!jStr) {
return "";
}
const jclass stringClass = env->GetObjectClass(jStr);
const jmethodID getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
const jbyteArray stringJbytes = (jbyteArray) env->CallObjectMethod(jStr, getBytes, env->NewStringUTF("UTF-8"));
size_t length = (size_t) env->GetArrayLength(stringJbytes);
jbyte* pBytes = env->GetByteArrayElements(stringJbytes, NULL);
std::string ret = std::string((char *)pBytes, length);
env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT);
env->DeleteLocalRef(stringJbytes);
env->DeleteLocalRef(stringClass);
return ret;
}
}
参考文献
https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/intro.html
https://medium.com/@sarafanshul/jni-101-introduction-to-java-native-interface-8a1256ca4d8e
https://medium.com/@sarafanshul/jni-201-dynamic-registration-a1ad7fa50b21
https://gist.github.com/okamayana/79c98545eb99c4877979