Flutter打包APK遇R8版本错误:需更新kotlinx-metadata-jvm
解决Flutter打包APK时R8版本不兼容错误
错误信息
ERROR: R8: java.lang.IllegalArgumentException: Provided Metadata instance has version 2.1.0, while maximum supported version is 2.0.0. To support newer versions, update the kotlinx-metadata-jvm library. FAILURE: Build failed with an exception.
问题详情
- 任务
:app:minifyReleaseWithR8执行失败
执行com.android.build.gradle.internal.tasks.R8Task$R8Runnable时出错,编译未完成
问题原因
当前使用的Kotlin Android插件版本(2.1.0)过高,与Android Gradle Plugin(AGP 8.3.2)内置的R8工具不兼容——R8仅支持最高2.0.0版本的kotlinx-metadata-jvm,而Kotlin 2.1.0会生成更高版本的元数据。
解决方案
方案1:降低Kotlin插件版本(推荐)
修改settings.gradle中的Kotlin插件版本为AGP 8.3.2兼容的1.9.x系列,比如1.9.24:
// settings.gradle中修改该行 id "org.jetbrains.kotlin.android" version "1.9.24" apply false
方案2:升级Android Gradle Plugin
将AGP版本升级到支持Kotlin 2.1.0的版本(如8.4.0及以上),同时同步更新Kotlin插件版本确保匹配:
// settings.gradle中修改 id "com.android.application" version "8.4.0" apply false id "org.jetbrains.kotlin.android" version "2.1.0" apply false
验证步骤
- 执行清理命令:
flutter clean - 重新打包Release APK:
flutter build apk --release
用户原配置文件
android/build.gradle
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" // START: FlutterFire Configuration id 'com.google.gms.google-services' // END: FlutterFire Configuration id "kotlin-android" // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins. id "dev.flutter.flutter-gradle-plugin" } android { namespace = "com.mynoona.noona" compileSdk = flutter.compileSdkVersion ndkVersion = "25.1.8937393" compileOptions { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } kotlinOptions { jvmTarget = JavaVersion.VERSION_17 } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId = "com.mynoona.noona" // 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 } 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 = "../.." }
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.2" apply false // START: FlutterFire Configuration id "com.google.gms.google-services" version "4.3.15" apply false // END: FlutterFire Configuration id "org.jetbrains.kotlin.android" version "2.1.0" apply false } include ":app"
gradle.properties
org.gradle.jvmargs=-Xmx4G -XX:MaxMetaspaceSize=2G -XX:+HeapDumpOnOutOfMemoryError android.useAndroidX=true android.enableJetifier=true
内容的提问来源于stack exchange,提问作者Asfandyar
相关产品推荐
相关产品推荐

