Encrypting database with Room createFromAsset() method

Does the SqlCipher support createFromAsset() method?
I have seen topic about using export method for prepopullated database.
I have a prepopulated database which also has to be encrypted, code i have posted below should create database.
After i run this code i cannot access db on app. I’ve got an error:
net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master;
When I download db file from device files and open on db browser it is not encrypted.
Thank in advance for any help.

    final SupportFactory factory = new SupportFactory(key.getBytes());
            myDatabase = Room
                    .databaseBuilder(application, MyDatabase.class, DB_NAME)
                    .openHelperFactory(factory)
                    .createFromAsset("MyDatabase.db")
                    .build();
1 Like

createFromAsset() copies the asset into position to serve as a database. Room handles that on its own — SupportFactory is not involved. Hence, if MyDatabase.db is not encrypted, the result of createFromAsset() should be an unencrypted database, and that will not work with SupportFactory.

I have filed an issue to try to get Room to support this scenario better.

It is theoretically possible that there is a way to craft some sort of hook SQL that can detect this scenario and encrypt the database on first use, but I have not researched how to pull that off.

@commonsguy Thank for a quick response! The library you have made helped us so much.
Anyway if someone will face same problem in the future we have solved it this way:

  1. We have created a database with createFromAsset() method
  2. Then we have encrypted it with export method. There is really useful logic in this library (Utils class) to handle this operation: https://github.com/commonsguy/cwac-saferoom
  3. On the every next run, app uses db init without createFromAsset() logic, but uses openHelperFactory() (provided by SqlCipher) which provides key for encrypted db.