用Tauri开发HarmonyOS应用

更新于
约 6 分钟阅读
·
825 字
鸿蒙开发
Coding
#Tauri #HarmonyOS #应用开发 #跨平台

Tauri简介

创建小型、快速、安全、跨平台的应用程序,基于Rust构建。详见Tauri官网

开发、构建与安装步骤

1. 安装适配鸿蒙的tauri-cli以及ohrs支持

cargo install tauri-cli --git https://github.com/tauri-apps/tauri --branch feat/open-harmony
cargo install ohrs

之前官方仓库的tauri-cli鸿蒙模板的oh-package.json里面的@ohos-rs/ability版本定在0.2.1,会导致打包出来的.hap只显示白屏,将@ohos-rs/ability版本改到0.4.0-beta.0就不会白屏了。该问题的解决我已提交PR,详见fix(cli): bump @ohos-rs/ability to resolve white screen issue#15237

该PR已合并到feat/open-harmony分支,所以可以放心从tauri-apps/tauri安装了,不需要再从fork安装了。

2. 创建tauri项目

cargo install create-tauri-app --locked
cargo create-tauri-app

不多赘述,相信来到这里的读者具备基础的Tauri开发经验,如果没有也可以自己去Tauri官网学习。

3. 确保 Cargo.toml里patch了适配鸿蒙的tauri依赖

截止到2026年4月15日,wry和tao的适配鸿蒙的分支还没有被合并到主仓库,所以需要从richerfu的fork安装,详见

[build-dependencies]
tauri-build = { git = "https://github.com/tauri-apps/tauri", branch = "feat/open-harmony", features = [
] }


[dependencies]
tauri = { git = "https://github.com/tauri-apps/tauri", branch = "feat/open-harmony", features = [
] }
tauri-plugin-opener = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
napi-ohos = { version = "1.1" }
napi-derive-ohos = "1.1"
tauri-plugin-fs = "2"


[patch.crates-io]
wry = { git = "https://github.com/richerfu/wry", rev = "814340ecf52e638ca84f300e5753e623fd040bd3" }
tao = { git = "https://github.com/richerfu/tao", branch = "feat-ohos-webview" }
openharmony-ability = { git = "https://github.com/harmony-contrib/openharmony-ability.git" }
openharmony-ability-derive = { git = "https://github.com/harmony-contrib/openharmony-ability.git" }


tauri = { git = "https://github.com/tauri-apps/tauri", branch = "feat/open-harmony" }
tauri-runtime = { git = "https://github.com/tauri-apps/tauri", branch = "feat/open-harmony" }
tauri-utils = { git = "https://github.com/tauri-apps/tauri", branch = "feat/open-harmony", features = [
  "resources",
] }
tauri-macros = { git = "https://github.com/tauri-apps/tauri", branch = "feat/open-harmony" }
tauri-runtime-wry = { git = "https://github.com/tauri-apps/tauri", branch = "feat/open-harmony" }

不然会过不了编译,大概率是会提示说找不到tauri依赖或者找不到tauri的某个模块

1. cargo tauri ohos init初始化鸿蒙模板

如果你想要修改应用图标,应当去gen\ohos\AppScope\resources\base\media目录下进行配置

{
  "layered-image":
  {
    "background" : "$media:background",
    "foreground" : "$media:foreground"
  }
}

如果你想要修改应用名称,应当去gen\ohos\entry\src\main\resources\base\element\string.json里面修改包含"name": "EntryAbility_label"的对象下的value值即可,默认是"value": "label",把label修改成你想要的名字就行了

{
  "string": [
    {
      "name": "module_desc",
      "value": "module description"
    },
    {
      "name": "EntryAbility_desc",
      "value": "description"
    },
    {
      "name": "EntryAbility_label",
      "value": "TauriOhosTest"
    }
  ]
}

5. cargo tauri ohos build打包成.hap文件

这里需要注意的是,在构建之前需要为 Deveco IDE 内置的hvigor以及javac配置好环境变量,他们默认位于C:\Program Files\Huawei\DevEco Studio\tools\hvigor\binC:\Program Files\Huawei\DevEco Studio\jbr\bin,否则会报错找不到可执行程序

6. 使用AutoInstaller安装测试

参考auto-installer来为你的HarmonyOS设备安装.hap文件即可。

不多赘述,相信来到这里的读者具备基础的鸿蒙应用调试经验,如果没有也可以自己去auto-installer仓库学习。

结语

tauri的鸿蒙开发体验还不错,在这之后我简单写了tauri-ohos-test来测试鸿蒙对TauriAPI的支持情况。

Image

比较有意思的是windowAPI的测试部分,他会报错提示说这个API不支持移动设备——在我的鸿蒙电脑上,tauri对ohos的支持还是按照mobile设备来进行的,没有区分鸿蒙PC和鸿蒙手机,这一点后续还有待改进。

此外就是tauri的插件生态在HarmonyO这里也还有待完发展,目前三方插件基本上对HarmonyOS是零支持,还有很大的发展空间。

Changelog

  • 2026-04-15:由于richerfu的PR以及我的PR均已经被合并到tauri-app/tauri的feat/open-harmony分支了,所以更新了相关依赖的版本号以及安装命令,改为直接从tauri-app/tauri安装即可,不需要再从我的fork安装了。
CC BY-NC-SA 4.0

非商业转载请注明出处,商业转载请联系作者获得授权。

For non-commercial use, please indicate the source. For commercial use, please contact the author for authorization.

View license

评论