Java Android 4.2.2版上的sqlcipher出现Android错误
当我尝试在Android 4.2.2上启动我的项目时,我遇到了一个问题。 以下是stacktrace:Java Android 4.2.2版上的sqlcipher出现Android错误,java,android,android-4.0-ice-cream-sandwich,android-4.2-jelly-bean,sqlcipher,Java,Android,Android 4.0 Ice Cream Sandwich,Android 4.2 Jelly Bean,Sqlcipher,当我尝试在Android 4.2.2上启动我的项目时,我遇到了一个问题。 以下是stacktrace: 08-06 11:00:50.041: E/AndroidRuntime(10606): Caused by: net.sqlcipher.database.SQLiteException: not an error 08-06 11:00:50.041: E/AndroidRuntime(10606): at net.sqlcipher.database.SQLiteDatabase.
08-06 11:00:50.041: E/AndroidRuntime(10606): Caused by: net.sqlcipher.database.SQLiteException: not an error
08-06 11:00:50.041: E/AndroidRuntime(10606): at net.sqlcipher.database.SQLiteDatabase.dbopen(Native Method)
08-06 11:00:50.041: E/AndroidRuntime(10606): at net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.java:1951)
08-06 11:00:50.041: E/AndroidRuntime(10606): at de.greenrobot.dao.wrapper.SQLiteDatabaseWrapper.<init>(SQLiteDatabaseWrapper.java:61)
08-06 11:00:50.041: E/AndroidRuntime(10606): at de.greenrobot.dao.wrapper.SQLiteDatabaseWrapper.openDatabase(SQLiteDatabaseWrapper.java:224)
08-06 11:00:50.041: E/AndroidRuntime(10606): at de.greenrobot.dao.wrapper.SQLiteDatabaseWrapper.openOrCreateDatabase(SQLiteDatabaseWrapper.java:276)
08-06 11:00:50.041: E/AndroidRuntime(10606): at de.greenrobot.dao.wrapper.SQLiteOpenHelperWrapper.getWritableDatabase(SQLiteOpenHelperWrapper.java:95)
08-06 11:00:50.041: E/AndroidRuntime(10606): at com.e_i.bad.utils.DAOManager.DAOInit(DAOManager.java:62)
最后,这就是工作。为此,我只需使用greenDao将所有导入库android.database替换为net.sqlcipher,然后将这个新的greenDao导出到.jar中,我使用sqlcipher.jar,不要忘记从sqlcipher导入commons-codec.jar和guavra-r09.jar,以及文件夹armeabi和文件夹x86,并在文件夹资产文件icudt46l.zip中
在我的例子中,我有一个包含SQLCipher的项目daocore,它是依赖性的,我的项目Android依赖于在Android 4.1及以下版本中工作的daocore项目,但不是在4.2及以上版本中工作的daocore项目,所以问题是项目之间的依赖性,我不知道为什么,但如果你导入源项目中的所有内容,这在所有Android系统中都能工作,那就是了。最后,这就是工作。为此,我只需使用greenDao将所有导入库android.database替换为net.sqlcipher,然后将这个新的greenDao导出到.jar中,我使用sqlcipher.jar,不要忘记从sqlcipher导入commons-codec.jar和guavra-r09.jar,以及文件夹armeabi和文件夹x86,并在文件夹资产文件icudt46l.zip中
在我的例子中,我有一个包含SQLCipher的项目daocore,它是依赖性的,我的项目Android依赖于在Android 4.1及以下版本中工作的daocore项目,但不是在4.2及以上版本中工作的daocore项目,所以问题是项目之间的依赖性,我不知道为什么,但如果您导入源项目中的所有内容,那么所有Android都可以使用。尝试升级到适用于Android 2.2.1的SQLCipher。我尝试过,但这会导致与具有相同堆栈的版本2.2.0相同的错误。如果有助于您理解的话,我可以给出部分代码。在这里发布一些源代码,或者更好地创建一个可复制的测试用例。通过我的编辑,您可以获得代码通过的所有状态。如果您需要更多信息,请告诉我,非常感谢您在这个问题上的考虑和帮助。这似乎非常复杂,没有明显的好处。请尝试升级到适用于Android 2.2.1的SQLCipher。我尝试过,但这让我遇到了与具有相同堆栈的版本2.2.0相同的错误。如果有助于您理解的话,我可以给出部分代码。在这里发布一些源代码,或者更好地创建一个可复制的测试用例。通过我的编辑,您可以获得代码通过的所有状态。如果你需要更多的信息,请告诉我,非常感谢你在这个问题上的考虑和帮助,我非常感激。这看起来非常复杂,没有明显的好处。
helper = new DaoMaster.DevOpenHelper(getContext(), id, password, null);
db = helper.getWritableDatabase();
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
public static class DevOpenHelper extends OpenHelper {
public DevOpenHelper(Context context, String name, String password, CursorFactory factory) {
super(context, name, password, factory);
}
@Override
public void onUpgrade(SQLiteDatabaseWrapper db, int oldVersion, int newVersion) {
dropAllTables(db, true);
onCreate(db);
}
}
public static abstract class OpenHelper extends SQLiteOpenHelperWrapper {
public OpenHelper(Context context, String name, String password, CursorFactory factory) {
super(context, name, password, factory, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabaseWrapper db) {
createAllTables(db, false);
}
}
public synchronized SQLiteDatabaseWrapper getWritableDatabase() {
if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
return mDatabase; // The database is already open for business
}
if (mIsInitializing) {
throw new IllegalStateException("getWritableDatabase called recursively");
}
// If we have a read-only database open, someone could be using it
// (though they shouldn't), which would cause a lock to be held on
// the file, and our attempts to open the database read-write would
// fail waiting for the file lock. To prevent that, we acquire the
// lock on the read-only database, which shuts out other users.
boolean success = false;
SQLiteDatabaseWrapper db = null;
if (mDatabase != null)
mDatabase.lock();
try {
mIsInitializing = true;
if (mName == null) {
db = SQLiteDatabaseWrapper.create(null, mPassword);
} else {
String path = mContext.getDatabasePath(mName).getPath();
File dbPathFile = new File(path);
if (!dbPathFile.exists())
dbPathFile.getParentFile().mkdirs();
db = SQLiteDatabaseWrapper.openOrCreateDatabase(path, mPassword, mFactory);
}
int version = db.getVersion();
if (version != mNewVersion) {
db.beginTransaction();
try {
if (version == 0) {
onCreate(db);
} else {
if (version > mNewVersion) {
Log.w(TAG, "Can't downgrade read-only database from version " + version + " to " + mNewVersion + ": " + db.getPath());
}
onUpgrade(db, version, mNewVersion);
}
db.setVersion(mNewVersion);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
onOpen(db);
success = true;
return db;
} finally {
mIsInitializing = false;
if (success) {
if (mDatabase != null) {
try {
mDatabase.close();
} catch (Exception e) {
}
mDatabase.unlock();
}
mDatabase = db;
} else {
if (mDatabase != null)
mDatabase.unlock();
if (db != null)
db.close();
}
}
}
public SQLiteOpenHelperWrapper(Context context, String name, String password, CursorFactory factory, int version) {
if (version < 1)
throw new IllegalArgumentException("Version must be >= 1, was " + version);
mContext = context;
mName = name;
mPassword = password;
mFactory = factory;
mNewVersion = version;
if (mPassword != null && mPassword.length() > 0) {
// Load SQLcipher libraries if needed
SQLiteDatabaseWrapper.loadLibs(mContext);
}
}
SQLiteDatabase.loadLibs(getContext());
String path = "";
try {
path = getContext().getDatabasePath(pathDB + ".db").getPath();
} catch (NullPreferencesException e) {
}
File dbPathFile = new File(path);
if (!dbPathFile.exists()) {
dbPathFile.getParentFile().mkdirs();
}
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(path, "123456", null);
db = new SQLiteDatabaseWrapper(database);
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();