github地址:https://github.com/rovo89/XposedInstaller
1:新建项目
新建工程XposeTest .修改manifest.xml
``
<meta-data android:name="xposedmodule" android:value="true"></meta-data> <meta-data android:name="xposeddescription" android:value="我是个test111"></meta-data> <meta-data android:name="xposedminversion" android:value="30"></meta-data>
2:添加依赖
``
compileOnly 'de.robv.android.xposed:api:82' compileOnly 'de.robv.android.xposed:api:82:sources'
注意:这里不要使用implementation。
3:添加测试代码
``
public class MainActivity extends AppCompatActivity { private TextView tvDesc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvDesc=findViewById(R.id.tv_desc); tvDesc.setText("我是test"); } }
4:编写hook代码
新建HookModule类,继承IXposedHookLoadPackage,实现 handleLoadPackage方法。
测试实现效果:找到对应的按钮,替换文案。
``
public class HookModule implements IXposedHookLoadPackage { public static final String TAG = "HookModule"; @Override public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable { Log.e(TAG, "handleLoadPackage"); XposedHelpers.findAndHookMethod("com.zh.xpose.MainActivity", lpparam.classLoader, "onCreate", Bundle.class, new XC_MethodHook() { @Override protected void beforeHookedMethod(MethodHookParam param) throws Throwable { Log.e(TAG, "handleLoadPackage beforeHookedMethod "); } @Override protected void afterHookedMethod(MethodHookParam param) throws Throwable { Log.e(TAG, "handleLoadPackage afterHookedMethod "); Class<?> aClass = lpparam.classLoader.loadClass("com.zh.xpose.MainActivity"); Field field = aClass.getDeclaredField("tvDesc"); field.setAccessible(true); TextView tvDesc = (TextView) field.get(param.thisObject); tvDesc.setText("我是hook后的test"); } }); } }
5:运行
安装成功后会提示Xpose未激活,点击激活Xpose,选择某个应用,强制停止后重启该应用,会发现demo中的文案已经被替换了。