Kotlin Multiplatform Mobile入門
![kenken](https://res.cloudinary.com/zenn/image/fetch/s--NawV2LsJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_70/https://storage.googleapis.com/zenn-user-upload/avatar/b9da4a7b17.jpeg)
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](https://res.cloudinary.com/zenn/image/fetch/s--NawV2LsJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_70/https://storage.googleapis.com/zenn-user-upload/avatar/b9da4a7b17.jpeg)
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](https://res.cloudinary.com/zenn/image/fetch/s--NawV2LsJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_70/https://storage.googleapis.com/zenn-user-upload/avatar/b9da4a7b17.jpeg)
次に
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](https://res.cloudinary.com/zenn/image/fetch/s--NawV2LsJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_70/https://storage.googleapis.com/zenn-user-upload/avatar/b9da4a7b17.jpeg)
Android/iOSともに初期画面は表示できた。
Android![kenken](https://res.cloudinary.com/zenn/image/fetch/s--NawV2LsJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_70/https://storage.googleapis.com/zenn-user-upload/avatar/b9da4a7b17.jpeg)
Android StudioからiOSのシミュレータを起動できるのは地味に便利だな〜
ちなみに、起動するシミュレータのバージョンは
Run/Debug Configurations
の
Execution Terget
から変更できる模様。
![kenken](https://res.cloudinary.com/zenn/image/fetch/s--NawV2LsJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_70/https://storage.googleapis.com/zenn-user-upload/avatar/b9da4a7b17.jpeg)
公式サイトの 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.
androidApp
module
androidApp
is a Kotlin module that builds into an Android application.
iosApp
module
iosApp
is an Xcode project that builds into an iOS application.
![kenken](https://res.cloudinary.com/zenn/image/fetch/s--NawV2LsJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_70/https://storage.googleapis.com/zenn-user-upload/avatar/b9da4a7b17.jpeg)
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](https://res.cloudinary.com/zenn/image/fetch/s--NawV2LsJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_70/https://storage.googleapis.com/zenn-user-upload/avatar/b9da4a7b17.jpeg)
ここに全部書いてあったw