or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

desktop-application.mddesktop-components.mdgradle-plugin.mdindex.mdresource-management.mdui-tooling.md

resource-management.mddocs/

0

# Resource Management

1

2

Resource management in Compose Multiplatform provides tools for bundling, generating, and accessing application resources across different platforms.

3

4

## Resources Extension

5

6

```kotlin { .api }

7

abstract class ResourcesExtension {

8

// Resource configuration API

9

}

10

```

11

12

The resources extension is available in your build script:

13

14

```kotlin

15

compose {

16

resources {

17

// Resource configuration

18

}

19

}

20

```

21

22

## Resource Generation Tasks

23

24

The plugin provides several tasks for resource processing and code generation.

25

26

### Generate Resource Accessors

27

28

```kotlin { .api }

29

abstract class GenerateResourceAccessorsTask : DefaultTask() {

30

@get:InputFiles

31

abstract val resourcesDirectories: ConfigurableFileCollection

32

33

@get:OutputDirectory

34

abstract val outputDirectory: DirectoryProperty

35

36

@TaskAction

37

fun generate()

38

}

39

```

40

41

This task generates type-safe accessor code for resources.

42

43

### Generate Resource Classes

44

45

```kotlin { .api }

46

abstract class GenerateResClassTask : DefaultTask() {

47

@get:InputFiles

48

abstract val resourceFiles: ConfigurableFileCollection

49

50

@get:OutputFile

51

abstract val outputFile: RegularFileProperty

52

53

@TaskAction

54

fun generateResClass()

55

}

56

```

57

58

Generates resource class definitions.

59

60

### Prepare Compose Resources

61

62

```kotlin { .api }

63

abstract class PrepareComposeResources : DefaultTask() {

64

@get:InputFiles

65

abstract val resourceSources: ConfigurableFileCollection

66

67

@get:OutputDirectory

68

abstract val outputDirectory: DirectoryProperty

69

70

@TaskAction

71

fun prepare()

72

}

73

```

74

75

Prepares resources for compilation and packaging.

76

77

### Assemble Target Resources

78

79

```kotlin { .api }

80

abstract class AssembleTargetResourcesTask : DefaultTask() {

81

@get:InputFiles

82

abstract val resourceDirectories: ConfigurableFileCollection

83

84

@get:OutputDirectory

85

abstract val outputDirectory: DirectoryProperty

86

87

@TaskAction

88

fun assembleResources()

89

}

90

```

91

92

Assembles resources for specific target platforms.

93

94

## Resource DSL

95

96

```kotlin { .api }

97

abstract class ResourcesDSL {

98

// DSL for configuring resource processing

99

}

100

```

101

102

## Generated Resource Classes

103

104

The resource generation tasks create type-safe accessors:

105

106

### Generated Res Class Example

107

108

```kotlin

109

// Generated by Compose Resources plugin

110

object Res {

111

object drawable {

112

val icon: DrawableResource = DrawableResource("drawable/icon.png")

113

val logo: DrawableResource = DrawableResource("drawable/logo.svg")

114

}

115

116

object string {

117

val app_name: StringResource = StringResource("string/app_name")

118

val welcome_message: StringResource = StringResource("string/welcome_message")

119

}

120

121

object font {

122

val roboto_regular: FontResource = FontResource("font/roboto_regular.ttf")

123

}

124

}

125

```

126

127

## Resource Types

128

129

```kotlin { .api }

130

sealed class Resource {

131

abstract val id: String

132

abstract val items: Set<ResourceItem>

133

}

134

135

data class ResourceItem(

136

val path: String,

137

val offset: Long,

138

val size: Long

139

)

140

141

class DrawableResource(

142

id: String,

143

items: Set<ResourceItem>

144

) : Resource()

145

146

class StringResource(

147

id: String,

148

items: Set<ResourceItem>

149

) : Resource()

150

151

class FontResource(

152

id: String,

153

items: Set<ResourceItem>

154

) : Resource()

155

156

class RawResource(

157

id: String,

158

items: Set<ResourceItem>

159

) : Resource()

160

161

enum class ResourceEnvironment {

162

COMMON,

163

ANDROID,

164

IOS,

165

DESKTOP,

166

WEB

167

}

168

```

169

