The SDK in Kotlin Multiplatform
The SDK is a Kotlin Multiplatform library, so a Kotlin Multiplatform project adds it once, to its common code, and reads the configuration with the same Kotlin API on every target: Android, the JVM, iOS, macOS, tvOS, Linux and Windows natively, and the browser with Kotlin/JS or Kotlin/Wasm. The options, the configuration, the gate, the strings, the errors and the push update are on the page of Android and the JVM, which is the same Kotlin API; this page is what changes from one target to the next: the entry point, the files, when the SDK checks, the language and the app type it sends. What the SDK does, what the API key allows and how the SDK behaves offline are on the SDK page.
Install
The SDK is on Maven Central, at version 0.9.0, the same release as on the other platforms:
kotlin {
sourceSets {
commonMain.dependencies {
implementation("cloud.squiddy:squiddy:0.9.0")
}
}
}
Gradle picks each target's artifact from the SDK's module metadata, and the common code sees the API every target has: Squiddy, SquiddyOptions, the states, the gate, the strings and the errors. config, gate and strings are StateFlows, so kotlinx-coroutines comes with the SDK. The SDK is built with Kotlin 2.4.21: a project on an older Kotlin may not read its libraries, so use Kotlin 2.4 or later. A project needs only the targets it builds: the SDK does not ask for the others.
The targets
| Platform | Kotlin targets | Entry point | app_type sent to Squiddy | Minimum |
|---|---|---|---|---|
| Android | the Android target | Squiddy.initialize(context, options) | android | API 24; the app compiles against API 37 |
| JVM: desktop and server | jvm | Squiddy.initialize(options, storageDirectory = null), a java.nio.file.Path | desktop | Java 17 |
| iOS | iosArm64, iosSimulatorArm64, iosX64 | Squiddy.initialize(options) | ios | iOS 15 |
| macOS | macosArm64, macosX64 | Squiddy.initialize(options) | desktop | macOS 12 |
| tvOS | tvosArm64, tvosSimulatorArm64, tvosX64 | Squiddy.initialize(options) | tvos | tvOS 15 |
| Linux | linuxX64, linuxArm64 | Squiddy.initialize(options, storageDirectory = null), a String | desktop | glibc 2.19 on x64, 2.25 on ARM |
| Windows | mingwX64 | Squiddy.initialize(options, storageDirectory = null), a String | desktop | Windows 10, x64 |
| Browser and Node.js, Kotlin/JS | js | Squiddy.initialize(options); in Node.js Squiddy.initialize(options, storageDirectory) | web | a browser with ES modules, fetch and localStorage; Node.js 22.3 |
| Browser, Kotlin/Wasm | wasmJs | Squiddy.initialize(options) | web | a browser with WebAssembly GC: Chrome 119, Firefox 120, Safari 18.2 |
The app type is the platform's, never the app's choice, and it is what a configuration's blocks match in appTypes (the format of a configuration): a configuration that has no block for an app type refuses its apps with 5001. The desktop is one app type, desktop, whether the app runs on the JVM or natively, on a Mac, on Linux or on Windows; an Apple TV app has its own, tvos. An app for Apple Vision Pro runs the iOS targets as an iPad app, and sends ios (visionOS). There is no watchOS target yet (watchOS).
macosX64 and tvosX64, the targets of Intel Macs, are deprecated by Kotlin 2.4 for a removal in a later release: the SDK keeps publishing them while Kotlin has them.
Create the instance
Each entry point is the platform's, since Android needs a Context and the JVM, Linux and Windows take the directory of the files, so there is no initialize in the common code: the common code declares a function, and each platform implements it with its own. The entry points are extensions of Squiddy.Companion, imported as cloud.squiddy.sdk.initialize. Create one instance per app and Squiddy environment, and keep it.
// commonMain
expect fun createSquiddy(options: SquiddyOptions): Squiddy
// appleMain: iOS, macOS and tvOS
actual fun createSquiddy(options: SquiddyOptions): Squiddy = Squiddy.initialize(options)
// jvmMain, linuxMain and mingwMain: the default directory of the files, or one of the app's
actual fun createSquiddy(options: SquiddyOptions): Squiddy = Squiddy.initialize(options, storageDirectory = null)
// jsMain and wasmJsMain: a page
actual fun createSquiddy(options: SquiddyOptions): Squiddy = Squiddy.initialize(options)
// androidMain: the application's context, which the Application stores in onCreate
actual fun createSquiddy(options: SquiddyOptions): Squiddy = Squiddy.initialize(applicationContext, options)
The options are common. bundleId is required where the platform has no bundle id to read: on the JVM, on Linux, on Windows, in Node.js, and in a macOS program outside an app bundle; elsewhere the SDK reads the app's own (the package name, the main bundle, the page's host name). On the JVM, Linux and Windows it names the SDK's directory, so it is made of letters, digits, ., - and _, such as com.example.app. initialize checks every option before it creates anything and throws an IllegalArgumentException that names the one to fix; on the JVM, Linux and Windows, an IllegalStateException when it finds no directory for the files and none was chosen.
What changes from one target to the next
| Target | Files | When the SDK checks | Language |
|---|---|---|---|
| Android | noBackupFilesDir/squiddy, never backed up | at start, on every return to the foreground (an activity started) | the default locale, followed while the app runs |
| JVM | the chosen directory, or <bundle id>/squiddy in the user's data directory: Application Support on macOS, %LOCALAPPDATA% on Windows, $XDG_DATA_HOME or ~/.local/share elsewhere | always in the foreground: at start, then at the configuration's refreshInterval | the default locale, read again at every check |
| iOS | Library/Application Support/squiddy, out of the backups | at start, on every return to the foreground | the first preferred language |
| macOS | Library/Application Support/<bundle id>/squiddy, out of Time Machine | at start, whenever a window becomes visible again (macOS) | the first preferred language |
| tvOS | Library/Caches/squiddy, which tvOS may purge (tvOS) | at start, on every return to the foreground | the first preferred language |
| Linux | the chosen directory, or $XDG_DATA_HOME/<bundle id>/squiddy, or ~/.local/share/<bundle id>/squiddy | always in the foreground, as on the JVM | the locale of the messages: LC_ALL, LC_MESSAGES, then LANG, read again at every check |
| Windows | the chosen directory, or %LOCALAPPDATA%\<bundle id>\squiddy | always in the foreground, as on the JVM | the user's display language, read again at every check |
| Browser, Kotlin/JS and Kotlin/Wasm | the page's localStorage, under keys that start with cloud.squiddy.sdk/ | at start, whenever the page becomes visible again | navigator.language, followed while the page is open |
| Node.js, Kotlin/JS | the chosen directory, or the memory of the process | always in the foreground, as on the JVM | the language option, or the process's default locale |
On every target the configuration's refreshInterval adds a check at that interval while the app is in the foreground (the periodic check), and refresh() checks at once. Where the app is always in the foreground, those are the only checks after the start, with a change of language through setLanguage and handlePush. The JVM and the native builds of one application, on the same computer, keep the same files in the same directory.
Linux
- The C library: the Linux targets need glibc, as every Kotlin/Native program does, so a distribution with musl, such as Alpine, cannot run them. From the system they need
libz.so.1andlibcrypt.so.1: Ubuntu, Debian, Rocky Linux and openSUSE have both; on Fedora, installlibxcrypt-compat. - HTTPS: the calls go through libcurl with OpenSSL, which the SDK brings into the program. They trust the certification authorities of the system: the file
SSL_CERT_FILEnames, or else the first that exists among the places of the distributions (/etc/ssl/certs/ca-certificates.crtof Debian and Ubuntu,/etc/pki/tls/certs/ca-bundle.crtof Fedora and Red Hat,/etc/ssl/ca-bundle.pemof openSUSE, and the others). A container image without its distribution's certificates (theca-certificatespackage) fails every call with a TLS error, theNetworkerror of the SDK. - The language is
undfor the localesCandPOSIX, andLANGUAGEis not read, as the JVM does not read it: an app with a language of its own passes thelanguageoption or callssetLanguage.
Windows
The calls go through WinHttp, Windows' own HTTP stack, with the certificates of Windows. The files are in %LOCALAPPDATA%, which a roaming profile does not carry to another computer, and the environment is read in UTF-16, so a profile such as C:\Users\Zoë works. The SDK has one Windows target, mingwX64, for Windows on x64.
The browser: Kotlin/JS and Kotlin/Wasm
A web app written in Kotlin, compiled to JavaScript or to WebAssembly (Compose for Web), uses the same Kotlin API as the other targets, from Maven Central. JavaScript and TypeScript use the npm package instead, with an API written for them: the SDK on the web, which is the same library as the js target.
- Kotlin/Wasm needs a browser that runs WebAssembly with garbage collection, as Kotlin documents it: Chrome 119, Firefox 120, Safari 18.2 or later. Its target is for the browser only: a Kotlin/Wasm program in Node.js is not supported.
- The files are the page's
localStorage, with the format of the other platforms: the npm package, a Kotlin/JS app and a Kotlin/Wasm app of the same origin read the same stored configuration and the same installation id. Where the browser refuseslocalStorage, the SDK keeps them in memory, for the life of the page. - The calls are the browser's
fetch, with the CORS answers of the data plane, as in Calls and CORS. - No push update in a page, which cannot subscribe to Firebase: a page checks whenever it becomes visible again, and at the configuration's
refreshInterval.
Push update
pushTopic and handlePush(data) are common, and the push update reaches an app through Firebase Cloud Messaging, which the app receives with Firebase's own SDK for its platform and hands to handlePush: on Android (push update), and on the Apple platforms through APNs (iOS, macOS, tvOS). On the JVM, on Linux, on Windows and in a browser no Firebase message reaches the app, so a change of the configuration arrives at the next check: give their block a refreshInterval if the app must see it while it runs.