jeudi 17 août 2017

Method annotated with @hide returns null when invoked via reflection

I want to use a method of the class annotated by @hide, so I am using reflection. Although all the parameters have been used, null is returned.

This is my code:

Class<?> packageParserClass = Class.forName("android.content.pm.PackageParser");

Class<?> packageParser$PackageClass = Class.forName("android.content.pm.PackageParser$Pack;

Class<?> packageUserStateClass = Class.forName("android.content.pm.PackageUserState");

//Android API19
Method generateApplicationInfoMethod = packageParserClass.getDeclaredMethod("generateApplicationInfo",`enter code here`packageParser$PackageClass, int.class, packageUserStateClass);

//Create packageParser object 
Constructor<?> constructor = packageParserClass.getConstructor(String.class);
Object packageParser = constructor.newInstance(apkFile.getPath());

// Call PackageParser.parsePackage to parse the apk's info
Method parsePackageMethod = packageParserClass.getDeclaredMethod("parsePackage", apkFile.getClass(), String.class,DisplayMetrics.class,int.class);
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
parsePackageMethod.setAccessible(true);
path = apkFile.getPath();

// The packageObj is null
Object packageObj = parsePackageMethod.invoke(packageParser, apkFile, path, metrics, 0);

The packageObj is null, why? I don't understand what is happening. How can I fix this?





Aucun commentaire:

Enregistrer un commentaire