HarmonyOS 鸿蒙Next中如何正确解析 ABC 文件中的 Literals
HarmonyOS 鸿蒙Next中如何正确解析 ABC 文件中的 Literals
根据文档解析 ABC 文件,未能发现解析 LITERALS 的方式,原本 Header 中的 numLiteralArrays
和 literalArrayIdxOff
已变成保留字段,内部数据也全是 0xFFFFFFFF
。
想解析出这部分信息:
# LITERALS
0 0x184d { 10 [ string:"id", i32:16777224, string:"type", i32:10002, string:"params", null_value:0, string:"bundleName", string:"com.example.myapplication", string:"moduleName", string:"entry", ]}
1 0x1810 { 3 [ i32:1, string:"DOMAIN", i32:0, ]}
10 0x17a4 { 1 [ string:"EntryBackupAbility", ]}
11 0x171d { 5 [ i32:2, string:"4newTarget", i32:0, string:"this", i32:1, ]}
14 0x173a { 19 [ string:"onCreate", method:#~@0>#onCreate, method_affiliate:2, string:"onDestroy", method:#~@0>#onDestroy, method_affiliate:0, string:"onWindowStageCreate", method:#~@0>#onWindowStageCreate, method_affiliate:1, string:"onWindowStageDestroy", method:#~@0>#onWindowStageDestroy, method_affiliate:0, string:"onForeground", method:#~@0>#onForeground, method_affiliate:0, string:"onBackground", method:#~@0>#onBackground, method_affiliate:0, i32:6, ]}
2 0x1791 { 3 [ i32:1, string:"DOMAIN", i32:0, ]}
3 0x17ed { 7 [ string:"onBackup", method:#~@0>#onBackup, method_affiliate:0, string:"onRestore", method:#~@0>#onRestore, method_affiliate:1, i32:2, ]}
4 0x189d { 13 [ string:"setInitiallyProvidedValue", method:#~@0>#setInitiallyProvidedValue, method_affiliate:1, string:"updateStateVars", method:#~@0>#updateStateVars, method_affiliate:1, string:"purgeVariableDependenciesOnElmtId", method:#~@0>#purgeVariableDependenciesOnElmtId, method_affiliate:1, string:"aboutToBeDeleted", method:#~@0>#aboutToBeDeleted, method_affiliate:0, i32:4, ]}
5 0x18da { 12 [ string:"bundleName", string:"com.example.myapplication", string:"moduleName", string:"entry", string:"pagePath", string:"pages/Index", string:"pageFullPath", string:"entry/src/main/ets/pages/Index", string:"integratedHsp", string:"false", string:"moduleType", string:"followWithHap", ]}
6 0x16b3 { 2 [ string:"EntryAbility", string:"onWindowStageCreate", ]}
7 0x1823 { 2 [ string:"Index", string:"initialRender", ]}
8 0x191a { 7 [ i32:3, string:"Index", i32:0, string:"4newTarget", i32:1, string:"this", i32:2, ]}
9 0x1880 { 5 [ i32:2, string:"4newTarget", i32:0, string:"this", i32:1, ]}
12 0x16c1 { 5 [
MODULE_REQUEST_ARRAY: {
0 : @ohos:app.ability.ConfigurationConstant,
1 : @ohos:app.ability.UIAbility,
2 : @ohos:hilog,
3 : @normalized:Y&&&libentry.so&,
};
ModuleTag: REGULAR_IMPORT, local_name: ConfigurationConstant, import_name: default, module_request: @ohos:app.ability.ConfigurationConstant;
ModuleTag: REGULAR_IMPORT, local_name: UIAbility, import_name: default, module_request: @ohos:app.ability.UIAbility;
ModuleTag: REGULAR_IMPORT, local_name: hilog, import_name: default, module_request: @ohos:hilog;
ModuleTag: REGULAR_IMPORT, local_name: testNapi, import_name: default, module_request: @normalized:Y&&&libentry.so&;
ModuleTag: LOCAL_EXPORT, local_name: EntryAbility, export_name: default;
]}
13 0x1831 { 0 [
MODULE_REQUEST_ARRAY: {
};
]}
15 0x17ad { 3 [
MODULE_REQUEST_ARRAY: {
0 : @ohos:hilog,
1 : @ohos:application.BackupExtensionAbility,
};
ModuleTag: REGULAR_IMPORT, local_name: BackupExtensionAbility, import_name: default, module_request: @ohos:application.BackupExtensionAbility;
ModuleTag: REGULAR_IMPORT, local_name: hilog, import_name: default, module_request: @ohos:hilog;
ModuleTag: LOCAL_EXPORT, local_name: EntryBackupAbility, export_name: default;
]}
如何着手?
更多关于HarmonyOS 鸿蒙Next中如何正确解析 ABC 文件中的 Literals的实战教程也可以访问 https://www.itying.com/category-93-b0.html
-
找到
ark_disasm.exe
这个程序
编译器安装目录下面:C:\Program Files\Huawei\DevEco Studio\sdk\default\openharmony\toolchains
2. 将这个程序放在abc
同级目录下- 在当前目录下
cnd
执行命令:
ark_disasm.exe modules.abc 11.pa
- 生成
pa
文件
这个文件就是解析出来的
abc
文件 - 在当前目录下
更多关于HarmonyOS 鸿蒙Next中如何正确解析 ABC 文件中的 Literals的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
您好,我是需要在自己的程序中解析 abc
文件,所以我想了解的是 ark_disasm.exe
解析 LITERALS
的方式,其他信息(类、字段、方法等)我已经成功解析出来了。
问题在于,LITERALS
信息的解析方式没有在官方文档中描述,而 ark_disasm
的开源代码还停留在三四年前,已经和最新的文档不一致了,所以看开源代码中的 header 中还包含 literalArrayIdxOff
数据,而现在已经没有这部分数据了,无法解析出来想要的内容。
那有点难搞了这两个字段又是被弃用的,楼主可以咨询一下官方人员那边 或者开源鸿蒙上还有没有 其他的替代方案,
在HarmonyOS Next中解析ABC文件中的Literals需要使用ArkCompiler提供的API。主要步骤包括:
- 通过
BytecodeReader
加载ABC文件; - 使用
AbcFile
类获取常量池; - 调用
LiteralArray
相关方法读取具体Literal值。
关键API有getStringLiteral()
、getNumberLiteral()
等。解析时需注意ABC文件的版本兼容性,不同HarmonyOS版本可能采用不同字节码规范。
在HarmonyOS Next中解析ABC文件的LITERALS部分需要注意以下几点:
-
文档中提到的numLiteralArrays和literalArrayIdxOff字段确实已弃用,改为使用新的索引结构。
-
实际解析时需要:
- 查找文件中的literal pool区域
- 按照新的格式解析literal条目
- 每个literal条目包含类型标记和值
- 具体解析流程建议:
- 定位到literal pool起始位置
- 读取条目数量
- 循环解析每个条目:
- 读取类型标记(1字节)
- 根据类型读取对应长度的值
- 处理特殊类型(null/undefined等)
- 对于示例中的复杂结构,需要递归解析嵌套的数组和对象。
关键是要参考最新的ABC文件格式规范,旧文档可能不完全适用。建议直接分析实际ABC文件的二进制结构来验证解析逻辑。