Compose Compiler与Kotlin版本兼容问题求助
解决Compose Compiler与Kotlin版本不兼容的编译错误
问题概述
升级Android项目后出现编译失败,错误信息如下:
> Task :app:compileDebugKotlin FAILED e: This version (1.0.3) of the Compose Compiler requires Kotlin version 1.5.30 but you appear to be using Kotlin version 1.8.20 which is not known to be compatible. Please fix your configuration (or `suppressKotlinVersionCompatibilityCheck` but don't say I didn't warn you!).
已安装Android Studio Giraffe 2022.3.1的最新Kotlin插件(223-1.8.0-release-345-AS8836.35.2231.10406996),但问题未解决。项目的Gradle配置如下:
app级build.gradle
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' } android { buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion '1.0.3' kotlinCompilerVersion '1.5.30' } kotlinOptions { jvmTarget = "1.8" } namespace '__________' compileSdk 33 defaultConfig { applicationId "___________" minSdk 30 targetSdkVersion 33 versionCode 1 versionName "1.0.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" multiDexEnabled true vectorDrawables{ useSupportLibrary = true } } buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 } kotlinOptions { jvmTarget = "17" } dataBinding { enabled = true } } dependencies { implementation 'androidx.activity:activity-compose:1.7.2' implementation 'androidx.activity:activity-ktx:1.7.2' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.appcompat:appcompat-resources:1.6.1' implementation 'androidx.core:core-ktx:1.10.1' implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.multidex:multidex:2.0.1' implementation 'androidx.legacy:legacy-support-v4:1.0.0' // Lifecycle implementation "androidx.lifecycle:lifecycle-runtime-compose:2.6.1" implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.6.1" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1" implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1" implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:2.6.1" implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.6.1" // Compose implementation platform('androidx.compose:compose-bom:2023.06.01') implementation 'androidx.compose.ui:ui' implementation 'androidx.compose.ui:ui-graphics' implementation 'androidx.compose.ui:ui-tooling-preview:1.4.3' implementation 'androidx.compose.material3:material3:1.1.1' implementation "androidx.compose.material:material-icons-extended" implementation 'androidx.compose.material:material:1.4.3' implementation 'androidx.cardview:cardview:1.0.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' debugImplementation 'androidx.compose.ui:ui-tooling:1.4.3' debugImplementation 'androidx.compose.ui:ui-test-manifest:1.4.3' // Navigation implementation 'androidx.navigation:navigation-fragment-ktx:2.6.0' implementation 'androidx.navigation:navigation-ui-ktx:2.6.0' // Testing androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' testImplementation 'junit:junit:4.13.2' // Firebase implementation 'com.google.firebase:firebase-firestore:24.7.0' implementation 'com.google.firebase:firebase-auth:22.1.1' implementation 'com.google.firebase:firebase-messaging:23.2.1' implementation 'com.google.firebase:firebase-core:21.1.1' implementation 'com.google.firebase:firebase-storage:20.2.1' implementation 'com.google.android.gms:play-services-auth:20.6.0' implementation 'com.google.firebase:firebase-analytics:21.3.0' // Material Dialogs implementation 'com.afollestad.material-dialogs:core:3.3.0' implementation 'com.afollestad.material-dialogs:input:3.3.0' implementation 'com.afollestad.material-dialogs:datetime:3.3.0' implementation 'com.afollestad.material-dialogs:lifecycle:3.3.0' debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.12' // Coroutines implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3' }
项目级build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() mavenCentral() } dependencies { classpath 'com.google.gms:google-services:4.3.15' classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0' classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21' classpath 'com.android.tools.build:gradle:8.1.0' } } allprojects { repositories { google() mavenCentral() maven { url 'https://developer.android.com/studio/build/repository' } } } task clean(type: Delete) { delete rootProject.buildDir }
解决方案
1. 对齐Kotlin与Compose Compiler版本
Compose Compiler和Kotlin版本有严格的兼容对应关系,当前Kotlin插件版本是1.8.20,需匹配对应的Compose Compiler版本:
- 修改项目级build.gradle中的Kotlin Gradle插件版本为1.8.20:
dependencies { classpath 'com.google.gms:google-services:4.3.15' classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0' classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.20' // 升级到1.8.20 classpath 'com.android.tools.build:gradle:8.1.0' } - 修改app级build.gradle中的Compose Compiler版本为1.4.3(与Kotlin 1.8.20兼容),同时移除
kotlinCompilerVersion配置(无需手动指定,插件会自动匹配):composeOptions { kotlinCompilerExtensionVersion '1.4.3' // 升级到兼容版本 // 移除kotlinCompilerVersion '1.5.30' }
2. 清理冗余配置
app级build.gradle中重复定义了kotlinOptions,保留jvmTarget = "17"的配置,删除jvmTarget = "1.8"的那一段。
3. 同步并重建项目
- 点击Android Studio工具栏的「Sync Project with Gradle Files」按钮同步配置
- 执行「Build → Clean Project」清理旧构建文件,再执行「Build → Rebuild Project」重新编译项目
内容的提问来源于stack exchange,提问作者Scamparelli
相关产品推荐
相关产品推荐

