R2generator is a gradle plugin that generate R2.java for android library.
Inside android library, we usually usegetResources().getIdentifier("res_name", "res_type", "package_name")
to access resources.
This plugin provide an easier way to handle the "res_name" above by generating R2.java file during build time.
Here's a sample of auto generated R2.java :
public final class R2 {
public static final class anim {
@AnimRes
public static final String abc_fade_in = "abc_fade_in";
}
//...
}
After applying this gradle plugin, you can simply use R2.anim.abc_fade_in
to replace your hardcoded "abc_fade_in"
.
If you take a look at the source code, you may find out that this library is nothing but a Butterknife Library project copycat. But it does make some difference : ) .
Add this to your bulidscript
:
buildscript {
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'com.github.nichbar:r2generator:1.0.5'
}
}
then apply it in your module:
apply plugin: 'r2generator-plugin'
If you wanna modify this plugin, you may follow these procedure.
- Clone this project.
- Remove
maven { url 'https://jitpack.io' }
from rootbuild.gradle
file. - Apply your changes to r2generator library.
- Execute
./gradlew publish
to generate plugin. - Run the demo project to check your changes.