Flutter构建APK时遭遇Kotlin版本不兼容及Gradle相关错误,请求解决
我在构建APK时遇到了以下错误:
C:/Users/Hao/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.9.10/dafaf2c27f27c09220cee312df10917d9a5d97ce/kotlin-stdlib-common-1.9.10.jar!/META-INF/kotlin-stdlib-common.kotlin_module: 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.
还有一些类似的错误,元数据的二进制版本是1.8.0或1.9.9999,预期版本同样是1.6.0。
我尝试过以下解决方法,但都没有效果:
- 在
android\build.gradle中配置ext.kotlin_version,但没有任何变化 - 尝试降级Kotlin版本,却收到“版本过旧”的提示,导致构建失败
- 清理/重建项目,没有改善
- 清理
Users/user/.android下的缓存,也没用 - 配置Android Studio的设置/项目结构/插件
- 执行
./gradlew build同步Gradle时,又出现新错误:无法确定任务:google_mobile_ads:test的依赖项
Could not create task ':google_mobile_ads:testProfileUnitTest'.
this and base files have different roots: E:\Projects\project\build\google_mobile_ads and C:\Users\Hao\AppData\Local\Pub\Cache\hosted\pub.dev\google_mobile_ads-5.2.0\android.
自错误出现以来,我已经修改了以下配置文件:
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 "7.3.1" apply false //downgraded and upgraded id "org.jetbrains.kotlin.android" version "1.9.10" apply false //changed to 1.6.x / 1.7.x / 1.8.x id 'com.google.gms.google-services' version '4.4.2' apply false //changed to 4.3.15 for another error } include ":app"
android\build.gradle
buildscript { ext.kotlin_version = '1.9.10' //comment in, out repositories { google() mavenCentral() // maven("https://maven.pkg.jetbrains.space/public/p/compose/dev") } dependencies { classpath("com.android.tools.build:gradle:7.3.1") classpath("com.google.gms:google-services:4.4.2") //changed with the one in settings.gradle classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version") // comment in, out with ext.kotlin_version } } allprojects { repositories { google() mavenCentral() } } rootProject.buildDir = "../build" subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } subprojects { project.evaluationDependsOn(":app") } tasks.register("clean", Delete) { delete rootProject.buildDir }
app\build.gradle
plugins { id "com.android.application" id "kotlin-android" // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins. id "dev.flutter.flutter-gradle-plugin" id "com.google.gms.google-services" } dependencies { implementation platform('com.google.firebase:firebase-bom:33.5.1') implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version") implementation('com.google.firebase:firebase-auth:23.1.0') implementation('com.google.firebase:firebase-firestore:25.1.1') implementation('com.google.android.gms:play-services-base:18.5.0') implementation("com.google.android.gms:play-services-ads:23.5.0") implementation("com.google.firebase:firebase-ads:23.5.0") // implementation("androidx.core:core-ktx:1.9.0") // implementation('com.google.android.gms:play-services-security:18.0.0') // implementation('androidx.appcompat:appcompat:1.7.0') // implementation('androidx.legacy:legacy-support-v4:1.0.0') // implementation('androidx.annotation:annotation:1.9.1') // configurations.all { // resolutionStrategy { // force "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // force "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" // force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" // force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" // } // } } //apply plugins('com.google.gms.google-services') def localProperties = new Properties() def localPropertiesFile = rootProject.file("local.properties") if (localPropertiesFile.exists()) { localPropertiesFile.withReader("UTF-8") { reader -> localProperties.load(reader) } } def flutterVersionCode = localProperties.getProperty("flutter.versionCode") if (flutterVersionCode == null) { flutterVersionCode = "1" } def flutterVersionName = localProperties.getProperty("flutter.versionName") if (flutterVersionName == null) { flutterVersionName = "1.0" } android { namespace = "com.example.tictactoe_rebuild" compileSdk = flutter.compileSdkVersion ndkVersion = flutter.ndkVersion compileOptions { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId = "com.example.tictactoe_rebuild" // You can update the following values to match your application needs. // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration. minSdk = 23/*flutter.minSdkVersion*/ targetSdk = flutter.targetSdkVersion versionCode = flutterVersionCode.toInteger() versionName = flutterVersionName } kotlinOptions { jvmTarget = "17" } buildTypes { release { // TODO: Add your own signing config for the release build. // Signing with the debug keys for now, so `flutter run --release` works. signingConfig = signingConfigs.debug } } } flutter { source = "../.." }
我需要解决这些问题,才能让广告正常运行。
备注:内容来源于stack exchange,提问作者Quoc Hao




