0
0
Android Kotlinmobile~5 mins

Firebase project setup in Android Kotlin

Choose your learning style9 modes available
Introduction

Firebase helps your app store data, authenticate users, and more. Setting it up connects your app to these services easily.

You want to save user data in the cloud without building your own server.
You need to add user login with email or social accounts.
You want to send notifications to your app users.
You want to track app usage and errors automatically.
You want to add real-time features like chat or live updates.
Syntax
Android Kotlin
1. Go to https://console.firebase.google.com/ and create a new project.
2. Add an Android app to the project by entering your app's package name.
3. Download the google-services.json file and place it in your app/ folder.
4. In your project-level build.gradle, add:
   classpath 'com.google.gms:google-services:4.3.15'
5. In your app-level build.gradle, add:
   apply plugin: 'com.google.gms.google-services'
   implementation platform('com.google.firebase:firebase-bom:32.1.1')
   implementation 'com.google.firebase:firebase-analytics-ktx'
6. Sync your project with Gradle files.

The package name must match exactly what you use in your AndroidManifest.xml.

Always place google-services.json in the app/ folder, not the root.

Examples
Add this in the build.gradle file at the project level inside dependencies.
Android Kotlin
classpath 'com.google.gms:google-services:4.3.15'
Add these lines in the app-level build.gradle to enable Firebase services.
Android Kotlin
apply plugin: 'com.google.gms.google-services'
implementation platform('com.google.firebase:firebase-bom:32.1.1')
implementation 'com.google.firebase:firebase-analytics-ktx'
Sample App

This simple activity initializes Firebase Analytics to confirm the Firebase setup works.

Android Kotlin
/* No Kotlin code needed for setup, but here is a minimal MainActivity to confirm setup */

package com.example.myfirebaseapp

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.analytics.FirebaseAnalytics

class MainActivity : AppCompatActivity() {
    private lateinit var firebaseAnalytics: FirebaseAnalytics

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(android.R.layout.simple_list_item_1)

        // Initialize Firebase Analytics instance
        firebaseAnalytics = FirebaseAnalytics.getInstance(this)
    }
}
OutputSuccess
Important Notes

Make sure to sync Gradle after adding Firebase dependencies to avoid build errors.

Check the Firebase console to see if your app connects successfully after running.

Summary

Firebase setup connects your Android app to powerful cloud services.

Download and add google-services.json to your app folder.

Add Firebase plugins and dependencies in Gradle files and sync your project.