获取运行时编译构建参数

通过BuildProfile类在运行时获取编译构建参数

HarmonyOS
2024-05-28 22:01:06
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
xcbaby

使用的核心API        

参考文档:

获取自定义编译构建参数

核心代码解释

配置build-profile-json5

如下:

{ 
  "apiType": "stageMode", 
"buildOption": { 
  "arkOptions": { 
    // "apPath": "./modules.ap"  /* 用于按配置文件优化 (PGO) 的配置文件,这是一种用于提高应用运行时性能的编译器优化技术. */ 
    "buildProfileFields": { 
      "buildOptionDataString": '模块在构建过程中的相关配置{"name":"zw111","age":116}', 
      "buildOptionDataNumber": 15818416, 
      "buildOptionDataBoolean": false 
    } 
  } 
}, 
  "buildOptionSet": [ 
  { 
    "name": "debug", 
  "arkOptions": { 
    "buildProfileFields": { 
      "buildOptionSetData": '构建配置集的数据', 
    }, 
    "obfuscation": { 
      "ruleOptions": { 
        "enable": true, 
        "files": [ 
        "./obfuscation-rules.txt" 
        ] 
      } 
    } 
  } 
  }, 
  ], 
  "targets": [ 
  { 
    "name": "default" 
  }, 
  { 
    "name": "ohosTest", 
  } 
  ] 
}

2生成buildprofile文件:

            选中需要entry模块,在菜单栏选择“Build > Generate Build Profile 'entry'”。

 3 在代码中获取构建参数

import BuildProfile from 'BuildProfile'; 
@Entry 
@Component 
struct Index { 
  @State bundleName: string = ""; 
  @State bundleType: string =  ""; 
  @State versionCode: number=-1 ; 
  @State versionName: string = ""; 
  @State targetName: string =  ""; 
  @State productName: string =  ""; 
  @State buildMode: string = ""; 
  @State debugAble: boolean=false ; 
  @State dataString: string =""; 
  @State dataNumber: number=-1; 
  @State dataBoolean: Boolean=false; 
  @State buildOptionSetData:string=""; 
  controller: TextInputController = new TextInputController() 
 
