openharmony 鸿蒙 subsys-build-gn-hap-compilation-guide

2022-08-09 浏览 (926)

HAP Build Guide

Overview

Basic Concepts

TermDescription
HAPHarmony Ability Package, released as a HAP file, with the file name extension .hap. One HAP file describes all content of an application, including code, resources, third-party libraries, and an application configuration file.
AbilityAn abstraction of a functionality that an application can provide. It is the minimum unit for the system to schedule the application. An application can contain one or more Ability instances.
FAFeature Ability, an ability that provides a UI for user interaction in the ability framework of the FA model. The FA supports only the Page ability template.
PAParticle Ability, an ability that does not have a UI in the ability framework of the FA model. It provides services and support for FAs. For example, a PA can function as a background service to provide computing power or as a data store to provide data access capabilities. The PA supports three types of templates: Service, Data, and Form ability templates.
FA modelOne of the two ability framework models. The FA model applies to application development using API version 8 and earlier versions. The FA model provides FAs and PAs. The FA supports the Page ability template, and the PA supports the Service, Data, and Form ability templates. For details, see FA Model Overview.
Stage modelOne of the two ability framework models. The stage model applies to application development using API version 9 and later versions. The stage model provides abilities and Extension abilities. The latter ones are extended to Service Extension abilities, Form Extension abilities, Data Share Extension abilities, and more.

Function

The HAP provides HAP build functions and supports FA and stage models.

How to Develop

OpenHarmony Templates

ohos_hap

This template declares a HAP target, which generates a HAP that will be packaged into the system image.

VariableDescription
hap_profileConfiguration file of the HAP. It is config.json for the FA model and module.json for the stage model.
raw_assetsRaw assets, which are directly copied to the assets directory of the HAP.
resourcesResource files, which are stored in the assets/entry/resources directory after build.
js_assetsJS resources, which are stored in the assets/js/default directory after built.
ets_assetseTS resources, which are stored in the assets/js/default directory after built.
depsDependencies of the target.
shared_librariesNative libraries on which the target depends.
hap_nameHAP name, which is optional. By default, it is the target name.
final_hap_pathPath of the generated HAP. This variable is optional. It takes precedence over hap_name.
subsystem_nameName of the subsystem that the HAP belongs to. The name must be consistent with that defined in ohos.build. Otherwise, the HAP will fail to be installed in the system image.
part_nameName of the part that the HAP belongs to. The name must be the same as that of subsystem_name.
js2abcWhether to convert JS code of the HAP into ARK bytecode.
ets2abcWhether to convert eTS code of the HAP into ARK bytecode.
certificate_profileCertificate profile used to sign the HAP.
certificate_fileCertificate file. The certificate and profile must be applies from the official OpenHarmony website.
keystore_pathKeystore file, which is used for signature.
keystore_passwordKeystore password, which is used for signature.
key_aliasAlias of a key.
module_install_nameName of the HAP during installation.
module_install_dirInstallation path of the HAP in the system. The default path is system/app.
js_build_modeBuild mode of the HAP, which can be release or debug. This variable is optional. The default value is release.

ohos_app_scope

Declares the AppScope module of the HAP. The app_profile and sources variables of the target will be concatenated to a specific entry for build. This template is used only in the stage model.

VariableDescription
app_profileapp.json file in the AppScope module of the HAP. This variable is used only in the stage model.
sourcesResources in the AppScope module of the HAP. This variable is used only in the stage model.

ohos_js_assets

Provides JS or eTS code, which is stored in the assets/js/default directory after built. In the stage model, this template is stored in the assets/js or assets/ets directory, depending on the programming language.

VariableDescription
hap_profileConfiguration file of the HAP. It is config.json for the FA model and module.json for the stage model.
source_dirJS or eTS code path, which is compatible with the ability directory of the FA model.
ets2abceTS code path. This variable is used only for widget configuration. In other development scenarios, use the configuration in the ohos_hap template.
js2abcJS code path. This variable is used only for widget configuration. In other development scenarios, use the configuration in the ohos_hap template.

ohos_assets

Provides raw assets, which are directly copied to the assets directory of the HAP.

VariableDescription
sourcesPath of the raw assets.

ohos_resources

Resource files, which are stored in the assets/entry/resources directory for the FA model and in the resources directory for the stage model.

VariableDescription
hap_profileConfiguration file of the HAP. It is config.json for the FA model and module.json for the stage model.
sourcesResource file path.
depsDependencies of the target. You must configure dependencies on the ohos_app_scope target for the stage model.

Procedure

  1. Save the developed application example to the applications/standard/ directory.

  2. Configure the GN script applications/standard/example/BUILD.gn.
    The following is an example of the FA model. For details about more BUILD.gn configurations, see GN Script Configuration Example.

    import("//build/ohos.gni") # Import ohos.gni.
    
    ohos_hap("example") {
      hap_profile = "./src/main/config.json" # config.json
      js_assets = ["./src/main/js/default"]
      raw_assets = ["./raw_assets"]
      resources = ["./src/main/resources"]
      shared_libraries = [
        "//third_party/libpng:libpng", # Native library
      ]
      certificate_profile = "../signature/systemui.p7b" # Certificate profile
      hap_name = "SystemUI-NavigationBar" # HAP name
      part_name = "prebuilt_hap"
      subsystem_name = "applications"
    }
    
  3. Modify the applications/standard/hap/ohos.build file.
    The following is an example:

    {
      "subsystem": "applications",
      "parts": {
        "prebuilt_hap": {
          "module_list": [
            ...
            "//applications/standard/example:example" # Add a build target.
          ]
        }
      }
    }
    
  4. Start build.

    # Perform full building.
    ./build.sh --product-name {product_name}
    
    # Build the HAP only.
    ./build.sh --product-name {product_name} --build-target applications/standard/example:example
    

The build target is generated. The following shows the decompressed HAP in the FA model.

  Length      Date    Time    Name
---------  ---------- -----   ----
     1439  2009-01-01 00:00   assets/raw_assets                                 -----> raw_assets
      354  2009-01-01 00:00   assets/entry/resources.index                      ------> resources
        1  2009-01-01 00:00   assets/entry/resources/base/media/attributes.key  ------> resources
        1  2009-01-01 00:00   assets/entry/resources/base/media/constants.key   ------> resources
        1  2009-01-01 00:00   assets/entry/resources/base/media/contents.key    ------> resources
     6790  2009-01-01 00:00   assets/entry/resources/base/media/icon.png        ------> resources
        1  2009-01-01 00:00   assets/entry/resources/base/media/nodes.key       ------> resources
    11170  2009-01-01 00:00   assets/js/default/app.js                          ------> js_assets
       48  2009-01-01 00:00   assets/js/default/i18n/en-US.json                 ------> js_assets
       50  2009-01-01 00:00   assets/js/default/i18n/zh-CN.json                 ------> js_assets
      224  2009-01-01 00:00   assets/js/default/manifest.json                   ------> js_assets
    41481  2009-01-01 00:00   assets/js/default/pages/index/index.js            ------> js_assets
      909  2009-01-01 00:00   config.json                                       ------> hap_profile
   266248  2009-01-01 00:00   libs/libpng.z.so                                  ------> shared_libraries

GN Script Configuration Example

  • Example of multiple abilities in the FA model

    import("//build/ohos.gni")
    
    ohos_hap("dataability") {
      hap_profile = "entry/src/main/config.json"
      deps = [
        ":dataability_js_assets",
        ":dataability_resources",
      ]
      certificate_profile = "signature/openharmony_sx.p7b"
      hap_name = "dataability"
      part_name = "prebuilt_hap"
      subsystem_name = "applications"
    }
    
    ohos_js_assets("dataability_js_assets") {
      ets2abc = true
      source_dir = "entry/src/main/ets"
      hap_profile = "entry/src/main/config.json"
    }
    
    ohos_resources("dataability_resources") {
      sources = [
        "entry/src/main/resources",
      ]
      hap_profile = "entry/src/main/config.json"
    }
    
    
  • Example of a JS widget in the FA model

    import("//build/ohos.gni")
    
    ohos_hap("FormOfFaJs") {
      hap_profile = "entry/src/main/config.json"
      deps = [
        ":FormOfFaJs_js_assets",
        ":FormOfFaJs_resources",
      ]
      certificate_profile = "signature/openharmony_sx.p7b"
      hap_name = "FormOfFaJs"
      part_name = "prebuilt_hap"
      subsystem_name = "applications"
    }
    
    ohos_js_assets("FormOfFaJs_js_assets") {
      hap_profile = "entry/src/main/config.json"
      js2abc = true
      source_dir = "entry/src/main/js"
    }
    
    ohos_resources("FormOfFaJs_resources") {
      sources = [
        "entry/src/main/resources",
      ]
      hap_profile = "entry/src/main/config.json"
    }
    
  • Example of an eTS widget in the FA model

    import("//build/ohos.gni")
    
    ohos_hap("FormOfFaEts") {
      hap_profile = "entry/src/main/config.json"
      deps = [
        ":FormOfFaEts_js_assets",
        ":FormOfFaEts_form_js_assets",
        ":FormOfFaEts_resources",
      ]
      certificate_profile = "signature/openharmony_sx.p7b"
      hap_name = "FormOfFaEts"
      part_name = "prebuilt_hap"
      subsystem_name = "applications"
    }
    
    ohos_js_assets("FormOfFaEts_js_assets") {
      hap_profile = "entry/src/main/config.json"
      ets2abc = true
      source_dir = "entry/src/main/ets"
    }
    
    ohos_js_assets("FormOfFaEts_form_js_assets") {
      hap_profile = "entry/src/main/config.json"
      js2abc = true
      source_dir = "entry/src/main/js"
    }
    
    ohos_resources("FormOfFaEts_resources") {
      sources = [
        "entry/src/main/resources",
      ]
      hap_profile = "entry/src/main/config.json"
    }
    
  • Example of the stage model

    import("//build/ohos.gni")
    
    ohos_hap("actmoduletest") {
      hap_profile = "entry/src/main/module.json"
      deps = [
        ":actmoduletest_js_assets",
        ":actmoduletest_resources",
      ]
      certificate_profile = "signature/openharmony_sx.p7b"
      hap_name = "actmoduletest"
      part_name = "prebuilt_hap"
      subsystem_name = "applications"
    }
    
    ohos_app_scope("actmoduletest_app_profile") {
      app_profile = "AppScope/app.json"
      sources = [ "AppScope/resources" ]
    }
    
    ohos_js_assets("actmoduletest_js_assets") {
      ets2abc = true
      source_dir = "entry/src/main/ets"
    }
    
    ohos_resources("actmoduletest_resources") {
      sources = [
        "entry/src/main/resources",
      ]
      deps = [
        ":actmoduletest_app_profile",
      ]
      hap_profile = "entry/src/main/module.json"
    }
    
  • Example of a widget in the stage model

    import("//build/ohos.gni")
    
    ohos_hap("FormOfStageEts") {
      hap_profile = "entry/src/main/module.json"
      deps = [
        ":FormOfStageEts_js_assets",
        ":FormOfStageEts_form_js_assets",
        ":FormOfStageEts_resources",
      ]
      js_build_mode = "debug" # The default value is release.
      certificate_profile = "signature/openharmony_sx.p7b"
      hap_name = "FormOfStageEts"
      part_name = "prebuilt_hap"
      subsystem_name = "applications"
    }
    
    ohos_js_assets("FormOfStageEts_js_assets") {
      hap_profile = "entry/src/main/module.json"
      ets2abc = true
      source_dir = "entry/src/main/ets"
    }
    
    ohos_js_assets("FormOfStageEts_form_js_assets") {
      hap_profile = "entry/src/main/module.json"
      js2abc = true
      source_dir = "entry/src/main/js"
    }
    
    ohos_app_scope("FormOfStageEts_app_profile") {
      app_profile = "AppScope/app.json"
      sources = [ "AppScope/resources" ]
    }
    
    ohos_resources("FormOfStageEts_resources") {
      sources = [
        "entry/src/main/resources",
      ]
      deps = [
        ":FormOfStageEts_app_profile",
      ]
      hap_profile = "entry/src/main/module.json"
    }
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Subsystems

harmony 鸿蒙AI Framework Development Guide

harmony 鸿蒙Neural Network Runtime Device Access

harmony 鸿蒙Application Privilege Configuration

harmony 鸿蒙Development Example

harmony 鸿蒙Setting Up a Development Environment

harmony 鸿蒙Development Guidelines

harmony 鸿蒙Application Framework Overview

harmony 鸿蒙ArkCompiler Development

harmony 鸿蒙Window Title Bar Customization Development (ArkTS)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/3e5332f583394b588491c9755401aad0