You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何更新Jetpack Compose至最新版本?修改Gradle后遇构建错误

解决Jetpack Compose版本更新构建错误问题

一、核心问题:版本强绑定不匹配

Jetpack Compose的版本与Kotlin版本、Android Gradle插件(AGP)版本严格绑定,直接修改Compose版本但不同步更新依赖的Kotlin/AGP版本,必然导致构建错误。对应稳定版匹配关系如下:

  • Compose 1.6.x → Kotlin 1.9.x → AGP 8.2+
  • Compose 1.5.x → Kotlin 1.8.x → AGP 8.0+
  • 你当前使用的Compose 1.2.0对应Kotlin 1.7.0、AGP 7.4.1,属于旧版本组合。

二、查询最新Compose稳定版的本地方法

无需访问外部网站,直接在Android Studio内操作:

  • 点击顶部菜单栏 File → Project Structure
  • 选择 Modules → app → Dependencies
  • 找到任意Compose依赖(如androidx.compose.ui:ui),点击版本号下拉箭头,即可查看所有可用稳定版本,AS会自动标记最新版。

三、修改Gradle配置(以Compose 1.6.8最新稳定版为例)

1. 项目根目录 build.gradle(Project)

plugins {
    id 'com.android.application' version '8.2.2' apply false
    id 'com.android.library' version '8.2.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.9.22' apply false
}

// 统一管理依赖版本
ext {
    compose_version = '1.6.8'
    material3_version = '1.2.1'
}

注意:AGP 8.x版本不再需要buildscript块来定义全局变量,直接在plugins块后声明即可。

2. App模块 build.gradle(Module:app)

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    namespace 'com.example.android'
    compileSdk 34 // 适配Compose 1.6.x的最新compileSdk

    defaultConfig {
        applicationId "com.example.android"
        minSdk 24
        targetSdk 34
        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'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17 // Compose 1.6.x推荐使用Java 17,兼容Java 8但性能更优
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = '17' // 与Java版本保持一致
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.6.8' // 必须与Compose版本完全一致
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.12.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.7.0'
    implementation 'androidx.activity:activity-compose:1.8.2'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation "androidx.compose.material3:material3:$material3_version"
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    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"
}

四、构建错误收尾操作

  1. 点击Android Studio右上角的「Sync Project with Gradle Files」按钮同步项目
  2. 若同步仍报错,执行 File → Invalidate Caches... → Invalidate and Restart 清理缓存并重启AS
  3. 查看控制台错误信息,若为依赖冲突,按照AS提示的解决方案排除冲突即可

内容的提问来源于stack exchange,提问作者wyb253

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 08:59:21