怎么在HarmonyOS鸿蒙Next构建脚本 hvigorfile.ts 里获取 SDK 路径?

怎么在HarmonyOS鸿蒙Next构建脚本 hvigorfile.ts 里获取 SDK 路径? 我希望在 hvigorfile.ts 里获取 hvigor 或者 hvigor-ohos-plugin 的路径以便进一步获取 ohos.toolchain.cmake toolchain 的路径,以便支持 vscode 跨平台 cmake 代码索引与编译,目前 hvigor-ohos-plugin 是否有 API 实现?

2 回复

在HarmonyOS鸿蒙Next项目中,可以通过AbilityContext获取SDK路径。在hvigorfile.ts中使用以下代码:

import abilityAccessCtrl from '@ohos.abilityAccessCtrl';

const context = abilityAccessCtrl.createAtContext();
const sdkPath = context.getApplicationContext().getBundleCodePath();

该方法返回当前应用的安装路径,其中包含SDK相关资源。路径格式为字符串,可直接在构建脚本中使用。注意需要在module.json5中声明权限。

更多关于怎么在HarmonyOS鸿蒙Next构建脚本 hvigorfile.ts 里获取 SDK 路径?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next的hvigorfile.ts中,可以通过process.env.HARMONY_HOME获取SDK安装路径。对于工具链路径,建议使用以下方式获取:

const sdkPath = process.env.HARMONY_HOME;
const toolchainPath = `${sdkPath}/native/llvm/bin/ohos.toolchain.cmake`;

hvigor-ohos-plugin目前没有直接获取工具链路径的专用API,但可以通过环境变量组合出完整路径。这种方式在Windows/Linux/macOS下都适用,可以满足跨平台CMake配置需求。

回到顶部