• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Common Issues with ArkGuard
2
3## Troubleshooting Functional Issues
4
5### Procedure
61. Configure the **-disable-obfuscation** option in the **obfuscation-rules.txt** file to disable obfuscation, and check whether the issue is caused by obfuscation.
72. If the issue is related to obfuscation, review the documentation for the following options and understand when to configure trustlists to avoid issues:
8    1. [-enable-toplevel-obfuscation](source-obfuscation.md#-enable-toplevel-obfuscation): obfuscates top-level scope names.
9    2. [-enable-property-obfuscation](source-obfuscation.md#-enable-property-obfuscation): obfuscates property names. Use [-keep-property-name](source-obfuscation.md#-keep-property-name) to configure a trustlist for property names used in network calls, JSON field, dynamic accesses, and so library interfaces.
10    3. [-enable-export-obfuscation](source-obfuscation.md#-enable-export-obfuscation): obfuscates exported names. Generally, it is used together with **-enable-toplevel-obfuscation** and **-enable-property-obfuscation**. Use [-keep-global-name](source-obfuscation.md#-keep-global-name) to configure a trustlist for exported/imported interfaces.
11    4. [-enable-filename-obfuscation](source-obfuscation.md#-enable-filename-obfuscation): obfuscates file names. Use [-keep-file-name](source-obfuscation.md#-keep-file-name) to configure a trustlist for file paths and names in dynamically import or runtime loading scenarios.
123. If your issue matches any cases listed below, apply the suggested solutions.
134. If the issue is not covered, use a positive approach to identify the problem (remove specific configuration items if the corresponding functionality is not needed).
145. Analyze runtime crashes as follows:
15    1. Open the application runtime logs or the **Crash** dialog box in DevEco Studio to find the crash stack.
16    2. The line numbers in the crash stack match the [compiled product](source-obfuscation-guide.md#viewing-obfuscation-effects), and method names might be obfuscated. Therefore, you are advised to check the compiled product based on the crash stack, analyze the names that cannot be obfuscated, and add them to the trustlist.
176. Analyze functional exceptions (for example, white screens) as follows:
18    1. Opening the application runtime logs: Select HiLog and search for logs directly related to the exceptions.
19    2. Locating the problematic code segment: Identify the specific code block causing the exceptions through log analysis.
20    3. Enhancing log output: Add log records for data fields in the suspected code segment.
21    4. Analyzing and identifying critical fields: Determine if the data exception is caused by obfuscation through the enhanced log output.
22    5. Configuring a trustlist for critical fields: Add fields that directly affect application functionality after obfuscation to the trustlist.
23
24#### Troubleshooting Unexpected Obfuscation Behavior
25If unexpected obfuscation behavior occurs, check whether certain obfuscation options are configured for the dependent local modules or third-party libraries.
26
27Example:
28
29If **-compact** is not configured for the current module, but the code in the obfuscated intermediate product is compressed into a single line, perform the following steps:
30
311. Check the **dependencies** field in the **oh-package.json5** file of the current module to identify dependencies.
322. Search for **-compact** in the obfuscation configuration file of the dependent modules or third-party libraries.
33    * Search for **-compact** in the **consumer-rules.txt** file in the local libraries.
34    * Search for **-compact** in all **obfuscation.txt** files in the project-level **oh_modules** directory.
35
36> **NOTE**
37>
38> You are not advised to configure the following options in the **obfuscation.txt** file of third-party libraries, as they can cause unexpected behavior or crashes when the main module is obfuscated. Contact the library providers to remove these options and repackage the libraries.
39> -enable-property-obfuscation
40> -enable-string-property-obfuscation
41> -enable-toplevel-obfuscation
42> -remove-log
43> -compact
44
45## Property Obfuscation Issues
46
47### Database Field Errors When Property Name Obfuscation is Enabled
48
49**Symptom**
50
51The error message is "table Account has no column named a23 in 'INSET INTO Account(a23)'."
52
53**Possible Causes**
54
55The SQL statement uses database field names that are obfuscated, whereas the database expects the original names.
56
57**Solution**
58
59Use the **-keep-property-name** option to add the database fields to the trustlist.
60
61### Properties Obfuscated When Record<string, Object> Is Used as an Object Type
62
63**Symptom**
64
65When **Record<string, Object>** is used as an object type, properties like **linkSource** are obfuscated, causing the error. Example:
66
67```
68// Before obfuscation:
69import { Want } from '@kit.AbilityKit';
70let petalMapWant: Want = {
71  bundleName: 'com.example.myapplication',
72  uri: 'maps://',
73  parameters: {
74    linkSource: 'com.other.app'
75  }
76}
77```
78
79```
80// After obfuscation:
81import type Want from "@ohos:app.ability.Want";
82let petalMapWant: Want = {
83    bundleName: 'com.example.myapplication',
84    uri: 'maps://',
85    parameters: {
86        i: 'com.other.app'
87    }
88};
89```
90
91**Possible Causes**
92
93In this example, the object's properties need to be passed to the system to load a specific page, so the property names should not be obfuscated. The type **Record<string, Object>** is a generic definition for an object with string keys and does not describe the internal structure or property types in detail. Therefore, the obfuscation tool cannot identify which properties should not be obfuscated, leading to the obfuscation of internal property names like **linkSource**.
94
95**Solution**
96
97Add the problematic property names to the property trustlist. The following is an example:
98
99```
100-keep-property-name
101linkSource
102```
103
104### Property Name Obfuscation Inconsistency Across Files
105
106**Symptom**
107
108The following obfuscation configuration is used:
109
110```
111-enable-property-obfuscation
112-keep
113./file1.ts
114```
115
116**file2.ts** imports an interface from **file1.ts**, and the interface contains object-type properties. As a result, these properties are retained in **file1.ts** but obfuscated in **file2.ts**, leading to function exceptions. Example:
117
118```
119// Before obfuscation:
120// file1.ts
121export interface MyInfo {
122  age: number;
123  address: {
124    city1: string;
125  }
126}
127
128// file2.ts
129import { MyInfo } from './file1';
130const person: MyInfo = {
131  age: 20,
132  address: {
133    city1: "shanghai"
134  }
135}
136```
137
138```
139// After obfuscation:
140// The code of file1.ts is retained.
141// file2.ts
142import { MyInfo } from './file1';
143const person: MyInfo = {
144  age: 20,
145  address: {
146    i: "shanghai"
147  }
148}
149```
150
151**Possible Causes**
152
153The **-keep** option retains the code in the **file1.ts** file, but properties within exported types (for example, **address**) are not automatically added to the property trustlist. Therefore, these properties are obfuscated when being used in other files.
154
155**Solution**
156
157Solution 1: Define the property type using **interface** and export it. This will automatically add the property to the trustlist. Example:
158
159```
160// file1.ts
161export interface AddressType {
162  city1: string;
163}
164export interface MyInfo {
165  age: number;
166  address: AddressType;
167}
168```
169
170Solution 2: Use the **-keep-property-name** option to add properties within types that are not directly exported to the trustlist. Example:
171
172```
173-keep-property-name
174city1
175```
176
177### String Literal Property Names Obfuscated When -enable-string-property-obfuscation Is Not Configured
178
179**Symptom**
180
181```
182// Before obfuscation:
183person["age"] = 22;
184```
185
186```
187// After obfuscation:
188person["b"] = 22;
189```
190
191**Possible Causes**
192
193A dependent module may have enabled string property name obfuscation, affecting the main module.
194
195**Solution**
196
197Solution 1: Check whether any dependent modules have enabled string property name obfuscation. If yes, disable it.
198
199Solution 2: If the problematic module cannot be identified, add the string literal property names to the trustlist directly.
200
201## Inconsistent Imported/Exported Names
202
203### Dynamic Import of a Class with Mismatched Obfuscation
204
205**Symptom**
206
207When **-enable-property-obfuscation** is not configured, dynamically importing a class results in the class being obfuscated during its definition, but not during the call, causing the error.
208
209```
210// Before obfuscation:
211// file1.ts
212export class Test1 {}
213// file2.ts
214let mytest = (await import('./file1')).Test1;
215```
216
217```
218// After obfuscation:
219// file1.ts
220export class w1 {}
221// file2.ts
222let mytest = (await import('./file1')).Test1;
223```
224
225**Possible Causes**
226
227The exported class **Test1** is a top-level domain name. When being 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.
228
229**Solution**
230
231Use **-keep-global-name** to add **Test1** to the trustlist.
232
233### Exported Method in Namespace with Mismatched Obfuscation
234
235**Symptom**
236
237When **-enable-property-obfuscation** is not configured, the definition of the method in the namespace is obfuscated, but not obfuscated during the call, causing the error.
238
239```
240// Before obfuscation:
241//file1.ts
242export namespace ns1 {
243  export class person1 {}
244}
245//file2.ts
246import {ns1} from './file1';
247let person1 = new ns1.person1();
248```
249
250```
251// After obfuscation:
252//file1.ts
253export namespace a3 {
254  export class b2 {}
255}
256//file2.ts
257import {a3} from './file1';
258let person1 = new a3.person1();
259```
260
261**Possible Causes**
262
263**person1** in the namespace is an exported element. When being called through **ns1.person1**, it is a property. Because the **-enable-property-obfuscation** option is not configured, the property name is not obfuscated during the call.
264
265**Solution**
266
267Solution 1: Configure the **-enable-property-obfuscation** option.
268Solution 2: Use the **-keep-global-name** option to add the methods exported from the namespace to the trustlist.
269
270## Inter-Module Dependency Issues
271
272### Incorrect Obfuscation of HSP Module Exports
273
274**Symptom**
275
276The exported interfaces of an HSP module are incorrectly obfuscated in the main module.
277
278```
279// Before obfuscation:
280import { MyHspClass } from "myhsp";
281new MyHspClass().myHspMethod();
282```
283
284```
285// After obfuscation:
286import { t } from "@normalized:N&myhsp&&myhsp/Index&";
287new t().a1();
288```
289
290**Possible Causes**
291
292When **-enable-export-obfuscation** and **-enable-toplevel-obfuscation** are configured, the following scenarios apply to method name obfuscation when the main module calls methods from other modules:
293
294| Main Module| Dependent Module| Imported/Exported Name Obfuscation|
295| ------- | ------- | ----------------------------|
296| HAP/HSP | HSP     | The HSP and main module are independently compiled, resulting in inconsistent obfuscated names. Both need trustlist configurations.|
297| HAP/HSP | Local HAR| The local HAR is compiled together with the main module, resulting in consistent obfuscated names.|
298| HAP/HSP | Third-party library | Exported names and their properties in third-party libraries are collected into the trustlist, so they are not obfuscated during import/export.|
299
300**Solution**
301
302To enable other modules to correctly call the methods of the HSP module, add a trustlist. Since both the main module and the HSP module need consistent trustlist configurations, follow these steps:
303
3041. Add the trustlist to the obfuscation configuration file (for example, **hsp-white-list.txt**) of the HSP module.
3052. In the obfuscation configuration of other modules that depend on the HSP module, include this configuration file via the **files** field.
306This ensures consistent trustlist configurations and avoids redundant maintenance. The configuration method is shown in the figure below.
307
308![obfuscation-config](figures/obfuscation-config.png)
309
310### Singleton Function Exceptions and Interface Call Failures in HAP and HSP with a Shared Local Source Code HAR
311
312**Symptom**
313
314* If file name obfuscation is enabled, the following issue may occur:
315  * Singleton function exceptions: The reason is that the HAP and HSP modules execute independent build and obfuscation processes. The same file names in the shared local source code HAR may be obfuscated differently in the HAP and HSP packages.
316  * Interface call failures: The reason is that the HAP and HSP modules execute independent build and obfuscation processes. Different file names in the shared local source code HAR may be obfuscated to the same name in the HAP and HSP packages.
317* If **-enable-export-obfuscation** and **-enable-toplevel-obfuscation** are configured, the interface loading failures may occur at runtime.
318
319**Possible Causes**
320
321The HAP and HSP modules execute independent build and obfuscation processes, resulting in different obfuscated names for the interfaces exposed by the shared local source code HAR.
322
323**Solution**
324
325Solution 1: Change the shared local source code HAR to a [bytecode HAR](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V5/ide-hvigor-build-har-V5#section179161312181613). This prevents the HAR from being re-obfuscated when being depended upon.
326
327Solution 2: Build the shared local source code HAR in [release mode](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V5/ide-hvigor-build-har-V5#section19788284410). This ensures that file names and exported interfaces are not obfuscated when the HAR is depended upon.
328