Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Firebase offers a suite of cloud-based services that help developers to develop high-quality apps, grow their user base, and earn more profit. Adding Firebase to an Android app can be broken down into a series of steps:
Add project
.Android
icon to add an Android app to your project.Register app
.google-services.json
file.build.gradle
file (<project>/<project-name>/build.gradle
):Add the following to the dependencies
section:
classpath 'com.google.gms:google-services:4.3.8' // Check for the latest version
build.gradle
file (<project>/<project-name>/app/build.gradle
):Add the following at the bottom of the file:
apply plugin: 'com.google.gms.google-services'
build.gradle
:implementation 'com.google.firebase:firebase-auth:21.0.1' // Check for the latest version
If you want to add Firestore:
implementation 'com.google.firebase:firebase-firestore:23.0.3' // Check for the latest version
Remember to always check for the latest versions of these dependencies.
In your main Application
class or the main activity, initialize Firebase:
import com.google.firebase.FirebaseApp; public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); FirebaseApp.initializeApp(this); } }
If you're initializing in the main activity, replace Application
with Activity
and MyApplication
with your activity name.
That's it! You've now successfully added Firebase to your Android app. Depending on the Firebase services you wish to use, you'll have different steps for initialization and usage. Always refer to the official Firebase documentation for detailed and up-to-date instructions.
Firebase authentication in Android example:
Firebase Authentication provides simple authentication using various methods. Here's an example of email and password authentication:
// MainActivity.java FirebaseAuth mAuth = FirebaseAuth.getInstance(); // ... // Example of email and password authentication mAuth.createUserWithEmailAndPassword("user@example.com", "password123") .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { // Authentication successful FirebaseUser user = mAuth.getCurrentUser(); } else { // Authentication failed Toast.makeText(MainActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } } });
Firebase Authentication with Email and Password Android:
We already covered email and password authentication in the first example. To sign in an existing user:
mAuth.signInWithEmailAndPassword("user@example.com", "password123") .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { // Sign-in successful FirebaseUser user = mAuth.getCurrentUser(); } else { // If sign in fails, display a message to the user. Toast.makeText(MainActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } } });