#盲盒+码##跟着小白一起学鸿蒙#如何编译OpenHarmony自带APP

时间:2022-12-07 17:58:27

如何编译OpenHarmony自带APP

作者:王石

概述

OpenHarmony的主干代码是开源社区的重要学习资源,对于想进行应用开发和熟悉OpenHarmony能力的同学主干代码是非常重要的资源,在主干代码的applications目录里聚集了很多原生的应用实现,那么如何编译这些代码就是我们这篇文章的主要议题。

下载代码

参考《[#跟着小白一起鸿蒙# ]运行OpenHarmony》文章下载OpenHarmony代码

使用DevEco导入编译

  1. 导入代码

#盲盒+码##跟着小白一起学鸿蒙#如何编译OpenHarmony自带APP

  1. 自动下载npm包

导入应用后,IDE工具就会自动sync相关依赖npm包,完成之后会出现以下界面 #盲盒+码##跟着小白一起学鸿蒙#如何编译OpenHarmony自带APP

  1. 然后即可以开始build

#盲盒+码##跟着小白一起学鸿蒙#如何编译OpenHarmony自带APP

  1. 一些报错处理

    • sdk依赖缺失

       ETS:ERROR File: D:/gitee/applications_app_samples-master/settings1118/settings/settings/product/phone/src/main/ets/model/wifiImpl/WifiModel.ts:386:20
       Property 'removeDevice' does not exist on type 'typeof wifi'.
      
       ETS:ERROR File: D:/gitee/applications_app_samples-master/settings1118/settings/settings/product/phone/src/main/ets/model/wifiImpl/WifiModel.ts:391:37
       Property 'getDeviceConfigs' does not exist on type 'typeof wifi'.
      
       ETS:ERROR File: D:/gitee/applications_app_samples-master/settings1118/settings/settings/product/phone/src/main/ets/model/wifiImpl/WifiModel.ts:395:24
       Property 'connectToDevice' does not exist on type 'typeof wifi'.
      
       ETS:ERROR File: D:/gitee/applications_app_samples-master/settings1118/settings/settings/product/phone/src/main/ets/model/wifiImpl/WifiModel.ts:439:41
       Property 'getDeviceConfigs' does not exist on type 'typeof wifi'.
      
      

      以上错误即为sdk缺失导致,也就是import wifi from '@ohos.wifi';这个d.ts文件里没有和主干最新的接口和功能同步,这时候偷懒的法子就是在对应的文件头增加// @ts-nocheck,如下。如果是正规方式,则应该找主干代码里的对应文件,拷贝到IDE的sdk目录下。

      // @ts-nocheck
      import LogUtil from '../../../../../../../common/utils/src/main/ets/default/baseUtil/LogUtil';
      import ConfigData from '../../../../../../../common/utils/src/main/ets/default/baseUtil/ConfigData';
      import wifi from '@ohos.wifi';
      import BaseModel from '../../../../../../../common/utils/src/main/ets/default/model/BaseModel';
      

      按以上套路能编译出来,提示信息如下:

      > hvigor Finished :phone:CompileResources... after 602 ms
      > hvigor WARN:  ETS:WARN File: D:\gitee\applications_app_samples-master\settings1118\settings\settings\product\phone\src\main\ets\pages\volumeControl.ets
       It's not a recommended way to export struct with @Entry decorator, which may cause ACE Engine error in component preview mode. 
      
       COMPILE RESULT:SUCCESS { WARN:1} 
      > hvigor Finished :phone:CompileETS... after 33 s
      > hvigor Finished :phone:CompileJS... after 988 μs
      > hvigor Finished :phone:SyscapTransform... after 2 ms 210 μs
      > hvigor Finished :phone:PackageHap... after 1 s 510 ms
      > hvigor WARN: Will skip sign 'hap'. Invalid signingConfig is configured for 'default' product.
      > hvigor Finished :phone:SignHap... after 3 ms 580 μs
      > hvigor Finished :phone:assembleHap... after 73 μs
      > hvigor BUILD SUCCESSFUL in 37 s
      
      Process finished with exit code 0
      
      
    • hap包没有签名

      按以上套路编出来的,会发现没有签名的hap包。如图:

#盲盒+码##跟着小白一起学鸿蒙#如何编译OpenHarmony自带APP

 这时候应该走File -> Project Structure -> Signing Configs -> automatically generate signature,如果有选中需要取消选中然后再选中,然后apply,最后OK。

 但是这时候其实还没有显示出来,其实上面的报错也有显示,即:

 ```bash
 > hvigor WARN: Will skip sign 'hap'. Invalid signingConfig is configured for 'default' product.
 ```

 这是因为命名不匹配,需要修改build-profile.json5文件的signingConfig:

 ```bash
 "signingConfig": "release" 改成 "signingConfig": "default"
 ```

 这样应该就能编译出signed.hap这应用程序包了
  • install的权限问题

    如果在安装的时候发现install的权限报错,则是很有可能是权限配置问题。OpenHarmony的应用分一般应用和系统应用,系统自带的applications大多是系统自带应用,所以配置IDE工具sdk里的UnsgnedReleasedProfileTemplate.json,具体位置在:sdk\toolchains\3.2.7.5(对应下载和使用的sdk版本)\lib。在这个文件里,我们需要修改以下位置:

    {
      "version-name": "2.0.0",
      "version-code": 2,
      "app-distribution-type": "os_integration",
      "uuid": "5027b99e-5f9e-465d-9508-a9e0134ffe18",
      "validity": {
        "not-before": 1594865258,
        "not-after": 1689473258
      },
      "type": "release",
      "bundle-info": {
        "developer-id": "OpenHarmony",
        "distribution-certificate": "-----BEGIN CERTIFICATE-----\nMIICFTCCAZmgAwIBAgIEH4/ajjAMBggqhkjOPQQDAwUAMGMCzAJBgNVBAYTAkNO\nMRQwEgYDVQQKEwtPcGVuSGFybW9ueTEZMBcGA1UECxMQT3Blbkhhcm1vbnkgVGVh\nbTEjMCEGA1UEAxMaT3Blbkhhcm1vbnkgQXBwbGljYXRpb24gQ0EwHhcNMjIxMjAy\nMDM0NjMzWhcNMzIxMTI5MDM0NjMzWjBKMRUwEwYDVQQDDAxpZGVfZGVtb19hcHAx\nDTALBgNVBAsTBFVuaXQxFTATBgNVBAoTDE9yZ2FuaXphdGlvbjELMAkGA1UEBhMC\nQ04wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQbPC8LX1YcpBZ5+cnrF6XuofHq\nH9b1ZUURQdNx5EoYLcRvhVgDbNoG7npMRhXO/WmCP6cH2u3RExbECzDoAaNo1Iw\nUDAdBgNVHQ4EFgQUSvsBUWNW6lvbdYMOuKqtniwqU4MwDgYDVR0PAQH/BAQDAgeA\nMB8GA1UdIwQYMBaAFNuGtyIW1QuhS7fdJXu58QV9oi1HMAwGCCqGSM49BAMDBQAD\naAAwZQIwYeO7qTU9TicVAd8gfYrvRJDspUlPjwU+k2BETJC4ZyaI5s3gpdDofsNs\nwd4PGVB9AjEAtUZpYZOsFMCoDwEQgRfSfIRVi8f7TJPUubRgrt89OZE9ml3e5ez6\nL5RlJ/m5o13Q\n-----END CERTIFICATE-----\n",
        "bundle-name": "com.ohos.settings",
        //需要修改的系统应用权限
        "apl": "system_core",
        "app-feature": "hos_normal_app"
      },
      "acls": {
      	//需要增加的权限,根据hap的权限申请设置
        "allowed-acls": [
          "ohos.permission.CAPTURE_SCREEN",
          "ohos.permission.MANAGE_USER_IDM"
        ]
      },
      "permissions": {
        "restricted-permissions": []
      },
      "issuer": "pki_internal"
    }
    

    这样一般就可以安装了

  • Install的包名问题

    我们OpenHarmony主干拷贝出来的application的包名一定会和板子上运行的原程序同名的,这样安装时也会提示报错,这种时候,我们需要修改"bundleName": "com.ohos.settings", 以及sign文件里的对应的名字。大概是两个文件:

    1. product\phone\build\default\intermediates\merge_profile\default\module.json

      "bundleName": "com.ohos.settings", //改成新名字如newsetting
      
    2. build-profile.json5

      "signingConfigs": [
            {
              "name": "default",
              "material": {
                "certpath": "C:\\Users\\Administrator\\.ohos\\config\\openharmony\\auto_ohos_default_com.ohos.settings.cer", //使用上面新名字替换com.ohos.settings
                "storePassword": "0000001BC6E86D64AD786A77BBF86702F23E6DD32E016E263D961DBCD4710340CD6EAC6B2A4A43A53DDFA0",
                "keyAlias": "debugKey",
                "keyPassword": "0000001B9CB15173695726ED3460368DEEC246A602353461BBD9321C77513130E462B2BEB9F39DB201E92A",
                "profile": "C:\\Users\\Administrator\\.ohos\\config\\openharmony\\auto_ohos_default_com.ohos.settings.p7b",
                "signAlg": "SHA256withECDSA",
                "storeFile": "C:\\Users\\Administrator\\.ohos\\config\\openharmony\\auto_ohos_default_com.ohos.settings.p12"
              }
            }
          ]
      

小结

通过上述方式,我们就能编译并安装运行OpenHarmony自带的应用并学习基础能力的开发了

本文作者:左翼风发

想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com/#bkwz​