There are three ways to add the SQLite Android bindings to an application:
- By adding a pre-built aar file to the applications Android Studio project.
- By building an aar file, then adding it to the applications Android Studio project as in (1).
- 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 [#obtaincode|obtaining] if you wish to use the version compatible with API level 9.
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:
- 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"
. - 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 modulesbuild.gradle
file: <verbatim> dependencies { // Change "sqlite-android-3130000" to the name of the new module! compile project(':sqlite-android-3130000') }</verbatim>
A more detailed description of using the steps above to create a very simple applictation is [./verysimpleapp.wiki].
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.
Building a custom aar file requires both the Android SDK and NDK.
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:
<verbatim> $ fossil clone http://www.sqlite.org/android android.fossil $ mkdir sqlite $ cd sqlite $ fossil open ../android.fossil</verbatim>
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:
<verbatim>
$ fossil open ../android.fossil api-level-9</verbatim>
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.wiki], then replace the sqlite3.c
and sqlite3.h
files at the following locations:
<verbatim>
sqlite3/src/main/jni/sqlite/sqlite3.c
sqlite3/src/main/jni/sqlite/sqlite3.h</verbatim>
By default, SQLite is built with the following options:
<verbatim>
-DSQLITE_ENABLE_FTS5
-DSQLITE_ENABLE_RTREE
-DSQLITE_ENABLE_JSON1
-DSQLITE_ENABLE_FTS3</verbatim>
To build the SQLite library with some other combination of command line
switches, edit the Android.mk
file at this location:
<verbatim>
sqlite3/src/main/jni/sqlite/Android.mk</verbatim>
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&amp;&#35;45&#59;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.
</ol>
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:
- Obtain the code in the same way as [#obtaincode].
- Recursively copy the contents of the
sqlite3/main/src/jni/
directory into the application or application modulesjni/
directory. Then, from the parent of thejni/
directory, run thendk&amp;amp&#59;amp&amp;&#35;59&#59;&amp;amp&#59;&amp;&#35;35&#59;35&amp;&#35;59&#59;45&amp;amp&#59;&amp;&#35;35&#59;59&amp;&#35;59&#59;build
command, as [#buildnative]. - Recursively copy the contents of the
sqlite3/main/src/java/
directory to whereever the application java code is.