第一步:应用库的SQLCLIPER库。
方法一:直接引用官方库。
参考地址:https://www.zetetic.net/sqlcipher/sqlcipher-for-android/
修改build.gradle。packagingOptions { exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/LICENSE' } productFlavors { } repositories { flatDir { dirs 'libs' } } } dependencies { testImplementation 'junit:junit:4.12' compile fileTree(include: ['*.jar'], dir: 'libs') compile 'net.zetetic:android-database-sqlcipher:3.5.9@aar'// //implementation 'com.android.support:appcompat-v7:+' }
方法二:直接下载.JAR包。
下载地址:
https://www.baidu.com/link?url=fBhS3XoOWsh6ttI37CayXngrgPOh-ZqUZxSqH0jNhzcMzL3XLx3BrHwKUcT_CAsUBdm4vBgNnbnRkG15r5X9T_&wd=&eqid=de8bef1d0000304d000000035ade9396
https://download.csdn.net/download/xieqingsheng/10208123
下载引用库:
//-------------------------------------------
第二步:代码编写注意点
当然最好方便实用,将SqlCliper包装起来,方便扩展和兼容。、但是有如下注意事项:
关键要点1:
SQLiteDatabase.loadLibs(context);//必须在之前加载SO库,否则会报错。
否则会报错:SQLiteException not an error at net.sqlcipher.database.SQLiteDatabase.dbopen(Native Method)
以下是节选:
import net.sqlcipher.database.SQLiteDatabase; import net.sqlcipher.database.SQLiteOpenHelper; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; /** * */ public abstract class DbDaoHelper extends SQLiteOpenHelper { private final String TAG="DbDaoHelper"; protected SQLiteDatabase db; protected List<DbDao<?>> daos; public DbDaoHelper(Context context, String dbName, int version) { super(context, dbName, null, version); this.daos=new ArrayList<DbDao<?>>(); //SQLiteDatabase.loadLibs(context); SQLiteDatabase.loadLibs(context); } protected SQLiteDatabase getDb(){ if (db==null) { db = getWritableDatabase(password); } return db; } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { this.db = sqLiteDatabase; createtables(false); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int ver1, int ver2) { this.db = sqLiteDatabase; createtables(true); } public long insert(String Table_Name, ContentValues values) { return getDb().insert(Table_Name, null, values); } public void beginTransaction(){ this.getDb().beginTransaction(); } public void endTransaction(){ this.getDb().endTransaction(); } /** * * @param Table_Name * @param id * @return 影响行数 */ public int delete(String Table_Name, int id) { return getDb().delete(Table_Name, BaseColumns._ID + "=?", new String[] { String.valueOf(id) }); } /** * @param Table_Name * @param values * @param WhereClause * @param whereArgs * @return 影响行数 */ public int update(String Table_Name, ContentValues values, String WhereClause, String[] whereArgs) { return getDb().update(Table_Name, values, WhereClause, whereArgs); } public Cursor query(String Table_Name, String[] columns, String whereStr, String[] whereArgs) { return getDb().query(Table_Name, columns, whereStr, whereArgs, null, null, null); } public Cursor rawQuery(String sql, String[] args) { return getDb().rawQuery(sql, args); } public void execSQL(String sql) { getDb().execSQL(sql); } public void close() { if (db != null) { db.close(); db = null; } }
----------------------
关键要点2:必须关闭游标。
否则报错:
android-SQLCipher -Application did not close the cursor or database object that was opened here
protected List<T> queryList(String sqlstr){
List<T> list =new ArrayList<T>();
Cursor cursor =helper.rawQuery(sqlstr, new String[] {});;//SqlCliper已经被包装了。
if (cursor.getCount()==0) return list;
//循环获得值
cursor.moveToFirst();
do {
T o = this.ConvertForm(cursor);
list.add(o);
}while(cursor.moveToNext());
cursor.close();//关闭cursor;,
return list;
}
OK就绪了。
后一步:再混淆,加壳等,包装利于混淆和使用做SDK等。