Chapter 4. Building Berkeley DB for Android
Table of Contents
Building the Drop-In Replacement for Android
Migrating from SQLite to Berkeley DB
Building the Android JDBC Driver
Berkeley DB provides support for the Android platform enabling you to develop and deploy a wide range of mobile applications and services. Android provides SQLite as the default database for developing applications that need database support. Berkeley DB SQL API is fully compatible with SQLite and can be used as a replacement. The build_android directory in the Berkeley DB distribution contains a makefile, Android.mk, for building a drop-in replacement for SQLite.
Oracle offers two different solutions for building the BDB SQL API for Android. The first creates a library that can be used as a drop-in replacement for SQLite on Android. The second creates a JDBC driver for Android.
Building the Drop-In Replacement for Android
Migrating from SQLite to Berkeley DB
This section describes how to build a library that can be used as a drop-in replacement for SQLite on Android.
Download and compile the Android source tree.
The compiling process takes time but is a one time activity. For information on downloading and compiling the Android source code, see http://source.android.com/source/download.html.
Copy the Berkeley DB code into the Android build tree.
$ cd <root>/external/sqlite/dist $ tar zxf ${DB_PATH}where <root> is the root of the Android source tree and ${DB_PATH} is the path where you saved the
db-xx.tar.gzversion of the Berkeley DB distribution.Update the Android build file to identify Berkeley DB.
Replace the
Android.mkfile with the one from the Berkeley DB source tree by doing the following:$ cd <root>/external/sqlite/dist $ mv Android.mk Android.mk.sqlite $ cp ${DB_INSTALL}/build_android/Android.mk ./where ${DB_INSTALL} is the directory into which you installed the Berkeley DB library.
Tuning parameters.
The configuration options for performance tuning can be added/edited in the
Android.mkfile by modifyingLOCAL_CFLAGSlocated in thebuild libsqlite replacementsection. For more information, see Android Configuration Options.It is also possible to change these settings using PRAGMA commands or through the DB_CONFIG file.
Build the new Android image.
To build the Android image with Berkeley DB SQL included, do the following:
$ cd <root> $ . build/envsetup.sh $ make clean-libsqlite $ mmm -B external/sqlite/dist $ make snodYou can locate the new image in
<root>/out/target/product/generic.
Migrating from SQLite to Berkeley DB
This section describes how to enable automatic conversion of SQLite format databases to Berkeley DB SQL when they are opened. To do this, you must first make sure that the -DBDBSQL_CONVERT_SQLITE option is added to LOCAL_CFLAGS when you configure your Berkeley DB database build.
Build a static SQLite shell for Android platform.
Create a script, build_sqlite3_shell.sh, in the <root>/external/sqlite/dist directory.
#!/bin/bash # This script shows how to use built-in toolchain to build # sqlite3 shell, which is required by Berkeley DB SQL # on-the-fly migration feature. # Note: these variables should be set per active Android source tree # We assume $PWD=$ROOT/external/sqlite/dist ROOT=${PWD}/../../.. TOOLCHAIN=${ROOT}/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0 CC=${TOOLCHAIN}/bin/arm-eabi-gcc LIB="${ROOT}/out/target/product/generic/obj/lib" INCLUDE="${ROOT}/ndk/build/platforms/android-8/arch-arm/usr/include" # CFLAGS should be set per Android.mk.sqlite (the original # version of SQLite's Android.mk) CFLAGS="-DHAVE_USLEEP=1 -DSQLITE_THREADSAFE=1 -DNDEBUG=1 \ -DSQLITE_DEFAULT_JOURNAL_SIZE_LIMIT=1048576 \ -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1 \ -DSQLITE_DEFAULT_AUTOVACUUM=1 \ -DSQLITE_TEMP_STORE=3 -DSQLITE_ENABLE_FTS3 \ -DSQLITE_ENABLE_FTS3_BACKWARDS -DTHREADSAFE=1" CFLAGS="${CFLAGS} -I${INCLUDE}" LDFLAGS="-ldl -nostdlib -Wl,--gc-sections -lc -llog -lgcc \ -Wl,--no-undefined,-z,nocopyreloc ${LIB}/crtend_android.o \ ${LIB}/crtbegin_dynamic.o -L${LIB} -Wl,-rpath,${LIB}" ${CC} -DANDROID -DOS_ANDROID --sysroot="${SYSROOT}" -mandroid \ -fvisibility=hidden -ffunction-sections -fdata-sections \ -fPIC ${LDFLAGS} ${CFLAGS} \ sqlite3.c shell.c -o sqlite3origEnsure you adjust the variables as per your actual Android environment. This script is suited for Android 2.2.
Execute the build_sqlite3_shell.sh script and to get the static sqlite3 shell utility - sqlite3orig.
Change the system image file.
Use the
xyaffs2utiltiy to decompress thesystem.imgand get the directory system.$ xyaffs2 ./system.img systemAdd static sqlite3 shell utility.
$ cp <root>/external/sqlite/dist/sqlite3orig \ system/xbin/sqlite3origUse the
mkyaffs2imageutility to rebuildsystem.imgfrom the changed directory system.$ mkyaffs2image -f $PWD/system system.imgNote
To open the database in the
SQLiteformat use thesqlite3origcommand.