Xposed框架入门

原理

有一个叫做“Zygote”的进程。这是Android运行时的核心。每个应用程序都作为它的副本(“fork”)启动。/init.rc手机启动时,脚本会启动此过程。进程开始完成/system/bin/app_process,加载所需的类并调用初始化方法。

这就是Xposed发挥作用的地方。安装框架时,会将扩展的app_process可执行文件复制到/system/bin。这个扩展的启动过程在类路径中添加了一个额外的jar,并在某些地方从那里调用方法。例如,在创建VM之后,甚至在main调用Zygote 的方法之前。在该方法中,我们是Zygote的一部分,可以在其背景下行动。

Root手机,并安装XposerInstaller到手机中

下载地址
http://repo.xposed.info/module/de.robv.android.xposed.installer

AndroidManifest引入信息


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="de.robv.android.xposed.mods.tutorial"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <meta-data
            android:name="xposedmodule"
            android:value="true" />
        <meta-data
            android:name="xposeddescription"
            android:value="TestModule" />
        <meta-data
            android:name="xposedminversion"
            android:value="53" />
    application>
manifest>

Gradle引入

      provided 'de.robv.android.xposed:api:82'
      provided 'de.robv.android.xposed:api:82:sources'

新建Hook类

Hook类引用IXposedHookLoadPackage接口
实现handleLoadPackage

package com.treeanimals.max.myapplication;

import android.os.Bundle;
import android.util.Log;
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.callbacks.XC_LoadPackage;

import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;

/**
 * Created by 95112 on 2018/7/2.
 */

public class MainHook implements IXposedHookLoadPackage {

//@Override
//public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
//    XposedBridge.log("Loaded Test app____________________________: " + lpparam.packageName);
//}

    @Override
    public void handleLoadPackage(final XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
        XposedBridge.log("test start");
        XposedBridge.log("----------------------------------"+ loadPackageParam.packageName+"---------------------------");
        if(loadPackageParam.packageName.equals("com.treeanimals.max.myapplication")){
            Log.e("hook","ss");
            XposedBridge.log("hook start");
            findAndHookMethod("com.treeanimals.max.myapplication.MainActivity", loadPackageParam.classLoader, "onCreate", Bundle.class, new XC_MethodHook() {

                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            XposedBridge.log("fixed finished");
                            Class c = loadPackageParam.classLoader.loadClass("com.treeanimals.max.myapplication.MainActivity");
                            Field field = c.getDeclaredField("showIMEI");
                            field.setAccessible(true);
                            TextView textView = (TextView)field.get(param.thisObject);
                            textView.setText("Hello World");
                        }
                    });
        }
    }
}

编写入口

在main目录下新建assets目录,新建xposed_init文件,文件内容为Hook类的完整包名
com.treeanimals.max.myapplication.MainHook

安装apk,绑定模块,重启。

你可能感兴趣的:(android)