blob: 98cd3793ebf3f1a4533da8646cfb4339f758b739 [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
54 // optional theme name. If specified, the overlay package will be applied
55 // only when the ro.boot.vendor.overlay.theme system property is set to the same value.
56 Theme *string
57
58 // if not blank, set to the version of the sdk to compile against.
59 // Defaults to compiling against the current platform.
60 Sdk_version *string
61
62 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
63 // Defaults to sdk_version if not set.
64 Min_sdk_version *string
65
66 // list of android_library modules whose resources are extracted and linked against statically
67 Static_libs []string
68
69 // list of android_app modules whose resources are extracted and linked against
70 Resource_libs []string
71
72 // Names of modules to be overridden. Listed modules can only be other overlays
73 // (in Make or Soong).
74 // This does not completely prevent installation of the overridden overlays, but if both
75 // overlays would be installed by default (in PRODUCT_PACKAGES) the other overlay will be removed
76 // from PRODUCT_PACKAGES.
77 Overrides []string
78}
79
80// RuntimeResourceOverlayModule interface is used by the apex package to gather information from
81// a RuntimeResourceOverlay module.
82type RuntimeResourceOverlayModule interface {
83 android.Module
84 OutputFile() android.Path
85 Certificate() Certificate
86 Theme() string
87}
88
89func (r *RuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
90 sdkDep := decodeSdkDep(ctx, sdkContext(r))
91 if sdkDep.hasFrameworkLibs() {
92 r.aapt.deps(ctx, sdkDep)
93 }
94
95 cert := android.SrcIsModule(String(r.properties.Certificate))
96 if cert != "" {
97 ctx.AddDependency(ctx.Module(), certificateTag, cert)
98 }
99
100 ctx.AddVariationDependencies(nil, staticLibTag, r.properties.Static_libs...)
101 ctx.AddVariationDependencies(nil, libTag, r.properties.Resource_libs...)
102}
103
104func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
105 // Compile and link resources
106 r.aapt.hasNoCode = true
107 // Do not remove resources without default values nor dedupe resource configurations with the same value
108 aaptLinkFlags := []string{"--no-resource-deduping", "--no-resource-removal"}
109 // Allow the override of "package name" and "overlay target package name"
110 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
111 if overridden || r.overridableProperties.Package_name != nil {
112 // The product override variable has a priority over the package_name property.
113 if !overridden {
114 manifestPackageName = *r.overridableProperties.Package_name
115 }
116 aaptLinkFlags = append(aaptLinkFlags, generateAaptRenamePackageFlags(manifestPackageName, false)...)
117 }
118 if r.overridableProperties.Target_package_name != nil {
119 aaptLinkFlags = append(aaptLinkFlags,
120 "--rename-overlay-target-package "+*r.overridableProperties.Target_package_name)
121 }
122 r.aapt.buildActions(ctx, r, nil, aaptLinkFlags...)
123
124 // Sign the built package
125 _, certificates := collectAppDeps(ctx, r, false, false)
126 certificates = processMainCert(r.ModuleBase, String(r.properties.Certificate), certificates, ctx)
127 signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
128 var lineageFile android.Path
129 if lineage := String(r.properties.Lineage); lineage != "" {
130 lineageFile = android.PathForModuleSrc(ctx, lineage)
131 }
132 SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil, lineageFile)
133 r.certificate = certificates[0]
134
135 r.outputFile = signed
136 r.installDir = android.PathForModuleInstall(ctx, "overlay", String(r.properties.Theme))
137 ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile)
138}
139
140func (r *RuntimeResourceOverlay) sdkVersion() sdkSpec {
141 return sdkSpecFrom(String(r.properties.Sdk_version))
142}
143
144func (r *RuntimeResourceOverlay) systemModules() string {
145 return ""
146}
147
148func (r *RuntimeResourceOverlay) minSdkVersion() sdkSpec {
149 if r.properties.Min_sdk_version != nil {
150 return sdkSpecFrom(*r.properties.Min_sdk_version)
151 }
152 return r.sdkVersion()
153}
154
155func (r *RuntimeResourceOverlay) targetSdkVersion() sdkSpec {
156 return r.sdkVersion()
157}
158
159func (r *RuntimeResourceOverlay) Certificate() Certificate {
160 return r.certificate
161}
162
163func (r *RuntimeResourceOverlay) OutputFile() android.Path {
164 return r.outputFile
165}
166
167func (r *RuntimeResourceOverlay) Theme() string {
168 return String(r.properties.Theme)
169}
170
171// runtime_resource_overlay generates a resource-only apk file that can overlay application and
172// system resources at run time.
173func RuntimeResourceOverlayFactory() android.Module {
174 module := &RuntimeResourceOverlay{}
175 module.AddProperties(
176 &module.properties,
177 &module.aaptProperties,
178 &module.overridableProperties)
179
180 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
181 android.InitDefaultableModule(module)
182 android.InitOverridableModule(module, &module.properties.Overrides)
183 return module
184}
185
186// runtime_resource_overlay properties that can be overridden by override_runtime_resource_overlay
187type OverridableRuntimeResourceOverlayProperties struct {
188 // the package name of this app. The package name in the manifest file is used if one was not given.
189 Package_name *string
190
191 // the target package name of this overlay app. The target package name in the manifest file is used if one was not given.
192 Target_package_name *string
193}
194
195type OverrideRuntimeResourceOverlay struct {
196 android.ModuleBase
197 android.OverrideModuleBase
198}
199
200func (i *OverrideRuntimeResourceOverlay) GenerateAndroidBuildActions(_ android.ModuleContext) {
201 // All the overrides happen in the base module.
202 // TODO(jungjw): Check the base module type.
203}
204
205// override_runtime_resource_overlay is used to create a module based on another
206// runtime_resource_overlay module by overriding some of its properties.
207func OverrideRuntimeResourceOverlayModuleFactory() android.Module {
208 m := &OverrideRuntimeResourceOverlay{}
209 m.AddProperties(&OverridableRuntimeResourceOverlayProperties{})
210
211 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
212 android.InitOverrideModule(m)
213 return m
214}