ResourceManager is an Android plugin that simplifies accessing Android resources (strings, colors, drawables, etc.) in both Android (e.g., Activity, Fragment, Composable) and non-Android components (e.g., ViewModel) using generated code.
Add resourcemanager plugin to your project's root build.gradle(.kts) file.
// If your project uses the plugins block, add the following:
plugins {
id("com.android.application") version "8.0.1" apply false
id("dev.randos.resourcemanager") version "0.1.1" apply false
....
}
// Alternatively, if your project uses the buildscript block, include this:
buildScripts {
dependencies {
classpath "com.android.tools.build:gradle:8.0.1"
classpath 'dev.randos:resourcemanager:0.1.1'
....
}
Apply the ResourceManager plugin in your module-level build.gradle(.kts) file.
// If you are using the plugins block, add the following:
plugins {
id("com.android.application")
id("dev.randos.resourcemanager")
....
}
// Alternatively, if your project uses the apply statement, include this:
apply plugin: 'com.android.application'
apply plugin: 'dev.randos.resourcemanager'
....
To enable ResourceManager, follow these steps:
- Build the project to trigger code generation.
- Initialize ResourceManager in the
onCreate
method of yourApplication
class.
class MyApplication: Application() {
override fun onCreate() {
super.onCreate()
ResourceManager.initialize(this)
}
}
Here’s an example of how ResourceManager
is typically used in an Activity
and a ViewModel
.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding.titleTextView.setText(ResourceManager.Strings.greetings("Kumar"))
binding.avatarImageView.setImageDrawable(ResourceManager.Drawables.icAvatar())
}
}
// Constructor injection for easier testing and improved decoupling.
class MyViewModel(
private val strings: ResourceManager.Strings,
private val drawables: ResourceManager.Drawables
) : ViewModel() {
fun getData() {
_title.postValue(strings.title())
_icon.postValue(drawables.icDoneButton())
}
}
Note* If your app supports dynamic locale, theme or orientation changes at runtime (via code), pass the appropriate context (e.g., an Activity
context) to the function (i.e ResourceManager.Colors.primaryGreen(context)
).
To streamline the transition to ResourceManager, plugin comes with a Gradle task to automate key aspects of the migration process. Please follow these steps carefully.
Before starting the migration process, it is highly recommend to:
- Use Version Control: Ensure your project is tracked with a version control system like Git.
- Create a Backup: Either create a new branch or make a copy of your project to prevent unintended changes or data loss during migration.
To perform the migration, execute the following Gradle command:
./gradlew migrateToResourceManager -PconfirmMigration=true
Note:* The -PconfirmMigration=true
parameter confirms that you understand the potential impacts of the migration and agree to proceed.
- Review the generated migration report, located at .../build/reports/migration/resourcemanager-migration-report.html.
- Verify your project builds successfully without warnings or errors.
- Ensure that all resources are correctly migrated and that the application behaves as expected.
Contributions are highly encouraged, whether it’s fixing a bug, suggesting improvements, or adding new features.
Create a branch for your changes, and submit a pull request. Let’s build something amazing together! 🚀