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

为何Jetpack Compose BOM未用指定映射版本,却解析为最新版本?

问题

在Gradle依赖中使用了androidx.compose:compose-bom:2023.06.01,执行./gradlew app:dependencies命令后,发现Compose相关依赖被解析为*:1.5.0-beta03,而非BOM指定的版本。以下是完整的依赖配置代码:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    // Support libraries
    implementation "androidx.appcompat:appcompat:$appCompatVersion"
    implementation "androidx.activity:activity-ktx:$activityVersion"

    // Fragment
    implementation "androidx.fragment:fragment-ktx:$fragmentktxVersion"

    implementation "androidx.core:core-ktx:$corektxVersion"
    implementation "androidx.cardview:cardview:$androidxCardViewVersion"
    implementation "androidx.constraintlayout:constraintlayout:$constraintlayoutVersion"
    implementation "androidx.gridlayout:gridlayout:$androidxGridLayoutVersion"
    implementation "androidx.recyclerview:recyclerview:$recyclerviewVersion"
    implementation "androidx.preference:preference:$preferenceVersion"
    implementation "androidx.vectordrawable:vectordrawable:$androidxVectordrawableVersion"
    implementation "androidx.vectordrawable:vectordrawable-animated:$androidxVectordrawableAnimatedVersion"
    implementation "com.google.android.material:material:$materialVersion"

    // Google libraries
    implementation("com.google.android.gms:play-services-oss-licenses:17.0.0")
    implementation "com.google.android.gms:play-services-base:$playServicesVersion"
    implementation "com.google.android.gms:play-services-maps:18.1.0"
    implementation "com.google.android.gms:play-services-analytics:18.0.2"
    implementation "com.google.android.gms:play-services-location:17.0.0"
    implementation 'com.google.maps.android:android-maps-utils:0.5'
    implementation "com.google.code.gson:gson:$googleGsonVersion"

    // Firebase
    implementation platform("com.google.firebase:firebase-bom:$firebaseVersion")
    implementation 'com.google.firebase:firebase-config-ktx'
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation 'com.google.firebase:firebase-messaging-ktx'
    implementation 'com.google.firebase:firebase-inappmessaging-display-ktx'

    // Retrofit
    implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
    implementation "com.squareup.retrofit2:converter-gson:$gsonVersion"
    implementation "com.squareup.okhttp3:logging-interceptor:$okhttp3Version"

    // Rx
    implementation "io.reactivex:rxjava:$rxjavaVersion"
    implementation "io.reactivex:rxandroid:$rxandroidVersion"

    // Timber
    implementation "com.jakewharton.timber:timber:$timberVersion"

    // Dagger
    implementation "com.google.dagger:dagger:$daggerVersion"
    kapt "com.google.dagger:dagger-compiler:$daggerVersion"
    compileOnly 'javax.annotation:jsr250-api:1.0'

    implementation 'com.squareup.picasso:picasso:2.71828'

    //compose
    def composeBom = platform("androidx.compose:compose-bom:$composeBOMVersion")
    implementation composeBom
    androidTestImplementation composeBom

    implementation ("androidx.compose.ui:ui")
    implementation ("androidx.compose.material:material")
    implementation ("androidx.activity:activity-compose")
    implementation ("androidx.compose.runtime:runtime-livedata")
    implementation ("androidx.compose.runtime:runtime")
    implementation ("androidx.compose.animation:animation")
    implementation ("androidx.compose.foundation:foundation")
    implementation ("androidx.compose.foundation:foundation-layout")
    implementation ("androidx.lifecycle:lifecycle-viewmodel-compose")
    implementation ("androidx.lifecycle:lifecycle-viewmodel-savedstate")
    implementation ("androidx.lifecycle:lifecycle-runtime-compose")
    implementation ("androidx.navigation:navigation-compose:$composeNavigationVersion")

    // Android Studio Preview support
    implementation 'androidx.compose.ui:ui-tooling-preview'
    debugImplementation 'androidx.compose.ui:ui-tooling'

    implementation ("com.google.accompanist:accompanist-swiperefresh:$accompanistSwiperefreshVersion")

    //Splash screen
    implementation 'androidx.core:core-splashscreen:1.0.1'

    // UI Tests
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
    debugImplementation 'androidx.compose.ui:ui-test-manifest'

    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesAndroidVersion"

