Skip to content

About

Fast and secure encrypted SharedPreferences for Android with no deprecated methods

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

SecureSharedPreference

An Android library for storing strings and primitive values in an encrypted preferences file. It uses an AES key held by Android Keystore and AES-GCM authenticated encryption. The library has no third-party runtime dependencies.

Requirements

  • Android 6.0 (API 23) or newer
  • Java 17 source and target compatibility
  • Android Keystore support

The library source is compiled with Android API 36 and supports devices from API 23 onward. The demo app targets API 36. Android Keystore AES key generation is only available from API 23; older Android versions are not supported by this implementation.

Hardware-backed storage depends on the device. Android Keystore may use a hardware security module when one is available; this library does not require or verify that the key is hardware-backed.

Why use this library?

Keep sensitive preferences simple

Need to save a small private value without building an encryption layer yourself? SecureSharedPreference gives your Android app a straightforward Java API for storing strings and primitive values encrypted with AES-GCM, using a key managed by Android Keystore.

SecureSharedPreference preferences = new SecureSharedPreference(context, "user_preferences");
preferences.setData("access_token", token);
String savedToken = preferences.getData("access_token", "");

It is designed to be easy to add to an existing app: no third-party runtime dependencies, familiar preference-style operations, support for Android 6.0 (API 23) and newer, and an interactive demo app to explore the API. The library includes typed getters and setters, contains, removeData, and clear.

Android's regular SharedPreferences is still a good fit for non-sensitive settings such as a theme or selected language. For small values that your app wants to keep encrypted at rest, SecureSharedPreference offers a focused alternative. AndroidX EncryptedSharedPreferences is deprecated in the official Android API reference; this project is an independent open-source library, not an official Android or Google product.

SecureSharedPreference is intentionally focused on small preference values rather than every storage need. It does not implement the full SharedPreferences API, and its data format is not compatible with AndroidX EncryptedSharedPreferences. As with any on-device storage, choose based on your app's security needs and handle backup, key invalidation, and migration deliberately. Android Keystore may use hardware-backed key storage when the device supports it, but this library does not require or verify hardware backing.

The Java package is io.github.AbhiTheNullPointer.securesharedpreference. If updating from an earlier package, change the import to:

import io.github.AbhiTheNullPointer.securesharedpreference.SecureSharedPreference;

Install

Add JitPack to the repositories in settings.gradle:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}

Add the dependency to the app module. Replace VERSION with a published release tag:

dependencies {
    implementation 'com.github.AbhiTheNullPointer:SecureSharedPreference:VERSION'
}

Quick start

import io.github.AbhiTheNullPointer.securesharedpreference.SecureSharedPreference;

SecureSharedPreference preferences = new SecureSharedPreference(context, "user_preferences");

preferences.setData("username", "alex");
preferences.setData("user_id", 42);
preferences.setData("last_login", System.currentTimeMillis());
preferences.setData("score", 98.6f);
preferences.setData("premium", true);

String username = preferences.getData("username", "guest");
int userId = preferences.getData("user_id", 0);
long lastLogin = preferences.getData("last_login", 0L);
float score = preferences.getData("score", 0f);
boolean premium = preferences.getData("premium", false);

boolean exists = preferences.contains("username");
preferences.removeData("username");
preferences.clear();

The typed methods putString/getString, putInt/getInt, putLong/getLong, putFloat/getFloat, and putBoolean/getBoolean are also available. setData and getData are overloaded for each of those types.

Construct the class with an application or activity context. Initialization can fail if Android Keystore is unavailable. Write operations can also throw a RuntimeException if encryption or persistence fails. Read operations return their supplied default for missing, malformed, or undecryptable values.

Encryption and storage

  • A 256-bit AES key is generated and retained by Android Keystore.
  • Values are encrypted with AES-GCM and a fresh IV generated by Android Keystore for each encryption.
  • Preference names are stored as Base64-encoded SHA-256 hashes. This obscures names but does not make guessable names secret.
  • Values are stored as Base64 text in a private SharedPreferences file.
  • clear() removes values from the preferences file; the Keystore key remains.
  • Reinstalling the app, clearing app data, restoring preferences without the matching Keystore key, or invalidating the key can make previous values unreadable. Back up or migrate data according to your app's requirements.

The class is not a complete implementation of Android's SharedPreferences interface. It supports only String, int, long, float, and boolean; it does not provide listeners, sets, or editor transactions. Choose storage based on your app's threat model and test key invalidation and backup behavior on supported devices.

Demo app

The :app module is a small interactive demo. It lets you save, load, check whether a key exists, remove, and clear values. Run all checks to see results for contains, set/get, remove, clear, defaults, and every supported value type. Test values use unique keys, and the clear check uses a separate preferences file so existing demo values are preserved. The demo is not a production app.

GitHub releases

Pushing a version tag such as v1.0.0 starts the GitHub Actions release workflow. It builds the library AAR and attaches an installable, debug-signed demo APK to the GitHub Release. The APK is for trying the demo; it is not signed with a production release key.

Build

Open the project in Android Studio with Android SDK 36 installed, or use the Gradle wrapper:

./gradlew :secureprefs:assembleRelease :app:assembleDebug

License

This project is licensed under the MIT License. See LICENSE.

About

Fast and secure encrypted SharedPreferences for Android with no deprecated methods

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages