1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Kotlin Multiplatform Sample

Last updated at Posted at 2025-01-13

概要

(日記)Kotlin Multiplatformでどのあたりまで共通で使えるものか試してみる。
ここでは下記プラットフォームで動作確認

  • jvm Java
  • js Javascript/Node.js
  • mingwX64 Windows
  • linuxX64 Linux
  • wasmJs WebAssembly/Node.js (実験的)

試した機能

環境

  • Windwos 11 / Ubuntu 24.04(wsl)
  • Kotlin 2.2.0

プロジェクト設定

build.gradle.kts
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl

plugins {
    kotlin("multiplatform") version "2.2.0" // https://plugins.gradle.org/plugin/org.jetbrains.kotlin.multiplatform
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

kotlin {
    jvm()
    js { binaries.executable(); nodejs() }
    @OptIn(ExperimentalWasmDsl::class)
    wasmJs { binaries.executable(); nodejs() }
    mingwX64 { binaries.executable() }
    linuxX64 { binaries.executable() }

    sourceSets {
        commonMain.dependencies {
            implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.8.0")
        }
    }
}

kotlinx-io サンプル

➔GitHub

src/commonMain/kotlin/AppMain.kt : commonコード
fun appMain() = with(SystemFileSystem) {
    val path = Path("./tmp.txt")
    sink(path).buffered().use { sink ->
        sink.writeInt(1)
        sink.writeString("test.")
    }
    source(path).buffered().use { src ->
        println(src.readInt())
        println(src.readString())
    }
}
src/jvmMain/kotlin/Main.kt :jvmターゲット、他はjvmと同一
fun main() {
    appMain()
}

kotlinx-coroutine サンプル

➔GitHub

src/commonMain/kotlin/AppMain.kt : commonコード
suspend fun appMain() {
    flowOf(1, 2, 3).onEach { delay(1.seconds) }.collect {
        println(it)
    }
}
src/jvmMain/kotlin/Main.kt :jvmターゲット
suspend fun main() {
    appMain()
}
src/mingwX64Main/kotlin/Main.kt :linux/windows。他はjvmと同一
fun main() = runBlocking{
    appMain()
}

テスト実行

Gradleから実行
sh gradlew jvmRun -DmainClass=MainKt    # jvm
sh gradlew jsNodeProductionRun          # js(*2)
sh gradlew runReleaseExecutableMingwX64 # mingwX64 (Windows上で実行) (*1)
sh gradlew runReleaseExecutableLinuxX64 # linuxX64 (*1)
sh gradlew wasmJsNodeProductionRun      # wasmJs(*2)

実行ファイル生成・実行

sh gradlew build
node build/js/packages/multiplatform/kotlin/multiplatform.js # js
./build/bin/mingwX64/releaseExecutable/multiplatform.exe     # mingwX64 (Windows上で実行)
./build/bin/linuxX64/releaseExecutable/multiplatform.kexe    # linuxX64 (Ubuntu上で実行)
node build/js/packages/multiplatform-wasm-js/kotlin/multiplatform-wasm-js.mjs # wasmJs

Note

*1: kotestは非対応なのかNativeでTest実行(sh gradlew linuxX64Test)時エラー
*2: js/wasmJsでは仮想的なファイルシステムへの操作となる

Refer

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?