0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

KMPを触ってみる

0
Posted at

KMPってなんだっけ

Kotlin Multiplatform。Kotlinでマルチプラットフォームの共通ロジックを一つにまとめられる仕組み。元々はKotlin Multiplatform Mobileと呼ばれてiOSとAndroidのアプリ開発の共通化を目指してたそうですが、今はもっと多くのプラットフォームがターゲットになっているようです。

KMPのいいところは共通ロジックをKotlinで書いて、それぞれのプラットフォームはネイティブで書けるところだと思ってます。

最近SwiftUIで古いプロジェクトを書き直してはいるのですが、Androidもやらないといけないんだよなーと思い、Flutterよりはそれぞれ得意なネイティブコードが書ける点でKMPを導入してみようかなと。思い立ったが吉日。

とりあえず準備

何はともあれサンプルコードでもビルドできるようにしよう。

  • Android Studio
    • Kotlin Multiplatform Plugin(0.5.1)
  • Xcode
  • VSCode

Android StudioやXcodeはすでに入ってたのでpluginだけ入れる。
Fleetっていう公式のエディタもあるけれど、VSCodeでいく。
あ、環境はmacOSです。

プロジェクト作成

Android StudioのNew ProjectからKotlin Multiplatform Appを選んでテンプレートを作成してみた。

まずいきなりiOSシミュレータでビルドを試す。sharedが共通ロジック層なのでまずはこれをビルドする。

./gradlew :shared:assembleXCFramework

Kotlinのバージョンが低かったようでエラーが出るので修正。
1.7.10→1.9.23

プロジェクトのbuild.gradle.kts
plugins {
    //trick: for the same plugin versions in all sub-modules
    id("com.android.application").version("7.3.0").apply(false)
    id("com.android.library").version("7.3.0").apply(false)
    //kotlin("android").version("1.7.10").apply(false)
    kotlin("android").version("1.9.23").apply(false)
    //kotlin("multiplatform").version("1.7.10").apply(false)
    kotlin("multiplatform").version("1.9.23").apply(false)
}

次は以下のエラー。1.9.23に上げたことでAndroidのSourceSet名が変更されているせいっぽいね。

KotlinSourceSet with name 'androidTest' not found:
The SourceSet requested ('androidTest') was renamed in Kotlin 1.9.0

androidTestをandroidUnitTest変更。

sharedのbuild.gradle.kts
sourceSets {
    val commonMain by getting
    val commonTest by getting {
        dependencies {
            implementation(kotlin("test"))
        }
    }
    val androidMain by getting
    // val androidTest by getting
    val androidUnitTest by getting
    val iosX64Main by getting
    val iosArm64Main by getting
    val iosSimulatorArm64Main by getting
    val iosMain by creating {
        dependsOn(commonMain)
        iosX64Main.dependsOn(this)
        iosArm64Main.dependsOn(this)
        iosSimulatorArm64Main.dependsOn(this)
    }
    val iosX64Test by getting
    val iosArm64Test by getting
    val iosSimulatorArm64Test by getting
    val iosTest by creating {
        dependsOn(commonTest)
        iosX64Test.dependsOn(this)
        iosArm64Test.dependsOn(this)
        iosSimulatorArm64Test.dependsOn(this)
    }
}

今度はビルドできたので、XcodeのTARGETSを開いて、Link Binary With Librariesにshared.xcframeworkを追加する。

これでAndroidとiOSでロジックの共通化ができる。

ドキュメント

0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?