I would like to use ORMLite with SLQCipher. I followed the instruction of ge0rg in this link: How can I use ORMLite with SQLCipher together in Android? but in step 4, I don't know how to add a password, because in the source here: https://github.com/d-tarasov/ormlite-android-sqlcipher/blob/master/src/main/java/com/j256/ormlite/sqlcipher/android/apptools/OrmLiteSqliteOpenHelper.java there are some constructors with config file. I really don't know how to use them. My application throws a NullPointerException at SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:99) Can anyone help me to fix this. Where can I put the password and why I got the NullPointerException?? Thanks!
1 Answers
If you look at the documentation for the parent OrmLiteSqliteOpenHelper class constructor:
public OrmLiteSqliteOpenHelper(android.content.Context context,
String databaseName,
android.database.sqlite.SQLiteDatabase.CursorFactory factory,
int databaseVersion,
int configFileId)
Same as the other constructor with the addition of a file-id of the table config-file.
See OrmLiteConfigUtil for details.
Parameters:
configFileId - file-id which probably should be a R.raw.ormlite_config.txt or some
static value.
It directs you to look at OrmLiteConfigUtil for details. In summary- before Ice Cream Sandwich (Google API 15), calls to annotation methods in Android are very expensive and it was causing folks to see 2-3 seconds startup times when configuring 10-15 DAOs. So they added a utility class which converts your annotations and writes a configuration file. This file can then be loaded into the DaoManager, without any runtime calls to annotations.
So depending on your minSDKVersion, if it's Ice Cream Sandwich (Google API 15) or higher, then you can keep using annotations. Looking at the source, you would just pass in null for the configFile:
public class MyDatabaseHelper extends OrmLiteSqliteOpenHelper {
public static final String DATABASE_NAME = "Example.db";
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_PASSWORD = "Password";
public MyDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION, (InputStream)null, DATABASE_PASSWORD);
}
}
Note: In order to avoid ambiguous constructor issues, be sure to cast null.
And if your minSDKVersion is less than Google API 15 (e.g. Honeycomb), then you should create a configuration file for your classes.
OrmLiteConfigUtil.writeConfigFile("ormlite_config.txt");
And then you would pass R.raw.ormlite_config.txt into the constructor above instead of null.

- 4,083
- 1
- 30
- 27