Search code examples
androidfirebaseleakcanary

Leakcanary crashes with Google's Firebase


When processing a leak, I get a java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. inside my Application.onCreate. From what I understand, this happens because LeakCanary creates my application class without all the Google Play Services stuff and Firebase cannot initialize successfully. Any ideas how to fix this?

Edit: I'm using leakcanary-android:1.4-beta2 and this happens after dumping the heap.


Solution

  • I believe I found a workaround. Instead of using the Google Play Services plugin and google-services.json you can configure and initialize Firebase yourself in Application.onCreate or wherever.

    @Provides
    @Singleton
    protected FirebaseApp proviceFirebaseApp(Application application) {
        FirebaseOptions options = new FirebaseOptions.Builder()
                .setApplicationId(application.getPackageName())
                .setDatabaseUrl(BuildConfig.FIREBASE_DATABASE_URL)
                .setApiKey(BuildConfig.FIREBASE_API_KEY)
                .build();
        return FirebaseApp.initializeApp(application, options);
    }
    

    Finding the keys was a bit of a mystery. Inside google-services.json you'll need the database url, which is project_info.firebase_url. If you have more than one application using the Firebase repo, there will be multiple client objects here.

    If you're using Google Sign In, the oauth id is inside client[app].oauth_client[1].client_id. You can figure out your client according to the package name and the oauth_client was the second object that looked like this:

    {
      "client_id": "asfagasdgdas",
      "client_type": 3
    }
    

    The api key is client[app].api_key, again find the client based on your package.

    Forgive the pseudo-json.

    The structure might change later, this was written for Play Services version 9.0.1.