如何动态生成编译构建产物HAP、HAR和APP的名称
·
前言
自定义产物名称在鸿蒙应用开发中,当前有两种方式。一种是在build-profile.json5配置文件中修改artifactName属性,来改变最终的产物名称。参考:
{
"products": [
{
"name": "default",
"signingConfig": "default",
"bundleName": "com.example.myapplication",
"output": {
"artifactName": "Test-1.0.0"
}
}
]
}
另外一种就是今天要介绍的动态方式,动态方式是通过修改应用打包编译流程实现更改产物名称。
如何动态生成编译构建产物HAP、HAR和APP的名称
1. 更改HAP包产物名称,编辑HAP所在的模块中的hvigorfile.ts。
import { hvigor } from '@ohos/hvigor';
import { hapTasks, OhosAppContext, OhosHapContext, OhosPluginId } from '@ohos/hvigor-ohos-plugin';
const appContext = hvigor.getRootNode().getContext(OhosPluginId.OHOS_APP_PLUGIN) as OhosAppContext;
const appJsonOpt = appContext.getAppJsonOpt();
const versionName = appJsonOpt['app']['versionName'];
hvigor.afterNodeEvaluate(node => {
const hapContext = node.getContext(OhosPluginId.OHOS_HAP_PLUGIN) as OhosHapContext;
if (hapContext && hapContext.getBuildProfileOpt) {
const buildProfile = hapContext.getBuildProfileOpt();
const targets = buildProfile.targets;
targets.forEach((target) => {
if (target.name === 'default') {
target['output'] = {
"artifactName": "Test-" + appContext.getBuildMode() + '-' + versionName + '_' + getTime(),
};
}
})
hapContext.setBuildProfileOpt(buildProfile);
}
});
function getTime(): string {
let date = new Date();
let year = date.getFullYear();
let month = (date.getMonth() + 1).toString().padStart(2, '0');
let day = date.getDate().toString().padStart(2, '0');
let hours = date.getHours().toString().padStart(2, '0');
let minutes = date.getMinutes().toString().padStart(2, '0');
return `${year}${month}${day}${hours}${minutes}`;
}
export default {
system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins: [] /* Custom plugin to extend the functionality of Hvigor. */
}
2. 更改HAR包名称,编辑HAR所在的模块中的hvigorfile.ts。
import { harTasks,OhosPluginId } from '@ohos/hvigor-ohos-plugin';
import { hvigor } from '@ohos/hvigor'
hvigor.afterNodeEvaluate((hvigorNode)=>{
const context = hvigorNode.getContext(OhosPluginId.OHOS_HAR_PLUGIN)
if (context && context.getBuildProfileOpt) {
const buildProfile = context.getBuildProfileOpt();
const targets = buildProfile.targets
for (const target of targets) {
if (target.name === 'default') {
target.output={
"artifactName": 'Test'
}
}
}
context.setBuildProfileOpt(buildProfile);
}
})
export default {
system: harTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[] /* Custom plugin to extend the functionality of Hvigor. */
}
3. 更改APP包名称,编辑工程级的hvigorfile.ts。
// 动态修改App包名加版本号,工程级hvigorfile.ts
import { appTasks , OhosPluginId} from '@ohos/hvigor-ohos-plugin';
import { hvigor } from '@ohos/hvigor'
hvigor.afterNodeEvaluate((hvigorNode)=>{
const context = hvigorNode.getContext(OhosPluginId.OHOS_APP_PLUGIN)
if (context && context.getBuildProfileOpt) {
const buildProfile = context.getBuildProfileOpt();
const products = buildProfile.app.products;
for (const product of products) {
if (product.name === context.getCurrentProduct().productBuildOpt.name) {
product.output = {
"artifactName": "Test"
}
}
}
context.setBuildProfileOpt(buildProfile);
}
})
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[] /* Custom plugin to extend the functionality of Hvigor. */
}
更多推荐

所有评论(0)