diff --git a/README.md b/README.md index ab47714..3803981 100644 --- a/README.md +++ b/README.md @@ -1,61 +1,70 @@ # ResourceManager -ResourceManager is an Android plugin that simplifies accessing Android resources (strings, colors, drawables, etc.) in both Android and non-Android components (e.g., ViewModel) using generated code. +ResourceManager is an Android plugin that simplifies accessing Android resources (strings, colors, drawables, etc.) in both Android and non-Android components (e.g., ViewModel) using generated code. It supports both Kotlin and Java projects. + +[![Maven Central](https://img.shields.io/maven-central/v/dev.randos/resourcemanager-runtime.svg)](https://central.sonatype.com/artifact/dev.randos/resourcemanager-runtime) +![Platform Support](https://img.shields.io/badge/platform-Android-brightgreen.svg) ## Setup -### Step 1: Add JitPack Repository -Add the JitPack repository to your root __build.gradle__ (or __settings.gradle__ for newer projects). +### Step 1: Set Up the KSP Plugin +Add the KSP plugin to your project's (root) __build.gradle__. ```kotlin -repositories { - google() - mavenCentral() +plugins { + id("com.android.application") version "8.0.1" apply false + id("org.jetbrains.kotlin.android") version "1.8.21" apply false ... - maven("https://jitpack.io") <-- + id("com.google.devtools.ksp") version "1.9.0-1.0.12" // Add this line for KSP support } -``` -### Step 2: Apply the KSP Plugin -Include the KSP plugin in your root __build.gradle__. -```kotlin -plugins { - id("com.android.application") version '8.0.1' apply false - id("org.jetbrains.kotlin.android") version '1.8.21' apply false - ... - id("com.google.devtools.ksp") version "1.9.0-1.0.12" <-- +ext { + set("kspVersion", "1.9.0-1.0.12") // Define kspVersion to be used by ResourceManager } ``` +__Note:__ Ensure the KSP version matches your Kotlin version to avoid compatibility issues. -### Step 3: Add Dependencies +### Step 2: Add Dependencies Add the ResourceManager dependencies in your module-level __build.gradle__. ```kotlin plugins { id("com.android.application") id("org.jetbrains.kotlin.android") ... - id("com.google.devtools.ksp") <-- + id("com.google.devtools.ksp") // Apply KSP } ... dependencies { ... - implementation("com.github.vsnappy1:ResourceManager:1.0.0") <-- - ksp("com.github.vsnappy1:ResourceManager:1.0.0") <-- + implementation("dev.randos:resourcemanager-runtime:0.0.2") // Runtime dependency + ksp("dev.randos:resourcemanager-compiler:0.0.2") // Compiler dependency for KSP } ``` -### Step 4: Initialize ResourceManager -Annotate your application class, initialize ResourceManager and build the project to enable KSP to generate the necessary code. +### Step 3: Initialize ResourceManager +To enable ResourceManager, follow these steps: + +1. Annotate your `Application` class with `@InstallResourceManager`. +2. Build the project to trigger KSP code generation. +3. Initialize ResourceManager in the `onCreate` method of your `Application` class. ```kotlin -@InstallResourceManager <-- +@InstallResourceManager // Annotation to set up ResourceManager class MyApplication: Application() { override fun onCreate() { super.onCreate() ... - ResourceManager.initialize(this) <-- + ResourceManager.initialize(this) // Initializes ResourceManager } } ``` +__Note:__ +If your `Application` class is located in the root package (e.g., `com.example.yourapp`), you don’t need to specify a namespace. However, if your Application class is in a sub-package (e.g., `com.example.yourapp.app`), you must specify the namespace in the annotation to correctly reference the resources. +```kotlin +@InstallResourceManager(namespace = "com.example.yourapp") // Specify the namespace if Application is in a sub-package + class MyApplication: Application() { + ... +} +``` ## Usage Here’s an example of how to use ResourceManager in a ViewModel @@ -65,25 +74,18 @@ class MyViewModel : ViewModel() { ... fun getData() { _title.postValue(ResourceManager.Strings.title()) + _icon.postValue(ResourceManager.Drawables.icDoneButton()) + _color.postValue(ResourceManager.Colors.primaryGreen()) } } ``` -## Note -If your namespace differs from the application file location, specify the namespace in the annotation. - -```kotlin -@InstallResourceManager(namespace = "") -class MyApplication: Application() { -... +__Note:__ +To ensure that ResourceManager works correctly with __*ProGuard*__ or __*R8*__, add the following rule to your `proguard-rules.pro` file: +```python +# Keep all classes named ResourceManager, regardless of their package, and retain the initialize method. +-keepclassmembers class **.ResourceManager { + public void initialize(android.app.Application); } ``` -By default, this library uses KSP version __1.9.0-1.0.12__. Make sure to use the same version for project and the library. You can set the KSP version in your project-level __build.gradle__ file, and the library will use this version. - -```kotlin -... -ext { - set("kspVersion", ) -} -```