Blog Infos
Author
Published
Topics
Published
Topics

In Android development, modularization of the application is a very common pattern that helps organize the code, increase the readability and make sharing between projects more straightforward. Some say it also decreases the build time, but this is not a part of this article. If you would like to learn more, check out this video from Google I/O’19.

Why make a separate module for resources?

Whenever your modules are features or layers based, the resources like localized texts or icons can be used anywhere in the app UI and reused in many modules. Now when compose is becoming a standard we can create UI-only modules with compose dependencies and app resources. The composables do not need to know about any business class.

In the KMM project, the additional benefit can be seen while importing the module. Having the resources independently, the iOS view class will import only this. Same on the Android side, the common module for a ui-only module will not be needed.

Multiplatform resources

For my KMM projects, I use moko-resources. The library from icerock devs provides access to resources on different platforms with the support of the default system localization.

Let’s implement it step by step:

  • create a separate module for resources:

 

  • build.gradle needs to include the icerock plugin dev.icerock.mobile.multiplatform-resources and an extension function multiplatformResources that will generate the resources classes. Also, we need to add api keyword to the library dependency (A gist with a full example)
plugins {    
kotlin("multiplatform")    
id("com.android.library")                      id("dev.icerock.mobile.multiplatform-resources")
}
kotlin {
android()
ios {
binaries {
framework {
baseName = "shared"
export(project(":sharedResources"))
export(Deps.Resources.core)
}
}
}
sourceSets {
commonMain {
dependencies {
api(project(":sharedResources"))
api(Deps.Resources.core)
}
}
}
}

The framework that will be generated for the iOS target needs sharedResources to be exported together with moko-resources dependency. The first one will contain generated classes while the other is allowing the iOS to use a specific library API. Together with the plugin installed and extension function in the common module, this project will build iOS, without any change from iOS devs required.

I believe Kotlin Multiplatform is the framework that will be widely used in the mobile world. The reason is, that the KMM is optional and every team can start testing it and implement only a part of it. The resources are a perfect example of how KMM can reduce the amount of duplicated code on mobile platforms.

Big thanks to Carlos Mota for the review 👍

This article was originally published on proandroiddev.com on May 14, 2022

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

,

Building Bridges with Kotlin Multiplatform Mobile (KMM): How we added KMM to our established App

Learn how we integrated Kotlin Multiplatform Mobile (KMM) into our Android and iOS codebase. We will share our experience, challenges, and best practices for adding KMM to existing apps.
Watch Video

Building Bridges with Kotlin Multiplatform Mobile (KMM): How we added KMM to our established App

Benedict Pregler & Kristijan Vidovic
Senior Mobile Engineer & Mobile Engineer
GetYourGuide

Building Bridges with Kotlin Multiplatform Mobile (KMM): How we added KMM to our established App

Benedict Pregler & ...
Senior Mobile Engine ...
GetYourGuide

Building Bridges with Kotlin Multiplatform Mobile (KMM): How we added KMM to our established App

Benedict Pregler ...
Senior Mobile Engineer & ...
GetYourGuide

Jobs

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
First of all, let’s explain why I need the CookiesStorage. To identify the user,…
READ MORE
blog
I thought Kotlin Multiplatform Mobile was complicated to use before I decided to try…
READ MORE
blog
Hey everyone, I want to share more of Kotlin Multiplatform Mobile Content since it…
READ MORE
blog
In my previous story, I’ve talked about why I believe we can strongly improve…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu