I would like to use OrmLite with SQLCipher in my Android project, but both libraries have their own abstract SQLiteOpenHelper
class to implement. Java don\'t a
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.
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.
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:
libs/ormlite-android.jar
with ormlite-android-sqlcipher.jarHowever, 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.
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 know this is quite old thread. But I had to go the same way recently. I've read two threads in search for solution: this and this.
And now I have solution that works (you can clone working demo from GitHub). Placing my answer into both threads to help others in the future.