如何在Xposed中获取钩子包版本?

时间:2017-03-12 19:42:37

标签: android xposed

如何在应用初始化之前获取versionName函数中的versionCodeIXposedHookLoadPackage.handleLoadPackage包?我尝试过使用AndroidAppHelper.currentApplication(),但会返回null

1 个答案:

答案 0 :(得分:1)

这是我使用的代码(来自this post的想法)。它返回格式versionName (versionCode)

的字符串
private static String getPackageVersion(XC_LoadPackage.LoadPackageParam lpparam) {
    try {
        Class<?> parserCls = XposedHelpers.findClass("android.content.pm.PackageParser", lpparam.classLoader);
        Object parser = parserCls.newInstance();
        File apkPath = new File(lpparam.appInfo.sourceDir);
        Object pkg = XposedHelpers.callMethod(parser, "parsePackage", apkPath, 0);
        String versionName = (String)XposedHelpers.getObjectField(pkg, "mVersionName");
        int versionCode = XposedHelpers.getIntField(pkg, "mVersionCode");
        return String.format("%s (%d)", versionName, versionCode);
    } catch (Throwable e) {
        return "(unknown)";
    }
}