tiistai 12. helmikuuta 2013

Running Qt applications in Android

Digia (owner of Qt) is working on official Android and iOS ports of Qt, but meanwhile Qt applications can be built and run in Android using Necessitas, an open source project which forks official Qt SDK. Currently it is still in beta phase but is already usable. This document has been written in early February 2013 so it may not be valid with later Necessitas versions.

Setting up

 Necessitas SDK

Download and install Necessitas SDK from http://necessitas.kde.org/necessitas/necessitas_sdk_installer.php

Launch SDKMaintenanceTool from Necessitas. Select the desired API level from Miscellaneous/Android NDK. You could here also install Android SDK under Necessitas if you don't already have it installed.
Note: While testing API level 10 seemed to work the best. Later versions launched always in AVD even when a device was connected.




Android SDK

Download and install Android SDK from http://developer.android.com/sdk/index.html
Load the Android SDK version you want to support using android_sdk/tools/android tool.



Building

Launch Qt Creator from Necessitas installation using QtCreator/bin/necessitas (not bin/qtcreator!)

Open a project (existing one or create a new). A "Configure Project" dialog is shown.

Set the project as "Necessitas Qt 4.8.2 for Android armv7a" project. You can also select other platforms you might want to build to.

Select from menu Tools | Options and Android page. Set "Android SDK location" to the path you installed Android SDK. If you need to create a Android Virtual Device (emulator) create it now.

Some items were not autoconfigured in my install, so:

Set x86 GDB to android-ndk/toolchains/x86-4.4.3/prebuilt/linux-x86/bin/i686-linux-android-gdb
Set x86 gdbserver to android-ndk/prebuilt/android-x86/gdbserver/gdbserver
Set OpenJDK location to /usr/bin/java

 Click "Ok" to close dialog.
Click on build configuration icon in bottom left corner, above "Run" button. Select the Necessitas build and Debug or Release, which one you prefer.
Click Projects (on the left side bar) -> Run Settings -> Deployment -> Package configurations -> Android target SDK. Change it to the android version you plan to use.
In "Deploy configurations" you can select wheter to include Qt libraries in your package or use Ministro to download libraries on demand. Ministro seemed to work on device but not in AVD. Ministro is a "library downloader" which will install the required Qt libraries to Android so that they can be shared between Qt applications.

Press Ctrl-b to build the application.

Running

Press Ctrl-r to run the application. Application should start in device (if connected) or in AVD.
Sometimes i got error "Found some build errors in current task. Do you want to ignore them?" but ignoring errors worked and the application would run nicely.

Conclusion

The Necessitas SDK is clearly still beta quality, but already can be used to port existing Qt applications to Android or create Android apps from scratch. Source changes are normally not needed at all. Naturally UI's may need adjustment for touch interface if they have been implemented on desktop. Applications built
using Necessitas can be and have been published in Google Play store.