blob: 506187959e7e82e0fb324258f9a56db3077241af [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"`
Jihoon Kang9049c272024-03-19 21:57:36 +0000107
108 // Names of aconfig_declarations modules that specify aconfig flags that the module depends on.
109 Flags_packages []string
Colin Crossa97c5d32018-03-28 14:58:31 -0700110}
111
112type aapt struct {
Colin Cross312634e2023-11-21 15:13:56 -0800113 aaptSrcJar android.Path
114 transitiveAaptRJars android.Paths
115 transitiveAaptResourcePackagesFile android.Path
116 exportPackage android.Path
117 manifestPath android.Path
118 proguardOptionsFile android.Path
119 rTxt android.Path
120 rJar android.Path
121 extraAaptPackagesFile android.Path
122 mergedManifestFile android.Path
123 noticeFile android.OptionalPath
124 assetPackage android.OptionalPath
125 isLibrary bool
126 defaultManifestVersion string
127 useEmbeddedNativeLibs bool
128 useEmbeddedDex bool
129 usesNonSdkApis bool
130 hasNoCode bool
131 LoggingParent string
132 resourceFiles android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700133
Colin Crosse560c4a2019-03-19 16:03:11 -0700134 splitNames []string
135 splits []split
136
Colin Crossa97c5d32018-03-28 14:58:31 -0700137 aaptProperties aaptProperties
Colin Crossab8d1382023-07-14 17:23:41 +0000138
139 resourcesNodesDepSet *android.DepSet[*resourcesNode]
140 rroDirsDepSet *android.DepSet[rroDir]
141 manifestsDepSet *android.DepSet[android.Path]
Alix96ea88452023-08-31 15:48:23 +0000142
143 manifestValues struct {
144 applicationId string
145 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700146}
147
Colin Crosse560c4a2019-03-19 16:03:11 -0700148type split struct {
149 name string
150 suffix string
151 path android.Path
152}
153
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700154// Propagate RRO enforcement flag to static lib dependencies transitively.
155func propagateRROEnforcementMutator(ctx android.TopDownMutatorContext) {
156 m := ctx.Module()
157 if d, ok := m.(AndroidLibraryDependency); ok && d.IsRROEnforced(ctx) {
158 ctx.VisitDirectDepsWithTag(staticLibTag, func(d android.Module) {
159 if a, ok := d.(AndroidLibraryDependency); ok {
160 a.SetRROEnforcedForDependent(true)
161 }
162 })
163 }
164}
165
Colin Cross8f1b0332024-01-25 13:39:06 -0800166func (a *aapt) useResourceProcessorBusyBox(ctx android.BaseModuleContext) bool {
167 return BoolDefault(a.aaptProperties.Use_resource_processor, ctx.Config().UseResourceProcessorByDefault())
Colin Cross4eae06d2023-06-20 22:40:02 -0700168}
169
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900170func (a *aapt) filterProduct() string {
171 return String(a.aaptProperties.Filter_product)
172}
173
Colin Crossa97c5d32018-03-28 14:58:31 -0700174func (a *aapt) ExportPackage() android.Path {
175 return a.exportPackage
176}
Colin Crossab8d1382023-07-14 17:23:41 +0000177func (a *aapt) ResourcesNodeDepSet() *android.DepSet[*resourcesNode] {
178 return a.resourcesNodesDepSet
Colin Crossc1c37552019-01-31 11:42:41 -0800179}
180
Colin Crossab8d1382023-07-14 17:23:41 +0000181func (a *aapt) RRODirsDepSet() *android.DepSet[rroDir] {
182 return a.rroDirsDepSet
Colin Crossc1c37552019-01-31 11:42:41 -0800183}
184
Colin Crossab8d1382023-07-14 17:23:41 +0000185func (a *aapt) ManifestsDepSet() *android.DepSet[android.Path] {
186 return a.manifestsDepSet
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800187}
188
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700189func (a *aapt) SetRROEnforcedForDependent(enforce bool) {
190 a.aaptProperties.RROEnforcedForDependent = enforce
191}
192
193func (a *aapt) IsRROEnforced(ctx android.BaseModuleContext) bool {
194 // True if RRO is enforced for this module or...
195 return ctx.Config().EnforceRROForModule(ctx.ModuleName()) ||
Jeongik Chacee5ba92021-02-19 12:11:51 +0900196 // if RRO is enforced for any of its dependents.
197 a.aaptProperties.RROEnforcedForDependent
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700198}
199
Jiyong Parkf1691d22021-03-29 20:11:58 +0900200func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext android.SdkContext,
Colin Crossa0ba2f52019-06-22 12:59:27 -0700201 manifestPath android.Path) (compileFlags, linkFlags []string, linkDeps android.Paths,
202 resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700203
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800204 hasVersionCode := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-code")
205 hasVersionName := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-name")
Colin Crossa97c5d32018-03-28 14:58:31 -0700206
Colin Crossa97c5d32018-03-28 14:58:31 -0700207 // Flags specified in Android.bp
208 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
209
Eric Miao40eab202023-03-30 16:57:17 +0000210 linkFlags = append(linkFlags, "--enable-compact-entries")
Colin Crossa97c5d32018-03-28 14:58:31 -0700211
212 // Find implicit or explicit asset and resource dirs
Jiakai Zhangba82e282023-10-13 18:08:59 +0100213 assets := android.PathsRelativeToModuleSourceDir(android.SourceInput{
214 Context: ctx,
215 Paths: a.aaptProperties.Assets,
216 IncludeDirs: false,
217 })
Colin Crossa97c5d32018-03-28 14:58:31 -0700218 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
219 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
Colin Cross8a497952019-03-05 22:25:09 -0800220 resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips)
Colin Crossa97c5d32018-03-28 14:58:31 -0700221
Colin Crossa97c5d32018-03-28 14:58:31 -0700222 // Glob directories into lists of paths
223 for _, dir := range resourceDirs {
224 resDirs = append(resDirs, globbedResourceDir{
225 dir: dir,
226 files: androidResourceGlob(ctx, dir),
227 })
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700228 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, a, dir)
Colin Crossa97c5d32018-03-28 14:58:31 -0700229 overlayDirs = append(overlayDirs, resOverlayDirs...)
230 rroDirs = append(rroDirs, resRRODirs...)
231 }
232
Colin Crossc20dc852020-11-10 12:27:45 -0800233 var assetDeps android.Paths
234 for i, dir := range assetDirs {
235 // Add a dependency on every file in the asset directory. This ensures the aapt2
236 // rule will be rerun if one of the files in the asset directory is modified.
237 assetDeps = append(assetDeps, androidResourceGlob(ctx, dir)...)
238
239 // Add a dependency on a file that contains a list of all the files in the asset directory.
240 // This ensures the aapt2 rule will be run if a file is removed from the asset directory,
241 // or a file is added whose timestamp is older than the output of aapt2.
242 assetFileListFile := android.PathForModuleOut(ctx, "asset_dir_globs", strconv.Itoa(i)+".glob")
243 androidResourceGlobList(ctx, dir, assetFileListFile)
244 assetDeps = append(assetDeps, assetFileListFile)
Colin Crossa97c5d32018-03-28 14:58:31 -0700245 }
246
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700247 assetDirStrings := assetDirs.Strings()
248 if a.noticeFile.Valid() {
249 assetDirStrings = append(assetDirStrings, filepath.Dir(a.noticeFile.Path().String()))
Colin Crossc20dc852020-11-10 12:27:45 -0800250 assetDeps = append(assetDeps, a.noticeFile.Path())
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700251 }
Jiakai Zhangba82e282023-10-13 18:08:59 +0100252 if len(assets) > 0 {
253 // aapt2 doesn't support adding individual asset files. Create a temp directory to hold asset
254 // files and pass it to aapt2.
255 tmpAssetDir := android.PathForModuleOut(ctx, "tmp_asset_dir")
256
257 rule := android.NewRuleBuilder(pctx, ctx)
258 rule.Command().
259 Text("rm -rf").Text(tmpAssetDir.String()).
260 Text("&&").
261 Text("mkdir -p").Text(tmpAssetDir.String())
262
263 for _, asset := range assets {
264 output := tmpAssetDir.Join(ctx, asset.Rel())
265 assetDeps = append(assetDeps, output)
266 rule.Command().Text("mkdir -p").Text(filepath.Dir(output.String()))
267 rule.Command().Text("cp").Input(asset).Output(output)
268 }
269
270 rule.Build("tmp_asset_dir", "tmp_asset_dir")
271
272 assetDirStrings = append(assetDirStrings, tmpAssetDir.String())
273 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700274
Colin Crossa97c5d32018-03-28 14:58:31 -0700275 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
276 linkDeps = append(linkDeps, manifestPath)
277
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700278 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirStrings, "-A "))
Colin Crossc20dc852020-11-10 12:27:45 -0800279 linkDeps = append(linkDeps, assetDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700280
Spandan Das50885c02023-02-23 21:31:33 +0000281 // Returns the effective version for {min|target}_sdk_version
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000282 effectiveVersionString := func(sdkVersion android.SdkSpec, minSdkVersion android.ApiLevel) string {
Spandan Das50885c02023-02-23 21:31:33 +0000283 // If {min|target}_sdk_version is current, use sdk_version to determine the effective level
284 // This is necessary for vendor modules.
285 // The effective version does not _only_ depend on {min|target}_sdk_version(level),
286 // but also on the sdk_version (kind+level)
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000287 if minSdkVersion.IsCurrent() {
Spandan Das50885c02023-02-23 21:31:33 +0000288 ret, err := sdkVersion.EffectiveVersionString(ctx)
289 if err != nil {
290 ctx.ModuleErrorf("invalid sdk_version: %s", err)
291 }
292 return ret
293 }
294 ret, err := minSdkVersion.EffectiveVersionString(ctx)
295 if err != nil {
296 ctx.ModuleErrorf("invalid min_sdk_version: %s", err)
297 }
298 return ret
Jiyong Park6a927c42020-01-21 02:03:43 +0900299 }
Spandan Das50885c02023-02-23 21:31:33 +0000300 // SDK version flags
301 sdkVersion := sdkContext.SdkVersion(ctx)
302 minSdkVersion := effectiveVersionString(sdkVersion, sdkContext.MinSdkVersion(ctx))
Colin Crossa97c5d32018-03-28 14:58:31 -0700303
Colin Cross83bb3162018-06-25 15:48:06 -0700304 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
Spandan Das6450b552023-02-23 19:27:07 +0000305 // Use minSdkVersion for target-sdk-version, even if `target_sdk_version` is set
306 // This behavior has been copied from Make.
Colin Cross83bb3162018-06-25 15:48:06 -0700307 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700308
Colin Crossa97c5d32018-03-28 14:58:31 -0700309 // Version code
310 if !hasVersionCode {
Dan Albert4f378d72020-07-23 17:32:15 -0700311 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion().String())
Colin Crossa97c5d32018-03-28 14:58:31 -0700312 }
313
314 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700315 var versionName string
316 if ctx.ModuleName() == "framework-res" {
317 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
318 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700319 // if it contains the build number. Use the PlatformVersionName instead.
320 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700321 } else {
322 versionName = ctx.Config().AppsDefaultVersionName()
323 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800324 versionName = proptools.NinjaEscape(versionName)
Colin Crossa97c5d32018-03-28 14:58:31 -0700325 linkFlags = append(linkFlags, "--version-name ", versionName)
326 }
327
Colin Crossa0ba2f52019-06-22 12:59:27 -0700328 linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"})
329
330 // Always set --pseudo-localize, it will be stripped out later for release
331 // builds that don't want it.
332 compileFlags = append(compileFlags, "--pseudo-localize")
333
334 return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
Colin Crossa97c5d32018-03-28 14:58:31 -0700335}
336
Paul Duffin250e6192019-06-07 10:44:37 +0100337func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) {
Colin Cross42308aa2018-11-14 21:44:17 -0800338 if sdkDep.frameworkResModule != "" {
339 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700340 }
341}
342
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800343var extractAssetsRule = pctx.AndroidStaticRule("extractAssets",
344 blueprint.RuleParams{
345 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "assets/**/*"`,
346 CommandDeps: []string{"${config.Zip2ZipCmd}"},
347 })
348
Alixf7a10272023-09-27 16:47:56 +0000349type aaptBuildActionOptions struct {
350 sdkContext android.SdkContext
351 classLoaderContexts dexpreopt.ClassLoaderContextMap
352 excludedLibs []string
353 enforceDefaultTargetSdkVersion bool
Rico Winda2fa2632024-03-13 13:09:17 +0100354 forceNonFinalResourceIDs bool
Alixf7a10272023-09-27 16:47:56 +0000355 extraLinkFlags []string
Jihoon Kang84b25892023-12-01 22:01:06 +0000356 aconfigTextFiles android.Paths
Alixf7a10272023-09-27 16:47:56 +0000357}
358
359func (a *aapt) buildActions(ctx android.ModuleContext, opts aaptBuildActionOptions) {
Colin Cross5446e882019-05-22 10:46:27 -0700360
Colin Cross8676c8c2023-10-12 15:58:57 -0700361 staticResourcesNodesDepSet, sharedResourcesNodesDepSet, staticRRODirsDepSet, staticManifestsDepSet, sharedExportPackages, libFlags :=
Alixf7a10272023-09-27 16:47:56 +0000362 aaptLibs(ctx, opts.sdkContext, opts.classLoaderContexts)
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100363
Paul Duffin06530572022-02-03 17:54:15 +0000364 // Exclude any libraries from the supplied list.
Alixf7a10272023-09-27 16:47:56 +0000365 opts.classLoaderContexts = opts.classLoaderContexts.ExcludeLibs(opts.excludedLibs)
Paul Duffin06530572022-02-03 17:54:15 +0000366
Colin Cross31656952018-05-24 16:11:20 -0700367 // App manifest file
368 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
369 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
370
Gurpreet Singh7deabfa2022-02-10 13:28:35 +0000371 manifestPath := ManifestFixer(ctx, manifestSrcPath, ManifestFixerParams{
Alixf7a10272023-09-27 16:47:56 +0000372 SdkContext: opts.sdkContext,
373 ClassLoaderContexts: opts.classLoaderContexts,
Harshit Mahajan5b8b7302022-06-10 11:24:05 +0000374 IsLibrary: a.isLibrary,
375 DefaultManifestVersion: a.defaultManifestVersion,
376 UseEmbeddedNativeLibs: a.useEmbeddedNativeLibs,
377 UsesNonSdkApis: a.usesNonSdkApis,
378 UseEmbeddedDex: a.useEmbeddedDex,
379 HasNoCode: a.hasNoCode,
380 LoggingParent: a.LoggingParent,
Alixf7a10272023-09-27 16:47:56 +0000381 EnforceDefaultTargetSdkVersion: opts.enforceDefaultTargetSdkVersion,
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000382 })
Colin Cross90c25c62019-04-19 16:22:57 -0700383
Colin Crossab8d1382023-07-14 17:23:41 +0000384 staticDeps := transitiveAarDeps(staticResourcesNodesDepSet.ToList())
Colin Cross8676c8c2023-10-12 15:58:57 -0700385 sharedDeps := transitiveAarDeps(sharedResourcesNodesDepSet.ToList())
Colin Crossab8d1382023-07-14 17:23:41 +0000386
Luca Stefanifd898822019-09-10 22:13:31 +0200387 // Add additional manifest files to transitive manifests.
388 additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)
Colin Crossab8d1382023-07-14 17:23:41 +0000389 transitiveManifestPaths := append(android.Paths{manifestPath}, additionalManifests...)
390 // TODO(b/288358614): Soong has historically not merged manifests from dependencies of android_library_import
391 // modules. Merging manifests from dependencies could remove the need for pom2bp to generate the "-nodeps" copies
392 // of androidx libraries, but doing so triggers errors due to errors introduced by existing dependencies of
393 // android_library_import modules. If this is fixed, staticManifestsDepSet can be dropped completely in favor of
394 // staticResourcesNodesDepSet.manifests()
395 transitiveManifestPaths = append(transitiveManifestPaths, staticManifestsDepSet.ToList()...)
Colin Cross90c25c62019-04-19 16:22:57 -0700396
Colin Crossab8d1382023-07-14 17:23:41 +0000397 if len(transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) {
Alixf7a10272023-09-27 16:47:56 +0000398 manifestMergerParams := ManifestMergerParams{
399 staticLibManifests: transitiveManifestPaths[1:],
Alix96ea88452023-08-31 15:48:23 +0000400 isLibrary: a.isLibrary,
401 packageName: a.manifestValues.applicationId,
402 }
Alixf7a10272023-09-27 16:47:56 +0000403 a.mergedManifestFile = manifestMerger(ctx, transitiveManifestPaths[0], manifestMergerParams)
Colin Cross90c25c62019-04-19 16:22:57 -0700404 if !a.isLibrary {
405 // Only use the merged manifest for applications. For libraries, the transitive closure of manifests
406 // will be propagated to the final application and merged there. The merged manifest for libraries is
407 // only passed to Make, which can't handle transitive dependencies.
408 manifestPath = a.mergedManifestFile
409 }
410 } else {
411 a.mergedManifestFile = manifestPath
412 }
Colin Cross31656952018-05-24 16:11:20 -0700413
Alixf7a10272023-09-27 16:47:56 +0000414 compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, opts.sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700415
416 linkFlags = append(linkFlags, libFlags...)
Colin Cross8676c8c2023-10-12 15:58:57 -0700417 linkDeps = append(linkDeps, sharedExportPackages...)
Colin Crossab8d1382023-07-14 17:23:41 +0000418 linkDeps = append(linkDeps, staticDeps.resPackages()...)
Alixf7a10272023-09-27 16:47:56 +0000419 linkFlags = append(linkFlags, opts.extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700420 if a.isLibrary {
421 linkFlags = append(linkFlags, "--static-lib")
422 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700423
Colin Cross7c4dc5d2024-02-13 14:29:45 -0800424 linkFlags = append(linkFlags, "--no-static-lib-packages")
Colin Cross8f1b0332024-01-25 13:39:06 -0800425 if a.isLibrary && a.useResourceProcessorBusyBox(ctx) {
Colin Cross7c4dc5d2024-02-13 14:29:45 -0800426 // When building an android_library using ResourceProcessorBusyBox pass --merge-only to skip resource
427 // references validation until the final app link step when all static libraries are present.
Colin Cross4eae06d2023-06-20 22:40:02 -0700428 linkFlags = append(linkFlags, "--merge-only")
Colin Cross4eae06d2023-06-20 22:40:02 -0700429 }
430
Colin Crossa97c5d32018-03-28 14:58:31 -0700431 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
Colin Crossa97c5d32018-03-28 14:58:31 -0700432 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
433 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700434 // This file isn't used by Soong, but is generated for exporting
435 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Cross4eae06d2023-06-20 22:40:02 -0700436 var transitiveRJars android.Paths
Colin Crossf3b7bad2023-08-02 15:49:00 -0700437 var srcJar android.WritablePath
Colin Crossa97c5d32018-03-28 14:58:31 -0700438
Colin Cross4aaa84a2018-08-21 15:14:37 -0700439 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700440 for _, dir := range resDirs {
Colin Cross014489c2020-06-02 20:09:13 -0700441 a.resourceFiles = append(a.resourceFiles, dir.files...)
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900442 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags, a.filterProduct()).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700443 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700444
Colin Crossa592e3e2019-02-19 16:59:53 -0800445 for i, zip := range resZips {
446 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
Colin Crossa0ba2f52019-06-22 12:59:27 -0700447 aapt2CompileZip(ctx, flata, zip, "", compileFlags)
Colin Crossa592e3e2019-02-19 16:59:53 -0800448 compiledResDirs = append(compiledResDirs, android.Paths{flata})
449 }
450
Colin Cross4aaa84a2018-08-21 15:14:37 -0700451 var compiledRes, compiledOverlay android.Paths
452
Colin Crossab8d1382023-07-14 17:23:41 +0000453 // AAPT2 overlays are in lowest to highest priority order, reverse the topological order
454 // of transitiveStaticLibs.
455 transitiveStaticLibs := android.ReversePaths(staticDeps.resPackages())
456
Colin Cross8f1b0332024-01-25 13:39:06 -0800457 if a.isLibrary && a.useResourceProcessorBusyBox(ctx) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700458 // When building an android_library with ResourceProcessorBusyBox enabled treat static library dependencies
459 // as imports. The resources from dependencies will not be merged into this module's package-res.apk, and
460 // instead modules depending on this module will reference package-res.apk from all transitive static
461 // dependencies.
Colin Cross1d3f5902024-03-05 11:51:54 -0800462 for _, sharedDep := range sharedDeps {
463 if sharedDep.usedResourceProcessor {
464 transitiveRJars = append(transitiveRJars, sharedDep.rJar)
465 }
466 }
Colin Cross4eae06d2023-06-20 22:40:02 -0700467 for _, staticDep := range staticDeps {
468 linkDeps = append(linkDeps, staticDep.resPackage)
469 linkFlags = append(linkFlags, "-I "+staticDep.resPackage.String())
470 if staticDep.usedResourceProcessor {
471 transitiveRJars = append(transitiveRJars, staticDep.rJar)
472 }
473 }
474 } else {
475 // When building an app or building a library without ResourceProcessorBusyBox enabled all static
476 // dependencies are compiled into this module's package-res.apk as overlays.
477 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
478 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700479
Colin Crossbec85302019-02-13 13:15:46 -0800480 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700481 // If we are using static android libraries, every source file becomes an overlay.
482 // This is to emulate old AAPT behavior which simulated library support.
483 for _, compiledResDir := range compiledResDirs {
484 compiledOverlay = append(compiledOverlay, compiledResDir...)
485 }
Colin Crossbec85302019-02-13 13:15:46 -0800486 } else if a.isLibrary {
487 // Otherwise, for a static library we treat all the resources equally with no overlay.
488 for _, compiledResDir := range compiledResDirs {
489 compiledRes = append(compiledRes, compiledResDir...)
490 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700491 } else if len(compiledResDirs) > 0 {
492 // Without static libraries, the first directory is our directory, which can then be
493 // overlaid by the rest.
494 compiledRes = append(compiledRes, compiledResDirs[0]...)
495 for _, compiledResDir := range compiledResDirs[1:] {
496 compiledOverlay = append(compiledOverlay, compiledResDir...)
497 }
498 }
499
Colin Crossa97c5d32018-03-28 14:58:31 -0700500 for _, dir := range overlayDirs {
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900501 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags, a.filterProduct()).Paths()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700502 }
503
Colin Crosse560c4a2019-03-19 16:03:11 -0700504 var splitPackages android.WritablePaths
505 var splits []split
506
507 for _, s := range a.splitNames {
508 suffix := strings.Replace(s, ",", "_", -1)
509 path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
510 linkFlags = append(linkFlags, "--split", path.String()+":"+s)
511 splitPackages = append(splitPackages, path)
512 splits = append(splits, split{
513 name: s,
514 suffix: suffix,
515 path: path,
516 })
517 }
518
Colin Cross8f1b0332024-01-25 13:39:06 -0800519 if !a.useResourceProcessorBusyBox(ctx) {
Colin Crossf3b7bad2023-08-02 15:49:00 -0700520 // the subdir "android" is required to be filtered by package names
521 srcJar = android.PathForModuleGen(ctx, "android", "R.srcjar")
522 }
523
Colin Crossab8d1382023-07-14 17:23:41 +0000524 // No need to specify assets from dependencies to aapt2Link for libraries, all transitive assets will be
525 // provided to the final app aapt2Link step.
526 var transitiveAssets android.Paths
527 if !a.isLibrary {
528 transitiveAssets = android.ReverseSliceInPlace(staticDeps.assets())
529 }
Colin Crossf3b7bad2023-08-02 15:49:00 -0700530 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt,
Jihoon Kang84b25892023-12-01 22:01:06 +0000531 linkFlags, linkDeps, compiledRes, compiledOverlay, transitiveAssets, splitPackages,
532 opts.aconfigTextFiles)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800533 // Extract assets from the resource package output so that they can be used later in aapt2link
534 // for modules that depend on this one.
Colin Crossab8d1382023-07-14 17:23:41 +0000535 if android.PrefixInList(linkFlags, "-A ") {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800536 assets := android.PathForModuleOut(ctx, "assets.zip")
537 ctx.Build(pctx, android.BuildParams{
538 Rule: extractAssetsRule,
539 Input: packageRes,
540 Output: assets,
541 Description: "extract assets from built resource file",
542 })
543 a.assetPackage = android.OptionalPathForPath(assets)
544 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700545
Colin Cross8f1b0332024-01-25 13:39:06 -0800546 if a.useResourceProcessorBusyBox(ctx) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700547 rJar := android.PathForModuleOut(ctx, "busybox/R.jar")
Rico Winda2fa2632024-03-13 13:09:17 +0100548 resourceProcessorBusyBoxGenerateBinaryR(ctx, rTxt, a.mergedManifestFile, rJar, staticDeps, a.isLibrary, a.aaptProperties.Aaptflags,
549 opts.forceNonFinalResourceIDs)
Colin Crossf3b7bad2023-08-02 15:49:00 -0700550 aapt2ExtractExtraPackages(ctx, extraPackages, rJar)
Colin Cross4eae06d2023-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 Cross4eae06d2023-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 Cross1d3f5902024-03-05 11:51:54 -0800564 // Reverse the list of R.jar files so that the current module comes first, and direct dependencies come before
565 // transitive dependencies.
566 transitiveRJars = android.ReversePaths(transitiveRJars)
567
Colin Crossa97c5d32018-03-28 14:58:31 -0700568 a.aaptSrcJar = srcJar
Colin Cross4eae06d2023-06-20 22:40:02 -0700569 a.transitiveAaptRJars = transitiveRJars
Colin Cross312634e2023-11-21 15:13:56 -0800570 a.transitiveAaptResourcePackagesFile = transitiveAaptResourcePackagesFile
Colin Crossa97c5d32018-03-28 14:58:31 -0700571 a.exportPackage = packageRes
572 a.manifestPath = manifestPath
573 a.proguardOptionsFile = proguardOptionsFile
Colin Cross66f78822018-05-02 12:58:28 -0700574 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700575 a.rTxt = rTxt
Colin Crosse560c4a2019-03-19 16:03:11 -0700576 a.splits = splits
Colin Crossab8d1382023-07-14 17:23:41 +0000577 a.resourcesNodesDepSet = android.NewDepSetBuilder[*resourcesNode](android.TOPOLOGICAL).
578 Direct(&resourcesNode{
579 resPackage: a.exportPackage,
580 manifest: a.manifestPath,
581 additionalManifests: additionalManifests,
Colin Cross4eae06d2023-06-20 22:40:02 -0700582 rTxt: a.rTxt,
583 rJar: a.rJar,
Colin Crossab8d1382023-07-14 17:23:41 +0000584 assets: a.assetPackage,
Colin Cross4eae06d2023-06-20 22:40:02 -0700585
Colin Cross8f1b0332024-01-25 13:39:06 -0800586 usedResourceProcessor: a.useResourceProcessorBusyBox(ctx),
Colin Crossab8d1382023-07-14 17:23:41 +0000587 }).
588 Transitive(staticResourcesNodesDepSet).Build()
589 a.rroDirsDepSet = android.NewDepSetBuilder[rroDir](android.TOPOLOGICAL).
590 Direct(rroDirs...).
591 Transitive(staticRRODirsDepSet).Build()
592 a.manifestsDepSet = android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).
593 Direct(a.manifestPath).
594 DirectSlice(additionalManifests).
595 Transitive(staticManifestsDepSet).Build()
596}
597
Colin Cross4eae06d2023-06-20 22:40:02 -0700598var resourceProcessorBusyBox = pctx.AndroidStaticRule("resourceProcessorBusyBox",
599 blueprint.RuleParams{
600 Command: "${config.JavaCmd} -cp ${config.ResourceProcessorBusyBox} " +
601 "com.google.devtools.build.android.ResourceProcessorBusyBox --tool=GENERATE_BINARY_R -- @${out}.args && " +
602 "if cmp -s ${out}.tmp ${out} ; then rm ${out}.tmp ; else mv ${out}.tmp ${out}; fi",
603 CommandDeps: []string{"${config.ResourceProcessorBusyBox}"},
604 Rspfile: "${out}.args",
605 RspfileContent: "--primaryRTxt ${rTxt} --primaryManifest ${manifest} --classJarOutput ${out}.tmp ${args}",
606 Restat: true,
607 }, "rTxt", "manifest", "args")
608
609// resourceProcessorBusyBoxGenerateBinaryR converts the R.txt file produced by aapt2 into R.class files
610// using Bazel's ResourceProcessorBusyBox tool, which is faster than compiling the R.java files and
611// supports producing classes for static dependencies that only include resources from that dependency.
612func resourceProcessorBusyBoxGenerateBinaryR(ctx android.ModuleContext, rTxt, manifest android.Path,
Rico Winda2fa2632024-03-13 13:09:17 +0100613 rJar android.WritablePath, transitiveDeps transitiveAarDeps, isLibrary bool, aaptFlags []string,
614 forceNonFinalIds bool) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700615
616 var args []string
617 var deps android.Paths
618
619 if !isLibrary {
620 // When compiling an app, pass all R.txt and AndroidManifest.xml from transitive static library dependencies
621 // to ResourceProcessorBusyBox so that it can regenerate R.class files with the final resource IDs for each
622 // package.
623 args, deps = transitiveDeps.resourceProcessorDeps()
Rico Winda2fa2632024-03-13 13:09:17 +0100624 if forceNonFinalIds {
625 args = append(args, "--finalFields=false")
626 }
Colin Cross4eae06d2023-06-20 22:40:02 -0700627 } else {
628 // When compiling a library don't pass any dependencies as it only needs to generate an R.class file for this
629 // library. Pass --finalFields=false so that the R.class file contains non-final fields so they don't get
630 // inlined into the library before the final IDs are assigned during app compilation.
631 args = append(args, "--finalFields=false")
632 }
633
Colin Crossd3f7d1a2024-01-03 19:42:25 -0800634 for i, arg := range aaptFlags {
635 const AAPT_CUSTOM_PACKAGE = "--custom-package"
636 if strings.HasPrefix(arg, AAPT_CUSTOM_PACKAGE) {
637 pkg := strings.TrimSpace(strings.TrimPrefix(arg, AAPT_CUSTOM_PACKAGE))
638 if pkg == "" && i+1 < len(aaptFlags) {
639 pkg = aaptFlags[i+1]
640 }
641 args = append(args, "--packageForR "+pkg)
642 }
643 }
644
Colin Cross4eae06d2023-06-20 22:40:02 -0700645 deps = append(deps, rTxt, manifest)
646
647 ctx.Build(pctx, android.BuildParams{
648 Rule: resourceProcessorBusyBox,
649 Output: rJar,
650 Implicits: deps,
651 Description: "ResourceProcessorBusyBox",
652 Args: map[string]string{
653 "rTxt": rTxt.String(),
654 "manifest": manifest.String(),
655 "args": strings.Join(args, " "),
656 },
657 })
658}
659
Colin Crossab8d1382023-07-14 17:23:41 +0000660type resourcesNode struct {
661 resPackage android.Path
662 manifest android.Path
663 additionalManifests android.Paths
Colin Cross4eae06d2023-06-20 22:40:02 -0700664 rTxt android.Path
665 rJar android.Path
Colin Crossab8d1382023-07-14 17:23:41 +0000666 assets android.OptionalPath
Colin Cross4eae06d2023-06-20 22:40:02 -0700667
668 usedResourceProcessor bool
Colin Crossab8d1382023-07-14 17:23:41 +0000669}
670
671type transitiveAarDeps []*resourcesNode
672
673func (t transitiveAarDeps) resPackages() android.Paths {
Colin Cross4eae06d2023-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.resPackage)
677 }
Colin Cross4eae06d2023-06-20 22:40:02 -0700678 return paths
Colin Crossab8d1382023-07-14 17:23:41 +0000679}
680
681func (t transitiveAarDeps) manifests() android.Paths {
Colin Cross4eae06d2023-06-20 22:40:02 -0700682 paths := make(android.Paths, 0, len(t))
Colin Crossab8d1382023-07-14 17:23:41 +0000683 for _, dep := range t {
684 paths = append(paths, dep.manifest)
685 paths = append(paths, dep.additionalManifests...)
686 }
Colin Cross4eae06d2023-06-20 22:40:02 -0700687 return paths
688}
689
690func (t transitiveAarDeps) resourceProcessorDeps() (args []string, deps android.Paths) {
691 for _, dep := range t {
692 args = append(args, "--library="+dep.rTxt.String()+","+dep.manifest.String())
693 deps = append(deps, dep.rTxt, dep.manifest)
694 }
695 return args, deps
Colin Crossab8d1382023-07-14 17:23:41 +0000696}
697
698func (t transitiveAarDeps) assets() android.Paths {
Colin Cross4eae06d2023-06-20 22:40:02 -0700699 paths := make(android.Paths, 0, len(t))
Colin Crossab8d1382023-07-14 17:23:41 +0000700 for _, dep := range t {
701 if dep.assets.Valid() {
702 paths = append(paths, dep.assets.Path())
703 }
704 }
705 return paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700706}
707
708// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Jiyong Parkf1691d22021-03-29 20:11:58 +0900709func aaptLibs(ctx android.ModuleContext, sdkContext android.SdkContext, classLoaderContexts dexpreopt.ClassLoaderContextMap) (
Colin Cross8676c8c2023-10-12 15:58:57 -0700710 staticResourcesNodes, sharedResourcesNodes *android.DepSet[*resourcesNode], staticRRODirs *android.DepSet[rroDir],
Colin Crossab8d1382023-07-14 17:23:41 +0000711 staticManifests *android.DepSet[android.Path], sharedLibs android.Paths, flags []string) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700712
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100713 if classLoaderContexts == nil {
Ulya Trafimovich18554242020-11-03 15:55:11 +0000714 // Not all callers need to compute class loader context, those who don't just pass nil.
715 // Create a temporary class loader context here (it will be computed, but not used).
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100716 classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000717 }
718
Colin Cross83bb3162018-06-25 15:48:06 -0700719 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700720 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700721 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700722 }
723
Colin Cross8676c8c2023-10-12 15:58:57 -0700724 var staticResourcesNodeDepSets []*android.DepSet[*resourcesNode]
725 var sharedResourcesNodeDepSets []*android.DepSet[*resourcesNode]
Colin Crossab8d1382023-07-14 17:23:41 +0000726 rroDirsDepSetBuilder := android.NewDepSetBuilder[rroDir](android.TOPOLOGICAL)
727 manifestsDepSetBuilder := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL)
728
Colin Crossa97c5d32018-03-28 14:58:31 -0700729 ctx.VisitDirectDeps(func(module android.Module) {
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000730 depTag := ctx.OtherModuleDependencyTag(module)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000731
Colin Crossa97c5d32018-03-28 14:58:31 -0700732 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700733 aarDep, _ := module.(AndroidLibraryDependency)
734 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700735 exportPackage = aarDep.ExportPackage()
736 }
737
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000738 switch depTag {
Colin Cross4b964c02018-10-15 16:18:06 -0700739 case instrumentationForTag:
740 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Liz Kammeref28a4c2022-09-23 16:50:56 -0400741 case sdkLibTag, libTag:
Colin Cross5446e882019-05-22 10:46:27 -0700742 if exportPackage != nil {
Colin Cross8676c8c2023-10-12 15:58:57 -0700743 sharedResourcesNodeDepSets = append(sharedResourcesNodeDepSets, aarDep.ResourcesNodeDepSet())
Colin Cross5446e882019-05-22 10:46:27 -0700744 sharedLibs = append(sharedLibs, exportPackage)
745 }
Colin Cross5446e882019-05-22 10:46:27 -0700746 case frameworkResTag:
Colin Crossa97c5d32018-03-28 14:58:31 -0700747 if exportPackage != nil {
748 sharedLibs = append(sharedLibs, exportPackage)
749 }
750 case staticLibTag:
751 if exportPackage != nil {
Colin Cross8676c8c2023-10-12 15:58:57 -0700752 staticResourcesNodeDepSets = append(staticResourcesNodeDepSets, aarDep.ResourcesNodeDepSet())
Colin Crossab8d1382023-07-14 17:23:41 +0000753 rroDirsDepSetBuilder.Transitive(aarDep.RRODirsDepSet())
754 manifestsDepSetBuilder.Transitive(aarDep.ManifestsDepSet())
Colin Crossa97c5d32018-03-28 14:58:31 -0700755 }
756 }
Ulya Trafimovich18554242020-11-03 15:55:11 +0000757
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +0000758 addCLCFromDep(ctx, module, classLoaderContexts)
Colin Crossa97c5d32018-03-28 14:58:31 -0700759 })
760
Colin Crossab8d1382023-07-14 17:23:41 +0000761 // AAPT2 overlays are in lowest to highest priority order, the topological order will be reversed later.
762 // Reverse the dependency order now going into the depset so that it comes out in order after the second
763 // reverse later.
764 // NOTE: this is legacy and probably incorrect behavior, for most other cases (e.g. conflicting classes in
765 // dependencies) the highest priority dependency is listed first, but for resources the highest priority
766 // dependency has to be listed last.
767 staticResourcesNodes = android.NewDepSet(android.TOPOLOGICAL, nil,
Colin Cross8676c8c2023-10-12 15:58:57 -0700768 android.ReverseSliceInPlace(staticResourcesNodeDepSets))
769 sharedResourcesNodes = android.NewDepSet(android.TOPOLOGICAL, nil,
770 android.ReverseSliceInPlace(sharedResourcesNodeDepSets))
Colin Crossa97c5d32018-03-28 14:58:31 -0700771
Colin Crossab8d1382023-07-14 17:23:41 +0000772 staticRRODirs = rroDirsDepSetBuilder.Build()
773 staticManifests = manifestsDepSetBuilder.Build()
774
775 if len(staticResourcesNodes.ToList()) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700776 flags = append(flags, "--auto-add-overlay")
777 }
778
779 for _, sharedLib := range sharedLibs {
780 flags = append(flags, "-I "+sharedLib.String())
781 }
782
Colin Cross8676c8c2023-10-12 15:58:57 -0700783 return staticResourcesNodes, sharedResourcesNodes, staticRRODirs, staticManifests, sharedLibs, flags
Colin Crossa97c5d32018-03-28 14:58:31 -0700784}
785
786type AndroidLibrary struct {
787 Library
788 aapt
789
790 androidLibraryProperties androidLibraryProperties
791
792 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700793}
794
Saeid Farivar Asanjan1fca3012021-09-14 18:40:19 +0000795var _ android.OutputFileProducer = (*AndroidLibrary)(nil)
796
797// For OutputFileProducer interface
798func (a *AndroidLibrary) OutputFiles(tag string) (android.Paths, error) {
799 switch tag {
800 case ".aar":
801 return []android.Path{a.aarFile}, nil
802 default:
803 return a.Library.OutputFiles(tag)
804 }
805}
806
Colin Crossa97c5d32018-03-28 14:58:31 -0700807var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
808
809func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
810 a.Module.deps(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900811 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
Paul Duffin250e6192019-06-07 10:44:37 +0100812 if sdkDep.hasFrameworkLibs() {
813 a.aapt.deps(ctx, sdkDep)
Colin Crossa97c5d32018-03-28 14:58:31 -0700814 }
Colin Cross4a80a152022-12-21 21:51:52 -0800815 a.usesLibrary.deps(ctx, false)
Jihoon Kang9049c272024-03-19 21:57:36 +0000816
817 for _, aconfig_declaration := range a.aaptProperties.Flags_packages {
818 ctx.AddDependency(ctx.Module(), aconfigDeclarationTag, aconfig_declaration)
819 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700820}
821
822func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800823 a.aapt.isLibrary = true
Ulya Trafimovich42c7f0d2021-08-17 16:20:29 +0100824 a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Spandan Das0727ba72024-02-13 16:37:43 +0000825 if a.usesLibrary.shouldDisableDexpreopt {
826 a.dexpreopter.disableDexpreopt()
827 }
Alixf7a10272023-09-27 16:47:56 +0000828 a.aapt.buildActions(ctx,
829 aaptBuildActionOptions{
830 sdkContext: android.SdkContext(a),
831 classLoaderContexts: a.classLoaderContexts,
832 enforceDefaultTargetSdkVersion: false,
Jihoon Kang9049c272024-03-19 21:57:36 +0000833 aconfigTextFiles: getAconfigFilePaths(ctx),
Alixf7a10272023-09-27 16:47:56 +0000834 },
835 )
Colin Crossa97c5d32018-03-28 14:58:31 -0700836
Colin Crossff694a82023-12-13 15:54:49 -0800837 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
838 a.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -0700839
yangbill2af0b6e2024-03-15 09:29:29 +0000840 a.stem = proptools.StringDefault(a.overridableProperties.Stem, ctx.ModuleName())
Jihoon Kang1bfb6f22023-07-01 00:13:47 +0000841
Colin Cross4eae06d2023-06-20 22:40:02 -0700842 ctx.CheckbuildFile(a.aapt.proguardOptionsFile)
843 ctx.CheckbuildFile(a.aapt.exportPackage)
Colin Cross8f1b0332024-01-25 13:39:06 -0800844 if a.useResourceProcessorBusyBox(ctx) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700845 ctx.CheckbuildFile(a.aapt.rJar)
Colin Crossf3b7bad2023-08-02 15:49:00 -0700846 } else {
847 ctx.CheckbuildFile(a.aapt.aaptSrcJar)
Colin Cross4eae06d2023-06-20 22:40:02 -0700848 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700849
850 // apps manifests are handled by aapt, don't let Module see them
851 a.properties.Manifest = nil
852
Colin Cross014489c2020-06-02 20:09:13 -0700853 a.linter.mergedManifest = a.aapt.mergedManifestFile
854 a.linter.manifest = a.aapt.manifestPath
855 a.linter.resources = a.aapt.resourceFiles
856
Sam Delmericoc8e040c2023-10-31 17:27:02 +0000857 proguardSpecInfo := a.collectProguardSpecInfo(ctx)
Colin Cross40213022023-12-13 15:19:49 -0800858 android.SetProvider(ctx, ProguardSpecInfoProvider, proguardSpecInfo)
Colin Cross312634e2023-11-21 15:13:56 -0800859 exportedProguardFlagsFiles := proguardSpecInfo.ProguardFlagsFiles.ToList()
860 a.extraProguardFlagsFiles = append(a.extraProguardFlagsFiles, exportedProguardFlagsFiles...)
861 a.extraProguardFlagsFiles = append(a.extraProguardFlagsFiles, a.proguardOptionsFile)
862
863 combinedExportedProguardFlagFile := android.PathForModuleOut(ctx, "export_proguard_flags")
864 writeCombinedProguardFlagsFile(ctx, combinedExportedProguardFlagFile, exportedProguardFlagsFiles)
865 a.combinedExportedProguardFlagsFile = combinedExportedProguardFlagFile
Colin Crossa97c5d32018-03-28 14:58:31 -0700866
Colin Cross4eae06d2023-06-20 22:40:02 -0700867 var extraSrcJars android.Paths
868 var extraCombinedJars android.Paths
869 var extraClasspathJars android.Paths
Colin Cross8f1b0332024-01-25 13:39:06 -0800870 if a.useResourceProcessorBusyBox(ctx) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700871 // When building a library with ResourceProcessorBusyBox enabled ResourceProcessorBusyBox for this
872 // library and each of the transitive static android_library dependencies has already created an
873 // R.class file for the appropriate package. Add all of those R.class files to the classpath.
874 extraClasspathJars = a.transitiveAaptRJars
875 } else {
876 // When building a library without ResourceProcessorBusyBox the aapt2 rule creates R.srcjar containing
877 // R.java files for the library's package and the packages from all transitive static android_library
878 // dependencies. Compile the srcjar alongside the rest of the sources.
879 extraSrcJars = android.Paths{a.aapt.aaptSrcJar}
880 }
881
882 a.Module.compile(ctx, extraSrcJars, extraClasspathJars, extraCombinedJars)
Colin Crossa97c5d32018-03-28 14:58:31 -0700883
Colin Crossf57c5782019-01-25 13:20:38 -0800884 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700885 var res android.Paths
886 if a.androidLibraryProperties.BuildAAR {
887 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
888 ctx.CheckbuildFile(a.aarFile)
889 }
Colin Cross89c31582018-04-30 15:55:11 -0700890
Sam Delmerico82602492022-06-10 17:05:42 +0000891 prebuiltJniPackages := android.Paths{}
892 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800893 if info, ok := android.OtherModuleProvider(ctx, module, JniPackageProvider); ok {
Sam Delmerico82602492022-06-10 17:05:42 +0000894 prebuiltJniPackages = append(prebuiltJniPackages, info.JniPackages...)
895 }
896 })
897 if len(prebuiltJniPackages) > 0 {
Colin Cross40213022023-12-13 15:19:49 -0800898 android.SetProvider(ctx, JniPackageProvider, JniPackageInfo{
Sam Delmerico82602492022-06-10 17:05:42 +0000899 JniPackages: prebuiltJniPackages,
900 })
901 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700902}
903
Colin Cross95b53b82023-10-17 13:21:02 -0700904func (a *AndroidLibrary) IDEInfo(dpInfo *android.IdeInfo) {
905 a.Library.IDEInfo(dpInfo)
906 a.aapt.IDEInfo(dpInfo)
907}
908
909func (a *aapt) IDEInfo(dpInfo *android.IdeInfo) {
Colin Cross8f1b0332024-01-25 13:39:06 -0800910 if a.rJar != nil {
Colin Cross95b53b82023-10-17 13:21:02 -0700911 dpInfo.Jars = append(dpInfo.Jars, a.rJar.String())
912 }
913}
914
Colin Cross1b16b0e2019-02-12 14:41:32 -0800915// android_library builds and links sources into a `.jar` file for the device along with Android resources.
916//
917// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
Sam Delmerico82602492022-06-10 17:05:42 +0000918// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
Colin Cross1b16b0e2019-02-12 14:41:32 -0800919// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
920// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700921func AndroidLibraryFactory() android.Module {
922 module := &AndroidLibrary{}
923
Colin Crossce6734e2020-06-15 16:09:53 -0700924 module.Module.addHostAndDeviceProperties()
Colin Crossa97c5d32018-03-28 14:58:31 -0700925 module.AddProperties(
Colin Crossa97c5d32018-03-28 14:58:31 -0700926 &module.aaptProperties,
927 &module.androidLibraryProperties)
928
929 module.androidLibraryProperties.BuildAAR = true
Colin Cross014489c2020-06-02 20:09:13 -0700930 module.Module.linter.library = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700931
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900932 android.InitApexModule(module)
Colin Cross48de9a42018-10-02 13:53:33 -0700933 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700934 return module
935}
936
Colin Crossfabb6082018-02-20 17:22:23 -0800937//
938// AAR (android library) prebuilts
939//
Colin Crossfabb6082018-02-20 17:22:23 -0800940
Vinh Trance0781f2022-04-13 01:30:44 +0000941// Properties for android_library_import
Colin Crossfabb6082018-02-20 17:22:23 -0800942type AARImportProperties struct {
Vinh Trance0781f2022-04-13 01:30:44 +0000943 // ARR (android library prebuilt) filepath. Exactly one ARR is required.
Colin Cross27b922f2019-03-04 22:35:41 -0800944 Aars []string `android:"path"`
Vinh Trance0781f2022-04-13 01:30:44 +0000945 // If not blank, set to the version of the sdk to compile against.
946 // Defaults to private.
947 // Values are of one of the following forms:
948 // 1) numerical API level, "current", "none", or "core_platform"
949 // 2) An SDK kind with an API level: "<sdk kind>_<API level>"
950 // See build/soong/android/sdk_version.go for the complete and up to date list of SDK kinds.
951 // If the SDK kind is empty, it will be set to public
952 Sdk_version *string
953 // If not blank, set the minimum version of the sdk that the compiled artifacts will run against.
954 // Defaults to sdk_version if not set. See sdk_version for possible values.
Colin Cross479884c2018-07-10 13:39:30 -0700955 Min_sdk_version *string
Vinh Trance0781f2022-04-13 01:30:44 +0000956 // List of java static libraries that the included ARR (android library prebuilts) has dependencies to.
Colin Crossa97c5d32018-03-28 14:58:31 -0700957 Static_libs []string
Vinh Trance0781f2022-04-13 01:30:44 +0000958 // List of java libraries that the included ARR (android library prebuilts) has dependencies to.
959 Libs []string
960 // If set to true, run Jetifier against .aar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -0700961 Jetifier *bool
Sam Delmerico82602492022-06-10 17:05:42 +0000962 // If true, extract JNI libs from AAR archive. These libs will be accessible to android_app modules and
963 // will be passed transitively through android_libraries to an android_app.
964 //TODO(b/241138093) evaluate whether we can have this flag default to true for Bazel conversion
965 Extract_jni *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800966}
967
968type AARImport struct {
969 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700970 android.DefaultableModuleBase
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900971 android.ApexModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800972 prebuilt android.Prebuilt
973
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900974 // Functionality common to Module and Import.
975 embeddableInModuleAndImport
976
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500977 providesTransitiveHeaderJars
978
Colin Crossfabb6082018-02-20 17:22:23 -0800979 properties AARImportProperties
980
Colin Cross312634e2023-11-21 15:13:56 -0800981 classpathFile android.WritablePath
982 proguardFlags android.WritablePath
983 exportPackage android.WritablePath
984 transitiveAaptResourcePackagesFile android.Path
985 extraAaptPackagesFile android.WritablePath
986 manifest android.WritablePath
987 assetsPackage android.WritablePath
988 rTxt android.WritablePath
989 rJar android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700990
Colin Crossab8d1382023-07-14 17:23:41 +0000991 resourcesNodesDepSet *android.DepSet[*resourcesNode]
992 manifestsDepSet *android.DepSet[android.Path]
Colin Cross56a83212020-09-15 18:30:11 -0700993
994 hideApexVariantFromMake bool
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000995
Sam Delmerico82602492022-06-10 17:05:42 +0000996 aarPath android.Path
997 jniPackages android.Paths
Jiyong Park92315372021-04-02 08:45:46 +0900998
999 sdkVersion android.SdkSpec
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001000 minSdkVersion android.ApiLevel
LaMont Jonesafe7baf2024-01-09 22:47:39 +00001001
1002 // Single aconfig "cache file" merged from this module and all dependencies.
1003 mergedAconfigFiles map[string]android.Paths
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001004}
1005
1006var _ android.OutputFileProducer = (*AARImport)(nil)
1007
1008// For OutputFileProducer interface
1009func (a *AARImport) OutputFiles(tag string) (android.Paths, error) {
1010 switch tag {
1011 case ".aar":
1012 return []android.Path{a.aarPath}, nil
1013 case "":
1014 return []android.Path{a.classpathFile}, nil
1015 default:
1016 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1017 }
Colin Crossfabb6082018-02-20 17:22:23 -08001018}
1019
Jiyong Park92315372021-04-02 08:45:46 +09001020func (a *AARImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1021 return android.SdkSpecFrom(ctx, String(a.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07001022}
1023
Jiyong Parkf1691d22021-03-29 20:11:58 +09001024func (a *AARImport) SystemModules() string {
Paul Duffine25c6442019-10-11 13:50:28 +01001025 return ""
1026}
1027
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001028func (a *AARImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Colin Cross479884c2018-07-10 13:39:30 -07001029 if a.properties.Min_sdk_version != nil {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001030 return android.ApiLevelFrom(ctx, *a.properties.Min_sdk_version)
Colin Cross479884c2018-07-10 13:39:30 -07001031 }
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001032 return a.SdkVersion(ctx).ApiLevel
Colin Cross83bb3162018-06-25 15:48:06 -07001033}
1034
Spandan Dasa26eda72023-03-02 00:56:06 +00001035func (a *AARImport) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
1036 return android.SdkSpecFrom(ctx, "").ApiLevel
William Loh5a082f92022-05-17 20:21:50 +00001037}
1038
Spandan Dasca70fc42023-03-01 23:38:49 +00001039func (a *AARImport) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
1040 return a.SdkVersion(ctx).ApiLevel
Dan Willemsen419290a2018-10-31 15:28:47 -07001041}
1042
Colin Cross1e743852019-10-28 11:37:20 -07001043func (a *AARImport) javaVersion() string {
1044 return ""
1045}
1046
Colin Crossa97c5d32018-03-28 14:58:31 -07001047var _ AndroidLibraryDependency = (*AARImport)(nil)
1048
1049func (a *AARImport) ExportPackage() android.Path {
1050 return a.exportPackage
1051}
Colin Crossab8d1382023-07-14 17:23:41 +00001052func (a *AARImport) ResourcesNodeDepSet() *android.DepSet[*resourcesNode] {
1053 return a.resourcesNodesDepSet
Colin Crossc1c37552019-01-31 11:42:41 -08001054}
1055
Colin Crossab8d1382023-07-14 17:23:41 +00001056func (a *AARImport) RRODirsDepSet() *android.DepSet[rroDir] {
1057 return android.NewDepSet[rroDir](android.TOPOLOGICAL, nil, nil)
Colin Cross66f78822018-05-02 12:58:28 -07001058}
1059
Colin Crossab8d1382023-07-14 17:23:41 +00001060func (a *AARImport) ManifestsDepSet() *android.DepSet[android.Path] {
1061 return a.manifestsDepSet
Jaewoong Jung6431ca72020-01-15 14:15:10 -08001062}
1063
Jaewoong Jungc779cd42020-10-06 18:56:10 -07001064// RRO enforcement is not available on aar_import since its RRO dirs are not
1065// exported.
1066func (a *AARImport) SetRROEnforcedForDependent(enforce bool) {
1067}
1068
1069// RRO enforcement is not available on aar_import since its RRO dirs are not
1070// exported.
1071func (a *AARImport) IsRROEnforced(ctx android.BaseModuleContext) bool {
1072 return false
1073}
1074
Colin Crossfabb6082018-02-20 17:22:23 -08001075func (a *AARImport) Prebuilt() *android.Prebuilt {
1076 return &a.prebuilt
1077}
1078
1079func (a *AARImport) Name() string {
1080 return a.prebuilt.Name(a.ModuleBase.Name())
1081}
1082
Jiyong Park618922e2020-01-08 13:35:43 +09001083func (a *AARImport) JacocoReportClassesFile() android.Path {
1084 return nil
1085}
1086
Colin Crossfabb6082018-02-20 17:22:23 -08001087func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Jeongik Cha816a23a2020-07-08 01:09:23 +09001088 if !ctx.Config().AlwaysUsePrebuiltSdks() {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001089 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -07001090 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -07001091 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -08001092 }
1093 }
Colin Crossa97c5d32018-03-28 14:58:31 -07001094
Colin Cross42d48b72018-08-29 14:10:52 -07001095 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
1096 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -08001097}
1098
Sam Delmerico82602492022-06-10 17:05:42 +00001099type JniPackageInfo struct {
1100 // List of zip files containing JNI libraries
1101 // Zip files should have directory structure jni/<arch>/*.so
1102 JniPackages android.Paths
1103}
1104
Colin Crossbc7d76c2023-12-12 16:39:03 -08001105var JniPackageProvider = blueprint.NewProvider[JniPackageInfo]()
Sam Delmerico82602492022-06-10 17:05:42 +00001106
1107// Unzip an AAR and extract the JNI libs for $archString.
1108var extractJNI = pctx.AndroidStaticRule("extractJNI",
1109 blueprint.RuleParams{
1110 Command: `rm -rf $out $outDir && touch $out && ` +
1111 `unzip -qoDD -d $outDir $in "jni/${archString}/*" && ` +
1112 `jni_files=$$(find $outDir/jni -type f) && ` +
1113 // print error message if there are no JNI libs for this arch
1114 `[ -n "$$jni_files" ] || (echo "ERROR: no JNI libs found for arch ${archString}" && exit 1) && ` +
Sam Delmerico80ee45c2023-06-22 15:36:02 -04001115 `${config.SoongZipCmd} -o $out -L 0 -P 'lib/${archString}' ` +
Sam Delmerico82602492022-06-10 17:05:42 +00001116 `-C $outDir/jni/${archString} $$(echo $$jni_files | xargs -n1 printf " -f %s")`,
1117 CommandDeps: []string{"${config.SoongZipCmd}"},
1118 },
1119 "outDir", "archString")
1120
Colin Crossfabb6082018-02-20 17:22:23 -08001121// 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 -07001122// 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 -08001123var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
1124 blueprint.RuleParams{
Dan Willemsen304cfec2019-05-28 14:49:06 -07001125 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
Colin Cross205e9112020-08-06 13:20:17 -07001126 `unzip -qoDD -d $outDir $in && rm -rf $outDir/res && touch $out && ` +
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001127 `${config.Zip2ZipCmd} -i $in -o $assetsPackage 'assets/**/*' && ` +
Colin Cross205e9112020-08-06 13:20:17 -07001128 `${config.MergeZipsCmd} $combinedClassesJar $$(ls $outDir/classes.jar 2> /dev/null) $$(ls $outDir/libs/*.jar 2> /dev/null)`,
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001129 CommandDeps: []string{"${config.MergeZipsCmd}", "${config.Zip2ZipCmd}"},
Colin Crossfabb6082018-02-20 17:22:23 -08001130 },
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001131 "outDir", "combinedClassesJar", "assetsPackage")
Colin Crossfabb6082018-02-20 17:22:23 -08001132
1133func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1134 if len(a.properties.Aars) != 1 {
1135 ctx.PropertyErrorf("aars", "exactly one aar is required")
1136 return
1137 }
1138
Jiyong Park92315372021-04-02 08:45:46 +09001139 a.sdkVersion = a.SdkVersion(ctx)
1140 a.minSdkVersion = a.MinSdkVersion(ctx)
1141
Colin Crossff694a82023-12-13 15:54:49 -08001142 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1143 a.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07001144
Nan Zhang4c819fb2018-08-27 18:31:46 -07001145 aarName := ctx.ModuleName() + ".aar"
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001146 a.aarPath = android.PathForModuleSrc(ctx, a.properties.Aars[0])
1147
Colin Cross1001a792019-03-21 22:21:39 -07001148 if Bool(a.properties.Jetifier) {
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001149 inputFile := a.aarPath
1150 a.aarPath = android.PathForModuleOut(ctx, "jetifier", aarName)
1151 TransformJetifier(ctx, a.aarPath.(android.WritablePath), inputFile)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001152 }
Colin Crossfabb6082018-02-20 17:22:23 -08001153
1154 extractedAARDir := android.PathForModuleOut(ctx, "aar")
Colin Cross205e9112020-08-06 13:20:17 -07001155 a.classpathFile = extractedAARDir.Join(ctx, "classes-combined.jar")
Colin Cross10f7c4a2018-05-23 10:59:28 -07001156 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossbb77d8e2024-02-15 14:43:47 -08001157 a.rTxt = extractedAARDir.Join(ctx, "R.txt")
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001158 a.assetsPackage = android.PathForModuleOut(ctx, "assets.zip")
Sam Delmerico95d70942023-08-02 18:00:35 -04001159 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross40213022023-12-13 15:19:49 -08001160 android.SetProvider(ctx, ProguardSpecInfoProvider, ProguardSpecInfo{
Sam Delmerico95d70942023-08-02 18:00:35 -04001161 ProguardFlagsFiles: android.NewDepSet[android.Path](
1162 android.POSTORDER,
1163 android.Paths{a.proguardFlags},
1164 nil,
1165 ),
1166 })
Colin Crossfabb6082018-02-20 17:22:23 -08001167
1168 ctx.Build(pctx, android.BuildParams{
1169 Rule: unzipAAR,
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001170 Input: a.aarPath,
Colin Crossbb77d8e2024-02-15 14:43:47 -08001171 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest, a.assetsPackage, a.rTxt},
Colin Crossfabb6082018-02-20 17:22:23 -08001172 Description: "unzip AAR",
1173 Args: map[string]string{
Colin Cross205e9112020-08-06 13:20:17 -07001174 "outDir": extractedAARDir.String(),
1175 "combinedClassesJar": a.classpathFile.String(),
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001176 "assetsPackage": a.assetsPackage.String(),
Colin Crossfabb6082018-02-20 17:22:23 -08001177 },
1178 })
1179
Colin Crossa0ba2f52019-06-22 12:59:27 -07001180 // Always set --pseudo-localize, it will be stripped out later for release
1181 // builds that don't want it.
1182 compileFlags := []string{"--pseudo-localize"}
Colin Crossfabb6082018-02-20 17:22:23 -08001183 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
Colin Crossfabb6082018-02-20 17:22:23 -08001184 flata := compiledResDir.Join(ctx, "gen_res.flata")
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001185 aapt2CompileZip(ctx, flata, a.aarPath, "res", compileFlags)
Colin Crossfabb6082018-02-20 17:22:23 -08001186
1187 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
Colin Crossfabb6082018-02-20 17:22:23 -08001188 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossbb77d8e2024-02-15 14:43:47 -08001189 aaptRTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -07001190 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -08001191
1192 var linkDeps android.Paths
1193
1194 linkFlags := []string{
1195 "--static-lib",
Colin Cross4eae06d2023-06-20 22:40:02 -07001196 "--merge-only",
Colin Crossfabb6082018-02-20 17:22:23 -08001197 "--auto-add-overlay",
Colin Cross7c4dc5d2024-02-13 14:29:45 -08001198 "--no-static-lib-packages",
Colin Crossfabb6082018-02-20 17:22:23 -08001199 }
1200
Colin Cross10f7c4a2018-05-23 10:59:28 -07001201 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
1202 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -08001203
Colin Cross8676c8c2023-10-12 15:58:57 -07001204 staticResourcesNodesDepSet, sharedResourcesNodesDepSet, staticRRODirsDepSet, staticManifestsDepSet, sharedLibs, libFlags :=
Jiyong Parkf1691d22021-03-29 20:11:58 +09001205 aaptLibs(ctx, android.SdkContext(a), nil)
Colin Cross31656952018-05-24 16:11:20 -07001206
Colin Cross8676c8c2023-10-12 15:58:57 -07001207 _ = sharedResourcesNodesDepSet
Colin Crossab8d1382023-07-14 17:23:41 +00001208 _ = staticRRODirsDepSet
Colin Cross8676c8c2023-10-12 15:58:57 -07001209
Colin Crossab8d1382023-07-14 17:23:41 +00001210 staticDeps := transitiveAarDeps(staticResourcesNodesDepSet.ToList())
Colin Crossfabb6082018-02-20 17:22:23 -08001211
Colin Crossab8d1382023-07-14 17:23:41 +00001212 linkDeps = append(linkDeps, sharedLibs...)
Colin Cross4eae06d2023-06-20 22:40:02 -07001213 linkDeps = append(linkDeps, staticDeps.resPackages()...)
Colin Crossa97c5d32018-03-28 14:58:31 -07001214 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -08001215
Colin Cross4eae06d2023-06-20 22:40:02 -07001216 overlayRes := android.Paths{flata}
1217
1218 // Treat static library dependencies of static libraries as imports.
1219 transitiveStaticLibs := staticDeps.resPackages()
1220 linkDeps = append(linkDeps, transitiveStaticLibs...)
1221 for _, staticLib := range transitiveStaticLibs {
1222 linkFlags = append(linkFlags, "-I "+staticLib.String())
1223 }
Colin Crossfabb6082018-02-20 17:22:23 -08001224
Colin Crossab8d1382023-07-14 17:23:41 +00001225 transitiveAssets := android.ReverseSliceInPlace(staticDeps.assets())
Colin Crossbb77d8e2024-02-15 14:43:47 -08001226 aapt2Link(ctx, a.exportPackage, nil, proguardOptionsFile, aaptRTxt,
Jihoon Kang84b25892023-12-01 22:01:06 +00001227 linkFlags, linkDeps, nil, overlayRes, transitiveAssets, nil, nil)
Colin Crossfabb6082018-02-20 17:22:23 -08001228
Colin Cross4eae06d2023-06-20 22:40:02 -07001229 a.rJar = android.PathForModuleOut(ctx, "busybox/R.jar")
Rico Winda2fa2632024-03-13 13:09:17 +01001230 resourceProcessorBusyBoxGenerateBinaryR(ctx, a.rTxt, a.manifest, a.rJar, nil, true, nil, false)
Colin Cross4eae06d2023-06-20 22:40:02 -07001231
Colin Crossf3b7bad2023-08-02 15:49:00 -07001232 aapt2ExtractExtraPackages(ctx, a.extraAaptPackagesFile, a.rJar)
1233
Colin Crossab8d1382023-07-14 17:23:41 +00001234 resourcesNodesDepSetBuilder := android.NewDepSetBuilder[*resourcesNode](android.TOPOLOGICAL)
1235 resourcesNodesDepSetBuilder.Direct(&resourcesNode{
1236 resPackage: a.exportPackage,
1237 manifest: a.manifest,
Colin Cross4eae06d2023-06-20 22:40:02 -07001238 rTxt: a.rTxt,
1239 rJar: a.rJar,
Colin Crossab8d1382023-07-14 17:23:41 +00001240 assets: android.OptionalPathForPath(a.assetsPackage),
Colin Cross4eae06d2023-06-20 22:40:02 -07001241
1242 usedResourceProcessor: true,
Colin Crossab8d1382023-07-14 17:23:41 +00001243 })
1244 resourcesNodesDepSetBuilder.Transitive(staticResourcesNodesDepSet)
1245 a.resourcesNodesDepSet = resourcesNodesDepSetBuilder.Build()
1246
1247 manifestDepSetBuilder := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(a.manifest)
1248 // TODO(b/288358614): Soong has historically not merged manifests from dependencies of android_library_import
1249 // modules. Merging manifests from dependencies could remove the need for pom2bp to generate the "-nodeps" copies
1250 // of androidx libraries, but doing so triggers errors due to errors introduced by existing dependencies of
1251 // android_library_import modules. If this is fixed, AndroidLibraryDependency.ManifestsDepSet can be dropped
1252 // completely in favor of AndroidLibraryDependency.ResourceNodesDepSet.manifest
1253 //manifestDepSetBuilder.Transitive(transitiveStaticDeps.manifests)
1254 _ = staticManifestsDepSet
1255 a.manifestsDepSet = manifestDepSetBuilder.Build()
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001256
Colin Cross312634e2023-11-21 15:13:56 -08001257 transitiveAaptResourcePackages := staticDeps.resPackages().Strings()
1258 transitiveAaptResourcePackages = slices.DeleteFunc(transitiveAaptResourcePackages, func(p string) bool {
1259 return p == a.exportPackage.String()
1260 })
1261 transitiveAaptResourcePackagesFile := android.PathForModuleOut(ctx, "transitive-res-packages")
1262 android.WriteFileRule(ctx, transitiveAaptResourcePackagesFile, strings.Join(transitiveAaptResourcePackages, "\n"))
1263 a.transitiveAaptResourcePackagesFile = transitiveAaptResourcePackagesFile
Colin Cross4eae06d2023-06-20 22:40:02 -07001264
Sam Delmerico9f9c0a22022-11-29 11:19:37 -05001265 a.collectTransitiveHeaderJars(ctx)
Colin Cross40213022023-12-13 15:19:49 -08001266 android.SetProvider(ctx, JavaInfoProvider, JavaInfo{
Colin Crossdcf71b22021-02-01 13:59:03 -08001267 HeaderJars: android.PathsIfNonNil(a.classpathFile),
Sam Delmerico9f9c0a22022-11-29 11:19:37 -05001268 TransitiveLibsHeaderJars: a.transitiveLibsHeaderJars,
1269 TransitiveStaticLibsHeaderJars: a.transitiveStaticLibsHeaderJars,
Colin Crossdcf71b22021-02-01 13:59:03 -08001270 ImplementationAndResourcesJars: android.PathsIfNonNil(a.classpathFile),
1271 ImplementationJars: android.PathsIfNonNil(a.classpathFile),
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001272 StubsLinkType: Implementation,
Joe Onorato6fe59eb2023-07-16 13:20:33 -07001273 // TransitiveAconfigFiles: // TODO(b/289117800): LOCAL_ACONFIG_FILES for prebuilts
Colin Crossdcf71b22021-02-01 13:59:03 -08001274 })
Sam Delmerico82602492022-06-10 17:05:42 +00001275
1276 if proptools.Bool(a.properties.Extract_jni) {
1277 for _, t := range ctx.MultiTargets() {
1278 arch := t.Arch.Abi[0]
1279 path := android.PathForModuleOut(ctx, arch+"_jni.zip")
1280 a.jniPackages = append(a.jniPackages, path)
1281
1282 outDir := android.PathForModuleOut(ctx, "aarForJni")
1283 aarPath := android.PathForModuleSrc(ctx, a.properties.Aars[0])
1284 ctx.Build(pctx, android.BuildParams{
1285 Rule: extractJNI,
1286 Input: aarPath,
1287 Outputs: android.WritablePaths{path},
1288 Description: "extract JNI from AAR",
1289 Args: map[string]string{
1290 "outDir": outDir.String(),
1291 "archString": arch,
1292 },
1293 })
1294 }
Sam Delmerico82602492022-06-10 17:05:42 +00001295 }
Colin Crosse8eeec92023-12-14 14:50:05 -08001296
Colin Cross40213022023-12-13 15:19:49 -08001297 android.SetProvider(ctx, JniPackageProvider, JniPackageInfo{
Colin Crosse8eeec92023-12-14 14:50:05 -08001298 JniPackages: a.jniPackages,
1299 })
LaMont Jonesafe7baf2024-01-09 22:47:39 +00001300 android.CollectDependencyAconfigFiles(ctx, &a.mergedAconfigFiles)
Colin Crossdcf71b22021-02-01 13:59:03 -08001301}
Colin Crossfabb6082018-02-20 17:22:23 -08001302
1303func (a *AARImport) HeaderJars() android.Paths {
1304 return android.Paths{a.classpathFile}
1305}
1306
Colin Cross331a1212018-08-15 20:40:52 -07001307func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
1308 return android.Paths{a.classpathFile}
1309}
1310
Spandan Das59a4a2b2024-01-09 21:35:56 +00001311func (a *AARImport) DexJarBuildPath(ctx android.ModuleErrorfContext) android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -08001312 return nil
1313}
1314
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001315func (a *AARImport) DexJarInstallPath() android.Path {
1316 return nil
1317}
1318
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001319func (a *AARImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
Jiyong Park1be96912018-05-28 18:02:19 +09001320 return nil
1321}
1322
Jiyong Park45bf82e2020-12-15 22:29:02 +09001323var _ android.ApexModule = (*AARImport)(nil)
1324
1325// Implements android.ApexModule
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09001326func (a *AARImport) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1327 return a.depIsInSameApex(ctx, dep)
1328}
1329
Jiyong Park45bf82e2020-12-15 22:29:02 +09001330// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001331func (g *AARImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1332 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001333 return nil
1334}
1335
Sam Delmericoaf8bb702022-07-25 15:39:32 -04001336var _ android.PrebuiltInterface = (*AARImport)(nil)
Colin Crossfabb6082018-02-20 17:22:23 -08001337
Colin Cross1b16b0e2019-02-12 14:41:32 -08001338// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
1339//
1340// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
1341// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -08001342func AARImportFactory() android.Module {
1343 module := &AARImport{}
1344
1345 module.AddProperties(&module.properties)
1346
1347 android.InitPrebuiltModule(module, &module.properties.Aars)
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09001348 android.InitApexModule(module)
Sam Delmerico82602492022-06-10 17:05:42 +00001349 InitJavaModuleMultiTargets(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -08001350 return module
1351}