0

我想在 android studio 中集成 Greendao 和 Sqlcipher 以获得加密的数据库。我使用sqlcipher-for-android

我将这些添加到我的 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'

我从这里下载了 v3.5.6 在第二步中我必须执行这些命令

% 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/

但 zip 文件中没有任何 sqlcipher.jar、armeabi、armeabi-v7a、x86 和 ...

我错过了什么或做错了吗?请帮忙

4

2 回答 2

1

将此与示例进行比较解决了该问题

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

和下面的代码:

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;
}
}
于 2017-04-15T07:30:57.263 回答
0

要读取和修改数据,我有一个基础存储库

首先是我的实体://Category.java

package Entities;

import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.NotNull;
import org.greenrobot.greendao.annotation.Generated;

@org.greenrobot.greendao.annotation.Entity(
    nameInDb = "Categories"
)

public class Category {

@Id
public Long CategoryId;

@NotNull
public String Name;

public String Description;

@Generated(hash = 1903625692)
public Category(Long CategoryId, @NotNull String Name, String Description) {
this.CategoryId = CategoryId;
this.Name = Name;
this.Description = Description;
}

@Generated(hash = 1150634039)
public Category() {
}

public Long getCategoryId() {
return this.CategoryId;
}

public void setCategoryId(Long CategoryId) {
this.CategoryId = CategoryId;
}

public String getName() {
return this.Name;
}

public void setName(String Name) {
this.Name = Name;
}

public String getDescription() {
return this.Description;
}

public void setDescription(String Description) {
this.Description = Description;
}

}

和 User.java

package Entities;

import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.NotNull;
import org.greenrobot.greendao.annotation.Generated;

@org.greenrobot.greendao.annotation.Entity(
    nameInDb = "Users"
)
public class User {

@Id
public Long UserId;

public String Username;

public String Mobile;

@NotNull
public String UserApiKey;

@NotNull
public String SecretKey;

public String FirstName;

public String LastName;

public boolean IsFirstLogin;

@Generated(hash = 1246565860)
public User(Long UserId, String Username, String Mobile,
    @NotNull String UserApiKey, @NotNull String SecretKey, String FirstName,
    String LastName, boolean IsFirstLogin) {
this.UserId = UserId;
this.Username = Username;
this.Mobile = Mobile;
this.UserApiKey = UserApiKey;
this.SecretKey = SecretKey;
this.FirstName = FirstName;
this.LastName = LastName;
this.IsFirstLogin = IsFirstLogin;
}

@Generated(hash = 586692638)
public User() {
}

public Long getUserId() {
return this.UserId;
}

public void setUserId(Long UserId) {
this.UserId = UserId;
}

public String getUsername() {
return this.Username;
}

public void setUsername(String Username) {
this.Username = Username;
}

public String getMobile() {
return this.Mobile;
}

public void setMobile(String Mobile) {
this.Mobile = Mobile;
}

public String getUserApiKey() {
return this.UserApiKey;
}

public void setUserApiKey(String UserApiKey) {
this.UserApiKey = UserApiKey;
}

public String getSecretKey() {
return this.SecretKey;
}

public void setSecretKey(String SecretKey) {
this.SecretKey = SecretKey;
}

public String getFirstName() {
return this.FirstName;
}

public void setFirstName(String FirstName) {
this.FirstName = FirstName;
}

public String getLastName() {
return this.LastName;
}

public void setLastName(String LastName) {
this.LastName = LastName;
}

public boolean getIsFirstLogin() {
return this.IsFirstLogin;
}

public void setIsFirstLogin(boolean IsFirstLogin) {
this.IsFirstLogin = IsFirstLogin;
}
}

BaseRepository.java

package services;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.query.DeleteQuery;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.ArrayList;
import java.util.List;

import Entities.DaoSession;
import ir.swifttips.fidiboo.MainDbContext;
import Interfaces.IBaseRepository;

/**
 * Created by macintosh on 1/20/18.
 */

public class BaseRepository<T> implements IBaseRepository<T> {


Class<T> type;
public AbstractDao<T, ?> dao;
public BaseRepository(Class<T> _type) {
    type = _type;

    DaoSession daoSession = MainDbContext.getDaoSession();
    dao = (AbstractDao<T, ?>) daoSession.getDao(_type);

}

public Boolean Create(T entity)
{
    dao.insert(entity);
    return true;
}


public void Update(T entity)
{
    dao.update(entity);
}


public boolean Delete(Long id)
{

    DeleteQuery<T> deleteQuery= dao.queryBuilder()
            .where(dao.getPkProperty().eq(id)).buildDelete();

    deleteQuery.executeDeleteWithoutDetachingEntities();
    MainDbContext.getDaoSession().clear();
    return  true;
}

public boolean DeleteAll()
{
    dao.deleteAll();
    return  true;
}

public boolean Delete(WhereCondition cond)
{

    DeleteQuery<T> deleteQuery= dao.queryBuilder()
            .where(cond).buildDelete();

    deleteQuery.executeDeleteWithoutDetachingEntities();
    MainDbContext.getDaoSession().clear();
    return  true;
}

public T FirstOrDefault(Long id)
{
     List<T> entities = dao.queryBuilder()
        .where(dao.getPkProperty().eq(id)).limit(1).list();

     if (entities.size() > 0)
         return  entities.get(0);
     else
         return  null;
}

public boolean Exists(WhereCondition cond)
{
    Long counts = dao.queryBuilder()
            .where(cond).count();

    if (counts > 0)
        return  true;
    else
        return  false;
}

public Long Count(WhereCondition cond)
{
    Long counts = dao.queryBuilder()
            .where(cond).count();

    return  counts;
}

public T FirstOrDefault(WhereCondition... cond)
{
    List<T> lst = new ArrayList<>();
    if (cond.length == 1)
        lst = dao.queryBuilder().where(cond[0]).limit(1).list();
    else
    if (cond.length == 2)
        lst =  dao.queryBuilder().where(cond[0],cond[1]).limit(1).list();
    else
    if (cond.length == 3)
        lst =  dao.queryBuilder().where(cond[0],cond[1],cond[2]).limit(1).list();

    if (lst.size() > 0)
        return  lst.get(0);

    return null;

}

public T LastOrDefault()
{
    List<T> entities = dao.queryBuilder()
            .orderDesc(dao.getPkProperty()).limit(1).list();

    if (entities.size() > 0)
        return  entities.get(0);
    else
        return  null;
}

public List<T> FetchMulti()
{
    return (List<T>)dao.loadAll();
}

public List<T> FetchMulti(WhereCondition... cond)
{
    if (cond.length == 1)
        return dao.queryBuilder().where(cond[0]).list();
    else
    if (cond.length == 2)
        return dao.queryBuilder().where(cond[0],cond[1]).list();
    else
    if (cond.length == 3)
        return dao.queryBuilder().where(cond[0],cond[1],cond[2]).list();
   else
        return dao.loadAll();
}
}

及其界面

package Interfaces;

import org.greenrobot.greendao.query.DeleteQuery;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.List;


/**
 * Created by macintosh on 1/20/18.
 */

public interface IBaseRepository<T> {

 Boolean Create(T entity);

 void Update(T entity);

 boolean Delete(Long id);

 boolean DeleteAll();

 boolean Delete(WhereCondition cond);

 T FirstOrDefault(Long id);

 boolean Exists(WhereCondition cond);

 Long Count(WhereCondition cond);

 T FirstOrDefault(WhereCondition... cond);

 T LastOrDefault();

 List<T> FetchMulti();

 List<T> FetchMulti(WhereCondition... cond);
}

然后我可以在我的服务中访问这些方法CategoryService.java

package services;

import android.content.Context;

import Common.VolleyCallback;
import Entities.Category;
import Entities.DaoSession;
import ir.swifttips.fidiboo.MainDbContext;
import Interfaces.ICategoryService;
import Interfaces.IProductService;
import iOC.ServiceFactory;
import restful.restfulApiInterfaces.IProductApi;
import restful.restfulResponse.ProductCategoryApiResponse;
import viewModels.ProductCategoryViewModel;

public class CategoryService extends BaseRepository<Category>  implements     ICategoryService {

Context context;
public CategoryService() {
    super(Category.class);
    context =  MainDbContext.getAppContext();
}

}

及其界面

package Interfaces;

import Common.VolleyCallback;
import Entities.Category;

public interface ICategoryService extends IBaseRepository<Category> {


}

或 userService.java

package services;
import android.content.Context;
import java.util.List;

import Entities.DaoSession;
import ir.swifttips.fidiboo.MainDbContext;
import Entities.User;
import Entities.UserDao;
import Interfaces.IUserService;

/**
 * Created by macintosh on 12/30/17.
 */
public class UserService extends BaseRepository<User> implements IUserService {

Context context;

public UserService() {
    super(User.class);
    context =  MainDbContext.getAppContext();
}


public User GetUserById_Query(Long id) {

    List<User> users = dao.queryBuilder().where(UserDao.Properties.UserId.eq(id)).list();

    if (users.size() > 0)
        return users.get(0);

    return null;

}

public User GetUserById(Long id) {

    DaoSession daoSession = MainDbContext.getDaoSession();
    User user = daoSession.getUserDao().load(id);
    return user;

}

public boolean Exists(Long id) {

    DaoSession daoSession = MainDbContext.getDaoSession();
    User user = daoSession.getUserDao().load(id);


    if (user == null)
        return false;
    else
        return true;

}
}

及其界面

package Interfaces;

import Entities.User;
import Entities.DaoSession;
/**
 * Created by macintosh on 1/3/18.
 */

public interface IUserService   extends IBaseRepository<User> {

User LoggedinUser();

User GetUserById_Query(Long id);

 User GetUserById(Long id) ;

 boolean Exists(Long id) ;
}
于 2018-09-15T03:54:52.670 回答