[MUSIC PLAYING]
Hey, what's up, everybody?
Laurence Moroney here from the Firebase team,
and welcome to another episode of "Firecasts."
On today's episode, I'm going to show you
how to sign in with Google using Firebase Auth on Android.
And from that, you'll be able to see how you can sign in
with other providers, too.
You'll be up and running with this feature in just a few
minutes, so let's get started.
The first thing you'll need to do
is to make sure that you have a development
environment properly set up.
To get started with Firebase and Android,
use Android Studio to create a new project.
Just accept the default form factors,
and create one with an empty activity.
Whenever you change any of the Gradle files,
you'll see this message telling you
that a sync may be necessary.
It's good just to sync it.
In your module-level build.gradle,
you'll need to add the class path to the Google services
package as shown.
Then, in your app-level build.gradle,
you'll apply the google-services plug-in.
If you sync, you'll see an error that there's
a missing google-services JSON file.
Don't worry, that's easy to fix.
You do this from the Firebase console.
If you don't have a project already,
create one and follow its steps.
Once you're done, you'll see the project on this screen.
Simply select the project, and you'll
see the apps associated with it.
Click the Add App button, and select Android.
You'll now need the package name and the SHA1.
The package name is what you used when you were specifying
your app, and the SHA1 can be gotten
from your debug keystore, like this.
Paste them in, and then add the app.
You'll see that the google-services JSON file gets
downloaded to your machine.
Select the project view, and then open your app.
Drag the file into the App folder as shown.
Now, when you Gradle sync, you'll
see that everything works, and you're
ready to build in Firebase.
OK, great.
Now that you have a working development environment set up,
the next steps will be to build a simple app that lets you sign
in with Google and Android.
You'll import the required files,
create a simple single-activity app,
and then finally code this app with a sign-in button that
signs into Google and then tells your app
the ID of the signed-in person.
Let's get going.
Before you start coding your Android app,
make sure you have everything properly
set up in the Firebase console.
If you've done that, you'll see it in the Overview screen
like this.
Next, ensure that auth is enabled for your project, which
you can do in the Auth section.
Select the sign-in method that you want.
And in this case, it's Google.
In Android Studio, check your build.gradle.
Make sure that the Play Services Auth Libraries are included.
And then, in your layout file, specify
that you're using a sign-in button,
and you'll get it at runtime.
Then, in your code, there's a few steps
that you need to follow.
First is to create a Google sign-in options object that
specifies your sign-in scope and use this to create a Google API
client.
When the user clicks the Sign-in button,
you create a sign-in intent, and the activity for that.
You can see it here.
You get the intent from the API and start an activity for it.
Then, on the result, you'll be able to get the user's data.
You get a Google sign-in result object from the data that
comes back from the intent.
And from this, you can get a Google sign-in
account, from which you can get the display name.
As you'll see in the demo, we get the user's email address
and effectively turn it into a friendly name and display it.
Signing out is even easier.
You simply call the Sign-out API, catch its result,
and you can update your UI from that.
Let's look at this in action.
Here's the app we've been building.
We have the branded Sign-in button and a Sign-out button.
When we click the Sign-in button, we're given a picker.
You pick the account that you want,
and then once you've been signed in, you're good to go.
You can sign back out again, and this clears the UI.
When you sign in, you're also given the option
to add an account.
And then you're taken into the flow to add a new user.
I won't go through that now.
I'll just go back and pick an account with the picker.
That's how easy it is to add Sign-in with Google
to an Android app with Firebase.
There you go.
You've just built your first app that
signs in with Google and Android,
and it only took a few minutes.
You're now ready to personalize your app,
or maybe add other service providers such as Facebook,
GitHub, and more.
That's it for this "Firecast."
Please leave your questions in the comments below,
or reach out on Twitter and G+ using the #askfirebase hashtag.
Don't forget to hit that Subscribe button.
I'm Laurence Moroney, and thanks for watching.
[MUSIC PLAYING]