获取apk签名的方法

  1. 获取本地文件系统中apk文件的签名方法,android3.0以前需要通过反射方式,以下反射方法在5.0上会失败。
public static String getAPKSignatures(String apkPath) {
        File appFile = new File(apkPath);
        if (!appFile.exists())
            return "";

        String PATH_PackageParser = "android.content.pm.PackageParser";
        try {
            // apk包的文件路径
            // 这是一个Package 解释器, 是隐藏的
            // 构造函数的参数只有一个, apk文件的路径
            // PackageParser packageParser = new PackageParser(apkPath);
            Class pkgParserCls = Class.forName(PATH_PackageParser);
            Class[] typeArgs = new Class[1];
            typeArgs[0] = String.class;
            Constructor pkgParserCt = pkgParserCls.getConstructor(typeArgs);
            Object[] valueArgs = new Object[1];
            valueArgs[0] = apkPath;
            Object pkgParser = pkgParserCt.newInstance(valueArgs);
            // 这个是与显示有关的, 里面涉及到一些像素显示等等, 我们使用默认的情况
            DisplayMetrics metrics = new DisplayMetrics();
            metrics.setToDefaults();
            // PackageParser.Package mPkgInfo = packageParser.parsePackage(new
            // File(apkPath), apkPath,
            // metrics, 0);
            typeArgs = new Class[4];
            typeArgs[0] = File.class;
            typeArgs[1] = String.class;
            typeArgs[2] = DisplayMetrics.class;
            typeArgs[3] = Integer.TYPE;
            Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod("parsePackage",
                    typeArgs);
            valueArgs = new Object[4];
            valueArgs[0] = new File(apkPath);
            valueArgs[1] = apkPath;
            valueArgs[2] = metrics;
            valueArgs[3] = PackageManager.GET_SIGNATURES;
            Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser, valueArgs);

            typeArgs = new Class[2];
            typeArgs[0] = pkgParserPkg.getClass();
            typeArgs[1] = Integer.TYPE;
            Method pkgParser_collectCertificatesMtd = pkgParserCls.getDeclaredMethod(
                    "collectCertificates",
                    typeArgs);
            valueArgs = new Object[2];
            valueArgs[0] = pkgParserPkg;
            valueArgs[1] = PackageManager.GET_SIGNATURES;
            pkgParser_collectCertificatesMtd.invoke(pkgParser, valueArgs);
            // 应用程序信息包, 这个公开的, 不过有些函数, 变量没公开
            Field packageInfoFld = pkgParserPkg.getClass().getDeclaredField("mSignatures");
            Signature[] info = (Signature[]) packageInfoFld.get(pkgParserPkg);
            if (info == null || info.length < 1)
                return "";
            return info[0].toCharsString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

android4.0 以后版本可以直接通过api获取未安装的apk签名:

    private static String getApkSignatures(Context context, File apkFile) {
        String sign = "";
        if (apkFile != null && apkFile.exists()) {
            PackageManager pm = context.getPackageManager();
            PackageInfo pkgInfo = pm.getPackageArchiveInfo(
                    apkFile.getAbsolutePath(), PackageManager.GET_SIGNATURES);
            if (pkgInfo != null && pkgInfo.signatures != null
                    && pkgInfo.signatures.length > 0) {
                sign = pkgInfo.signatures[0].toCharsString();
            }
        }
        return sign;
    }

获取已安装apk的签名方法

 public static String getInstalledAPKSignature(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo appInfo = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_SIGNATURES);
            if (appInfo == null || appInfo.signatures == null)
                return "";
            return appInfo.signatures[0].toCharsString();
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    } 

你可能感兴趣的:(小知识点)