// testing

}

 ext {
        targetSdkVersion = 33
        compileSdkVersion = 33
        minSdkVersion = 24

        kotlinVersion = '1.7.21'

        activityVersion = "1.5.1"
        appCompatVersion = '1.5.1'
        appCompatResourcesVersion = "1.6.1"
        fragmentktxVersion="1.5.4"
        corektxVersion="1.8.0"
        androidxCardViewVersion = '1.0.0'
        androidxGridLayoutVersion = '1.0.0'
        androidxVectordrawableVersion = '1.0.0'
        androidxVectordrawableAnimatedVersion = '1.0.0'
        androidxAnnotations = "1.6.0"

        viewModelktxVersion="2.6.1"

        androidxSqliteVersion="2.1.0"

        constraintlayoutVersion = "2.1.4"
        recyclerviewVersion = "1.3.0"
        preferenceVersion = "1.1.1"
        materialVersion = "1.1.0"

        securityCryptoVersion = "1.0.0"

        googleGsonVersion = '2.8.9'
        daggerVersion = '2.42'

        amplifyVersion = "1.38.7"
        amplifyKotlinVersion="0.22.7"
        desugarVersion = "1.1.5"

        playServicesVersion = '18.1.0'

        firebaseVersion = '32.1.1'

        dbflowVersion = "4.2.4"

        workManagerVersion = "2.7.1"

        usabillaVersion = "7.3.5"

        retrofitVersion = "2.7.0"
        gsonVersion = "2.6.1"
        okhttp3Version = "4.2.1"
        okhttpInterceptorVersion = "4.9.1"

        rxjavaVersion="1.3.8"
        rxandroidVersion="1.2.1"

        coroutinesAndroidVersion="1.3.9"
        coroutinesPlayServicesVersion="1.6.4"

        composeBOMVersion = "2023.06.01"
        composeCompilerVersion="1.4.0-alpha02"
        composeNavigationVersion = "2.5.3"
        immutableLibVersion = "0.3.5"
        accompanistDrawablePainterVersion = "0.31.5-beta"
        accompanistSwiperefreshVersion = "0.25.1"
        coilComposeVersion = "2.4.0" //Not Compatible with current dependencies

        newRelicVersion = '6.10.0'
        leakCanaryVersion = "2.7"
        timberVersion = "4.7.1"
        ktlintVersion="0.35.0"

        jetbrainAnnotationVersion = "24.0.1"

        //Testing
        junitVersion="4.13.2"
        mockkVersion="1.13.5"
        coroutines_test="1.7.3"
        turbine="1.0.0"
        androidxTextCoreVersion = "1.5.0"
        mockitoCoreVersion = "5.3.0"
        robolectricVersion = "4.9"
        junitExtVersion = "1.1.5"
        espressoVersion = "3.5.1"

    }
原因分析与解决方案

核心原因

  • Accompanist库版本不兼容:你使用的accompanist-swiperefresh:0.25.1是旧版本,它依赖的Compose版本正是1.5.0-beta03,Gradle会因为这个传递依赖强制拉低整个Compose生态的版本,覆盖BOM的指定版本。
  • Compose Compiler版本不匹配:composeCompilerVersion="1.4.0-alpha02"与2023.06.01 BOM对应的Compiler版本(1.5.1)不兼容,版本解析时会优先选择适配旧Compiler的Compose库版本。
  • 旧AndroidX库的传递干扰:项目中的activity-ktx:1.5.1、fragmentktx:1.5.4等旧版库,可能传递依赖较低版本的Compose相关组件,与BOM版本产生冲突。

修复步骤

  1. 升级Accompanist库:将accompanistSwiperefreshVersion改为0.31.5-beta(你已经定义了这个版本但未使用),该版本适配Compose 1.5.x稳定版,与2023.06.01 BOM兼容。
  2. 同步Compiler版本:把composeCompilerVersion改为1.5.1,这是2023.06.01 BOM官方推荐的Compiler版本,确保和Compose库版本完全匹配。
  3. 更新相关AndroidX库:将activityVersion升级到1.7.2、fragmentktxVersion升级到1.6.1,这些版本更好地支持Compose 1.5.x,避免传递依赖冲突。
  4. 强制BOM版本优先级(可选):如果上述调整后仍有问题,在模块的build.gradle中添加版本强制规则,确保BOM版本优先:
configurations.all {
    resolutionStrategy {
        force platform("androidx.compose:compose-bom:2023.06.01")
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 19:54:50