Kotlin Multiplatform: Platform Features as a Nullable Interface
By Nesim Razon, independent mobile developer · 2026-11-26
Sooner or later a shared Kotlin screen needs something that only one platform has yet: a purchase, a photo picker, a cloud save. The usual answer is expect/actual. I found a smaller one in my own apps: give shared code an interface, and let the property be null when the platform cannot do it. Here is how that looks in real code, and where it stops working.
The problem: a feature that exists on one side only
My Astrolings app is a Kotlin Multiplatform project with an Android app and an iOS app sharing one Compose interface. When I ported it to iOS, the Android version already sold a remove-ads purchase. The iOS version, in its first release, did not have that product defined in the store yet.
The shared screen still had to draw a profile page, and that page has a remove-ads button. So the real question was: how does shared code learn that this button should not exist on this platform? I did not want an if on a platform name scattered over screens, and I did not want a fake purchase that quietly does nothing.
The answer in the code: a nullable property
The shared module declares a small interface called RemoveAds. It has two members: a price, which is a nullable string, and a function buy that returns true if the purchase screen opened. The Platform interface, which every shared screen receives, then has one property: removeAds, whose type is RemoveAds with a question mark.
On Android, that property returns an object that forwards to the billing code. On iOS, the class IosPlatform sets it to null, with a comment saying the store product was not defined in the first release. That is the whole platform difference. No expect, no actual, no flag.
The shared screen reads it in one place. The call site passes a parameter called canRemoveAds, which is simply removeAds not equal to null. When that is false, the profile page shows a calm sentence saying the app is free and shows one small ad at the bottom, instead of a button. I have simplified the description here, but the names come straight from the project.
Why not expect/actual
I do use expect/actual in these projects, but for small pure functions. Snowlings, for instance, declares one to decode image bytes into a bitmap and one to turn a bitmap into a PNG. Both platforms can always do that, so a missing implementation is simply a compile error, which is exactly what you want.
A purchase is different, for three reasons.
- An actual must exist for every target. If iOS cannot buy yet, the actual has to lie: return a fake price, or do nothing when called. Nothing in the type says so.
- It needs state and a screen. A real purchase holds a billing connection and an Activity. Those are objects with a life cycle, and a top-level function declaration is a poor home for them.
- The shared code cannot ask a function whether it is real. With a nullable interface, one comparison with null answers it, and the compiler forces every call site to handle the answer.
Null, null object, or a default body: a rule I use
I use three different ways to say a platform cannot do something, and they are not interchangeable. The choice depends on one question: what should the user see when the feature is missing?
If the user should see nothing different, except that the thing silently does not happen, I use a default body or a null object. In Snowlings, the Platform interface gives cloud sync an empty default body, and the photo picker a default that reports no photo. A platform that does not override them simply does nothing. In Slinglings, a small object named NoServices stands in for ads and purchases: it has no ads, and a rewarded request gives the reward at once. Because the callback always fires, the game never waits.
If the user should see a different screen, I use null. A hidden button, a different paragraph, a menu entry that is not there. The null is not an error here. It is information that the shared screen needs in order to draw correctly.
- Callback that must always fire: null object, as in NoServices.
- Optional action with nothing to show: default body, as in the photo picker.
- Feature that changes what is drawn: nullable interface, as in removeAds.
Slinglings shows the in-between case. Its GameServices interface has a flag called billingAvailable and a nullable price. That works, but the flag and the buy function can disagree. With the nullable object in Astrolings, you cannot call buy on something that does not exist.
Three states, not two
A nullable capability has a trap: null can mean too many things. I keep the meanings separate by putting each one in a different place.
Absent on this platform is the null on the property. Present, but not allowed right now is a separate question on the object, like the permission check in the notifications interface. Present, but not available at the moment is a nullable value inside it: in RemoveAds, a null price means the product cannot be bought at this moment, for example because the store has not answered yet. The screen reacts to each differently. The first hides the button, the second asks for permission, the third shows the button without a price.
If you collapse them into one null, you end up debugging a missing button that only appears sometimes.
Testing gets easier
The shared code only sees an interface, so a test can hand it any platform it likes. In Astrolings the test folder has a fake Platform, in a file named IosShots, in which the members do nothing and removeAds is null. That single fake lets the screens run as a plain unit test, without a phone and without a purchase library.
The same idea gives the opposite test for free. Pass an object with a price and the button appears. Pass one that returns false from buy and you can check the failure message. In both cases, the only thing under test is the shared screen's reaction.
Compare that with an actual declaration: you cannot easily swap it in a test, because it is chosen at compile time.
The iOS side: null at the boundary
The same idea shows up one level lower. In Snowlings and Astrolings, the iOS entry point is a Kotlin function named MainViewController, and its parameter is the Swift bridge object, with a question mark in the type. In the Snowlings version of IosPlatform, the cloud save does nothing if that object is missing, and the photo picker answers with no photo.
A missing bridge therefore means a quieter app, not a crash. That is useful during a port, when some Swift pieces are not written yet: the Kotlin side can still run, and you can see what is still missing.
A practical note from my iOS port checklist: the shared Platform interface lists the nullable capability explicitly, so a new port starts by asking, for every member, whether this platform has it yet.
I should be precise about what I did and did not do. In these apps the Platform object itself is never null, and the Swift bridge is the only null-typed object at the entry point. The nullable capability property exists in Astrolings. The other capabilities use default bodies or the null object.
Where it hurts
Nullable capabilities are not free.
- Every call site must handle null. That is the point, but a feature used in ten places gets ten question marks. If that hurts, wrap the check in one small function and call that.
- Kotlin exports an interface to Swift as a protocol, and nullability crosses over as optional. It works, but I only rely on it in the entry point, where the type is simple.
- Do not use it when the platforms are always equal. A function that every target can implement belongs in expect/actual or in a plain interface.
- Do not use it for something that is mostly present. If it is null on one platform in a hundred, the question marks become noise.
My rule of thumb: when the shared screen has to look different, the capability is nullable. When it only has to keep working, give it a default.
Frequently asked questions
When is a nullable interface better than expect/actual?
When a platform may not have the feature at all and the shared screen must look different without it. Expect/actual forces every target to provide something, even a fake.
Is a nullable interface the same as the null object pattern?
No. A null object pretends the feature exists and does nothing. A nullable interface says plainly that it does not exist, so shared code can hide the button or change the text.
How do you test shared code that depends on platform features?
Write a small fake Platform in the test folder. Return null for the capability to test the missing case, or an object with a price to test the present case.
Does a nullable interface work across the Swift bridge?
In my apps the iOS entry function takes the Swift bridge object as nullable, and the Kotlin side does nothing when it is missing. I did not export the nullable capability property itself to Swift.
What should null never mean?
Never use one null for several situations. Keep absent on this platform, not allowed now and not available right now in three separate places.
More from the blog
- Moving From GitHub Pages to Your Own Domain Without Losing SEO
- Kotlin/Native and Swift: The 'Different Argument Labels' Error
- Measuring Play Installs: UTM Links and Listing Experiments
- Secret Santa Draw Algorithm: No Self-Picks, No Swaps
- No macOS Minutes Left on GitHub Actions: Moving to Codemagic
- Ads in Kotlin Multiplatform: Shared Interface, Platform Shell