• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# HAP Build Guide
2
3## Overview
4
5### Basic Concepts
6| Term| Description|
7| -------------- | ---------------------- |
8| 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.|
9| 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.|
10| 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### Function
16The HAP provides HAP build functions and supports FA and stage models.
17
18## How to Develop
19
20### Templates
21#### ohos_hap
22
23This template declares a HAP target, which generates a HAP that will be packaged into the system image.
24
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| resources | Resource files, which are stored in the **assets/entry/resources** directory after build.|
30| js_assets | JS resources, which are stored in the **assets/js/default** directory after built with Ace.|
31| ets_assets | eTS resources, which are stored in the **assets/js/default** directory after built with Ace.|
32| deps | Dependencies of the target.|
33| shared_libraries | Native libraries on which the target depends.|
34| 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| 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.|
37| part_name | Name of the part that the HAP belongs to. The name must be the same as that of **subsystem_name**.|
38| js2abc | Whether to convert JS code of the HAP into ARK bytecode.|
39| ets2abc | Whether to convert eTS code of the HAP into ARK bytecode.|
40| certificate_profile | Certificate profile used to sign the HAP.|
41| certificate_file | Certificate file. The certificate and profile must be applies from the official OpenHarmony website.|
42| keystore_path | Keystore file, which is used for signature.|
43| keystore_password | Keystore password, which is used for signature.|
44| key_alias | Alias of a key.|
45| module_install_name | Name of the HAP during installation.|
46| 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#### ohos_app_scope
50Declares 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.
51
52| Variable| Description|
53| --------- | ---- |
54| app_profile | **app.json** file in the AppScope module of the HAP. This variable is used only in the stage model.|
55| sources | Resources in the AppScope module of the HAP. This variable is used only in the stage model.|
56
57#### ohos_js_assets
58Provides 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| ets2abc | eTS code path. This variable is used only for widget configuration. In other development scenarios, use the configuration in the **ohos_hap** template.|
65| 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#### ohos_assets
68Provides raw assets, which are directly copied to the **assets** directory of the HAP.
69
70| Variable| Description|
71|---------- | ---- |
72| sources | Path of the raw assets.|
73
74#### ohos_resources
75Resource 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| sources | Resource file path.|
81| deps | Dependencies of the target. You must configure dependencies on the **ohos_app_scope** target for the stage model.|
82
83### Procedure
84
851. Save the developed application example to the **applications/standard/** directory.
86
872. 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   ```
89   import("//build/ohos.gni") # Import ohos.gni.
90
91   ohos_hap("example") {
92     hap_profile = "./src/main/config.json" # config.json
93     js_assets = ["./src/main/js/default"]
94     raw_assets = ["./raw_assets"]
95     resources = ["./src/main/resources"]
96     shared_libraries = [
97       "//third_party/libpng:libpng", # Native library
98     ]
99     certificate_profile = "../signature/systemui.p7b" # Certificate profile
100     hap_name = "SystemUI-NavigationBar" # HAP name
101     part_name = "prebuilt_hap"
102     subsystem_name = "applications"
103   }
104   ```
105
1063. Modify the **applications/standard/hap/ohos.build** file. <br>The following is an example:
107   ```
108   {
109     "subsystem": "applications",
110     "parts": {
111       "prebuilt_hap": {
112         "module_list": [
113           ...
114           "//applications/standard/example:example" # Add a build target.
115         ]
116       }
117     }
118   }
119   ```
120
1214. Start build.
122   ```
123   # Perform full building.
124   ./build.sh --product-name {product_name}
125
126   # Build the HAP only.
127   ./build.sh --product-name {product_name} --build-target applications/standard/example:example
128   ```
129
130
131The build target is generated. The following shows the decompressed HAP in the FA model.
132```
133  Length      Date    Time    Name
134---------  ---------- -----   ----
135     1439  2009-01-01 00:00   assets/raw_assets                                 -----> raw_assets
136      354  2009-01-01 00:00   assets/entry/resources.index                      ------> resources
137        1  2009-01-01 00:00   assets/entry/resources/base/media/attributes.key  ------> resources
138        1  2009-01-01 00:00   assets/entry/resources/base/media/constants.key   ------> resources
139        1  2009-01-01 00:00   assets/entry/resources/base/media/contents.key    ------> resources
140     6790  2009-01-01 00:00   assets/entry/resources/base/media/icon.png        ------> resources
141        1  2009-01-01 00:00   assets/entry/resources/base/media/nodes.key       ------> resources
142    11170  2009-01-01 00:00   assets/js/default/app.js                          ------> js_assets
143       48  2009-01-01 00:00   assets/js/default/i18n/en-US.json                 ------> js_assets
144       50  2009-01-01 00:00   assets/js/default/i18n/zh-CN.json                 ------> js_assets
145      224  2009-01-01 00:00   assets/js/default/manifest.json                   ------> js_assets
146    41481  2009-01-01 00:00   assets/js/default/pages/index/index.js            ------> js_assets
147      909  2009-01-01 00:00   config.json                                       ------> hap_profile
148   266248  2009-01-01 00:00   libs/libpng.z.so                                  ------> shared_libraries
149```
150
151### GN Script Configuration Example
152- Example of multiple abilities in the FA model
153
154   ```
155   import("//build/ohos.gni")
156
157   ohos_hap("dataability") {
158     hap_profile = "entry/src/main/config.json"
159     deps = [
160       ":dataability_js_assets",
161       ":dataability_resources",
162     ]
163     certificate_profile = "signature/openharmony_sx.p7b"
164     hap_name = "dataability"
165     part_name = "prebuilt_hap"
166     subsystem_name = "applications"
167   }
168
169   ohos_js_assets("dataability_js_assets") {
170     ets2abc = true
171     source_dir = "entry/src/main/ets"
172     hap_profile = "entry/src/main/config.json"
173   }
174
175   ohos_resources("dataability_resources") {
176     sources = [
177       "entry/src/main/resources",
178     ]
179     hap_profile = "entry/src/main/config.json"
180   }
181
182   ```
183
184- Example of a JS widget in the FA model
185
186   ```
187   import("//build/ohos.gni")
188
189   ohos_hap("FormOfFaJs") {
190     hap_profile = "entry/src/main/config.json"
191     deps = [
192       ":FormOfFaJs_js_assets",
193       ":FormOfFaJs_resources",
194     ]
195     certificate_profile = "signature/openharmony_sx.p7b"
196     hap_name = "FormOfFaJs"
197     part_name = "prebuilt_hap"
198     subsystem_name = "applications"
199   }
200
201   ohos_js_assets("FormOfFaJs_js_assets") {
202     hap_profile = "entry/src/main/config.json"
203     js2abc = true
204     source_dir = "entry/src/main/js"
205   }
206
207   ohos_resources("FormOfFaJs_resources") {
208     sources = [
209       "entry/src/main/resources",
210     ]
211     hap_profile = "entry/src/main/config.json"
212   }
213   ```
214
215- Example of an eTS widget in the FA model
216
217   ```
218   import("//build/ohos.gni")
219
220   ohos_hap("FormOfFaEts") {
221     hap_profile = "entry/src/main/config.json"
222     deps = [
223       ":FormOfFaEts_js_assets",
224       ":FormOfFaEts_form_js_assets",
225       ":FormOfFaEts_resources",
226     ]
227     certificate_profile = "signature/openharmony_sx.p7b"
228     hap_name = "FormOfFaEts"
229     part_name = "prebuilt_hap"
230     subsystem_name = "applications"
231   }
232
233   ohos_js_assets("FormOfFaEts_js_assets") {
234     hap_profile = "entry/src/main/config.json"
235     ets2abc = true
236     source_dir = "entry/src/main/ets"
237   }
238
239   ohos_js_assets("FormOfFaEts_form_js_assets") {
240     hap_profile = "entry/src/main/config.json"
241     js2abc = true
242     source_dir = "entry/src/main/js"
243   }
244
245   ohos_resources("FormOfFaEts_resources") {
246     sources = [
247       "entry/src/main/resources",
248     ]
249     hap_profile = "entry/src/main/config.json"
250   }
251   ```
252
253- Example of the stage model
254
255   ```
256   import("//build/ohos.gni")
257
258   ohos_hap("actmoduletest") {
259     hap_profile = "entry/src/main/module.json"
260     deps = [
261       ":actmoduletest_js_assets",
262       ":actmoduletest_resources",
263     ]
264     certificate_profile = "signature/openharmony_sx.p7b"
265     hap_name = "actmoduletest"
266     part_name = "prebuilt_hap"
267     subsystem_name = "applications"
268   }
269
270   ohos_app_scope("actmoduletest_app_profile") {
271     app_profile = "AppScope/app.json"
272     sources = [ "AppScope/resources" ]
273   }
274
275   ohos_js_assets("actmoduletest_js_assets") {
276     ets2abc = true
277     source_dir = "entry/src/main/ets"
278   }
279
280   ohos_resources("actmoduletest_resources") {
281     sources = [
282       "entry/src/main/resources",
283     ]
284     deps = [
285       ":actmoduletest_app_profile",
286     ]
287     hap_profile = "entry/src/main/module.json"
288   }
289   ```
290
291- Example of a widget in the stage model
292
293   ```
294   import("//build/ohos.gni")
295
296   ohos_hap("FormOfStageEts") {
297     hap_profile = "entry/src/main/module.json"
298     deps = [
299       ":FormOfStageEts_js_assets",
300       ":FormOfStageEts_form_js_assets",
301       ":FormOfStageEts_resources",
302     ]
303     js_build_mode = "debug" # The default value is release.
304     certificate_profile = "signature/openharmony_sx.p7b"
305     hap_name = "FormOfStageEts"
306     part_name = "prebuilt_hap"
307     subsystem_name = "applications"
308   }
309
310   ohos_js_assets("FormOfStageEts_js_assets") {
311     hap_profile = "entry/src/main/module.json"
312     ets2abc = true
313     source_dir = "entry/src/main/ets"
314   }
315
316   ohos_js_assets("FormOfStageEts_form_js_assets") {
317     hap_profile = "entry/src/main/module.json"
318     js2abc = true
319     source_dir = "entry/src/main/js"
320   }
321
322   ohos_app_scope("FormOfStageEts_app_profile") {
323     app_profile = "AppScope/app.json"
324     sources = [ "AppScope/resources" ]
325   }
326
327   ohos_resources("FormOfStageEts_resources") {
328     sources = [
329       "entry/src/main/resources",
330     ]
331     deps = [
332       ":FormOfStageEts_app_profile",
333     ]
334     hap_profile = "entry/src/main/module.json"
335   }
336   ```
337