1e41f4b71Sopenharmony_ci# Code Obfuscation
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciSource code obfuscation helps reduce the risk of application hacking as well as the application package size by shortening class names and member names.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciOriginally, the code obfuscation function was enabled by default in DevEco Studio. For a project developed on the stage model of API version 10 or later, code obfuscation was automatically performed on parameter names and local variable names when the [build mode is release](#usage-description). 
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciSince DevEco Studio 5.0.3.600, code obfuscation is disabled by default for new projects. If code obfuscation is enabled in the **build-profile.json5** file of the module, four options are enabled by default in the **obfuscation-rules.txt** file: **-enable-property-obfuscation**, **-enable-toplevel-obfuscation**, **-enable-filename-obfuscation**, and **-enable-export-obfuscation**. You can modify the configuration in this file.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci### Constraints
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci* Only the projects developed on the stage model are supported.
14e41f4b71Sopenharmony_ci* The build mode of the project is release.
15e41f4b71Sopenharmony_ci* The **disable-obfuscation** option is not configured for the module and the HAR on which the module depends.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci### Code Obfuscation Scope
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciIn a project, the following files can be obfuscated, and the cache files after obfuscation are stored in **build/[...]/release** in the module directory.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci* ArkTS files
22e41f4b71Sopenharmony_ci* TS files
23e41f4b71Sopenharmony_ci* JS files
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci## Enabling Code Obfuscation
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ciCode obfuscation has been integrated into the system and can be enabled for use in DevEco Studio.
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ciCurrently, only name obfuscation is provided. Other obfuscation capabilities deteriorate performance. You can use code obfuscation to obfuscate the following names:
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci* Parameter names and local variable names 
32e41f4b71Sopenharmony_ci* Top-level scope names 
33e41f4b71Sopenharmony_ci* Property names
34e41f4b71Sopenharmony_ci* Exported names
35e41f4b71Sopenharmony_ci* File names
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ciAfter code obfuscation is enabled, parameter names and local variable names will be obfuscated by default. No option needs to be configured. Obfuscation of top-level scope name, property names, exported names, and file names may cause runtime errors. You can enable or disable these obfuscation capabilities by configuring obfuscate options.
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ciWhen a project is created, the following content is automatically generated in the **build-profile.json5** file:
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci```
42e41f4b71Sopenharmony_ci"arkOptions": {
43e41f4b71Sopenharmony_ci  "obfuscation": {
44e41f4b71Sopenharmony_ci    "ruleOptions": {
45e41f4b71Sopenharmony_ci      "enable": true,
46e41f4b71Sopenharmony_ci      "files": ["./obfuscation-rules.txt"],
47e41f4b71Sopenharmony_ci    }
48e41f4b71Sopenharmony_ci  }
49e41f4b71Sopenharmony_ci}
50e41f4b71Sopenharmony_ci```
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ciWhen a library is created, the **consumerFiles** field is automatically generated in addition to the preceding content.
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci```
55e41f4b71Sopenharmony_ci"arkOptions": {
56e41f4b71Sopenharmony_ci  "obfuscation": {
57e41f4b71Sopenharmony_ci    "ruleOptions": {
58e41f4b71Sopenharmony_ci      "enable": true,
59e41f4b71Sopenharmony_ci      "files": ["./obfuscation-rules.txt"],
60e41f4b71Sopenharmony_ci    }
61e41f4b71Sopenharmony_ci    "consumerFiles": ["./consumer-rules.txt"]
62e41f4b71Sopenharmony_ci  }
63e41f4b71Sopenharmony_ci}
64e41f4b71Sopenharmony_ci```
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ciIf you disable code obfuscation and then want to enable it again, the value of **ruleOptions.enable** must be **true**.
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ciThe obfuscation configuration files specified in the **ruleOptions.files** field take effect when the HAP, HSP, or HAR is built.
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ciThe obfuscation configuration files specified in the **consumerFiles** field take effect when the module that depends on the library is built. The contents of these obfuscation configuration files are also merged into the **obfuscation.txt** file in the HAR.
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ciDuring the build of the HAP, HSP, or HAR, the final obfuscation rules are the combination of the **ruleOptions.files** field of the current module, the **consumerFiles** field of the dependent library, and the **obfuscation.txt** file in the dependent HAR.
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ciDuring HAR build, the **obfuscation.txt** file in the HAR contains the combination of the **consumerFiles** field of the current HAR, the **consumerFiles** field of the dependent library, and the **obfuscation.txt** file in the dependent HAR. No **obfuscation.txt** file is generated during HAP or HSP build. For details about the merge policy, see [Obfuscation Rule Merge Policy](#obfuscation-rule-merge-policy).
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci**Reminder of Rule Changes**
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ciSince DevEco Studio 5.0.3.600, code obfuscation is disabled by default (**"enable": false**) for a new project. In the **obfuscation-rules.txt** file, **-enable-property-obfuscation**, **-enable-toplevel-obfuscation**, **-enable-filename-obfuscation**, and **-enable-export-obfuscation** are enabled by default. Enabling the four options may cause the application to crash at runtime. You are advised to read [Troubleshooting](#troubleshooting) to rectify the fault.
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci### Obfuscation Rule Configuration File
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ciDuring the creation of a project or library, DevEco Studio automatically generates the **obfuscation-rules.txt** and **consumer-rules.txt** files. You can write obfuscation rules into these files or other user-defined files, and then add the file paths in **ruleOptions.files** and **consumerFiles**, as shown in the following example.
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci```
85e41f4b71Sopenharmony_ci"buildOption": {
86e41f4b71Sopenharmony_ci  "arkOptions": {
87e41f4b71Sopenharmony_ci    "obfuscation": {
88e41f4b71Sopenharmony_ci      "ruleOptions": {
89e41f4b71Sopenharmony_ci        "enable": true,
90e41f4b71Sopenharmony_ci        "files": ["./obfuscation-rules.txt", "./myrules.txt"], // Place myrules.txt in the same directory as the build-profile.json5 file.
91e41f4b71Sopenharmony_ci      }
92e41f4b71Sopenharmony_ci      "consumerFiles": ["./consumer-rules.txt", "./my-consumer-rules.txt"]
93e41f4b71Sopenharmony_ci    }
94e41f4b71Sopenharmony_ci  }
95e41f4b71Sopenharmony_ci}
96e41f4b71Sopenharmony_ci```
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci## Configuring Obfuscation Rules
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ciThere are two types of obfuscation rules: [obfuscate options](#obfuscate-options) and [keep options](#keep-options). The former provides a switch for multiple obfuscation capabilities, such as obfuscation of top-level scope names, property names, and file names. The latter provides the trustlist configuration of various obfuscation capabilities.
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci**NOTE**
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ciAny modification to the obfuscation configuration takes effect only after a full build of the application.
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ci### Obfuscate Options
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci#### -disable-obfuscation
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ciDisables code obfuscation. If this option is used, the built HAP, HSP, or HAR is not obfuscated.
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ci#### -enable-property-obfuscation
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ciEnables property obfuscation. If this option is used, all property names except the following are obfuscated:
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci* Property names of classes and objects that are directly imported or exported by using the **import** or **export** statement. For example, the property name **data** in the following example is not obfuscated.
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci    ```
119e41f4b71Sopenharmony_ci    export class MyClass {
120e41f4b71Sopenharmony_ci       data: string;
121e41f4b71Sopenharmony_ci    }
122e41f4b71Sopenharmony_ci    ```
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci* Property names in ArkUI components. For example, **message** and **data** in the following example are not obfuscated.
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci    ```
127e41f4b71Sopenharmony_ci    @Component struct MyExample {
128e41f4b71Sopenharmony_ci        @State message: string = "hello";
129e41f4b71Sopenharmony_ci        data: number[] = [];
130e41f4b71Sopenharmony_ci        ...
131e41f4b71Sopenharmony_ci    }
132e41f4b71Sopenharmony_ci    ```
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci* Property names specified by [keep options](#keep-options).
135e41f4b71Sopenharmony_ci* Property names in the SDK API list. The SDK API list is a name set automatically extracted from the SDK during build. The cache file is **systemApiCache.json**, which is stored in **build/default/cache/{...}/release/obfuscation** in the project directory.
136e41f4b71Sopenharmony_ci* String literal property names. For example, **"name"** and **"age"** in the following example are not obfuscated.
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci    ```
139e41f4b71Sopenharmony_ci    let person = {"name": "abc"};
140e41f4b71Sopenharmony_ci    person["age"] = 22;
141e41f4b71Sopenharmony_ci    ```
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ci    If you want to obfuscate the string literal property name, use the **-enable-string-property-obfuscation** option in addition to the current option, as follows:
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci    ```
146e41f4b71Sopenharmony_ci    -enable-property-obfuscation
147e41f4b71Sopenharmony_ci    -enable-string-property-obfuscation
148e41f4b71Sopenharmony_ci    ```
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci    **NOTE** 
151e41f4b71Sopenharmony_ci    
152e41f4b71Sopenharmony_ci    * If a string literal property name in the code contains special characters, for example, **let obj = {"\n": 123, "": 4, " ": 5}**, you are advised not to use the **-enable-string-property-obfuscation** option because these names may fail to be kept using [keep options](#keep-options). Special characters refer to characters other than lowercase letters a-z, uppercase letters A-Z, digits 0-9, and underscores (_).
153e41f4b71Sopenharmony_ci    
154e41f4b71Sopenharmony_ci    * The property trustlist of the SDK API list does not contain the string constants used in the declaration file. For example, the string **'ohos.want.action.home'** in the example is not included in the property trustlist.
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci      ```
157e41f4b71Sopenharmony_ci      // Part of the SDK API file @ohos.app.ability.wantConstant:
158e41f4b71Sopenharmony_ci      export enum Params {
159e41f4b71Sopenharmony_ci        ACTION_HOME = 'ohos.want.action.home'
160e41f4b71Sopenharmony_ci      }
161e41f4b71Sopenharmony_ci      // Source code example:
162e41f4b71Sopenharmony_ci      let params = obj['ohos.want.action.home'];
163e41f4b71Sopenharmony_ci      ```
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ci    When the **-enable-string-property-obfuscation** option is used, use the keep option if you want to keep the property names of the SDK API string constants used in the source code, for example, **obj['ohos.want.action.home']**.
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ci#### -enable-toplevel-obfuscation
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ciEnables top-level scope name obfuscation. If this option is used, all names of all top-level scopes except the following are obfuscated:
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ci* Top-level scope names imported or exported using the **import** or **export** statement.
172e41f4b71Sopenharmony_ci* Top-level scope names that are not declared in the current file.
173e41f4b71Sopenharmony_ci* Top-level scope names specified by [keep options](#keep-options).
174e41f4b71Sopenharmony_ci* Top-level scope names in the SDK API list.
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci#### -enable-filename-obfuscation
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ciEnables file or folder name obfuscation. If this option is used, all file or folder names except the following are obfuscated:
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci* File or folder names specified by the **main** and **types** fields in the **oh-package.json5** file.
181e41f4b71Sopenharmony_ci* File or folder names specified by the **srcEntry** field in the **module.json5** file of the module.
182e41f4b71Sopenharmony_ci* File or folder names specified by [-keep-file-name](#keep-options).
183e41f4b71Sopenharmony_ci* File or folder names in non-ECMAScript module reference mode. An ECMAScript module example is **import {foo} from './filename'**.
184e41f4b71Sopenharmony_ci* File or folder names in non-path reference mode. For example, **json5** in the example **import module from 'json5'** is not obfuscated. 
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci**NOTE** 
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ciThe system loads certain files during application running. For these files, manually configure a trustlist in the [`-keep-file-name`] option to prevent them from being obfuscated. Otherwise, the application may fail to run.
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ciIn the following scenarios, you need to manually configure a trustlist: 
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ci* The module contains an ability component. In this case, add all paths configured for **srcEntry** under the **abilities** field in **src/main/module.json5** to the trustlist. 
193e41f4b71Sopenharmony_ci* The module contains the multithreading service: Worker. In this case, add all paths under the **buildOption'-'sourceOption'-'workers'** field in **build-profiles.json5** to the trustlist.
194e41f4b71Sopenharmony_ci
195e41f4b71Sopenharmony_ci**Reminder**
196e41f4b71Sopenharmony_ci
197e41f4b71Sopenharmony_ciThe names of the compilation entry file, ability component file, and worker multithreaded file cannot be obfuscated and have been automatically added to the trustlist in DevEco Studio 5.0.3.500. No manual configuration is required. For other files that cannot be obfuscated, you need to manually configure their names in the trustlist.
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ci#### -enable-export-obfuscation
200e41f4b71Sopenharmony_ci
201e41f4b71Sopenharmony_ciEnables obfuscation for names of classes or objects that are directly imported or exported and their property names. If this option is used, the names directly imported or exported in the module are obfuscated, except the following:
202e41f4b71Sopenharmony_ci
203e41f4b71Sopenharmony_ci* Names of classes or objects exported from remote HARs (packages whose real paths are in oh_modules) and their property names.
204e41f4b71Sopenharmony_ci* Names and property names specified by [keep options](#keep-options).
205e41f4b71Sopenharmony_ci* Names in the SDK API list.
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_ci**NOTE**
208e41f4b71Sopenharmony_ci
209e41f4b71Sopenharmony_ci- To obfuscate property names in imported or exported classes, use both the **-enable-property-obfuscation** and **-enable-export-obfuscation** options. 
210e41f4b71Sopenharmony_ci- If the **-enable-export-obfuscation** option is used during HSP build, the externally exposed interfaces must be kept in the **obfuscation-rules.txt** file of the module.
211e41f4b71Sopenharmony_ci- During the build of an HAP, an HSP, and an HSP-dependent HAR, if the **-enable-export-obfuscation** option is used, the interfaces imported from the HSP must be kept in the **obfuscation-rules.txt** file in the module. 
212e41f4b71Sopenharmony_ci
213e41f4b71Sopenharmony_ci    ```
214e41f4b71Sopenharmony_ci    // Code example (entry file Index.ets in the HSP):
215e41f4b71Sopenharmony_ci    export { add, customApiName } from './src/main/ets/utils/Calc'
216e41f4b71Sopenharmony_ci
217e41f4b71Sopenharmony_ci    // Example of keeping an interface name:
218e41f4b71Sopenharmony_ci    // Configuration of the obfuscation-rules.txt file in the HSP and modules that depend on the HSP:
219e41f4b71Sopenharmony_ci    -keep-global-name
220e41f4b71Sopenharmony_ci    add
221e41f4b71Sopenharmony_ci    customApiName
222e41f4b71Sopenharmony_ci    ```
223e41f4b71Sopenharmony_ci
224e41f4b71Sopenharmony_ci#### -compact
225e41f4b71Sopenharmony_ci
226e41f4b71Sopenharmony_ciRemoves unnecessary spaces and all line feeds. If this option is used, all code is compressed to one line.
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ci**NOTE**
229e41f4b71Sopenharmony_ci
230e41f4b71Sopenharmony_ciThe stack information built in release mode contains the line number of code, but not the column number. Therefore, when the **compact** option is used, the source code cannot be located based on the line number in the stack information.
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci#### -remove-log
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ciRemoves the expressions involving direct calls to the **console.** statement in the following scenarios:
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ci- Calls at the top layer of a file.
237e41f4b71Sopenharmony_ci- Calls within a code block.
238e41f4b71Sopenharmony_ci- Calls within a module.
239e41f4b71Sopenharmony_ci- Calls within a switch statement.
240e41f4b71Sopenharmony_ci
241e41f4b71Sopenharmony_ci#### -print-namecache *filepath*
242e41f4b71Sopenharmony_ci
243e41f4b71Sopenharmony_ciSaves the name cache to the specified file path. The name cache contains mappings before and after name obfuscation. 
244e41f4b71Sopenharmony_ci
245e41f4b71Sopenharmony_ci**NOTE**
246e41f4b71Sopenharmony_ci
247e41f4b71Sopenharmony_ciA new **namecache.json** file is generated each time the module if fully built. Save a copy of the file each time you publish a new version.
248e41f4b71Sopenharmony_ci
249e41f4b71Sopenharmony_ci#### -apply-namecache *filepath*
250e41f4b71Sopenharmony_ci
251e41f4b71Sopenharmony_ciReuses the specified name cache file. The names will be obfuscated according to the cache mappings. If there is no corresponding name, a new random segment name is used.
252e41f4b71Sopenharmony_ci
253e41f4b71Sopenharmony_ciThis option should be used in incremental build scenarios.
254e41f4b71Sopenharmony_ci
255e41f4b71Sopenharmony_ciBy default, DevEco Studio saves cache files in a temporary cache directory and automatically applies the cache files during incremental build. 
256e41f4b71Sopenharmony_ci
257e41f4b71Sopenharmony_ciCache Directory: **build/default/cache/{...}/release/obfuscation**
258e41f4b71Sopenharmony_ci
259e41f4b71Sopenharmony_ci#### -remove-comments
260e41f4b71Sopenharmony_ci
261e41f4b71Sopenharmony_ciRemoves JsDoc comments from the declaration file generated after compilation. 
262e41f4b71Sopenharmony_ci
263e41f4b71Sopenharmony_ci**NOTE** 
264e41f4b71Sopenharmony_ci
265e41f4b71Sopenharmony_ciBy default, all comments in the source code file generated after the build are removed and cannot be kept.
266e41f4b71Sopenharmony_ci
267e41f4b71Sopenharmony_ciYou can configure **keep-comments** to keep the JsDoc comments in the declaration file from being obfuscated.
268e41f4b71Sopenharmony_ci
269e41f4b71Sopenharmony_ci#### -print-kept-names *filepath*
270e41f4b71Sopenharmony_ci
271e41f4b71Sopenharmony_ciPrints the keep-name list and full trustlist. The **filepath** parameter is optional.
272e41f4b71Sopenharmony_ci
273e41f4b71Sopenharmony_ciIf the **filepath** parameter is not specified, the keep-name list (**keptNames.json**) and full trustlist (**whitelist.json**) are output to the cache directory **build/default/cache/{...}/release/obfuscation** by default.
274e41f4b71Sopenharmony_ci
275e41f4b71Sopenharmony_ciIf the **filepath** parameter is specified, the keep-name list is also exported to the path specified by this parameter. The file path specified must be a relative path, which starts from the directory where the obfuscation configuration file is currently located. The file name extension in the file path must be .json.
276e41f4b71Sopenharmony_ci
277e41f4b71Sopenharmony_ciThe full trustlist (**whitelist.json**) contains all trustlists collected during module compilation. It is classified into the following types:
278e41f4b71Sopenharmony_ci
279e41f4b71Sopenharmony_ci(1) 'sdk': system APIs.
280e41f4b71Sopenharmony_ci
281e41f4b71Sopenharmony_ci(2) 'lang': keywords in the language.
282e41f4b71Sopenharmony_ci
283e41f4b71Sopenharmony_ci(3) 'conf': trustlist in the user-defined keep options.
284e41f4b71Sopenharmony_ci
285e41f4b71Sopenharmony_ci(4) 'struct': properties in ArkUI structs.
286e41f4b71Sopenharmony_ci
287e41f4b71Sopenharmony_ci(5) 'export': names and properties exported.
288e41f4b71Sopenharmony_ci
289e41f4b71Sopenharmony_ci(6) 'strProp': string properties.
290e41f4b71Sopenharmony_ci
291e41f4b71Sopenharmony_ci(7) 'enum': members in enums (available only during the compilation of the HAR module).
292e41f4b71Sopenharmony_ci
293e41f4b71Sopenharmony_ciThe **keptNames.json** file contains the names that are not obfuscated and the reasons why they are not obfuscated. There are seven reasons: The name is the same as that in the SDK trustlist, language trustlist, user-defined trustlist, struct trustlist, exported name trustlist, or string property trustlist (when string property obfuscation is disabled), or enum trustlist (during HAR module compilation).
294e41f4b71Sopenharmony_ci
295e41f4b71Sopenharmony_ci
296e41f4b71Sopenharmony_ci### Keep Options
297e41f4b71Sopenharmony_ci
298e41f4b71Sopenharmony_ci#### -keep-property-name *[,identifiers,...]*
299e41f4b71Sopenharmony_ci
300e41f4b71Sopenharmony_ciKeeps the specified property names from being obfuscated. Name wildcards are supported. An example is as follows.
301e41f4b71Sopenharmony_ci
302e41f4b71Sopenharmony_ci```
303e41f4b71Sopenharmony_ci-keep-property-name
304e41f4b71Sopenharmony_ciage
305e41f4b71Sopenharmony_cifirstName
306e41f4b71Sopenharmony_cilastName
307e41f4b71Sopenharmony_ci```
308e41f4b71Sopenharmony_ci
309e41f4b71Sopenharmony_ci**NOTE**
310e41f4b71Sopenharmony_ci
311e41f4b71Sopenharmony_ciThis option takes effect when **-enable-property-obfuscation** is used.
312e41f4b71Sopenharmony_ci
313e41f4b71Sopenharmony_ciWhat property names should be kept?
314e41f4b71Sopenharmony_ci
315e41f4b71Sopenharmony_ciFor safety, you are advised to keep all properties that are not accessed through dot notation.
316e41f4b71Sopenharmony_ci
317e41f4b71Sopenharmony_ciExample:
318e41f4b71Sopenharmony_ci
319e41f4b71Sopenharmony_ci```
320e41f4b71Sopenharmony_civar obj = {x0: 0, x1: 0, x2: 0};
321e41f4b71Sopenharmony_cifor (var i = 0; i <= 2; i++) {
322e41f4b71Sopenharmony_ci    console.log(obj['x' + i]); // x0, x1, and x2 should be kept.
323e41f4b71Sopenharmony_ci}
324e41f4b71Sopenharmony_ci
325e41f4b71Sopenharmony_ciObject.defineProperty(obj, 'y', {}); // y should be kept.
326e41f4b71Sopenharmony_ciconsole.log(obj.y);
327e41f4b71Sopenharmony_ci
328e41f4b71Sopenharmony_ciobj.s = 0;
329e41f4b71Sopenharmony_cilet key = 's';
330e41f4b71Sopenharmony_ciconsole.log(obj[key]);        // s should be kept.
331e41f4b71Sopenharmony_ci
332e41f4b71Sopenharmony_ciobj.u = 0;
333e41f4b71Sopenharmony_ciconsole.log(obj.u);           // u can be correctly obfuscated.
334e41f4b71Sopenharmony_ci
335e41f4b71Sopenharmony_ciobj.t = 0;
336e41f4b71Sopenharmony_ciconsole.log(obj['t']);        // When obfuscation of string literal property names is enabled, both t and 't' can be correctly obfuscated. However, it is recommended that 't' be kept.
337e41f4b71Sopenharmony_ci
338e41f4b71Sopenharmony_ciobj['v'] = 0;
339e41f4b71Sopenharmony_ciconsole.log(obj['v']);        // When obfuscation of string literal property names is enabled, 'v' can be correctly obfuscated. However, it is recommended that 'v' be kept.
340e41f4b71Sopenharmony_ci```
341e41f4b71Sopenharmony_ci
342e41f4b71Sopenharmony_ciIn the case of indirect exports, for example, **export MyClass** and **let a = MyClass; export {a}**, if you do not want to obfuscate property names, use [keep options](#keep-options) to keep them. For property names of directly exported classes or objects, such as **name** and **age** in the following example, if you do not want to obfuscate them, use [keep options](#keep-options) to keep them.
343e41f4b71Sopenharmony_ci
344e41f4b71Sopenharmony_ci```
345e41f4b71Sopenharmony_ciexport class MyClass {
346e41f4b71Sopenharmony_ci    person = {name: "123", age: 100};
347e41f4b71Sopenharmony_ci}
348e41f4b71Sopenharmony_ci```
349e41f4b71Sopenharmony_ci
350e41f4b71Sopenharmony_ciIf an API (for example, **foo** in the example) of the .so library needs to be used in the ArkTS/TS/JS file, manually keep the API name.
351e41f4b71Sopenharmony_ci
352e41f4b71Sopenharmony_ci```
353e41f4b71Sopenharmony_ciimport testNapi from 'library.so'
354e41f4b71Sopenharmony_citestNapi.foo() // foo should be kept. Example: -keep-property-name foo
355e41f4b71Sopenharmony_ci```
356e41f4b71Sopenharmony_ci
357e41f4b71Sopenharmony_ciManually keep the fields used in JSON files.
358e41f4b71Sopenharmony_ci
359e41f4b71Sopenharmony_ci```
360e41f4b71Sopenharmony_ciconst jsonData = ('./1.json')
361e41f4b71Sopenharmony_cilet jsonStr = JSON.parse(jsonData)
362e41f4b71Sopenharmony_cilet jsonObj = jsonStr.jsonProperty // jsonProperty should be kept.
363e41f4b71Sopenharmony_ci```
364e41f4b71Sopenharmony_ci
365e41f4b71Sopenharmony_ciManually keep database-related fields.
366e41f4b71Sopenharmony_ci
367e41f4b71Sopenharmony_ci```
368e41f4b71Sopenharmony_ciconst dataToInsert = {  
369e41f4b71Sopenharmony_ci  value1: 'example1',   // value1 should be kept.
370e41f4b71Sopenharmony_ci};
371e41f4b71Sopenharmony_ci```
372e41f4b71Sopenharmony_ci
373e41f4b71Sopenharmony_ci#### -keep-global-name *[,identifiers,...]*
374e41f4b71Sopenharmony_ci
375e41f4b71Sopenharmony_ciKeep the top-level scope names from being obfuscated. Name wildcards are supported. Example:
376e41f4b71Sopenharmony_ci
377e41f4b71Sopenharmony_ci```
378e41f4b71Sopenharmony_ci-keep-global-name
379e41f4b71Sopenharmony_ciPerson
380e41f4b71Sopenharmony_ciprintPersonName
381e41f4b71Sopenharmony_ci```
382e41f4b71Sopenharmony_ci
383e41f4b71Sopenharmony_ciWhat top-level scope names should be kept?
384e41f4b71Sopenharmony_ci
385e41f4b71Sopenharmony_ciIn JavaScript, variables in the top-level scope are properties of **globalThis**. If **globalThis** is used to access a global variable in the code, the variable name should be kept.
386e41f4b71Sopenharmony_ci
387e41f4b71Sopenharmony_ciExample:
388e41f4b71Sopenharmony_ci
389e41f4b71Sopenharmony_ci```
390e41f4b71Sopenharmony_civar a = 0;
391e41f4b71Sopenharmony_ciconsole.log(globalThis.a); // a should be kept.
392e41f4b71Sopenharmony_ci
393e41f4b71Sopenharmony_cifunction foo(){}
394e41f4b71Sopenharmony_ciglobalThis.foo();           // foo should be kept.
395e41f4b71Sopenharmony_ci
396e41f4b71Sopenharmony_civar c = 0;
397e41f4b71Sopenharmony_ciconsole.log(c);             // c can be correctly obfuscated.
398e41f4b71Sopenharmony_ci
399e41f4b71Sopenharmony_cifunction bar(){}
400e41f4b71Sopenharmony_cibar();                      // bar can be correctly obfuscated.
401e41f4b71Sopenharmony_ci
402e41f4b71Sopenharmony_ciclass MyClass {}
403e41f4b71Sopenharmony_cilet d = new MyClass();      // MyClass can be correctly obfuscated.
404e41f4b71Sopenharmony_ci```
405e41f4b71Sopenharmony_ci
406e41f4b71Sopenharmony_ci#### -keep-file-name *[,identifiers,...]*
407e41f4b71Sopenharmony_ci
408e41f4b71Sopenharmony_ciKeeps the file or folder names from being obfuscated. You do not need to specify the file name extension. Name wildcards are supported. Example:
409e41f4b71Sopenharmony_ci
410e41f4b71Sopenharmony_ci```
411e41f4b71Sopenharmony_ci-keep-file-name
412e41f4b71Sopenharmony_ciindex
413e41f4b71Sopenharmony_cientry
414e41f4b71Sopenharmony_ci```
415e41f4b71Sopenharmony_ci
416e41f4b71Sopenharmony_ciWhat file names should be kept?
417e41f4b71Sopenharmony_ci
418e41f4b71Sopenharmony_ci```
419e41f4b71Sopenharmony_ciconst module1 = require('./file1')   // ArkTS does not support CommonJS. Therefore, the path reference should be kept.
420e41f4b71Sopenharmony_ciconst moduleName = './file2'
421e41f4b71Sopenharmony_ciconst module2 = import(moduleName)    // In dynamic reference mode, it cannot tell whether moduleName is a path. Therefore, the module name should be kept.
422e41f4b71Sopenharmony_ci```
423e41f4b71Sopenharmony_ci
424e41f4b71Sopenharmony_ci#### -keep-comments *[,identifiers,...]*
425e41f4b71Sopenharmony_ci
426e41f4b71Sopenharmony_ciKeeps the classes, functions, namespaces, enums, structs, interfaces, modules, types, and JsDoc comments above properties in the declaration file generated after compilation from being obfuscated. Name wildcards are supported. For example, to keep the JSDoc comments above the **Human** class in the declaration file, use the following configuration:
427e41f4b71Sopenharmony_ci
428e41f4b71Sopenharmony_ci```
429e41f4b71Sopenharmony_ci-keep-comments
430e41f4b71Sopenharmony_ciHuman
431e41f4b71Sopenharmony_ci```
432e41f4b71Sopenharmony_ci
433e41f4b71Sopenharmony_ci**NOTE**
434e41f4b71Sopenharmony_ci
435e41f4b71Sopenharmony_ci- This option takes effect when **-remove-comments** is used.
436e41f4b71Sopenharmony_ci- If the classes, functions, namespaces, enums, structs, interfaces, modules, types, and property names in the declaration file generated after compilation are confused, the JsDoc comments above the element cannot be kept using **-keep-comments**. For example, when **exportClass** is configured in **-keep-comments**, if the class name is **exportClass** obfuscated, its JSDoc comments cannot be kept:
437e41f4b71Sopenharmony_ci
438e41f4b71Sopenharmony_ci```
439e41f4b71Sopenharmony_ci/*
440e41f4b71Sopenharmony_ci * @class exportClass
441e41f4b71Sopenharmony_ci */
442e41f4b71Sopenharmony_ciexport class exportClass {}
443e41f4b71Sopenharmony_ci```
444e41f4b71Sopenharmony_ci
445e41f4b71Sopenharmony_ci#### -keep-dts *filepath*
446e41f4b71Sopenharmony_ci
447e41f4b71Sopenharmony_ciKeeps the names in the .d.ts file in the specified absolute path from being obfuscated. The file path can be a directory. In this case, the names in all .d.ts files in the specified directory are kept.
448e41f4b71Sopenharmony_ci
449e41f4b71Sopenharmony_ci#### -keep *filepath*
450e41f4b71Sopenharmony_ci
451e41f4b71Sopenharmony_ciKeeps all names (such as variable names, class names, and property names) in the specified relative path from being obfuscated. The path can be a file or directory. If the path is a directory, the files in the directory and subdirectories are not obfuscated. 
452e41f4b71Sopenharmony_ci
453e41f4b71Sopenharmony_ciThe path must be a relative path. **./** and **../** are relative to the directory where the obfuscation configuration file is located. Path wildcards are supported.
454e41f4b71Sopenharmony_ci
455e41f4b71Sopenharmony_ci```
456e41f4b71Sopenharmony_ci-keep
457e41f4b71Sopenharmony_ci./src/main/ets/fileName.ts   // Names in the fileName.ts file are not obfuscated.
458e41f4b71Sopenharmony_ci../folder                    // Names in all the files under the folder directory and its subdirectories are not obfuscated.
459e41f4b71Sopenharmony_ci../oh_modules/json5          // Names in all the files in the imported third-party library json5 are not obfuscated.
460e41f4b71Sopenharmony_ci```
461e41f4b71Sopenharmony_ci
462e41f4b71Sopenharmony_ci**NOTE**
463e41f4b71Sopenharmony_ci
464e41f4b71Sopenharmony_ciThis option does not affect the capability provided by the **-enable-filename-obfuscation** option.
465e41f4b71Sopenharmony_ci
466e41f4b71Sopenharmony_ci#### Wildcards Supported by Keep Options
467e41f4b71Sopenharmony_ci
468e41f4b71Sopenharmony_ci##### Name Wildcards
469e41f4b71Sopenharmony_ci
470e41f4b71Sopenharmony_ciThe table below lists the name wildcards supported.
471e41f4b71Sopenharmony_ci
472e41f4b71Sopenharmony_ci| Wildcard| Description                  | Example                                      |
473e41f4b71Sopenharmony_ci| ------ | ---------------------- | ------------------------------------------ |
474e41f4b71Sopenharmony_ci| ?      | Matches any single character.      | "AB?" matches "ABC", but not "AB".        |
475e41f4b71Sopenharmony_ci| \*     | Matches any number of characters.| "\*AB\*" matches "AB", "aABb", "cAB", and "ABc".|
476e41f4b71Sopenharmony_ci
477e41f4b71Sopenharmony_ci**Use Example**
478e41f4b71Sopenharmony_ci
479e41f4b71Sopenharmony_ciKeep all property names that start with **a**.
480e41f4b71Sopenharmony_ci
481e41f4b71Sopenharmony_ci```
482e41f4b71Sopenharmony_ci-keep-property-name
483e41f4b71Sopenharmony_cia*
484e41f4b71Sopenharmony_ci```
485e41f4b71Sopenharmony_ci
486e41f4b71Sopenharmony_ciKeep all single-character property names.
487e41f4b71Sopenharmony_ci
488e41f4b71Sopenharmony_ci```
489e41f4b71Sopenharmony_ci-keep-property-name
490e41f4b71Sopenharmony_ci?
491e41f4b71Sopenharmony_ci```
492e41f4b71Sopenharmony_ci
493e41f4b71Sopenharmony_ciKeep all property names.
494e41f4b71Sopenharmony_ci
495e41f4b71Sopenharmony_ci```
496e41f4b71Sopenharmony_ci-keep-property-name
497e41f4b71Sopenharmony_ci*
498e41f4b71Sopenharmony_ci```
499e41f4b71Sopenharmony_ci
500e41f4b71Sopenharmony_ci##### Path Wildcards
501e41f4b71Sopenharmony_ci
502e41f4b71Sopenharmony_ciThe table below lists the path wildcards supported.
503e41f4b71Sopenharmony_ci
504e41f4b71Sopenharmony_ci| Wildcard| Description                                                                    | Example                                             |
505e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------------------------ | ------------------------------------------------- |
506e41f4b71Sopenharmony_ci| ?     | Matches any single character except the path separator (/).                                     | "../a?" matches "../ab", but not "../a/".        |
507e41f4b71Sopenharmony_ci| \*      | Matches any number of characters except the path separator (/).                               | "../a*/c" matches "../ab/c", but not "../ab/d/s/c".|
508e41f4b71Sopenharmony_ci| \*\*   | Matches any number of characters.                                                  | "../a**/c" matches "../ab/c" and "../ab/d/s/c". |
509e41f4b71Sopenharmony_ci| !      | Negation. It can only be placed at the beginning of a path to exclude a certain case configured in the trustlist.| "!../a/b/c.ets" indicates all paths other than "../a/b/c.ets".          |
510e41f4b71Sopenharmony_ci
511e41f4b71Sopenharmony_ci**Use Example**
512e41f4b71Sopenharmony_ci
513e41f4b71Sopenharmony_ciKeep the **c.ets** file in the **../a/b/** directory (excluding subdirectories).
514e41f4b71Sopenharmony_ci
515e41f4b71Sopenharmony_ci```
516e41f4b71Sopenharmony_ci-keep
517e41f4b71Sopenharmony_ci../a/b/*/c.ets
518e41f4b71Sopenharmony_ci```
519e41f4b71Sopenharmony_ci
520e41f4b71Sopenharmony_ciKeep the **c.ets** file in the **../a/b/** directory and its subdirectories.
521e41f4b71Sopenharmony_ci
522e41f4b71Sopenharmony_ci```
523e41f4b71Sopenharmony_ci-keep
524e41f4b71Sopenharmony_ci../a/b/**/c.ets
525e41f4b71Sopenharmony_ci```
526e41f4b71Sopenharmony_ci
527e41f4b71Sopenharmony_ciKeep all files except the **c.ets** file in the **../a/b/** directory. The exclamation mark (!) cannot be used alone. It can only be used to exclude existing cases in the trustlist.
528e41f4b71Sopenharmony_ci
529e41f4b71Sopenharmony_ci```
530e41f4b71Sopenharmony_ci-keep
531e41f4b71Sopenharmony_ci../a/b/
532e41f4b71Sopenharmony_ci!../a/b/c.ets
533e41f4b71Sopenharmony_ci```
534e41f4b71Sopenharmony_ci
535e41f4b71Sopenharmony_ciKeep all the files in the **../a/** directory (excluding subdirectories).
536e41f4b71Sopenharmony_ci
537e41f4b71Sopenharmony_ci```
538e41f4b71Sopenharmony_ci-keep
539e41f4b71Sopenharmony_ci../a/*
540e41f4b71Sopenharmony_ci```
541e41f4b71Sopenharmony_ci
542e41f4b71Sopenharmony_ciKeep all the files in the **../a/** directory and its subdirectories.
543e41f4b71Sopenharmony_ci
544e41f4b71Sopenharmony_ci```
545e41f4b71Sopenharmony_ci-keep
546e41f4b71Sopenharmony_ci../a/**
547e41f4b71Sopenharmony_ci```
548e41f4b71Sopenharmony_ci
549e41f4b71Sopenharmony_ciKeep all the files in the module.
550e41f4b71Sopenharmony_ci
551e41f4b71Sopenharmony_ci```
552e41f4b71Sopenharmony_ci-keep
553e41f4b71Sopenharmony_ci./**
554e41f4b71Sopenharmony_ci```
555e41f4b71Sopenharmony_ci
556e41f4b71Sopenharmony_ci**NOTE**
557e41f4b71Sopenharmony_ci
558e41f4b71Sopenharmony_ci- In these options, the wildcards *, ?, and ! cannot be used for other meanings.
559e41f4b71Sopenharmony_ci  Example:
560e41f4b71Sopenharmony_ci  
561e41f4b71Sopenharmony_ci  ```
562e41f4b71Sopenharmony_ci  class A {
563e41f4b71Sopenharmony_ci    '*'= 1
564e41f4b71Sopenharmony_ci  }
565e41f4b71Sopenharmony_ci  
566e41f4b71Sopenharmony_ci  -keep-property-name
567e41f4b71Sopenharmony_ci  *
568e41f4b71Sopenharmony_ci  ```
569e41f4b71Sopenharmony_ci
570e41f4b71Sopenharmony_ciIn this example, * indicates any number of characters, and all property names are kept (not obfuscated). It does not mean that only the * property is kept.
571e41f4b71Sopenharmony_ci
572e41f4b71Sopenharmony_ci- In the **-keep** option, only the path format / is allowed. The path format \ or \\ is not.
573e41f4b71Sopenharmony_ci
574e41f4b71Sopenharmony_ci### Comments
575e41f4b71Sopenharmony_ci
576e41f4b71Sopenharmony_ciYou can use **#** to comment out an obfuscation rule file. The line that starts with **#** is treated as a comment.
577e41f4b71Sopenharmony_ci
578e41f4b71Sopenharmony_ciExample:
579e41f4b71Sopenharmony_ci
580e41f4b71Sopenharmony_ci```
581e41f4b71Sopenharmony_ci# white list for MainAbility.ets
582e41f4b71Sopenharmony_ci-keep-global-name
583e41f4b71Sopenharmony_ciMyComponent
584e41f4b71Sopenharmony_ciGlobalFunction
585e41f4b71Sopenharmony_ci
586e41f4b71Sopenharmony_ci-keep-property-name # white list for dynamic property names
587e41f4b71Sopenharmony_cifirstName
588e41f4b71Sopenharmony_cilastName
589e41f4b71Sopenharmony_ciage
590e41f4b71Sopenharmony_ci```
591e41f4b71Sopenharmony_ci
592e41f4b71Sopenharmony_ciDuring HAR build, comments are not merged into the final **obfuscation.txt** file.
593e41f4b71Sopenharmony_ci
594e41f4b71Sopenharmony_ci### Obfuscation Rule Merge Policy
595e41f4b71Sopenharmony_ci
596e41f4b71Sopenharmony_ciA project often has many obfuscation rule files, which are:
597e41f4b71Sopenharmony_ci
598e41f4b71Sopenharmony_ci* **ruleOptions.files** of the main project (the project being built)
599e41f4b71Sopenharmony_ci* File specified by the **consumerFiles** field in the local dependent libraries
600e41f4b71Sopenharmony_ci* **obfuscation.txt** file in remote dependent HARs
601e41f4b71Sopenharmony_ci
602e41f4b71Sopenharmony_ciWhen the main project is built, the obfuscation rules in these files are merged according to the following policy (in pseudo code):
603e41f4b71Sopenharmony_ci
604e41f4b71Sopenharmony_ci```
605e41f4b71Sopenharmony_cilet `listRules` indicates the list of all obfuscation rule files mentioned above.
606e41f4b71Sopenharmony_cilet finalRule = {
607e41f4b71Sopenharmony_ci    disableObfuscation: false,
608e41f4b71Sopenharmony_ci    enablePropertyObfuscation: false,
609e41f4b71Sopenharmony_ci    enableToplevelObfuscation: false,
610e41f4b71Sopenharmony_ci    compact: false,
611e41f4b71Sopenharmony_ci    removeLog: false,
612e41f4b71Sopenharmony_ci    keepPropertyName: [],
613e41f4b71Sopenharmony_ci    keepGlobalName: [],
614e41f4b71Sopenharmony_ci    keepDts: [],
615e41f4b71Sopenharmony_ci    printNamecache: string,
616e41f4b71Sopenharmony_ci    applyNamecache: string
617e41f4b71Sopenharmony_ci}
618e41f4b71Sopenharmony_cifor each file in `listRules`:
619e41f4b71Sopenharmony_ci    for each option in file:
620e41f4b71Sopenharmony_ci        switch(option) {
621e41f4b71Sopenharmony_ci            case -disable-obfuscation:
622e41f4b71Sopenharmony_ci                finalRule.disableObfuscation = true;
623e41f4b71Sopenharmony_ci                continue;
624e41f4b71Sopenharmony_ci            case -enable-property-obfuscation:
625e41f4b71Sopenharmony_ci                finalRule.enablePropertyObfuscation = true;
626e41f4b71Sopenharmony_ci                continue;
627e41f4b71Sopenharmony_ci            case -enable-toplevel-obfuscation:
628e41f4b71Sopenharmony_ci                finalRule.enableToplevelObfuscation = true;
629e41f4b71Sopenharmony_ci                continue;
630e41f4b71Sopenharmony_ci            case -compact:
631e41f4b71Sopenharmony_ci                finalRule.compact = true;
632e41f4b71Sopenharmony_ci                continue;
633e41f4b71Sopenharmony_ci            case -remove-log:
634e41f4b71Sopenharmony_ci                finalRule.removeLog = true;
635e41f4b71Sopenharmony_ci                continue;
636e41f4b71Sopenharmony_ci            case -print-namecache:
637e41f4b71Sopenharmony_ci                finalRule.printNamecache = #{specifiedPathName};
638e41f4b71Sopenharmony_ci                continue;
639e41f4b71Sopenharmony_ci            case -apply-namecache:
640e41f4b71Sopenharmony_ci                finalRule.applyNamecache = #{specifiedPathName};
641e41f4b71Sopenharmony_ci                continue;
642e41f4b71Sopenharmony_ci            case -keep-property-name:
643e41f4b71Sopenharmony_ci                finalRule.keepPropertyName.push(#{specifiedName});
644e41f4b71Sopenharmony_ci                continue;
645e41f4b71Sopenharmony_ci            case -keep-global-name:
646e41f4b71Sopenharmony_ci                finalRule.keepGlobalName.push(#{specifiedName});
647e41f4b71Sopenharmony_ci                continue;
648e41f4b71Sopenharmony_ci            case -keep-dts:
649e41f4b71Sopenharmony_ci                finalRule.keepDts.push(#{specifiedPath});
650e41f4b71Sopenharmony_ci                continue;
651e41f4b71Sopenharmony_ci        }
652e41f4b71Sopenharmony_ci    end-for
653e41f4b71Sopenharmony_ciend-for
654e41f4b71Sopenharmony_ci```
655e41f4b71Sopenharmony_ci
656e41f4b71Sopenharmony_ciThe final obfuscation rule comes from the object **finalRule**.
657e41f4b71Sopenharmony_ci
658e41f4b71Sopenharmony_ciDuring HAR build, the final **obfuscation.txt** file comes from the combination of the **consumerFiles** field of the main project and the local dependent libraries, and the **obfuscation.txt** file of the dependent HARs. The merge policy is the same as the preceding one except for the following differences:
659e41f4b71Sopenharmony_ci
660e41f4b71Sopenharmony_ci* The options **-keep-global-name** and **-keep-property-name** are used instead of **-keep-dts**.
661e41f4b71Sopenharmony_ci* The **-print-namecache** and **apply-namecache** options are ignored and will not appear in the final **obfuscation.txt** file.
662e41f4b71Sopenharmony_ci
663e41f4b71Sopenharmony_ci## Retracing Stack Traces
664e41f4b71Sopenharmony_ci
665e41f4b71Sopenharmony_ciThe function names of an application project after obfuscation are changed. As a result, the stack trace printed during crash is more difficult to understand because it is not completely consistent with the source code. You can use the hstack plugin in DevEco Studio Command Line Tools to restore the source stack trace and analyze problems. The **sourceMap.json** file generated during compilation and the obfuscation name mapping file **nameCache.json** are essential for de-obfuscation. Ensure to back them up locally.
666e41f4b71Sopenharmony_ci
667e41f4b71Sopenharmony_ci![obfuscation-product](figures/obfuscation-product.png)
668e41f4b71Sopenharmony_ci
669e41f4b71Sopenharmony_ci## Usage Description
670e41f4b71Sopenharmony_ci
671e41f4b71Sopenharmony_ci* Currently, custom obfuscation plugins cannot be inserted into the hvigor build process.
672e41f4b71Sopenharmony_ci* If a module that depends on an obfuscated HAR enables obfuscation, the HAR will be obfuscated again.
673e41f4b71Sopenharmony_ci* To enable the release build mode, select **Product** in the upper right corner of DevEco Studio and set **Build Mode** to **release**.
674e41f4b71Sopenharmony_ci
675e41f4b71Sopenharmony_ci  ![product-release](figures/product-release.png)
676e41f4b71Sopenharmony_ci
677e41f4b71Sopenharmony_ci## Appendix
678e41f4b71Sopenharmony_ci
679e41f4b71Sopenharmony_ci### Mappings Between Obfuscate Options and Minimum SDK Versions
680e41f4b71Sopenharmony_ci
681e41f4b71Sopenharmony_ci| Obfuscate Option| Description | Minimum SDK Version|
682e41f4b71Sopenharmony_ci| ------- | --------- | ------ |
683e41f4b71Sopenharmony_ci| -disable-obfuscation         | Disables obfuscation.| 4.0.9.2 |
684e41f4b71Sopenharmony_ci| -enable-property-obfuscation | Enables property obfuscation.| 4.0.9.2 |
685e41f4b71Sopenharmony_ci| -enable-string-property-obfuscation | Enables obfuscation for string literal property names.| 4.0.9.2 |
686e41f4b71Sopenharmony_ci| -enable-toplevel-obfuscation | Enables top-level scope name obfuscation.| 4.0.9.2 |
687e41f4b71Sopenharmony_ci| -enable-filename-obfuscation | Enables file or folder name obfuscation for the HAR.<br> Enables file or folder name obfuscation for the HAP/HSP.| 4.1.5.3 <br> 5.0.0.19 |
688e41f4b71Sopenharmony_ci| -enable-export-obfuscation   | Enables obfuscation for imported or exported names.| 4.1.5.3 |
689e41f4b71Sopenharmony_ci| -compact                     | Removes unnecessary spaces and all line feeds.| 4.0.9.2 |
690e41f4b71Sopenharmony_ci| -remove-log                  | Removes the expressions involving direct calls to the **console.** statement in specific scenarios.| 4.0.9.2 |
691e41f4b71Sopenharmony_ci| -print-namecache             | Saves the name cache to the specified file path.| 4.0.9.2 |
692e41f4b71Sopenharmony_ci| -apply-namecache             | Reuses the specified name cache file.| 4.0.9.2 |
693e41f4b71Sopenharmony_ci| -remove-comments             | Removes all comments in the file.| 4.1.5.3 |
694e41f4b71Sopenharmony_ci| -keep-property-name          | Keeps property names from being obfuscated.| 4.0.9.2 |
695e41f4b71Sopenharmony_ci| -keep-global-name            | Keeps top-level scope names from being obfuscated.| 4.0.9.2 |
696e41f4b71Sopenharmony_ci| -keep-file-name              | Keeps file or folder names in the HAR from being obfuscated.<br> Keeps file or folder names in the HAP/HSP from being obfuscated.| 4.1.5.3 <br> 5.0.0.19 |
697e41f4b71Sopenharmony_ci| -keep-dts                    | Keeps the names in the .d.ts file in the specified path from being obfuscated.| 4.0.9.2 |
698e41f4b71Sopenharmony_ci| -keep-comments               | Keeps the classes, functions, namespaces, enums, structs, interfaces, modules, types, and JsDoc comments above properties in the declaration file generated after compilation from being obfuscated.| 4.1.5.3 |
699e41f4b71Sopenharmony_ci| -keep                        | Keeps all names in the specified path from being obfuscated.| 5.0.0.18 |
700e41f4b71Sopenharmony_ci| Wildcard                      | The keep options of the name classes and path classes support wildcards.| 5.0.0.24 |
701e41f4b71Sopenharmony_ci
702e41f4b71Sopenharmony_ci### Viewing the Obfuscation Effect
703e41f4b71Sopenharmony_ci
704e41f4b71Sopenharmony_ciYou can find the obfuscated files, name mapping file, and system API trustlist file in the **build** directory of the build product.
705e41f4b71Sopenharmony_ci
706e41f4b71Sopenharmony_ci- Obfuscated file directory: build/default/[...]/release/moduleName
707e41f4b71Sopenharmony_ci- Directory of the name mapping file and system API trustlist file: build/default/[...]/release/obfuscation
708e41f4b71Sopenharmony_ci  - The name mapping file, named **nameCache.json**, records the mappings between source code names and names after obfuscation.
709e41f4b71Sopenharmony_ci  - The system API trustlist file, named **systemApiCache.json**, records the APIs and property names that will be kept.
710e41f4b71Sopenharmony_ci
711e41f4b71Sopenharmony_ci  ![build-product](figures/build-product.png)
712e41f4b71Sopenharmony_ci
713e41f4b71Sopenharmony_ci### Troubleshooting
714e41f4b71Sopenharmony_ci
715e41f4b71Sopenharmony_ci1. Configure the **-disable-obfuscation** option in **obfuscation-rules.txt** to disable obfuscation, and check whether the exception is caused by obfuscation.
716e41f4b71Sopenharmony_ci2. If the function is abnormal after obfuscation is enabled, read the document to understand the capabilities of obfuscation rules, such as [-enable-property-obfuscation](#keep-options), [-enable-toplevel-obfuscation](#keep-options), [-enable-filename-obfuscation](#keep-options), and [-enable-export-obfuscation](#keep-options), and the syntax scenarios that require the configuration of a [trustlist](#keep-options) to ensure that the application functions properly. The following briefly describes the four keep options that are enabled by default. For details, see the complete description of the corresponding options.
717e41f4b71Sopenharmony_ci    1. [-enable-toplevel-obfuscation](#keep-options): keeps top-level scope name from being obfuscated.
718e41f4b71Sopenharmony_ci    2. [-enable-property-obfuscation](#keep-options): keeps properties from being obfuscated. You need to use [-keep-property-name](#keep-options) to configure a trustlist for specified property names in scenarios where obfuscation is not allowed, such as network data access, JSON field access, dynamic property access, and .so library API calls.
719e41f4b71Sopenharmony_ci    3. [-enable-export-obfuscation](#keep-options): keeps exported names from being obfuscated. Generally, this option is used together with the preceding two options. You need to use [-keep-global-name](#keep-options) to configure a trustlist for exported or imported names in scenarios where external APIs of the module cannot be obfuscated.
720e41f4b71Sopenharmony_ci    4. [-enable-filename-obfuscation]: keeps file names from being obfuscated. You need to use [-keep-file-name](#keep-options) to configure a trustlist for file paths and names in scenarios where file paths are dynamically imported or directly loaded at runtime.
721e41f4b71Sopenharmony_ci3. If you find a [troubleshooting](#troubleshooting) case similar to your scenario, you can quickly resolve the issue by following the recommended solution.
722e41f4b71Sopenharmony_ci4. If no similar case is found, you should identify the issue by examining the configuration functions. If the relevant functions are unnecessary, you can remove the corresponding configuration items.
723e41f4b71Sopenharmony_ci5. Analyze application crashes during running as follows:
724e41f4b71Sopenharmony_ci    1. Open the application run log or click the Crash dialog box in DevEco Studio to find the crash stack.
725e41f4b71Sopenharmony_ci    2. The line number in the crash stack is the line number of the [build product](#viewing-the-obfuscation-effect), and the method name may also be the obfuscated name. Therefore, you are advised to check the build product based on the crash stack, analyze the names that cannot be obfuscated, and add them to the trustlist.
726e41f4b71Sopenharmony_ci
727e41f4b71Sopenharmony_ci### **FAQs**
728e41f4b71Sopenharmony_ci
729e41f4b71Sopenharmony_ci#### Errors That May Occur When -enable-property-obfuscation Is Configured
730e41f4b71Sopenharmony_ci
731e41f4b71Sopenharmony_ci**Case 1**: The error message "Cannot read property 'xxx' of undefined" is reported.
732e41f4b71Sopenharmony_ci
733e41f4b71Sopenharmony_ci```
734e41f4b71Sopenharmony_ci// Before obfuscation
735e41f4b71Sopenharmony_ciconst jsonData = ('./1.json')
736e41f4b71Sopenharmony_cilet jsonStr = JSON.parse(jsonData)
737e41f4b71Sopenharmony_cilet jsonObj = jsonStr.jsonProperty
738e41f4b71Sopenharmony_ci
739e41f4b71Sopenharmony_ci// After obfuscation
740e41f4b71Sopenharmony_ciconst jsonData = ('./1.json')
741e41f4b71Sopenharmony_cilet jsonStr = JSON.parse(jsonData)
742e41f4b71Sopenharmony_cilet jsonObj = jsonStr.i
743e41f4b71Sopenharmony_ci```
744e41f4b71Sopenharmony_ci
745e41f4b71Sopenharmony_ciAfter property obfuscation is enabled, **jsonProperty** is obfuscated as a random character **i**. However, the original name is used in the JSON file, causing the error.
746e41f4b71Sopenharmony_ci
747e41f4b71Sopenharmony_ci**Solution**: Use the **-keep-property-name** option to add the fields used in JSON files to the trustlist.
748e41f4b71Sopenharmony_ci
749e41f4b71Sopenharmony_ci**Case 2**: An error message is reported when database-related fields are used and property obfuscation is enabled.
750e41f4b71Sopenharmony_ci
751e41f4b71Sopenharmony_ciThe error message is "table Account has no column named a23 in 'INSET INTO Account(a23)'."
752e41f4b71Sopenharmony_ci
753e41f4b71Sopenharmony_ciDatabase fields are used in the code. During obfuscation, the field names in the SQL statements are obfuscated, but the original field names are used in the database, causing the error.
754e41f4b71Sopenharmony_ci
755e41f4b71Sopenharmony_ci**Solution**: Use the **-keep-property-name** option to add the database fields to the trustlist.
756e41f4b71Sopenharmony_ci
757e41f4b71Sopenharmony_ci#### Errors That May Occur When -enable-export-obfuscation and -enable-toplevel-obfuscation Are Configured
758e41f4b71Sopenharmony_ci
759e41f4b71Sopenharmony_ciWhen the two options are configured, method name confusion in the following scenarios is involved when the main module calls the methods of other modules:
760e41f4b71Sopenharmony_ci
761e41f4b71Sopenharmony_ci| Main Module| Dependent Module| Confusion of Imported and Exported Names|
762e41f4b71Sopenharmony_ci| ------- | ------- | ----------------------------|
763e41f4b71Sopenharmony_ci| HAP/HSP | HSP     | The HSP and main module are built independently, and different names are generated after obfuscation. Therefore, a trustlist must be configured for both the HSP and main module.|
764e41f4b71Sopenharmony_ci| HAP/HSP | Local HAR| The local HAR is built together with the main module. After obfuscation, the names are the same.|
765e41f4b71Sopenharmony_ci| HAP/HSP | Third-party library | The names and properties exported from a third-party library are collected to the trustlist. They are not confused during import and export.|
766e41f4b71Sopenharmony_ci
767e41f4b71Sopenharmony_ciFor the HSP, you must add the methods used by other modules to the trustlist. You must add the same trustlist for the main module. Therefore, you are advised to add the obfuscation file configured with the trustlist (for example, **hsp-white-list.txt**) to the obfuscation configuration item of the module that depends on the obfuscation file, that is, the **files** field shown in the following figure.
768e41f4b71Sopenharmony_ci
769e41f4b71Sopenharmony_ci![obfuscation-config](figures/obfuscation-config.png)
770e41f4b71Sopenharmony_ci
771e41f4b71Sopenharmony_ci**Case 1**: When a class is dynamically imported, the class definition is confused, but the class name is not, causing an error.
772e41f4b71Sopenharmony_ci
773e41f4b71Sopenharmony_ci```
774e41f4b71Sopenharmony_ci// Before obfuscation
775e41f4b71Sopenharmony_ciexport class Test1 {}
776e41f4b71Sopenharmony_ci
777e41f4b71Sopenharmony_cilet mytest = (await import('./file')).Test1
778e41f4b71Sopenharmony_ci
779e41f4b71Sopenharmony_ci// After obfuscation
780e41f4b71Sopenharmony_ciexport class w1 {}
781e41f4b71Sopenharmony_ci
782e41f4b71Sopenharmony_cilet mytest = (await import('./file')).Test1
783e41f4b71Sopenharmony_ci```
784e41f4b71Sopenharmony_ci
785e41f4b71Sopenharmony_ciThe exported class **Test1** is a top-level domain name. When **Test1** is dynamically used, it is a property. Because the **-enable-property-obfuscation** option is not configured, the class name is confused, but the property name is not.
786e41f4b71Sopenharmony_ci
787e41f4b71Sopenharmony_ci**Solution**: Use the **-keep-global-name** option to add **Test1** to the trustlist.
788e41f4b71Sopenharmony_ci
789e41f4b71Sopenharmony_ci**Case 2**: For a method in a namespace, the method definition is confused, but the statement that uses the method is not, causing an error.
790e41f4b71Sopenharmony_ci
791e41f4b71Sopenharmony_ci```
792e41f4b71Sopenharmony_ci// Before obfuscation
793e41f4b71Sopenharmony_ciexport namespace ns1 {
794e41f4b71Sopenharmony_ci  export class person1 {}
795e41f4b71Sopenharmony_ci}
796e41f4b71Sopenharmony_ci
797e41f4b71Sopenharmony_ciimport {ns1} from './file1'
798e41f4b71Sopenharmony_cilet person1 = new ns1.person1()
799e41f4b71Sopenharmony_ci
800e41f4b71Sopenharmony_ci// After obfuscation
801e41f4b71Sopenharmony_ciexport namespace a3 {
802e41f4b71Sopenharmony_ci  export class b2 {}
803e41f4b71Sopenharmony_ci}
804e41f4b71Sopenharmony_ci
805e41f4b71Sopenharmony_ciimport {a3} from './file1'
806e41f4b71Sopenharmony_cilet person1 = new a3.person1()
807e41f4b71Sopenharmony_ci```
808e41f4b71Sopenharmony_ci
809e41f4b71Sopenharmony_ci**person1** in the namespace is a top-level class name. When it is called by using **ns1.person1**, **person1** is a property and is not obfuscated because property obfuscation is not enabled.
810e41f4b71Sopenharmony_ci
811e41f4b71Sopenharmony_ci**Solution:**
812e41f4b71Sopenharmony_ci
813e41f4b71Sopenharmony_ci1. Configure the **-enable-property-obfuscation** option.
814e41f4b71Sopenharmony_ci2. Use the **-keep-global-name** option to add the methods exported from the namespace to the trustlist.
815e41f4b71Sopenharmony_ci
816e41f4b71Sopenharmony_ci**Case 3**: When declare global is used, a syntax error is reported after obfuscation.
817e41f4b71Sopenharmony_ci
818e41f4b71Sopenharmony_ci```
819e41f4b71Sopenharmony_ci// Before obfuscation
820e41f4b71Sopenharmony_cideclare global {
821e41f4b71Sopenharmony_ci  var age : string
822e41f4b71Sopenharmony_ci}
823e41f4b71Sopenharmony_ci
824e41f4b71Sopenharmony_ci// After obfuscation
825e41f4b71Sopenharmony_cideclare a2 {
826e41f4b71Sopenharmony_ci  var b2 : string
827e41f4b71Sopenharmony_ci}
828e41f4b71Sopenharmony_ci```
829e41f4b71Sopenharmony_ci
830e41f4b71Sopenharmony_ciThe error message "SyntaxError: Unexpected token" is reported.
831e41f4b71Sopenharmony_ci
832e41f4b71Sopenharmony_ci**Solution**: Use **-keep-global-name** to **add __global** to the trustlist.
833e41f4b71Sopenharmony_ci
834e41f4b71Sopenharmony_ci#### The **-enable-string-property-obfuscation** option is not configured, but the string literal property name is obfuscated. As a result, the value of the string literal property name is undefined.
835e41f4b71Sopenharmony_ci
836e41f4b71Sopenharmony_ci```
837e41f4b71Sopenharmony_ciperson["age"] = 22; // Before obfuscation
838e41f4b71Sopenharmony_ci
839e41f4b71Sopenharmony_ciperson["b"] = 22; // After obfuscation
840e41f4b71Sopenharmony_ci```
841e41f4b71Sopenharmony_ci
842e41f4b71Sopenharmony_ci**Solution:**
843e41f4b71Sopenharmony_ci
844e41f4b71Sopenharmony_ci1. Check whether **-enable-string-property-obfuscation** is configured for the dependent HAR. If it is configured, the main project will be affected, and you should disable it.
845e41f4b71Sopenharmony_ci2. If it must be configured, add the property name to the trustlist.
846e41f4b71Sopenharmony_ci3. If it is not configured and the SDK version is earlier than 4.1.5.3, update the SDK.
847e41f4b71Sopenharmony_ci
848e41f4b71Sopenharmony_ci#### Errors That May Occur When -enable-filename-obfuscation Is Configured
849e41f4b71Sopenharmony_ci
850e41f4b71Sopenharmony_ci**Case 1**: The error message "Error Failed to get a resolved OhmUrl for 'D:code/MyApplication/f12/library1/pages/d.ets' imported by 'undefined'" is reported.
851e41f4b71Sopenharmony_ci
852e41f4b71Sopenharmony_ciAs shown below, the outer layer of the **library1** module contains a directory named **directory**. When file name obfuscation is enabled, **directory** is obfuscated as **f12**, causing the error indicating that the path is not found.
853e41f4b71Sopenharmony_ci
854e41f4b71Sopenharmony_ci![directory-offuscation](figures/directory-obfuscation.png)
855e41f4b71Sopenharmony_ci
856e41f4b71Sopenharmony_ci**Solution:**
857e41f4b71Sopenharmony_ci
858e41f4b71Sopenharmony_ci1. If the project directory structure and error message are similar, update the SDK to 5.0.0.26 or later.
859e41f4b71Sopenharmony_ci2. Use the **-keep-file-name** option to add the directory name **directory** of the module to the trustlist.
860e41f4b71Sopenharmony_ci
861e41f4b71Sopenharmony_ci**Case 2**: The error message "Cannot find module 'ets/appability/AppAbility' which is application Entry Point" is reported.
862e41f4b71Sopenharmony_ci
863e41f4b71Sopenharmony_ciThe system loads the ability file when the application is running. Therefore, you must manually configure the trustlist to prevent the specified file from being obfuscated.
864e41f4b71Sopenharmony_ci
865e41f4b71Sopenharmony_ci**Solution**: Use the **-keep-file-name** option to add the path corresponding to the **srcEntry** field in the **src/main/module.json5** file to the trustlist.
866e41f4b71Sopenharmony_ci
867e41f4b71Sopenharmony_ci```
868e41f4b71Sopenharmony_ci-keep-file-name
869e41f4b71Sopenharmony_ciappability
870e41f4b71Sopenharmony_ciAppAbility
871e41f4b71Sopenharmony_ci```
872e41f4b71Sopenharmony_ci
873e41f4b71Sopenharmony_ci#### Errors That May Occur When -keep-global-name and a Trustlist Are Configured
874e41f4b71Sopenharmony_ci
875e41f4b71Sopenharmony_ciThe error message "Cannot read properties of undefined (reading 'has')" is reported.
876e41f4b71Sopenharmony_ci
877e41f4b71Sopenharmony_ci**Solution**: Upgrade the SDK to 4.1.6.3 or later.
878