Step 1: Apply the SQLCLIPER library of the library.
Method 1: Directly refer to the official library.
Reference address: https://www.zetetic.net/sqlcipher/sqlcipher-for-android/
Modify 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:+' }
Method 2: Download the .JAR package directly.
download link:
https://www.baidu.com/link?url=fBhS3XoOWsh6ttI37CayXngrgPOh-ZqUZxSqH0jNhzcMzL3XLx3BrHwKUcT_CAsUBdm4vBgNnbnRkG15r5X9T_&wd=&eqid=de8bef1d0000304d000000035ade9396
https://download.csdn.net/download/xieqingsheng/10208123
Download the reference library:
//-------------------------------------------
Step 2: Notes on coding
Of course, it is best to be convenient and practical, wrapping SqlCliper for easy expansion and compatibility. , but there are the following precautions:
Key takeaway 1:
SQLiteDatabase.loadLibs(context);//The SO library must be loaded before, otherwise an error will be reported.
否则会报错:SQLiteException not an error at net.sqlcipher.database.SQLiteDatabase.dbopen(Native Method)
The following are excerpts:
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 affects the number of lines */ 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 affects the number of lines */ 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; } }
----------------------
Key takeaway 2: The cursor must be closed.
Otherwise an error is reported:
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 has been wrapped.
if (cursor.getCount()==0) return list;
//loop to get the value
cursor.moveToFirst();
do {
T o = this.ConvertForm(cursor);
list.add(o);
}while(cursor.moveToNext());
cursor.close();//Close cursor;,
return list;
}
OK and ready.
The next step: re-obfuscation, packing, etc., packaging is conducive to obfuscation and use as SDK, etc.