Kotlin结合Jetpack Compose开发应用遇Gradle编译依赖错误,找不到指定版本Compose编译器
问题根因
- 你当前配置的
androidx.compose.compiler:compiler:0.1.0-dev07属于2019年的超早期Compose开发预览版,这类极早期dev版本早已从所有公共Maven仓库下架,无论新增多少仓库配置,都无法拉取到对应构件,不存在通过补仓库解决的可能。 - 你当前的版本组合完全不兼容:Kotlin 1.3.61、AGP 7.0.0-alpha14、Compose编译器扩展0.1.0-dev07、配套UI依赖0.1.0-dev07的版本搭配,和当前公开可获取的版本链路完全错位。
可落地解决步骤
- 第一步:全量对齐可获取的稳定版本组合,替换所有已下架的超旧版本
- 调整项目级根
build.gradle配置
将原Kotlin版本从1.3.61替换为和Compose编译器适配的稳定版1.9.22;将原AGP(Android Gradle插件)版本从7.0.0-alpha14替换为稳定版7.4.2;同时删除已停止服务的JCenter仓库配置,替换为Maven Central仓库。
调整后的项目级构建脚本参考:buildscript { ext.kotlin_version = "1.9.22" repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.4.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() mavenCentral() maven { url 'https://jitpack.io' } } } task clean(type: Delete) { delete rootProject.buildDir } - 调整app模块下的
build.gradle配置- 删除已废弃的
kotlin-android-extensions插件引用 - 将
compileSdkVersion、targetSdkVersion调整为34,匹配当前稳定版Compose的要求 - 将原Compose编译器扩展版本从
0.1.0-dev07替换为匹配Kotlin1.9.22的稳定版1.5.8 - 将原所有
androidx.ui开头的0.1.0-dev07版本依赖,替换为当前官方稳定坐标的依赖 - 将原
compose-router:0.7.0版本替换为适配新Compose版本的0.26.1 - 同步将appcompat、core-ktx、lifecycle等基础依赖升级到对应匹配的稳定版本
调整后的app模块构建脚本参考:
apply plugin: 'kotlin-android' android { compileSdkVersion 34 defaultConfig { applicationId "com.github.zsoltk.pokedex" minSdkVersion 21 targetSdkVersion 34 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion "1.5.8" } } dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.core:core-ktx:1.12.0' implementation "androidx.lifecycle:lifecycle-livedata:2.7.0" // 替换原早期dev版Compose依赖 implementation 'androidx.compose.ui:ui:1.6.0' implementation 'androidx.compose.foundation:foundation:1.6.0' implementation 'androidx.compose.material:material:1.6.0' implementation 'androidx.compose.ui:ui-tooling-preview:1.6.0' debugImplementation 'androidx.compose.ui:ui-tooling:1.6.0' // 替换原旧版compose-router implementation 'com.github.zsoltk:compose-router:0.26.1' implementation "io.reactivex.rxjava2:rxjava:2.2.17" implementation "io.reactivex.rxjava2:rxandroid:2.1.1" testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' } - 删除已废弃的
- 调整项目级根
- 第二步:同步调整Gradle wrapper版本
把项目下gradle/wrapper/gradle-wrapper.properties里的Gradle版本调整为7.5,匹配AGP 7.4.2的最低版本要求。 - 第三步:清理缓存重新构建
先执行clean任务清理旧的构建缓存,再重新触发Gradle同步,即可正常拉取所有依赖完成构建。
注意:调整过程中不要强行留存
0.1.0-dev07这类已下架的超旧版本依赖,否则即使补充仓库也无法解决拉取失败的问题。
内容的提问来源于stack exchange,提问作者Nunofernandes.official
相关产品推荐
相关产品推荐

