Skip to content

Commit 3c6b2a4

Browse files
committed
updated docs
1 parent 30ef6e7 commit 3c6b2a4

File tree

1 file changed

+60
-8
lines changed

1 file changed

+60
-8
lines changed

README.md

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,72 @@ Error: file is encrypted or is not a database
4444

4545
### Application Integration
4646

47-
We’ve packaged up a very simple SDK for any Android developer to add SQLCipher into their app with the following three steps:
47+
You have a two main options for using SQLCipher for Android in your app:
4848

49-
1. Add a [reference](https://search.maven.org/search?q=a:android-database-sqlcipher) to the AAR package: `implementation 'net.zetetic:android-database-sqlcipher:Current.Version.Here@aar'`
50-
2. Update the package import path from `android.database.sqlite.*` to `net.sqlcipher.database.*` in any source files that reference it. The original `android.database.Cursor` may still be used unchanged.
51-
3. Initialize the database library and pass a variable argument to the open database method with a password:
49+
- Using it with Room or other consumers of the `androidx.sqlite` API
5250

51+
- Using the native SQLCipher for Android classes
52+
53+
In both cases, you will need to add a dependency on `net.zetetic:android-database-sqlcipher`,
54+
such as having the following line in your module's `build.gradle` `dependencies`
55+
closure:
56+
57+
```gradle
58+
implementation 'net.zetetic:android-database-sqlcipher:4.2.0'
5359
```
54-
// First initilize the native database libraries with the context
55-
SQLiteDatabase.loadLibs(this);
5660

57-
// Request a database connection with passwrod
58-
SQLiteDatabase.openOrCreateDatabase(databasePath.getAbsolutePath(), password, null);
61+
(replacing `4.2.0` with the version you want)
62+
63+
[![Latest version from Maven Central](https://maven-badges.herokuapp.com/maven-central/net.zetetic/android-database-sqlcipher/badge.png)
64+
65+
#### Using SQLCipher for Android With Room
66+
67+
SQLCipher for Android has a `SupportFactory` class in the `net.sqlcipher.database` package
68+
that can be used to configure Room to use SQLCipher for Android.
69+
70+
There are two `SupportFactory` constructors:
71+
72+
- Both take a `byte[]` to use as the passphrase (if you have a `char[]`, use
73+
`SQLiteDatabase.getBytes()` to get a suitable `byte[]` to use)
74+
75+
- One constructor has a second parameter: a `SQLiteDatabaseHook` that you can use
76+
for executing SQL statements before or after the passphrase is used to decrypt
77+
the database
78+
79+
Then, pass your `SupportFactory` to `openHelperFactory()` on your `RoomDatabase.Builder`:
80+
81+
```java
82+
final byte[] passphrase = SQLiteDatabase.getBytes(userEnteredPassphrase);
83+
final SupportFactory factory = new SupportFactory(passphrase);
84+
final SomeDatabase room = Room.databaseBuilder(activity, SomeDatabase.class, DB_NAME)
85+
.openHelperFactory(factory)
86+
.build();
5987
```
6088

89+
Now, Room will make all of its database requests using SQLCipher for Android instead
90+
of the framework copy of SQLCipher.
91+
92+
Note that `SupportFactory` should work with other consumers of the `androidx.sqlite` API;
93+
Room is merely a prominent example.
94+
95+
#### Using SQLCipher for Android's Native API
96+
97+
If you have existing SQLite code using classes like `SQLiteDatabase` and `SQLiteOpenHelper`,
98+
converting your code to use SQLCipher for Android mostly is a three-step process:
99+
100+
1. Replace all `android.database.sqlite.*` `import` statements with ones that
101+
use `net.sqlcipher.database.*` (e.g., convert `android.database.sqlite.SQLiteDatabase`
102+
to `net.sqlcipher.database.SQLiteDatabase`)
103+
104+
2. Before attempting to open a database, call `SQLiteDatabase.loadLibs()`, passing
105+
in a `Context` (e.g., add this to `onCreate()` of your `Application` subclass, using
106+
the `Application` itself as the `Context`)
107+
108+
3. When opening a database (e.g., `SQLiteDatabase.openOrCreateDatabase()`), pass
109+
in the passphrase as a `char[]` or `byte[]`
110+
111+
The rest of your code may not need any changes.
112+
61113
An article covering both integration of SQLCipher into an Android application as well as building the source can be found [here](https://www.zetetic.net/sqlcipher/sqlcipher-for-android/).
62114

63115
### Building

0 commit comments

Comments
 (0)