# Introducing PSync

DevFeed: [Introducing PSync](<https://devfeed.tech/articles/introducing-psync-31893.md>)

Original publisher: [Read original article](<http://engineering.flipboard.com//2015/09/psync>)

Author: https://github.com/hzsweers (Zac Sweers)

Published: 2015-09-08T00:00:00Z

Content type: article

Language: en

Sources: [Flipboard](<https://devfeed.tech/sources/flipboard.md>)

Topics: [Android](<https://devfeed.tech/topics/android.md>), [gradle-plugin](<https://devfeed.tech/topics/gradle-plugin.md>), [Gradle](<https://devfeed.tech/topics/gradle.md>), [Java](<https://devfeed.tech/topics/java.md>), [XML](<https://devfeed.tech/topics/xml.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [code](<https://devfeed.tech/tags/code.md>), [gradle](<https://devfeed.tech/tags/gradle.md>), [gradle-plugin](<https://devfeed.tech/tags/gradle-plugin.md>), [java](<https://devfeed.tech/tags/java.md>), [open-source](<https://devfeed.tech/tags/open-source.md>), [xml](<https://devfeed.tech/tags/xml.md>)

## AI overview

Flipboard introduces PSync, an Android-specific Gradle plugin that generates Java representations of XML preferences. The plugin reduces the boilerplate required to mirror preference values in code and supports both library and application projects.

## Source excerpt

Here on the Android team at Flipboard, we have a lot of settings for users to adjust their experience. If you throw in internal settings, we have about 100 total preferences to manage. This is a lot of boilerplate to maintain, because preferences in Android have no built-in synchronization (unlike Resources). Our 1 PreferenceFragment class has a couple hundred lines of boilerplate fields at the top where we keep in-code mirrors to these preference values. This design is tedious, brittle, and requires a lot of overhead to keep in sync with XML. We developed a Gradle plugin called PSync to solve this problem. PSync is an Android-specific Gradle plugin that generates Java representations of XML preferences. These Java classes can then be used directly in your code. The generated code is very much inspired by how 1 R.java works for resources, and should feel familiar to developers. It's easy to use, has some simple configurations for fine-tuning your generated code, and is ready to drop into both library and application projects. A full overview can be found on the GitHub README, but here's a quick preview: Say you have a preference: 1 2 3 4 <CheckboxPreference android:key="show_images" android:defaultValue="true" /> You can reference it like this: 1 2 3 4 5 6 7 8 9 String theKey = P.showImages.key; boolean current = P.showImages.get(); P.showImages.put(false).apply(); // If you use Rx-Preferences P.showImages.rx().asObservable().omgDoRxStuff! // If you need it boolean theDefault = P.showImages.defaultValue(); The generated Java code looks like so: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public final class P { public static final class showImages { public static final String key = "show_images"; public static final boolean defaultValue() { return true; } public static final boolean get() { return PREFERENCES.getBoolean(key, defaultValue()); } public static final SharedPreferences.Editor put(final boolean val) { return PREFERENCES.edit().putBoolean(key, val