You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter集成Google Mobile Ads后编译报错且App启动崩溃

Flutter集成Google Mobile Ads后Kotlin版本不兼容崩溃的解决办法

问题情况

  • 刚完成首个Flutter应用开发,集成Google Mobile Ads插屏广告后,编译时报错:Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.6.0
  • 虽能完成编译,但安装到安卓手机后直接崩溃无法启动,移除广告依赖后应用恢复正常
  • 尝试修改安卓目录下build.gradle的Kotlin版本,但未找到对应配置行,其他无广告依赖的Flutter应用可正常运行
  • 已在AndroidManifest、Info.plist中添加AdMob配置,并导入相关依赖包

广告相关代码

AdDisplay类

class AdDisplay{
  final adUnitIdInterstitial = Platform.isAndroid
      ? 'ca-app-pub-3940256099942544/1033173712'
      : 'ca-app-pub-3940256099942544/4411468910';

   loadIntersitial (){
     InterstitialAd.load(
         adUnitId: adUnitIdInterstitial,
         request: const AdRequest(),
         adLoadCallback: InterstitialAdLoadCallback(
           onAdLoaded: (ad) {
             debugPrint('$ad loaded.');
             showInterstitial(ad);
           },
           onAdFailedToLoad: (LoadAdError error) {
             debugPrint('InterstitialAd failed to load: $error');
           },
         ));
  }

  void showInterstitial(InterstitialAd ad) {
     ad.show();
  }
}

触发广告的按钮代码

ElevatedButton(
                    onPressed: () {
                      AdDisplay().loadIntersitial();
                   
                    },
                    child: Padding(
                      padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 20.0),
                      child: Text(
                        'إعادة الحساب',
                        style: TextStyle(fontSize: 20, color: Colors.white),
                      ),
                    ),
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.pink,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(30.0),
                      ),
                    ),
                  ),

main函数代码

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  MobileAds.instance.initialize();
  runApp(BMICalculatorApp());
}

编译提示信息

Note: C:\Users\RGG\AppData\Local\Pub\Cache\hosted\pub.dev\google_mobile_ads-5.1.0\android\src\main\java\io\flutter\plugins\googlemobileads\FlutterRequestAgentProvider.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
C:/Users/RGG/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.8.21/43d50ab85bc7587adfe3dda3dbe579e5f8d51265/kotlin-stdlib-1.8.21.jar!/META-INF/kotlin-stdlib-jdk8.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0.

解决办法

1. 配置兼容的Kotlin版本

Flutter安卓项目的Kotlin版本配置通常在两个位置,按顺序检查修改:

  • 项目根目录/android/build.gradle:找到buildscript块,添加或修改ext.kotlin_version,版本号需与报错中的1.8.0兼容,比如用1.8.21或1.9.0:
buildscript {
    ext.kotlin_version = '1.8.21'
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.4.2" // gradle版本需与Kotlin版本匹配
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
  • 若上述文件无配置,修改android/app/build.gradle的插件版本:
plugins {
    id "com.android.application"
    id "kotlin-android" version "1.8.21"
    id "dev.flutter.flutter-gradle-plugin"
}

2. 清理缓存重新编译

  • 清理Flutter缓存:
flutter clean
flutter pub get
  • 清理Gradle缓存:删除C:\Users\你的用户名\.gradle\caches目录,再重新编译项目。

3. 升级Google Mobile Ads依赖版本

当前使用的是5.1.0版本,可在pubspec.yaml中升级到最新兼容版本:

dependencies:
  google_mobile_ads: ^6.1.0 # 替换为pub.dev上的最新版本

修改后执行flutter pub get,再编译项目。

4. 修复广告代码的引用问题

原代码中加载广告后直接显示,易因广告实例被GC回收导致崩溃,修改AdDisplay类保留实例引用:

class AdDisplay{
  final adUnitIdInterstitial = Platform.isAndroid
      ? 'ca-app-pub-3940256099942544/1033173712'
      : 'ca-app-pub-3940256099942544/4411468910';
  
  InterstitialAd? _interstitialAd; // 保存广告实例引用

  loadIntersitial (){
    InterstitialAd.load(
        adUnitId: adUnitIdInterstitial,
        request: const AdRequest(),
        adLoadCallback: InterstitialAdLoadCallback(
          onAdLoaded: (ad) {
            debugPrint('$ad loaded.');
            _interstitialAd = ad;
            showInterstitial();
          },
          onAdFailedToLoad: (LoadAdError error) {
            debugPrint('InterstitialAd failed to load: $error');
            _interstitialAd = null;
          },
        ));
  }

  void showInterstitial() {
    _interstitialAd?.show();
    _interstitialAd = null; // 显示后释放引用
  }
}

内容的提问来源于stack exchange,提问作者Skyro lark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 17:37:10