Using ODB on Android

From Code Synthesis Wiki

Jump to: navigation, search

This guide shows how to use ODB and SQLite on Android with Android NDK. Specifically, it shows how to cross-compile the ODB runtime libraries and the hello example. The initial steps for this guide were contributed by Peter Wu.

Note that this guide builds all the libraries as static. It should be possible to use dynamic libraries if you can build the C++ runtime as a dynamic library (by default it is built as static). The guide was tested with Android NDK r8d. Later versions should work as well. Earlier versions may work also.

1. Build the Andriod toolchain as described in the NDK documentation (you can change the installation directory if desired):

   $NDK/build/tools/make-standalone-toolchain.sh --platform=android-8 --install-dir=/usr/local/android-ndk

To make the cross-compiler accessible for later builds, add the bin sub-directory of the installation directory to the PATH environment variable:

   export PATH=/usr/local/android-ndk/bin:$PATH

To test that the compiler is accessible, run the following command:

   arm-linux-androideabi-g++ --version

If you see the version of the compiler, then you have successfully built your Android NDK toolchain.

2. Building the SQLite library (you can change the installation location in --prefix if desired):

   tar xfz sqlite-autoconf-XYZ.tar.gz
   cd sqlite-autoconf-XYZ
   ./configure CFLAGS="-Os -DSQLITE_ENABLE_UNLOCK_NOTIFY=1" --disable-shared --host=arm-linux-androideabi --prefix=/tmp/android
   make
   make install

3. Build the common ODB runtime library (again, change the installation location if desired):

   tar xfz libodb-X.Y.Z.tar.gz
   cd libodb-X.Y.Z
   ./configure CXXFLAGS="-Os" --disable-shared --host=arm-linux-androideabi --prefix=/tmp/android
   make
   make install

4. Build the SQLite ODB runtime library (again, change the installation location if desired; in this case also remember to change the paths in the -I and -L options):

   tar xfz libodb-sqlite-X.Y.Z.tar.gz
   cd libodb-sqlite-X.Y.Z
   ./configure CXXFLAGS="-Os" CPPFLAGS="-I/tmp/android/include" LDFLAGS="-L/tmp/android/lib" --disable-shared --host=arm-linux-androideabi --prefix=/tmp/android
   make
   make install

5. Build the hello example (if you have changed the installation location on the previous steps, remember to also update the paths in the -I and -L options). Add the ODB compiler to your PATH environment variable unless it is already there. Then run:

   tar xfz odb-examples-X.Y.Z.tar.gz
   cd odb-examples-X.Y.Z/hello
   odb -d sqlite --generate-query --generate-schema person.hxx
   arm-linux-androideabi-g++ -I/tmp/android/include -Os -c person-odb.cxx
   arm-linux-androideabi-g++ -I/tmp/android/include -Os -c -DDATABASE_SQLITE driver.cxx
   arm-linux-androideabi-g++ -L/tmp/android/lib -o driver driver.o person-odb.o -lodb-sqlite -lodb -lsqlite3 -ldl

Note that the library order (-lodb-sqlite -lodb -lsqlite3) in the last line is important.

5. Run the hello example on Android:

   adb push driver /data/local/tmp
   ./driver
Personal tools