HarmonyOS鸿蒙Next应用其他模块module资源如何获取?

HarmonyOS鸿蒙Next应用其他模块module资源如何获取? 鸿蒙应用其他模块module资源如何获取?

3 回复

方案一:

以获取音效文件举例:

// 主模块中的resource-rawfile中
let fileDescriptor = await getContext(this).resourceManager.getRawFd("test.mp3");
// 子模块中的resource-rawfile中
let fileDescriptor = await getContext(this).createModuleContext("模块名").resourceManager.getRawFd("test.mp3");

// 主模块中的resource-element-字符串资源
getContext(this).resourceManager.getStringByNameSync('app.string.EntryAbility_label');
// 子模块中的resource-element-字符串资源
getContext(this).createModuleContext("模块名").resourceManager.getStringByNameSync('app.string.EntryAbility_label');

// 同理颜色,字体大小等配置资源的获取都是如此。需要在上下文后面,在指定创建子模块的上下文。再通过resourceManager操作获取资源。

createModuleContext从 API Version 12 开始废弃,建议使用application.createModuleContext替代。

和老接口对比,只不过换成主体从getContext 切换到application中创建子模块上下文了

import { UIAbility, application, common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

export default class EntryAbility extends UIAbility {
  onCreate() {
    let moduleContext: common.Context;
    try {
      application.createModuleContext(this.context, '模块名').then((data: Context) => {
        moduleContext = data;
        console.info('createBundleContext success!');
      }).catch((error: BusinessError) => {
        let code: number = (error as BusinessError).code;
        let message: string = (error as BusinessError).message;
        console.error(`createModuleContext failed, error.code: ${code}, error.message: ${message}`);
      })
    } catch (error) {
      let code: number = (error as BusinessError).code;
      let message: string = (error as BusinessError).message;
      console.error(`createModuleContext failed, error.code: ${code}, error.message: ${message}`);
    }
  }
}

注意:

1、createModuleContext获取的是基类Context,主要是用来根据不同模块名获取Context,分别指向不同的HSP。 2、HSP只是一个动态共享包,其包含了静态资源,但是本身是没有上下文的概念。所以需要通过创建Context的方式去获取该资源或者Module的信息。 3、createModuleContext获取的是一个通用的、模块级的Context,不是ApplicationContext。

方案二:

将资源文件进行静态变量获取后导出,暴露给其他模块使用。这种方式针对资源文件少的情况下建议这样写。

export static ImageRes {
	public appBg(){
	 	return $r("app.media.icon_bg");
	}
	public appEmpty(){
	 	return $r("app.media.icon_empty");
	}
}

更多关于HarmonyOS鸿蒙Next应用其他模块module资源如何获取?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中,跨模块获取资源需使用资源管理器ResourceManager。首先通过UI上下文(如组件内的this.context)获取ResourceManager实例。然后调用其getString、getMedia等接口,并传入目标资源ID来访问。资源ID需通过$r(‘模块名:资源类型/资源名’)形式引用,确保模块依赖已正确配置。

在HarmonyOS Next中,跨模块访问资源(如图片、字符串、颜色等)需要通过明确的资源引用方式来实现。以下是核心方法:

1. 使用 $r 引用资源 这是最常用的方式。语法为:$r('app.模块名.type.资源名')

  • app: 固定前缀,代表当前应用。
  • 模块名: 资源所在模块的名称(在模块的 module.json5 中定义的 "name")。
  • type: 资源类型,例如:
    • string (字符串)
    • media (图片、音频等媒体)
    • color (颜色)
    • float (尺寸)
    • pattern (样式)
  • 资源名: 在资源文件中定义的资源ID(名称)。

示例: 假设你的应用有一个名为 news 的模块,其中包含一个图片资源 ic_logo.png 和一个字符串资源 app_name

  • entry (主模块) 或其它模块的UI代码中引用:
    // 获取news模块的图片
    Image($r('app.news.media.ic_logo'))
    // 获取news模块的字符串
    Text($r('app.news.string.app_name'))
    

2. 在 module.json5 中声明资源依赖 如果模块A需要频繁使用模块B的资源,可以在模块A的 module.json5 配置文件中声明对模块B的资源依赖。

{
  "module": {
    "name": "moduleA",
    "dependencies": [
      {
        "bundleName": "你的应用包名",
        "moduleName": "moduleB" // 依赖的资源所在模块
      }
    ]
  }
}

声明后,在模块A中即可使用 $r('app.moduleB.type.resource_id') 的方式访问模块B的资源。

关键点总结

  • 路径必须完整:必须指定完整的模块名、资源类型和资源ID。
  • 模块隔离:HarmonyOS Next默认模块资源隔离,不声明依赖或使用完整路径则无法直接访问。
  • $r 是核心:所有资源引用(包括本模块)都推荐使用 $r('app.模块名.type.资源名') 这一统一语法,这是区别于旧版的重要变化。
  • 编译时检查:资源引用在编译时会进行校验,如果路径错误或资源不存在,会报编译错误。

因此,获取其他模块资源的直接答案就是:使用 $r('app.目标模块名.资源类型.资源ID') 这一标准格式进行引用,并确保在需要时配置模块依赖。

回到顶部