Firebase数据库getInstance崩溃

时间:2016-11-12 01:20:18

标签: java android firebase firebase-realtime-database

我看到很多问题,询问为什么应用程序崩溃时出现以下错误日志:无法获取FirebaseDatabase实例:FirebaseApp对象的FirebaseOptions对象中没有DatabaseURL。

我通过Android studio配置了firebase。

enter image description here

我仍然遇到错误。

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

任何帮助将不胜感激!

我很乐意添加有关我要求提供的任何代码的更多详细信息。

编辑:创建数据库实例

 FirebaseDatabase database = FirebaseDatabase.getInstance();

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

这是我的应用级别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'

这是我的项目级别:

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

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

谢谢

5 个答案:

答案 0 :(得分:6)

请在Firebase上创建项目,然后使用适当的包名添加应用程序,将下载的json文件放入您的应用中:

我认为当前文件没有

 "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网址表示尚未添加任何应用程序可能在项目配置中存在一些问题

首先尝试使用默认参考
mDatabase = FirebaseDatabase.getInstance().getReference();

答案 1 :(得分:3)

我建议您查看Firebase Authentication并从Firebase Console 的“项目设置”下载 google-services.json

我建议尝试使用上述解决方案。我希望它能奏效。

答案 2 :(得分:3)

问题在于应用程序的每种风格都需要自己的google-services.json文件

所以我将它们添加到每个falvor的模块文件夹中。

希望这可以帮助将来的某个人!

答案 3 :(得分:0)

添加实现“ com.google.firebase:firebase-database:17.0.0”或最新版本以在Android Studio中进行build.gradle。

在配置新项目期间,

Console.firebase.google.com/project设置还将为您提供com.google.firebase:firebase-core:x.x.x:版本。只需在gradle文件中使两个版本匹配即可。

这对我有用,可能是因为在更高版本中针对FirebaseDatabase.getInstance()方法实现了错误修复。

答案 4 :(得分:0)

在您的应用中将 Firebase 实时数据库与 ProGuard 一起使用时,您需要考虑在混淆后您的模型对象将如何序列化和反序列化。如果使用 DataSnapshot.getValue(Class) 或 DatabaseReference.setValue(Object) 读写数据,则需要在 proguard-rules.pro 文件中添加规则:

# 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.** {
  *;
}

参考:https://firebase.google.com/docs/database/android/start/

相关问题