|
| 1 | +/* |
| 2 | + * Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation |
| 6 | +import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget |
| 7 | +import org.jetbrains.kotlin.konan.target.KonanTarget |
| 8 | + |
| 9 | +plugins { |
| 10 | + id("org.jetbrains.kotlin.multiplatform") |
| 11 | +} |
| 12 | + |
| 13 | +/* |
| 14 | +This code ensures that the Bundle in an iOS application, built with Kotlin Multiplatform (KMP), can be correctly |
| 15 | +located at runtime. The issue arises because Kotlin doesn’t allow direct lookup of a Bundle by a class from |
| 16 | +Objective-C. To resolve this, a static library written in Objective-C was created and automatically included in the |
| 17 | +Kotlin Framework during the build process. This library contains a class used to locate the required Bundle. |
| 18 | +
|
| 19 | +Key steps performed by the code: |
| 20 | +
|
| 21 | +1. Handling Apple targets in KMP: |
| 22 | + The code automatically configures the build for Apple platforms only (iOS, macOS, tvOS, watchOS). |
| 23 | +2. Compiling and linking the static library: |
| 24 | + - clang is used to compile the source file MRResourcesBundle.m into an object file. |
| 25 | + - The object file is linked into a static library (libMRResourcesBundle.a) using the ar utility. |
| 26 | +3. Integrating the static library into the Kotlin Framework: |
| 27 | + - A C-interop is created, enabling Kotlin to interact with the Objective-C code from the library. |
| 28 | + - The C-interop task is configured to depend on the compilation and linking tasks, ensuring the library is ready for |
| 29 | + use during the build process. |
| 30 | +4. Support for multiple Apple platforms: |
| 31 | + - The code adapts the build process for specific Apple SDKs and architectures by using helper functions getAppleSdk |
| 32 | + and getClangTarget. |
| 33 | +5. Retrieving the SDK path: |
| 34 | + The xcrun utility is used to dynamically fetch the SDK path required by clang. |
| 35 | +
|
| 36 | +What does this achieve? |
| 37 | +
|
| 38 | +As a result, a Kotlin Multiplatform application for iOS, macOS, tvOS, or watchOS can correctly locate the Bundle |
| 39 | +containing resources by leveraging standard Apple APIs wrapped in the static library. This process is fully automated |
| 40 | +during the project build, requiring no manual intervention from the developer. |
| 41 | +
|
| 42 | +Bundle search logic: |
| 43 | +resources/src/appleMain/kotlin/dev/icerock/moko/resources/utils/NSBundleExt.kt |
| 44 | +*/ |
| 45 | + |
| 46 | +kotlin.targets |
| 47 | + .withType<KotlinNativeTarget>() |
| 48 | + .matching { it.konanTarget.family.isAppleFamily } |
| 49 | + .configureEach { |
| 50 | + val sdk: String = this.konanTarget.getAppleSdk() |
| 51 | + val target: String = this.konanTarget.getClangTarget() |
| 52 | + |
| 53 | + val sdkPath: String = getSdkPath(sdk) |
| 54 | + |
| 55 | + val libsDir = File(buildDir, "moko-resources/cinterop/$name") |
| 56 | + libsDir.mkdirs() |
| 57 | + val sourceFile = File(projectDir, "src/appleMain/objective-c/MRResourcesBundle.m") |
| 58 | + val objectFile = File(libsDir, "MRResourcesBundle.o") |
| 59 | + val libFile = File(libsDir, "libMRResourcesBundle.a") |
| 60 | + val kotlinTargetPostfix: String = this.name.capitalize() |
| 61 | + |
| 62 | + val compileStaticLibrary = tasks.register("mokoBundleSearcherCompile$kotlinTargetPostfix", Exec::class) { |
| 63 | + group = "moko-resources" |
| 64 | + |
| 65 | + commandLine = listOf( |
| 66 | + "clang", |
| 67 | + "-target", |
| 68 | + target, |
| 69 | + "-isysroot", |
| 70 | + sdkPath, |
| 71 | + "-c", |
| 72 | + sourceFile.absolutePath, |
| 73 | + "-o", |
| 74 | + objectFile.absolutePath |
| 75 | + ) |
| 76 | + outputs.file(objectFile.absolutePath) |
| 77 | + } |
| 78 | + val linkStaticLibrary = tasks.register("mokoBundleSearcherLink$kotlinTargetPostfix", Exec::class) { |
| 79 | + group = "moko-resources" |
| 80 | + |
| 81 | + dependsOn(compileStaticLibrary) |
| 82 | + |
| 83 | + commandLine = listOf( |
| 84 | + "ar", |
| 85 | + "rcs", |
| 86 | + libFile.absolutePath, |
| 87 | + objectFile.absolutePath |
| 88 | + ) |
| 89 | + outputs.file(libFile.absolutePath) |
| 90 | + } |
| 91 | + |
| 92 | + compilations.getByName(KotlinCompilation.MAIN_COMPILATION_NAME) { |
| 93 | + val bundleSearcher by cinterops.creating { |
| 94 | + defFile(project.file("src/appleMain/def/bundleSearcher.def")) |
| 95 | + |
| 96 | + includeDirs("$projectDir/src/appleMain/objective-c") |
| 97 | + extraOpts("-libraryPath", libsDir.absolutePath) |
| 98 | + } |
| 99 | + |
| 100 | + tasks.named(bundleSearcher.interopProcessingTaskName).configure { |
| 101 | + dependsOn(linkStaticLibrary) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +fun KonanTarget.getAppleSdk(): String { |
| 107 | + return when (this) { |
| 108 | + KonanTarget.IOS_ARM32, |
| 109 | + KonanTarget.IOS_ARM64 -> "iphoneos" |
| 110 | + |
| 111 | + KonanTarget.IOS_SIMULATOR_ARM64, |
| 112 | + KonanTarget.IOS_X64 -> "iphonesimulator" |
| 113 | + |
| 114 | + KonanTarget.MACOS_ARM64, |
| 115 | + KonanTarget.MACOS_X64 -> "macosx" |
| 116 | + |
| 117 | + KonanTarget.TVOS_ARM64 -> "appletvos" |
| 118 | + |
| 119 | + KonanTarget.TVOS_SIMULATOR_ARM64, |
| 120 | + KonanTarget.TVOS_X64 -> "appletvsimulator" |
| 121 | + |
| 122 | + KonanTarget.WATCHOS_ARM32, |
| 123 | + KonanTarget.WATCHOS_DEVICE_ARM64 -> "watchos" |
| 124 | + |
| 125 | + KonanTarget.WATCHOS_ARM64, |
| 126 | + KonanTarget.WATCHOS_SIMULATOR_ARM64, |
| 127 | + KonanTarget.WATCHOS_X64, |
| 128 | + KonanTarget.WATCHOS_X86 -> "watchsimulator" |
| 129 | + |
| 130 | + else -> error("Unsupported target for selecting SDK: $this") |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +fun KonanTarget.getClangTarget(): String { |
| 135 | + return when (this) { |
| 136 | + KonanTarget.IOS_ARM32 -> "armv7-apple-ios" |
| 137 | + KonanTarget.IOS_ARM64 -> "aarch64-apple-ios" |
| 138 | + KonanTarget.IOS_SIMULATOR_ARM64 -> "aarch64-apple-ios-simulator" |
| 139 | + KonanTarget.IOS_X64 -> "x86_64-apple-ios-simulator" |
| 140 | + |
| 141 | + KonanTarget.MACOS_ARM64 -> "aarch64-apple-macosx" |
| 142 | + KonanTarget.MACOS_X64 -> "x86_64-apple-macosx" |
| 143 | + |
| 144 | + KonanTarget.TVOS_ARM64 -> "aarch64-apple-tvos" |
| 145 | + KonanTarget.TVOS_SIMULATOR_ARM64 -> "aarch64-apple-tvos-simulator" |
| 146 | + KonanTarget.TVOS_X64 -> "x86_64-apple-tvos-simulator" |
| 147 | + |
| 148 | + KonanTarget.WATCHOS_ARM32 -> "armv7k-apple-watchos" |
| 149 | + KonanTarget.WATCHOS_ARM64 -> "arm64_32-apple-watchos" |
| 150 | + KonanTarget.WATCHOS_DEVICE_ARM64 -> "aarch64-apple-watchos" |
| 151 | + KonanTarget.WATCHOS_SIMULATOR_ARM64 -> "aarch64-apple-watchos-simulator" |
| 152 | + KonanTarget.WATCHOS_X64 -> "x86_64-apple-watchos-simulator" |
| 153 | + KonanTarget.WATCHOS_X86 -> "i386-apple-watchos" |
| 154 | + |
| 155 | + else -> error("Unsupported target for selecting clang target: $this") |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +fun getSdkPath(sdk: String): String { |
| 160 | + val process = ProcessBuilder("xcrun", "--sdk", sdk, "--show-sdk-path") |
| 161 | + .redirectErrorStream(true) |
| 162 | + .start() |
| 163 | + return process.inputStream.bufferedReader().use { it.readText().trim() } |
| 164 | +} |
0 commit comments