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,js,mingwX64,linuxX64,wasmJsの6種のターゲット

[TODO]ktor

前提

  • Windwos 11 / Ubuntu 24.04(wsl)
  • Kotlin 2.1.0
    • coroutine 1.10.1
    • okio 3.10.2

プロジェクト設定

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

plugins {
    kotlin("multiplatform") version "2.1.0"
}

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

repositories {
    mavenCentral()
    maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
    google()
}

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

    sourceSets {
        val kotlin_coroutine = "1.10.1"  // https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core
        val okio_version="3.10.2"         // https://mvnrepository.com/artifact/com.squareup.okio/okio
        val commonMain by getting
        commonMain.dependencies {
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutine")
            implementation("com.squareup.okio:okio:$okio_version")
        }
        val jsMain by getting
        jsMain.dependencies {
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$kotlin_coroutine")
            implementation("com.squareup.okio:okio-js:$okio_version")
            implementation("com.squareup.okio:okio-nodefilesystem:$okio_version")
        }
        val wasmJsMain by getting
        wasmJsMain.dependencies {
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-wasm-js:$kotlin_coroutine")
            implementation("com.squareup.okio:okio-wasm-js:$okio_version")
            implementation("com.squareup.okio:okio-fakefilesystem-wasm-js:$okio_version")
        }

        val mingwX64Main by getting
        mingwX64Main.dependencies {
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-mingwx64:$kotlin_coroutine")
            implementation("com.squareup.okio:okio-mingwx64:$okio_version")
        }
        val linuxX64Main by getting
        linuxX64Main.dependencies {
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-linuxx64:$kotlin_coroutine")
            implementation("com.squareup.okio:okio-linuxx64:$okio_version")
        }
    }
}

共通コード

  • ファイルの読み書き
  • coroutineによる非同期処理
src/commonMain/kotlin/AppMain.kt
expect val fileSystem: FileSystem

suspend fun appMain() {
    fileSystem.write("test.txt".toPath()) { writeUtf8("test") }
    val f = flow {
        (1..5).forEach {
            emit(it)
            delay(1000)
        }
    }
    GlobalScope.launch { f.collect { println(it) } }
    f.collect { println(it) }
    println(fileSystem.read("test.txt".toPath()) { readUtf8() })
}

ターゲット別コード

jvmターゲット

src/jvmMain/kotlin/Main.kt
fun main() = runBlocking {
    appMain()
}

actual val fileSystem = FileSystem.SYSTEM

jvm以外のターゲット➔GitHub参照。

テスト実行
sh gradlew jvmRun -DmainClass=MainKt    # jvm
sh gradlew jsNodeRun                    # js
sh gradlew runReleaseExecutableMingwX64 # mingwX64 (Windows上で実行)
sh gradlew runReleaseExecutableLinuxX64 # linuxX64
sh gradlew wasmJsNodeRun                # wasmJs

ビルド・実行

Windows上で確認

ビルド
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

  • wasmJsでは仮想的なファイルシステムへの操作となる
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?