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,, 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

  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 file with the one from the Berkeley DB source tree by doing the following:

    $ cd <root>/external/sqlite/dist
    $ mv
    $ cp ${DB_INSTALL}/build_android/ ./

    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 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/
    $ 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,, in the <root>/external/sqlite/dist directory.

    # 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
    # CFLAGS should be set per (the original 
    # version of SQLite's
    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 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 \

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

    $ mkyaffs2image -f $PWD/system system.img


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