N Kaushik

Failed to connect to a different Firebase project from Android Studio

December 27, 2021

Failed to connect to a different Firebase project:

If you are switching to a different Firebase project from Android Studio, it might give you permission error. For example, if you are already using a Firebase project in Android studio and you want to point it to a different Firebase project. You can replace the firebase json file, but it might throw an error.

Permission error, DatabaseError:

This is the most common error. You might see a DatabaseError in the logs.

2021-11-30 19:47:11.353 11339-11388/com.nkd.myapplication W/Firestore: (24.0.0) [WriteStream]: (29f59da) Stream closed with status: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}.

2021-11-30 19:47:11.386 11339-11388/com.nkd.myapplication W/Firestore: (24.0.0) [Firestore]: Write failed at subscribers/username@email.com: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}
2021-11-30 19:47:11.390 11339-11339/com.nkd.myapplication D/CompatibilityChangeReporter: Compat change id reported: 147798919; UID 10154; state: ENABLED
2021-11-30 19:47:11.400 11339-11339/com.nkd.myapplication W/System.err: com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.
2021-11-30 19:47:11.400 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.util.Util.exceptionFromStatus(Util.java:117)

You can go to your Firebase console and recheck the permission added for the database.

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if
          request.time < timestamp.date(2021, 12, 30);
    }
  }
}

This is a common error. You can read this post to learn how to fix this.

Another way to fix this error:

If there is no issue with the permission in the Firebase console, you need to follow the following steps to fix this:

  1. Delete the google-services.json file from your project.
  2. From the build.gradle file, remove the classpath ‘com.google.gms:google-services:x.x.x’ line.
  3. In the application level build.gradle file, i.e. the build.gradle file inside app folder, remove id ‘com.google.gms.google-services’ from the plugins{} block. Also remove any implementation of Firebase from the dependencies block. For example, if you have implementation ‘com.google.firebase:firebase-database-ktx:x.x.x’ for firebase database, remove it.
  4. Invalidate the cache and restart Android Studio. To do that, open Android studio, click on File and click on Invalidate cache/restart option to restart Android studio.

Android studio invalidate cache/restart

Once it is done, connect to firebase again from Firebase assistant window. Click on Tools in the Android studio menu and click on Firebase to open the assistant window.

Firebase assistant android studio

You can login to the new Firebase account from this window and it will connect your project with the new account. You can restore all dependencies of Firebase once it is connected to the new account. It also downloads the firebase .json file and adds all firebase related configs in the build.gradle files.


Subscribe to my Newsletter