Skip to content

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.

License

Notifications You must be signed in to change notification settings

vsnappy1/resourcemanager

Repository files navigation

ResourceManager

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.

License API Build Status License

Setup

Step 1: Add ResourceManager Plugin

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'
        ....
    }

Step 2: Apply ResourceManager Plugin

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'
....

Step 3: Initialize ResourceManager

To enable ResourceManager, follow these steps:

  1. Build the project to trigger code generation.
  2. Initialize ResourceManager in the onCreate method of your Application class.
class MyApplication: Application() {
    
    override fun onCreate() {
        super.onCreate()
        ResourceManager.initialize(this)
    }
}

Usage

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)).

Migration (Beta)

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.

Important Warning ⚠️

Before starting the migration process, it is highly recommend to:

  1. Use Version Control: Ensure your project is tracked with a version control system like Git.
  2. Create a Backup: Either create a new branch or make a copy of your project to prevent unintended changes or data loss during migration.

Running the Migration Task

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.

Post-Migration Checklist

  1. Review the generated migration report, located at .../build/reports/migration/resourcemanager-migration-report.html.
  2. Verify your project builds successfully without warnings or errors.
  3. Ensure that all resources are correctly migrated and that the application behaves as expected.

Contributions

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! 🚀

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages