Skip to content

Commit

Permalink
修复GreenDao数据库在部分高版本系统手机上崩溃的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
LJYcoder committed May 16, 2019
1 parent 9676eb6 commit fe53bd3
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 8 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# DevRing
[![label1](https://img.shields.io/badge/Version-1.1.7-blue.svg)](https://github.com/LJYcoder/DevRing)
[![label1](https://img.shields.io/badge/Version-1.1.8-blue.svg)](https://github.com/LJYcoder/DevRing)
[![label2](https://img.shields.io/badge/License-Apache%202.0-green.svg)](http://www.apache.org/licenses/LICENSE-2.0)
[![label3](https://img.shields.io/badge/API-14%2B-yellow.svg)](https://github.com/LJYcoder/DevRing)
[![label4](https://img.shields.io/badge/Blog-%E7%AE%80%E4%B9%A6-orange.svg)](https://www.jianshu.com/u/2ebe42698573)
Expand All @@ -18,7 +18,7 @@
### 1.添加依赖
在项目module下的gradle中添加以下依赖:
```
compile 'com.ljy.ring:devring:1.1.7'
compile 'com.ljy.ring:devring:1.1.8'
```

### 2.初始化、配置、构建
Expand Down Expand Up @@ -133,6 +133,9 @@ api ('com.ljy.ring:devring:x.x.x'){
<br>

## 版本信息
- v1.1.8 (2019/5/16)
- 修复GreenDao数据库在部分高版本系统手机上崩溃的问题。[问题和解决](https://github.com/greenrobot/greenDAO/issues/428)

- v1.1.7 (2019/4/27)
- 小优化

Expand Down
3 changes: 3 additions & 0 deletions VERSION.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 版本信息
- v1.1.8 (2019/5/16)
- 修复GreenDao数据库在部分高版本系统手机上崩溃的问题。[问题和解决](https://github.com/greenrobot/greenDAO/issues/428)

- v1.1.7 (2019/4/27)
- 小优化

Expand Down
7 changes: 4 additions & 3 deletions devring/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
versionCode 24
versionName "1.1.7"
versionCode 25
versionName "1.1.8"

}

Expand Down Expand Up @@ -53,7 +53,8 @@ dependencies {
annotationProcessor 'com.github.bumptech.glide:compiler:4.4.0'

//greenDAO数据库
api 'org.greenrobot:greendao:3.2.0'
api 'org.greenrobot:greendao:3.2.2'
compileOnly 'net.zetetic:android-database-sqlcipher:3.5.7'//使用加密数据库时需要添加,使用后会apk会变大很多,如果对安全性要求不高,不建议使用

//EventBus事件总线
api 'org.greenrobot:eventbus:3.0.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
/*
* Copyright (C) 2011-2016 Markus Junginger, greenrobot (http://greenrobot.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.ljy.devring.db.support;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.EncryptedDatabase;
import org.greenrobot.greendao.database.StandardDatabase;

/**
* SQLiteOpenHelper to allow working with greenDAO's {@link Database} abstraction to create and update database schemas.
*
* 修复在部分高版本系统手机上运行时报错崩溃
* Rejecting re-init on previously-failed class java.lang.Class<org.greenrobot.greendao.database.DatabaseOpenHelper$EncryptedHelper>:
* java.lang.NoClassDefFoundError: Failed resolution of: Lnet/sqlcipher/database/SQLiteOpenHelper;
* 具体原因及解决方案请查看issue <a>https://github.com/greenrobot/greenDAO/issues/428</a>
*/
public abstract class DatabaseOpenHelperFixed extends SQLiteOpenHelper {

private final Context context;
private final String name;
private final int version;

private EncryptedHelper encryptedHelper;
private boolean loadSQLCipherNativeLibs = true;

public DatabaseOpenHelperFixed(Context context, String name, int version) {
this(context, name, null, version);
}

public DatabaseOpenHelperFixed(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
this.context = context;
this.name = name;
this.version = version;
}

/**
* Flag to load SQLCipher native libs (default: true).
*/
public void setLoadSQLCipherNativeLibs(boolean loadSQLCipherNativeLibs) {
this.loadSQLCipherNativeLibs = loadSQLCipherNativeLibs;
}

/**
* Like {@link #getWritableDatabase()}, but returns a greenDAO abstraction of the database.
* The backing DB is an standard {@link SQLiteDatabase}.
*/
public Database getWritableDb() {
return wrap(getWritableDatabase());
}

/**
* Like {@link #getReadableDatabase()}, but returns a greenDAO abstraction of the database.
* The backing DB is an standard {@link SQLiteDatabase}.
*/
public Database getReadableDb() {
return wrap(getReadableDatabase());
}

protected Database wrap(SQLiteDatabase sqLiteDatabase) {
return new StandardDatabase(sqLiteDatabase);
}

/**
* Delegates to {@link #onCreate(Database)}, which uses greenDAO's database abstraction.
*/
@Override
public void onCreate(SQLiteDatabase db) {
onCreate(wrap(db));
}

/**
* Override this if you do not want to depend on {@link SQLiteDatabase}.
*/
public void onCreate(Database db) {
// Do nothing by default
}

/**
* Delegates to {@link #onUpgrade(Database, int, int)}, which uses greenDAO's database abstraction.
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(wrap(db), oldVersion, newVersion);
}

/**
* Override this if you do not want to depend on {@link SQLiteDatabase}.
*/
public void onUpgrade(Database db, int oldVersion, int newVersion) {
// Do nothing by default
}

/**
* Delegates to {@link #onOpen(Database)}, which uses greenDAO's database abstraction.
*/
@Override
public void onOpen(SQLiteDatabase db) {
onOpen(wrap(db));
}

/**
* Override this if you do not want to depend on {@link SQLiteDatabase}.
*/
public void onOpen(Database db) {
// Do nothing by default
}

private EncryptedHelper checkEncryptedHelper() {
if (encryptedHelper == null) {
encryptedHelper = new EncryptedHelperImpl(context, name, version, loadSQLCipherNativeLibs, this);
}
return encryptedHelper;
}

/**
* Use this to initialize an encrypted SQLCipher database.
*
* @see #onCreate(Database)
* @see #onUpgrade(Database, int, int)
*/
public Database getEncryptedWritableDb(String password) {
EncryptedHelper encryptedHelper = checkEncryptedHelper();
return encryptedHelper.getEncryptedWritableDb(password);
}

/**
* Use this to initialize an encrypted SQLCipher database.
*
* @see #onCreate(Database)
* @see #onUpgrade(Database, int, int)
*/
public Database getEncryptedWritableDb(char[] password) {
EncryptedHelper encryptedHelper = checkEncryptedHelper();
return encryptedHelper.getEncryptedWritableDb(password);
}

/**
* Use this to initialize an encrypted SQLCipher database.
*
* @see #onCreate(Database)
* @see #onUpgrade(Database, int, int)
*/
public Database getEncryptedReadableDb(String password) {
EncryptedHelper encryptedHelper = checkEncryptedHelper();
return encryptedHelper.getEncryptedReadableDb(password);
}

/**
* Use this to initialize an encrypted SQLCipher database.
*
* @see #onCreate(Database)
* @see #onUpgrade(Database, int, int)
*/
public Database getEncryptedReadableDb(char[] password) {
EncryptedHelper encryptedHelper = checkEncryptedHelper();
return encryptedHelper.getEncryptedReadableDb(password);
}

interface EncryptedHelper {
Database getEncryptedReadableDb(String password);
Database getEncryptedReadableDb(char[] password);
Database getEncryptedWritableDb(String password);
Database getEncryptedWritableDb(char[] password);
}

static final class EncryptedHelperImpl extends net.sqlcipher.database.SQLiteOpenHelper implements DatabaseOpenHelperFixed.EncryptedHelper {
private final DatabaseOpenHelperFixed delegate;

EncryptedHelperImpl(Context context, String name, int version, boolean loadLibs, DatabaseOpenHelperFixed delegate) {
super(context, name, null, version);
this.delegate = delegate;
if (loadLibs) {
net.sqlcipher.database.SQLiteDatabase.loadLibs(context);
}
}

@Override
public void onCreate(net.sqlcipher.database.SQLiteDatabase db) {
delegate.onCreate(wrap(db));
}

@Override
public void onUpgrade(net.sqlcipher.database.SQLiteDatabase db, int oldVersion, int newVersion) {
delegate.onUpgrade(wrap(db), oldVersion, newVersion);
}

@Override
public void onOpen(net.sqlcipher.database.SQLiteDatabase db) {
delegate.onOpen(wrap(db));
}

@Override
public Database getEncryptedReadableDb(String password) {
return wrap(getReadableDatabase(password));
}

@Override
public Database getEncryptedReadableDb(char[] password) {
return wrap(getReadableDatabase(password));
}

@Override
public Database getEncryptedWritableDb(String password) {
return wrap(getWritableDatabase(password));
}

@Override
public Database getEncryptedWritableDb(char[] password) {
return wrap(getWritableDatabase(password));
}

private Database wrap(net.sqlcipher.database.SQLiteDatabase sqLiteDatabase) {
return new EncryptedDatabase(sqLiteDatabase);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseOpenHelper;

/**
* author: ljy
* date: 2018/3/10
* description: 用于在数据库更新版本时,对旧数据进行迁移,避免数据丢失
* 20190516更新:继承DatabaseOpenHelperFixed,修复在部分高版本系统手机上运行时报错崩溃
* Rejecting re-init on previously-failed class java.lang.Class<org.greenrobot.greendao.database.DatabaseOpenHelper$EncryptedHelper>:
* java.lang.NoClassDefFoundError: Failed resolution of: Lnet/sqlcipher/database/SQLiteOpenHelper;
* 具体原因及解决方案请查看issue <a>https://github.com/greenrobot/greenDAO/issues/428</a>
*/

public class GreenOpenHelper extends DatabaseOpenHelper {
public class GreenOpenHelper extends DatabaseOpenHelperFixed {

Class<? extends AbstractDao<?, ?>>[] mDaoClasses;

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ KEY_ALIAS=dev_base_test
KEY_PASSWORD=123456
STORE_PASSWORD=123456

VERSION=1.1.7
VERSION=1.1.8
GROUP=com.ljy.ring

SITE_URL=https://github.com/LJYcoder/DevRing
Expand Down

0 comments on commit fe53bd3

Please sign in to comment.