Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.
HarmonyOS
In addition to DevEco Studio, you can also invoke Hvigor tasks through the CLI to build your app/atomic service. This approach allows you to set up a continuous integration (CI) pipeline to automate scheduled operations such as HAP/App Pack building, signing, installation, and running.
You can run commands on Windows, Linux, or macOS to build your app/atomic service through the CLI. This section uses Linux as an example. The procedure for invoking commands is the same regardless of the operating system.
Operating system: 64-bit Linux
glibc: 2.28 or later
Memory: 16 GB or larger recommended, 8 GB minimum
Hard disk: 100 GB or above
- tar -xvf jdk-17.0.6_linux-x64_bin.tar.gz
- #jdk
- export JAVA_HOME=/opt/jdk-17.0.6_linux-x64_bin
- export PATH=$PATH:$JAVA_HOME/bin
- java -version

- unzip commandline-tools-linux-x64-5.0.3.XXX.zip

- export COMMANDLINE_TOOL_DIR=/opt
The Command Line Tools package comes with a compatible version of Node.js. You can configure the environment variables by performing the following steps:
- # The following uses Linux as an example. The Node.js path varies with the operating system. Replace it with the actual path.
- export NODE_HOME=${COMMANDLINE_TOOL_DIR}/command-line-tools/tool/node
- export PATH=$PATH:$NODE_HOME/bin
The Node.js path varies depending on the OS. On Windows, the Node.js path is tool/node. On Linux and macOS, the Node.js path is tool/node/bin.
- node -v

