How to use PackageInfo.GET_SIGNING_CERTIFICATES in API 28?

前端 未结 4 1364
长发绾君心
长发绾君心 2020-12-06 05:53

The documentation of PackageManager.GET_SIGNATURES says \"This constant was deprecated in API level 28. Use GET_SIGNING_CERTIFICATES instead\".

Unfortunately it was

4条回答
  •  無奈伤痛
    2020-12-06 06:50

    My solution is:

    In the gradle build set "compileSdkVersion 28" and "targetSdkVersion 28", now you can use this sample code:

    try {
        if(Build.VERSION.SDK_INT >= 28) {
            @SuppressLint("WrongConstant") final PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNING_CERTIFICATES);
            final Signature[] signatures = packageInfo.signingInfo.getApkContentsSigners();
            final MessageDigest md = MessageDigest.getInstance("SHA");
            for (Signature signature : signatures) {
                md.update(signature.toByteArray());
                final String signatureBase64 = new String(Base64.encode(md.digest(), Base64.DEFAULT));
                Log.d("Signature Base64", signatureBase64);
            }
        }
    } catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    

    If strangely Android Studio does not recognize the constant GET_SIGNING_CERTIFICATES you can use the @SuppressLint ("WrongConstant") annotation.

提交回复
热议问题