我正在尝试使用firebase执行一个简单的电子邮件和密码活动。
我的build.gradle(应用程序(:
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'
}
主要活动.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();
}
}
});
}
}
}
尽管我在主活动文件中初始化了FirebaseApp.initializeApp(this);
,但我还是收到了错误
确保首先调用FirebaseApp.initializeApp(Context(
当我在谷歌上搜索它时,在这里确保在Android 中首先调用FirebaseApp.initializeApp(Context(
我找到了一些指令,但我不能做的是,把apply plugin: 'com.google.gms.google-services'
放在我的gradle文件的末尾,因为每当我这样做时,我都会得到另一个错误
Gradle错误:任务":app:processDebugGoogleServices"执行失败
当我在谷歌上搜索时,我在这里找到了Gradle错误:任务';的执行失败:app:processDebugGoogleServices';这是不必要的,因为"com.android.application"包已经有了相同的包。现在我不知道该怎么办,也不知道该如何修复。
更新:这是我的构建。gradle(项目(:
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
}
您需要添加:
apply plugin: 'com.google.gms.google-services'
在文件末尾可以使用firebase库。
在顶级gradle文件中添加最新版本的谷歌服务插件:
dependencies {
classpath 'com.google.gms:google-services:4.0.2'
// ...
}
这样你就不会得到以下错误:
错误:任务":app:processDebugGoogleServices"的执行失败。
请修复版本冲突。
还要检查这个:
https://developers.google.com/android/guides/google-services-plugin