• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Feature
2### Configuration Rules
3
4This document describes how to declare, define, and use features.
5
6- Declare a feature
7
8  Declare the features of a component in **feature_list** of the **bundle.json** file of the component. Each feature must start with the **{component_name}**.
9
10  The following is an example:
11
12  ```
13  {
14    "name": "@ohos/xxx",
15    "component": {
16      "name": "partName",
17      "subsystem": "subsystemName",
18      "features": [
19        "{partName}_feature_A"
20      ]
21    }
22  }
23  ```
24
25   You can declare multiple features in **features** for a component.
26
27- Define a feature
28
29  You can define the default value of a feature as follows:
30
31  ```
32  declare_args() {
33    {partName}_feature_A = true
34  }
35  ```
36
37  The value defined is the default value of the feature for this component. The product can overload the feature default values in the component list.
38
39  If a feature is used by multiple modules of a component, you are advised to define the feature in the global .gni file of the component and import the .gni file to the **BUILD.gn** file of each module.
40
41- Use a feature
42
43  In the **BUILD.gn** file, determine the code or modules to build based on features.
44
45  ```
46  if ({partName}_feature_A) {
47      sources += [ "xxx.c" ]
48  }
49
50  # Dependency introduced by a feature can be isolated by feature.
51  if ({partName}_feature_A) {
52      deps += [ "xxx" ]
53      external_deps += [ "xxx" ]
54  }
55
56  # The **bundle.json** file does not support the if statement. If the sub_component contained in the **bundle.json** file needs to be deleted, define group().
57  group("testGroup") {
58    deps = []
59    if ({partName}_feature_A) {
60      deps += [ "xxx" ]
61    }
62  }
63  ```
64
65  You can also define code macros for modules in the following way to implement differentiated configuration:
66
67  ```
68  if ({partName}_feature_A) {
69      defines += ["FEATUREA_DEFINE"]
70  }
71  ```
72