apache / cordova-android

Apache Cordova Android
https://cordova.apache.org/
Apache License 2.0
3.59k stars 1.52k forks source link

Increased number of ANRs after updating to Android 12 platform #1650

Open DavidTalevski opened 8 months ago

DavidTalevski commented 8 months ago

Bug Report

Problem

Google Android Vitals are reporting an increased amount of ANRs after updating to the Cordova Android 12 platform.

Information

I am unsure if the ANRs are caused by the new Cordova platform update or by the new Android SDK version. Since I was unable to find any issue similar to this, I decided to open one here. Any information related to this ANR would be most helpful, as I am unable to reproduce it. I am also curious if any other developers are experiencing the same thing.

Google stack trace

"main" tid=1 Blocked
  at android.content.res.AssetManager.getResourceValue (AssetManager.java:545)
  at android.content.res.ResourcesImpl.getValue (ResourcesImpl.java:232)
  at android.content.res.Resources.getInteger (Resources.java:1192)
  at org.chromium.ui.base.DeviceFormFactor.isTablet (chromium-TrichromeWebViewGoogle.aab-stable-579016630:9)
  at J.N.Mq8l4Aae (Native method)
  at org.chromium.android_webview.AwContents.A (chromium-TrichromeWebViewGoogle.aab-stable-579016630:373)
  at org.chromium.android_webview.AwContents.<init> (chromium-TrichromeWebViewGoogle.aab-stable-<US_SOCIAL_SECURITY_NUMBER>:383)
  at com.android.webview.chromium.k.run (chromium-TrichromeWebViewGoogle.aab-stable-579016630:111)
  at WV.QB0.b (chromium-TrichromeWebViewGoogle.aab-stable-579016630:20)
  at WV.PB0.run (chromium-TrichromeWebViewGoogle.aab-stable-579016630:3)
  at org.chromium.base.task.PostTask.d (chromium-TrichromeWebViewGoogle.aab-stable-579016630:11)
  at WV.QB0.a (chromium-TrichromeWebViewGoogle.aab-stable-579016630:28)
  at com.android.webview.chromium.WebViewChromiumFactoryProvider.a (chromium-TrichromeWebViewGoogle.aab-stable-579016630:3)
  at com.android.webview.chromium.WebViewChromium.init (chromium-TrichromeWebViewGoogle.aab-stable-579016630:332)
  at android.webkit.WebView.<init> (WebView.java:443)
  at android.webkit.WebView.<init> (WebView.java:363)
  at android.webkit.WebView.<init> (WebView.java:345)
  at android.webkit.WebView.<init> (WebView.java:332)
  at org.apache.cordova.engine.SystemWebView.<init> (SystemWebView.java:47)
  at org.apache.cordova.engine.SystemWebView.<init> (SystemWebView.java:43)
  at org.apache.cordova.engine.SystemWebViewEngine.<init> (SystemWebViewEngine.java:76)
  at java.lang.reflect.Constructor.newInstance0 (Native method)
  at java.lang.reflect.Constructor.newInstance (Constructor.java:343)
  at org.apache.cordova.CordovaWebViewImpl.createEngine (CordovaWebViewImpl.java:82)
  at org.apache.cordova.CordovaActivity.makeWebViewEngine (CordovaActivity.java)
  at org.apache.cordova.CordovaActivity.makeWebView (CordovaActivity.java)
  at org.apache.cordova.CordovaActivity.init (CordovaActivity.java:153)
  at org.apache.cordova.CordovaActivity.loadUrl (CordovaActivity.java:234)
  at com.appname.MainActivity.onCreate (MainActivity.java:40)
  at android.app.Activity.performCreate (Activity.java:8050)
  at android.app.Activity.performCreate (Activity.java:8030)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1329)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3648)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3832)
  at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:103)
  at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
  at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2247)
  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:7881)
  at java.lang.reflect.Method.invoke (Native method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:568)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1045)

Environment, Platform, Device

The ANR details specify that this ANR has been reported on Android 11, Android 12 and Android 13 devices. Here are some of the specific devices: BLU M0176_ND, NUU T0801LAV2, samsung a20p, samsung gta4lwifi, motorola borneo.

Version information

cordova 12.0.0 android 12.0.0

Checklist

breautek commented 8 months ago

If you're delaying the dismissal of the newer android splashscreen system introduced in cordova-android@11, it will trigger an ANR report.

