解决sqlcipher从3.5.9升级到4.0.1后,greendao3.2.2编译引起的问题

org.greenrobot.greendao.database.DatabaseOpenHelper中的EncryptedHelper类的构造函数需要修改,增加一个net.sqlcipher.database.SQLiteDatabaseHook类型的参数,这样就将升级的决定权交给了最终用户,这应该算是greendao设计者考虑不周全引起的问题。

由于时间有限,再有就是惰性原因,本人采用了一个简单的方法,直接自定义一个SQLiteOpenHelper子类,代码完全参照org.greenrobot.greendao.database.DatabaseOpenHelper即可。只是将EncryptedHelper部分修改下

private class EncryptedHelperextends net.sqlcipher.database.SQLiteOpenHelper {

public EncryptedHelper(Context context, String name, int version, boolean loadLibs) {

super(context, name, null, version, new SQLiteDatabaseHook() {

@Override

            public void preKey(net.sqlcipher.database.SQLiteDatabase database) {

}

@Override

            public void postKey(net.sqlcipher.database.SQLiteDatabase database) {

database.execSQL("PRAGMA cipher_page_size = 1024");

                database.execSQL("PRAGMA kdf_iter = 64000");

                database.execSQL("PRAGMA cipher_hmac_algorithm = HMAC_SHA1");

                database.execSQL("PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1");

            }

});

        if (loadLibs) {

net.sqlcipher.database.SQLiteDatabase.loadLibs(context);

        }

}

@Override

    public void onCreate(net.sqlcipher.database.SQLiteDatabase db) {

NewDatabaseOpenHelper.this.onCreate(wrap(db));

    }

@Override

    public void onUpgrade(net.sqlcipher.database.SQLiteDatabase db, int oldVersion, int newVersion) {

NewDatabaseOpenHelper.this.onUpgrade(wrap(db), oldVersion, newVersion);

    }

@Override

    public void onOpen(net.sqlcipher.database.SQLiteDatabase db) {

NewDatabaseOpenHelper.this.onOpen(wrap(db));

    }

protected Databasewrap(net.sqlcipher.database.SQLiteDatabase sqLiteDatabase) {

return new EncryptedDatabase(sqLiteDatabase);

    }

}

然后再修改greendao-generator-3.2.2.jar中的模板文件dao-master.ftl将DatabaseOpenHelper替换为自己定义的类。

然后再重新打包 jar -cvf0 greendao-generator-3.2.2.jar  META-INF/MANIFEST.MF ./

替换/Users/用户名/.gradle/caches/modules-2/files-2.1/org.greenrobot/greendao-generator/3.2.2/c1d222f0f518f6d32150c566ef378e0dcba8de1e/greendao-generator-3.2.2.jar即可。

上面的目录在不同的电脑上存在不同,通过 find . -name "greendao-generator-3.2.2.jar" 命令找到自己电脑上路径。

你可能感兴趣的:(解决sqlcipher从3.5.9升级到4.0.1后,greendao3.2.2编译引起的问题)