blob: 4a6202484150a39921e594f4c7ea192630e3bdb3 [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"
Colin Crossfabb6082018-02-20 17:22:23 -080026 "github.com/google/blueprint"
Colin Crossa97c5d32018-03-28 14:58:31 -070027 "github.com/google/blueprint/proptools"
Colin Crossfabb6082018-02-20 17:22:23 -080028)
29
Colin Crossa97c5d32018-03-28 14:58:31 -070030type AndroidLibraryDependency interface {
Colin Crossa97c5d32018-03-28 14:58:31 -070031 ExportPackage() android.Path
Colin Crossab8d1382023-07-14 17:23:41 +000032 ResourcesNodeDepSet() *android.DepSet[*resourcesNode]
33 RRODirsDepSet() *android.DepSet[rroDir]
34 ManifestsDepSet() *android.DepSet[android.Path]
Jaewoong Jungc779cd42020-10-06 18:56:10 -070035 SetRROEnforcedForDependent(enforce bool)
36 IsRROEnforced(ctx android.BaseModuleContext) bool
Colin Crossa97c5d32018-03-28 14:58:31 -070037}
38
39func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000040 RegisterAARBuildComponents(android.InitRegistrationContext)
41}
42
43func RegisterAARBuildComponents(ctx android.RegistrationContext) {
44 ctx.RegisterModuleType("android_library_import", AARImportFactory)
45 ctx.RegisterModuleType("android_library", AndroidLibraryFactory)
Paul Duffin04ba70d2021-03-22 13:56:43 +000046 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
Colin Cross7e6a9012024-01-17 14:58:38 -080047 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator)
Paul Duffin04ba70d2021-03-22 13:56:43 +000048 })
Colin Crossa97c5d32018-03-28 14:58:31 -070049}
50
51//
52// AAR (android library)
53//
54
55type androidLibraryProperties struct {
56 BuildAAR bool `blueprint:"mutated"`
57}
58
59type aaptProperties struct {
60 // flags passed to aapt when creating the apk
61 Aaptflags []string
62
Dan Willemsen72be5902018-10-24 20:24:57 -070063 // include all resource configurations, not just the product-configured
64 // ones.
65 Aapt_include_all_resources *bool
66
Jiakai Zhangba82e282023-10-13 18:08:59 +010067 // list of files to use as assets.
68 Assets []string `android:"path"`
69
Colin Crossa97c5d32018-03-28 14:58:31 -070070 // list of directories relative to the Blueprints file containing assets.
Colin Cross0ddae7f2019-02-07 15:30:01 -080071 // Defaults to ["assets"] if a directory called assets exists. Set to []
72 // to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070073 Asset_dirs []string
74
75 // list of directories relative to the Blueprints file containing
Colin Cross0ddae7f2019-02-07 15:30:01 -080076 // Android resources. Defaults to ["res"] if a directory called res exists.
77 // Set to [] to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070078 Resource_dirs []string
79
Colin Crossa592e3e2019-02-19 16:59:53 -080080 // list of zip files containing Android resources.
Colin Cross27b922f2019-03-04 22:35:41 -080081 Resource_zips []string `android:"path"`
Colin Crossa592e3e2019-02-19 16:59:53 -080082
Colin Crossa97c5d32018-03-28 14:58:31 -070083 // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml".
Colin Cross27b922f2019-03-04 22:35:41 -080084 Manifest *string `android:"path"`
changho.shinb5432b72019-08-08 18:37:17 +090085
86 // paths to additional manifest files to merge with main manifest.
87 Additional_manifests []string `android:"path"`
Sasha Smundak541056c2019-10-28 15:50:06 -070088
89 // do not include AndroidManifest from dependent libraries
90 Dont_merge_manifests *bool
Jaewoong Jungc779cd42020-10-06 18:56:10 -070091
Colin Cross039d8df2023-06-20 22:40:02 -070092 // If use_resource_processor is set, use Bazel's resource processor instead of aapt2 to generate R.class files.
93 // The resource processor produces more optimal R.class files that only list resources in the package of the
94 // library that provided them, as opposed to aapt2 which produces R.java files for every package containing
95 // every resource. Using the resource processor can provide significant build time speedups, but requires
96 // fixing the module to use the correct package to reference each resource, and to avoid having any other
97 // libraries in the tree that use the same package name. Defaults to false, but will default to true in the
98 // future.
99 Use_resource_processor *bool
100
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700101 // true if RRO is enforced for any of the dependent modules
102 RROEnforcedForDependent bool `blueprint:"mutated"`
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900103
104 // Filter only specified product and ignore other products
105 Filter_product *string `blueprint:"mutated"`
Colin Crossa97c5d32018-03-28 14:58:31 -0700106}
107
108type aapt struct {
Colin Cross312634e2023-11-21 15:13:56 -0800109 aaptSrcJar android.Path
110 transitiveAaptRJars android.Paths
111 transitiveAaptResourcePackagesFile android.Path
112 exportPackage android.Path
113 manifestPath android.Path
114 proguardOptionsFile android.Path
115 rTxt android.Path
116 rJar android.Path
117 extraAaptPackagesFile android.Path
118 mergedManifestFile android.Path
119 noticeFile android.OptionalPath
120 assetPackage android.OptionalPath
121 isLibrary bool
122 defaultManifestVersion string
123 useEmbeddedNativeLibs bool
124 useEmbeddedDex bool
125 usesNonSdkApis bool
126 hasNoCode bool
127 LoggingParent string
128 resourceFiles android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700129
Colin Crosse560c4a2019-03-19 16:03:11 -0700130 splitNames []string
131 splits []split
132
Colin Crossa97c5d32018-03-28 14:58:31 -0700133 aaptProperties aaptProperties
Colin Crossab8d1382023-07-14 17:23:41 +0000134
135 resourcesNodesDepSet *android.DepSet[*resourcesNode]
136 rroDirsDepSet *android.DepSet[rroDir]
137 manifestsDepSet *android.DepSet[android.Path]
Alix96ea88452023-08-31 15:48:23 +0000138
139 manifestValues struct {
140 applicationId string
141 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700142}
143
Colin Crosse560c4a2019-03-19 16:03:11 -0700144type split struct {
145 name string
146 suffix string
147 path android.Path
148}
149
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700150// Propagate RRO enforcement flag to static lib dependencies transitively.
151func propagateRROEnforcementMutator(ctx android.TopDownMutatorContext) {
152 m := ctx.Module()
153 if d, ok := m.(AndroidLibraryDependency); ok && d.IsRROEnforced(ctx) {
154 ctx.VisitDirectDepsWithTag(staticLibTag, func(d android.Module) {
155 if a, ok := d.(AndroidLibraryDependency); ok {
156 a.SetRROEnforcedForDependent(true)
157 }
158 })
159 }
160}
161
Colin Cross8f1b0332024-01-25 13:39:06 -0800162func (a *aapt) useResourceProcessorBusyBox(ctx android.BaseModuleContext) bool {
163 return BoolDefault(a.aaptProperties.Use_resource_processor, ctx.Config().UseResourceProcessorByDefault())
Colin Cross039d8df2023-06-20 22:40:02 -0700164}
165
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900166func (a *aapt) filterProduct() string {
167 return String(a.aaptProperties.Filter_product)
168}
169
Colin Crossa97c5d32018-03-28 14:58:31 -0700170func (a *aapt) ExportPackage() android.Path {
171 return a.exportPackage
172}
Colin Crossab8d1382023-07-14 17:23:41 +0000173func (a *aapt) ResourcesNodeDepSet() *android.DepSet[*resourcesNode] {
174 return a.resourcesNodesDepSet
Colin Crossc1c37552019-01-31 11:42:41 -0800175}
176
Colin Crossab8d1382023-07-14 17:23:41 +0000177func (a *aapt) RRODirsDepSet() *android.DepSet[rroDir] {
178 return a.rroDirsDepSet
Colin Crossc1c37552019-01-31 11:42:41 -0800179}
180
Colin Crossab8d1382023-07-14 17:23:41 +0000181func (a *aapt) ManifestsDepSet() *android.DepSet[android.Path] {
182 return a.manifestsDepSet
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800183}
184
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700185func (a *aapt) SetRROEnforcedForDependent(enforce bool) {
186 a.aaptProperties.RROEnforcedForDependent = enforce
187}
188
189func (a *aapt) IsRROEnforced(ctx android.BaseModuleContext) bool {
190 // True if RRO is enforced for this module or...
191 return ctx.Config().EnforceRROForModule(ctx.ModuleName()) ||
Jeongik Chacee5ba92021-02-19 12:11:51 +0900192 // if RRO is enforced for any of its dependents.
193 a.aaptProperties.RROEnforcedForDependent
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700194}
195
Jiyong Parkf1691d22021-03-29 20:11:58 +0900196func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext android.SdkContext,
Colin Crossa0ba2f52019-06-22 12:59:27 -0700197 manifestPath android.Path) (compileFlags, linkFlags []string, linkDeps android.Paths,
198 resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700199
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800200 hasVersionCode := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-code")
201 hasVersionName := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-name")
Colin Crossa97c5d32018-03-28 14:58:31 -0700202
Colin Crossa97c5d32018-03-28 14:58:31 -0700203 // Flags specified in Android.bp
204 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
205
Eric Miao40eab202023-03-30 16:57:17 +0000206 linkFlags = append(linkFlags, "--enable-compact-entries")
Colin Crossa97c5d32018-03-28 14:58:31 -0700207
208 // Find implicit or explicit asset and resource dirs
Jiakai Zhangba82e282023-10-13 18:08:59 +0100209 assets := android.PathsRelativeToModuleSourceDir(android.SourceInput{
210 Context: ctx,
211 Paths: a.aaptProperties.Assets,
212 IncludeDirs: false,
213 })
Colin Crossa97c5d32018-03-28 14:58:31 -0700214 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
215 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
Colin Cross8a497952019-03-05 22:25:09 -0800216 resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips)
Colin Crossa97c5d32018-03-28 14:58:31 -0700217
Colin Crossa97c5d32018-03-28 14:58:31 -0700218 // Glob directories into lists of paths
219 for _, dir := range resourceDirs {
220 resDirs = append(resDirs, globbedResourceDir{
221 dir: dir,
222 files: androidResourceGlob(ctx, dir),
223 })
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700224 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, a, dir)
Colin Crossa97c5d32018-03-28 14:58:31 -0700225 overlayDirs = append(overlayDirs, resOverlayDirs...)
226 rroDirs = append(rroDirs, resRRODirs...)
227 }
228
Colin Crossc20dc852020-11-10 12:27:45 -0800229 var assetDeps android.Paths
230 for i, dir := range assetDirs {
231 // Add a dependency on every file in the asset directory. This ensures the aapt2
232 // rule will be rerun if one of the files in the asset directory is modified.
233 assetDeps = append(assetDeps, androidResourceGlob(ctx, dir)...)
234
235 // Add a dependency on a file that contains a list of all the files in the asset directory.
236 // This ensures the aapt2 rule will be run if a file is removed from the asset directory,
237 // or a file is added whose timestamp is older than the output of aapt2.
238 assetFileListFile := android.PathForModuleOut(ctx, "asset_dir_globs", strconv.Itoa(i)+".glob")
239 androidResourceGlobList(ctx, dir, assetFileListFile)
240 assetDeps = append(assetDeps, assetFileListFile)
Colin Crossa97c5d32018-03-28 14:58:31 -0700241 }
242
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700243 assetDirStrings := assetDirs.Strings()
244 if a.noticeFile.Valid() {
245 assetDirStrings = append(assetDirStrings, filepath.Dir(a.noticeFile.Path().String()))
Colin Crossc20dc852020-11-10 12:27:45 -0800246 assetDeps = append(assetDeps, a.noticeFile.Path())
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700247 }
Jiakai Zhangba82e282023-10-13 18:08:59 +0100248 if len(assets) > 0 {
249 // aapt2 doesn't support adding individual asset files. Create a temp directory to hold asset
250 // files and pass it to aapt2.
251 tmpAssetDir := android.PathForModuleOut(ctx, "tmp_asset_dir")
252
253 rule := android.NewRuleBuilder(pctx, ctx)
254 rule.Command().
255 Text("rm -rf").Text(tmpAssetDir.String()).
256 Text("&&").
257 Text("mkdir -p").Text(tmpAssetDir.String())
258
259 for _, asset := range assets {
260 output := tmpAssetDir.Join(ctx, asset.Rel())
261 assetDeps = append(assetDeps, output)
262 rule.Command().Text("mkdir -p").Text(filepath.Dir(output.String()))
263 rule.Command().Text("cp").Input(asset).Output(output)
264 }
265
266 rule.Build("tmp_asset_dir", "tmp_asset_dir")
267
268 assetDirStrings = append(assetDirStrings, tmpAssetDir.String())
269 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700270
Colin Crossa97c5d32018-03-28 14:58:31 -0700271 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
272 linkDeps = append(linkDeps, manifestPath)
273
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700274 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirStrings, "-A "))
Colin Crossc20dc852020-11-10 12:27:45 -0800275 linkDeps = append(linkDeps, assetDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700276
Spandan Das50885c02023-02-23 21:31:33 +0000277 // Returns the effective version for {min|target}_sdk_version
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000278 effectiveVersionString := func(sdkVersion android.SdkSpec, minSdkVersion android.ApiLevel) string {
Spandan Das50885c02023-02-23 21:31:33 +0000279 // If {min|target}_sdk_version is current, use sdk_version to determine the effective level
280 // This is necessary for vendor modules.
281 // The effective version does not _only_ depend on {min|target}_sdk_version(level),
282 // but also on the sdk_version (kind+level)
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000283 if minSdkVersion.IsCurrent() {
Spandan Das50885c02023-02-23 21:31:33 +0000284 ret, err := sdkVersion.EffectiveVersionString(ctx)
285 if err != nil {
286 ctx.ModuleErrorf("invalid sdk_version: %s", err)
287 }
288 return ret
289 }
290 ret, err := minSdkVersion.EffectiveVersionString(ctx)
291 if err != nil {
292 ctx.ModuleErrorf("invalid min_sdk_version: %s", err)
293 }
294 return ret
Jiyong Park6a927c42020-01-21 02:03:43 +0900295 }
Spandan Das50885c02023-02-23 21:31:33 +0000296 // SDK version flags
297 sdkVersion := sdkContext.SdkVersion(ctx)
298 minSdkVersion := effectiveVersionString(sdkVersion, sdkContext.MinSdkVersion(ctx))
Colin Crossa97c5d32018-03-28 14:58:31 -0700299
Colin Cross83bb3162018-06-25 15:48:06 -0700300 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
Spandan Das6450b552023-02-23 19:27:07 +0000301 // Use minSdkVersion for target-sdk-version, even if `target_sdk_version` is set
302 // This behavior has been copied from Make.
Colin Cross83bb3162018-06-25 15:48:06 -0700303 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700304
Colin Crossa97c5d32018-03-28 14:58:31 -0700305 // Version code
306 if !hasVersionCode {
Dan Albert4f378d72020-07-23 17:32:15 -0700307 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion().String())
Colin Crossa97c5d32018-03-28 14:58:31 -0700308 }
309
310 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700311 var versionName string
312 if ctx.ModuleName() == "framework-res" {
313 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
314 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700315 // if it contains the build number. Use the PlatformVersionName instead.
316 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700317 } else {
318 versionName = ctx.Config().AppsDefaultVersionName()
319 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800320 versionName = proptools.NinjaEscape(versionName)
Colin Crossa97c5d32018-03-28 14:58:31 -0700321 linkFlags = append(linkFlags, "--version-name ", versionName)
322 }
323
Colin Crossa0ba2f52019-06-22 12:59:27 -0700324 linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"})
325
326 // Always set --pseudo-localize, it will be stripped out later for release
327 // builds that don't want it.
328 compileFlags = append(compileFlags, "--pseudo-localize")
329
330 return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
Colin Crossa97c5d32018-03-28 14:58:31 -0700331}
332
Paul Duffin250e6192019-06-07 10:44:37 +0100333func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) {
Colin Cross42308aa2018-11-14 21:44:17 -0800334 if sdkDep.frameworkResModule != "" {
335 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700336 }
337}
338
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800339var extractAssetsRule = pctx.AndroidStaticRule("extractAssets",
340 blueprint.RuleParams{
341 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "assets/**/*"`,
342 CommandDeps: []string{"${config.Zip2ZipCmd}"},
343 })
344
Alixf7a10272023-09-27 16:47:56 +0000345type aaptBuildActionOptions struct {
346 sdkContext android.SdkContext
347 classLoaderContexts dexpreopt.ClassLoaderContextMap
348 excludedLibs []string
349 enforceDefaultTargetSdkVersion bool
350 extraLinkFlags []string
Jihoon Kang84b25892023-12-01 22:01:06 +0000351 aconfigTextFiles android.Paths
Alixf7a10272023-09-27 16:47:56 +0000352}
353
354func (a *aapt) buildActions(ctx android.ModuleContext, opts aaptBuildActionOptions) {
Colin Cross5446e882019-05-22 10:46:27 -0700355
Colin Cross8676c8c2023-10-12 15:58:57 -0700356 staticResourcesNodesDepSet, sharedResourcesNodesDepSet, staticRRODirsDepSet, staticManifestsDepSet, sharedExportPackages, libFlags :=
Alixf7a10272023-09-27 16:47:56 +0000357 aaptLibs(ctx, opts.sdkContext, opts.classLoaderContexts)
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100358
Paul Duffin06530572022-02-03 17:54:15 +0000359 // Exclude any libraries from the supplied list.
Alixf7a10272023-09-27 16:47:56 +0000360 opts.classLoaderContexts = opts.classLoaderContexts.ExcludeLibs(opts.excludedLibs)
Paul Duffin06530572022-02-03 17:54:15 +0000361
Colin Cross31656952018-05-24 16:11:20 -0700362 // App manifest file
363 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
364 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
365
Gurpreet Singh7deabfa2022-02-10 13:28:35 +0000366 manifestPath := ManifestFixer(ctx, manifestSrcPath, ManifestFixerParams{
Alixf7a10272023-09-27 16:47:56 +0000367 SdkContext: opts.sdkContext,
368 ClassLoaderContexts: opts.classLoaderContexts,
Harshit Mahajan5b8b7302022-06-10 11:24:05 +0000369 IsLibrary: a.isLibrary,
370 DefaultManifestVersion: a.defaultManifestVersion,
371 UseEmbeddedNativeLibs: a.useEmbeddedNativeLibs,
372 UsesNonSdkApis: a.usesNonSdkApis,
373 UseEmbeddedDex: a.useEmbeddedDex,
374 HasNoCode: a.hasNoCode,
375 LoggingParent: a.LoggingParent,
Alixf7a10272023-09-27 16:47:56 +0000376 EnforceDefaultTargetSdkVersion: opts.enforceDefaultTargetSdkVersion,
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000377 })
Colin Cross90c25c62019-04-19 16:22:57 -0700378
Colin Crossab8d1382023-07-14 17:23:41 +0000379 staticDeps := transitiveAarDeps(staticResourcesNodesDepSet.ToList())
Colin Cross8676c8c2023-10-12 15:58:57 -0700380 sharedDeps := transitiveAarDeps(sharedResourcesNodesDepSet.ToList())
Colin Crossab8d1382023-07-14 17:23:41 +0000381
Luca Stefanifd898822019-09-10 22:13:31 +0200382 // Add additional manifest files to transitive manifests.
383 additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)
Colin Crossab8d1382023-07-14 17:23:41 +0000384 transitiveManifestPaths := append(android.Paths{manifestPath}, additionalManifests...)
385 // TODO(b/288358614): Soong has historically not merged manifests from dependencies of android_library_import
386 // modules. Merging manifests from dependencies could remove the need for pom2bp to generate the "-nodeps" copies
387 // of androidx libraries, but doing so triggers errors due to errors introduced by existing dependencies of
388 // android_library_import modules. If this is fixed, staticManifestsDepSet can be dropped completely in favor of
389 // staticResourcesNodesDepSet.manifests()
390 transitiveManifestPaths = append(transitiveManifestPaths, staticManifestsDepSet.ToList()...)
Colin Cross90c25c62019-04-19 16:22:57 -0700391
Colin Crossab8d1382023-07-14 17:23:41 +0000392 if len(transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) {
Alixf7a10272023-09-27 16:47:56 +0000393 manifestMergerParams := ManifestMergerParams{
394 staticLibManifests: transitiveManifestPaths[1:],
Alix96ea88452023-08-31 15:48:23 +0000395 isLibrary: a.isLibrary,
396 packageName: a.manifestValues.applicationId,
397 }
Alixf7a10272023-09-27 16:47:56 +0000398 a.mergedManifestFile = manifestMerger(ctx, transitiveManifestPaths[0], manifestMergerParams)
Colin Cross90c25c62019-04-19 16:22:57 -0700399 if !a.isLibrary {
400 // Only use the merged manifest for applications. For libraries, the transitive closure of manifests
401 // will be propagated to the final application and merged there. The merged manifest for libraries is
402 // only passed to Make, which can't handle transitive dependencies.
403 manifestPath = a.mergedManifestFile
404 }
405 } else {
406 a.mergedManifestFile = manifestPath
407 }
Colin Cross31656952018-05-24 16:11:20 -0700408
Alixf7a10272023-09-27 16:47:56 +0000409 compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, opts.sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700410
411 linkFlags = append(linkFlags, libFlags...)
Colin Cross8676c8c2023-10-12 15:58:57 -0700412 linkDeps = append(linkDeps, sharedExportPackages...)
Colin Crossab8d1382023-07-14 17:23:41 +0000413 linkDeps = append(linkDeps, staticDeps.resPackages()...)
Alixf7a10272023-09-27 16:47:56 +0000414 linkFlags = append(linkFlags, opts.extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700415 if a.isLibrary {
416 linkFlags = append(linkFlags, "--static-lib")
417 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700418
Colin Cross8f1b0332024-01-25 13:39:06 -0800419 if a.isLibrary && a.useResourceProcessorBusyBox(ctx) {
Colin Cross039d8df2023-06-20 22:40:02 -0700420 // When building an android_library using ResourceProcessorBusyBox the resources are merged into
421 // package-res.apk with --merge-only, but --no-static-lib-packages is not used so that R.txt only
422 // contains resources from this library.
423 linkFlags = append(linkFlags, "--merge-only")
424 } else {
425 // When building and app or when building an android_library without ResourceProcessorBusyBox
426 // --no-static-lib-packages is used to put all the resources into the app. If ResourceProcessorBusyBox
427 // is used then the app's R.txt will be post-processed along with the R.txt files from dependencies to
428 // sort resources into the right packages in R.class.
429 linkFlags = append(linkFlags, "--no-static-lib-packages")
430 }
431
Colin Crossa97c5d32018-03-28 14:58:31 -0700432 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
Colin Crossa97c5d32018-03-28 14:58:31 -0700433 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
434 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700435 // This file isn't used by Soong, but is generated for exporting
436 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Cross039d8df2023-06-20 22:40:02 -0700437 var transitiveRJars android.Paths
Colin Crossf3b7bad2023-08-02 15:49:00 -0700438 var srcJar android.WritablePath
Colin Crossa97c5d32018-03-28 14:58:31 -0700439
Colin Cross4aaa84a2018-08-21 15:14:37 -0700440 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700441 for _, dir := range resDirs {
Colin Cross014489c2020-06-02 20:09:13 -0700442 a.resourceFiles = append(a.resourceFiles, dir.files...)
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900443 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags, a.filterProduct()).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700444 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700445
Colin Crossa592e3e2019-02-19 16:59:53 -0800446 for i, zip := range resZips {
447 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
Colin Crossa0ba2f52019-06-22 12:59:27 -0700448 aapt2CompileZip(ctx, flata, zip, "", compileFlags)
Colin Crossa592e3e2019-02-19 16:59:53 -0800449 compiledResDirs = append(compiledResDirs, android.Paths{flata})
450 }
451
Colin Cross4aaa84a2018-08-21 15:14:37 -0700452 var compiledRes, compiledOverlay android.Paths
453
Colin Crossab8d1382023-07-14 17:23:41 +0000454 // AAPT2 overlays are in lowest to highest priority order, reverse the topological order
455 // of transitiveStaticLibs.
456 transitiveStaticLibs := android.ReversePaths(staticDeps.resPackages())
457
Colin Cross8f1b0332024-01-25 13:39:06 -0800458 if a.isLibrary && a.useResourceProcessorBusyBox(ctx) {
Colin Cross039d8df2023-06-20 22:40:02 -0700459 // When building an android_library with ResourceProcessorBusyBox enabled treat static library dependencies
460 // as imports. The resources from dependencies will not be merged into this module's package-res.apk, and
461 // instead modules depending on this module will reference package-res.apk from all transitive static
462 // dependencies.
463 for _, staticDep := range staticDeps {
464 linkDeps = append(linkDeps, staticDep.resPackage)
465 linkFlags = append(linkFlags, "-I "+staticDep.resPackage.String())
466 if staticDep.usedResourceProcessor {
467 transitiveRJars = append(transitiveRJars, staticDep.rJar)
468 }
469 }
Colin Cross8676c8c2023-10-12 15:58:57 -0700470 for _, sharedDep := range sharedDeps {
471 if sharedDep.usedResourceProcessor {
472 transitiveRJars = append(transitiveRJars, sharedDep.rJar)
473 }
474 }
Colin Cross039d8df2023-06-20 22:40:02 -0700475 } else {
476 // When building an app or building a library without ResourceProcessorBusyBox enabled all static
477 // dependencies are compiled into this module's package-res.apk as overlays.
478 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
479 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700480
Colin Crossbec85302019-02-13 13:15:46 -0800481 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700482 // If we are using static android libraries, every source file becomes an overlay.
483 // This is to emulate old AAPT behavior which simulated library support.
484 for _, compiledResDir := range compiledResDirs {
485 compiledOverlay = append(compiledOverlay, compiledResDir...)
486 }
Colin Crossbec85302019-02-13 13:15:46 -0800487 } else if a.isLibrary {
488 // Otherwise, for a static library we treat all the resources equally with no overlay.
489 for _, compiledResDir := range compiledResDirs {
490 compiledRes = append(compiledRes, compiledResDir...)
491 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700492 } else if len(compiledResDirs) > 0 {
493 // Without static libraries, the first directory is our directory, which can then be
494 // overlaid by the rest.
495 compiledRes = append(compiledRes, compiledResDirs[0]...)
496 for _, compiledResDir := range compiledResDirs[1:] {
497 compiledOverlay = append(compiledOverlay, compiledResDir...)
498 }
499 }
500
Colin Crossa97c5d32018-03-28 14:58:31 -0700501 for _, dir := range overlayDirs {
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900502 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags, a.filterProduct()).Paths()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700503 }
504
Colin Crosse560c4a2019-03-19 16:03:11 -0700505 var splitPackages android.WritablePaths
506 var splits []split
507
508 for _, s := range a.splitNames {
509 suffix := strings.Replace(s, ",", "_", -1)
510 path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
511 linkFlags = append(linkFlags, "--split", path.String()+":"+s)
512 splitPackages = append(splitPackages, path)
513 splits = append(splits, split{
514 name: s,
515 suffix: suffix,
516 path: path,
517 })
518 }
519
Colin Cross8f1b0332024-01-25 13:39:06 -0800520 if !a.useResourceProcessorBusyBox(ctx) {
Colin Crossf3b7bad2023-08-02 15:49:00 -0700521 // the subdir "android" is required to be filtered by package names
522 srcJar = android.PathForModuleGen(ctx, "android", "R.srcjar")
523 }
524
Colin Crossab8d1382023-07-14 17:23:41 +0000525 // No need to specify assets from dependencies to aapt2Link for libraries, all transitive assets will be
526 // provided to the final app aapt2Link step.
527 var transitiveAssets android.Paths
528 if !a.isLibrary {
529 transitiveAssets = android.ReverseSliceInPlace(staticDeps.assets())
530 }
Colin Crossf3b7bad2023-08-02 15:49:00 -0700531 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt,
Jihoon Kang84b25892023-12-01 22:01:06 +0000532 linkFlags, linkDeps, compiledRes, compiledOverlay, transitiveAssets, splitPackages,
533 opts.aconfigTextFiles)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800534 // Extract assets from the resource package output so that they can be used later in aapt2link
535 // for modules that depend on this one.
Colin Crossab8d1382023-07-14 17:23:41 +0000536 if android.PrefixInList(linkFlags, "-A ") {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800537 assets := android.PathForModuleOut(ctx, "assets.zip")
538 ctx.Build(pctx, android.BuildParams{
539 Rule: extractAssetsRule,
540 Input: packageRes,
541 Output: assets,
542 Description: "extract assets from built resource file",
543 })
544 a.assetPackage = android.OptionalPathForPath(assets)
545 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700546
Colin Cross8f1b0332024-01-25 13:39:06 -0800547 if a.useResourceProcessorBusyBox(ctx) {
Colin Cross039d8df2023-06-20 22:40:02 -0700548 rJar := android.PathForModuleOut(ctx, "busybox/R.jar")
Colin Crossd3f7d1a2024-01-03 19:42:25 -0800549 resourceProcessorBusyBoxGenerateBinaryR(ctx, rTxt, a.mergedManifestFile, rJar, staticDeps, a.isLibrary, a.aaptProperties.Aaptflags)
Colin Crossf3b7bad2023-08-02 15:49:00 -0700550 aapt2ExtractExtraPackages(ctx, extraPackages, rJar)
Colin Cross039d8df2023-06-20 22:40:02 -0700551 transitiveRJars = append(transitiveRJars, rJar)
552 a.rJar = rJar
Colin Crossf3b7bad2023-08-02 15:49:00 -0700553 } else {
554 aapt2ExtractExtraPackages(ctx, extraPackages, srcJar)
Colin Cross039d8df2023-06-20 22:40:02 -0700555 }
556
Colin Cross312634e2023-11-21 15:13:56 -0800557 transitiveAaptResourcePackages := staticDeps.resPackages().Strings()
558 transitiveAaptResourcePackages = slices.DeleteFunc(transitiveAaptResourcePackages, func(p string) bool {
559 return p == packageRes.String()
560 })
561 transitiveAaptResourcePackagesFile := android.PathForModuleOut(ctx, "transitive-res-packages")
562 android.WriteFileRule(ctx, transitiveAaptResourcePackagesFile, strings.Join(transitiveAaptResourcePackages, "\n"))
563
Colin Crossa97c5d32018-03-28 14:58:31 -0700564 a.aaptSrcJar = srcJar
Colin Cross039d8df2023-06-20 22:40:02 -0700565 a.transitiveAaptRJars = transitiveRJars
Colin Cross312634e2023-11-21 15:13:56 -0800566 a.transitiveAaptResourcePackagesFile = transitiveAaptResourcePackagesFile
Colin Crossa97c5d32018-03-28 14:58:31 -0700567 a.exportPackage = packageRes
568 a.manifestPath = manifestPath
569 a.proguardOptionsFile = proguardOptionsFile
Colin Cross66f78822018-05-02 12:58:28 -0700570 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700571 a.rTxt = rTxt
Colin Crosse560c4a2019-03-19 16:03:11 -0700572 a.splits = splits
Colin Crossab8d1382023-07-14 17:23:41 +0000573 a.resourcesNodesDepSet = android.NewDepSetBuilder[*resourcesNode](android.TOPOLOGICAL).
574 Direct(&resourcesNode{
575 resPackage: a.exportPackage,
576 manifest: a.manifestPath,
577 additionalManifests: additionalManifests,
Colin Cross039d8df2023-06-20 22:40:02 -0700578 rTxt: a.rTxt,
579 rJar: a.rJar,
Colin Crossab8d1382023-07-14 17:23:41 +0000580 assets: a.assetPackage,
Colin Cross039d8df2023-06-20 22:40:02 -0700581
Colin Cross8f1b0332024-01-25 13:39:06 -0800582 usedResourceProcessor: a.useResourceProcessorBusyBox(ctx),
Colin Crossab8d1382023-07-14 17:23:41 +0000583 }).
584 Transitive(staticResourcesNodesDepSet).Build()
585 a.rroDirsDepSet = android.NewDepSetBuilder[rroDir](android.TOPOLOGICAL).
586 Direct(rroDirs...).
587 Transitive(staticRRODirsDepSet).Build()
588 a.manifestsDepSet = android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).
589 Direct(a.manifestPath).
590 DirectSlice(additionalManifests).
591 Transitive(staticManifestsDepSet).Build()
592}
593
Colin Cross039d8df2023-06-20 22:40:02 -0700594var resourceProcessorBusyBox = pctx.AndroidStaticRule("resourceProcessorBusyBox",
595 blueprint.RuleParams{
596 Command: "${config.JavaCmd} -cp ${config.ResourceProcessorBusyBox} " +
597 "com.google.devtools.build.android.ResourceProcessorBusyBox --tool=GENERATE_BINARY_R -- @${out}.args && " +
598 "if cmp -s ${out}.tmp ${out} ; then rm ${out}.tmp ; else mv ${out}.tmp ${out}; fi",
599 CommandDeps: []string{"${config.ResourceProcessorBusyBox}"},
600 Rspfile: "${out}.args",
601 RspfileContent: "--primaryRTxt ${rTxt} --primaryManifest ${manifest} --classJarOutput ${out}.tmp ${args}",
602 Restat: true,
603 }, "rTxt", "manifest", "args")
604
605// resourceProcessorBusyBoxGenerateBinaryR converts the R.txt file produced by aapt2 into R.class files
606// using Bazel's ResourceProcessorBusyBox tool, which is faster than compiling the R.java files and
607// supports producing classes for static dependencies that only include resources from that dependency.
608func resourceProcessorBusyBoxGenerateBinaryR(ctx android.ModuleContext, rTxt, manifest android.Path,
Colin Crossd3f7d1a2024-01-03 19:42:25 -0800609 rJar android.WritablePath, transitiveDeps transitiveAarDeps, isLibrary bool, aaptFlags []string) {
Colin Cross039d8df2023-06-20 22:40:02 -0700610
611 var args []string
612 var deps android.Paths
613
614 if !isLibrary {
615 // When compiling an app, pass all R.txt and AndroidManifest.xml from transitive static library dependencies
616 // to ResourceProcessorBusyBox so that it can regenerate R.class files with the final resource IDs for each
617 // package.
618 args, deps = transitiveDeps.resourceProcessorDeps()
619 } else {
620 // When compiling a library don't pass any dependencies as it only needs to generate an R.class file for this
621 // library. Pass --finalFields=false so that the R.class file contains non-final fields so they don't get
622 // inlined into the library before the final IDs are assigned during app compilation.
623 args = append(args, "--finalFields=false")
624 }
625
Colin Crossd3f7d1a2024-01-03 19:42:25 -0800626 for i, arg := range aaptFlags {
627 const AAPT_CUSTOM_PACKAGE = "--custom-package"
628 if strings.HasPrefix(arg, AAPT_CUSTOM_PACKAGE) {
629 pkg := strings.TrimSpace(strings.TrimPrefix(arg, AAPT_CUSTOM_PACKAGE))
630 if pkg == "" && i+1 < len(aaptFlags) {
631 pkg = aaptFlags[i+1]
632 }
633 args = append(args, "--packageForR "+pkg)
634 }
635 }
636
Colin Cross039d8df2023-06-20 22:40:02 -0700637 deps = append(deps, rTxt, manifest)
638
639 ctx.Build(pctx, android.BuildParams{
640 Rule: resourceProcessorBusyBox,
641 Output: rJar,
642 Implicits: deps,
643 Description: "ResourceProcessorBusyBox",
644 Args: map[string]string{
645 "rTxt": rTxt.String(),
646 "manifest": manifest.String(),
647 "args": strings.Join(args, " "),
648 },
649 })
650}
651
Colin Crossab8d1382023-07-14 17:23:41 +0000652type resourcesNode struct {
653 resPackage android.Path
654 manifest android.Path
655 additionalManifests android.Paths
Colin Cross039d8df2023-06-20 22:40:02 -0700656 rTxt android.Path
657 rJar android.Path
Colin Crossab8d1382023-07-14 17:23:41 +0000658 assets android.OptionalPath
Colin Cross039d8df2023-06-20 22:40:02 -0700659
660 usedResourceProcessor bool
Colin Crossab8d1382023-07-14 17:23:41 +0000661}
662
663type transitiveAarDeps []*resourcesNode
664
665func (t transitiveAarDeps) resPackages() android.Paths {
Colin Cross039d8df2023-06-20 22:40:02 -0700666 paths := make(android.Paths, 0, len(t))
Colin Crossab8d1382023-07-14 17:23:41 +0000667 for _, dep := range t {
668 paths = append(paths, dep.resPackage)
669 }
Colin Cross039d8df2023-06-20 22:40:02 -0700670 return paths
Colin Crossab8d1382023-07-14 17:23:41 +0000671}
672
673func (t transitiveAarDeps) manifests() android.Paths {
Colin Cross039d8df2023-06-20 22:40:02 -0700674 paths := make(android.Paths, 0, len(t))
Colin Crossab8d1382023-07-14 17:23:41 +0000675 for _, dep := range t {
676 paths = append(paths, dep.manifest)
677 paths = append(paths, dep.additionalManifests...)
678 }
Colin Cross039d8df2023-06-20 22:40:02 -0700679 return paths
680}
681
682func (t transitiveAarDeps) resourceProcessorDeps() (args []string, deps android.Paths) {
683 for _, dep := range t {
684 args = append(args, "--library="+dep.rTxt.String()+","+dep.manifest.String())
685 deps = append(deps, dep.rTxt, dep.manifest)
686 }
687 return args, deps
Colin Crossab8d1382023-07-14 17:23:41 +0000688}
689
690func (t transitiveAarDeps) assets() android.Paths {
Colin Cross039d8df2023-06-20 22:40:02 -0700691 paths := make(android.Paths, 0, len(t))
Colin Crossab8d1382023-07-14 17:23:41 +0000692 for _, dep := range t {
693 if dep.assets.Valid() {
694 paths = append(paths, dep.assets.Path())
695 }
696 }
697 return paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700698}
699
700// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Jiyong Parkf1691d22021-03-29 20:11:58 +0900701func aaptLibs(ctx android.ModuleContext, sdkContext android.SdkContext, classLoaderContexts dexpreopt.ClassLoaderContextMap) (
Colin Cross8676c8c2023-10-12 15:58:57 -0700702 staticResourcesNodes, sharedResourcesNodes *android.DepSet[*resourcesNode], staticRRODirs *android.DepSet[rroDir],
Colin Crossab8d1382023-07-14 17:23:41 +0000703 staticManifests *android.DepSet[android.Path], sharedLibs android.Paths, flags []string) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700704
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100705 if classLoaderContexts == nil {
Ulya Trafimovich18554242020-11-03 15:55:11 +0000706 // Not all callers need to compute class loader context, those who don't just pass nil.
707 // Create a temporary class loader context here (it will be computed, but not used).
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100708 classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000709 }
710
Colin Cross83bb3162018-06-25 15:48:06 -0700711 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700712 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700713 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700714 }
715
Colin Cross8676c8c2023-10-12 15:58:57 -0700716 var staticResourcesNodeDepSets []*android.DepSet[*resourcesNode]
717 var sharedResourcesNodeDepSets []*android.DepSet[*resourcesNode]
Colin Crossab8d1382023-07-14 17:23:41 +0000718 rroDirsDepSetBuilder := android.NewDepSetBuilder[rroDir](android.TOPOLOGICAL)
719 manifestsDepSetBuilder := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL)
720
Colin Crossa97c5d32018-03-28 14:58:31 -0700721 ctx.VisitDirectDeps(func(module android.Module) {
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000722 depTag := ctx.OtherModuleDependencyTag(module)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000723
Colin Crossa97c5d32018-03-28 14:58:31 -0700724 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700725 aarDep, _ := module.(AndroidLibraryDependency)
726 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700727 exportPackage = aarDep.ExportPackage()
728 }
729
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000730 switch depTag {
Colin Cross4b964c02018-10-15 16:18:06 -0700731 case instrumentationForTag:
732 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Liz Kammeref28a4c2022-09-23 16:50:56 -0400733 case sdkLibTag, libTag:
Colin Cross5446e882019-05-22 10:46:27 -0700734 if exportPackage != nil {
Colin Cross8676c8c2023-10-12 15:58:57 -0700735 sharedResourcesNodeDepSets = append(sharedResourcesNodeDepSets, aarDep.ResourcesNodeDepSet())
Colin Cross5446e882019-05-22 10:46:27 -0700736 sharedLibs = append(sharedLibs, exportPackage)
737 }
Colin Cross5446e882019-05-22 10:46:27 -0700738 case frameworkResTag:
Colin Crossa97c5d32018-03-28 14:58:31 -0700739 if exportPackage != nil {
740 sharedLibs = append(sharedLibs, exportPackage)
741 }
742 case staticLibTag:
743 if exportPackage != nil {
Colin Cross8676c8c2023-10-12 15:58:57 -0700744 staticResourcesNodeDepSets = append(staticResourcesNodeDepSets, aarDep.ResourcesNodeDepSet())
Colin Crossab8d1382023-07-14 17:23:41 +0000745 rroDirsDepSetBuilder.Transitive(aarDep.RRODirsDepSet())
746 manifestsDepSetBuilder.Transitive(aarDep.ManifestsDepSet())
Colin Crossa97c5d32018-03-28 14:58:31 -0700747 }
748 }
Ulya Trafimovich18554242020-11-03 15:55:11 +0000749
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +0000750 addCLCFromDep(ctx, module, classLoaderContexts)
Colin Crossa97c5d32018-03-28 14:58:31 -0700751 })
752
Colin Crossab8d1382023-07-14 17:23:41 +0000753 // AAPT2 overlays are in lowest to highest priority order, the topological order will be reversed later.
754 // Reverse the dependency order now going into the depset so that it comes out in order after the second
755 // reverse later.
756 // NOTE: this is legacy and probably incorrect behavior, for most other cases (e.g. conflicting classes in
757 // dependencies) the highest priority dependency is listed first, but for resources the highest priority
758 // dependency has to be listed last.
759 staticResourcesNodes = android.NewDepSet(android.TOPOLOGICAL, nil,
Colin Cross8676c8c2023-10-12 15:58:57 -0700760 android.ReverseSliceInPlace(staticResourcesNodeDepSets))
761 sharedResourcesNodes = android.NewDepSet(android.TOPOLOGICAL, nil,
762 android.ReverseSliceInPlace(sharedResourcesNodeDepSets))
Colin Crossa97c5d32018-03-28 14:58:31 -0700763
Colin Crossab8d1382023-07-14 17:23:41 +0000764 staticRRODirs = rroDirsDepSetBuilder.Build()
765 staticManifests = manifestsDepSetBuilder.Build()
766
767 if len(staticResourcesNodes.ToList()) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700768 flags = append(flags, "--auto-add-overlay")
769 }
770
771 for _, sharedLib := range sharedLibs {
772 flags = append(flags, "-I "+sharedLib.String())
773 }
774
Colin Cross8676c8c2023-10-12 15:58:57 -0700775 return staticResourcesNodes, sharedResourcesNodes, staticRRODirs, staticManifests, sharedLibs, flags
Colin Crossa97c5d32018-03-28 14:58:31 -0700776}
777
778type AndroidLibrary struct {
779 Library
780 aapt
781
782 androidLibraryProperties androidLibraryProperties
783
784 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700785}
786
Saeid Farivar Asanjan1fca3012021-09-14 18:40:19 +0000787var _ android.OutputFileProducer = (*AndroidLibrary)(nil)
788
789// For OutputFileProducer interface
790func (a *AndroidLibrary) OutputFiles(tag string) (android.Paths, error) {
791 switch tag {
792 case ".aar":
793 return []android.Path{a.aarFile}, nil
794 default:
795 return a.Library.OutputFiles(tag)
796 }
797}
798
Colin Crossa97c5d32018-03-28 14:58:31 -0700799var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
800
801func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
802 a.Module.deps(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900803 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
Paul Duffin250e6192019-06-07 10:44:37 +0100804 if sdkDep.hasFrameworkLibs() {
805 a.aapt.deps(ctx, sdkDep)
Colin Crossa97c5d32018-03-28 14:58:31 -0700806 }
Colin Cross4a80a152022-12-21 21:51:52 -0800807 a.usesLibrary.deps(ctx, false)
Colin Crossa97c5d32018-03-28 14:58:31 -0700808}
809
810func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800811 a.aapt.isLibrary = true
Ulya Trafimovich42c7f0d2021-08-17 16:20:29 +0100812 a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Alixf7a10272023-09-27 16:47:56 +0000813 a.aapt.buildActions(ctx,
814 aaptBuildActionOptions{
815 sdkContext: android.SdkContext(a),
816 classLoaderContexts: a.classLoaderContexts,
817 enforceDefaultTargetSdkVersion: false,
818 },
819 )
Colin Crossa97c5d32018-03-28 14:58:31 -0700820
Colin Crossff694a82023-12-13 15:54:49 -0800821 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
822 a.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -0700823
Jihoon Kang1bfb6f22023-07-01 00:13:47 +0000824 a.stem = proptools.StringDefault(a.overridableDeviceProperties.Stem, ctx.ModuleName())
825
Colin Cross039d8df2023-06-20 22:40:02 -0700826 ctx.CheckbuildFile(a.aapt.proguardOptionsFile)
827 ctx.CheckbuildFile(a.aapt.exportPackage)
Colin Cross8f1b0332024-01-25 13:39:06 -0800828 if a.useResourceProcessorBusyBox(ctx) {
Colin Cross039d8df2023-06-20 22:40:02 -0700829 ctx.CheckbuildFile(a.aapt.rJar)
Colin Crossf3b7bad2023-08-02 15:49:00 -0700830 } else {
831 ctx.CheckbuildFile(a.aapt.aaptSrcJar)
Colin Cross039d8df2023-06-20 22:40:02 -0700832 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700833
834 // apps manifests are handled by aapt, don't let Module see them
835 a.properties.Manifest = nil
836
Colin Cross014489c2020-06-02 20:09:13 -0700837 a.linter.mergedManifest = a.aapt.mergedManifestFile
838 a.linter.manifest = a.aapt.manifestPath
839 a.linter.resources = a.aapt.resourceFiles
840
Sam Delmericoc8e040c2023-10-31 17:27:02 +0000841 proguardSpecInfo := a.collectProguardSpecInfo(ctx)
Colin Cross40213022023-12-13 15:19:49 -0800842 android.SetProvider(ctx, ProguardSpecInfoProvider, proguardSpecInfo)
Colin Cross312634e2023-11-21 15:13:56 -0800843 exportedProguardFlagsFiles := proguardSpecInfo.ProguardFlagsFiles.ToList()
844 a.extraProguardFlagsFiles = append(a.extraProguardFlagsFiles, exportedProguardFlagsFiles...)
845 a.extraProguardFlagsFiles = append(a.extraProguardFlagsFiles, a.proguardOptionsFile)
846
847 combinedExportedProguardFlagFile := android.PathForModuleOut(ctx, "export_proguard_flags")
848 writeCombinedProguardFlagsFile(ctx, combinedExportedProguardFlagFile, exportedProguardFlagsFiles)
849 a.combinedExportedProguardFlagsFile = combinedExportedProguardFlagFile
Colin Crossa97c5d32018-03-28 14:58:31 -0700850
Colin Cross039d8df2023-06-20 22:40:02 -0700851 var extraSrcJars android.Paths
852 var extraCombinedJars android.Paths
853 var extraClasspathJars android.Paths
Colin Cross8f1b0332024-01-25 13:39:06 -0800854 if a.useResourceProcessorBusyBox(ctx) {
Colin Cross039d8df2023-06-20 22:40:02 -0700855 // When building a library with ResourceProcessorBusyBox enabled ResourceProcessorBusyBox for this
856 // library and each of the transitive static android_library dependencies has already created an
857 // R.class file for the appropriate package. Add all of those R.class files to the classpath.
858 extraClasspathJars = a.transitiveAaptRJars
859 } else {
860 // When building a library without ResourceProcessorBusyBox the aapt2 rule creates R.srcjar containing
861 // R.java files for the library's package and the packages from all transitive static android_library
862 // dependencies. Compile the srcjar alongside the rest of the sources.
863 extraSrcJars = android.Paths{a.aapt.aaptSrcJar}
864 }
865
866 a.Module.compile(ctx, extraSrcJars, extraClasspathJars, extraCombinedJars)
Colin Crossa97c5d32018-03-28 14:58:31 -0700867
Colin Crossf57c5782019-01-25 13:20:38 -0800868 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700869 var res android.Paths
870 if a.androidLibraryProperties.BuildAAR {
871 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
872 ctx.CheckbuildFile(a.aarFile)
873 }
Colin Cross89c31582018-04-30 15:55:11 -0700874
Sam Delmerico82602492022-06-10 17:05:42 +0000875 prebuiltJniPackages := android.Paths{}
876 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800877 if info, ok := android.OtherModuleProvider(ctx, module, JniPackageProvider); ok {
Sam Delmerico82602492022-06-10 17:05:42 +0000878 prebuiltJniPackages = append(prebuiltJniPackages, info.JniPackages...)
879 }
880 })
881 if len(prebuiltJniPackages) > 0 {
Colin Cross40213022023-12-13 15:19:49 -0800882 android.SetProvider(ctx, JniPackageProvider, JniPackageInfo{
Sam Delmerico82602492022-06-10 17:05:42 +0000883 JniPackages: prebuiltJniPackages,
884 })
885 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700886}
887
Colin Cross95b53b82023-10-17 13:21:02 -0700888func (a *AndroidLibrary) IDEInfo(dpInfo *android.IdeInfo) {
889 a.Library.IDEInfo(dpInfo)
890 a.aapt.IDEInfo(dpInfo)
891}
892
893func (a *aapt) IDEInfo(dpInfo *android.IdeInfo) {
Colin Cross8f1b0332024-01-25 13:39:06 -0800894 if a.rJar != nil {
Colin Cross95b53b82023-10-17 13:21:02 -0700895 dpInfo.Jars = append(dpInfo.Jars, a.rJar.String())
896 }
897}
898
Colin Cross1b16b0e2019-02-12 14:41:32 -0800899// android_library builds and links sources into a `.jar` file for the device along with Android resources.
900//
901// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
Sam Delmerico82602492022-06-10 17:05:42 +0000902// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
Colin Cross1b16b0e2019-02-12 14:41:32 -0800903// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
904// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700905func AndroidLibraryFactory() android.Module {
906 module := &AndroidLibrary{}
907
Colin Crossce6734e2020-06-15 16:09:53 -0700908 module.Module.addHostAndDeviceProperties()
Colin Crossa97c5d32018-03-28 14:58:31 -0700909 module.AddProperties(
Colin Crossa97c5d32018-03-28 14:58:31 -0700910 &module.aaptProperties,
911 &module.androidLibraryProperties)
912
913 module.androidLibraryProperties.BuildAAR = true
Colin Cross014489c2020-06-02 20:09:13 -0700914 module.Module.linter.library = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700915
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900916 android.InitApexModule(module)
Colin Cross48de9a42018-10-02 13:53:33 -0700917 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700918 return module
919}
920
Colin Crossfabb6082018-02-20 17:22:23 -0800921//
922// AAR (android library) prebuilts
923//
Colin Crossfabb6082018-02-20 17:22:23 -0800924
Vinh Trance0781f2022-04-13 01:30:44 +0000925// Properties for android_library_import
Colin Crossfabb6082018-02-20 17:22:23 -0800926type AARImportProperties struct {
Vinh Trance0781f2022-04-13 01:30:44 +0000927 // ARR (android library prebuilt) filepath. Exactly one ARR is required.
Colin Cross27b922f2019-03-04 22:35:41 -0800928 Aars []string `android:"path"`
Vinh Trance0781f2022-04-13 01:30:44 +0000929 // If not blank, set to the version of the sdk to compile against.
930 // Defaults to private.
931 // Values are of one of the following forms:
932 // 1) numerical API level, "current", "none", or "core_platform"
933 // 2) An SDK kind with an API level: "<sdk kind>_<API level>"
934 // See build/soong/android/sdk_version.go for the complete and up to date list of SDK kinds.
935 // If the SDK kind is empty, it will be set to public
936 Sdk_version *string
937 // If not blank, set the minimum version of the sdk that the compiled artifacts will run against.
938 // Defaults to sdk_version if not set. See sdk_version for possible values.
Colin Cross479884c2018-07-10 13:39:30 -0700939 Min_sdk_version *string
Vinh Trance0781f2022-04-13 01:30:44 +0000940 // List of java static libraries that the included ARR (android library prebuilts) has dependencies to.
Colin Crossa97c5d32018-03-28 14:58:31 -0700941 Static_libs []string
Vinh Trance0781f2022-04-13 01:30:44 +0000942 // List of java libraries that the included ARR (android library prebuilts) has dependencies to.
943 Libs []string
944 // If set to true, run Jetifier against .aar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -0700945 Jetifier *bool
Sam Delmerico82602492022-06-10 17:05:42 +0000946 // If true, extract JNI libs from AAR archive. These libs will be accessible to android_app modules and
947 // will be passed transitively through android_libraries to an android_app.
948 //TODO(b/241138093) evaluate whether we can have this flag default to true for Bazel conversion
949 Extract_jni *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800950}
951
952type AARImport struct {
953 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700954 android.DefaultableModuleBase
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900955 android.ApexModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800956 prebuilt android.Prebuilt
957
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900958 // Functionality common to Module and Import.
959 embeddableInModuleAndImport
960
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500961 providesTransitiveHeaderJars
962
Colin Crossfabb6082018-02-20 17:22:23 -0800963 properties AARImportProperties
964
Colin Cross312634e2023-11-21 15:13:56 -0800965 classpathFile android.WritablePath
966 proguardFlags android.WritablePath
967 exportPackage android.WritablePath
968 transitiveAaptResourcePackagesFile android.Path
969 extraAaptPackagesFile android.WritablePath
970 manifest android.WritablePath
971 assetsPackage android.WritablePath
972 rTxt android.WritablePath
973 rJar android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700974
Colin Crossab8d1382023-07-14 17:23:41 +0000975 resourcesNodesDepSet *android.DepSet[*resourcesNode]
976 manifestsDepSet *android.DepSet[android.Path]
Colin Cross56a83212020-09-15 18:30:11 -0700977
978 hideApexVariantFromMake bool
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000979
Sam Delmerico82602492022-06-10 17:05:42 +0000980 aarPath android.Path
981 jniPackages android.Paths
Jiyong Park92315372021-04-02 08:45:46 +0900982
983 sdkVersion android.SdkSpec
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000984 minSdkVersion android.ApiLevel
LaMont Jonesafe7baf2024-01-09 22:47:39 +0000985
986 // Single aconfig "cache file" merged from this module and all dependencies.
987 mergedAconfigFiles map[string]android.Paths
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000988}
989
990var _ android.OutputFileProducer = (*AARImport)(nil)
991
992// For OutputFileProducer interface
993func (a *AARImport) OutputFiles(tag string) (android.Paths, error) {
994 switch tag {
995 case ".aar":
996 return []android.Path{a.aarPath}, nil
997 case "":
998 return []android.Path{a.classpathFile}, nil
999 default:
1000 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1001 }
Colin Crossfabb6082018-02-20 17:22:23 -08001002}
1003
Jiyong Park92315372021-04-02 08:45:46 +09001004func (a *AARImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1005 return android.SdkSpecFrom(ctx, String(a.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07001006}
1007
Jiyong Parkf1691d22021-03-29 20:11:58 +09001008func (a *AARImport) SystemModules() string {
Paul Duffine25c6442019-10-11 13:50:28 +01001009 return ""
1010}
1011
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001012func (a *AARImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Colin Cross479884c2018-07-10 13:39:30 -07001013 if a.properties.Min_sdk_version != nil {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001014 return android.ApiLevelFrom(ctx, *a.properties.Min_sdk_version)
Colin Cross479884c2018-07-10 13:39:30 -07001015 }
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001016 return a.SdkVersion(ctx).ApiLevel
Colin Cross83bb3162018-06-25 15:48:06 -07001017}
1018
Spandan Dasa26eda72023-03-02 00:56:06 +00001019func (a *AARImport) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
1020 return android.SdkSpecFrom(ctx, "").ApiLevel
William Loh5a082f92022-05-17 20:21:50 +00001021}
1022
Spandan Dasca70fc42023-03-01 23:38:49 +00001023func (a *AARImport) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
1024 return a.SdkVersion(ctx).ApiLevel
Dan Willemsen419290a2018-10-31 15:28:47 -07001025}
1026
Colin Cross1e743852019-10-28 11:37:20 -07001027func (a *AARImport) javaVersion() string {
1028 return ""
1029}
1030
Colin Crossa97c5d32018-03-28 14:58:31 -07001031var _ AndroidLibraryDependency = (*AARImport)(nil)
1032
1033func (a *AARImport) ExportPackage() android.Path {
1034 return a.exportPackage
1035}
Colin Crossab8d1382023-07-14 17:23:41 +00001036func (a *AARImport) ResourcesNodeDepSet() *android.DepSet[*resourcesNode] {
1037 return a.resourcesNodesDepSet
Colin Crossc1c37552019-01-31 11:42:41 -08001038}
1039
Colin Crossab8d1382023-07-14 17:23:41 +00001040func (a *AARImport) RRODirsDepSet() *android.DepSet[rroDir] {
1041 return android.NewDepSet[rroDir](android.TOPOLOGICAL, nil, nil)
Colin Cross66f78822018-05-02 12:58:28 -07001042}
1043
Colin Crossab8d1382023-07-14 17:23:41 +00001044func (a *AARImport) ManifestsDepSet() *android.DepSet[android.Path] {
1045 return a.manifestsDepSet
Jaewoong Jung6431ca72020-01-15 14:15:10 -08001046}
1047
Jaewoong Jungc779cd42020-10-06 18:56:10 -07001048// RRO enforcement is not available on aar_import since its RRO dirs are not
1049// exported.
1050func (a *AARImport) SetRROEnforcedForDependent(enforce bool) {
1051}
1052
1053// RRO enforcement is not available on aar_import since its RRO dirs are not
1054// exported.
1055func (a *AARImport) IsRROEnforced(ctx android.BaseModuleContext) bool {
1056 return false
1057}
1058
Colin Crossfabb6082018-02-20 17:22:23 -08001059func (a *AARImport) Prebuilt() *android.Prebuilt {
1060 return &a.prebuilt
1061}
1062
1063func (a *AARImport) Name() string {
1064 return a.prebuilt.Name(a.ModuleBase.Name())
1065}
1066
Jiyong Park618922e2020-01-08 13:35:43 +09001067func (a *AARImport) JacocoReportClassesFile() android.Path {
1068 return nil
1069}
1070
Colin Crossfabb6082018-02-20 17:22:23 -08001071func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Jeongik Cha816a23a2020-07-08 01:09:23 +09001072 if !ctx.Config().AlwaysUsePrebuiltSdks() {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001073 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -07001074 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -07001075 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -08001076 }
1077 }
Colin Crossa97c5d32018-03-28 14:58:31 -07001078
Colin Cross42d48b72018-08-29 14:10:52 -07001079 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
1080 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -08001081}
1082
Sam Delmerico82602492022-06-10 17:05:42 +00001083type JniPackageInfo struct {
1084 // List of zip files containing JNI libraries
1085 // Zip files should have directory structure jni/<arch>/*.so
1086 JniPackages android.Paths
1087}
1088
Colin Crossbc7d76c2023-12-12 16:39:03 -08001089var JniPackageProvider = blueprint.NewProvider[JniPackageInfo]()
Sam Delmerico82602492022-06-10 17:05:42 +00001090
1091// Unzip an AAR and extract the JNI libs for $archString.
1092var extractJNI = pctx.AndroidStaticRule("extractJNI",
1093 blueprint.RuleParams{
1094 Command: `rm -rf $out $outDir && touch $out && ` +
1095 `unzip -qoDD -d $outDir $in "jni/${archString}/*" && ` +
1096 `jni_files=$$(find $outDir/jni -type f) && ` +
1097 // print error message if there are no JNI libs for this arch
1098 `[ -n "$$jni_files" ] || (echo "ERROR: no JNI libs found for arch ${archString}" && exit 1) && ` +
Sam Delmerico80ee45c2023-06-22 15:36:02 -04001099 `${config.SoongZipCmd} -o $out -L 0 -P 'lib/${archString}' ` +
Sam Delmerico82602492022-06-10 17:05:42 +00001100 `-C $outDir/jni/${archString} $$(echo $$jni_files | xargs -n1 printf " -f %s")`,
1101 CommandDeps: []string{"${config.SoongZipCmd}"},
1102 },
1103 "outDir", "archString")
1104
Colin Crossfabb6082018-02-20 17:22:23 -08001105// 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 -07001106// 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 -08001107var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
1108 blueprint.RuleParams{
Dan Willemsen304cfec2019-05-28 14:49:06 -07001109 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
Colin Cross205e9112020-08-06 13:20:17 -07001110 `unzip -qoDD -d $outDir $in && rm -rf $outDir/res && touch $out && ` +
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001111 `${config.Zip2ZipCmd} -i $in -o $assetsPackage 'assets/**/*' && ` +
Colin Cross205e9112020-08-06 13:20:17 -07001112 `${config.MergeZipsCmd} $combinedClassesJar $$(ls $outDir/classes.jar 2> /dev/null) $$(ls $outDir/libs/*.jar 2> /dev/null)`,
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001113 CommandDeps: []string{"${config.MergeZipsCmd}", "${config.Zip2ZipCmd}"},
Colin Crossfabb6082018-02-20 17:22:23 -08001114 },
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001115 "outDir", "combinedClassesJar", "assetsPackage")
Colin Crossfabb6082018-02-20 17:22:23 -08001116
1117func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1118 if len(a.properties.Aars) != 1 {
1119 ctx.PropertyErrorf("aars", "exactly one aar is required")
1120 return
1121 }
1122
Jiyong Park92315372021-04-02 08:45:46 +09001123 a.sdkVersion = a.SdkVersion(ctx)
1124 a.minSdkVersion = a.MinSdkVersion(ctx)
1125
Colin Crossff694a82023-12-13 15:54:49 -08001126 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1127 a.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07001128
Nan Zhang4c819fb2018-08-27 18:31:46 -07001129 aarName := ctx.ModuleName() + ".aar"
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001130 a.aarPath = android.PathForModuleSrc(ctx, a.properties.Aars[0])
1131
Colin Cross1001a792019-03-21 22:21:39 -07001132 if Bool(a.properties.Jetifier) {
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001133 inputFile := a.aarPath
1134 a.aarPath = android.PathForModuleOut(ctx, "jetifier", aarName)
1135 TransformJetifier(ctx, a.aarPath.(android.WritablePath), inputFile)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001136 }
Colin Crossfabb6082018-02-20 17:22:23 -08001137
1138 extractedAARDir := android.PathForModuleOut(ctx, "aar")
Colin Cross205e9112020-08-06 13:20:17 -07001139 a.classpathFile = extractedAARDir.Join(ctx, "classes-combined.jar")
Colin Cross10f7c4a2018-05-23 10:59:28 -07001140 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Cross039d8df2023-06-20 22:40:02 -07001141 aarRTxt := extractedAARDir.Join(ctx, "R.txt")
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001142 a.assetsPackage = android.PathForModuleOut(ctx, "assets.zip")
Sam Delmerico95d70942023-08-02 18:00:35 -04001143 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross40213022023-12-13 15:19:49 -08001144 android.SetProvider(ctx, ProguardSpecInfoProvider, ProguardSpecInfo{
Sam Delmerico95d70942023-08-02 18:00:35 -04001145 ProguardFlagsFiles: android.NewDepSet[android.Path](
1146 android.POSTORDER,
1147 android.Paths{a.proguardFlags},
1148 nil,
1149 ),
1150 })
Colin Crossfabb6082018-02-20 17:22:23 -08001151
1152 ctx.Build(pctx, android.BuildParams{
1153 Rule: unzipAAR,
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001154 Input: a.aarPath,
Colin Cross039d8df2023-06-20 22:40:02 -07001155 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest, a.assetsPackage, aarRTxt},
Colin Crossfabb6082018-02-20 17:22:23 -08001156 Description: "unzip AAR",
1157 Args: map[string]string{
Colin Cross205e9112020-08-06 13:20:17 -07001158 "outDir": extractedAARDir.String(),
1159 "combinedClassesJar": a.classpathFile.String(),
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001160 "assetsPackage": a.assetsPackage.String(),
Colin Crossfabb6082018-02-20 17:22:23 -08001161 },
1162 })
1163
Colin Crossa0ba2f52019-06-22 12:59:27 -07001164 // Always set --pseudo-localize, it will be stripped out later for release
1165 // builds that don't want it.
1166 compileFlags := []string{"--pseudo-localize"}
Colin Crossfabb6082018-02-20 17:22:23 -08001167 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
Colin Crossfabb6082018-02-20 17:22:23 -08001168 flata := compiledResDir.Join(ctx, "gen_res.flata")
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001169 aapt2CompileZip(ctx, flata, a.aarPath, "res", compileFlags)
Colin Crossfabb6082018-02-20 17:22:23 -08001170
1171 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
Colin Crossfabb6082018-02-20 17:22:23 -08001172 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Cross039d8df2023-06-20 22:40:02 -07001173 a.rTxt = android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -07001174 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -08001175
1176 var linkDeps android.Paths
1177
1178 linkFlags := []string{
1179 "--static-lib",
Colin Cross039d8df2023-06-20 22:40:02 -07001180 "--merge-only",
Colin Crossfabb6082018-02-20 17:22:23 -08001181 "--auto-add-overlay",
1182 }
1183
Colin Cross10f7c4a2018-05-23 10:59:28 -07001184 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
1185 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -08001186
Colin Cross8676c8c2023-10-12 15:58:57 -07001187 staticResourcesNodesDepSet, sharedResourcesNodesDepSet, staticRRODirsDepSet, staticManifestsDepSet, sharedLibs, libFlags :=
Jiyong Parkf1691d22021-03-29 20:11:58 +09001188 aaptLibs(ctx, android.SdkContext(a), nil)
Colin Cross31656952018-05-24 16:11:20 -07001189
Colin Cross8676c8c2023-10-12 15:58:57 -07001190 _ = sharedResourcesNodesDepSet
Colin Crossab8d1382023-07-14 17:23:41 +00001191 _ = staticRRODirsDepSet
Colin Cross8676c8c2023-10-12 15:58:57 -07001192
Colin Crossab8d1382023-07-14 17:23:41 +00001193 staticDeps := transitiveAarDeps(staticResourcesNodesDepSet.ToList())
Colin Crossfabb6082018-02-20 17:22:23 -08001194
Colin Crossab8d1382023-07-14 17:23:41 +00001195 linkDeps = append(linkDeps, sharedLibs...)
Colin Cross039d8df2023-06-20 22:40:02 -07001196 linkDeps = append(linkDeps, staticDeps.resPackages()...)
Colin Crossa97c5d32018-03-28 14:58:31 -07001197 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -08001198
Colin Cross039d8df2023-06-20 22:40:02 -07001199 overlayRes := android.Paths{flata}
1200
1201 // Treat static library dependencies of static libraries as imports.
1202 transitiveStaticLibs := staticDeps.resPackages()
1203 linkDeps = append(linkDeps, transitiveStaticLibs...)
1204 for _, staticLib := range transitiveStaticLibs {
1205 linkFlags = append(linkFlags, "-I "+staticLib.String())
1206 }
Colin Crossfabb6082018-02-20 17:22:23 -08001207
Colin Crossab8d1382023-07-14 17:23:41 +00001208 transitiveAssets := android.ReverseSliceInPlace(staticDeps.assets())
Colin Crossf3b7bad2023-08-02 15:49:00 -07001209 aapt2Link(ctx, a.exportPackage, nil, proguardOptionsFile, a.rTxt,
Jihoon Kang84b25892023-12-01 22:01:06 +00001210 linkFlags, linkDeps, nil, overlayRes, transitiveAssets, nil, nil)
Colin Crossfabb6082018-02-20 17:22:23 -08001211
Colin Cross039d8df2023-06-20 22:40:02 -07001212 a.rJar = android.PathForModuleOut(ctx, "busybox/R.jar")
Colin Crossd3f7d1a2024-01-03 19:42:25 -08001213 resourceProcessorBusyBoxGenerateBinaryR(ctx, a.rTxt, a.manifest, a.rJar, nil, true, nil)
Colin Cross039d8df2023-06-20 22:40:02 -07001214
Colin Crossf3b7bad2023-08-02 15:49:00 -07001215 aapt2ExtractExtraPackages(ctx, a.extraAaptPackagesFile, a.rJar)
1216
Colin Crossab8d1382023-07-14 17:23:41 +00001217 resourcesNodesDepSetBuilder := android.NewDepSetBuilder[*resourcesNode](android.TOPOLOGICAL)
1218 resourcesNodesDepSetBuilder.Direct(&resourcesNode{
1219 resPackage: a.exportPackage,
1220 manifest: a.manifest,
Colin Cross039d8df2023-06-20 22:40:02 -07001221 rTxt: a.rTxt,
1222 rJar: a.rJar,
Colin Crossab8d1382023-07-14 17:23:41 +00001223 assets: android.OptionalPathForPath(a.assetsPackage),
Colin Cross039d8df2023-06-20 22:40:02 -07001224
1225 usedResourceProcessor: true,
Colin Crossab8d1382023-07-14 17:23:41 +00001226 })
1227 resourcesNodesDepSetBuilder.Transitive(staticResourcesNodesDepSet)
1228 a.resourcesNodesDepSet = resourcesNodesDepSetBuilder.Build()
1229
1230 manifestDepSetBuilder := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(a.manifest)
1231 // TODO(b/288358614): Soong has historically not merged manifests from dependencies of android_library_import
1232 // modules. Merging manifests from dependencies could remove the need for pom2bp to generate the "-nodeps" copies
1233 // of androidx libraries, but doing so triggers errors due to errors introduced by existing dependencies of
1234 // android_library_import modules. If this is fixed, AndroidLibraryDependency.ManifestsDepSet can be dropped
1235 // completely in favor of AndroidLibraryDependency.ResourceNodesDepSet.manifest
1236 //manifestDepSetBuilder.Transitive(transitiveStaticDeps.manifests)
1237 _ = staticManifestsDepSet
1238 a.manifestsDepSet = manifestDepSetBuilder.Build()
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001239
Colin Cross312634e2023-11-21 15:13:56 -08001240 transitiveAaptResourcePackages := staticDeps.resPackages().Strings()
1241 transitiveAaptResourcePackages = slices.DeleteFunc(transitiveAaptResourcePackages, func(p string) bool {
1242 return p == a.exportPackage.String()
1243 })
1244 transitiveAaptResourcePackagesFile := android.PathForModuleOut(ctx, "transitive-res-packages")
1245 android.WriteFileRule(ctx, transitiveAaptResourcePackagesFile, strings.Join(transitiveAaptResourcePackages, "\n"))
1246 a.transitiveAaptResourcePackagesFile = transitiveAaptResourcePackagesFile
Colin Cross039d8df2023-06-20 22:40:02 -07001247
Sam Delmerico9f9c0a22022-11-29 11:19:37 -05001248 a.collectTransitiveHeaderJars(ctx)
Colin Cross40213022023-12-13 15:19:49 -08001249 android.SetProvider(ctx, JavaInfoProvider, JavaInfo{
Colin Crossdcf71b22021-02-01 13:59:03 -08001250 HeaderJars: android.PathsIfNonNil(a.classpathFile),
Sam Delmerico9f9c0a22022-11-29 11:19:37 -05001251 TransitiveLibsHeaderJars: a.transitiveLibsHeaderJars,
1252 TransitiveStaticLibsHeaderJars: a.transitiveStaticLibsHeaderJars,
Colin Crossdcf71b22021-02-01 13:59:03 -08001253 ImplementationAndResourcesJars: android.PathsIfNonNil(a.classpathFile),
1254 ImplementationJars: android.PathsIfNonNil(a.classpathFile),
Joe Onorato6fe59eb2023-07-16 13:20:33 -07001255 // TransitiveAconfigFiles: // TODO(b/289117800): LOCAL_ACONFIG_FILES for prebuilts
Colin Crossdcf71b22021-02-01 13:59:03 -08001256 })
Sam Delmerico82602492022-06-10 17:05:42 +00001257
1258 if proptools.Bool(a.properties.Extract_jni) {
1259 for _, t := range ctx.MultiTargets() {
1260 arch := t.Arch.Abi[0]
1261 path := android.PathForModuleOut(ctx, arch+"_jni.zip")
1262 a.jniPackages = append(a.jniPackages, path)
1263
1264 outDir := android.PathForModuleOut(ctx, "aarForJni")
1265 aarPath := android.PathForModuleSrc(ctx, a.properties.Aars[0])
1266 ctx.Build(pctx, android.BuildParams{
1267 Rule: extractJNI,
1268 Input: aarPath,
1269 Outputs: android.WritablePaths{path},
1270 Description: "extract JNI from AAR",
1271 Args: map[string]string{
1272 "outDir": outDir.String(),
1273 "archString": arch,
1274 },
1275 })
1276 }
Sam Delmerico82602492022-06-10 17:05:42 +00001277 }
Colin Crosse8eeec92023-12-14 14:50:05 -08001278
Colin Cross40213022023-12-13 15:19:49 -08001279 android.SetProvider(ctx, JniPackageProvider, JniPackageInfo{
Colin Crosse8eeec92023-12-14 14:50:05 -08001280 JniPackages: a.jniPackages,
1281 })
LaMont Jonesafe7baf2024-01-09 22:47:39 +00001282 android.CollectDependencyAconfigFiles(ctx, &a.mergedAconfigFiles)
Colin Crossdcf71b22021-02-01 13:59:03 -08001283}
Colin Crossfabb6082018-02-20 17:22:23 -08001284
1285func (a *AARImport) HeaderJars() android.Paths {
1286 return android.Paths{a.classpathFile}
1287}
1288
Colin Cross331a1212018-08-15 20:40:52 -07001289func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
1290 return android.Paths{a.classpathFile}
1291}
1292
Spandan Das59a4a2b2024-01-09 21:35:56 +00001293func (a *AARImport) DexJarBuildPath(ctx android.ModuleErrorfContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -08001294 return nil
1295}
1296
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001297func (a *AARImport) DexJarInstallPath() android.Path {
1298 return nil
1299}
1300
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001301func (a *AARImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
Jiyong Park1be96912018-05-28 18:02:19 +09001302 return nil
1303}
1304
Jiyong Park45bf82e2020-12-15 22:29:02 +09001305var _ android.ApexModule = (*AARImport)(nil)
1306
1307// Implements android.ApexModule
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09001308func (a *AARImport) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1309 return a.depIsInSameApex(ctx, dep)
1310}
1311
Jiyong Park45bf82e2020-12-15 22:29:02 +09001312// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001313func (g *AARImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1314 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001315 return nil
1316}
1317
Sam Delmericoaf8bb702022-07-25 15:39:32 -04001318var _ android.PrebuiltInterface = (*AARImport)(nil)
Colin Crossfabb6082018-02-20 17:22:23 -08001319
Colin Cross1b16b0e2019-02-12 14:41:32 -08001320// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
1321//
1322// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
1323// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -08001324func AARImportFactory() android.Module {
1325 module := &AARImport{}
1326
1327 module.AddProperties(&module.properties)
1328
1329 android.InitPrebuiltModule(module, &module.properties.Aars)
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09001330 android.InitApexModule(module)
Sam Delmerico82602492022-06-10 17:05:42 +00001331 InitJavaModuleMultiTargets(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -08001332 return module
1333}