Porting to Android
In this section, we are going to port an existing Qt Application to Android and deploy it to the device.
Most Qt applications should be portable to Android with ease, unless they depend on a specific hardware or software feature not supported by Android. If your application is not using any such feature, deployment is probably the only step that demands some changes to your application.
Like most UI applications, Qt applications also depend on resources such as images, icons, translation files, and so on. These resources must be made available on the device as they are required for the application to function correctly.
The most convenient option is to bundle the resources into a qrc file, which gets built into the application binary. This approach reduces the porting effort considerably and provides faster access to the resources. It is also a cross-platform approach, which makes porting to other platforms easier.
By default, all Qt applications can access the contents of a qrc file using the ":/" prefix or the URL scheme prefix, "qrc:". To know more about qrc files and how they are handled, see the Qt Resource System.
The other approach is to deploy the resources into the package's assets
directory. It is the best option if you want to achieve better interoperability with the Android APIs. You can access all resources in the directory using the "assets:" prefix. Unlike qrc, this approach is not a cross-platform solution.
The following step-by-step instructions guide you to port an existing Qt Quick application to Android using the qrc approach:
- Open the existing project in Qt Creator and configure it with "Android for ARM" kit. For more information, see Qt Creator: Configuring Projects
- Identify all the resources used by your application and add them to one or more qrc files. Qt Creator updates your qmake project file with the "RESOURCES" variable listing the qrc files you added.
- To load or refer to the resources in the qrc file from your C++ code, use the "qrc:" scheme followed by the absolute URL. For example, to load the
main.qml
file fromresources.qrc
, you can use the following C++ code:QQuickView viewer; viewer.setSource(QUrl("qrc:/qml/main.qml")); viewer.show();
Note: QML documents can refer to the contents in qrc files using the relative path to the document. Such references do not require the "
qrc:
" or ":/
" prefix. - Update the "Run" settings for your project as described in the Qt Creator: Specifying Run Settings
Note: You can change the default settings for application icons and identifier.
- If your application uses imports or plugins which depend on special Qt modules, these Qt modules should be added to the .pro file. For example, if your application uses the Qt Multimedia import in QML, you should add the following to your .pro file:
QT += multimedia
- Save the changes to your project and run the application.
Qt Creator deploys your application on the Android device, if the device is detected by the PC. Otherwise, it tries to run the application on an AVD (Android Virtual Device). You will be prompted to create one if there are no AVDs found.
© 2015 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.