简体   繁体   中英

Xposed can't hook getInstalledApplications

I wanted to hook getInstalledApplications and getInstalledPackages to monitor if the app use this method to get my appliactionlist.
My code:

XposedHelpers.findAndHookMethod(

                    android.content.pm.PackageManager.class.getName(),
                    lpparam.classLoader,
                    "getInstalledApplications",
                    new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            XposedBridge.log("getInstalledApplications()");
                            Log.d("HookLogin", "getInstalledApplications()");
                        }
                    });

And I got this error: Xposed: java.lang.NoSuchMethodError: android.content.pm.PackageManager#getInstalledApplications()#exact Then I googled and found I should use android.app.ApplicationPackageManager ,and I changed my code:

     XposedHelpers.findAndHookMethod(

                    "android.app.ApplicationPackageManager",
                    lpparam.classLoader,
                    "getInstalledPackages",
                    new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            XposedBridge.log("getInstalledPackages()");
                            Log.d("HookLogin", "getInstalledPackages()");
                        }
                    });

But i still got NoSuchMethodError,this time it is:

Xposed: java.lang.NoSuchMethodError: android.app.ApplicationPackageManager#getInstalledApplications()#exact

I'm confused now, how should I hook the method getInstalledPackages .

Both code versions have one major problem:

Your hook can not work because you are trying to hook a method that does not exist: You are trying to hook the method getInstalledApplications() but that method does not exist, only the method [getInstalledApplications(int)][1] does exist and can be hooked.

You alerady notived that android.content.pm.PackageManager is an abstract class and the method getInstalledApplications(int) is abstract, too. As Xposed can not hook abstract Methods and Interfaces you have to specify the class that that actually implements the method getInstalledApplications(int) .

The actual implementation of PackageManager is located in class android.app.ApplicationPackageManager as you already found out.

Therefore the correct hooking code is:

XposedHelpers.findAndHookMethod(
    "android.app.ApplicationPackageManager",
    lpparam.classLoader,
    "getInstalledPackages", 
    int.class, // this line has been added as the getInstalledPackages method has one int parameter
    new XC_MethodHook() {
    @Override
    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
        XposedBridge.log("getInstalledPackages()");
            Log.d("HookLogin", "getInstalledPackages()");
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM