wrkm.app
Search
Mobile Development

Migrating a Real Android App to Kotlin Multiplatform 4/4 — Shipping WebAssembly to Productio

Migrating a Real Android App to Kotlin Multiplatform 4/4 — Shipping WebAssembly to Productio

Editor
Nov 17, 2026 · 9 min read
Migrating a Real Android App to Kotlin Multiplatform 4/4 — Shipping WebAssembly to Productio

The wasm build runs locally in minutes. ./gradlew wasmJsBrowserDevelopmentRun, a browser tab opens, the app is there. It feels finished.It isn’t, and the reason is the subject of this article. The browser isn’t a fourth target that happens to render the same Compose code. It’s a host with its own rules about what paints when, who owns navigation, what a page is allowed to fetch, and what a database even is. Compose Multiplatform hides none of that — it just gets your UI onto the canvas, and then you’re in someone else’s house.Getting a multiplatform app to build is different from making it production-ready. Everything below is a thing that worked locally and didn’t work for users.Nothing paints until the bundle arrivesOn Android, the launcher draws your icon the instant the user taps it. The OS owns that moment, and you get it for free.On the web you get nothing. A blank page, for as long as it takes to fetch a wasm bundle — around 19MB — uncompressed, and around 6.5MB Brotli transfer size — and compile it. Users don’t read that as “loading”, they read it as broken, and they leave before the first frame.So the first thing I had to write for the web was something Android had given me for a decade: a splash screen. It lives inline in index.html, CSS and all, specifically so it costs no request of its own — a splash that waits on a stylesheet defeats the purpose.The color values are a hand-copied duplicate of the theme tokens in core:designsystem, so the canvas takes over without a colour change. It’s duplication I’d normally reject, and here there’s no alternative: the theme lives in Kotlin inside the bundle that hasn’t downloaded yet.It also has a known flaw I decided to keep. The splash follows the OS colour scheme, while the app also honours a theme the user picked by hand in settings. Someone who forced the theme against their OS gets one flash at the handover. Reading their choice would mean parsing the stored settings JSON from plain JavaScript, and coupling the boot page to that format is worse than a flash.Handing control back to Kotlin is a global function and an @JsFun:https://medium.com/media/443a1311a5de1a41527f3efc2360611d/hrefThe progress bar I didn’t buildThe obvious upgrade is a real percentage: read Content-Length, count the bytes as they arrive, fill a bar.I looked into it and didn’t build it, because the number would have lied. Served with Brotli, Content-Length is the compressed size while the stream hands you decompressed bytes, so the counter sails past 100% and sits there. The fixes are real but none of them are free: write the true uncompressed size into the build with a Gradle task, or clamp the bar at 0.99 and hope nobody notices.And underneath that is a question I hadn’t answered: is the wait download or compile? A progress bar fed by downloaded bytes reaches 100% and then stops, while the browser compiles — and a bar stuck at full is worse than no bar at all.What shipped instead is a spinner that can’t lie, plus the thing a progress bar wouldn’t have given me anyway: a boot that fails visibly. Thirty seconds, or three after an uncaught error, and the spinner is replaced by a message and a Reload button. A failed load used to be a spinner turning forever.That’s the honest version of this section: the fancy feature was the wrong problem, and the unglamorous failure state was the right one.The browser owns navigationCompose Multiplatform can mirror the nav back stack into browser history:https://medium.com/media/ec50e295b82217ac947bdb62de40acdf/hrefOne line, and Back, Forward and the address bar drive in-app navigation. It’s the single highest-value line in the wasm build.It also decides the shape of every URL the app has, and that turned out to matter a great deal.A deep link that 404'd before the app existedBikeHub’s nav graph declares deep links in commonMain, shared across all three platforms:https://medium.com/media/50204cd0bc37c3110ac1e09cd17e2b70/hrefAndroid resolves those through an intent filter, iOS through its own handler. The web can’t, and the reason is instructive: a deep link on the web is a request to a web server, and the server has never heard of your routes. The static host serves the app at one path. Ask it for /part-items/42 and you get a 404 — before any Kotlin runs, before the bundle is even requested.This became concrete in the Strava authorization flow, where the backend has to send the user back to the app after consent. The native path is a bikehub:// link, which is simply inert in a browser tab. So the backend branches on the client header and sends web sessions a 302 instead. The first version of that redirect pointed at /strava/auth/success. It 404'd, exactly as above.The fix is to put the route where no server is involved at all — the URL fragment, which is where bindToBrowserNavigation() keeps the back stack:https://medium.com/media/8b0430e08fcd1bec2ba0363854ccee8b/hrefThe browser asks the host for /, gets the app, and the app resolves the rest itself. No hosting routing, no server-side rewrite rule, no 404.The price is that URL. The text after the # is the kotlinx.serialization serialName of a Kotlin class, which means renaming that class or moving its package silently breaks the Strava return in every environment until a backend environment variable is updated to match. A contract test pins the name, the argument names and their defaults, so the break happens in CI instead of in production.CORS: where a platform difference reached the backendThis is the part I’d least have predicted. A browser constraint changed the backend.Strava’s photo CDN answers a cross-origin request with no Access-Control-Allow-Origin header.Android and iOS don’t enforce CORS, so they load those URLs directly and always have. The web build fetches image bytes through the browser’s fetch(), the browser drops the response, and the user gets a placeholder. A redirect doesn’t help: the browser follows it and the final response is still the CORS-less one. The only fix that works is reading the bytes server-side and re-serving them from an origin the browser trusts.So the backend grew an image proxy — but only for the client that needs it. Every request carries X-BikeHubApp-client (android | ios | web), and native clients keep getting the original URLs, which is what keeps native traffic off the proxy entirely.The takeaway isn’t about proxies. It’s that the browser’s security model is part of your application architecture.The database isn’t a databaseSQLDelight was the easiest layer to migrate (Part 1) — right up until the web.On Android and iOS, the generated schema runs against a real SQLite driver. In the browser it runs against sql.js in a worker, and the worker doesn’t execute the generated schema at all. It builds the database from sqljs.schema.sql, a hand-maintained mirror of the .sq files that has to be updated by hand alongside them.That’s the most dangerous thing in the wasm build, and it’s not a bug — it’s a maintenance obligation with no compiler behind it. Add a table, forget the mirror, and Android and iOS are fine while the web is broken in a way no test catches.Getting sql.js into the bundle also needed webpack, which is the first time in this migration that the build stopped being Gradlehttps://medium.com/media/99c9f110a58cb9ffed05d935c885a1b9/hrefA Kotlin Multiplatform project with a yarn.lock checked in is a thing you get used to.The small stuff that only breaks on the webOne representative example of the long tail. The wasm media picker hands out blob: object URLs, and Coil’s URI parser throws StringIndexOutOfBoundsException on the blob:https://host/id form that a deployed origin produces. Locally, on localhost, it was fine. The fix is a mapper that intercepts blob URLs before Coil’s built-in string mapper sees them, plus a fetcher, because Coil only fetches http/https over the network — and without it, the optimistic local thumbnail shown while a photo uploads renders as a placeholder.A bug that appears only once you deploy, in the code path that makes uploads feel fast. That’s the web in one story.The series, in four sentencesPart 1 — the migration cost was largely determined before the migration started, by library choices made for other reasons and a module structure that happened to be ready.Part 2 — portable architecture moves almost mechanically; coupling to platform APIs doesn’t, and that’s your own code, not your dependencies.Part 3 — sharing application code turned out to be easier than sharing the ecosystem around it.Part 4 — getting a multiplatform app to build is different from making it production-ready.Which adds up to something I didn’t expect when I started:KMP didn’t make BikeHub multiplatform. It showed me which parts of it already were. The domain models, the pure functions, the polyline decoder — those were portable the whole time. Everything that hurt was a place where the app had quietly agreed with Android about how something works: what a date is, where a file comes from, who owns the back button, what happens when you ask for a URL.Migrating a Real Android App to Kotlin Multiplatform 4/4 — Shipping WebAssembly to Productio was originally published in ProAndroidDev on Medium, where people are continuing the conversation by highlighting and responding to this story.

Kaynak: Daniel Voko

#Android#Kotlin
Source: Daniel Voko
Share

From other categories