升级Compose版本至1.2.1失败,移除kotlinCompilerExtensionVersion却正常?
我使用Android Studio Chipmunk | 2021.2.1 Patch 1的「Empty Compose Activity」向导创建项目,原本配置compose_version = '1.1.0-beta01'时可正常编译运行。将compose_version升级至1.2.1后,出现如下编译错误:
Could not resolve all files for configuration ':app:kotlin-extension'.
Could not find androidx.compose.compiler:compiler:1.2.1.
Searched in the following locations:
- https://dl.google.com/dl/android/maven2/androidx/compose/compiler/compiler/1.2.1/compiler-1.2.1.pom
- https://repo.maven.apache.org/maven2/androidx/compose/compiler/compiler/1.2.1/compiler-1.2.1.pom
但移除Module级build.gradle中composeOptions内的kotlinCompilerExtensionVersion compose_version配置后,应用又能正常运行,这是为什么?
项目级build.gradle
buildscript { ext { compose_version = '1.1.0-beta01' //I will upgrade it to 1.2.1 } } plugins { id 'com.android.application' version '7.2.1' apply false id 'com.android.library' version '7.2.1' apply false id 'org.jetbrains.kotlin.android' version '1.5.31' apply false }
模块级build.gradle
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' } android { compileSdk 32 defaultConfig { applicationId "com.example.myapplication" minSdk 25 targetSdk 32 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary true } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } composeOptions { kotlinCompilerExtensionVersion compose_version //OK after I removed it } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } buildFeatures { compose true } packagingOptions { resources { excludes += '/META-INF/{AL2.0,LGPL2.1}' } } } dependencies { implementation 'androidx.core:core-ktx:1.7.0' implementation "androidx.compose.ui:ui:$compose_version" implementation "androidx.compose.material:material:$compose_version" implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1' implementation 'androidx.activity:activity-compose:1.3.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version" debugImplementation "androidx.compose.ui:ui-tooling:$compose_version" debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version" }
Compose Compiler版本与UI库版本不再绑定
在Compose 1.1.x及更早版本,Compiler(编译器)版本和UI库版本是完全一致的,但从1.2.x版本开始,Compiler版本号独立迭代——根本不存在androidx.compose.compiler:compiler:1.2.1这个版本。比如Compose UI 1.2.1对应的Compiler版本是1.3.2,你硬指定1.2.1自然会找不到资源。移除配置后AGP会自动匹配兼容版本
当你删掉kotlinCompilerExtensionVersion配置时,Android Gradle插件(AGP)会自动根据你当前使用的Compose UI库版本、Kotlin版本,选择一个兼容的Compiler版本。你的项目用的是Kotlin 1.5.31,AGP会匹配到和它兼容的Compiler版本,因此编译能正常通过。额外注意:Kotlin版本需同步升级
你的项目Kotlin版本是1.5.31,但Compose 1.2.1要求最低Kotlin版本为1.7.10。虽然现在移除配置能临时运行,但长期会有兼容性隐患,建议将Kotlin版本升级到1.7.10及以上,同时手动指定正确的Compiler版本(比如Compose UI 1.2.1对应Compiler 1.3.2)。
内容的提问来源于stack exchange,提问作者HelloCW

