| # Build-time system feature support | |
| ## Overview | |
| System features exposed from `PackageManager` are defined and aggregated as | |
| `<feature>` xml attributes across various partitions, and are currently queried | |
| at runtime through the framework. This directory contains tooling that supports | |
| *build-time* queries of select system features, enabling optimizations | |
| like code stripping and conditional dependencies when so configured. | |
| ### System Feature Codegen | |
| As not all system features can be fully specified or defined at build time (e.g. | |
| updatable partitions and apex modules can change/remove such features), we | |
| use a conditional build flag approach that allows a given device to customize | |
| the subset of build-time defined system features that are immutable and cannot | |
| be updated. | |
| #### Build Flags | |
| System features that can be fixed at build-time may be declared in a common | |
| location, `build/release/flag_declarations/`. These have the form | |
| `RELEASE_SYSTEM_FEATURE_${X}`, where `${X}` corresponds to a feature defined in | |
| `PackageManager`, e.g., `TELEVISION` or `WATCH`. | |
| Build flag values can then be defined per device (or form factor), where such | |
| values either indicate the existence/version of the system feature, or that the | |
| feature is unavailable, e.g., for TV, we could define these build flag values: | |
| ``` | |
| name: "RELEASE_SYSTEM_FEATURE_TELEVISION" | |
| value: { | |
| string_value: "0" # Feature version = 0 | |
| } | |
| ``` | |
| ``` | |
| name: "RELEASE_SYSTEM_FEATURE_WATCH" | |
| value: { | |
| string_value: "UNAVAILABLE" | |
| } | |
| ``` | |
| See also [SystemFeaturesGenerator](src/com/android/systemfeatures/SystemFeaturesGenerator.kt) | |
| for more details. | |
| #### Unavailable Features | |
| In addition to hardcoded build flags for feature availability, we can also | |
| optimize features that are explicitly declared as unavailable via | |
| `<unavailable-feature />` elements. Such declarations always override any | |
| feature availability declarations, even those that might be updated. | |
| #### Build-time Optimizations | |
| When features are defined at build-time via flags or `<unavailable-feature />` | |
| elements, the tooling generates Proguard rules that allow the R8 compiler to optimize calls to | |
| `PackageManager.hasSystemFeature(...)` directly. These rules use the | |
| `-assumevalues` directive to provide the compiler with the fixed values for | |
| these queries, e.g., a feature configuration file `disable_watch.xml` that is | |
| bundled in the partition config, defined as | |
| ```xml | |
| <permissions> | |
| <unavailable-feature name="android.hardware.type.watch" /> | |
| </permissions> | |
| ``` | |
| would yield implicit Proguard rules as follows: | |
| ``` | |
| -assumevalues class android.content.pm.PackageManager { | |
| public boolean hasSystemFeature( | |
| java.lang.String = android.content.pm.PackageManager.FEATURE_WATCH) return false; | |
| } | |
| ``` | |
| ##### Targets and SDKs | |
| These optimizations are automatically applied to targets that are built against | |
| `SdkPrivate` (e.g., those with `platform_apis: true`). This includes key system | |
| components like SystemUI and System Server. By building against the platform | |
| APIs, these targets can leverage the build-time constants defined for the | |
| specific device configuration, leading to reduced code size and improved | |
| performance through code stripping. | |
| > *Note*: Updatable targets, e.g., mainline modules, cannot make any feature | |
| > availability assumptions, and lack the associated build-time optimizataions. | |
| #### Runtime Accelerations | |
| In addition to build-time stripping, the generated code also accelerates | |
| runtime queries. The internal class `com.android.internal.pm.RoSystemFeatures` | |
| provides a `maybeHasFeature` method that `ApplicationPackageManager` uses to | |
| short-circuit standard feature queries. This avoids unnecessary binder calls | |
| and lookups for features that are known to be fixed at build-time. | |
| ```java | |
| // ApplicationPackageManager.java internal usage: | |
| Boolean maybeHasSystemFeature = RoSystemFeatures.maybeHasFeature(name, version); | |
| if (maybeHasSystemFeature != null) { | |
| return maybeHasSystemFeature; | |
| } | |
| // Fallback to standard runtime query... | |
| ``` | |
| See also [RoSystemFeatures](src/com/android/systemfeatures/SystemFeaturesGenerator.kt) | |
| for the generated API surface. |