Creating the Database Asset
If you are going to use this approach in your app, you will need to get that packaged database from somewhere. And, perhaps you do not own a magnetized needle or a suitably-trained butterfly. Instead, you are going to need to use other approaches to create the database and fill in your starter data.
Build In Android
The original approach used for
PackagedFTS was simple: the author ran the
FTS app and used Device File Explorer to copy the populated database from that app over to
assets/ of the
In other words, you can create your database using one app that you then package into another app.
For example, you could:
- Have your Room entity classes in a library module
- Have your main app reference that library module
- Have a utility app also reference that library module
- Have the utility app contain the code to populate the database from some data sources
You would then run the utility app and copy the database (using Device File Explorer,
adb pull, etc.) to the main app’s
Build By Hand
There are plenty of SQLite client programs available, such as DB Browser for SQLite. You could use one to hand-populate your database.
In this case, you would also need to consider your database schema. Room defines what the tables are and what SQL statements are used to create them and related structures (e.g., indices). If you have enabled automatic exporting of database schemas, then you will have the SQL statements to use.
Build By Script
Another possibility is to write some software that creates and populates your database, but have that software run on your development machine (or perhaps some server), rather than on an Android device. SQLite client APIs are available for many languages. You could:
- Create a standalone command-line program that you run manually
- Create a standalone command-line program that you run via a custom Gradle task
- Create a Gradle plugin that creates the database
- Create the database directly in a custom Gradle task
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.