0
# Resource Management
1
2
Compose Multiplatform resource management system for handling strings, images, fonts, and other assets across multiple platforms with type-safe access and localization support.
3
4
## Capabilities
5
6
### String Resources
7
8
Type-safe string resource management with localization and pluralization support.
9
10
```kotlin { .api }
11
/**
12
* Composable function to resolve and display a string resource
13
* @param resource The string resource to display
14
* @return The localized string value
15
*/
16
@Composable
17
fun stringResource(resource: StringResource): String
18
19
/**
20
* Type-safe string resource representation
21
*/
22
@Immutable
23
data class StringResource(
24
val key: String,
25
val bundle: String = "strings"
26
) {
27
companion object {
28
/**
29
* Create a string resource from a key
30
*/
31
fun fromKey(key: String, bundle: String = "strings"): StringResource
32
}
33
}
34
35
/**
36
* Composable function for plural string resources
37
* @param resource The plural string resource
38
* @param quantity The quantity to determine plural form
39
* @param formatArgs Optional format arguments for string interpolation
40
* @return The localized plural string
41
*/
42
@Composable
43
fun pluralStringResource(
44
resource: PluralStringResource,
45
quantity: Int,
46
vararg formatArgs: Any
47
): String
48
49
/**
50
* Type-safe plural string resource representation
51
*/
52
@Immutable
53
data class PluralStringResource(
54
val key: String,
55
val bundle: String = "strings"
56
)
57
58
/**
59
* Get string resource value without composable context
60
* @param resource The string resource to resolve
61
* @param resourceEnvironment Resource environment for resolution
62
* @return The resolved string value
63
*/
64
fun getString(
65
resource: StringResource,
66
resourceEnvironment: ResourceEnvironment = LocalResourceEnvironment.current
67
): String
68
```
69
70
**Usage Examples:**
71
72
```kotlin
73
// Define string resources (typically in generated code)
74
object Res {
75
object string {
76
val app_name = StringResource("app_name")
77
val welcome_message = StringResource("welcome_message")
78
val item_count = PluralStringResource("item_count")
79
}
80
}
81
82
// Use string resources in composables
83
@Composable
84
fun WelcomeScreen() {
85
Column {
86
Text(
87
text = stringResource(Res.string.app_name),
88
style = MaterialTheme.typography.headlineLarge
89
)
90
91
Text(
92
text = stringResource(Res.string.welcome_message),
93
style = MaterialTheme.typography.bodyLarge
94
)
95
96
val itemCount = 5
97
Text(
98
text = pluralStringResource(
99
Res.string.item_count,
100
quantity = itemCount,
101
formatArgs = arrayOf(itemCount)
102
)
103
)
104
}
105
}
106
107
// Use strings outside composables
108
fun getAppTitle(): String {
109
return getString(Res.string.app_name)
110
}
111
```
112
113
### Image Resources
114
115
Type-safe image resource management with support for vector graphics, raster images, and platform-specific assets.
116
117
```kotlin { .api }
118
/**
119
* Composable function to load and display an image resource
120
* @param resource The image resource to display
121
* @param contentDescription Accessibility description
122
* @param modifier Modifier for styling
123
* @param alignment Alignment within the composable bounds
124
* @param contentScale How to scale the image content
125
* @param alpha Opacity of the image
126
* @param colorFilter Color filter to apply to the image
127
* @param filterQuality Quality of image filtering
128
*/
129
@Composable
130
fun Image(
131
resource: DrawableResource,
132
contentDescription: String?,
133
modifier: Modifier = Modifier,
134
alignment: Alignment = Alignment.Center,
135
contentScale: ContentScale = ContentScale.Fit,
136
alpha: Float = DefaultAlpha,
137
colorFilter: ColorFilter? = null,
138
filterQuality: FilterQuality = DrawScope.DefaultFilterQuality
139
)
140
141
/**
142
* Load an image resource as a Painter
143
* @param resource The image resource to load
144
* @return Painter for the loaded image
145
*/
146
@Composable
147
fun painterResource(resource: DrawableResource): Painter
148
149
/**
150
* Load an image resource as an ImageBitmap
151
* @param resource The image resource to load
152
* @return ImageBitmap of the loaded image
153
*/
154
@Composable
155
fun imageResource(resource: DrawableResource): ImageBitmap
156
157
/**
158
* Load a vector image resource as an ImageVector
159
* @param resource The vector image resource to load
160
* @return ImageVector of the loaded vector graphic
161
*/
162
@Composable
163
fun vectorResource(resource: DrawableResource): ImageVector
164
165
/**
166
* Type-safe drawable resource representation
167
*/
168
@Immutable
169
data class DrawableResource(
170
val key: String,
171
val bundle: String = "drawable"
172
)
173
```
174
175
**Usage Examples:**
176
177
```kotlin
178
// Define image resources (typically in generated code)
179
object Res {
180
object drawable {
181
val ic_launcher = DrawableResource("ic_launcher")
182
val profile_placeholder = DrawableResource("profile_placeholder")
183
val logo_vector = DrawableResource("logo_vector")
184
}
185
}
186
187
// Use image resources
188
@Composable
189
fun ProfileScreen() {
190
Column {
191
// Vector image as icon
192
Icon(
193
imageVector = vectorResource(Res.drawable.logo_vector),
194
contentDescription = "App Logo",
195
modifier = Modifier.size(48.dp)
196
)
197
198
// Raster image
199
Image(
200
resource = Res.drawable.profile_placeholder,
201
contentDescription = "Profile Picture",
202
modifier = Modifier
203
.size(120.dp)
204
.clip(CircleShape),
205
contentScale = ContentScale.Crop
206
)
207
208
// Using painter resource for custom drawing
209
val painter = painterResource(Res.drawable.ic_launcher)
210
Canvas(modifier = Modifier.size(100.dp)) {
211
with(painter) {
212
draw(Size(100.dp.toPx(), 100.dp.toPx()))
213
}
214
}
215
}
216
}
217
```
218
219
### Font Resources
220
221
Type-safe font resource management with support for custom fonts and font families.
222
223
```kotlin { .api }
224
/**
225
* Load a font resource as a Font
226
* @param resource The font resource to load
227
* @param weight Font weight variant
228
* @param style Font style variant
229
* @return Font object for typography
230
*/
231
@Composable
232
fun fontResource(
233
resource: FontResource,
234
weight: FontWeight = FontWeight.Normal,
235
style: FontStyle = FontStyle.Normal
236
): Font
237
238
/**
239
* Create a font family from font resources
240
* @param vararg fonts Font resources with their weights and styles
241
* @return FontFamily containing the specified fonts
242
*/
243
fun fontFamilyResource(vararg fonts: Font): FontFamily
244
245
/**
246
* Type-safe font resource representation
247
*/
248
@Immutable
249
data class FontResource(
250
val key: String,
251
val bundle: String = "font"
252
)
253
```
254
255
**Usage Examples:**
256
257
```kotlin
258
// Define font resources
259
object Res {
260
object font {
261
val roboto_regular = FontResource("roboto_regular")
262
val roboto_bold = FontResource("roboto_bold")
263
val custom_font = FontResource("custom_font")
264
}
265
}
266
267
// Create custom font family
268
val RobotoFamily = fontFamilyResource(
269
fontResource(Res.font.roboto_regular, FontWeight.Normal),
270
fontResource(Res.font.roboto_bold, FontWeight.Bold)
271
)
272
273
// Use custom fonts in typography
274
@Composable
275
fun StyledText() {
276
Column {
277
Text(
278
text = "Regular Text",
279
fontFamily = RobotoFamily,
280
fontWeight = FontWeight.Normal
281
)
282
283
Text(
284
text = "Bold Text",
285
fontFamily = RobotoFamily,
286
fontWeight = FontWeight.Bold
287
)
288
289
Text(
290
text = "Custom Font Text",
291
fontFamily = fontFamilyResource(
292
fontResource(Res.font.custom_font)
293
)
294
)
295
}
296
}
297
```
298
299
### Raw Resources
300
301
Access to raw resource files like JSON, XML, or binary data.
302
303
```kotlin { .api }
304
/**
305
* Read raw resource content as a string
306
* @param resource The raw resource to read
307
* @return String content of the resource
308
*/
309
@Composable
310
suspend fun stringResource(resource: RawResource): String
311
312
/**
313
* Read raw resource content as bytes
314
* @param resource The raw resource to read
315
* @return Byte array content of the resource
316
*/
317
@Composable
318
suspend fun bytesResource(resource: RawResource): ByteArray
319
320
/**
321
* Type-safe raw resource representation
322
*/
323
@Immutable
324
data class RawResource(
325
val key: String,
326
val bundle: String = "raw"
327
)
328
```
329
330
### Resource Environment
331
332
Resource resolution environment for handling different locales and configurations.
333
334
```kotlin { .api }
335
/**
336
* Resource environment providing context for resource resolution
337
*/
338
@Immutable
339
data class ResourceEnvironment(
340
val language: String,
341
val region: String? = null,
342
val theme: ResourceTheme = ResourceTheme.LIGHT,
343
val density: ResourceDensity = ResourceDensity.MDPI
344
)
345
346
/**
347
* Resource theme variants
348
*/
349
enum class ResourceTheme {
350
LIGHT, DARK
351
}
352
353
/**
354
* Resource density variants
355
*/
356
enum class ResourceDensity {
357
LDPI, MDPI, HDPI, XHDPI, XXHDPI, XXXHDPI
358
}
359
360
/**
361
* CompositionLocal for providing resource environment
362
*/
363
val LocalResourceEnvironment: ProvidableCompositionLocal<ResourceEnvironment>
364
365
/**
366
* Get current resource environment
367
*/
368
@Composable
369
fun rememberResourceEnvironment(): ResourceEnvironment
370
```
371
372
**Usage Examples:**
373
374
```kotlin
375
// Provide custom resource environment
376
@Composable
377
fun LocalizedApp() {
378
val resourceEnvironment = ResourceEnvironment(
379
language = "es",
380
region = "ES",
381
theme = ResourceTheme.DARK
382
)
383
384
CompositionLocalProvider(
385
LocalResourceEnvironment provides resourceEnvironment
386
) {
387
MyApp()
388
}
389
}
390
391
// Use resource environment for manual resolution
392
@Composable
393
fun ManualResourceResolution() {
394
val environment = rememberResourceEnvironment()
395
val appName = remember(environment) {
396
getString(Res.string.app_name, environment)
397
}
398
399
Text(text = "App: $appName")
400
}
401
```
402
403
### Resource Code Generation
404
405
Resource access is typically provided through generated code that creates type-safe resource references.
406
407
**Generated Resource Structure:**
408
```kotlin
409
// Generated by Compose Multiplatform Resource plugin
410
object Res {
411
object string {
412
val app_name: StringResource
413
val welcome_message: StringResource
414
val item_count: PluralStringResource
415
}
416
417
object drawable {
418
val ic_launcher: DrawableResource
419
val logo_vector: DrawableResource
420
val profile_image: DrawableResource
421
}
422
423
object font {
424
val custom_font_regular: FontResource
425
val custom_font_bold: FontResource
426
}
427
428
object raw {
429
val config_json: RawResource
430
val sample_data: RawResource
431
}
432
}
433
```
434
435
### Resource Configuration
436
437
Build configuration for resource processing and generation.
438
439
**Gradle Configuration Example:**
440
```kotlin
441
compose {
442
resources {
443
// Resource directories
444
resourcesPackage = "com.example.app.resources"
445
446
// Generate resource classes
447
generateResClass = always
448
449
// Platform-specific resources
450
publicResClass = true
451
452
// Resource optimization
453
optimizeImages = true
454
}
455
}
456
```
457
458
## Required Imports
459
460
```kotlin
461
// Core resource management
462
import org.jetbrains.compose.resources.*
463
464
// Resource types
465
import org.jetbrains.compose.resources.StringResource
466
import org.jetbrains.compose.resources.DrawableResource
467
import org.jetbrains.compose.resources.FontResource
468
import org.jetbrains.compose.resources.RawResource
469
470
// Resource loading functions
471
import org.jetbrains.compose.resources.stringResource
472
import org.jetbrains.compose.resources.painterResource
473
import org.jetbrains.compose.resources.vectorResource
474
import org.jetbrains.compose.resources.fontResource
475
476
// Resource environment
477
import org.jetbrains.compose.resources.ResourceEnvironment
478
import org.jetbrains.compose.resources.LocalResourceEnvironment
479
```