Android Studio Dolphin中Compose Compiler与Kotlin版本兼容问题求助
使用Android Studio Dolphin | 2021.3.1开发项目时构建报错:当前使用的Compose Compiler版本1.2.0-alpha05要求Kotlin版本1.6.10,但检测到使用的是Kotlin 1.6.21。错误提示建议修复配置或使用suppressKotlinVersionCompatibilityCheck标记,但添加标记后构建仍失败。
项目已在根构建文件中设置Kotlin版本为1.6.10:
buildscript { ext.kotlin_version = '1.6.10' }
此前曾将Kotlin升级到1.7.10后降级回1.6.10,但问题依旧。尝试显式添加androidx.compose.compiler:compiler依赖也被忽略,找不到Kotlin 1.6.21和Compose Compiler 1.2.0-alpha05的配置来源,构建文件中均未配置这两个版本。
项目模块构建文件(app/build.gradle):
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' apply plugin: 'androidx.navigation.safeargs' apply plugin: 'com.google.firebase.crashlytics' android { namespace "com.blah" compileSdk 33 defaultConfig { applicationId "com.blah" minSdk 21 targetSdk 33 versionCode 123 versionName "1.2.3" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } flavorDimensions "environment" productFlavors { staging { dimension "environment" applicationIdSuffix ".staging" } production { dimension "environment" } } applicationVariants.all { variant -> variant.outputs.all { output -> outputFileName = "MyApp-${variant.versionName}-${variant.name}.apk" } } sourceSets { main { jniLibs.srcDirs = ['libs'] } } buildFeatures { dataBinding true compose true } lint { disable 'RtlHardcoded' } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) // Coroutines implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.2' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.2' // Android X Jetpack implementation "androidx.appcompat:appcompat:1.5.1" implementation "androidx.lifecycle:lifecycle-extensions:2.2.0" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1" implementation 'androidx.core:core-ktx:1.9.0' implementation "androidx.constraintlayout:constraintlayout:2.1.4" implementation "androidx.recyclerview:recyclerview:1.2.1" implementation "androidx.fragment:fragment-ktx:1.5.2" implementation "androidx.collection:collection-ktx:1.2.0" implementation "androidx.window:window:1.0.0" // Jetpack Compose implementation("androidx.compose.ui:ui:1.2.1") implementation("androidx.compose.ui:ui-tooling:1.2.1") // Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.) implementation("androidx.compose.foundation:foundation:1.2.1") implementation("androidx.compose.material:material:1.2.1") implementation("androidx.compose.material:material-icons-core:1.2.1") implementation("androidx.compose.material:material-icons-extended:1.2.1") // Integration with observables implementation("androidx.compose.runtime:runtime-livedata:1.2.1") implementation("androidx.compose.runtime:runtime-rxjava2:1.2.1") // Google implementation "com.google.android.material:material:1.6.1" implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0' // Navigation Component implementation "androidx.navigation:navigation-fragment-ktx:2.5.2" implementation "androidx.navigation:navigation-ui-ktx:2.5.2" // Firebase & Crashlytics implementation "com.google.firebase:firebase-core:21.1.1" implementation "com.google.firebase:firebase-analytics:21.1.1" implementation "com.google.firebase:firebase-crashlytics:18.2.13" implementation "com.google.firebase:firebase-messaging:21.0.1" // Urban Airship push messaging implementation "com.urbanairship.android:urbanairship-fcm:13.3.5" // Twilio (video calling) implementation "com.twilio:video-android-ktx:7.1.1" implementation "com.twilio:audioswitch:1.1.4" // ExoPlayer implementation "com.google.android.exoplayer:exoplayer-core:2.18.1" implementation "com.google.android.exoplayer:exoplayer-ui:2.18.1" // Third-party implementation "com.jakewharton.timber:timber:5.0.1" implementation "com.jakewharton.threetenabp:threetenabp:1.4.0" implementation "com.squareup.retrofit2:retrofit:2.9.0" implementation "com.squareup.retrofit2:converter-moshi:2.9.0" implementation "com.squareup.picasso:picasso:2.71828" implementation "net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:2.3.0" implementation "com.squareup.okhttp3:okhttp:4.9.3" implementation "com.squareup.okhttp3:logging-interceptor:4.9.3" // Open Source Licence List implementation "com.github.franmontiel:AttributionPresenter:1.0.1" // Shake to Send Feedback implementation "com.linkedin.shaky:shaky:3.0.4" implementation "com.jraska:falcon:2.2.0" // Page Indicator View implementation 'com.github.romandanylyk:PageIndicatorView:v.1.0.3' // Testing implementation "androidx.legacy:legacy-support-v4:1.0.0" testImplementation "junit:junit:4.13.2" testImplementation "androidx.test:core:1.4.0" testImplementation "org.mockito:mockito-core:4.6.0" androidTestImplementation "androidx.test:runner:1.4.0" androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0" androidTestImplementation("androidx.compose.ui:ui-test-junit4:1.2.1") apply plugin: 'com.google.gms.google-services' }
根目录构建文件(build.gradle):
apply plugin: "com.github.ben-manes.versions" buildscript { ext.kotlin_version = '1.6.10' repositories { google() mavenCentral() gradlePluginPortal() } dependencies { classpath 'com.android.tools.build:gradle:7.3.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.5.2" classpath "com.google.firebase:firebase-crashlytics-gradle:2.9.2" classpath "com.google.gms:google-services:4.3.14" classpath "com.github.ben-manes:gradle-versions-plugin:0.42.0" } } allprojects { repositories { google() maven { url "https://jitpack.io" } mavenCentral() jcenter() { content { includeModule("com.google.android", "flexbox") } } } } task clean(type: Delete) { delete rootProject.buildDir }
1. 统一Compose与Kotlin版本匹配
Compose Compiler和Kotlin版本有严格绑定关系,你当前用的Compose 1.2.1需要搭配Kotlin 1.7.10,而非1.6.10:
- 更新根
build.gradle的ext块:
ext { kotlin_version = '1.7.10' compose_compiler_version = '1.3.0' // 与Kotlin 1.7.10匹配的版本 }
- 在模块
build.gradle的dependencies中显式添加Compose Compiler依赖,覆盖默认版本:
dependencies { // ... 其他依赖 implementation "androidx.compose.compiler:compiler:$compose_compiler_version" }
2. 彻底清理缓存并同步
- 执行
File > Invalidate Caches... > Invalidate and Restart,清除Android Studio缓存 - 手动删除项目根目录下的
.gradle、build文件夹,以及模块的build文件夹 - 重新执行
Sync Project with Gradle Files
3. 排查并解决依赖冲突
用Gradle命令分析依赖树,找出引入Kotlin 1.6.21或旧版Compose Compiler的来源:
./gradlew app:dependencies --configuration implementation
找到冲突依赖后,在模块build.gradle中添加强制版本规则:
configurations.all { resolutionStrategy { force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" force "androidx.compose.compiler:compiler:$compose_compiler_version" } }
将这段代码放在android块之后。
4. 验证IDE Kotlin插件版本
打开File > Settings > Languages & Frameworks > Kotlin,确保插件版本与项目配置的kotlin_version一致(1.7.10),不一致则点击Update升级插件。
版本对应参考
后续升级时遵循以下匹配规则:
- Compose 1.2.x → Kotlin 1.7.10
- Compose 1.1.x → Kotlin 1.6.10
- Compose 1.3.x → Kotlin 1.8.0+
内容的提问来源于stack exchange,提问作者Ollie

