I want to build a shared multiplatform library for...
# multiplatform
d
I want to build a shared multiplatform library for iOS and Android. The library would provide shared business logic, the networking layer, etc. I would also like to use it for shared resources like static strings, images, and colors. I thought that perhaps Moko Resources would be the answer, but it appears it only works in the context of an app. I can't figure out how to use it to create a library that encapsulates it. I made an initial attempt at a library, but the resources aren't copied into the XCFramework created by the Moko gradle task. According to the docs, when Moko Resources is used directly in an app, one must add some build phases to the Xcode project, which obviously doesn't exist in a KMP Library project. Could anyone give me some guidance or references on projects that have successfully built a shared resources multiplatform library? This doesn't necessarily have to be based on Moko Resources, although initially, it seemed the most straightforward approach.
a
Check the official implementation of Multiplatform resouces. Also this wizard my be helpful.
👍 1
d
I did look at that, though it appears to be specific to Compose multiplatform. I'll look closer look though. Thanks for the link to the wizard. I'll play with that. The wizard provided by JetBrains is pretty specific to multiplatform apps, not libraries.
m
As long as you don't mind having Compose as a dependency (even if you're not using it for UI) it shouldn't be a problem. I'm using Multiplatform Resources to load graphics shader from common resources and it works fine even outside of composable functions (though some cases do require being called from a composable function, so YMMV). If you start from the wizard to make a multiplatform app, it's pretty straightforward to change it to a library. The exact steps depend on what targets you have enabled, so it's hard to give specific instructions, but very roughly here's what I do: 1. Duplicate the
composeApp
folder and rename the copy to something based on the library name. 2. Make a top-level
sample
folder and move the original
composeApp
and any other target folders (
iosApp
etc) inside it. 3. Fix up the various Gradle files to match the reorganized structure. 4. Change
rootProject.name
if necessary to avoid conflicts 5. Replace references to
libs.plugins.androidApplication
with references to
libs.plugins.androidLibrary
6. Delete anything app-specific from the Gradle files like
applicationId
,
targetSdk
,
versionCode
and
versionName
in
android {}
(you'll get a build error if you don't anyway) 7. Add your library as a dependency to the
sample
module. 8. If you want to publish the library somewhere (I use GitHub's private gradle registry) add the
maven-publish
plugin to Gradle and configure it appropriately. It takes a bit of messing around the first time to get it to work right, but once you've done a few, it becomes second nature to adjust a wizard-generated project for use as a library. The biggest pitfall I ran into was
maven-publish
... unbeknownst to me, Kotlin Multiplatform gradle plugin automatically detects
maven-publish
and does most of the configuration for you. So all you need to do is configure WHERE the library is being published and any meta data (version number, etc.). Don't try to configure what is published or you'll end up with duplicate things because Kotlin Multiplatform is already doing that work automatically (it stupidly took me days to figure this out, and the solution was just to delete most of the stuff I added in Gradle, and then it just worked...) Keep in mind that there's a lot of new stuff coming down the pipeline with regard to library packaging formats for Kotlin in the future (there was a whole session on it in the last KotlinConf — if you're making libraries, it's very much worth a watch on YouTube!)