Use of JNI (Android studio)

Posted by spivey on Thu, 04 Nov 2021 09:57:28 +0100

1, Role of JNI:

1. Make JAVA interact with other local languages (C, C + +);

2. Call C, C + + and other language codes in JAVA code or C, C + + call JAVA code.

Because JAVA has the characteristics of cross platform, the interaction ability between JAVA and local code is weak. Using JNI feature can enhance the interaction ability between JAVA and local code.

2, How to use JNI in Android Studio:


1. In Android studio, click file - > Settings... To check whether the two tools in the red box in the figure below have been downloaded. If not, they need to be downloaded:

2. Configuration environment:

Open your own environment configuration file. bashrc and add the following two lines at the end:

export ANDROID_NDK=/home/xayf/Android/Sdk/ndk/23.1.7779620

Note: the bold and red parts are the positions where NDK is installed in its own system.

Operation method:

1. Create a new project JNITest. Under the package name of the project, create a new JAVA class named JNITest:

package com.example.jnitest;

public class JNITest {
    public native String getJNIString();

2. Compile file with javac command to generate JNITest.class file.

In the project directory ~ / AndroidStudioProjects/JNITest, execute the following command: javac app/src/main/java/com/example/jnitest/

After execution, you can see that the corresponding class file is generated under the package name, as shown in the following figure:

  3. After generating the JNITest.class file, you need to generate the corresponding. h file:

In the project directory ~ / AndroidStudioProjects/JNITest, execute the following command: javah -d app/src/main/jni -classpah app/src/main/java/ -jni -v com.example.jnitest.JNITest

After execution, you can see that the jni directory is generated in the app/src/main directory, and the corresponding. H file is generated in the directory. The file name is com_example_jnitest_JNITest.h

4. Under the jni directory, create a new cpp file named JNITest.cpp, which implements the local methods defined in the file:

Right click JNI - > New - > C / C + + source file - > and enter JNITest

// Created by xayf on 2021/11/4.

#include <stdio.h>
#include <stdlib.h>
#include <jni.h>

#ifndef _Included_com_example_jnitest_JNITest
#define _Included_com_example_jnitest_JNITest
#ifdef __cplusplus
extern "C" {

JNIEXPORT jstring JNICALL Java_com_example_jnitest_JNITest_getJNIString
(JNIEnv* env, jobject obj)
     return env->NewStringUTF("Hello From JNITest Function(getJNIString)");
#ifdef __cplusplus

5. Create another file in jni Directory:

Right click jni Directory - > New - > file - > enter, and write the following configuration in the file:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
JNITest.cpp \

6. Enter the "~ / AndroidStudioProjects/JNITest/app/src/main/jni" directory in the terminal, execute NDK build, and then automatically generate the lib folder in the app/src/main directory. The generated so library file is in the lib folder, as shown in the following figure:

  7. Establish the relationship between so library and JAVA file, and create the layout file activity corresponding to the MainActivity activity activity of the project_ In main, add a TextView control:

        android:text="Hello World!"
        app:layout_constraintTop_toTopOf="parent" />

  In the MainActivity activity, add code. The file contents are as follows:

package com.example.jnitest;


import android.os.Bundle;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    static {
    protected void onCreate(Bundle savedInstanceState) {
        TextView textView = (TextView) findViewById(;
        JNITest jniTest = new JNITest();

8. Key steps: add the following code in the build.gradle file of Android project:

android {
    sourceSets {
         main() {
             jniLibs.srcDirs = ['src/main/libs']
             jni.srcDirs = [] //Mask the default jni build process


9. Run the project to the mobile phone, and the display screen is as follows:

The text shown in the figure is the string value returned by the method defined in the cpp file.





Topics: Java Android