Search code examples
androidgreendaosqlcipher-android

How to integrate Greendao with sqlcipher


I want to integrate Greendao and Sqlcipher in android studio to have an encrypted db. I use sqlcipher-for-android

which I added these to my app/build.gradle

    compile 'org.greenrobot:greendao:3.2.0'
compile 'org.greenrobot:greendao-generator:3.2.0'
compile 'net.zetetic:android-database-sqlcipher:3.5.6@aar'

I downloaded v3.5.6 from Here in the second step I have to execute these commands

% unzip sqlcipher-for-android-v3.5.6.zip
% mkdir -p app/libs
% cp sqlcipher-for-android-v3.5.6/sqlcipher.jar app/libs
% cp -r sqlcipher-for-android-v3.5.6/armeabi \
    sqlcipher-for-android-v3.5.6/armeabi-v7a \
    sqlcipher-for-android-v3.5.6/x86
    sqlcipher-for-android-v3.5.6/x86_64 \
    sqlcipher-for-android-v3.5.6/arm64_v8a app/src/main/jniLibs/

but there is not any sqlcipher.jar, armeabi, armeabi-v7a, x86 and ... in the zip file

Am I missing something or doing wrong ? please help


Solution

  • Comparing this with the example solved the issue

    buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
    }
    }
    
    buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
    }
    }
    
    apply plugin: 'com.android.application'
    apply plugin: 'org.greenrobot.greendao'
    
    android {
    buildToolsVersion '25.0.2'
    compileSdkVersion 25
    
    defaultConfig {
        applicationId "org.greenrobot.greendao.example"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "3"
    
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }
    }
    
    greendao {
    schemaVersion 1000
    }
    
    dependencies {
    compile 'org.greenrobot:greendao:3.2.2'
    
    // This is only needed if you want to use encrypted databases
    compile 'net.zetetic:android-database-sqlcipher:3.5.6'
    
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    }
    
    uploadArchives.enabled = falseapply plugin: 'com.android.application'
    apply plugin: 'org.greenrobot.greendao'
    
    android {
    buildToolsVersion '25.0.2'
    compileSdkVersion 25
    
    defaultConfig {
        applicationId "org.greenrobot.greendao.example"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "3"
    
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }
    }
    
    greendao {
    schemaVersion 1000
    }
    
    dependencies {
    compile 'org.greenrobot:greendao:3.2.2'
    
    // This is only needed if you want to use encrypted databases
    compile 'net.zetetic:android-database-sqlcipher:3.5.6'
    
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    }
    
    uploadArchives.enabled = false
    

    and the below code :

    package org.greenrobot.greendao.example;
    
    import android.app.Application;
    
    import org.greenrobot.greendao.database.Database;
    import org.greenrobot.greendao.example.DaoMaster.DevOpenHelper;
    
    public class App extends Application {
    /** A flag to show how easily you can switch from standard SQLite to the 
    encrypted SQLCipher. */
    public static final boolean ENCRYPTED = true;
    
    private DaoSession daoSession;
    
    @Override
    public void onCreate() {
        super.onCreate();
    
        DevOpenHelper helper = new DevOpenHelper(this, ENCRYPTED ? "notes-db-encrypted" : "notes-db");
        Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
        daoSession = new DaoMaster(db).newSession();
    }
    
    public DaoSession getDaoSession() {
        return daoSession;
    }
    }