java Google 登录签名的 apk 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36045652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:55:13  来源:igfitidea点击:

Google sign in signed apk not working

javaandroidgoogle-apigoogle-signin

提问by Sarthak Mishra

Well all works till i generate the signed apk . I followed the entire process as told on the google developers page

一切正常,直到我生成签名的 apk。我按照谷歌开发者页面上的说明遵循了整个过程

1.I generated the google-services.json file with keyhash and package name in it
2.Included all the class level and application level dependencies like this

1.我生成了带有keyhash和包名的google-services.json文件
2.Included所有类级别和应用程序级别的依赖项

// Top-level build file where you can add configuration options common to all sub-projects/modules.

 buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'
    classpath 'com.google.gms:google-services:2.0.0-alpha6'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}
 allprojects {
  repositories {
     jcenter()
   }
  }

Application gradle file

应用程序gradle文件

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
compileSdkVersion 23
buildToolsVersion "23.0.0"

defaultConfig {
    applicationId "com.example.skmishra.finalgooglesignin"
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),          'proguard-rules.pro'
    }
   }
}

 dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.google.android.gms:play-services:8.3.0'


}
  1. My Sign In java Code

    package com.example.skmishra.finalgooglesignin;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    import com.google.android.gms.auth.api.Auth;
    import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
    import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
    import com.google.android.gms.auth.api.signin.GoogleSignInResult;
    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.SignInButton;
    import com.google.android.gms.common.api.GoogleApiClient;
    
    public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
    
        private static final int RC_SIGN_IN = 200 ;
        private static final String TAG = "Sign In" ;
        private GoogleApiClient mGoogleApiClient;
       SignInButton google;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // Configure sign-in to request the user's ID, email address, and basic
    // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestEmail()
                    .build();
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .build();
            SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
            signInButton.setSize(SignInButton.SIZE_STANDARD);
            signInButton.setScopes(gso.getScopeArray());
            google=(SignInButton)findViewById(R.id.sign_in_button);
            google.setOnClickListener(this);
    
    
        }
    
    
        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            Toast.makeText(this,"Failed to connect",Toast.LENGTH_LONG).show();
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.sign_in_button:
                    signIn();
                    break;
                // ...
            }
        }
    
        private void signIn() {
            Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
            startActivityForResult(signInIntent, RC_SIGN_IN);
        }
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
            if (requestCode == RC_SIGN_IN) {
                GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
                handleSignInResult(result);
            }
        }
    
        private void handleSignInResult(GoogleSignInResult result) {
            Log.d(TAG, "handleSignInResult:" + result.isSuccess());
            if (result.isSuccess()) {
                // Signed in successfully, show authenticated UI.
                GoogleSignInAccount acct = result.getSignInAccount();
                Toast.makeText(this,"Name :"+acct.getDisplayName()+" Email :"+acct.getEmail(),Toast.LENGTH_LONG).show();
            } else {
                // Signed out, show unauthenticated UI.
                Toast.makeText(this,"Signed out ",Toast.LENGTH_LONG).show();
            }
        }
    }
    
    1. My layout code

          <com.google.android.gms.common.SignInButton
              android:id="@+id/sign_in_button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Check this out"
      
              />
      
  1. 我的登录java代码

    package com.example.skmishra.finalgooglesignin;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    import com.google.android.gms.auth.api.Auth;
    import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
    import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
    import com.google.android.gms.auth.api.signin.GoogleSignInResult;
    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.SignInButton;
    import com.google.android.gms.common.api.GoogleApiClient;
    
    public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
    
        private static final int RC_SIGN_IN = 200 ;
        private static final String TAG = "Sign In" ;
        private GoogleApiClient mGoogleApiClient;
       SignInButton google;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // Configure sign-in to request the user's ID, email address, and basic
    // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestEmail()
                    .build();
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .build();
            SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
            signInButton.setSize(SignInButton.SIZE_STANDARD);
            signInButton.setScopes(gso.getScopeArray());
            google=(SignInButton)findViewById(R.id.sign_in_button);
            google.setOnClickListener(this);
    
    
        }
    
    
        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            Toast.makeText(this,"Failed to connect",Toast.LENGTH_LONG).show();
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.sign_in_button:
                    signIn();
                    break;
                // ...
            }
        }
    
        private void signIn() {
            Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
            startActivityForResult(signInIntent, RC_SIGN_IN);
        }
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
            if (requestCode == RC_SIGN_IN) {
                GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
                handleSignInResult(result);
            }
        }
    
        private void handleSignInResult(GoogleSignInResult result) {
            Log.d(TAG, "handleSignInResult:" + result.isSuccess());
            if (result.isSuccess()) {
                // Signed in successfully, show authenticated UI.
                GoogleSignInAccount acct = result.getSignInAccount();
                Toast.makeText(this,"Name :"+acct.getDisplayName()+" Email :"+acct.getEmail(),Toast.LENGTH_LONG).show();
            } else {
                // Signed out, show unauthenticated UI.
                Toast.makeText(this,"Signed out ",Toast.LENGTH_LONG).show();
            }
        }
    }
    
    1. 我的布局代码

          <com.google.android.gms.common.SignInButton
              android:id="@+id/sign_in_button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Check this out"
      
              />
      

