-1

I am trying to perform a simple email and password activity using firebase.

my 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();
                    }
                }
            });
        }

    }
}

Even though i initialized FirebaseApp.initializeApp(this); in my mainactivity file, i am getting error

Make sure to call FirebaseApp.initializeApp(Context) first

when i googled it, and here Make sure to call FirebaseApp.initializeApp(Context) first in Android

i found some instructions and the one that i am not able to do is, to put apply plugin: 'com.google.gms.google-services' at the end of my gradle file because whenever i do that i get the other error

Gradle Error:Execution failed for task ':app:processDebugGoogleServices'

when i googled that i found here Gradle Error:Execution failed for task ':app:processDebugGoogleServices' that it is not necessary because "com.android.application" package already has same package.Now i don't know what to do and how to fix it.

Update : here is my 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 Answers1

1

You need to add:

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

at the end of the file to be able to use the firebase libraries.

And in the top level gradle file add the latest version of the google-service plugin:

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

This way you won't get the following error:

Error:Execution failed for task ':app:processDebugGoogleServices'.

Please fix the version conflict.

Also check this:

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

Community
  • 1
  • 1
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • no, its still giving the same error. Task failed and org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugGoogleServices'. – Sidharth K.Burnwal Jul 30 '18 at 16:45
  • 1
    @SidharthK.Burnwal do this then, create a new project and add firebase using the tools menu then update all the firebase versions and google-services plugin that got added and sync.( Because the issue might be from the google-services.json file) – Peter Haddad Jul 30 '18 at 17:05
  • its working. there was some problem with my google-services.json file. thank you for your help. – Sidharth K.Burnwal Jul 30 '18 at 17:28