NDK
Native Development Suite (NDK) is a set of tools that can be written in C and C++ code in Android applications for Java to Native communication.
The flow chart
The first step
Write the target class files Directory: app/SRC/main/com/zhyx/jnidemo1 / jniclass/JniExample Java
public class JniExample {
//native
public static native String getJniStr(a);
}
Copy the code
The second step
Command to generate the corresponding header file
javac -h -jni_temp ./jniclass/JniExample.java
Copy the code
Javac -h < directory > specifies the location of the native header file generated – jni_temp generated directory file called “- jni_temp”. / jniclass JniExample. Java target class file name
The third step
Write c/ C ++ concrete implementation in app/ SRC /main directory create a JNI directory
Create a.c filejniExample.c
Concrete implementation:
#include <stdlib.h>
#include <stdio.h>
#include <jni.h>
#include "com_zhyx_jnidemo1_jniclass_JniExample.h"
JNIEXPORT jstring JNICALL Java_com_zhyx_jnidemo1_jniclass_JniExample_getJniStr
(JNIEnv *env, jclass jcs) {
char* tempStr = "I created a string from C.";
return (*env)->NewStringUTF(env,tempStr);
}
Copy the code
The function name and return value can be copied directly from the contents of the generated. H file
The fourth step
(1) to createjni/Android.mk
file
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := jniExample
LOCAL_SRC_FILES := jniExample.c
include $(BUILD_SHARED_LIBRARY)
Copy the code
LOCAL_PATCH
This variable represents the location of the source file in the development tree,my-dir
The path to the current directory (where the Android.mk file itself resides) is returnedCLEAR_VARS
The variable points to a special GNU Makefile that clears many LOCAL_XXX variables for you. Note that the GNU Makefile does not clear LOCAL_PATH. This variable must retain its value because the system parses all build control files in a single GNU Make execution context (where all variables are global). This variable must be declared (redeclared) before each module can be describedLOCAL_MODULE
The variable stores the name of the module you want to build. Use this variable once in each module of your applicationLOCAL_SRC_FILES
The variable must contain a list of C and/or C++ source files to build into the moduleBUILD_SHARED_LIBRARY
The variable points to a GNU Makefile script that collects all the information you have defined in the LOCAL_XXX variable since the most recent include. This script determines what to build and how
This is a basic must, see Android.mk for more
(2) to createjni/Application.mk
file
APP_ABI := all
APP_OPTM := debug
APP_STL := c++_static
Copy the code
APP_ABI
This variable represents the supported Android architecture platformall
Multiple platforms can be separated by Spaces, for example, APP_ABI := armeabi -v7A arm64-v8aAPP_OPTM
Define this optional variable asrelease
或debug
. By default, the publishing binaries are built. Publish mode enables optimization and may generate binaries that cannot be used with the debugger. Debug mode deactivates optimization so that the debugger can be used.APP_STL
The C++ standard library for this application. By default, system STL is used. Other options include c++_shared, c++_static, and none.This may vary slightly depending on your current VERSION of the NDK, please check the usage.
Step 5
Enter the created jni directory and run the command to generate the corresponding. So file
Perform the NDK - buildCopy the code
So libraries that are then generated are automatically prefixed with ‘lib’ to automatically generate libs folders
Step 6
Configure the libs directory in app/build
defaultConfig {
...
ndk {
moduleName "jniExample" // This is the same as LOCAL_MODULE name
}
}
sourceSets {
main {
jniLibs.srcDirs = ["src/main/libs"]}}Copy the code
Step 7
Use the C/C++ native method JniExample class file to load the library
public class JniExample {
static {
// The system loads the lib method
System.loadLibrary("jniExample");
}
//native
public static native String getJniStr(a);
}
Copy the code
Step 8
As with normal method calls, use MainActivity directly:
findViewById<TextView>(R.id.text).text = JniExample.getJniStr()
Copy the code