Thursday, September 25, 2014

How to build MySQL plugin for QtAndroid on Windows (step by step tutorial, with sample app)

Prerequisites:

MSYS
MinGW
Android NDK
Cmake
Qt source code
Qt for Android
LibIconv source code
MariaDB Client Library source code


First, you need to download and build LibIconv (details here), or you can download a prebuild version from here.

Download and extract MariaDB Client Library.

Let's assume that we have the following directory structure:
Android NDK in: D:\tools\android\android-ndk-r10b-win
MinGW in: D:\tools\mingw482_32
Cmake in: D:\tools\cmake-2.8
Qt source code in: D:\tools\qtbase-opensource-src-5.3.2
Qt for Android in: D:\tools\Qt5.3.2\android_armv5
LibIconv header files in: D:\temp\android\libiconv-1.14_android\include
LibIconv lib file (libiconv.a) in: D:\temp\android\libiconv-1.14_android\lib
MariaDB Client Library in: D:\temp\android\mariadb_client-2.0.0-src


Go to  MariaDB include subdirectory, D:\temp\android\mariadb_client-2.0.0-src\include .

Open my_global.h with a text editor and add:
 #ifndef ushort  
 #define ushort uint16  
 #endif  

Now open mariadb_client-2.0.0-src\libmariadb\CMakeLists.txt
Change:
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include   
           ${ZLIB_INC}  
           ${CMAKE_SOURCE_DIR}/libmariadb)  
to:
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include   
           ${ZLIB_INC}  
           ${CMAKE_SOURCE_DIR}/libmariadb  
           "/d/temp/android/libiconv-1.14_android/include")  
On line 175, change:
 SET_TARGET_PROPERTIES(libmariadb PROPERTIES VERSION   
   ${CPACK_PACKAGE_VERSION_MAJOR}  
   SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR})  
to:
 #SET_TARGET_PROPERTIES(libmariadb PROPERTIES VERSION   
 #  ${CPACK_PACKAGE_VERSION_MAJOR}  
 #  SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR})  



Open a MSYS console.

Add MinGW and Cmake to PATH:
 export PATH=$PATH:/d/tools/mingw482_32/bin  
 export PATH=$PATH:/d/tools/cmake-2.8/bin  

Set the enviroment variables:
 export ICONV_LIB=/d/temp/android/libiconv-1.14_android/lib/libiconv.a   
 export ANDROID_NDK_ROOT=/d/tools/android/android-ndk-r10b-win   
 export SR="$ANDROID_NDK_ROOT"/platforms/android-12/arch-arm/usr   
 export BR="$ANDROID_NDK_ROOT"/toolchains/arm-linux-androideabi-4.8/prebuilt/windows/bin/arm-linux-androideabi-  

Go to MariaDB directory:
 cd /d/temp/android/mariadb_client-2.0.0-src   

 mkdir build && cd build  

Run cmake:
 cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release \  
 -DCMAKE_AR="$BR"ar.exe \  
 -DCMAKE_C_COMPILER="$BR"gcc.exe \  
 -DCMAKE_C_FLAGS=--sysroot=$SR \  
 -DCMAKE_LINKER="$BR"ld.exe \  
 -DCMAKE_RANLIB="$BR"ranlib.exe \  
 -DCMAKE_STRIP="$BR"strip.exe \  
 -DWITH_EXTERNAL_ZLIB=ON \  
 -DZLIB_INCLUDE_DIR=$SR/usr/include \  
 -DZLIB_LIBRARY=$SR/usr/lib/libz.so \  
 -DICONV_LIBRARIES=$ICONV_LIB \  
 -DCMAKE_SYSTEM_NAME=Linux  

Build:
 make  

Rename the lib:
 mv libmariadb/libmariadbclient.a libmariadb/libmysqlclient.a  


Now, let's build the plugin

 mkdir QtMySqlDriver && cd QtMySqlDriver  

 /d/tools/Qt5.3.2/android_armv5/bin/qmake.exe \  
 /d/tools/qtbase-opensource-src-5.3.2/src/plugins/sqldrivers/mysql/mysql.pro \  
 INCLUDEPATH+="../include ../../include" \  
 LIBPATH+="/d/temp/android/libiconv-1.14_android/lib ../libmariadb" \  
 LIBS+="-lmysqlclient -liconv"  

 make  

Move libqsqlmysql.so into QtInstallDir/plugins/sqldrivers (D:\tools\Qt5.3.2\android_armv5\plugins\sqldrivers).
Now androiddeployqt tool will bundle the plugin with the app.

You can download the prebuild plugin here (libqsqlmysql.7z 544 KB).


