Handbook: Moving from Android to KMP

Gabriel Bronzatti Moro
CodandoTV
Published in
3 min readApr 13, 2024

An essential handbook to aid in migrating your Android app to embrace Compose Multiplatform.

I’ve been observing a growing trend towards embracing Compose Multiplatform lately. The versatility of Kotlin combined with Compose Multiplatform is extending its reach to platforms like iOS, desktop, and beyond 🚀. Excitedly, I’m sharing my initial experience migrating an existing Android app (MovieDB-App) to KMP. My aim is for this article to serve as a valuable resource for you on your own journey!

Reducing reliance on the Android platform is the secret 🤯

The initial phase involves minimizing the project’s dependence on the Android platform. During this stage, it’s essential to assess the libraries that are specifically linked to Android (some may even include Java code). In our MovieDB-App project, we encounter the following scenario:

Bye Hilt ✌️… Hi Koin 👋

Regrettably, Hilt isn’t compatible in the KMP (Kotlin Multiplatform) ecosystem. However, excellent alternatives such as Koin or Kodein exist. I opted for Koin due to my familiarity with it. Migrating dependency injection is a complex process, involving updates to numerous files.

💡Tip: Take your time and approach it methodically for a smoother transition

Bye Paging library✌️…

The paging library proved to be the most challenging to remove. Its integration spans multiple project layers, from the data layer all the way up to the UI. I replaced the pagination mechanism with a custom implementation.

💡Tip: Begin by removing the paging library and aim to load only the initial page. Once that’s accomplished, you can then shift your attention to implementing the paging mechanism

️ Update: Paging library provided by Google for KMP projects -> here

Bye Retrofit✌️… Hi Ktor 👋

Retrofit, a dependency confined to the data layer, greatly facilitates our migration process. An excellent alternative to Retrofit is Ktor. Ktor boasts user-friendly features and seamless compatibility with OkHttp, allowing you to leverage existing interceptors within your project.

💡Tip: If Retrofit is currently confined to your data layer as a data source, transitioning to Ktor is straightforward. Simply create a new data source utilizing Ktor, enabling you to migrate repository calls gradually until Retrofit is entirely replaced.

Bye ViewModels, and Navigation✌️… Hi Voyager 👋

ViewModels are intricately connected to the Android framework and operate within their own lifecycle. This characteristic makes them particularly suited for native Android projects, as they serve as reliable data holders with a lifespan that exceeds that of Composables, Screens, and Fragments.

We have an excellent replacement for ViewModels known as Voyager. No, I’m not referring to the spaceship commanded by Captain Janeway (only Star Trek fans would catch that reference). Instead, I’m talking about an incredible library that offers ScreenModels (the Voyager equivalent of ViewModels) and Navigation capabilities.

️ Update: Lifecycle library (ViewModel included) provided by Google for KMP -> here

️ Update: Navigation Library provided by Jetbrains for KMP -> here

Bye Android resources ✌️… Hi Moko-resources 👋

When it comes to resources like strings, icons, images, and files, how can we effectively utilize them in KMP?

The answer to that question is a bit variable 😅. If you’re working with a single-module project, JetBrains provides a resource system designed for KMP projects, akin to the native Android setup. However, for multi-module projects, I strongly suggest giving the Moko-Resources library a try.

️ Update: Resources provided by Jetbrains for KMP -> here

Conclusion

I’m convinced that KMP offers solutions to numerous challenges we face. Compose Multiplatform, in particular, stands out as a robust technology that continues to evolve and improve with each iteration. If you intend to try, I recommend you three things:

  • Stay current with Compose Multiplatform releases; it’s important to consistently update your project to leverage the latest versions;
  • Given the reliance on third-party libraries for project functionality, contributing to open-source libraries and adopting the latest versions is essential;
  • Enjoy 🚀

--

--