Resource File¶
Inside the src
directory are a collection of files and an images
directory. One of the files is an XML file called app_resources.gresource.xml
that describes the resources to be included with the application.
The Resource Description¶
The app_resources.gresource.xml
file is an XML resource description file
that will be compiled by the build system to produce the
app_resources.gresource
resource bundle file. This file is then packaged
with the application and can be loaded at run-time.
The XML file itself provides a manifest of the data files to be included in the resource bundle.
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/com/example/app_resources">
<file>images/picture.svg</file>
</gresource>
</gresources>
Two details are important in this description:
- The
gresource
element’sprefix
attribute defines the path to the resources for this application. Everything provided by the resource bundle will be accessible inside the virtual/com/example/app_resources
directory.- The image file we want to include in the resource bundle is specified as a file path inside the
file
element. The path is relative to the location of the resource description file on the workstation’s file system.
When the app_resources.gresource.xml
file is processed by the build system,
the tool used to compile it to a bundle locates the image in the directory
structure:
- app_resources.gresource.xml
- images/
- picture.svg
Because the file is specified as images/picture.svg
in the description, an
images
directory will be included in the virtual directory structure
contained in the app_resources.gresource
resource bundle file.
Accessing Resources¶
Since the resource description included a prefix and the picture.svg
image
is stored in the images
directory, the application needs to combine the
prefix and the path in order to access its data:
/com/example/app_resources/images/picture.svg
The use of a prefix means that an application can load multiple resources file from different producers and, in theory, the paths used to access resources should not overlap. This provides a unified way to access resources via a single mechanism.
Summary¶
In this part of the tutorial we saw that resource description files provide information about the files to be included in a resource bundle, and we looked at how they specify a directory structure for them. More details about this type of file can be found in Application Resources.
Next, we will look at the application’s source code and see how resources are accessed at run-time.