当前位置: 首页 > news >正文

Kotlin Multiplatform--01:项目结构基础

Kotlin Multiplatform--01:项目结构基础

    • 引言
    • Common Code
    • Targets
    • Expected 和 actual
        • 1.使用函数
        • 2.使用接口

引言

        以下为使用 Android Studio 创建的默认 Kotlin Multiplatform 的项目结构,本章将对项目结构进行简单介绍,让读者对 Kotlin Multiplatform 项目能够有一个大概的认识。
                


Common Code

        Common Code 是指与平台无关的代码,比如业务逻辑等。在项目结构中对应 commonMain 文件夹。需要注意的是,Common Code 不能使用任何与平台相关的 API ,比如 java.io.File 等,也不能添加任何与平台相关的依赖库。但可以使用专门为 Kotlin Multiplatform 设计的 Kotlin 库,比如 kotlinx.coroutines 等。

Targets

        Target 指需要将项目编译到哪个平台上,比如 JVM, JS, Android, iOS, or Linux 等。需要注意:平台相关的代码可以调用 Common Code,但 Common Code 中不能调用平台相关的代码。在 build.gradle.kts 文件中配置,如下所示:

kotlin {androidTarget() // Declares a target that corresponds to Android phonesiosArm64() // Declares a target that corresponds to 64-bit iPhonesmacosArm64() // Declares a target that corresponds to Modern Apple Silicon-based Macs
}

        声明编译目标后,还需要在项目中新建相应的文件夹和文件,命名规则为xxxMain,还需要在里面新建名为 kotlin 的子文件夹,放该平台相关的源代码。因为我在默认项目结构上额外声明了 macosArm64 的目标,所以需要新建一个 macosMain 文件夹,如下所示:
                

        最后在 build.gradle.kts 中的 sourceSets 中可以配置不同目标的依赖库文件,如下所示:

    sourceSets {androidMain {//put your multiplatform dependencies hereimplementation("xxx")}commonMain.dependencies {//put your multiplatform dependencies hereimplementation("xxx")}iosMain.dependencies {//put your multiplatform dependencies hereimplementation("xxx")}macosMain.dependencies { //put your multiplatform dependencies hereimplementation("xxx")}}

Expected 和 actual

        有时候我们确实需要在 Common Code 中调用平台相关的代码,并在不同的平台下有不同的表现该如何处理呢?

1.使用函数

        在 CommonMain 中声明一个 except 函数,供其他 Common Code 使用:

package identityclass Identity(val userName: String, val processID: Long)expect fun buildIdentity(): Identity

        在不同平台中使用 actual 进行不同的实现,比如在 jvm 中进行以下实现:

package identityimport java.lang.System
import java.lang.ProcessHandleactual fun buildIdentity() = Identity(System.getProperty("user.name") ?: "None",ProcessHandle.current().pid()
)

        在 POSIX 系统中进行以下实现:

package identityimport kotlinx.cinterop.toKString
import platform.posix.getlogin
import platform.posix.getpidactual fun buildIdentity() = Identity(getlogin()?.toKString() ?: "None",getpid().toLong()
)
2.使用接口

        在 CommonMain 中声明一个 Identity 接口和 buildIdentity 函数,供其他 Common Code 使用:

// In the commonMain source set:
expect fun buildIdentity(): Identityinterface Identity {val userName: Stringval processID: Long
}

        在不同平台中使用 actual 进行不同的实现,比如在 jvm 中进行以下实现:

// In the jvmMain source set:
actual fun buildIdentity(): Identity = JVMIdentity()class JVMIdentity(override val userName: String = System.getProperty("user.name") ?: "none",override val processID: Long = ProcessHandle.current().pid()
) : Identity

        在 POSIX 系统中进行以下实现:

// In the nativeMain source set:
actual fun buildIdentity(): Identity = NativeIdentity()class NativeIdentity(override val userName: String = getlogin()?.toKString() ?: "None",override val processID: Long = getpid().toLong()
) : Identity

相关文章:

  • ctfhow——web入门214~218(时间盲注开始)
  • 【FAQ】安装Agent的主机,为何不能更改显示分辨率
  • SQL Server 2008 R2中varchar(max)的含义
  • Hive 数据同步到 Doris 最佳实践方案:从场景适配到性能调优全解析
  • Python3 基础:控制流结构(条件语句、循环)
  • 【C++基础知识】C++类型特征组合:`disjunction_v` 和 `conjunction_v` 深度解析
  • Visual Studio C/C++编译器cl.exe的/source-charset与/execution-charset设置项
  • 扩展中国剩余定理
  • day 32 学习笔记
  • 【前端】【业务场景】【面试】在前端开发中,如何优化 SVG(可缩放矢量图形)的性能,特别是在处理复杂图形和动画时
  • ZooKeeper配置优化秘籍:核心参数说明与性能优化
  • 多维时序 | LightGBM多变量时序预测(Matlab完整源码和数据,适合基础小白研究)
  • 最高支持高速L3商用,华为发布ADS 4智驾系统
  • AT45DB161串行FLASH操作
  • 晶振不集成到芯片内部的原因分析
  • Ubuntu中选择Python虚拟环境
  • 考拉悠然:科技与匠心,以烟草虫情AI监测系统共筑品质未来
  • git tag使用场景和实践
  • BDO分厂开展地沟“大清肠”工作
  • 交通运输行业综合智慧监管平台:商贸物流的安全与效率引擎
  • 期待会师!神二十与空间站完成对接
  • “两高”司法解释:升档为境外非法提供商业秘密罪的量刑标准
  • 俄外长拉夫罗夫将出席金砖国家外长会
  • 集合多家“最美书店”,松江成立书店联盟“书香满云间”
  • 新“出差三人组”亮相!神二十乘组简历来了
  • 美国那点事|特朗普的“刀”砍向国务院,美国霸权迎来历史拐点?