1. Andrew Tipton
  2. android-staticdb


Android library to allow read-only access to sqlite databases contained in an
applications .apk file. This is ideal for apps which need to ship with a large
quantity of static data (e.g. an offline Wikipedia reader).

Due to the way that the SQLiteDatabase class has been implemented in Android,
it cannot be extended or subclassed.  So what we do is register a new "virtual
filesystem" layer with the sqlite library from native C code, which is able to
intercept the filesystem calls that sqlite makes.  (Because there is only one
copy of the sqlite library per VM process, we actually end up intercepting any
sqlite calls made by *any* application in the same process as us!)

In normal operation, our VFS layer simply proxies all calls to the "default"
VFS, which just uses open() and read() to access regular database files.  But
when a special filename matching the "*.apk!filename" pattern is encountered,
our VFS grabs control.  Using zlib and minizip, it opens the .apk file and
looks inside for the database file;  it will then read chunks of this file to
satisfy any read() requests from sqlite.

By doing this in this manner, applications can continue to use the standard
Android database APIs.

Special thanks to Krystian Bigaj for his sqlite-ndk project, which showed me
how to use the sqlite VFS layer.