Switch branches/tags
Clone or download
Latest commit cb085c4 Mar 14, 2018

Simple Preferences

Android Arsenal Bintray

Simple Preference is a SharedPreference Manager generator.

The library generates code for managing SharedPreferences at compile time with Annotation Processor.
So you don't need to manage SharedPreferences' key string by your hand anymore.

And because getter/setter is generated by Annotation Processor, you can take full advantage of AndroidStudio's auto completion.

Currently the library supports get/set/has/remove operation for each keys(String/int/float/long/Set<String>), and clear operation for SharedPreference itself.


SimplePreferences is distributed via jCenter. Bintray.
This library requires JDK8 to run annotation processor.

dependencies {
  annotationProcessor 'net.yslibrary.simplepreferences:simplepreferences-processor:LATEST_LIBRARY_VERSION'
  compile 'net.yslibrary.simplepreferences:simplepreferences:LATEST_LIBRARY_VERSION'


define ordinary POJO with @Preferences and @Key.

public class Config {
  // you'd better define these variable as protected to avoid accidental access
  protected int userId = 0;

  protected String userName = "";

  @Key(omitGetterPrefix = true)
  protected boolean isPremium = false;

and following class is generated

public class ConfigPrefs extends Config {
  private final SharedPreferences prefs;

  private ConfigPrefs(@NonNull Context context) {
    prefs = context.getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);

  public static ConfigPrefs create(@NonNull Context context) {
    if (context == null) {
      throw new NullPointerException("Context is Null!");
    return new ConfigPrefs(context);

  public void clear() {

  public ConfigPrefs setUserId(int value) {
    prefs.edit().putInt("user_id", value).apply();
    return this;

  public int getUserId() {
    return prefs.getInt("user_id", userId);

  public int getUserId(int defaultValue) {
    return prefs.getInt("user_id", defaultValue);

  public boolean hasUserId() {
    return prefs.contains("user_id");

  public ConfigPrefs removeUserId() {
    return this;

  public ConfigPrefs setUserName(String value) {
    prefs.edit().putString("user_name", value).apply();
    return this;

  public String getUserName() {
    return prefs.getString("user_name", userName);

  public String getUserName(String defaultValue) {
    return prefs.getString("user_name", defaultValue);

  public boolean hasUserName() {
    return prefs.contains("user_name");

  public ConfigPrefs removeUserName() {
    return this;

  public ConfigPrefs setIsPremium(boolean value) {
    prefs.edit().putBoolean("is_premium", value).apply();
    return this;

  public boolean isPremium() {
    return prefs.getBoolean("is_premium", isPremium);

  public boolean isPremium(boolean defaultValue) {
    return prefs.getBoolean("is_premium", defaultValue);

  public boolean hasIsPremium() {
    return prefs.contains("is_premium");

  public ConfigPrefs removeIsPremium() {
    return this;

As you may noticed from generated getter method, the values you set in your POJO become default values for each keys.

Also you can provide custom default value with overloaded method.

Generated classes will be placed at the same package as parent classes.



Declare annotated class as SharedPreferences model.

parameter description default vale
value SharedPreferences name empty (class name is converted to lower_snake_case and used as SharedPreferences name)
useDefault use DefaultSharedPreferences or not false
needCommitMethodForClear whether or not create additional clear method which uses SharedPreferences.Editor#commit() false
expose whether to make generated classes public or not true


Declare annotated variable as SharedPreferences key.

parameter description default vale
name preference's key name empty (variable name is converted to lower_snake_case and used as key)
omitGetterPrefix whether or not prepend prefix for getter method false (prepend prefix)
needCommitMethod whether or not create additional setter method which use SharedPreferences.Editor#commit() false (disabled)

omitGetterPrefix is useful when you define boolean value.

needCommitMethodForClear/needCommitMethod is useful when you want to update the backing file synchronously.


Copyright 2016-2017 Shimizu Yasuhiro (yshrsmz)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.