Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 {
...
}
like image 793
Vahe Gharibyan Avatar asked Dec 09 '17 16:12

Vahe Gharibyan


3 Answers

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"
  }, 
...
like image 119
Wilmer Avatar answered Oct 16 '22 04:10

Wilmer


If any one face this issue in 2021 then simply follow these steps:- Add these dependencies in build.gradle/app

  implementation platform('com.google.firebase:firebase-bom:26.3.0')
implementation 'com.google.firebase:firebase-database-ktx'

and then use databasereference object

databaseReference=Firebase.database.getReference("Users")

still confused then read this documentation here1

like image 19
Manohar Kàśhýãp Avatar answered Oct 16 '22 03:10

Manohar Kàśhýãp


This is what I did for an iOS project.

After I downloaded the GoogleService-Info.plist file and added it to my project I kept getting the crash

Failed to get FirebaseDatabase instance: Specify DatabaseURL within FIRApp or from your databaseForApp:URL: call.

After googling around and not finding a concrete answer I looked at the GoogleService-info.plist file from another project that I have and the DATABASE_URL key was there but was missing in the one with the crash

All you have to do is 3 simple steps:

1- open your google GoogleService-info.plist

2- enter the DATABASE_URL key back into the file

enter image description here

3- go to your Firebase console > RealtimeDatabase > copy and paste or enter the https//... url string from there as the value to the DATABASE_URL key in .plist file

enter image description here

Be sure to add the https//: part (it might not be obvious from the picture)

like image 13
Lance Samaria Avatar answered Oct 16 '22 03:10

Lance Samaria