  build() { 
    Row() { 
      Column() { 
        Row() { 
          Text('Bundle名称').fontSize(20).margin({ left: 2, top: 10 }) 
          TextInput({ controller: this.controller, text: this.bundleName }) 
            .placeholderColor(Color.Grey) 
            .placeholderFont({ size: 14, weight: 400 }) 
            .caretColor(Color.Blue) 
            .width(400) 
            .height(40) 
            .margin(20) 
            .fontSize(14) 
            .fontColor(Color.Black) 
            .type(InputType.Normal) 
            .margin({ top: 10 }) 
        } 
 
        Row() { 
          Text('Bundle类型').fontSize(20).margin({ left: 2, top: 10 }) 
          TextInput({ controller: this.controller, text: this.bundleType }) 
            .placeholderColor(Color.Grey) 
            .placeholderFont({ size: 14, weight: 400 }) 
            .caretColor(Color.Blue) 
            .width(400) 
            .height(40) 
            .margin(20) 
            .fontSize(14) 
            .fontColor(Color.Black) 
            .type(InputType.Normal) 
            .margin({ top: 10 }) 
        } 
 
        Row() { 
          Text('应用的版本号').fontSize(20).margin({ left: 2, top: 10 }) 
          TextInput({ controller: this.controller, text: this.versionCode.toString() }) 
            .placeholderColor(Color.Grey) 
            .placeholderFont({ size: 14, weight: 400 }) 
            .caretColor(Color.Blue) 
            .width(400) 
            .height(40) 
            .margin(20) 
            .fontSize(14) 
            .fontColor(Color.Black) 
            .type(InputType.Normal) 
            .margin({ top: 10 }) 
        } 
 
        Row() { 
          Text('应用版本名称').fontSize(20).margin({ left: 2, top: 10 }) 
          TextInput({ controller: this.controller, text: this.versionName }) 
            .placeholderColor(Color.Grey) 
            .placeholderFont({ size: 14, weight: 400 }) 
            .caretColor(Color.Blue) 
            .width(400) 
            .height(40) 
            .margin(20) 
            .fontSize(14) 
            .fontColor(Color.Black) 
            .type(InputType.Normal) 
            .margin({ top: 10 }) 
        } 
 
        Row() { 
          Text('Target名称').fontSize(20).margin({ left: 2, top: 10 }) 
          TextInput({ controller: this.controller, text: this.targetName }) 
            .placeholderColor(Color.Grey) 
            .placeholderFont({ size: 14, weight: 400 }) 
            .caretColor(Color.Blue) 
            .width(400) 
            .height(40) 
            .margin(20) 
            .fontSize(14) 
            .fontColor(Color.Black) 
            .type(InputType.Normal) 
            .margin({ top: 10 }) 
        } 
 
        Row() { 
          Text('Product名称').fontSize(20).margin({ left: 2, top: 10 }) 
          TextInput({ controller: this.controller, text: this.productName }) 
            .placeholderColor(Color.Grey) 
            .placeholderFont({ size: 14, weight: 400 }) 
            .caretColor(Color.Blue) 
            .width(400) 
            .height(40) 
            .margin(20) 
            .fontSize(14) 
            .fontColor(Color.Black) 
            .type(InputType.Normal) 
            .margin({ top: 10 }) 
        } 
 
        Row() { 
          Text('编译模式').fontSize(20).margin({ left: 2, top: 10 }) 
          TextInput({ controller: this.controller, text: this.bundleType }) 
            .placeholderColor(Color.Grey) 
            .placeholderFont({ size: 14, weight: 400 }) 
            .caretColor(Color.Blue) 
            .width(400) 
            .height(40) 
            .margin(20) 
            .fontSize(14) 
            .fontColor(Color.Black) 
            .type(InputType.Normal) 
            .margin({ top: 10 }) 
        } 
 
        Row() { 
          Text('可调试').fontSize(20).margin({ left: 2, top: 10 }) 
          TextInput({ controller: this.controller, text: this.debugAble ? "可调试" : "不可调试" }) 
            .placeholderColor(Color.Grey) 
            .placeholderFont({ size: 14, weight: 400 }) 
            .caretColor(Color.Blue) 
            .width(400) 
            .height(40) 
            .margin(20) 
            .fontSize(14) 
            .fontColor(Color.Black) 
            .type(InputType.Normal) 
            .margin({ top: 10 }) 
        } 
 
        Row() { 
          Text('自定义字符串字段').fontSize(20).margin({ left: 2, top: 10 }) 
          TextInput({ controller: this.controller, text: this.dataString }) 
            .placeholderColor(Color.Grey) 
            .placeholderFont({ size: 14, weight: 400 }) 
            .caretColor(Color.Blue) 
            .width(400) 
            .height(40) 
            .margin(20) 
            .fontSize(14) 
            .fontColor(Color.Black) 
            .type(InputType.Normal) 
            .margin({ top: 10 }) 
        } 
 
        Row() { 
          Text('自定义数字字段').fontSize(20).margin({ left: 2, top: 10 }) 
          TextInput({ controller: this.controller, text: this.dataNumber.toString() }) 
            .placeholderColor(Color.Grey) 
            .placeholderFont({ size: 14, weight: 400 }) 
            .caretColor(Color.Blue) 
            .width(400) 
            .height(40) 
            .margin(20) 
            .fontSize(14) 
            .fontColor(Color.Black) 
            .type(InputType.Normal) 
            .margin({ top: 10 }) 
        } 
 
        Row() { 
          Text('自定义布尔字段').fontSize(20).margin({ left: 2, top: 10 }) 
          TextInput({ controller: this.controller, text: this.dataBoolean.toString() }) 
            .placeholderColor(Color.Grey) 
            .placeholderFont({ size: 14, weight: 400 }) 
            .caretColor(Color.Blue) 
            .width(400) 
            .height(40) 
            .margin(20) 
            .fontSize(14) 
            .fontColor(Color.Black) 
            .type(InputType.Normal) 
            .margin({ top: 10 }) 
        } 
 
        Row() { 
          Text('自定义信息').fontSize(20).margin({ left: 2, top: 10 }) 
          TextInput({ controller: this.controller, text: this.buildOptionSetData.toString() }) 
            .placeholderColor(Color.Grey) 
            .placeholderFont({ size: 14, weight: 400 }) 
            .caretColor(Color.Blue) 
            .width(400) 
            .height(40) 
            .margin(20) 
            .fontSize(14) 
            .fontColor(Color.Black) 
            .type(InputType.Normal) 
            .margin({ top: 10 }) 
        } 
 
        Button({ type: ButtonType.Normal, stateEffect: true }) { 
          Text('获取构建参数') 
            .fontColor(Color.White) 
            .fontSize(20) 
        } 
        .borderRadius(8) 
        .width('45%') 
        .height('5%') 
        .backgroundColor(0x317aff) 
        .onClick(() => { 
          this.bundleName = BuildProfile.BUNDLE_NAME; 
          this.bundleType = BuildProfile.BUNDLE_TYPE; 
          this.versionCode = BuildProfile.VERSION_CODE; 
          this.versionName = BuildProfile.VERSION_NAME; 
          this.targetName = BuildProfile.TARGET_NAME; 
          this.productName = BuildProfile.PRODUCT_NAME; 
          this.buildMode = BuildProfile.BUILD_MODE_NAME; 
          this.debugAble = BuildProfile.DEBUG; 
          this.dataString = BuildProfile.buildOptionDataString; 
          this.dataNumber = BuildProfile.buildOptionDataNumber; 
          this.dataBoolean = BuildProfile.buildOptionDataBoolean; 
          this.buildOptionSetData=BuildProfile.buildOptionSetData; 
        }) 
        .margin(10) 
 
        Button({ type: ButtonType.Normal, stateEffect: true }) { 
          Text('清除') 
            .fontColor(Color.White) 
            .fontSize(20) 
        } 
        .borderRadius(8) 
        .width('45%') 
        .height('5%') 
        .backgroundColor(0x317aff) 
        .onClick(() => { 
          this.bundleName = ""; 
          this.bundleType = ""; 
          this.versionCode =-1; 
          this.versionName =""; 
          this.targetName = ""; 
          this.productName = ""; 
          this.buildMode = ""; 
          this.debugAble = false; 
          this.dataString = ""; 
          this.dataNumber = -1; 
          this.dataBoolean = false; 
        }) 
        .margin(10) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 

实现效果

图1-1  

适配的版本信息

      IDE:DevEco Studio 4.1.3.500

      SDK:HarmoneyOS NEXT Developer Preview1

分享
微博
QQ
微信
回复
2024-05-29 23:18:49
相关问题
运行时AOP插桩demo测试
472浏览 • 1回复 待解决
应用运行时进程资源使用规格
717浏览 • 1回复 待解决
如何动态查看代码运行时变量值?
285浏览 • 1回复 待解决
把arkts运行时,当成 JS上下文用
570浏览 • 1回复 待解决
延迟任务执行时机及运行线程
862浏览 • 1回复 待解决
编译构建怎么编写自定义任务?
262浏览 • 1回复 待解决
编译构建报错,提示CONFIGURE_SDK_ERROR
298浏览 • 1回复 待解决
OpenHarmony startAbility 如何获取参数
6239浏览 • 0回复 待解决
泛型参数转换为Object编译失败
290浏览 • 1回复 待解决