# Cloud Firestore on Android (Kotlin) This guide walks you through using Cloud Firestore in your Android app using Kotlin. ### Enable Firestore via CLI Before adding dependencies in your app, make sure you enable the Firestore service in your Firebase Project using the Firebase CLI: ```bash npx -y firebase-tools@latest init firestore ``` ______________________________________________________________________ ### 1. Add Dependencies In your module-level `build.gradle.kts` (usually `app/build.gradle.kts`), add the dependency for Cloud Firestore: ```kotlin dependencies { // [AGENT] Fetch the latest available BoM version from https://firebase.google.com/support/release-notes/android before adding this implementation(platform("com.google.firebase:firebase-bom:")) // Add the dependency for the Cloud Firestore library // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-firestore") } ``` ______________________________________________________________________ ### 2. Initialize Firestore In your Activity or Fragment, initialize the `FirebaseFirestore` instance: ```kotlin import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.ktx.firestore import com.google.firebase.ktx.Firebase class MainActivity : AppCompatActivity() { private lateinit var db: FirebaseFirestore override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val db = Firebase.firestore setContent { MaterialTheme { Text("Firestore initialized!") } } } } ``` #### Jetpack Compose (Modern) Initialize inside a `ComponentActivity` using `setContent`: ```kotlin import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import com.google.firebase.Firebase import com.google.firebase.firestore.firestore class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val db = Firebase.firestore setContent { MaterialTheme { Text("Firestore initialized!") } } } } ``` ______________________________________________________________________ ### 3. Add Data Add a new document with a generated ID using `add()`: ```kotlin // Create a new user with a first and last name val user = hashMapOf( "first" to "Ada", "last" to "Lovelace", "born" to 1815 ) // Add a new document with a generated ID db.collection("users") .add(user) .addOnSuccessListener { documentReference -> Log.d(TAG, "DocumentSnapshot added with ID: ${documentReference.id}") } .addOnFailureListener { e -> Log.w(TAG, "Error adding document", e) } ``` Or set a document with a specific ID using `set()`: ```kotlin val city = hashMapOf( "name" to "Los Angeles", "state" to "CA", "country" to "USA" ) db.collection("cities").document("LA") .set(city) .addOnSuccessListener { Log.d(TAG, "DocumentSnapshot successfully written!") } .addOnFailureListener { e -> Log.w(TAG, "Error writing document", e) } ``` ______________________________________________________________________ ### 4. Read Data Read a single document using `get()`: ```kotlin val docRef = db.collection("cities").document("SF") docRef.get() .addOnSuccessListener { document -> if (document != null && document.exists()) { Log.d(TAG, "DocumentSnapshot data: ${document.data}") } else { Log.d(TAG, "No such document") } } .addOnFailureListener { exception -> Log.d(TAG, "get failed with ", exception) } ``` Read multiple documents using a query: ```kotlin db.collection("cities") .whereEqualTo("capital", true) .get() .addOnSuccessListener { documents -> for (document in documents) { Log.d(TAG, "${document.id} => ${document.data}") } } .addOnFailureListener { exception -> Log.w(TAG, "Error getting documents: ", exception) } ``` ______________________________________________________________________ ### 5. Update Data Update some fields of a document using `update()` without overwriting the entire document: ```kotlin val washingtonRef = db.collection("cities").document("DC") // Set the "isCapital" field to true washingtonRef .update("capital", true) .addOnSuccessListener { Log.d(TAG, "DocumentSnapshot successfully updated!") } .addOnFailureListener { e -> Log.w(TAG, "Error updating document", e) } ``` ______________________________________________________________________ ### 6. Delete Data Delete a document using `delete()`: ```kotlin db.collection("cities").document("DC") .delete() .addOnSuccessListener { Log.d(TAG, "DocumentSnapshot successfully deleted!") } .addOnFailureListener { e -> Log.w(TAG, "Error deleting document", e) } ```