10

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 -

com.skapsdevelopment.firebase E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.skapsdevelopment.firebase, PID: 31993
Theme: themes:{default=overlay:com.resurrectionremix.pitchblack, iconPack:com.baranovgroup.nstyle, fontPkg:com.resurrectionremix.pitchblack, com.android.systemui=overlay:com.resurrectionremix.pitchblack, com.android.systemui.navbar=overlay:com.resurrectionremix.pitchblack}
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skapsdevelopment.firebase/com.skapsdevelopment.firebase.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.skapsdevelopment.firebase. Make sure to call FirebaseApp.initializeApp(Context) first.
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
   at android.app.ActivityThread.-wrap11(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5461)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.skapsdevelopment.firebase. Make sure to call FirebaseApp.initializeApp(Context) first.
   at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
   at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source)
   at com.skapsdevelopment.firebase.MainActivity.onCreate(MainActivity.java:35)
   at android.app.Activity.performCreate(Activity.java:6251)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5461) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

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?

jacefarm
  • 6,747
  • 6
  • 36
  • 46
Subham Kaps
  • 193
  • 1
  • 4
  • 11

14 Answers14

22

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

george mano
  • 5,948
  • 6
  • 33
  • 43
  • When you register an app on Firebase, the steps you mentioned are precisely there as one of the steps required to get your app registered. So, yes, this is a necessary requirement for working with Firebase. – Rajan Prasad Oct 25 '17 at 10:29
5

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 ?

Orabîg
  • 11,718
  • 6
  • 38
  • 58
  • 1
    Yeah... I am doing the same. But still getting the same error. Here's my **onCreate()** Method : – Subham Kaps Oct 20 '16 at 14:35
  • 1
    ` protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FirebaseApp.initializeApp(this); 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); }` – Subham Kaps Oct 20 '16 at 14:35
3

First thing Make sure you register your application in the Firebase Console and you have added the google-services.json file in the app.

Second Check the whether you have add all the dependency or not. Add following dependency in the App-bulid Gradle file:-

buildscript {

repositories {
    google()
    jcenter()
}
dependencies {
    classpath 'com.google.gms:google-services:4.0.0'
    }

And add in project level bulid gradle file:-

dependencies
 {
     implementation 'com.google.firebase:firebase-core:16.0.1'
     implementation 'com.google.firebase:firebase-auth:16.0.2'
 } 
 apply plugin: 'com.google.gms.google-services'    
Dhruvisha
  • 189
  • 5
  • 9
3

please add this classpath 'com.google.gms:google-services:4.0.1' at the place of classpath 'com.google.gms:google-services:4.1.0'

2

Add the following line in your app's Project-level build.gradle inside dependencies

classpath 'com.google.gms:google-services:4.3.5'

Then add the following line as the last line in your app's Module-level build.gradle

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

Now, sync both the gradle files and you are good to go.

I hope this will solve your problem.

Happy coding :)

Vaibhav Pallod
  • 466
  • 7
  • 21
Razz
  • 452
  • 6
  • 9
1

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...

jacefarm
  • 6,747
  • 6
  • 36
  • 46
Lean Vitale
  • 25
  • 1
  • 5
  • I am using FirebaseAuth for registration with email & password. Is it necessary to add FirebaseDatabase as you written in onCreate() method above? – Subham Kaps Oct 20 '16 at 15:00
  • No, FirebaseApp is only for register app, if you can register user can use FirebaseAuth in any class, and use FirebaseAuth.getInstanse() for get user data. – Lean Vitale Oct 20 '16 at 17:21
1

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

Gene Bo
  • 11,284
  • 8
  • 90
  • 137
  • Can you please elaborate on how did you add the new package name?I have a native android app with unity integrated how do i do that? – Suhail Pappu Dec 24 '19 at 08:51
  • 1
    Here's the Firebase setup page https://firebase.google.com/docs/android/setup . There is a video there, at 2:36 it shows where you can enter a new package name. If you scroll down to section **Step 2: Register your app with Firebase**, there are instructions for adding a new package name. Here's the equivalent for Unity https://firebase.google.com/docs/unity/setup – Gene Bo Dec 24 '19 at 17:29
  • Thanks for the comment but I solved it bro .in my case the issue was never related to firebase so i worked things out by unity plugin. – Suhail Pappu Dec 24 '19 at 17:35
1

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
rsssssonsss
  • 413
  • 5
  • 6
1

Follow these steps:

  1. Remove FirebaseApp.initializeApp(context); as you do not need it in the newer versions of Firebase
  2. Add your app with package name to your Firebase project. Download the Google JSON file with your package name and add the downloaded file to your app
  3. Add the instructed libraries into both app level and project level gradle
  4. Sync the project and you are ready to use Firebase

For further info see the Firebase Authentication Guide.

scopchanov
  • 7,966
  • 10
  • 40
  • 68
0

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"

Darush
  • 11,403
  • 9
  • 62
  • 60
0

Downgrade google service plugin from version 4.1.0 to version 4.0.1 in project's build.gradle

classpath 'com.google.gms:google-services:4.0.1'
Zia
  • 2,365
  • 2
  • 17
  • 14
0

Bit late for the party, but I wanted to share my solution. Hopefully it helps others

I had a very similar problem, a few hours ago. Tried everything here, but nothing worked. All my dependencies were correct, which made no sense.

What I didn't know was that this application needed to be registered in Firebase. I followed this step by step guide and voilà! It works! Instead of using the recommended classpath dependency, be sure to use this one com.google.gms:google-services:4.2.0.

Jcorretjer
  • 467
  • 2
  • 6
  • 24
0

What worked for me was:

  1. Adding FirebaseApp.initializeApp(this); on the activity onCreate() method.

  2. Downgrading com.google.gms:google-srvices in project level build.gradle from 4.2.0 to 4.0.0

that is changing:

dependencies {

        classpath 'com.android.tools.build:gradle:3.3.2'

        classpath 'com.google.gms:google-services:4.2.0'
}

to:

dependencies {

        classpath 'com.android.tools.build:gradle:3.3.2'

        classpath 'com.google.gms:google-services:4.0.0'
}
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Johnny
  • 51
  • 1
  • 4
0

I tried every available method from above, but nothing solved this problem entirely. I did this:

  1. Add firebase_core: in your pubspec.yaml. Add latest version from pub.dev

2. Now, open your main.dart, and then modify your code as follows:

   import 'package:firebase_core/firebase_core.dart';
   import 'package:flutter/material.dart';

      void main() async {
        WidgetsFlutterBinding.ensureInitialized();
        await Firebase.initializeApp();
        runApp(MyApp());
   }

You just need to call Firebase.initializeApp() once in your app.

Cribber
  • 2,513
  • 2
  • 21
  • 60