1

I am seeing a few exceptions with the message Default FirebaseApp is not initialized in this process com.armoured. Make sure to call FirebaseApp.initializeApp(Context) first.

The stack trace is given below:

2019-03-12 20:42:33.607 11511-11511/com.armoured E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.armoured, PID: 11511
java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.armoured. Make sure to call FirebaseApp.initializeApp(Context) first.
    at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.1:219)
    at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source:1)
    at com.armoured.fragments.LoginFragment.onCreateView(LoginFragment.java:59)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
    at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
    at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
    at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
    at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
    at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
    at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
    at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
    at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:620)
    at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
    at android.app.Activity.performStart(Activity.java:7165)
    at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2975)
    at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
    at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

My problem is exactly like the one asked in this question. However, none of the solutions in that question worked for me. I already have apply plugin: 'com.google.gms.google-services' added to my gradle file. I use firebase:16.0.3 dependencies in all of my code.

One final detail: I have already added FirebaseApp.initializeApp(Context) in my fragment. For completeness, I am adding the function where I am getting this error to the question so that you guys might have a context about the error.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    FirebaseApp.initializeApp(getActivity());
    View view = inflater.inflate(R.layout.fragment_login, container, false);
    mViewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
    mAuth = FirebaseAuth.getInstance();
    ButterKnife.bind(this,view);
    return view;
}

EDIT: I am attaching the app level gradle level file as requested.

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

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.armoured"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    api 'com.google.android.material:material:1.0.0-beta01'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    implementation 'com.jakewharton:butterknife:10.1.0'
    implementation 'com.google.firebase:firebase-core:16.0.3'
    implementation 'com.google.firebase:firebase-auth:16.0.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0-beta01'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-beta01'
    implementation 'androidx.recyclerview:recyclerview:1.0.0-beta01'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'

}
Rohit Shinde
  • 1,575
  • 5
  • 21
  • 47

2 Answers2

1

In your question you said that you added apply plugin: 'com.google.gms.google-services' to your build.gradle, but I don't see it there. It should be at the very bottom of your app-level build.gradle. Perhaps you modified the top level build.gradle instead of the app's. Not having this line will definitely cause the problem you're seeing.

Also there is no longer any need to use the same version of Firebase dependencies. They each revision independently now. If you are just integrating Firebase for the first time, you should use all the latest versions as shown in the setup documentation.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Hey Doug, if you look at the very top of the file, I do add the line that you mention. – Rohit Shinde Mar 13 '19 at 01:59
  • And it should be at the very bottom instead, according to the same page of linked instructions. – Doug Stevenson Mar 13 '19 at 02:15
  • Hi Doug, I put it at the very bottom and still it wouldn't work. Sunil's comment above:Changing to 4.2 gradle version instead of using 4.1 helped. I guess this is a bug in the 4.1 version. – Rohit Shinde Mar 13 '19 at 02:35
1

Upgrading classpath in root build.gradle from 4.1 to 4.2 should fix this issue because 4.1 had an issue which was fixed in 4.2.

So your dependencies should look something like this.

dependencies {
   // other classpaths
    classpath 'com.google.gms:google-services:4.2.0'
}
Sunil Sunny
  • 3,949
  • 4
  • 23
  • 53