應(yīng)用內(nèi)HSP開發(fā)指導(dǎo)

2024-01-25 11:56 更新

應(yīng)用內(nèi)HSP指的是專門為某一應(yīng)用開發(fā)的HSP,只能被該應(yīng)用內(nèi)部其他HAP/HSP使用,用于應(yīng)用內(nèi)部代碼、資源的共享。

應(yīng)用內(nèi)HSP跟隨其宿主應(yīng)用的APP包一起發(fā)布,與該宿主應(yīng)用具有相同的包名和生命周期。

開發(fā)應(yīng)用內(nèi)HSP

HSP模塊可以在DevEco Studio中由指定模板創(chuàng)建,我們以創(chuàng)建一個名為library的HSP模塊為例?;镜墓こ棠夸浗Y(jié)構(gòu)大致如下:

  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"標(biāo)識模塊類型,HSP的"type"是"shared"。

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

HSP通過在入口文件中導(dǎo)出接口,對外提供能力。入口文件在模塊oh-package.json5的"main"中配置。例如:

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

導(dǎo)出ts類和方法

通過export導(dǎo)出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'

導(dǎo)出ArkUI組件

ArkUI組件也可以通過export導(dǎo)出,例如:

  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調(diào)用方(如entry)下的資源entry/src/main/ets/common/example.png。

導(dǎo)出native方法

在HSP中也可以包含C++編寫的so。對于so中的native方法,HSP通過間接的方式導(dǎo)出,以導(dǎo)出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'

使用應(yīng)用內(nèi)HSP

要使用HSP中的接口,首先需要在使用方的oh-package.json5中配置對它的依賴。如果應(yīng)用內(nèi)HSP和使用方在同一工程下,可以直接本地引用,例如:

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

然后就可以像使用HAR一樣調(diào)用HSP的對外接口了。

例如,上面的library已經(jīng)導(dǎo)出了下面這些接口:

  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. }

跨包頁面路由跳轉(zhuǎn)

若開發(fā)者想在entry模塊中,添加一個按鈕跳轉(zhuǎn)至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. // 添加按鈕,以響應(yīng)用戶點擊
  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方法的入?yún)⒅衭rl的內(nèi)容為:

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

url內(nèi)容的模板為:

  1. '@bundle:包名(bundleName)/模塊名(moduleName)/路徑/頁面所在的文件名(不加.ets后綴)'
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號