Compose记录之一:项目引入Compose依赖

1. 在app层build.gradle中添加

plugins {
    ...
    id 'org.jetbrains.kotlin.plugin.compose' // 引入Compose插件
}

...

android {
    ...
    buildFeatures {
        compose true // 启用compose
    }
    ...
}

...

dependencies {
    // compose bom 用于统一管理 Jetpack Compose 依赖的版本
    def composeBom = platform('androidx.compose:compose-bom:2024.09.03')
    implementation(composeBom)
    androidTestImplementation(composeBom)

    // Jetpack Compose 核心 UI 库,提供 UI 组件基础功能
    implementation "androidx.compose.ui:ui"

    // Material 3 库,为 Jetpack Compose 提供 Material Design 3 风格的组件
    implementation 'androidx.compose.material3:material3'

    // 提供 Material 3 的窗口尺寸类(WindowSizeClass),用于响应式设计,适应不同的屏幕尺寸
    implementation 'androidx.compose.material3:material3-window-size-class:1.3.0'

    // 扩展的 Material 图标库,包含 Material Design 的额外图标
    implementation "androidx.compose.material:material-icons-extended"

    // Material 图标库的核心依赖,包含了 Material Design 图标的基本集
    implementation 'androidx.compose.material:material-icons-core'

    // Compose UI 工具库,提供 UI 预览功能,用于设计和查看 UI 组件
    implementation "androidx.compose.ui:ui-tooling-preview"

    // Google 的 Material 设计库,用于 Android View 系统的 Material 组件
    implementation "com.google.android.material:material:1.12.0"

    // Jetpack Lifecycle 库的 Kotlin 扩展,简化生命周期感知组件的使用
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.8.6'

    // 提供与 Activity 集成的 Jetpack Compose 扩展,简化在 Activity 中使用 Compose 的配置
    implementation 'androidx.activity:activity-compose:1.9.2'

    // Jetpack Compose 的 UI 工具库,提供 UI 调试工具(包括实时预览)
    debugImplementation "androidx.compose.ui:ui-tooling"


    implementation "androidx.navigation:navigation-compose:2.8.3" // compose路由
    implementation "io.coil-kt:coil-compose:2.7.0" //  compose网络图片加载


    // 用于构建自适应界面的 Compose 组件,2024年10月才发布了第一个正式版本
    implementation "androidx.compose.material3.adaptive:adaptive:1.1.0-alpha05"
    implementation "androidx.compose.material3.adaptive:adaptive-layout:1.1.0-alpha02"
    implementation "androidx.compose.material3.adaptive:adaptive-navigation:1.1.0-alpha05"
    // Adaptive 导航的集成组件 有几个好用方便的Scaffold
    implementation 'androidx.compose.material3:material3-adaptive-navigation-suite:1.3.0'
    
    // 将 ViewModel Livedata 转为 Compose State
    implementation libs.runtime.livedata 

    // Compose 中使用 ViewModel
    implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.8.6' 

    // Compose Navigation 中使用 Hilt
    implementation 'androidx.hilt:hilt-navigation-compose:1.2.0' 


    // 更多待续...
}

2. 在项目层也引入Compose 插件

plugins {
    ...
    id 'org.jetbrains.kotlin.plugin.compose' version '2.0.20' apply false
}

3. Gradle Sync 同步项目