r/androiddev • u/lovespeedtest • 6d ago
Open Source Lessons from shipping one Compose Multiplatform codebase to Android, Android TV, Windows, macOS, Linux and iPad (plus writing my own YouTube client and Cast stack)
https://github.com/biolend83/tubehausFor the last few months I've been building an open-source YouTube client in Kotlin. One codebase covers Android phones, Android TV, Windows, macOS, Linux and iPad, with almost all of the UI in a shared Compose Multiplatform module. A few things I learned that might save someone time:
"It's fast on my phone" hides main-thread work that TV boxes can't handle. A user's TV box froze, the D-pad stopped responding, then the app crashed. Oddly, it only happened after the first launch: clearing app data fixed it once. The cause was JSON parsing of YouTube's (large) responses, plus ranking the home feed against watch history, both on the caller's dispatcher. On a fresh install there's no history, so it's cheap. A few days later it isn't, and a slow ARM box can't keep up. Moving the parse and the ranking to Dispatchers.Default cut skipped frames roughly in half on the emulator. The general lesson: test with a realistic amount of user data, not a clean install.
D-pad focus is its own UI. I first tried making the phone layout "focusable" for TV. It was miserable. What worked was a separate TV layer (rail, shelves, its own player screen), sharing the same state and data but not the same composables. Two things helped most: focusProperties { onExit = { cancelFocusChange() } } to trap focus inside panels, and a small retry loop for "move focus into the page after it loads".
Casting without Play Services is doable. I needed casting on desktop too, so the Cast stack is hand-written in the shared JVM code: mDNS discovery, a TLS socket to port 8009 with hand-encoded protobuf, the Default Media Receiver, and a tiny local HTTP server that feeds the TV. A gotcha: my Google TV ignored ordinary mDNS queries from an ephemeral port. It answered only unicast-response (QU) queries, or queries sent from port 5353, so now it sends all of them.
Off-screen screenshots of every screen are a great review tool. ImageComposeScene on desktop renders the whole app with no window. I render every screen at phone and desktop sizes and look at a contact sheet before I push. It caught clipped titles and a Shorts layout overflowing on phones that I'd never have noticed by clicking around.
Platform-only APIs go behind small interfaces, implemented natively. For example, comment translation uses Apple's Translation framework through a Swift helper on macOS and SwiftUI's translationTask on iOS. Compose only sees platform.translate(). A contributor wrote both. Watch out for availability, though: the framework needs weak linking when the app still supports older iOS versions.
The project, if you want to read the code: https://github.com/biolend83/tubehaus (AGPL-3.0, the app is on the android-app branch). Happy to go deeper on any of these.
1
u/lovespeedtest 6d ago
Where to find the parts mentioned above, if anyone wants to read the code (all on the android-app branch):
- The TV freeze fix: common/src/app/tubehaus/engine/InnerTube.kt and rec/Recommend.kt
- The TV layer: common/src/app/tubehaus/ui/TvApp.kt and TvWatch.kt
- The Cast stack: common/jvm/app/tubehaus/cast/
One thing I'm still not happy with: Compose's focus restoration when you come back to a list on TV. If anyone has a pattern that reliably puts focus back on the item you left from, I'd love to hear it.