Harbour and Qt on Android part 2

This is the second part of a multi part article on how to build and setup your Harbour/Qt/QtContribs development tools on your workstation.

First test

The first test to have a Harbour program running on Android is built using gtqtc (a GT based on Qt). We need to create one project in Harbour and one project in Qt Creator. In Harbour we will compile our test program as a library (not an executable). In Qt Creator we will modify the default project to include all standard Harbour libs and our program, and calling the Harbour vm from the main c++ function.

Setting up the dirs

Create the directory c:\android\tests to store all the tests. Let’s start creating one subdir test01 and 2 subdirs: test01/harbour and test01/qt where we will store the two parts of the project.

The Harbour program

in tests\test01\harbour dir create app01.prg file:

PROCEDURE main

   setmode( 12, 12 )

   clear screen
   
   ? padc( "Android", 12 )
   ? padc( "test",12)
   ? padc( "application",12 )
   wait "Press a key"

and app01.hbp file:

-hblib
-gtqtc
gtqtc.hbc
app01.prg
  • -hblib
    to compile as a library
  • -gtqtc
    to link GT QTC
  • gtqtc.hbc
    to set defines, variables…
  • app01.prg
    the name of the app to compile
  • Compile the harbour program with:

    hbmk2a app01.hbp
    

    PLEASE NOTE: I’m using hbmk2A (note the A). It is the batch file we copied in part 1.
    Check that file libapp01.a is created.

    The Qt program

    Open Qt Creator of the Qt version you are using with Harbour. Ctrl-n, select Qt Widgets application and press button “Choose…”, use test01 as Name and set the dir C:\android\tests\test01\qt, click Next.

    In kits panel, activate “Android for armeabi-v7a” and deselect “Desktop Qt 5.2.1 MinGW 32bit“. Complete the setup clicking on Next, Next, Finish.

    Now we have to make some changes to the project.

    Delete mainwindow.cpp file (rightclick and Remove file). Delete file mainwindow.ui (under Forms) and mainwindow.h (under Headers).

    Edit main.cpp file:

    #include "hbapi.h"
    #include "hbapigt.h"
    #include "hbvm.h"
    #include "hbgtcore.h"
    
    HB_GT_REQUEST( QTC )
    HB_FUNC_TRANSLATE( request_MAIN, MAIN )
    
    int main(int argc, char *argv[])
    {
       hb_cmdargInit( argc, argv );
       hb_gtSetDefault( "GTQTC" );
       hb_vmInit( HB_TRUE );
       return hb_vmQuit();
    }
    

    Then edit test01.pro file (PLEASE, pay attention to the long line of LIBS !)

    
    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = T02
    TEMPLATE = app
    
    INCLUDEPATH += C:\android\harbour\include
    
    SOURCES += main.cpp
    
    HEADERS  +=
    
    #FORMS    += -
    
    #CONFIG += mobility
    #MOBILITY = 
    
    LIBS += -LC:\android\r9d\sources\cxx-stl\gnu-libstdc++\4.8\libs\armeabi-v7a
    LIBS += -LC:\android\harbour\lib\android\gccarm -Lc:\android\tests\test01\harbour \
            -lapp01 \
            -Wl,--start-group -lhbrdd -lgttrm -lhbrtl -lhbcommon -lrddntx -lrddfpt -lrddnsx -lhbsix -lhbgt -lhbmacro -lhbvm -lhbvmmt -lhbformat -lgtqtc -Wl,--end-group
    
    

    In LIBS we specify:

  • -LC:\android\r9d\sources\cxx-stl\gnu-libstdc++\4.8\libs
    search lib gnustl_shared that is needed by ndk r9d
  • -LC:\android\harbour\lib\android\gccarm
    search libs inside this dir that is the Harbour for android version of harbour
  • -Lc:\android\tests\test01\harbour
    search libs inside this dir that is the dir of our app
  • -lapp01
    link our app as a library
  • -lhbrdd -lgttrm -lhbrtl -lhbcommon -lrddntx -lrddfpt -lrddnsx -lhbsix -lhbgt -lhbmacro -lhbvm -lhbvmmt
    standard Harbour libs
  • -lgtqtc
    add gtqtc lib, the GT system based on Qt
  • IMPORTANT: if we change the .pro file, select menu Build->Run qmake otherwise some changes are not recognized.
    It also happened to me that the first build stopped with an error. Second build recognized the previous error and did an automatic project reset.

    Run the app

    Save all files (File->Save all) then in the left column menu there will be a botton with “test01″ and “Debug”. Clicking on it you may change kit…you have disabled the Windows kit but it is always better to check that the selected kit is Android and Build type Debug. Select Android/debug. Then click on the green “play” button.
    A form with a list of Compatible devices appears. Choose the one you want to test.

    screenshot01

    Harbour and Qt on Android part 1

    PLEASE NOTE: These are instructions on how I setup my dev machine on APRIL 2 2014! I used the directories and the setup I think are better suited for my development!!! If you don’t have a standard or want to follow my next posts please use the same dir tree.

    Prepare the dir tree

    I will put all the stuff related to Harbour on Android inside c:\android directory. This will include standard x86 Harbour, Harbour for android, android SDK and NDK.

    Qt and Java will be installed in default directories so that they will be available systemwide.

    Download and install Java JDK

    Download and install Java JDK as usual (you may already have it on your pc)

    Download and install Qt 5.2

    Go to http://qt-project.org/downloads and download “Qt 5.2.1 for Android (Windows 32-bit, 1.1GB)”.

    This is the version available on april 2, 2014.

    Install it using default dir (c:\qt\qt5.2.1). In “Select Components” open Tools and select MinGW 4.8. As I said this is a system-wide installation of Qt and this compiler will be used to develop windows based programs. In the past I also installed Qt source code but I don’t do anymore…

    Proceed with installation but do not launch Qt Creator at the end.

    Download & unzip SDK and NDK

    Go here https://developer.android.com/sdk/index.html?hl=sk and download ADT bundle (button on the right). It’s a 510 MB zip download.

    On the left menu click on “Download NDK” and download it. It’s another 496 zip download.

    Unzip both files into c:\android and shorten the dir names. I prefer to keep the reference to the version instead of using names like sdk or ndk. For your reference at the end you should have these dirs:

    c:\android\20140321\sdk
    c:\android\r9d\tests
    

    Go to c:\android\20140321 directory and run SDK Manager application. It will take some seconds to start (it’s java) and it will propose to install a couple of programs (Android support Library and Google USB drivers) and upgrade a couple more (depending on the installed ADT).

    Open the Android 4.3 (API18) and select SDK Platform and ARMEABI v7a System Image.

    Proceed to install.

    Download & unzip ant

    Download Apache ant from http://ant.apache.org

    Unzip into c:\android so that you have the following dir tree (versions may change, one level of dir must be removed):

    c:\android\apache-ant-1.9.3

    Setup Qt Creator

    Configure directories

    Start Qt Creator. Menu Tools -> Options.

    On the leftmenu click on “Android”. On the right panel browse and select the folders requested.

    The values should be:

    Android SDK location: C:\android\20140321\sdk
    Android NDK location: C:\android\r9d
    Ant location: C:\android\apache-ant-1.9.3\bin\ant.bat   (it was autodiscovered in my setup)
    JDK location: C:\Program Files\Java\jdk1.7.0_45

    Click on Apply button.

    Configure test devices

    Click on Start Android AVD Manager, it will open a new window. Click on New and crete a new device. I usually create a generic tablet and a phone.

    AVD Name: Tablet
    Device: 10.1" WXGA
    Target: Android 4.3 - API Level 18
    CPU/ABI: ARM (armeabi-v7a)  (autoselected)
    Skin: Skin with dynamic hardware controls
    Check Use Host GPU

     

    AVD Name: Phone
    Device: Nexus S
    Target: Android 4.3 - API Level 18
    CPU/ABI: ARM (armeabi-v7a)  (autoselected)
    Skin: Skin with dynamic hardware controls
    Check Use Host GPU

    It is possible to create specific devices but it can be done later. Close the Android AVD Manager. To make the new devices appear on the list, click the Add button under the Start Android AVD Manager button, and then click Cancel. The devices Tablet and Phone should appear in the list. Click Apply and the Ok to close the configuration window.

    To solve a problem introduced by the new Android SDK, waiting for the new Qt to adapt, it is necessary to add an environment variable to our operating system.

        ANDROID_TARGET_ARCH     default/armeabi-v7a

    Since this env variable must be available to programs running from the gui, I added it in the system environment variables as in this picture (you have to find it in your operating system and language):
    env_variables

    Compile Harbour & Harbour for android

    Clone Harbour source code (see http://www.mozzarellaincarrozza.it/harbour/compiling-harbour-for-qt-from-git/) but clone it in this way:

    c:
    cd \android
    git clone https://github.com/harbour/core.git Harbour

    Check you have this dir tree:

    C:\android\harbour\win-make.exe

    We now have to compile standard Harbour and then Harbour for android.

    First of all we need to set some env variables. I don’t use system-wide env settings so that I can have multiple Harbour and qt versions on the same workstation. I use batch files to set them.

    Save this script into c:\android\harbour with name set_android.bat and run it:

    PATH=c:\qt\qt5.2.1\tools\mingw48_32\bin;C:\qt\qt5.2.1\5.2.1\mingw48_32\bin;C:\android\harbour\bin\win\mingw;c:\bin;%PATH%;
    SET QT_PLUGIN_PATH=C:\Qt\qt5.2.1\5.2.1\mingw48_32\plugins
    SET HB_WITH_QT=C:\qt\qt5.2.1\5.2.1\mingw48_32\include
    rem HB_QT_MAJOR_VER can probably become deprecated in next
    rem Harbour/qtcontribs releases.
    SET HB_QT_MAJOR_VER=5
    

    Then run win-make and wait while standard Harbour is compiled.

    cd \andorid\Harbour
    win-make

    Since this is a Harbour that is used only for android, we may avoid compiling contribs to save time/space…

    When compilation is completed, copy and paste the build_cross_android.bat file listed below and store it in c:\android\harbour. The original script is from Przemek. The script must be updated to point to the right directories.

    Run build_cross_android.bat. The script will compile a cross-compiled version of Harbour for android that is installed in the same tree.

    The script will also create a new script to be used to cross-compile.
    PLEASE NOTE: the script creates file hbmk2a.bat in c:\android\harbour directory. Please copy the file into c:\android\harbour\bin\win\mingw so that it is under PATH.

    rem @echo off
    rem Original version of this script by Przemek
    rem 
    rem Please change directory names
    
    set NDK=C:\android\r9d
    set QT=C:\Qt\Qt5.2.1\5.2.1\android_armv7\include
    rem set HB_INSTALL_PREFIX=C:\android\hb.android
    
    rem set LVL=3
    set LVL=9
    rem set LVL=14
    
    set CPU=arm
    rem set set CPU=x86
    rem set set CPU=mips
    
    rem set CCOMP=4.6
    set CCOMP=4.8
    
    set PLATFORM=android-%LVL%
    set HOST_CPU=windows-x86_64
    
    set SYSROOT=%NDK%\platforms\%PLATFORM%\arch-%CPU%
    
    if "%CPU%" == "arm"  set TCHAIN=arm-linux-androideabi
    if "%CPU%" == "mips" set TCHAIN=mipsel-linux-android
    if "%CPU%" == "x86"  set TCHAIN=x86
    
    set HB_PLATFORM=android
    set HB_COMPILER=gccarm
    set HB_CCPREFIX=%NDK%\toolchains\%TCHAIN%-%CCOMP%\prebuilt\%HOST_CPU%\bin\%TCHAIN%-
    
    set HB_BUILD_3RDEXT=no
    set HB_WITH_QT=%QT%
    
    set HB_USER_CFLAGS=
    set HB_USER_DFLAGS=
    set HB_USER_LDFLAGS=
    set HB_USER_CFLAGS=%HB_USER_CFLAGS% --sysroot=%SYSROOT%
    set HB_USER_DFLAGS=%HB_USER_DFLAGS% --sysroot=%SYSROOT%
    set HB_USER_LDFLAGS=%HB_USER_LDFLAGS% --sysroot=%SYSROOT% -lm
    
    set HB_USER_CFLAGS=%HB_USER_CFLAGS% --sysroot=%SYSROOT% -I%NDK%\sources\cxx-stl\gnu-libstdc++\4.8\include -I%NDK%\sources\cxx-stl\gnu-libstdc++\4.8\libs\armeabi-v7a\include -I%NDK%\platforms\android-9\arch-arm\usr\include
    
    if not "%1" == "clean" goto skip_clean
       win-make %1
       shift
    :skip_clean
    win-make install %1 %2 %3 %4 %5 %6 %7 %8 %9 > log-%PLATFORM% 2> log-%PLATFORM%.err
    
    set HBMK2A=%HB_INSTALL_PREFIX%\bin\hbmk2a.bat
    
    echo @echo off> %HBMK2A%
    echo.>> %HBMK2A%
    echo set HB_WITH_QT=%QT%>> %HBMK2A%
    echo set SYSROOT=%NDK%\platforms\%PLATFORM%\arch-%CPU%>> %HBMK2A%
    echo.>> %HBMK2A%
    echo.>> %HBMK2A%
    echo set HB_PLATFORM=%HB_PLATFORM%>> %HBMK2A%
    echo set HB_COMPILER=%HB_COMPILER%>> %HBMK2A%
    echo set HB_CCPREFIX=%HB_CCPREFIX%>> %HBMK2A%
    echo set HB_INSTALL_PREFIX=%HB_INSTALL_PREFIX%>> %HBMK2A%
    echo.>> %HBMK2A%
    echo set HB_USER_CFLAGS=%HB_USER_CFLAGS%>> %HBMK2A%
    echo set HB_USER_DFLAGS=%HB_USER_DFLAGS%>> %HBMK2A%
    echo set HB_USER_LDFLAGS=%HB_USER_LDFLAGS%>> %HBMK2A%
    echo.>> %HBMK2A%
    echo hbmk2 -plat=android %%1 %%2 %%3 %%4 %%5 %%6 %%7 %%8 %%9>> %HBMK2A%
    

    Compiling Harbour for Qt from GIT

    This is a step-by-step guide to install Harbour and QtContribs from source code.

    Our goal is to create a self-contained setup that can be used on a development workstation alongside different versions of Harbour and Qt.

    In this setup, Harbour is never “installed” but just compiled with proper settings and only environment variables are set.

    Since the nigthly build of Harbour uses an old version of the compiler not compatible with recent Qt, it is necessary to build Harbour ourselves.

    If we want to be able to easily receive the updates from Harbour and QtContribs projects we should use git and svn. Otherwise you can simply download the source zip files and unpack them.
    I suggest to use git and svn to download source code.

    So proceed to install git and svn (TortoiseSVN, from sourceforge).

    Download and install Qt 5.2

    Go to http://qt-project.org/downloads and download “Qt 5.2.1 for Android (Windows 32-bit, 1.1GB)”.

    This is the version available on april 2, 2014. I use the android version since this is a system-wide installation and I will use it for Android development.

    Install it using default dir (c:\qt\qt5.2.1). In “Select Components” open Tools and select MinGW 4.8: this will install the compiler used to build Qt and it is the compiler we will use to compile Harbour to have maximum compatibility. In the past I also installed Qt source code but I don’t do anymore…

    Proceed with installation but do not launch Qt Creator at the end since we don’t need it.

    Build Harbour

    I put all these source code in a directory called cvs in c:
    In this directory I put all the configuration files (see below) and the source dirs.

    From command prompt, go to c:\cvs directory

    c:
    cd \cvs
    git clone https://github.com/harbour/core.git harbour
    

    It takes some time.
    Please note that we specify harbour as last parameter in the git command: it will create a clone of the repository in a directory called harbour. You should have now a c:\cvs\harbour\README.md file.

    You can download the source code as a zip file clicking on the button on the right ( https://github.com/harbour/core.git ), unzipping and move/rename the directories to have the README.md file in the position shown above.

    If you want to create multiple parallel versions of Harbour, for example one for production code and one to test new features, you can clone Harbour changing the last paramenter.

    Now it is time to set the environment. I never do this in a system-wide setting but use a batch file that I call after the Qt version or the specific use of that setup.
    This file sets the environment variables needed to compile and use Harbour and I called it set_qt521.bat.

    PATH=c:\qt\qt5.2.1\tools\mingw48_32\bin;C:\qt\qt5.2.1\5.2.1\mingw48_32\bin;C:\cvs\harbour\bin\win\mingw;c:\bin;%PATH%;
    SET QT_PLUGIN_PATH=C:\Qt\qt5.2.1\5.2.1\mingw48_32\plugins
    SET HB_WITH_QT=C:\qt\qt5.2.1\5.2.1\mingw48_32\include
    rem HB_QT_MAJOR_VER can probably become deprecated in next
    rem Harbour/qtcontribs releases.
    SET HB_QT_MAJOR_VER=5
    

    Let’s examine the dirs of PATH:

  • c:\qt\qt5.2.1\tools\mingw48_32\bin
    c/c++ mingw compiler included in Qt
  • C:\qt\qt5.2.1\5.2.1\mingw48_32\bin
    all the tools of Qt
  • C:\cvs\harbour\bin\win\mingw
    This dir doesn’t exist yet but Harbour build process will copy here all the Harbour executable
  • c:\bin
    is a dir where I put all the tools I use during code development (diffs tools, grep…)
  • QT_PLUGIN_PATH is needed to run programs that use Qt.
    HB_WITH_QT specifies at build time which version of Qt we want to use
    HB_QT_MAJOR_VER specifies which Qt major version we are using. It may be removed in the future

    It is now easy to create copies of this file and adapt the dirs to use other Qt versions or other Harbour dirs.

    Now build Harbour.

    set_qt521.bat
    cd harbour
    win-make clean
    win-make
    

    Harbour will be compiled. It is normal that near the end there are some warnings/errors during compilation of contribs: the build process checks for the presence of supported libraries and skip build if they are not present.

    When compilation is terminated we can check:

    C:\cvs\harbour>harbour --version
    Harbour 3.2.0dev (r1404111536)
    Copyright (c) 1999-2014, http://harbour-project.org/
    

    Compile QtContribs

    With TortoiseSVN do a checkout of the repository, using data as per the following screenshot. If you are behind a firewall, use http instead of svn in the url (http://svn….)
    (To help with copy&paste: svn://svn.code.sf.net/p/qtcontribs/code/trunk C:\cvs\harbour\addons )
    svn-addons
    Click ok and wait for the checkout.

    You now have this file:
    c:\cvs\harbour\addons\qtcontribs.hbp

    You can download the source file zip from Sourceforge, uncompress it, rename the directory and move it to have the file in the position shown above.

    NOTE OF 17 April 2014: Pritpal added some new features but there are some problems compiling them. So please edit qtcontribs.hbp and rem these lines:

    #### hbqt/qtpositioning/hbqtpositioning.hbp
    #### hbqt/qtwebsockets/hbqtwebsockets.hbp
    #### hbqt/qtbluetooth/hbqtbluetooth.hbp
    

    Now start compilation:

    cd addons
    hbmk2 qtcontribs.hbp
    

    When completed you can check running hbide.

    hbide
    

    Opening menu Help -> About Harbour IDE you should get something like this:
    hbide-about

    What’s next

    From now on, when you need to use this version of Harbour with this version of Qt you can run set_qt521.bat and compile your programs. Please note that you can compile programs that don’t use Qt !