之前有简单的接触逆向破解,没有过多的记录。这些东西还是需要记录下来,以便之后翻阅来回想起,所以记录触坑历程。
学习来自 链接
首先是编写so
在java文件里面新建一个java类来加载java native interface
例如
public class myJNI {
static {
System.loadLibrary("JniTest");//加载so的名字
}
public static native String sayHello();//so实现的方法
public static native int add(int a,int b);//so实现的方法
}
- 在Android studio 下面的terminal进入文件的路径,然后编译这个java类
命令1:
cd xxxxxx //xxx是java代码的路径
命令2:
javac myJNI.java// javac是编译,myJNI是你对应java代码的名字
确认之后会生成对应的class文件
- 在回退到java的目录下
1. cd xxxxx//java源代码的路径
2. javah -jni 包名+类名
- 在java目录刷新之后会生成一个.h文件
- 然后在新建一个jni folder
- 然后在生成的文件夹里面新建一个 xx.c
- 还需要新建一个empty.c这个是mk的bug我不太懂
这个是自动生成的 .h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_t_lidashao_viewpager_fragment_myJNI */
#ifndef _Included_com_example_t_lidashao_viewpager_fragment_myJNI
#define _Included_com_example_t_lidashao_viewpager_fragment_myJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_t_lidashao_viewpager_fragment_myJNI
* Method: sayHello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_t_1lidashao_viewpager_1fragment_myJNI_sayHello
(JNIEnv *, jclass);
/*
* Class: com_example_t_lidashao_viewpager_fragment_myJNI
* Method: add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_example_t_1lidashao_viewpager_1fragment_myJNI_add
(JNIEnv *, jclass, jint, jint);
#ifdef __cplusplus
}
#endif
#endif
实现声明的函数,在xx.c里面复制之后,进行实现功能
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_t_lidashao_viewpager_fragment_myJNI */
#ifndef _Included_com_example_t_lidashao_viewpager_fragment_myJNI
#define _Included_com_example_t_lidashao_viewpager_fragment_myJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_t_lidashao_viewpager_fragment_myJNI
* Method: sayHello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_t_1lidashao_viewpager_1fragment_myJNI_sayHello
(JNIEnv *env, jclass jobj)//修改了这里
{
return (*env)->NewStringUTF(env,"hello yesubtter");//返回一个字符串
}
/*
* Class: com_example_t_lidashao_viewpager_fragment_myJNI
* Method: add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_example_t_1lidashao_viewpager_1fragment_myJNI_add
(JNIEnv *env, jclass jobj, jint a, jint b) //修改了这里
{
return a+b;//返回俩个数字相加
}
#ifdef __cplusplus
}
#endif
#endif
- 安装了native development kit之后就可以生成了,需要在app build.gradle里面增加一些内容
- 在defaultconfig里面增加
ndk {
moduleName "JniTest"
ldLibs "log", "z", "m"
abiFilters "armeabi", "armeabi-v7a", "x86"
}
- 在Android 里面增加
sourceSets.main {
jni.srcDirs = []
}
- 在gradle.properties中添加
android.useDeprecatedNdk=true
如果build出现 bug
Error: Flag android.useDeprecatedNdk is no longer supported
and will be removed in the next version of Android Studio.
Please switch to a supported build system.
xxxxxxxxxxxxxxxxxxxxxxxx
android.deprecatedNdkCompileLease=1515138691572
in gradle.properties
只需要把 android.useDeprecatedNdk=true替换成你给的
android.deprecatedNdkCompileLease=1515138691572 即可
在project环境main里面 new一个directory 命名为jniLIB
在build->rebuild project整理生成,没有bug之后会生成一个文件夹
把生成的armeabi,armeabi-v7a,x86都复制在这个文件下面,然后编译运行,调用之后就会返回对应的函数功能
通过打印og的方式可以查看对应的字符串和函数功能,如果正确,说明已成功