How can I solve the Android Firebase error “Default FirebaseApp is not initialised in this process”?

匿名 (未验证) 提交于 2019-12-03 08:52:47

问题:

I am using FirebaseAuth for user registration with email and password, and I have already added the plugin and dependencies in my project.

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener { EditText ed_email, ed_pass; Button but_login; ProgressDialog progressDialog; FirebaseAuth firebaseAuth; Context context;  @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     context=getApplicationContext();     FirebaseApp.initializeApp(context);     firebaseAuth=FirebaseAuth.getInstance();      ed_email= (EditText) findViewById(R.id.ed_email);     ed_pass= (EditText) findViewById(R.id.ed_pass);     but_login= (Button) findViewById(R.id.but_login);     but_login.setOnClickListener(this);     progressDialog=new ProgressDialog(this); } public void registerUser(){     String email=ed_email.getText().toString().trim();     String pass=ed_pass.getText().toString().trim();     if(TextUtils.isEmpty(email)){         Toast.makeText(getApplicationContext(),"Invalid Input",Toast.LENGTH_SHORT).show();         return;     }     if(TextUtils.isEmpty(pass)){         Toast.makeText(getApplicationContext(),"Invalid Input",Toast.LENGTH_SHORT).show();         return;     }     progressDialog.setMessage("You are registering...");     progressDialog.show();     firebaseAuth.createUserWithEmailAndPassword(email,pass).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {         @Override         public void onComplete(@NonNull Task<AuthResult> task) {             if (task.isSuccessful()) {                 Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();                 progressDialog.hide();             } else {                 Toast.makeText(getApplicationContext(), "Sorry...!!!", Toast.LENGTH_SHORT).show();                 progressDialog.hide();             }         }      });   }  @Override public void onClick(View v) {     registerUser(); } 

}

logcat -

My App is not starting and showing the following error:

java.lang.IllegalStateException: Default FirebaseApp is not initialized in this  process com.skapsdevelopment.firebase.  Make sure to call FirebaseApp.initializeApp(Context) first. 

Why is the app not starting properly?

回答1:

Double-check the following as this has worked for me:

Added in your project level gradle file:

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

And on your app-build gradle file:

apply plugin: 'com.android.application'  android {   // ... }  dependencies {   // ...   compile 'com.google.firebase:firebase-core:10.0.1'    // Getting a "Could not find" error? Make sure you have   // the latest Google Repository in the Android SDK manager }  // ADD THIS AT THE BOTTOM apply plugin: 'com.google.gms.google-services'  

Link



回答2:

It's stated in the Docs that the FirebaseApp.initializeApp() should be called from the Application.

Did you try to create and Application instance (if you don't have one already), and call FirebaseApp.initializeApp(this) from its onCreate() method ?



回答3:

You need to add initializeApp in your program. I can show you how it's done.

First create a new class and extend Application.

public class SimpleBlog extends Application {  @Override public void onCreate() {     super.onCreate();     if(!FirebaseApp.getApps(this).isEmpty()) {         FirebaseDatabase.getInstance().setPersistenceEnabled(true);     } } 

Lastly, go to Manifest and add in the application like this:

<application     android:name=".SimpleBlog"  <------     android:allowBackup="true"     android:icon="@mipmap/ic_launcher"     android:label="@string/app_name"     android:supportsRtl="true"     android:theme="@style/AppTheme"> 

And this is all...



回答4:

In my case Proguard was causing the problem so this is what I did: - If there is any class in the AndroidManifest.xml with "YourClass" or ".YourClass", change it to its full package name: "com.yourpackage.yourClass"



回答5:

Thanks to the answers here, I went through the check list of possible issues when I encountered this error.

Finally I realized .. I had started a new module in Android Studio, with a different package name than what's already registered on the Firebase Console for the Firebase instance I had set up. Simple mistake - I forgot to add the new package name to the Console config.

After I got this right, it started to work. Remembering all the little details expected by Firebase Console can be tricky sometimes.. gotta be alert/"config-cognizant" there for sure



回答6:

I also faced the above issue. I downloaded all plugins and checked for updates with NuGet. In the end, I found the crashing was due to the Application Package Name not matching the one assigned in Google Dev.

  • Make sure that your google-services.json is assigned a build action of GoogleServicesJson

  • Check that your application package name matches the one that you assigned in the Google Dev Console



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!