Android为现有的应用工程设置 Compose

点击查看:Compose 快速入门

点击查看:Jetpack Compose 与 Kotlin 的兼容性对应关系

要开始使用 Compose,您需要先向项目中添加一些 build 配置。将以下定义添加到应用的 build.gradle 或者build.gradle.kts 文件中:

build.gradle

android {
   
    buildFeatures {
   
        compose true
    }

    composeOptions {
   
        kotlinCompilerExtensionVersion = "1.5.9"
    }

    kotlinOptions {
   
        jvmTarget = "19"
    }
}

build.gradle.kts

android {
   
    buildFeatures {
   
        compose = true
    }

    composeOptions {
   
        kotlinCompilerExtensionVersion = "1.5.9"
    }

    kotlinOptions {
   
        jvmTarget = "19"
    }
}

需要注意以下几点

  • 在 Android BuildFeatures 代码块内将 compose 标志设置为 true 会启用 Compose 功能。
  • ComposeOptions 代码块中定义的 Kotlin 编译器扩展版本控制与 Kotlin 版本控制相关联。请务必参阅兼容性对应图,并选择与您项目的 Kotlin 版本匹配的库版本。

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

build.gradle

dependencies {
   

    def 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'

}

build.gradle.kts

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")

}

注意:Jetpack Compose 使用物料清单 (BoM) 提供,以确保所有库组的版本保持同步。如需了解详情,请参阅“物料清单”页面。

相关推荐

  1. Android现有应用工程设置 Compose

    2024-02-21 09:46:02       54 阅读
  2. [Android]Jetpack Compose设置颜色

    2024-02-21 09:46:02       41 阅读
  3. Android设置三方应用默认Launcher

    2024-02-21 09:46:02       59 阅读
  4. Android Studio导入现有项目方法

    2024-02-21 09:46:02       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-21 09:46:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-21 09:46:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-21 09:46:02       87 阅读
  4. Python语言-面向对象

    2024-02-21 09:46:02       96 阅读

热门阅读

  1. 【数据结构——顺序表三种数据结构差异】

    2024-02-21 09:46:02       57 阅读
  2. zookeeper动态扩缩容(无需重启)

    2024-02-21 09:46:02       43 阅读
  3. 物联网平台构成与边缘计算

    2024-02-21 09:46:02       50 阅读
  4. 从查字典到查网络再到查大语言模型

    2024-02-21 09:46:02       52 阅读
  5. 单精度(float)和双精度(double)的区别

    2024-02-21 09:46:02       55 阅读
  6. Go 空切片与nil切片

    2024-02-21 09:46:02       57 阅读