Android Compose项目中Gradle自动升级activity-compose版本引发编译问题
Android Compose项目中Gradle自动升级activity-compose引发编译错误问题
在我的Android Compose项目里,Gradle会自动将androidx.activity:activity-compose库升级至1.8.0-alpha05版本,但该版本要求使用compileSdk 34才能编译,而当前使用的Gradle 8.0.2并不支持compileSdk 34。
项目build.gradle配置
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id("com.google.dagger.hilt.android") id("com.google.devtools.ksp") id 'org.jetbrains.kotlin.plugin.serialization' version '1.8.20' id 'com.google.gms.google-services' } android { namespace 'com.my_app' compileSdk 33 defaultConfig { applicationId "com.my_app" minSdk 21 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary true } } buildTypes { release { minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' signingConfig signingConfigs.debug } } applicationVariants.all { variant -> variant.addJavaSourceFoldersToModel( new File(buildDir, "generated/ksp/${variant.name}/kotlin") ) } compileOptions { sourceCompatibility JavaVersion.VERSION_18 targetCompatibility JavaVersion.VERSION_18 } kotlinOptions { jvmTarget = '18' } buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion '1.4.6' } packagingOptions { resources { excludes += '/META-INF/{AL2.0,LGPL2.1}' } } } dependencies { def exoPlayerVersion = "1.0.2" def room_version = "2.5.1" def composeBom = platform('androidx.compose:compose-bom:2023.05.01') implementation composeBom androidTestImplementation composeBom implementation 'androidx.core:core-ktx:1.10.1' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1' implementation("androidx.lifecycle:lifecycle-runtime-compose:2.6.1") implementation 'androidx.activity:activity-compose:1.7.2' // 问题相关依赖 implementation "androidx.compose.animation:animation-graphics" implementation 'androidx.compose.ui:ui' implementation 'androidx.compose.ui:ui-graphics' implementation 'androidx.compose.ui:ui-tooling-preview' implementation 'androidx.compose.material3:material3' implementation 'androidx.compose.foundation:foundation' implementation 'com.google.android.material:material:1.10.0-alpha04' implementation('org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1') // ... 其他依赖 }
编译错误信息
执行./gradlew checkDebugAarMetadata命令后抛出以下错误:
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction > 3 issues were found when checking AAR metadata: 1. Dependency 'androidx.activity:activity-compose:1.8.0-alpha05' requires libraries and applications that depend on it to compile against version 34 or later of the Android APIs. :app is currently compiled against android-33. Also, the maximum recommended compile SDK version for Android Gradle plugin 8.0.2 is 33. 2. Dependency 'androidx.activity:activity-ktx:1.8.0-alpha05' requires libraries and applications that depend on it to compile against version 34 or later of the Android APIs. // 同上注释 3. Dependency 'androidx.activity:activity:1.8.0-alpha05' requires libraries and applications that depend on it to compile against version 34 or later of the Android APIs. // 同上注释
已尝试的无效操作
我曾尝试用Gradle版本解析策略强制指定版本,但未生效:
configurations.all { resolutionStrategy { force 'androidx.activity:activity-compose:1.7.2' } }
解决方案
1. 强制所有activity相关库的版本
错误显示activity、activity-ktx、activity-compose三个库都被升级到了1.8.0-alpha05,需同时强制这三个库的版本:
configurations.all { resolutionStrategy { force 'androidx.activity:activity:1.7.2' force 'androidx.activity:activity-ktx:1.7.2' force 'androidx.activity:activity-compose:1.7.2' } }
2. 定位版本来源
执行以下命令查看依赖树,找到自动引入高版本activity库的依赖来源:
./gradlew app:dependencies
搜索androidx.activity相关条目,确认是否是某依赖(比如当前使用的Compose BOM)间接引入了高版本,再针对性调整该依赖版本或覆盖其指定的activity版本。
3. 调整Compose BOM版本(可选)
当前使用的Compose BOM版本为2023.05.01,若该BOM默认指定了高版本activity库,可切换到兼容compileSdk 33的低版本BOM,比如2023.03.00。
内容的提问来源于stack exchange,提问作者Firate
相关产品推荐
相关产品推荐

