繁体   English   中英

Firebase 数据库 getInstance 崩溃

[英]Firebase database getInstance crash

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

我通过 Android Studio 配置了 firebase。

在此处输入图片说明

我仍然遇到错误。

我相信 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'

这是我的项目级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
}

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

谢谢

请在 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 url 这意味着尚未添加任何应用程序,项目配置中可能存在一些问题

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

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

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

希望这对未来的人有所帮助!

我会建议检查Firebase 身份验证并从Firebase 控制台中的项目设置下载google-services.json

我会建议尝试使用上述解决方案。 我希望它会起作用。

将实现 'com.google.firebase:firebase-database:17.0.0' 或最新版本添加到 Android Studio 中的 build.gradle。

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

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

在您的应用中将 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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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