Isolating kotlin api called by swift to iosapi module to improve compile time

From 14 minutes to 15 seconds!
This commit is contained in:
Olof Hedman
2025-10-15 19:32:23 +02:00
parent 2c662dbb4c
commit cef1f50c56
666 changed files with 25147 additions and 73 deletions

41
iosapi/build.gradle.kts Normal file
View File

@@ -0,0 +1,41 @@
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework
plugins {
kotlin("multiplatform")
// no Android app here; this module exists only to publish the iOS framework
kotlin("native.cocoapods")
}
kotlin {
// Only device if that's all you use:
iosArm64()
// Cocoapods is used for 3rd party library dependencies in iOS
cocoapods {
version = "1.0.0" // Out version of the podspec
ios.deploymentTarget = "14.0"
summary = "iOS facade"
homepage = "https://example.com"
framework {
baseName = "shared"
isStatic = true
}
// This generates the cinterop => `platform.GoogleMaps.*`
pod("GoogleMaps") // keep version unspecified to track latest podspec
{
// To support clang modules
extraOpts += listOf("-compiler-option", "-fmodules")
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation(project(":shared"))
}
}
val commonTest by getting
val iosArm64Main by getting
}
}

54
iosapi/iosapi.podspec Normal file
View File

@@ -0,0 +1,54 @@
Pod::Spec.new do |spec|
spec.name = 'iosapi'
spec.version = '1.0.0'
spec.homepage = 'https://example.com'
spec.source = { :http=> ''}
spec.authors = ''
spec.license = ''
spec.summary = 'iOS facade'
spec.vendored_frameworks = 'build/cocoapods/framework/shared.framework'
spec.libraries = 'c++'
spec.ios.deployment_target = '14.0'
spec.dependency 'GoogleMaps'
if !Dir.exist?('build/cocoapods/framework/shared.framework') || Dir.empty?('build/cocoapods/framework/shared.framework')
raise "
Kotlin framework 'shared' doesn't exist yet, so a proper Xcode project can't be generated.
'pod install' should be executed after running ':generateDummyFramework' Gradle task:
./gradlew :iosapi:generateDummyFramework
Alternatively, proper pod installation is performed during Gradle sync in the IDE (if Podfile location is set)"
end
spec.xcconfig = {
'ENABLE_USER_SCRIPT_SANDBOXING' => 'NO',
}
spec.pod_target_xcconfig = {
'KOTLIN_PROJECT_PATH' => ':iosapi',
'PRODUCT_MODULE_NAME' => 'shared',
}
spec.script_phases = [
{
:name => 'Build iosapi',
:execution_position => :before_compile,
:shell_path => '/bin/sh',
:script => <<-SCRIPT
if [ "YES" = "$OVERRIDE_KOTLIN_BUILD_IDE_SUPPORTED" ]; then
echo "Skipping Gradle build task invocation due to OVERRIDE_KOTLIN_BUILD_IDE_SUPPORTED environment variable set to \"YES\""
exit 0
fi
set -ev
REPO_ROOT="$PODS_TARGET_SRCROOT"
"$REPO_ROOT/../gradlew" -p "$REPO_ROOT" $KOTLIN_PROJECT_PATH:syncFramework \
-Pkotlin.native.cocoapods.platform=$PLATFORM_NAME \
-Pkotlin.native.cocoapods.archs="$ARCHS" \
-Pkotlin.native.cocoapods.configuration="$CONFIGURATION"
SCRIPT
}
]
end

View File

@@ -0,0 +1,56 @@
package app.klottr.api
// This file defines ONLY what Swift needs. Everything else stays in :shared.
/**
* Public delegate protocol visible to Swift.
* Keep this in :iosapi so we don't leak :shared types in the public headers.
*/
interface AuthStorageDelegate {
fun get(key: String): String?
fun set(key: String, value: String?)
fun clear()
}
/**
* IMPORTANT: make this an `object`, not a `class`.
* Kotlin/Native exposes `object Foo` to Swift as `Foo.shared`.
*/
object PlatformAuthStorage {
// Adapter to bridge iosapi.AuthStorageDelegate -> shared.AuthStorageDelegate
private class SharedDelegateAdapter(
private val d: AuthStorageDelegate
) : app.klottr.util.AuthStorageDelegate {
override fun get(key: String): String? = d.get(key)
override fun set(key: String, value: String?) = d.set(key, value)
override fun clear() = d.clear()
}
fun install(delegate: AuthStorageDelegate) {
app.klottr.util.PlatformAuthStorage.install(SharedDelegateAdapter(delegate))
}
fun get(key: String): String? = app.klottr.util.PlatformAuthStorage.get(key)
fun set(key: String, value: String?) { app.klottr.util.PlatformAuthStorage.set(key, value) }
fun clear() { app.klottr.util.PlatformAuthStorage.clear() }
}
// Public API that Swift calls, delegating into your real implementation in :shared
//
object AuthShared {
fun applyLogin(token: String?, sub: String?, mail: String?) {
// call into your existing code in :shared
app.klottr.util.AuthShared.applyLogin(token, sub, mail)
}
fun loadFromStorage() {
app.klottr.util.AuthShared.loadFromStorage()
}
fun clearAll() {
app.klottr.util.AuthShared.clearAll()
}
}
// (Optional) whatever tiny test surface Swift currently uses
class SharedPing {
fun ping(): String = "iosapi ping"
}

View File

@@ -0,0 +1,19 @@
@file:Suppress("FunctionName")
package app.klottr.api
import platform.UIKit.UIViewController
// Keep the exact symbol Swift uses: MainKt.MainViewController(...)
fun MainViewController(
onRequestPerms: () -> Unit = {},
onLogin: () -> Unit = {},
onLogout: () -> Unit = {},
): UIViewController =
// Forward to your existing implementation in :shared
app.klottr.MainViewController(
onRequestPerms = onRequestPerms,
onLogin = onLogin,
onLogout = onLogout
)