回答by Nika Kurdadze

As I understand, you have provided the debug SHA1 in the developer console, then you signed the apk and the SHA1 changed. If this is the case try the following you should obtain the release SHA1 from the keystoreand replace the old SHA with that.

据我了解,您已经在开发人员控制台中提供了调试 SHA1,然后您对 apk 进行了签名,并且 SHA1 已更改。如果是这种情况,请尝试以下操作,您应该从密钥库中获取发行版 SHA1,并用它替换旧的 SHA。

1.Open terminal and change the directory to JDK bin directory. Include your installed JDK version inside the path, for me it was - jdk1.8.0_101(type javac -versionto get the Java version) :

1、打开终端,将目录切换到JDK bin目录。在路径中包含您安装的 JDK 版本,对我来说它是 - jdk1.8.0_101(键入javac -version以获取 Java 版本):

Mac

苹果电脑

    cd /Library/Java/JavaVirtualMachines/<your_JDK_version>.jdk/Contents/Home/bin

Windows

视窗

    cd C:\Program Files\Java\your_JDK_version\bin 

2.Use keytoolto obtain the release SHA1 :

2.使用keytool获取发布SHA1:

    keytool -list -v -keystore <keystore_name> -alias <alias_name>

3.Go to your project's credentials pageand replace the SHA1 to your keystore's release SHA1.

3.转到您项目的凭据页面并将 SHA1 替换为您的密钥库的发行版 SHA1。

回答by Paula Kristin

I had the same problem. I think I found out that Google doesn't allow you to have one certification for both the debug and the release apk of your app. You need to choose to either get the certificate only for one of them. Please correct me if I am wrong.

我有同样的问题。我想我发现谷歌不允许你对你的应用程序的调试和发布 apk 都拥有一个认证。您需要选择仅获取其中之一的证书。如果我错了,请纠正我。

What I did was to input the SHA1 fingerprint credentials for my releasekey and not my debug key on this link here

我所做的就是输入我的SHA1指纹的凭证发布密钥,这个链接不是我调试的关键在这里

Afterwards, my released apk worked and not my debug key.

之后,我发布的 apk 起作用了,而不是我的调试密钥。

回答by vijay bharati

@-vj-@ ==> The API key is based on a short form of your app's digital certificate, known as its SHA-1 fingerprint. To display the SHA-1 fingerprint for your certificate, first ensure that you are using the right certificate. You may have two certificates:

@-vj-@ ==> API 密钥基于应用程序数字证书的一种简短形式,称为其 SHA-1 指纹。要为您的证书显示 SHA-1 指纹,请首先确保您使用的是正确的证书。您可能有两个证书:

-> A debug certificate: The Android SDK tools generate this certificate automatically when you do a debug build. Only use this certificate with apps that you're testing. Do not attempt to publish an app that's signed with a debug certificate. The debug certificate is described in more detail in Signing in Debug Mode in the Android Developer Documentation.

-> 调试证书:当您进行调试构建时,Android SDK 工具会自动生成此证书。仅将此证书用于您正在测试的应用程序。不要尝试发布使用调试证书签名的应用程序。调试证书在 Android 开发人员文档中的调试模式下签名中有更详细的描述。

-> A release certificate: The Android SDK tools generate this certificate when you do a release build. You can also generate this certificate using the keytool program. Use this certificate when you are ready to release your app to the world.

-> 发布证书:当您进行发布构建时,Android SDK 工具会生成此证书。您还可以使用 keytool 程序生成此证书。当您准备好向全世界发布您的应用程序时,请使用此证书。

==> Displaying the debug certificate fingerprint

==> 显示调试证书指纹

Locate your debug keystore file. The file name is debug.keystore, and is created the first time you build your project. By default, it is stored in the same directory as your Android Virtual Device (AVD) files:

