Vahe Gharibyan
Vahe Gharibyan

Reputation: 5683

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

Upvotes: 17

Views: 49296

Answers (14)

purnima hanchinal
purnima hanchinal

Reputation: 1

  • Go to your Firebase Console
  • in the Project overview you can find Project Settings
  • download the google-services.Json file in your apps section
  • go to app in your project
  • paste the json file deleting the previous one if it already exists

Upvotes: 0

Lance Samaria
Lance Samaria

Reputation: 19572

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 DATABASE_URL as the key back into the .plist. You get the value from step 3

enter image description here

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

enter image description here

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

Upvotes: 14

Pranav Subramanian
Pranav Subramanian

Reputation: 11

Solution

First, check the location.

US Central 1

FirebaseDatabase db = Firebase.database;
FirebaseDatabase db = FirebaseDatabase.getInstance();

Other Locations

val db: FirebaseDatabase = FirebaseDatabase.getInstance("$URL")

Upvotes: 0

Softon Tech Solution
Softon Tech Solution

Reputation: 31

if you are encountering such error in 2022 then solve it with this easy steps

  1. get your firebase database url
  2. put in int the "getInstance()" function

Problem solved for me the image shows how i solved my own problem

Upvotes: 0

mightyandweakcoder
mightyandweakcoder

Reputation: 846

Upgrade your build dependency to 19.7.0 in AndroidManifest.xml

    implementation 'com.google.firebase:firebase-database:19.7.0'

The default one set up in Android Studio -> Tools -> Firebase used an old version which causes this problem.

Upvotes: 3

Awsome_user
Awsome_user

Reputation: 65

<key>DATABASE_URL</key>
    <string>https://appName-bgbtty-default-bblebut.firebaseio.com</string>

go to google service open as source code, then insert this below the google api id (bottom)

Upvotes: 0

king-of-everything
king-of-everything

Reputation: 57

This is an issue with your Google p.list. Simply redownload it from firebase dashboard/project settings after you enable Firebase Database.

Upvotes: -2

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

Upvotes: 20

Wilmer
Wilmer

Reputation: 512

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

Upvotes: 19

Raghib Arshi
Raghib Arshi

Reputation: 755

enter image description here

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)

Upvotes: 1

Thusitha Wijerathne
Thusitha Wijerathne

Reputation: 19

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'
}

Upvotes: 1

kiran biradar
kiran biradar

Reputation: 11

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

Upvotes: -1

3at
3at

Reputation: 51

Just add your firebase url inside the getInstance()

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

That worked for me.

Upvotes: 4

mohammed youser sawwas
mohammed youser sawwas

Reputation: 611

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?

Upvotes: 4

Related Questions