我试图在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();
        }

谢谢你!

其他回答

获取生产密钥库的SHA1:

Build --> Generate Signed APK... Create keystore with password and follow the steps Go to your Mac/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/bin and drag the bin folder to the terminal after cd command to point at it so you can use the keytool tool. So, in terminal write cd (drag bin here) then press enter. Then, copy and paste this in the terminal: keytool -exportcert -alias Your_keystore_AliasName -keystore /Users/Home/Development/AndroidStudioProjects/YoutubeApp/app/YoutubeApp_keystore.jks -list -v Erase my path and go where you stored your keystore and drag your keystone and drop it after -keystore in the command line so the path will get created. Also, erase Your_keystore_AliaseName to put your alias keystone name that you used when you created it. Press Enter and enter the password :) When you enter the password, the terminal won't show that it receives keyboard entries, but it actually does, so put the password and press Enter even if you don't see the password is typed out.

对于Mac来说,这非常有效:

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

我发现了一个非常简单的过程来找到你的MD5, SHA-1指纹使用 Android工作室。 运行你的项目 转到Gradle菜单(菜单:视图->工具窗口-> Gradle) 转到Gradle窗口的signingReport。(你的项目->任务-> android -> signingReport) 运行它。(使用双击或Ctrl + Shift + F10) 在运行窗口,你会发现所有的信息。

它只适用于调试模式。在释放模式下,我看不到sha-1。 这里是gradlew signingReport的结果

Variant: release
Config: none
----------
Variant: releaseUnitTest
Config: none
----------
Variant: debug
Config: debug
Store: path\Android\avd\.android\debug.keystore
Alias: AndroidDebugKey
MD5: xx:xx:xx:62:86:B7:9C:BC:FB:AD:C8:C6:64:69:xx:xx
SHA1: xx:xx:xx:xx:0F:B0:82:86:1D:14:0D:AF:67:99:58:1A:01:xx:xx:xx
Valid until: Friday, July 19, 2047
----------

所以我必须使用keytool来获取sha-1。下面是Firebase官方文档:

Get_sha-1_for_release

点击右边面板上的Gradle图标,然后点击(根)。

任务> android > signingReport

然后Gradle脚本将执行,您将看到您的键。

这在Gnu/Linux - Mac - Windows中非常非常简单

首先:—复制此命令

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android 

第二步:现在在Android Studio的终端中复制并粘贴命令

结果!

祝你好运! !