Firebase database getInstance crash

帅比萌擦擦* 提交于 2019-12-23 18:50:34

问题


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


回答1:


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




回答2:


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!




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/40558588/firebase-database-getinstance-crash

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