Firebase Firestore Setup Guide

To use Firebase Firestore in an Android app, you need to configure your Firebase project and Gradle settings within your app. This guide focuses on the key configurations required at the Firebase project level.


1. Create Firebase Project and Register App

Access the Firebase Console to create a new project or select an existing one, then register your Android app.

  1. Access Firebase Console: Go to Firebase Console to create a new project or select an existing one.
  2. Add Android App: Add your Android app to the project. During this process, you will need to download the google-services.json file and place it in your Android project's app module directory. This file contains the necessary configuration information for your app to communicate with Firebase.

2. Create Firestore Database

Create a Firestore database within your Firebase project and set up initial security rules.

  1. Navigate to Firestore Database: In the Firebase Console, go to Build > Firestore Database from the left menu.
  2. Create Database: Click the Create database button.
  3. Choose Security Rules: You can choose between two modes:
    • Start in production mode: This mode defaults to denying all read/write access, so you must carefully configure Firestore security rules for actual service.
    • Start in test mode: This mode allows all client read/write access for quick development in the early stages. However, since this mode exposes your data to all users, it should never be used in a production environment.

    Since the current code uses anonymous authentication and the Firestore security rules use the request.auth != null condition, you need to set rules that allow read/write access.


3. Configure Firestore Security Rules

Configure security rules to allow your app to store user-specific data in artifacts/{appId}/users/{userId}/{your_collection_name} and public data in artifacts/{appId}/public/data/{your_collection_name}.

  1. Navigate to Rules Tab: In the Firebase Console's Firestore Database section, go to the Rules tab.
  2. Write Security Rules: Set up your security rules as shown in the following code snippet:
    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        // Private data (user-specific)
        match /artifacts/{appId}/users/{userId}/{document=**} {
          allow read, write: if request.auth != null && request.auth.uid == userId;
        }
        // Public data (shared among authenticated users)
        match /artifacts/{appId}/public/data/{collection}/{document=**} {
          allow read, write: if request.auth != null;
        }
      }
    }
    

    These rules ensure that authenticated users (including anonymous users) have read/write access only to their own data (private data), and all authenticated users have read/write access to shared data.


4. Configure Firebase Authentication

Since your app uses anonymous authentication, you must enable the Anonymous provider in Firebase Authentication.

  1. Navigate to Authentication: In the Firebase Console, go to Build > Authentication from the left menu.
  2. Go to Sign-in method tab: Click Get started and go to the Sign-in method tab.
  3. Enable Anonymous provider: You must enable the Anonymous provider.

Applying these configurations to your Firebase project will enable your app to seamlessly use the Firestore database.