Easy to build Xposed Hook

0x1. Open AS to create an empty project without interface, and then add the following code to the manifest file:

<application
    android:allowBackup="true" android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name" android:supportsRtl="true"
    android:theme="@style/AppTheme"
    >
    <!--  send xposed Module effective  -->
    <meta-data android:name="xposedmodule" android:value="true"/>
    <!-- xposed Module name  -->
    <meta-data android:name="xposeddescription" android:value="Xposed Module example"/>
    <!-- xposed Module minimum version  -->
    <meta-data android:name="xposedminversion" android:value="54"/>
</application>

0x2. Import xposed library file XposedBridgeApi-XX.jar, put the library file in the app/lib directory, create a lib directory by yourself, do not put it in the libs directory, otherwise there will be an error, import it into the library, and modify Scope to Provided

0x3. Create a class Main, and implement the interface IXposedHookLoadPackage in xposed in Main and override the method handleLoadPackage

public class Main implements IXposedHookLoadPackage {
    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable{
        //  The package that doesn't need Hook returns directly
        if (!loadPackageParam.packageName.equals("com.example.y0n.hookdemo"))
            return;
        XposedBridge.log("Loaded app: " + loadPackageParam.packageName);
    }
}

0x4. To declare the main entry class path, create a new xposed init file in the assets folder under the main folder, and declare the main entry class, com.example.y0n.hookdemo.MainActivity, as shown in the following figure:

0x5. Write the function you want to hook in the overloaded function, etc

//  Find the corresponding method and replace it
//  Parameter 1: class name
//  Parameter 2: method name
//  Parameter 3: implement monitoring and rewrite methods
// replaceHookedMethod replace method
// beforeHookedMethod method
// After hookedmethod method
XposedHelpers.findAndHookMethod(TelephonyManager.class,
        "getDeviceId", new XC_MethodReplacement() {
            @Override
            protected Object replaceHookedMethod(MethodHookParam param)
                    throws Throwable {
                return "this is y0n";
            }
        });
XposedHelpers.findAndHookMethod(TelephonyManager.class,
        "getSubscriberId", new XC_MethodReplacement() {
            @Override
            protected Object replaceHookedMethod(MethodHookParam param)
                    throws Throwable {
                return "this is y0n";
            }
        });

0x6. If the hook is a non system class, but a third-party class, and contains parameters, the first parameter is the class name string to be hook, and the classloader of the second parameter needs to be modified to be the classloader of the current instance, the third parameter is the method name of hook, and the fourth parameter is the parameter of hook function, specifically written according to the actual hook function, and the fifth parameter is hook's Rewrite, the reference code is as follows:

//  Find the corresponding method and replace it
//  Parameter 1: class name
//  Parameter 2: method name
//  Parameter 3: implement monitoring and rewrite methods
// replaceHookedMethod replace method
// beforeHookedMethod method
// After hookedmethod method
XposedHelpers.findAndHookMethod("org.sugram.base.MainActivity",
        loadPackageParam.classLoader,
        "onCreate",
        Bundle.class,
        new XC_MethodReplacement() {
            @Override
            protected Object replaceHookedMethod(MethodHookParam param)
                    throws Throwable {
                XposedBridge.log("y0n log : onCreate() is hooked!");
                return 0;
            }
        });

0x7. Compile it into apk, install it, restart the activation plug-in and restart the mobile phone, the plug-in will take effect. For the specific plug-in installation and tool download, please refer to:

https://www.xda-developers.com/xposed-framework-for-android-oreo-beta/

0x8. Operation effect

 

 

 

Keywords: Android Mobile

Added by kingconnections on Wed, 08 Jan 2020 18:31:18 +0200