Firebase database getInstance crash

12,641

Solution 1

The problem was that every flavor of the app needed its own google-services.json file

So I added them to each falvor's module folder.

Hope this helps someone in the future!

Solution 2

Please create the project on Firebase then add application with appropriate package name, put the downloaded json file in your app:

I think current file does not have

 "project_info": {
    "project_number": "566986018979",
    "firebase_url": "https://test-fcm-17958.firebaseio.com",
    "project_id": "test-fcm-17958",
    "storage_bucket": "test-fcm-17958.appspot.com"

  }

Firebase url it means didn't added any app yet on there may b some issue in project configuration

Also first try with default reference
mDatabase = FirebaseDatabase.getInstance().getReference();

Solution 3

I will suggest to check the Firebase Authentication and download google-services.json from Project Settings in the Firebase Console .

I will suggest to try to use above solution. I hope it will work.

Solution 4

Add implementation 'com.google.firebase:firebase-database:17.0.0' or latest version to build.gradle in Android Studio.

Console.firebase.google.com/project settings will also give you the com.google.firebase:firebase-core:x.x.x: version during configuration of new project. Just make both versions match in the gradle file.

This worked for me probably because a bug fix was implemented in later versions for the FirebaseDatabase.getInstance() method.

Share:
12,641
Hormigas
Author by

Hormigas

Updated on July 28, 2022

Comments

  • Hormigas
    Hormigas almost 2 years

    I've seen numerous questions asking why an app crashes with the following error log : Failed to get FirebaseDatabase instance: FirebaseApp object has no DatabaseURL in its FirebaseOptions object.

    I configured firebase through Android studio.

    enter image description here

    I still encounter the error.

    I believe there's no error with the google-services.json file as this was auto created by Android Studio.

    Any help would be appreciated!

    I'd be glad to add more details about any piece of code that I'm asked to supply.

    EDIT : Creating the database instance

     FirebaseDatabase database = FirebaseDatabase.getInstance();
    
        DatabaseReference myRef = database.getReference("message");
        myRef.setValue("Hello, Wrld!");
    

    Here's my app level gradle :

    apply plugin: 'com.android.application'
    
    android {
    compileSdkVersion 24
    buildToolsVersion "24.0.3"
    
    defaultConfig {
        applicationId "com.mypackage"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    
    productFlavors {
        lite {
            applicationId "com.mypackage.a"
            versionCode 6
            versionName "3.0.0-lite"
        }
        pro {
            applicationId "com.mypackage.b"
            versionCode 1
            versionName "3.0.0-pro"
        }
    }
    }
    
    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.facebook.android:facebook-android-sdk:4.16.1'
    testCompile 'junit:junit:4.12'
    compile 'com.google.firebase:firebase-database:9.8.0'
    compile 'com.google.firebase:firebase-core:9.8.0'
    compile 'com.google.firebase:firebase-crash:9.8.0'
    
    compile 'com.google.android.gms:play-services-ads:9.8.0'
    
    compile 'com.google.android.gms:play-services-places:9.8.0'
    compile 'joda-time:joda-time:2.9.4'
    
    }
    apply plugin: 'com.google.gms.google-services'
    

    And here's my project level gradle:

    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    
    buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'
    
        classpath 'com.google.gms:google-services:3.0.0'
    
    
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
    }
    
    allprojects {
    repositories {
        jcenter()
    }
    }
    
    task clean(type: Delete) {
    delete rootProject.buildDir
    }
    

    EDIT: Also , even if I remove my google-services.json file , the code still compiles and fails at runtime. This is unexpected. I Should be getting an error that doesn't let the file compile.

    Thanks

  • Hormigas
    Hormigas over 7 years
    Yes, I did download google-service.json from Project settings too. Doesn't seem to work
  • Nitin Karande
    Nitin Karande over 7 years
    Have you checked Firebase database Authentication permission?
  • Hormigas
    Hormigas over 7 years
    Yes I have , both set to true
  • Hormigas
    Hormigas over 7 years
    The weird thing is , on a test app I made , if I remove the contents of the google-services.json I get a malformed json error , however in the app I've presented above , it still goes ahead and crashes even if I delete everything in the json file
  • Hormigas
    Hormigas over 7 years
    Didn't add any app - could you please explain ?
  • Si8
    Si8 over 7 years
    I see u figured out the issue... Great :)
  • Hormigas
    Hormigas over 7 years
    @Si8 Thanks for the help anyway!
  • Usman Ali Siddiqui Sabzwari
    Usman Ali Siddiqui Sabzwari over 7 years
    hey!! I am stuck at some where near. Can you elaborate what you did? I can share my code if needed.
  • Hormigas
    Hormigas over 7 years
    @newUser Hey! I simply put in the son files into each of the flavors' specific folders. What error are you encountering ?