DEV Community

HarmonyOS
HarmonyOS

Posted on

In Release mode, objects cannot use the Object.attribute method to access attributes

Read the original article:In Release mode, objects cannot use the Object.attribute method to access attributes

Problem Description

  • When the build mode is set to release, the value obtained from the Record object through T.a is undefined. It can only be accessed using T["a"]. However, in debug or other build modes, the value can be accessed normally.
  • Developers hope that DevEco Studio can provide an out-of-the-box obfuscation configuration, or automatically generate a general obfuscation rule file when creating a new project, eliminating the need for additional configuration.

Background Knowledge

  • Starting from DevEco Studio NEXT Developer Beta3 (5.0.3.600), the code obfuscation feature is disabled by default for new projects and modules. If code obfuscation is enabled in the module-level build-profile.json5 configuration file, the obfuscation rule file obfuscation-rules.txt will enable the recommended rules by default, including -enable-property-obfuscation, -enable-toplevel-obfuscation, -enable-filename-obfuscation, and -enable-export-obfuscation. Developers can further choose which rules to enable in the obfuscation-rules.txt file. For more information about these rules, please refer to Obfuscation Rules.
  • After enabling obfuscation, methods, properties, or paths in the code are obfuscated. However, the runtime accesses the unobfuscated names, which may lead to functionality issues. Therefore, it is necessary to configure the keep options for the corresponding fields. For troubleshooting and configuring keep options, refer to Obfuscation Rules. If you do not want properties to be obfuscated, you can open the obfuscation-rules.txt file in the module directory and configure the rules to keep the properties. For details on keep options, see Keep Options.
  • If there are many scenarios or fields that need to be checked and configured, DevEco Studio provides an Obfuscation Helper tool. It can scan the source code based on modules and scenarios to quickly identify the fields and keep options that need configuration, and generate a whitelist rule file with one click. However, please note that for some cases where names or properties are accessed dynamically at runtime, the Obfuscation Helper will detect such scenarios, and developers will need to manually verify and configure the whitelist according to business needs. For details, refer to Configuring Keep Options via Obfuscation Helper.

Troubleshooting Process

  • Check the data source to confirm whether the data was already obfuscated before being passed in.
  • Compare the behavior with obfuscation enabled and disabled to see if the issue can be reproduced. It was found that when obfuscation is enabled, T.a cannot access the property correctly and returns undefined, but the issue disappears when obfuscation is disabled.
  • The Obfuscation Helper can scan the source code based on modules and scenarios to quickly identify the required keep options and whitelist fields, and generate a whitelist rule file with one click. This not only reduces manual configuration but also helps avoid runtime issues caused by missing critical configurations.

Analysis Conclusion

  • The root cause is that code obfuscation is enabled. Enabling obfuscation causes object property names to be obfuscated, resulting in T.a being unable to correctly access the property value. The reason T["a"] works is that string keys are not obfuscated.
  • For simplifying obfuscation configuration, it is recommended to use the Obfuscation Helper tool.

Reference document: Code Obfuscation.

Solution

  • Disable obfuscation to resolve the issue.
  • If obfuscation must be enabled, use T["a"] to access the property.
  • If the codebase is large and you do not want to disable obfuscation, configure the properties in the Record object to the whitelist using the -keep-property-name option.
  • During HarmonyOS app development, if certain classes or methods need to be excluded from obfuscation, developers must manually configure whitelist rules. To simplify this process, it is recommended to use the Obfuscation Helper to automate part of the configuration.

Written by Bunyamin Akcay

Top comments (0)