Skip to content

Most visited

Recently visited

navigation

Sample: hello-jni

This sample guides you through HelloJNI, a minimal application built with the NDK. This sample is in the samples/hello-jni/ directory under the root directory of your NDK installation.

Android.mk

The following two lines provide the name of the native source file, along with the name of the shared library to build. The full name of the built library is libhello-jni.so, once the build system adds the lib prefix and the .so extension.

LOCAL_SRC_FILES := hello-jni.c
LOCAL_MODULE    := hello-jni

For more information about what the Android.mk file does, and how to use it, see Android.mk.

Application.mk

This line tells the build system the CPU and architecture against which to build. In this example, the build system builds for all supported architectures.

APP_ABI := all

For more information about the Application.mk file, and how to use it, see Application.mk.

Java-side Implementation

The helloJNI.java file is located in hellojni/src/com/example/hellojni/. It calls a function to retrieve a string from the native side, then displays it on the screen.

The source code contains three lines of particular interest to the NDK user. They are presented here in the order in which they are used, rather than by line order.

This function call loads the .so file upon application startup.

System.loadLibrary("hello-jni");

The native keyword in this method declaration tells the virtual machine that the function is in the shared library (that is, implemented on the native side).

public native String stringFromJNI();

The Android framework calls the function loaded and declared in the previous steps, displaying the string on the screen.

tv.setText( stringFromJNI() );

C-side Implementation

The hello-jni.c file is located in hello-jni/jni/. It contains a function that returns a string that the Java side requested). The function declaration is as follows:

JNIEXPORT jstring JNICALL
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )

This declaration corresponds to the native function declared in the Java source code. The return type, jstring, is a data type defined in the Java Native Interface Specification. It is not actually a string, but a pointer to a Java string.

After jstring comes the function name, which is based on the Java function name and and the path to the file containing it. Construct it according to the following rules:

  • Prepend Java_ to it.
  • Describe the filepath relative to the top-level source directory.
  • Use underscores in place of forward slashes.
  • Omit the .java file extension.
  • After the last underscore, append the function name.

Following these rules, this example uses the function name Java_com_example_hellojni_HelloJni_stringFromJNI. This name refers to a Java function called stringFromJNI(), which resides in hellojni/src/com/example/hellojni/HelloJni.java.

JNIEnv* is the pointer to the VM, and jobject is a pointer to the implicit this object passed from the Java side.

The following line calls the VM API (*env), and passes it a return value: that is, the string that the function on the Java side had requested.

return (*env)->NewStringUTF(env, "Hello from JNI !
Compiled with ABI " ABI ".");
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)