package cn.edu.buaa.act.sdp.malwaredetector.entity; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.util.DisplayMetrics; import java.io.File; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * Created by yaodh on 2014/11/12. */ public class PackageInfoAsAPK { Context context; String apkPath; String appName; Drawable icon; String appVersion; long size; public PackageInfoAsAPK(Context context, String apkPath) { this.apkPath = apkPath; this.context = context; parseAPKInfo(); } public String getApkPath() { return apkPath; } public String getAppName() { return appName; } public Drawable getIcon() { return icon; } public String getAppVersion() { return appVersion; } public long getSize() { return size; } private void parseAPKInfo() { // the size of apk File file = new File(apkPath); size = file.length(); try { Class pkgParserClass = Class.forName("android.content.pm.PackageParser"); Class[] typeArgs = new Class[]{String.class}; Constructor pkgParserCst = pkgParserClass.getConstructor(typeArgs); Object[] valueArgs = new Object[]{apkPath}; Object pkgParser = pkgParserCst.newInstance(valueArgs); DisplayMetrics metrics = new DisplayMetrics(); metrics.setToDefaults(); typeArgs = new Class[]{File.class, String.class, DisplayMetrics.class, Integer.TYPE}; Method method = pkgParserClass.getDeclaredMethod("parsePackage", typeArgs); valueArgs = new Object[]{new File(apkPath), apkPath, metrics, 0}; Object pkgParsePkg = method.invoke(pkgParser, valueArgs); Field appInfoField = pkgParsePkg.getClass().getDeclaredField("applicationInfo"); ApplicationInfo info = (ApplicationInfo) appInfoField.get(pkgParsePkg); Class assetMagClass = Class.forName("android.content.res.AssetManager"); Constructor assetMagCst = assetMagClass.getConstructor(); Object assetMag = assetMagClass.newInstance(); typeArgs = new Class[]{String.class}; Method asset_addAssetPathMethod = assetMagClass.getDeclaredMethod("addAssetPath", typeArgs); valueArgs = new Object[]{apkPath}; asset_addAssetPathMethod.invoke(assetMag, valueArgs); Resources resources = context.getResources(); typeArgs = new Class[]{assetMag.getClass(), resources.getDisplayMetrics().getClass(), resources.getConfiguration().getClass()}; Constructor resCst = Resources.class.getConstructor(typeArgs); valueArgs = new Object[]{assetMag, resources.getDisplayMetrics(), resources.getConfiguration()}; resources = (Resources) resCst.newInstance(valueArgs); if (info.labelRes != 0) { appName = resources.getText(info.labelRes).toString(); } if (info.icon != 0) { icon = resources.getDrawable(info.icon); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } } }