Build Qt 5 MySQL Plugin for Android

From Qt Wiki
Revision as of 09:25, 24 February 2015 by Maintenance script (talk | contribs)

Jump to: navigation, search

[toc align_right="yes" depth="2"]

Overview

Building the Qt5 mysql plugin for Android turned out to not be as straightforward as you may hope. I'll cover the necessary steps to accomplish this below.

Setting up the general environment

Set the following environment variables:

  1. SR=/path/to/android/ndk/platforms/android-9/arch-arm
  2. BR=/path/to/android/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-

NOTE: I have been unable to get openssl to compile on the x86 version of android, hence the ARMv7 variables above. If you are successful at getting openssl to compile with the x86 android, by all means update this wiki :).

Prerequisites

The mysql module requires the following to compile:

  1. The mysql client library. For mariadb (recommended mysql implementation), you also require:
    1. openssl
    2. libiconv

Compiling OpenSSL

I've tested this with openssl version 1.0.1e. This should work with future versions as well, but I can't guarantee that.

First, grab the sources and unpack them. Then, enter the source directory and do the following:

RANLIB=&quot;$BR&amp;quot;ranlib CC=&quot;$BR&amp;quot;gcc ./Configure android-armv7 prefix=$SR/usr<br />ANDROID_DEV=$SR/usr make<br />make install

Note: There should be a way to get openssl to compile and install only the library, but I haven't found the right make targets for that yet. The above will make and install everything for openssl rather than just the library.

To install only the binary files use
make install_sw<code>

== Compiling libiconv ==

I've tested this with libiconv version 1.14. Again, this should work with future versions, but I can't guarantee as much.

First, grab the sources and unpack them. Then, enter the source directory and do the following:
STRIP="$BR&quot;strip RANLIB="$BR&quot;ranlib OBJDUMP="$BR&quot;objdump AR="$BR&quot;ar CC="$BR&quot;gcc CFLAGS=—sysroot=$SR CPP="$BR&quot;cpp CPPFLAGS=$CFLAGS ./configure —build=x86_64 —host=arm —prefix=$SR/usr —with-sysroot=$SR && make install</code>

Compiling mariadbclient (Option 1)

After testing, I've found that the released sources are not compatible with mysql (as of version 1.0.0). They are missing the mysql_library_init function, which the Qt mysql plugin uses. However, the bazaar sources are compatible with mysql and include this function. Grab the mariadb sources from bazaar, configure, compile, and install the mariadb connector client library as described below:

bzr branch lp:mariadb-native-client<br />cd maria-native-client

The mariadb unit tests will fail and halt the compilation of maria db due to some missing pthread routines. I recommend commenting out the unit tests so they don't block the install. Modify CMakeLists.txt in the base source directory and comment out the following line:

ADD_SUBDIRECTORY(unittest/libmariadb)<code>

UPDATE: As of connector version 2.0.0, mariadb is fully compatible with the released version. If you download the 2.0.0 (or later) connector, you can start from this point. You will not need the bazaar sources, nor will you need to disable the unit tests above.

Now you can configure via cmake:
mkdir build && cd build
PKG_CONFIG_PATH=$SR/usr/lib/pkgconfig cmake -DCMAKE_AR="$BR&quot;ar -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER="$BR&quot;gcc -DCMAKE_C_FLAGS=—sysroot=$SR -DCMAKE_INSTALL_PREFIX=$SR/usr -DCMAKE_LINKER="$BR&quot;ld -DCMAKE_NM="$BR&quot;nm -DCMAKE_OBJCOPY="$BR&quot;objcopy -DCMAKE_OBJDUMP="$BR&quot;objdump -DCMAKE_RANLIB="$BR&quot;ranlib -DCMAKE_STRIP="$BR&quot;strip -DWITH_EXTERNAL_ZLIB=ON -DICONV_INCLUDE_DIR=$SR/usr/include -DICONV_LIBRARIES=$SR/usr/lib/libiconv.a -DZLIB_INCLUDE_DIR=$SR/usr/include -DZLIB_LIBRARY=$SR/usr/lib/libz.so ../
At this point, you'll want to make some minor changes to the sources to make them compile on android. For one, the android compiler does not know what a 'ushort' is. So, define it in include/my_global.h inside the #define _global_h block:
#ifndef ushort
#define ushort uint16
#endif
Now you're ready to compile and install:
make install</code>

Deployment Notes

You will need to add the mariadb shared object library file the list of additional libraries to add to the APK image. To get the exact name of the file needed after compiling the QT mysql plugin use the following command:
&quot;$BR&amp;quot;objdump <s>p libqsqlmysql.so | grep NEEDED<code>
<br />To correctly create a mariadb.so file instead of mariadb.so.1 edit libmariadb/CMakeLists.txt and comment out the block:<br />
SET_TARGET_PROPERTIES(libmariadb PROPERTIES VERSION ${CPACK_PACKAGE_VERSION_MAJOR} SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR})</code>


h2. Compiling mysqlclient (Option 2)
p. Stub for someone to experiment with and update this page
h1. Compiling the Qt mysql plugin
p. You cannot compile the mysql plugin from the configure script for android</s> that appears to be broken as of this writing. You can, however, compile it manually from the sources. Grab the Qt5 sources from git, configure them for android as normal (without the qt-sql-mysql or plugin-sql-mysql flags), make, and install as normal. Then enter the qtbase/src/plugins/sqldrivers/mysql directory and execute the following: