我们再申请第三方平台的参数时,需要上传签名文件的MD5,下面通过java代码生成
!!!记住一定要在build.gradle配置将应用的签名文件设置成你自己的,使用debug的默认签名是不对的
import android.content.Context; import android.content.pm.PackageManager; import android.content.pm.Signature; import android.util.Base64; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public String getSignatureMD5(Context context) { try { PackageManager pm = context.getPackageManager(); Signature[] signatures = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES).signatures; // 只取第一个签名 Signature signature = signatures[0]; MessageDigest md = MessageDigest.getInstance("MD5"); byte[] digest = md.digest(signature.toByteArray()); // 转换为十六进制字符串 return bytesToHex(digest); } catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) { // 处理异常 } return null; } private String bytesToHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format("%02x", b)); } return sb.toString(); }
标签:signatures,生成,pm,import,Android,PackageManager,android,MD5 From: https://www.cnblogs.com/maowuge/p/18674839