38

I'm having trouble writing to my Firebase database in my simple Android app. I've set my database rules to be public:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

Created a simple class Message:

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class Message {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference dbRef = database.getReference();
    public String sender;
    public String message;

    public Message(){}

    public Message(String sender, String message) {
        this.sender = sender;
        this.message = message;
    }
}

And set a button to perform the onClick method writeNewMessage with these being the significant lines of code:

int counter = 0;

public void writeNewMessage(View view) {

        counter += 1;
        Message messageToWrite = new Message("John", "Simple message: " + counter);
        String messageId = String.valueOf(counter);

        dbRef.child("users").child(messageId).setValue(messageToWrite);
    }

When running, I receive the error:

W/RepoOperation: setValue at /users/1 failed: DatabaseError: Permission denied

I know this question seems simple and likely to have been repeated, but I have yet to find any that asks this problem without having their error be involved in authentication or some other component. The simplicity of the problem is what confuses me the most.

EDIT: I've also found this error as well:

Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.firebase.auth.ModuleDescriptor"
5
  • Hmmm... the rules look good, so indeed that sounds odd. Are you sure you saved/published the rules? May 24, 2016 at 22:39
  • I hit the published button and tried the simulator successfully so as far as I know it's published!
    – Ben
    May 24, 2016 at 23:29
  • Very odd. Couple of things to check: first, can you check your google-services.json and make sure the project you're using is the one you expect. Second, can you try removing auth from the project (comment out the gradle dependency for firebase-auth if its there) and see if that makes any difference?
    – Ian Barber
    May 25, 2016 at 0:21
  • Commenting out firebase-auth did not work in my case, but I think it's because I had a problem with the google-services.json. I had an error setting it up, so I recreated the whole project on Firebase and it was alright in the end. Thanks a lot!
    – Ben
    May 25, 2016 at 0:45
  • As a side note, you better instantiate your database and get reference to it outside your Message class.
    – Sumit Jha
    Jan 9, 2017 at 3:42

7 Answers 7

62

I've been changing firestore rules instead of DB... Hope it helps to someone dumb like me.

enter image description here

5
  • Please include the code as text. Also you appear to be calling the OP dumb?!
    – Artemis
    Mar 25, 2019 at 17:23
  • 17
    There is no code to be included. Not calling the OP dumb, but the people that make the same mistake as I did. I am sure they can get over being called dumb.
    – Zogrof
    Mar 25, 2019 at 17:45
  • 1
    Yeah, Bro, that's it, but it's not your problem, we are all used to have realtime database setting by default. Sep 4, 2019 at 16:48
  • 4
    This was my issue. THANK YOU. Nov 30, 2019 at 4:47
  • 2
    hilarious mistake :/
    – ruben
    Jul 13, 2020 at 13:54
36

Complete Solution:

1). Firstly go to Authentication Tab and select SIGN-IN-METHOD turn Anonymous On

2). Secondly go to Database Tab and write Rule

{
"rules": {
".read": true,
".write": true
}}

3). Thirdly go to Storage Tab and write following rule

service firebase.storage {
 match /b/myapplicationname-f2266.appspot.com/o {
 match /{allPaths=**} {
 allow read, write;
}}}

Note This solution is only for testing purpose with following steps you are giving public permission for Read and Write your database.

3
  • 1
    it is not working for me...when I am adding rules in database as mentioned in second step firebase giving me error it is not recognizing { ruels and onward code...where to exactly put this code in second step... Dec 14, 2018 at 7:00
  • 1
    If you're confused by this answer make sure that after you click on the database sidebar option, you select the realtime database and change the rules for that database and not the default "Cloud Firestore". Take a look at Zogrof's answer
    – Axel
    Apr 4, 2019 at 7:53
  • Should be fine fine step 1 and add firebase-auth into your project implementation implementation 'com.google.firebase:firebase-auth:18.1.0' Jul 29, 2019 at 22:06
29

This looks like problem with your google-services.json.

Make sure app name and client id are same as in firebase console.

If you are not sure re-download google-services.json from your project's console and add it your project.

4
  • 1
    Worked like a dream.
    – Jacob
    Jun 1, 2017 at 17:18
  • What if it happen with react native ? Nov 15, 2018 at 4:51
  • 1
    Hey devprashant, I get this error as well trying to add an empty string to the database next to the current user ID but my google-services.json is right and the database is public Nov 16, 2018 at 5:20
  • The location of google-services.json should be located in ./android/app folder, not ./android
    – Marek
    Mar 12, 2019 at 11:52
2

I'm not entirely sure what the problem was, but I just deleted my project from Firebase and recreated it, deleting and installing the new google-services.json and it worked perfectly the second time.

Luckily this is a test app and my database was empty, but if you're in this situation and have valuable information stored in your account, I would recommend exporting it before trying this solution.

1
  • "I would recommend exporting it before trying this solution" No, dont do this at all... Jul 19, 2022 at 13:33
1

Maybe you should set your rules to: (I had the same problem. It works for me.)

// These rules require authentication
    {
      "rules": {
        ".read": "auth != null",
        ".write": "auth != null"
      }
    }
1
  • Hi @Vandre where i have to apply this rule ?
    – MARSH
    Jan 12 at 4:42
1

The reason for this error can also be in this thing described in the documentation https://firebase.google.com/docs/database/android/start

Note: To get a reference to a database other than a us-central1 default dstabase, you must pass the database URL to getInstance() (or Kotlin+KTX database()) . For a us-central1 default database, you can call getInstance() (or database) without arguments. You can find your Realtime Database URL in the Realtime Database section of the Firebase console. It will have the form https://.firebaseio.com (for us-central1 databases) or https://.firebasedatabase.app (for databases in all other locations).

0

Okay I have tried all above solution and failed. After a while I found that my Project has 2 modules and I call firebase in the second module instead of the main one, so the solution is to add the second one to the firebase project too, and it works.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.