ProxyPref is an Android library, which represents SharedPreferences as an interface
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
app-no-rx general refactoring (100% test coverage) Sep 17, 2015
app general refactoring (100% test coverage) Sep 17, 2015
gradle sonatype release Sep 15, 2015
proxypref general refactoring (100% test coverage) Sep 17, 2015
build.gradle 0.1 Sep 15, 2015 before general refactoring Sep 16, 2015
gradlew.bat 0.1 Sep 15, 2015
settings.gradle general refactoring (100% test coverage) Sep 17, 2015


ProxyPref is a simple Android library, which allows to easily access SharedPreferences.


Define an interface with set/get method for types: String, Integer, Long, Float, Boolean, Set<String>

Keys are method names from the interface, but without set/get prefixes. You can omit set/get part if you like.

Annotate fields with Preference annotation to set a preference key name if you're going to use ProGuard.


There is also rx.Observable and rx.functions.Action1 interface support to chain preferences into RxJava operators.


null is does not exist value. If you don't want to get nulls - define default values with DefaultString, DefaultInteger, DefaultLong, DefaultFloat, DefaultBoolean, DefaultSet.

You can also remove a key-value pair from preferences by passing null into the set method.

interface MyPreferences {

    // access with get/set prefix
    String getTestString();         // key = testString
    void setTestString(String x);   // key = testString

    // without get/set prefix
    Integer testInteger();          // key = testInteger
    void testInteger(Integer x);    // key = testInteger

    // observe with rx.Observable
    Observable<Integer> lastSelectedItem(); // key = lastSelectedItem

    // set with rx.functions.Action1
    Action1<Integer> setLastSelectedItem(); // key = lastSelectedItem

    // ProGuard ready
    String a12();               // key = username

    // Default value
    String username();          // key = username

    // Default set
    @DefaultSet({"1", "2", "3"})
    Set<String> getSomeSet();   // key = someSet


Is easy!

MyPreferences pref = ProxyPreferences
    .build(MyPreferences.class, getSharedPreferences("preferences", 0));

Log.v("test", pref.username());
MyPreferences pref = ProxyPreferences
    .buildWithRx(MyPreferences.class, getSharedPreferences("preferences", 0));

If you want to have RxJava features. Don't forget to include RxJava itself. ;)

RxJava warning

Keep hard references to RxJava subscriptions otherwise they can be GC'ed due to the SharedPreferences bug.


compile 'info.android15.proxypref:proxypref:0.2.0'