我试图在Android工作室,在Windows上做一个地图应用程序。如何查询SHA-1指纹证书编号?
当我使用Eclipse时,它就在Windows ->首选项-> Android ->构建下。但在Android Studio中,我找不到类似的选项。
我在文档里看到
Android Studio自动在调试模式下签署应用程序
从IDE运行或调试项目。
所以我试着设置我的Java bin路径,并运行以下命令,从这里取:
keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
然而,我得到了一个错误消息,说它是一个非法选项。
一步一步地,我如何在Android Studio中获得这些信息?
作为解决方案:我之前使用Eclipse生成的SHA-1指纹注册了我的应用程序。我可以使用相同的API键工作在我的项目在Android工作室?
简单,在你的启动屏幕中调用这个方法:hash()和getCertificateSHA1Fingerprint(),然后键将在日志中可见
private void hash() {
PackageInfo info;
try {
info = getPackageManager().getPackageInfo(
this.getPackageName(), PackageManager.GET_SIGNATURES);
for (android.content.pm.Signature signature : info.signatures) {
MessageDigest md;
md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.e("sagar sha key", md.toString());
String something = new String(Base64.encode(md.digest(), 0));
Log.e("sagar Hash key", something);
System.out.println("Hash key" + something);
}
} catch (PackageManager.NameNotFoundException e1) {
Log.e("name not found", e1.toString());
} catch (NoSuchAlgorithmException e) {
Log.e("no such an algorithm", e.toString());
} catch (Exception e) {
Log.e("exception", e.toString());
}
}
private void getCertificateSHA1Fingerprint() {
PackageManager pm = this.getPackageManager();
String packageName = this.getPackageName();
int flags = PackageManager.GET_SIGNATURES;
PackageInfo packageInfo = null;
try {
packageInfo = pm.getPackageInfo(packageName, flags);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
Signature[] signatures = packageInfo.signatures;
byte[] cert = signatures[0].toByteArray();
InputStream input = new ByteArrayInputStream(cert);
CertificateFactory cf = null;
try {
cf = CertificateFactory.getInstance("X509");
} catch (CertificateException e) {
e.printStackTrace();
}
X509Certificate c = null;
try {
c = (X509Certificate) cf.generateCertificate(input);
} catch (CertificateException e) {
e.printStackTrace();
}
String hexString = "";
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
byte[] publicKey = md.digest(c.getEncoded());
Log.e("sagar SHA",byte2HexFormatted(publicKey));
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (CertificateEncodingException e) {
e.printStackTrace();
}
}
public static String byte2HexFormatted(byte[] arr) {
StringBuilder str = new StringBuilder(arr.length * 2);
for (int i = 0; i < arr.length; i++) {
String h = Integer.toHexString(arr[i]);
int l = h.length();
if (l == 1) h = "0" + h;
if (l > 2) h = h.substring(l - 2, l);
str.append(h.toUpperCase());
if (i < (arr.length - 1)) str.append(':');
}
return str.toString();
}
谢谢你!