2

I am working on Firebase Storage in Android. I am facing issue while initializing the FirebaseStorage reference.

MyCode :

MainActivity.java

public class MainActivity extends AppCompatActivity {

private StorageReference mStorage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FirebaseStorage storage = FirebaseStorage.getInstance();  // App crash in this line
    mStorage = storage.getReferenceFromUrl("my firebase storage url");

} }

Application Class

public class FirebaseApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();

    Firebase.setAndroidContext(this);
    Firebase.getDefaultConfig().setPersistenceEnabled(true);
}

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}}

LogCat

 Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.sample.firebase.upload. Make sure to call FirebaseApp.initializeApp(Context) first.
                                                                            at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
                                                                            at com.google.firebase.storage.FirebaseStorage.getInstance(Unknown Source)
                                                                            at com.sample.firebase.upload.MainActivity.onCreate(MainActivity.java:34)
                                                                            at android.app.Activity.performCreate(Activity.java:6100)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601) 
                                                                            at android.app.ActivityThread.access$800(ActivityThread.java:178) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                            at android.os.Looper.loop(Looper.java:194) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5637) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

The application is getting crashed in the MainActivity. Log says , Firebase is not initialized in the app but i have already initialize in the application class.

Please help me out , suggest me some solution.

deeptimancode
  • 1,139
  • 4
  • 18
  • 40
  • Post the dependencies section of your build.gradle file. It looks like you are using both the legacy 2.5.x API (ex: `Firebase.setAndroidContext(this)`) and the new 9.x.x API (ex:`FirebaseStorage.getInstance()`). They cannot be used together. Read the [Upgrade Guide](https://firebase.google.com/support/guides/firebase-android). – Bob Snyder Oct 18 '16 at 13:42

2 Answers2

2

Please make sure that you've added the dependency compile 'com.google.firebase:firebase-storage:11.0.4' in Gradle (app level)

If still, you face the same issue.

Error: com.google.firebase.storage.FirebaseStorage.getInstance(Unknown Source)

You may be using the Firebase Authentication. The issue will be resolved by making the version of firebase-auth and firebase-storage same.

compile 'com.google.firebase:firebase-storage:11.0.4'
compile 'com.google.firebase:firebase-auth:11.0.4'

Please also make sure you've import statement in Java file

import com.google.firebase.storage.FirebaseStorage;
Hassnain Jamil
  • 1,651
  • 17
  • 21
1

It does look to me like you have not gone through the getting started steps for Android on Firebase:

https://firebase.google.com/docs/android/setup

You need to add a json config file to your application as a part of this process. Once you have set everything up, the Android SDK will initialize automatically for you.

Benjamin Wulfe
  • 1,705
  • 13
  • 9