应用内HSP开发指导

2024-01-25 11:56 更新

应用内HSP指的是专门为某一应用开发的HSP,只能被该应用内部其他HAP/HSP使用,用于应用内部代码、资源的共享。

应用内HSP跟随其宿主应用的APP包一起发布,与该宿主应用具有相同的包名和生命周期。

开发应用内HSP

HSP模块可以在DevEco Studio中由指定模板创建,我们以创建一个名为library的HSP模块为例。基本的工程目录结构大致如下:

  1. library
  2. ├── src
  3. │ └── main
  4. │ ├── ets
  5. │ │ ├── pages
  6. │ │ └── index.ets
  7. │ ├── resources
  8. │ └── module.json5
  9. └── oh-package.json5

模块module.json5中的"type"标识模块类型,HSP的"type"是"shared"。

  1. {
  2. "type": "shared"
  3. }

HSP通过在入口文件中导出接口,对外提供能力。入口文件在模块oh-package.json5的"main"中配置。例如:

  1. {
  2. "main": "./src/main/ets/index.ets"
  3. }

导出ts类和方法

通过export导出ts类和方法,例如:

  1. // library/src/main/ets/utils/test.ts
  2. export class Log {
  3. static info(msg) {
  4. console.info(msg);
  5. }
  6. }
  7. export function add(a: number, b: number) {
  8. return a + b;
  9. }
  10. export function minus(a: number, b: number) {
  11. return a - b;
  12. }

对外暴露的接口,需要在入口文件index.ets中声明:

  1. // library/src/main/ets/index.ets
  2. export { Log, add, minus } from './utils/test'

导出ArkUI组件

ArkUI组件也可以通过export导出,例如:

  1. // library/src/main/ets/components/MyTitleBar.ets
  2. @Component
  3. export struct MyTitleBar {
  4. build() {
  5. Row() {
  6. Text($r('app.string.library_title'))
  7. .fontColor($r('app.color.white'))
  8. .fontSize(25)
  9. .margin({left:15})
  10. }
  11. .width('100%')
  12. .height(50)
  13. .padding({left:15})
  14. .backgroundColor('#0D9FFB')
  15. }
  16. }

对外暴露的接口,需要在入口文件index.ets中声明:

  1. // library/src/main/ets/index.ets
  2. export { MyTitleBar } from './components/MyTitleBar'

HSP中资源使用说明

注意,在HSP中,通过$r/$rawfile可以使用本模块resources目录下的资源。

如果使用相对路径的方式,例如:

在HSP模块中使用Image("common/example.png"),实际上该Image组件访问的是HSP调用方(如entry)下的资源entry/src/main/ets/common/example.png。

导出native方法

在HSP中也可以包含C++编写的so。对于so中的native方法,HSP通过间接的方式导出,以导出libnative.so的乘法接口multi为例:

  1. // ibrary/src/main/ets/utils/nativeTest.ts
  2. import native from "libnative.so"
  3. export function nativeMulti(a: number, b: number) {
  4. return native.multi(a, b);
  5. }

对外暴露的接口,需要在入口文件index.ets中声明:

  1. // library/src/main/ets/index.ets
  2. export { nativeMulti } from './utils/nativeTest'

使用应用内HSP

要使用HSP中的接口,首先需要在使用方的oh-package.json5中配置对它的依赖。如果应用内HSP和使用方在同一工程下,可以直接本地引用,例如:

  1. // entry/oh-package.json5
  2. "dependencies": {
  3. "library": "file:../library"
  4. }

然后就可以像使用HAR一样调用HSP的对外接口了。

例如,上面的library已经导出了下面这些接口:

  1. // library/src/main/ets/index.ets
  2. export { Log, add, minus } from './utils/test'
  3. export { MyTitleBar } from './components/MyTitleBar'
  4. export { nativeMulti } from './utils/nativeTest'

在使用方的代码中,可以这样使用:

  1. // entry/src/main/ets/pages/index.ets
  2. import { Log, add, MyTitleBar, nativeMulti } from "library"
  3. @Entry
  4. @Component
  5. struct Index {
  6. @State message: string = 'Hello World'
  7. build() {
  8. Row() {
  9. Column() {
  10. MyTitleBar()
  11. Text(this.message)
  12. .fontSize(30)
  13. .fontWeight(FontWeight.Bold)
  14. Button('add(1, 2)')
  15. .onClick(()=>{
  16. Log.info("add button click!");
  17. this.message = "result: " + add(1, 2);
  18. })
  19. Button('nativeMulti(3, 4)')
  20. .onClick(()=>{
  21. Log.info("nativeMulti button click!");
  22. this.message = "result: " + nativeMulti(3, 4);
  23. })
  24. }
  25. .width('100%')
  26. }
  27. .height('100%')
  28. }
  29. }

跨包页面路由跳转

若开发者想在entry模块中,添加一个按钮跳转至library模块中的menu页面(路径为:library/src/main/ets/pages/menu.ets),那么可以在使用方的代码(entry模块下的Index.ets,路径为:entry/src/main/ets/MainAbility/Index.ets)里这样使用:

  1. import router from '@ohos.router';
  2. @Entry
  3. @Component
  4. struct Index {
  5. @State message: string = 'Hello World'
  6. build() {
  7. Row() {
  8. Column() {
  9. Text(this.message)
  10. .fontSize(50)
  11. .fontWeight(FontWeight.Bold)
  12. // 添加按钮,以响应用户点击
  13. Button() {
  14. Text('click to menu')
  15. .fontSize(30)
  16. .fontWeight(FontWeight.Bold)
  17. }
  18. .type(ButtonType.Capsule)
  19. .margin({
  20. top: 20
  21. })
  22. .backgroundColor('#0D9FFB')
  23. .width('40%')
  24. .height('5%')
  25. // 绑定点击事件
  26. .onClick(() => {
  27. router.pushUrl({
  28. url: '@bundle:com.example.hmservice/library/ets/pages/menu'
  29. }).then(() => {
  30. console.log("push page success");
  31. }).catch(err => {
  32. console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
  33. })
  34. })
  35. .width('100%')
  36. }
  37. .height('100%')
  38. }
  39. }
  40. }

其中router.pushUrl方法的入参中url的内容为:

  1. '@bundle:com.example.hmservice/library/ets/pages/menu'

url内容的模板为:

  1. '@bundle:包名(bundleName)/模块名(moduleName)/路径/页面所在的文件名(不加.ets后缀)'
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号