Different package types serve distinct purposes and have unique build processes, which means you need to consider different factors when applying obfuscation. This topic offers tailored recommendations for three package types: HAP, HAR, and HSP, aiming to help you implement obfuscation effectively.
To better understand how obfuscation works with different package types, you are advised to first gain a thorough understanding of obfuscation principles and the obfuscation enabling process before configuring settings for specific package types. You are also advised to read Application Package Structure in Stage Model to familiarize yourself with the differences between package types.
In addition to the default obfuscation feature (which provides obfuscation for local variables and parameters), you are encouraged to configure the following basic obfuscation options in the obfuscation configuration file:
After the option configuration, you must configure the corresponding trustlists to ensure the application functions correctly.
Once the application is functioning correctly, you can enable additional features such as code compression (-compact) and log removal (-remove-log or -remove-nosideeffects-calls) to enhance the code obfuscation effect and prepare the release package.
Enable the obfuscation rules. Start with the recommended four 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 correct interaction with third-party libraries in the release state, ArkGuard automatically adds the export names and related properties of remote HARs in the project-level oh_modules directory 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 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. If issues arise, check whether the trustlist is complete.
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 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.
Since local source code HAR packages are obfuscated together with the main module, when multiple HAPs or HSPs depend on the same local source HAR, the obfuscation results of that local source HAR may differ across modules. When useNormalizedOHMUrl is enabled (specifically, by setting the useNormalizedOHMUrl field under the strictMode property to true in the project-level build-profile.json5 file), only one instance of the HAR is loaded at runtime. This prevents HAPs and HSPs from locating their corresponding HAR, leading to errors where the HAR's methods cannot be found when called.
Solution:
When your HAR is used by other modules, pay attention to the following points:
After obfuscation is enabled for the bytecode HAR package, you do not need to configure the trustlist in consumer-rules.txt, and the rest of the adaptation process remains consistent with that of the released source code HAR packages. This is because ArkGuard is a source code obfuscation tool that obfuscates the module only once when the bytecode HAR package is released. When other relying parties introduce the HAR and enable obfuscation, ArkGuard does not perform secondary obfuscation on the HAR.
The obfuscation.txt file for integrated HSPs is generated based on the current module's consumer-rules.txt, and does not include the consumer-rules.txt or obfuscation.txt files from dependent modules.
To ensure proper internal obfuscation of integrated HSPs, you can follow the HAP obfuscation recommendations. After an integrated HSP is released, it is necessary to verify that the APIs can be called properly when obfuscation is enabled.