- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
One-Click Deployment of HarmonyOS 5 HSP and HAP Files: Script Analysis
During HarmonyOS 5 application development and testing, frequent installation and update of test packages are common needs. This article introduces how to use an automated batch script (install_v1.0.1.bat) to quickly uninstall, install, and launch HarmonyOS 5 applications, significantly improving development and testing efficiency.
Core Functions of the Script
The script achieves the following functions:
File Description
1. Configuration File config.txt
2. Batch Script install_v1.0.1.bat
Step 1: Prepare Files
Double-click install_v1.0.1.bat and follow the prompts:
1. Dynamic Package Name Reading
if exist config.txt (
for /f "delims=" %%i in (config.txt) do set PACKAGE_NAME=%%i
)
if %errorlevel% neq 0 (
echo Failed to install HSP file: %%~nxf
set INSTALL_SUCCESS=0
goto END
)
hdc shell aa start -a EntryAbility -b %PACKAGE_NAME% -m entry
This script enables developers to quickly deploy HarmonyOS 5 test packages, achieving an automated "one-click install+launch" process. It is particularly suitable for scenarios requiring frequent feature validation or bug fixing. With dynamic configuration file reading and rigorous error handling, it enhances efficiency while reducing manual operation errors.
Download the script now to experience efficient HarmonyOS 5 development!
Appendix:
During HarmonyOS 5 application development and testing, frequent installation and update of test packages are common needs. This article introduces how to use an automated batch script (install_v1.0.1.bat) to quickly uninstall, install, and launch HarmonyOS 5 applications, significantly improving development and testing efficiency.
Core Functions of the Script
The script achieves the following functions:
- Dynamic Package Name Reading: Supports automatic package name retrieval from a configuration file (config.txt) or manual input.
- Automatic Uninstallation of Old Versions: Cleans up old applications via the hdc uninstall command.
- Batch Installation of HSP Files: Scans all .hsp files (Harmony Shared Packages) in the current directory and installs them sequentially.
- Main HAP File Installation: Detects and installs .hap files (Harmony Application Packages) in the directory, proceeding only if all HSP installations succeed.
- Automatic Application Launch: Starts the application's EntryAbility via hdc shell after successful installation.
- Robust Error Handling: Real-time installation status detection, with process interruption and detailed error prompts on failure.
File Description
1. Configuration File config.txt
- Purpose: Stores the HarmonyOS 5 application package name (e.g., com.atomicservice.5765880207855877209).
- Advantage: Avoids repeated input, suitable for fixed-package-name testing scenarios.
- Format Requirement: Single-line content with no additional syntax.
2. Batch Script install_v1.0.1.bat
- Compatibility: Supports Windows environments.
- Dependent Tool: Requires pre-configured HarmonyOS 5 Device Connector (hdc) added to the system path.
Step 1: Prepare Files
- Place the script file install_v1.0.1.bat and configuration file config.txt in the project root directory.
- Ensure the current directory contains .hsp (shared packages) and .hap (application packages) to be installed.
- Directly Modify config.txt: Write the target package name (overwriting existing content).
- No Configuration File Modification: Manually input the package name when running the script.
Double-click install_v1.0.1.bat and follow the prompts:
- If config.txt exists and contains a valid package name, it will be automatically read to uninstall the old version.
- If no package name is configured, the script will prompt for manual input.
- Install all HSP files in sequence, then install the HAP file.
- After successful installation, the application will start automatically with log output.
1. Dynamic Package Name Reading
if exist config.txt (
for /f "delims=" %%i in (config.txt) do set PACKAGE_NAME=%%i
)
- Reads the first line of config.txt as the package name if the file exists.
if %errorlevel% neq 0 (
echo Failed to install HSP file: %%~nxf
set INSTALL_SUCCESS=0
goto END
)
- Immediately terminates the process and marks failure if any HSP installation fails.
hdc shell aa start -a EntryAbility -b %PACKAGE_NAME% -m entry
- Starts the application's EntryAbility via HarmonyOS 5' aa command, requiring the EntryAbility name to match the project configuration.
- Difference Between HSP and HAP
- HSP (Harmony Shared Package): Reusable shared function modules for multiple applications.
- HAP (Harmony Application Package): Main application package containing entry logic.
- Troubleshooting Script Execution Failures
- Check if the hdc tool is properly connected to the device.
- Ensure the HAP file exists and is not corrupted.
- Confirm the package name matches the project configuration (to avoid permission conflicts).
- Expansion Suggestions
- Modify the script to support multi-HAP installation (requires adjusting loop logic).
- Add version number validation to prevent repeated installation of the same version.
This script enables developers to quickly deploy HarmonyOS 5 test packages, achieving an automated "one-click install+launch" process. It is particularly suitable for scenarios requiring frequent feature validation or bug fixing. With dynamic configuration file reading and rigorous error handling, it enhances efficiency while reducing manual operation errors.
Download the script now to experience efficient HarmonyOS 5 development!
Appendix:
- Script Source Code Address:
- How to Cache Images Quickly in Ability: