在entry模块调用harA的文件,harA中用@标识加载harB的Worker文件,运行闪退。
Reason:Signal:SIGABRT(SI_TKILL)@0x01317b55000014a6 from:5286:20020053 LastFatalMessage:[default] [LoadJSPandaFile:106] resolveBufferCallback get hsp buffer failed, hsp path:/data/storage/el1/bundle/harA/ets/modules.abc, errorMsg:hap path error: /data/storage/el1/bundle/harA.hsp
harA中改用相对路径加载harB的Worker文件,运行依旧闪退。
const testWorker: worker.ThreadWorker = new worker.ThreadWorker('../../../../../harB/src/main/ets/workers/Worker.ets'); Reason:ReferenceError Error name:ReferenceError Error message:Cannot find module '@normalized:N&&&harB/src/main/ets/workers/Worker&1.0.0&1.0.0' imported from ''.
使用Worker模块具体功能时,须先构造Worker实例对象,其构造函数与API版本相关,且构造函数需要传入Worker线程文件的路径(scriptURL)。参考文件路径注意事项:
Stage模型下加载HAR中Worker线程文件存在以下两种情况:
当开启useNormalizedOHMUrl(即将工程目录中与entry同级别的应用级build-profile.json5文件中strictMode属性的useNormalizedOHMUrl字段配置为true)或HAR包会被打包成三方包使用时,则HAR包中使用Worker仅支持通过相对路径的加载形式创建。
工程级build-profile.json5配置useNormalizedOHMUrl为true,仅支持通过相对路径的加载形式创建。创建Worker对象所在文件与Worker线程文件不在同一包内,导致用相对路径加载Worker无法识别。
当useNormalizedOHMUrl设置为true时需满足以下两个条件:
import { worker } from '@kit.ArkTS';
export class WorkerUtil {
// 在worker文件同一包内创建实例
private testWorker: worker.ThreadWorker = new worker.ThreadWorker('../workers/WorkerTest.ets');
postMsg(args: Object) {
try {
// 使用实例发送消息
this.testWorker.postMessage(args);
} catch (error) {
// Implement error handling.
}
}
} export { WorkerUtil } from './src/main/ets/workers/WorkerUtil'; {
"name": "hara",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "Index.ets",
"author": "",
"license": "Apache-2.0",
"dependencies": {
"harb": 'file:../harB'
}
} import { WorkerUtil } from 'harb';
@Component
export struct MainPage {
build() {
Row() {
Column() {
Text('Hello World')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Bold)
.onClick(() => {
// 创建harB的工具类WorkerUtil实例
let workerUtil: WorkerUtil = new WorkerUtil();
workerUtil.postMsg(1);
})
}
.width('100%')
}
.height('100%')
}
} Q:在项目中引入ijkplayer实现录屏功能,引入ijkplayer本地HAR包后,编译时提示useNormalizedOHMUrl is not true,将该属性设置为true时,再打开项目时,整个项目直接出现白屏,无法进入到首页。报错信息如下:

A:根据报错信息分析得出是Worker路径不对,Worker使用有这样一个限制,当开启useNormalizedOHMUrl(在工程目录中与entry同级别的应用级build-profile.json5文件中,将strictMode属性下的useNormalizedOHMUrl字段配置为true)或HAR包被打包成三方包使用时,HAR包中使用Worker仅支持通过相对路径的加载形式创建。可参考Worker线程文件路径注意事项。