Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
b8773bb3
D
Docs
项目概览
OpenHarmony
/
Docs
1 年多 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
b8773bb3
编写于
5月 17, 2023
作者:
O
openharmony_ci
提交者:
Gitee
5月 17, 2023
浏览文件
操作
浏览文件
下载
差异文件
!18250 应用间hsp资料描述修改
Merge pull request !18250 from junyi233/myfeature
上级
0bdb1d5e
881de9c2
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
30 addition
and
60 deletion
+30
-60
zh-cn/application-dev/quick-start/cross-app-hsp.md
zh-cn/application-dev/quick-start/cross-app-hsp.md
+30
-60
未找到文件。
zh-cn/application-dev/quick-start/cross-app-hsp.md
浏览文件 @
b8773bb3
...
...
@@ -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方法,
`native
Hello
()`
方法为应用间HSP导出的native方法。具体实现如下:
其中UIComponent为导出的ArkUI组件,
`hello()`
、
`foo1()`
与
`foo2()`
为应用间HSP导出的ts方法,
`native
Multi
()`
方法为应用间HSP导出的native方法。具体实现如下:
#### **ArkUI组件**
在
`HSP`
中ArkUI组件的具体实现样例:
```
ts
// lib/src/main/ets/ui/MyUIComponent.ets
// lib
a
/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
{
native
Hello
}
from
'
liba
'
import
{
native
Multi
}
from
'
liba
'
@
Component
struct
Index
{
...
...
@@ -190,7 +160,7 @@ struct Index {
Button
(
'
Button
'
)
.
onClick
(()
=>
{
// 引用HAR的native方法
native
Hello
();
native
Multi
();
})
}
.
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录