Firebase Tutorial for Android: Getting Started

In this Firebase Tutorial for Android you’ll learn how to work with Realtime Databases and Authentication by creating a Joke Telling app. By Filip Babić.

Leave a rating/review
Download materials
Save for later
Share

Nearly all projects we work on every day use a REST service. It’s a concept where you use a certain set of methods (GET, POST, DELETE…) to send data, receive data or both.

And REST is great, but sometimes you just can’t find a (back)end to all the challenges! More often than not, the variable names are in a different style than you prefer or the paths to the requests are confusing…

Then comes Google, introducing Google Firebase and saying: “Why would you work with other people when you can write a backend of your own?” And you start thinking: “IS THAT POSSIBLE??”, suddenly you’re inspired. You cannot keep your mind straight, and all you want to do is create backends all day.

Firebase works similar to hosting, so to speak. You pick what services you like, connect them to your application and bam, you’re ready to take over the world. Firebase’s backend is just another service. It provides a JSON like database in which you store and read data. It doesn’t really follow the REST standard as it has no REST methods (GET, POST…). Here, you’re working directly with the database.

You won’t be using all of Firebase in this tutorial. Instead, we’ll focus on Firebase’s Realtime Database to create a mobile friendly backend. Your goal for this tutorial is to build a joke-sharing social network application, using the Authentication and Realtime Database services. So let’s heat it up. :]

Note: This Firebase tutorial assumes that you’re familiar with the basics of Android development and REST services. If you’re new to Android, please check out the Beginner Android series and other Android tutorials.

Note: This Firebase tutorial assumes that you’re familiar with the basics of Android development and REST services. If you’re new to Android, please check out the Beginner Android series and other Android tutorials.

Getting started

Before doing anything, download the starter and final projects by clicking on the Download Materials button at the top or the bottom of the tutorial. Once you’ve downloaded the starter project, open it up in Android Studio 3.1.1 or later so we can do an overview of the materials to get you caught up.

Most of the code is already pre-baked for you. It’s better to focus entirely on Firebase in the tutorial, rather than the rest of the application. Don’t worry, you’ll be guided through everything in the starter project.

Tasting the pre-baked goodies

A picture showing the project structure: common, di, firebase, model, presentation and ui packages.

The project setup is simple.

Note: If you want to learn more about Dagger and how it works, Ray Wenderlich has you covered. Checkout the this tutorial to learn more about Dagger with Kotlin.

  • The common package contains all the extensions and global functions you’ll use for showing errors, validating data and handling the UI events like clicks.
  • The di package contains the Dependecy Injection setup. I’ve chosen Dagger because it’s the standard and most people will feel at home using it. If you’re looking for a Kotlin alternative to Dagger, check out Koin. It’s simpler than Dagger, but still not at its 1.0 version milestone.

    Note: If you want to learn more about Dagger and how it works, Ray Wenderlich has you covered. Checkout the this tutorial to learn more about Dagger with Kotlin.

  • The ui package contains all the Activities and Fragments you’ll use. And the model package has model classes for the app data.

Note: If you want to learn more about Dagger and how it works, Ray Wenderlich has you covered. Checkout the this tutorial to learn more about Dagger with Kotlin.

The presentation and firebase packages are the ones you’ll be working with. The projects uses an MVP pattern for the application. Splitting the project with this package structure allows you to focus on the Firebase implementation in this tutorial instead of getting lost in the pre-baked code.

A hot package

There are two parts of the Firebase implementation you’ll be finishing up, the FirebaseAuthenticationManager and the FirebaseDatabaseManager. Both are in subpackages of the firebase package.

The former will take care of all-things-user, like logging a user in or out and registering a new user. The latter will be your man(ager) in the middle, and will read data from the database and store new data when it comes in.

Authentication will be done using an email and a password. Reading the data on the other hand will be done multiple ways. You’ll listen for single data events, for individual updates and for general updates. Don’t worry if that doesn’t make sense, it’ll be explained in further sections. :]

Rev it up!

In order to start working, you need to add Firebase specific dependencies. However, to be able to add them you also need something called a google-services.json file. It’s a JSON file which contains all the configuration data Firebase uses internally. To get it, you need a Firebase project, let’s create one!

Visit the Firebase website. You should see something similar to this:

Firebase landing page, with an overview of the site and services

Login using a Google account if you haven’t already and if you want, take a quick tour around the landing page. When ready, click the Go to Console button in the upper right. You’ll now create a new Firebase project. You can do it by clicking on the Add project card like below:

Add project card

Choose your project name. In this tutorial you’ll use the name “Why so serious”, and choose your current country like so:

Firebase Add project window with the Why so serious project name

Accept any terms if needed and click Create Project. After finishing the first step, you should see a success message:

Firebase project created success message

Click Continue.

You should see the project dashboard now. Next you need to add an Android application to the project, so click on the Add Firebase to your Android app card:

Firebase dashboard with three options, adding an iOS, Android or a Web app

Fill in the package name com.raywenderlich.android.whysoserious from the starter project and click Register app. The SHA key can be empty for now, you need it only when signing an APK.

Now follow the instructions on the Firebase page to add the google-services.json file to the project and click Next.

Add the required library dependencies to your app module build.gradle file:

ext {
  //Add the playServices version
  playServices = "15.0.0"
}
dependencies {
  //Add the following lines
  implementation "com.google.firebase:firebase-core:$playServices"
  implementation "com.google.firebase:firebase-auth:$playServices"
  implementation "com.google.firebase:firebase-database:$playServices"
}
//Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'

And add the following to the project-level build.gradle:

buildscript {
  dependencies {
    //Add this line
    classpath 'com.google.gms:google-services:3.3.1'
  }
}

Hit Sync Now to sync your Gradle files.

Finally you can Build and Run the project! You should see something like this:

A welcome screen with a greeting message, a login and a register button.

Woohoo! Now you can start working on the juicy parts of this app.