Qt 5.2 Including External File into an Android Package?
Asked Answered
H

5

14

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.

Hamitosemitic answered 13/12, 2013 at 18:49 Comment(0)
E
11

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
Engineering answered 13/12, 2013 at 19:28 Comment(5)
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.Hamitosemitic
2. Qt 5.2 does not generate an "assets" directory. What should I do?Hamitosemitic
I've updated the answer with an example of how I have used the assets: directory.Engineering
This solution is tested and works on Qt-5.3.0 for AndroidTeen
This solution still works on Qt 5.12.0, while the solution from the previous comment didn't work for me.Rora
M
8

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);
}
Mota answered 13/5, 2014 at 7:43 Comment(1)
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.Thaine
L
2

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

Lully answered 6/1, 2014 at 7:15 Comment(1)
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.Thaine
O
1

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.

Overcrop answered 16/7, 2015 at 22:21 Comment(0)
A
-2

One more simple way to do that:

  1. Add this string into your .pro

ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources

  1. 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.
Annuitant answered 6/10, 2016 at 12:38 Comment(1)
This is just copy/pasted from the answer by art926 that was posted more than a year ago.Fir

© 2022 - 2024 — McMap. All rights reserved.