How can I use ORMLite with SQLCipher together in Android?

Applying the patch supplied by ge0rg I to found that queryForAll() throws a NoSuchMethod exception. After some investigation I discovered this is a result of rawQuery returning a net.sqlcipher.Cursor and the cursor returned by getQuery (in AndroidCompiledStatement) being an android.database.Cursor. I simply modified AndoridCompiledStatement's imports to:

import net.sqlcipher.Cursor;

And modified getCursor() to return a android.database.Cursor:

public android.database.Cursor getCursor() { ...

With these changes it appears to work for me. Though I'll need to play about a bit more to be completely sure.


Just to update those who need help on this you can copy the entire android source portion of ormlite into your project and change the import of all reference of android's db package to SQLCipher's version.

Basically you have to change all of the package which matches android.database.sqlite to net.sqlcipher.database

For example, from

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

To

import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;

After that put the db password param to the call of getWriteableDatabase in AndroidConnectionSource.java


I have distilled the answer by Rejinderi into a patch for ORMLite 4.43 and compiled it into a JAR file. To integrate it in your Android project, do the following:

  1. Follow the SQLCipher for Android HOWTO to get SQLCipher into your project
  2. Add ORMLite to your project (see What is a good tutorial for using ORMLite with SQLite and Android)
  3. Replace libs/ormlite-android.jar with ormlite-android-sqlcipher.jar
  4. Add your secret database password to the appropriate DB opening methods

However, you should not trust me to provide an unmanipulated JAR file and follow the build instructions in the patch instead.

EDIT: With the patched library, the calls to getReadableDatabase(), getWritableDatabase() and the OrmLiteSqliteOpenHelper constructor need to be passed the password as additional parameter. If you are using a DB helper, extend it appropriately to pass the password to OrmLiteSqliteOpenHelper.


How can I use ORMLite with SQLCipher together in Android?

It should be possible @Bruno.

One way that should work is to just copy ORMLite's OrmLiteSqliteOpenHelper class into your project, rename it to LocalOrmLiteSqliteOpenHelper or something, and change the base class to be the SQLCipher helper class. I can't believe they didn't rename the class to be SQLCipherSQLiteOpenHelper. (grumble)

public abstract class LocalOrmLiteSqliteOpenHelper
    extends info.guardianproject.database.sqlcipher.SQLiteOpenHelper {

Another way would be to have your helper extend SQLCipher's SQLiteOpenHelper and then implement the various things you need from OrmLiteSqliteOpenHelper yourself. That would take a bit more work however. ORMLite has to do a little dance with database connections while the database is being created otherwise it goes recursive.

Let me know if either of these work.