Home

Awesome

Circle CI Release <a target="_blank" href="https://android-arsenal.com/api?level=15"><img src="https://img.shields.io/badge/API-15%2B-orange.svg"></a> GitHub license

I'm archiving this repro as 1) Realm is now deprecated by MongoDB, but equally importantly, 2) this functionality was built into the Realm Android SDK at some stage.

I hope this was useful to a few of you!

Android Realm Asset Helper

A small library of methods to help with Realm.IO integration in Android apps

Copies a realm database from a the assets folder. Efficienty handles versioning of read-only realm databases.

About

Motivated by direct support for read-only databases in iOS app bundles and modelled on ideas in Android SQLite Assset Helper, the goal of this library is to help with some common tasks found with using Realm.io in Android. It is actively maintained and used by Egghead Games for their Andriod & iOS brain puzzle apps.

Features

Simplest Example

In this simplest scenario, the library handles locating and copying the realm file from your apk into the Android file system so it is ready to use.

Say your app has a single Realm database that ships with some sample data. You need to copy that database on first install so it is ready to use in your app. Store your file called, say, appdata.realm in your assets/data folder and include code like this:

import com.eggheadgames.realmassethelper;

Realm realm;

RealmAssetHelper.getInstance(context).loadDatabaseToStorage("data", "appdata", new IRealmAssetHelperStorageListener() {
    @Override
    public void onLoadedToStorage(String realmDbName, RealmAssetHelperStatus status) {
                realmConfig = new RealmConfiguration.Builder(context)
                        .name(realmDbName)
                        .build();
                realm = Realm.getInstance(realmConfig);
            }
    }
});

This will:

Read-only Data Example

Another scenario is where you include a large amount of data to use read-only in your application, such as product catalogue information or game level data.

Say you have the 3rd version of your company's products.realm database to ship with your latest apk. Store it in assets/data with the name products_3.realm, then load it with:

RealmAssetHelper.getInstance(context).loadDatabaseToStorage("data", "products", new IRealmAssetHelperStorageListener() {
    @Override
    public void onLoadedToStorage(String realmDbName, RealmAssetHelperStatus status) {
                realmConfig = new RealmConfiguration.Builder(context)
                        .name(realmDbName)
                        .build();
                realm = Realm.getInstance(realmConfig);
            }
    }
});

This will:

Installation Instructions

Add the JitPack.io repository to your root build.gradle:

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

Add a dependency to your application related build.gradle

dependencies {
    compile 'com.github.eggheadgames:android-realm-asset-helper:2.1.0'
}

Explanation

It can be convenient to have a read-only database as part of an apk. This might have game level information, for example, or other data like zip codes or product information. If the app treats it as "read-only" (perhaps also using a separate realm database for storing other state data), then data updates are as simple as updating the "master" realm file in the apk and then copying it over on first run after the user updates.

iOS note: This is conceptually simpler in iOS, because realm can access read-only data directly from the application bundle (= apk). This library was originally created so that our iOS and Android apps could share the same read-only realm database.

This helper library adds support for this "read-only data included in apk" scenario.

For efficiency, the copy should only be made when the database has changed. This is handled as follows:

Thus, the workflow for an apk with read-only data becomes:

The helper will see the change and copy the database as needed.

Caveats

There is no consideration given (so far) to database migration requirements or any sort of "update my user's existing realm database from this new realm database". That is clearly a useful enhancement to think about for the future but was beyond the scope of the initial release.

Pull Requests, Issues, Feedback

We welcome pull requests. If you have questions or bugs, please open an issue and we'll respond promptly.