Zenn
Closed7

kotlin multiplatformでmacosArm64向けビルドしたい

K.IK.I

環境: M1 MacBookAir

下記記事を参考にしようとしたらkotlin何もわからないせいで前段階でつまづいているので、一旦macosArm64で動くバイナリを作れる状態にするのが目標。
https://qiita.com/tkhskt/items/2bb7c20405e720a759eb

まずIntelliJ IDEAで新しいプロジェクトを作ってみる。
サンプルコードを実行すると下記エラー

Inconsistent JVM-target compatibility detected for tasks 'compileJava' (23) and 'compileKotlin' (22)

build.gradle.ktsのkotlin("jvm") version "2.0.21"kotlin("jvm") version "2.1.0"に修正したら動いた。
参考: https://discuss.gradle.org/t/inconsistent-jvm-target-compatibility-detected-for-tasks-compilejava-23-and-compilekotlin-20/49971/3

K.IK.I

build.gradle.ktsを変更してみる。

build.gradle.kts
plugins {
    kotlin("multiplatform") version "2.1.0"
}

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

repositories {
    mavenCentral()
}

kotlin {
    macosArm64("macosArm64") {
        binaries {
            executable {
                entryPoint = "org.example.main"
            }
        }
    }
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib"))
                implementation("com.github.ajalt.clikt:clikt:5.0.2")
            }
        }
    }
}

dependencies {
    testImplementation(kotlin("test"))
}

tasks.test {
    useJUnitPlatform()
}

macosArm64Unresolved reference: macosArm64というエラーがIDEに出ているが修正方法わからず。

K.IK.I

一度./gradlew build実行してみる。

build.gradle.kts:30:7: Unresolved reference: test
build.gradle.kts:31:5: Unresolved reference: useJUnitPlatform

…一度dependenciesとtasks.testをコメントアウトし、再度実行。

org.jetbrains.kotlin.konan.MissingXcodeException: An error occurred during an xcrun execution. Make sure that Xcode and its command line tools are properly installed.
Failed command: /usr/bin/xcrun xcodebuild -version
Try running this command in Terminal and fix the errors by making Xcode (and its command line tools) configuration correct.

開発マシン(macbookAir)にxcodeがインストールされていなかったためAppStoreからダウンロードする。

K.IK.I
e: Failed to build cache for /Users/user/.gradle/caches/modules-2/files-2.1/com.github.ajalt.clikt/clikt-core-macosarm64/5.0.2/7e622b4851d9408b15eb4326a7db537acafd626a/clikt-macosArm64Main.
As a workaround, please try to disable compiler caches (kotlin.native.cacheKind=none)

Also, consider filing an issue with full Gradle log here: https://kotl.in/issue

An error occurred during an xcrun execution. Make sure that Xcode and its command line tools are properly installed.
Failed command: /usr/bin/xcrun -f ld
Try running this command in Terminal and fix the errors by making Xcode (and its command line tools) configuration correct.

org.jetbrains.kotlin.konan.MissingXcodeException: An error occurred during an xcrun execution. Make sure that Xcode and its command line tools are properly installed.
Failed command: /usr/bin/xcrun -f ld
Try running this command in Terminal and fix the errors by making Xcode (and its command line tools) configuration correct.

xcodeのパスを変更する。
$ xcode-select -switch /Applications/Xcode.app
xcodebuildのライセンスに同意する。下記を実行してagreeを入力。
$ sudo xcodebuild -license
再度./gradlew build実行したところビルド成功。

生成されたバイナリを下記コマンドで実行することができた。
$ ./build/bin/macosArm64/releaseExecutable/cli.kexe

K.IK.I

バイナリは実行できたがIntelliJ IDEAでデバッグモードを起動することができないので修正する。

Cannot locate tasks that match ':classes' as task 'classes' not found in root project 'cli'.

build.gradle.ktsにjvm()を追加。

build.gradle.kts
...
kotlin {
    jvm() // ←追加
    macosArm64("macosArm64") {
      ...
    }
    sourceSets {
      ...
    }
}

サイドバーのgradleプラグインからSyncアイコン🔄をクリック。

Syncが完了したら、IntelliJ IDEAを閉じて再度プロジェクトを開く。

main関数のデバッグができるようになった。

K.IK.I

テストを実行できるようにする。
まずサンプルとして./src/commonMein/kotlin/Hello.ktと./src/commonTest/kotlin/TestHello.ktを作成する。

Hello.kt
package org.example

class Hello(name: String) {
    val value = "Hello, $name!"
}
TestHello.kt
import org.example.Hello
import kotlin.test.Test
import kotlin.test.assertEquals

class HelloTest {

    @Test
    fun testHello() {
        val hello = Hello("Kotlin")
        assertEquals("Hello, Kotlin!", hello.value)
    }
}

build.gradle.ktにテスト用の設定を追加する。

build.gradle.kt
...
kotlin {
    ...
    sourceSets {
        ...
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
                implementation("org.junit.jupiter:junit-jupiter-api:5.11.4")
                runtimeOnly("org.junit.jupiter:junit-jupiter-engine:5.11.4")
            }
        }
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

テスト用クラスをIDEで実行 or $ ./gradlew jvmTestで実行できればOK

K.IK.I

なお$ ./gradlew macosArm64Testを実行するとCould not resolve org.junit.jupiter:junit-jupiter-api:5.11.4.というエラー。
JUnitがnativeプラットフォームに対応していないためエラーになる模様。
マルチプラットフォーム対応のテストフレームワークを使うのも手かも。
https://www.slideshare.net/slideshow/kotest-kotlinfest-2024/269819350

このスクラップは3ヶ月前にクローズされました
作成者以外のコメントは許可されていません