Passer au contenu

Firebase Google Connexion on Android

This Guide will Aide you integrate Google Sign-In with Firebase Authentication on Android. I assume you have already completed the general Firebase Google Configuration

I will assume that you have not yet created your Android Application in the Firebase Console. If you have, your steps will be slightly different.

  1. Go to your project Aperçu over at console.cloud.google.com

    Firebase Project Overview
  2. Click on the Add app button

    Firebase Add App Button

    It is possible that you will have to look here for this button. This applies only if you have already created a different Application in the Firebase Console.

    Firebase Add App Button
  3. Select Android

    Firebase Add App Android Button
  4. Fill the first part of the form

    1. Fill the Android package name
      1. Open Android Studio at your app using npx cap open android
      2. At the very bottom of the navigator, find the Gradle Scripts Gradle Scripts section in Android Studio project navigator
      3. Find build.gradle for the module app build.gradle (Module: app) file in Gradle Scripts section
      4. Copy the android.defaultConfig.applicationId. This will be your package name in the Firebase console Build.gradle file showing applicationId configuration

      The ID shown here will differ from the one I will use for the rest of the guide. I will use app.capgo.plugin.SocialLogin for the rest of the guide.

      1. Paste it in the Firebase console Firebase Add App Android Package Name Field
    2. Click on the Register app button Firebase Add App Android Register Button
  5. Skip the Download and then add config file step

    Firebase Add App Android Skip 'Download and then add config file'
  6. Skip the Add firebase SDK step

    Firebase Add App Android Skip 'Add firebase SDK'
  7. Click on the Continue to console button

    Firebase Add App Android Continue to Console Button
  8. If you do not get automatically authenticated, go to settings -> general -> your apps -> find your android app and click on it

    Firebase Add App Android Settings General Your Apps Button
  9. Get your SHA1 fingerprint

    Follow steps 10-11 from the Google Connexion Android Configuration Guide:

    1. Now, open the terminal. Make sure that you are in the android folder of your app and run ./gradlew signInReport

      Terminal showing gradlew signInReport command
    2. Scroll to the top of this command. You should see the following. Copy the SHA1.

      Terminal output showing SHA1 certificate fingerprint

    I will use a slightly different SHA1 for the rest of the Guide, because I have changed computes since writing the original Google Connexion Android Configuration Guide.

    The SHA1 is very Important to get right. If you mess up, the authentication will fail in strange ways. Please READ THE GOOGLE Connexion Android Configuration Guide to get it right.

  10. Ajouter the SHA1 to the Firebase project

    1. Click on the Add fingerprint button Firebase Add App Android Add Fingerprint Button
    2. Paste the SHA1 you copied in the Précédent step Firebase Add App Android Add Fingerprint SHA1 Field
    3. Click on the Save button Firebase Add App Android Add Fingerprint Save Button
  11. Get your web client ID

    1. Go to Build -> Authentication Firebase Authentication Menu
    2. Click on the Sign-in method button Firebase Authentication Sign-in Method Button
    3. Click on the Google provider Firebase Authentication Sign-in Method Google Provider
    4. Click on the Web SDK configuration button Firebase Authentication Sign-in Method Web SDK Configuration Button
    5. Copy the Web client ID. This will be your webClientId in the initialize method of the plugin.
    Firebase Authentication Sign-in Method Web SDK Configuration Web Client ID
  12. Use the web client ID in JS.

    I recommend using the authenticateWithGoogle helper function available in the authUtils.ts file of the example app.

At this point, you are TECHNICALLY ready to use Google Sign-In with Firebase Authentication on Android. However, I would recommend double-checking the Configuration in the Google Cloud console as explained in the Suivant step.

Double-Vérifier the Configuration in Google Cloud console

Section titled “Double-Vérifier the Configuration in Google Cloud console”

In order to make sure that the Configuration is correct, you should double-Vérifier the Configuration in the Google Cloud console.

  1. Go to console.cloud.google.com

  2. Find your project

    1. Click on the project selector Google Cloud Console Project Selector
    2. Search up your project by the exact name of your Firebase project and click on it. In my case, it is sociallogin-tutorial-app. Firebase Project Selector Project
  3. Open the search bar and open credentials

    1. Open the Recherche bar Google Cloud Console Search Bar
    2. Search for credentials and click on the APIs and Services one (number 2 on the screenshot) Google Cloud Console Credentials Search
  4. Verify that you see both the Android and Web client IDs in the list.

    Google Cloud Console Credentials List

    If you do not see both the Android and Web client IDs in the list, you have made a mistake in the Configuration. Please go Retour and Vérifier your steps.

    It is also possible, and it has happened to me, that you already have added the Android SHA1 hash with the same Application ID in a different project. This will result in Firbase being unable to Créer an Android client ID. In this case, you will need to Retirer the SHA1 from the other project as well as on Firebase (using the Firebase console to Retirer the Android Application) and recreate it on Firbase

  5. Verify that the Android client ID is correctly configured in the Firebase console.

    1. Click on the Android app Google Cloud Console Android App Select
    2. Confirm that the SHA1 hash is correctly configured and that it matches the one you copied in the Précédent steps. Google Cloud Console Android App SHA1 Configured
  6. Verify that the Web client ID is correctly configured in the Firebase console.

    1. Click on the Web app Google Cloud Console Web App Select
    2. Confirm that the client ID matches the one you copied in the Précédent steps. Google Cloud Console Web App Client ID Configured

    Please ignore the rest of the Paramètres of the web client. We will discuss this on the web Configuration Guide.

Voila! You are now ready to use Google Sign-In with Firebase Authentication on Android.

If authentication hangs or fails:

  • Verify the idToken audience matches your Firebase web client ID
  • Vérifier that Google Sign-In is enabled in Firebase Console
  • Ensure the SHA-1 fingerprint is correctly configured
  • Review the Exemple Application code for Référence