Firebase数据库getInstance崩溃 [英] Firebase database getInstance crash

查看:203
本文介绍了Firebase数据库getInstance崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到许多问题,问为什么应用程序崩溃,并显示以下错误日志:无法获取FirebaseDatabase实例:FirebaseApp对象的FirebaseOptions对象中没有DatabaseURL。

我通过Android Studio配置了firebase。





我仍然遇到错误。

我相信google-services.json文件没有错误,因为这是由Android Studio自动创建的。



任何帮助将不胜感激!



我很乐意添加更多关于我要求提供的代码的细节编辑:创建数据库实例

  FirebaseDatabase数据库= FirebaseDatabase .getInstance(); 

DatabaseReference myRef = database.getReference(message);
myRef.setValue(Hello,Wrld!);

以下是我的应用级别gradle:

 buildToolsVersion24.0.3
$应用插件:'com.android.application'

android {
compileSdkVersion 24
b $ b defaultConfig {
applicationIdcom.mypackage
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName1.0
testInstrumentationRunnerandroid.support .test.runner.AndroidJUnitRunner


buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt' ),'proguard-rules.pro'
}
}

productFlavors {
lite {
applicationIdcom.mypackage.a
versionCode 6
versionName3.0.0-lite
}
pro {
applicationIdcom.mypackage.b
versionCode 1
versionName 3.0.0-pro
}
}
}

依赖项{
编译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'c​​om.android.support:appcompat -v7:24.2.1'
compile'c​​om.facebook.android:facebook-android-sdk:4.16.1'
testCompile'junit:junit:4.12'
compile'c​​om.google .firebase:firebase-database:9.8.0'
compile'c​​om.google.firebase:firebase-core:9.8.0'
compile'c​​om.google.firebase:firebase-crash:9.8.0 '

compile'c​​om.google.android.gms:play-services-ads:9.8.0'

compile'c​​om.google.android.gms:play-services -places:9.8.0'
compile'joda-time:joda-time:2.9.4'

}
apply plugin:'com.google.gms.google-服务'

这里是我的项目级别gradle:

  //顶层构建文件您可以在其中添加所有子项目/模块通用的配置选项。 

uildscript {
repositories {
jcenter()
}
依赖关系{
classpath'com.android.tools.build:gradle: 2.2.2'

classpath'com.google.gms:google-services:3.0.0'


//注意:不要放置应用程序依赖关系这里;它们属于单个模块build.gradle文件中的
//
}
}

allprojects {
repositories {
jcenter()



任务清理(类型:删除){
删除rootProject.buildDir
}

编辑:另外,即使我删除我的google-services.json文件,代码仍然编译并在运行时失败。这是意想不到的。我应该得到一个错误,不让文件编译。

谢谢

解决方案<问题是每个应用程序的风格都需要自己的google-services.json文件所以我将它们添加到每个falvor的模块文件夹中。

/ p>

希望这可以帮助未来的人!


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

解决方案

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!

这篇关于Firebase数据库getInstance崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