This simple project demonstrates how to integrate Firebase Authentication into an Android app and configure it to use the Firebase Emulator Suite for local development and testing.
- Overview
- Prerequisites
- Project Setup
- Configure Firebase Emulator Suite
- Authentication Setup
- Testing Authentication with Emulator
- Running the Project
This project showcases how to set up Firebase Authentication in an Android project using the Firebase Emulator Suite for local testing. The Emulator Suite allows testing Firebase functionalities locally without interacting with live production data.
Before setting up Firebase Authentication, make sure you have the following installed:
- Android Studio
- Node.js (for running Firebase Emulator)
- Firebase CLI (v9.0.0 or later)
- A Google Firebase project
-
Add Firebase SDK to Android Project
-
Open your project-level
build.gradle
file and ensure you have the following classpath for Firebase:dependencies { classpath 'com.google.gms:google-services:4.3.15' }
-
In your app-level
build.gradle
file, add Firebase Authentication dependency:dependencies { implementation 'com.google.firebase:firebase-auth:22.1.1' }
-
Apply the Firebase plugin at the bottom of the
build.gradle
file:apply plugin: 'com.google.gms.google-services'
-
-
Add Firebase Configuration File
- Download the
google-services.json
file from the Firebase Console. - Place the file in your project’s
app
directory.
- Download the
-
Firebase CLI Setup
-
Install Firebase CLI by running the following command:
npm install -g firebase-tools
-
Log in to Firebase:
firebase login
-
-
Initialize Firebase Emulator
-
Open your terminal in the root directory of the project and run:
firebase init emulators
-
Select
Authentication
and configure the port (default is9099
).
-
-
Emulator Configuration
-
After initialization, modify the
firebase.json
configuration file to add the authentication emulator:{ "emulators": { "auth": { "port": 9099 } } }
-
-
Start Firebase Emulator
-
Start the Firebase Emulator Suite by running:
firebase emulators:start
-
-
Connect Firebase to Emulator in Android
-
In your Android project, configure Firebase Authentication to use the emulator by adding the following code to your initialization logic:
val firebaseAuth = FirebaseAuth.getInstance() if (BuildConfig.DEBUG) { firebaseAuth.useEmulator("10.0.2.2", 9099) // For Android Emulator }
Note: Use
10.0.2.2
to access the host machine's localhost when running the Android app in the emulator.
-
-
Sign-In Methods
- Go to your Firebase Console, navigate to Authentication → Sign-In Methods.
- Enable the desired sign-in methods (e.g., Email/Password, Google, etc.).
-
Sample Authentication Code
-
Here is an example of how to authenticate a user using email and password:
val email = "user@example.com" val password = "yourpassword" firebaseAuth.signInWithEmailAndPassword(email, password) .addOnCompleteListener { task -> if (task.isSuccessful) { // Authentication succeeded val user = firebaseAuth.currentUser println("User signed in: ${user?.email}") } else { // Authentication failed println("Sign-in failed: ${task.exception?.message}") } }
-
-
Using the Emulator
-
When running your Android app, ensure the Firebase Emulator is active by starting it with the command:
firebase emulators:start
-
-
Test Sign-In and Sign-Up
- Use your app to perform sign-in or sign-up with the email/password flow. The data will be stored in the Emulator Suite and not in the actual Firebase backend.
-
View Emulator Suite Dashboard
To run the project with Firebase Authentication connected to the Emulator Suite:
- Ensure the Firebase Emulator Suite is running.
- Launch your Android project from Android Studio.
- Sign in with a test account or create a new one. Test accounts will be stored locally in the emulator environment.