编写第一个Xposed模块
1.创建项目
在创建项目时,选择一个有MainActivity的项目,要是注册空项目还要自己去注册入口activity
2.添加Xposed提供的jar到lib目录
3.修改build.gradle
只编译这两个
compileOnly 'de.robv.android.xposed:api:82'
compileOnly 'de.robv.android.xposed:api:82:sources'
不能参与打包APK,否则安到手机上XP框架会报错
(Gradle的语法要学习一下)
而且,//implementation fileTree(dir: ‘libs’, include: [‘*.jar’])
最好注释掉,详见https://blog.csdn.net/OneT1me/article/details/93968206
编译打包成APK在测试机上运行成功!
4.定义MainActivity
代码:
MainActivity.java
package com.javahk.hookdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=findViewById(R.id.tv);
tv.setText("hello!");
}
}
5.定义XposedInit(钩子方法)
XposedInit.java
这里有一个小问题,在setText()这里写入英文字符会报出个警告,还没有解决。
package com.javahk.hookdemo;
import android.os.Bundle;
import android.widget.TextView;
import java.lang.reflect.Field;
import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
public class XposedInit implements IXposedHookLoadPackage{
@Override
public void handleLoadPackage(final XC_LoadPackage.LoadPackageParam lpparam) throws Throwable{
if(lpparam.packageName.equals("com.javahk.hookdemo")){
XposedHelpers.findAndHookMethod("com.javahk.hookdemo.MainActivity",
lpparam.classLoader, "onCreate", Bundle.class, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
super.beforeHookedMethod(param);
}
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Class c=lpparam.classLoader.loadClass("com.javahk.hookdemo.MainActivity");
Field field=c.getDeclaredField("tv");
field.setAccessible(true);
XposedBridge.log("Test");
TextView tv=(TextView)field.get(param.thisObject);
tv.setText("!!!!!");
}
});
}
}
}
6.AndroidManifest.xml
这个文件主要要配置几个<meta>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javahk.hookdemo">
<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">
<meta-data
android:name="xposedmodule"
android:value="true" />
<meta-data
android:name="xposeddescription"
android:value="Hook log test" />
<meta-data
android:name="xposedminversion"
android:value="82" />
<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>
activity_main.xml
布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>