Android Unable to encrypt database using sqlcipher using greendao
Asked Answered
A

1

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");
Alienism answered 9/2, 2017 at 3:37 Comment(2)
Do you have the Gradle plugin in your project classpath ? classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'Cockade
@Cockade when i added this line, I got this duplicate files error like the one mentioned here..... #39936416 how to resolve this?Alienism
M
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

Melchizedek answered 23/2, 2017 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.