170

## Usage in Compose

171

172

### Loading Resources

173

174

```kotlin

175

import androidx.compose.runtime.*

176

import org.jetbrains.compose.resources.*

177

178

@Composable

179

fun ResourceExample() {

180

// Load drawable resource

181

val icon = painterResource(Res.drawable.icon)

182

183

// Load string resource

184

val appName = stringResource(Res.string.app_name)

185

186

// Load font resource

187

val customFont = fontResource(Res.font.roboto_regular)

188

189

Column {

190

Image(

191

painter = icon,

192

contentDescription = appName

193

)

194

195

Text(

196

text = appName,

197

fontFamily = FontFamily(customFont)

198

)

199

}

200

}

201

```

202

203

### Resource Functions

204

205

```kotlin { .api }

206

@Composable

207

fun painterResource(resource: DrawableResource): Painter

208

209

@Composable

210

fun imageResource(resource: DrawableResource): ImageBitmap

211

212

@Composable

213

fun vectorResource(resource: DrawableResource): ImageVector

214

215

@Composable

216

fun stringResource(resource: StringResource): String

217

218

@Composable

219

fun stringResource(resource: StringResource, vararg formatArgs: Any): String

220

221

@Composable

222

fun pluralStringResource(resource: StringResource, quantity: Int): String

223

224

@Composable

225

fun pluralStringResource(resource: StringResource, quantity: Int, vararg formatArgs: Any): String

226

227

@Composable

228

fun fontResource(resource: FontResource): Font

229

230

suspend fun readResource(resource: RawResource): ByteArray

231

suspend fun readResourceText(resource: RawResource): String

232

233

suspend fun getDrawableResourceBytes(resource: DrawableResource): ByteArray

234

suspend fun getStringResourceBytes(resource: StringResource): ByteArray

235

suspend fun getFontResourceBytes(resource: FontResource): ByteArray

236

```

237

238

## Project Structure

239

240

Organize resources in your project:

241

242

```

243

src/

244

├── commonMain/

245

│ ├── kotlin/

246

│ └── composeResources/

247

│ ├── drawable/

248

│ │ ├── icon.png

249

│ │ └── logo.svg

250

│ ├── font/

251

│ │ └── roboto_regular.ttf

252

│ ├── string/

253

│ │ └── strings.xml

254

│ └── raw/

255

│ └── data.json

256

├── desktopMain/

257

│ └── composeResources/

258

│ ├── drawable/

259

│ │ └── desktop_icon.png

260

│ └── string/

261

│ └── desktop_strings.xml

262

└── androidMain/

263

└── composeResources/

264

└── drawable/

265

└── android_icon.png

266

```

267

268

## String Resources

269

270

Define string resources in XML format:

271

272

```xml

273

<!-- src/commonMain/composeResources/string/strings.xml -->

274

<resources>

275

<string name="app_name">My Desktop App</string>

276

<string name="welcome_message">Welcome to %1$s!</string>

277

<string name="item_count">You have %1$d items</string>

278

</resources>

279

```

280

281

Use in Compose:

282

283

```kotlin

284

@Composable

285

fun StringExample() {

286

val appName = stringResource(Res.string.app_name)

287

val welcomeMessage = stringResource(Res.string.welcome_message, appName)

288

val itemCount = stringResource(Res.string.item_count, 42)

289

290

Column {

291

Text(appName)

292

Text(welcomeMessage)

293

Text(itemCount)

294

}

295

}

296

```

297

298

## Platform-Specific Resources

299

300

Resources can be organized by platform:

301

302

- `commonMain/composeResources/` - Shared across all platforms

303

- `desktopMain/composeResources/` - Desktop-specific resources

304

- `androidMain/composeResources/` - Android-specific resources

305

- `iosMain/composeResources/` - iOS-specific resources

306

307

The resource system automatically selects the appropriate resource based on the target platform, with platform-specific resources taking precedence over common ones.

308

309

## Build Configuration

310

311

Resources are automatically processed when you build your project. The generated resource accessors are available in the `compose.resources` package and can be imported and used directly in your Compose code.

312

313

```kotlin

314

// Build script configuration

315

compose {

316

resources {

317

// Resource processing configuration if needed

318

}

319

}

320

```