15

I am creating an Android application and I'm currently trying to implement user authentication using Firebase. As far as I can tell, my app is connected to my Firebase server.

I encounter a runtime error when attempting to switch from the SignIn activity to the SignUp activity via a button press. The app crashes and I encounter a runtime error.

So far as I can tell, the runtime error is from the SignUp activity's onCreate() call when I attempt to initialize a FirebaseAuth object with FirebaseAuth.getInstance(). This call fails due to

java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process seniordesign.phoneafriend. Make sure to call FirebaseApp.initializeApp(Context).

However, I make this call in my Application class' onCreate() method which I thought would be fine. I added the initalizeApp() call to the SignUp's onCreate() call but no dice. I've looked for others with this issue but have not found anything similar. Thanks for any help.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="seniordesign.phoneafriend">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name="seniordesign.phoneafriend.PhoneAFriend">
        <activity android:name=".SignIn">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SignUp"></activity>
    </application>

</manifest>

PhoneAFriend.java (My Application class)

public class PhoneAFriend extends Application {

    public void onCreate(){
        super.onCreate();
        Firebase.setAndroidContext(this);
        FirebaseApp.initializeApp(this);
    }
}

SignUp.java

public class SignUp extends AppCompatActivity {
    protected Firebase ref;
    protected EditText emailText;
    protected EditText passText;
    protected EditText confirmText;
    protected Button button;
    protected SignUp thisContext;

    protected FirebaseAuth auth;
    protected FirebaseAuth.AuthStateListener authListener;
    private View.OnClickListener onClickListener;

    public static Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        ref = new Firebase("https://phoneafriend-7fb6b.firebaseio.com");
        emailText = (EditText) findViewById(R.id.signup_emailText);
        passText = (EditText) findViewById(R.id.signup_passwordText);
        confirmText = (EditText) findViewById(R.id.signup_passwordConfirm);
        intent = new Intent(this, SignIn.class);
        //Tried this already
        //FirebaseApp.initializeApp(this);
        auth = FirebaseAuth.getInstance();


        button = (Button) findViewById(R.id.signup_button);
        onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createUser(view);
                Log.v("SignUp Button" , "Clicked; Attempting to create user");
            }
        };
        button.setOnClickListener(onClickListener);

        authListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged( FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    // User is signed in
                    Log.d("FirebaseAuth", "onAuthStateChanged:signed_in:" + user.getUid());
                } else {
                    // User is signed out
                    Log.d("FirebaseAuth", "onAuthStateChanged:signed_out");
                }
                // ...
            }
        };
        thisContext = this;

    }
    @Override
    public void onStart(){
        super.onStart();
        //auth.addAuthStateListener(authListener);
    }

    @Override
    public void onStop(){
        super.onStop();
        if(authListener != null) {
            //auth.removeAuthStateListener(authListener);
        }
    }

    protected void createUser(View view){
        String cString = null;
        String pString = null;
        String eString  = emailText.getText().toString();
        if(passText.getText() != null && confirmText.getText() != null) {
            pString = passText.getText().toString();
            cString = confirmText.getText().toString();
            Log.v("SignUP: Pass Null check" , "Pass" );
            if (emailText.getText() != null && pString.equals(cString) && passText.getText() != null) {
                Log.v("SignUP: Sign up check " , "Pass");
                auth.createUserWithEmailAndPassword(emailText.getText().toString() , passText.getText().toString())
                        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                Log.v("createUser complete" , "status: " + task.isSuccessful());
                                if(task.isSuccessful()){
                                    startActivity(SignUp.intent);
                                }
                            }

                        });

            }
        }

        return;
    }
}
AL.
  • 36,815
  • 10
  • 142
  • 281
The Alex
  • 211
  • 1
  • 5
  • 11
  • You are mixing API calls from the legacy 2.x.x. SDK with the new 9.x.x SDK, They are not compatible. You should use the new SDK only. See this related answer: http://stackoverflow.com/a/39321686/4815718 – Bob Snyder Sep 26 '16 at 16:56

8 Answers8

9

I know there's already an accepted answer. However, I ran into the same error message saying: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process (name). Make sure to call FirebaseApp.initializeApp(Context). I tried several solutions found on SO, and double checking everything, until I finally found that the package name defined in the Firebase Console didn't match the package name defined in my manifest file.

Try go to your Firebase Console -> Project settings -> check if package names matches.

Hope it may help some :)

FJJ
  • 131
  • 5
4

Just as qbix stated, you much use the API calls from whichever version you are going to use. If possible, you should use the newer API because it will definitely be supported much further into the future.

See the docs here:

https://firebase.google.com/docs/database/android/start/

Remove:

Firebase.setAndroidContext(this);
FirebaseApp.initializeApp(this);

And put:

FirebaseDatabase database = FirebaseDatabase.getInstance();

If qbix puts his comment into an answer, you should accept his rather than mine seeing has how he beat me by a few minutes.

Also:

If you are using the old firebase and need help switching, this guide is spot on and will help you with the switch. It's a fairly simple switch.

https://firebase.google.com/support/guides/firebase-android

Ryan
  • 1,988
  • 4
  • 21
  • 34
  • `classpath 'com.google.gms:google-services:3.0.0'` for project gradle. `compile 'com.google.android.gms:play-services:9.8.0' compile 'com.google.firebase:firebase-core:9.8.0'` for app gradle but i still get the issue. Any idea? – Si8 Nov 01 '16 at 10:23
  • The link you provide is Firebase Realtime Database. It has no conflict with FirebaseApp.initializeApp() – Chandler May 29 '18 at 00:48
1

I encountered this problem after I deleted and re-cloned my app and forgot to include the google-services.json inside the app module. After I re-added it, the problem went away. Nevertheless, you should init the context inside your custom Application class:

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        FirebaseApp.initializeApp(this)
    }
}
<manifest 
    package="your.app">

    <application
        android:name=".MyApplication"
Andre Thiele
  • 3,202
  • 3
  • 20
  • 43
0

It is easier to use the Firebase wizard that is included in Android Studio: it adds all the dependencies in the gradle files, creates the firebase project if needed, connects the app with the project, etc.

In Android Studio open menu Tools / Firebase and follow the instructions.

Then FirebaseApp.initializeApp() will return a valid value.

Arnaud SmartFun
  • 1,573
  • 16
  • 21
0

First check up should be the Firebase documentation and the tools from

Android Studio -> Tools -> Firebase -> Cloud Messaging.

Even if you have done that before.

See this, that and that.

Again, even if you already done that before as firebase versions tend to require an up to date configuration JSON.

Maneki Neko
  • 1,177
  • 1
  • 14
  • 24
0

After much frustration, enabling Internet Permissions fixed this issue for me.

stepheaw
  • 1,683
  • 3
  • 22
  • 35
0

Upgrading classpath 'com.google.gms:google-services:4.1.0' to classpath 'com.google.gms:google-services:4.2.0' in the project Gradle worked for me.

nb2998
  • 469
  • 1
  • 4
  • 13
0

Update 2019 : If you update your Firebase dependencies to latest then you should update Google play services as well.

During the writing of this answer I had updated all my Firebase libs to 17.0.+ but my Google play service was still pointing at 4.1.0.

Updating play service version to 4.3.0(Latest) fixed it for me.

enter image description here

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154