Lesson Weekend

Note: If you receive Gradle Sync errors, verify whether your SDK version requires updating. Please note that the update process may take additional time.

In this lesson we'll learn how to create a Firebase database, install and configure the Firebase client into our Android application, and connect our app to its database in the cloud! In subsequent lessons we'll address how to read, write and delete Firebase entries.

Firebase Accounts and Applications

First off, in order to use Firebase services we'll need to create a free Firebase account. If you already have one from JavaScript unit, you can use the same account.

Once registered, we will be taken to a dashboard reading "Welcome to the new Firebase Console". Click Create new project to make a new Firebase app:

create-firebase-app1

In the modal window, enter the name of the app, MyRestaurants, and click Create Project.

create-firebase-app2

We will be taken to our app's overview page. Select the Add Firebase to your Android App button from the top panel.

add-firebase-to-app

In the modal popup window, enter your app's package name and the SHA-1 Key. If you don't know how to create an SHA-1 key, click on the question mark icon to learn how to generate the debug key. If you are on a windows machine and find the instructions don't lead to your success, check this thread on StackOverflow The password for the Android debug-keystore is android by default: for both windows and mac.

add-firebase-to-app2

Click Add App . Another modal window will appear, and a file called google-services.json will begin downloading:

google-json-downloading-plus-modal

Follow the instructions depicted in the modal, and copy the google-services.json file into your app module root directory. Then, hit Continue.

One last modal will appear containing instructions for adding the necessary Google services plugins to both your project-level and app-level build.gradle files. Follow these instructions to add the necessary dependencies.

We'll add the google services classpath to our project-level build.gradle file. The class path tells JDK tools and applications where to find third-party and user-defined classes -- that is, classes that are not Java extensions or part of the Java platform.

build.gradle (Project: MyRestaurants)

...
    dependencies {
        ...
        classpath 'com.google.gms:google-services:3.0.0'
    }
...

Our project-level build.gradle file should now look like this:

build.gradle (Project: MyRestaurants)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

We'll need to add the Firebase-database dependency in order to utilize the realtime database service:

build.gradle (Module: app)
...
dependencies {
    ...
    compile 'com.google.firebase:firebase-database:9.2.1'
}
...

We'll also need to add the google-services plugin to the bottom of your app-level build.gradle file. Your build.gradle (Module: app) should now look like this:

build.gradle (Module: app)
apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "23.0.3"
    useLibrary 'org.apache.http.legacy'

    repositories {
        mavenCentral()
        jcenter()
    }

    defaultConfig {
        applicationId "com.epicodus.myrestaurants"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    buildTypes.each {
        it.buildConfigField 'String', 'YELP_TOKEN', YelpToken
    }

}

apply plugin: 'com.neenbedankt.android-apt'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'org.robolectric:robolectric:3.0'
    testCompile 'org.robolectric:shadows-support-v4:3.0'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.squareup.picasso:picasso:2.5.2'

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }

    androidTestCompile('com.android.support.test:runner:0.3') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }

    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'se.akerfeldt:okhttp-signpost:1.1.0'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'org.parceler:parceler-api:1.1.1'
    apt 'org.parceler:parceler:1.1.1'
    compile 'com.google.firebase:firebase-database:9.2.1'
    compile 'com.android.support:support-v4:25.3.1'
}

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

Make sure to sync your project with Gradle before continuing.

Permissions

Because our Firebase database exists online, we also need to ensure our application has internet access to communicate with our database. To do this, we need to add internet permissions throughout the app.

We've already added these permissions in MyRestaurants when we configured it to use OkHttp. As a refresher, adding internet permissions is done by placing the following code in your AndroidManifest:

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.epicodus.myrestaurants">

        <uses-permission android:name="android.permission.INTERNET" /> 
        ...

Packaging Options

Each time Gradle builds our project it creates meta files for each SDK included in the dependencies. These files don't contain code our application needs to operate. They actually hold information like legal notices, license information, dependency information, etc. When we use multiple third party libraries they may each have meta files with the same name (especially common names like license.txt or notice.txt). Unfortunately, these duplicate files can create issues during the build process.

To avoid this, let's exclude these files from our build by adding the following packaging options to build.gradle :

build.gradle Module:app
apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "23.0.3"
    useLibrary 'org.apache.http.legacy'

    repositories {
        mavenCentral()
        jcenter()
    }

    defaultConfig {
        applicationId "com.epicodus.myrestaurants"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/notice.txt'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
...

Update Google Play Services

Before running your app, check the SDK Manager to ensure you have the latest version of Google Play Services installed. Finally, run your app to make sure you don't receive errors.

Additional Resources

For more information, check out the Add Firebase to your Android Project portion of Firebase's Android documentation.


Example GitHub Repo for MyRestaurants

Terminology


  • Firebase is a cloud services provider that offers realtime databases.

  • Cloud services are simply any resource provided over the internet.

Tips


  • The password for the Android debug-keystore is in Firebase is android by default.

Examples


Example GitHub Repo for MyRestaurants

Additional Resources