blob: 42d42b86af3a93eca3b299a1508e6b7a75dbb3b6 [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
Spandan Dase2f98da2024-11-18 19:22:39 +000023 "github.com/google/blueprint"
Cole Faustb7493472024-08-28 11:55:52 -070024 "github.com/google/blueprint/proptools"
25)
Jaewoong Jungf9b44652020-12-21 12:29:12 -080026
27func init() {
28 RegisterRuntimeResourceOverlayBuildComponents(android.InitRegistrationContext)
29}
30
31func RegisterRuntimeResourceOverlayBuildComponents(ctx android.RegistrationContext) {
32 ctx.RegisterModuleType("runtime_resource_overlay", RuntimeResourceOverlayFactory)
Spandan Dase2f98da2024-11-18 19:22:39 +000033 ctx.RegisterModuleType("autogen_runtime_resource_overlay", AutogenRuntimeResourceOverlayFactory)
Jaewoong Jungf9b44652020-12-21 12:29:12 -080034 ctx.RegisterModuleType("override_runtime_resource_overlay", OverrideRuntimeResourceOverlayModuleFactory)
35}
36
Yu Liu0a37d422025-02-13 02:05:00 +000037type RuntimeResourceOverlayInfo struct {
38 OutputFile android.Path
39 Certificate Certificate
40 Theme string
41 OverriddenManifestPackageName string
42}
43
44var RuntimeResourceOverlayInfoProvider = blueprint.NewProvider[RuntimeResourceOverlayInfo]()
45
Jaewoong Jungf9b44652020-12-21 12:29:12 -080046type RuntimeResourceOverlay struct {
47 android.ModuleBase
48 android.DefaultableModuleBase
49 android.OverridableModuleBase
50 aapt
51
52 properties RuntimeResourceOverlayProperties
53 overridableProperties OverridableRuntimeResourceOverlayProperties
54
55 certificate Certificate
56
57 outputFile android.Path
58 installDir android.InstallPath
59}
60
61type RuntimeResourceOverlayProperties struct {
62 // the name of a certificate in the default certificate directory or an android_app_certificate
63 // module name in the form ":module".
Cole Fausteb9c1482024-11-18 16:49:19 -080064 Certificate proptools.Configurable[string] `android:"replace_instead_of_append"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -080065
66 // Name of the signing certificate lineage file.
67 Lineage *string
68
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -040069 // For overriding the --rotation-min-sdk-version property of apksig
70 RotationMinSdkVersion *string
71
Jaewoong Jungf9b44652020-12-21 12:29:12 -080072 // optional theme name. If specified, the overlay package will be applied
73 // only when the ro.boot.vendor.overlay.theme system property is set to the same value.
74 Theme *string
75
Tobias Thierer1b3e9492021-01-02 19:01:16 +000076 // If not blank, set to the version of the sdk to compile against. This
77 // can be either an API version (e.g. "29" for API level 29 AKA Android 10)
78 // or special subsets of the current platform, for example "none", "current",
79 // "core", "system", "test". See build/soong/java/sdk.go for the full and
80 // up-to-date list of possible values.
Jaewoong Jungf9b44652020-12-21 12:29:12 -080081 // Defaults to compiling against the current platform.
82 Sdk_version *string
83
84 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
85 // Defaults to sdk_version if not set.
86 Min_sdk_version *string
87
88 // list of android_library modules whose resources are extracted and linked against statically
Cole Faustb7493472024-08-28 11:55:52 -070089 Static_libs proptools.Configurable[[]string]
Jaewoong Jungf9b44652020-12-21 12:29:12 -080090
91 // list of android_app modules whose resources are extracted and linked against
92 Resource_libs []string
93
94 // Names of modules to be overridden. Listed modules can only be other overlays
95 // (in Make or Soong).
96 // This does not completely prevent installation of the overridden overlays, but if both
97 // overlays would be installed by default (in PRODUCT_PACKAGES) the other overlay will be removed
98 // from PRODUCT_PACKAGES.
99 Overrides []string
100}
101
102// RuntimeResourceOverlayModule interface is used by the apex package to gather information from
103// a RuntimeResourceOverlay module.
104type RuntimeResourceOverlayModule interface {
105 android.Module
106 OutputFile() android.Path
107 Certificate() Certificate
108 Theme() string
109}
110
Spandan Das5d1b9292021-06-03 19:36:41 +0000111// RRO's partition logic is different from the partition logic of other modules defined in soong/android/paths.go
112// The default partition for RRO is "/product" and not "/system"
113func rroPartition(ctx android.ModuleContext) string {
114 var partition string
115 if ctx.DeviceSpecific() {
116 partition = ctx.DeviceConfig().OdmPath()
117 } else if ctx.SocSpecific() {
118 partition = ctx.DeviceConfig().VendorPath()
119 } else if ctx.SystemExtSpecific() {
120 partition = ctx.DeviceConfig().SystemExtPath()
121 } else {
122 partition = ctx.DeviceConfig().ProductPath()
123 }
124 return partition
125}
126
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800127func (r *RuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900128 sdkDep := decodeSdkDep(ctx, android.SdkContext(r))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800129 if sdkDep.hasFrameworkLibs() {
130 r.aapt.deps(ctx, sdkDep)
131 }
132
Cole Fausteb9c1482024-11-18 16:49:19 -0800133 cert := android.SrcIsModule(r.properties.Certificate.GetOrDefault(ctx, ""))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800134 if cert != "" {
135 ctx.AddDependency(ctx.Module(), certificateTag, cert)
136 }
137
Cole Faustb7493472024-08-28 11:55:52 -0700138 ctx.AddVariationDependencies(nil, staticLibTag, r.properties.Static_libs.GetOrDefault(ctx, nil)...)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800139 ctx.AddVariationDependencies(nil, libTag, r.properties.Resource_libs...)
Jihoon Kang9f442dc2024-03-20 22:09:04 +0000140
141 for _, aconfig_declaration := range r.aaptProperties.Flags_packages {
142 ctx.AddDependency(ctx.Module(), aconfigDeclarationTag, aconfig_declaration)
143 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800144}
145
146func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
147 // Compile and link resources
148 r.aapt.hasNoCode = true
149 // Do not remove resources without default values nor dedupe resource configurations with the same value
150 aaptLinkFlags := []string{"--no-resource-deduping", "--no-resource-removal"}
Artem Anashkin038866a2024-12-16 17:31:55 +0300151
152 // Add TARGET_AAPT_CHARACTERISTICS values to AAPT link flags if they exist and --product flags were not provided.
153 hasProduct := android.PrefixInList(r.aaptProperties.Aaptflags, "--product")
154 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
155 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
156 }
157
158 if !Bool(r.aaptProperties.Aapt_include_all_resources) {
159 // Product AAPT config
160 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
161 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
162 }
163
164 // Product AAPT preferred config
165 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
166 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
167 }
168 }
169
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800170 // Allow the override of "package name" and "overlay target package name"
171 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
172 if overridden || r.overridableProperties.Package_name != nil {
173 // The product override variable has a priority over the package_name property.
174 if !overridden {
175 manifestPackageName = *r.overridableProperties.Package_name
176 }
177 aaptLinkFlags = append(aaptLinkFlags, generateAaptRenamePackageFlags(manifestPackageName, false)...)
178 }
179 if r.overridableProperties.Target_package_name != nil {
180 aaptLinkFlags = append(aaptLinkFlags,
181 "--rename-overlay-target-package "+*r.overridableProperties.Target_package_name)
182 }
Jeremy Meyer7e671292022-10-07 18:21:34 +0000183 if r.overridableProperties.Category != nil {
184 aaptLinkFlags = append(aaptLinkFlags,
185 "--rename-overlay-category "+*r.overridableProperties.Category)
186 }
Jihoon Kang9aef7772024-06-14 23:45:06 +0000187 aconfigTextFilePaths := getAconfigFilePaths(ctx)
Alixf7a10272023-09-27 16:47:56 +0000188 r.aapt.buildActions(ctx,
189 aaptBuildActionOptions{
190 sdkContext: r,
191 enforceDefaultTargetSdkVersion: false,
192 extraLinkFlags: aaptLinkFlags,
Jihoon Kang9aef7772024-06-14 23:45:06 +0000193 aconfigTextFiles: aconfigTextFilePaths,
Alixf7a10272023-09-27 16:47:56 +0000194 },
195 )
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800196
197 // Sign the built package
Sam Delmerico82602492022-06-10 17:05:42 +0000198 _, _, certificates := collectAppDeps(ctx, r, false, false)
Cole Fausteb9c1482024-11-18 16:49:19 -0800199 r.certificate, certificates = processMainCert(r.ModuleBase, r.properties.Certificate.GetOrDefault(ctx, ""), certificates, ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800200 signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
201 var lineageFile android.Path
202 if lineage := String(r.properties.Lineage); lineage != "" {
203 lineageFile = android.PathForModuleSrc(ctx, lineage)
204 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400205
206 rotationMinSdkVersion := String(r.properties.RotationMinSdkVersion)
207
208 SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil, lineageFile, rotationMinSdkVersion)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800209
210 r.outputFile = signed
Spandan Das5d1b9292021-06-03 19:36:41 +0000211 partition := rroPartition(ctx)
212 r.installDir = android.PathForModuleInPartitionInstall(ctx, partition, "overlay", String(r.properties.Theme))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800213 ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile)
Jihoon Kang9aef7772024-06-14 23:45:06 +0000214
215 android.SetProvider(ctx, FlagsPackagesProvider, FlagsPackages{
216 AconfigTextFiles: aconfigTextFilePaths,
217 })
Wei Li986fe742025-01-30 15:14:42 -0800218
Yu Liu0a37d422025-02-13 02:05:00 +0000219 android.SetProvider(ctx, RuntimeResourceOverlayInfoProvider, RuntimeResourceOverlayInfo{
220 OutputFile: r.OutputFile(),
221 Certificate: r.Certificate(),
222 Theme: r.Theme(),
223 })
224
Wei Li986fe742025-01-30 15:14:42 -0800225 buildComplianceMetadata(ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800226}
227
Jiyong Park92315372021-04-02 08:45:46 +0900228func (r *RuntimeResourceOverlay) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
229 return android.SdkSpecFrom(ctx, String(r.properties.Sdk_version))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800230}
231
Jiyong Parkf1691d22021-03-29 20:11:58 +0900232func (r *RuntimeResourceOverlay) SystemModules() string {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800233 return ""
234}
235
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000236func (r *RuntimeResourceOverlay) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800237 if r.properties.Min_sdk_version != nil {
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000238 return android.ApiLevelFrom(ctx, *r.properties.Min_sdk_version)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800239 }
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000240 return r.SdkVersion(ctx).ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800241}
242
Spandan Dasa26eda72023-03-02 00:56:06 +0000243func (r *RuntimeResourceOverlay) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
244 return android.SdkSpecPrivate.ApiLevel
William Loh5a082f92022-05-17 20:21:50 +0000245}
246
Spandan Dasca70fc42023-03-01 23:38:49 +0000247func (r *RuntimeResourceOverlay) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
248 return r.SdkVersion(ctx).ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800249}
250
251func (r *RuntimeResourceOverlay) Certificate() Certificate {
252 return r.certificate
253}
254
255func (r *RuntimeResourceOverlay) OutputFile() android.Path {
256 return r.outputFile
257}
258
259func (r *RuntimeResourceOverlay) Theme() string {
260 return String(r.properties.Theme)
261}
262
263// runtime_resource_overlay generates a resource-only apk file that can overlay application and
264// system resources at run time.
265func RuntimeResourceOverlayFactory() android.Module {
266 module := &RuntimeResourceOverlay{}
267 module.AddProperties(
268 &module.properties,
269 &module.aaptProperties,
270 &module.overridableProperties)
271
272 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
273 android.InitDefaultableModule(module)
274 android.InitOverridableModule(module, &module.properties.Overrides)
275 return module
276}
277
278// runtime_resource_overlay properties that can be overridden by override_runtime_resource_overlay
279type OverridableRuntimeResourceOverlayProperties struct {
280 // the package name of this app. The package name in the manifest file is used if one was not given.
281 Package_name *string
282
283 // the target package name of this overlay app. The target package name in the manifest file is used if one was not given.
284 Target_package_name *string
Jeremy Meyer7e671292022-10-07 18:21:34 +0000285
286 // the rro category of this overlay. The category in the manifest file is used if one was not given.
287 Category *string
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800288}
289
290type OverrideRuntimeResourceOverlay struct {
291 android.ModuleBase
292 android.OverrideModuleBase
293}
294
295func (i *OverrideRuntimeResourceOverlay) GenerateAndroidBuildActions(_ android.ModuleContext) {
296 // All the overrides happen in the base module.
297 // TODO(jungjw): Check the base module type.
298}
299
300// override_runtime_resource_overlay is used to create a module based on another
301// runtime_resource_overlay module by overriding some of its properties.
302func OverrideRuntimeResourceOverlayModuleFactory() android.Module {
303 m := &OverrideRuntimeResourceOverlay{}
304 m.AddProperties(&OverridableRuntimeResourceOverlayProperties{})
305
306 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
307 android.InitOverrideModule(m)
308 return m
309}
Spandan Dase2f98da2024-11-18 19:22:39 +0000310
311var (
312 generateOverlayManifestFile = pctx.AndroidStaticRule("generate_overlay_manifest",
313 blueprint.RuleParams{
314 Command: "build/make/tools/generate-enforce-rro-android-manifest.py " +
315 "--package-info $in " +
316 "--partition ${partition} " +
317 "--priority ${priority} -o $out",
318 CommandDeps: []string{"build/make/tools/generate-enforce-rro-android-manifest.py"},
319 }, "partition", "priority",
320 )
321)
322
323type AutogenRuntimeResourceOverlay struct {
324 android.ModuleBase
325 aapt
326
327 properties AutogenRuntimeResourceOverlayProperties
328
Spandan Dasaf8a3f52024-12-08 18:22:45 +0000329 certificate Certificate
330 outputFile android.Path
Spandan Dase2f98da2024-11-18 19:22:39 +0000331}
332
333type AutogenRuntimeResourceOverlayProperties struct {
334 Base *string
335 Sdk_version *string
336 Manifest *string `android:"path"`
337}
338
339func AutogenRuntimeResourceOverlayFactory() android.Module {
340 m := &AutogenRuntimeResourceOverlay{}
341 m.AddProperties(&m.properties)
342 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
343
344 return m
345}
346
347type rroDependencyTag struct {
348 blueprint.DependencyTag
349}
350
351// Autogenerated RROs should always depend on the source android_app that created it.
352func (tag rroDependencyTag) ReplaceSourceWithPrebuilt() bool {
353 return false
354}
355
356var rroDepTag = rroDependencyTag{}
357
358func (a *AutogenRuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
359 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
360 if sdkDep.hasFrameworkLibs() {
361 a.aapt.deps(ctx, sdkDep)
362 }
363 ctx.AddDependency(ctx.Module(), rroDepTag, proptools.String(a.properties.Base))
364}
365
366func (a *AutogenRuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
367 if !a.Enabled(ctx) {
368 return
369 }
370 var rroDirs android.Paths
371 // Get rro dirs of the base app
372 ctx.VisitDirectDepsWithTag(rroDepTag, func(m android.Module) {
373 aarDep, _ := m.(AndroidLibraryDependency)
374 if ctx.InstallInProduct() {
375 rroDirs = filterRRO(aarDep.RRODirsDepSet(), product)
376 } else {
377 rroDirs = filterRRO(aarDep.RRODirsDepSet(), device)
378 }
379 })
380
381 if len(rroDirs) == 0 {
382 return
383 }
384
385 // Generate a manifest file
386 genManifest := android.PathForModuleGen(ctx, "AndroidManifest.xml")
387 partition := "vendor"
388 priority := "0"
389 if ctx.InstallInProduct() {
390 partition = "product"
391 priority = "1"
392 }
393 ctx.Build(pctx, android.BuildParams{
394 Rule: generateOverlayManifestFile,
395 Input: android.PathForModuleSrc(ctx, proptools.String(a.properties.Manifest)),
396 Output: genManifest,
397 Args: map[string]string{
398 "partition": partition,
399 "priority": priority,
400 },
401 })
402
403 // Compile and link resources into package-res.apk
404 a.aapt.hasNoCode = true
405 aaptLinkFlags := []string{"--auto-add-overlay", "--keep-raw-values", "--no-resource-deduping", "--no-resource-removal"}
406
407 a.aapt.buildActions(ctx,
408 aaptBuildActionOptions{
Spandan Das13193892025-03-03 21:49:06 +0000409 sdkContext: a,
410 extraLinkFlags: aaptLinkFlags,
411 rroDirs: &rroDirs,
412 manifestForAapt: genManifest,
413 aconfigTextFiles: getAconfigFilePaths(ctx),
Spandan Dase2f98da2024-11-18 19:22:39 +0000414 },
415 )
416
417 if a.exportPackage == nil {
418 return
419 }
420 // Sign the built package
Spandan Dasaf8a3f52024-12-08 18:22:45 +0000421 var certificates []Certificate
422 a.certificate, certificates = processMainCert(a.ModuleBase, "", nil, ctx)
Spandan Dase2f98da2024-11-18 19:22:39 +0000423 signed := android.PathForModuleOut(ctx, "signed", a.Name()+".apk")
424 SignAppPackage(ctx, signed, a.exportPackage, certificates, nil, nil, "")
425 a.outputFile = signed
426
427 // Install the signed apk
428 installDir := android.PathForModuleInstall(ctx, "overlay")
429 ctx.InstallFile(installDir, signed.Base(), signed)
Spandan Das1d1a0b82025-03-06 00:47:09 +0000430
431 android.SetProvider(ctx, RuntimeResourceOverlayInfoProvider, RuntimeResourceOverlayInfo{
432 OutputFile: signed,
433 Certificate: a.certificate,
434 })
Spandan Dase2f98da2024-11-18 19:22:39 +0000435}
436
437func (a *AutogenRuntimeResourceOverlay) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
438 return android.SdkSpecFrom(ctx, String(a.properties.Sdk_version))
439}
440
441func (a *AutogenRuntimeResourceOverlay) SystemModules() string {
442 return ""
443}
444
445func (a *AutogenRuntimeResourceOverlay) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
446 return a.SdkVersion(ctx).ApiLevel
447}
448
449func (r *AutogenRuntimeResourceOverlay) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
450 return android.SdkSpecPrivate.ApiLevel
451}
452
453func (a *AutogenRuntimeResourceOverlay) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
454 return a.SdkVersion(ctx).ApiLevel
455}
456
457func (a *AutogenRuntimeResourceOverlay) InstallInProduct() bool {
458 return a.ProductSpecific()
459}