FirebaseApp初始化失败-应用崩溃

时间:2019-03-16 21:42:09

标签: java android firebase google-play-services

在我尝试初始化两个引用之后,在我的主要Activity中,它崩溃了,只是发现了错误“ FirebaseApp初始化失败”

private StorageReference mStorageReference; private DatabaseReference mDatabaseReference; mStorageReference = FirebaseStorage.getInstance().getReference("uploads"); mDatabaseReference = FirebaseDatabase.getInstance().getReference("uploads");

私有声明和代码本身之间有一些代码,但这与该问题无关

清单是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firebase10">

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

build.gradle(Project):

buildscript {
   repositories {
      google()
      jcenter()
    }
    dependencies {
         classpath 'com.android.tools.build:gradle:3.3.1'
         classpath 'com.google.gms:google-services:4.1.0'
    }
 }

allprojects {
      repositories {
      google()
      jcenter()
     }  
}
task clean(type: Delete) {
   delete rootProject.buildDir
}

build.gradle(app):

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
      compileSdkVersion 28
      defaultConfig {
      applicationId "com.example.firebase10"
      minSdkVersion 19
      targetSdkVersion 28
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner 
      "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android- 
        optimize.txt'), 'proguard-rules.pro'
        }
    }
 }

dependencies {
     api "com.google.android.gms:play-services-base:15.0.1"
     api "com.google.android.gms:play-services-auth:16.0.0"
     api "com.google.android.gms:play-services-identity:15.0.1"
     implementation fileTree(dir: 'libs', include: ['*.jar'])
     implementation 'com.android.support:appcompat-v7:28.0.0'
     implementation "com.android.support:customtabs:28.0.0"
     implementation 'com.android.support.constraint:constraint-layout:1.1.3'
     implementation 'com.google.firebase:firebase-database:16.0.1'
     implementation 'com.google.firebase:firebase-storage:16.0.1'
     implementation 'com.google.firebase:firebase-auth:16.0.3'
     implementation 'com.android.support:recyclerview-v7:28.0.0'
     implementation 'com.android.support:cardview-v7:28.0.0'
     implementation 'com.squareup.picasso:picasso:2.71828'
     implementation 'com.google.gms:google-services:4.2.0'
     testImplementation 'junit:junit:4.12'
     androidTestImplementation 'com.android.support.test:runner:1.0.2'
     androidTestImplementation 'com.android.support.test.espresso:espresso- 
     core:3.0.2'
}

我使用Tools-Firebase设置了所有内容,而且我知道代码看起来像是粘在一起的,因为它确实是。我一直在研究线程,只是添加了一些东西,但没有任何效果。如果这很重要,我将在手机上而不是模拟器上运行该应用程序。

3 个答案:

答案 0 :(得分:0)

您的google-json文件设置是否正确?

Firebase还需要时间连接到它的服务器,并为您提供引用到您设备的实例,给它一些时间或使用承诺来确保Firebase已连接到它的服务器并生成实例,然后执行所需的操作Firebase实例。

答案 1 :(得分:0)

在build.gradle(项目)中,我将'com.google.gms:google-services:4.1.0'更改为'com.google.gms:google-services:4.2 .0',问题就解决了

答案 2 :(得分:0)

这是个废话,因为那是Gradle插件:

dependencies {
    implementation 'com.google.gms:google-services:4.2.0'
}

这个属于文件的底部:

apply plugin: 'com.google.gms.google-services'