找到您的调试密钥库文件。文件名为 debug.keystore,在您第一次构建项目时创建。默认情况下,它与您的 Android 虚拟设备 (AVD) 文件存储在同一目录中:

macOS and Linux: ~/.android/Windows Vista and Windows 7: C:\Users\your_user_name\.android\List the SHA-1 fingerprint:

macOS 和 Linux:~/.android/Windows Vista 和 Windows 7:C:\Users\your_user_name\.android\列出 SHA-1 指纹:

For Linux or macOS, open a terminal window and enter the following:

对于 Linux 或 macOS,打开终端窗口并输入以下内容:

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

For Windows Vista and Windows 7, run:

对于 Windows Vista 和 Windows 7,运行:

keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

==> Displaying the release certificate fingerprint

==> 显示发布证书指纹

Locate your release certificate keystore file. There is no default location or name for the release keystore. If you don't specify one when you build your app for release, the build will leave your .apk unsigned, and you'll have to sign it before you can publish it. For the release certificate, you also need the certificate's alias and the passwords for the keystore and the certificate. You can list the aliases for all the keys in a keystore by entering:

找到您的发布证书密钥库文件。发布密钥库没有默认位置或名称。如果您在构建要发布的应用程序时未指定一个,则构建将保​​留您的 .apk 未签名,您必须先对其进行签名,然后才能发布它。对于发布证书,您还需要证书的别名以及密钥库和证书的密码。您可以通过输入以下内容列出密钥库中所有密钥的别名:

keytool -list -keystore your_keystore_name

Replace your_keystore_namewith the fully-qualified path and name of the keystore, including the .keystore extension. You'll be prompted for the keystore's password. Then keytool displays all the aliases in the keystore. Enter the following at a terminal or command prompt:

替换your_keystore_name为密钥库的完全限定路径和名称,包括 .keystore 扩展名。系统将提示您输入密钥库的密码。然后 keytool 显示密钥库中的所有别名。在终端或命令提示符下输入以下内容:

keytool -list -v -keystore your_keystore_name -alias your_alias_name

Replace your_keystore_namewith the fully-qualified path and name of the keystore, including the .keystore extension. Replace your_alias_namewith the alias that you assigned to the certificate when you created it.

替换your_keystore_name为密钥库的完全限定路径和名称,包括 .keystore 扩展名。替换your_alias_name为您在创建证书时分配给该证书的别名。

回答by Rajesh Nasit

You have to put two SHA-1 key in Firebase console Project->Setting->Android

您必须在 Firebase 控制台项目->设置->Android 中放置两个 SHA-1 密钥

1 key: your key store's key i.e keytool -list -v -keystore -alias

1 个密钥:您的密钥库的密钥,即 keytool -list -v -keystore -alias

2 key: your play store's key i.e Release management->App signing->App signing Certificate->SHA 1 key fingerprint

2键:你的Play商店的密钥,即发布管理->应用签名->应用签名证书->SHA 1密钥指纹

回答by kip2

FWIW:

FWIW:

For me, I had the release and debug configuration (Oath client IDs, SHA-1, etc) all set up in the Google dev console and google-services.json file installed on the project.

对我来说,我在 Google 开发控制台和项目上安装的 google-services.json 文件中设置了发布和调试配置(Oath 客户端 ID、SHA-1 等)。

Because I'm working with multiple build flavors, I put the various config files in their respective ./app/<flavorN>/directories.

因为我正在使用多种构建风格,所以我将各种配置文件放在各自的./app/<flavorN>/目录中。

My fault was failing to generate the signed APK with the correct keystore file, alias & passwordin the Generate Signed APK wizard. The wizard was caching keys and credentials from a previous build flavor (tsk). But after reseting the keys to map to the target flavor, I now have a working Google Sign in.

我的错误是未能在 Generate Signed APK 向导中使用正确的密钥库文件、别名和密码生成已签名的 APK。该向导正在缓存来自先前构建风格 (tsk) 的密钥和凭据。但是在重置键以映射到目标风格后,我现在可以使用 Google 登录了。

回答by Vyacheslav

It is hardly difficult to find the place to activate inside the huge developer console. May be I will help somebody in the future. You have to register your Android application here - "enable Sign-In" in this page:

在巨大的开发者控制台中找到激活的地方并不困难。也许我将来会帮助某人。您必须在此处注册您的 Android 应用程序 - 在此页面中“启用登录” :

Just input usual for console developer values this your signed SHA-1. That's it!

只需输入控制台开发人员常用的值,这是您签名的 SHA-1。而已!