androiddatabasefirebasefirebase-realtime-database

Failed to get FirebaseDatabase instance. Specify DatabaseURL within FirebaseApp


FirebaseApp throws an exception when invoke FirebaseDatabase.getInstance().

Error message

Failed to get FirebaseDatabase instance: Specify DatabaseURL within FirebaseApp or from your getInstance() call.

Firebase project configured correctly. Authentication works without issue, but cannot connect to firebase.

Here is my app level gradle.build file

build.gradle

dependencies {
    .....

    //Firebase database
    implementation 'com.google.firebase:firebase-database:11.6.2'
    // Firebase Invitation
    implementation 'com.google.firebase:firebase-invites:11.6.2'
    // Firebase Authentication
    implementation 'com.google.firebase:firebase-auth:11.6.2'
    // Google Sign In SDK (only required for Google Sign In)
    implementation 'com.google.android.gms:play-services-auth:11.6.2'

    // people api request libraries
    implementation 'com.google.api-client:google-api-client:1.22.0'
    implementation 'com.google.api-client:google-api-client-android:1.22.0'
    implementation 'com.google.apis:google-api-services-people:v1-rev4-1.22.0'
    compile project(':customsupport')
}

apply plugin: 'com.google.gms.google-services' 

And project level build.gradle file

buildscript {
    repositories {
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.google.gms:google-services:3.1.2'

        // We recommend changing it to the latest version from our changelog:
        // https://docs.fabric.io/android/changelog.html#fabric-gradle-plugin
        classpath 'io.fabric.tools:gradle:1.24.4'
    }
}

An DatabaseException will occur in FirebaseDatabase.class

public static synchronized FirebaseDatabase getInstance(FirebaseApp var0, String var1) {
        if(TextUtils.isEmpty(var1)) {
            throw new DatabaseException("Failed to get FirebaseDatabase instance: Specify DatabaseURL within FirebaseApp or from your getInstance() call.");
        } else {
...
}

Solution

  • I downloaded the google-services.json file again, because I just added realtime database.

    Your file should have

    "project_info": {
        "project_number": "123456789",
        "firebase_url": "https://example.firebaseio.com",
        "project_id": "example",
        "storage_bucket": "example.appspot.com"
      }, 
    ...