blob: 8bb9be2eb3097dd7ec7decfc1b5cd6aa8f20c406 [file] [log] [blame]
Jaewoong Jungf9b44652020-12-21 12:29:12 -08001// Copyright 2020 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module implementations for runtime_resource_overlay and
18// override_runtime_resource_overlay.
19
Cole Faustb7493472024-08-28 11:55:52 -070020import (
21 "android/soong/android"
22
23 "github.com/google/blueprint/proptools"
24)
Jaewoong Jungf9b44652020-12-21 12:29:12 -080025
26func init() {
27 RegisterRuntimeResourceOverlayBuildComponents(android.InitRegistrationContext)
28}
29
30func RegisterRuntimeResourceOverlayBuildComponents(ctx android.RegistrationContext) {
31 ctx.RegisterModuleType("runtime_resource_overlay", RuntimeResourceOverlayFactory)
32 ctx.RegisterModuleType("override_runtime_resource_overlay", OverrideRuntimeResourceOverlayModuleFactory)
33}
34
35type RuntimeResourceOverlay struct {
36 android.ModuleBase
37 android.DefaultableModuleBase
38 android.OverridableModuleBase
39 aapt
40
41 properties RuntimeResourceOverlayProperties
42 overridableProperties OverridableRuntimeResourceOverlayProperties
43
44 certificate Certificate
45
46 outputFile android.Path
47 installDir android.InstallPath
48}
49
50type RuntimeResourceOverlayProperties struct {
51 // the name of a certificate in the default certificate directory or an android_app_certificate
52 // module name in the form ":module".
53 Certificate *string
54
55 // Name of the signing certificate lineage file.
56 Lineage *string
57
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -040058 // For overriding the --rotation-min-sdk-version property of apksig
59 RotationMinSdkVersion *string
60
Jaewoong Jungf9b44652020-12-21 12:29:12 -080061 // optional theme name. If specified, the overlay package will be applied
62 // only when the ro.boot.vendor.overlay.theme system property is set to the same value.
63 Theme *string
64
Tobias Thierer1b3e9492021-01-02 19:01:16 +000065 // If not blank, set to the version of the sdk to compile against. This
66 // can be either an API version (e.g. "29" for API level 29 AKA Android 10)
67 // or special subsets of the current platform, for example "none", "current",
68 // "core", "system", "test". See build/soong/java/sdk.go for the full and
69 // up-to-date list of possible values.
Jaewoong Jungf9b44652020-12-21 12:29:12 -080070 // Defaults to compiling against the current platform.
71 Sdk_version *string
72
73 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
74 // Defaults to sdk_version if not set.
75 Min_sdk_version *string
76
77 // list of android_library modules whose resources are extracted and linked against statically
Cole Faustb7493472024-08-28 11:55:52 -070078 Static_libs proptools.Configurable[[]string]
Jaewoong Jungf9b44652020-12-21 12:29:12 -080079
80 // list of android_app modules whose resources are extracted and linked against
81 Resource_libs []string
82
83 // Names of modules to be overridden. Listed modules can only be other overlays
84 // (in Make or Soong).
85 // This does not completely prevent installation of the overridden overlays, but if both
86 // overlays would be installed by default (in PRODUCT_PACKAGES) the other overlay will be removed
87 // from PRODUCT_PACKAGES.
88 Overrides []string
89}
90
91// RuntimeResourceOverlayModule interface is used by the apex package to gather information from
92// a RuntimeResourceOverlay module.
93type RuntimeResourceOverlayModule interface {
94 android.Module
95 OutputFile() android.Path
96 Certificate() Certificate
97 Theme() string
98}
99
Spandan Das5d1b9292021-06-03 19:36:41 +0000100// RRO's partition logic is different from the partition logic of other modules defined in soong/android/paths.go
101// The default partition for RRO is "/product" and not "/system"
102func rroPartition(ctx android.ModuleContext) string {
103 var partition string
104 if ctx.DeviceSpecific() {
105 partition = ctx.DeviceConfig().OdmPath()
106 } else if ctx.SocSpecific() {
107 partition = ctx.DeviceConfig().VendorPath()
108 } else if ctx.SystemExtSpecific() {
109 partition = ctx.DeviceConfig().SystemExtPath()
110 } else {
111 partition = ctx.DeviceConfig().ProductPath()
112 }
113 return partition
114}
115
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800116func (r *RuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900117 sdkDep := decodeSdkDep(ctx, android.SdkContext(r))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800118 if sdkDep.hasFrameworkLibs() {
119 r.aapt.deps(ctx, sdkDep)
120 }
121
122 cert := android.SrcIsModule(String(r.properties.Certificate))
123 if cert != "" {
124 ctx.AddDependency(ctx.Module(), certificateTag, cert)
125 }
126
Cole Faustb7493472024-08-28 11:55:52 -0700127 ctx.AddVariationDependencies(nil, staticLibTag, r.properties.Static_libs.GetOrDefault(ctx, nil)...)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800128 ctx.AddVariationDependencies(nil, libTag, r.properties.Resource_libs...)
Jihoon Kang9f442dc2024-03-20 22:09:04 +0000129
130 for _, aconfig_declaration := range r.aaptProperties.Flags_packages {
131 ctx.AddDependency(ctx.Module(), aconfigDeclarationTag, aconfig_declaration)
132 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800133}
134
135func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
136 // Compile and link resources
137 r.aapt.hasNoCode = true
138 // Do not remove resources without default values nor dedupe resource configurations with the same value
139 aaptLinkFlags := []string{"--no-resource-deduping", "--no-resource-removal"}
140 // Allow the override of "package name" and "overlay target package name"
141 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
142 if overridden || r.overridableProperties.Package_name != nil {
143 // The product override variable has a priority over the package_name property.
144 if !overridden {
145 manifestPackageName = *r.overridableProperties.Package_name
146 }
147 aaptLinkFlags = append(aaptLinkFlags, generateAaptRenamePackageFlags(manifestPackageName, false)...)
148 }
149 if r.overridableProperties.Target_package_name != nil {
150 aaptLinkFlags = append(aaptLinkFlags,
151 "--rename-overlay-target-package "+*r.overridableProperties.Target_package_name)
152 }
Jeremy Meyer7e671292022-10-07 18:21:34 +0000153 if r.overridableProperties.Category != nil {
154 aaptLinkFlags = append(aaptLinkFlags,
155 "--rename-overlay-category "+*r.overridableProperties.Category)
156 }
Jihoon Kang9aef7772024-06-14 23:45:06 +0000157 aconfigTextFilePaths := getAconfigFilePaths(ctx)
Alixf7a10272023-09-27 16:47:56 +0000158 r.aapt.buildActions(ctx,
159 aaptBuildActionOptions{
160 sdkContext: r,
161 enforceDefaultTargetSdkVersion: false,
162 extraLinkFlags: aaptLinkFlags,
Jihoon Kang9aef7772024-06-14 23:45:06 +0000163 aconfigTextFiles: aconfigTextFilePaths,
Alixf7a10272023-09-27 16:47:56 +0000164 },
165 )
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800166
167 // Sign the built package
Sam Delmerico82602492022-06-10 17:05:42 +0000168 _, _, certificates := collectAppDeps(ctx, r, false, false)
Colin Crossbc2c8a72022-09-14 12:45:42 -0700169 r.certificate, certificates = processMainCert(r.ModuleBase, String(r.properties.Certificate), certificates, ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800170 signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
171 var lineageFile android.Path
172 if lineage := String(r.properties.Lineage); lineage != "" {
173 lineageFile = android.PathForModuleSrc(ctx, lineage)
174 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400175
176 rotationMinSdkVersion := String(r.properties.RotationMinSdkVersion)
177
178 SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil, lineageFile, rotationMinSdkVersion)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800179
180 r.outputFile = signed
Spandan Das5d1b9292021-06-03 19:36:41 +0000181 partition := rroPartition(ctx)
182 r.installDir = android.PathForModuleInPartitionInstall(ctx, partition, "overlay", String(r.properties.Theme))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800183 ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile)
Jihoon Kang9aef7772024-06-14 23:45:06 +0000184
185 android.SetProvider(ctx, FlagsPackagesProvider, FlagsPackages{
186 AconfigTextFiles: aconfigTextFilePaths,
187 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800188}
189
Jiyong Park92315372021-04-02 08:45:46 +0900190func (r *RuntimeResourceOverlay) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
191 return android.SdkSpecFrom(ctx, String(r.properties.Sdk_version))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800192}
193
Jiyong Parkf1691d22021-03-29 20:11:58 +0900194func (r *RuntimeResourceOverlay) SystemModules() string {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800195 return ""
196}
197
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000198func (r *RuntimeResourceOverlay) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800199 if r.properties.Min_sdk_version != nil {
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000200 return android.ApiLevelFrom(ctx, *r.properties.Min_sdk_version)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800201 }
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000202 return r.SdkVersion(ctx).ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800203}
204
Spandan Dasa26eda72023-03-02 00:56:06 +0000205func (r *RuntimeResourceOverlay) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
206 return android.SdkSpecPrivate.ApiLevel
William Loh5a082f92022-05-17 20:21:50 +0000207}
208
Spandan Dasca70fc42023-03-01 23:38:49 +0000209func (r *RuntimeResourceOverlay) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
210 return r.SdkVersion(ctx).ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800211}
212
213func (r *RuntimeResourceOverlay) Certificate() Certificate {
214 return r.certificate
215}
216
217func (r *RuntimeResourceOverlay) OutputFile() android.Path {
218 return r.outputFile
219}
220
221func (r *RuntimeResourceOverlay) Theme() string {
222 return String(r.properties.Theme)
223}
224
225// runtime_resource_overlay generates a resource-only apk file that can overlay application and
226// system resources at run time.
227func RuntimeResourceOverlayFactory() android.Module {
228 module := &RuntimeResourceOverlay{}
229 module.AddProperties(
230 &module.properties,
231 &module.aaptProperties,
232 &module.overridableProperties)
233
234 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
235 android.InitDefaultableModule(module)
236 android.InitOverridableModule(module, &module.properties.Overrides)
237 return module
238}
239
240// runtime_resource_overlay properties that can be overridden by override_runtime_resource_overlay
241type OverridableRuntimeResourceOverlayProperties struct {
242 // the package name of this app. The package name in the manifest file is used if one was not given.
243 Package_name *string
244
245 // the target package name of this overlay app. The target package name in the manifest file is used if one was not given.
246 Target_package_name *string
Jeremy Meyer7e671292022-10-07 18:21:34 +0000247
248 // the rro category of this overlay. The category in the manifest file is used if one was not given.
249 Category *string
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800250}
251
252type OverrideRuntimeResourceOverlay struct {
253 android.ModuleBase
254 android.OverrideModuleBase
255}
256
257func (i *OverrideRuntimeResourceOverlay) GenerateAndroidBuildActions(_ android.ModuleContext) {
258 // All the overrides happen in the base module.
259 // TODO(jungjw): Check the base module type.
260}
261
262// override_runtime_resource_overlay is used to create a module based on another
263// runtime_resource_overlay module by overriding some of its properties.
264func OverrideRuntimeResourceOverlayModuleFactory() android.Module {
265 m := &OverrideRuntimeResourceOverlay{}
266 m.AddProperties(&OverridableRuntimeResourceOverlayProperties{})
267
268 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
269 android.InitOverrideModule(m)
270 return m
271}