Qt Creator: include additional files inside Android apk
Full project example here
Basically you have to instruct qmake tool used by Qt Creator to copy specific files inside assets folder during compilation phase. What you have to do is to add the following instruction inside the Qt Creator project file .pro:
android { my_files.path = /assets my_files.files = $$PWD/myfolder/* INSTALLS += my_files }
path is the path where the files will be copied. In case of Android this have to be assets folder but you can add subfolders if you need it. files is the list of files to copy. In the example $$PWD is the root where .pro file is located and using asterisk at the end mean copy entire subfolder structure (files and subfolders). In case you need to specify a list of files located in different paths you can add manually as follow:
my_files.files += $$PWD/path_to_file1/file1 my_files.files += $$PWD/path_to_file2/file2 my_files.files += $$PWD/path_to_file3/file3 ... ...
Once finished compilation you can verify if your files has been added correctly by open the apk file using some archive tool like the free 7-zip software (apk is basically a compressed file containing all required data used by Android). Now you need a way to access these files by code. Qt have a special way to access assets folder as explained in official documentation below:
Qt for Android provides a special, virtual file system which is based on the assets mechanism in Android. Files that are put under assets in the android folder created by Qt Creator, will be packaged as part of your application package. These can be accessed in Qt by prefixing the paths with assets:/. For instance, to access the image logo.png in the folder assets/images, you can use QPixmap("assets:/images/logo.png").
Please note, in case you plan to use assets folder for store sqlite database files you have to know that, unfortunately, you can not use these database directly from assets folder itself. The reason is assets folder is read only but sqlite engine require read/write access also if you plan to use your database file only for read data without any write. In this case you have to copy these files in the app data writable folder as showed in the example below:
QFile DbFile; QString DatabaseDataStoragePath = QStandardPaths::writableLocation(QStandardPaths::StandardLocation::AppDataLocation); DbFile.setFileName("assets:/MyDatabase.sqlite"); DbFile.copy(DatabaseDataStoragePath + "/MyDatabase.sqlite") QFile::setPermissions(DatabaseDataStoragePath + "/MyDatabase.sqlite", QFile::WriteOwner | QFile::ReadOwner);
Once done you can use the database from data folder you just copied to. Be carefully since this "problem" of assets folder have significant consequence to the space occupied by your app cause the space of database files is duplicated as consequence of copy operation (Android calculate app space by sum apk content size of app data folder size).
Comments
Post a Comment