1# Package-specific Bytecode Obfuscation Recommendations 2 3Different 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](https://gitee.com/openharmony/docs/blob/master/en/application-dev/quick-start/hap-package.md), [HAR](https://gitee.com/openharmony/docs/blob/master/en/application-dev/quick-start/har-package.md), and [HSP](https://gitee.com/openharmony/docs/blob/master/en/application-dev/quick-start/in-app-hsp.md), aiming to help you effectively implement obfuscation. 4 5To 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](https://gitee.com/openharmony/docs/blob/master/en/application-dev/quick-start/application-package-structure-stage.md) to grasp the differences between package types. 6 7## Recommended Obfuscation Options 8 9In addition to the default obfuscation feature, you are encouraged to configure the following basic obfuscation options in the obfuscation configuration file: 10 11- Top-level scope name obfuscation (**-enable-toplevel-obfuscation**) 12- Property name obfuscation (**-enable-property-obfuscation**) 13- Imported/Exported name obfuscation (**-enable-export-obfuscation**) 14- File name obfuscation (**-enable-filename-obfuscation**) 15 16After the option configuration, you must configure the corresponding trustlists to ensure the application functions correctly. 17 18* For new applications, you are advised to configure these options from the start and gradually add trustlist configurations as needed during development. 19* For existing applications, configure these options one by one in the suggested order. Compare the obfuscation products to understand the impact of each option, and refer to [Obfuscation Configuration Guidelines](bytecode-obfuscation-guide.md#obfuscation-configuration-guidelines) for troubleshooting. 20 21Once the application functions correctly, you can enable additional features like log removal (**-remove-log**) to prepare the release package. 22 23## HAP Package Obfuscation Recommendations 24 251. Enable the obfuscation rules. Start with the [recommended options](#recommended-obfuscation-options) and add other options as needed. 262. Familiarize yourself with the [obfuscation rule merging strategies](bytecode-obfuscation.md#obfuscation-rule-merging-strategies) to understand how these rules affect the current module during compilation. 273. 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: 28 1. When your HAP depends on a local HAR package, its code is obfuscated together with your HAP. The original and obfuscated names in this code will appear in the `obfuscation/nameCache.json` file. 29 2. When your HAP depends on a released source code HAR package, its code in the project-level **oh_modules** directory is obfuscated together with your HAP. The original and obfuscated names in this code will appear in the `obfuscation/nameCache.json` file. 30 3. When your HAP depends on a released bytecode HAR package or HSP package, only the binary bytecode and declaration files are used for compilation. Since ArkGuard supports only bytecode obfuscation, these files are not obfuscated. (This also maintains the consistency between the interfaces in the declaration file and those in the binary file.) However, if third-party libraries lack proper interface declarations or trustlist configurations in **consumer-rules**, their interfaces may be accidentally obfuscated. You should manually configure trustlists to ensure runtime correctness. 31 324. 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**. 335. 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. 34 35 The table below summarizes the compilation and obfuscation scenarios for common dependent packages. 36 37 | Dependent Module| Files Involved in Compilation| Obfuscated with the Current Module| Passes Retention Configuration to the Current Module| Collects Exported Names into a Trustlist| 38 | --- | --- | --- | --- | --- | 39 | [Local source code HAR package](#local-source-code-har-package)| Source code directory of the module in the local environment| Yes| Yes| No| 40 | [Released source code HAR package](#released-source-code-har-package)| Module directory under project-level oh_modules| Yes| Yes| Yes| 41 | [Released bytecode HAR package](#released-bytecode-har-package)| Binary bytecode and declaration files| No| Yes| Yes| 42 | [Local source code HSP package](#local-source-code-hsp-package)| NA | No| Yes| No| 43 | [Integrated HSP package](#integrated-hsp-package)| Binary bytecode and declaration files| No| Yes| Yes| 44 456. Identify the [scenarios that require trustlist configurations](bytecode-obfuscation.md#retention-options), and configure a trustlist in **obfuscation-rules.txt**. 467. Build and verify the HAP package. If issues arise, review the trustlist configurations. 478. Once the HAP functions correctly, you can release the package. 48 49## HAR Package Obfuscation Recommendations 50 51### General Recommendations 52 531. Familiarize yourself with the [three types of obfuscation configuration files](bytecode-obfuscation-guide.md#obfuscation-configuration-files) and the [obfuscation rule merging strategies](bytecode-obfuscation.md#obfuscation-rule-merging-strategies). Understand the precautions provided in [HAP Package Obfuscation Recommendations](#hap-package-obfuscation-recommendations) to ensure that your HAR functions correctly when it is used by HAP packages. 54 552. 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. 56 573. 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. 58 59### Local Source Code HAR Package 60 61As 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](#hap-package-obfuscation-recommendations). 62 63### Released Source Code HAR Package 64 651. Enable the obfuscation rules. Start with the [recommended options](#recommended-obfuscation-options) and add other options as needed. 662. Understand the [scenarios that require trustlist configurations](bytecode-obfuscation.md#retention-options), and configure a trustlist in the HAR. 67 68 * In **obfuscation-rules.txt**, configure the exported interface names and related property names of your HAR, as well as any names that should not be obfuscated during the build. 69 * In **consumer-rules.txt**, configure the names of interfaces and properties that should not be re-obfuscated. 70 713. 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. 72 734. Release the HAR package. 74 75> **NOTE** 76> 77> When your HAR is used by other modules, pay attention to the following points: 78> 79> * During obfuscation of dependent modules (such as HAP), ArkGuard collects exported names and properties from your released HAR package into a trustlist. 80> * During obfuscation of dependent modules (such as HAP), ArkGuard collects the trustlist from the **obfuscation.txt** file of your released HAR package but does not recursively collect from its dependencies (four-party libraries). This is because the trustlist of the four-party libraries has been collected during the construction of the third-party libraries. Therefore, if a released HAR package depends on other HAR packages, the versions of these other HAR packages should be fixed and should not be configured to automatically match the latest version. Otherwise, when the fourth-party libraries are upgraded and the trustlist is changed, it will not take effect in the HAP, leading to stability issues. 81 82### Released Bytecode HAR Package 83 84The obfuscation adaptation process for bytecode HAR packages is similar to that for released source code HAR packages. However, since ArkGuard only supports source code 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. 85 86## HSP Package Obfuscation Recommendations 87 88### General Recommendations 89 901. Familiarize yourself with the [three types of obfuscation configuration files](bytecode-obfuscation-guide.md#obfuscation-configuration-files) and the [obfuscation rule merging strategies](bytecode-obfuscation.md#obfuscation-rule-merging-strategies). Understand the precautions provided in [HAP Package Obfuscation Recommendations](#hap-package-obfuscation-recommendations) to ensure that your HSP functions correctly when it is used by HAP packages. 912. HSP packages are built independently and are built only once. Therefore, focus on the obfuscation effects within the module and ensure proper interface calls from other modules. 923. Due to the transitivity of consumer configurations, avoid enabling obfuscation directly in your HSP. Instead, use **-keep-global-name** and **-keep-property-name** to add specific names to a trustlist and minimize impact on dependent modules. 93 94### Local Source Code HSP Package 95 961. Clearly define external interfaces and properties that your source code HSP package provides, and configure these names in **obfuscation-rules.txt** and **consumer-rules.txt**. 972. Follow the [HAP Package Obfuscation Recommendations](#hap-package-obfuscation-recommendations) to ensure the obfuscation effects within the module. 983. Build and verify the HSP package together with the main module to ensure all interfaces provided by the HSP package work correctly. 99 100### Integrated HSP Package 101 1021. Clearly define external interfaces and properties that your integrated HSP package provides, and configure these names in **obfuscation-rules.txt**. There is no need to configure **consumer-rules.txt**, because the .tgz file contains both HAR and HSP packages, and the exported interfaces and properties defined in the declaration files of the HAR package are automatically collected into the trustlist. 1032. Follow the [HAP Package Obfuscation Recommendations](#hap-package-obfuscation-recommendations) to ensure the obfuscation effects within the module. 1043. Ensure that the interfaces that your integrated HSP package provides can be called correctly by dependent modules, especially when obfuscation is enabled. 105 106> **NOTE** 107> 108> 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. 109