Android JNI development entry and actual combat

Introduction: When it
comes to some algorithms or underlying drivers, it is often necessary to use jni to develop. Now it is officially recommended to use CMake tool to develop jni.
Using CMake to develop Jni is actually quite simple. If the requirements are not high, you only need to configure it briefly.


Configuration Environment
Using CMake for Jni development requires the use of CMake plugins, LLDB plugins, and NDK plugins, all of which can be installed quickly through Android Studio.
Open SDK Manager, find the Android SDK->SDK Tool option, install CMake, LLDB, NDK plug-ins.


Create a project that supports C++ code

The default is Next, Finish completes the project creation

Look at the configuration under app build.gradle

CMakeLists.txt under cpp is a very important configuration file, look at the configuration inside

Take a look at the code in native-lib.cpp, here is the specific implementation of the key method of JAVA calling C, and the native method declared in the Java code is used here to implement the functional algorithm in C/C++.

The loading of native-lib library and method call are implemented in MainActivity

 

So easy! This completes the basic development of JNI!

 

In the specific implementation, how to convert from Java native method declaration to C implementation, the method is as follows:

 1. Create the Java class Demo.java  

public class Demo {
    public native int addTwo(int a, int b);
    public native int addMore(int[] nums, int target);
}

2. Open the cmd command of windows (of course you can also use the Terminal console that comes with Android Studio), and switch to the path of the native method interface class Demo.java. Then use javac Demo.javato convert the class to a .class file. Note: Do not include Chinese comments in Demo.java.

3. Then switch back to the java directory of the project and use the javah -classpath . -jni + 包名 + 文件名generated .h file (the dot before -jni in the command is necessary). Use the cd... command to switch the upper-level directory of windows

4. After the execution is complete, you can see a .h file with package name + file name in the java directory, and the jni function name code corresponding to Demo.java is written in it.

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_wasu_myjni_jni_Demo */

#ifndef _Included_com_wasu_myjni_jni_Demo
#define _Included_com_wasu_myjni_jni_Demo
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class: com_wasu_myjni_jni_Demo
 * Method:    sum
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_com_wasu_myjni_jni_Demo_addTwo
  (JNIEnv *, jobject, jit, jit);

/*
 * Class: com_wasu_myjni_jni_Demo
 * Method:    twoSum
 * Signature: ([II)I
 */
JNIEXPORT jint JNICALL Java_com_wasu_myjni_jni_Demo_addMore
  (JNIEnv *, jobject, jintArray, jint);

#ifdef __cplusplus
}
#endif
#endif

As you can see, the jni function name corresponding to the method in Demo.java has been written in the file

At this time, copy and paste the contents of the generated .h file into the native-lib.cpp file we created before, and the naming part in the jni code is complete. Then implement the corresponding method in native-lib.cpp.

Note: Android Sudio now provides a shortcut implementation. Click the method that shows red in the Demo.java class and a create JNI function for addTwo will pop up. Click to create the corresponding C++ method in native-lib.cpp to achieve specific Function.


After writing the jni function name, you will find that the code in Demo.java is no longer red, indicating that the method in Demo.java can call the jni function correctly. Load native-lib in MainActivity, and call the method in Demo in specific places.

// Used to load the 'native-lib' library on application startup.
static {
    System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView(R.layout.activity_main);

    // Example of a call to a native method
    TextView tv = findViewById(R.id.sample_text);
    tv.setText(" a + b = "+ new Demo().addTwo(2, 3)); //Call
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/MYBOYER/article/details/106115830