サンプル: hello-jni
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
このサンプルは、NDK を使用して開発された hello-jni という小さな C / C++ アプリを紹介します。このサンプルは、android-mk ブランチ内の ndk-samples リポジトリの hello-jni ディレクトリにあります。
Android.mk
次の 2 行は、ネイティブ ソースファイルの名前と、ビルドする共有ライブラリの名前を指定しています。ビルドシステムにより 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 ユーザーにとって特に重要な 3 つの行があります。
ここでは、それらを記述されている順ではなく、使用される順に説明します。
この関数呼び出しは、アプリの起動時に .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 Native Interface Specification で定義されているデータ型です。実際は文字列ではなく、Java 文字列へのポインタです。
jstring
の後に関数名を記述します。関数名は、Java の関数名とその関数を含むファイルへのパスに基づいており、次のルールに従って記述します。
- 先頭に
Java_
を付加する。
- ソース ディレクトリの最上位からの相対ファイルパスを記述する。
- スラッシュの代わりにアンダースコアを使用する。
- ファイル拡張子(
.java
)は省略する。
- 最後のアンダースコアの後に関数名を付加する。
上記のルールにより、このサンプルで使用される関数名は Java_com_example_hellojni_HelloJni_stringFromJNI
になります。この名前は、hellojni/src/com/example/hellojni/HelloJni.java
にある stringFromJNI()
という Java 関数を指しています。
JNIEnv*
は VM へのポインタで、jobject
は Java サイドから渡される暗黙的な this
オブジェクトへのポインタです。
次の行は、VM API (*env)
を呼び出して、この API に戻り値(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```"]]