Here is a sample app running on Android 2.3:
Downloads:
Sample Android app (TestMYSQL.apk) 6,95 MB
Sample app source code (TestMYSQL-src.7z) 2,36 KB

23 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Use the MinGW version that comes with Qt (/path/to/Qt/Tools/mingw482_32).

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. hi,
    i get the following error message after i run cmake:
    Check for working C compiler: c:/...arm-linux-androideabi-gcc.exe -- broken

    i have no idea what i can do...

    ReplyDelete
  6. Thanks you very much for this article, but have have a error when in use "make" (after cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release \....) :

    "D:/Android/libs/MariaDB/include/my_global.h:76:62: fatal error: pthread.h: No such file or directory
    #include /* AIX must have this included first */"

    Thanks for you help.

    ReplyDelete
    Replies
    1. Before make type:

      echo $SR
      ls "$SR"/*pthread*

      What is the result?

      Delete
    2. Today when trying :
      $ cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_AR="$BR"ar.exe -DCMAKE_C_COMPILER="$BR"gcc.exe -DCMAK
      E_C_FLAGS=--sysroot=$SR -DCMAKE_LINKER="$BR"ld.exe -DCMAKE_RANLIB="$BR"ranlib.exe -DCMAKE_STRIP="$BR"strip.exe -DWITH_E
      XTERNAL_ZLIB=ON -DZLIB_INCLUDE_DIR=$SR/usr/include -DZLIB_LIBRARY=$SR/usr/lib/libz.so -DICONV_LIBRARIES=$ICONV_LIB -DCM
      AKE_SYSTEM_NAME=Linux
      -- Could NOT find Threads (missing: Threads_FOUND)
      -- Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_LIBRARIES) (found version "1.1.0")
      -- Configuring done
      -- Generating done
      -- Build files have been written to: D:/Android/libs/MariaDB

      Delete
  7. $ ls "$SR"/*pthread*
    ls: /d/Android/Android-ndk/platforms/android-12/arch-arm/usr/*pthread*: No such file or directory

    ReplyDelete
    Replies
    1. and
      ls /d/Android/Android-ndk/platforms/
      ?

      Delete
    2. android-12 android-14 android-16 android-18 android-21 android-24 android-4 android-8
      android-13 android-15 android-17 android-19 android-23 android-3 android-5 android-9

      Do you see my message September 16, 2016 at 2:05 PM ?

      Delete
    3. So, in folder:
      /d/Android/Android-ndk/platforms/android-12/arch-arm/usr/
      is there a file pthread.h ?

      Delete
    4. No...
      /d/Android/Android-ndk/platforms/android-12/arch-arm/usr/include, Yes !

      Delete
    5. Look inside android-14 or android-16 (and so on) for that file, and use that platform instead of android-12:

      export SR="$ANDROID_NDK_ROOT"/platforms/android-14/arch-arm/usr

      Delete
    6. All android-xx folders are empty. Only sub-folders 'include' and 'lib'...

      Delete
    7. I understand.. I'm sorry, but I can't help you.
      Try to use android-ndk-r10b-win (the one used in this tutorial).

      Delete
    8. I have download http://dl.google.com/android/ndk/android-ndk32-r10b-windows-x86.zip.

      I unzip to /d/Android/libs/ndk
      I use export ANDROID_NDK_ROOT=/d/Android/libs/ndk

      I have errors during cmake :
      -- Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_LIBRARIES OPENSSL_INCLUDE_DIR)
      -- Could NOT find Threads (missing: Threads_FOUND)
      -- Looking for include file alloca.h
      -- Looking for include file alloca.h - not found
      -- Looking for include file arpa/inet.h
      -- Looking for include file arpa/inet.h - not found
      -- Looking for include file crypt.h
      -- Looking for include file crypt.h - not found
      -- Looking for include file dirent.h
      -- Looking for include file dirent.h - not found
      -- Looking for include file dlfcn.h
      -- Looking for include file dlfcn.h - not found
      -- Looking for include file execinfo.h
      -- Looking for include file execinfo.h - not found
      ...
      ...

      Idea ?

      I really need MySQL plugin Android for my Qt 5.7.0 for my project. it's urgent

      Many thanks.

      Delete
    9. Same problem... I try since 2 days.
      My project is now blocked. :(
      Is not possible de find prebuild plugin for Qt 5.7.0 (android_armv7) ?

      Many thanks.

      Delete
    10. I finally succeeded !
      I had to base myself on the information presented here : http://pavelk.ru/tag/mysql

      For this feature, I had to make changes.
      Bye !

      Delete
    11. could you give me the file compiled to qt 5.7 please

      Delete
  8. This comment has been removed by the author.

    ReplyDelete
  9. whenever i run cmake: than shown cmake:command not found in my MSYS console window

    ReplyDelete