Faster loading of Qt apps on Android

Edit: In QtCreator 3.1, the method used here (using resource files) will become the default way to perform deployment on all platforms. I.e., this guide is only useful if you have created your project using QtCreator < 3.1.

If your Qt application is taking too long to load on Android, it is likely because accessing asset-files is really slow on some devices. I just tested an application with about 12 QML-files and it took 40(!) seconds to load. This appears to have been an issue for quite some time, and although efforts have been made to reduce the problem, my app still loads too slow on my Asus Transformer TF300T. In Qt and Android’s defence, this device is known for its extremely slow eMMC storage.

The cure in my case was to use a Qt Resource Collection file for all my QML assets. In Qt Creator, this is as simple as clicking  File > New File or Project > Qt > Qt Resource file and then add a prefix “/” before adding all files from each QML folder. After creating and setting up the .qrc-file, I changed the URL in my main-function to “qrc:/qml/myproject/main.qml”.

This worked flawlessly, and the load time dropped from 40 seconds to less than a second!

You should of course be careful with what you add to your resource file. All listed files will be compiled into your executable, so if there are files you don’t want loaded right away, you might want to keep them outside your .qrc file.

The only downside with this approach is adding all new QML files to the resource file. This  is a bit tedious in the long run, so I suppose I will automate it with a script.

 

Published by

Svenn-Arne Dragly

I'm a physicist and programmer, writing about the stuff I figure out as I go.

6 thoughts on “Faster loading of Qt apps on Android”

  1. Hi,
    just to save few kB, after moving files to resources you don’t have to deploy them,
    so you can safty remove
    folder_01.source=….
    folder_01.target=….
    DEPLOYMENTFOLDERS=…..
    from you *.pro file.

  2. Hello!

    I just have a small question related to whats explain before, maybe you can point out if i have something wrong in my logic.

    In my project we add all the assets and the qml to the qrc resources file, since its compiled, its a way to obfuscate the files and avoid someone getting the code.

    So basically, after once my .apk is compiled, and pushed to the phone, it still takes quite long to load and launch the application.

    Is there something wrong???

    1. It depends on what you mean by “quite long”. It typically takes 2-3 seconds to launch a Qt app after it has been installed on my phone (that’s a bit longer than native apps). Make sure that you are actually using the resource URLs and don’t accidentally use the files on your local file system. The url of the main qml file should look something like “qrc://main.qml”.

      Be aware of that compiling your QML file into a resource is a weak form of obfuscation. A tech-savvy person can still get to the code quite easily. There are better ways to obfuscate the code, but if you use obfuscation for security, you should first consider using server-side security instead. Client applications are close to impossible to secure because all system and network calls can be traced.

Leave a Reply