Use NDK to generate so for third-party use

 Refer to https://blog.csdn.net/zi413293813/article/details/50074239 and then rearrange and supplement

I use ndk-r10d

ndk download address http://developer.android.com/sdk/ndk/index.html

eclipse development environment

1. Create an android project with eclipse

2. Right-click on the selected project->Properties->Builders->New->Select Program and click OK->Various configuration texts are too difficult to describe directly above

The path configured with ndk-build and the android project just created

tick

Specify Resources Browse to find the android project you just created, and that's all for the project configuration

First create a class file in the src directory with the following content

public class JniClient {
    static public native String AddStr(String strA, String strB);
    static public native int AddInt(int a, int b);
}

Find the location of JniClient.java (the class you just created), right-click to open cmd or powershell and execute the command javac JniClient.java

Enter the src disk directory and right-click to open cmd or powershell and execute the command javah com.example.mtest.JniClient (this needs to be determined according to the actual path of your own project)

In this way, the content of the com_example_mtest_JniClient.h header file is generated, and the content of the header file is not displayed. The first connection address contains

Then manually create a com_example_mtest_JniClient.c There is a connection address in the front, the editor can't post it if there is a problem

Cut this header file and source file to the jni directory (create it if it is not in the project root directory)

Add an Android.m to the jni directory

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE :=_SRC_FILES := com_example_mtest_JniClient.c
include $(BUILD_SHARED_LIBRARY)

and Application.mk

#Use STL library
APP_STL := gnustl_static
#Compatible with old syntax, make some errors down to warnings
APP_CPPFLAGS += -fpermissive
#Select the platform, if you need to generate a space separated by a space
APP_ABI := armeabi armeabi-v7a 

Refreshing the project will generate the corresponding so file in the lib directory

Let's write how to use the so library just generated

Re-create a new android project. I am using a game project of cocos2d-x here. 

Put the generated so in the jni directory of the new android project

 

Add the following to this new project Android.mk

 include $(CLEAR_VARS)
LOCAL_MODULE := libTestNdk
LOCAL_SRC_FILES := TestNdk/$(TARGET_ARCH_ABI)/libTestNdk.so
include $(PREBUILT_SHARED_LIBRARY)

 

LOCAL_STATIC_LIBRARIES += libTestNdk

 

It can't be used directly, go back to the first created android project and type the JniClient.java file into a jar package

I executed jar -cvf mtest.jar com\example\mtest\JniClient.class in powershell in the src directory

Generate the mtest.jar package, but it seems to be automatically generated in the bin directory

Put this jar package into the second project lib, and add the following code in appactivity

Then call it in the onCreate() method

Run the project on the phone and you can see the following output

.748: I/System.out(5703): ...abelmou...HelloWorld from JNI !

Indicates that the call was successful, and the writing is a bit messy to facilitate review by yourself

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325297136&siteId=291194637