Declaration of VAR

and some other stuff

Qt for Android - setting up development environment

2017-12-28 21:27:12 +0100

2017-12-28 21:27:12 +0100 | Comments

Android is one of the Qt’s supported target platforms, so you can create applications for Android using Qt.

Here I’ll show you how to set-up Qt development environment for Android.

Actually, the process is already described in the Qt documentation: setting up both the environment and Qt Creator. However (as usual), there are some surprises that are waiting for you down the road, so I decided to complement the official guide with my comments and screenshots.

I recommend you to use the latest version of Qt (5.10 at the moment) and Qt Creator (4.5.0 at the moment), because some annoying Android-related issues were fixed there, and thus everything is easier now.

The process is more or less the same across all the major development hosts (Linux, Mac OS, Windows). I’ll do it for Linux (running inside a virtual machine on my Mac).

First, install Android component from the Qt installer:

I have here HTC Evo 3D (Android 4.0.3), Google Nexus 7 (Android 6.0.1), and Google Pixel C (Android 8.1.0):

…so I chose Android ARMv7.

Download and install Android Studio. I unpacked it to ~/programs/android-studio/. Now run ~/programs/android-studio/bin/studio.sh. It will ask you for an installation path (I chose ~/programs/android/) and then install Android SDK and Android SDK Platform (latest version by default).

If you want to check what’s installed, launch Android Studio again and go to settings:

Here’s what I have:

On Windows you will also need to install Google USB driver:

Now download and unpack Android NDK. I put mine to ~/programs/android-ndk/. Note that at the moment (Qt Creator 4.7.1 and Qt 5.11.1) NDK version r18 won’t work, you should take 17c.

Download and unpack JDK. At the moment there are some problems with JDK 9, so it is recommended to go with JDK 8. I put mine to ~/programs/jdk/.

Check if you have some not accepted licenses:

~/programs/android/tools/bin/sdkmanager --licenses

You might get this error:

ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.

I tried adding JAVA_HOME to environment variables, but it didn’t make any difference, so I simply added path to JRE (~/programs/jdk/jre/bin) to the PATH variable.

Run it again:

~/programs/android/tools/bin/sdkmanager --licenses
5 of 5 SDK package licenses not accepted. 100% Computing updates...             
Review licenses that have not been accepted (y/N)?

and accept the licenses:

...the terms of any applicable Open Source software license agreement.
---------------------------------------
Accept? (y/N): y
All SDK package licenses accepted

If you won’t accept all the licenses, something simply might not work.

So, here are all my paths:

  • Android Studio: ~/programs/android-studio/ (although, this path is not needed);
  • Android SDK: ~/programs/android/;
  • Android NDK: ~/programs/android-ndk/;
  • JDK: ~/programs/jdk/.

And now you can launch Qt Creator and set them all at the Android tab:

If something is wrong, it will offer you to download missing components:

And if you installed JDK 9, it will fail. I warned you.

If you don’t have Android tab in the Qt Creator settings, check if you have Android plugin enabled:

This part is done.

Now enable Developer mode (and USB debugging) on your Android device and connect it to your PC. You can check the connection by running:

~/programs/android/platform-tools/adb devices

I connected all 3 devices, so here’s my output:

List of devices attached
HT22VV208818    device
5A28001752      device
07ab2aea        device

On Linux you might see something like this, though:

List of devices attached
5A28001752    no permissions (user in plugdev group; are your udev rules wrong?); see [http://developer.android.com/tools/device.html]

On Windows it will be just:

List of devices attached
5A28001752      unauthorized

Or, which is more confusing, you might get ABI is incompatible error while trying to deploy your application:

Both issues (no permissions/unauthorized and incompatible ABI) most probably indicate that you didn’t allow your PC to debug your Android device via USB. It’s not necessarily your fault, especially if you (like me) never got this request in the first place. In order for your Android device to show you debugging request, you need to restart ADB daemon (while your device is connected to the PC):

sudo ~/programs/android/platform-tools/adb kill-server
sudo ~/programs/android/platform-tools/adb start-server
~/programs/android/platform-tools/adb devices

Now you should get a dialog like this on your device:

Information from this page also tells to install Android adb tools package. I’m not sure how much use does it bring since we already have it from Android Studio, but okay:

apt-get install android-tools-adb

Anyway, once you allow the debugging request, your device will be recognized by ADB (and Qt Creator deployment dialog).

Time to build and run some application on your Android device. I will use the Color Corners example again, which is not an Android application by design, but it’s a cross-platform framework we are dealing here with, so why the hell not.

If everything was set-up correctly, you should be able to see the Android build SDK version in your project build settings:

You can also add Android manifest and other templates:

That will allow you to specify the application name, icon, permissions and other stuff (but don’t use spaces, otherwise your application will fail to launch saying that it couldn’t find a library):

Now you can just run your application from Qt Creator:

And here we say “sorry” to HTC Evo 3D, because it has a really old Android API (15):

So we’ll launch our application only on Nexus 7 and Pixel C targets:

As you can see, our application was successfully built for Android platform, so it is an .apk file, which can be either run on the connected device from Qt Creator, or you can copy it to the device manually and install it there.