How can I use ORMLite with SQLCipher together in Android?

前端 未结 5 2166
轻奢々
轻奢々 2020-12-05 15:12

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

相关标签:
5条回答
  • 2020-12-05 15:33

    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.

    0 讨论(0)
  • 2020-12-05 15:38

    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.

    0 讨论(0)
  • 2020-12-05 15:44

    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.

    0 讨论(0)
  • 2020-12-05 15:45

    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

    0 讨论(0)
  • 2020-12-05 15:51

    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.

    1. I followed ge0rg's answer, it almost worked, were some problems and I have to replace methods inside my code (I wanted to avoid it).
    2. I did what Eliott Roynette suggested here and it worked good except I needed to add one method to Helper class to accept password and I did it.
    3. I wanted to exclude code into lib to have more smart solution instead of code mix, so I build lib/module separately.

    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.

    0 讨论(0)
提交回复
热议问题