SQLCipher for Android Application Integration

The community edition of SQLCipher for Android is distributed via the AAR package format allowing usage in either Java or Kotlin-based Android projects. Integration within Gradle can be performed by adding the following entry to the dependencies section of the app/build.gradle file:

implementation 'net.zetetic:android-database-sqlcipher:4.5.0@aar'
implementation "androidx.sqlite:sqlite:2.0.1"

This tutorial will cover integrating the binaries of SQLCipher for Android into an existing Android application. This tutorial assumes the developer has the latest SQLCipher for Android commercial binaries.

We need to copy the AAR library file over into the app libs directory of our application. Execute the following commands:

% cp android-database-sqlcipher-4.5.0.aar app/libs

Next, modify the project's build.gradle file, adding the following flatDir element:

allProjects {
  repositories {
    flatDir {
      dirs 'libs'


Within the app module build.gradle, add the following to the dependencies block:

implementation (name: 'android-database-sqlcipher-4.5.0', ext: 'aar')
implementation "androidx.sqlite:sqlite:2.0.1"

For Commercial Edition only, the license key may be provided when opening a connection via SQLiteDatabaseHook. Below is an example:

SQLiteDatabaseHook hook = new SQLiteDatabaseHook() {
  public void preKey(SQLiteDatabase database) {
    database.rawExec(String.format("PRAGMA cipher_license = '%s';", LICENSE_KEY));
  public void postKey(SQLiteDatabase database) {}
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databasePath.getAbsolutePath(), password, null, hook);

Next we will modify the source of the activity to properly initialize the native libraries for SQLCipher and then create our database file inserting a record. In particular, note the import of net.sqlcipher.database.SQLiteDatabase instead of android.database.sqlite.SQLiteDatabase as well as the call to SQLiteDatabase.loadLibs(this). The call to SQLiteDatabase.loadLibs(this) must occur before any other database operation. The following is a usage example in Java:

package com.demo.sqlcipher;

import java.io.File;
import net.sqlcipher.database.SQLiteDatabase;
import android.app.Activity;
import android.os.Bundle;

public class HelloSQLCipherActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {

    private void InitializeSQLCipher() {
        File databaseFile = getDatabasePath("demo.db");
        SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFile, "test123", null);
        database.execSQL("create table t1(a, b)");
        database.execSQL("insert into t1(a, b) values(?, ?)", new Object[]{"one for the money",
                                                                        "two for the show"});

SQLCipher for Android can be seamlessly used with Kotlin as well:

package com.demo.sqlcipher

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import net.sqlcipher.database.SQLiteDatabase

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        val databaseFile = getDatabasePath("demo.db")
        if(databaseFile.exists()) databaseFile.delete()
        val database = SQLiteDatabase.openOrCreateDatabase(databaseFile, "test123", null)
        database.execSQL("create table t1(a, b)")
        database.execSQL("insert into t1(a, b) values(?, ?)",
            arrayOf<Any>("one for the money", "two for the show")

The application should now be able to run on either an emulator or device.