Getting started with Android on QT — Revisited

2019-05-06

There were a few issues in the previous post on getting setting up QT+Android:

  1. Android SDK had to be downgraded to be compatible with QT (5.6)
  2. QT Android apps would show a white screen if ran inside the emulator without the swiftshader flag set to gpu.
  3. The entire Android Studio was downloaded.

This post revisits the previous documentation on setting up the required build tools with Android and Qt.

Setup

  1. Download the Android SDK.

    You can only donwload the needed command-line tools (147MB zipped). I choose to move the extracted content to /opt

    curl -O https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
    unzip sdk-tools-linux-4333796.zip # unzips to `tool`
    su -c "mkdir /opt/android/"
    su -c "mv tool /opt/android/sdk/"
    
  2. Download the Android NDK

    Direct download:

  3. curl -O https://dl.google.com/android/repository/android-ndk-r19c-linux-x86_64.zip
    unzip android-ndk-r19c-linux-x86_64.zip
    su -c "mkdir /opt/android/ndk"
    su -c "mv android-ndk-r19c /opt/android/ndk"
    
  4. Download Qt (used Qt 5.12.2 at the time of this writing)
  5. download-qt5.12.2-from-website # .run file
    chmod u+x qt5.12.2.run
    ./qt5.12.2.run
    mv-extracted-contents-to /opt/Qt/5.12.2
    
  6. Install java prerequisites.

    If OpenSUSE system, e.g. run (with privileged access)

    zypper in java-1_8_0-openjdk java-1_8_0-openjdk-devel
    
  7. Create and/or download Android images and tools
    sdkmanager --list
    sdkmanager "platform-tools" "platforms;android-25"
    # Download system image files -- it's a big file, might take a while
    sdkmanager "system-images;android-25;google_apis;x86"
    sdkmanager "emulator"
    
  8. Create an android virtual device
    avdmanager create avd \
    -name "insert-device-name-here-with-quotes" \
    --path path/to/device-image \
    -k "<image-name>"
    

    where <image-name> may be e.g. "system-images;android-25;google_apis;x86" Test emulator and make sure it runs

    emulator -avd <created-device-name> -skin 540x960 # (or e.g. 1200x1920)
    

    Optional: spice up the virtual device by adding keyboard support to it

    echo "hw.keyboard=yes" >> /path/to/device-avd-directory/config.ini
    # E.g. path `~/.android/avd/mydevice.avd/config.ini`
    
  9. Setup environment variables. For example,
    export ANDROID_HOME='/opt/android/android-sdk/'
    export ANDROID_SDK='/opt/android/android-sdk/'
    
    # needed for android-...-....json
    export ANDROID_SDK_ROOT='/opt/android/android-sdk/'
    
    export ANDROID_NDK='/opt/android/android-ndk-r19c/'
    export ANDROID_NDK_ROOT='/opt/android/android-ndk-r19c/'
    
    QT_ROOT='/opt/Qt5.12.2/'
    QT_VER='5.12.2'
    
    # Qt
    alias qmake64="${QT_ROOT}/${QT_VER}/gcc_64/bin/qmake"
    alias qmake_andx86="${QT_ROOT}/${QT_VER}/android_x86/bin/qmake"
    alias qmake_arm7="${QT_ROOT}/${QT_VER}/android_armv7/bin/qmake"
    alias qmlscene="${QT_ROOT}/${QT_VER}/gcc_64/bin/qmlscene"
    alias qtcreator="${QT_ROOT}/Tools/QtCreator/bin/qtcreator"
    alias qtdeploy_andx86="${QT_ROOT}/${QT_VER}/android_x86/bin/androiddeployqt"
    alias qtdeploy_armv7="${QT_ROOT}/${QT_VER}/android_armv7/bin/androiddeployqt"
    
    # Needed for running desktop builds
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"${QT_ROOT}/${QT_VER}/${target_platform}/lib/"
    export QML2_IMPORT_PATH="${QT_ROOT}/${QT_VER}/${target_platform}/qml/"
    export QT_PLUGIN_PATH="${QT_ROOT}/${QT_VER}/${target_platform}/plugins/"
    export QT_QPA_PLATFORM_PLUGIN_PATH="${QT_ROOT}/${QT_VER}/${target_platform}/plugins/platforms/"
    
    # Android
    alias avdmanager="${ANDROID_SDK}/tools/bin/avdmanager"
    alias androidemulator="${ANDROID_SDK}/tools/emulator"
    alias adb="${ANDROID_HOME}/platform-tools/adb"
    
    

    Be careful about adding these to your ~/.bashrc if running a QT-based desktop environment like KDE: it may break your system when trying to login.

  10. Build QT application

    Originally I tried using cmake but was greeted with a continuous influx of errors which seemed to emanate from bugs and version incompatiblities. After a few hours I gave up and settled for qmake. It is possible that all the issues are fixed in the latest versions of the toolkits, but I never revisited it.

    Latest toolkits doesn't seem to work with ant anymore and must use gradle instead.

    qmake # (armv7, x86, etc depending on target platform)
    BUILD_DIR=/some/path/for/build/directory
    make
    # will auto create directory if nonexistent
    make install INSTALL_ROOT=$BUILD_DIR
    androiddeployqt --output $BUILD_DIR \
       --input /path/to/qmake-json-output-file.json \
       --android-platform $ANDROID_API_PLATOFRM \
       --verbose \
       --gradle
    

    Look under ANDROID_NDK_ROOT/platforms for valid $ANDROID_API_PLATORM.

    E.g.

    qtdeploy_x86 --output ./build_and86/ \
       --input android-<project-name>.so-deployment-settings.json \
       --android-platform android-24 \
       --verbose \
       --gradle
    

    If running for the first time, it will download and extract gradle to ~/.gradle (but for some reason it won't explicitly tell you this)

    To push APKs to an emulator while the emulator is running, do:

    adb install -r PATH/TO/file.apk