I’ve came across the “hustle” of creating an app that will use Realm, having the database previously populated with data and just include the created database file in the published app, omitting the Json (or any other file) that is being used to populate it.
Doing so for a “to be published” app saves time (parsing the file and populating a db) and space (omitting the file which used to create the database).
I’m going to show the simplest possible way to achieve this, but this can be much improved (a separate Gradle task maybe?) and even be a part of the main project, not having to maintain 2 code bases and 2 different projects for one app.
The project is on Github, but check the “Keep in mind” section first
I am using Android Studio (3.1) and Realm Java version 5.0.0.The build.gradle of the project looks like this:
And the build.gradle of the app module looks like this:
It’s time to add a class that extends android.app.Application, so our whole application starts from this class and Realm gets initialized and configured on OnCreate. It’s handy to set a name for your .realm file containing the database. More configurations can be passed in this way. Just remember to point out this class in the Android Manifest.
Now we need the Json file. A simple one for brevity reasons named “people.json” :
The .json file has to be placed into raw resources in order to be accessible. Create the raw resource folder if it does not exist
The representing Java class for parsing the Json:
Normally, we would use a helper class extending AsyncTask. But thanks to the Realm.executeTransaction we can just this built in method instead.
And a simple button to call .importFromJson()
Keep in mind that calling importFromJson does not delete the database or the data in it, it just adds the results from the Json parsing.
On Android monitor we can view the time it took to complete, and possibly saved from a published app.
Once you run the application to a device, in Android Studio, open “Device File Explorer”, navigate under the “ Data / Data / <Package Name> / files ” directory you can find the .realm file and pull it to your computer.
You can add another button for simplicity to return the number of Persons found in the database. The code of the two simple buttons could look like this:
Now just take the .realm file and place it under raw resources again to be used as the database.
Just put the .realm file in the raw resource directory and copy it to the appropriate place (FilesDir) using copyBundledRealmFile method on App.java .
I added an shouldOverwriteDatabaseOnAppStartup flag if you want to overwrite the database every time the app starts with a fresh one, or just keep the one that copyBundledRealmFile created the first time the app ran:
I hope you found this article useful. The project can be found here https://github.com/chronvas/RealmTesting
Special thanks to Kenneth Geisshirt for reading this and suggesting realm.executeTransaction over AsyncTask