Kotlin MultiPlatform 项目教程
1. 项目的目录结构及介绍
KotlinMultiPlatform/
├── build.gradle.kts
├── settings.gradle.kts
├── common
│ ├── src
│ │ ├── commonMain
│ │ │ ├── kotlin
│ │ │ │ └── com
│ │ │ │ └── example
│ │ │ │ └── common
│ │ │ │ └── Greeting.kt
│ │ │ └── resources
│ │ └── commonTest
│ │ ├── kotlin
│ │ │ └── com
│ │ │ └── example
│ │ │ └── common
│ │ │ └── GreetingTest.kt
│ │ └── resources
├── androidApp
│ ├── build.gradle.kts
│ ├── src
│ │ ├── main
│ │ │ ├── kotlin
│ │ │ │ └── com
│ │ │ │ └── example
│ │ │ │ └── android
│ │ │ │ └── MainActivity.kt
│ │ │ └── res
│ │ │ ├── drawable
│ │ │ ├── layout
│ │ │ │ └── activity_main.xml
│ │ │ ├── mipmap-hdpi
│ │ │ ├── mipmap-mdpi
│ │ │ ├── mipmap-xhdpi
│ │ │ ├── mipmap-xxhdpi
│ │ │ ├── mipmap-xxxhdpi
│ │ │ └── values
│ │ │ └── strings.xml
│ │ └── test
│ │ └── kotlin
│ │ └── com
│ │ └── example
│ │ └── android
│ │ └── ExampleUnitTest.kt
├── iosApp
│ ├── build.gradle.kts
│ ├── src
│ │ ├── commonMain
│ │ │ ├── kotlin
│ │ │ │ └── com
│ │ │ │ └── example
│ │ │ │ └── ios
│ │ │ │ └── IosGreeting.kt
│ │ │ └── resources
│ │ └── commonTest
│ │ ├── kotlin
│ │ │ └── com
│ │ │ └── example
│ │ │ └── ios
│ │ │ └── IosGreetingTest.kt
│ │ └── resources
└── README.md
目录结构介绍
build.gradle.kts
和settings.gradle.kts
: 项目的构建和配置文件。common
: 包含共享的 Kotlin 代码,适用于所有平台。commonMain
: 共享的 Kotlin 主代码。commonTest
: 共享的 Kotlin 测试代码。
androidApp
: 包含 Android 平台特定的代码和资源。MainActivity.kt
: Android 应用的主活动。res
: Android 资源文件夹。
iosApp
: 包含 iOS 平台特定的代码和资源。IosGreeting.kt
: iOS 应用的共享代码。
2. 项目的启动文件介绍
Android 启动文件
androidApp/src/main/kotlin/com/example/android/MainActivity.kt
package com.example.android
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.common.Greeting
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
Surface(color = MaterialTheme.colors.background) {
GreetingView(Greeting().greet())
}
}
}
}
}
@Composable
fun Greeting
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考