My problem is about implementation of Firebase in android app. I have followed all steps told in the tutorials but I still cannot figure out why data is not saved to the firestore. I am aware of the fact that in this website people have come up with similar problems as mine but none helped me to solve my problem. Despite checking everything (Internet connection, rules etc), I could not solve the issue. I would be gratefull, if someone would suggest a different solution. Here are all details about the problem.
Error:
2024-05-31 15:37:19.251 8739-8739 Firestore com.example.project2 D Firestore initialized successfully
2024-05-31 15:37:19.251 8739-8739 Firestore com.example.project2 D Data to be saved: {Message=Sample Message, Title=Sample Title}
2024-05-31 15:37:19.297 8739-8774 Firestore com.example.project2 I (25.0.0) [FirestoreClient]: Initializing. user=null
2024-05-31 15:37:19.514 8739-8774 Firestore com.example.project2 I (25.0.0) [Persistence]: Starting transaction: build overlays
2024-05-31 15:37:19.518 8739-8774 Firestore com.example.project2 I (25.0.0) [Persistence]: Starting transaction: Start IndexManager
2024-05-31 15:37:19.523 8739-8774 Firestore com.example.project2 I (25.0.0) [Persistence]: Starting transaction: Start MutationQueue
2024-05-31 15:37:19.630 8739-8774 Firestore com.example.project2 I (25.0.0) [Persistence]: Starting transaction: Locally write mutations
2024-05-31 15:37:19.752 8739-8774 Firestore com.example.project2 I (25.0.0) [Persistence]: Starting transaction: notifyLocalViewChanges
2024-05-31 15:37:19.788 8739-8775 Firestore com.example.project2 I (25.0.0) [GrpcCallProvider]: Channel successfully reset.
2024-05-31 15:37:19.794 8739-8774 Firestore com.example.project2 I (25.0.0) [GrpcCallProvider]: Current gRPC connectivity state: IDLE
2024-05-31 15:37:19.817 8739-8774 Firestore com.example.project2 I (25.0.0) [WriteStream]: (ec142a2) Stream is open
2024-05-31 15:37:19.832 8739-8774 Firestore com.example.project2 I (25.0.0) [WriteStream]: (ec142a2) Stream sending: # com.google.firestore.v1.WriteRequest@d5b69189
database: "projects/project2-1605c/databases/(default)"
2024-05-31 15:37:19.978 8739-8774 Firestore com.example.project2 I (25.0.0) [GrpcCallProvider]: Current gRPC connectivity state: CONNECTING
2024-05-31 15:37:19.978 8739-8774 Firestore com.example.project2 I (25.0.0) [GrpcCallProvider]: Setting the connectivityAttemptTimer
2024-05-31 15:37:34.633 8739-8774 Firestore com.example.project2 I (25.0.0) [Persistence]: Starting transaction: Backfill Indexes
2024-05-31 15:37:34.634 8739-8774 Firestore com.example.project2 I (25.0.0) [IndexBackfiller]: Documents written: 0
2024-05-31 15:37:34.981 8739-8774 Firestore com.example.project2 I (25.0.0) [GrpcCallProvider]: connectivityAttemptTimer elapsed. Resetting the channel.
2024-05-31 15:37:34.982 8739-8774 Firestore com.example.project2 I (25.0.0) [GrpcCallProvider]: Clearing the connectivityAttemptTimer
2024-05-31 15:37:34.996 8739-8774 Firestore com.example.project2 I (25.0.0) [GrpcCallProvider]: Current gRPC connectivity state: SHUTDOWN
2024-05-31 15:37:34.998 8739-8774 Firestore com.example.project2 W (25.0.0) [WriteStream]: (ec142a2) Stream closed with status: Status{code=UNAVAILABLE, description=Channel shutdownNow invoked, cause=null}.
2024-05-31 15:37:35.035 8739-8775 Firestore com.example.project2 I (25.0.0) [GrpcCallProvider]: Channel successfully reset.
2024-05-31 15:37:35.045 8739-8774 Firestore com.example.project2 I (25.0.0) [GrpcCallProvider]: Current gRPC connectivity state: IDLE
MainActivity.class
package com.example.project2;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Firestore";
private FirebaseFirestore db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseFirestore.setLoggingEnabled(true);
try {
db = FirebaseFirestore.getInstance();
Log.d(TAG, "Firestore initialized successfully");
} catch (Exception e) {
Log.e(TAG, "Error initializing Firestore", e);
}
saveData();
}
private void saveData() {
Map<String, Object> data = new HashMap<>();
data.put("Title", "Sample Title");
data.put("Message", "Sample Message");
Log.d(TAG, "Data to be saved: " + data);
db.collection("Journal").document("My first message")
.set(data)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Log.d(TAG, "Data successfully saved");
Toast.makeText(MainActivity.this, "Message successfully sent!", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Error saving data", e);
Toast.makeText(MainActivity.this, "Message could not be sent!", Toast.LENGTH_SHORT).show();
}
});
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.project2">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Project2"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
build.gradle
plugins {
alias(libs.plugins.androidApplication)
id 'com.google.gms.google-services'
}
android {
namespace 'com.example.project2'
compileSdk 34
defaultConfig {
applicationId "com.example.project2"
minSdk 30
targetSdk 34
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation platform(libs.firebase.bom)
implementation libs.okhttp
implementation libs.appcompat // Example version, update to the latest
implementation libs.firebase.firestore.v2471 // Example version, update to the latest
implementation libs.appcompat
implementation libs.material
implementation libs.activity
implementation libs.constraintlayout
implementation libs.firebase.firestore
implementation libs.firebase.core
testImplementation libs.junit
androidTestImplementation libs.ext.junit
androidTestImplementation libs.espresso.core
}
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
build.gradle(Project)
buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.4.2'
classpath libs.google.services.v442
classpath libs.google.services
classpath libs.gradle // Use the latest version
classpath libs.google.services.v4314 // Use the latest version
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
alias(libs.plugins.androidApplication) apply false
}
I just want to save a couple of simple data to be saved to database in firestore.