Kotlin Compose Kotlin Compose 02 Create Project

文章目录

开发环境

  • macOS
  • Idea 2022.2.1
  • jdk 17

开发环境这里,compose 打包时要求jdk版本大于15,而 java 的 lts 版本是8,11,17,我个人建议直接用17而不是15,开发时运行反而可以使用11,不过我个人觉得直接用17就好了

创建项目

新建项目,选择 Compose Mutilplatform类型

image.png

这里左边可以单独选择支持的类型,右边是多类型(桌面端+安卓) 然后,等待依赖更新完成,我创建的是多类型的项目

看看配置文件

image.png

项目的配置文件是 kts 文件,这是 Gradle 项目 groovy 以外的另一种一级语言,使用 kotlin 语法,相比 groovy 来说,语法更加的“严谨”

settings.kts

 1// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
 2pluginManagement { // 这个闭包是用于配置项目中用到的 plugin 的一些配置,以便做到不同的 module 使用相同的配置
 3    repositories { // 配置仓库
 4        google()  // 因为引入了安卓项目, google 仓库就是必须了的
 5        gradlePluginPortal()  // 这个是 gradle 的插件目录,一般很少用到
 6        mavenCentral()
 7        maven("https://maven.pkg.jetbrains.space/public/p/compose/dev") // kotlin compose 目前很多文件还是使用的这个仓库
 8    }
 9
10    plugins { // 这个闭包内指定了所有 plugin 的版本号,如果这里不指定,则子module中需要单独引入
11    // extra[key] 是读取了 gradle.properties 中定义的版本号
12        
13        kotlin("multiplatform").version(extra["kotlin.version"] as String)
14        kotlin("android").version(extra["kotlin.version"] as String)
15        id("com.android.application").version(extra["agp.version"] as String)
16        id("com.android.library").version(extra["agp.version"] as String)
17        id("org.jetbrains.compose").version(extra["compose.version"] as String)
18    }
19}
20
21rootProject.name = "KotlinComposeExample2"
22
23include(":android", ":desktop", ":common")

<root>/build.gradle.kts

 1group "top.kikt"
 2version "1.0-SNAPSHOT"
 3
 4allprojects { // 给所有子项目做配置
 5    repositories { // 给所有子项目指定仓库
 6        google()
 7        mavenCentral()
 8        maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
 9    }
10}
11
12plugins { // 配置插件, apply false 是懒加载
13    kotlin("multiplatform") apply false
14    kotlin("android") apply false
15    id("com.android.application") apply false
16    id("com.android.library") apply false
17    id("org.jetbrains.compose") apply false
18}

android/build.gradle.kts

安卓子项目的配置文件

 1plugins {
 2    id("org.jetbrains.compose")
 3    id("com.android.application")
 4    kotlin("android")
 5}
 6
 7group "top.kikt"
 8version "1.0-SNAPSHOT"
 9
10repositories {
11    jcenter()
12}
13
14dependencies {
15    implementation(project(":common")) // 相比传统项目只有这一点不太一样
16    implementation("androidx.activity:activity-compose:1.3.0")
17}
18
19android {
20    compileSdkVersion(31)
21    defaultConfig {
22        applicationId = "top.kikt.android"
23        minSdkVersion(24)
24        targetSdkVersion(31)
25        versionCode = 1
26        versionName = "1.0-SNAPSHOT"
27    }
28    compileOptions {
29        sourceCompatibility = JavaVersion.VERSION_1_8
30        targetCompatibility = JavaVersion.VERSION_1_8
31    }
32    buildTypes {
33        getByName("release") {
34            isMinifyEnabled = false
35        }
36    }
37}

desktop/build.gradle.kts

桌面子项目的配置文件

 1import org.jetbrains.compose.compose
 2import org.jetbrains.compose.desktop.application.dsl.TargetFormat
 3import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
 4
 5plugins {
 6    kotlin("multiplatform")
 7    id("org.jetbrains.compose")
 8}
 9
10group = "top.kikt"
11version = "1.0-SNAPSHOT"
12
13
14kotlin {
15    jvm {
16        compilations.all {
17            kotlinOptions.jvmTarget = "11"
18        }
19        withJava()
20    }
21    sourceSets {
22        val jvmMain by getting {
23            dependencies {
24                implementation(project(":common"))
25                implementation(compose.desktop.currentOs) // 插件会识别当前的开发平台
26            }
27        }
28        val jvmTest by getting
29    }
30}
31
32compose.desktop {
33    application {
34        mainClass = "MainKt"
35        nativeDistributions {
36            targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
37            packageName = "KotlinComposeExample2"
38            packageVersion = "1.0.0"
39        }
40    }
41}

common/build.gradle.kts

公用部分的配置文件

 1import org.jetbrains.compose.compose
 2
 3plugins { // 这里的插件都不需要再指定版本号,因为在 settings.gradle.kts 里配置过了
 4    kotlin("multiplatform") 
 5    id("org.jetbrains.compose")
 6    id("com.android.library")
 7}
 8
 9group = "top.kikt"
10version = "1.0-SNAPSHOT"
11
12kotlin {
13    android()
14    jvm("desktop") {
15        compilations.all {
16            kotlinOptions.jvmTarget = "11"
17        }
18    }
19    sourceSets {
20        // 这里会有几处子项目的配置,所有的 dependencies 都需要配置在这里
21        val commonMain by getting {
22            dependencies {
23                api(compose.runtime)
24                api(compose.foundation)
25                api(compose.material)
26            }
27        }
28        val commonTest by getting {
29            dependencies {
30                implementation(kotlin("test"))
31            }
32        }
33        val androidMain by getting {
34            dependencies {
35                api("androidx.appcompat:appcompat:1.2.0")
36                api("androidx.core:core-ktx:1.3.1")
37            }
38        }
39        val androidTest by getting {
40            dependencies {
41                implementation("junit:junit:4.13")
42            }
43        }
44        val desktopMain by getting {
45            dependencies {
46                api(compose.preview)
47            }
48        }
49        val desktopTest by getting
50    }
51}
52
53android {
54    compileSdkVersion(31)
55    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
56    defaultConfig {
57        minSdkVersion(24)
58        targetSdkVersion(31)
59    }
60    compileOptions {
61        sourceCompatibility = JavaVersion.VERSION_1_8
62        targetCompatibility = JavaVersion.VERSION_1_8
63    }
64}

运行项目

桌面端的入口文件

 1// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
 2import androidx.compose.ui.window.Window
 3import androidx.compose.ui.window.application
 4import top.kikt.common.App
 5
 6
 7fun main() = application {
 8    Window(onCloseRequest = ::exitApplication) {
 9        App()
10    }
11}
 1package top.kikt.common
 2
 3import androidx.compose.material.Text
 4import androidx.compose.material.Button
 5import androidx.compose.runtime.Composable
 6import androidx.compose.runtime.getValue
 7import androidx.compose.runtime.mutableStateOf
 8import androidx.compose.runtime.remember
 9import androidx.compose.runtime.setValue
10
11@Composable
12fun App() {
13    var text by remember { mutableStateOf("Hello, World!") }
14    val platformName = getPlatformName()
15
16    Button(onClick = {
17        text = "Hello, ${platformName}"
18    }) {
19        Text(text)
20    }
21}

点击运行

image.png

image.png

点击后变成了下面这样

image.png