JakeWharton / ProcessPhoenix

Process Phoenix facilitates restarting your application process.
Apache License 2.0
2.03k stars 132 forks source link

Firebase initialization problems after the rebirth/restart #60

Open aktasmehmet opened 1 year ago

aktasmehmet commented 1 year ago

I've faced the problem when recently I integrated Remote Config of Firebase. After some investigation, it seems that I was already facing this issue after application is re-launched but some of the Firebase libraries were handling the errors in the background. It came to surface when Remote Config caused crashes.

@Override
public void onCreate() {
   ...
   FirebaseRemoteConfig.getInstance();
   MobileAds.initialize(this);
}

Above works fine when I launch the app normally. Then I call ProcessPhoenix.triggerRebirth(MyApplication.getContext()), the app restarts and then crashes with the exception below in line: FirebaseRemoteConfig.getInstance().

FATAL EXCEPTION: main
Process: com.myapp:phoenix, PID: 11061
java.lang.RuntimeException: Unable to create application com.myapp.activity.MyApplication: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.myapp:phoenix. Make sure to call FirebaseApp.initializeApp(Context) first.
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7624)
    at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2400)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8762)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.myapp:phoenix. Make sure to call FirebaseApp.initializeApp(Context) first.
    at com.google.firebase.FirebaseApp.getInstance(FirebaseApp.java:179)
    at com.google.firebase.remoteconfig.FirebaseRemoteConfig.getInstance(FirebaseRemoteConfig.java:80)
    at com.myapp.activity.MyApplication.initializeRemoteConfig(MyApplication.java:100)
    at com.myapp.activity.MyApplication.onCreate(MyApplication.java:60)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1266)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7619)
    at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2400) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loopOnce(Looper.java:226) 
    at android.os.Looper.loop(Looper.java:313) 
    at android.app.ActivityThread.main(ActivityThread.java:8762) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067) 

It's just not the Firebase remote config itself. When I remove Remote Config, then I got the exception below in line MobileAds.initialize(this). This time it is well catched by Firebase and the app doesn't crash but error is there.

ClassLoaderContext classpath size mismatch. expected=1, found=0 (PCL[/system/framework/com.android.location.provider.jar*3175360615] | PCL[])
2023-10-22 11:39:35.091 15021-15021 ft.myapp:phoenix         com.myapp                  W  Entry not found
2023-10-22 11:39:36.620 15021-15021 Ads                     com.myapp                  E  Error creating webview.
com.google.android.gms.ads.internal.webview.u: Webview initialization failed.
myappat com.google.android.gms.ads.internal.webview.v.a(:com.google.android.gms.policy_ads_fdr_dynamite@233012805@233012802057.555274289.555274289:78)
myappat com.google.android.gms.ads.internal.js.k.<init>(:com.google.android.gms.policy_ads_fdr_dynamite@233012805@233012802057.555274289.555274289:29)
myappat com.google.android.gms.ads.internal.js.q.run(:com.google.android.gms.policy_ads_fdr_dynamite@233012805@233012802057.555274289.555274289:23)
myappat com.google.android.gms.ads.internal.util.future.c.execute(:com.google.android.gms.policy_ads_fdr_dynamite@233012805@233012802057.555274289.555274289:15)
myappat com.google.android.gms.ads.internal.util.future.d.execute(:com.google.android.gms.policy_ads_fdr_dynamite@233012805@233012802057.555274289.555274289:3)
myappat com.google.android.gms.ads.internal.js.ah.b(:com.google.android.gms.policy_ads_fdr_dynamite@233012805@233012802057.555274289.555274289:25)
myappat com.google.android.gms.ads.internal.js.ah.a(:com.google.android.gms.policy_ads_fdr_dynamite@233012805@233012802057.555274289.555274289:77)
myappat com.google.android.gms.ads.internal.js.function.n.b(:com.google.android.gms.policy_ads_fdr_dynamite@233012805@233012802057.555274289.555274289:7)
myappat com.google.android.gms.ads.internal.e.b(:com.google.android.gms.policy_ads_fdr_dynamite@233012805@233012802057.555274289.555274289:233)
myappat com.google.android.gms.ads.internal.e.a(:com.google.android.gms.policy_ads_fdr_dynamite@233012805@233012802057.555274289.555274289:10)
myappat com.google.android.gms.ads.nonagon.az.i(:com.google.android.gms.policy_ads_fdr_dynamite@233012805@233012802057.555274289.555274289:117)
myappat com.google.android.gms.ads.internal.client.bu.bN(:com.google.android.gms.policy_ads_fdr_dynamite@233012805@233012802057.555274289.555274289:277)
myappat m.aig.onTransact(:com.google.android.gms.policy_ads_fdr_dynamite@233012805@233012802057.555274289.555274289:21)
myappat android.os.Binder.transact(Binder.java:1200)
myappat com.google.android.gms.internal.ads.zzatv.zzbh(com.google.android.gms:play-services-ads-base@@22.4.0:2)
myappat com.google.android.gms.ads.internal.client.zzcm.zzl(com.google.android.gms:play-services-ads-lite@@22.4.0:4)
myappat com.google.android.gms.ads.internal.client.zzej.zzz(com.google.android.gms:play-services-ads-lite@@22.4.0:4)
myappat com.google.android.gms.ads.internal.client.zzej.zzn(com.google.android.gms:play-services-ads-lite@@22.4.0:23)
myappat com.google.android.gms.ads.MobileAds.initialize(com.google.android.gms:play-services-ads-lite@@22.4.0:2)
myappat com.myapp.ads.AdsUtils.initializeAds(AdsUtils.java:76)
myappat com.myapp.activity.MyApplication.onCreate(MyApplication.java:67)
myappat android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1266)
myappat android.app.ActivityThread.handleBindApplication(ActivityThread.java:7619)
myappat android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
myappat android.app.ActivityThread$H.handleMessage(ActivityThread.java:2400)
myappat android.os.Handler.dispatchMessage(Handler.java:106)
myappat android.os.Looper.loopOnce(Looper.java:226)
myappat android.os.Looper.loop(Looper.java:313)
myappat android.app.ActivityThread.main(ActivityThread.java:8762)
myappat java.lang.reflect.Method.invoke(Native Method)
myappat com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
myappat com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
Caused by: java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported. https://crbug.com/558377 : Current process com.myapp:phoenix (pid 15021), lock owner com.myapp (pid 14631)
myappat org.chromium.android_webview.AwDataDirLock.b(chromium-TrichromeWebViewGoogle6432.aab-stable-593815333:186)
myappat org.chromium.android_webview.AwBrowserProcess.k(chromium-TrichromeWebViewGoogle6432.aab-stable-593815333:15)

