Different package types serve various purposes and have distinct build processes. This requires you to consider different aspects when applying obfuscation. This topic provides tailored recommendations for three types of packages: HAP, HAR, and HSP, aiming to help you effectively implement obfuscation.
To better understand the obfuscation behavior across different package types, you should familiarize yourself with the principles of obfuscation and the process of enabling it. You are also recommended to read Application Package Structure in Stage Model to grasp the differences between package types.
In addition to the default obfuscation feature, you are encouraged to configure the following basic obfuscation options in the obfuscation configuration file:
1. Top-level scope name obfuscation (-enable-toplevel-obfuscation)
2. Property name obfuscation (-enable-property-obfuscation)
3. Imported/Exported name obfuscation (-enable-export-obfuscation)
4. File name obfuscation (-enable-filename-obfuscation)
After the option configuration, you must configure the corresponding trustlists to ensure the application functions correctly.
Once the application functions correctly, you can enable additional features like log removal (-remove-log) to prepare the release package.
Enable the obfuscation rules. Start with the recommended options and add other options as needed.
Familiarize yourself with the obfuscation rule merging strategies to understand how these rules affect the current module during compilation.
Understand which code from dependent packages will be compiled and obfuscated together with the current module. This will affect the newly generated obfuscated names and some globally effective trustlists (such as property names), and consequently affect the final obfuscation effect of the current module. Specifically:
To ensure the correctness of the interaction with third-party libraries in the release state, ArkGuard automatically collects exported names and related properties from dependent modules in oh_modules and adds them to a trustlist. Since you can reference files from any path within the dependent modules, this collection includes exported names from both entry files (such as Index.ets) and all files of the third-party libraries in oh_modules.
For local source code HAR packages and local HSP packages, ArkGuard does not automatically collect exported names in all files and add them to a trustlist.
The table below summarizes the compilation and obfuscation scenarios for common dependent packages.
| Dependent Module | Files Involved in Compilation | Obfuscated with the Current Module | Passes Retention Configuration to the Current Module | Collects Exported Names into a Trustlist |
|---|---|---|---|---|
| Local source code HAR package | Source code directory of the module in the local environment | Yes | Yes | No |
| Released source code HAR package | Module directory under project-level oh_modules | Yes | Yes | Yes |
| Released bytecode HAR package | Binary bytecode and declaration files | No | Yes | Yes |
| Local source code HSP package | NA | No | Yes | No |
| Integrated HSP package | Binary bytecode and declaration files | No | Yes | Yes |
Identify the scenarios that require trustlist configurations, and configure a trustlist in obfuscation-rules.txt.
Build and verify the HAP package. If issues arise, review the trustlist configurations.
Once the HAP functions correctly, you can release the package.
Familiarize yourself with the three types of obfuscation configuration files and the obfuscation rule merging strategies. Understand the precautions provided in HAP Package Obfuscation Recommendations to ensure that your HAR functions correctly when it is used by HAP packages.
HAR packages can affect the obfuscation process of the main module that uses them. No matter whether obfuscation is enabled for your HAR, configure consumer-rules.txt to ensure that your HAR package functions correctly when the main module enables any obfuscation.
Due to the transitivity of consumer configurations, avoid enabling obfuscation directly in your HAR. Instead, use -keep-global-name and -keep-property-name to add specific names to a trustlist and minimize impact on dependent modules.
As unreleased static packages, local source code HAR packages are compiled and obfuscated together with the main module (such as HAP), rather than independently. Therefore, follow the HAP Package Obfuscation Recommendations.
Enable the obfuscation rules. Start with the recommended options and add other options as needed.
Understand the scenarios that require trustlist configurations, and configure a trustlist in the HAR.
Verify the functionality of the HAR package. Note that during the compilation of the current module, one round of obfuscation is triggered for your HAR. When the published HAR package is used by a dependent module, if the dependent module enables obfuscation, the code of the released HAR package will be re-obfuscated. Therefore, you should fully verify whether the HAR functions correctly when it is used by a dependent module with obfuscation enabled.
Release the HAR package.
When your HAR is used by other modules, pay attention to the following points:
The obfuscation adaptation process for bytecode HAR packages is similar to that for released source code HAR packages. However, since ArkGuard only supports bytecode obfuscation, bytecode HAR packages are not re-obfuscated when used by a dependent module. They are compiled and obfuscated only during the building of the current HAR module.
The obfuscation.txt file for integrated HSP packages is generated based on the current module's consumer-rules.txt, excluding the consumer-rules.txt or obfuscation.txt file of dependent modules.