14

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

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

// 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

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"
      
              />
      
Sarthak Mishra
  • 1,033
  • 1
  • 11
  • 27

7 Answers7

20

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 keystore and replace the old SHA with that.

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 -version to get the Java version) :

Mac

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

Windows

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

2. Use keytool to obtain the release SHA1 :

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

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

Nika Kurdadze
  • 2,502
  • 4
  • 18
  • 28
4

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.

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

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

Paula Kristin
  • 813
  • 2
  • 11
  • 27
  • It is possible to change your SHA-1 to your debug. I had saved both release and debug SHA-1 previously. In order to change it: open your google cloud console > open API Manager Click on Credentials > Click on the Android Client for you App under OAuth2 Client IDs. Now you can change your type of SHA-1 and save. If you reload your configuration file creation page, you will see that the SHA-1 has changed. Now just create the new config file and paste it into you app/ directory. – Chad Mx Aug 17 '16 at 03:16
  • @ChadMx How you chagne SHA-1 I can understand – Praneeth Jun 20 '17 at 09:45
2

@-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:

-> 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.

-> 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.

==> 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:

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

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

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

For Windows Vista and Windows 7, run:

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:

keytool -list -keystore your_keystore_name

Replace your_keystore_name with 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:

keytool -list -v -keystore your_keystore_name -alias your_alias_name

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

Ilario Pierbattista
  • 3,175
  • 2
  • 31
  • 41
  • This commande saved me !!!! keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android After days of search... THANK YOU – Rémi P Jan 18 '19 at 19:03
2

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

1 key: your key store's key i.e 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

Rajesh N
  • 6,198
  • 2
  • 47
  • 58
0

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:

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

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
0

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.

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

My fault was failing to generate the signed APK with the correct keystore file, alias & password in 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.

kip2
  • 6,473
  • 4
  • 55
  • 72
0

If you have opted App Signing, you need to put the SHA1 generated from developer console

enter image description here

For Firebase, put the same SHA1 in your Project Setting

enter image description here

Aziz
  • 1,976
  • 20
  • 23