restool Tool
Note:
Currently in the beta phase.
Introduction
restool is an application engineering resource compilation tool that creates resource indexes and parses resources by compiling resource files. Developers can access corresponding resources by calling the Resource Management Interface. The restool tool is located in the toolchains subdirectory of the SDK installation directory.
Parameter Description
The restool currently supports the following command options:
| Option | Optional | With Parameter | Description |
|---|---|---|---|
| -i | Required | With parameter | Specifies the resource directory to be compiled or the resource middleware to be built. This command can be specified multiple times in the same command. For details, refer to Compile Resource Command. |
| -j | Required | With parameter | Specifies the path of the config.json or module.json file. |
| -o | Required | With parameter | Specifies the output path for compiled resources. |
| -p | Required | With parameter | Specifies the bundle name of the compiled resources. |
| -r | Required | With parameter | Specifies the header file path for resources, with three formats: .txt, .js, or .h. |
| -e | Optional | With parameter | Specifies the starting ID value for generated resources, e.g., 0x01000000. Valid ranges: [0x01000000, 0x06FFFFFF), [0x08000000, 0x41FFFFFF). |
| -f | Optional | Without parameter | If the output path already exists, forcibly delete and regenerate it. |
| -h | Optional | Without parameter | Displays tool help information. |
| -m | Optional | With parameter | Specifies multiple module names for multi-module joint compilation, connected by commas. |
| -x | Optional | With parameter | Specifies the resource directory or single resource path for generating intermediate files. This command can be specified multiple times in the same command. |
| -z | Optional | Without parameter | Generates compilation results for the resource intermediate file directory. |
| -v | Optional | Without parameter | Displays the tool version number. |
| --ids | Optional | With parameter | Specifies the output directory for generating id_defined.json. |
| --defined-ids | Optional | With parameter | Specifies the path of the id_defined.json file, typically generated via --ids. id_defined.json contains a list of resource types, names, and their IDs. Developers can customize resource IDs in id_defined.json. |
| --icon-check | Optional | Without parameter | Enables PNG image validation for icon and startWindowIcon. |
| --target-config | Optional | With parameter | Used with the -i command to support selective compilation. Parameter Description: Specifies the configurations to include. |
target-config Parameter Description
Supported parameter configuration types: MccMnc, Locale, Orientation, Device, ColorMode, Density.
Parameter format: Configurations are separated by ;, and values within configurations are enclosed in [] and separated by ,.
MccMnc Matching Rules:
- Mcc (country code) must be the same.
- Mnc (network code) defaults to a match if absent; otherwise, Mnc must be the same.
Locale Matching Rules:
Locale matching must satisfy the following three rules:
- Language must be the same.
- Script (writing system) defaults to a match if absent; otherwise, it must be the same.
- Country or region defaults to a match if absent; otherwise, it must be the same.
Example Parameter:
Locale[zh_CN,en_US];Device[phone]
- This parameter filters out other languages, retaining only those matching
zh_CNanden_US. - Filters out other devices, retaining only
phone. - Other configurations (e.g.,
MccMnc,Orientation) are not filtered and are retained.
Usage Examples
For example, the directory structure of entry is as follows:
entry/src/main
| |----resource
| | |----base
| | | |----element
| | | |----media
| | | |----profile
| | |----rawfile
| | |----resfile
| |----config.json/module.json
Compile Resources
There are two ways to compile resources: full resource compilation and incremental resource compilation.
-
Full Resource Compilation:
Command:restool -i entry/src/main -j entry/src/main/module.json -p com.ohos.demo -o out -r out/ResourceTable.txt -f -
Incremental Resource Compilation:
Steps:Step 1: Generate resource middleware. Command:
restool -x entry/src/main/resource -o outStep 2: Compile resource middleware. Command:
restool -i out1 -i out2 -o out -p com.ohos.demo -r out/ResourceTable.txt -j entry/src/main/module.json -f -z
Fix Resource IDs
To fix resource IDs, follow these steps:
Step 1: Create the id_defined.json file. There are two methods: via command line or custom creation.
-
Method 1: Generate via command line. Command:
restool -i entry/src/main -j entry/src/main/module.json -p com.ohos.demo -o out -r out/ResourceTable.txt --ids out -f -
Method 2: Custom file. The filename must be
id_defined.json, with the following content:{ "record" : [ { "id" : "0x01000000", // The fixed ID value for the resource "name" : "app_name", // Resource name "type" : "string" // Resource type } ] }
Step 2: Fix the resource IDs. There are two ways:
-
Command 1:
restool -i entry/src/main -j entry/src/main/module.json -p com.ohos.demo -o out1 -r out1/ResourceTable.txt --defined-ids out/id_defined.json -f -
Command 2: Place the custom
id_defined.jsonin theresource/base/element/directory, then run:restool -i entry/src/main -j entry/src/main/module.json -p com.ohos.demo -o out1 -r out1/ResourceTable.txt -f