添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Zenn
Open 18

Kotlin Multiplatform Mobile入門

kenken kenken

Projectを作成して Sync Project with Gradle Files したら早速エラー😅

A problem occurred configuring root project 'KmmSandbox'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve com.android.tools.build:gradle:8.0.2.
     Required by:
         project : > com.android.application:com.android.application.gradle.plugin:8.0.2
         project : > com.android.library:com.android.library.gradle.plugin:8.0.2
      > No matching variant of com.android.tools.build:gradle:8.0.2 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.0' but:
          - Variant 'apiElements' capability com.android.tools.build:gradle:8.0.2 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.0')
          - Variant 'javadocElements' capability com.android.tools.build:gradle:8.0.2 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.0')
          - Variant 'runtimeElements' capability com.android.tools.build:gradle:8.0.2 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.0')
          - Variant 'sourcesElements' capability com.android.tools.build:gradle:8.0.2 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.0')
kenken kenken

The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar

とあるので、 Android Studio > Settings... > Build, Execution, Deployment > Gradle の中のGradle JDKを確認したところ、確かにJava 8が指定されていた。

Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8

ともあるので、一旦これをJava 11に上げることで Sync Project with Gradle Files は成功。

kenken kenken

次に Run 'androidApp' を実行したところ、次のエラー。

Build file '/Users/XXXX/AndroidStudioProjects/KmmSandbox/androidApp/build.gradle.kts' line: 1
An exception occurred applying plugin request [id: 'com.android.application']
> Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 17 to run. You are currently using Java 11.

Gradle JDKをJava 17に上げることで Run 'androidApp' は成功。

スタートラインに立つまでが一番大変😇

kenken kenken

Android StudioからiOSのシミュレータを起動できるのは地味に便利だな〜
ちなみに、起動するシミュレータのバージョンは Run/Debug Configurations Execution Terget から変更できる模様。

kenken kenken

公式サイトの Examine the project structure を見ると、各モジュールの説明が書いてある。

shared module shared is a Kotlin module that contains the logic common for both Android and iOS applications – the code you share between platforms.
  • It uses Gradle as the build system that helps you automate your build process. The shared module builds into an Android library and an iOS framework .
  • androidApp module androidApp is a Kotlin module that builds into an Android application.
  • It uses Gradle as the build system.
  • The androidApp module depends on and uses the shared module as a regular Android library.
  • iosApp module iosApp is an Xcode project that builds into an iOS application.
  • It depends on and uses the shared module as an iOS framework.
  • The shared module can be used as a regular framework or as a CocoaPods dependency, based on what you've chosen in the previous step in iOS framework distribution.
  • In this tutorial, it's a regular framework dependency.
  • kenken kenken

    iosApp モジュールが shared モジュールを参照するためにデフォルトで行っている設定があまりよく分かっていないが、 この部分 shared という名前のiOS frameworkを作成している模様。

        listOf(
            iosX64(),
            iosArm64(),
            iosSimulatorArm64()
        ).forEach {
            it.binaries.framework {
                baseName = "shared"
    

    iosApp.xcodeproj をXcodeで開き、 TARGETS > Build Settings の中のLinkingを確認したところ、Other Linker Flagsに -framework shared とあるので、どうやらこの部分の設定によって iosApp モジュールが shared モジュール(実体はiOS framework)を参照できるようになっている模様。

    kenken kenken

    ここに全部書いてあったw