I thought this might be about the singleton init process of Firebase. I've checked the code and saw the method isPhoenixProcess which is stated as to prevent running code that is not multi-process ready. Then I've changed the code to run when it is not a PhoenixProcess and voila! it worked with out any crashes or exceptions.

I want to know whether this is a correct approach or there is an issue with the library to fix. Since all of the replies to similar problems on the Internet only points out some gradle plugin issues with Firebase, I wanted to post this to make someone aware :)

nikunjparadva commented 11 months ago

Got the same error.

  FATAL EXCEPTION: main Process: test.demo.app.debug:phoenix, PID: 13998
                                                                                                java.lang.RuntimeException: Unable to create application test.demo.app.AppLockApplication: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process test.demo.app.debug:phoenix. Make sure to call FirebaseApp.initializeApp(Context) first.
                                                                                                    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6767)
                                                                                                    at android.app.ActivityThread.access$1500(ActivityThread.java:256)
                                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2091)
                                                                                                    at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                    at android.os.Looper.loopOnce(Looper.java:201)
                                                                                                    at android.os.Looper.loop(Looper.java:288)
                                                                                                    at android.app.ActivityThread.main(ActivityThread.java:7870)
                                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
                                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
                                                                                                Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process test.demo.app.debug:phoenix. Make sure to call FirebaseApp.initializeApp(Context) first.
                                                                                                    at com.google.firebase.FirebaseApp.getInstance(FirebaseApp.java:179)
                                                                                                    at com.google.firebase.remoteconfig.FirebaseRemoteConfig.getInstance(FirebaseRemoteConfig.java:81)
                                                                                                    at com.google.firebase.remoteconfig.ktx.RemoteConfigKt.getRemoteConfig(RemoteConfig.kt:46)
                                                                                                    at com.google.sdk_bmik.sd.initConfig(SourceFile:15)
                                                                                                    at com.google.sdk_bmik.ka.a(SourceFile:81)
                                                                                                    at com.google.sdk_bmik.ka.onCreate(SourceFile:18)
                                                                                                    at com.bmik.android.sdk.SDKBaseApplication.onCreate(SourceFile:2)
                                                                                                    at test.demo.app.AppLockApplication.onCreate(AppLockApplication.java:141)
                                                                                                    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1223)
                                                                                                    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6762)

Fixed with this one @aktasmehmet's Solution

if (!ProcessPhoenix.isPhoenixProcess(this)){
        // add the affected code here
    }