Qt 5.2 Including External File into an Android Package?

user2400925 picture user2400925 · Dec 13, 2013 · Viewed 10.7k times · Source

How to include an external file into 'apk' ?

Example:

There is "123.txt" in the main directory where .pro file exists. What should I add to pro file to put "123.txt" into apk.

I tried DEPLOYMENT, DEPLOYMENTFOLDERS. But they only works with Symbian and Windows CE.

Answer

jwernerny picture jwernerny · Dec 13, 2013

There are two ways to do it, both mentioned under "Porting an Existing Qt Application" on Qt 5.1 Documentation For Android.

  1. Bundle them into a qrc file (works cross platform)
  2. Add them to the "assets:" directory (Android specific)

For #2:

The "assets" directory will be created when you build the project. I have found it easiest to use the "INSTALLS" qmake variable to copy the files into the directory before it is packaged into an apk. The following is from a qmake file for a project I made. Note that for INSTALLS, the path to assets reads "/assets", not "assets" as you would expect. (It actually ends up in a subdirectory of the Android build workspace.)

To access the directory from the code in android, you use "assets:". (In the example, /assets/Samples ==> assets:/Samples.)

# - setup the correct location to install to and load from
android {
    # android platform
    # From: http://community.kde.org/Necessitas/Assets
    SAMPLES_INSTALL_PATH=/assets/Samples
} else {
    # other platforms
    SAMPLES_INSTALL_PATH=$$OUT_PWD/Samples
}

# - setup the 'make install' step
samples.path = $$SAMPLES_INSTALL_PATH
samples.files += $$SAMPLE_FILES
samples.depends += FORCE

INSTALLS += samples