安装xposed
我这里用的逍遥模拟器,安装XposedInstaller_3.1.5.apk,再点一下安装Version 89,重启模拟器;这样就安装完成了。
开发xposed模块
- Androidstudio新建一个工程
- 修改AndroidManifest.xml中添加声明
添加3个meta-data
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xposed">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
//这里添加3个meta-data
<meta-data
android:name="xposedmodule"
android:value="true"/>
<meta-data
android:name="xposeddescription"
android:value="Xposed Demo"/>
<meta-data
android:name="xposedminversion"
android:value="83"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
-
添加依赖
在build.gradle文件中的dependencies添加两行compileOnly ‘de.robv.android.xposed:api:82’
//非必要
compileOnly ‘de.robv.android.xposed:api:82:sources’
dependencies {
compileOnly 'de.robv.android.xposed:api:82'
//非必要
compileOnly 'de.robv.android.xposed:api:82:sources'
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
-
创建xposed_init
新建一个Assets目录
然后在Assets目录下创建xposed_init文件,New->File文件名xposed_init;在xposed_init里面写入模块的入口;
XposedBridge 从assets 目录中的xposed_init 文件中获取入口点。xposed_init文件中每行配置一个;
例如:我这里新建一个类名为xphook的类作为模块入口,
那xposed_init里面就写上xphook的路径:com.example.xposed.xphook -
编写hook模块代码
入口类xphook实现接口IXposedHookLoadPackage重写handleLoadPackage方法;
然后在handleLoadPackage方法添加自己的逻辑代码;
例如:我这里hook 系统的Toast事件,打印调用堆栈
public class xphook implements IXposedHookLoadPackage {
@Override
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
//hook Toast
Class<?> clazz = lpparam.classLoader.loadClass("android.widget.Toast");
if (clazz!=null){
XposedHelpers.findAndHookMethod(clazz, "show", new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
try{
throw new NullPointerException();
}catch (Exception e){
Log.i("vicHook","Toast show trace "+Log.getStackTraceString(e));
}
}
});
}
}
}
运行xposed模块
在XposedInstaller的模块里面勾选上我们写的应用。然后重启模拟器;
新建一个测试应用打印一个toast消息,用来测试我们的hook模块
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast toast=Toast.makeText(getApplicationContext(), "这是一个测试的Toast", Toast.LENGTH_SHORT);
toast.show();
}
运行查看log:
调用Toast.show的调用链就打印出来了