Catalog
Just like View Binding, but for resources.
Catalog is a Gradle plugin that generates type-safe, user-friendly extensions to resolve Android resources.
Let's see how the following string resource gets resolved without and with Catalog:
<string name="good_morning_with_weather">Good morning, %1$s! It’s %2$d°C outside.</string>
Without Catalog:
Android resource resolution is overly verbose. It also uses snake case for resource identifiers, which diverts away from Java/Kotlin naming conventions. In addition, parametrized strings and plurals are not type safe. Android Studio comes with a very loose lint check for argument types, but it treats all arguments as optional and it won't flag if you miss one.
Here's how we resolve string resources without Catalog:
With Catalog:
You can also use Catalog to access the resource id directly:
Catalog also works with plurals:
<plurals name="unread_messages">
<item quantity="one">You have %1$d unread message</item>
<item quantity="other">You have %1$d unread messages</item>
</plurals>
string arrays:
<string-array name="seasons">
<item>Spring</item>
<item>Summer</item>
<item>Fall</item>
<item>Winter</item>
</string-array>
and simple color resources:
<color name="red">#FFFF0000</color>
In the future, other resource types like integer arrays, dimensions, etc. will also be supported.
Comment support
Resource comments are also carried over to extension properties and methods:
<!-- This string resource is used in the launcher screen. -->
<string name="app_name">Catalog</string>
Compose support
If you're using compose, @Composable
extensions will also be generated. In order to avoid
signature clashes, Compose extensions are extension methods of
com.flaviofaria.catalog.runtime.compose.[String, Plurals, StringArray]
whereas standard extensions
are extension methods of com.flaviofaria.catalog.runtime.resources.[String, Plurals, StringArray]
,
so make sure you're importing the right class.
How it works
Catalog generates Context
and Fragment
extensions using context receivers.
Since these extensions are inline
, there will be no increase in your app method count or any
significant impact on runtime performance.
Setup and configuration
To use Catalog, just apply the plugin to your module:
plugins {
id 'com.flaviofaria.catalog' version '0.2.1'
}
By default, Catalog generates non-Compose extensions only. Compose extensions will also be generated if it detects Compose among your module dependencies. If your project is 100% written in Compose, you can explicitly turn off non-Compose extensions by adding:
catalog {
generateResourcesExtensions = false
}
Similarly, you can also turn off Compose extensions:
catalog {
generateComposeExtensions = false
}