示例:hello-jni
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本示例将引导您开发一个使用 NDK 构建的超小 C/C++ 应用 hello-jni。此示例位于 NDK 示例代码库的 hello-jni 目录下的 android-mk 分支中。
Android.mk
以下两行代码会提供原生源文件的名称,以及要构建的共享库的名称。当构建系统添加 lib
前缀和 .so
扩展名后,该构建库的全名将为 libhello-jni.so
。
LOCAL_SRC_FILES := hello-jni.c
LOCAL_MODULE := hello-jni
如需详细了解 Android.mk
文件的用途和用法,请参阅 Android.mk。
Application.mk
这行代码的作用是告诉构建系统要针对哪些 CPU 和架构进行构建。在此示例中,构建系统会针对所有支持的架构进行构建。
APP_ABI := all
如需详细了解 Application.mk
文件及其用法,请参阅 Application.mk。
Java 端实现
helloJNI.java
文件位于 hellojni/src/com/example/hellojni/
中。它会调用一个函数,以从原生端提取字符串,然后在屏幕上显示这个字符串。
源代码中包含 NDK 用户应特别注意的三行代码。本文会按调用顺序(而非代码编写顺序)展示这三行代码。
应用启动后,此函数调用便会加载 .so
文件。
Kotlin
System.loadLibrary("hello-jni")
Java
System.loadLibrary("hello-jni");
在此方法声明中,使用 native
关键字的作用是告知虚拟机,函数位于共享库中(即在原生端实现)。
Kotlin
external fun stringFromJNI(): String
Java
public native String stringFromJNI();
Android 框架会调用在前面步骤中加载和声明的函数,从而在屏幕上显示字符串。
Kotlin
tv.text = stringFromJNI()
Java
tv.setText( stringFromJNI() );
C 端实现
hello-jni.c
文件位于 hello-jni/jni/
中。其中包含的函数能够返回 Java 端请求的字符串。此函数声明如下:
JNIEXPORT jstring JNICALL
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
此声明与 Java 源代码中声明的原生函数相对应。返回类型 jstring
是在 Java 原生接口规格中定义的数据类型。它是指向 Java 字符串的指针,并非真正的字符串。
返回 jstring
后,接下来是根据 Java 函数名和该函数名所在文件的路径返回的函数名。根据以下规则构建该函数名:
- 将
Java_
附加到它开头。
- 描述与顶级源目录相关的文件路径。
- 使用下划线代替正斜线。
- 删掉
.java
文件扩展名。
- 在最后一个下划线后,附加函数名。
按照这些规则,此示例使用的函数名为 Java_com_example_hellojni_HelloJni_stringFromJNI
。此名称指的是 hellojni/src/com/example/hellojni/HelloJni.java
中一个名为 stringFromJNI()
的 Java 函数。
JNIEnv*
是指向虚拟机的指针,jobject
是指向从 Java 端传递的隐式 this
对象的指针。
以下代码行会调用虚拟机 API (*env)
并向其传递返回值(即 Java 端函数请求的字符串)。
return (*env)->NewStringUTF(env, "Hello from JNI !
Compiled with ABI " ABI ".");
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-26。"],[],[],null,["# Sample: hello-jni\n\nThis sample guides you through hello-jni, a minimal\nC/C++ application built with the NDK. This sample is in the [hello-jni](https://github.com/android/ndk-samples/tree/android-mk/hello-jni) directory\nof ndk-samples repo, inside [android-mk branch](https://github.com/android/ndk-samples/tree/android-mk).\n\nAndroid.mk\n----------\n\nThe following two lines provide the name of the native source file, along\nwith the name of the shared library to build. The full name of the built\nlibrary is `libhello-jni.so`, once the build system adds the\n`lib` prefix and the `.so` extension. \n\n```\nLOCAL_SRC_FILES := hello-jni.c\nLOCAL_MODULE := hello-jni\n```\n\nFor more information about what the `Android.mk` file does, and how to use it, see\n[Android.mk](/ndk/guides/android_mk).\n\nApplication.mk\n--------------\n\nThis line tells the build system the CPU and architecture against which to build. In this\nexample, the build system builds for all supported architectures. \n\n```\nAPP_ABI := all\n```\n\nFor more information about the `Application.mk` file, and how to use it, see\n[Application.mk](/ndk/guides/application_mk).\n\nJava-side Implementation\n------------------------\n\nThe `helloJNI.java` file is located in `hellojni/src/com/example/hellojni/`. It calls\na function to retrieve a string from the native side, then displays it on the screen.\n\nThe source code contains three lines of particular interest to the NDK user.\nThey are presented here in the order in which they are used, rather than by\nline order.\n\nThis function call loads the `.so` file upon application startup. \n\n### Kotlin\n\n```kotlin\nSystem.loadLibrary(\"hello-jni\")\n```\n\n### Java\n\n```java\nSystem.loadLibrary(\"hello-jni\");\n```\n\nThe `native` keyword in this method declaration tells the\nvirtual machine that the function is in the shared library (that is, implemented on the native\nside). \n\n### Kotlin\n\n```kotlin\nexternal fun stringFromJNI(): String\n```\n\n### Java\n\n```java\npublic native String stringFromJNI();\n```\n\nThe Android framework calls the function loaded and declared in the\nprevious steps, displaying the string on the screen. \n\n### Kotlin\n\n```kotlin\ntv.text = stringFromJNI()\n```\n\n### Java\n\n```java\ntv.setText( stringFromJNI() );\n```\n\nC-side Implementation\n---------------------\n\nThe `hello-jni.c` file is located in `hello-jni/jni/`. It contains a function that\nreturns a string that [the Java side requested](#ji)). The function declaration is as\nfollows: \n\n```c++\nJNIEXPORT jstring JNICALL\nJava_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,\n jobject thiz )\n```\n\nThis declaration corresponds to the native function declared in the\nJava source code. The return type, `jstring`, is a data type defined\nin the\n[Java Native\nInterface Specification](http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html). It is not actually a string, but a\npointer to a Java string.\n\nAfter `jstring` comes the function name, which is based on the\nJava function name and the path to the file containing it. Construct it\naccording to the following rules:\n\n- Prepend `Java_` to it.\n- Describe the filepath relative to the top-level source directory.\n- Use underscores in place of forward slashes.\n- Omit the `.java` file extension.\n- After the last underscore, append the function name.\n\nFollowing these rules, this example uses the function name\n`Java_com_example_hellojni_HelloJni_stringFromJNI`. This name refers to a Java\nfunction called `stringFromJNI()`, which resides in\n`hellojni/src/com/example/hellojni/HelloJni.java`.\n\n`JNIEnv*` is the pointer to the VM, and\n`jobject` is a pointer to the implicit `this` object passed from\nthe Java side.\n\nThe following line calls the VM API `(*env)`, and passes it a return value:\nthat is, the string that the function on the Java side had requested. \n\n```c++\nreturn (*env)-\u003eNewStringUTF(env, \"Hello from JNI !\nCompiled with ABI \" ABI \".\");\n```"]]