jeudi 2 avril 2015

Programmatically extract SQLite .db file from app and bundle it in the same app via Gradle

I am in a bit of a pickle. My SQLiteOpenHelper's onCreate() method takes too long (60+ seconds). To fix this, I need to bundle an already created .db file as an asset. Unfortunately, I have too many Android dependencies in my onCreate() method (Content Values, etc), so I cannot create my .db file on its own without emulating Android.


Here's my plan - create two different SQLiteOpenHelpers - one for the initial database creation that will generate a .db file, and the second openHelper to consume that file. Every end user of my app will only ever use the second openHelper, but my Gradle build script will use the first openHelper.


What type of Gradle logic could be used to accomplish this? The steps would be:



  1. run the first SQLiteOpenHelper to capture the .db file (maybe use Roblectric to run the onCreate() on the JVM)

  2. copy the .db file to my apps /assets directory

  3. Rebuild my app a final time with the newly generated .db file


Is Gradle even capable of solving something like this?



  • Note: I know that this is bad design and I am in circular dependency hell. If I was allowed the time (I am not), I would build the initial .db file as a separate submodule.

  • Note2: Ultimately, I would like to be able to build my app with ./gradlew clean build


Aucun commentaire:

Enregistrer un commentaire