Flutter集成google_mobile_ads后Release模式崩溃问题(Admob相关)
问题
在Flutter项目中集成google_mobile_ads: ^6.0.0插件后,Debug和Profile模式运行完全正常,但使用Play商店已存在的自有应用包名打包Release版本时,安装后应用立即崩溃;若使用随机测试包名(如com.something.test)则无崩溃问题。
报错日志
FATAL EXCEPTION: main E/AndroidRuntime(11466): Process: com.ragory.lse, PID: 11466 E/AndroidRuntime(11466): java.lang.VerifyError: Rejecting class com.google.android.gms.internal.ads.JM that attempts to sub-type erroneous class com.google.android.gms.internal.ads.mN (declaration of 'com.google.android.gms.internal.ads.JM' appears in /data/app/~~DddutjzhIYdS_tcJD_fw0w==/com.ragory.lse-EL0YXaJbxpaaDFtCJqlcGw==/base.apk) E/AndroidRuntime(11466): at com.google.android.gms.internal.ads.If.s(SourceFile:3) E/AndroidRuntime(11466): at com.google.android.gms.internal.ads.bL.<init>(SourceFile:128) E/AndroidRuntime(11466): at com.google.android.gms.internal.ads.KL.<init>(Unknown Source:13) E/AndroidRuntime(11466): at com.google.android.gms.internal.ads.If.<init>(SourceFile:117) E/AndroidRuntime(11466): at com.google.android.gms.internal.ads.Z9.h(SourceFile:324) E/AndroidRuntime(11466): at com.google.android.gms.internal.ads.jg.X(SourceFile:86) E/AndroidRuntime(11466): at com.google.android.gms.internal.ads.jg.W0(SourceFile:141) E/AndroidRuntime(11466): at com.google.android.gms.internal.ads.ab.run(SourceFile:1391) E/AndroidRuntime(11466): at android.os.Handler.handleCallback(Handler.java:959) E/AndroidRuntime(11466): at android.os.Handler.dispatchMessage(Handler.java:100) E/AndroidRuntime(11466): at com.google.android.gms.internal.ads.Wy.a(SourceFile:1) E/AndroidRuntime(11466): at N2.L.a(SourceFile:1) E/AndroidRuntime(11466): at com.google.android.gms.internal.ads.Wy.dispatchMessage(SourceFile:10) E/AndroidRuntime(11466): at android.os.Looper.loopOnce(Looper.java:232) E/AndroidRuntime(11466): at android.os.Looper.loop(Looper.java:317) E/AndroidRuntime(11466): at android.app.ActivityThread.main(ActivityThread.java:8592) E/AndroidRuntime(11466): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(11466): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580) E/AndroidRuntime(11466): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878) E/AndroidRuntime(11466): Caused by: java.lang.VerifyError: Verifier rejected class com.google.android.gms.internal.ads.mN: void com.google.android.gms.internal.ads.mN.F() failed to verify: void com.google.android.gms.internal.ads.mN.F(): [0xF0] expected to be within a catch-all for an instruction where a monitor is held (declaration of 'com.google.android.gms.internal.ads.mN' appears in /data/app/~~DddutjzhIYdS_tcJD_fw0w==/com.ragory.lse-EL0YXaJbxpaaDFtCJqlcGw==/base.apk) E/AndroidRuntime(11466): ... 19 more E/flutter (12022): [ERROR:flutter/lib/ui/dart_runtime_hooks.cc(38)] Dart Error: Dart_LookupLibrary: library 'xtw' not found. E/flutter (12022): [ERROR:flutter/shell/common/shell.cc(117)] Dart Error: Dart_LookupLibrary: library 'xtw' not found. E/flutter (12022): [ERROR:flutter/shell/common/shell.cc(117)] Dart Error: Dart_LookupLibrary: library 'xtw' not found.
相关配置信息
settings.gradle
pluginManagement { def flutterSdkPath = { def properties = new Properties() file("local.properties").withInputStream { properties.load(it) } def flutterSdkPath = properties.getProperty("flutter.sdk") assert flutterSdkPath != null, "flutter.sdk not set in local.properties" return flutterSdkPath }() includeBuild("$flutterSdkPath/packages/flutter_tools/gradle") repositories { google() mavenCentral() gradlePluginPortal() } } plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.0" id "com.android.application" version "8.3.0" apply false id "org.jetbrains.kotlin.android" version "2.0.20" apply false id "com.google.gms.google-services" version "4.3.15" apply false id "com.google.firebase.crashlytics" version "2.8.1" apply false } include ":app"
app/build.gradle
defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId = "com.example.admob_testing" // You can update the following values to match your application needs. // For more information, see: https://flutter.dev/to/review-gradle-config. minSdk = 23 targetSdk = flutter.targetSdkVersion versionCode = flutter.versionCode versionName = flutter.versionName multiDexEnabled true } dependencies { coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3' implementation ("com.android.support:multidex:2.0.1") }
gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
问题原因分析
- 签名验证冲突:已在Play商店发布的包名会关联Play应用签名,本地打包使用的签名与Play商店的签名不一致时,Android系统会拒绝验证AdMob相关的内部类字节码,触发
VerifyError。测试包名未在Play商店注册,无签名验证冲突,因此可正常运行。 - 混淆规则缺失:Release模式默认开启代码混淆,若未添加
google_mobile_ads的专属混淆规则,混淆过程会误删AdMob依赖的关键类/方法,导致类继承关系失效(如JM类无法继承错误状态的mN类)。测试包名的打包配置可能未触发严格混淆,因此无此问题。 - 编译产物与包名不匹配:报错中的
Dart_LookupLibrary: library 'xtw' not found说明,Release编译生成的Dart库与当前包名绑定的资源不匹配。已发布包名关联了特定的编译配置或资源文件,本地打包时未同步这些配置,导致运行时无法找到对应Dart库。
内容的提问来源于stack exchange,提问作者Shafi Munshi
相关产品推荐
相关产品推荐

