Qt 5.2 Including External File into an Android Package?

11,256

Solution 1

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

Solution 2

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);
}

Solution 3

Specific to User2400925

In QT 5.1 I had used to copy the database from Assets folder to the home folder of the user, if the file does not exist. Which can be used by the App.

You may go through this link

Solution 4

One more simple way to do that:

1) Add this string into your .pro

ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources

2) Create android-sources folder in your proj folder. Put anything you need into android-sources/assets/. You can also put there any other files, such as AndroidManifest.xml or android-sources/res/drawable/icon.png that you want to be copied and updated into the target bundle.

Share:
11,256

Related videos on Youtube

Nejat
Author by

Nejat

A fan of Qt/C++/QML.

Updated on October 19, 2022

Comments

  • Nejat
    Nejat over 1 year

    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.

  • Admin
    Admin over 10 years
    1. I tried it (no success) but then I learned that Qt does not allow using databases bundled with qrc file even in read-only mode.
  • Admin
    Admin over 10 years
    2. Qt 5.2 does not generate an "assets" directory. What should I do?
  • jwernerny
    jwernerny over 10 years
    I've updated the answer with an example of how I have used the assets: directory.
  • Avio
    Avio almost 10 years
    This solution is tested and works on Qt-5.3.0 for Android
  • RamenChef
    RamenChef over 7 years
    This is just copy/pasted from the answer by art926 that was posted more than a year ago.
  • Alexandr Zarubkin
    Alexandr Zarubkin over 5 years
    This solution still works on Qt 5.12.0, while the solution from the previous comment didn't work for me.
  • tanius
    tanius almost 4 years
    Copying to a filesystem folder is indeed required for SQLite database files, even for read-only access. Because Qt hands the file identifier down to the underlying SQLite3 library (see) and that can only handle filesystem paths, not the assets:/ and qrc:/ schemes.
  • tanius
    tanius almost 4 years
    You can access all resources in the directory using the "assets:" prefix. – Yes, with exceptions. The Qt SQLite database driver for example only accepts plain filesystem paths.