TinyDB is a lightweight, high-performance database library for Android designed for effortless data persistence. Occupying less than 8KB, it provides a streamlined alternative to SharedPreferences with support for multiple database instances and real-time data observers.
- Ultra-Lightweight: Minimal impact on APK size (~5KB–8KB).
- High Compatibility: Supports API level 19+ (covering 99.7%+ of Android devices).
- Observer Pattern: Built-in
ValueChangeListenerto react to data modifications. - Multi-Instance Support: Maintain separate database files for different data scopes.
- Fast Integration: Boilerplate-free implementation.
Add the JitPack repository to your settings.gradle file:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url '[https://jitpack.io](https://jitpack.io)' }
}
}Add the following line to your app-level build.gradle:
dependencies {
implementation 'com.github.sambhav2358:TinyDB:2.0.1'
}Initialize Database You can use the default instance or create a custom named database for specific use cases.
// Default Instance
TinyDefaultDB defaultDB = TinyDB.getInstance().getDefaultDatabase(this);
// Custom Named Instance
TinyCustomDB customDB = TinyDB.getInstance().getCustomDatabase(this, "SettingsDB");TinyDB simplifies data handling compared to the standard SharedPreferences.Editor workflow.
// Saving Data (Automatic background persistence)
defaultDB.putInt("user_score", 1024);
defaultDB.putString("username", "JohnDoe");
// Retrieving Data
int score = defaultDB.getInt("user_score", 0);
// Deleting Data
defaultDB.clearKey("user_score"); // Single key
defaultDB.clearAll(); // Entire databaseImplement ValueChangeListener to observe changes across your application.
public class MainActivity extends AppCompatActivity implements ValueChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TinyDefaultDB db = TinyDB.getInstance().getDefaultDatabase(this);
db.setValueChangeListener(this);
}
@Override
public <E> void onValueAdded(String key, E value, String dbName) {
// Triggered on putX() methods
}
@Override
public void onKeyRemoved(String key, String dbName) {
// Triggered on clearKey()
}
@Override
public void onAllKeysRemoved(String dbName) {
// Triggered on clearAll()
}
}Primitives: Integer, Boolean, Float, String
Collections: List
Generic Objects: Use put() and get() for custom object serialization.
TinyDB removes the need for manual .edit() and .apply() calls, significantly reducing boilerplate code.
| Feature | Standard SharedPreferences | TinyDB |
|---|---|---|
| Code Verbosity | High (Requires Editor) | Minimal (Direct API) |
| Change Listeners | Manual/Complex | Native Interface |
| Multiple Files | Requires manual management | Instance-based |
| Learning Curve | Moderate | Low |
Feature Requests: Please open an Issue describing your proposed feature.
Sample App: View the MainActivity.java for a full implementation example.
Special thanks to the Paper project for inspiration.
Free to use and modify under the terms of the project's license.
Built with ❤️ by Sambhav Khandelwal