R for Gradle
Type-safe hierarchical resource access for Java Gradle projects – inspired by Android’s R.java!
Generate a type-safe R.java class that mirrors your resource directory structure. Access files and folders with intuitive syntax like R.config.database.readContent() while enjoying full IDE autocompletion and compile-time safety.
β¨ Features
- π Hierarchical Structure β Mirrors your
src/main/resourcesdirectory - ποΈ Gradle Integration β Generates during Gradle build
- π€ Smart Naming β Converts file/folder names to camelCase Java identifiers
- π Rich File API β Read, stream, and manipulate paths easily
- π Folder Methods β Access folder metadata with
_self.getName()and_self.getPath() - β‘ Fast Generation β Lightweight and efficient
π¦ Installation
Add the plugin to your build.gradle:
plugins {
id "com.enosistudio.r-for-gradle" version "1.0.2"
}
dependencies {
implementation "com.enosistudio:r-for-gradle:1.0.2"
}
generateR{
keepInProjectFiles = false // Optional: Keep generated R files in the project directory or not
// Other optionnal conf ...
}
πββοΈ Usage
Before (β Error-prone)
// Hardcoded strings everywhere!
InputStream config = getClass().getResourceAsStream("/config/database.properties");
InputStream logo = getClass().getResourceAsStream("/images/icons/logo.png");
// Typos cause runtime errors π₯
String content = Files.readString(Paths.get("config/databse.properties")); // Whoops!
After (β Type-safe & Intuitive)
import com.enosistudio.generated.R;
// Hierarchical access with autocompletion!
String content = R.config.databaseProperties.readContent();
InputStream logo = R.images.icons.logoPng.openStream();
URL resource = R.templates.emailHtml.getURL();
// Folder information
String folderName = R.config._self.getName(); // "config"
String folderPath = R.config._self.getPath(); // "config"
// Compile-time safety π‘οΈ
R.config.databseProperties.readContent(); // Won't compile - typo caught!
π Generated Structure
Resources:
src/main/resources/
βββ config/
β βββ database.properties
β βββ app-settings.yml
βββ templates/
β βββ email.html
β βββ reports/
β βββ invoice.pdf
βββ logo.png
Generated R.java:
package com.enosistudio.generated;
public final class R {
public static final RFile logoPng = new RFile("logo.png");
public static final class config extends RFolder {
public static final RFolder _self = new config();
private config() { super("config", "config"); }
public static final RFile databaseProperties = new RFile("config/database.properties");
public static final RFile appSettingsYml = new RFile("config/app-settings.yml");
}
public static final class templates extends RFolder {
public static final RFolder _self = new templates();
private templates() { super("templates", "templates"); }
public static final RFile emailHtml = new RFile("templates/email.html");
public static final class reports extends RFolder {
public static final RFolder _self = new reports();
private reports() { super("reports", "templates/reports"); }
public static final RFile invoicePdf = new RFile("templates/reports/invoice.pdf");
}
}
public static class RFolder { /* folder methods */ }
public static final class RFile { /* rich file API */ }
}
βοΈ Configuration
| Parameter | Default | Description |
|---|---|---|
keepInProjectFiles | true | Keep generated files in src/main/java |
resourcesDir | src/main/resources | Resources directory to scan |
packageName | com.enosistudio.generated | Package for generated R.java |
outputSrcDirectory | src/main/java | Output when keepInProjectFiles=true |
outputTargetDirectory | build/generated/sources | Output when keepInProjectFiles=false |
π§ Requirements
- β Java 11+
- π¨ Gradle 7+
β Star this repo if it helps!