The splashscreen should only appear roughly for about 1 second, but I think there is a 5 second timer for ANR triggers.

So that would be the first thing I'd check.

DavidTalevski commented 8 months ago

If you're delaying the dismissal of the newer android splashscreen system introduced in cordova-android@11, it will trigger an ANR report.

The splashscreen should only appear roughly for about 1 second, but I think there is a 5 second timer for ANR triggers.

So that would be the first thing I'd check.

Here are the preferences associated with the splashscreen in the config.xml:

<preference name="AutoHideSplashScreen" value="true" />
<preference name="SplashScreenDelay" value="0"/>
<preference name="ShowSplashScreenSpinner" value="false" />
<preference name="SplashShowOnlyFirstTime" value="false" />
<preference name="FadeSplashScreenDuration" value="500" />
<preference name="SplashMaintainAspectRatio" value="true" />
<preference name="AndroidWindowSplashScreenAnimatedIcon" value="res/screen/android/logo.png" />
<preference name="AndroidWindowSplashScreenBackground" value="#ffffff" />
<preference name="AndroidWindowSplashScreenIconBackgroundColor" value="#ffffff" />

I must mention that I am using this hook https://github.com/apache/cordova-android/issues/1465#issuecomment-1476238564 to edit the themes.xml file in order to set windowLayoutInDisplayCutoutMode to "never".

breautek commented 8 months ago

Your config indicates that you're letting Cordova auto hides the splashscreen with a 0 delay, which (without checking) I believe occurs when the webview loads up.

You can have a look at the splashscreen plugin that's embedded into the cordova framework to see when it exactly dismisses it. Note that by explicitly assigning SplashScreenDelay Cordova is treating this as a custom delay, so it creates a handler to be delayed 0ms later (in otherwords when the next time the main thread is free).

If this is taking more than 5 seconds to trigger then it could be a cause for the ANR. If this is the case, I suspect a plugin or plugin(s) to be the cause of the delay.

If you remove the SplashScreenDelay preference, or set it to -1 , then it should trigger Cordova to immediately set the keep flag to false, which allows the Android to dismiss the splashscreen as soon as possible, though this may occur before your webview actually has finished loading.

For more info on how splashscreens trigger ANR you can read: https://github.com/apache/cordova-android/issues/1510

If your splashscreen is dismissing quickly (less than 5 seconds), then splashscreen is probably not the source of your ANR. In which case, you'll probably need to use Android Studio to trace the problem.

DavidTalevski commented 8 months ago

Your config indicates that you're letting Cordova auto hides the splashscreen with a 0 delay, which (without checking) I believe occurs when the webview loads up.

You can have a look at the splashscreen plugin that's embedded into the cordova framework to see when it exactly dismisses it. Note that by explicitly assigning SplashScreenDelay Cordova is treating this as a custom delay, so it creates a handler to be delayed 0ms later (in otherwords when the next time the main thread is free).

If this is taking more than 5 seconds to trigger then it could be a cause for the ANR. If this is the case, I suspect a plugin or plugin(s) to be the cause of the delay.

If you remove the SplashScreenDelay preference, or set it to -1 , then it should trigger Cordova to immediately set the keep flag to false, which allows the Android to dismiss the splashscreen as soon as possible, though this may occur before your webview actually has finished loading.

For more info on how splashscreens trigger ANR you can read: #1510

If your splashscreen is dismissing quickly (less than 5 seconds), then splashscreen is probably not the source of your ANR. In which case, you'll probably need to use Android Studio to trace the problem.

I've done a lot of testing with SplashScreenDelay and I've gotten the best results by setting it to 0. Here are some averages I collected on a hot startup time on a Xiaomi Redmi Note 8 Pro:

Setting SplashScreenDelay to "-1" gave an average of +2s454ms. Setting SplashScreenDelay to "0" gave an average of +930ms. Removing the SplashScreenDelay preference gave the same results as setting it to "-1" which is normal as I believe it is the default value.

By reading the documentation on SplashScreenDelay it says that Android -1: The splash screen will automatically hide when the onPageFinished has been triggered., which, to my understanding, means that my page has to finish loading before the splashscreen is removed, which explains why it is about 1.5 seconds slower.

When I compare the app to other apps, the startup time is about twice to three times slower. Is there a way to hide the splashscreen before the page loads without setting a custom delay?