Android examples for App:APK File
get Apk Signature By File Path
//package com.java2s; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.Signature; import android.util.DisplayMetrics; import android.util.Log; import java.io.File; import java.lang.reflect.Constructor; import java.lang.reflect.Method; public class Main { private static Signature[] getApkSignatureByFilePath(Context context, String apkFile) {//from w w w.ja v a 2 s .co m PackageInfo newInfo = getPackageArchiveInfo(apkFile, PackageManager.GET_ACTIVITIES | PackageManager.GET_SIGNATURES); if (newInfo != null) { if (newInfo.signatures != null && newInfo.signatures.length > 0) { return newInfo.signatures; } } Log.d("aaaa", "getApkSignatureByFilePath " + newInfo.signatures.toString()); return null; } @SuppressWarnings("unchecked") private static PackageInfo getPackageArchiveInfo( String archiveFilePath, int flags) { // Workaround for // https://code.google.com/p/android/issues/detail?id=9151#c8 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 metrics = new DisplayMetrics(); metrics.setToDefaults(); final File sourceFile = new File(archiveFilePath); Object pkg = parsePackageMethod.invoke(packageParser, sourceFile, archiveFilePath, metrics, 0); if (pkg == null) { return null; } if ((flags & android.content.pm.PackageManager.GET_SIGNATURES) != 0) { collectCertificatesMethod.invoke(packageParser, pkg, 0); } return (PackageInfo) generatePackageInfoMethod.invoke(null, pkg, null, flags, 0, 0); } catch (Exception e) { Log.e("Signature Monitor", "android.content.pm.PackageParser reflection failed: " + e.toString()); } return null; } }