blob: 7cb362a9fd8da2ecdf79f433b51f88df94b0de86 [file] [log] [blame]
Colin Crossfabb6082018-02-20 17:22:23 -08001// Copyright 2018 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
17import (
Colin Crossa592e3e2019-02-19 16:59:53 -080018 "fmt"
Jaewoong Jung5b425e22019-06-17 17:40:56 -070019 "path/filepath"
Colin Cross312634e2023-11-21 15:13:56 -080020 "slices"
Colin Crossc20dc852020-11-10 12:27:45 -080021 "strconv"
Colin Crossa97c5d32018-03-28 14:58:31 -070022 "strings"
Colin Crossfabb6082018-02-20 17:22:23 -080023
Jaewoong Jung9befb0c2020-01-18 10:33:43 -080024 "android/soong/android"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010025 "android/soong/dexpreopt"
Jihoon Kangfe914ed2024-02-12 22:49:21 +000026
Colin Crossfabb6082018-02-20 17:22:23 -080027 "github.com/google/blueprint"
Colin Crossa97c5d32018-03-28 14:58:31 -070028 "github.com/google/blueprint/proptools"
Colin Crossfabb6082018-02-20 17:22:23 -080029)
30
Colin Crossa97c5d32018-03-28 14:58:31 -070031type AndroidLibraryDependency interface {
Colin Crossa97c5d32018-03-28 14:58:31 -070032 ExportPackage() android.Path
Colin Crossab8d1382023-07-14 17:23:41 +000033 ResourcesNodeDepSet() *android.DepSet[*resourcesNode]
34 RRODirsDepSet() *android.DepSet[rroDir]
35 ManifestsDepSet() *android.DepSet[android.Path]
Jaewoong Jungc779cd42020-10-06 18:56:10 -070036 SetRROEnforcedForDependent(enforce bool)
37 IsRROEnforced(ctx android.BaseModuleContext) bool
Colin Crossa97c5d32018-03-28 14:58:31 -070038}
39
40func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000041 RegisterAARBuildComponents(android.InitRegistrationContext)
42}
43
44func RegisterAARBuildComponents(ctx android.RegistrationContext) {
45 ctx.RegisterModuleType("android_library_import", AARImportFactory)
46 ctx.RegisterModuleType("android_library", AndroidLibraryFactory)
Paul Duffin04ba70d2021-03-22 13:56:43 +000047 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
Colin Cross7e6a9012024-01-17 14:58:38 -080048 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator)
Paul Duffin04ba70d2021-03-22 13:56:43 +000049 })
Colin Crossa97c5d32018-03-28 14:58:31 -070050}
51
52//
53// AAR (android library)
54//
55
56type androidLibraryProperties struct {
57 BuildAAR bool `blueprint:"mutated"`
58}
59
60type aaptProperties struct {
61 // flags passed to aapt when creating the apk
62 Aaptflags []string
63
Dan Willemsen72be5902018-10-24 20:24:57 -070064 // include all resource configurations, not just the product-configured
65 // ones.
66 Aapt_include_all_resources *bool
67
Jiakai Zhangba82e282023-10-13 18:08:59 +010068 // list of files to use as assets.
69 Assets []string `android:"path"`
70
Colin Crossa97c5d32018-03-28 14:58:31 -070071 // list of directories relative to the Blueprints file containing assets.
Colin Cross0ddae7f2019-02-07 15:30:01 -080072 // Defaults to ["assets"] if a directory called assets exists. Set to []
73 // to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070074 Asset_dirs []string
75
76 // list of directories relative to the Blueprints file containing
Colin Cross0ddae7f2019-02-07 15:30:01 -080077 // Android resources. Defaults to ["res"] if a directory called res exists.
78 // Set to [] to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070079 Resource_dirs []string
80
Colin Crossa592e3e2019-02-19 16:59:53 -080081 // list of zip files containing Android resources.
Colin Cross27b922f2019-03-04 22:35:41 -080082 Resource_zips []string `android:"path"`
Colin Crossa592e3e2019-02-19 16:59:53 -080083
Colin Crossa97c5d32018-03-28 14:58:31 -070084 // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml".
Colin Cross27b922f2019-03-04 22:35:41 -080085 Manifest *string `android:"path"`
changho.shinb5432b72019-08-08 18:37:17 +090086
87 // paths to additional manifest files to merge with main manifest.
88 Additional_manifests []string `android:"path"`
Sasha Smundak541056c2019-10-28 15:50:06 -070089
90 // do not include AndroidManifest from dependent libraries
91 Dont_merge_manifests *bool
Jaewoong Jungc779cd42020-10-06 18:56:10 -070092
Colin Cross4eae06d2023-06-20 22:40:02 -070093 // If use_resource_processor is set, use Bazel's resource processor instead of aapt2 to generate R.class files.
94 // The resource processor produces more optimal R.class files that only list resources in the package of the
95 // library that provided them, as opposed to aapt2 which produces R.java files for every package containing
96 // every resource. Using the resource processor can provide significant build time speedups, but requires
97 // fixing the module to use the correct package to reference each resource, and to avoid having any other
98 // libraries in the tree that use the same package name. Defaults to false, but will default to true in the
99 // future.
100 Use_resource_processor *bool
101
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700102 // true if RRO is enforced for any of the dependent modules
103 RROEnforcedForDependent bool `blueprint:"mutated"`
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900104
105 // Filter only specified product and ignore other products
106 Filter_product *string `blueprint:"mutated"`
Colin Crossa97c5d32018-03-28 14:58:31 -0700107}
108
109type aapt struct {
Colin Cross312634e2023-11-21 15:13:56 -0800110 aaptSrcJar android.Path
111 transitiveAaptRJars android.Paths
112 transitiveAaptResourcePackagesFile android.Path
113 exportPackage android.Path
114 manifestPath android.Path
115 proguardOptionsFile android.Path
116 rTxt android.Path
117 rJar android.Path
118 extraAaptPackagesFile android.Path
119 mergedManifestFile android.Path
120 noticeFile android.OptionalPath
121 assetPackage android.OptionalPath
122 isLibrary bool
123 defaultManifestVersion string
124 useEmbeddedNativeLibs bool
125 useEmbeddedDex bool
126 usesNonSdkApis bool
127 hasNoCode bool
128 LoggingParent string
129 resourceFiles android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700130
Colin Crosse560c4a2019-03-19 16:03:11 -0700131 splitNames []string
132 splits []split
133
Colin Crossa97c5d32018-03-28 14:58:31 -0700134 aaptProperties aaptProperties
Colin Crossab8d1382023-07-14 17:23:41 +0000135
136 resourcesNodesDepSet *android.DepSet[*resourcesNode]
137 rroDirsDepSet *android.DepSet[rroDir]
138 manifestsDepSet *android.DepSet[android.Path]
Alix96ea88452023-08-31 15:48:23 +0000139
140 manifestValues struct {
141 applicationId string
142 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700143}
144
Colin Crosse560c4a2019-03-19 16:03:11 -0700145type split struct {
146 name string
147 suffix string
148 path android.Path
149}
150
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700151// Propagate RRO enforcement flag to static lib dependencies transitively.
152func propagateRROEnforcementMutator(ctx android.TopDownMutatorContext) {
153 m := ctx.Module()
154 if d, ok := m.(AndroidLibraryDependency); ok && d.IsRROEnforced(ctx) {
155 ctx.VisitDirectDepsWithTag(staticLibTag, func(d android.Module) {
156 if a, ok := d.(AndroidLibraryDependency); ok {
157 a.SetRROEnforcedForDependent(true)
158 }
159 })
160 }
161}
162
Colin Cross8f1b0332024-01-25 13:39:06 -0800163func (a *aapt) useResourceProcessorBusyBox(ctx android.BaseModuleContext) bool {
164 return BoolDefault(a.aaptProperties.Use_resource_processor, ctx.Config().UseResourceProcessorByDefault())
Colin Cross4eae06d2023-06-20 22:40:02 -0700165}
166
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900167func (a *aapt) filterProduct() string {
168 return String(a.aaptProperties.Filter_product)
169}
170
Colin Crossa97c5d32018-03-28 14:58:31 -0700171func (a *aapt) ExportPackage() android.Path {
172 return a.exportPackage
173}
Colin Crossab8d1382023-07-14 17:23:41 +0000174func (a *aapt) ResourcesNodeDepSet() *android.DepSet[*resourcesNode] {
175 return a.resourcesNodesDepSet
Colin Crossc1c37552019-01-31 11:42:41 -0800176}
177
Colin Crossab8d1382023-07-14 17:23:41 +0000178func (a *aapt) RRODirsDepSet() *android.DepSet[rroDir] {
179 return a.rroDirsDepSet
Colin Crossc1c37552019-01-31 11:42:41 -0800180}
181
Colin Crossab8d1382023-07-14 17:23:41 +0000182func (a *aapt) ManifestsDepSet() *android.DepSet[android.Path] {
183 return a.manifestsDepSet
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800184}
185
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700186func (a *aapt) SetRROEnforcedForDependent(enforce bool) {
187 a.aaptProperties.RROEnforcedForDependent = enforce
188}
189
190func (a *aapt) IsRROEnforced(ctx android.BaseModuleContext) bool {
191 // True if RRO is enforced for this module or...
192 return ctx.Config().EnforceRROForModule(ctx.ModuleName()) ||
Jeongik Chacee5ba92021-02-19 12:11:51 +0900193 // if RRO is enforced for any of its dependents.
194 a.aaptProperties.RROEnforcedForDependent
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700195}
196
Jiyong Parkf1691d22021-03-29 20:11:58 +0900197func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext android.SdkContext,
Colin Crossa0ba2f52019-06-22 12:59:27 -0700198 manifestPath android.Path) (compileFlags, linkFlags []string, linkDeps android.Paths,
199 resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700200
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800201 hasVersionCode := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-code")
202 hasVersionName := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-name")
Colin Crossa97c5d32018-03-28 14:58:31 -0700203
Colin Crossa97c5d32018-03-28 14:58:31 -0700204 // Flags specified in Android.bp
205 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
206
Colin Crossa97c5d32018-03-28 14:58:31 -0700207 // Find implicit or explicit asset and resource dirs
Jiakai Zhangba82e282023-10-13 18:08:59 +0100208 assets := android.PathsRelativeToModuleSourceDir(android.SourceInput{
209 Context: ctx,
210 Paths: a.aaptProperties.Assets,
211 IncludeDirs: false,
212 })
Colin Crossa97c5d32018-03-28 14:58:31 -0700213 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
214 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
Colin Cross8a497952019-03-05 22:25:09 -0800215 resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips)
Colin Crossa97c5d32018-03-28 14:58:31 -0700216
Colin Crossa97c5d32018-03-28 14:58:31 -0700217 // Glob directories into lists of paths
218 for _, dir := range resourceDirs {
219 resDirs = append(resDirs, globbedResourceDir{
220 dir: dir,
221 files: androidResourceGlob(ctx, dir),
222 })
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700223 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, a, dir)
Colin Crossa97c5d32018-03-28 14:58:31 -0700224 overlayDirs = append(overlayDirs, resOverlayDirs...)
225 rroDirs = append(rroDirs, resRRODirs...)
226 }
227
Colin Crossc20dc852020-11-10 12:27:45 -0800228 var assetDeps android.Paths
229 for i, dir := range assetDirs {
230 // Add a dependency on every file in the asset directory. This ensures the aapt2
231 // rule will be rerun if one of the files in the asset directory is modified.
232 assetDeps = append(assetDeps, androidResourceGlob(ctx, dir)...)
233
234 // Add a dependency on a file that contains a list of all the files in the asset directory.
235 // This ensures the aapt2 rule will be run if a file is removed from the asset directory,
236 // or a file is added whose timestamp is older than the output of aapt2.
237 assetFileListFile := android.PathForModuleOut(ctx, "asset_dir_globs", strconv.Itoa(i)+".glob")
238 androidResourceGlobList(ctx, dir, assetFileListFile)
239 assetDeps = append(assetDeps, assetFileListFile)
Colin Crossa97c5d32018-03-28 14:58:31 -0700240 }
241
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700242 assetDirStrings := assetDirs.Strings()
243 if a.noticeFile.Valid() {
244 assetDirStrings = append(assetDirStrings, filepath.Dir(a.noticeFile.Path().String()))
Colin Crossc20dc852020-11-10 12:27:45 -0800245 assetDeps = append(assetDeps, a.noticeFile.Path())
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700246 }
Jiakai Zhangba82e282023-10-13 18:08:59 +0100247 if len(assets) > 0 {
248 // aapt2 doesn't support adding individual asset files. Create a temp directory to hold asset
249 // files and pass it to aapt2.
250 tmpAssetDir := android.PathForModuleOut(ctx, "tmp_asset_dir")
251
252 rule := android.NewRuleBuilder(pctx, ctx)
253 rule.Command().
254 Text("rm -rf").Text(tmpAssetDir.String()).
255 Text("&&").
256 Text("mkdir -p").Text(tmpAssetDir.String())
257
258 for _, asset := range assets {
259 output := tmpAssetDir.Join(ctx, asset.Rel())
260 assetDeps = append(assetDeps, output)
261 rule.Command().Text("mkdir -p").Text(filepath.Dir(output.String()))
262 rule.Command().Text("cp").Input(asset).Output(output)
263 }
264
265 rule.Build("tmp_asset_dir", "tmp_asset_dir")
266
267 assetDirStrings = append(assetDirStrings, tmpAssetDir.String())
268 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700269
Colin Crossa97c5d32018-03-28 14:58:31 -0700270 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
271 linkDeps = append(linkDeps, manifestPath)
272
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700273 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirStrings, "-A "))
Colin Crossc20dc852020-11-10 12:27:45 -0800274 linkDeps = append(linkDeps, assetDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700275
Spandan Das50885c02023-02-23 21:31:33 +0000276 // Returns the effective version for {min|target}_sdk_version
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000277 effectiveVersionString := func(sdkVersion android.SdkSpec, minSdkVersion android.ApiLevel) string {
Spandan Das50885c02023-02-23 21:31:33 +0000278 // If {min|target}_sdk_version is current, use sdk_version to determine the effective level
279 // This is necessary for vendor modules.
280 // The effective version does not _only_ depend on {min|target}_sdk_version(level),
281 // but also on the sdk_version (kind+level)
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000282 if minSdkVersion.IsCurrent() {
Spandan Das50885c02023-02-23 21:31:33 +0000283 ret, err := sdkVersion.EffectiveVersionString(ctx)
284 if err != nil {
285 ctx.ModuleErrorf("invalid sdk_version: %s", err)
286 }
287 return ret
288 }
289 ret, err := minSdkVersion.EffectiveVersionString(ctx)
290 if err != nil {
291 ctx.ModuleErrorf("invalid min_sdk_version: %s", err)
292 }
293 return ret
Jiyong Park6a927c42020-01-21 02:03:43 +0900294 }
Spandan Das50885c02023-02-23 21:31:33 +0000295 // SDK version flags
296 sdkVersion := sdkContext.SdkVersion(ctx)
297 minSdkVersion := effectiveVersionString(sdkVersion, sdkContext.MinSdkVersion(ctx))
Colin Crossa97c5d32018-03-28 14:58:31 -0700298
Colin Cross83bb3162018-06-25 15:48:06 -0700299 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
Spandan Das6450b552023-02-23 19:27:07 +0000300 // Use minSdkVersion for target-sdk-version, even if `target_sdk_version` is set
301 // This behavior has been copied from Make.
Colin Cross83bb3162018-06-25 15:48:06 -0700302 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700303
Colin Crossa97c5d32018-03-28 14:58:31 -0700304 // Version code
305 if !hasVersionCode {
Dan Albert4f378d72020-07-23 17:32:15 -0700306 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion().String())
Colin Crossa97c5d32018-03-28 14:58:31 -0700307 }
308
309 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700310 var versionName string
311 if ctx.ModuleName() == "framework-res" {
312 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
313 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700314 // if it contains the build number. Use the PlatformVersionName instead.
315 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700316 } else {
317 versionName = ctx.Config().AppsDefaultVersionName()
318 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800319 versionName = proptools.NinjaEscape(versionName)
Colin Crossa97c5d32018-03-28 14:58:31 -0700320 linkFlags = append(linkFlags, "--version-name ", versionName)
321 }
322
Colin Crossa0ba2f52019-06-22 12:59:27 -0700323 linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"})
324
325 // Always set --pseudo-localize, it will be stripped out later for release
326 // builds that don't want it.
327 compileFlags = append(compileFlags, "--pseudo-localize")
328
329 return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
Colin Crossa97c5d32018-03-28 14:58:31 -0700330}
331
Paul Duffin250e6192019-06-07 10:44:37 +0100332func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) {
Colin Cross42308aa2018-11-14 21:44:17 -0800333 if sdkDep.frameworkResModule != "" {
334 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700335 }
336}
337
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800338var extractAssetsRule = pctx.AndroidStaticRule("extractAssets",
339 blueprint.RuleParams{
340 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "assets/**/*"`,
341 CommandDeps: []string{"${config.Zip2ZipCmd}"},
342 })
343
Alixf7a10272023-09-27 16:47:56 +0000344type aaptBuildActionOptions struct {
345 sdkContext android.SdkContext
346 classLoaderContexts dexpreopt.ClassLoaderContextMap
347 excludedLibs []string
348 enforceDefaultTargetSdkVersion bool
349 extraLinkFlags []string
Jihoon Kang84b25892023-12-01 22:01:06 +0000350 aconfigTextFiles android.Paths
Alixf7a10272023-09-27 16:47:56 +0000351}
352
353func (a *aapt) buildActions(ctx android.ModuleContext, opts aaptBuildActionOptions) {
Colin Cross5446e882019-05-22 10:46:27 -0700354
Colin Cross8676c8c2023-10-12 15:58:57 -0700355 staticResourcesNodesDepSet, sharedResourcesNodesDepSet, staticRRODirsDepSet, staticManifestsDepSet, sharedExportPackages, libFlags :=
Alixf7a10272023-09-27 16:47:56 +0000356 aaptLibs(ctx, opts.sdkContext, opts.classLoaderContexts)
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100357
Paul Duffin06530572022-02-03 17:54:15 +0000358 // Exclude any libraries from the supplied list.
Alixf7a10272023-09-27 16:47:56 +0000359 opts.classLoaderContexts = opts.classLoaderContexts.ExcludeLibs(opts.excludedLibs)
Paul Duffin06530572022-02-03 17:54:15 +0000360
Colin Cross31656952018-05-24 16:11:20 -0700361 // App manifest file
362 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
363 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
364
Gurpreet Singh7deabfa2022-02-10 13:28:35 +0000365 manifestPath := ManifestFixer(ctx, manifestSrcPath, ManifestFixerParams{
Alixf7a10272023-09-27 16:47:56 +0000366 SdkContext: opts.sdkContext,
367 ClassLoaderContexts: opts.classLoaderContexts,
Harshit Mahajan5b8b7302022-06-10 11:24:05 +0000368 IsLibrary: a.isLibrary,
369 DefaultManifestVersion: a.defaultManifestVersion,
370 UseEmbeddedNativeLibs: a.useEmbeddedNativeLibs,
371 UsesNonSdkApis: a.usesNonSdkApis,
372 UseEmbeddedDex: a.useEmbeddedDex,
373 HasNoCode: a.hasNoCode,
374 LoggingParent: a.LoggingParent,
Alixf7a10272023-09-27 16:47:56 +0000375 EnforceDefaultTargetSdkVersion: opts.enforceDefaultTargetSdkVersion,
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000376 })
Colin Cross90c25c62019-04-19 16:22:57 -0700377
Colin Crossab8d1382023-07-14 17:23:41 +0000378 staticDeps := transitiveAarDeps(staticResourcesNodesDepSet.ToList())
Colin Cross8676c8c2023-10-12 15:58:57 -0700379 sharedDeps := transitiveAarDeps(sharedResourcesNodesDepSet.ToList())
Colin Crossab8d1382023-07-14 17:23:41 +0000380
Luca Stefanifd898822019-09-10 22:13:31 +0200381 // Add additional manifest files to transitive manifests.
382 additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)
Colin Crossab8d1382023-07-14 17:23:41 +0000383 transitiveManifestPaths := append(android.Paths{manifestPath}, additionalManifests...)
384 // TODO(b/288358614): Soong has historically not merged manifests from dependencies of android_library_import
385 // modules. Merging manifests from dependencies could remove the need for pom2bp to generate the "-nodeps" copies
386 // of androidx libraries, but doing so triggers errors due to errors introduced by existing dependencies of
387 // android_library_import modules. If this is fixed, staticManifestsDepSet can be dropped completely in favor of
388 // staticResourcesNodesDepSet.manifests()
389 transitiveManifestPaths = append(transitiveManifestPaths, staticManifestsDepSet.ToList()...)
Colin Cross90c25c62019-04-19 16:22:57 -0700390
Colin Crossab8d1382023-07-14 17:23:41 +0000391 if len(transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) {
Alixf7a10272023-09-27 16:47:56 +0000392 manifestMergerParams := ManifestMergerParams{
393 staticLibManifests: transitiveManifestPaths[1:],
Alix96ea88452023-08-31 15:48:23 +0000394 isLibrary: a.isLibrary,
395 packageName: a.manifestValues.applicationId,
396 }
Alixf7a10272023-09-27 16:47:56 +0000397 a.mergedManifestFile = manifestMerger(ctx, transitiveManifestPaths[0], manifestMergerParams)
Colin Cross90c25c62019-04-19 16:22:57 -0700398 if !a.isLibrary {
399 // Only use the merged manifest for applications. For libraries, the transitive closure of manifests
400 // will be propagated to the final application and merged there. The merged manifest for libraries is
401 // only passed to Make, which can't handle transitive dependencies.
402 manifestPath = a.mergedManifestFile
403 }
404 } else {
405 a.mergedManifestFile = manifestPath
406 }
Colin Cross31656952018-05-24 16:11:20 -0700407
Alixf7a10272023-09-27 16:47:56 +0000408 compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, opts.sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700409
410 linkFlags = append(linkFlags, libFlags...)
Colin Cross8676c8c2023-10-12 15:58:57 -0700411 linkDeps = append(linkDeps, sharedExportPackages...)
Colin Crossab8d1382023-07-14 17:23:41 +0000412 linkDeps = append(linkDeps, staticDeps.resPackages()...)
Alixf7a10272023-09-27 16:47:56 +0000413 linkFlags = append(linkFlags, opts.extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700414 if a.isLibrary {
415 linkFlags = append(linkFlags, "--static-lib")
416 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700417
Colin Cross7c4dc5d2024-02-13 14:29:45 -0800418 linkFlags = append(linkFlags, "--no-static-lib-packages")
Colin Cross8f1b0332024-01-25 13:39:06 -0800419 if a.isLibrary && a.useResourceProcessorBusyBox(ctx) {
Colin Cross7c4dc5d2024-02-13 14:29:45 -0800420 // When building an android_library using ResourceProcessorBusyBox pass --merge-only to skip resource
421 // references validation until the final app link step when all static libraries are present.
Colin Cross4eae06d2023-06-20 22:40:02 -0700422 linkFlags = append(linkFlags, "--merge-only")
Colin Cross4eae06d2023-06-20 22:40:02 -0700423 }
424
Colin Crossa97c5d32018-03-28 14:58:31 -0700425 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
Colin Crossa97c5d32018-03-28 14:58:31 -0700426 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
427 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700428 // This file isn't used by Soong, but is generated for exporting
429 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Cross4eae06d2023-06-20 22:40:02 -0700430 var transitiveRJars android.Paths
Colin Crossf3b7bad2023-08-02 15:49:00 -0700431 var srcJar android.WritablePath
Colin Crossa97c5d32018-03-28 14:58:31 -0700432
Colin Cross4aaa84a2018-08-21 15:14:37 -0700433 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700434 for _, dir := range resDirs {
Colin Cross014489c2020-06-02 20:09:13 -0700435 a.resourceFiles = append(a.resourceFiles, dir.files...)
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900436 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags, a.filterProduct()).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700437 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700438
Colin Crossa592e3e2019-02-19 16:59:53 -0800439 for i, zip := range resZips {
440 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
Colin Crossa0ba2f52019-06-22 12:59:27 -0700441 aapt2CompileZip(ctx, flata, zip, "", compileFlags)
Colin Crossa592e3e2019-02-19 16:59:53 -0800442 compiledResDirs = append(compiledResDirs, android.Paths{flata})
443 }
444
Colin Cross4aaa84a2018-08-21 15:14:37 -0700445 var compiledRes, compiledOverlay android.Paths
446
Colin Crossab8d1382023-07-14 17:23:41 +0000447 // AAPT2 overlays are in lowest to highest priority order, reverse the topological order
448 // of transitiveStaticLibs.
449 transitiveStaticLibs := android.ReversePaths(staticDeps.resPackages())
450
Colin Cross8f1b0332024-01-25 13:39:06 -0800451 if a.isLibrary && a.useResourceProcessorBusyBox(ctx) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700452 // When building an android_library with ResourceProcessorBusyBox enabled treat static library dependencies
453 // as imports. The resources from dependencies will not be merged into this module's package-res.apk, and
454 // instead modules depending on this module will reference package-res.apk from all transitive static
455 // dependencies.
456 for _, staticDep := range staticDeps {
457 linkDeps = append(linkDeps, staticDep.resPackage)
458 linkFlags = append(linkFlags, "-I "+staticDep.resPackage.String())
459 if staticDep.usedResourceProcessor {
460 transitiveRJars = append(transitiveRJars, staticDep.rJar)
461 }
462 }
Colin Cross8676c8c2023-10-12 15:58:57 -0700463 for _, sharedDep := range sharedDeps {
464 if sharedDep.usedResourceProcessor {
465 transitiveRJars = append(transitiveRJars, sharedDep.rJar)
466 }
467 }
Colin Cross4eae06d2023-06-20 22:40:02 -0700468 } else {
469 // When building an app or building a library without ResourceProcessorBusyBox enabled all static
470 // dependencies are compiled into this module's package-res.apk as overlays.
471 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
472 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700473
Colin Crossbec85302019-02-13 13:15:46 -0800474 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700475 // If we are using static android libraries, every source file becomes an overlay.
476 // This is to emulate old AAPT behavior which simulated library support.
477 for _, compiledResDir := range compiledResDirs {
478 compiledOverlay = append(compiledOverlay, compiledResDir...)
479 }
Colin Crossbec85302019-02-13 13:15:46 -0800480 } else if a.isLibrary {
481 // Otherwise, for a static library we treat all the resources equally with no overlay.
482 for _, compiledResDir := range compiledResDirs {
483 compiledRes = append(compiledRes, compiledResDir...)
484 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700485 } else if len(compiledResDirs) > 0 {
486 // Without static libraries, the first directory is our directory, which can then be
487 // overlaid by the rest.
488 compiledRes = append(compiledRes, compiledResDirs[0]...)
489 for _, compiledResDir := range compiledResDirs[1:] {
490 compiledOverlay = append(compiledOverlay, compiledResDir...)
491 }
492 }
493
Colin Crossa97c5d32018-03-28 14:58:31 -0700494 for _, dir := range overlayDirs {
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900495 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags, a.filterProduct()).Paths()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700496 }
497
Colin Crosse560c4a2019-03-19 16:03:11 -0700498 var splitPackages android.WritablePaths
499 var splits []split
500
501 for _, s := range a.splitNames {
502 suffix := strings.Replace(s, ",", "_", -1)
503 path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
504 linkFlags = append(linkFlags, "--split", path.String()+":"+s)
505 splitPackages = append(splitPackages, path)
506 splits = append(splits, split{
507 name: s,
508 suffix: suffix,
509 path: path,
510 })
511 }
512
Colin Cross8f1b0332024-01-25 13:39:06 -0800513 if !a.useResourceProcessorBusyBox(ctx) {
Colin Crossf3b7bad2023-08-02 15:49:00 -0700514 // the subdir "android" is required to be filtered by package names
515 srcJar = android.PathForModuleGen(ctx, "android", "R.srcjar")
516 }
517
Colin Crossab8d1382023-07-14 17:23:41 +0000518 // No need to specify assets from dependencies to aapt2Link for libraries, all transitive assets will be
519 // provided to the final app aapt2Link step.
520 var transitiveAssets android.Paths
521 if !a.isLibrary {
522 transitiveAssets = android.ReverseSliceInPlace(staticDeps.assets())
523 }
Colin Crossf3b7bad2023-08-02 15:49:00 -0700524 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt,
Jihoon Kang84b25892023-12-01 22:01:06 +0000525 linkFlags, linkDeps, compiledRes, compiledOverlay, transitiveAssets, splitPackages,
526 opts.aconfigTextFiles)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800527 // Extract assets from the resource package output so that they can be used later in aapt2link
528 // for modules that depend on this one.
Colin Crossab8d1382023-07-14 17:23:41 +0000529 if android.PrefixInList(linkFlags, "-A ") {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800530 assets := android.PathForModuleOut(ctx, "assets.zip")
531 ctx.Build(pctx, android.BuildParams{
532 Rule: extractAssetsRule,
533 Input: packageRes,
534 Output: assets,
535 Description: "extract assets from built resource file",
536 })
537 a.assetPackage = android.OptionalPathForPath(assets)
538 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700539
Colin Cross8f1b0332024-01-25 13:39:06 -0800540 if a.useResourceProcessorBusyBox(ctx) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700541 rJar := android.PathForModuleOut(ctx, "busybox/R.jar")
Colin Crossd3f7d1a2024-01-03 19:42:25 -0800542 resourceProcessorBusyBoxGenerateBinaryR(ctx, rTxt, a.mergedManifestFile, rJar, staticDeps, a.isLibrary, a.aaptProperties.Aaptflags)
Colin Crossf3b7bad2023-08-02 15:49:00 -0700543 aapt2ExtractExtraPackages(ctx, extraPackages, rJar)
Colin Cross4eae06d2023-06-20 22:40:02 -0700544 transitiveRJars = append(transitiveRJars, rJar)
545 a.rJar = rJar
Colin Crossf3b7bad2023-08-02 15:49:00 -0700546 } else {
547 aapt2ExtractExtraPackages(ctx, extraPackages, srcJar)
Colin Cross4eae06d2023-06-20 22:40:02 -0700548 }
549
Colin Cross312634e2023-11-21 15:13:56 -0800550 transitiveAaptResourcePackages := staticDeps.resPackages().Strings()
551 transitiveAaptResourcePackages = slices.DeleteFunc(transitiveAaptResourcePackages, func(p string) bool {
552 return p == packageRes.String()
553 })
554 transitiveAaptResourcePackagesFile := android.PathForModuleOut(ctx, "transitive-res-packages")
555 android.WriteFileRule(ctx, transitiveAaptResourcePackagesFile, strings.Join(transitiveAaptResourcePackages, "\n"))
556
Colin Crossa97c5d32018-03-28 14:58:31 -0700557 a.aaptSrcJar = srcJar
Colin Cross4eae06d2023-06-20 22:40:02 -0700558 a.transitiveAaptRJars = transitiveRJars
Colin Cross312634e2023-11-21 15:13:56 -0800559 a.transitiveAaptResourcePackagesFile = transitiveAaptResourcePackagesFile
Colin Crossa97c5d32018-03-28 14:58:31 -0700560 a.exportPackage = packageRes
561 a.manifestPath = manifestPath
562 a.proguardOptionsFile = proguardOptionsFile
Colin Cross66f78822018-05-02 12:58:28 -0700563 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700564 a.rTxt = rTxt
Colin Crosse560c4a2019-03-19 16:03:11 -0700565 a.splits = splits
Colin Crossab8d1382023-07-14 17:23:41 +0000566 a.resourcesNodesDepSet = android.NewDepSetBuilder[*resourcesNode](android.TOPOLOGICAL).
567 Direct(&resourcesNode{
568 resPackage: a.exportPackage,
569 manifest: a.manifestPath,
570 additionalManifests: additionalManifests,
Colin Cross4eae06d2023-06-20 22:40:02 -0700571 rTxt: a.rTxt,
572 rJar: a.rJar,
Colin Crossab8d1382023-07-14 17:23:41 +0000573 assets: a.assetPackage,
Colin Cross4eae06d2023-06-20 22:40:02 -0700574
Colin Cross8f1b0332024-01-25 13:39:06 -0800575 usedResourceProcessor: a.useResourceProcessorBusyBox(ctx),
Colin Crossab8d1382023-07-14 17:23:41 +0000576 }).
577 Transitive(staticResourcesNodesDepSet).Build()
578 a.rroDirsDepSet = android.NewDepSetBuilder[rroDir](android.TOPOLOGICAL).
579 Direct(rroDirs...).
580 Transitive(staticRRODirsDepSet).Build()
581 a.manifestsDepSet = android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).
582 Direct(a.manifestPath).
583 DirectSlice(additionalManifests).
584 Transitive(staticManifestsDepSet).Build()
585}
586
Colin Cross4eae06d2023-06-20 22:40:02 -0700587var resourceProcessorBusyBox = pctx.AndroidStaticRule("resourceProcessorBusyBox",
588 blueprint.RuleParams{
589 Command: "${config.JavaCmd} -cp ${config.ResourceProcessorBusyBox} " +
590 "com.google.devtools.build.android.ResourceProcessorBusyBox --tool=GENERATE_BINARY_R -- @${out}.args && " +
591 "if cmp -s ${out}.tmp ${out} ; then rm ${out}.tmp ; else mv ${out}.tmp ${out}; fi",
592 CommandDeps: []string{"${config.ResourceProcessorBusyBox}"},
593 Rspfile: "${out}.args",
594 RspfileContent: "--primaryRTxt ${rTxt} --primaryManifest ${manifest} --classJarOutput ${out}.tmp ${args}",
595 Restat: true,
596 }, "rTxt", "manifest", "args")
597
598// resourceProcessorBusyBoxGenerateBinaryR converts the R.txt file produced by aapt2 into R.class files
599// using Bazel's ResourceProcessorBusyBox tool, which is faster than compiling the R.java files and
600// supports producing classes for static dependencies that only include resources from that dependency.
601func resourceProcessorBusyBoxGenerateBinaryR(ctx android.ModuleContext, rTxt, manifest android.Path,
Colin Crossd3f7d1a2024-01-03 19:42:25 -0800602 rJar android.WritablePath, transitiveDeps transitiveAarDeps, isLibrary bool, aaptFlags []string) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700603
604 var args []string
605 var deps android.Paths
606
607 if !isLibrary {
608 // When compiling an app, pass all R.txt and AndroidManifest.xml from transitive static library dependencies
609 // to ResourceProcessorBusyBox so that it can regenerate R.class files with the final resource IDs for each
610 // package.
611 args, deps = transitiveDeps.resourceProcessorDeps()
612 } else {
613 // When compiling a library don't pass any dependencies as it only needs to generate an R.class file for this
614 // library. Pass --finalFields=false so that the R.class file contains non-final fields so they don't get
615 // inlined into the library before the final IDs are assigned during app compilation.
616 args = append(args, "--finalFields=false")
617 }
618
Colin Crossd3f7d1a2024-01-03 19:42:25 -0800619 for i, arg := range aaptFlags {
620 const AAPT_CUSTOM_PACKAGE = "--custom-package"
621 if strings.HasPrefix(arg, AAPT_CUSTOM_PACKAGE) {
622 pkg := strings.TrimSpace(strings.TrimPrefix(arg, AAPT_CUSTOM_PACKAGE))
623 if pkg == "" && i+1 < len(aaptFlags) {
624 pkg = aaptFlags[i+1]
625 }
626 args = append(args, "--packageForR "+pkg)
627 }
628 }
629
Colin Cross4eae06d2023-06-20 22:40:02 -0700630 deps = append(deps, rTxt, manifest)
631
632 ctx.Build(pctx, android.BuildParams{
633 Rule: resourceProcessorBusyBox,
634 Output: rJar,
635 Implicits: deps,
636 Description: "ResourceProcessorBusyBox",
637 Args: map[string]string{
638 "rTxt": rTxt.String(),
639 "manifest": manifest.String(),
640 "args": strings.Join(args, " "),
641 },
642 })
643}
644
Colin Crossab8d1382023-07-14 17:23:41 +0000645type resourcesNode struct {
646 resPackage android.Path
647 manifest android.Path
648 additionalManifests android.Paths
Colin Cross4eae06d2023-06-20 22:40:02 -0700649 rTxt android.Path
650 rJar android.Path
Colin Crossab8d1382023-07-14 17:23:41 +0000651 assets android.OptionalPath
Colin Cross4eae06d2023-06-20 22:40:02 -0700652
653 usedResourceProcessor bool
Colin Crossab8d1382023-07-14 17:23:41 +0000654}
655
656type transitiveAarDeps []*resourcesNode
657
658func (t transitiveAarDeps) resPackages() android.Paths {
Colin Cross4eae06d2023-06-20 22:40:02 -0700659 paths := make(android.Paths, 0, len(t))
Colin Crossab8d1382023-07-14 17:23:41 +0000660 for _, dep := range t {
661 paths = append(paths, dep.resPackage)
662 }
Colin Cross4eae06d2023-06-20 22:40:02 -0700663 return paths
Colin Crossab8d1382023-07-14 17:23:41 +0000664}
665
666func (t transitiveAarDeps) manifests() android.Paths {
Colin Cross4eae06d2023-06-20 22:40:02 -0700667 paths := make(android.Paths, 0, len(t))
Colin Crossab8d1382023-07-14 17:23:41 +0000668 for _, dep := range t {
669 paths = append(paths, dep.manifest)
670 paths = append(paths, dep.additionalManifests...)
671 }
Colin Cross4eae06d2023-06-20 22:40:02 -0700672 return paths
673}
674
675func (t transitiveAarDeps) resourceProcessorDeps() (args []string, deps android.Paths) {
676 for _, dep := range t {
677 args = append(args, "--library="+dep.rTxt.String()+","+dep.manifest.String())
678 deps = append(deps, dep.rTxt, dep.manifest)
679 }
680 return args, deps
Colin Crossab8d1382023-07-14 17:23:41 +0000681}
682
683func (t transitiveAarDeps) assets() android.Paths {
Colin Cross4eae06d2023-06-20 22:40:02 -0700684 paths := make(android.Paths, 0, len(t))
Colin Crossab8d1382023-07-14 17:23:41 +0000685 for _, dep := range t {
686 if dep.assets.Valid() {
687 paths = append(paths, dep.assets.Path())
688 }
689 }
690 return paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700691}
692
693// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Jiyong Parkf1691d22021-03-29 20:11:58 +0900694func aaptLibs(ctx android.ModuleContext, sdkContext android.SdkContext, classLoaderContexts dexpreopt.ClassLoaderContextMap) (
Colin Cross8676c8c2023-10-12 15:58:57 -0700695 staticResourcesNodes, sharedResourcesNodes *android.DepSet[*resourcesNode], staticRRODirs *android.DepSet[rroDir],
Colin Crossab8d1382023-07-14 17:23:41 +0000696 staticManifests *android.DepSet[android.Path], sharedLibs android.Paths, flags []string) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700697
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100698 if classLoaderContexts == nil {
Ulya Trafimovich18554242020-11-03 15:55:11 +0000699 // Not all callers need to compute class loader context, those who don't just pass nil.
700 // Create a temporary class loader context here (it will be computed, but not used).
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100701 classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000702 }
703
Colin Cross83bb3162018-06-25 15:48:06 -0700704 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700705 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700706 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700707 }
708
Colin Cross8676c8c2023-10-12 15:58:57 -0700709 var staticResourcesNodeDepSets []*android.DepSet[*resourcesNode]
710 var sharedResourcesNodeDepSets []*android.DepSet[*resourcesNode]
Colin Crossab8d1382023-07-14 17:23:41 +0000711 rroDirsDepSetBuilder := android.NewDepSetBuilder[rroDir](android.TOPOLOGICAL)
712 manifestsDepSetBuilder := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL)
713
Colin Crossa97c5d32018-03-28 14:58:31 -0700714 ctx.VisitDirectDeps(func(module android.Module) {
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000715 depTag := ctx.OtherModuleDependencyTag(module)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000716
Colin Crossa97c5d32018-03-28 14:58:31 -0700717 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700718 aarDep, _ := module.(AndroidLibraryDependency)
719 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700720 exportPackage = aarDep.ExportPackage()
721 }
722
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000723 switch depTag {
Colin Cross4b964c02018-10-15 16:18:06 -0700724 case instrumentationForTag:
725 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Liz Kammeref28a4c2022-09-23 16:50:56 -0400726 case sdkLibTag, libTag:
Colin Cross5446e882019-05-22 10:46:27 -0700727 if exportPackage != nil {
Colin Cross8676c8c2023-10-12 15:58:57 -0700728 sharedResourcesNodeDepSets = append(sharedResourcesNodeDepSets, aarDep.ResourcesNodeDepSet())
Colin Cross5446e882019-05-22 10:46:27 -0700729 sharedLibs = append(sharedLibs, exportPackage)
730 }
Colin Cross5446e882019-05-22 10:46:27 -0700731 case frameworkResTag:
Colin Crossa97c5d32018-03-28 14:58:31 -0700732 if exportPackage != nil {
733 sharedLibs = append(sharedLibs, exportPackage)
734 }
735 case staticLibTag:
736 if exportPackage != nil {
Colin Cross8676c8c2023-10-12 15:58:57 -0700737 staticResourcesNodeDepSets = append(staticResourcesNodeDepSets, aarDep.ResourcesNodeDepSet())
Colin Crossab8d1382023-07-14 17:23:41 +0000738 rroDirsDepSetBuilder.Transitive(aarDep.RRODirsDepSet())
739 manifestsDepSetBuilder.Transitive(aarDep.ManifestsDepSet())
Colin Crossa97c5d32018-03-28 14:58:31 -0700740 }
741 }
Ulya Trafimovich18554242020-11-03 15:55:11 +0000742
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +0000743 addCLCFromDep(ctx, module, classLoaderContexts)
Colin Crossa97c5d32018-03-28 14:58:31 -0700744 })
745
Colin Crossab8d1382023-07-14 17:23:41 +0000746 // AAPT2 overlays are in lowest to highest priority order, the topological order will be reversed later.
747 // Reverse the dependency order now going into the depset so that it comes out in order after the second
748 // reverse later.
749 // NOTE: this is legacy and probably incorrect behavior, for most other cases (e.g. conflicting classes in
750 // dependencies) the highest priority dependency is listed first, but for resources the highest priority
751 // dependency has to be listed last.
752 staticResourcesNodes = android.NewDepSet(android.TOPOLOGICAL, nil,
Colin Cross8676c8c2023-10-12 15:58:57 -0700753 android.ReverseSliceInPlace(staticResourcesNodeDepSets))
754 sharedResourcesNodes = android.NewDepSet(android.TOPOLOGICAL, nil,
755 android.ReverseSliceInPlace(sharedResourcesNodeDepSets))
Colin Crossa97c5d32018-03-28 14:58:31 -0700756
Colin Crossab8d1382023-07-14 17:23:41 +0000757 staticRRODirs = rroDirsDepSetBuilder.Build()
758 staticManifests = manifestsDepSetBuilder.Build()
759
760 if len(staticResourcesNodes.ToList()) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700761 flags = append(flags, "--auto-add-overlay")
762 }
763
764 for _, sharedLib := range sharedLibs {
765 flags = append(flags, "-I "+sharedLib.String())
766 }
767
Colin Cross8676c8c2023-10-12 15:58:57 -0700768 return staticResourcesNodes, sharedResourcesNodes, staticRRODirs, staticManifests, sharedLibs, flags
Colin Crossa97c5d32018-03-28 14:58:31 -0700769}
770
771type AndroidLibrary struct {
772 Library
773 aapt
774
775 androidLibraryProperties androidLibraryProperties
776
777 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700778}
779
Saeid Farivar Asanjan1fca3012021-09-14 18:40:19 +0000780var _ android.OutputFileProducer = (*AndroidLibrary)(nil)
781
782// For OutputFileProducer interface
783func (a *AndroidLibrary) OutputFiles(tag string) (android.Paths, error) {
784 switch tag {
785 case ".aar":
786 return []android.Path{a.aarFile}, nil
787 default:
788 return a.Library.OutputFiles(tag)
789 }
790}
791
Colin Crossa97c5d32018-03-28 14:58:31 -0700792var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
793
794func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
795 a.Module.deps(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900796 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
Paul Duffin250e6192019-06-07 10:44:37 +0100797 if sdkDep.hasFrameworkLibs() {
798 a.aapt.deps(ctx, sdkDep)
Colin Crossa97c5d32018-03-28 14:58:31 -0700799 }
Colin Cross4a80a152022-12-21 21:51:52 -0800800 a.usesLibrary.deps(ctx, false)
Colin Crossa97c5d32018-03-28 14:58:31 -0700801}
802
803func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800804 a.aapt.isLibrary = true
Ulya Trafimovich42c7f0d2021-08-17 16:20:29 +0100805 a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Spandan Das0727ba72024-02-13 16:37:43 +0000806 if a.usesLibrary.shouldDisableDexpreopt {
807 a.dexpreopter.disableDexpreopt()
808 }
Alixf7a10272023-09-27 16:47:56 +0000809 a.aapt.buildActions(ctx,
810 aaptBuildActionOptions{
811 sdkContext: android.SdkContext(a),
812 classLoaderContexts: a.classLoaderContexts,
813 enforceDefaultTargetSdkVersion: false,
814 },
815 )
Colin Crossa97c5d32018-03-28 14:58:31 -0700816
Colin Crossff694a82023-12-13 15:54:49 -0800817 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
818 a.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -0700819
Jihoon Kang1bfb6f22023-07-01 00:13:47 +0000820 a.stem = proptools.StringDefault(a.overridableDeviceProperties.Stem, ctx.ModuleName())
821
Colin Cross4eae06d2023-06-20 22:40:02 -0700822 ctx.CheckbuildFile(a.aapt.proguardOptionsFile)
823 ctx.CheckbuildFile(a.aapt.exportPackage)
Colin Cross8f1b0332024-01-25 13:39:06 -0800824 if a.useResourceProcessorBusyBox(ctx) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700825 ctx.CheckbuildFile(a.aapt.rJar)
Colin Crossf3b7bad2023-08-02 15:49:00 -0700826 } else {
827 ctx.CheckbuildFile(a.aapt.aaptSrcJar)
Colin Cross4eae06d2023-06-20 22:40:02 -0700828 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700829
830 // apps manifests are handled by aapt, don't let Module see them
831 a.properties.Manifest = nil
832
Colin Cross014489c2020-06-02 20:09:13 -0700833 a.linter.mergedManifest = a.aapt.mergedManifestFile
834 a.linter.manifest = a.aapt.manifestPath
835 a.linter.resources = a.aapt.resourceFiles
836
Sam Delmericoc8e040c2023-10-31 17:27:02 +0000837 proguardSpecInfo := a.collectProguardSpecInfo(ctx)
Colin Cross40213022023-12-13 15:19:49 -0800838 android.SetProvider(ctx, ProguardSpecInfoProvider, proguardSpecInfo)
Colin Cross312634e2023-11-21 15:13:56 -0800839 exportedProguardFlagsFiles := proguardSpecInfo.ProguardFlagsFiles.ToList()
840 a.extraProguardFlagsFiles = append(a.extraProguardFlagsFiles, exportedProguardFlagsFiles...)
841 a.extraProguardFlagsFiles = append(a.extraProguardFlagsFiles, a.proguardOptionsFile)
842
843 combinedExportedProguardFlagFile := android.PathForModuleOut(ctx, "export_proguard_flags")
844 writeCombinedProguardFlagsFile(ctx, combinedExportedProguardFlagFile, exportedProguardFlagsFiles)
845 a.combinedExportedProguardFlagsFile = combinedExportedProguardFlagFile
Colin Crossa97c5d32018-03-28 14:58:31 -0700846
Colin Cross4eae06d2023-06-20 22:40:02 -0700847 var extraSrcJars android.Paths
848 var extraCombinedJars android.Paths
849 var extraClasspathJars android.Paths
Colin Cross8f1b0332024-01-25 13:39:06 -0800850 if a.useResourceProcessorBusyBox(ctx) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700851 // When building a library with ResourceProcessorBusyBox enabled ResourceProcessorBusyBox for this
852 // library and each of the transitive static android_library dependencies has already created an
853 // R.class file for the appropriate package. Add all of those R.class files to the classpath.
854 extraClasspathJars = a.transitiveAaptRJars
855 } else {
856 // When building a library without ResourceProcessorBusyBox the aapt2 rule creates R.srcjar containing
857 // R.java files for the library's package and the packages from all transitive static android_library
858 // dependencies. Compile the srcjar alongside the rest of the sources.
859 extraSrcJars = android.Paths{a.aapt.aaptSrcJar}
860 }
861
862 a.Module.compile(ctx, extraSrcJars, extraClasspathJars, extraCombinedJars)
Colin Crossa97c5d32018-03-28 14:58:31 -0700863
Colin Crossf57c5782019-01-25 13:20:38 -0800864 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700865 var res android.Paths
866 if a.androidLibraryProperties.BuildAAR {
867 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
868 ctx.CheckbuildFile(a.aarFile)
869 }
Colin Cross89c31582018-04-30 15:55:11 -0700870
Sam Delmerico82602492022-06-10 17:05:42 +0000871 prebuiltJniPackages := android.Paths{}
872 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800873 if info, ok := android.OtherModuleProvider(ctx, module, JniPackageProvider); ok {
Sam Delmerico82602492022-06-10 17:05:42 +0000874 prebuiltJniPackages = append(prebuiltJniPackages, info.JniPackages...)
875 }
876 })
877 if len(prebuiltJniPackages) > 0 {
Colin Cross40213022023-12-13 15:19:49 -0800878 android.SetProvider(ctx, JniPackageProvider, JniPackageInfo{
Sam Delmerico82602492022-06-10 17:05:42 +0000879 JniPackages: prebuiltJniPackages,
880 })
881 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700882}
883
Colin Cross95b53b82023-10-17 13:21:02 -0700884func (a *AndroidLibrary) IDEInfo(dpInfo *android.IdeInfo) {
885 a.Library.IDEInfo(dpInfo)
886 a.aapt.IDEInfo(dpInfo)
887}
888
889func (a *aapt) IDEInfo(dpInfo *android.IdeInfo) {
Colin Cross8f1b0332024-01-25 13:39:06 -0800890 if a.rJar != nil {
Colin Cross95b53b82023-10-17 13:21:02 -0700891 dpInfo.Jars = append(dpInfo.Jars, a.rJar.String())
892 }
893}
894
Colin Cross1b16b0e2019-02-12 14:41:32 -0800895// android_library builds and links sources into a `.jar` file for the device along with Android resources.
896//
897// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
Sam Delmerico82602492022-06-10 17:05:42 +0000898// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
Colin Cross1b16b0e2019-02-12 14:41:32 -0800899// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
900// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700901func AndroidLibraryFactory() android.Module {
902 module := &AndroidLibrary{}
903
Colin Crossce6734e2020-06-15 16:09:53 -0700904 module.Module.addHostAndDeviceProperties()
Colin Crossa97c5d32018-03-28 14:58:31 -0700905 module.AddProperties(
Colin Crossa97c5d32018-03-28 14:58:31 -0700906 &module.aaptProperties,
907 &module.androidLibraryProperties)
908
909 module.androidLibraryProperties.BuildAAR = true
Colin Cross014489c2020-06-02 20:09:13 -0700910 module.Module.linter.library = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700911
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900912 android.InitApexModule(module)
Colin Cross48de9a42018-10-02 13:53:33 -0700913 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700914 return module
915}
916
Colin Crossfabb6082018-02-20 17:22:23 -0800917//
918// AAR (android library) prebuilts
919//
Colin Crossfabb6082018-02-20 17:22:23 -0800920
Vinh Trance0781f2022-04-13 01:30:44 +0000921// Properties for android_library_import
Colin Crossfabb6082018-02-20 17:22:23 -0800922type AARImportProperties struct {
Vinh Trance0781f2022-04-13 01:30:44 +0000923 // ARR (android library prebuilt) filepath. Exactly one ARR is required.
Colin Cross27b922f2019-03-04 22:35:41 -0800924 Aars []string `android:"path"`
Vinh Trance0781f2022-04-13 01:30:44 +0000925 // If not blank, set to the version of the sdk to compile against.
926 // Defaults to private.
927 // Values are of one of the following forms:
928 // 1) numerical API level, "current", "none", or "core_platform"
929 // 2) An SDK kind with an API level: "<sdk kind>_<API level>"
930 // See build/soong/android/sdk_version.go for the complete and up to date list of SDK kinds.
931 // If the SDK kind is empty, it will be set to public
932 Sdk_version *string
933 // If not blank, set the minimum version of the sdk that the compiled artifacts will run against.
934 // Defaults to sdk_version if not set. See sdk_version for possible values.
Colin Cross479884c2018-07-10 13:39:30 -0700935 Min_sdk_version *string
Vinh Trance0781f2022-04-13 01:30:44 +0000936 // List of java static libraries that the included ARR (android library prebuilts) has dependencies to.
Colin Crossa97c5d32018-03-28 14:58:31 -0700937 Static_libs []string
Vinh Trance0781f2022-04-13 01:30:44 +0000938 // List of java libraries that the included ARR (android library prebuilts) has dependencies to.
939 Libs []string
940 // If set to true, run Jetifier against .aar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -0700941 Jetifier *bool
Sam Delmerico82602492022-06-10 17:05:42 +0000942 // If true, extract JNI libs from AAR archive. These libs will be accessible to android_app modules and
943 // will be passed transitively through android_libraries to an android_app.
944 //TODO(b/241138093) evaluate whether we can have this flag default to true for Bazel conversion
945 Extract_jni *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800946}
947
948type AARImport struct {
949 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700950 android.DefaultableModuleBase
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900951 android.ApexModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800952 prebuilt android.Prebuilt
953
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900954 // Functionality common to Module and Import.
955 embeddableInModuleAndImport
956
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500957 providesTransitiveHeaderJars
958
Colin Crossfabb6082018-02-20 17:22:23 -0800959 properties AARImportProperties
960
Colin Cross312634e2023-11-21 15:13:56 -0800961 classpathFile android.WritablePath
962 proguardFlags android.WritablePath
963 exportPackage android.WritablePath
964 transitiveAaptResourcePackagesFile android.Path
965 extraAaptPackagesFile android.WritablePath
966 manifest android.WritablePath
967 assetsPackage android.WritablePath
968 rTxt android.WritablePath
969 rJar android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700970
Colin Crossab8d1382023-07-14 17:23:41 +0000971 resourcesNodesDepSet *android.DepSet[*resourcesNode]
972 manifestsDepSet *android.DepSet[android.Path]
Colin Cross56a83212020-09-15 18:30:11 -0700973
974 hideApexVariantFromMake bool
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000975
Sam Delmerico82602492022-06-10 17:05:42 +0000976 aarPath android.Path
977 jniPackages android.Paths
Jiyong Park92315372021-04-02 08:45:46 +0900978
979 sdkVersion android.SdkSpec
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000980 minSdkVersion android.ApiLevel
LaMont Jonesafe7baf2024-01-09 22:47:39 +0000981
982 // Single aconfig "cache file" merged from this module and all dependencies.
983 mergedAconfigFiles map[string]android.Paths
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000984}
985
986var _ android.OutputFileProducer = (*AARImport)(nil)
987
988// For OutputFileProducer interface
989func (a *AARImport) OutputFiles(tag string) (android.Paths, error) {
990 switch tag {
991 case ".aar":
992 return []android.Path{a.aarPath}, nil
993 case "":
994 return []android.Path{a.classpathFile}, nil
995 default:
996 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
997 }
Colin Crossfabb6082018-02-20 17:22:23 -0800998}
999
Jiyong Park92315372021-04-02 08:45:46 +09001000func (a *AARImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1001 return android.SdkSpecFrom(ctx, String(a.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07001002}
1003
Jiyong Parkf1691d22021-03-29 20:11:58 +09001004func (a *AARImport) SystemModules() string {
Paul Duffine25c6442019-10-11 13:50:28 +01001005 return ""
1006}
1007
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001008func (a *AARImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Colin Cross479884c2018-07-10 13:39:30 -07001009 if a.properties.Min_sdk_version != nil {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001010 return android.ApiLevelFrom(ctx, *a.properties.Min_sdk_version)
Colin Cross479884c2018-07-10 13:39:30 -07001011 }
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001012 return a.SdkVersion(ctx).ApiLevel
Colin Cross83bb3162018-06-25 15:48:06 -07001013}
1014
Spandan Dasa26eda72023-03-02 00:56:06 +00001015func (a *AARImport) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
1016 return android.SdkSpecFrom(ctx, "").ApiLevel
William Loh5a082f92022-05-17 20:21:50 +00001017}
1018
Spandan Dasca70fc42023-03-01 23:38:49 +00001019func (a *AARImport) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
1020 return a.SdkVersion(ctx).ApiLevel
Dan Willemsen419290a2018-10-31 15:28:47 -07001021}
1022
Colin Cross1e743852019-10-28 11:37:20 -07001023func (a *AARImport) javaVersion() string {
1024 return ""
1025}
1026
Colin Crossa97c5d32018-03-28 14:58:31 -07001027var _ AndroidLibraryDependency = (*AARImport)(nil)
1028
1029func (a *AARImport) ExportPackage() android.Path {
1030 return a.exportPackage
1031}
Colin Crossab8d1382023-07-14 17:23:41 +00001032func (a *AARImport) ResourcesNodeDepSet() *android.DepSet[*resourcesNode] {
1033 return a.resourcesNodesDepSet
Colin Crossc1c37552019-01-31 11:42:41 -08001034}
1035
Colin Crossab8d1382023-07-14 17:23:41 +00001036func (a *AARImport) RRODirsDepSet() *android.DepSet[rroDir] {
1037 return android.NewDepSet[rroDir](android.TOPOLOGICAL, nil, nil)
Colin Cross66f78822018-05-02 12:58:28 -07001038}
1039
Colin Crossab8d1382023-07-14 17:23:41 +00001040func (a *AARImport) ManifestsDepSet() *android.DepSet[android.Path] {
1041 return a.manifestsDepSet
Jaewoong Jung6431ca72020-01-15 14:15:10 -08001042}
1043
Jaewoong Jungc779cd42020-10-06 18:56:10 -07001044// RRO enforcement is not available on aar_import since its RRO dirs are not
1045// exported.
1046func (a *AARImport) SetRROEnforcedForDependent(enforce bool) {
1047}
1048
1049// RRO enforcement is not available on aar_import since its RRO dirs are not
1050// exported.
1051func (a *AARImport) IsRROEnforced(ctx android.BaseModuleContext) bool {
1052 return false
1053}
1054
Colin Crossfabb6082018-02-20 17:22:23 -08001055func (a *AARImport) Prebuilt() *android.Prebuilt {
1056 return &a.prebuilt
1057}
1058
1059func (a *AARImport) Name() string {
1060 return a.prebuilt.Name(a.ModuleBase.Name())
1061}
1062
Jiyong Park618922e2020-01-08 13:35:43 +09001063func (a *AARImport) JacocoReportClassesFile() android.Path {
1064 return nil
1065}
1066
Colin Crossfabb6082018-02-20 17:22:23 -08001067func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Jeongik Cha816a23a2020-07-08 01:09:23 +09001068 if !ctx.Config().AlwaysUsePrebuiltSdks() {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001069 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -07001070 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -07001071 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -08001072 }
1073 }
Colin Crossa97c5d32018-03-28 14:58:31 -07001074
Colin Cross42d48b72018-08-29 14:10:52 -07001075 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
1076 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -08001077}
1078
Sam Delmerico82602492022-06-10 17:05:42 +00001079type JniPackageInfo struct {
1080 // List of zip files containing JNI libraries
1081 // Zip files should have directory structure jni/<arch>/*.so
1082 JniPackages android.Paths
1083}
1084
Colin Crossbc7d76c2023-12-12 16:39:03 -08001085var JniPackageProvider = blueprint.NewProvider[JniPackageInfo]()
Sam Delmerico82602492022-06-10 17:05:42 +00001086
1087// Unzip an AAR and extract the JNI libs for $archString.
1088var extractJNI = pctx.AndroidStaticRule("extractJNI",
1089 blueprint.RuleParams{
1090 Command: `rm -rf $out $outDir && touch $out && ` +
1091 `unzip -qoDD -d $outDir $in "jni/${archString}/*" && ` +
1092 `jni_files=$$(find $outDir/jni -type f) && ` +
1093 // print error message if there are no JNI libs for this arch
1094 `[ -n "$$jni_files" ] || (echo "ERROR: no JNI libs found for arch ${archString}" && exit 1) && ` +
Sam Delmerico80ee45c2023-06-22 15:36:02 -04001095 `${config.SoongZipCmd} -o $out -L 0 -P 'lib/${archString}' ` +
Sam Delmerico82602492022-06-10 17:05:42 +00001096 `-C $outDir/jni/${archString} $$(echo $$jni_files | xargs -n1 printf " -f %s")`,
1097 CommandDeps: []string{"${config.SoongZipCmd}"},
1098 },
1099 "outDir", "archString")
1100
Colin Crossfabb6082018-02-20 17:22:23 -08001101// Unzip an AAR into its constituent files and directories. Any files in Outputs that don't exist in the AAR will be
Dan Willemsen304cfec2019-05-28 14:49:06 -07001102// touched to create an empty file. The res directory is not extracted, as it will be extracted in its own rule.
Colin Crossfabb6082018-02-20 17:22:23 -08001103var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
1104 blueprint.RuleParams{
Dan Willemsen304cfec2019-05-28 14:49:06 -07001105 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
Colin Cross205e9112020-08-06 13:20:17 -07001106 `unzip -qoDD -d $outDir $in && rm -rf $outDir/res && touch $out && ` +
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001107 `${config.Zip2ZipCmd} -i $in -o $assetsPackage 'assets/**/*' && ` +
Colin Cross205e9112020-08-06 13:20:17 -07001108 `${config.MergeZipsCmd} $combinedClassesJar $$(ls $outDir/classes.jar 2> /dev/null) $$(ls $outDir/libs/*.jar 2> /dev/null)`,
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001109 CommandDeps: []string{"${config.MergeZipsCmd}", "${config.Zip2ZipCmd}"},
Colin Crossfabb6082018-02-20 17:22:23 -08001110 },
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001111 "outDir", "combinedClassesJar", "assetsPackage")
Colin Crossfabb6082018-02-20 17:22:23 -08001112
1113func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1114 if len(a.properties.Aars) != 1 {
1115 ctx.PropertyErrorf("aars", "exactly one aar is required")
1116 return
1117 }
1118
Jiyong Park92315372021-04-02 08:45:46 +09001119 a.sdkVersion = a.SdkVersion(ctx)
1120 a.minSdkVersion = a.MinSdkVersion(ctx)
1121
Colin Crossff694a82023-12-13 15:54:49 -08001122 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1123 a.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07001124
Nan Zhang4c819fb2018-08-27 18:31:46 -07001125 aarName := ctx.ModuleName() + ".aar"
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001126 a.aarPath = android.PathForModuleSrc(ctx, a.properties.Aars[0])
1127
Colin Cross1001a792019-03-21 22:21:39 -07001128 if Bool(a.properties.Jetifier) {
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001129 inputFile := a.aarPath
1130 a.aarPath = android.PathForModuleOut(ctx, "jetifier", aarName)
1131 TransformJetifier(ctx, a.aarPath.(android.WritablePath), inputFile)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001132 }
Colin Crossfabb6082018-02-20 17:22:23 -08001133
1134 extractedAARDir := android.PathForModuleOut(ctx, "aar")
Colin Cross205e9112020-08-06 13:20:17 -07001135 a.classpathFile = extractedAARDir.Join(ctx, "classes-combined.jar")
Colin Cross10f7c4a2018-05-23 10:59:28 -07001136 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossbb77d8e2024-02-15 14:43:47 -08001137 a.rTxt = extractedAARDir.Join(ctx, "R.txt")
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001138 a.assetsPackage = android.PathForModuleOut(ctx, "assets.zip")
Sam Delmerico95d70942023-08-02 18:00:35 -04001139 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross40213022023-12-13 15:19:49 -08001140 android.SetProvider(ctx, ProguardSpecInfoProvider, ProguardSpecInfo{
Sam Delmerico95d70942023-08-02 18:00:35 -04001141 ProguardFlagsFiles: android.NewDepSet[android.Path](
1142 android.POSTORDER,
1143 android.Paths{a.proguardFlags},
1144 nil,
1145 ),
1146 })
Colin Crossfabb6082018-02-20 17:22:23 -08001147
1148 ctx.Build(pctx, android.BuildParams{
1149 Rule: unzipAAR,
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001150 Input: a.aarPath,
Colin Crossbb77d8e2024-02-15 14:43:47 -08001151 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest, a.assetsPackage, a.rTxt},
Colin Crossfabb6082018-02-20 17:22:23 -08001152 Description: "unzip AAR",
1153 Args: map[string]string{
Colin Cross205e9112020-08-06 13:20:17 -07001154 "outDir": extractedAARDir.String(),
1155 "combinedClassesJar": a.classpathFile.String(),
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001156 "assetsPackage": a.assetsPackage.String(),
Colin Crossfabb6082018-02-20 17:22:23 -08001157 },
1158 })
1159
Colin Crossa0ba2f52019-06-22 12:59:27 -07001160 // Always set --pseudo-localize, it will be stripped out later for release
1161 // builds that don't want it.
1162 compileFlags := []string{"--pseudo-localize"}
Colin Crossfabb6082018-02-20 17:22:23 -08001163 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
Colin Crossfabb6082018-02-20 17:22:23 -08001164 flata := compiledResDir.Join(ctx, "gen_res.flata")
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001165 aapt2CompileZip(ctx, flata, a.aarPath, "res", compileFlags)
Colin Crossfabb6082018-02-20 17:22:23 -08001166
1167 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
Colin Crossfabb6082018-02-20 17:22:23 -08001168 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossbb77d8e2024-02-15 14:43:47 -08001169 aaptRTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -07001170 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -08001171
1172 var linkDeps android.Paths
1173
1174 linkFlags := []string{
1175 "--static-lib",
Colin Cross4eae06d2023-06-20 22:40:02 -07001176 "--merge-only",
Colin Crossfabb6082018-02-20 17:22:23 -08001177 "--auto-add-overlay",
Colin Cross7c4dc5d2024-02-13 14:29:45 -08001178 "--no-static-lib-packages",
Colin Crossfabb6082018-02-20 17:22:23 -08001179 }
1180
Colin Cross10f7c4a2018-05-23 10:59:28 -07001181 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
1182 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -08001183
Colin Cross8676c8c2023-10-12 15:58:57 -07001184 staticResourcesNodesDepSet, sharedResourcesNodesDepSet, staticRRODirsDepSet, staticManifestsDepSet, sharedLibs, libFlags :=
Jiyong Parkf1691d22021-03-29 20:11:58 +09001185 aaptLibs(ctx, android.SdkContext(a), nil)
Colin Cross31656952018-05-24 16:11:20 -07001186
Colin Cross8676c8c2023-10-12 15:58:57 -07001187 _ = sharedResourcesNodesDepSet
Colin Crossab8d1382023-07-14 17:23:41 +00001188 _ = staticRRODirsDepSet
Colin Cross8676c8c2023-10-12 15:58:57 -07001189
Colin Crossab8d1382023-07-14 17:23:41 +00001190 staticDeps := transitiveAarDeps(staticResourcesNodesDepSet.ToList())
Colin Crossfabb6082018-02-20 17:22:23 -08001191
Colin Crossab8d1382023-07-14 17:23:41 +00001192 linkDeps = append(linkDeps, sharedLibs...)
Colin Cross4eae06d2023-06-20 22:40:02 -07001193 linkDeps = append(linkDeps, staticDeps.resPackages()...)
Colin Crossa97c5d32018-03-28 14:58:31 -07001194 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -08001195
Colin Cross4eae06d2023-06-20 22:40:02 -07001196 overlayRes := android.Paths{flata}
1197
1198 // Treat static library dependencies of static libraries as imports.
1199 transitiveStaticLibs := staticDeps.resPackages()
1200 linkDeps = append(linkDeps, transitiveStaticLibs...)
1201 for _, staticLib := range transitiveStaticLibs {
1202 linkFlags = append(linkFlags, "-I "+staticLib.String())
1203 }
Colin Crossfabb6082018-02-20 17:22:23 -08001204
Colin Crossab8d1382023-07-14 17:23:41 +00001205 transitiveAssets := android.ReverseSliceInPlace(staticDeps.assets())
Colin Crossbb77d8e2024-02-15 14:43:47 -08001206 aapt2Link(ctx, a.exportPackage, nil, proguardOptionsFile, aaptRTxt,
Jihoon Kang84b25892023-12-01 22:01:06 +00001207 linkFlags, linkDeps, nil, overlayRes, transitiveAssets, nil, nil)
Colin Crossfabb6082018-02-20 17:22:23 -08001208
Colin Cross4eae06d2023-06-20 22:40:02 -07001209 a.rJar = android.PathForModuleOut(ctx, "busybox/R.jar")
Colin Crossd3f7d1a2024-01-03 19:42:25 -08001210 resourceProcessorBusyBoxGenerateBinaryR(ctx, a.rTxt, a.manifest, a.rJar, nil, true, nil)
Colin Cross4eae06d2023-06-20 22:40:02 -07001211
Colin Crossf3b7bad2023-08-02 15:49:00 -07001212 aapt2ExtractExtraPackages(ctx, a.extraAaptPackagesFile, a.rJar)
1213
Colin Crossab8d1382023-07-14 17:23:41 +00001214 resourcesNodesDepSetBuilder := android.NewDepSetBuilder[*resourcesNode](android.TOPOLOGICAL)
1215 resourcesNodesDepSetBuilder.Direct(&resourcesNode{
1216 resPackage: a.exportPackage,
1217 manifest: a.manifest,
Colin Cross4eae06d2023-06-20 22:40:02 -07001218 rTxt: a.rTxt,
1219 rJar: a.rJar,
Colin Crossab8d1382023-07-14 17:23:41 +00001220 assets: android.OptionalPathForPath(a.assetsPackage),
Colin Cross4eae06d2023-06-20 22:40:02 -07001221
1222 usedResourceProcessor: true,
Colin Crossab8d1382023-07-14 17:23:41 +00001223 })
1224 resourcesNodesDepSetBuilder.Transitive(staticResourcesNodesDepSet)
1225 a.resourcesNodesDepSet = resourcesNodesDepSetBuilder.Build()
1226
1227 manifestDepSetBuilder := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(a.manifest)
1228 // TODO(b/288358614): Soong has historically not merged manifests from dependencies of android_library_import
1229 // modules. Merging manifests from dependencies could remove the need for pom2bp to generate the "-nodeps" copies
1230 // of androidx libraries, but doing so triggers errors due to errors introduced by existing dependencies of
1231 // android_library_import modules. If this is fixed, AndroidLibraryDependency.ManifestsDepSet can be dropped
1232 // completely in favor of AndroidLibraryDependency.ResourceNodesDepSet.manifest
1233 //manifestDepSetBuilder.Transitive(transitiveStaticDeps.manifests)
1234 _ = staticManifestsDepSet
1235 a.manifestsDepSet = manifestDepSetBuilder.Build()
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001236
Colin Cross312634e2023-11-21 15:13:56 -08001237 transitiveAaptResourcePackages := staticDeps.resPackages().Strings()
1238 transitiveAaptResourcePackages = slices.DeleteFunc(transitiveAaptResourcePackages, func(p string) bool {
1239 return p == a.exportPackage.String()
1240 })
1241 transitiveAaptResourcePackagesFile := android.PathForModuleOut(ctx, "transitive-res-packages")
1242 android.WriteFileRule(ctx, transitiveAaptResourcePackagesFile, strings.Join(transitiveAaptResourcePackages, "\n"))
1243 a.transitiveAaptResourcePackagesFile = transitiveAaptResourcePackagesFile
Colin Cross4eae06d2023-06-20 22:40:02 -07001244
Sam Delmerico9f9c0a22022-11-29 11:19:37 -05001245 a.collectTransitiveHeaderJars(ctx)
Colin Cross40213022023-12-13 15:19:49 -08001246 android.SetProvider(ctx, JavaInfoProvider, JavaInfo{
Colin Crossdcf71b22021-02-01 13:59:03 -08001247 HeaderJars: android.PathsIfNonNil(a.classpathFile),
Sam Delmerico9f9c0a22022-11-29 11:19:37 -05001248 TransitiveLibsHeaderJars: a.transitiveLibsHeaderJars,
1249 TransitiveStaticLibsHeaderJars: a.transitiveStaticLibsHeaderJars,
Colin Crossdcf71b22021-02-01 13:59:03 -08001250 ImplementationAndResourcesJars: android.PathsIfNonNil(a.classpathFile),
1251 ImplementationJars: android.PathsIfNonNil(a.classpathFile),
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001252 StubsLinkType: Implementation,
Joe Onorato6fe59eb2023-07-16 13:20:33 -07001253 // TransitiveAconfigFiles: // TODO(b/289117800): LOCAL_ACONFIG_FILES for prebuilts
Colin Crossdcf71b22021-02-01 13:59:03 -08001254 })
Sam Delmerico82602492022-06-10 17:05:42 +00001255
1256 if proptools.Bool(a.properties.Extract_jni) {
1257 for _, t := range ctx.MultiTargets() {
1258 arch := t.Arch.Abi[0]
1259 path := android.PathForModuleOut(ctx, arch+"_jni.zip")
1260 a.jniPackages = append(a.jniPackages, path)
1261
1262 outDir := android.PathForModuleOut(ctx, "aarForJni")
1263 aarPath := android.PathForModuleSrc(ctx, a.properties.Aars[0])
1264 ctx.Build(pctx, android.BuildParams{
1265 Rule: extractJNI,
1266 Input: aarPath,
1267 Outputs: android.WritablePaths{path},
1268 Description: "extract JNI from AAR",
1269 Args: map[string]string{
1270 "outDir": outDir.String(),
1271 "archString": arch,
1272 },
1273 })
1274 }
Sam Delmerico82602492022-06-10 17:05:42 +00001275 }
Colin Crosse8eeec92023-12-14 14:50:05 -08001276
Colin Cross40213022023-12-13 15:19:49 -08001277 android.SetProvider(ctx, JniPackageProvider, JniPackageInfo{
Colin Crosse8eeec92023-12-14 14:50:05 -08001278 JniPackages: a.jniPackages,
1279 })
LaMont Jonesafe7baf2024-01-09 22:47:39 +00001280 android.CollectDependencyAconfigFiles(ctx, &a.mergedAconfigFiles)
Colin Crossdcf71b22021-02-01 13:59:03 -08001281}
Colin Crossfabb6082018-02-20 17:22:23 -08001282
1283func (a *AARImport) HeaderJars() android.Paths {
1284 return android.Paths{a.classpathFile}
1285}
1286
Colin Cross331a1212018-08-15 20:40:52 -07001287func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
1288 return android.Paths{a.classpathFile}
1289}
1290
Spandan Das59a4a2b2024-01-09 21:35:56 +00001291func (a *AARImport) DexJarBuildPath(ctx android.ModuleErrorfContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -08001292 return nil
1293}
1294
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001295func (a *AARImport) DexJarInstallPath() android.Path {
1296 return nil
1297}
1298
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001299func (a *AARImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
Jiyong Park1be96912018-05-28 18:02:19 +09001300 return nil
1301}
1302
Jiyong Park45bf82e2020-12-15 22:29:02 +09001303var _ android.ApexModule = (*AARImport)(nil)
1304
1305// Implements android.ApexModule
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09001306func (a *AARImport) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1307 return a.depIsInSameApex(ctx, dep)
1308}
1309
Jiyong Park45bf82e2020-12-15 22:29:02 +09001310// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001311func (g *AARImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1312 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001313 return nil
1314}
1315
Sam Delmericoaf8bb702022-07-25 15:39:32 -04001316var _ android.PrebuiltInterface = (*AARImport)(nil)
Colin Crossfabb6082018-02-20 17:22:23 -08001317
Colin Cross1b16b0e2019-02-12 14:41:32 -08001318// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
1319//
1320// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
1321// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -08001322func AARImportFactory() android.Module {
1323 module := &AARImport{}
1324
1325 module.AddProperties(&module.properties)
1326
1327 android.InitPrebuiltModule(module, &module.properties.Aars)
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09001328 android.InitApexModule(module)
Sam Delmerico82602492022-06-10 17:05:42 +00001329 InitJavaModuleMultiTargets(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -08001330 return module
1331}