A Gradle plugin for generating typesafe helper code for using together with the Jetpack Preference library for Android.
For learning about how to use Jetpack Preference, please have a look at the official user guide.
This plugin is for developers who build settings UIs using XML files, and who want to maintain typesafe code without
having to hardcode preference keys in Kotlin/Java code.
For this exact purpose, prefgen
generates a PrefR
class that contains constants of the properties defined in the XML
file, and will synchronize them with any changes made to those files.
Additionally, it generates an implementation of PreferenceFragmentCompat
for every XML preferences file.
This implementation takes care about loading the XML file, and it provides type safe getters to access defined
properties.
The naming of the generated fragment classes is based on the XML file names.
As an example, my_preferences.xml
will generate PreferenceFragmentMyPreferences
.
Both PrefR
and fragment classes are generated by default, unless disabled in the configuration.
The plugin is not published on the Gradle plugins repository. Instead, it relies on JitPack as a repository.
First, you need to declare the JitPack repository and the dependency in your buildscript (this is usually done in the root
build.gradle
):
buildscript {
repositories {
// other repositories
maven { url "https://jitpack.io" }
}
dependencies {
// other dependencies
classpath 'com.github.kremi151:prefgen:1.0.0'
}
}
Next, apply the plugin in the build.gradle
file of your app module:
apply plugin: 'lu.kremi151.prefgen'
Then, configure the plugin to your liking:
prefgen {
packageName = rootProject.applicationId // This must be set
generateFragments = true // Generate typesafe fragments by default
generatePrefR = true // Generate PrefR by default
}
Sources are generated when building the app.