Chapter 10 Firebase

Googles Firebase Service (https://console.firebase.google.com/) will provide authentication services and service to manage data.

Now we follow option 1 in https://firebase.google.com/docs/android/setup?hl=de#console.

Step 1: Create a Firebase Project

Now we follow option 1 in https://firebase.google.com/docs/android/setup?hl=de#console.

Navigate to the console and select Create a project. As Project name select Pluto25. Then press continue. Accept Google Analytics, select Default Account for Firebase and complete the process. Finally you should see a card like this:

The Backend Project in Firebase
The Backend Project in Firebase

Step 2: Register app with Firebase

This brings you the following screen:

Download google-services.json
Download google-services.json

Now download and add google-services.json {-} ans store the file in the directory shown in the figure above.

Step 3: Add Firebase SDK

Changes in root level gradle-file {-}

Add the line after the comment to the root-level gradle-file

plugins {
    alias(libs.plugins.android.application) apply false
    
    // This file is added during connection with Firebase
    id("com.google.gms.google-services") version "4.4.2" apply false  
}

Changes in the app level gradle-file

Add the line marked with “// <—“

plugins {
    alias(libs.plugins.android.application)
    id("com.google.gms.google-services") // <--
}

Scroll down to the dependencies section and again add the lines marked with // <—

dependencies {
    implementation(platform("com.google.firebase:firebase-bom:33.5.1"))  // <--
    implementation("com.google.firebase:firebase-analytics")             // <--
    implementation("com.google.firebase:firebase-auth")                  // <-- Needed for auth

This is the final app level gradle-file:

plugins {
    alias(libs.plugins.android.application)
    id("com.google.gms.google-services") // <----
}

android {
    namespace = "de.hawlandshut.pluto25"
    compileSdk = 34

    defaultConfig {
        applicationId = "de.hawlandshut.pluto25"
        minSdk = 25
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation(platform("com.google.firebase:firebase-bom:33.5.1"))  // <--
    implementation("com.google.firebase:firebase-analytics")             // <--
    implementation("com.google.firebase:firebase-auth")                  // <-- Needed for auth
    implementation(libs.appcompat)
    implementation(libs.material)
    implementation(libs.activity)
    implementation(libs.constraintlayout)
    testImplementation(libs.junit)
    androidTestImplementation(libs.ext.junit)
    androidTestImplementation(libs.espresso.core)
}

Step 4: Sync your project

You should not receive any errors after the sync. Add the following declaration in ActivityMain.java and make sure that the build runs smooth.

FirebaseAuth mAuth;