Chapter 4. Building Berkeley DB for Android

Table of Contents

Migrating from SQLite to Berkeley DB
Configuration options

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.

This chapter contains instructions on building Berkeley DB for Android platforms.

Building for Android

This section provides instructions to build Berkeley DB as a drop-in replacement for SQLite on Android.

  1. Download and compile 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.

  2. Copy the Berkeley DB code into the Android build tree.
    $ cd <root>/external/sqlite/dist
    $ tar ${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.gz version of the Berkeley DB distribution.

  3. Update the Android build file to identify Berkeley DB.

    Replace the Android.mk file 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.

  4. Tuning parameters.

    The configuration options for performance tuning can be added/edited in the Android.mk file by modifying LOCAL_CFLAGS located in the build libsqlite replacement section. For more information, see Configuration options.

    It is also possible to change these settings using PRAGMA commands or through the DB_CONFIG file.

  5. 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 snod
    

    You can locate the new image in <root>/out/target/product/generic.

Migrating from SQLite to Berkeley DB

The instructions in this section enable converting SQLite format databases to Berkeley DB SQL automatically when they are opened. Ensure that the -DBDBSQL_CONVERT_SQLITE option is added to LOCAL_CFLAGS.

  1. 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 sqlite3orig
    

    Ensure you adjust the variables as per your actual Android environment. This script is suited for Android 2.2.

  2. Execute the build_sqlite3_shell.sh script and to get the static sqlite3 shell utility - sqlite3orig.
  3. Change the system image file.

    Use the xyaffs2 utiltiy to decompress system.img and get the directory system.

    $ xyaffs2 ./system.img system

    Add static sqlite3 shell utility.

    $ cp <root>/external/sqlite/dist/sqlite3orig \
                      system/xbin/sqlite3orig

    Use the mkyaffs2image utility to rebuild system.img from the changed directory system.

    $ mkyaffs2image -f $PWD/system system.img

    Note

    To open the database in the SQLite format use the sqlite3orig command.