googleads / googleads-mobile-flutter

A Flutter plugin for the Google Mobile Ads SDK
Apache License 2.0
338 stars 277 forks source link

Several ANRs reported in Play Console (android) in the call to io.flutter.plugins.googlemobileads.FlutterMobileAdsWrapper.initialize #1019

Closed Gilianp closed 6 months ago

Gilianp commented 6 months ago

Describe your environment

Plugin Version

flutter: 3.16.9 google_mobile_ads: 4.0.0 platform is android

Describe the problem: Multiple ANRs are reported in the Play Console, in the call to "io.flutter.plugins.googlemobileads.FlutterMobileAdsWrapper.initialize". This ANR occurs very often, almost 10% of all ANRs in my app. Apparently, ANR occurs at initialization, when calling MobileAds.instance.initialize(). The reports come from Android versions 11, 10, 9 and 8.1. These are the logs reported in the play console:

  #00  pc 0x0000000000023824  /apex/com.android.runtime/lib/libdexfile.so (art::DexFileVerifier::CheckInterFieldIdItem()+60)
  #01  pc 0x0000000000024e19  /apex/com.android.runtime/lib/libdexfile.so (art::DexFileVerifier::CheckInterSectionIterate(unsigned int, unsigned int, art::DexFile::MapItemType)+192)
  #02  pc 0x0000000000024fc7  /apex/com.android.runtime/lib/libdexfile.so (art::DexFileVerifier::CheckInterSection()+110)
  #03  pc 0x000000000001e00d  /apex/com.android.runtime/lib/libdexfile.so (art::DexFileVerifier::Verify(art::DexFile const*, unsigned char const*, unsigned int, char const*, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>*)+156)
  #04  pc 0x000000000001bc17  /apex/com.android.runtime/lib/libdexfile.so (art::DexFileLoader::OpenCommon(unsigned char const*, unsigned int, unsigned char const*, unsigned int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, unsigned int, art::OatDexFile const*, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>*, std::__1::unique_ptr<art::DexFileContainer, std::__1::default_delete<art::DexFileContainer>>, art::DexFileLoader::VerifyResult*)+198)
  #05  pc 0x0000000000015689  /apex/com.android.runtime/lib/libdexfile.so (art::ArtDexFileLoader::OpenOneDexFileFromZip(art::ZipArchive const&, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>*, art::DexFileLoaderErrorCode*) const+700)
  #06  pc 0x0000000000015179  /apex/com.android.runtime/lib/libdexfile.so (art::ArtDexFileLoader::OpenAllDexFilesFromZip(art::ZipArchive const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>*, std::__1::vector<std::__1::unique_ptr<art::DexFile const, std::__1::default_delete<art::DexFile const>>, std::__1::allocator<std::__1::unique_ptr<art::DexFile const, std::__1::default_delete<art::DexFile const>>>>*) const+264)
  #07  pc 0x0000000000014f97  /apex/com.android.runtime/lib/libdexfile.so (art::ArtDexFileLoader::OpenZip(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>*, std::__1::vector<std::__1::unique_ptr<art::DexFile const, std::__1::default_delete<art::DexFile const>>, std::__1::allocator<std::__1::unique_ptr<art::DexFile const, std::__1::default_delete<art::DexFile const>>>>*) const+150)
  #08  pc 0x0000000000014db9  /apex/com.android.runtime/lib/libdexfile.so (art::ArtDexFileLoader::OpenWithMagic(unsigned int, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>*, std::__1::vector<std::__1::unique_ptr<art::DexFile const, std::__1::default_delete<art::DexFile const>>, std::__1::allocator<std::__1::unique_ptr<art::DexFile const, std::__1::default_delete<art::DexFile const>>>>*) const+204)
  #09  pc 0x0000000000014cc5  /apex/com.android.runtime/lib/libdexfile.so (art::ArtDexFileLoader::Open(char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>*, std::__1::vector<std::__1::unique_ptr<art::DexFile const, std::__1::default_delete<art::DexFile const>>, std::__1::allocator<std::__1::unique_ptr<art::DexFile const, std::__1::default_delete<art::DexFile const>>>>*) const+80)
  #10  pc 0x000000000032f8b9  /apex/com.android.runtime/lib/libart.so (art::OatFileManager::OpenDexFilesFromOat(char const*, _jobject*, _jobjectArray*, art::OatFile const**, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>>*)+2232)
  #11  pc 0x00000000002ffa05  /apex/com.android.runtime/lib/libart.so (art::DexFile_openDexFileNative(_JNIEnv*, _jclass*, _jstring*, _jstring*, int, _jobject*, _jobjectArray*)+88)
  at dalvik.system.DexFile.openDexFileNative (Native method)
  at dalvik.system.DexFile.openDexFile (DexFile.java:365)
  at dalvik.system.DexFile.<init> (DexFile.java:107)
  at dalvik.system.DexFile.<init> (DexFile.java:80)
  at dalvik.system.DexPathList.loadDexFile (DexPathList.java:444)
  at dalvik.system.DexPathList.makeDexElements (DexPathList.java:403)
  at dalvik.system.DexPathList.<init> (DexPathList.java:164)
  at dalvik.system.BaseDexClassLoader.<init> (BaseDexClassLoader.java:126)
  at dalvik.system.BaseDexClassLoader.<init> (BaseDexClassLoader.java:101)
  at dalvik.system.PathClassLoader.<init> (PathClassLoader.java:74)
  at com.android.internal.os.ClassLoaderFactory.createClassLoader (ClassLoaderFactory.java:87)
  at com.android.internal.os.ClassLoaderFactory.createClassLoader (ClassLoaderFactory.java:116)
  at android.app.ApplicationLoaders.getClassLoader (ApplicationLoaders.java:114)
  at android.app.ApplicationLoaders.getClassLoaderWithSharedLibraries (ApplicationLoaders.java:60)
  at android.app.LoadedApk.createOrUpdateClassLoaderLocked (LoadedApk.java:851)
  at android.app.LoadedApk.getClassLoader (LoadedApk.java:950)
  at android.app.ContextImpl.getClassLoader (ContextImpl.java:372)
  at com.google.android.gms.dynamic.RemoteCreator.getRemoteCreatorInstance (com.google.android.gms:play-services-basement@@18.2.0:4)
  at com.google.android.gms.ads.internal.client.zzeq.zza (com.google.android.gms:play-services-ads-lite@@22.5.0:2)
  at com.google.android.gms.ads.internal.client.zzaq.zzc (com.google.android.gms:play-services-ads-lite@@22.5.0:11)
  at com.google.android.gms.ads.internal.client.zzax.zzf (com.google.android.gms:play-services-ads-lite@@22.5.0:1)
  at com.google.android.gms.ads.internal.client.zzax.zzd (com.google.android.gms:play-services-ads-lite@@22.5.0:11)
  at com.google.android.gms.ads.internal.client.zzej.zzA (com.google.android.gms:play-services-ads-lite@@22.5.0:3)
  at com.google.android.gms.ads.internal.client.zzej.zzn (com.google.android.gms:play-services-ads-lite@@22.5.0:8)
  at com.google.android.gms.ads.MobileAds.initialize (com.google.android.gms:play-services-ads-lite@@22.5.0:3)
  at io.flutter.plugins.googlemobileads.FlutterMobileAdsWrapper.initialize (FlutterMobileAdsWrapper.java:39)
  at io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.onMethodCall (GoogleMobileAdsPlugin.java:341)
  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage (MethodChannel.java:267)
  at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler (DartMessenger.java:295)
  at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0 (DartMessenger.java:322)
  at android.os.Handler.handleCallback (Handler.java:883)
  at android.os.Handler.dispatchMessage (Handler.java:100)
  at android.os.Looper.loop (Looper.java:214)
  at android.app.ActivityThread.main (ActivityThread.java:7386)
  at java.lang.reflect.Method.invoke (Native method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:492)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:980)
