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

创建Jetpack Compose新项目时依赖缺失/冲突问题求助

修复Jetpack Compose依赖标红问题

核心问题分析

你的问题主要源于Kotlin版本与Compose Compiler版本不匹配、重复引入Compose BOM依赖,以及可能的Gradle同步不彻底。以下是具体修复步骤:


步骤1:对齐Kotlin与Compose Compiler版本

Compose Compiler和Kotlin版本存在严格的对应关系,版本不匹配会直接导致依赖无法解析。你当前使用的kotlinCompilerExtensionVersion '1.4.3'对应的Kotlin版本应为1.8.20;如果项目使用更高版本Kotlin(如1.9.x),需同步升级Compose Compiler:

  • Kotlin 1.9.0 → Compose Compiler 1.5.3
  • Kotlin 1.8.20 → Compose Compiler 1.4.3

修改composeOptions块:

composeOptions {
    // 根据你的Kotlin版本调整,示例为Kotlin 1.8.20对应版本
    kotlinCompilerExtensionVersion '1.4.3'
}

步骤2:移除重复的Compose BOM依赖

你的依赖配置中重复多次引入了Compose BOM,这会引发依赖解析冲突。只需在implementation、androidTestImplementation块各保留一次即可:

dependencies {
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
    implementation 'androidx.activity:activity-compose:1.8.1'
    // 仅保留一次implementation级别的BOM
    implementation platform('androidx.compose:compose-bom:2023.03.00')
    implementation 'androidx.compose.ui:ui'
    implementation 'androidx.compose.ui:ui-graphics'
    implementation 'androidx.compose.ui:ui-tooling-preview'
    implementation 'androidx.compose.material3:material3'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    // 仅保留一次androidTest级别的BOM
    androidTestImplementation platform('androidx.compose:compose-bom:2023.03.00')
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4'

    debugImplementation 'androidx.compose.ui:ui-tooling'
    debugImplementation 'androidx.compose.ui:ui-test-manifest'
}

步骤3:执行Gradle同步与清理

  1. 点击Android Studio工具栏的 Sync Project with Gradle Files 按钮
  2. 依次执行 Build → Clean Project 和 Build → Rebuild Project

步骤4:检查Project级仓库配置

确保项目根目录的build.gradle中配置了google()仓库(Compose依赖仅在Google仓库中提供):

plugins {
    id 'com.android.application' version '8.1.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.20' apply false
}

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

修改后的完整Module级build.gradle示例

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

android {
    namespace 'com.example.myapplication'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdk 24
        targetSdk 33
        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_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.4.3' // 与Kotlin版本1.8.20对应
    }
    packaging {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
    implementation 'androidx.activity:activity-compose:1.8.1'
    implementation platform('androidx.compose:compose-bom:2023.03.00')
    implementation 'androidx.compose.ui:ui'
    implementation 'androidx.compose.ui:ui-graphics'
    implementation 'androidx.compose.ui:ui-tooling-preview'
    implementation 'androidx.compose.material3:material3'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation platform('androidx.compose:compose-bom:2023.03.00')
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4'

    debugImplementation 'androidx.compose.ui:ui-tooling'
    debugImplementation 'androidx.compose.ui:ui-test-manifest'
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 03:43:11