Java tutorial
//package com.java2s; //License from project: Apache License import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import java.io.File; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import android.util.DisplayMetrics; public class Main { /** * Because of a BUG of Android (API 13-), * get signature info by using "getPackageArchiveInfo" of "PackageManager" * always causes "NullPointerException". * Lack of code in method "getPackageArchiveInfo": * if ((flags & GET_SIGNATURES) != 0) * { * packageParser.collectCertificates(pkg, 0); * } */ @SuppressWarnings("unchecked") public static PackageInfo getPackageArchiveInfo(String archiveFilePath, int flags) { try { Class packageParserClass = Class.forName("android.content.pm.PackageParser"); Class[] innerClasses = packageParserClass.getDeclaredClasses(); Class packageParserPackageClass = null; for (Class innerClass : innerClasses) { if (0 == innerClass.getName().compareTo("android.content.pm.PackageParser$Package")) { packageParserPackageClass = innerClass; break; } } Constructor packageParserConstructor = packageParserClass.getConstructor(String.class); Method parsePackageMethod = packageParserClass.getDeclaredMethod("parsePackage", File.class, String.class, DisplayMetrics.class, int.class); Method collectCertificatesMethod = packageParserClass.getDeclaredMethod("collectCertificates", packageParserPackageClass, int.class); Method generatePackageInfoMethod = packageParserClass.getDeclaredMethod("generatePackageInfo", packageParserPackageClass, int[].class, int.class, long.class, long.class); packageParserConstructor.setAccessible(true); parsePackageMethod.setAccessible(true); collectCertificatesMethod.setAccessible(true); generatePackageInfoMethod.setAccessible(true); Object packageParser = packageParserConstructor.newInstance(archiveFilePath); DisplayMetrics displayMetrics = new DisplayMetrics(); displayMetrics.setToDefaults(); final File sourceFile = new File(archiveFilePath); Object pkg = parsePackageMethod.invoke(packageParser, sourceFile, archiveFilePath, displayMetrics, 0); if (pkg == null) { return null; } if ((flags & PackageManager.GET_SIGNATURES) != 0) { collectCertificatesMethod.invoke(packageParser, pkg, 0); } return (PackageInfo) generatePackageInfoMethod.invoke(null, pkg, null, flags, 0, 0); } catch (Exception e) { e.printStackTrace(); } return null; } }