HarmonyOS鸿蒙Next中怎么才能做到切换product和mode的时候,把指定路径下的文件拷贝到rawfile目录下?
HarmonyOS鸿蒙Next中怎么才能做到切换product和mode的时候,把指定路径下的文件拷贝到rawfile目录下?
描述: 切换product和build mode的时候,sync或者build的时候,自动吧指定目录的文件放到entry下的rawfile文件里
现象: 
版本: 5.1.1
更多关于HarmonyOS鸿蒙Next中怎么才能做到切换product和mode的时候,把指定路径下的文件拷贝到rawfile目录下?的实战教程也可以访问 https://www.itying.com/category-93-b0.html
开发者您好,可以通过配置hvigorfile.ets脚本实现,具体示例参考如下:
【问题现象】
项目根目录mpass/fat/中分别存在debug和release两个文件夹以及对应的图片,当产物名称是fat时,根据build mode自动将指定目录的文件放到HAP下的rawfile文件里。
【背景知识】
配置多目标产物:通常情况下,应用厂商会根据不同的部署环境,不同的目标人群,不同的运行环境等,将同一个应用定制为不同的版本,如国内版、国际版、普通版、VIP版、免费版、付费版等。针对以上场景,DevEco Studio支持通过少量的代码配置以实例化不同的差异版本,在编译构建过程中实现一个应用构建出不同的目标产物版本,从而实现源代码、资源文件等的高效复用。
扩展构建能力:提供任务和插件的形式,通过编码改变编译构建过程中的配置文件。
【解决方案】
1、获取当前产物名称,若是fat则继续。
2、获取当前编译模式以及项目文件路径。
3、遍历模块,获取HAP模块类型的上下文,然后获取模块路径。
4、拼接文件路径。
5、将文件复制到指定路径。
import { appTasks, hapTasks, OhosHapContext, OhosAppContext, OhosPluginId } from '@ohos/hvigor-ohos-plugin';
import { getNode, hvigor, HvigorNode, FileUtil } from '@ohos/hvigor';
// 仅当项目是fat时触发
const productName = "fat";
// debug编译模式文件路径
const debugFilePath = "\\mpass\\fat\\debug\\debug.png";
// release编译模式文件路径
const releaseFilePath = "\\mpass\\fat\\release\\release.png";
// 复制rawfile相对模块路径
const rawfilePath = "\\src\\main\\resources\\rawfile\\target.png";
// debug编译模式
const buildModeDebug = "debug";
// release编译模式
const buildModeRelease = "release";
hvigor.nodesEvaluated(() => {
// 获取根路径
const appRootNode = hvigor.getRootNode();
// 获取APP环境变量
const appContext = appRootNode.getContext(OhosPluginId.OHOS_APP_PLUGIN) as OhosAppContext;
// 获取当前编译产物的名称
const currentProductName = appContext.getCurrentProduct().getProductName();
console.info(`[hvigorfile] currentProduct name:${currentProductName}`);
if (currentProductName === productName) {
// 获取项目根目录
const currentProductPath = appContext.getProjectPath();
console.info(`[hvigorfile] currentProduct path:${currentProductPath}`);
// 获取编译模式
const currentBuildMode = appContext.getBuildMode();
console.info(`[hvigorfile] currentProduct build mode:${currentBuildMode}`);
appRootNode.subNodes((subNode: HvigorNode) => {
// 获取HAP环境变量
const hapContext = subNode.getContext(OhosPluginId.OHOS_HAP_PLUGIN) as OhosHapContext;
if (hapContext) {
// 获取HAP路径
const hapPath = hapContext.getModulePath()
console.info(`[hvigorfile] currentModule path:${hapPath}`);
// 根据不同的编译模式,取不同路径
let filePath = currentProductPath;
if (currentBuildMode === buildModeDebug) {
filePath = filePath + debugFilePath;
} else if (currentBuildMode === buildModeRelease) {
filePath = filePath + releaseFilePath;
}
// 目标路径
const copyFilePath = hapPath + rawfilePath;
console.info(`[hvigorfile] filePath: ${filePath}, copyFilePath: ${copyFilePath}`);
if (filePath && copyFilePath) {
// 同步复制文件
FileUtil.copyFileSync(filePath, copyFilePath);
}
}
});
}
})
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins: [] /* Custom plugin to extend the functionality of Hvigor. */
}
更多关于HarmonyOS鸿蒙Next中怎么才能做到切换product和mode的时候,把指定路径下的文件拷贝到rawfile目录下?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS Next中,可通过模块级build-profile.json5文件配置rawFiles规则实现。在buildOption字段下添加rawFiles条目,使用srcDir指定源路径,配置when条件匹配product和mode。示例:
"buildOption": {
"externalNativeOptions": {
"path": "./src/main/cpp/CMakeLists.txt"
},
"rawFiles": [
{
"srcDir": "src/main/resources/${productName}/${buildMode}",
"when": ["productName", "buildMode"]
}
]
}
构建系统会根据当前product和mode自动拷贝对应路径文件到rawfile目录。
在HarmonyOS Next中,可以通过配置模块级build-profile.json5文件实现切换product和mode时自动拷贝文件到rawfile目录。具体步骤如下:
- 在模块级
build-profile.json5中配置buildOption:
{
"apiType": 'stageMode',
"buildOption": {
"externalNativeOptions": {
"path": "./src/main/cpp/CMakeLists.txt"
},
"copyToRawFile": [
{
"sourcePath": "./src/main/resources/${productName}/${buildMode}",
"targetPath": "resources/rawfile"
}
]
}
}
- 创建对应的目录结构:
src/main/resources/
├── default/
│ ├── debug/
│ └── release/
└── productA/
├── debug/
└── release/
- 将需要拷贝的文件按product和mode分类存放至对应目录,构建时会自动同步到
entry/src/main/resources/rawfile目录。
注意:${productName}和${buildMode}为预定义变量,分别对应当前构建的产品名称和构建模式。此配置在每次构建时都会执行文件同步,确保rawfile内容与当前构建配置一致。

