Kotlin Multiplatform 项目模板教程
1. 项目的目录结构及介绍
kotlin-multiplatform-template/
├── build.gradle.kts
├── settings.gradle.kts
├── shared/
│ ├── build.gradle.kts
│ ├── src/
│ │ ├── commonMain/
│ │ │ ├── kotlin/
│ │ │ │ └── com/example/shared/
│ │ │ │ └── Platform.kt
│ │ │ └── resources/
│ │ ├── androidMain/
│ │ │ ├── kotlin/
│ │ │ │ └── com/example/shared/
│ │ │ │ └── Platform.kt
│ │ │ └── resources/
│ │ ├── iosMain/
│ │ │ ├── kotlin/
│ │ │ │ └── com/example/shared/
│ │ │ │ └── Platform.kt
│ │ │ └── resources/
│ │ └── jsMain/
│ │ ├── kotlin/
│ │ │ └── com/example/shared/
│ │ │ └── Platform.kt
│ │ └── resources/
├── androidApp/
│ ├── build.gradle.kts
│ ├── src/
│ │ ├── main/
│ │ │ ├── kotlin/
│ │ │ │ └── com/example/androidApp/
│ │ │ │ └── MainActivity.kt
│ │ │ └── res/
│ │ │ └── ...
│ │ └── test/
│ │ └── ...
├── iosApp/
│ ├── build.gradle.kts
│ ├── src/
│ │ ├── main/
│ │ │ ├── kotlin/
│ │ │ │ └── com/example/iosApp/
│ │ │ │ └── MainViewController.kt
│ │ │ └── resources/
│ │ └── test/
│ │ └── ...
└── jsApp/
├── build.gradle.kts
├── src/
│ ├── main/
│ │ ├── kotlin/
│ │ │ └── com/example/jsApp/
│ │ │ └── Main.kt
│ │ └── resources/
│ └── test/
│ └── ...
目录结构介绍
- build.gradle.kts 和 settings.gradle.kts: 项目的 Gradle 配置文件。
- shared/: 包含共享代码和资源的目录。
- commonMain/: 包含跨平台的通用代码和资源。
- androidMain/: 包含 Android 平台特定的代码和资源。
- iosMain/: 包含 iOS 平台特定的代码和资源。
- jsMain/: 包含 JavaScript 平台特定的代码和资源。
- androidApp/: 包含 Android 应用程序的代码和资源。
- iosApp/: 包含 iOS 应用程序的代码和资源。
- jsApp/: 包含 JavaScript 应用程序的代码和资源。
2. 项目的启动文件介绍
Android 启动文件
- androidApp/src/main/kotlin/com/example/androidApp/MainActivity.kt:
package com.example.androidApp import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.material.MaterialTheme import androidx.compose.material.Surface import androidx.compose.material.Text import androidx.compose.runtime.Composable import androidx.compose.ui.tooling.preview.Preview import com.example.shared.Greeting class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { MyApplicationTheme { Surface(color = MaterialTheme.colors.background) { GreetingView(Greeting().greet()) } } } } } @Composable fun GreetingView(text: String) {
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考