4

It might be really same with other questions already but I don't really know what's wrong in here. Thanks in advance for the help.

build.gradle (project)

        // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'com.google.gms:google-services:3.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com" // Google's Maven repository
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle(module)

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.bustracker.usc.myapplication"
        minSdkVersion 21
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
    compile 'com.google.android.gms:play-services-maps:10.2.6'
    compile 'com.google.android.gms:play-services-location:10.2.6'
    compile 'com.google.firebase:firebase-database:10.2.6'
    testCompile 'junit:junit:4.12'
}

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

MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FirebaseApp.initializeApp(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        firebaseDatabase = FirebaseDatabase.getInstance();
        mRootReference = firebaseDatabase.getReference();
        mheadingReference = mRootReference.child("users");

ERROR:

FATAL EXCEPTION: main
                                                 Process: com.bustracker.sample.myapplication, PID: 2658
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bustracker.sample.myapplication/com.bustracker.sample.myapplication.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.bustracker.sample.myapplication. Make sure to call FirebaseApp.initializeApp(Context) first.
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:154)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                  Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.bustracker.sample.myapplication. Make sure to call FirebaseApp.initializeApp(Context) first.
                                                     at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
                                                     at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
                                                     at com.bustracker.sample.myapplication.MainActivity.onCreate(MainActivity.java:53)
                                                     at android.app.Activity.performCreate(Activity.java:6679)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:154) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

It might be really same with other questions already but I don't really know what's wrong in here. Thanks in advance for the help.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134

4 Answers4

12

For me none of the solution worked that were given any where. Only this worked. Just had to download grade my google services from 4.1.0 to 4.0.0

dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0-alpha08'
    classpath 'com.google.gms:google-services:4.0.0'
    /*classpath 'com.google.gms:google-services:4.1.0' <-- this was the problem */
}

So if you have updated the google services, just try to downgrade or change to an older version. Hope it helps someone else.

Ammar Bukhari
  • 2,082
  • 2
  • 16
  • 16
  • This temporary work-around did the trick! Both versions 4.2.0 and 4.1.0 resulted in the same error. Version 4.0.0 worked great. A call to FirebaseApp.initializeApp() is NOT required unless accessing other Firebase projects! – Always Lucky Jan 15 '19 at 22:59
  • It is somewhat disappointing that the gradle dependency configuration automatically generated by the Firebase plugin in android studio causes this issue. This work around seems to be the easiest (and only) way I've found after searching to resolve it. – John Feb 10 '19 at 05:58
  • 4.2.0 Fixed this problem for me – user1838169 Feb 14 '19 at 15:38
8

According to the docs:

As said in the docs:

Any FirebaseApp initialization must occur only in the main process of the app. Use of Firebase in processes other than the main process is not supported and will likely cause problems related to resource contention.

you need to initialize it not in the activity.

add an application class to your manifest example:

      <applicaton
       android:name="MyApplication"
       

then do this:

public class MyApplication extends Application {
  @Override
public void onCreate() {
    super.onCreate();
   FirebaseApp.initializeApp(this);
}

and remove the initialization from the activity. You need to initialize it in the application class which is the base class.

Edit(about application):

Base class for maintaining global application state. You can provide your own implementation by creating a subclass and specifying the fully-qualified name of this subclass as the "android:name" attribute in your AndroidManifest.xml's tag. The Application class, or your subclass of the Application class, is instantiated before any other class when the process for your application/package is created.

Useful link: https://developer.android.com/reference/android/app/Application.html

Community
  • 1
  • 1
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • does it mean that I should do the codes in saving/retrieving data in this class? – Romar Jhon Maulana Dec 19 '17 at 13:35
  • no, all it means that you need to initialize firebase in the base class which is application.. you retrieve and save in activity as normal. You do not initialize it in the activity as said in the answer.. Also this very old firebase(not sure why you are using this..) – Peter Haddad Dec 19 '17 at 13:36
  • what's the use of the MyApplication class? do I call it or what when I use firebaseDatabase reference? – Romar Jhon Maulana Dec 19 '17 at 13:38
  • no, MyApplication is not an activity... you do not call it anywhere its a class that will extend the base class which is `Application` and firebase has to be initialized in the base class not in every activity.. – Peter Haddad Dec 19 '17 at 13:40
  • check edit might help understand more. You will still have to retrieve and save data in activity as normal, only initialization is done in the base class of the application – Peter Haddad Dec 19 '17 at 13:41
  • what does it mean when after I initialized this as base class that I can now use it whenever I need id? – Romar Jhon Maulana Dec 19 '17 at 13:49
  • @RomarJhonMaulana yes when you write this `FirebaseApp.initializeApp(this);` in `myApplication` class it means you are initializing firebase to be able to use it, yes you will be able to use it in all activities – Peter Haddad Dec 19 '17 at 13:50
  • yes, I've read it. I've tried doing the above suggestions, but still got troubles, maybe in my manifest? Can't turn this into chat I'm new to this (no reputation yet) – Romar Jhon Maulana Dec 19 '17 at 13:58
  • no need to turn into chat, not a problem. Did you remove the initialization from the activity? Did you create a class that extends `Application` and added it in the manifest and inside the class added the initialization? – Peter Haddad Dec 19 '17 at 14:00
  • yes I removed the initialization from the activity and initialized it to the class wherein it extends to Application. I added this line to manifest inside android:name=".com.bustracker.usc.myapplication.FirebaseApplication" – Romar Jhon Maulana Dec 19 '17 at 14:07
  • did you write public class FirebaseApplication extend Application{ //and wrote the code writtin in answer here } ? – Peter Haddad Dec 19 '17 at 14:13
  • @RomarJhonMaulana that's great, happy coding! – Peter Haddad Dec 19 '17 at 14:26
5

For me, it was enough to ensure that the google-services plugin is initialized in my app.gradle:

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

Then I did not even have to call FirebaseApp.initializeApp(Context) at all. Maybe it helps someone.

thatbrainiac
  • 155
  • 2
  • 9
0

I don't know why this worked for me but removing

tools:node="replace" 

from my AndroidManifest.xml improved the crash from right away to from some other thing. Hope I helped someone.

kevinlinxc
  • 470
  • 5
  • 20