9

I am using greendao ORM. I am trying to encrypt my database using SQLCipher. Greendao automativally supports sqlcipher. So I wrote the following code for encryption.

 DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "encrypted-db",null);

        Database db = helper.getEncryptedWritableDb("mySecretPassword");
        DaoSession session = new DaoMaster(db).newSession();
        return session;

However whenever I perform any database operation using this session,it gives an error

 Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/greenrobot/greendao/database/DatabaseOpenHelper$EncryptedHelper;
                                                                       at org.greenrobot.greendao.database.DatabaseOpenHelper.checkEncryptedHelper(DatabaseOpenHelper.java:121)
                                                                       at org.greenrobot.greendao.database.DatabaseOpenHelper.getEncryptedWritableDb(DatabaseOpenHelper.java:133)

My gradle dependencies are->

compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'org.greenrobot:greendao:3.2.0'
    compile 'com.google.code.gson:gson:2.8.0'

My proguard rules are

-keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
public static java.lang.String TABLENAME;
}
-keep class **$Properties
# If you do not use Rx:
-dontwarn rx.**

So how to encrypt my database using greendao and SQLCipher?

PS: Database db = helper.getEncryptedWritableDb("mySecretPassword"); this line generates the error on performing any database operation.

 Database db = helper.getEncryptedWritableDb("mySecretPassword");
Prateek Ratnaker
  • 817
  • 11
  • 35
  • Do you have the Gradle plugin in your project classpath ? [`classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'`](https://github.com/greenrobot/greenDAO#add-greendao-to-your-project) – maxoumime Feb 17 '17 at 22:40
  • @maxoumime when i added this line, I got this duplicate files error like the one mentioned here..... http://stackoverflow.com/questions/39935916/generate-duplicate-class-in-greenado-in-android how to resolve this? – Prateek Ratnaker Feb 19 '17 at 12:34

1 Answers1

2

You also need to add the dependency for SQLCipher. Add this line to your Gradle dependencies:

compile 'net.zetetic:android-database-sqlcipher:3.5.4@aar'

Source: Database Encryption

Carl Poole
  • 1,970
  • 2
  • 23
  • 28