DEV Community

ZHZL-m
ZHZL-m

Posted on

【Journey of HarmonyOS Next】DevEco Studio User Guide (37)

Image description

1 -> Configure CPP

Hvigor integrates cmake, ninja is a tool for building cpp code. In the initial state, you do not need additional configuration, in order to customize your CPP code compilation, you can add custom configuration through the following configuration.

In the build-profile.json5 of the module, the following configuration items exist:

{
  ...
  "buildOptionSet": [
    {
      "name": "release",
      "arkOptions": {
        "obfuscation": {
          "ruleOptions": {
            "enable": true,
            "files": [
              "./obfuscation-rules.txt"
            ]
          }
        }
      },
      "externalNativeOptions": {
        "path": "./src/main/cpp/CMakeLists.txt", // 自定义cmake配置脚本CMakeLists.txt的位置,它是以模块根目录为起始位置的相对路径
        "arguments": ["-DCMAKE_BUILD_TYPE=Debug"], // Hvigor将会把此处的自定义参数传递给cmake构建工具,您可通过cmake官方文档查找您所需的编译参数,同时它也将覆盖默认同名参数
        "cppFlags": "-g", // 自定义cpp flags参数
        "abiFilters": ["arm64-v8a"] // 自定义cpp编译架构,默认编译架构为arm64-v8a
      },
      "nativeLib": {
        "debugSymbol": { // 可通过此配置对cpp编译产物so执行strip,移除so中的调试信息与符号表等
          "strip": true, // 执行strip
          "exclude": [] //执行strip的过滤正则表达式规则
        },
        "filter": { // 可通过此选项自定义此cpp产物so是否打包到应用包中
          "excludes": [ // 根据正则表达式排除匹配到的.so文件,匹配到的so文件将不会被打包,可用于打包时缩小包体积
            "**/3.so", // 排除所有名称为“3”的so文件
            "**/x86_64/*.so" // 排除所有x86_64架构的so文件
          ], 
          "pickFirsts": [], // 按照.so文件的优先级顺序,打包最高优先级的.so文件
          "pickLasts": [], // 按照.so文件的优先级顺序,打包最低优先级的.so文件
          "enableOverride": true, // 当.so重名冲突时,允许打包时so文件的覆盖
          "select": [ // select提供native产物的精准选择能力,根据包名、版本、产物名称等选择或排除,select的优先级高于excludes、pickFirsts等配置项
            {
              "package": "@ohos/curl", // 包名
              "version": "1.3.5", // 包版本
              "include": ["libcurl.so"], // 选择打包的native产物
              "exclude": ["libc++_shared.so"] // 排除的native产物
            }
          ],
        },
        "headerPath": "./src/main/cpp/include" // 声明模块打包共享的c/cpp接口
        "librariesInfo":[
          {
             "name": "libentry.so",
             "linkLibraries": ["curl::curl"]
          }
        ]
      },
    },
  ],
  ...
}
Enter fullscreen mode Exit fullscreen mode

You can modify the compilation performance of cpp by customizing the externalNativeOptions parameter. nativeLib/headerPath declares the module's c/cpp interface file and exposes it to the dependent module via packaging. You can modify the volume and packaging rules of so products through configurations such as debugSymbol and filter.

1.1 -> about the priority of the library file so

The priority selection of library file so can be selected by pickFirsts, pickLasts option, where pickFirsts selects the high-priority library file and pickLasts selects the low-priority library file.

This priority is determined by the order in which the module or third-party package is collected, and the dependency declaration of this module is in the dependencies configuration item of the oh-package.json5 file, and the collection order collects the dependencies according to the breadth-first traversal method.

As shown in the figure below, the order of precedence is current > library0 > library1 > library5 > library2 > library3 > library4.

Image description

1.2 -> Use of select

Select provides the ability to accurately select native products, and selects or excludes native products to hap/hsp/har products based on package name, version, product name, etc.

For example, if the HAR package of the libcurl.so that this module depends on is @ohos/curl, and there are multiple versions of the libcurl.so, and you need to package version 1.3.5, you can use the following configuration to accurately package this SO into the product.

{
  buildOption: {
    nativeLib: {
      filter: {
        select: [ // select的优先级高于excludes、pickFirsts等配置项
          {
            package: "@ohos/curl", // 包名
            version: "1.3.5", // 包版本
            include: ["libcurl.so"], // 选择打包的native产物
            exclude: ["libc++_shared.so"] // 排除的native产物
           }
        ]
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

2-> Multi-engineering builds

In order to reduce the complexity of multi-team collaborative development of large-scale applications, a multi-project development mode is provided to improve the efficiency of collaborative development. The multi-project development capability allows you to split a large application into multiple modules, with each module corresponding to a separate project. After each project is compiled and generated separately, an APP needs to be packaged and generated for the application market.

  1. In the project-level build-profile.json5 configuration file of each project, set the value of the multiProjects field to true.
{
  "app": {
    ...
    "multiProjects": true,
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. Prepare the HAP packaging tool app_packing_tool.jar (in the $DevEco Studio installation directory/sdk/default/openharmony/toolchains/lib).

  2. In the HAP Packaging Tool directory, run the command to package multiple HAPs, as shown in the following example.

java -jar app_packing_tool.jar --mode multiApp --hap-list D:\project\MyApplication\1.hap,D:\project\MyApplication1\2.hap --out-path D:\project\final.app
Enter fullscreen mode Exit fullscreen mode

hap-list: Multiple HAP file paths, separated by commas.
out-path: the path of the generated APP file, for example, "D:\project\final.app".

3 -> Customize the .hvigor directory path

By default, the .hvigor directory is located in the user directory:

Windows:C:\Users\username.hvigor
macOS:/Users/UserName/.hvigor
Linux:
root: /root/.hvigor
Non-root user: /home/UserName/.hvigor
If the disk space of the default directory is insufficient, you need to customize the path of the .hvigor directory, which can be configured by following the following methods:

illustrate

This feature is supported from DevEco Studio 5.0 Canary1 release.

How to set Windows environment variables:
In the system or user variables, add the absolute path of the custom .hvigor directory.

Variable name: HVIGOR_USER_HOME

Variable value: Customize the absolute path to store the .hvigor directory. For example, D:\HvigorUserHome

Image description

macOS environment variable setting method:

Setting environment variables for DevEco Studio on macOS requires launchd.

How to set it to take effect temporarily:
Run the launchctl setenv statement in the terminal and restart DevEco Studio.

launchctl setenv HVIGOR_USER_HOME /User/xx #本处路径请替换为.hvigor目录的绝对路径
Enter fullscreen mode Exit fullscreen mode

This setting method will not work after restarting the computer.

How to set the restart without invalidation:
illustrate

The following is just an example of how to set system variables for DevEco Studio on macOS, depending on the system version.

Add the following content to /etc/launchd.conf (if the file does not exist, you can create it yourself):

setenv HVIGOR_USER_HOME /User/xx #本处路径请替换为.hvigor目录的绝对路径
Enter fullscreen mode Exit fullscreen mode

Once the settings are complete, restart your computer before it takes effect.

How to set Linux environment variables:
Open Terminal Tools and run the following command:

vim ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Add HVIGOR_USER_HOME environment variables.

export HVIGOR_USER_HOME=/home/xx  #本处路径请替换为.hvigor目录的绝对路径
Enter fullscreen mode Exit fullscreen mode

Save and close the file, use the source command to reload the .bashrc configuration file.

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Top comments (0)