R for Gradle

Maven Central
License
Java

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/resources directory
  • πŸ—οΈ 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

ParameterDefaultDescription
keepInProjectFilestrueKeep generated files in src/main/java
resourcesDirsrc/main/resourcesResources directory to scan
packageNamecom.enosistudio.generatedPackage for generated R.java
outputSrcDirectorysrc/main/javaOutput when keepInProjectFiles=true
outputTargetDirectorybuild/generated/sourcesOutput when keepInProjectFiles=false

πŸ”§ Requirements

  • β˜• Java 11+
  • πŸ”¨ Gradle 7+

⭐ Star this repo if it helps!