The documentation of PackageManager.GET_SIGNATURES says \"This constant was deprecated in API level 28. Use GET_SIGNING_CERTIFICATES instead\".
Unfortunately it was
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.