rro.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // 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. package java
  15. // This file contains the module implementations for runtime_resource_overlay and
  16. // override_runtime_resource_overlay.
  17. import "android/soong/android"
  18. func init() {
  19. RegisterRuntimeResourceOverlayBuildComponents(android.InitRegistrationContext)
  20. }
  21. func RegisterRuntimeResourceOverlayBuildComponents(ctx android.RegistrationContext) {
  22. ctx.RegisterModuleType("runtime_resource_overlay", RuntimeResourceOverlayFactory)
  23. ctx.RegisterModuleType("override_runtime_resource_overlay", OverrideRuntimeResourceOverlayModuleFactory)
  24. }
  25. type RuntimeResourceOverlay struct {
  26. android.ModuleBase
  27. android.DefaultableModuleBase
  28. android.OverridableModuleBase
  29. aapt
  30. properties RuntimeResourceOverlayProperties
  31. overridableProperties OverridableRuntimeResourceOverlayProperties
  32. certificate Certificate
  33. outputFile android.Path
  34. installDir android.InstallPath
  35. }
  36. type RuntimeResourceOverlayProperties struct {
  37. // the name of a certificate in the default certificate directory or an android_app_certificate
  38. // module name in the form ":module".
  39. Certificate *string
  40. // Name of the signing certificate lineage file.
  41. Lineage *string
  42. // For overriding the --rotation-min-sdk-version property of apksig
  43. RotationMinSdkVersion *string
  44. // optional theme name. If specified, the overlay package will be applied
  45. // only when the ro.boot.vendor.overlay.theme system property is set to the same value.
  46. Theme *string
  47. // If not blank, set to the version of the sdk to compile against. This
  48. // can be either an API version (e.g. "29" for API level 29 AKA Android 10)
  49. // or special subsets of the current platform, for example "none", "current",
  50. // "core", "system", "test". See build/soong/java/sdk.go for the full and
  51. // up-to-date list of possible values.
  52. // Defaults to compiling against the current platform.
  53. Sdk_version *string
  54. // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
  55. // Defaults to sdk_version if not set.
  56. Min_sdk_version *string
  57. // list of android_library modules whose resources are extracted and linked against statically
  58. Static_libs []string
  59. // list of android_app modules whose resources are extracted and linked against
  60. Resource_libs []string
  61. // Names of modules to be overridden. Listed modules can only be other overlays
  62. // (in Make or Soong).
  63. // This does not completely prevent installation of the overridden overlays, but if both
  64. // overlays would be installed by default (in PRODUCT_PACKAGES) the other overlay will be removed
  65. // from PRODUCT_PACKAGES.
  66. Overrides []string
  67. }
  68. // RuntimeResourceOverlayModule interface is used by the apex package to gather information from
  69. // a RuntimeResourceOverlay module.
  70. type RuntimeResourceOverlayModule interface {
  71. android.Module
  72. OutputFile() android.Path
  73. Certificate() Certificate
  74. Theme() string
  75. }
  76. // RRO's partition logic is different from the partition logic of other modules defined in soong/android/paths.go
  77. // The default partition for RRO is "/product" and not "/system"
  78. func rroPartition(ctx android.ModuleContext) string {
  79. var partition string
  80. if ctx.DeviceSpecific() {
  81. partition = ctx.DeviceConfig().OdmPath()
  82. } else if ctx.SocSpecific() {
  83. partition = ctx.DeviceConfig().VendorPath()
  84. } else if ctx.SystemExtSpecific() {
  85. partition = ctx.DeviceConfig().SystemExtPath()
  86. } else {
  87. partition = ctx.DeviceConfig().ProductPath()
  88. }
  89. return partition
  90. }
  91. func (r *RuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
  92. sdkDep := decodeSdkDep(ctx, android.SdkContext(r))
  93. if sdkDep.hasFrameworkLibs() {
  94. r.aapt.deps(ctx, sdkDep)
  95. }
  96. cert := android.SrcIsModule(String(r.properties.Certificate))
  97. if cert != "" {
  98. ctx.AddDependency(ctx.Module(), certificateTag, cert)
  99. }
  100. ctx.AddVariationDependencies(nil, staticLibTag, r.properties.Static_libs...)
  101. ctx.AddVariationDependencies(nil, libTag, r.properties.Resource_libs...)
  102. }
  103. func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  104. // Compile and link resources
  105. r.aapt.hasNoCode = true
  106. // Do not remove resources without default values nor dedupe resource configurations with the same value
  107. aaptLinkFlags := []string{"--no-resource-deduping", "--no-resource-removal"}
  108. // Allow the override of "package name" and "overlay target package name"
  109. manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
  110. if overridden || r.overridableProperties.Package_name != nil {
  111. // The product override variable has a priority over the package_name property.
  112. if !overridden {
  113. manifestPackageName = *r.overridableProperties.Package_name
  114. }
  115. aaptLinkFlags = append(aaptLinkFlags, generateAaptRenamePackageFlags(manifestPackageName, false)...)
  116. }
  117. if r.overridableProperties.Target_package_name != nil {
  118. aaptLinkFlags = append(aaptLinkFlags,
  119. "--rename-overlay-target-package "+*r.overridableProperties.Target_package_name)
  120. }
  121. if r.overridableProperties.Category != nil {
  122. aaptLinkFlags = append(aaptLinkFlags,
  123. "--rename-overlay-category "+*r.overridableProperties.Category)
  124. }
  125. r.aapt.buildActions(ctx, r, nil, nil, false, aaptLinkFlags...)
  126. // Sign the built package
  127. _, _, certificates := collectAppDeps(ctx, r, false, false)
  128. r.certificate, certificates = processMainCert(r.ModuleBase, String(r.properties.Certificate), certificates, ctx)
  129. signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
  130. var lineageFile android.Path
  131. if lineage := String(r.properties.Lineage); lineage != "" {
  132. lineageFile = android.PathForModuleSrc(ctx, lineage)
  133. }
  134. rotationMinSdkVersion := String(r.properties.RotationMinSdkVersion)
  135. SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil, lineageFile, rotationMinSdkVersion)
  136. r.outputFile = signed
  137. partition := rroPartition(ctx)
  138. r.installDir = android.PathForModuleInPartitionInstall(ctx, partition, "overlay", String(r.properties.Theme))
  139. ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile)
  140. }
  141. func (r *RuntimeResourceOverlay) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
  142. return android.SdkSpecFrom(ctx, String(r.properties.Sdk_version))
  143. }
  144. func (r *RuntimeResourceOverlay) SystemModules() string {
  145. return ""
  146. }
  147. func (r *RuntimeResourceOverlay) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
  148. if r.properties.Min_sdk_version != nil {
  149. return android.ApiLevelFrom(ctx, *r.properties.Min_sdk_version)
  150. }
  151. return r.SdkVersion(ctx).ApiLevel
  152. }
  153. func (r *RuntimeResourceOverlay) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
  154. return android.SdkSpecPrivate.ApiLevel
  155. }
  156. func (r *RuntimeResourceOverlay) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
  157. return r.SdkVersion(ctx).ApiLevel
  158. }
  159. func (r *RuntimeResourceOverlay) Certificate() Certificate {
  160. return r.certificate
  161. }
  162. func (r *RuntimeResourceOverlay) OutputFile() android.Path {
  163. return r.outputFile
  164. }
  165. func (r *RuntimeResourceOverlay) Theme() string {
  166. return String(r.properties.Theme)
  167. }
  168. // runtime_resource_overlay generates a resource-only apk file that can overlay application and
  169. // system resources at run time.
  170. func RuntimeResourceOverlayFactory() android.Module {
  171. module := &RuntimeResourceOverlay{}
  172. module.AddProperties(
  173. &module.properties,
  174. &module.aaptProperties,
  175. &module.overridableProperties)
  176. android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
  177. android.InitDefaultableModule(module)
  178. android.InitOverridableModule(module, &module.properties.Overrides)
  179. return module
  180. }
  181. // runtime_resource_overlay properties that can be overridden by override_runtime_resource_overlay
  182. type OverridableRuntimeResourceOverlayProperties struct {
  183. // the package name of this app. The package name in the manifest file is used if one was not given.
  184. Package_name *string
  185. // the target package name of this overlay app. The target package name in the manifest file is used if one was not given.
  186. Target_package_name *string
  187. // the rro category of this overlay. The category in the manifest file is used if one was not given.
  188. Category *string
  189. }
  190. type OverrideRuntimeResourceOverlay struct {
  191. android.ModuleBase
  192. android.OverrideModuleBase
  193. }
  194. func (i *OverrideRuntimeResourceOverlay) GenerateAndroidBuildActions(_ android.ModuleContext) {
  195. // All the overrides happen in the base module.
  196. // TODO(jungjw): Check the base module type.
  197. }
  198. // override_runtime_resource_overlay is used to create a module based on another
  199. // runtime_resource_overlay module by overriding some of its properties.
  200. func OverrideRuntimeResourceOverlayModuleFactory() android.Module {
  201. m := &OverrideRuntimeResourceOverlay{}
  202. m.AddProperties(&OverridableRuntimeResourceOverlayProperties{})
  203. android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
  204. android.InitOverrideModule(m)
  205. return m
  206. }