androidandroid-sqliteandroid-roomandroid-room-prepackageddatabase

Prepopulated Android Room database stays empty


I'm using Room version 2.2.6., Android Studio 4.2.2 and writing in Java.

I filled my app's database manually interacting with the app in an emulator, and it shows up both in the app and in the database inspector as one would expect. I built a certain initial example state that I would like my users to have upon first start. I then downloaded the .db .db-shm .db-wal files straight from the device file explorer and saved them in src/main/assets/databases/ as one does.

In my database initialization code I added the line .createFromAsset("databases/Example.db") between the .databaseBuilder and .build calls, to prepopulate my database from those files.

Yet after wiping my local data and reinstalling the app, the database stays empty.

Any clue what direction I could search in? All the posts here I found relating to createFromAsset only contain the same copied paragraph from the documentation.


Solution

  • createFromAsset() assumes that just the .db file is needed.

    So, close the database first before copying. If you have .db-shm and .db-wal files, you did not close the database first.