未验证 提交 b8773bb3 编写于 作者: O openharmony_ci 提交者: Gitee

!18250 应用间hsp资料描述修改

Merge pull request !18250 from junyi233/myfeature
......@@ -17,34 +17,31 @@
另外一部分为HSP,这部分为应用间`HSP`的具体实现,里面包含js/ts代码、C++库、资源和配置文件。这部分会上架到应用市场或者集成到系统版本中。
### 集成应用间HSP的HAR
`HAR`中的`index.d.ets`文件是应用间`HSP`导出的声明文件的入口,所有需要导出的接口,统一在`index.d.ets`文件中定义。`index.d.ets`文件路径如下:
`HAR`中的`index.ets`文件是应用间`HSP`导出的声明文件的入口,所有需要导出的接口,统一在`index.ets`文件中定义。`index.ets`文件路径如下:
```
src
├── main
| └── module.json5
├── index.d.ets
liba
├── src
│ └── main
│ ├── ets
│ │ ├── pages
│ │ └── index.ets
│ ├── resources
│ └── module.json5
└── oh-package.json5
```
`index.d.ets`内容样例如下:
```ts
@Component
export declare struct UIComponent {
build():void;
}
对外暴露的接口,需要在入口文件`index.ets`中声明:
export declare function hello(): string;
`index.ets`内容样例如下:
export declare function foo1(): string;
export declare function foo2(): string;
export declare function nativeHello(): string;
```ts
// liba/src/main/ets/index.ets
export { hello, foo1, foo2, nativeMulti, UIComponent } from './ui/MyUIComponent'
```
其中UIComponent为导出的ArkUI组件,`hello()``foo1()``foo2()`为应用间HSP导出的ts方法,`nativeHello()`方法为应用间HSP导出的native方法。具体实现如下:
其中UIComponent为导出的ArkUI组件,`hello()``foo1()``foo2()`为应用间HSP导出的ts方法,`nativeMulti()`方法为应用间HSP导出的native方法。具体实现如下:
#### **ArkUI组件**
`HSP`中ArkUI组件的具体实现样例:
```ts
// lib/src/main/ets/ui/MyUIComponent.ets
// liba/src/main/ets/ui/MyUIComponent.ets
@Component
export struct UIComponent {
@State message: string = 'Hello World'
......@@ -63,6 +60,7 @@ export struct UIComponent {
#### **ts方法**
`HSP`中ts方法的具体实现:
```ts
// liba/src/main/ets/ui/MyUIComponent.ets
export function hello(name: string): string {
return "hello + " + name;
}
......@@ -74,50 +72,22 @@ export function foo1() {
export function foo2() {
return "foo2";
}
```
#### **native方法**
`HSP`中native方法的具体实现:
```C++
#include "napi/native_api.h"
#include <js_native_api.h>
#include <js_native_api_types.h>
#include <string>
const std::string libname = "liba";
const std::string version = "v10001";
static napi_value Hello(napi_env env, napi_callback_info info) {
napi_value ret;
std::string msg = libname + ":native hello, " + version;
napi_create_string_utf8(env, msg.c_str(), msg.length(), &ret);
return ret;
}
`HSP`中也可以包含`C++`编写的`so`。对于`so`中的`native`方法,`HSP`通过间接的方式导出,以导出`libnative.so`的乘法接口`multi`为例:
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor desc[] = {
{"nativeHello", nullptr, Hello, nullptr, nullptr, nullptr, napi_default, nullptr}};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END
static napi_module demoModule = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "liba",
.nm_priv = ((void *)0),
.reserved = {0},
};
extern "C" __attribute__((constructor)) void RegisterLibaModule(void) {
napi_module_register(&demoModule);
```ts
// liba/src/main/ets/ui/MyUIComponent.ets
import native from "libnative.so"
export function nativeMulti(a: number, b: number) {
return native.multi(a, b);
}
```
### 使用HAR导出的能力
引用`HAR`前,需要先配置对`HAR`的依赖,配置方式可参考[文档](https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ohos-development-npm-package-0000001222578434#section89674298391)`HAR`配置成功后,在配置模块的`module.json5`中会生成相关依赖项信息,如下所示:
引用`HAR`前,需要先配置对`HAR`的依赖,配置方式可参考[文档](https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/creating_har_api9-0000001518082393-V3#section611662614153)`HAR`配置成功后,在配置模块的`module.json5`中会生成相关依赖项信息,如下所示:
```json
"dependencies": [
{
......@@ -180,7 +150,7 @@ struct Index {
#### **使用HAR中的native方法**
通过`import`引用`HAR`导出的native方法,示例如下所示:
``` ts
import { nativeHello } from 'liba'
import { nativeMulti } from 'liba'
@Component
struct Index {
......@@ -190,7 +160,7 @@ struct Index {
Button('Button')
.onClick(()=>{
// 引用HAR的native方法
nativeHello();
nativeMulti();
})
}
.width('100%')
......@@ -218,4 +188,4 @@ bm install -p feature.hap
```
4. 启动开发者自身的应用,调试相关功能。
**注意**:步骤2和步骤3不可以颠倒,否则会由于缺少必要的应用间`HSP`导致开发者的应用安装失败。更多`bm`相关指令可以参考[文档](https://gitee.com/openharmony/bundlemanager_bundle_framework#bm%E5%B7%A5%E5%85%B7%E5%91%BD%E4%BB%A4)
**注意**:步骤2和步骤3不可以颠倒,否则会由于缺少必要的应用间`HSP`导致开发者的应用安装失败。更多`bm`相关指令可以参考[文档](https://gitee.com/openharmony/bundlemanager_bundle_tool#bm工具命令)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册