blob: 3e0f8e94d4f3c43b186dd8481c16b9ece9e750bf [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
20import "android/soong/android"
21
22func init() {
23 RegisterRuntimeResourceOverlayBuildComponents(android.InitRegistrationContext)
24}
25
26func RegisterRuntimeResourceOverlayBuildComponents(ctx android.RegistrationContext) {
27 ctx.RegisterModuleType("runtime_resource_overlay", RuntimeResourceOverlayFactory)
28 ctx.RegisterModuleType("override_runtime_resource_overlay", OverrideRuntimeResourceOverlayModuleFactory)
29}
30
31type RuntimeResourceOverlay struct {
32 android.ModuleBase
33 android.DefaultableModuleBase
34 android.OverridableModuleBase
35 aapt
36
37 properties RuntimeResourceOverlayProperties
38 overridableProperties OverridableRuntimeResourceOverlayProperties
39
40 certificate Certificate
41
42 outputFile android.Path
43 installDir android.InstallPath
44}
45
46type RuntimeResourceOverlayProperties struct {
47 // the name of a certificate in the default certificate directory or an android_app_certificate
48 // module name in the form ":module".
49 Certificate *string
50
51 // Name of the signing certificate lineage file.
52 Lineage *string
53
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -040054 // For overriding the --rotation-min-sdk-version property of apksig
55 RotationMinSdkVersion *string
56
Jaewoong Jungf9b44652020-12-21 12:29:12 -080057 // optional theme name. If specified, the overlay package will be applied
58 // only when the ro.boot.vendor.overlay.theme system property is set to the same value.
59 Theme *string
60
Tobias Thierer1b3e9492021-01-02 19:01:16 +000061 // If not blank, set to the version of the sdk to compile against. This
62 // can be either an API version (e.g. "29" for API level 29 AKA Android 10)
63 // or special subsets of the current platform, for example "none", "current",
64 // "core", "system", "test". See build/soong/java/sdk.go for the full and
65 // up-to-date list of possible values.
Jaewoong Jungf9b44652020-12-21 12:29:12 -080066 // Defaults to compiling against the current platform.
67 Sdk_version *string
68
69 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
70 // Defaults to sdk_version if not set.
71 Min_sdk_version *string
72
73 // list of android_library modules whose resources are extracted and linked against statically
74 Static_libs []string
75
76 // list of android_app modules whose resources are extracted and linked against
77 Resource_libs []string
78
79 // Names of modules to be overridden. Listed modules can only be other overlays
80 // (in Make or Soong).
81 // This does not completely prevent installation of the overridden overlays, but if both
82 // overlays would be installed by default (in PRODUCT_PACKAGES) the other overlay will be removed
83 // from PRODUCT_PACKAGES.
84 Overrides []string
85}
86
87// RuntimeResourceOverlayModule interface is used by the apex package to gather information from
88// a RuntimeResourceOverlay module.
89type RuntimeResourceOverlayModule interface {
90 android.Module
91 OutputFile() android.Path
92 Certificate() Certificate
93 Theme() string
94}
95
Spandan Das5d1b9292021-06-03 19:36:41 +000096// RRO's partition logic is different from the partition logic of other modules defined in soong/android/paths.go
97// The default partition for RRO is "/product" and not "/system"
98func rroPartition(ctx android.ModuleContext) string {
99 var partition string
100 if ctx.DeviceSpecific() {
101 partition = ctx.DeviceConfig().OdmPath()
102 } else if ctx.SocSpecific() {
103 partition = ctx.DeviceConfig().VendorPath()
104 } else if ctx.SystemExtSpecific() {
105 partition = ctx.DeviceConfig().SystemExtPath()
106 } else {
107 partition = ctx.DeviceConfig().ProductPath()
108 }
109 return partition
110}
111
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800112func (r *RuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900113 sdkDep := decodeSdkDep(ctx, android.SdkContext(r))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800114 if sdkDep.hasFrameworkLibs() {
115 r.aapt.deps(ctx, sdkDep)
116 }
117
118 cert := android.SrcIsModule(String(r.properties.Certificate))
119 if cert != "" {
120 ctx.AddDependency(ctx.Module(), certificateTag, cert)
121 }
122
123 ctx.AddVariationDependencies(nil, staticLibTag, r.properties.Static_libs...)
124 ctx.AddVariationDependencies(nil, libTag, r.properties.Resource_libs...)
125}
126
127func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
128 // Compile and link resources
129 r.aapt.hasNoCode = true
130 // Do not remove resources without default values nor dedupe resource configurations with the same value
131 aaptLinkFlags := []string{"--no-resource-deduping", "--no-resource-removal"}
132 // Allow the override of "package name" and "overlay target package name"
133 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
134 if overridden || r.overridableProperties.Package_name != nil {
135 // The product override variable has a priority over the package_name property.
136 if !overridden {
137 manifestPackageName = *r.overridableProperties.Package_name
138 }
139 aaptLinkFlags = append(aaptLinkFlags, generateAaptRenamePackageFlags(manifestPackageName, false)...)
140 }
141 if r.overridableProperties.Target_package_name != nil {
142 aaptLinkFlags = append(aaptLinkFlags,
143 "--rename-overlay-target-package "+*r.overridableProperties.Target_package_name)
144 }
Jeremy Meyer7e671292022-10-07 18:21:34 +0000145 if r.overridableProperties.Category != nil {
146 aaptLinkFlags = append(aaptLinkFlags,
147 "--rename-overlay-category "+*r.overridableProperties.Category)
148 }
Alixf7a10272023-09-27 16:47:56 +0000149 r.aapt.buildActions(ctx,
150 aaptBuildActionOptions{
151 sdkContext: r,
152 enforceDefaultTargetSdkVersion: false,
153 extraLinkFlags: aaptLinkFlags,
154 },
155 )
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800156
157 // Sign the built package
Sam Delmerico82602492022-06-10 17:05:42 +0000158 _, _, certificates := collectAppDeps(ctx, r, false, false)
Colin Crossbc2c8a72022-09-14 12:45:42 -0700159 r.certificate, certificates = processMainCert(r.ModuleBase, String(r.properties.Certificate), certificates, ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800160 signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
161 var lineageFile android.Path
162 if lineage := String(r.properties.Lineage); lineage != "" {
163 lineageFile = android.PathForModuleSrc(ctx, lineage)
164 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400165
166 rotationMinSdkVersion := String(r.properties.RotationMinSdkVersion)
167
168 SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil, lineageFile, rotationMinSdkVersion)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800169
170 r.outputFile = signed
Spandan Das5d1b9292021-06-03 19:36:41 +0000171 partition := rroPartition(ctx)
172 r.installDir = android.PathForModuleInPartitionInstall(ctx, partition, "overlay", String(r.properties.Theme))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800173 ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile)
174}
175
Jiyong Park92315372021-04-02 08:45:46 +0900176func (r *RuntimeResourceOverlay) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
177 return android.SdkSpecFrom(ctx, String(r.properties.Sdk_version))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800178}
179
Jiyong Parkf1691d22021-03-29 20:11:58 +0900180func (r *RuntimeResourceOverlay) SystemModules() string {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800181 return ""
182}
183
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000184func (r *RuntimeResourceOverlay) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800185 if r.properties.Min_sdk_version != nil {
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000186 return android.ApiLevelFrom(ctx, *r.properties.Min_sdk_version)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800187 }
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000188 return r.SdkVersion(ctx).ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800189}
190
Spandan Dasa26eda72023-03-02 00:56:06 +0000191func (r *RuntimeResourceOverlay) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
192 return android.SdkSpecPrivate.ApiLevel
William Loh5a082f92022-05-17 20:21:50 +0000193}
194
Spandan Dasca70fc42023-03-01 23:38:49 +0000195func (r *RuntimeResourceOverlay) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
196 return r.SdkVersion(ctx).ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800197}
198
199func (r *RuntimeResourceOverlay) Certificate() Certificate {
200 return r.certificate
201}
202
203func (r *RuntimeResourceOverlay) OutputFile() android.Path {
204 return r.outputFile
205}
206
207func (r *RuntimeResourceOverlay) Theme() string {
208 return String(r.properties.Theme)
209}
210
211// runtime_resource_overlay generates a resource-only apk file that can overlay application and
212// system resources at run time.
213func RuntimeResourceOverlayFactory() android.Module {
214 module := &RuntimeResourceOverlay{}
215 module.AddProperties(
216 &module.properties,
217 &module.aaptProperties,
218 &module.overridableProperties)
219
220 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
221 android.InitDefaultableModule(module)
222 android.InitOverridableModule(module, &module.properties.Overrides)
223 return module
224}
225
226// runtime_resource_overlay properties that can be overridden by override_runtime_resource_overlay
227type OverridableRuntimeResourceOverlayProperties struct {
228 // the package name of this app. The package name in the manifest file is used if one was not given.
229 Package_name *string
230
231 // the target package name of this overlay app. The target package name in the manifest file is used if one was not given.
232 Target_package_name *string
Jeremy Meyer7e671292022-10-07 18:21:34 +0000233
234 // the rro category of this overlay. The category in the manifest file is used if one was not given.
235 Category *string
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800236}
237
238type OverrideRuntimeResourceOverlay struct {
239 android.ModuleBase
240 android.OverrideModuleBase
241}
242
243func (i *OverrideRuntimeResourceOverlay) GenerateAndroidBuildActions(_ android.ModuleContext) {
244 // All the overrides happen in the base module.
245 // TODO(jungjw): Check the base module type.
246}
247
248// override_runtime_resource_overlay is used to create a module based on another
249// runtime_resource_overlay module by overriding some of its properties.
250func OverrideRuntimeResourceOverlayModuleFactory() android.Module {
251 m := &OverrideRuntimeResourceOverlay{}
252 m.AddProperties(&OverridableRuntimeResourceOverlayProperties{})
253
254 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
255 android.InitOverrideModule(m)
256 return m
257}