The static registration of NDK calls layer C and returns a string
1. Preface
Hello, everyone. I'm Kenon. It's like this. I'm testing Android Java layer and calling so through NDK recently © Some inexplicable problems occurred in the middle of the layer, so as to record it.
2. Create Android project
This project is created through Android studio.
The process of creating a project is not introduced too much, which is not the focus of this article.
Ensure that it can be compiled and run.
Just one project.
3. Modify a method with native
code
//Get the string returned by layer C public native CharSequence GetSoBackString();
As shown in the figure
This will prompt errors, but don't worry. There will be no errors when compiling.
Introduction to JNI
1.definition: JAVA Native Interface JNI It's an agreement 2.effect: adopt JNI Can make java and c++Mutual call
It's useless to copy. Anyway, it realizes the communication between Java and C through JNI.
4. Static registration
4.1 cd to < project > \ app \ SRC \ main \ Java
The arrow seems to be reversed, but it doesn't matter. Anyway, it is cd to the java directory of the project.
4.2 execute command
command
javah -jni -encoding UTF-8 com.example.ti.testndk.MainActivity
As shown in the figure
Just like me.
The final parameters of javah are package name and class name.
4.3 create jni folder under < project > / APP
4.4 copy the generated by the above command h file
It's wrong to move here. Change it to copy.
4.5 create another one under jni folder c document, which reads as follows:
JNIstudy.c
#include <JNIstudy.h> JNIEXPORT jobject JNICALL Java_com_example_ti_testndk_MainActivity_GetSoBackString (JNIEnv *env, jobject obj){ //Type conversion NewStringUTF(env, string) jstring str = (*env)->NewStringUTF(env, "hello test NDK"); return str; }
JNIstudy.h
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_example_ti_testndk_MainActivity */ #ifndef _Included_com_example_ti_testndk_MainActivity #define _Included_com_example_ti_testndk_MainActivity #ifdef __cplusplus extern "C" { #endif /* * Class: com_example_ti_testndk_MainActivity * Method: GetSoBackString * Signature: ()Ljava/lang/CharSequence; */ JNIEXPORT jobject JNICALL Java_com_example_ti_testndk_MainActivity_GetSoBackString (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
As shown in the figure
4.6 create Android in jni file MK and application MK file
Android.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := JNIstudy #Module name LOCAL_SRC_FILES := JNIstudy.c #Source file c or cpp LOCAL_ARM_MODE := arm #Compiled instruction set ARM instruction LOCAL_LDLIBS := -llog #Dependency Library include $(BUILD_SHARED_LIBRARY) #Specifies the type of compiled file
Application.mk
APP_ABI := armeabi-v7a
As shown in the figure
4.7 ndk compilation
cd to jni folder first.
After the compilation is successful, the compiled things will be in libs.
5. Configure libs
Build. Under app Add the following code under android of gradle.
sourceSets { main { jniLibs.srcDirs = ['libs'] } }
6. Load libs resources in MainActivity
code
static { System.loadLibrary("JNIstudy"); }
As shown in the figure
Running into simulator
At this point, it should be basically OK. Just run it.
No, Toast gets the string returned by layer C.
summary
The focus of this article is not to let you learn more nb things, but to get started with NDK static registration.
After all, it's not that the code is too difficult to be discouraged, but an inexplicable problem.
The harder you work, the luckier you are.
If there is any problem during the operation, remember to leave a message below, and we will solve the problem at the first time.
I'm Manon. If you think it's good, remember to praise it.
Thank you for watching.