'add collection to firestore with kotlin problems
I am trying to understand and use firebasefirestore from android studio with kotlin.
I have been following tutorials from the official docs, and from udemy and youtube. I do the exact same thing as shown, but still my collection is not created in the firestore console.
I want to add a collection to firestore from within android studio with kotlin.
To my understanding all my code is correct, I have internet permission, I have all the dependencies needed in my gradle files.
And my code for adding a collection to firebase is the exact same as given in the official docs.
Still no matter what I do, I can not create a collection from running my app.
I can create a firestore collection from within the firestore console without a problem. My app runs and execute with out any errors (beside not creating the collection in firestore).
Here is my code
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="eu.example.firestoretest">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.FireStoreTest">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.FireStoreTest">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Gradle project
buildscript {
ext {
compose_version = '1.0.1'
}
dependencies {
classpath 'com.google.gms:google-services:4.3.10'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.1.2' apply false
id 'com.android.library' version '7.1.2' apply false
id 'org.jetbrains.kotlin.android' version '1.5.21' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Gradle module
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'com.google.gms.google-services'
}
android {
compileSdk 31
defaultConfig {
applicationId "eu.example.firestoretest"
minSdk 24
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
// Firebase
implementation "com.google.firebase:firebase-auth:19.2.0"
implementation platform('com.google.firebase:firebase-bom:29.0.0')
implementation "com.google.firebase:firebase-auth-ktx"
implementation "com.google.firebase:firebase-firestore-ktx"
implementation "com.google.firebase:firebase-storage-ktx"
implementation 'com.google.firebase:firebase-firestore:24.0.1'
implementation 'com.google.firebase:firebase-storage:20.0.0'
}
Creating a db
package eu.example.firestoretest
import android.util.Log
import androidx.compose.runtime.Composable
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
@Composable
fun fireStoreTest(){
// Create a FirebaseFirestore object
val db = Firebase.firestore
// Pass data onto the firestore db, it must be of type hashmap
val user = hashMapOf(
"first" to "Ada",
"last" to "Lovelace",
"born" to 1815
)
// add the user data to the collection
db.collection("users")
.add(user)
.addOnSuccessListener {
Log.d("FB", "onCreate: ${it.id}")
}
.addOnFailureListener {
Log.d("FB", "onCreate: ${it}")
}
}
I then call the function fireStoreTest in my main function, but it doesn't work.
Is there someone who could explain in very simple terms what it is I am doing wrong ?
link to logcat https://github.com/McCt68/logcat/blob/main/firestore_logcat.txt
Solution 1:[1]
I was also having similar issue where I was not able to add collection on Firestore from Android Studio. What worked for me is setting the rules of Firestore to this :
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if
request.time < timestamp.date(2022, 5, 3);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | oyeraghib |
