最近在openharmony4.1的源码中发现了官方新增了一个在源码环境下编译hap的脚本,正好有个修改Launcher的需求,IDE的环境配置比较麻烦,所以研究了一下这个脚本,但是自己的编译环境无法正常运行脚本,这边经过修改后可以正常编译Launcher,这里记录一下修改过程;
编译过程
路径:/applications/standard/hap/build.sh
用法:
build.sh --project=/home/yuwei/project_root_dir/applications/standard/launcher/
1.工具找不到报错:
./build.sh: line 146: npm: command not found
./build.sh: line 252: ohpm: command not found
npm和ohpm 找不到,在146行导入以下语句:
export PATH=${ROOT_PATH}/prebuilts/build-tools/common/nodejs/current/bin:$PATH
export PATH=${ROOT_PATH}/prebuilts/build-tools/common/oh-command-line-tools/ohpm/bin:$PATH
2.SDK路径找不到:
> hvigor ERROR: Invalid value of 'sdk.dir' in 'local.properties' or 'OHOS_BASE_SDK_HOME' in the system environment path.
at
> hvigor ERROR: BUILD FAILED in 6 s 506 ms
这个原因是没有指定SDK的目录导致的,有两种方案:
- 自己指定SDK的目录 在/applications/standard/hap/build.sh 的arg_sdk_path指定路劲即可
- 通过脚本编译SDK 将/applications/standard/hap/build.sh 中的arg_build_sdk 改成true
我这里采用第二种方案:
3.编译SDK错误:
[OHOS INFO] ERROR at //build/config/BUILDCONFIG.gn:52:5: Script returned non-zero exit code.
[OHOS INFO] exec_script(check_mac_system_and_cpu_script, [ "system" ], "string")
[OHOS INFO] ^----------
[OHOS INFO] Current dir: /home/yuwei/project_root_dir/out/sdk/
[OHOS INFO] Command: /usr/bin/env /home/yuwei/project_root_dir/build/scripts/check_mac_system_and_cpu.py system
[OHOS INFO] Returned 1.
[OHOS INFO] stderr:
[OHOS INFO]
[OHOS INFO] File "/home/yuwei/project_root_dir/build/scripts/check_mac_system_and_cpu.py", line 21
[OHOS INFO] def run_cmd(cmd: str):
[OHOS INFO] ^
[OHOS INFO] SyntaxError: invalid syntax
[OHOS INFO]
[OHOS INFO] root_out_dir=//out/sdk
[OHOS INFO] root_build_dir=//out/sdk
[OHOS INFO] root_gen_dir=//out/sdk/gen
[OHOS INFO] current_toolchain=
[OHOS ERROR] ERROR at //build/config/BUILDCONFIG.gn:52:5: Script returned non-zero exit code.
[OHOS ERROR] exec_script(check_mac_system_and_cpu_script, [ "system" ], "string")
[OHOS ERROR] ^----------
[OHOS ERROR] Current dir: /home/yuwei/project_root_dir/out/sdk/
[OHOS ERROR] Command: /usr/bin/env /home/yuwei/project_root_dir/build/scripts/check_mac_system_and_cpu.py system
[OHOS ERROR] Returned 1.
[OHOS ERROR] stderr:
[OHOS ERROR]
[OHOS ERROR] File "/home/yuwei/project_root_dir/build/scripts/check_mac_system_and_cpu.py", line 21
[OHOS ERROR] def run_cmd(cmd: str):
[OHOS ERROR] ^
[OHOS ERROR] SyntaxError: invalid syntax
[OHOS ERROR]
[OHOS ERROR] root_out_dir=//out/sdk
[OHOS ERROR] root_build_dir=//out/sdk
[OHOS ERROR] root_gen_dir=//out/sdk/gen
[OHOS ERROR] current_toolchain=
[OHOS ERROR] Traceback (most recent call last):
[OHOS ERROR] File "/home/yuwei/project_root_dir/build/hb/containers/status.py", line 47, in wrapper
[OHOS ERROR] return func(*args, **kwargs)
[OHOS ERROR] File "/home/yuwei/project_root_dir/build/hb/services/gn.py", line 197, in _execute_gn_gen_cmd
[OHOS ERROR] SystemUtil.exec_command(gn_gen_cmd, self.config.log_path)
[OHOS ERROR] File "/home/yuwei/project_root_dir/build/hb/util/system_util.py", line 64, in exec_command
[OHOS ERROR] LogUtil.get_failed_log(log_path)
[OHOS ERROR] File "/home/yuwei/project_root_dir/build/hb/util/log_util.py", line 191, in get_failed_log
[OHOS ERROR] LogUtil.get_gn_failed_log(log_path)
[OHOS ERROR] File "/home/yuwei/project_root_dir/build/hb/util/log_util.py", line 137, in get_gn_failed_log
[OHOS ERROR] raise OHOSException(
[OHOS ERROR] exceptions.ohos_exception.OHOSException: GN Failed! Please check error in /home/yuwei/project_root_dir/out/sdk/error.log, and for more build information in /home/yuwei/project_root_dir/out/sdk/build.log
[OHOS ERROR]
[OHOS ERROR] Code: 3000
[OHOS ERROR]
[OHOS ERROR] Reason: GN Failed! Please check error in /home/yuwei/project_root_dir/out/sdk/error.log, and for more build information in /home/yuwei/project_root_dir/out/sdk/build.log
[OHOS ERROR]
[OHOS ERROR] Error Type: UNKNOWN
[OHOS ERROR]
[OHOS ERROR] Description: An unknown error occurred while executing 'gn gen'.
[OHOS ERROR]
[OHOS ERROR] Solution: There is no solution available. You can check the 'gn_error.log' in the output directory for more information
[OHOS ERROR]
这个错误其实是python的环境问题,但有个更简单的方法,将/applications/standard/hap/build.sh 第71行的build.py 改成build.sh 使用project_root_dir下的build.sh 指令去编译SDK即可
4.SDK用户协议报错:
> hvigor ERROR: Cause: The SDK license agreement is not accepted.
Solution: Go to Tools > SDK Manager > SDK > OpenHarmony, download the Toolchains:4.0.10.16 SDK, and accept the license agreement. Open SDK Manager
> hvigor ERROR: BUILD FAILED in 7 s 834 ms
主要是一个SDK的用户协议,我这边的解决方案是将 IDE下载的SDK目录下的licenses文件夹拷贝到了上面编译的SDK的目录下,${ROOT_PATH}/out/sdk/packages/ohos-sdk/linux
5.HAP源码编译报错:
> hvigor ERROR: Failed :launcher_settings:default@CompileArkTS...
> hvigor ERROR: ArkTS Compiler Error
这个主要是因为Launcher的源码中使用的是SDK版本是10 而我们编译的SDK版本是11导致的,需要修改Launcher的SDK版本
/applications/standard/launcher/build-profile.json5
{
"app": {
"products": [
{
"name": "default",
"signingConfig": "default",
"compileSdkVersion": 10, //这里改成11即可
"compatibleSdkVersion": 10 //这里改成11即可
}
],
将上面的compileSdkVersion和compatibleSdkVersion改成11即可
6.hivgor执行报错:
dependencies:
+ @ohos/hvigor 3.0.9 (4.0.4 is available)
+ @ohos/hvigor-ohos-plugin 3.0.9 (4.0.4 is available)
Done in 22s
Hvigor install success.
> hvigor @ohos/hvigor-ohos-plugin dependency has changed and need to re-install
ENOENT ENOENT: no such file or directory, open '/home/yuwei/.hvigor/project_caches/51881010c49be65805a24b786b95a4fb/workspace/lib/rollup.tgz'
This error happened while installing the dependencies of @ohos/hvigor-ohos-plugin@3.0.9
at @ohos/hvigor-arkts-compose@3.0.9
at @ohos/hvigor-arkts-base@3.0.9
Progress: resolved 36, reused 36, downloaded 0, added 0
> hvigor ERROR: /home/yuwei/.hvigor/wrapper/tools/node_modules/.bin/pnpm execute failed.
这个是因为launcher的hvigor的版本低导致的,需要在下面的配置文件中将版本改成上面的 x.x.x is available中的版本号
/applications/standard/launcher/hvigor/hvigor-config.json5
{
"hvigorVersion": "3.0.9",
"dependencies": {
"@ohos/hvigor-ohos-plugin": "3.0.9"
}
}
将上面的3.0.9改成4.0.4即可
至此Launcher的应用就编译成功了,编译后的hap在out/hap目录下