[MUSIC PLAYING]
MARINA COELHO: Hey, there, Android developers.
This is Marina from the Firebase Team.
And in this video, I'm going to show you
how to get started with Firebase on Android.
I'm going to use this Android app to illustrate step
by step how to start using Firebase services in an Android
application.
It's a simple app that is built with Jetpack Compose.
And it only has one composable function
that creates a single button in the center of the screen.
But before we start, there are some prerequisites
that I need to pay attention to.
First, I need to install the most recent version of Android
Studio.
I can download it from developer.android.com I also
need to make sure my Android app uses AndroidX and meets
the minimum version requirements.
This includes the Target API, Compile SDK,
and Gradle build tools.
And if I'm not sure about the minimum version requirements,
I can always have a look at the official Firebase
documentation.
Here's a quick tip.
Android Studio will highlight the Target API and Gradle
build tools versions if they are not the most up to date ones.
So instead of looking at the documentation,
I can simply hover my cursor over the highlighted area
and ask Android Studio to update to the latest version.
Once I have Android Studio installed
and meet all the version requirements,
I need to set up a physical Android device
or use an emulator to run my application.
I choose to create the emulator from within the Android Studio
by clicking on Tools, Device Manager, Create Device.
Once I've done all this, I can now
sign into the Firebase Console with my Google account
to kick off adding Firebase to my app.
And the first thing I see here is something
about a Firebase project.
Before adding Firebase to this Android app,
let me first explain what the Firebase project is,
and what's the difference between Firebase projects
and Firebase apps.
A Firebase project is actually a Google Cloud project
behind the scenes but with all the Firebase services
enabled for it.
Inside of Firebase project, I can register platform variants
of an application.
And they will all share the same resources.
For example, all app switching in a Firebase project
will share the same database, the same user base,
the same analytics data, and so on.
Firebase currently supports apps that are built for Android,
iOS, and Apple's other platforms, web, Flutter, C++.
And Unity.
So if I'm developing the same application
for different platforms, I should
register all the platform variants in the same Firebase
project.
But if I'm developing entirely different applications
with different use cases and different users in mind,
I should create two separate Firebase projects--
one for each application.
One icon, one Firebase project.
If you're interested in learning more about Firebase projects
and apps, there is a series of videos on the Firebase YouTube
Channel called How Firebase Projects Work.
So definitely check that out.
The links are available in the description.
OK, here's what I'm going to do next.
I will create a Firebase project,
register a new Android app to it,
configure the code base to connect to Firebase,
and add the first Firebase service to it.
If you want to follow along with this video,
now, is the time to open your Android
project in Android Studio.
I'll wait.
Ready?
There are two different ways to create a Firebase project
and configure an Android app-- via the Firebase Console
or via the Android Studio.
The steps to do everything via Android Studio
are very straightforward.
And you can see them in this YouTube short
and in the official documentation linked
the video description.
Today, I want to take the time to walk you
through the Firebase Console steps.
When I go to the Firebase Console URL,
there is an option to add a new Firebase project.
The next screen will prompt me to create
a name for this project.
As I said before, a Firebase project
is also a Google Cloud project.
So if I already have a Google Cloud project to which I would
like to add Firebase services, I should select it
from this dropdown instead of creating a new one.
I'll create a new project.
So I need to type the project's name.
Note that there is a string that updates automatically
as I type the name for my brand new project.
This is the project ID.
It uniquely identifies my project
across Firebase and Google Cloud.
It's helpful if I remember my project ID.
But I can pretty easily find it again if I don't.
The next screen, we will allow me
to enable Google Analytics for my Firebase project.
This is optional, but it's highly recommended
since it allows me to use many useful features,
like the ones I see here on this screen.
It enables targeting, reporting, and more
in Firebase Crashlytics called messaging, in-app messaging,
Remote Config, A/B testing, and called functions.
I will enable analytics because I really
want to try these features.
So the next step is associating my new Firebase project
with a Google Analytics account.
A Google Analytics account is the access point for analytics.
And it can have one or more analytics properties.
Each property is linked to a different Firebase project.
I can select an existing Google Analytics account to use,
or I can select Create a New Account.
To create a new account, first, I
need to choose a Google Analytics account name.
Then I need to choose the analytics location.
This represents the region of my organization.
So I'll select United Kingdom.
I can also modify the data share settings according to my needs.
I'll just use the default settings
and accept the Google Analytics terms.
The last thing I need to do is click on the Create project
button.
Now, Firebase will take care of creating the Google Analytics
account and setting up the Firebase project for me.
Once it's finished, I can click on the Continue button,
and it will take me to the Project Overview
page of the Console.
All right, now that I have a new Firebase project,
I need to register my Android app so I can prepare my code
base to use Firebase services.
To do so, I will click on the Android icon
that I see in the Project Overview page.
To register an Android app with my Firebase project,
I need to know the package name of my app.
And I can get this information in the Android Studio.
I just need to open the app level Gradle file and search
for the application ID keyword.
I can copy this ID and paste it inside the Android package name
field in the Firebase Console.
This is the only mandatory information
that I need to input when registering an Android
app in a Firebase project.
But there are a couple of optional fields
that I want to explain before we move forward.
The first one is called App Nickname.
This is the internal only name that
will display in several places throughout the Firebase
Console.
And it can help me to identify my app easily in the console.
I'm going to combine Android Logging App.
The second optional field asks for a SHA-1 hash
of debug signing certificate so I can prove
that my app belongs to me.
The SHA-1 hash is required by Firebase authentication
when using Google sign in or phone authentication
and by Firebase Dynamic Links.
Firebase recommends filling in this information
only when necessary because I can't have the same combination
of SHA-1 hash and Android package name in more
than one Firebase project.
Right now, I'm not planning on using any of the services that
require SHA-1.
So I'm just going to leave this field blank
and click on the Register app button.
Next, Firebase will give me a file called
google-services.json.
This is the Firebase configuration file
for my Android app.
I need to download this file and move it
into the app module root directory of my Android
project.
If I open this file, I can see that it contains
a lot of configuration values.
A Firebase SDK needs these values
in order to configure itself and to connect my Android
app to my Firebase project and the app
that I registered with my Firebase project.
None of this information is secret.
And I need to include it when I build my production
app since the config values are required for my app
to access Firebase services.
But here goes another tip for you.
If you want to share your code as an open-source project,
you probably want each person who
uses your code to create and configure their own Firebase
project and use their own Firebase config file.
It's probably a good idea to not include this file
in your version control system and instead,
include some instructions on how to set up
a Firebase project in your projects readme file
or link to this video.
Once I download and add the JSON file to my Android project,
I can click Next and see the steps to add the Firebase
SDK to my code base.
To make the google-services.json config values accessible
to Firebase SDKs, I need to add the Google services greater
plugin.
Since I'm using a version of Android Gradle plugin higher
than 7.1, I need to add the Google services
plugin to my build.gradle file under the plugins block.
But if you are using another version of the Android Gradle
plugin, then you need to add it under the build script block.
Next in my app level Gradle file,
I'll add the Google-services plugin.
Now, I'm ready to add the dependencies for the Firebase
products that I want to use in my app.
I can do it by adding the libraries
to the dependencies list and specifying the libraries
versions.
But there's actually a better way
to do it using the Firebase Bill of Materials.
This Bill of Materials, or BoM for short,
enables me to manage the versions of all
the Firebase libraries that I want to use in my app
by specifying only the BoM version.
And the BoM will automatically pull all the individual library
versions that work well together.
For this simple app, our dependency
and specify the most recent version of the BoM
and now, whether dependency for Google Analytics.
But I will not specify the analytics version.
The BoM will do that for me behind the scenes.
Depending on when you watch this video,
the BoM version may already be higher.
So check out the release notes link
to the description for the most recent version.
After adding a desired SD case, I
need to sync my Android project with Gradle files.
And then back in the Firebase Console, I click Next, then
continue to Console to close out the workflow
for registering my Android app with my Firebase project.
If I followed all the steps correctly,
I should be able to build, install, and run
my Android app in this device emulator
back in Android Studio.
There it is.
My simple app is up and running.
If I search for Firebase in the Logcat,
I can see this line here confirming
that the installation worked and that my application is now
accessing Firebase and ready to use Google Analytics.
Since I'm already here, and I've enabled Google Analytics when
setting up the Firebase project, I
will now add a basic analytics code
to know when the user clicked on the Log button in my app.
I'll navigate to this method that
is called when the user clicks on the button, unlock button
click, and now called Firebase.Analytics.logevent.
I need to press the name of the button click event, which
I'll call log_button_click.
I can also optionally pass a map of event parameters.
But for this case, I'll leave it as no
since there isn't anything else I would
like to know about this event.
Now, I can run the app and click on the Log button.
To make sure it worked, I can either
search for analytics in the Logcat as I see here.
Or I can open the Firebase project in the Firebase Console
and click on Analytics DebugView.
All events logged by the apps registered in this Firebase
project should appear here.
This was just a quick and simple example of analytics usage.
But there's much, much more I can do.
I can use analytics with Remote Config to personalize my app.
I can run A/B testing.
I can even send push notifications
with Firebase, Cloud Messaging that
target different audiences that I create with Google Analytics.
And analytics is only one of the many Firebase products
that I can use in my Android app.
For example, I can use Firestore and NoSQL database
to store structured data in the cloud.
I can improve the quality of my app
by monitoring crashes and performance
issues with Crashlytics and Performance Monitoring.
The list of what I can do with Firebase when
building, releasing, and growing my app is huge.
So if you're interested in learning more
about Firebase products and about how
to get started using Firebase with other platforms
such as iOS and Apple's other platforms or Web,
Unity, even Flutter, subscribe to our YouTube channel
and have fun creating.
[MUSIC PLAYING]