The target
Simplify JNI work with Android Studio. With the help of the IDE, the JNI part of the code is automatically generated, and you only need to focus on the processing of the business code. Complete a simple Demo, define a native function, call this method in the Java layer, return a string,native layer calls a Java function, complete Java-> Jni, Jni->Java.
The source code
The code in the following article is in this section
steps
- Create a new project that supports Native
- Define a new Natvie function
- The Java layer method implementation is invoked through native methods
Function implementation
New Native project
What if it’s an existing project?
What if it’s an off-the-shelf feature that needs to support JNI
The corresponding code in the newly generated location can be collated into the old project
- build.gradle
- CPP folder
Complete the definition and implementation of Java ->Jni ->Java methods
Define a native method, because it is not defined, so Android Studio is marked red. Right click and select the template code for creating Jni as shown in the image below.
At this time, the template code will be automatically generated in the native-lib. CPP file, so you can write business code directly in the template.
Java layer code that displays a toast by passing through the context, then processes the passed string and returns the processed string
Java->Jni ->Java and the context pass through and display the return value. Can address a large portion of business Jni invocation requirements.