Failed to get FirebaseDatabase instance. Specify DatabaseURL within FirebaseApp

為{幸葍}努か 提交于 2021-01-27 18:50:27

问题


Application crashed when I call

FirebaseDatabase database = FirebaseDatabase.getInstance();

Logs

 com.google.firebase.database.DatabaseException: Failed to get FirebaseDatabase instance: Specify DatabaseURL within FirebaseApp or from your getInstance() call.
 at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
 at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)

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 {
...
}

回答1:


You can go to the Firebase console: 1-Select your project. 2-From "Realtime Database" tap on the left menu go to the "Data" tab and here you can easily access the database URL needed which you can find it at the first line,then please use the following line of code:

DatabaseRefernce mdatabaseref=FirebaseDatabase.getInstance().getReferenceFromUrl(url);.

and you are all set. for example you can find the URL in this picture underlined by red

Please have a look on this: Assign a firebase database url(String) to a Database Reference variable?




回答2:


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"
  }, 
...



回答3:


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




回答4:


Just add your firebase url inside the getInstance()

FirebaseDatabase database = FirebaseDatabase.getInstance("YOUR FIREBASE URL HERE");

That worked for me.




回答5:


Upgrade the versions below implementation packages in build.gradle(modular).

dependencies {

    //noinspection GradleDependency
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.google.firebase:firebase-database:19.6.0'
    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}



回答6:


In Kotlin Copy your database url from here and then, paste this url in your code.

refUsers = FirebaseDatabase.getInstance("https://tictic-97910.firebaseio.com/").reference.child("Users").child(firebaseUserId)



回答7:


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 .plist file from another project that I have and the DATABASE_URL key was missing

All you have to do is 1, enter the key back into your .plist file

Then 2, 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




回答8:


google-services.json
"project_info": { "firebase_url":""<- here is your Realtime Database url` }



来源:https://stackoverflow.com/questions/47730910/failed-to-get-firebasedatabase-instance-specify-databaseurl-within-firebaseapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!