目录

Using Obfuscation for Code Hardening

To protect code assets, source code obfuscation is originally enabled by default in DevEco Studio: For projects developed using API version 10 or later based on the stage model and built in release mode, parameter names and local variable names in source code are obfuscated by default.

Since DevEco Studio NEXT Developer Beta3 (5.0.3.600), source code obfuscation is disabled by default for new projects and modules. If source code obfuscation is enabled in the module-level build-profile.json5 file, the following recommended obfuscation options are enabled by default: -enable-property-obfuscation, -enable-toplevel-obfuscation, -enable-filename-obfuscation, and -enable-export-obfuscation. You can customize these and other obfuscation options in the obfuscation-rules.txt configuration file. For details, see ArkGuard Obfuscation Options.

Constraints

Fields

You can configure source code obfuscation in the module-level build-profile.json5 file. The available fields are described as follows.

Field

Type

Mandatory

Description

ruleOptions

Object

No

Configuration of the code obfuscation rule.

  

enable

Boolean

Yes

Whether to enable source code obfuscation.

  • true: enabled.
  • false (default): disabled
NOTE:

Since DevEco Studio NEXT Developer Beta3 (5.0.3.600), the default value is changed from true to false.

files

String array

No

Relative paths of the obfuscation rule files. By default, the obfuscation-rules.txt file is used. The obfuscation rules configured in the file take effect only during the module's compilation process (including the dependent code).

NOTE:

consumerFiles

String/String array

No

Relative paths of the obfuscation rule files transferred to the integrator. This field is exclusive to HAR/HSP modules. Multiple files can be configured, and each file can have a custom name.

NOTE:
  • To ensure seamless integration and functionality of the HAR or HSP module, configure retention options in the rule files for any content that you do not want the integrator to obfuscate, such as variables or functions exported from the HAR or HSP module.
  • The obfuscation options configured in your rule files will be merged with obfuscation rules of the integrator, which may affect the final output. To minimize complications, limit your obfuscation configuration to retention options.
  • This field can be configured in the HSP module since DevEco Studio 5.1.0 Release.

Enabling Code Obfuscation

To protect code assets, code obfuscation is recommended. You can enable source code obfuscation in the module-level build-profile.json5 file.

"arkOptions": {
  "obfuscation": {
    "ruleOptions": {
      "enable": true  // Set this field to true to enable source code obfuscation.
    }
  }
}

Since DevEco Studio NEXT Developer Beta3 (5.0.3.600), enabling obfuscation automatically enables the recommended obfuscation rules in the obfuscation-rules.txt file, including the -enable-property-obfuscation, -enable-toplevel-obfuscation, -enable-filename-obfuscation, and -enable-export-obfuscation options.

In release build mode, you are advised to enable obfuscation and correctly configure obfuscation rules. Failure to do so could lead to runtime issues.

Enabling Advanced Obfuscation

After obfuscation is enabled, you can proceed to set up advanced obfuscation rules for more refined control over the obfuscation process.

[h2]Configuring All Obfuscation Rules

  1. Open the module-level build-profile.json5 file and configure the relative paths of the obfuscation rule files under the files field. You can specify multiple files, and the default file is at ./obfuscation-rules.txt.
    {
      "apiType": "stageMode",
      "buildOptionSet": [
        {
          "name": "release",
          "arkOptions": {
            "obfuscation": {
              "ruleOptions": {
                "enable": true,
                "files": [
                  "./obfuscation-rules.txt"  // Obfuscation rule file.
                ]
              }
            }
          }
        },
      ],
    }
  2. Open the obfuscation-rules.txt file in the module directory to configure obfuscation rules. For details, see Obfuscation Options. To preserve specific content, configure Retention Options.

    When multiple obfuscation rule files are specified, their rules are merged. For details about the merging and how the merged rules are applied, see Obfuscation Rule Merging Strategies.

[h2]Configuring Retention Options for HAR/HSP Modules

To ensure seamless integration and functionality of the HAR or HSP module, configure retention options in the rule files for any content that you do not want the integrator to obfuscate, such as variables or functions exported from the HAR or HSP module.

  1. Open the module-level build-profile.json5 file and configure the relative paths of the obfuscation rule files to be passed to the integrator under the consumerFiles field. You can specify multiple files. The default file is located at ./consumer-rules.txt, which corresponds to the obfuscation.txt file in the compiled HAR package.
    {
      "apiType": "stageMode",
      "buildOptionSet": [
        {
          "name": "release",
          "arkOptions": {
            "obfuscation": {
              "ruleOptions": {
                "enable": true,
                "files": [
                  "./obfuscation-rules.txt"   
                ]
              },
              "consumerFiles": [              // Obfuscation rule in use when the module is depended on.
                "./consumer-rules.txt"   
              ]
            }
          }
        },
      ],
    }
  2. Open the consumer-rules.txt file in the module directory to configure retention options.

    When multiple obfuscation rule files are specified, their rules are merged. For details about the merging and how the merged rules are applied, see Obfuscation Rule Merging Strategies.

Configuring Retention Options Using ObfuscationHelper

When source code obfuscation is enabled, methods, properties, or paths in your code may be renamed. However, if the application runtime still references the original, unobfuscated names, functionality may fail. To resolve this, you must configure retention options for the affected fields. For specific scenarios and configuration guidelines, see Retention Options.

Given the numerous scenarios to troubleshoot and fields to configure, DevEco Studio provides the ObfuscationHelper tool. It scans source code by module and usage scenario to rapidly identify required retention options and trustlist fields, allowing developers to generate obfuscation rule files with a single click. For dynamically accessed names or properties (determined only at runtime), ObfuscationHelper identifies such scenarios. You must further verify and configure trustlists based on service requirements.

[h2]Scanning Code

  1. Open ObfuscationHelper using either of the following methods:
    • Choose Tools > ObfuscationHelper from the menu bar.
    • Right-click the module directory and choose ObfuscationHelper from the shortcut menu.

  2. Select the module to be scanned from the drop-down list.
  3. If a module has been previously scanned and a trustlist has been generated from the troubleshooting process, corresponding historical records will be created. Select a relevant historical record, with which the current scan automatically associates. Fields already checked in the historical record require no re-examination.

    Historical records can be associated since DevEco Studio 6.0.0 Beta1.

  4. Select one or more scanning tasks based on the involved obfuscation scenarios and click Scan. For details about scanning tasks, see Scanning Tasks.

  5. After the scan is successful, go to the Recommended Trustlist and Supplemental Trustlist pages. You can also click Cancel to stop the scan.

[h2]Configuring the Recommended Trustlist

On the Recommended Trustlist configuration page, view identified retention options and trustlist fields from the scan results, then generate an obfuscation rule file with one click.

[h2]Configuring the Supplemental Trustlist

On the Supplemental Trustlist page, view the scanned core code and further check the core code, identify trustlist fields, and configure the fields in the target file.

[h2]Viewing Historical Records

After you generate a recommended trustlist or supplemental trustlist, a historical record is generated, which can be used for further check.

On the home screen of ObfuscationHelper, click History at the bottom to view all historical records.

[h2]Scanning Tasks

The following describes the scanning tasks performed by ObfuscationHelper.

Property obfuscation

Top-level scope name obfuscation

File name obfuscation

Imported/exported name obfuscation