Pregunta

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.

¿Fue útil?

Solución

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

Otros consejos

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

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

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.

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.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top