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

Flutter集成Google Mobile Ads时Gradle构建失败求助

错误含义解析

这个Gradle构建错误核心是无法处理Android 31版本的SDK平台文件:

  • 构建任务:google_mobile_ads:parseDebugLocalResources执行失败,根源是Gradle无法解析google_mobile_ads插件所需的androidApis配置依赖;
  • 具体表现为无法转换C:\Users\bayuk\AppData\Local\Android\sdk\platforms\android-31\android.jar文件以匹配android-platform-attr等属性要求,大概率是该android.jar文件损坏、Android 31 SDK未完整安装,或是google_mobile_ads插件版本与SDK版本不兼容导致的。
修复方案
  • 重新安装Android 31 SDK平台
    打开Android Studio的SDK Manager,找到Android 12(API 31),先卸载再重新安装,确保android.jar文件完整无损坏。
  • 更新google_mobile_ads插件版本
    当前使用的google_mobile_ads: ^1.2.0版本较旧,建议升级到最新稳定版(如^2.0.0+),新版本通常修复了SDK兼容性问题。修改pubspec.yaml中的依赖后,执行flutter pub get。
  • 调整Gradle配置的SDK版本
    打开项目android/app/build.gradle文件,确保compileSdkVersion、targetSdkVersion与已安装的SDK版本匹配,若不想使用API 31,可改为已完整安装的更高版本(如API 33、34)。
  • 清理构建缓存
    执行以下命令清除本地缓存后重新构建:
    flutter clean
    cd android && ./gradlew clean
    cd .. && flutter pub get
    
你的配置文件参考

pubspec.yaml 依赖部分

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.2
  get: ^4.6.6
  accordion: ^2.6.0
  firebase_core: ^2.17.0
  firebase_auth: ^4.10.1
  google_sign_in: ^6.1.5
  rflutter_alert: ^2.0.7
  firebase_database: ^10.3.1
  cloud_firestore: ^4.11.0
  google_mobile_ads: ^1.2.0

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application
        android:label="myapp"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3940256099942544~3347511713"/>
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

内容的提问来源于stack exchange,提问作者Muhammad Bayu Kurniawan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 05:47:33