LoginSignup
5
3

Kotlin 2.0.0でKMPのSwiftPM export機能が入る(のかもしれない)

Last updated at Posted at 2024-04-27

Kotlin Multiplatform Gradle Plugin supports SwiftPM Export

  • 今までFramework or XCFrameworkのDilectLink, CocoaPodsしかサポートされていなかったがSwiftPMのexportも公式にサポートされることになった
  • 2.0.0-Beta5で彗星の如く機能が追加されてRC-1にも入っている
  • RC-1で確認した

  • YouTrackもinternalなものなようで記事執筆時点ではみれない

  • まだ2.0.0時点でもExperimentalな機能なのでgradle.propertiesでフラグを有効にしないと使えない

kotlin.swift-export.enabled=true

どうなる

  • ${target}${BuildType}SwiftExportというタスクが生える
  • 実行するとエラーになってこける
Showing Recent Issues
Execution failed for task ':shared:iosSimulatorArm64DebugSwiftExport'.

> Could not resolve all files for configuration ':shared:swiftExportClasspathResolvable'.
   > Could not find org.jetbrains.kotlin:swift-export-embeddable:2.0.0-RC1.
     Searched in the following locations:
       - https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/swift-export-embeddable/2.0.0-RC1/swift-export-embeddable-2.0.0-RC1.pom
     Required by:
         project :shared

* Try:
> If the artifact you are trying to retrieve can be found in the repository but without metadata in 'Maven POM' format, you need to adjust the 'metadataSources { ... }' of the repository declaration.
> Run with --info or --debug option to get more log output.
  • swift-export-embeddableなるモジュールをGradle Pluginが追加するらしいがそれがmavenで参照できないエラーを起こしている


integration testが追加されているのになぜartifactをpublishしていない・・・・・なぜ・・・・

どうなるであろうかを見てみる

  • integration testを見てみるとSwiftPM Exportと言いつつ実態はkotlinをswiftに変換した上でSwiftPMに突っ込むっぽい
import SwiftUI
@testable import Shared

struct ContentView: View {
    var body: some View {
        VStack(spacing: 8, content: {
            Text("foo: \(com.github.jetbrains.swiftexport.foo())")
            Text("bar: \(com.github.jetbrains.swiftexport.bar())")
            Text("foobar 5: \(com.github.jetbrains.swiftexport.foobar(param: 5))")
        })
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
  • 今までであればtop level functionは${file_name}Kt.${fun}みたいになっていたものが素直に呼べるようになっているのと名前付き引数もサポートしている

  • いろいろハードルはありそう

  • Kotlin上でval宣言(immutable)しているのにSwift上ではvar(mutable)として定義されてしまってたり・・・・

  • 以下のクラスたちはそもそもexportされないっぽいので銀の弾丸ではない様子・・・・悲しい・・・

public annotation class OptIn

enum ENUM {
    class INSIDE_ENUM
}

interface OUTSIDE_PROTO {
    class INSIDE_PROTO
}

class INHERITANCE_COUPLE : OUTSIDE_PROTO.INSIDE_PROTO, OUTSIDE_PROTO
class INHERITANCE_SINGLE_PROTO : OUTSIDE_PROTO.INSIDE_PROTO

open class OPEN_CLASS

class INHERITANCE_SINGLE_CLASS : OPEN_CLASS

object OBJECT

data class DATA_CLASS(val a: Int)

data class DATA_CLASS_WITH_REF(val o: OBJECT)

inline class INLINE_CLASS(val a: Int)

inline class INLINE_CLASS_WITH_REF(val i: DATA_CLASS_WITH_REF)

abstract class ABSTRACT_CLASS

sealed class SEALED {
    object O : SEALED()
}
5
3
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
5
3