blob: 1d05e48166c461e8268a9421b039794c8fea8b3f [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
Jihoon Kangce320f82024-05-07 00:53:28 +0000110
111 // If set, `--non-updatable-system` flag is not passed to aapt2 even when
112 // `--version-code` is not specified in the `aaptFlags` property list or in the manifest.
113 DisableNonUpdatableSystem bool `blueprint:"mutated"`
Colin Crossa97c5d32018-03-28 14:58:31 -0700114}
115
116type aapt struct {
Colin Cross312634e2023-11-21 15:13:56 -0800117 aaptSrcJar android.Path
118 transitiveAaptRJars android.Paths
119 transitiveAaptResourcePackagesFile android.Path
120 exportPackage android.Path
121 manifestPath android.Path
122 proguardOptionsFile android.Path
123 rTxt android.Path
124 rJar android.Path
125 extraAaptPackagesFile android.Path
126 mergedManifestFile android.Path
127 noticeFile android.OptionalPath
128 assetPackage android.OptionalPath
129 isLibrary bool
130 defaultManifestVersion string
131 useEmbeddedNativeLibs bool
132 useEmbeddedDex bool
133 usesNonSdkApis bool
134 hasNoCode bool
135 LoggingParent string
136 resourceFiles android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700137
Colin Crosse560c4a2019-03-19 16:03:11 -0700138 splitNames []string
139 splits []split
140
Colin Crossa97c5d32018-03-28 14:58:31 -0700141 aaptProperties aaptProperties
Colin Crossab8d1382023-07-14 17:23:41 +0000142
143 resourcesNodesDepSet *android.DepSet[*resourcesNode]
144 rroDirsDepSet *android.DepSet[rroDir]
145 manifestsDepSet *android.DepSet[android.Path]
Alix96ea88452023-08-31 15:48:23 +0000146
147 manifestValues struct {
148 applicationId string
149 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700150}
151
Colin Crosse560c4a2019-03-19 16:03:11 -0700152type split struct {
153 name string
154 suffix string
155 path android.Path
156}
157
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700158// Propagate RRO enforcement flag to static lib dependencies transitively.
159func propagateRROEnforcementMutator(ctx android.TopDownMutatorContext) {
160 m := ctx.Module()
161 if d, ok := m.(AndroidLibraryDependency); ok && d.IsRROEnforced(ctx) {
162 ctx.VisitDirectDepsWithTag(staticLibTag, func(d android.Module) {
163 if a, ok := d.(AndroidLibraryDependency); ok {
164 a.SetRROEnforcedForDependent(true)
165 }
166 })
167 }
168}
169
Colin Cross8f1b0332024-01-25 13:39:06 -0800170func (a *aapt) useResourceProcessorBusyBox(ctx android.BaseModuleContext) bool {
Colin Crosseee4ab12024-03-27 11:54:10 -0700171 return BoolDefault(a.aaptProperties.Use_resource_processor, ctx.Config().UseResourceProcessorByDefault()) &&
172 // TODO(b/331641946): remove this when ResourceProcessorBusyBox supports generating shared libraries.
173 !slices.Contains(a.aaptProperties.Aaptflags, "--shared-lib")
Colin Cross4eae06d2023-06-20 22:40:02 -0700174}
175
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900176func (a *aapt) filterProduct() string {
177 return String(a.aaptProperties.Filter_product)
178}
179
Colin Crossa97c5d32018-03-28 14:58:31 -0700180func (a *aapt) ExportPackage() android.Path {
181 return a.exportPackage
182}
Colin Crossab8d1382023-07-14 17:23:41 +0000183func (a *aapt) ResourcesNodeDepSet() *android.DepSet[*resourcesNode] {
184 return a.resourcesNodesDepSet
Colin Crossc1c37552019-01-31 11:42:41 -0800185}
186
Colin Crossab8d1382023-07-14 17:23:41 +0000187func (a *aapt) RRODirsDepSet() *android.DepSet[rroDir] {
188 return a.rroDirsDepSet
Colin Crossc1c37552019-01-31 11:42:41 -0800189}
190
Colin Crossab8d1382023-07-14 17:23:41 +0000191func (a *aapt) ManifestsDepSet() *android.DepSet[android.Path] {
192 return a.manifestsDepSet
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800193}
194
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700195func (a *aapt) SetRROEnforcedForDependent(enforce bool) {
196 a.aaptProperties.RROEnforcedForDependent = enforce
197}
198
199func (a *aapt) IsRROEnforced(ctx android.BaseModuleContext) bool {
200 // True if RRO is enforced for this module or...
201 return ctx.Config().EnforceRROForModule(ctx.ModuleName()) ||
Jeongik Chacee5ba92021-02-19 12:11:51 +0900202 // if RRO is enforced for any of its dependents.
203 a.aaptProperties.RROEnforcedForDependent
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700204}
205
Jiyong Parkf1691d22021-03-29 20:11:58 +0900206func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext android.SdkContext,
Colin Crossa0ba2f52019-06-22 12:59:27 -0700207 manifestPath android.Path) (compileFlags, linkFlags []string, linkDeps android.Paths,
208 resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700209
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800210 hasVersionCode := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-code")
211 hasVersionName := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-name")
Colin Crossa97c5d32018-03-28 14:58:31 -0700212
Colin Crossa97c5d32018-03-28 14:58:31 -0700213 // Flags specified in Android.bp
214 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
215
Eric Miao40eab202023-03-30 16:57:17 +0000216 linkFlags = append(linkFlags, "--enable-compact-entries")
Colin Crossa97c5d32018-03-28 14:58:31 -0700217
218 // Find implicit or explicit asset and resource dirs
Jiakai Zhangba82e282023-10-13 18:08:59 +0100219 assets := android.PathsRelativeToModuleSourceDir(android.SourceInput{
220 Context: ctx,
221 Paths: a.aaptProperties.Assets,
222 IncludeDirs: false,
223 })
Colin Crossa97c5d32018-03-28 14:58:31 -0700224 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
225 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
Colin Cross8a497952019-03-05 22:25:09 -0800226 resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips)
Colin Crossa97c5d32018-03-28 14:58:31 -0700227
Colin Crossa97c5d32018-03-28 14:58:31 -0700228 // Glob directories into lists of paths
229 for _, dir := range resourceDirs {
230 resDirs = append(resDirs, globbedResourceDir{
231 dir: dir,
232 files: androidResourceGlob(ctx, dir),
233 })
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700234 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, a, dir)
Colin Crossa97c5d32018-03-28 14:58:31 -0700235 overlayDirs = append(overlayDirs, resOverlayDirs...)
236 rroDirs = append(rroDirs, resRRODirs...)
237 }
238
Colin Crossc20dc852020-11-10 12:27:45 -0800239 var assetDeps android.Paths
240 for i, dir := range assetDirs {
241 // Add a dependency on every file in the asset directory. This ensures the aapt2
242 // rule will be rerun if one of the files in the asset directory is modified.
243 assetDeps = append(assetDeps, androidResourceGlob(ctx, dir)...)
244
245 // Add a dependency on a file that contains a list of all the files in the asset directory.
246 // This ensures the aapt2 rule will be run if a file is removed from the asset directory,
247 // or a file is added whose timestamp is older than the output of aapt2.
248 assetFileListFile := android.PathForModuleOut(ctx, "asset_dir_globs", strconv.Itoa(i)+".glob")
249 androidResourceGlobList(ctx, dir, assetFileListFile)
250 assetDeps = append(assetDeps, assetFileListFile)
Colin Crossa97c5d32018-03-28 14:58:31 -0700251 }
252
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700253 assetDirStrings := assetDirs.Strings()
254 if a.noticeFile.Valid() {
255 assetDirStrings = append(assetDirStrings, filepath.Dir(a.noticeFile.Path().String()))
Colin Crossc20dc852020-11-10 12:27:45 -0800256 assetDeps = append(assetDeps, a.noticeFile.Path())
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700257 }
Jiakai Zhangba82e282023-10-13 18:08:59 +0100258 if len(assets) > 0 {
259 // aapt2 doesn't support adding individual asset files. Create a temp directory to hold asset
260 // files and pass it to aapt2.
261 tmpAssetDir := android.PathForModuleOut(ctx, "tmp_asset_dir")
262
263 rule := android.NewRuleBuilder(pctx, ctx)
264 rule.Command().
265 Text("rm -rf").Text(tmpAssetDir.String()).
266 Text("&&").
267 Text("mkdir -p").Text(tmpAssetDir.String())
268
269 for _, asset := range assets {
270 output := tmpAssetDir.Join(ctx, asset.Rel())
271 assetDeps = append(assetDeps, output)
272 rule.Command().Text("mkdir -p").Text(filepath.Dir(output.String()))
273 rule.Command().Text("cp").Input(asset).Output(output)
274 }
275
276 rule.Build("tmp_asset_dir", "tmp_asset_dir")
277
278 assetDirStrings = append(assetDirStrings, tmpAssetDir.String())
279 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700280
Colin Crossa97c5d32018-03-28 14:58:31 -0700281 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
282 linkDeps = append(linkDeps, manifestPath)
283
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700284 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirStrings, "-A "))
Colin Crossc20dc852020-11-10 12:27:45 -0800285 linkDeps = append(linkDeps, assetDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700286
Spandan Das50885c02023-02-23 21:31:33 +0000287 // Returns the effective version for {min|target}_sdk_version
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000288 effectiveVersionString := func(sdkVersion android.SdkSpec, minSdkVersion android.ApiLevel) string {
Spandan Das50885c02023-02-23 21:31:33 +0000289 // If {min|target}_sdk_version is current, use sdk_version to determine the effective level
290 // This is necessary for vendor modules.
291 // The effective version does not _only_ depend on {min|target}_sdk_version(level),
292 // but also on the sdk_version (kind+level)
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000293 if minSdkVersion.IsCurrent() {
Spandan Das50885c02023-02-23 21:31:33 +0000294 ret, err := sdkVersion.EffectiveVersionString(ctx)
295 if err != nil {
296 ctx.ModuleErrorf("invalid sdk_version: %s", err)
297 }
298 return ret
299 }
300 ret, err := minSdkVersion.EffectiveVersionString(ctx)
301 if err != nil {
302 ctx.ModuleErrorf("invalid min_sdk_version: %s", err)
303 }
304 return ret
Jiyong Park6a927c42020-01-21 02:03:43 +0900305 }
Spandan Das50885c02023-02-23 21:31:33 +0000306 // SDK version flags
307 sdkVersion := sdkContext.SdkVersion(ctx)
308 minSdkVersion := effectiveVersionString(sdkVersion, sdkContext.MinSdkVersion(ctx))
Colin Crossa97c5d32018-03-28 14:58:31 -0700309
Colin Cross83bb3162018-06-25 15:48:06 -0700310 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
Spandan Das6450b552023-02-23 19:27:07 +0000311 // Use minSdkVersion for target-sdk-version, even if `target_sdk_version` is set
312 // This behavior has been copied from Make.
Colin Cross83bb3162018-06-25 15:48:06 -0700313 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700314
Jihoon Kangce320f82024-05-07 00:53:28 +0000315 // Mark non updatable when the module does not specify a version code
316 if !a.aaptProperties.DisableNonUpdatableSystem && !hasVersionCode {
317 linkFlags = append(linkFlags, "--non-updatable-system")
Colin Crossa97c5d32018-03-28 14:58:31 -0700318 }
319
320 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700321 var versionName string
322 if ctx.ModuleName() == "framework-res" {
323 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
324 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700325 // if it contains the build number. Use the PlatformVersionName instead.
326 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700327 } else {
328 versionName = ctx.Config().AppsDefaultVersionName()
329 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800330 versionName = proptools.NinjaEscape(versionName)
Colin Crossa97c5d32018-03-28 14:58:31 -0700331 linkFlags = append(linkFlags, "--version-name ", versionName)
332 }
333
Colin Crossa0ba2f52019-06-22 12:59:27 -0700334 linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"})
335
336 // Always set --pseudo-localize, it will be stripped out later for release
337 // builds that don't want it.
338 compileFlags = append(compileFlags, "--pseudo-localize")
339
340 return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
Colin Crossa97c5d32018-03-28 14:58:31 -0700341}
342
Paul Duffin250e6192019-06-07 10:44:37 +0100343func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) {
Colin Cross42308aa2018-11-14 21:44:17 -0800344 if sdkDep.frameworkResModule != "" {
345 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700346 }
347}
348
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800349var extractAssetsRule = pctx.AndroidStaticRule("extractAssets",
350 blueprint.RuleParams{
351 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "assets/**/*"`,
352 CommandDeps: []string{"${config.Zip2ZipCmd}"},
353 })
354
Alixf7a10272023-09-27 16:47:56 +0000355type aaptBuildActionOptions struct {
356 sdkContext android.SdkContext
357 classLoaderContexts dexpreopt.ClassLoaderContextMap
358 excludedLibs []string
359 enforceDefaultTargetSdkVersion bool
Rico Winda2fa2632024-03-13 13:09:17 +0100360 forceNonFinalResourceIDs bool
Alixf7a10272023-09-27 16:47:56 +0000361 extraLinkFlags []string
Jihoon Kang84b25892023-12-01 22:01:06 +0000362 aconfigTextFiles android.Paths
Jiakai Zhang36937082024-04-15 11:15:50 +0000363 usesLibrary *usesLibrary
Alixf7a10272023-09-27 16:47:56 +0000364}
365
366func (a *aapt) buildActions(ctx android.ModuleContext, opts aaptBuildActionOptions) {
Colin Cross5446e882019-05-22 10:46:27 -0700367
Colin Cross8676c8c2023-10-12 15:58:57 -0700368 staticResourcesNodesDepSet, sharedResourcesNodesDepSet, staticRRODirsDepSet, staticManifestsDepSet, sharedExportPackages, libFlags :=
Jiakai Zhang36937082024-04-15 11:15:50 +0000369 aaptLibs(ctx, opts.sdkContext, opts.classLoaderContexts, opts.usesLibrary)
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100370
Paul Duffin06530572022-02-03 17:54:15 +0000371 // Exclude any libraries from the supplied list.
Alixf7a10272023-09-27 16:47:56 +0000372 opts.classLoaderContexts = opts.classLoaderContexts.ExcludeLibs(opts.excludedLibs)
Paul Duffin06530572022-02-03 17:54:15 +0000373
Colin Cross31656952018-05-24 16:11:20 -0700374 // App manifest file
375 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
376 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
377
Gurpreet Singh7deabfa2022-02-10 13:28:35 +0000378 manifestPath := ManifestFixer(ctx, manifestSrcPath, ManifestFixerParams{
Alixf7a10272023-09-27 16:47:56 +0000379 SdkContext: opts.sdkContext,
380 ClassLoaderContexts: opts.classLoaderContexts,
Harshit Mahajan5b8b7302022-06-10 11:24:05 +0000381 IsLibrary: a.isLibrary,
382 DefaultManifestVersion: a.defaultManifestVersion,
383 UseEmbeddedNativeLibs: a.useEmbeddedNativeLibs,
384 UsesNonSdkApis: a.usesNonSdkApis,
385 UseEmbeddedDex: a.useEmbeddedDex,
386 HasNoCode: a.hasNoCode,
387 LoggingParent: a.LoggingParent,
Alixf7a10272023-09-27 16:47:56 +0000388 EnforceDefaultTargetSdkVersion: opts.enforceDefaultTargetSdkVersion,
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000389 })
Colin Cross90c25c62019-04-19 16:22:57 -0700390
Colin Crossab8d1382023-07-14 17:23:41 +0000391 staticDeps := transitiveAarDeps(staticResourcesNodesDepSet.ToList())
Colin Cross8676c8c2023-10-12 15:58:57 -0700392 sharedDeps := transitiveAarDeps(sharedResourcesNodesDepSet.ToList())
Colin Crossab8d1382023-07-14 17:23:41 +0000393
Luca Stefanifd898822019-09-10 22:13:31 +0200394 // Add additional manifest files to transitive manifests.
395 additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)
Colin Crossab8d1382023-07-14 17:23:41 +0000396 transitiveManifestPaths := append(android.Paths{manifestPath}, additionalManifests...)
Colin Crossab8d1382023-07-14 17:23:41 +0000397 transitiveManifestPaths = append(transitiveManifestPaths, staticManifestsDepSet.ToList()...)
Colin Cross90c25c62019-04-19 16:22:57 -0700398
Colin Crossab8d1382023-07-14 17:23:41 +0000399 if len(transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) {
Alixf7a10272023-09-27 16:47:56 +0000400 manifestMergerParams := ManifestMergerParams{
401 staticLibManifests: transitiveManifestPaths[1:],
Alix96ea88452023-08-31 15:48:23 +0000402 isLibrary: a.isLibrary,
403 packageName: a.manifestValues.applicationId,
404 }
Alixf7a10272023-09-27 16:47:56 +0000405 a.mergedManifestFile = manifestMerger(ctx, transitiveManifestPaths[0], manifestMergerParams)
Colin Cross90c25c62019-04-19 16:22:57 -0700406 if !a.isLibrary {
407 // Only use the merged manifest for applications. For libraries, the transitive closure of manifests
408 // will be propagated to the final application and merged there. The merged manifest for libraries is
409 // only passed to Make, which can't handle transitive dependencies.
410 manifestPath = a.mergedManifestFile
411 }
412 } else {
413 a.mergedManifestFile = manifestPath
414 }
Colin Cross31656952018-05-24 16:11:20 -0700415
Alixf7a10272023-09-27 16:47:56 +0000416 compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, opts.sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700417
418 linkFlags = append(linkFlags, libFlags...)
Colin Cross8676c8c2023-10-12 15:58:57 -0700419 linkDeps = append(linkDeps, sharedExportPackages...)
Colin Crossab8d1382023-07-14 17:23:41 +0000420 linkDeps = append(linkDeps, staticDeps.resPackages()...)
Alixf7a10272023-09-27 16:47:56 +0000421 linkFlags = append(linkFlags, opts.extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700422 if a.isLibrary {
423 linkFlags = append(linkFlags, "--static-lib")
424 }
Rico Wind7152e822024-04-19 08:14:15 +0200425 if opts.forceNonFinalResourceIDs {
426 linkFlags = append(linkFlags, "--non-final-ids")
427 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700428
Colin Cross7c4dc5d2024-02-13 14:29:45 -0800429 linkFlags = append(linkFlags, "--no-static-lib-packages")
Colin Cross8f1b0332024-01-25 13:39:06 -0800430 if a.isLibrary && a.useResourceProcessorBusyBox(ctx) {
Colin Cross7c4dc5d2024-02-13 14:29:45 -0800431 // When building an android_library using ResourceProcessorBusyBox pass --merge-only to skip resource
432 // references validation until the final app link step when all static libraries are present.
Colin Cross4eae06d2023-06-20 22:40:02 -0700433 linkFlags = append(linkFlags, "--merge-only")
Colin Cross4eae06d2023-06-20 22:40:02 -0700434 }
435
Colin Crossa97c5d32018-03-28 14:58:31 -0700436 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
Colin Crossa97c5d32018-03-28 14:58:31 -0700437 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
438 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700439 // This file isn't used by Soong, but is generated for exporting
440 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Cross4eae06d2023-06-20 22:40:02 -0700441 var transitiveRJars android.Paths
Colin Crossf3b7bad2023-08-02 15:49:00 -0700442 var srcJar android.WritablePath
Colin Crossa97c5d32018-03-28 14:58:31 -0700443
Colin Cross4aaa84a2018-08-21 15:14:37 -0700444 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700445 for _, dir := range resDirs {
Colin Cross014489c2020-06-02 20:09:13 -0700446 a.resourceFiles = append(a.resourceFiles, dir.files...)
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900447 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags, a.filterProduct()).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700448 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700449
Colin Crossa592e3e2019-02-19 16:59:53 -0800450 for i, zip := range resZips {
451 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
Colin Crossa0ba2f52019-06-22 12:59:27 -0700452 aapt2CompileZip(ctx, flata, zip, "", compileFlags)
Colin Crossa592e3e2019-02-19 16:59:53 -0800453 compiledResDirs = append(compiledResDirs, android.Paths{flata})
454 }
455
Colin Cross4aaa84a2018-08-21 15:14:37 -0700456 var compiledRes, compiledOverlay android.Paths
457
Colin Crossab8d1382023-07-14 17:23:41 +0000458 // AAPT2 overlays are in lowest to highest priority order, reverse the topological order
459 // of transitiveStaticLibs.
460 transitiveStaticLibs := android.ReversePaths(staticDeps.resPackages())
461
Colin Cross8f1b0332024-01-25 13:39:06 -0800462 if a.isLibrary && a.useResourceProcessorBusyBox(ctx) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700463 // When building an android_library with ResourceProcessorBusyBox enabled treat static library dependencies
464 // as imports. The resources from dependencies will not be merged into this module's package-res.apk, and
465 // instead modules depending on this module will reference package-res.apk from all transitive static
466 // dependencies.
Colin Cross1d3f5902024-03-05 11:51:54 -0800467 for _, sharedDep := range sharedDeps {
468 if sharedDep.usedResourceProcessor {
469 transitiveRJars = append(transitiveRJars, sharedDep.rJar)
470 }
471 }
Colin Cross4eae06d2023-06-20 22:40:02 -0700472 for _, staticDep := range staticDeps {
473 linkDeps = append(linkDeps, staticDep.resPackage)
474 linkFlags = append(linkFlags, "-I "+staticDep.resPackage.String())
475 if staticDep.usedResourceProcessor {
476 transitiveRJars = append(transitiveRJars, staticDep.rJar)
477 }
478 }
479 } else {
480 // When building an app or building a library without ResourceProcessorBusyBox enabled all static
481 // dependencies are compiled into this module's package-res.apk as overlays.
482 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
483 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700484
Colin Crossbec85302019-02-13 13:15:46 -0800485 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700486 // If we are using static android libraries, every source file becomes an overlay.
487 // This is to emulate old AAPT behavior which simulated library support.
488 for _, compiledResDir := range compiledResDirs {
489 compiledOverlay = append(compiledOverlay, compiledResDir...)
490 }
Colin Crossbec85302019-02-13 13:15:46 -0800491 } else if a.isLibrary {
492 // Otherwise, for a static library we treat all the resources equally with no overlay.
493 for _, compiledResDir := range compiledResDirs {
494 compiledRes = append(compiledRes, compiledResDir...)
495 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700496 } else if len(compiledResDirs) > 0 {
497 // Without static libraries, the first directory is our directory, which can then be
498 // overlaid by the rest.
499 compiledRes = append(compiledRes, compiledResDirs[0]...)
500 for _, compiledResDir := range compiledResDirs[1:] {
501 compiledOverlay = append(compiledOverlay, compiledResDir...)
502 }
503 }
504
Colin Crossa97c5d32018-03-28 14:58:31 -0700505 for _, dir := range overlayDirs {
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900506 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags, a.filterProduct()).Paths()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700507 }
508
Colin Crosse560c4a2019-03-19 16:03:11 -0700509 var splitPackages android.WritablePaths
510 var splits []split
511
512 for _, s := range a.splitNames {
513 suffix := strings.Replace(s, ",", "_", -1)
514 path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
515 linkFlags = append(linkFlags, "--split", path.String()+":"+s)
516 splitPackages = append(splitPackages, path)
517 splits = append(splits, split{
518 name: s,
519 suffix: suffix,
520 path: path,
521 })
522 }
523
Colin Cross8f1b0332024-01-25 13:39:06 -0800524 if !a.useResourceProcessorBusyBox(ctx) {
Colin Crossf3b7bad2023-08-02 15:49:00 -0700525 // the subdir "android" is required to be filtered by package names
526 srcJar = android.PathForModuleGen(ctx, "android", "R.srcjar")
527 }
528
Colin Crossab8d1382023-07-14 17:23:41 +0000529 // No need to specify assets from dependencies to aapt2Link for libraries, all transitive assets will be
530 // provided to the final app aapt2Link step.
531 var transitiveAssets android.Paths
532 if !a.isLibrary {
533 transitiveAssets = android.ReverseSliceInPlace(staticDeps.assets())
534 }
Colin Crossf3b7bad2023-08-02 15:49:00 -0700535 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt,
Jihoon Kang84b25892023-12-01 22:01:06 +0000536 linkFlags, linkDeps, compiledRes, compiledOverlay, transitiveAssets, splitPackages,
537 opts.aconfigTextFiles)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800538 // Extract assets from the resource package output so that they can be used later in aapt2link
539 // for modules that depend on this one.
Colin Crossab8d1382023-07-14 17:23:41 +0000540 if android.PrefixInList(linkFlags, "-A ") {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800541 assets := android.PathForModuleOut(ctx, "assets.zip")
542 ctx.Build(pctx, android.BuildParams{
543 Rule: extractAssetsRule,
544 Input: packageRes,
545 Output: assets,
546 Description: "extract assets from built resource file",
547 })
548 a.assetPackage = android.OptionalPathForPath(assets)
549 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700550
Colin Cross8f1b0332024-01-25 13:39:06 -0800551 if a.useResourceProcessorBusyBox(ctx) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700552 rJar := android.PathForModuleOut(ctx, "busybox/R.jar")
Rico Winda2fa2632024-03-13 13:09:17 +0100553 resourceProcessorBusyBoxGenerateBinaryR(ctx, rTxt, a.mergedManifestFile, rJar, staticDeps, a.isLibrary, a.aaptProperties.Aaptflags,
554 opts.forceNonFinalResourceIDs)
Colin Crossf3b7bad2023-08-02 15:49:00 -0700555 aapt2ExtractExtraPackages(ctx, extraPackages, rJar)
Colin Cross4eae06d2023-06-20 22:40:02 -0700556 transitiveRJars = append(transitiveRJars, rJar)
557 a.rJar = rJar
Colin Crossf3b7bad2023-08-02 15:49:00 -0700558 } else {
559 aapt2ExtractExtraPackages(ctx, extraPackages, srcJar)
Colin Cross4eae06d2023-06-20 22:40:02 -0700560 }
561
Colin Cross312634e2023-11-21 15:13:56 -0800562 transitiveAaptResourcePackages := staticDeps.resPackages().Strings()
563 transitiveAaptResourcePackages = slices.DeleteFunc(transitiveAaptResourcePackages, func(p string) bool {
564 return p == packageRes.String()
565 })
566 transitiveAaptResourcePackagesFile := android.PathForModuleOut(ctx, "transitive-res-packages")
567 android.WriteFileRule(ctx, transitiveAaptResourcePackagesFile, strings.Join(transitiveAaptResourcePackages, "\n"))
568
Colin Cross1d3f5902024-03-05 11:51:54 -0800569 // Reverse the list of R.jar files so that the current module comes first, and direct dependencies come before
570 // transitive dependencies.
571 transitiveRJars = android.ReversePaths(transitiveRJars)
572
Colin Crossa97c5d32018-03-28 14:58:31 -0700573 a.aaptSrcJar = srcJar
Colin Cross4eae06d2023-06-20 22:40:02 -0700574 a.transitiveAaptRJars = transitiveRJars
Colin Cross312634e2023-11-21 15:13:56 -0800575 a.transitiveAaptResourcePackagesFile = transitiveAaptResourcePackagesFile
Colin Crossa97c5d32018-03-28 14:58:31 -0700576 a.exportPackage = packageRes
577 a.manifestPath = manifestPath
578 a.proguardOptionsFile = proguardOptionsFile
Colin Cross66f78822018-05-02 12:58:28 -0700579 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700580 a.rTxt = rTxt
Colin Crosse560c4a2019-03-19 16:03:11 -0700581 a.splits = splits
Colin Crossab8d1382023-07-14 17:23:41 +0000582 a.resourcesNodesDepSet = android.NewDepSetBuilder[*resourcesNode](android.TOPOLOGICAL).
583 Direct(&resourcesNode{
584 resPackage: a.exportPackage,
585 manifest: a.manifestPath,
586 additionalManifests: additionalManifests,
Colin Cross4eae06d2023-06-20 22:40:02 -0700587 rTxt: a.rTxt,
588 rJar: a.rJar,
Colin Crossab8d1382023-07-14 17:23:41 +0000589 assets: a.assetPackage,
Colin Cross4eae06d2023-06-20 22:40:02 -0700590
Colin Cross8f1b0332024-01-25 13:39:06 -0800591 usedResourceProcessor: a.useResourceProcessorBusyBox(ctx),
Colin Crossab8d1382023-07-14 17:23:41 +0000592 }).
593 Transitive(staticResourcesNodesDepSet).Build()
594 a.rroDirsDepSet = android.NewDepSetBuilder[rroDir](android.TOPOLOGICAL).
595 Direct(rroDirs...).
596 Transitive(staticRRODirsDepSet).Build()
597 a.manifestsDepSet = android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).
598 Direct(a.manifestPath).
599 DirectSlice(additionalManifests).
600 Transitive(staticManifestsDepSet).Build()
601}
602
Colin Cross4eae06d2023-06-20 22:40:02 -0700603var resourceProcessorBusyBox = pctx.AndroidStaticRule("resourceProcessorBusyBox",
604 blueprint.RuleParams{
605 Command: "${config.JavaCmd} -cp ${config.ResourceProcessorBusyBox} " +
606 "com.google.devtools.build.android.ResourceProcessorBusyBox --tool=GENERATE_BINARY_R -- @${out}.args && " +
607 "if cmp -s ${out}.tmp ${out} ; then rm ${out}.tmp ; else mv ${out}.tmp ${out}; fi",
608 CommandDeps: []string{"${config.ResourceProcessorBusyBox}"},
609 Rspfile: "${out}.args",
610 RspfileContent: "--primaryRTxt ${rTxt} --primaryManifest ${manifest} --classJarOutput ${out}.tmp ${args}",
611 Restat: true,
612 }, "rTxt", "manifest", "args")
613
614// resourceProcessorBusyBoxGenerateBinaryR converts the R.txt file produced by aapt2 into R.class files
615// using Bazel's ResourceProcessorBusyBox tool, which is faster than compiling the R.java files and
616// supports producing classes for static dependencies that only include resources from that dependency.
617func resourceProcessorBusyBoxGenerateBinaryR(ctx android.ModuleContext, rTxt, manifest android.Path,
Rico Winda2fa2632024-03-13 13:09:17 +0100618 rJar android.WritablePath, transitiveDeps transitiveAarDeps, isLibrary bool, aaptFlags []string,
619 forceNonFinalIds bool) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700620
621 var args []string
622 var deps android.Paths
623
624 if !isLibrary {
625 // When compiling an app, pass all R.txt and AndroidManifest.xml from transitive static library dependencies
626 // to ResourceProcessorBusyBox so that it can regenerate R.class files with the final resource IDs for each
627 // package.
628 args, deps = transitiveDeps.resourceProcessorDeps()
Rico Winda2fa2632024-03-13 13:09:17 +0100629 if forceNonFinalIds {
630 args = append(args, "--finalFields=false")
631 }
Colin Cross4eae06d2023-06-20 22:40:02 -0700632 } else {
633 // When compiling a library don't pass any dependencies as it only needs to generate an R.class file for this
634 // library. Pass --finalFields=false so that the R.class file contains non-final fields so they don't get
635 // inlined into the library before the final IDs are assigned during app compilation.
636 args = append(args, "--finalFields=false")
637 }
638
Colin Crossd3f7d1a2024-01-03 19:42:25 -0800639 for i, arg := range aaptFlags {
640 const AAPT_CUSTOM_PACKAGE = "--custom-package"
641 if strings.HasPrefix(arg, AAPT_CUSTOM_PACKAGE) {
642 pkg := strings.TrimSpace(strings.TrimPrefix(arg, AAPT_CUSTOM_PACKAGE))
643 if pkg == "" && i+1 < len(aaptFlags) {
644 pkg = aaptFlags[i+1]
645 }
646 args = append(args, "--packageForR "+pkg)
647 }
648 }
649
Colin Cross4eae06d2023-06-20 22:40:02 -0700650 deps = append(deps, rTxt, manifest)
651
652 ctx.Build(pctx, android.BuildParams{
653 Rule: resourceProcessorBusyBox,
654 Output: rJar,
655 Implicits: deps,
656 Description: "ResourceProcessorBusyBox",
657 Args: map[string]string{
658 "rTxt": rTxt.String(),
659 "manifest": manifest.String(),
660 "args": strings.Join(args, " "),
661 },
662 })
663}
664
Colin Crossab8d1382023-07-14 17:23:41 +0000665type resourcesNode struct {
666 resPackage android.Path
667 manifest android.Path
668 additionalManifests android.Paths
Colin Cross4eae06d2023-06-20 22:40:02 -0700669 rTxt android.Path
670 rJar android.Path
Colin Crossab8d1382023-07-14 17:23:41 +0000671 assets android.OptionalPath
Colin Cross4eae06d2023-06-20 22:40:02 -0700672
673 usedResourceProcessor bool
Colin Crossab8d1382023-07-14 17:23:41 +0000674}
675
676type transitiveAarDeps []*resourcesNode
677
678func (t transitiveAarDeps) resPackages() android.Paths {
Colin Cross4eae06d2023-06-20 22:40:02 -0700679 paths := make(android.Paths, 0, len(t))
Colin Crossab8d1382023-07-14 17:23:41 +0000680 for _, dep := range t {
681 paths = append(paths, dep.resPackage)
682 }
Colin Cross4eae06d2023-06-20 22:40:02 -0700683 return paths
Colin Crossab8d1382023-07-14 17:23:41 +0000684}
685
686func (t transitiveAarDeps) manifests() android.Paths {
Colin Cross4eae06d2023-06-20 22:40:02 -0700687 paths := make(android.Paths, 0, len(t))
Colin Crossab8d1382023-07-14 17:23:41 +0000688 for _, dep := range t {
689 paths = append(paths, dep.manifest)
690 paths = append(paths, dep.additionalManifests...)
691 }
Colin Cross4eae06d2023-06-20 22:40:02 -0700692 return paths
693}
694
695func (t transitiveAarDeps) resourceProcessorDeps() (args []string, deps android.Paths) {
696 for _, dep := range t {
697 args = append(args, "--library="+dep.rTxt.String()+","+dep.manifest.String())
698 deps = append(deps, dep.rTxt, dep.manifest)
699 }
700 return args, deps
Colin Crossab8d1382023-07-14 17:23:41 +0000701}
702
703func (t transitiveAarDeps) assets() android.Paths {
Colin Cross4eae06d2023-06-20 22:40:02 -0700704 paths := make(android.Paths, 0, len(t))
Colin Crossab8d1382023-07-14 17:23:41 +0000705 for _, dep := range t {
706 if dep.assets.Valid() {
707 paths = append(paths, dep.assets.Path())
708 }
709 }
710 return paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700711}
712
713// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Jiakai Zhang36937082024-04-15 11:15:50 +0000714func aaptLibs(ctx android.ModuleContext, sdkContext android.SdkContext,
715 classLoaderContexts dexpreopt.ClassLoaderContextMap, usesLibrary *usesLibrary) (
Colin Cross8676c8c2023-10-12 15:58:57 -0700716 staticResourcesNodes, sharedResourcesNodes *android.DepSet[*resourcesNode], staticRRODirs *android.DepSet[rroDir],
Colin Crossab8d1382023-07-14 17:23:41 +0000717 staticManifests *android.DepSet[android.Path], sharedLibs android.Paths, flags []string) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700718
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100719 if classLoaderContexts == nil {
Ulya Trafimovich18554242020-11-03 15:55:11 +0000720 // Not all callers need to compute class loader context, those who don't just pass nil.
721 // Create a temporary class loader context here (it will be computed, but not used).
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100722 classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000723 }
724
Colin Cross83bb3162018-06-25 15:48:06 -0700725 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700726 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700727 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700728 }
729
Colin Cross8676c8c2023-10-12 15:58:57 -0700730 var staticResourcesNodeDepSets []*android.DepSet[*resourcesNode]
731 var sharedResourcesNodeDepSets []*android.DepSet[*resourcesNode]
Colin Crossab8d1382023-07-14 17:23:41 +0000732 rroDirsDepSetBuilder := android.NewDepSetBuilder[rroDir](android.TOPOLOGICAL)
733 manifestsDepSetBuilder := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL)
734
Colin Crossa97c5d32018-03-28 14:58:31 -0700735 ctx.VisitDirectDeps(func(module android.Module) {
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000736 depTag := ctx.OtherModuleDependencyTag(module)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000737
Colin Crossa97c5d32018-03-28 14:58:31 -0700738 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700739 aarDep, _ := module.(AndroidLibraryDependency)
740 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700741 exportPackage = aarDep.ExportPackage()
742 }
743
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000744 switch depTag {
Colin Cross4b964c02018-10-15 16:18:06 -0700745 case instrumentationForTag:
746 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Liz Kammeref28a4c2022-09-23 16:50:56 -0400747 case sdkLibTag, libTag:
Colin Cross5446e882019-05-22 10:46:27 -0700748 if exportPackage != nil {
Colin Cross8676c8c2023-10-12 15:58:57 -0700749 sharedResourcesNodeDepSets = append(sharedResourcesNodeDepSets, aarDep.ResourcesNodeDepSet())
Colin Cross5446e882019-05-22 10:46:27 -0700750 sharedLibs = append(sharedLibs, exportPackage)
751 }
Colin Cross5446e882019-05-22 10:46:27 -0700752 case frameworkResTag:
Colin Crossa97c5d32018-03-28 14:58:31 -0700753 if exportPackage != nil {
754 sharedLibs = append(sharedLibs, exportPackage)
755 }
756 case staticLibTag:
757 if exportPackage != nil {
Colin Cross8676c8c2023-10-12 15:58:57 -0700758 staticResourcesNodeDepSets = append(staticResourcesNodeDepSets, aarDep.ResourcesNodeDepSet())
Colin Crossab8d1382023-07-14 17:23:41 +0000759 rroDirsDepSetBuilder.Transitive(aarDep.RRODirsDepSet())
760 manifestsDepSetBuilder.Transitive(aarDep.ManifestsDepSet())
Colin Crossa97c5d32018-03-28 14:58:31 -0700761 }
762 }
Ulya Trafimovich18554242020-11-03 15:55:11 +0000763
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +0000764 addCLCFromDep(ctx, module, classLoaderContexts)
Jiakai Zhang36937082024-04-15 11:15:50 +0000765 if usesLibrary != nil {
766 addMissingOptionalUsesLibsFromDep(ctx, module, usesLibrary)
767 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700768 })
769
Colin Crossab8d1382023-07-14 17:23:41 +0000770 // AAPT2 overlays are in lowest to highest priority order, the topological order will be reversed later.
771 // Reverse the dependency order now going into the depset so that it comes out in order after the second
772 // reverse later.
773 // NOTE: this is legacy and probably incorrect behavior, for most other cases (e.g. conflicting classes in
774 // dependencies) the highest priority dependency is listed first, but for resources the highest priority
Colin Cross9055e212024-03-23 04:43:41 +0000775 // dependency has to be listed last. This is also inconsistent with the way manifests from the same
776 // transitive dependencies are merged.
Colin Crossab8d1382023-07-14 17:23:41 +0000777 staticResourcesNodes = android.NewDepSet(android.TOPOLOGICAL, nil,
Colin Cross8676c8c2023-10-12 15:58:57 -0700778 android.ReverseSliceInPlace(staticResourcesNodeDepSets))
779 sharedResourcesNodes = android.NewDepSet(android.TOPOLOGICAL, nil,
780 android.ReverseSliceInPlace(sharedResourcesNodeDepSets))
Colin Crossa97c5d32018-03-28 14:58:31 -0700781
Colin Crossab8d1382023-07-14 17:23:41 +0000782 staticRRODirs = rroDirsDepSetBuilder.Build()
783 staticManifests = manifestsDepSetBuilder.Build()
784
785 if len(staticResourcesNodes.ToList()) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700786 flags = append(flags, "--auto-add-overlay")
787 }
788
789 for _, sharedLib := range sharedLibs {
790 flags = append(flags, "-I "+sharedLib.String())
791 }
792
Colin Cross8676c8c2023-10-12 15:58:57 -0700793 return staticResourcesNodes, sharedResourcesNodes, staticRRODirs, staticManifests, sharedLibs, flags
Colin Crossa97c5d32018-03-28 14:58:31 -0700794}
795
796type AndroidLibrary struct {
797 Library
798 aapt
799
800 androidLibraryProperties androidLibraryProperties
801
802 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700803}
804
Saeid Farivar Asanjan1fca3012021-09-14 18:40:19 +0000805var _ android.OutputFileProducer = (*AndroidLibrary)(nil)
806
807// For OutputFileProducer interface
808func (a *AndroidLibrary) OutputFiles(tag string) (android.Paths, error) {
809 switch tag {
810 case ".aar":
811 return []android.Path{a.aarFile}, nil
812 default:
813 return a.Library.OutputFiles(tag)
814 }
815}
816
Colin Crossa97c5d32018-03-28 14:58:31 -0700817var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
818
819func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiakai Zhangf98da192024-04-15 11:15:41 +0000820 a.usesLibrary.deps(ctx, false)
Colin Crossa97c5d32018-03-28 14:58:31 -0700821 a.Module.deps(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900822 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
Paul Duffin250e6192019-06-07 10:44:37 +0100823 if sdkDep.hasFrameworkLibs() {
824 a.aapt.deps(ctx, sdkDep)
Colin Crossa97c5d32018-03-28 14:58:31 -0700825 }
Jihoon Kang9049c272024-03-19 21:57:36 +0000826
827 for _, aconfig_declaration := range a.aaptProperties.Flags_packages {
828 ctx.AddDependency(ctx.Module(), aconfigDeclarationTag, aconfig_declaration)
829 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700830}
831
832func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800833 a.aapt.isLibrary = true
Ulya Trafimovich42c7f0d2021-08-17 16:20:29 +0100834 a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Spandan Das0727ba72024-02-13 16:37:43 +0000835 if a.usesLibrary.shouldDisableDexpreopt {
836 a.dexpreopter.disableDexpreopt()
837 }
Alixf7a10272023-09-27 16:47:56 +0000838 a.aapt.buildActions(ctx,
839 aaptBuildActionOptions{
840 sdkContext: android.SdkContext(a),
841 classLoaderContexts: a.classLoaderContexts,
842 enforceDefaultTargetSdkVersion: false,
Jihoon Kang9049c272024-03-19 21:57:36 +0000843 aconfigTextFiles: getAconfigFilePaths(ctx),
Jiakai Zhang36937082024-04-15 11:15:50 +0000844 usesLibrary: &a.usesLibrary,
Alixf7a10272023-09-27 16:47:56 +0000845 },
846 )
Colin Crossa97c5d32018-03-28 14:58:31 -0700847
Colin Crossff694a82023-12-13 15:54:49 -0800848 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
849 a.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -0700850
yangbill2af0b6e2024-03-15 09:29:29 +0000851 a.stem = proptools.StringDefault(a.overridableProperties.Stem, ctx.ModuleName())
Jihoon Kang1bfb6f22023-07-01 00:13:47 +0000852
Colin Cross4eae06d2023-06-20 22:40:02 -0700853 ctx.CheckbuildFile(a.aapt.proguardOptionsFile)
854 ctx.CheckbuildFile(a.aapt.exportPackage)
Colin Cross8f1b0332024-01-25 13:39:06 -0800855 if a.useResourceProcessorBusyBox(ctx) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700856 ctx.CheckbuildFile(a.aapt.rJar)
Colin Crossf3b7bad2023-08-02 15:49:00 -0700857 } else {
858 ctx.CheckbuildFile(a.aapt.aaptSrcJar)
Colin Cross4eae06d2023-06-20 22:40:02 -0700859 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700860
861 // apps manifests are handled by aapt, don't let Module see them
862 a.properties.Manifest = nil
863
Colin Cross014489c2020-06-02 20:09:13 -0700864 a.linter.mergedManifest = a.aapt.mergedManifestFile
865 a.linter.manifest = a.aapt.manifestPath
866 a.linter.resources = a.aapt.resourceFiles
867
Sam Delmericoc8e040c2023-10-31 17:27:02 +0000868 proguardSpecInfo := a.collectProguardSpecInfo(ctx)
Colin Cross40213022023-12-13 15:19:49 -0800869 android.SetProvider(ctx, ProguardSpecInfoProvider, proguardSpecInfo)
Colin Cross312634e2023-11-21 15:13:56 -0800870 exportedProguardFlagsFiles := proguardSpecInfo.ProguardFlagsFiles.ToList()
871 a.extraProguardFlagsFiles = append(a.extraProguardFlagsFiles, exportedProguardFlagsFiles...)
872 a.extraProguardFlagsFiles = append(a.extraProguardFlagsFiles, a.proguardOptionsFile)
873
874 combinedExportedProguardFlagFile := android.PathForModuleOut(ctx, "export_proguard_flags")
875 writeCombinedProguardFlagsFile(ctx, combinedExportedProguardFlagFile, exportedProguardFlagsFiles)
876 a.combinedExportedProguardFlagsFile = combinedExportedProguardFlagFile
Colin Crossa97c5d32018-03-28 14:58:31 -0700877
Colin Cross4eae06d2023-06-20 22:40:02 -0700878 var extraSrcJars android.Paths
879 var extraCombinedJars android.Paths
880 var extraClasspathJars android.Paths
Colin Cross8f1b0332024-01-25 13:39:06 -0800881 if a.useResourceProcessorBusyBox(ctx) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700882 // When building a library with ResourceProcessorBusyBox enabled ResourceProcessorBusyBox for this
883 // library and each of the transitive static android_library dependencies has already created an
884 // R.class file for the appropriate package. Add all of those R.class files to the classpath.
885 extraClasspathJars = a.transitiveAaptRJars
886 } else {
887 // When building a library without ResourceProcessorBusyBox the aapt2 rule creates R.srcjar containing
888 // R.java files for the library's package and the packages from all transitive static android_library
889 // dependencies. Compile the srcjar alongside the rest of the sources.
890 extraSrcJars = android.Paths{a.aapt.aaptSrcJar}
891 }
892
893 a.Module.compile(ctx, extraSrcJars, extraClasspathJars, extraCombinedJars)
Colin Crossa97c5d32018-03-28 14:58:31 -0700894
Colin Crossf57c5782019-01-25 13:20:38 -0800895 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700896 var res android.Paths
897 if a.androidLibraryProperties.BuildAAR {
898 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
899 ctx.CheckbuildFile(a.aarFile)
900 }
Colin Cross89c31582018-04-30 15:55:11 -0700901
Sam Delmerico82602492022-06-10 17:05:42 +0000902 prebuiltJniPackages := android.Paths{}
903 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800904 if info, ok := android.OtherModuleProvider(ctx, module, JniPackageProvider); ok {
Sam Delmerico82602492022-06-10 17:05:42 +0000905 prebuiltJniPackages = append(prebuiltJniPackages, info.JniPackages...)
906 }
907 })
908 if len(prebuiltJniPackages) > 0 {
Colin Cross40213022023-12-13 15:19:49 -0800909 android.SetProvider(ctx, JniPackageProvider, JniPackageInfo{
Sam Delmerico82602492022-06-10 17:05:42 +0000910 JniPackages: prebuiltJniPackages,
911 })
912 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700913}
914
Colin Cross95b53b82023-10-17 13:21:02 -0700915func (a *AndroidLibrary) IDEInfo(dpInfo *android.IdeInfo) {
916 a.Library.IDEInfo(dpInfo)
917 a.aapt.IDEInfo(dpInfo)
918}
919
920func (a *aapt) IDEInfo(dpInfo *android.IdeInfo) {
Colin Cross8f1b0332024-01-25 13:39:06 -0800921 if a.rJar != nil {
Colin Cross95b53b82023-10-17 13:21:02 -0700922 dpInfo.Jars = append(dpInfo.Jars, a.rJar.String())
923 }
924}
925
Colin Cross1b16b0e2019-02-12 14:41:32 -0800926// android_library builds and links sources into a `.jar` file for the device along with Android resources.
927//
928// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
Sam Delmerico82602492022-06-10 17:05:42 +0000929// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
Colin Cross1b16b0e2019-02-12 14:41:32 -0800930// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
931// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700932func AndroidLibraryFactory() android.Module {
933 module := &AndroidLibrary{}
934
Colin Crossce6734e2020-06-15 16:09:53 -0700935 module.Module.addHostAndDeviceProperties()
Colin Crossa97c5d32018-03-28 14:58:31 -0700936 module.AddProperties(
Colin Crossa97c5d32018-03-28 14:58:31 -0700937 &module.aaptProperties,
Ronald Braunsteincdc66f42024-04-12 11:23:19 -0700938 &module.androidLibraryProperties,
939 &module.sourceProperties)
Colin Crossa97c5d32018-03-28 14:58:31 -0700940
941 module.androidLibraryProperties.BuildAAR = true
Colin Cross014489c2020-06-02 20:09:13 -0700942 module.Module.linter.library = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700943
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900944 android.InitApexModule(module)
Colin Cross48de9a42018-10-02 13:53:33 -0700945 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700946 return module
947}
948
Colin Crossfabb6082018-02-20 17:22:23 -0800949//
950// AAR (android library) prebuilts
951//
Colin Crossfabb6082018-02-20 17:22:23 -0800952
Vinh Trance0781f2022-04-13 01:30:44 +0000953// Properties for android_library_import
Colin Crossfabb6082018-02-20 17:22:23 -0800954type AARImportProperties struct {
Vinh Trance0781f2022-04-13 01:30:44 +0000955 // ARR (android library prebuilt) filepath. Exactly one ARR is required.
Colin Cross27b922f2019-03-04 22:35:41 -0800956 Aars []string `android:"path"`
Vinh Trance0781f2022-04-13 01:30:44 +0000957 // If not blank, set to the version of the sdk to compile against.
958 // Defaults to private.
959 // Values are of one of the following forms:
960 // 1) numerical API level, "current", "none", or "core_platform"
961 // 2) An SDK kind with an API level: "<sdk kind>_<API level>"
962 // See build/soong/android/sdk_version.go for the complete and up to date list of SDK kinds.
963 // If the SDK kind is empty, it will be set to public
964 Sdk_version *string
965 // If not blank, set the minimum version of the sdk that the compiled artifacts will run against.
966 // Defaults to sdk_version if not set. See sdk_version for possible values.
Colin Cross479884c2018-07-10 13:39:30 -0700967 Min_sdk_version *string
Vinh Trance0781f2022-04-13 01:30:44 +0000968 // List of java static libraries that the included ARR (android library prebuilts) has dependencies to.
Colin Crossa97c5d32018-03-28 14:58:31 -0700969 Static_libs []string
Vinh Trance0781f2022-04-13 01:30:44 +0000970 // List of java libraries that the included ARR (android library prebuilts) has dependencies to.
971 Libs []string
972 // If set to true, run Jetifier against .aar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -0700973 Jetifier *bool
Sam Delmerico82602492022-06-10 17:05:42 +0000974 // If true, extract JNI libs from AAR archive. These libs will be accessible to android_app modules and
975 // will be passed transitively through android_libraries to an android_app.
976 //TODO(b/241138093) evaluate whether we can have this flag default to true for Bazel conversion
977 Extract_jni *bool
Colin Cross21ed4692024-04-24 20:23:38 +0000978
979 // If set, overrides the manifest extracted from the AAR with the provided path.
980 Manifest *string `android:"path"`
Colin Crossfabb6082018-02-20 17:22:23 -0800981}
982
983type AARImport struct {
984 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700985 android.DefaultableModuleBase
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900986 android.ApexModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800987 prebuilt android.Prebuilt
988
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900989 // Functionality common to Module and Import.
990 embeddableInModuleAndImport
991
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500992 providesTransitiveHeaderJars
993
Colin Crossfabb6082018-02-20 17:22:23 -0800994 properties AARImportProperties
995
Colin Cross9055e212024-03-23 04:43:41 +0000996 headerJarFile android.WritablePath
997 implementationJarFile android.WritablePath
Colin Cross607bbd62024-04-12 13:44:45 -0700998 implementationAndResourcesJarFile android.WritablePath
Colin Cross312634e2023-11-21 15:13:56 -0800999 proguardFlags android.WritablePath
1000 exportPackage android.WritablePath
1001 transitiveAaptResourcePackagesFile android.Path
1002 extraAaptPackagesFile android.WritablePath
Colin Cross21ed4692024-04-24 20:23:38 +00001003 manifest android.Path
Colin Cross312634e2023-11-21 15:13:56 -08001004 assetsPackage android.WritablePath
1005 rTxt android.WritablePath
1006 rJar android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -07001007
Colin Crossab8d1382023-07-14 17:23:41 +00001008 resourcesNodesDepSet *android.DepSet[*resourcesNode]
1009 manifestsDepSet *android.DepSet[android.Path]
Colin Cross56a83212020-09-15 18:30:11 -07001010
1011 hideApexVariantFromMake bool
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001012
Sam Delmerico82602492022-06-10 17:05:42 +00001013 aarPath android.Path
1014 jniPackages android.Paths
Jiyong Park92315372021-04-02 08:45:46 +09001015
1016 sdkVersion android.SdkSpec
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001017 minSdkVersion android.ApiLevel
LaMont Jonesafe7baf2024-01-09 22:47:39 +00001018
Colin Cross9055e212024-03-23 04:43:41 +00001019 usesLibrary
1020 classLoaderContexts dexpreopt.ClassLoaderContextMap
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001021}
1022
1023var _ android.OutputFileProducer = (*AARImport)(nil)
1024
1025// For OutputFileProducer interface
1026func (a *AARImport) OutputFiles(tag string) (android.Paths, error) {
1027 switch tag {
1028 case ".aar":
1029 return []android.Path{a.aarPath}, nil
1030 case "":
Colin Cross607bbd62024-04-12 13:44:45 -07001031 return []android.Path{a.implementationAndResourcesJarFile}, nil
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001032 default:
1033 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1034 }
Colin Crossfabb6082018-02-20 17:22:23 -08001035}
1036
Jiyong Park92315372021-04-02 08:45:46 +09001037func (a *AARImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1038 return android.SdkSpecFrom(ctx, String(a.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07001039}
1040
Jiyong Parkf1691d22021-03-29 20:11:58 +09001041func (a *AARImport) SystemModules() string {
Paul Duffine25c6442019-10-11 13:50:28 +01001042 return ""
1043}
1044
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001045func (a *AARImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Colin Cross479884c2018-07-10 13:39:30 -07001046 if a.properties.Min_sdk_version != nil {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001047 return android.ApiLevelFrom(ctx, *a.properties.Min_sdk_version)
Colin Cross479884c2018-07-10 13:39:30 -07001048 }
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001049 return a.SdkVersion(ctx).ApiLevel
Colin Cross83bb3162018-06-25 15:48:06 -07001050}
1051
Spandan Dasa26eda72023-03-02 00:56:06 +00001052func (a *AARImport) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
1053 return android.SdkSpecFrom(ctx, "").ApiLevel
William Loh5a082f92022-05-17 20:21:50 +00001054}
1055
Spandan Dasca70fc42023-03-01 23:38:49 +00001056func (a *AARImport) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
1057 return a.SdkVersion(ctx).ApiLevel
Dan Willemsen419290a2018-10-31 15:28:47 -07001058}
1059
Colin Cross1e743852019-10-28 11:37:20 -07001060func (a *AARImport) javaVersion() string {
1061 return ""
1062}
1063
Colin Crossa97c5d32018-03-28 14:58:31 -07001064var _ AndroidLibraryDependency = (*AARImport)(nil)
1065
1066func (a *AARImport) ExportPackage() android.Path {
1067 return a.exportPackage
1068}
Colin Crossab8d1382023-07-14 17:23:41 +00001069func (a *AARImport) ResourcesNodeDepSet() *android.DepSet[*resourcesNode] {
1070 return a.resourcesNodesDepSet
Colin Crossc1c37552019-01-31 11:42:41 -08001071}
1072
Colin Crossab8d1382023-07-14 17:23:41 +00001073func (a *AARImport) RRODirsDepSet() *android.DepSet[rroDir] {
1074 return android.NewDepSet[rroDir](android.TOPOLOGICAL, nil, nil)
Colin Cross66f78822018-05-02 12:58:28 -07001075}
1076
Colin Crossab8d1382023-07-14 17:23:41 +00001077func (a *AARImport) ManifestsDepSet() *android.DepSet[android.Path] {
1078 return a.manifestsDepSet
Jaewoong Jung6431ca72020-01-15 14:15:10 -08001079}
1080
Jaewoong Jungc779cd42020-10-06 18:56:10 -07001081// RRO enforcement is not available on aar_import since its RRO dirs are not
1082// exported.
1083func (a *AARImport) SetRROEnforcedForDependent(enforce bool) {
1084}
1085
1086// RRO enforcement is not available on aar_import since its RRO dirs are not
1087// exported.
1088func (a *AARImport) IsRROEnforced(ctx android.BaseModuleContext) bool {
1089 return false
1090}
1091
Colin Crossfabb6082018-02-20 17:22:23 -08001092func (a *AARImport) Prebuilt() *android.Prebuilt {
1093 return &a.prebuilt
1094}
1095
1096func (a *AARImport) Name() string {
1097 return a.prebuilt.Name(a.ModuleBase.Name())
1098}
1099
Jiyong Park618922e2020-01-08 13:35:43 +09001100func (a *AARImport) JacocoReportClassesFile() android.Path {
1101 return nil
1102}
1103
Colin Crossfabb6082018-02-20 17:22:23 -08001104func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Jeongik Cha816a23a2020-07-08 01:09:23 +09001105 if !ctx.Config().AlwaysUsePrebuiltSdks() {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001106 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -07001107 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -07001108 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -08001109 }
1110 }
Colin Crossa97c5d32018-03-28 14:58:31 -07001111
Colin Cross42d48b72018-08-29 14:10:52 -07001112 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
1113 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Cross9055e212024-03-23 04:43:41 +00001114
1115 a.usesLibrary.deps(ctx, false)
Colin Crossfabb6082018-02-20 17:22:23 -08001116}
1117
Sam Delmerico82602492022-06-10 17:05:42 +00001118type JniPackageInfo struct {
1119 // List of zip files containing JNI libraries
1120 // Zip files should have directory structure jni/<arch>/*.so
1121 JniPackages android.Paths
1122}
1123
Colin Crossbc7d76c2023-12-12 16:39:03 -08001124var JniPackageProvider = blueprint.NewProvider[JniPackageInfo]()
Sam Delmerico82602492022-06-10 17:05:42 +00001125
1126// Unzip an AAR and extract the JNI libs for $archString.
1127var extractJNI = pctx.AndroidStaticRule("extractJNI",
1128 blueprint.RuleParams{
1129 Command: `rm -rf $out $outDir && touch $out && ` +
1130 `unzip -qoDD -d $outDir $in "jni/${archString}/*" && ` +
1131 `jni_files=$$(find $outDir/jni -type f) && ` +
1132 // print error message if there are no JNI libs for this arch
1133 `[ -n "$$jni_files" ] || (echo "ERROR: no JNI libs found for arch ${archString}" && exit 1) && ` +
Sam Delmerico80ee45c2023-06-22 15:36:02 -04001134 `${config.SoongZipCmd} -o $out -L 0 -P 'lib/${archString}' ` +
Sam Delmerico82602492022-06-10 17:05:42 +00001135 `-C $outDir/jni/${archString} $$(echo $$jni_files | xargs -n1 printf " -f %s")`,
1136 CommandDeps: []string{"${config.SoongZipCmd}"},
1137 },
1138 "outDir", "archString")
1139
Colin Crossfabb6082018-02-20 17:22:23 -08001140// 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 -07001141// 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 -08001142var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
1143 blueprint.RuleParams{
Dan Willemsen304cfec2019-05-28 14:49:06 -07001144 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
Colin Cross205e9112020-08-06 13:20:17 -07001145 `unzip -qoDD -d $outDir $in && rm -rf $outDir/res && touch $out && ` +
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001146 `${config.Zip2ZipCmd} -i $in -o $assetsPackage 'assets/**/*' && ` +
Colin Cross205e9112020-08-06 13:20:17 -07001147 `${config.MergeZipsCmd} $combinedClassesJar $$(ls $outDir/classes.jar 2> /dev/null) $$(ls $outDir/libs/*.jar 2> /dev/null)`,
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001148 CommandDeps: []string{"${config.MergeZipsCmd}", "${config.Zip2ZipCmd}"},
Colin Crossfabb6082018-02-20 17:22:23 -08001149 },
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001150 "outDir", "combinedClassesJar", "assetsPackage")
Colin Crossfabb6082018-02-20 17:22:23 -08001151
1152func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1153 if len(a.properties.Aars) != 1 {
1154 ctx.PropertyErrorf("aars", "exactly one aar is required")
1155 return
1156 }
1157
Jiyong Park92315372021-04-02 08:45:46 +09001158 a.sdkVersion = a.SdkVersion(ctx)
1159 a.minSdkVersion = a.MinSdkVersion(ctx)
1160
Colin Crossff694a82023-12-13 15:54:49 -08001161 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1162 a.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07001163
Nan Zhang4c819fb2018-08-27 18:31:46 -07001164 aarName := ctx.ModuleName() + ".aar"
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001165 a.aarPath = android.PathForModuleSrc(ctx, a.properties.Aars[0])
1166
Colin Cross1001a792019-03-21 22:21:39 -07001167 if Bool(a.properties.Jetifier) {
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001168 inputFile := a.aarPath
1169 a.aarPath = android.PathForModuleOut(ctx, "jetifier", aarName)
1170 TransformJetifier(ctx, a.aarPath.(android.WritablePath), inputFile)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001171 }
Colin Crossfabb6082018-02-20 17:22:23 -08001172
Colin Cross607bbd62024-04-12 13:44:45 -07001173 jarName := ctx.ModuleName() + ".jar"
Colin Crossfabb6082018-02-20 17:22:23 -08001174 extractedAARDir := android.PathForModuleOut(ctx, "aar")
Colin Cross607bbd62024-04-12 13:44:45 -07001175 classpathFile := extractedAARDir.Join(ctx, jarName)
Colin Cross21ed4692024-04-24 20:23:38 +00001176
1177 extractedManifest := extractedAARDir.Join(ctx, "AndroidManifest.xml")
1178 providedManifest := android.OptionalPathForModuleSrc(ctx, a.properties.Manifest)
1179 if providedManifest.Valid() {
1180 a.manifest = providedManifest.Path()
1181 } else {
1182 a.manifest = extractedManifest
1183 }
1184
Colin Crossbb77d8e2024-02-15 14:43:47 -08001185 a.rTxt = extractedAARDir.Join(ctx, "R.txt")
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001186 a.assetsPackage = android.PathForModuleOut(ctx, "assets.zip")
Sam Delmerico95d70942023-08-02 18:00:35 -04001187 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Crosscde55342024-03-27 14:11:51 -07001188 transitiveProguardFlags, transitiveUnconditionalExportedFlags := collectDepProguardSpecInfo(ctx)
Colin Cross40213022023-12-13 15:19:49 -08001189 android.SetProvider(ctx, ProguardSpecInfoProvider, ProguardSpecInfo{
Sam Delmerico95d70942023-08-02 18:00:35 -04001190 ProguardFlagsFiles: android.NewDepSet[android.Path](
1191 android.POSTORDER,
1192 android.Paths{a.proguardFlags},
Colin Crosscde55342024-03-27 14:11:51 -07001193 transitiveProguardFlags,
1194 ),
1195 UnconditionallyExportedProguardFlags: android.NewDepSet[android.Path](
1196 android.POSTORDER,
Sam Delmerico95d70942023-08-02 18:00:35 -04001197 nil,
Colin Crosscde55342024-03-27 14:11:51 -07001198 transitiveUnconditionalExportedFlags,
Sam Delmerico95d70942023-08-02 18:00:35 -04001199 ),
1200 })
Colin Crossfabb6082018-02-20 17:22:23 -08001201
1202 ctx.Build(pctx, android.BuildParams{
1203 Rule: unzipAAR,
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001204 Input: a.aarPath,
Colin Cross21ed4692024-04-24 20:23:38 +00001205 Outputs: android.WritablePaths{classpathFile, a.proguardFlags, extractedManifest, a.assetsPackage, a.rTxt},
Colin Crossfabb6082018-02-20 17:22:23 -08001206 Description: "unzip AAR",
1207 Args: map[string]string{
Colin Cross205e9112020-08-06 13:20:17 -07001208 "outDir": extractedAARDir.String(),
Colin Cross9055e212024-03-23 04:43:41 +00001209 "combinedClassesJar": classpathFile.String(),
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001210 "assetsPackage": a.assetsPackage.String(),
Colin Crossfabb6082018-02-20 17:22:23 -08001211 },
1212 })
1213
Colin Crossa0ba2f52019-06-22 12:59:27 -07001214 // Always set --pseudo-localize, it will be stripped out later for release
1215 // builds that don't want it.
1216 compileFlags := []string{"--pseudo-localize"}
Colin Crossfabb6082018-02-20 17:22:23 -08001217 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
Colin Crossfabb6082018-02-20 17:22:23 -08001218 flata := compiledResDir.Join(ctx, "gen_res.flata")
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001219 aapt2CompileZip(ctx, flata, a.aarPath, "res", compileFlags)
Colin Crossfabb6082018-02-20 17:22:23 -08001220
1221 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
Colin Crossfabb6082018-02-20 17:22:23 -08001222 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossbb77d8e2024-02-15 14:43:47 -08001223 aaptRTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -07001224 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -08001225
1226 var linkDeps android.Paths
1227
1228 linkFlags := []string{
1229 "--static-lib",
Colin Cross4eae06d2023-06-20 22:40:02 -07001230 "--merge-only",
Colin Crossfabb6082018-02-20 17:22:23 -08001231 "--auto-add-overlay",
Colin Cross7c4dc5d2024-02-13 14:29:45 -08001232 "--no-static-lib-packages",
Colin Crossfabb6082018-02-20 17:22:23 -08001233 }
1234
Colin Cross10f7c4a2018-05-23 10:59:28 -07001235 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
1236 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -08001237
Colin Cross8676c8c2023-10-12 15:58:57 -07001238 staticResourcesNodesDepSet, sharedResourcesNodesDepSet, staticRRODirsDepSet, staticManifestsDepSet, sharedLibs, libFlags :=
Jiakai Zhang36937082024-04-15 11:15:50 +00001239 aaptLibs(ctx, android.SdkContext(a), nil, nil)
Colin Cross31656952018-05-24 16:11:20 -07001240
Colin Cross8676c8c2023-10-12 15:58:57 -07001241 _ = sharedResourcesNodesDepSet
Colin Crossab8d1382023-07-14 17:23:41 +00001242 _ = staticRRODirsDepSet
Colin Cross8676c8c2023-10-12 15:58:57 -07001243
Colin Crossab8d1382023-07-14 17:23:41 +00001244 staticDeps := transitiveAarDeps(staticResourcesNodesDepSet.ToList())
Colin Crossfabb6082018-02-20 17:22:23 -08001245
Colin Crossab8d1382023-07-14 17:23:41 +00001246 linkDeps = append(linkDeps, sharedLibs...)
Colin Cross4eae06d2023-06-20 22:40:02 -07001247 linkDeps = append(linkDeps, staticDeps.resPackages()...)
Colin Crossa97c5d32018-03-28 14:58:31 -07001248 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -08001249
Colin Cross4eae06d2023-06-20 22:40:02 -07001250 overlayRes := android.Paths{flata}
1251
1252 // Treat static library dependencies of static libraries as imports.
1253 transitiveStaticLibs := staticDeps.resPackages()
1254 linkDeps = append(linkDeps, transitiveStaticLibs...)
1255 for _, staticLib := range transitiveStaticLibs {
1256 linkFlags = append(linkFlags, "-I "+staticLib.String())
1257 }
Colin Crossfabb6082018-02-20 17:22:23 -08001258
Colin Crossab8d1382023-07-14 17:23:41 +00001259 transitiveAssets := android.ReverseSliceInPlace(staticDeps.assets())
Colin Crossbb77d8e2024-02-15 14:43:47 -08001260 aapt2Link(ctx, a.exportPackage, nil, proguardOptionsFile, aaptRTxt,
Jihoon Kang84b25892023-12-01 22:01:06 +00001261 linkFlags, linkDeps, nil, overlayRes, transitiveAssets, nil, nil)
Colin Crossfabb6082018-02-20 17:22:23 -08001262
Colin Cross4eae06d2023-06-20 22:40:02 -07001263 a.rJar = android.PathForModuleOut(ctx, "busybox/R.jar")
Rico Winda2fa2632024-03-13 13:09:17 +01001264 resourceProcessorBusyBoxGenerateBinaryR(ctx, a.rTxt, a.manifest, a.rJar, nil, true, nil, false)
Colin Cross4eae06d2023-06-20 22:40:02 -07001265
Colin Crossf3b7bad2023-08-02 15:49:00 -07001266 aapt2ExtractExtraPackages(ctx, a.extraAaptPackagesFile, a.rJar)
1267
Colin Crossab8d1382023-07-14 17:23:41 +00001268 resourcesNodesDepSetBuilder := android.NewDepSetBuilder[*resourcesNode](android.TOPOLOGICAL)
1269 resourcesNodesDepSetBuilder.Direct(&resourcesNode{
1270 resPackage: a.exportPackage,
1271 manifest: a.manifest,
Colin Cross4eae06d2023-06-20 22:40:02 -07001272 rTxt: a.rTxt,
1273 rJar: a.rJar,
Colin Crossab8d1382023-07-14 17:23:41 +00001274 assets: android.OptionalPathForPath(a.assetsPackage),
Colin Cross4eae06d2023-06-20 22:40:02 -07001275
1276 usedResourceProcessor: true,
Colin Crossab8d1382023-07-14 17:23:41 +00001277 })
1278 resourcesNodesDepSetBuilder.Transitive(staticResourcesNodesDepSet)
1279 a.resourcesNodesDepSet = resourcesNodesDepSetBuilder.Build()
1280
1281 manifestDepSetBuilder := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(a.manifest)
Colin Cross9055e212024-03-23 04:43:41 +00001282 manifestDepSetBuilder.Transitive(staticManifestsDepSet)
Colin Crossab8d1382023-07-14 17:23:41 +00001283 a.manifestsDepSet = manifestDepSetBuilder.Build()
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001284
Colin Cross312634e2023-11-21 15:13:56 -08001285 transitiveAaptResourcePackages := staticDeps.resPackages().Strings()
1286 transitiveAaptResourcePackages = slices.DeleteFunc(transitiveAaptResourcePackages, func(p string) bool {
1287 return p == a.exportPackage.String()
1288 })
1289 transitiveAaptResourcePackagesFile := android.PathForModuleOut(ctx, "transitive-res-packages")
1290 android.WriteFileRule(ctx, transitiveAaptResourcePackagesFile, strings.Join(transitiveAaptResourcePackages, "\n"))
1291 a.transitiveAaptResourcePackagesFile = transitiveAaptResourcePackagesFile
Colin Cross4eae06d2023-06-20 22:40:02 -07001292
Sam Delmerico9f9c0a22022-11-29 11:19:37 -05001293 a.collectTransitiveHeaderJars(ctx)
Colin Cross9055e212024-03-23 04:43:41 +00001294
1295 a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
1296
1297 var staticJars android.Paths
1298 var staticHeaderJars android.Paths
Colin Cross607bbd62024-04-12 13:44:45 -07001299 var staticResourceJars android.Paths
Colin Cross9055e212024-03-23 04:43:41 +00001300 ctx.VisitDirectDeps(func(module android.Module) {
1301 if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
1302 tag := ctx.OtherModuleDependencyTag(module)
1303 switch tag {
1304 case staticLibTag:
1305 staticJars = append(staticJars, dep.ImplementationJars...)
1306 staticHeaderJars = append(staticHeaderJars, dep.HeaderJars...)
Colin Cross607bbd62024-04-12 13:44:45 -07001307 staticResourceJars = append(staticResourceJars, dep.ResourceJars...)
Colin Cross9055e212024-03-23 04:43:41 +00001308 }
1309 }
1310 addCLCFromDep(ctx, module, a.classLoaderContexts)
Jiakai Zhang36937082024-04-15 11:15:50 +00001311 addMissingOptionalUsesLibsFromDep(ctx, module, &a.usesLibrary)
Colin Cross9055e212024-03-23 04:43:41 +00001312 })
1313
Colin Cross28ac2ff2024-04-02 12:21:34 -07001314 var implementationJarFile android.OutputPath
Colin Cross9055e212024-03-23 04:43:41 +00001315 if len(staticJars) > 0 {
1316 combineJars := append(android.Paths{classpathFile}, staticJars...)
Colin Cross607bbd62024-04-12 13:44:45 -07001317 implementationJarFile = android.PathForModuleOut(ctx, "combined", jarName).OutputPath
Colin Cross28ac2ff2024-04-02 12:21:34 -07001318 TransformJarsToJar(ctx, implementationJarFile, "combine", combineJars, android.OptionalPath{}, false, nil, nil)
Colin Cross9055e212024-03-23 04:43:41 +00001319 } else {
Colin Cross28ac2ff2024-04-02 12:21:34 -07001320 implementationJarFile = classpathFile
Colin Cross9055e212024-03-23 04:43:41 +00001321 }
1322
Colin Cross607bbd62024-04-12 13:44:45 -07001323 var resourceJarFile android.Path
1324 if len(staticResourceJars) > 1 {
1325 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1326 TransformJarsToJar(ctx, combinedJar, "for resources", staticResourceJars, android.OptionalPath{},
1327 false, nil, nil)
1328 resourceJarFile = combinedJar
1329 } else if len(staticResourceJars) == 1 {
1330 resourceJarFile = staticResourceJars[0]
1331 }
1332
1333 // merge implementation jar with resources if necessary
1334 implementationAndResourcesJar := implementationJarFile
1335 if resourceJarFile != nil {
1336 jars := android.Paths{resourceJarFile, implementationAndResourcesJar}
1337 combinedJar := android.PathForModuleOut(ctx, "withres", jarName).OutputPath
1338 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1339 false, nil, nil)
1340 implementationAndResourcesJar = combinedJar
1341 }
1342
1343 a.implementationJarFile = implementationJarFile
Colin Cross28ac2ff2024-04-02 12:21:34 -07001344 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
Colin Cross607bbd62024-04-12 13:44:45 -07001345 a.implementationAndResourcesJarFile = implementationAndResourcesJar.WithoutRel()
Colin Cross28ac2ff2024-04-02 12:21:34 -07001346
Colin Cross9055e212024-03-23 04:43:41 +00001347 if len(staticHeaderJars) > 0 {
1348 combineJars := append(android.Paths{classpathFile}, staticHeaderJars...)
Colin Cross607bbd62024-04-12 13:44:45 -07001349 a.headerJarFile = android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross9055e212024-03-23 04:43:41 +00001350 TransformJarsToJar(ctx, a.headerJarFile, "combine header jars", combineJars, android.OptionalPath{}, false, nil, nil)
1351 } else {
1352 a.headerJarFile = classpathFile
1353 }
1354
Colin Cross40213022023-12-13 15:19:49 -08001355 android.SetProvider(ctx, JavaInfoProvider, JavaInfo{
Colin Cross9055e212024-03-23 04:43:41 +00001356 HeaderJars: android.PathsIfNonNil(a.headerJarFile),
Colin Cross607bbd62024-04-12 13:44:45 -07001357 ResourceJars: android.PathsIfNonNil(resourceJarFile),
Sam Delmerico9f9c0a22022-11-29 11:19:37 -05001358 TransitiveLibsHeaderJars: a.transitiveLibsHeaderJars,
1359 TransitiveStaticLibsHeaderJars: a.transitiveStaticLibsHeaderJars,
Colin Cross607bbd62024-04-12 13:44:45 -07001360 ImplementationAndResourcesJars: android.PathsIfNonNil(a.implementationAndResourcesJarFile),
Colin Cross9055e212024-03-23 04:43:41 +00001361 ImplementationJars: android.PathsIfNonNil(a.implementationJarFile),
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001362 StubsLinkType: Implementation,
Joe Onorato6fe59eb2023-07-16 13:20:33 -07001363 // TransitiveAconfigFiles: // TODO(b/289117800): LOCAL_ACONFIG_FILES for prebuilts
Colin Crossdcf71b22021-02-01 13:59:03 -08001364 })
Sam Delmerico82602492022-06-10 17:05:42 +00001365
1366 if proptools.Bool(a.properties.Extract_jni) {
1367 for _, t := range ctx.MultiTargets() {
1368 arch := t.Arch.Abi[0]
1369 path := android.PathForModuleOut(ctx, arch+"_jni.zip")
1370 a.jniPackages = append(a.jniPackages, path)
1371
1372 outDir := android.PathForModuleOut(ctx, "aarForJni")
1373 aarPath := android.PathForModuleSrc(ctx, a.properties.Aars[0])
1374 ctx.Build(pctx, android.BuildParams{
1375 Rule: extractJNI,
1376 Input: aarPath,
1377 Outputs: android.WritablePaths{path},
1378 Description: "extract JNI from AAR",
1379 Args: map[string]string{
1380 "outDir": outDir.String(),
1381 "archString": arch,
1382 },
1383 })
1384 }
Sam Delmerico82602492022-06-10 17:05:42 +00001385 }
Colin Crosse8eeec92023-12-14 14:50:05 -08001386
Colin Cross40213022023-12-13 15:19:49 -08001387 android.SetProvider(ctx, JniPackageProvider, JniPackageInfo{
Colin Crosse8eeec92023-12-14 14:50:05 -08001388 JniPackages: a.jniPackages,
1389 })
Colin Crossdcf71b22021-02-01 13:59:03 -08001390}
Colin Crossfabb6082018-02-20 17:22:23 -08001391
1392func (a *AARImport) HeaderJars() android.Paths {
Colin Cross9055e212024-03-23 04:43:41 +00001393 return android.Paths{a.headerJarFile}
Colin Crossfabb6082018-02-20 17:22:23 -08001394}
1395
Colin Cross331a1212018-08-15 20:40:52 -07001396func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
Colin Cross607bbd62024-04-12 13:44:45 -07001397 return android.Paths{a.implementationAndResourcesJarFile}
Colin Cross331a1212018-08-15 20:40:52 -07001398}
1399
Colin Cross9055e212024-03-23 04:43:41 +00001400func (a *AARImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
1401 return OptionalDexJarPath{}
Colin Crossf24a22a2019-01-31 14:12:44 -08001402}
1403
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001404func (a *AARImport) DexJarInstallPath() android.Path {
1405 return nil
1406}
1407
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001408func (a *AARImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
Colin Cross9055e212024-03-23 04:43:41 +00001409 return a.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001410}
1411
Colin Cross9055e212024-03-23 04:43:41 +00001412var _ UsesLibraryDependency = (*AARImport)(nil)
1413
Jiyong Park45bf82e2020-12-15 22:29:02 +09001414var _ android.ApexModule = (*AARImport)(nil)
1415
1416// Implements android.ApexModule
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09001417func (a *AARImport) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1418 return a.depIsInSameApex(ctx, dep)
1419}
1420
Jiyong Park45bf82e2020-12-15 22:29:02 +09001421// Implements android.ApexModule
Colin Cross9055e212024-03-23 04:43:41 +00001422func (a *AARImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
Dan Albertc8060532020-07-22 22:32:17 -07001423 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001424 return nil
1425}
1426
Sam Delmericoaf8bb702022-07-25 15:39:32 -04001427var _ android.PrebuiltInterface = (*AARImport)(nil)
Colin Crossfabb6082018-02-20 17:22:23 -08001428
Jiakai Zhangf98da192024-04-15 11:15:41 +00001429func (a *AARImport) UsesLibrary() *usesLibrary {
1430 return &a.usesLibrary
1431}
1432
1433var _ ModuleWithUsesLibrary = (*AARImport)(nil)
1434
Colin Cross1b16b0e2019-02-12 14:41:32 -08001435// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
1436//
1437// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
1438// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -08001439func AARImportFactory() android.Module {
1440 module := &AARImport{}
1441
Colin Cross9055e212024-03-23 04:43:41 +00001442 module.AddProperties(
1443 &module.properties,
1444 &module.usesLibrary.usesLibraryProperties,
1445 )
Colin Crossfabb6082018-02-20 17:22:23 -08001446
1447 android.InitPrebuiltModule(module, &module.properties.Aars)
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09001448 android.InitApexModule(module)
Sam Delmerico82602492022-06-10 17:05:42 +00001449 InitJavaModuleMultiTargets(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -08001450 return module
1451}