SQLite Android Bindings



There are three ways to add the SQLite Android bindings to an application:

  1. By adding a pre-built aar file to the applications Android Studio project.
  2. By building an aar file, then adding it to the applications Android Studio project as in (1).
  3. By adding the SQLite Android bindings source code to and building it along with the other application code.

By default, the SQLite Android bindings support Android API levels 16 and greater (Android versions 4.1 and up). There is also a separate version that supports Android API levels 9 and greater (Android version 2.3 and up). Please note the extra step involved in obtaining the code if you wish to use the version compatible with API level 9.

1. Using a Pre-Built aar File

This is the most straightforward option. An "aar" file is similar to a jar file, except that it may contain both compiled java classes and native code. An aar file for the latest SQLite release usable with API levels 16 and up is available from this page.

There are two steps involved in adding an aar file to an Android Studio project:

  1. Import the module. In Android Studio 2.1 this is accomplished by selecting the "File" -> "New" -> "New Module..." menu and then choosing "Import JAR/AAR Package".
  2. Add a dependency on the new module to the main application module (or to all modules that will use the SQLite Android bindings). In Android Studio 2.1 the dependency may be created using the project structure dialog (select "File" -> "Project Structure...") or by adding code similar to the following to the application modules build.gradle file:
        dependencies {
            // Change "sqlite-android-3130000" to the name of the new module!
            compile project(':sqlite-android-3130000')

A more detailed description of using the steps above to create a very simple applictation is available here.

At time of writing, aar files may only be used directly in Android Studio projects, not projects created using other IDEs (e.g. Eclipse, IntelliJ IDEA). However, an aar is just a zip archive containing a classes.jar file that in turn contains the SQLite Android binding java classes and a jni/ directory that contains the native library for each platform. By extracting these two things from the aar file and adding them to the project separately it is often possible to use an aar file in non-Android Studio projects.

2. Building a Custom aar File

Building a custom aar file requires both the Android SDK and NDK.

  1. Obtain the code. The code for the SQLite Android bindings may be obtained either by checking out the fossil repository, or by downloading a zip file.

    To obtain the code using fossil, use the following series of commands. In this case, the "project directory" refered to in subsequent steps is the sqlite directory created by the second command below:

        $ fossil clone http://www.sqlite.org/android android.fossil
        $ mkdir sqlite
        $ cd sqlite
        $ fossil open ../android.fossil

    Alternatively, the latest code may be downloaded as a zip archive. In this case, the "project directory" is the SQLite_Android_Bindings/ directory created by unzipping the downloaded archive.

    API level 9-15 users: The code for the version that is compatible with Android API level 9 and greater may be obtained as a zip file from here. Or, if using fossil, the fossil open command above should be replaced with:

        $ fossil open ../android.fossil api-level-9

  2. Configure the native libraries.

    The latest release of the public domain SQLite library is bundled with the SQLite Android bindings code downloaded in step 1. If you wish to use a different version of SQLite, for example one that contains the proprietry SEE extension, then replace the sqlite3.c and sqlite3.h files at the following locations:


    By default, SQLite is built with the following options:

    To build the SQLite library with some other combination of command line switches, edit the Android.mk file at this location:

  3. Build and Assemble the aar file. To assemble the aar file using the command line, first set environment variable ANDROID_HOME to the SDK directory, then run the gradle "assembleRelease" target from within the "sqlite3" sub-directory of the project directory. For example:
        $ export ANDROID_HOME=~/Android/Sdk/
        $ cd sqlite3
        $ ../gradlew assembleRelease

    Assembling an aar file using Android Studio is similar. Open the SQLite Android bindings project using Android Studio, run a "gradle sync", then run the "assembleRelease" gradle task within the "sqlite3" module.

    Using either the command line or Android Studio to run the gradle task causes the aar file to be created at: sqlite3/build/outputs/aar/sqlite3-release.aar.

    Once the custom aar file has been created, it may be used in an Android Studio application as described above. The aar file should be roughly 3.2MB in size. If it is much smaller than this (closer to 100KB), this indicates that the aar file is missing the native libraries for one reason or another. Consult the build logs.

    If the Android.mk file described in step 2 above is edited after a build has been run, it may be necessary to run the gradle "clean" target (either with ../gradlew clean or through Android Studio) before rebuilding the aar file to ensure a correct build.

3. Adding Source Code Directly to the Application

The SQLite Android bindings code may also be added directly to the application project, so that it is built and deployed in the same way as all other application code.

To copy the SQLite Android bindings code into an application:

  1. Obtain the code in the same way as described above.
  2. Recursively copy the contents of the sqlite3/main/src/jni/ directory into the application or application modules jni/ directory. Then, from the parent of the jni/ directory, run the ndk-build command, as described here.
  3. Recursively copy the contents of the sqlite3/main/src/java/ directory to whereever the application java code is.