• Introducing XDA Computing: Discussion zones for Hardware, Software, and more!    Check it out!

Get App version before hooking.

Search This thread

slvrbllt

Senior Member
Feb 18, 2005
222
46
Rome (Caput Mundi)
Hi all,

My Xposed module deals with different versions of an app. Different versions means different hooks, so my question is: how can I get the application version during the handleLoadPackage execution? I mean... getPackageManager requires a context which I do not have *or* I don't know how to find out...

Any clues, guys?
Thanks.
 

GermainZ

Inactive Recognized Developer / Retired Forum Mod
Aug 3, 2012
6,174
8,795
From memory, I believe you can use the appInfo field in LoadPackageParam: lpparam.appInfo.
Otherwise you can get a context with AndroidAppHelper.currentApplication()
 

GermainZ

Inactive Recognized Developer / Retired Forum Mod
Aug 3, 2012
6,174
8,795
No luck!
AndroidAppHelper.currentApplication() returns null and lpparam.appInfo does not appear to be a Context object...
Any other idea?
lpparam.appInfo = ApplicationInfo, not Context. It might have the version, but I can't recall if it does or check it right now.
 

rovo89

Senior Recognized Developer
Jan 4, 2012
2,585
81,339
I assume you're placing your hooks in handleLoadPackage(). The Context/Application isn't created yet at that time. I think some modules managed to get a system context via reflection, but I don't know which ones. Maybe you can find something via the search function.

Just another idea: Instead of comparing versions, you might be able to achieve the same by checking whether certain classes/methods exist.
 

slvrbllt

Senior Member
Feb 18, 2005
222
46
Rome (Caput Mundi)
I assume you're placing your hooks in handleLoadPackage(). The Context/Application isn't created yet at that time. I think some modules managed to get a system context via reflection, but I don't know which ones. Maybe you can find something via the search function.

Just another idea: Instead of comparing versions, you might be able to achieve the same by checking whether certain classes/methods exist.

Thanks for your reply, mate.
You are correct! handleLoadPackage is where I placed my hooks.

As per your suggestion, I will try to find any other existing module that does what I'm looking for.

Unfortunately I need to identify the package version in any reliable way but classes/methods existence check.
The app is obfuscated... method a.a.b could be a.b.c in another version while method a.a.b could still exist but with different implementation.

Is there any other way to retrieve the installed version of a package, given it's package name?
Thanks.
 

rovo89

Senior Recognized Developer
Jan 4, 2012
2,585
81,339
Is there any other way to retrieve the installed version of a package, given it's package name?

Apart from using the system context to access the package manager, you could try to parse the package yourself. There is a PackageParser class which is hidden from the SDK, but if you manage to call parsePackage() via reflection (or by referencing a JAR which includes the hidden APIs as well, like XposedBridge does), then you can use it with ApplicationInfo.sourceDir and get the version numbers from the result. Not sure how expensive that operation is, but if you execute it just once, it should be ok.
 

Massi-X

Senior Member
Nov 28, 2013
230
329
For anyone wondering the snippet we used up until now is "deprecated" in Android 11 and totally gone in 12 (S). You can use the code below to get the version code for your app:

Java:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    Class<?> pkgParserClass = XposedHelpers.findClass("android.content.pm.PackageParser", lpparam.classLoader);
    Object packageLite = XposedHelpers.callStaticMethod(pkgParserClass, "parsePackageLite", apkPath, 0);
    versionCode = XposedHelpers.getIntField(packageLite, "versionCode");
} else {
    Class<?> parserCls = XposedHelpers.findClass("android.content.pm.PackageParser", lpparam.classLoader);
    Object pkg = XposedHelpers.callMethod(parserCls.newInstance(), "parsePackage", apkPath, 0);
    versionCode = XposedHelpers.getIntField(pkg, "mVersionCode");
}