Hook 获取序列号函数 例子
- 建立一个空工程,编写获取序列号的简单例子
- 获取手机状态需要设置权限
<uses-permission
android:name="android.permission.READ_PHONE_STATE"></uses-permission> - 编写布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bluesson.test.MainActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
- 编写主 Activity 类中的 onCreate 函数
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv1 = (TextView) findViewById(R.id.tv1);
TextView tv2 = (TextView) findViewById(R.id.tv2);
TelephonyManager tm = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
tv1.setText("imei: "+tm.getDeviceId());
tv2.setText("imsi: "+tm.getSubscriberId());
}
- 完成安装
- 建立一个无 activity 的工程,编写 Xposed 插件
- 设置清单文件
在 application 标签中增加模块说明信息
<application
android:label="@string/app_name">
<!-- 使 xposed 模块有效 -->
<meta-data android:name="xposedmodule" android:value="true"/>
<!-- xposed 模块名称 -->
<meta-data android:name="xposeddescription" android:value="Xposed
模块示例"/>
<!-- xposed 模块最低版本 -->
<meta-data android:name="xposedminversion" android:value="54"/>
</application>
- 导入 xposed 库文件
将 xposed 库文件 XposedBridgeApi-XX.jar,放入 app/lib 文件夹下
需要设置包的依赖,在工程文件夹右键,打开模块设置
选择对应的模块,app,右边选择依赖项,增加依赖
选择文件,导入对应包
需要注意的是:修改 Scope 为 Provided
- 创建一个类,Main,实现 xposed 中的接口 IXposedHookLoadPackage 并重写方法
handleLoadPackage
public class Main implements IXposedHookLoadPackage {
@Override
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam
lpparam) throws Throwable {
// 不是需要 Hook 的包直接返回
if (!lpparam.packageName.equals("com.bluesson.test"))
return;
XposedBridge.log("Loaded app: " + lpparam.packageName);
}
}
- 声明主入口类路径
需要在 main 文件夹下建立 assets 文件夹中新建一个 xposed_init 的文件,并在其中声明
主入口类
我们的主类:com.hello.xposdplugin.Main
- 以上做完可以做测试了
在模拟器或者真机中安装 xposedinstall
最新的稳定版:de.robv.android.xposed.installer_v32_de4f0d.apk
安装之后,在 xposed 的模块中可以找到我们安装的 apk(其实就是一个 xposed 插件)
选中重启之后,再安装测试的 apk 进行测试
- Hook 类方法 需要在刚才重写的 handleLoadPackage 方法中添加代码
// 找到对应的方法,进行替换
// 参数 1:类名
// 参数 2: 方法名
// 参数 3:实现监听,重写方法
// replaceHookedMethod 替换方法
// beforeHookedMethod 方法前执行
// afterHookedMethod 方法后执行
XposedHelpers.findAndHookMethod(TelephonyManager.class,
"getDeviceId", new XC_MethodReplacement() {
@Override
protected Object replaceHookedMethod(MethodHookParam param)
throws Throwable {
return "this is imei";
}
});
XposedHelpers.findAndHookMethod(TelephonyManager.class,
"getSubscriberId", new XC_MethodReplacement() {
@Override
protected Object replaceHookedMethod(MethodHookParam param)
throws Throwable {
return "this is imsi";
}
});