Android reverse 4. JNI static registration of Android Studio

1. Create a new Android project with the name: JniStaticTest

 

2. Create a native method named TestGetInt in MainActivity

 

3. Go to the JniStaticTest \ app \ src \ main \ java directory, we can see there is a com folder. This step needs attention, the path can not be wrong!

 

4. Open the CMD window, and switch to the path shown in step 3, enter: javah -jni com.crackme.jnistatictest.MainActivity

 

5. After success, return to the Android Studio project panel, you can see that a com_crackme_jnistatictest_MainActivity.h file is generated

 

6. Create a folder named jni in the project (as shown in the figure below, the folder path and name must be correct), and move the .h header file generated in step 5 to the jni folder

 

7. Create a new TestJniStatic.cpp file in the Jni folder and write the following code (note that the declaration of the exported function must be consistent with the .h header file):
#include "com_crackme_jnistatictest_MainActivity.h"

JNIEXPORT jint JNICALL Java_com_crackme_jnistatictest_MainActivity_TestGetInt(JNIEnv *, jobject)
{
   return 123456;
}

 

 

8. Add the Android.mk file under the Jni folder and insert the following code:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := TestJniStatic
LOCAL_SRC_FILES =: TestJniStatic.cpp
include $(BUILD_SHARED_LIBRARY)

 

9. Add the Application.mk file under the Jni folder and insert the following code:
APP_ABI := x86 armeabi-v7a

 

10. Switch to the Jni directory in the cmd window and execute the ndk-build command

 

11. After the .ndk-build command is successfully executed, the so library file can be seen in the libs directory of the project

 

12. In the build.gradle file under the App folder, find the defaultConfig item, and add the following code to its item (must, otherwise it will cause the LoadLibrary to fail to load):
ndk
{
      moduleName "TestJniStatic"
}
sourceSets.main
{
      jni.srcDirs = []
      jniLibs.srcDir "src/main/libs"
}

 

13. Go back to MainActivity and add the following two broken codes (Note: The module name in LoadLibrary must go to the beginning or end, otherwise it will not load successfully!):

 

 

14. Running test results

 

Guess you like

Origin www.cnblogs.com/fuhua/p/12695436.html