JakeWharton / JakeWharton/ProcessPhoenix

Firebase initialization problems after the rebirth/restart

Open
#60 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.1k
Forks
138
Avg merge
1h 23m
Merged PRs (30d)
11

Description

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.(: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 :)

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading the library's isPhoenixProcess path and compare it with the reported Application.onCreate calls for Firebase Remote Config and Mobile Ads. Reproduce the restart with those initializers in the Phoenix process, then establish whether the library needs a change or the caller must skip process-specific initialization; a regression test or documented limitation should make the result verifiable.

Written by the indexing model from the issue text.

Assessment

Tech stack
firebase, java
Domain
mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.