jetpack compose 学习(-)

年底了,无聊的时间总是缓慢的,找个事情做一做,打发打发时间,刚好看到jetpack compose 学习学习,毕竟androidStudio 默认创建的项目都带上了这个,学习网站:https://developer.android.com/jetpack/compose/modifiers?hl=zh-cn

1. 首先androidStudio创建一个新项目


喜欢kotlin的,可以在build configuration language 中选择kotlin

2. 创建好后项目目录大概如下
3. 创建好后run 一下
4.run 没问题 ,那就进入下一步看看model下的build.gradle发生了什么变化
groovy 复制代码
plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    namespace 'com.yyf.myjetpackdemo'
    compileSdk 33

    defaultConfig {
        applicationId "com.yyf.myjetpackdemo"
        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  //开启compose 功能
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.4.3'
    }
    packaging {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {
    //kotlin 核心库
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
    //compose 核心库
    implementation 'androidx.activity:activity-compose:1.8.0'
    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'
}

在对比官网看看

groovy 复制代码
dependencies {

    def composeBom = platform('androidx.compose:compose-bom:2023.01.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.6.1'
    // Optional - Integration with ViewModels
    implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1'
    // Optional - Integration with LiveData
    implementation 'androidx.compose.runtime:runtime-livedata'
    // Optional - Integration with RxJava
    implementation 'androidx.compose.runtime:runtime-rxjava2'

}
5. 由此对比可看出使用compose的必要条件
  1. 开启compose功能
groovy 复制代码
buildFeatures {
        compose true  //开启compose 功能
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.4.3'
    }
  1. 添加核心依赖库支持
groovy 复制代码
	//kotlin 核心库
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
    //compose 核心库
    implementation 'androidx.activity:activity-compose:1.8.0'
    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'

看来使用compose前,必须得先学习kotlin,在官网中也找到了关于支持java 和kotlin的对比

相关推荐
Tutankaaa8 分钟前
知识竞赛计分如何确保绝对准确?双机热备方案详解与实施要点
笔记·学习·职场和发展
xiaoxiaoxiaolll28 分钟前
Light: Sci & Appl. | 子阵列栅控HEMT超表面:太赫兹波前同时实现高速调制与物理层逻辑
学习
爱喝水的鱼丶42 分钟前
SAP-ABAP:SAP 与 ABAP 关联逻辑与入门路径:业务×开发的协作指南
服务器·前端·数据库·学习·sap·abap
蓝桉~MLGT1 小时前
中级软考(软件工程师)常用错题整理(不间断更新)
学习·中级软考
飞霄不推销1 小时前
SPI驱动学习(2)
学习
GISer_Jing1 小时前
考研|考公GIS:近5年地理信息产业数据全链路核心要点(背诵版)
学习·考研·arcgis
坚持是一种态度2 小时前
软考高级架构师选择题知识点高频100
学习·架构师·软考·考试
yxc_inspire2 小时前
25年CCPC福建邀请赛补题
学习·算法
Slow菜鸟2 小时前
Skill 学习篇(十)| 编排框架 · 五大编排框架 · 全方位决策指南
学习
用户86022504674722 小时前
Jetpack ViewModel 入门与实践
android