Use SqlCipher in android studio
Android studio and eclipse are slightly different.
Android studio 1.3
SqlCipher version 3.3.1
1. Copy sqlcipher. jar to the project folder libs;
Replicated to assets;
The structure is as follows:
3. Load the class
@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ButterKnife. bind (this); SQLiteDatabase. loadLibs (this); // load before using this class, and only load once}
Note that the imported package is: import net. sqlcipher. database. SQLiteDatabase;
4. Pay attention to the details when creating a database
This method cannot open the database
database = SQLiteDatabase.openOrCreateDatabase(data, 123456, null); if (database != null) { database.execSQL(CREATE TABLE IF NOT EXISTS person_student(name VARCHAR(20) NOT NULL , age INT(3))); }
This is acceptable.
File file = context.getDatabasePath(data); file.mkdirs(); database = SQLiteDatabase.openOrCreateDatabase(file, 123456, null); // if (database != null) { database.execSQL(CREATE TABLE IF NOT EXISTS person_student(name VARCHAR(20) NOT NULL , age INT(3))); }
To compare the two methods, you need to manually create a folder.
The usage is basically the same, and its encryption and decryption are all completed internally, which is basically irrelevant to the program we write. The main function is to prevent others from directly viewing the plaintext database through the root permission.