### Gradle Setup: Root Project Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Add the moko-resources-generator to your root build.gradle file. ```groovy buildscript { repositories { gradlePluginPortal() } dependencies { classpath "dev.icerock.moko:resources-generator:0.26.4" } } allprojects { repositories { mavenCentral() } } ``` -------------------------------- ### Gradle Setup: Project Configuration Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Apply the moko-resources plugin and declare dependencies in your project's build.gradle. Configure resource package, class name, visibility, and iOS settings. ```groovy apply plugin: "dev.icerock.mobile.multiplatform-resources" dependencies { commonMainApi("dev.icerock.moko:resources:0.26.4") commonMainApi("dev.icerock.moko:resources-compose:0.26.4") // for compose multiplatform commonTestImplementation("dev.icerock.moko:resources-test:0.26.4") } multiplatformResources { resourcesPackage.set("org.example.library") // required resourcesClassName.set("SharedRes") // optional, default MR resourcesVisibility.set(MRVisibility.Internal) // optional, default Public iosBaseLocalizationRegion.set("en") // optional, default "en" iosMinimalDeploymentTarget.set("11.0") // optional, default "9.0" } ``` -------------------------------- ### Gradle Plugin Setup for MOKO Resources Source: https://context7.com/icerockdev/moko-resources/llms.txt Configure the MOKO Resources Gradle plugin in your root and module build scripts. Ensure the `resources-generator` dependency is in the root build script and apply the plugin in the module. Configure the `multiplatformResources` extension with required and optional parameters like `resourcesPackage` and `resourcesClassName`. ```kotlin // root build.gradle.kts buildscript { repositories { gradlePluginPortal() } dependencies { classpath("dev.icerock.moko:resources-generator:0.26.4") } } // module build.gradle.kts plugins { id("dev.icerock.mobile.multiplatform-resources") } dependencies { commonMainApi("dev.icerock.moko:resources:0.26.4") commonMainApi("dev.icerock.moko:resources-compose:0.26.4") // for Compose Multiplatform commonTestImplementation("dev.icerock.moko:resources-test:0.26.4") } multiplatformResources { resourcesPackage.set("com.example.shared") // required: package for generated MR class resourcesClassName.set("MR") // optional, default "MR" resourcesVisibility.set(MRVisibility.Public) // optional: Public or Internal iosBaseLocalizationRegion.set("en") // optional, default "en" iosMinimalDeploymentTarget.set("12.0") // optional, default "9.0" } ``` -------------------------------- ### Multi-module Gradle Project Configuration Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Enable the moko-resources Gradle plugin in both the module containing resources and the module that compiles into an iOS framework. This is necessary for multi-module setups. ```groovy apply plugin: "dev.icerock.mobile.multiplatform-resources" ``` -------------------------------- ### Define Russian Localization String Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Create a `strings.xml` file in `commonMain/moko-resources/` (e.g., `ru`) to define localized strings. This example shows a Russian localization. ```xml Моя строка локализации по умолчанию ``` -------------------------------- ### Read Raw File as Text (iOS) Source: https://context7.com/icerockdev/moko-resources/llms.txt Accesses raw files placed in `commonMain/moko-resources/files/` as text on iOS. Note: This example is commented out in the source. ```kotlin // val html: String = MR.files.template.readText() ``` -------------------------------- ### Get Image by File Name Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Retrieve an ImageResource by its file name, with a fallback to a transparent image if not found. Useful for dynamic image loading. ```kotlin fun getImageByFileName(name: String): ImageResource { val fallbackImage = MR.images.transparent return MR.images.getImageByFileName(name) ?: fallbackImage } ``` -------------------------------- ### iOS: Get String from Raw or Resource Source: https://github.com/icerockdev/moko-resources/blob/master/README.md On iOS, use the localized() method to get the string, which will resolve either the raw string or the resource string. ```swift let string1 = getUserName(user: user).localized() // we got name from User model let string2 = getUserName(user: null).localized() // we got name_placeholder from resources ``` -------------------------------- ### iOS: Get Plural String Source: https://github.com/icerockdev/moko-resources/blob/master/README.md On iOS, use the localized() method on the StringDesc plural object to get the localized string. ```swift let string = getMyPluralDesc(quantity: 10).localized() ``` -------------------------------- ### Android: Get Plural String Source: https://github.com/icerockdev/moko-resources/blob/master/README.md On Android, convert the StringDesc plural object to a string using the provided context. ```kotlin val string = getMyPluralDesc(10).toString(context = this) ``` -------------------------------- ### Compose: Get Formatted Plural String Source: https://github.com/icerockdev/moko-resources/blob/master/README.md In Jetpack Compose, use the pluralStringResource function to display formatted plural strings, providing the quantity for selection and formatting. ```kotlin Text( text = pluralStringResource( MR.plurals.runtime_format, quantity, quantity ) ) ``` -------------------------------- ### Android: Get String from Raw or Resource Source: https://github.com/icerockdev/moko-resources/blob/master/README.md On Android, convert the StringDesc object to a string using the context. This will resolve either the raw string or the resource string. ```kotlin val string1 = getUserName(user).toString(context = this) // we got name from User model val string2 = getUserName(null).toString(context = this) // we got name_placeholder from resources ``` -------------------------------- ### Access MR Strings Directly from Native iOS Source: https://github.com/icerockdev/moko-resources/blob/master/README.md On iOS, access the string description using `MR.strings().my_string.desc()` and then call `localized()` to get the localized string. ```swift let string = MR.strings().my_string.desc().localized() ``` -------------------------------- ### Get Resource ID for Jetpack Compose / SwiftUI Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Retrieve the resource ID for use in UI frameworks like Jetpack Compose on Android or SwiftUI on iOS. This allows referencing resources by ID. ```kotlin val resId = MR.strings.my_string.resourceId ``` ```kotlin text = stringResource(id = MR.strings.email.resourceId) ``` ```swift let resource = MR.strings().email Text( LocalizedStringKey(resource.resourceId), bundle: resource.bundle ) ``` -------------------------------- ### Build Sample Project Source: https://github.com/icerockdev/moko-resources/blob/master/samples/gradle-agp-9-sample/README.md Build the sample project either through your IDE or by running the Gradle build command in the project's root directory. ```bash ./gradlew build ``` -------------------------------- ### Publish moko-resources to Local Maven Source: https://github.com/icerockdev/moko-resources/blob/master/samples/gradle-agp-9-sample/README.md Execute this command in the moko-resources root directory to publish the library to your local Maven repository. This is a prerequisite for building the sample project. ```bash ./gradlew publishToMavenLocal ``` -------------------------------- ### Run Local Checks for Moko-Resources Samples Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Execute this script to check the sample applications locally after making changes to Moko-Resources. This ensures samples function correctly with your local build. ```bash # check samples ./local-samples-check.sh ``` -------------------------------- ### Configure Copying Resources for XCFramework Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Add this script to your Xcode build phase to copy resources when using static frameworks with XCFramework. Ensure your build.gradle config includes the multiplatformResources plugin. ```bash "$SRCROOT/../gradlew" -p "$SRCROOT/../" :shared:copyResourcesMultiPlatformLibraryReleaseXCFrameworkToApp \ -Pmoko.resources.BUILT_PRODUCTS_DIR=$BUILT_PRODUCTS_DIR \ -Pmoko.resources.CONTENTS_FOLDER_PATH=$CONTENTS_FOLDER_PATH ``` ```kotlin multiplatformResources { configureCopyXCFrameworkResources("MultiPlatformLibrary") } ``` -------------------------------- ### ImageResource: Directory Structure and Dark Mode Variants Source: https://context7.com/icerockdev/moko-resources/llms.txt Illustrates the expected directory structure for images, including density suffixes (`@1x`, `@2x`) and dark mode variants (`-dark`). SVG files are also supported. ```text commonMain/moko-resources/images/ ├── logo@1x.png ├── logo@2x.png ├── logo@3x.png ├── banner.svg ├── icon@1x.png └── icon-dark@1x.png ← dark mode variant for icon ``` -------------------------------- ### Enable Android Resources for Host Tests (AGP 8.2.0 - 8.7.x) Source: https://github.com/icerockdev/moko-resources/blob/master/README.md As a workaround for older AGP versions, enable Android resources using the legacy android block. The withHostTest DSL is not available in these versions. ```kotlin android { testOptions { unitTests.isIncludeAndroidResources = true } } ``` -------------------------------- ### Xcode Build Phase Script for iOS Static Frameworks Source: https://context7.com/icerockdev/moko-resources/llms.txt A bash script to be added as a custom build phase in Xcode. It ensures resources are copied from the Kotlin framework to the app bundle after compilation, required for static iOS frameworks. ```bash # Xcode Build Phase script (run after Kotlin framework compilation) # With cocoapods plugin: "$SRCROOT/../gradlew" -p "$SRCROOT/../" \ :shared:copySharedFrameworkResourcesToApp \ -Pmoko.resources.BUILT_PRODUCTS_DIR="$BUILT_PRODUCTS_DIR" \ -Pmoko.resources.CONTENTS_FOLDER_PATH="$CONTENTS_FOLDER_PATH" \ -Pkotlin.native.cocoapods.platform="$PLATFORM_NAME" \ -Pkotlin.native.cocoapods.archs="$ARCHS" \ -Pkotlin.native.cocoapods.configuration="${KOTLIN_FRAMEWORK_BUILD_TYPE:-"$CONFIGURATION"}" ``` -------------------------------- ### Compose JS String Loaders from Multiple Modules Source: https://context7.com/icerockdev/moko-resources/llms.txt Demonstrates how to combine `RemoteJsStringLoader` instances from different modules to load strings from multiple sources. ```kotlin val composedLoader = module1.MR.stringsLoader + module2.MR.stringsLoader val provider = composedLoader.getOrLoad() ``` -------------------------------- ### Create Formatted String Description Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Create a `StringDesc` for formatted strings by passing the resource and the arguments to `StringDesc.ResourceFormatted`. Alternatively, use the `format` extension function. ```kotlin fun getMyFormatDesc(input: String): StringDesc { return StringDesc.ResourceFormatted(MR.strings.my_string_formatted, input) } ``` ```kotlin fun getMyFormatDesc(input: String): StringDesc { return MR.strings.my_string_formatted.format(input) } ``` -------------------------------- ### Dynamic Asset Lookup by File Path Source: https://context7.com/icerockdev/moko-resources/llms.txt Allows retrieving an `AssetResource` object dynamically by providing its file path. ```kotlin val asset: AssetResource? = MR.assets.getAssetByFilePath("data/cities.json") ``` -------------------------------- ### Run Local Checks for Moko-Resources Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Execute this script to check the Moko-Resources library and plugin locally before submitting changes. Ensure the project is set up correctly. ```bash # check lib & plugin ./local-check.sh ``` -------------------------------- ### Enable Android Resources for Host Tests (AGP 8.8.0+) Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Use the native DSL within the androidLibrary block to enable Android resources for host tests. This is required for moko-resources to access generated R classes during testing. ```kotlin kotlin { androidLibrary { // ... base configuration (namespace, compileSdk, etc.) withHostTest { isIncludeAndroidResources = true } // OR if you use builder for advanced configuration: /* withHostTestBuilder { // your builder config }.configure { isIncludeAndroidResources = true } */ } } ``` -------------------------------- ### Xcode Build Phase for Static Framework Resources (No Cocoapods) Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Add a build phase in Xcode to copy resources to the application when using static frameworks without cocoapods. This script should be placed after the Kotlin Framework Compilation phase. ```shell "$SRCROOT/../gradlew" -p "$SRCROOT/../" :yourframeworkproject:copyFrameworkResourcesToApp \ -Pmoko.resources.PLATFORM_NAME="$PLATFORM_NAME" \ -Pmoko.resources.CONFIGURATION="${KOTLIN_FRAMEWORK_BUILD_TYPE:-$CONFIGURATION}" \ -Pmoko.resources.ARCHS="$ARCHS" \ -Pmoko.resources.BUILT_PRODUCTS_DIR="$BUILT_PRODUCTS_DIR" \ -Pmoko.resources.CONTENTS_FOLDER_PATH="$CONTENTS_FOLDER_PATH" ``` -------------------------------- ### SwiftUI Image Extension Source: https://github.com/icerockdev/moko-resources/blob/master/README.md An Image extension for SwiftUI that allows initializing an Image directly from an ImageResource using a KeyPath. This provides compile-time checking for resource typos or missing assets. ```swift extension Image { init(resource: KeyPath) { let imageResource = MR.images()[keyPath: resource] self.init(imageResource.assetImageName, bundle: imageResource.bundle) } } ``` ```swift Image(resource: \.home_black_18) ``` -------------------------------- ### Create StringDesc from Raw or Resource Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Use StringDesc.Raw for literal strings or StringDesc.Resource for strings defined in resources. This allows dynamic selection of string sources. ```kotlin fun getUserName(user: User?): StringDesc { if (user != null) { return StringDesc.Raw(user.name) } else { return StringDesc.Resource(MR.strings.name_placeholder) } } ``` -------------------------------- ### Define Default Localization String Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Create a `strings.xml` file in `commonMain/moko-resources/base` to define default localization strings. This sets up the base for all other localizations. ```xml My default localization string ``` -------------------------------- ### Copy Framework Resources to App (Shell Script) Source: https://context7.com/icerockdev/moko-resources/llms.txt This script is used without the cocoapods plugin to copy framework resources to the app. It requires several environment variables to be set. ```shell "$SRCROOT/../gradlew" -p "$SRCROOT/../" \ :shared:copyFrameworkResourcesToApp \ -Pmoko.resources.PLATFORM_NAME="$PLATFORM_NAME" \ -Pmoko.resources.CONFIGURATION="${KOTLIN_FRAMEWORK_BUILD_TYPE:-$CONFIGURATION}" \ -Pmoko.resources.ARCHS="$ARCHS" \ -Pmoko.resources.BUILT_PRODUCTS_DIR="$BUILT_PRODUCTS_DIR" \ -Pmoko.resources.CONTENTS_FOLDER_PATH="$CONTENTS_FOLDER_PATH" ``` -------------------------------- ### Configure Moko-Resources Gradle Plugin Source: https://context7.com/icerockdev/moko-resources/llms.txt Configures the `multiplatformResources` Gradle extension in `build.gradle.kts`. Specifies package name, class name, visibility, iOS settings, and custom resource directories. ```kotlin multiplatformResources { // Required: package name for the generated MR class resourcesPackage.set("com.example.shared") // Optional: name of the generated class (default: "MR") resourcesClassName.set("SharedRes") // Optional: visibility of the generated class members resourcesVisibility.set(MRVisibility.Internal) // Optional: base localization region for iOS .lproj (default: "en") iosBaseLocalizationRegion.set("en") // Optional: iOS minimal deployment target for static framework resource copy iosMinimalDeploymentTarget.set("13.0") // Optional: custom resource source directory for a specific sourceSet resourcesSourceSets { getByName("jvmMain").srcDirs(File(projectDir, "jvmResources")) } } ``` -------------------------------- ### Define Formatted Localization String Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Add formatted strings to `commonMain/moko-resources/base/strings.xml` using placeholders like `%s`. Ensure localized versions also use the same placeholder style. ```xml My format \'%s\' ``` -------------------------------- ### Xcode Build Phase for Static Framework Resources (Cocoapods) Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Add a build phase in Xcode to copy resources to the application when using static frameworks with org.jetbrains.kotlin.native.cocoapods. This script should be placed after the Kotlin Framework Compilation phase. ```shell "$SRCROOT/../gradlew" -p "$SRCROOT/../" :yourframeworkproject:copy`YourFrameworkName`FrameworkResourcesToApp \ -Pmoko.resources.BUILT_PRODUCTS_DIR="$BUILT_PRODUCTS_DIR" \ -Pmoko.resources.CONTENTS_FOLDER_PATH="$CONTENTS_FOLDER_PATH" \ -Pkotlin.native.cocoapods.platform="$PLATFORM_NAME" \ -Pkotlin.native.cocoapods.archs="$ARCHS" \ -Pkotlin.native.cocoapods.configuration="${KOTLIN_FRAMEWORK_BUILD_TYPE:-$CONFIGURATION}" ``` -------------------------------- ### Access Plain Text Files Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Read plain text files from the `commonMain/moko-resources/files` directory. Requires a `test.txt` resource file. ```kotlin val text = MR.files.test.getText(context = this) ``` ```kotlin val text = MR.files.test.readText() ``` -------------------------------- ### StringDesc: Raw, Resource, Formatted, Plural, and Composed Strings Source: https://context7.com/icerockdev/moko-resources/llms.txt Demonstrates various ways to create and compose StringDesc objects for different string types. Use `StringDesc.localeType` to control runtime locale selection. ```kotlin import dev.icerock.moko.resources.desc.StringDesc import dev.icerock.moko.resources.desc.desc import dev.icerock.moko.resources.desc.plus // Raw string val raw: StringDesc = "Hello World".desc() // Resource string val res: StringDesc = MR.strings.app_name.desc() // Formatted resource string val formatted: StringDesc = MR.strings.greeting.format("Alice") // Plural string val plural: StringDesc = MR.plurals.item_count.desc(42) // Composed string (concatenation with separator) val composed: StringDesc = MR.strings.app_name.desc() + ": ".desc() + "v1.0".desc() val joined: StringDesc = listOf( MR.strings.app_name.desc(), MR.strings.greeting.format("Bob") ).joinToStringDesc(separator = " | ") // Override locale at runtime (affects all StringDesc resolution) StringDesc.localeType = StringDesc.LocaleType.Custom("es") // Revert to system locale StringDesc.localeType = StringDesc.LocaleType.System // Android resolution val text: String = composed.toString(context) // iOS resolution (Swift): let text = composed.localized() // Compose Multiplatform import dev.icerock.moko.resources.compose.localized @Composable fun Label(desc: StringDesc) { Text(text = desc.localized()) } ``` -------------------------------- ### Reactive File Access in Compose Multiplatform Source: https://context7.com/icerockdev/moko-resources/llms.txt Provides reactive state for reading raw files in Compose Multiplatform applications. Displays a loading indicator while the file is being fetched. ```kotlin @Composable fun ConfigView() { val configText: String? by MR.files.config.readTextAsState() configText?.let { Text(text = it) } ?: CircularProgressIndicator() } ``` -------------------------------- ### Define Formatted Plural String Resource Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Create a plurals.xml file for formatted plural strings. Use %d placeholders for arguments that will be inserted into the string. ```xml no items %d item %d items %d items %d items %d items ``` -------------------------------- ### Custom Resource SourceSet Configuration Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Configure custom paths for resource source sets in your project's build.gradle. This allows you to specify alternative directories for resources. ```groovy multiplatformResources { resourcesPackage.set("org.example.library.customResource") // required resourcesSourceSets { getByName("jvmMain").srcDirs( File(projectDir, "customResources") ) } } ``` -------------------------------- ### Configure iOS XCFramework Resource Copy Task Source: https://context7.com/icerockdev/moko-resources/llms.txt Adds configuration within the `multiplatformResources` block to register tasks for copying resources from an XCFramework to the iOS app bundle, essential for static iOS frameworks. ```kotlin multiplatformResources { resourcesPackage.set("com.example.shared") // Register tasks to copy resources from XCFramework to the iOS app bundle project.configureCopyXCFrameworkResources("MultiPlatformLibrary") } ``` -------------------------------- ### Define Plural String Resource Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Create a plurals.xml file in the base resources directory to define plural strings. This file specifies different text variations based on quantity. ```xml zero one two few many other ``` -------------------------------- ### Reactive Asset Loading in Compose Multiplatform Source: https://context7.com/icerockdev/moko-resources/llms.txt Loads asset file content as reactive state in Compose Multiplatform. Displays the loaded text length once available. ```kotlin @Composable fun CitiesLoader() { val citiesJson: String? by MR.assets.data.cities.readTextAsState() citiesJson?.let { Text(text = "Loaded: ${it.length} chars") } } ``` -------------------------------- ### Access Plain Text Files in Compose Multiplatform Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Read plain text files as a state-driven property in Compose Multiplatform using `readTextAsState()`. ```kotlin val fileContent: String? by MR.files.test.readTextAsState() ``` -------------------------------- ### ImageResource: Android, iOS, and Compose Multiplatform Access Source: https://context7.com/icerockdev/moko-resources/llms.txt Demonstrates how to load images using `MR.images.*` across different platforms. Includes methods for Android (`getDrawable`, `drawableResId`), iOS (`toUIImage`), and Compose Multiplatform (`painterResource`). ```kotlin // Android val drawable: Drawable? = MR.images.logo.getDrawable(context) imageView.setImageResource(MR.images.logo.drawableResId) // iOS (Swift) // let image: UIImage? = MR.images().logo.toUIImage() // Compose Multiplatform import dev.icerock.moko.resources.compose.painterResource @Composable fun Logo() { Image( painter = painterResource(MR.images.logo), contentDescription = "App logo", modifier = Modifier.size(120.dp) ) } // Dynamic lookup by file name (common code) fun getImageByName(name: String): ImageResource = MR.images.getImageByFileName(name) ?: MR.images.logo ``` -------------------------------- ### iOS/macOS Info.plist Localizations Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Add localizations to your iOS/macOS Info.plist file to use localization strings. Ensure all used languages are included in the array. ```xml CFBundleLocalizations en ru ``` -------------------------------- ### Revert to System Locale in Common Code Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Return StringDesc to using the device's system settings for localization by setting StringDesc.localeType to StringDesc.LocaleType.System. ```kotlin StringDesc.localeType = StringDesc.LocaleType.System ``` -------------------------------- ### Xcode Build Phase for iOS Executable Resources Source: https://github.com/icerockdev/moko-resources/blob/master/README.md When using an 'executable' Kotlin target, add a custom build phase in Xcode after Kotlin compilation to copy resources. The task name should be configured based on your target. ```shell "$SRCROOT/../gradlew" -p "$SRCROOT/../" :shared:copyResourcesDebugExecutableIosSimulatorArm64 \ -Pmoko.resources.BUILT_PRODUCTS_DIR=$BUILT_PRODUCTS_DIR \ -Pmoko.resources.CONTENTS_FOLDER_PATH=$CONTENTS_FOLDER_PATH ``` -------------------------------- ### Access Assets in Compose Multiplatform Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Access files stored in the `commonMain/moko-resources/assets` directory as state-driven properties in Compose Multiplatform. ```kotlin val assetContent: String? by MR.assets.test.readTextAsState() ``` -------------------------------- ### Access Formatted Plural String in Common Code (Method 2) Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Use the format extension function to create a formatted plural string, passing the quantity for selection and formatting. ```kotlin fun getMyPluralFormattedDesc(quantity: Int): StringDesc { // we pass quantity as selector for correct plural string and for pass quantity as argument for formatting return MR.plurals.my_plural.format(quantity, quantity) } ``` -------------------------------- ### Read Raw File as Text (Android) Source: https://context7.com/icerockdev/moko-resources/llms.txt Accesses raw files placed in `commonMain/moko-resources/files/` as text. Requires an Android context. ```kotlin val configJson: String = MR.files.config.readText(context) ``` -------------------------------- ### ColorResource: Android, iOS, JVM/Web, and Compose Multiplatform Access Source: https://context7.com/icerockdev/moko-resources/llms.txt Shows how to retrieve color values on different platforms using `MR.colors.*`. Note the specific methods for Android (`getColor`), iOS (`getUIColor`/`getNSColor`), and JVM/Web (`lightColor`/`darkColor`). ```kotlin // Android val color: Int = MR.colors.primary.getColor(context) val brandBg: Int = MR.colors.brand_bg.getColor(context) // respects system dark mode // iOS (Swift) // let uiColor: UIColor = MR.colors().primary.getUIColor() // macOS (Swift) // let nsColor: NSColor = MR.colors().primary.getNSColor() // JVM / Web val lightColor: java.awt.Color = MR.colors.brand_bg.lightColor val darkColor: java.awt.Color = MR.colors.brand_bg.darkColor // Compose Multiplatform import dev.icerock.moko.resources.compose.colorResource @Composable fun BrandButton() { Button( colors = ButtonDefaults.buttonColors(containerColor = colorResource(MR.colors.primary)) ) { Text("Click") } } ``` -------------------------------- ### StringResource Declaration and Usage Source: https://context7.com/icerockdev/moko-resources/llms.txt Define localized strings in `strings.xml` files within `commonMain/moko-resources/base/` and locale-specific subdirectories. Access these strings in common code using `MR.strings.*.desc()` and resolve them to platform `String` objects. For Compose Multiplatform, use the `stringResource` function. ```xml My App Hello, %s! Моё приложение Привет, %s! ``` ```kotlin // commonMain — access via StringDesc fun getAppName(): StringDesc = MR.strings.app_name.desc() fun getGreeting(name: String): StringDesc = MR.strings.greeting.format(name) // Android val appName: String = getAppName().toString(context) val greeting: String = getGreeting("Alice").toString(context) // iOS (Swift) // let appName = getAppName().localized() // let greeting = getGreeting(name: "Alice").localized() // Compose Multiplatform (commonMain) import dev.icerock.moko.resources.compose.stringResource @Composable fun Greeting() { Text(text = stringResource(MR.strings.app_name)) Text(text = stringResource(MR.strings.greeting, "Alice") } ``` -------------------------------- ### Access Theme Colors on JVM Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Retrieve theme-specific color values (light/dark) on the JVM using `MR.colors`. ```kotlin val light: Color = MR.colors.valueColor.lightColor val dark: Color = MR.colors.valueColor.darkColor ``` -------------------------------- ### Exporting Classes to Swift Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Add export declarations in your framework configuration to make Moko Resources extensions accessible from Swift. ```groovy framework { export("dev.icerock.moko:resources:0.26.4") export("dev.icerock.moko:graphics:0.10.0") // toUIColor here } ``` -------------------------------- ### Compose Multiplatform Image Loading Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Load images in Compose Multiplatform using the painterResource function with an ImageResource. Ensures type-safe access to resources. ```kotlin val painter: Painter = painterResource(MR.images.home_black_18) ``` -------------------------------- ### Load Localization Strings on JS Source: https://github.com/icerockdev/moko-resources/blob/master/README.md On JavaScript targets, you can load localization strings from a remote file using `MR.stringsLoader.getOrLoad()`. Then, use the `localized` extension function. ```kotlin val strings = MR.stringsLoader.getOrLoad() // loading localization from a remote file val string = getMyString().localized(strings) ``` -------------------------------- ### Disable Static Framework Warning Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Add this property to your gradle.properties file to disable warnings related to static framework usage. ```properties moko.resources.disableStaticFrameworkWarning=true ``` -------------------------------- ### SwiftUI Font Extension Source: https://github.com/icerockdev/moko-resources/blob/master/README.md A Font extension for SwiftUI that allows initializing a Font directly from a FontResource using a KeyPath and specifying a size. This ensures compile-time safety and simplifies font usage. ```swift extension Font { init(resource: KeyPath, withSize: Double = 14.0) { self.init(MR.fonts()[keyPath: resource].uiFont(withSize: withSize)) } } ``` ```swift Text("Text displayed resource font") .font(Font(resource: \.raleway_regular, withSize: 14.0)) ``` -------------------------------- ### ColorResource: Declaring and Accessing Colors Source: https://context7.com/icerockdev/moko-resources/llms.txt Defines colors in `colors.xml` with support for light/dark variants and references. Access generated `MR.colors.*` for platform-specific color retrieval. ```xml #FF6200EE #FFFFFFFF #FF121212 @color/primary ``` -------------------------------- ### FontResource: Directory Structure Source: https://context7.com/icerockdev/moko-resources/llms.txt Shows the placement of font files (`.ttf`, `.otf`) within the `commonMain/moko-resources/fonts/` directory. ```text commonMain/moko-resources/fonts/ ├── Raleway-Regular.ttf ├── Raleway-Bold.ttf └── Raleway-Italic.ttf ``` -------------------------------- ### Read Asset File as Text (Android) Source: https://context7.com/icerockdev/moko-resources/llms.txt Reads an asset file from the Android `AssetManager` using its original path. Requires an Android context. ```kotlin val assetManager: AssetManager = context.assets val stream: InputStream = assetManager.open(MR.assets.data.cities.originalPath) val json: String = stream.bufferedReader().readText() ``` -------------------------------- ### Image Resource Naming Convention Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Follow a specific naming convention for image files in the moko-resources/images directory to support different screen densities and resolutions on Android and iOS. ```text - @0.75x - android ldpi; - @1x - android mdpi, ios 1x; - @1.5x - android hdpi; - @2x - android xhdpi, ios 2x; - @3x - android xxhdpi, ios 3x; - @4x - android xxxhdpi. ``` -------------------------------- ### iOS: Load Shared Image Source: https://github.com/icerockdev/moko-resources/blob/master/README.md On iOS, use the toUIImage() method of an ImageResource to convert it to a UIImage for use in image views. ```swift imageView.image = image.toUIImage() ``` -------------------------------- ### Access Formatted Plural String in Common Code (Method 1) Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Retrieve a formatted plural string by passing the quantity as both the selector and the argument for formatting. ```kotlin fun getMyPluralFormattedDesc(quantity: Int): StringDesc { // we pass quantity as selector for correct plural string and for pass quantity as argument for formatting return StringDesc.PluralFormatted(MR.plurals.my_plural, quantity, quantity) } ``` -------------------------------- ### Use Localization String in Compose Multiplatform Source: https://github.com/icerockdev/moko-resources/blob/master/README.md In Compose Multiplatform, you can directly use the `stringResource` composable function with the generated `MR.strings.my_string` resource. ```kotlin val string: String = stringResource(MR.strings.my_string) ``` -------------------------------- ### Access Original Asset Path Source: https://context7.com/icerockdev/moko-resources/llms.txt Retrieves the original file path for an asset resource, useful for accessing assets via platform-specific APIs. ```kotlin val path: String = MR.assets.data.cities.originalPath // "data/cities.json" ``` -------------------------------- ### Load Localized Strings on JS Targets Source: https://context7.com/icerockdev/moko-resources/llms.txt Fetches and caches locale files for JS targets using `RemoteJsStringLoader`. This is necessary because string resources are loaded asynchronously at runtime. ```kotlin import dev.icerock.moko.resources.provider.RemoteJsStringLoader // Generated MR object exposes a loader: // val loader: RemoteJsStringLoader = MR.stringsLoader suspend fun loadStrings() { val provider = MR.stringsLoader.getOrLoad() val localizedString: String = MR.strings.app_name.localized(provider) } ``` -------------------------------- ### Android App Bundle Configuration for Locales Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Configure your Android app's build.gradle to include all locales in the App Bundle. This ensures all localized resources are available at runtime, not just the system locale. ```gradle android { bundle { language { enableSplit = false } } } ``` -------------------------------- ### Android Build Type Matching Fallbacks Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Use matchingFallbacks to specify alternative matches for a given build type when it's not directly defined, such as a 'staging' build type. ```gradle buildTypes { staging { initWith debug matchingFallbacks = ['debug'] } } ``` -------------------------------- ### FontResource: Android, iOS, and Compose Multiplatform Access Source: https://context7.com/icerockdev/moko-resources/llms.txt Illustrates how to access font resources using `MR.fonts.*` for Android (`getTypeface`), iOS (`uiFont`), and Compose Multiplatform (`fontFamilyResource`, `asFont`). ```kotlin // Android val typeface: Typeface? = MR.fonts.Raleway.regular.getTypeface(context) textView.typeface = typeface // iOS (Swift) // let uiFont: UIFont = MR.fonts().Raleway.regular.uiFont(withSize: 16.0) // Compose Multiplatform import dev.icerock.moko.resources.compose.fontFamilyResource @Composable fun StyledText(text: String) { val fontFamily = fontFamilyResource(MR.fonts.Raleway.regular) Text(text = text, fontFamily = fontFamily) } // Or using FontResource.asFont() for fine-grained control @Composable fun BoldText(text: String) { val font = MR.fonts.Raleway.bold.asFont( weight = FontWeight.Bold, style = FontStyle.Normal ) Text(text = text, fontFamily = font?.let { FontFamily(it) } ?: FontFamily.Default) } ``` -------------------------------- ### PluralsResource Declaration and Usage Source: https://context7.com/icerockdev/moko-resources/llms.txt Define plural strings in `plurals.xml` under `commonMain/moko-resources/base/` following ICU/Android conventions. Access them using `MR.plurals.*.format()` in common code and resolve them on the platform. For Compose Multiplatform, use `pluralStringResource`. ```xml %d item %d items ``` ```kotlin // commonMain fun itemCountDesc(count: Int): StringDesc = MR.plurals.item_count.format(count, count) // number selector + format arg // Android val text: String = itemCountDesc(3).toString(context) // "3 items" // Compose Multiplatform import dev.icerock.moko.resources.compose.pluralStringResource @Composable fun ItemCounter(count: Int) { Text(text = pluralStringResource(MR.plurals.item_count, count, count)) } ``` -------------------------------- ### Use Localization String on Android Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Convert `StringDesc` to a `String` on Android by calling `toString(context)`. A `Context` is required for resource access. ```kotlin val string = getMyString().toString(context = this) ``` -------------------------------- ### Declare Supported Localizations (XML) Source: https://context7.com/icerockdev/moko-resources/llms.txt This XML snippet is used in an iOS/macOS Info.plist file to declare the supported localizations for the application. ```xml CFBundleLocalizations en ru es ``` -------------------------------- ### Compose Multiplatform Font as Font Object Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Obtain a Font object from a FontResource in Compose Multiplatform, allowing for customization of weight and style. Useful for fine-grained control over font appearance. ```kotlin val font: Font = MR.fonts.Raleway.italic.asFont( weight = FontWeight.Normal, // optional style = FontStyle.Normal // optional ) ``` -------------------------------- ### Access Plural String in Common Code Source: https://github.com/icerockdev/moko-resources/blob/master/README.md In your common main code, create a function to retrieve a plural string resource using StringDesc.Plural. Pass the quantity to select the appropriate plural form. ```kotlin fun getMyPluralDesc(quantity: Int): StringDesc { return StringDesc.Plural(MR.plurals.my_plural, quantity) } ``` -------------------------------- ### Access Colors on macOS Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Retrieve color resources on macOS using the `MR.colors` object and `getNSColor` method. ```swift val color: NSColor = MR.colors.valueColor.getNSColor() ``` -------------------------------- ### Access Colors on Android Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Retrieve color resources on Android using the `MR.colors` object and `getColor` method. ```kotlin val color: Int = MR.colors.valueColor.getColor(context = this) ``` -------------------------------- ### Compose Multiplatform Font Loading Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Load font families in Compose Multiplatform using fontFamilyResource with a FontResource. This provides a convenient way to use custom fonts. ```kotlin val fontFamily: FontFamily = fontFamilyResource(MR.fonts.Raleway.italic) ``` -------------------------------- ### Android: Load Shared Image Source: https://github.com/icerockdev/moko-resources/blob/master/README.md On Android, use the drawableResId property of an ImageResource to set an image in an ImageView. ```kotlin imageView.setImageResource(image.drawableResId) ``` -------------------------------- ### Use Formatted String on Android Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Convert a formatted `StringDesc` to a `String` on Android by calling `toString(context)`. The provided arguments will be substituted into the string. ```kotlin val string = getMyFormatDesc("hello").toString(context = this) ``` -------------------------------- ### Access MR Strings Directly from Native Android Source: https://github.com/icerockdev/moko-resources/blob/master/README.md On Android, you can access the string description directly using `MR.strings.my_string.desc()` and then convert it to a string with `toString(context)`. ```kotlin val string = MR.strings.my_string.desc().toString(context = this) ``` -------------------------------- ### Access Colors in Compose Multiplatform Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Use the `colorResource` function to access color resources directly within Compose Multiplatform common code. ```kotlin val color: Color = colorResource(MR.colors.valueColor) ``` -------------------------------- ### Access Colors on iOS Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Retrieve color resources on iOS using the `MR.colors` object and `getUIColor` method. ```swift val color: UIColor = MR.colors.valueColor.getUIColor() ``` -------------------------------- ### Access Localization String in CommonMain Source: https://github.com/icerockdev/moko-resources/blob/master/README.md After running the `generateMRcommonMain` Gradle task, you can access generated string resources via the `MR` class in `commonMain`. `StringDesc` is a versatile container for strings. ```kotlin fun getMyString(): StringDesc { return StringDesc.Resource(MR.strings.my_string) } ``` -------------------------------- ### Define Colors in XML Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Define colors in XML format for use in Moko-Resources. Supports hex codes, references, and theme-specific values. ```xml #B02743FF @color/valueColor 0xB92743FF 7CCFEEFF @color/valueColor @color/referenceColor ``` ```xml #B02743FF ``` ```xml @color/valueColor ``` ```xml 0xB92743FF 7CCFEEFF ``` ```xml @color/valueColor @color/referenceColor ``` -------------------------------- ### Use Localization String on iOS Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Convert `StringDesc` to a `String` on iOS by calling `localized()`. This method handles the platform-specific localization. ```swift let string = getMyString().localized() ``` -------------------------------- ### Use Formatted String on iOS Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Convert a formatted `StringDesc` to a `String` on iOS by calling `localized()`. The arguments are passed to the function to perform the formatting. ```swift let string = getMyFormatDesc(input: "hello").localized() ``` -------------------------------- ### Set Custom Locale in Common Code Source: https://github.com/icerockdev/moko-resources/blob/master/README.md Force StringDesc to use a specific locale by setting StringDesc.localeType to a custom locale string. ```kotlin StringDesc.localeType = StringDesc.LocaleType.Custom("es") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.