HarmonyOS鸿蒙Next中添加了自定义编译字段,部分代码编译报错

HarmonyOS鸿蒙Next中添加了自定义编译字段,部分代码编译报错

buildProfile

```javascript
/**
 * Use these variables when you tailor your ArkTS code. They must be of the const type.
 */
export const BUNDLE_NAME = 'com.gwell.yoosee';
export const BUNDLE_TYPE = 'app';
export const VERSION_CODE = 6230;
export const VERSION_NAME = '00.46.06.23';
export const TARGET_NAME = 'default';
export const PRODUCT_NAME = 'default';
export const BUILD_MODE_NAME = 'debug';
export const DEBUG = true;
export const ModuleName = 'account_module';

/**
 * BuildProfile Class is used only for compatibility purposes.
 */
export default class BuildProfile { 
    static readonly BUNDLE_NAME = BUNDLE_NAME;
    static readonly BUNDLE_TYPE = BUNDLE_TYPE;
    static readonly VERSION_CODE = VERSION_CODE;
    static readonly VERSION_NAME = VERSION_NAME;
    static readonly TARGET_NAME = TARGET_NAME;
    static readonly PRODUCT_NAME = PRODUCT_NAME;
    static readonly BUILD_MODE_NAME = BUILD_MODE_NAME;
    static readonly DEBUG = DEBUG;
    static readonly ModuleName = ModuleName;
}

build-profile.json

{
  "apiType": "stageMode",
  "buildOption": {
    "arkOptions": {
      "buildProfileFields": {
        'ModuleName': "account_module"
      }
    }
  },
  "buildOptionSet": [
    {
      "name": "release",
      "arkOptions": {
        "obfuscation": {
          "ruleOptions": {
            "enable": true,
            "files": [
              "./obfuscation-rules.txt"
            ]
          }
        }
      },
    },
  ],
  "targets": [
    {
      "name": "default"
    }
  ]
}
import BuildProfile from 'BuildProfile'
export class KVKeyConstants { 
  /**
   * 存储的地区
   */
  static readonly KEY_DISTRICT_CODE = BuildProfile.ModuleName + "_key_district_code"
}
1 ERROR: ArkTS:ERROR File: D:/hmProject/yoosee/business_modules/account_module/src/main/ets/utils/KVKeyConstants.ets:12:54
 Property 'ModuleName' does not exist on type 'typeof BuildProfile'.

引用buildprofile并不是所有地方都编译报错,部分代码会出现这个问题


更多关于HarmonyOS鸿蒙Next中添加了自定义编译字段,部分代码编译报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

可以通过以下场景进行修复:

问题场景一:

编译态没问题,使用了自定义参数BuildProfile,编译态无异常但编译构建失败,提示“Property xxx does not exist on type ‘typeof BuildProfile’.”。

检查在当前模块下build-profile.json5中的targets > buildProfileFields配置的自定义参数中key值是否相同,如果不同请将targets内所有buildProfileFields中的key值保持相同。

"targets": [
  {
    "name": "default",
    "config": {
      "buildOption": {
        "arkOptions": {
          "buildProfileFields": {
            "targetName": "default"
          }
        }
      }
    }
  },
  {
    "name": "default1",
    "config": {
      "buildOption": {
        "arkOptions": {
          "buildProfileFields": {
            "targetName": "default1"
          }
        }
      }
    }
  },
]

问题场景二:

本地HSP模块对外提供的接口中使用了HAP未定义的自定义参数BuildProfileFileds,且HAP引用了HSP中的该接口,导致编译失败,提示“Property ‘XX’ does not exist on type ‘typeof BuildProfile’”。

可采用以下两种方式解决该问题:

  1. 在HAP中配置与HSP相同的自定义参数BuildProfileFileds。
  2. 将与HSP相同的自定义参数BuildProfileFileds配置到工程级build-profile.json5中,该方法会使HSP中的自定义参数在全局生效。

问题场景三:

编译态标红,使用了自定义参数BuildProfile并且编译器标红且构建失败,提示“Property xxx does not exist on type ‘typeof BuildProfile’.”。

请检查当前模块下build-profile.json5中buildProfileFields内是否添加了所使用的自定义参数,请确保该自定义参数已配置在buildProfileFields内。

更多关于HarmonyOS鸿蒙Next中添加了自定义编译字段,部分代码编译报错的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,自定义编译字段的添加可能导致部分代码编译报错。这通常是由于自定义字段与现有代码的逻辑或语法冲突所致。以下是可能导致编译报错的几个原因:

  1. 字段命名冲突:自定义编译字段可能与系统预定义的字段或第三方库中的字段同名,导致编译器无法正确解析。
  2. 字段类型不匹配:自定义字段的类型可能与代码中使用的类型不一致,导致类型转换或赋值时出错。
  3. 语法错误:自定义字段的声明或使用可能不符合鸿蒙系统的语法规范,例如缺少必要的符号或使用了不支持的语法结构。
  4. 作用域问题:自定义字段的作用域可能超出了预期范围,导致在某些代码块中无法访问或访问错误。

解决这类问题通常需要检查自定义字段的声明和使用,确保其与系统和其他代码部分兼容,并符合鸿蒙系统的开发规范。

在HarmonyOS鸿蒙Next中添加自定义编译字段可能导致部分代码编译报错,通常是因为字段定义与现有代码冲突或不兼容。建议检查以下方面:

  1. 字段冲突:确保自定义字段与系统或第三方库的字段不重名。
  2. 语法错误:验证字段定义是否符合HarmonyOS的语法规范。
  3. 依赖问题:确认相关依赖库是否支持新字段。
  4. 编译配置:检查build.gradleohos.build文件,确保字段正确引入。

如问题仍未解决,建议查阅官方文档或社区论坛获取更多支持。

回到顶部