HarmonyOS 鸿蒙Next开启混淆后报错Cannot read property includes of undefined
HarmonyOS 鸿蒙Next开启混淆后报错Cannot read property includes of undefined 在依次开启下面三项混淆规则后,项目报错:
-enable-toplevel-obfuscation
-enable-property-obfuscation
-enable-export-obfuscation
- 报错的详细信息如下:
VersionCode:7052
PreInstalled:No
Foreground:Yes
Pid:50927
Uid:20020005
Reason:TypeError
Error name:TypeError
Error message:Cannot read property includes of undefined
Stacktrace:
Cannot get SourceMap info, dump raw stack:
at anonymous (entry|entry|1.0.0|src/main/ets/pages/main/MainPage.ts:771:1)
- 混淆前的代码如下:
Image(tabBarModel.icon.includes("http") ? tabBarModel.icon : $r(tabBarModel.icon))
.width(24)
.height(24)
.objectFit(ImageFit.Contain)
.visibility(this.currentTabBarModel?.tabType === tabBarModel.tabType ? Visibility.Hidden :
Visibility.Visible)
- 混淆后的代码如下:
this.observeComponentCreation2((elmtId, isInitialRender) => {
Image.create(o135.icon.includes("http") ? o135.icon : { "id": -1, "type": -1, params: [o135.icon], "bundleName": "com.com.ccmm", "moduleName": "entry" });
Image.debugLine("entry/src/main/ets/pages/main/MainPage.ets(381:11)", "entry");
Image.width(24);
Image.height(24);
Image.objectFit(ImageFit.Contain);
Image.visibility(this.currentTabBarModel?.tabType === o135.tabType ? Visibility.Hidden :
Visibility.Visible);
}, Image);
报错信息显示是调用了includes
方法后导致了崩溃,即调用了tabBarModel.icon.includes("http")
这段代码后出问题。而tabBarModel
是一个传入进来的class对象,icon
是其一个string类型的属性。为什么这里会出现includes
的错误呢?我该怎么去keep?
更多关于HarmonyOS 鸿蒙Next开启混淆后报错Cannot read property includes of undefined的实战教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于HarmonyOS 鸿蒙Next开启混淆后报错Cannot read property includes of undefined的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中开启混淆后出现“Cannot read property ‘includes’ of undefined”错误,通常是由于混淆工具在处理代码时,未能正确识别或保留某些关键属性或方法。includes
是JavaScript中数组的原生方法,混淆过程中可能被误删或替换,导致运行时无法找到该方法。
解决方法可以尝试在混淆配置文件中明确排除includes
方法或相关代码段,确保其在混淆过程中不被修改。具体操作可以在proguard-rules.pro
文件中添加排除规则,如:
-keep class * {
public *;
-keepclassmembers class * {
public *;
}
}
或针对includes
方法的排除:
-keepclassmembers class * {
boolean includes(java.lang.Object);
}
确保混淆配置正确后重新编译项目,错误应可解决。