DEV Community

zhonghua
zhonghua

Posted on • Edited on

HarmonyOS Development in Practice: Flexible Customization of Compilation Options for Efficient Applications

HarmonyOS Development in Practice: Flexible Customization of Compilation Options for Efficient Applications

In the vast realm of HarmonyOS development, the ability to flexibly customize compilation options is an essential skill for every developer. This skill not only affects the performance of the application but also directly relates to its stability and user experience in different scenarios. In this article, we will delve into how to skillfully customize compilation options in HarmonyOS development and share some practical hands-on experience and professional tips.

I. Project-Level Configuration Analysis

  1. Product Category Configuration (products)

In the build configuration of a HarmonyOS project, the products field is key to defining product categories. Each product category has its unique compilation settings to meet different business needs. Here is a typical product category configuration example:

"products": [
  {
    "name": "default",
    "signingConfig": "defaultSigning",
    "compileSdkVersion": "4.1.0(11)",
    "compatibleSdkVersion": "4.1.0(11)",
    "runtimeOS": "HarmonyOS",
    "output": {
      "artifactName": "default_app"
    },
    "buildOption": {
      "arkOptions": {
        "buildProfileFields": {
          "IS_PRODUCTION": false,
          "URL_BASE": "https://*"
        }
      }
    }
  }
  // ... other product category configurations
]
Enter fullscreen mode Exit fullscreen mode
  • name: Defines the name of the product category, facilitating differentiation between products during the build process.
  • signingConfig: Specifies the signing configuration for the product category, ensuring the security of the application.
  • compileSdkVersion and compatibleSdkVersion: Define the SDK versions used for compilation and runtime, respectively, ensuring the compatibility and stability of the application.
  • runtimeOS: Specifies that the application's runtime environment is HarmonyOS.
  • output: Configures information related to the output of the product, such as the application name.
  • buildOption: A powerful field that allows us to customize the compilation options for ARK (HarmonyOS Application Runtime), such as buildProfileFields, which enables us to distinguish between different compilation environments and configurations through macro definitions in the code.
  1. Build Mode Configuration (buildModeSet)

The buildModeSet field is where the collection of build modes is defined. Different build modes correspond to different compilation parameters and output products to meet the development needs at various stages.

"buildModeSet": [
  {
    "name": "debug"
  },
  {
    "name": "release"
  }
]
Enter fullscreen mode Exit fullscreen mode
  • name: Defines the name of the build mode, such as debug (debug mode) and release (release mode). During the build process, we can select the appropriate build mode according to our needs to achieve the best build results.

In HarmonyOS development, flexibly customizing compilation options is key to achieving efficient and stable development. By properly configuring fields such as products and buildModeSet, we can ensure that the application achieves the best compilation results at different stages and under different environments. Moreover, by deeply understanding the meanings and functions of these configuration options, we can better grasp the essence of HarmonyOS development and improve development efficiency and quality.

  1. Module and Target Configuration (modules and targets)

In the world of HarmonyOS development, modules and targets play a crucial role. modules defines the various modules in the project and their source code locations, while targets specifies the APP products we expect to build from these modules.

"modules": [
  {
    "name": "entry",
    "srcPath": "./entry",
    "targets": [
      {
        "name": "default",
        "applyToProducts": ["default"]
      }
    ]
  },
  // ... other module configurations
]
Enter fullscreen mode Exit fullscreen mode
  • name: Each module has a unique name for easy identification and management.
  • srcPath: Specifies the directory where the module's source code is located, relative to the project root directory.
  • targets: Each module can have multiple targets, with each target corresponding to a build product.
    • name: The name of the target, usually corresponding to a product name within the module.
    • applyToProducts: This field defines which products (product) the target applies to. For example, we can apply the default target under the entry module to the default product.

II. In-Depth Module-Level Configuration

  1. buildOptionSet Configuration

buildOptionSet is a collection used in HarmonyOS development to define various build options. Each build option has its specific name and configuration details to meet different build requirements.

Release Mode Configuration (for release builds):

{
  "name": "release",
  "debuggable": false,
  "arkOptions": {
    "obfuscation": {
      "ruleOptions": {
        "enable": true,
        "files": [
          "./obfuscation-rules.txt"
        ]
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode
  • debuggable: Set to false, indicating that the application in this build mode cannot be debugged.
  • arkOptions: Configuration options for ARK (HarmonyOS Application Runtime).
    • obfuscation: Code obfuscation configuration to enhance application security.
    • enable: Enable code obfuscation.
    • files: Specifies the path to the obfuscation rule files.

Debug Mode Configuration (for debug builds):

{
  "name": "debug",
  "debuggable": true,
  "arkOptions": {
    "obfuscation": {
      "ruleOptions": {
        "enable": false
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode
  • debuggable: Set to true, allowing the application to be debugged on the device.
  • arkOptions: In debug mode, code obfuscation is usually turned off to make it easier for developers to view and debug the code.
  1. buildModeBinder Configuration

The buildModeBinder field establishes a mapping relationship between build modes (buildModeName), build options (buildOptionName), and targets (targetName). In this way, during the build process, specifying the build mode will automatically select the corresponding build options and targets for the build.

  • Debug Mode:
  {
    "buildModeName": "debug",
    "mappings": [
      {
        "targetName": "default", // Specifies the target to be bound
        "buildOptionName": "debug" // Specifies the build option to be bound
      }
    ]
  }
Enter fullscreen mode Exit fullscreen mode

In debug mode, the specified targets (such as default, tc, pro) will be built using the debug build option.

  • Release Mode:
  {
    "buildModeName": "release",
    "mappings": [
      {
        "targetName": "default", // Specifies the target to be bound
        "buildOptionName": "release" // Specifies the build option to be bound
      }
    ]
  }
Enter fullscreen mode Exit fullscreen mode

In release mode, the specified targets will be built using the release build option.

  1. targets Configuration

The targets field defines all the targets in the project. Each target has a unique name used to specify the application or component to be built during the build process.

Top comments (0)