Xposed模块1

android studio之Xposed模块开发1

准备

1
2
3
1、掌握基本的android开发知识
2、有xposed框架作为环境(真机或者模拟器,我下面直接真机调试)
3、Xposed框架API下载

创建一个apk程序

MainActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.ctf.test.xposedlab1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button ok = (Button) findViewById(R.id.input_ok);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, toastMessage(), Toast.LENGTH_SHORT).show();
}
});
}
public String toastMessage() {
return "我未被劫持";
}
}

actvity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<Button
android:id="@+id/input_ok"
android:layout_width="102dp"
android:layout_height="45dp"
android:layout_marginBottom="276dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:text="确认"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.458"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints" />

编写hook

创建一个项目,将下载的->
XposedBridgeApi-54.jar
添加到app->libs中,右键->Add As Libary。

在main下面的AndroidMainfest.xml中的application下添加(自己标识为一个Xposed模块)

1
2
3
4
5
6
7
8
9
<meta-data
android:name="xposedmodule"
android:value="true" />
<meta-data
android:name="xposeddescription"
android:value="a test xposed" />
<meta-data
android:name="xposedminversion"
android:value="54" />

在build.gradle的dependencies

1
2
3
4
5
6
7
8
9
的dependencies
{
implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
provided fileTree(include: ['*.jar'], dir: 'libs')
}

创建一个xModule.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import android.util.Log;
import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodReplacement;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
public class xModule implements IXposedHookLoadPackage {
@Override
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
String MethodName = "toastMessage";
Log.d("Xposed","packname---------------------" );
if(loadPackageParam.packageName.equals("com.ctf.test.xposedlab1")) {
Log.d("Xposed","inHook" );
XposedBridge.log("Xposed" + loadPackageParam.packageName);
Class clazz = loadPackageParam.classLoader.loadClass("com.ctf.test.xposedlab1.MainActivity");
XposedHelpers.findAndHookMethod(clazz, MethodName, new XC_MethodReplacement() {
@Override
protected Object replaceHookedMethod(MethodHookParam methodHookParam) throws Throwable {
return "我已经被劫持";
}
});
}
}
}

在main->创建一个assets目录,并在之下创建xposed_init(声明xposed入口)

1
com.ctf.test.xposedtest1.xModule //包名+类名

将这个工程,编译,打包,安装到已经支持Xposed的手机中。
需要注意的是,要关闭instant run功能(File->Settings->instant run->去掉打钩)

报错

1
java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation

dependencies配置,Android studio修改成上面那样

运行

将xopsed打钩,重启xposed框架,在运行xposedLab1,会出现->“我已经被劫”
这是已经hook成功啦。

参考:
https://www.jianshu.com/p/1c8d7682b630

Donate
-------------本文结束感谢您的阅读-------------