subsys-build-gn-hap-compilation-guide.md 13.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
# HAP Build Guide

## Overview

### Basic Concepts
| Term| Description|
| -------------- | ---------------------- |
| HAP            | OpenHarmony 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.|
| Ability        | An 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.|
| FA             | Feature 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.|
11
| PA             | Particle 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.|
12
| FA model        | One 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](../../application-dev/ability-deprecated/fa-brief.md).|
13
| Stage model| One 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.|
14 15 16 17 18 19

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

## How to Develop

20
### Templates
21 22 23 24
#### ohos_hap

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

25
| Variable| Description|
26
| --------- | ---- |
27
| hap_profile | Configuration file of the HAP. It is **config.json** for the FA model and **module.json** for the stage model.|
28
| raw_assets | Raw assets, which are directly copied to the **assets** directory of the HAP.|
29 30 31
| resources | Resource files, which are stored in the **assets/entry/resources** directory after build.|
| js_assets | JS resources, which are stored in the **assets/js/default** directory after built with Ace.|
| ets_assets | eTS resources, which are stored in the **assets/js/default** directory after built with Ace.|
32 33 34
| deps | Dependencies of the target.|
| shared_libraries | Native libraries on which the target depends.|
| hap_name | HAP name, which is optional. By default, it is the target name.|
35
| final_hap_path | Path of the generated HAP. This variable is optional. It takes precedence over **hap_name**.|
36 37 38 39
| subsystem_name | Name 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_name | Name of the part that the HAP belongs to. The name must be the same as that of **subsystem_name**.|
| js2abc | Whether to convert JS code of the HAP into ARK bytecode.|
| ets2abc | Whether to convert eTS code of the HAP into ARK bytecode.|
40 41
| certificate_profile | Certificate profile used to sign the HAP.|
| certificate_file | Certificate file. The certificate and profile must be applies from the official OpenHarmony website.|
42 43 44 45 46
| keystore_path | Keystore file, which is used for signature.|
| keystore_password | Keystore password, which is used for signature.|
| key_alias | Alias of a key.|
| module_install_name | Name of the HAP during installation.|
| module_install_dir | Installation path of the HAP in the system. The default path is **system/app**.|
47
| js_build_mode | Build mode of the HAP, which can be **release** or **debug**. This variable is optional. The default value is **release**.|
48 49 50 51

#### 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.

52
| Variable| Description|
53
| --------- | ---- |
54 55
| app_profile | **app.json** file in the AppScope module of the HAP. This variable is used only in the stage model.|
| sources | Resources in the AppScope module of the HAP. This variable is used only in the stage model.|
56 57

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

60
| Variable| Description|
61
| --------- | ---- |
62
| hap_profile | Configuration file of the HAP. It is **config.json** for the FA model and **module.json** for the stage model.|
63
| source_dir | JS or eTS code path, which is compatible with the **ability** directory of the FA model.|
64 65
| ets2abc | eTS code path. This variable is used only for widget configuration. In other development scenarios, use the configuration in the **ohos_hap** template.|
| js2abc | JS code path. This variable is used only for widget configuration. In other development scenarios, use the configuration in the **ohos_hap** template.|
66 67 68 69

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

70
| Variable| Description|
71 72 73 74
|---------- | ---- |
| sources | Path of the raw assets.|

#### ohos_resources
75
Resource files, which are stored in the **assets/entry/resources** directory for the FA model and in the **resources** directory for the stage model.
76

77
| Variable| Description|
78
| --------- | ---- |
79
| hap_profile | Configuration file of the HAP. It is **config.json** for the FA model and **module.json** for the stage model.|
80 81 82 83 84 85 86
| sources | Resource file path.|
| deps | Dependencies 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.

A
Annie_wang 已提交
87
2. Configure the GN script **applications/standard/example/BUILD.gn**. <br>The following is an example of the FA model. For details about more **BUILD.gn** configurations, see [GN Script Configuration Example](#gn-script-configuration-example).
88
   ```
A
Annie_wang 已提交
89
   import("//build/ohos.gni") # Import ohos.gni.
90 91 92 93 94 95 96 97 98

   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
     ]
99
     certificate_profile = "../signature/systemui.p7b" # Certificate profile
A
Annie_wang 已提交
100
     hap_name = "SystemUI-NavigationBar" # HAP name
101 102 103 104 105
     part_name = "prebuilt_hap"
     subsystem_name = "applications"
   }
   ```

A
Annie_wang 已提交
106
3. Modify the **applications/standard/hap/ohos.build** file. <br>The following is an example:
107 108 109 110 111 112 113 114 115 116 117 118 119 120
   ```
   {
     "subsystem": "applications",
     "parts": {
       "prebuilt_hap": {
         "module_list": [
           ...
           "//applications/standard/example:example" # Add a build target.
         ]
       }
     }
   }
   ```

A
Annie_wang 已提交
121
4. Start build.
122 123 124 125 126 127 128 129
   ```
   # 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
   ```

A
Annie_wang 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

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
```
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

### 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"
   }
   ```