You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android应用Firebase注册认证报错:Default FirebaseApp未初始化

Fixing Firebase Auth Initialization Error in Your Android Signup Flow

Hey there, let's get that Firebase Auth issue sorted out for your signup form! The error you're seeing—Default FirebaseApp is not initialized—is a common one, and even though you added FirebaseApp.initializeApp(this), there are a few key fixes we can apply to get things working.

Core Issue Breakdown

The error happens because Firebase isn't being set up correctly before your code tries to access FirebaseAuth.getInstance(). While you tried initializing it manually, the automatic initialization from Firebase's Google Services plugin is likely conflicting, or your project configuration is missing critical pieces.

Step-by-Step Solutions

1. Verify Firebase Project Configuration

First, double-check that your app is properly linked to Firebase:

  • Make sure the google-services.json file (downloaded from Firebase Console) is placed directly in the app/ directory of your Android project.
  • Confirm your app's package name in AndroidManifest.xml matches exactly what's registered in your Firebase project.

2. Remove Manual Firebase Initialization

The Google Services plugin automatically initializes Firebase for you when the app starts. Manually calling FirebaseApp.initializeApp(this) can cause conflicts. Delete this line from your onCreate method:

FirebaseApp.initializeApp(this); // Remove this line

3. Check Gradle Dependencies

Ensure your Gradle files have the required Firebase Auth setup:

  • Project-level build.gradle (root directory):
    buildscript {
        dependencies {
            // Make sure this classpath is present (use latest compatible version)
            classpath 'com.google.gms:google-services:4.3.15'
        }
    }
    
  • App-level build.gradle (app/ directory):
    dependencies {
        // Add Firebase Auth dependency (use latest version compatible with your SDK)
        implementation 'com.google.firebase:firebase-auth:22.3.1'
    }
    
    // This line must be at the VERY bottom of the file
    apply plugin: 'com.google.gms.google-services'
    

4. Clean and Rebuild Your Project

Cached build files can cause unexpected issues. Do the following in Android Studio:

  • Go to Build > Clean Project
  • Then Build > Rebuild Project
  • If that doesn't work, try File > Invalidate Caches... > Invalidate and Restart

Modified Signup Activity Code

Here's your code with the manual initialization removed, plus a small debugging improvement:

package official96.yohan.lms;

import android.content.Intent;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class signup extends AppCompatActivity {
    EditText mail, mophone, pswd, usrusr;
    TextView lin, sup;
    private FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        
        // Removed manual Firebase initialization here
        
        mAuth = FirebaseAuth.getInstance();
        getSupportActionBar().setTitle("Sign Up");
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);

        sup = findViewById(R.id.sup);
        lin = findViewById(R.id.lin);
        usrusr = findViewById(R.id.usrusr);
        pswd = findViewById(R.id.pswrdd);
        mail = findViewById(R.id.mail);
        mophone = findViewById(R.id.mobphone);

        Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/LatoLight.ttf");
        Typeface custom_font1 = Typeface.createFromAsset(getAssets(), "fonts/LatoRegular.ttf");

        mophone.setTypeface(custom_font);
        sup.setTypeface(custom_font1);
        pswd.setTypeface(custom_font);
        lin.setTypeface(custom_font);
        usrusr.setTypeface(custom_font);
        mail.setTypeface(custom_font);
    }

    public void signupButtonClicked(View view) {
        mAuth.createUserWithEmailAndPassword(mail.getText().toString(), pswd.getText().toString())
                .addOnCompleteListener(signup.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(signup.this, "New User Created successfully!", Toast.LENGTH_SHORT).show();
                            FirebaseUser user = mAuth.getCurrentUser();
                            Intent it = new Intent(signup.this, Login.class);
                            startActivity(it);
                        } else {
                            // Show exact error message for easier debugging
                            Toast.makeText(signup.this, "Authentication failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
}

Bonus Tip

The updated error toast will show you specific issues like invalid email formats, weak passwords, or network problems—super helpful for debugging beyond just initialization errors.

内容的提问来源于stack exchange,提问作者YohanDhananjaya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:17:23