13af6ab5fSopenharmony_ci# Document update instructions 23af6ab5fSopenharmony_ci 33af6ab5fSopenharmony_ci## This document is no longer maintained. Obfuscation documents will be moved to [official guide](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5) and [doc repository](https://gitee.com/openharmony/docs/blob/master/en/application-dev/arkts-utils/source-obfuscation.md) 43af6ab5fSopenharmony_ci 53af6ab5fSopenharmony_ciNote: The source obfuscation is updated synchronously with the DevEco Studio version. It is recommended to read the official guide that comes with the DevEco Studio version first. The community doc repository correspond to the latest open source code, ahead of the source obfuscation function in the released DevEco Studio. 63af6ab5fSopenharmony_ci 73af6ab5fSopenharmony_ci# Arkguard 83af6ab5fSopenharmony_ci 93af6ab5fSopenharmony_ciArkguard is a javascript and typescript obfuscation tool. 103af6ab5fSopenharmony_ciFor Chinese version please read [README-cn.md](README-cn.md) 113af6ab5fSopenharmony_ci(中文版说明请查看[README-cn.md](README-cn.md)). 123af6ab5fSopenharmony_ci 133af6ab5fSopenharmony_ci# Usage in DevEco Studio 143af6ab5fSopenharmony_ci 153af6ab5fSopenharmony_ciArkguard has been integrated into SDK. It is convenient to use Arkguard in DevEco Studio. 163af6ab5fSopenharmony_ciIn DevEco Studio, Arkguard can be enabled only in Stage Model (FA Model is not supported). 173af6ab5fSopenharmony_ciFor now only name obfuscations can be used in DevEco Studio (because other obfuscation 183af6ab5fSopenharmony_ciabilities of Arkguard may hurt execution performance). 193af6ab5fSopenharmony_ciYou can obfuscate the following names: 203af6ab5fSopenharmony_ci 213af6ab5fSopenharmony_ci* parameter names and local variable names 223af6ab5fSopenharmony_ci* names in global scope 233af6ab5fSopenharmony_ci* property names 243af6ab5fSopenharmony_ci 253af6ab5fSopenharmony_ciWe enable the obfuscation of parameter names and local variable names by default. However, 263af6ab5fSopenharmony_ciglobal names obfuscation and property names obfuscation are disabled by default, as they may 273af6ab5fSopenharmony_cicause runtime error if they are enabled by default. 283af6ab5fSopenharmony_ciYou can enable them by [obfuscation options](#obfuscation-options). 293af6ab5fSopenharmony_ci 303af6ab5fSopenharmony_ciWhen you create a new project, the following config will be generated in `build-profile.json5`. 313af6ab5fSopenharmony_ci 323af6ab5fSopenharmony_ci``` 333af6ab5fSopenharmony_ci"arkOptions": { 343af6ab5fSopenharmony_ci "obfuscation": { 353af6ab5fSopenharmony_ci "ruleOptions": { 363af6ab5fSopenharmony_ci "enable": true, 373af6ab5fSopenharmony_ci "files": ["obfuscation-rules.txt"], 383af6ab5fSopenharmony_ci } 393af6ab5fSopenharmony_ci } 403af6ab5fSopenharmony_ci} 413af6ab5fSopenharmony_ci``` 423af6ab5fSopenharmony_ci 433af6ab5fSopenharmony_ciWhen you create a new library, additional property `consumerFiles` will be added. 443af6ab5fSopenharmony_ci 453af6ab5fSopenharmony_ci``` 463af6ab5fSopenharmony_ci"arkOptions": { 473af6ab5fSopenharmony_ci "obfuscation": { 483af6ab5fSopenharmony_ci "ruleOptions": { 493af6ab5fSopenharmony_ci "enable": true, 503af6ab5fSopenharmony_ci "files": ["obfuscation-rules.txt"], 513af6ab5fSopenharmony_ci } 523af6ab5fSopenharmony_ci "consumerFiles": ["consumer-rules.txt"] 533af6ab5fSopenharmony_ci } 543af6ab5fSopenharmony_ci} 553af6ab5fSopenharmony_ci``` 563af6ab5fSopenharmony_ci 573af6ab5fSopenharmony_ciTo enable obfuscation, the following conditions should be satisfied: 583af6ab5fSopenharmony_ci 593af6ab5fSopenharmony_ci* the property `ruleOptions.enable` is `true` and the property `ruleOptions.enable` of every dependent library is `true`. 603af6ab5fSopenharmony_ci* build in release mode 613af6ab5fSopenharmony_ci 623af6ab5fSopenharmony_ciThe files in the property `ruleOptions.files` will be applied when you build HAP or HAR. 633af6ab5fSopenharmony_ci 643af6ab5fSopenharmony_ciThe files in the property `consumerFiles` will be applied when you build the project or library which 653af6ab5fSopenharmony_cidepends on this library. They will also be merged into a file `obfuscation.txt` in the resulting HAR. 663af6ab5fSopenharmony_ci 673af6ab5fSopenharmony_ciWhen you are building HAP or HAR, the final obfucation rules are combination of self's `ruleOptions.files` 683af6ab5fSopenharmony_ciproperty, dependent libraries' `consumerFiles` properties and dependent HAR's `obfuscation.txt`. 693af6ab5fSopenharmony_ciIf you are building HAR, the content of `obfuscation.txt` is the combination of self's `consumerFiles` property, 703af6ab5fSopenharmony_cidependent libraries' `consumerFiles` properties and dependent HAR's `obfuscation.txt`. If you are building 713af6ab5fSopenharmony_ciHAP, `obfuscation.txt` will not be generated. For more details, please jump to 723af6ab5fSopenharmony_ci"[How Arkguard merges rules](#how-arkguard-merges-rules)". 733af6ab5fSopenharmony_ci 743af6ab5fSopenharmony_ci## Write rules 753af6ab5fSopenharmony_ci 763af6ab5fSopenharmony_ciThe files `obfuscation-rules.txt` and `consumer-rules.txt` are created by DevEco Studio automatically, but they do not 773af6ab5fSopenharmony_cicontain any rule by default. You can write rules in these files or include rules from other files, as the following 783af6ab5fSopenharmony_ciexample shows. 793af6ab5fSopenharmony_ci 803af6ab5fSopenharmony_ci``` 813af6ab5fSopenharmony_ci"buildOption": { 823af6ab5fSopenharmony_ci "arkOptions": { 833af6ab5fSopenharmony_ci "obfuscation": { 843af6ab5fSopenharmony_ci "ruleOptions": { 853af6ab5fSopenharmony_ci "enable": true, 863af6ab5fSopenharmony_ci "files": ["obfuscation-rules.txt", "myrules.txt"], 873af6ab5fSopenharmony_ci } 883af6ab5fSopenharmony_ci "consumerFiles": ["consumer-rules.txt", "my-consumer-rules.txt"] 893af6ab5fSopenharmony_ci } 903af6ab5fSopenharmony_ci } 913af6ab5fSopenharmony_ci} 923af6ab5fSopenharmony_ci``` 933af6ab5fSopenharmony_ci 943af6ab5fSopenharmony_ciIn rule files, you can write [obfuscation options](#obfuscation-options) and [keep options](#keep-options). 953af6ab5fSopenharmony_ci 963af6ab5fSopenharmony_ci### Obfuscation options 973af6ab5fSopenharmony_ci 983af6ab5fSopenharmony_ci#### -disable-obfuscation 993af6ab5fSopenharmony_ci 1003af6ab5fSopenharmony_ciSpecifies to disable all obfuscations. If you use this option, the resulting HAP or HAR will not be obfuscated. By default, 1013af6ab5fSopenharmony_ciArkguard only obfuscates the parameter names and local variable names by assigning random short names to them. 1023af6ab5fSopenharmony_ci 1033af6ab5fSopenharmony_ci#### -enable-property-obfuscation 1043af6ab5fSopenharmony_ci 1053af6ab5fSopenharmony_ciSpecifies to obfuscate the property names. If you use this option, all property names will be obfuscated except the 1063af6ab5fSopenharmony_cifollowing: 1073af6ab5fSopenharmony_ci 1083af6ab5fSopenharmony_ci* the property names of classes or objects directly imported or exported by `import/export` will be kept. For example, the property name `data` in 1093af6ab5fSopenharmony_ci 1103af6ab5fSopenharmony_ci ``` 1113af6ab5fSopenharmony_ci export class MyClass { 1123af6ab5fSopenharmony_ci data: string; 1133af6ab5fSopenharmony_ci } 1143af6ab5fSopenharmony_ci ``` 1153af6ab5fSopenharmony_ci 1163af6ab5fSopenharmony_ci will not be obfuscated. 1173af6ab5fSopenharmony_ciFor 'indirectly export' cases such as `export MyClass` and `let a = MyClass; export {a};`, if you do not want to obfuscate 1183af6ab5fSopenharmony_citheir property names, you need to use [keep options](#keep-options) to keep them. Besides, for the property names of properties of directly exported classes or objects, like `name` and `age` in the following example, if you do not want to obfuscate them, then you also need [keep options](#keep-options) to keep them. 1193af6ab5fSopenharmony_ci 1203af6ab5fSopenharmony_ci ``` 1213af6ab5fSopenharmony_ci export class MyClass { 1223af6ab5fSopenharmony_ci person = {name: "123", age: 100}; 1233af6ab5fSopenharmony_ci } 1243af6ab5fSopenharmony_ci ``` 1253af6ab5fSopenharmony_ci 1263af6ab5fSopenharmony_ci If you want to obfuscate import/export names, please refer to the [`-enable-export-obfuscation`](#-enable-export-obfuscation) option. 1273af6ab5fSopenharmony_ci* the property names defined in UI components. For example, the property names `message` and `data` in 1283af6ab5fSopenharmony_ci 1293af6ab5fSopenharmony_ci ``` 1303af6ab5fSopenharmony_ci @Component struct MyExample { 1313af6ab5fSopenharmony_ci @State message: string = "hello"; 1323af6ab5fSopenharmony_ci data: number[] = []; 1333af6ab5fSopenharmony_ci ... 1343af6ab5fSopenharmony_ci } 1353af6ab5fSopenharmony_ci ``` 1363af6ab5fSopenharmony_ci 1373af6ab5fSopenharmony_ci will not be obfuscated. 1383af6ab5fSopenharmony_ci* the property names that are specified by [keep options](#keep-options). 1393af6ab5fSopenharmony_ci* the property names in system API list. System API list is a name set which is extracted from SDK automatically by default. The cache file is systemApiCache.json, and the path is build/cache/{...}/release/obfuscation in the module directory. 1403af6ab5fSopenharmony_ci* in the Native API scenario, the APIs in the d.ts file of so library will not be obfuscated. 1413af6ab5fSopenharmony_ci* the property names that are string literals. For example, the property names "name" and "age" in the following code will not be obfuscated. 1423af6ab5fSopenharmony_ci 1433af6ab5fSopenharmony_ci ``` 1443af6ab5fSopenharmony_ci let person = {"name": "abc"}; 1453af6ab5fSopenharmony_ci person["age"] = 22; 1463af6ab5fSopenharmony_ci ``` 1473af6ab5fSopenharmony_ci 1483af6ab5fSopenharmony_ci If you want to obfuscate these string literal property names, you should addtionally use the option `-enable-toplevel-obfuscation`. For example, 1493af6ab5fSopenharmony_ci 1503af6ab5fSopenharmony_ci ``` 1513af6ab5fSopenharmony_ci -enable-property-obfuscation 1523af6ab5fSopenharmony_ci -enable-string-property-obfuscation 1533af6ab5fSopenharmony_ci ``` 1543af6ab5fSopenharmony_ci 1553af6ab5fSopenharmony_ci **Note**: 1563af6ab5fSopenharmony_ci **1.** If there are string literal property names which contain special characters (that is, all characters except 1573af6ab5fSopenharmony_ci `a-z, A-Z, 0-9, _`, for example `let obj = {"\n": 123, "": 4, " ": 5}` then we would not suggest to enable the 1583af6ab5fSopenharmony_ci option `-enable-string-property-obfuscation`, because [keep options](#keep-options) may not allow to keep these 1593af6ab5fSopenharmony_ci names when you do not want to obfuscate them. 1603af6ab5fSopenharmony_ci **2.** The property white list of the system API does not include the string constant in the declaration file. For example, the string `'ohos.want.action.home'` in the example is not included in the white list. 1613af6ab5fSopenharmony_ci 1623af6ab5fSopenharmony_ci ``` 1633af6ab5fSopenharmony_ci // System Api @ohos.app.ability.wantConstant snippet: 1643af6ab5fSopenharmony_ci export enum Params { 1653af6ab5fSopenharmony_ci DLP_PARAM_SANDBOX = 'ohos.dlp.param.sandbox' 1663af6ab5fSopenharmony_ci } 1673af6ab5fSopenharmony_ci // Developer source example: 1683af6ab5fSopenharmony_ci let params = obj['ohos.want.action.home']; 1693af6ab5fSopenharmony_ci ``` 1703af6ab5fSopenharmony_ci 1713af6ab5fSopenharmony_ci Therefore, when `-enable-string-property-obfuscation` is enabled, if you don't want to obfuscate the property like `'ohos.dlp.param.sandbox'`, which is a string constant in system api. you should keep it manually. 1723af6ab5fSopenharmony_ci 1733af6ab5fSopenharmony_ciSpecifies to obfuscate the names in the global scope. If you use this option, all global names will be obfuscated 1743af6ab5fSopenharmony_ciexcept the following: 1753af6ab5fSopenharmony_ci 1763af6ab5fSopenharmony_ci* the `import/export` global names. 1773af6ab5fSopenharmony_ci* the global names that are not declared in the current file. 1783af6ab5fSopenharmony_ci* the global names that are specified by [keep options](#keep-options). 1793af6ab5fSopenharmony_ci* the global names in system API list. 1803af6ab5fSopenharmony_ci 1813af6ab5fSopenharmony_ci#### -enable-filename-obfuscation 1823af6ab5fSopenharmony_ci 1833af6ab5fSopenharmony_ciSpecifies to obfuscate the file/folder names. If you use this option, all file/folder names will be obfuscated except the following: 1843af6ab5fSopenharmony_ci 1853af6ab5fSopenharmony_ci* the file/folder names configured in the 'main' and 'types' fields in the oh-package.json5. 1863af6ab5fSopenharmony_ci* the file/folder names configured in the 'srcEntry' field in the module.json5. 1873af6ab5fSopenharmony_ci* the file/folder names that are specified by [`-keep-file-name`](#keep-options). 1883af6ab5fSopenharmony_ci* non-ECMAScript module reference (ECMAScript module example: `import {foo} from './filename'`) 1893af6ab5fSopenharmony_ci* non-path reference, such as json5 will not be obfuscated `import module from 'json5'` 1903af6ab5fSopenharmony_ci**Note**: 1913af6ab5fSopenharmony_ci**1.** Due to the system loading certain specified files during application runtime, developers need to configure the corresponding white list manually in the [` keep file name `] option to prevent specified files from being confused and causing runtime failures. 1923af6ab5fSopenharmony_ciThe above situations that require configure white list manually include but are not limited to the following scenarios: 1933af6ab5fSopenharmony_ci(1) When the module contains Ability component, you need to configure all paths corresponding to 'srcEntry' in the 'abilities' field in `scr/main/module.json5` to the white list. 1943af6ab5fSopenharmony_ci(2) When the module contains multithreading services: Worker, you need to configure all the paths in the field 'buildOption'-'sourceOption'-'workers' in `build-profiles.json5` to the white list. 1953af6ab5fSopenharmony_ci 1963af6ab5fSopenharmony_ci#### -enable-export-obfuscation 1973af6ab5fSopenharmony_ci 1983af6ab5fSopenharmony_ciEnable name and property name obfuscation for directly imported or exported classes or objects. If you use this option, the names of direct imports or exports in the module will be obfuscated, except in the following scenarios: 1993af6ab5fSopenharmony_ci 2003af6ab5fSopenharmony_ci* The names and property names of classes or objects exported in remote HAR (packages with real paths in oh_modules) will not be obfuscated. 2013af6ab5fSopenharmony_ci* Names and property names specified by [keep options](#keep-options) will not be obfuscated. 2023af6ab5fSopenharmony_ci* Names in the system API list will not be obfuscated. 2033af6ab5fSopenharmony_ci 2043af6ab5fSopenharmony_ci**Note**: 2053af6ab5fSopenharmony_ci 2063af6ab5fSopenharmony_ci1. To obfuscate the property names in imported or exported classes, you need to enable both the `-enable-property-obfuscation` and `-enable-export-obfuscation` options. 2073af6ab5fSopenharmony_ci2. When compiling HSP, if the `-enable-export-obfuscation` option is used, the externally exposed interfaces need to be kept in the obfuscation configuration file `obfuscation-rules.txt` in the module. 2083af6ab5fSopenharmony_ci3. In the scenario where HAP/HSP/HAR depends on HSP, if the `-enable-export-obfuscation` option is used during compilation, the interface imported from HSP needs to be kept in the obfuscation configuration file `obfuscation-rules.txt` in the module. 2093af6ab5fSopenharmony_ci 2103af6ab5fSopenharmony_ci ``` 2113af6ab5fSopenharmony_ci // Code example (entry file Index.ets in HSP): 2123af6ab5fSopenharmony_ci export { add, customApiName } from './src/main/ets/utils/Calc' 2133af6ab5fSopenharmony_ci 2143af6ab5fSopenharmony_ci // Example of keeping interface name: 2153af6ab5fSopenharmony_ci // obfuscation-rules.txt file configuration in HSP and modules that depend on this HSP: 2163af6ab5fSopenharmony_ci keep-global-name 2173af6ab5fSopenharmony_ci add 2183af6ab5fSopenharmony_ci customApiName 2193af6ab5fSopenharmony_ci ``` 2203af6ab5fSopenharmony_ci 2213af6ab5fSopenharmony_ci#### -compact 2223af6ab5fSopenharmony_ci 2233af6ab5fSopenharmony_ciSpecifies to remove unnecessary blank spaces and all line feeds. If you use this option, all code will be compressed into 2243af6ab5fSopenharmony_cione line. 2253af6ab5fSopenharmony_ci**Note**: The stack information in release mode only includes the line number of code, not the column number. Therefore, when the compact is enabled, the specific location of the source code cannot be located based on the line number of stack information. 2263af6ab5fSopenharmony_ci 2273af6ab5fSopenharmony_ci#### -remove-log 2283af6ab5fSopenharmony_ci 2293af6ab5fSopenharmony_ciDelete the expressions involving direct calls to console.* statements in the following scenarios: 2303af6ab5fSopenharmony_ci 2313af6ab5fSopenharmony_ci1. Calls at the toplevel level of a file. 2323af6ab5fSopenharmony_ci2. Calls within a block. 2333af6ab5fSopenharmony_ci3. Calls within a module. 2343af6ab5fSopenharmony_ci4. Calls within a switch statement. 2353af6ab5fSopenharmony_ciand the return value of console.* should not be called 2363af6ab5fSopenharmony_ci 2373af6ab5fSopenharmony_ci#### `-print-namecache` filepath 2383af6ab5fSopenharmony_ci 2393af6ab5fSopenharmony_ciSpecifies to print the name cache that contains the mapping from the old names to new names. 2403af6ab5fSopenharmony_ciNote: The namecache.json file will be generated every time the module is fully built, so you should save a copy each time you publish a new version. 2413af6ab5fSopenharmony_ci 2423af6ab5fSopenharmony_ci#### `-apply-namecache` filepath 2433af6ab5fSopenharmony_ci 2443af6ab5fSopenharmony_ciSpecifies to reuse the given cache file. The old names in the cache will receive the corresponding new names specified in 2453af6ab5fSopenharmony_cithe cache. Other names will receive new random short names. This option should be used in incremental obfuscation. 2463af6ab5fSopenharmony_ci 2473af6ab5fSopenharmony_ciBy default, DevEco Studio will keep and update the namecache file in the temporary cache directory and apply the cache for 2483af6ab5fSopenharmony_ciincremental compilation. 2493af6ab5fSopenharmony_ciCache directory: build/cache/{...}/release/obfuscation 2503af6ab5fSopenharmony_ci 2513af6ab5fSopenharmony_ci#### -remove-comments 2523af6ab5fSopenharmony_ci 2533af6ab5fSopenharmony_ciRemove all comments including single line, multi line and JsDoc comments, in a project except: 2543af6ab5fSopenharmony_ci 2553af6ab5fSopenharmony_ci* Those names of JsDoc comments above class, function, struct, enum ... in declaration files are in `-keep-comments`. 2563af6ab5fSopenharmony_ci**Note**: `-keep-comments` doesn't work for comments in generated source files, which will be deleted. 2573af6ab5fSopenharmony_ci 2583af6ab5fSopenharmony_ci### Keep options 2593af6ab5fSopenharmony_ci 2603af6ab5fSopenharmony_ci#### `-keep-property-name` [,identifiers,...] 2613af6ab5fSopenharmony_ci 2623af6ab5fSopenharmony_ciSpecifies property names that you want to keep. For example, 2633af6ab5fSopenharmony_ci 2643af6ab5fSopenharmony_ci``` 2653af6ab5fSopenharmony_ci-keep-property-name 2663af6ab5fSopenharmony_ciage 2673af6ab5fSopenharmony_cifirstName 2683af6ab5fSopenharmony_cilastName 2693af6ab5fSopenharmony_ci``` 2703af6ab5fSopenharmony_ci 2713af6ab5fSopenharmony_ci**Note**: This option is avaliable when `-enable-property-obfuscation` is enabled. 2723af6ab5fSopenharmony_ci 2733af6ab5fSopenharmony_ci`-keep-comments` 2743af6ab5fSopenharmony_ciTo retain JsDoc comments above elements in declaration files, such as preserving the JsDoc comment above the Human class, 2753af6ab5fSopenharmony_ciyou can make the following configuration: 2763af6ab5fSopenharmony_ci 2773af6ab5fSopenharmony_ci``` 2783af6ab5fSopenharmony_ci-keep-comments 2793af6ab5fSopenharmony_ciHuman 2803af6ab5fSopenharmony_ci``` 2813af6ab5fSopenharmony_ci 2823af6ab5fSopenharmony_ci**Note**: 2833af6ab5fSopenharmony_ci 2843af6ab5fSopenharmony_ci1. This option is avaliable when `-remove-comments` is enabled. 2853af6ab5fSopenharmony_ci2. If the name of an element is obfuscated, the JsDoc comments 2863af6ab5fSopenharmony_ciabove that element cannot be kept using `-keep-comments`. For example, when you have exportClass in `-keep-comments`, 2873af6ab5fSopenharmony_ciyou should make sure that the following class will not be obfuscated, or the JsDoc comments above the class will still be removed: 2883af6ab5fSopenharmony_ci 2893af6ab5fSopenharmony_ci``` 2903af6ab5fSopenharmony_ci/** 2913af6ab5fSopenharmony_ci** @class exportClass 2923af6ab5fSopenharmony_ci*/ 2933af6ab5fSopenharmony_ciexport class exportClass {} 2943af6ab5fSopenharmony_ci``` 2953af6ab5fSopenharmony_ci 2963af6ab5fSopenharmony_ci**What property names should be kept?** 2973af6ab5fSopenharmony_ci 2983af6ab5fSopenharmony_ciFor safety, we would suggest keeping all property names that are not accessed through dot syntax. 2993af6ab5fSopenharmony_ci 3003af6ab5fSopenharmony_ciExample: 3013af6ab5fSopenharmony_ci 3023af6ab5fSopenharmony_ci``` 3033af6ab5fSopenharmony_civar obj = {x0: 0, x1: 0, x2: 0}; 3043af6ab5fSopenharmony_cifor (var i = 0; i <= 2; i++) { 3053af6ab5fSopenharmony_ci console.log(obj['x' + i]); // x0, x1, x2 should be kept 3063af6ab5fSopenharmony_ci} 3073af6ab5fSopenharmony_ci 3083af6ab5fSopenharmony_ciObject.defineProperty(obj, 'y', {}); // y should be kept 3093af6ab5fSopenharmony_ciconsole.log(obj.y); 3103af6ab5fSopenharmony_ci 3113af6ab5fSopenharmony_ciobj.s = 0; 3123af6ab5fSopenharmony_cilet key = 's'; 3133af6ab5fSopenharmony_ciconsole.log(obj[key]); // s should be kept 3143af6ab5fSopenharmony_ci 3153af6ab5fSopenharmony_ciobj.u = 0; 3163af6ab5fSopenharmony_ciconsole.log(obj.u); // u can be safely obfuscated 3173af6ab5fSopenharmony_ci 3183af6ab5fSopenharmony_ciobj.t = 0; 3193af6ab5fSopenharmony_ciconsole.log(obj['t']); // t and 't' can be safely obfuscated when `-enable-string-property-obfuscation`, but we suggest keeping t 3203af6ab5fSopenharmony_ci 3213af6ab5fSopenharmony_ciobj['v'] = 0; 3223af6ab5fSopenharmony_ciconsole.log(obj['v']); // 'v' can be safely obfuscated when `-enable-string-property-obfuscation`, but we suggest keeping v 3233af6ab5fSopenharmony_ci``` 3243af6ab5fSopenharmony_ci 3253af6ab5fSopenharmony_ciIn the native API scenario, if in the ets/ts/js file you want to use APIs that are not declared in d.ts file, you need to keep these APIs. 3263af6ab5fSopenharmony_ci 3273af6ab5fSopenharmony_ci#### `-keep-global-name` [,identifiers,...] 3283af6ab5fSopenharmony_ci 3293af6ab5fSopenharmony_ciSpecifies names that you want to keep in the global scope. For example, 3303af6ab5fSopenharmony_ci 3313af6ab5fSopenharmony_ci``` 3323af6ab5fSopenharmony_ci-keep-global-name 3333af6ab5fSopenharmony_ciPerson 3343af6ab5fSopenharmony_ciprintPersonName 3353af6ab5fSopenharmony_ci``` 3363af6ab5fSopenharmony_ci 3373af6ab5fSopenharmony_ci**What global names should be kept?** 3383af6ab5fSopenharmony_ci 3393af6ab5fSopenharmony_ciIt is known that in javascript the variables in the global scope are properties of `globalThis`. So if in your code 3403af6ab5fSopenharmony_ciyou access a global variable as a property, then the global name should be kept. 3413af6ab5fSopenharmony_ci 3423af6ab5fSopenharmony_ciExample: 3433af6ab5fSopenharmony_ci 3443af6ab5fSopenharmony_ci``` 3453af6ab5fSopenharmony_civar a = 0; 3463af6ab5fSopenharmony_ciconsole.log(globalThis.a); // a should be kept 3473af6ab5fSopenharmony_ci 3483af6ab5fSopenharmony_cifunction foo(){} 3493af6ab5fSopenharmony_ciglobalThis.foo(); // foo should be kept 3503af6ab5fSopenharmony_ci 3513af6ab5fSopenharmony_civar c = 0; 3523af6ab5fSopenharmony_ciconsole.log(c); // c can be safely obfuscated 3533af6ab5fSopenharmony_ci 3543af6ab5fSopenharmony_cifunction bar(){} 3553af6ab5fSopenharmony_cibar(); // bar can be safely obfuscated 3563af6ab5fSopenharmony_ci 3573af6ab5fSopenharmony_ciclass MyClass {} 3583af6ab5fSopenharmony_cilet d = new MyClass(); // MyClass can be safely obfuscated 3593af6ab5fSopenharmony_ci``` 3603af6ab5fSopenharmony_ci 3613af6ab5fSopenharmony_ci#### `-keep-file-name` [,identifiers,...] 3623af6ab5fSopenharmony_ci 3633af6ab5fSopenharmony_ciSpecify the name of files/folders to keep (no need to write the file suffix). for example, 3643af6ab5fSopenharmony_ci 3653af6ab5fSopenharmony_ci``` 3663af6ab5fSopenharmony_ci-keep-file-name 3673af6ab5fSopenharmony_ciindex 3683af6ab5fSopenharmony_cientry 3693af6ab5fSopenharmony_ci``` 3703af6ab5fSopenharmony_ci 3713af6ab5fSopenharmony_ci**What file names should be kept?** 3723af6ab5fSopenharmony_ci 3733af6ab5fSopenharmony_ci``` 3743af6ab5fSopenharmony_ciconst module1 = require('./file1') // ARKTs doesn't support CommonJS, this path reference should be kept. 3753af6ab5fSopenharmony_ciconst moduleName = './file2' 3763af6ab5fSopenharmony_ciconst module2 = import(moduleName) // dynamic reference cannot identify whether moduleName is a path and should be retained. 3773af6ab5fSopenharmony_ci``` 3783af6ab5fSopenharmony_ci 3793af6ab5fSopenharmony_ci#### `-keep-dts` filepath 3803af6ab5fSopenharmony_ci 3813af6ab5fSopenharmony_ciSpecifies to keep names in the given `.d.ts` file. Here filepath can be also a directory. If so, then the names in all 3823af6ab5fSopenharmony_ci`d.ts` files under the given directory will be kept. 3833af6ab5fSopenharmony_ciIf your are building HAR with this option, then the kept names will be merged into the resulting `obfuscation.txt`. 3843af6ab5fSopenharmony_ci 3853af6ab5fSopenharmony_ci#### `-keep` path 3863af6ab5fSopenharmony_ci 3873af6ab5fSopenharmony_ciNames(such as variable names, class names, property names, etc.) in the specified path are not obfuscated. This path can be a file or a folder. If it is a folder, the files in the folder and the files in subfolders will not be obfuscated. 3883af6ab5fSopenharmony_ciThe path only supports relative paths, `./` and `../` are relative to the directory where the obfuscation configuration file is located. 3893af6ab5fSopenharmony_ci 3903af6ab5fSopenharmony_ci``` 3913af6ab5fSopenharmony_ci-keep 3923af6ab5fSopenharmony_ci./src/main/ets/fileName.ts // The names in fileName.ts are not obfusated. 3933af6ab5fSopenharmony_ci../folder // The names of files and subfolders in the folder directory are not obfusated. 3943af6ab5fSopenharmony_ci../oh_modules/json5 // The names of all files in the referenced library json5 are not obfusated. 3953af6ab5fSopenharmony_ci``` 3963af6ab5fSopenharmony_ci 3973af6ab5fSopenharmony_ciNote: This option does not affect the function of file name obfuscation `-enable-filename-obfuscation` 3983af6ab5fSopenharmony_ci 3993af6ab5fSopenharmony_ci### Keep options support wildcards 4003af6ab5fSopenharmony_ci 4013af6ab5fSopenharmony_ci#### Wildcards for name categories 4023af6ab5fSopenharmony_ci 4033af6ab5fSopenharmony_ciThe following options support configuring wildcards for name categories:<br> 4043af6ab5fSopenharmony_ci`-keep-property-name`<br> 4053af6ab5fSopenharmony_ci`-keep-global-name`<br> 4063af6ab5fSopenharmony_ci`-keep-file-name`<br> 4073af6ab5fSopenharmony_ci`-keep-comments`<br> 4083af6ab5fSopenharmony_ci 4093af6ab5fSopenharmony_ciThe usage of name categories wildcards is as follows: 4103af6ab5fSopenharmony_ci 4113af6ab5fSopenharmony_ci| Wildcard | Meaning | Example | 4123af6ab5fSopenharmony_ci| -------- | ------------------------------------ | -------------------------------------------------- | 4133af6ab5fSopenharmony_ci| ? | Matches any single character | "AB?" can match "ABC", etc., but cannot match "AB" | 4143af6ab5fSopenharmony_ci| \* | Matches any number of any characters | "*AB*" can match "AB", "aABb", "cAB", "ABc", etc. | 4153af6ab5fSopenharmony_ci 4163af6ab5fSopenharmony_ci**Examples**: 4173af6ab5fSopenharmony_ci 4183af6ab5fSopenharmony_ciRetains all property names starting with 'a': 4193af6ab5fSopenharmony_ci 4203af6ab5fSopenharmony_ci``` 4213af6ab5fSopenharmony_ci-keep-property-name 4223af6ab5fSopenharmony_cia* 4233af6ab5fSopenharmony_ci``` 4243af6ab5fSopenharmony_ci 4253af6ab5fSopenharmony_ciRetains all single-character property names: 4263af6ab5fSopenharmony_ci 4273af6ab5fSopenharmony_ci``` 4283af6ab5fSopenharmony_ci-keep-property-name 4293af6ab5fSopenharmony_ci? 4303af6ab5fSopenharmony_ci``` 4313af6ab5fSopenharmony_ci 4323af6ab5fSopenharmony_ciRetains all property names: 4333af6ab5fSopenharmony_ci 4343af6ab5fSopenharmony_ci``` 4353af6ab5fSopenharmony_ci-keep-property-name 4363af6ab5fSopenharmony_ci* 4373af6ab5fSopenharmony_ci``` 4383af6ab5fSopenharmony_ci 4393af6ab5fSopenharmony_ci#### Wildcards for path categories 4403af6ab5fSopenharmony_ci 4413af6ab5fSopenharmony_ciThe following options support configuring wildcards for path categories: 4423af6ab5fSopenharmony_ci 4433af6ab5fSopenharmony_ci`-keep` 4443af6ab5fSopenharmony_ci 4453af6ab5fSopenharmony_ciThe usage of path categories wildcards is as follows: 4463af6ab5fSopenharmony_ci 4473af6ab5fSopenharmony_ci| Wildcard | Meaning | Example | 4483af6ab5fSopenharmony_ci| -------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- | 4493af6ab5fSopenharmony_ci| ? | Matches any single character except path separator '/' | "../a?" can match "../ab", etc., but cannot match "../a/" | 4503af6ab5fSopenharmony_ci| \* | Matches any number of any characters except path separator '/' | "../a*/c" can match "../ab/c", but cannot match "../ab/d/s/c" | 4513af6ab5fSopenharmony_ci| \*\* | Matches any number of any characters | "../a**/c" can match "../ab/c", and also "../ab/d/s/c" | 4523af6ab5fSopenharmony_ci| ! | Represents negation and can only be written at the beginning of a path to exclude certain cases that already exist in the user-configured whitelist | "!../a/b/c.ets" means except "../a/b/c.ets" | 4533af6ab5fSopenharmony_ci 4543af6ab5fSopenharmony_ci**Examples**: 4553af6ab5fSopenharmony_ci 4563af6ab5fSopenharmony_ciIndicates that the c.ets files in all folders in ../a/b/ (excluding subfolders) will not be obfuscated: 4573af6ab5fSopenharmony_ci 4583af6ab5fSopenharmony_ci``` 4593af6ab5fSopenharmony_ci-keep 4603af6ab5fSopenharmony_ci../a/b/*/c.ets 4613af6ab5fSopenharmony_ci``` 4623af6ab5fSopenharmony_ci 4633af6ab5fSopenharmony_ciIndicates that the c.ets files in all folders in ../a/b/ (including subfolders) will not be obfuscated: 4643af6ab5fSopenharmony_ci 4653af6ab5fSopenharmony_ci``` 4663af6ab5fSopenharmony_ci-keep 4673af6ab5fSopenharmony_ci../a/b/**/c.ets 4683af6ab5fSopenharmony_ci``` 4693af6ab5fSopenharmony_ci 4703af6ab5fSopenharmony_ciIndicates that except for the c.ets file, all other files in ../a/b/ will not be obfuscated: 4713af6ab5fSopenharmony_ci 4723af6ab5fSopenharmony_ci``` 4733af6ab5fSopenharmony_ci-keep 4743af6ab5fSopenharmony_ci../a/b/ 4753af6ab5fSopenharmony_ci!../a/b/c.ets 4763af6ab5fSopenharmony_ci``` 4773af6ab5fSopenharmony_ci 4783af6ab5fSopenharmony_ciMeaningless: 4793af6ab5fSopenharmony_ci 4803af6ab5fSopenharmony_ci``` 4813af6ab5fSopenharmony_ci-keep 4823af6ab5fSopenharmony_ci!../a/b/c.ets 4833af6ab5fSopenharmony_ci``` 4843af6ab5fSopenharmony_ci 4853af6ab5fSopenharmony_ciIndicates that all files will not be obfuscated: 4863af6ab5fSopenharmony_ci 4873af6ab5fSopenharmony_ci``` 4883af6ab5fSopenharmony_ci-keep 4893af6ab5fSopenharmony_ci* 4903af6ab5fSopenharmony_ci``` 4913af6ab5fSopenharmony_ci 4923af6ab5fSopenharmony_ci**Note**: 4933af6ab5fSopenharmony_ci 4943af6ab5fSopenharmony_ci(1)The above options do not support configuring wildcards '*', '?', '!' for other meanings. 4953af6ab5fSopenharmony_ci 4963af6ab5fSopenharmony_ciFor example: 4973af6ab5fSopenharmony_ci 4983af6ab5fSopenharmony_ci``` 4993af6ab5fSopenharmony_ciclass A { 5003af6ab5fSopenharmony_ci '*'= 1 5013af6ab5fSopenharmony_ci} 5023af6ab5fSopenharmony_ci-keep-property-name 5033af6ab5fSopenharmony_ci* 5043af6ab5fSopenharmony_ci``` 5053af6ab5fSopenharmony_ci 5063af6ab5fSopenharmony_ciIt becomes ineffective when you only want to retain the '\*' property. 5073af6ab5fSopenharmony_ci 5083af6ab5fSopenharmony_ciHere, \* indicates matching any number of any characters, resulting in all property names not being obfuscated, rather than only '\*' not being obfuscated. 5093af6ab5fSopenharmony_ci 5103af6ab5fSopenharmony_ci(2) The -keep option only allows the use of '/' as the path separator and does not support '\\' or '\\\\'. 5113af6ab5fSopenharmony_ci 5123af6ab5fSopenharmony_ci### Comments 5133af6ab5fSopenharmony_ci 5143af6ab5fSopenharmony_ciYou can write comments in obfuscation rule file by using `#`. The line begins with `#` is treated as comment. 5153af6ab5fSopenharmony_ciFor example, 5163af6ab5fSopenharmony_ci 5173af6ab5fSopenharmony_ci``` 5183af6ab5fSopenharmony_ci# white list for MainAbility.ets 5193af6ab5fSopenharmony_ci-keep-global-name 5203af6ab5fSopenharmony_ciMyComponent 5213af6ab5fSopenharmony_ciGlobalFunction 5223af6ab5fSopenharmony_ci 5233af6ab5fSopenharmony_ci-keep-property-name # white list for dynamic property names 5243af6ab5fSopenharmony_cifirstName 5253af6ab5fSopenharmony_cilastName 5263af6ab5fSopenharmony_ciage 5273af6ab5fSopenharmony_ci``` 5283af6ab5fSopenharmony_ci 5293af6ab5fSopenharmony_ciIf your are building HAR, comments will not be merged into the resulting `obfuscation.txt`. 5303af6ab5fSopenharmony_ci 5313af6ab5fSopenharmony_ci### How Arkguard merges rules 5323af6ab5fSopenharmony_ci 5333af6ab5fSopenharmony_ciTypically there may be serveral rule files in your project. These rule files come from: 5343af6ab5fSopenharmony_ci 5353af6ab5fSopenharmony_ci* `ruleOptions.files` in main project (Here by main project we mean the project you are building) 5363af6ab5fSopenharmony_ci* `consumerFiles` in local dependent libraries 5373af6ab5fSopenharmony_ci* `obfuscate.txt` in remote dependent HARs 5383af6ab5fSopenharmony_ciWhen building your main project, all these rules will be merged by the following strategy (in pseudo code): 5393af6ab5fSopenharmony_ci 5403af6ab5fSopenharmony_ci``` 5413af6ab5fSopenharmony_cilet `listRules` be the list of all rule files that are mentioned above. 5423af6ab5fSopenharmony_cilet finalRule = { 5433af6ab5fSopenharmony_ci disableObfuscation: false, 5443af6ab5fSopenharmony_ci enablePropertyObfuscation: false, 5453af6ab5fSopenharmony_ci enableToplevelObfuscation: false, 5463af6ab5fSopenharmony_ci compact: false, 5473af6ab5fSopenharmony_ci removeLog: false, 5483af6ab5fSopenharmony_ci keepPropertyName: [], 5493af6ab5fSopenharmony_ci keepGlobalName: [], 5503af6ab5fSopenharmony_ci keepDts: [], 5513af6ab5fSopenharmony_ci printNamecache: string, 5523af6ab5fSopenharmony_ci applyNamecache: string 5533af6ab5fSopenharmony_ci} 5543af6ab5fSopenharmony_cifor each file in `listRules`: 5553af6ab5fSopenharmony_ci for each option in file: 5563af6ab5fSopenharmony_ci switch(option) { 5573af6ab5fSopenharmony_ci case -disable-obfuscation: 5583af6ab5fSopenharmony_ci finalRule.disableObfuscation = true; 5593af6ab5fSopenharmony_ci continue; 5603af6ab5fSopenharmony_ci case -enable-property-obfuscation: 5613af6ab5fSopenharmony_ci finalRule.enablePropertyObfuscation = true; 5623af6ab5fSopenharmony_ci continue; 5633af6ab5fSopenharmony_ci case -enable-toplevel-obfuscation: 5643af6ab5fSopenharmony_ci finalRule.enableToplevelObfuscation = true; 5653af6ab5fSopenharmony_ci continue; 5663af6ab5fSopenharmony_ci case -compact: 5673af6ab5fSopenharmony_ci finalRule.compact = true; 5683af6ab5fSopenharmony_ci continue; 5693af6ab5fSopenharmony_ci case -remove-log: 5703af6ab5fSopenharmony_ci finalRule.removeLog = true; 5713af6ab5fSopenharmony_ci continue; 5723af6ab5fSopenharmony_ci case -print-namecache: 5733af6ab5fSopenharmony_ci finalRule.printNamecache = #{specified path}; 5743af6ab5fSopenharmony_ci continue; 5753af6ab5fSopenharmony_ci case -apply-namecache: 5763af6ab5fSopenharmony_ci finalRule.applyNamecache = #{specified path}; 5773af6ab5fSopenharmony_ci continue; 5783af6ab5fSopenharmony_ci case -keep-property-name: 5793af6ab5fSopenharmony_ci finalRule.keepPropertyName.push(#{specified names}); 5803af6ab5fSopenharmony_ci continue; 5813af6ab5fSopenharmony_ci case -keep-global-name: 5823af6ab5fSopenharmony_ci finalRule.keepGlobalName.push(#{specified names}); 5833af6ab5fSopenharmony_ci continue; 5843af6ab5fSopenharmony_ci case -keep-dts: 5853af6ab5fSopenharmony_ci finalRule.keepDts.push(#{specified file path}); 5863af6ab5fSopenharmony_ci continue; 5873af6ab5fSopenharmony_ci } 5883af6ab5fSopenharmony_ci end-for 5893af6ab5fSopenharmony_ciend-for 5903af6ab5fSopenharmony_ci``` 5913af6ab5fSopenharmony_ci 5923af6ab5fSopenharmony_ciThe final obfuscation rules are in the object `finalRule`. 5933af6ab5fSopenharmony_ci 5943af6ab5fSopenharmony_ciIf you are building HAR, the resulting `obfuscate.txt` are obtained by merging the rules from `consumerFiles` in main 5953af6ab5fSopenharmony_ciproject and local dependent libraries, and `obfuscate.txt` in remote dependent HARs. The merging strategy is the same 5963af6ab5fSopenharmony_ciexcept: 5973af6ab5fSopenharmony_ci 5983af6ab5fSopenharmony_ci* The `-keep-dts` option will be converted to `-keep-global-name` and `-keep-property-name` options in the resulting 5993af6ab5fSopenharmony_ci`obfuscate.txt`. 6003af6ab5fSopenharmony_ci* The options `-print-namecache` and `apply-namecache` will be omitted and will not appear in the resulting 6013af6ab5fSopenharmony_ci`obfuscate.txt`. 602