You are advised to use the built-in Node.js of Command Line Tools. If you need to install it separately, use the matching Node.js version. For the version compatibility details, see DevEco Studio Compatibility and Version Mappings.
The hdc command line tool is used as a debugging tool for HarmonyOS apps/atomic services. This tool is located in the toolchains directory of the SDK that comes with the Command Line Tools. To facilitate the use of hdc, add it to the environment variables.
- export HDC_HOME=${COMMANDLINE_TOOL_DIR}/command-line-tools/sdk/default/openharmony/toolchains
- export PATH=$PATH:$HDC_HOME
- export PATH=${COMMANDLINE_TOOL_DIR}/command-line-tools/bin:$PATH
- hvigorw -v
If the dependencies configured the hvigor/hvigor-config.json5 file contain any npm third-party libraries, you need to set the npm image repository address in the pipeline so that the libraries can be correctly downloaded during compilation.
- npm config set registry https://repo.huaweicloud.com/repository/npm/
- npm config set "@ohos:registry" https://repo.harmonyos.com/npm/
- export PATH=${COMMANDLINE_TOOL_DIR}/command-line-tools/bin:$PATH
- ohpm -v
- ohpm config set registry https://ohpm.openharmony.cn/ohpm/
- ohpm config set strict_ssl false
To use texture compression on Linux, you need to install the libGL1 library. The installation command varies by operating system. For example, install libgl1-mesa-dev on Ubuntu/Debian and mesa-libGL-devel on CentOS/RHEL.
The command below uses libgl1-mesa-dev as an example. For other operating systems, replace it with the actual package name.
- apt install -y libgl1-mesa-dev
Before using the CLI to build a project, run the ohpm install command in the project and each module to install the third-party libraries on which the project and modules depend.
- # Switch to the specified directory $1 and run the ohpm install command.
- function ohpm_install() {
- cd $1 # $1: first parameter of the function, which must be a path.
- ohpm install --all # Install all dependencies.
- }
- PROJECT_PATH=xxx/xxx/project_name # Project path.
Do not store the project directory in a hidden directory. That is, each level in the project path must not start with a period (.), for example, xxx/.xxx/project is not recommended. Otherwise, during the build process, the module's code and configuration files may be packaged into the product as resources without being obfuscated or encrypted.
- # Install the third-party library dependencies based on service requirements.
- ohpm_install "${PROJECT_PATH}"
- ...
- # Run a build command based on the service requirements. The following is an example:
-
- # Clean the project.
- hvigorw clean --no-daemon
-
- # Build a HAP. The generated file is ${PROJECT_PATH}/{moduleName}/build/{productName}/outputs/{targetName}/xxx.hap.
- hvigorw assembleHap --mode module -p product=default -p buildMode=debug --no-daemon
-
- # Build an HSP. The generated files are ${PROJECT_PATH}/{moduleName}/build/{productName}/outputs/{targetName}/(xxx.har | xxx.hsp).
- hvigorw assembleHsp --mode module -p module=library@default -p product=default --no-daemon
-
- # Build a HAR. The generated file is ${PROJECT_PATH}/{moduleName}/build/{productName}/outputs/{targetName}/outputs/xxx.har.
- hvigorw assembleHar --mode module -p module=library1@default -p product=default --no-daemon
-
- # Build an App Pack. The generated file is ${PROJECT_PATH}/build/outputs/{productName}/xxx.app.
- hvigorw assembleApp --mode project -p product=default -p buildMode=debug --no-daemon
- // build-profile.json5 file
- {
- "name": "default",
- "compatibleSdkVersion": "26.0.0",
- "runtimeOS": "HarmonyOS",
- "buildOption": {
- "strictMode": {
- "caseSensitiveCheck" : true
- }
- }
- }
-
- NODE_OPTS="--max-old-space-size=10240"
The tables below list the common command-line options for compilation and building. For details about more Hvigor command-line options, see Common Commands.
| Options | Description |
|---|---|
| -p buildMode={debug | release} | Uses the debug or release mode for builds. By default, the debug mode is used in building HAPs, HSPs, or HARs, and the release mode is used in building App Packs. For details about build modes, see Specifying a Build Mode. For details about how to build a HAR, see Building a HAR. |
| -p product={ProductName} | Specifies the target product under which the module targets are built. Default value: default |
| -p module={ModuleName}@{TargetName} | Specifies the module and target for builds. You can specify multiple modules of the same type, separated with commas (,). Note: This option must be used together with --mode module. By default, all modules in the project are built when the AssembleHap task is executed, and default is used for TargetName. |
| -p ohos-test-coverage={true | false} | Executes the instrumentation compilation of the test framework code coverage. |
| Options | Description |
|---|---|
| clean | Cleans build output. |
| assembleHap | Builds a HAP. |
| assembleApp | Builds an App Pack. |
| assembleHsp | Builds an HSP. |
| assembleHar | Builds a HAR. |
If signature information has been configured via signingConfigs in the project-level build-profile.json5 file, and the pipeline contains signature files (including the .cer, .p7b, and .p12 files and the material folder, which is stored in the same directory as the .p12 file by default), both signed (for example, xxx-signed.hap) and unsigned (for example, xxx-unsigned.hap) packages will be generated after the build. The signed package can be run directly on a real device without re-signing. To re-sign the package, use the signing tool on the unsigned one, as described below.
To apply for a signature, three files are required: keystore file (.p12 file), digital certificate (.cer file), and profile (.p7b file).
Generating a Keystore File and CSR
Use the Keytool in the JDK to generate a keystore file and a CSR.
- keytool -genkeypair -alias "demo_key" -keyalg EC -groupname secp256r1 -sigalg SHA256withECDSA -dname "C=CN,O=HUAWEI,OU=HUAWEI IDE,CN=demo_key" -keystore /path/demo.p12 -storetype pkcs12 -validity 9125 -storepass 123456Abc -keypass 123456Abc
In the preceding command, modify settings of the following parameters and keep the other parameters as they are:
- keytool -certreq -alias "demo_key" -sigalg SHA256withECDSA -keystore /path/demo.p12 -storetype pkcs12 -file /path/demo.csr
Parameters in the CSR:
Requesting a Debug Digital Certificate and Profile File
With the CSR, request and download a debug digital certificate and profile in AppGallery Connect. For details, see Requesting a Debug Certificate and Requesting a Debug Profile.
- java -jar hap-sign-tool.jar sign-app -keyAlias "demo_key" -signAlg "SHA256withECDSA" -mode "localSign" -appCertFile "/path/demo.cer" -profileFile "/path/demo.p7b" -inFile "/path/hap-unsigned.hap" -keystoreFile "/path/demo.p12" -outFile "/path/hap-signed.hap" -keyPwd "123456Abc" -keystorePwd "123456Abc"
In the preceding command, modify settings of the following parameters and keep the other parameters as they are:
To sign an APP Pack, modify the inFile and outFile parameters in the preceding command to point to the APP Pack.
Use hdc to push a signed HAP to a real device for installation. Note that if the pushed HAP is not signed, the installation will fail.
- # Push the HAP to the device.
- hdc file send "{PROJECT_PATH}/entry/build/default/outputs/default/entry-default-signed.hap" "data/local/tmp/entry-default-signed.hap"
- # Install the HAP.
- hdc shell bm install -p "data/local/tmp/entry-default-signed.hap"
- # Delete the HAP.
- hdc shell rm -rf "data/local/tmp/entry-default-signed.hap"
To run the HAP on the device, run the following command:
- hdc shell aa start -a EntryAbility -b com.example.myapplication -m entry
The following example is not ready for direct use. Adapt it to meet your specific requirements.
- #!/bin/bash
- set -ex
-
- JAVA_HOME=xxx # Specify the installation directory of the JDK.
- COMMANDLINE_TOOL_DIR=xxx # Specify the installation directory of the command line tool.
-
- # Configure the hvigor and ohpm environment variables.
- export PATH=${COMMANDLINE_TOOL_DIR}/command-line-tools/bin:$PATH
-
- # Download and configure JDK.
- function init_JDK() {
- if [ ! -d "${JAVA_HOME}" ]; then
- mkdir "${JAVA_HOME}"
- fi
- cd ${JAVA_HOME}
- wget --no-check-certificate -q "${jdk_download_path}" -O jdk-linux.tar.xz # Download JDK, specifying the actual download path.
- tar -vxf jdk-linux.tar.xz
- JDK_DIR=xxx # Specify the directory in the compressed JDK package.
- cd ${JDK_DIR}
- mv -f ./* .[^.]* ../
- cd ..
- rm -rf JDK_DIR jdk-linux.tar.xz
- export JAVA_HOME=${JAVA_HOME}
- export PATH=$JAVA_HOME/bin:$PATH
- java -version
- }
-
- # Configure the hdc environment variable.
- function init_hdc() {
- export HDC_HOME=${COMMANDLINE_TOOL_DIR}/command-line-tools/sdk/default/openharmony/toolchains # Set the hdc environment variable. hdc is stored in toolchains.
- export PATH=$HDC_HOME:$PATH
- }
-
- # Install ohpm. Skip this step if ohpm already exists in the image.
- function init_ohpm() {
- ohpm -v
- # Configure the address of the ohpm repository.
- ohpm config set registry https://ohpm.openharmony.cn/ohpm/
- }
-
- # Initialize related paths.
- PROJECT_PATH=xxx # Project directory.
- # Go to the package directory and install the dependency.
- function ohpm_install {
- cd $1
- ohpm install
- }
- # Environment adaptation
- function buildHAP() {
- # Install the third-party library dependencies based on service requirements.
- ohpm_install "${PROJECT_PATH}"
- ohpm_install "${PROJECT_PATH}/entry"
- ohpm_install "${PROJECT_PATH}/xxx"
- # Run commands based on your service requirements. For details, see the commands in the DevEco Studio build log.
- cd ${PROJECT_PATH}
- hvigorw clean --no-daemon
- hvigorw assembleHap --mode module -p product=default -p debuggable=false --no-daemon # It is recommended that --no-daemon be added to the end of the command.
- }
- function install_hap() {
- hdc file send "${PROJECT_PATH}/entry/build/default/outputs/default/entry-default-signed.hap" "data/local/tmp/entry-default-signed.hap"
- hdc shell bm install -p "data/local/tmp/entry-default-signed.hap"
- hdc shell rm -rf "data/local/tmp/entry-default-signed.hap"
- hdc shell aa start -a MainAbility -b com.example.myapplication -m entry
- }
-
- # Use ohpm to publish the HAR.
- function upload_har {
- ohpm publish pkg.har
- }
-
- function main {
- local startTime=$(date '+%s')
- init_JDK
- init_hdc
- init_ohpm
- buildHAP
- install_hap
- upload_har
- local endTime=$(date '+%s')
- local elapsedTime=$(expr $endTime - $startTime)
- echo "build success in ${elapsedTime}s..."
- }
- main
If your computer is not connected to the Internet, you can build a pipeline environment by referring to the following steps.
- {
- "dependencies": {
- "pnpm": "8.13.1"
- }
- }

- npm config set offline true
- {
- "dependencies": {
- "ajv": "latest"
- }
- }

For details, see Installing a Third-Party Library.
To install the libGL1 library, which is required for using texture compression on Linux:
Download the libGL1 library installation package on a computer with Internet access, for example, download libgl1-mesa-dev on Ubuntu/Debian and mesa-libGL-devel on CentOS/RHEL, then copy the downloaded package to the offline computer for installation.
Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
Quick start
Helps you find desired resources with ease.