img console

Steps to Reproduce

I cannot reproduce this ANR in my environment. But the only thing I do is call MobileAds.instance.initialize() (without await) in main.dart

Logs ``` ``` ``` ``` [√] Flutter (Channel stable, 3.16.9, on Microsoft Windows [Version 10.0.19042.1110], locale pt-BR) • Flutter version 3.16.9 on channel stable at G:\SDKs e frameworks\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 41456452f2 (6 weeks ago), 2024-01-25 10:06:23 -0800 • Engine revision f40e976bed • Dart version 3.2.6 • DevTools version 2.28.5 [√] Windows Version (Installed version of Windows is version 10 or higher) Error 1 retrieving device properties for sdk gphone64 x86 64: error: closed [!] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at G:\sdk_android • Platform android-34, build-tools 34.0.0 • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301) X Android license status unknown. Run `flutter doctor --android-licenses` to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/windows#android-setup for more details. [√] Chrome - develop for the web • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe [!] Visual Studio - develop Windows apps (Visual Studio Community 2019 16.11.0) • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community • Visual Studio Community 2019 version 16.11.31605.320 • Windows 10 SDK version 10.0.16299.0 X Visual Studio is missing necessary components. Please re-run the Visual Studio installer for the "Desktop development with C++" workload, and include these components: MSVC v142 - VS 2019 C++ x64/x86 build tools - If there are multiple build tool versions available, install the latest C++ CMake tools for Windows Windows 10 SDK [√] Android Studio (version 2023.1) • Android Studio at C:\Program Files\Android\Android Studio1 • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314) • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart [√] Connected device (4 available) • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-arm • Android null (API null) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19042.1110] • Chrome (web) • chrome • web-javascript • Google Chrome 122.0.6261.71 • Edge (web) • edge • web-javascript • Microsoft Edge 122.0.2365.80 [√] Network resources • All expected network resources are available. ``` ```
Phlphat746 commented 6 months ago

คุณก็แก้ปัญหาใด้เก่งนี่ครับไม่ใช่เรื่องร้ายแรงอะไรสักหน่อย.

malandr2 commented 6 months ago

Hi @Gilianp, this appears to be an issue on app start. These crashes may be the result of the main thread doing too much work for older devices. Since it is only occurring in the Android layer, have you tried optimizing SDK initialization? This is one way to lighten the load on the main thread during app start.

Gilianp commented 6 months ago

Hi @Gilianp, this appears to be an issue on app start. These crashes may be the result of the main thread doing too much work for older devices. Since it is only occurring in the Android layer, have you tried optimizing SDK initialization? This is one way to lighten the load on the main thread during app start.

Hi Malandruccolo, thanks for your tip about optimizing the admob SDK on startup, I didn't really know about it! Investigating the Play Console reports further, I discovered that in fact, the ANRs were coming from older devices. It seems this optimization is what I need, thanks again!