ตัวอย่าง: Hello-jni
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ตัวอย่างนี้จะแนะนำ hello-jni ซึ่งเป็นแอปพลิเคชัน C/C++ ขั้นต่ำที่สร้างด้วย NDK ตัวอย่างนี้อยู่ในไดเรกทอรี hello-jni ของรีโป ndk-samples ภายในสาขา android-mk
Android mk
2 บรรทัดต่อไปนี้แสดงชื่อไฟล์ต้นทางเนทีฟ พร้อมกับชื่อคลังที่ใช้ร่วมกันเพื่อสร้าง ชื่อเต็มของไลบรารีที่สร้างขึ้นคือ libhello-jni.so
เมื่อระบบบิลด์เพิ่มคำนำหน้า lib
และส่วนขยาย .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/
โดยจะเรียกใช้ฟังก์ชันเพื่อดึงสตริงจากด้านดั้งเดิม แล้วแสดงบนหน้าจอ
โค้ดต้นฉบับมี 3 บรรทัดที่น่าสนใจสำหรับผู้ใช้ 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
ซึ่งเป็นไปตามกฎเหล่านี้ ชื่อนี้หมายถึงฟังก์ชันของ Java ชื่อ stringFromJNI()
ซึ่งอยู่ใน hellojni/src/com/example/hellojni/HelloJni.java
JNIEnv*
คือตัวชี้ไปยัง VM และ jobject
คือตัวชี้ไปยังออบเจ็กต์ this
โดยนัยที่ส่งผ่านจากด้าน Java
บรรทัดต่อไปนี้เรียกใช้ VM API (*env)
และส่งค่าส่งกลับไปให้ นั่นคือสตริงที่ฟังก์ชันฝั่ง Java ขอ
return (*env)->NewStringUTF(env, "Hello from JNI !
Compiled with ABI " ABI ".");
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[[["เข้าใจง่าย","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"]],["อัปเดตล่าสุด 2025-07-26 UTC"],[],[],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```"]]