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的对比

相关推荐
yong15858553433 分钟前
2. Linux C++ muduo 库学习——原子变量操作头文件
linux·c++·学习
RainyJiang4 分钟前
聊聊协程里的 Semaphore:别让协程挤爆门口
android·kotlin
IDIOT___IDIOT2 小时前
KNN and K-means 监督与非监督学习
学习·算法·kmeans
Dev7z2 小时前
在MySQL里创建数据库
android·数据库·mysql
Rousson2 小时前
硬件学习笔记--91 TMR型互感器介绍
笔记·学习
invicinble2 小时前
mysql建立存数据的表(一)
android·数据库·mysql
前端 贾公子2 小时前
Vue响应式原理学习:基本原理
javascript·vue.js·学习
Slaughter信仰3 小时前
图解大模型_生成式AI原理与实战学习笔记前四张问答(7题)
人工智能·笔记·学习
似霰3 小时前
传统 Hal 开发笔记1----传统 HAL简介
android·hal
Zender Han4 小时前
Flutter Gradients 全面指南:原理、类型与实战使用
android·flutter·ios