简体   繁体   中英

Firebase database getInstance crash

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.

在此处输入图片说明

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

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();

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!

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.

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:xxx: 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.

When using Firebase Realtime Database in your app along with ProGuard, you need to consider how your model objects will be serialized and deserialized after obfuscation. If you use DataSnapshot.getValue(Class) or DatabaseReference.setValue(Object) to read and write data, you will need to add rules to the proguard-rules.pro file:

# Add this global rule
-keepattributes Signature

# This rule will properly ProGuard all the model classes in
# the package com.yourcompany.models.
# Modify this rule to fit the structure of your app.
-keepclassmembers class com.yourcompany.models.** {
  *;
}

Ref: https://firebase.google.com/docs/database/android/start/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM