Gradle错误:任务':app:processDebugGoogleServices'的执行失败,请确保首先调用FirebaseApp.initializeApp(Context)

时间:2018-07-29 18:43:09

标签: android firebase android-studio firebase-authentication

我正在尝试使用Firebase执行简单的电子邮件和密码活动。

我的build.gradle(app):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.sidharth.android.firebase3login"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    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'

    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.google.firebase:firebase-crash:16.0.1'
    implementation 'com.google.firebase:firebase-auth:16.0.2'
    /*include the databse jar file*/
    implementation 'com.google.firebase:firebase-database:16.0.1'
}

MainActivity.java

package com.sidharth.android.firebase3login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class MainActivity extends Activity {

    private EditText mEmail, mPassword;
    private Button btnLogin;
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener authStateListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FirebaseApp.initializeApp(this);
        mAuth = FirebaseAuth.getInstance();

        mEmail = findViewById(R.id.emailField);
        mPassword = findViewById(R.id.passwordField);
        btnLogin = findViewById(R.id.loginBtn);

        authStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (firebaseAuth.getCurrentUser() != null) {
                    startActivity(new Intent(MainActivity.this, HomeActivity.class));
                }
            }
        };

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startSign();
            }
        });
    }

    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(authStateListener);
    }

    private void startSign() {
        String email = mEmail.getText().toString();
        String password = mPassword.getText().toString();
        if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
            Toast.makeText(MainActivity.this, "Fields are empty", Toast.LENGTH_SHORT).show();
        } else {
            mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (!task.isSuccessful()) {
                        Toast.makeText(MainActivity.this, "Login Problem", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }

    }
}

即使我在mainactivity文件中初始化了FirebaseApp.initializeApp(this);,我仍然遇到错误

  

确保首先调用FirebaseApp.initializeApp(Context)

当我用Google搜索时,在这里 Make sure to call FirebaseApp.initializeApp(Context) first in Android

我发现了一些说明,而我无法执行的说明是,将apply plugin: 'com.google.gms.google-services'放在gradle文件的末尾,因为每当执行此操作时,我都会遇到另一个错误

  

等级错误:任务':app:processDebugGoogleServices'的执行失败

当我在Google上搜索到Gradle Error:Execution failed for task ':app:processDebugGoogleServices'时  没必要,因为“ com.android.application”软件包已经具有相同的软件包。现在我不知道该怎么办以及如何解决它。

更新: 这是我的build.gradle(project):

buildscript {

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


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

1 个答案:

答案 0 :(得分:1)

您需要添加:

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

在文件末尾可以使用firebase库。

然后在顶级gradle文件中添加最新版本的google-service插件:

dependencies {
classpath 'com.google.gms:google-services:4.0.2'
// ...
}

这样,您不会收到以下错误:

  

错误:任务':app:processDebugGoogleServices'的执行失败。

     

请解决版本冲突。

也请检查以下内容:

https://developers.google.com/android/guides/google-services-plugin