<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mario Manzano Culebras on Medium]]></title>
        <description><![CDATA[Stories by Mario Manzano Culebras on Medium]]></description>
        <link>https://medium.com/@manzanus?source=rss-4f17f51baf26------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*KaIdiHiHAC-nH496</url>
            <title>Stories by Mario Manzano Culebras on Medium</title>
            <link>https://medium.com/@manzanus?source=rss-4f17f51baf26------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 15:18:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@manzanus/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Quick Guide to migrate Android Application to run on Android, desktop and IOs using Kotlin…]]></title>
            <link>https://medium.com/@manzanus/quick-guide-to-migrate-android-application-to-run-on-android-desktop-and-ios-using-kotlin-928a783c3491?source=rss-4f17f51baf26------2</link>
            <guid isPermaLink="false">https://medium.com/p/928a783c3491</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[multiplatform]]></category>
            <category><![CDATA[kotlin-multiplatform]]></category>
            <dc:creator><![CDATA[Mario Manzano Culebras]]></dc:creator>
            <pubDate>Thu, 29 Feb 2024 02:59:35 GMT</pubDate>
            <atom:updated>2024-02-29T02:59:35.108Z</atom:updated>
            <content:encoded><![CDATA[<h3>Quick Guide to migrate Android Application to run on Android, desktop and IOs using Kotlin Multiplatform</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TpS_hYrnWbl0pt1H1NhaVA.png" /><figcaption>The love starts with sharing</figcaption></figure><p>Kotlin Multiplatform allows you to create applications for various platforms and efficiently reuse code across them while retaining the benefits of native programming. Your multiplatform applications will work on different operating systems, such as iOS, Android, macOS, Windows, Linux, and others.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RhKsLoJAsMpkL4DsAUijKg.png" /></figure><p>Kotlin Multiplatform was announced at <a href="https://blog.jetbrains.com/kotlin/2017/11/kotlinconf-keynote-recap/">KotlinConf 2017</a> under the name “Kotlin Multiplatform Projects”, and it originally supported the JVM, Native, and JS targets. It quickly started gaining a number of early adopters. Until 2020, the technology was in the experimental stage, and the team was shaping it based on feedback from these early adopters. Nowadays, KMM is stable and production ready.</p><h3>KMP and Jetpack Compose lovers forever</h3><p>Most of the Android developers I’ve met love the the fact that the projects they write in Kotlin and targeting Android platform can be running in destkop and even in IOs system. But they would think; “That would be great, but even if I can shared the code from services and data to viewmodels, then I have to create the UI code using the native components of each platforms”. They are wrong, Jetpack Compose comes here to save us.</p><h3>My Experience</h3><p>As an experienced Android developer and a Jetpack Compose passionate I have to say that this idea attracted me from the begining, so I started the migration without hesitation and soon, I discovered that it is not an easy task if you are migrating, instead of creating from scratch. But no worries, I am here to help you, sharing my experience to help you.</p><h3>Some things to know before</h3><p>There are some considerations to have in mind before including the KMP plugin and create the shared module in our project. These are important and popular android libraries tomigrate to its KMP compatible partner:</p><ul><li><a href="https://square.github.io/retrofit/">Retrofit</a> → <a href="https://ktor.io/">Ktor</a>.</li><li><a href="https://developer.android.com/jetpack/androidx/releases/room">Room</a> → <a href="https://cashapp.github.io/sqldelight/2.0.1/">SQLDelight</a> (Be carefull here to follow the link <a href="https://cashapp.github.io/sqldelight/2.0.1/">https://cashapp.github.io/sqldelight/2.0.1/</a> and not this one <a href="https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-ktor-sqldelight.html">https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-ktor-sqldelight.html</a>, because sqldelight has reached 2.0.1 version and some things have changed.</li><li><a href="https://dagger.dev/hilt/">Hilt</a>/<a href="https://dagger.dev/">Dagger</a> → <a href="https://insert-koin.io/docs/quickstart/kmp">Koin</a>.</li><li><a href="https://github.com/bumptech/glide">Glide</a> → I recommend to use <a href="https://github.com/Kamel-Media/Kamel">Kamel</a>.</li></ul><p>And 2 quick tips that will make you KMP life easier:</p><ul><li>Remove all Calendar uses and use “org.jetbrains.kotlinx:kotlinx-datetime”.</li><li>For Compose navigation use <a href="https://github.com/adrielcafe/voyager">Voyager</a>.</li><li>Sadly, <a href="https://github.com/google/accompanist">Google Accompanist</a> library is not ready to use in KMP projects yet, so review your Compose code for components like SwipeRefresh that are not going to be allowed on the common logic using Compose. Of course you can always create expect/actual composables and create that behaviour using the native destkop/IOs code.</li></ul><h3>How to migrate your existing Android app</h3><p>I strongly recommend to follow the <a href="https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-integrate-in-existing-app.html">official documentation of JetBrains</a>.</p><p>I am not going to be more accurate than the official documentation steps, so, instead I decided to save you a lot of times with these considerations:</p><ul><li>For the shared/viewmodels coroutines management, create a class:</li></ul><p>On commonMain:</p><pre>import kotlinx.coroutines.CoroutineScope<br><br>expect abstract class ViewModel() {<br>    val viewModelScope: CoroutineScope<br>    protected open fun onCleared()<br>}</pre><p>On androidMain:</p><pre>import kotlinx.coroutines.CoroutineScope<br>import androidx.lifecycle.ViewModel as AndroidXViewModel<br>import androidx.lifecycle.viewModelScope as androidXViewModelScope<br><br>actual abstract class ViewModel actual constructor() : AndroidXViewModel() {<br>    actual val viewModelScope: CoroutineScope = androidXViewModelScope<br><br>    actual override fun onCleared() {<br>        super.onCleared()<br>    }<br>}</pre><p>On destkopMain and iosMain:</p><pre>import kotlinx.coroutines.MainScope<br>import kotlinx.coroutines.cancel<br><br>actual abstract class ViewModel {<br>    actual val viewModelScope = MainScope()<br><br>    protected actual open fun onCleared() {}<br><br>    fun clear() {<br>        onCleared()<br>        viewModelScope.cancel()<br>    }<br>}</pre><p>Sharing resources management. Here, if you search on the internet about this, you probably will find a popular library called <a href="https://github.com/icerockdev/moko-resources">moko-resources</a>, which I think is really good and you can set it up using this <a href="https://youtu.be/xtWzpLtCuY0?si=bYZ71FyWDYHWTqzE">amazing tutorial</a>. But, my experience after spending a lot of time configuring it on my project, I succesfully share strings between Android, desktop and IOs with its proper translations, but when I tried with images I succesfully accessed the resources on each platform but I wasn’t able to load an image using <em>org.jetbrains.compose.resources.painterResource</em> on the compose code on the share module, which was exactly what I want. I am sure this library will be a very good tool, but maybe in post-versions. So, my simplest approach with the resources is:</p><ul><li>For load images on format xml, png, jpg: Create a sealed class containing :</li></ul><pre>sealed class NasaIcon(val resourceId: String) {<br>    object Refresh : NasaIcon(&quot;ic_refresh.xml&quot;)<br>    object ArrowBack : NasaIcon(&quot;ic_arrow_ios_back.xml&quot;)<br>    object Spacecraft : NasaIcon(&quot;ic_spacecraft.xml&quot;)<br><br>    object RocketVertical : NasaIcon(&quot;ic_rocket.xml&quot;)<br>(...)<br>}</pre><p>And in commonMain module on Compose code just use:</p><pre>org.jetbrains.compose.resources.painterResource(ArrowBack.resourceId)</pre><p>Placed all the images svg (.xml), png under shared/commonMain/resources.</p><ul><li>For strings requires a little bit more code for set up but then it will be easy and clean to call, create another sealed class just the same as the images to store the string ids.</li></ul><pre>sealed class Strings(val resourceId: String) {<br>    object GoToDetail : Strings(&quot;go_to_detail&quot;)<br>(...)<br>}</pre><p>Then, on androidApp/res/values/strings.xml (you can add translations, this system is going to resolved in the correct string translation):</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;resources&gt;<br>    &lt;string name=&quot;go_to_detail&quot;&gt;Go to Detail&lt;/string&gt;<br>&lt;/resources&gt;</pre><p>On xcode use the wizard to create a new Localizable.xcstrings:</p><pre>{<br>  &quot;sourceLanguage&quot; : &quot;en&quot;,<br>  &quot;strings&quot; : {<br>    &quot;go_to_detail&quot; : {<br>      &quot;extractionState&quot; : &quot;manual&quot;,<br>      &quot;localizations&quot; : {<br>        &quot;en&quot; : {<br>          &quot;stringUnit&quot; : {<br>            &quot;state&quot; : &quot;translated&quot;,<br>            &quot;value&quot; : &quot;Go to Detail&quot;<br>          }<br>        }<br>      }<br>    }<br>  },<br>  &quot;version&quot; : &quot;1.0&quot;<br>}</pre><p>For desktop, placed the strings.xml under shared/commonMain/resources.</p><p>On commonMain create expect class:</p><pre>import androidx.compose.runtime.Composable<br><br>expect class Strings {<br>    fun get(id: String, quantity: Int): String<br>    fun get(id: String): String<br>    fun format(id: String, vararg formatArgs: Any): String<br>}<br><br>@Composable<br>expect fun stringResource(id: String): String<br><br>@Composable<br>expect fun stringResource(id: String, vararg args: Any): String</pre><p>On androidMain:</p><pre>import android.content.Context<br>import androidx.compose.runtime.Composable<br>import androidx.compose.ui.platform.LocalContext<br><br>actual class Strings(<br>    private val context: Context<br>) {<br>    actual fun get(id: String): String {<br>        val resourceId = context.resources.getIdentifier(id, &quot;string&quot;, context.packageName)<br>        if (resourceId == 0) return id<br>        return context.getString(resourceId)<br>    }<br><br>    actual fun get(id: String, quantity: Int): String {<br>        val resourceId = context.resources.getIdentifier(id, &quot;plurals&quot;, context.packageName)<br>        if (resourceId == 0) return id<br>        return context.resources.getQuantityString(resourceId, quantity, quantity)<br>    }<br><br>    actual fun format(id: String, vararg formatArgs: Any): String {<br>        val resourceId = context.resources.getIdentifier(id, &quot;string&quot;, context.packageName)<br>        if (resourceId == 0) return id<br>        return context.resources.getString(resourceId, *formatArgs)<br>    }<br>}<br><br>@Composable<br>actual fun stringResource(id: String): String {<br>    return Strings(LocalContext.current).get(id)<br>}<br><br>@Composable<br>actual fun stringResource(id: String, vararg args: Any): String {<br>    return Strings(LocalContext.current).format(id, args)<br>}</pre><p>On iosMain:</p><pre>import androidx.compose.runtime.Composable<br>import platform.Foundation.NSBundle<br>import platform.Foundation.NSString<br>import platform.Foundation.NSURL<br>import platform.Foundation.stringWithFormat<br><br>actual class Strings {<br>    actual fun get(id: String): String = id.localized()<br>    actual fun get(id: String, quantity: Int) = id.localized(quantity)<br>    actual fun format(id: String, vararg formatArgs: Any) = id.localized(*formatArgs)<br>}<br><br>fun String.localized(): String {<br>    val localizedString = NSBundle.mainBundle.localizedStringForKey(this, this, null)<br>    if (localizedString != this) return localizedString<br><br>    val baseResourcePath = NSBundle.mainBundle.pathForResource(&quot;Base&quot;, &quot;lproj&quot;)<br>        ?.let { NSURL(fileURLWithPath = it) }<br>    val baseBundle = baseResourcePath?.let { NSBundle(it) }<br><br>    if (baseBundle != null) {<br>        val baseString = baseBundle.localizedStringForKey(this, this, null)<br>        if (baseString != this) return baseString<br>    }<br><br>    return this<br>}<br><br>fun String.localized(vararg arguments: Any?): String {<br>    val format = localized()<br>    // Shorten the variable name<br>    val a = arguments<br>    // Kotlin does not support passing variadic parameters to Objective-C<br>    // We implement calling the method with up to 9 arguments which is enough in practice<br>    return when (arguments.size) {<br>        0 -&gt; NSString.stringWithFormat(format)<br>        1 -&gt; NSString.stringWithFormat(format, a[0])<br>        2 -&gt; NSString.stringWithFormat(format, a[0], a[1])<br>        3 -&gt; NSString.stringWithFormat(format, a[0], a[1], a[2])<br>        4 -&gt; NSString.stringWithFormat(format, a[0], a[1], a[2], a[3])<br>        5 -&gt; NSString.stringWithFormat(format, a[0], a[1], a[2], a[3], a[4])<br>        6 -&gt; NSString.stringWithFormat(format, a[0], a[1], a[2], a[3], a[4], a[5])<br>        7 -&gt; NSString.stringWithFormat(format, a[0], a[1], a[2], a[3], a[4], a[5], a[6])<br>        8 -&gt; NSString.stringWithFormat(format, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7])<br>        9 -&gt; NSString.stringWithFormat(format, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8])<br>        else -&gt; error(&quot;Too many arguments.&quot;)<br>    }<br>}<br><br>@Composable<br>actual fun stringResource(id: String): String {<br>    return Strings().get(id)<br>}<br><br>@Composable<br>actual fun stringResource(id: String, vararg args: Any): String {<br>    return Strings().format(id, args)<br>}</pre><p>For desktop implementation, I didn´t find a clean solution to access the strings, so, until I use moko-resources library for this, I just parse the file manually:</p><pre>@Composable<br>actual fun stringResource(id: String): String {<br>    val stringFile = useResource(&quot;strings.xml&quot;) {<br>        it.bufferedReader().readText()<br>    }<br>    val startIdIndex = stringFile.indexOf(id)<br>    val valueStartIndex = startIdIndex + id.length + 2<br>    val valueLine = stringFile.substring(valueStartIndex)<br>    val valueEndIndex = valueLine.indexOf(&#39;&lt;&#39;)<br>    return valueLine.substring(0, valueEndIndex)<br>}</pre><p>And then you just access them on Jetpack Compose code like this:</p><pre>Text(text = stringResource(Strings.GoToDetail))</pre><ul><li>And finally, for Koin injection and Ktor Http client:</li></ul><p>On commonMain/Platform.kt:</p><pre>import io.ktor.client.HttpClient<br>import io.ktor.client.HttpClientConfig<br>import org.koin.core.module.Module<br><br>interface Platform {<br>    val name: String<br>}<br><br>expect val platformModule: Module<br><br>expect fun getPlatform(): Platform<br><br>expect fun httpClient(config: HttpClientConfig&lt;*&gt;.() -&gt; Unit = {}): HttpClient</pre><p>On androidMain/Platform.android.kt</p><pre>import app.cash.sqldelight.db.SqlDriver<br>import app.cash.sqldelight.driver.android.AndroidSqliteDriver<br>import com.mariomanzano.kmm_nasa_explorer.shared.cache.NasaDatabase<br>import io.ktor.client.HttpClient<br>import io.ktor.client.HttpClientConfig<br>import io.ktor.client.engine.okhttp.OkHttp<br>import io.ktor.client.plugins.HttpTimeout<br>import io.ktor.client.plugins.contentnegotiation.ContentNegotiation<br>import io.ktor.client.plugins.defaultRequest<br>import io.ktor.client.request.header<br>import io.ktor.serialization.kotlinx.json.json<br>import kotlinx.serialization.ExperimentalSerializationApi<br>import kotlinx.serialization.json.Json<br>import org.koin.core.module.Module<br>import org.koin.dsl.module<br><br>class AndroidPlatform : Platform {<br>    override val name: String = &quot;Android ${android.os.Build.VERSION.SDK_INT}&quot;<br>}<br><br>actual val platformModule: Module = module {<br>    single&lt;SqlDriver&gt; {<br>        AndroidSqliteDriver(<br>            schema = NasaDatabase.Schema,<br>            context = get(),<br>            name = &quot;NasaDatabase.db&quot;<br>        )<br>    }<br>}<br><br>actual fun getPlatform(): Platform = AndroidPlatform()<br><br>@OptIn(ExperimentalSerializationApi::class)<br>actual fun httpClient(config: HttpClientConfig&lt;*&gt;.() -&gt; Unit) = HttpClient(OkHttp) {<br>    install(HttpTimeout) {<br>        socketTimeoutMillis = 60_000<br>        requestTimeoutMillis = 60_000<br>    }<br>    defaultRequest {<br>        header(&quot;Content-Type&quot;, &quot;application/json&quot;)<br>        url(https://medium.com/&quot;https://api.nasa.gov/&quot;)<br>    }<br>    install(ContentNegotiation) {<br>        json(Json {<br>            prettyPrint = true<br>            isLenient = true<br>            ignoreUnknownKeys = true<br>            explicitNulls = false<br>        })<br>    }<br>}</pre><p>On iosMain/Platform.ios.kt</p><pre>import app.cash.sqldelight.db.SqlDriver<br>import app.cash.sqldelight.driver.native.NativeSqliteDriver<br>import com.mariomanzano.kmm_nasa_explorer.shared.cache.NasaDatabase<br>import io.ktor.client.HttpClient<br>import io.ktor.client.HttpClientConfig<br>import io.ktor.client.engine.darwin.Darwin<br>import io.ktor.client.plugins.HttpTimeout<br>import io.ktor.client.plugins.contentnegotiation.ContentNegotiation<br>import io.ktor.client.plugins.defaultRequest<br>import io.ktor.client.request.header<br>import io.ktor.serialization.kotlinx.json.json<br>import kotlinx.serialization.ExperimentalSerializationApi<br>import kotlinx.serialization.json.Json<br>import org.koin.core.module.Module<br>import org.koin.dsl.module<br>import platform.UIKit.UIDevice<br><br>class IOSPlatform : Platform {<br>    override val name: String =<br>        UIDevice.currentDevice.systemName() + &quot; &quot; + UIDevice.currentDevice.systemVersion<br>}<br><br>actual val platformModule: Module = module {<br>    single&lt;SqlDriver&gt; {<br>        NativeSqliteDriver(NasaDatabase.Schema, &quot;NasaDatabase.db&quot;)<br>    }<br>}<br><br>actual fun getPlatform(): Platform = IOSPlatform()<br><br>@OptIn(ExperimentalSerializationApi::class)<br>actual fun httpClient(config: HttpClientConfig&lt;*&gt;.() -&gt; Unit) = HttpClient(Darwin) {<br>    install(HttpTimeout) {<br>        socketTimeoutMillis = 60_000<br>        requestTimeoutMillis = 60_000<br>    }<br>    defaultRequest {<br>        header(&quot;Content-Type&quot;, &quot;application/json&quot;)<br>        url(https://medium.com/&quot;https://api.nasa.gov/&quot;)<br>    }<br>    install(ContentNegotiation) {<br>        json(Json {<br>            prettyPrint = true<br>            isLenient = true<br>            ignoreUnknownKeys = true<br>            explicitNulls = false<br>        })<br>    }<br>}</pre><p>On desktopMain/Platform.desktop.kt</p><pre>import app.cash.sqldelight.db.SqlDriver<br>import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver<br>import com.mariomanzano.kmm_nasa_explorer.shared.cache.NasaDatabase<br>import io.ktor.client.HttpClient<br>import io.ktor.client.HttpClientConfig<br>import io.ktor.client.engine.cio.CIO<br>import io.ktor.client.plugins.HttpTimeout<br>import io.ktor.client.plugins.contentnegotiation.ContentNegotiation<br>import io.ktor.client.plugins.defaultRequest<br>import io.ktor.client.request.header<br>import io.ktor.serialization.kotlinx.json.json<br>import kotlinx.serialization.ExperimentalSerializationApi<br>import kotlinx.serialization.json.Json<br>import org.koin.core.module.Module<br>import org.koin.dsl.module<br>import java.io.File<br><br>class DesktopPlatform : Platform {<br>    override val name: String = &quot;Desktop&quot;<br>}<br><br>actual val platformModule: Module = module {<br>    single&lt;SqlDriver&gt; {<br>        val driver = JdbcSqliteDriver(&quot;jdbc:sqlite:database/NasaDatabase.db&quot;)<br>        if (!File(&quot;database/NasaDatabase.db&quot;).exists()) {<br>            NasaDatabase.Schema.create(driver)<br>            driver<br>        } else {<br>            driver<br>        }<br>    }<br>}<br><br><br>actual fun getPlatform(): Platform = DesktopPlatform()<br><br>@OptIn(ExperimentalSerializationApi::class)<br>actual fun httpClient(config: HttpClientConfig&lt;*&gt;.() -&gt; Unit) = HttpClient(CIO) {<br>    install(HttpTimeout) {<br>        socketTimeoutMillis = 60_000<br>        requestTimeoutMillis = 60_000<br>    }<br>    defaultRequest {<br>        header(&quot;Content-Type&quot;, &quot;application/json&quot;)<br>        url(https://medium.com/&quot;https://api.nasa.gov/&quot;)<br>    }<br>    install(ContentNegotiation) {<br>        json(Json {<br>            prettyPrint = true<br>            isLenient = true<br>            ignoreUnknownKeys = true<br>            explicitNulls = false<br>        })<br>    }<br>}</pre><h3>Example</h3><p>I leave you <a href="https://github.com/MarioManzanoCulebras/KMM-Nasa-Explorer">here</a> an example containig all the code in this article and where I show all the dependencies you need to add.</p><h3>Conclusion</h3><p>KMP is a really amazing feature that could potencially become the Mobile standart development, and I strongly invite everyone to test it, specially Android developers, who has this path easier than other because they are use to working with Kotlin and Jetpack Compose. However, I need to say here that this KMP still has a long way to reach that goal, specially the cool libraries Android developers are use to using like Retrofit, Room, Accompanist, Hilt that in the time writing this article they are not compatible yet with KMP.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=928a783c3491" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>