Tuesday, August 7, 2012

Rebuilding SQLite/SQLCipher for Android on OSX

NOTICE (June 2015): These instructions are completely out-of-date, the following Cordova plugin supports sqlcipher out-of-the-box: https://github.com/litehelpers/Cordova-sqlcipher-adapter

UPDATE (OLD): please see the updated directions for both OSX and Linux on my new blog.

From a previous post I have already built and run the Cordova SQLitePlugin with SQLCipher. The next task is to rebuild SQLCipher for Android from source. Note that the android-database-sqlcipher project repository includes the sub-repository and tasks to rebuild a special version of the sqlite C library itself.

From SQLCipher for Android the first task is to install the Android NDK (assuming that Android SDK and Java have been installed). Their directions tell you to install the Android ndk-build in a special directory and add it to your path. However, for OSX I believe there is an easy way if you are using Homebrew.

The assumption is that you have already installed the Android SDK using Homebrew. If not, make sure Homebrew is installed then install the Android SDK using:
$ brew install android (see my Notes on Running Cordova/PhoneGap on Android without Eclipse posting).

To install the Android NDK, simply: $ brew install android-ndk

The next step is to follow the directions for SQLCipher for Android to rebuild SQLCipher for Android from source. I made a special subdirectory and cloned the android-database-sqlcipher project like:
$ git clone git://github.com/sqlcipher/android-database-sqlcipher.git

Inside the android-database-sqlcipher directory the next step is to do
$ make init to update/init the git submodules and run an android update project command.

To start the build process: $ make

There seems to be an issue with the Makefile that tries to copy libstlport_shared.so from the wrong place. To fix:

$ git diff Makefile
diff --git a/Makefile b/Makefile
index b8067f2..d8b4883 100644
--- a/Makefile
+++ b/Makefile
@@ -42,7 +42,7 @@ copy-libs:
        cp ${JNI_DIR}/libs/armeabi/libdatabase_sqlcipher.so \
                ${LIBRARY_ROOT}/armeabi && \
        cp ${CURDIR}/bin/classes/sqlcipher.jar ${LIBRARY_ROOT} && \
-       cp ${ANDROID_NDK_ROOT}/sources/cxx-stl/stlport/libs/armeabi/libstlport_shared.so \
+       cp ${EXTERNAL_DIR}/libs/armeabi/libstlport_shared.so \
                 ${LIBRARY_ROOT}/armeabi

 copy-libs-dist:

Then to start the build process: $make

The libraries should show up under the libs subdirectory.

To test with a Cordova/PhoneGap project with Cordova-SQLitePlugin: follow the previous post for trying SQLCipher with Cordova-SQLitePlugin but copy the contents of the project libs directory from the newly-built libs subdirectory (and keep cordova-x.x.x.jar but remove commons-codec.jar). The test should now continue be working OK.