Qt 5.2 Including External File into an Android Package?

You can use the Qt Resource system. By default, all Qt applications can access the contents of a qrc file using the ":/" prefix or the URL scheme prefix, "qrc:".

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.

When you build your project, a folder named "assets" is created in the Build-Directory/android-build/. After copying your files in the assets directory, you can add these to your pro:

deployment.files += MyFile1
deployment.files += MyFile2
...
deployment.path = /assets
INSTALLS += deployment

The files in assets are readonly. So you should first copy it to some other location if you want to change them:

QFile dfile("assets:/MyFile1");
if (dfile.exists())
{
     dfile.copy("./MyFile1");
     QFile::setPermissions("./MyFile1",QFile::WriteOwner | QFile::ReadOwner);
}

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

Tags:

Qt

Qmake

Apk

Qt5

Qt5.1