Compose的gradle配置以及与Kotlin的兼容对应

本文指导如何在Android项目中启用Compose功能,包括在build.gradle.kts文件中添加必要的配置,选择CompositionBom和库依赖,以及兼容的Kotlin版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要开始使用Compose,需要先向项目中添加一些 build 配置。

将以下定义添加到应用的build.gradle.kts文件中:

android {
    buildFeatures {
        compose = true
    }

    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.9"
    }
}

此外,请将以下部分中您需要的Compose BoM和Compose库依赖项的子集添加到您的依赖项:

dependencies {

    val composeBom = platform("androidx.compose:compose-bom:2024.02.00")
    implementation(composeBom)
    androidTestImplementation(composeBom)

    // Choose one of the following:
    // Material Design 3
    implementation("androidx.compose.material3:material3")
    // or Material Design 2
    implementation("androidx.compose.material:material")
    // or skip Material Design and build directly on top of foundational components
    implementation("androidx.compose.foundation:foundation")
    // or only import the main APIs for the underlying toolkit systems,
    // such as input and measurement/layout
    implementation("androidx.compose.ui:ui")

    // Android Studio Preview support
    implementation("androidx.compose.ui:ui-tooling-preview")
    debugImplementation("androidx.compose.ui:ui-tooling")

    // UI Tests
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    debugImplementation("androidx.compose.ui:ui-test-manifest")

    // Optional - Included automatically by material, only add when you need
    // the icons but not the material library (e.g. when using Material3 or a
    // custom design system based on Foundation)
    implementation("androidx.compose.material:material-icons-core")
    // Optional - Add full set of material icons
    implementation("androidx.compose.material:material-icons-extended")
    // Optional - Add window size utils
    implementation("androidx.compose.material3:material3-window-size-class")

    // Optional - Integration with activities
    implementation("androidx.activity:activity-compose:1.8.2")
    // Optional - Integration with ViewModels
    implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1")
    // Optional - Integration with LiveData
    implementation("androidx.compose.runtime:runtime-livedata")
    // Optional - Integration with RxJava
    implementation("androidx.compose.runtime:runtime-rxjava2")

}

以下是Compose 与 Kotlin 的兼容性对应关系

Compose Compiler 版本兼容的 Kotlin 版本
1.5.91.9.22
1.5.81.9.22
1.5.71.9.21
1.5.61.9.21
1.5.51.9.20
1.5.41.9.20
1.5.31.9.10
1.5.21.9.0
1.5.11.9.0
1.5.01.9.0
1.4.81.8.22
1.4.71.8.21
1.4.61.8.20
1.4.51.8.20
1.4.41.8.10
1.4.31.8.10
1.4.21.8.10
1.4.11.8.0
1.4.01.8.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值