Architecting an Android application is a fascinating task. Besides designing how the component of you app will interact, you need to take care of the common functionality that any application needs. Typically that includes the requirements that go 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.

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.

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.

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:

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 the FirebaseAnalytics.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:

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.

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.

Summary

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.

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.

Sten Suitsev is JRebel for Android Product Manager. He started out as a mobile developer for Android during his studies at Tartu University. Following that, he took on the product owner role in different iOS, Android and Windows Phone projects. Today, he is looking to improve the life of all Android developers. In his free time he can be found fishing or playing soccer.