This article has participated in the call for good writing activities, click to view: back end, big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!
One: Environment configuration
Android development environment and Ndk version
Two: Create a project
1. Create an Android project and create a local reference utility class
public class JniUtils {
static {
System.loadLibrary("jnidemo-jni");
}
public static native String getStringFromNative();
public static native int getintFromNative();
}
Copy the code
Then click :– Build –make Project to compile
2: Get the header (.h) file of JniUtils
I'm going to get the.h File here and I'm going to do that using tool, so let's configure tool and I'm going to click File. Name: Javah (optional) Program: Javah (a Java command) Arguments :-v -jni -d $ModuleFileDir$/ SRC /main/jni $FileClass$ Working Directory :$SourcepathEntry :$SourcepathEntry :$SourcepathEntry :$SourcepathEntry :$SourcepathEntry :$SourcepathEntry :$SourcepathEntryCopy the code
3. Create a C file
Create a jni file in the main directory. 2. Copy the created. 4. Create a c + + / c create mk file (grammar explanation: https://blog.csdn.net/u014644594/article/details/90695201) LOCAL_PATH: = $(call my - dir) include $(CLEAR_VARS) LOCAL_MODULE := jnidemo-jni LOCAL_SRC_FILES := jnidemo.c include $(BUILD_SHARED_LIBRARY) *5. This file is created for the purpose of generating.so files. APP_ABI: = armeabi-v7a APP_ABI: CPU platform name APP_PLATFORM := Android-10 On Android Libs/xx(CPU platform name)/xx.so 6. Build add buildTypes {release {minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } externalNativeBuild { ndkBuild { path 'src/main/jni/Android.mk' } } }Copy the code
At this point, the basic configuration of JNI is over, and you can call the defined native methods. Everything is difficult at the beginning, and the first step towards jNI advanced level has been taken.Copy the code
My Github open source project