android jni

By Long Luo

Android NDK

静态注册

1
2
3
4
5
6
7
8
9
10
11
cmake_minimum_required(VERSION 3.18.1)

project("hello-jni")

add_library(hello-jni SHARED
hello-jni.cpp)

# Include libraries needed for hello-jni lib
target_link_libraries(hello-jni
android
log)

hello-jni/app/src/main/cpp/hello-jni.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <jni.h>
#include <string>

#include "hello_jni.h"


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.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef HELLO_JNI_H
#define HELLO_JNI_H

#include <jni.h>

#ifdef __cplusplus
extern "C" {
#endif

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);


#ifdef __cplusplus
}
#endif

#endif // HELLO_JNI_H
1
2
3
4
5
6
7
ndkVersion '25.1.8937393'

externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}

动态注册

hello-jni-dynamic/app/src/main/cpp/CMakeLists.txt

1
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
cmake_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.cpp

1
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
#include "hello-jni.h"

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.h

1
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
#ifndef HELLO_JNI_H
#define HELLO_JNI_H

#include <jni.h>
#include <string>
#include "logger.h"
#include "constants.h"

#define SIZE(X) sizeof(X) / sizeof(X[0])


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);

#endif //HELLO_JNI_H

hello-jni-dynamic/app/src/main/cpp/src/constants.h

1
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
#ifndef HELLO_CONSTANTS_H
#define HELLO_CONSTANTS_H

#include <jni.h>


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;
}
}

#endif //HELLO_CONSTANTS_H

参考文献

  1. JNI
  2. NDK 使用入门
  3. CMake
  4. 将 NDK 与其他构建系统配合使用
  5. 示例:hello-jni

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