Flutter sign in with Google in Android (without firebase)
By: Snir David
Tags: Authentication, Google, Flutter, Android

There seems to be missing a guide for Google sign in with flutter, without firebase. Guides for firebase are in abundance, but if for any reason you want to go bare with google sign in, you are on your own.

So let’s discover what is necessary for Google sign in with flutter;

Most of it is also relevant for any other framework (Android native, react-native etc’).

Our steps are:

- Generate signed key for your android app

- Registering the app in Google cloud services

- Using google_sign_in package to sign in with google

Necessary authentication background

Google sign in is using oAuth 2.0 protocol, which in turn use various ways of authentication depending on the platform.

The details themselves are interesting but not relevant for you now, all you need to know is that in mobile the method is using some secure signature of the app to allow you to make requests.

Should you want to use the same google sign in with web application or server-to-server the communications way will be different. Server-to-server for example is more reliant on secret key passing, as you are in a “safe” environment and not running on a customer client.

What that means for you, is that in order to start using Google sign in with your android application, you need to sign it. Something you’d need to do anyway if you wish to release it to the playstore, but now is a good place to start.

Generate signed key for your android app

When generating signed key for android, there are 2 “channels” in the build that use the signed key: debug and release .

If you wish to have the Google sign in works on your debug environment, you need to sign that too.

Generating a key for debug is identical to how we’ll do it for release , apart from the alias that must be androiddebugkey in that case.

To generate the key, run this:

keytool -genkey -v -keystore ~/key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias androiddebugkey

It will ask you to assign a password and some other parameters.

Then it will generate a key.jks file for you. This file is the secure key, so remember to keep it a secret. Do not commit this to git.

The next step is configuring build.gradle to build the app with this signed key.

In android/app/build.gradle under android add these settings:

signingConfigs {

debug {

storeFile file('key.jks')

storePassword 'android'

keyAlias 'androiddebugkey'

keyPassword 'android'

}

} buildTypes {

debug {

signingConfig signingConfigs.debug

}

}

A few things to note here:

- in storeFile we direct to the key file. Here it assumes it is in the same android/app folder, but you can direct it to wherever is convenient for you.

- we have storePassword and keyPassword that are the password you chose before when generating the key

- The keyAlias is as I mentioned before, and for debug it is always androiddebugkey

As for “release” key, it is pretty much the same. I recommend following the react-native guide on the matter, as it is far better than the flutter one. Don’t worry, this is just android configurations so it is perfectly suitable for flutter projects too; https://facebook.github.io/react-native/docs/signed-apk-android

Registering the app for google sign-in

First, you should have a project set up in google cloud platform. If you don’t have any yet, go to https://console.cloud.google.com/ and create one.

Once you have a google cloud project set up, you’ll need to configure your project to have Google sign-in enabled. You can do that here: https://developers.google.com/identity/sign-in/web/sign-in

Click to configure for Android platform, and you’ll get to this screen:

There are 2 things to fill here, package name and SHA-1 signature of the certificate we created earlier.

Find your package name at AndroidManifest.xml file, under the package= attribute.

For the SHA-1 signature, use the command showing in the window on the keystore generated earlier. Note: the keytool CLI tool is available only once you install JDK. So if for some reason you do not have JDK installed yet, do that first.

keytool -keystore key.jks -list -v

Is the command for the key you generated earlier, and in the output you’ll have the SHA-1 fingerprint (as well as SHA-256 but there is no use for it here).

Use `google_sign_in ` package in the app

To limit the scope of this article I will just show how to use the package directly. If you wish to use it with the Bloc pattern for example, there is a great demo app here to follow: https://bloclibrary.dev/#/flutterfirebaselogintutorial

The most basic usage of the package is to initialize:

_googleSignIn = GoogleSignIn()

and then, when you wish to authenticate just call the future:

_googleSignIn.signIn();

And the package will take care of everything else. The package will also add the flutter plugin for google sign in, so make sure to hard restart your app to make it work.

But going further, you can adjust the scopes you request from google when initializing the GoogleSignIn object as such:

GoogleSignIn _googleSignIn = GoogleSignIn(

scopes: [

'email',

'https://www.googleapis.com/auth/contacts.readonly',

],

);

There are many scopes, for every google service available. For a full list see here: https://developers.google.com/identity/protocols/googlescopes and remember that in order to use some of these scopes, you need to enable their APIs in your google cloud platform first.

When using the basic configuration, and calling

await _googleSignIn.signIn();

The resulted GoogleSignInAccount might look like this (In my case):

GoogleSignInAccount:{displayName: Snir David, email: snir***@gmail.com, id: 1234567123456712367, photoUrl: https://lh3.googleusercontent.com/a-/AAuuE42_4fdsAFV}

And that's a wrap. Hope this article helped you in any way.