关于harmonyos:HarmonyOS配置编译构建信息

7次阅读

共计 7699 个字符,预计需要花费 20 分钟才能阅读完成。

在进行利用 / 服务的编译构建前,须要对工程和编译构建的 Module 进行设置。API Version 9、API Version 8 与 API Version 4~7 的构建体系不同,因而在设置编译构建信息时也存在差别:

  • API Version 9:须要对构建配置文件、构建脚本、利用依赖的共享包等信息进行设置。
  • build-profile.json5:利用 / 服务构建配置文件。
  • hvigorfile.ts:自定义编译构建脚本。
  • oh-package.json5:利用的三方包依赖,反对共享包的依赖。
  • API Version 8:须要对构建配置文件、构建脚本、利用依赖的 npm 包等信息进行设置。
  • build-profile.json5:HarmonyOS 利用 / 服务构建配置文件。
  • hvigorfile.ts:自定义编译构建脚本。
  • package.json:利用的三方包依赖,反对 HAR(遵循 npm 标准规范)和 npm 包的依赖。
  • API Version 4~7:须要通过 build.gradle 来对工程编译构建参数进行设置。

build-profile.json5(API Version 8~9)

工程级 build-profile.json5

工程级 build-profile.json5 的示例如下所示:

{
  "app": { 
    // 工程的签名信息,可蕴含多个签名信息
    "signingConfigs": [  
      {
        "name": "default",  // 标识签名计划的名称
        "type": "HarmonyOS",  // 标识 HarmonyOS 利用
        // 该计划的签名资料
        "material": {  
          "certpath": "D:\\SigningConfig\\debug_hos.cer",  // 调试或公布证书文件,格局为.cer
          "storePassword": "******",  // 密钥库明码,以密文模式出现
          "keyAlias": "debugKey",  // 密钥别名信息
          "keyPassword": "******",  // 密钥明码,以密文模式出现
          "profile": "D:\\SigningConfig\\debug_hos.p7b",  // 调试或公布证书 Profile 文件,格局为.p7b
          "signAlg": "SHA256withECDSA",  // 密钥库 signAlg 参数
          "storeFile": "D:\\SigningConfig\\debug_hos.p12"  // 密钥库文件,格局为.p12
        }
      }
    ],
    "compileSdkVersion": 9,  // 指定 HarmonyOS 利用 / 服务编译时的 SDK 版本
    "compatibleSdkVersion": 9,  // 指定 HarmonyOS 利用 / 服务兼容的最低 SDK 版本
    // 定义构建的产品品类,如通用默认版、付费版、免费版等
    "products": [  
      {
        "name": "default",  // 定义产品的名称,反对定制多 product 指标产物,具体请参考定制多指标构建产物
        "signingConfig": "default",  // 指定以后产品品类对应的签名信息,签名信息须要在 signingConfigs 中进行定义
      }
    ]
  },
  "modules": [
    {
      "name": "entry",  // 模块名称
      "srcPath": "./entry",  // 表明模块根目录绝对工程根目录的相对路径
      "targets": [  // 定义构建的 APP 产物,由 product 和各模块定义的 targets 独特定义
        {
          "name": "default",  //target 名称,由各个模块的 build-profile.json5 中的 targets 字段定义
          "applyToProducts": ["default"   // 示意将该模块下的“default”Target 打包到“default”Product 中]
        }
      ]
    }
  ]
}

模块级 build-profile.json5

模块级 build-profile.json5 的示例如下所示:

{
  "apiType": 'faMode',  //API 类型,反对 FA 和 Stage 模型
  "showInServiceCenter": true,  // 是否在服务中心展现
  "buildOption": {
    // 配置筛选 har 依赖.so 资源文件的过滤规定
    "napiLibFilterOption": {
      // 依照.so 文件的优先级程序,打包最高优先级的.so 文件
      "pickFirsts": ["**/1.so"],
      // 依照.so 文件的优先级程序,打包最低优先级的.so 文件
      "pickLasts": ["**/2.so"],
      // 排除的.so 文件
      "excludes": ["**/3.so"],
      // 容许当.so 重名抵触时,应用高优先级的.so 文件笼罩低优先级的.so 文件
      "enableOverride": true
    },
    //cpp 相干编译配置
    "externalNativeOptions": {
      "path": "./src/main/cpp/CMakeLists.txt",  //CMake 配置文件,提供 CMake 构建脚本
      "arguments": "",  // 传递给 CMake 的可选编译参数"abiFilters": [// 用于设置本机的 ABI 编译环境"armeabi-v7a","arm64-v8a"],"cppFlags":""  // 设置 C ++ 编译器的可选参数
    },
  },
  "targets": [  // 定义的 Target,开发者能够定制不同的 Target,具体请参考定制多指标构建产物
    {
      "name": "default",
      "runtimeOS": "HarmonyOS",
    },
    {"name": "ohosTest",}    
  ]
}

oh-package.json5(API Version 9)

利用 / 服务反对通过 ohpm 来装置、共享、散发代码,治理我的项目的依赖关系。oh-package.json5 格局遵循规范的 ohpm 标准。

工程级 oh-package.json5

工程级 oh-package.json5 的示例如下所示:

{
  "name": "myapplication",
  "version": "1.0.0",
  "description": "Please describe the basic information.",
  "main": "","author":"",
  "license": "","dependencies": {},"devDependencies": {"@ohos/hypium":"1.0.6"}
}

模块级 oh-package.json5

模块级 oh-package.json5 的示例如下所示:

{
  "name": "entry",
  "version": "1.0.0",
  "description": "Please describe the basic information.",
  "main": "index.ets",
  "author": "","license":"",
  "dependencies": {},}

package.json(API Version 8)

利用 / 服务反对通过 npm 来装置、共享、散发代码,治理我的项目的依赖关系。HarmonyOS npm 标准是在规范 npm 标准的根底上,减少了对 HarmonyOS 平台的拓展。因而,package.json 格局遵循规范的 npm 标准(具体可查阅 npm 官网文档),接下来次要介绍 HarmonyOS npm 局部的标准。

工程级 package.json

工程级 package.json 的示例如下所示:

{
  "name": "myapplication",
  "version": "1.0.0",
  "ohos": {
    "org": "huawei",
    "buildTool": "hvigor",
    "directoryLevel": "project"
  },
  "description": "example description",
  "repository": {},
  "license": "ISC",
  "dependencies": {
    "@ohos/hypium": "1.0.5",
    "@ohos/hvigor": "1.4.0",
    "@ohos/hvigor-ohos-plugin": "1.4.0"
  }
}

HarmonyOS npm 相干字段阐明如下,其余字段遵循 package.json 标准规范。

  • ohos 闭包:HarmonyOS 利用 / 服务的扩大字段,示意在 npm 标准规范的根底上叠加了 HarmonyOS npm 的内容。
  • org:标识 HarmonyOS npm 包的保护主体。
  • buildTool:标识 HarmonyOS npm 包的构建工具是 hvigor。
  • directoryLevel:标识 HarmonyOS npm 包是工程的依赖。
  • dependencies 闭包:设置工程依赖的 npm 包及版本,在遵循 npm 原生的根底上,能够增加 @ohos 相干的依赖,如构建插件,HarmonyOS
    npm 三方共享包等。

模块级 package.json

模块级 package.json 的示例如下所示:

{
    "license": "ISC",
    "devDependencies": {"@types/libentry.so": "file:./src/main/cpp/types/libentry"},
    "name": "entry",
    "ohos": {
        "org": "huawei",
        "directoryLevel": "module",
        "buildTool": "hvigor"
    },
    "description": "example description",
    "repository": {},
    "version": "1.0.0",
    "dependencies": {}}

对于 HarmonyOS npm 包的相干字段阐明如下,其余字段遵循 package.json 标准规范。

  • ohos 闭包:HarmonyOS 利用 / 服务的扩大字段,示意在 npm 标准规范的根底上叠加了 HarmonyOS npm 的内容。
  • org:标识 HarmonyOS npm 包的保护主体。
  • directoryLevel:标识 HarmonyOS npm 包是模块级的依赖。
  • buildTool:标识 HarmonyOS npm 包的构建工具是 hvigor。
  • dependencies 闭包:设置模块依赖的 npm 包及版本,在遵循 npm 原生的根底上,能够增加 @ohos 相干的依赖,如 HarmonyOS
    npm 三方共享包等。

build.gradle(API Version 4 至 7)

工程级 build.gradle

工程级 build.gradle 的示例如下所示:

apply plugin: 'com.huawei.ohos.app'
ohos {
    compileSdkVersion 7
    signingConfigs {
        debug {storeFile file('D:\\Sdk\\storeFile')    // 本地 keystore
            storePassword 'storePassword'           // 生成本地 keystore 时的 storePassword 参数
            keyAlias 'keyAlias'                     // 生成本地 keystore 时的 alias 参数
            keyPassword 'keyPassword'               //keyPassword 参数,pkcs12 格局的 keystore 采纳生成本地 keystore 时的 keyPassword 参数
            signAlg 'SHA256withECDSA'               // 生成本地 keystore 时的 signAlg 参数
            profile file('D:\\Sdk\\profile')        // 导入调试 / 公布 profile
            certpath file('D:\\Sdk\\certpath')      // 导入调试 / 公布证书
        }
    }
}
    
buildscript {
    repositories {
        maven {url 'https://repo.huaweicloud.com/repository/maven/'}
        maven {url 'https://developer.huawei.com/repo/'}
    }
    dependencies {
        classpath 'com.huawei.ohos:hap:3.1.2.0'
        classpath 'com.huawei.ohos:decctest:1.2.7.19'
    }
}

allprojects {
    repositories {
        maven {
            url 'https://repo.huaweicloud.com/repository/maven/'            
            //allowInsecureProtocol = true    // 若 maven 仓是 http 协定地址,则须要增加该字段,示意容许应用非平安协定的 http 仓
        }
        maven {url 'https://developer.huawei.com/repo/'}
    }
}

对于工程级 build.gradle 各字段阐明如下:

  • apply plugin:在工程级 Gradle 中引入打包 app 的插件,不须要批改。
  • ohos 闭包:工程配置,包含如下配置项:
  • compileSdkVersion:利用 / 服务编译构建的指标 SDK 版本。
  • signingConfigs:利用 / 服务的签名信息,包含调试签名信息或公布签名信息。
  • buildscript 闭包:工程编译构建所须要的依赖,包含 Maven 仓地址和依赖的插件版本,例如 HarmonyOS 编译构建插件和 DECC 测试插件。
  • allprojects 闭包:工程本身所须要的依赖,比方援用第三方库的 Maven 仓库和依赖包。

模块级 build.gradle

模块级 build.gradle 的示例如下所示:

apply plugin: 'com.huawei.ohos.hap'    // 打包 hap 插件:将 entry/feature 模块打包为 hap 的插件
//apply plugin: 'com.huawei.ohos.library'    // 打包 har 插件:将 HarmonyOS Library 模块打包为 har 的插件
//apply plugin: 'java-library'    // 打包 jar 插件:将 Java Library 模块打包为 jar 的插件
apply plugin: 'com.huawei.ohos.decctest'    //decctest 测试框架插件
ohos {
    compileSdkVersion 7
    defaultConfig {compatibleSdkVersion 4}
    showInServiceCenter true
    buildTypes {
        release {
            proguardOpt {               
               proguardEnabled true    // 开启代码混同性能               
               rulesFiles 'proguard-rules.pro'    // 配置混同规定文件相对路径               
               consumerRulesFiles 'consumer-rules.pro'    // 配置打包混同规定文件相对路径,仅在 HarmonyOS Library 模块中配置
            }
        }
    }
    packageOptions {exclude "**/*.proto"}    
    //entryModules "entry"    // 该 Feature 模块关联的 Entry 模块
    // C++ 工程编译构建代码设置
    externalNativeBuild {
        path "src/main/cpp/CMakeLists.txt"   //CMake 配置入口,提供 CMake 构建脚本的相对路径
        arguments ""   // 传递给 CMake 的可选编译参数
        abiFilters "arm64-v8a"  // 用于设置本机的 ABI 编译环境
        cppFlags ""   // 设置 C ++ 编译器的可选参数
    }
    mergeJsSrc true
    compileOptions {annotationEnabled true}
}
dependencies {//entryImplementation project(':entry')    // 依赖的 entry 模块名称
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
    testImplementation 'junit:junit:4.13.1'
    ohosTestImplementation 'com.huawei.ohos.testkit:runner:2.0.0.200'
}
decc {supportType = ['html', 'xml']
}

对于模块级 build.gradle 各字段阐明如下:

  • apply plugin:在模块级 build.gradle 中引入打包 hap、har、Jar 以及 decctest 的插件。
  • ohos 闭包:模块配置,包含如下配置项:
  • compileSdkVersion:指定 HarmonyOS 利用 / 服务编译时的 SDK 版本。
  • compatibleSdkVersion:指定 HarmonyOS 利用 / 服务兼容的最低 SDK 版本。
  • showInServiceCenter:是否在服务中心展现,在创立工程时抉择了“Show in service
    center”选项会生成该字段。
  • buildTypes:配置 build 类型以及每种类型构建打包的签名、压缩、混同的配置。
  • externalNativeBuild:C/C++ 编译构建代码设置项。
  • entryModules:该 Feature 模块关联的 Entry 模块,仅 Feature 模块存在该字段。
  • packageOptions:将 entry/feature 模块打包为 hap 时,排除不须要打包到 HAP 中的文件。
  • mergeJsSrc:跨设施的利用 / 服务编译构建,是否须要合并 JS 代码。Wearable 和 Lite
    Wearable 共用一个工程,如下图所示:当进行编译构建时,将 wearable/liteWearable 目录下的 JS 文件与 pages 目录(Wearable 和 Lite
    Wearable 共用的源码)下的 JS 文件进行合并打包。
  • annotationEnabled:反对数据库正文。
  • dependencies 闭包:该模块所需的依赖项。

作为一名合格一线开发程序员,大家心里必定会有很多疑难!鸿蒙零碎这么弱小~~

为了可能让大家跟上互联网时代的技术迭代,在这里跟大家分享一下我本人近期学习心得以及参考网上材料整顿出的一份最新版的鸿蒙学习晋升材料 ,有须要的小伙伴自行支付, 限时开源,先到先得~~~~

支付以下高清学习路线原图请点击→《鸿蒙 (Harmony OS)开发学习手册》纯血鸿蒙 HarmonyOS 根底技能学习路线图

支付以上残缺高清学习路线图,请点击→《鸿蒙根底入门学习指南》小编本人整顿的局部学习材料(蕴含有高清视频、开发文档、电子书籍等)

以上分享的学习路线都适宜哪些人跟着学习?

- 应届生 / 计算机专业
通过学习鸿蒙新兴技术,入行互联网,将来高起点待业。
- 0 根底转行
提前布局新方向,抓住风口,自我晋升,取得更多就业机会。
- 技术晋升 / 进阶跳槽
倒退瓶颈期,晋升职场竞争力,疾速把握鸿蒙技术,享受蓝海红利。

总结

如果你感觉这篇内容对你有帮忙,我想麻烦大家动动小手给我:点赞,转发,有你们的『点赞和评论』,才是我发明的能源。

关注我,同时能够期待后续文章 ing,不定期分享原创常识。

想要获取更多残缺鸿蒙最新 VIP 学习材料,请点击→《鸿蒙全套学习指南》

正文完
 0