Getting Started With Firebase on Android

DZone 's Guide to

Getting Started With Firebase on Android

Stop, drop, and code! This walkthrough will help you set up an Android project to make use of the Firebase platform.

· Mobile Zone ·
Free Resource

Architecting an Android application is a fascinating task. Besides designing how the component of your app will interact, you need to take care of the common functionality that any application needs. Typically that includes the requirements that span across the functionality, like logging, managing users and credentials, metrics and analytics, and so on. Since you cannot always store all the necessary data on the device, you’ll need to think about some sort of a backend service.

However, when you are just starting a project you might be reluctant to build a backend service from scratch. One option you should consider is Firebase. A platform that contains a lot of the common functionality for you and offers features like notifications, file storage, analytics, remote config and so one as a service. At the moment it has eleven services that help you to develop, grow and earn money from your applications.

In this getting started with Firebase on Android post I’ll walk you through setting up an Android project to make use of the Firebase platform. We make our project implement its application analytics and send events to the Firebase console.

How to Register a Project

The first thing to do is to create a project in the Firebase console. It requires a name and a country. The country selection will not determine where your data will be kept, but rather specifies the country your company belongs to, influencing the currency and such. For instance, if you choose UK, the dialog will emit a little giggle and provide you with an ever falling Sterling option as default.

You can add multiple iOS, Android, or web apps to a single project. The project will tie together all your stats and metrics from different apps. You’ll see all the data about the users, the crash reporting incidents all in one view on the project dashboard.

Image title

Adding an Android Application

For the purposes of this post, I will use our regular sample application Retrofit2SampleApp. In case you’re not familiar with it, the application is pretty small was created for the getting started with Retrofit 2 post. The blog post teaches you how to query the Internet from your Android application; check it out! Now, however, it’s time to add the functionality for sending metrics to the Firebase dashboard.

First, we need to configure our Firebase project to know about our applications. To do that you will first need to provide the package name. This is available in your build.gradle as the applicationId property. You can optionally add the SHA-1 of the signing certificate of the debug keystore. It won’t be needed in the scope of this post, but I’m mentioning it in case you ever need to use it. Google has a good quick guide on how to do it here.Image title
Once we have added the app to the Firebase project, it will generate a google-services.json file that should be copied to the root of your app module. A quick peek at it shows loads of project metadata — such as API keys, project id, and so on. This approach is quite elegant, it saves you from the trouble of having to manually provide all that data in the code.

Image title

Specifying the Firebase Dependencies

Firebase has done a good job in terms of splitting the services into different libraries. There’s no need to include any unnecessary code in your project. Here is the full list of Firebase libraries that are available for you to make use of.

Let’s head over to our project’s build.gradle and add the following lines:

We need to add the following Firebase dependencies to the application’s build.gradle file:

dependencies {
    // ...
    compile 'com.google.firebase:firebase-core:9.0.2'

And also apply the plugin:

apply plugin: 'com.google.gms.google-services'

Image title

That’s it! We’re all set and ready to make use of the Firebase platform to handle our analytics data.

How to Send Firebase Analytics Events

Sending analytics events is really straightforward. All we need to do at the code level is acquire an instance of FirebaseAnalytics, create a Bundle with key value pairs of the properties we want to record and call the logEvent() method.

In our sample application which just queries the Github repositories metadata, I’m going to fire the analytics event with the search queries that the user has specified.

Firebase has conveniently provided us with a number of events and property keys in theFirebaseAnalytics.Event and FirebaseAnalytics.Param classes. If those don’t exactly scratch your troublesome itch, you can create your own custom events.

In my example application I use the following code snippet to send my search query keywords:

Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.SEARCH_TERM, repositorySearchKeyword + "&" + companySearchKeyword);
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SEARCH, bundle);

To verify our results, we can check the Firebase console for analytics, but it will take up to 24 hours before the events will be shown. Since that is not really convenient during development and debugging, here’s another option to verify the correct metrics are recorded.

Running the following adb commands:

adb shell setprop log.tag.FA VERBOSE
adb shell setprop log.tag.FA-SVC VERBOSE
adb logcat -v time -s FA FA-SVC

Now after running the adb commands and clicking the “search” button.

Image title

I now see the following output in adb logcat:

06-28 11:26:06.630 V/FA-SVC  ( 1734): Event recorded: Event{appId='org.zeroturnaround.githubcontributors', name='search', params=Bundle[{search_term=retrofit&square, _o=app}]}

Now, you can instrument all the essential actions in your application using analytics events and see exactly what your users are doing, the actions they are using most frequently and where you should focus your development efforts.


In this post, I tried to give you a quick summary of how to get started with Firebase. I found it to be really easy to set up and get started writing code. I definitely recommend taking a look at the different services Firebase provides. Two services that most interest me are the Firebase cloud database and remote notification services. I’d be interested in hearing which Firebase features most intrigue you. What do you see using in your projects and which parts do you think are easier to implement yourself? Ping me on Twitter: @stensuitsev and I’d be ecstatic to chat to you about it.

Also, if you want to become a happier Android developer, check out JRebel for Android, the tool to instantly update code and resources of your running app. I’m involved with its development, so I welcome any feedback!

Back to the Firebase now. Whether you decide to build your own backend service or start using Firebase is an important business decision. Firebase comes with a flexible pricing and can help you rapidly prototype and test your product during the early stages.

android, firebase, getting started

Published at DZone with permission of Sten Suitsev . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}