OpenHarmony开发者论坛

标题: OpenHarmony应用编译 - 如何在源码中编译复杂应用(4.0-Release) [打印本页]

作者: tizizzz    时间: 2023-12-8 10:22
标题: OpenHarmony应用编译 - 如何在源码中编译复杂应用(4.0-Release)
[md]# 概述

## 文档环境

开发环境:Windows 11

编译环境:Ubuntu 22.04

开发板型号:DAYU 200(RK3568)

系统版本:OpenHarmony-4.0-Release

涉及仓库:[applications\_launcher](https://gitee.com/openharmony/applications_launcher)

## 功能简介

* 在**OpenHarmony**系统中预安装应用的**hap**包会随系统编译打包到镜像中,目前有两种编译预安装应用**hap**包的方式,一种为随系统编译时,编译应用源码生成**hap**包的方式,另一种是将已生成的**hap**包放入系统源码中,再进行打包的方式。后者需要开发者使用**DevEco Studio**或其它途径,把应用源码编译构建为hap包,再将**hap**放入系统源码中。
* 在[OpenHarmony应用编译 - 如何在源码中编译复杂应用(3.2-Release)](https://forums.openharmony.cn/fo ... thread&tid=1374)文章中,介绍了如何在**3.2Release**版本的系统源码中编译系统应用**Launcher**。本文档将继续以**Launcher**为例,带大家了解如何通过**4.0Release**的系统源码编译应用的方式来打包预安装应用。
* 由于应用依赖和构建工具的升级和替换,**3.2Release**系统编译构建应用的方式是**NPM+Webpack**,而**4.0Release**使用**OHPM+Hvigor**的方式进行构建,差别较大。

# 4.0-Release系统编译Launcher

1.由于**Launcher**自身原因,导致需要做2处改动才可进行源码编译构建。本步骤并非所有项目通用,如果新建的项目可以跳过本步骤,后续**Launcher**或工具更新后也不需要本步骤。

(1)适配系统源码中**NODE\_HOME**环境变量的配置,需要修改应用目录下**hvigorw**工具为最新。

文件位置:**applications/standard/launcher/hvigorw**

内容如下:

```cpp
#!/bin/bash

# ----------------------------------------------------------------------------
#  Hvigor startup script, version 1.0.0
#
#  Required ENV vars:
#  ------------------
#    NODE_HOME - location of a Node home dir
#    or
#    Add /usr/local/nodejs/bin to the PATH environment variable
# ----------------------------------------------------------------------------

HVIGOR_APP_HOME="`pwd -P`"
HVIGOR_WRAPPER_SCRIPT=${HVIGOR_APP_HOME}/hvigor/hvigor-wrapper.js
warn() {
        echo ""
        echo -e "\033[1;33m`date '+[%Y-%m-%d %H:%M:%S]'`$@\033[0m"
}

error() {
        echo ""
        echo -e "\033[1;31m`date '+[%Y-%m-%d %H:%M:%S]'`$@\033[0m"
}

fail() {
        error "$@"
        exit 1
}

# Determine node to start hvigor wrapper script
if [ -n "${NODE_HOME}" ];then
   EXECUTABLE_NODE="${NODE_HOME}/bin/node"
   if [ ! -x "$EXECUTABLE_NODE" ];then
       fail "ERROR: NODE_HOME is set to an invalid directory,check $NODE_HOME\n\nPlease set NODE_HOME in your environment to the location where your nodejs installed"
   fi
else
   EXECUTABLE_NODE="node"
   which ${EXECUTABLE_NODE} > /dev/null 2>&1 || fail "ERROR: NODE_HOME is not set and not 'node' command found in your path"
fi

# Check hvigor wrapper script
if [ ! -r "$HVIGOR_WRAPPER_SCRIPT" ];then
        fail "ERROR: Couldn't find hvigor/hvigor-wrapper.js in ${HVIGOR_APP_HOME}"
fi

# start hvigor-wrapper script
exec "${EXECUTABLE_NODE}" \
        "${HVIGOR_WRAPPER_SCRIPT}" "$@"
```

![](https://harmonyos.oss-cn-beijing ... b1eb2ff7506a84b.png)

(2)由于系统编译应用目前不支持配置产品,所以需要把**Launcher**应用源码中有关**pad**的构建项删除。

文件位置:**applications/standard/launcher/build-profile.json5**

内容如下:

```cpp
{
          ...
    {
      "name": "phone_launcher",
      "srcPath": "./product/phone",
      "targets": [
        {
          "name": "default",
          "applyToProducts": [
            "default",
          ],
        },
      ],
    },
    {
      "name": "launcher_settings",
      "srcPath": "./feature/settings",
      "targets": [
        {
          "name": "default",
          "applyToProducts": [
            "default",
          ],
        },
      ],
    }
  ],
}
```

![](https://harmonyos.oss-cn-beijing ... 47ab05b7580cccc.png)

文件位置:**applications/standard/launcher/feature/settings/build-profile.json5**

内容如下:

```cpp
{
  "apiType": 'stageMode',
  "buildOption": {
  },
  "targets": [
    {
      "name": "default",
    }
  ],
  "entryModules": ["phone_launcher"]
}
```

![](https://harmonyos.oss-cn-beijing ... 4fe803d4091ed8e.png)

2.删除或注释系统中默认的**Launcher**应用**hap**包编译方式。

文件位置:**applications/standard/hap/BUILD.gn**

```cpp
group("hap") {
  deps = [
            ...
      # "//applications/standard/hap:launcher_hap", // 直接删除或注释,不参与编译
      # "//applications/standard/hap:launcher_settings_hap",  // 直接删除或注释,不参与编译
            ...
  ]
}
```

![](https://harmonyos.oss-cn-beijing ... 794bff93aa3e5b7.png)

3.在**applications/standard/launcher**目录中增加**BUILD.gn**文件。

![](https://harmonyos.oss-cn-beijing ... 576afa9340bf3a0.png)

内容如下:

```cpp
import("//build/ohos.gni")

ohos_app("launcher_OS") {
  part_name = "prebuilt_hap"
  subsystem_name = "applications"
  hap_name = "Launcher_OS"   
  certificate_profile = "./signature/launcher.p7b"
  module_libs_dir = "entry"
  module_install_dir = "app/com.ohos.launcher"
  js_build_mode = "release"
  build_level = "module"
  assemble_type = "assembleHap"
}
```

说明:

* 此处产物名称**hap\_name**定义为**Launcher\_OS**是为了区分原系统源码中默认的**hap**包名称,实际可以填写为**Launcher**或任意值。
* **BUILD.gn**的模板参数可以参考编译系统提供的模板。

4.在**applications/standard/hap/ohos.build**文件的**module\_list**中增加**launcher\_OS**模块编译。

```cpp
{
  "subsystem": "applications",
  "parts": {
    "prebuilt_hap": {
                  ...
      "module_list": [
               ...
       "//applications/standard/launcher:launcher_OS",
        ...
      ]
    }
  }
}
```

![](https://harmonyos.oss-cn-beijing ... 41c9f961150bf9e.png)

5.执行源码编译指令。如果以下2个目录产物正确,则说明应用源码编译方式修改成功。

(1)在**out/rk3568/obj/applications/standard/launcher/launcher\_OS**目录中,会生成2个hap的编译产物。

![](https://harmonyos.oss-cn-beijing ... c9d6064bab95981.png)

(2)在**out/rk3568/packages/phone/system/app/com.ohos.launcher**目录中,是实际系统环境中的**hap**包产物。

![](https://harmonyos.oss-cn-beijing ... 2387a9160468334.png)

6.烧录系统验证功能。

**Launcher**正常启动:

![](https://harmonyos.oss-cn-beijing ... fb94def16413a6.jpeg)

系统应用目录文件正确:

![](https://harmonyos.oss-cn-beijing ... 341a028c88976d3.png)
[/md]
作者: martin_hu    时间: 2024-4-1 11:29
在4.1 beta1上按照博主的步骤,全编译或使用模块编译命令会报错。
编译命令:./build.sh -p rk3568 --build-target applications/standard/launcher:launcher_OS

obj/applications/standard/launcher/launcher_OS目录为空
报错信息:

[OHOS INFO] [2/2] Hvigor warning: > hvigor ERROR: /home/xxx/.hvigor/wrapper/tools/node_modules/.bin/pnpm execute failed.

[OHOS ERROR] [1/3] ACTION //applications/standard/launcher:launcher_OS_compile_app(//build/toolchain/ohos:ohos_clang_arm)
[OHOS ERROR] FAILED: obj/applications/standard/launcher/launcher_OS/unsigned_hap_path_list.json
[OHOS ERROR] /usr/bin/env ../../build/scripts/compile_app.py --nodejs ../../prebuilts/build-tools/common/nodejs/node-v16.20.2-linux-x64/bin/node --cwd ../../applications/standard/launcher/ --build-profile ../../applications/standard/launcher/build-profile.json5 --sdk-home /home/xxx/ohos41/prebuilts/ohos-sdk/linux --output-file obj/applications/standard/launcher/launcher_OS/unsigned_hap_path_list.json --ohpm-registry  --module-libs-dir entry --build-level module --assemble-type assembleHap

作者: martin_hu    时间: 2024-4-1 13:29
回复 martin_hu: 已解决,需修改hvigor/hvigor-config.json5版本为4.0.4
{
  "hvigorVersion": "4.0.4",
  "dependencies": {
    "@ohos/hvigor-ohos-plugin": "4.0.4"
  }
}
作者: martin_hu    时间: 2024-4-3 10:56
请教下,out/rk3568/packages/phone/system/app/com.ohos.launcher下生成的hap名怎么配置,默认的hap名太冗余了。




欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/) Powered by Discuz! X3.5