blob: 07392f6e530a99d016716c3df6427ce938ba6086 [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 {
Colin Crosseee4ab12024-03-27 11:54:10 -0700167 return BoolDefault(a.aaptProperties.Use_resource_processor, ctx.Config().UseResourceProcessorByDefault()) &&
168 // TODO(b/331641946): remove this when ResourceProcessorBusyBox supports generating shared libraries.
169 !slices.Contains(a.aaptProperties.Aaptflags, "--shared-lib")
Colin Cross4eae06d2023-06-20 22:40:02 -0700170}
171
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900172func (a *aapt) filterProduct() string {
173 return String(a.aaptProperties.Filter_product)
174}
175
Colin Crossa97c5d32018-03-28 14:58:31 -0700176func (a *aapt) ExportPackage() android.Path {
177 return a.exportPackage
178}
Colin Crossab8d1382023-07-14 17:23:41 +0000179func (a *aapt) ResourcesNodeDepSet() *android.DepSet[*resourcesNode] {
180 return a.resourcesNodesDepSet
Colin Crossc1c37552019-01-31 11:42:41 -0800181}
182
Colin Crossab8d1382023-07-14 17:23:41 +0000183func (a *aapt) RRODirsDepSet() *android.DepSet[rroDir] {
184 return a.rroDirsDepSet
Colin Crossc1c37552019-01-31 11:42:41 -0800185}
186
Colin Crossab8d1382023-07-14 17:23:41 +0000187func (a *aapt) ManifestsDepSet() *android.DepSet[android.Path] {
188 return a.manifestsDepSet
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800189}
190
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700191func (a *aapt) SetRROEnforcedForDependent(enforce bool) {
192 a.aaptProperties.RROEnforcedForDependent = enforce
193}
194
195func (a *aapt) IsRROEnforced(ctx android.BaseModuleContext) bool {
196 // True if RRO is enforced for this module or...
197 return ctx.Config().EnforceRROForModule(ctx.ModuleName()) ||
Jeongik Chacee5ba92021-02-19 12:11:51 +0900198 // if RRO is enforced for any of its dependents.
199 a.aaptProperties.RROEnforcedForDependent
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700200}
201
Jiyong Parkf1691d22021-03-29 20:11:58 +0900202func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext android.SdkContext,
Colin Crossa0ba2f52019-06-22 12:59:27 -0700203 manifestPath android.Path) (compileFlags, linkFlags []string, linkDeps android.Paths,
204 resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700205
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800206 hasVersionCode := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-code")
207 hasVersionName := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-name")
Colin Crossa97c5d32018-03-28 14:58:31 -0700208
Colin Crossa97c5d32018-03-28 14:58:31 -0700209 // Flags specified in Android.bp
210 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
211
Eric Miao40eab202023-03-30 16:57:17 +0000212 linkFlags = append(linkFlags, "--enable-compact-entries")
Colin Crossa97c5d32018-03-28 14:58:31 -0700213
214 // Find implicit or explicit asset and resource dirs
Jiakai Zhangba82e282023-10-13 18:08:59 +0100215 assets := android.PathsRelativeToModuleSourceDir(android.SourceInput{
216 Context: ctx,
217 Paths: a.aaptProperties.Assets,
218 IncludeDirs: false,
219 })
Colin Crossa97c5d32018-03-28 14:58:31 -0700220 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
221 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
Colin Cross8a497952019-03-05 22:25:09 -0800222 resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips)
Colin Crossa97c5d32018-03-28 14:58:31 -0700223
Colin Crossa97c5d32018-03-28 14:58:31 -0700224 // Glob directories into lists of paths
225 for _, dir := range resourceDirs {
226 resDirs = append(resDirs, globbedResourceDir{
227 dir: dir,
228 files: androidResourceGlob(ctx, dir),
229 })
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700230 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, a, dir)
Colin Crossa97c5d32018-03-28 14:58:31 -0700231 overlayDirs = append(overlayDirs, resOverlayDirs...)
232 rroDirs = append(rroDirs, resRRODirs...)
233 }
234
Colin Crossc20dc852020-11-10 12:27:45 -0800235 var assetDeps android.Paths
236 for i, dir := range assetDirs {
237 // Add a dependency on every file in the asset directory. This ensures the aapt2
238 // rule will be rerun if one of the files in the asset directory is modified.
239 assetDeps = append(assetDeps, androidResourceGlob(ctx, dir)...)
240
241 // Add a dependency on a file that contains a list of all the files in the asset directory.
242 // This ensures the aapt2 rule will be run if a file is removed from the asset directory,
243 // or a file is added whose timestamp is older than the output of aapt2.
244 assetFileListFile := android.PathForModuleOut(ctx, "asset_dir_globs", strconv.Itoa(i)+".glob")
245 androidResourceGlobList(ctx, dir, assetFileListFile)
246 assetDeps = append(assetDeps, assetFileListFile)
Colin Crossa97c5d32018-03-28 14:58:31 -0700247 }
248
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700249 assetDirStrings := assetDirs.Strings()
250 if a.noticeFile.Valid() {
251 assetDirStrings = append(assetDirStrings, filepath.Dir(a.noticeFile.Path().String()))
Colin Crossc20dc852020-11-10 12:27:45 -0800252 assetDeps = append(assetDeps, a.noticeFile.Path())
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700253 }
Jiakai Zhangba82e282023-10-13 18:08:59 +0100254 if len(assets) > 0 {
255 // aapt2 doesn't support adding individual asset files. Create a temp directory to hold asset
256 // files and pass it to aapt2.
257 tmpAssetDir := android.PathForModuleOut(ctx, "tmp_asset_dir")
258
259 rule := android.NewRuleBuilder(pctx, ctx)
260 rule.Command().
261 Text("rm -rf").Text(tmpAssetDir.String()).
262 Text("&&").
263 Text("mkdir -p").Text(tmpAssetDir.String())
264
265 for _, asset := range assets {
266 output := tmpAssetDir.Join(ctx, asset.Rel())
267 assetDeps = append(assetDeps, output)
268 rule.Command().Text("mkdir -p").Text(filepath.Dir(output.String()))
269 rule.Command().Text("cp").Input(asset).Output(output)
270 }
271
272 rule.Build("tmp_asset_dir", "tmp_asset_dir")
273
274 assetDirStrings = append(assetDirStrings, tmpAssetDir.String())
275 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700276
Colin Crossa97c5d32018-03-28 14:58:31 -0700277 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
278 linkDeps = append(linkDeps, manifestPath)
279
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700280 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirStrings, "-A "))
Colin Crossc20dc852020-11-10 12:27:45 -0800281 linkDeps = append(linkDeps, assetDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700282
Spandan Das50885c02023-02-23 21:31:33 +0000283 // Returns the effective version for {min|target}_sdk_version
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000284 effectiveVersionString := func(sdkVersion android.SdkSpec, minSdkVersion android.ApiLevel) string {
Spandan Das50885c02023-02-23 21:31:33 +0000285 // If {min|target}_sdk_version is current, use sdk_version to determine the effective level
286 // This is necessary for vendor modules.
287 // The effective version does not _only_ depend on {min|target}_sdk_version(level),
288 // but also on the sdk_version (kind+level)
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000289 if minSdkVersion.IsCurrent() {
Spandan Das50885c02023-02-23 21:31:33 +0000290 ret, err := sdkVersion.EffectiveVersionString(ctx)
291 if err != nil {
292 ctx.ModuleErrorf("invalid sdk_version: %s", err)
293 }
294 return ret
295 }
296 ret, err := minSdkVersion.EffectiveVersionString(ctx)
297 if err != nil {
298 ctx.ModuleErrorf("invalid min_sdk_version: %s", err)
299 }
300 return ret
Jiyong Park6a927c42020-01-21 02:03:43 +0900301 }
Spandan Das50885c02023-02-23 21:31:33 +0000302 // SDK version flags
303 sdkVersion := sdkContext.SdkVersion(ctx)
304 minSdkVersion := effectiveVersionString(sdkVersion, sdkContext.MinSdkVersion(ctx))
Colin Crossa97c5d32018-03-28 14:58:31 -0700305
Colin Cross83bb3162018-06-25 15:48:06 -0700306 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
Spandan Das6450b552023-02-23 19:27:07 +0000307 // Use minSdkVersion for target-sdk-version, even if `target_sdk_version` is set
308 // This behavior has been copied from Make.
Colin Cross83bb3162018-06-25 15:48:06 -0700309 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700310
Colin Crossa97c5d32018-03-28 14:58:31 -0700311 // Version code
312 if !hasVersionCode {
Dan Albert4f378d72020-07-23 17:32:15 -0700313 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion().String())
Colin Crossa97c5d32018-03-28 14:58:31 -0700314 }
315
316 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700317 var versionName string
318 if ctx.ModuleName() == "framework-res" {
319 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
320 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700321 // if it contains the build number. Use the PlatformVersionName instead.
322 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700323 } else {
324 versionName = ctx.Config().AppsDefaultVersionName()
325 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800326 versionName = proptools.NinjaEscape(versionName)
Colin Crossa97c5d32018-03-28 14:58:31 -0700327 linkFlags = append(linkFlags, "--version-name ", versionName)
328 }
329
Colin Crossa0ba2f52019-06-22 12:59:27 -0700330 linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"})
331
332 // Always set --pseudo-localize, it will be stripped out later for release
333 // builds that don't want it.
334 compileFlags = append(compileFlags, "--pseudo-localize")
335
336 return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
Colin Crossa97c5d32018-03-28 14:58:31 -0700337}
338
Paul Duffin250e6192019-06-07 10:44:37 +0100339func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) {
Colin Cross42308aa2018-11-14 21:44:17 -0800340 if sdkDep.frameworkResModule != "" {
341 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700342 }
343}
344
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800345var extractAssetsRule = pctx.AndroidStaticRule("extractAssets",
346 blueprint.RuleParams{
347 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "assets/**/*"`,
348 CommandDeps: []string{"${config.Zip2ZipCmd}"},
349 })
350
Alixf7a10272023-09-27 16:47:56 +0000351type aaptBuildActionOptions struct {
352 sdkContext android.SdkContext
353 classLoaderContexts dexpreopt.ClassLoaderContextMap
354 excludedLibs []string
355 enforceDefaultTargetSdkVersion bool
Rico Winda2fa2632024-03-13 13:09:17 +0100356 forceNonFinalResourceIDs bool
Alixf7a10272023-09-27 16:47:56 +0000357 extraLinkFlags []string
Jihoon Kang84b25892023-12-01 22:01:06 +0000358 aconfigTextFiles android.Paths
Jiakai Zhang36937082024-04-15 11:15:50 +0000359 usesLibrary *usesLibrary
Alixf7a10272023-09-27 16:47:56 +0000360}
361
362func (a *aapt) buildActions(ctx android.ModuleContext, opts aaptBuildActionOptions) {
Colin Cross5446e882019-05-22 10:46:27 -0700363
Colin Cross8676c8c2023-10-12 15:58:57 -0700364 staticResourcesNodesDepSet, sharedResourcesNodesDepSet, staticRRODirsDepSet, staticManifestsDepSet, sharedExportPackages, libFlags :=
Jiakai Zhang36937082024-04-15 11:15:50 +0000365 aaptLibs(ctx, opts.sdkContext, opts.classLoaderContexts, opts.usesLibrary)
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100366
Paul Duffin06530572022-02-03 17:54:15 +0000367 // Exclude any libraries from the supplied list.
Alixf7a10272023-09-27 16:47:56 +0000368 opts.classLoaderContexts = opts.classLoaderContexts.ExcludeLibs(opts.excludedLibs)
Paul Duffin06530572022-02-03 17:54:15 +0000369
Colin Cross31656952018-05-24 16:11:20 -0700370 // App manifest file
371 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
372 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
373
Gurpreet Singh7deabfa2022-02-10 13:28:35 +0000374 manifestPath := ManifestFixer(ctx, manifestSrcPath, ManifestFixerParams{
Alixf7a10272023-09-27 16:47:56 +0000375 SdkContext: opts.sdkContext,
376 ClassLoaderContexts: opts.classLoaderContexts,
Harshit Mahajan5b8b7302022-06-10 11:24:05 +0000377 IsLibrary: a.isLibrary,
378 DefaultManifestVersion: a.defaultManifestVersion,
379 UseEmbeddedNativeLibs: a.useEmbeddedNativeLibs,
380 UsesNonSdkApis: a.usesNonSdkApis,
381 UseEmbeddedDex: a.useEmbeddedDex,
382 HasNoCode: a.hasNoCode,
383 LoggingParent: a.LoggingParent,
Alixf7a10272023-09-27 16:47:56 +0000384 EnforceDefaultTargetSdkVersion: opts.enforceDefaultTargetSdkVersion,
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000385 })
Colin Cross90c25c62019-04-19 16:22:57 -0700386
Colin Crossab8d1382023-07-14 17:23:41 +0000387 staticDeps := transitiveAarDeps(staticResourcesNodesDepSet.ToList())
Colin Cross8676c8c2023-10-12 15:58:57 -0700388 sharedDeps := transitiveAarDeps(sharedResourcesNodesDepSet.ToList())
Colin Crossab8d1382023-07-14 17:23:41 +0000389
Luca Stefanifd898822019-09-10 22:13:31 +0200390 // Add additional manifest files to transitive manifests.
391 additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)
Colin Crossab8d1382023-07-14 17:23:41 +0000392 transitiveManifestPaths := append(android.Paths{manifestPath}, additionalManifests...)
Colin Crossab8d1382023-07-14 17:23:41 +0000393 transitiveManifestPaths = append(transitiveManifestPaths, staticManifestsDepSet.ToList()...)
Colin Cross90c25c62019-04-19 16:22:57 -0700394
Colin Crossab8d1382023-07-14 17:23:41 +0000395 if len(transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) {
Alixf7a10272023-09-27 16:47:56 +0000396 manifestMergerParams := ManifestMergerParams{
397 staticLibManifests: transitiveManifestPaths[1:],
Alix96ea88452023-08-31 15:48:23 +0000398 isLibrary: a.isLibrary,
399 packageName: a.manifestValues.applicationId,
400 }
Alixf7a10272023-09-27 16:47:56 +0000401 a.mergedManifestFile = manifestMerger(ctx, transitiveManifestPaths[0], manifestMergerParams)
Colin Cross90c25c62019-04-19 16:22:57 -0700402 if !a.isLibrary {
403 // Only use the merged manifest for applications. For libraries, the transitive closure of manifests
404 // will be propagated to the final application and merged there. The merged manifest for libraries is
405 // only passed to Make, which can't handle transitive dependencies.
406 manifestPath = a.mergedManifestFile
407 }
408 } else {
409 a.mergedManifestFile = manifestPath
410 }
Colin Cross31656952018-05-24 16:11:20 -0700411
Alixf7a10272023-09-27 16:47:56 +0000412 compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, opts.sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700413
414 linkFlags = append(linkFlags, libFlags...)
Colin Cross8676c8c2023-10-12 15:58:57 -0700415 linkDeps = append(linkDeps, sharedExportPackages...)
Colin Crossab8d1382023-07-14 17:23:41 +0000416 linkDeps = append(linkDeps, staticDeps.resPackages()...)
Alixf7a10272023-09-27 16:47:56 +0000417 linkFlags = append(linkFlags, opts.extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700418 if a.isLibrary {
419 linkFlags = append(linkFlags, "--static-lib")
420 }
Rico Wind7152e822024-04-19 08:14:15 +0200421 if opts.forceNonFinalResourceIDs {
422 linkFlags = append(linkFlags, "--non-final-ids")
423 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700424
Colin Cross7c4dc5d2024-02-13 14:29:45 -0800425 linkFlags = append(linkFlags, "--no-static-lib-packages")
Colin Cross8f1b0332024-01-25 13:39:06 -0800426 if a.isLibrary && a.useResourceProcessorBusyBox(ctx) {
Colin Cross7c4dc5d2024-02-13 14:29:45 -0800427 // When building an android_library using ResourceProcessorBusyBox pass --merge-only to skip resource
428 // references validation until the final app link step when all static libraries are present.
Colin Cross4eae06d2023-06-20 22:40:02 -0700429 linkFlags = append(linkFlags, "--merge-only")
Colin Cross4eae06d2023-06-20 22:40:02 -0700430 }
431
Colin Crossa97c5d32018-03-28 14:58:31 -0700432 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
Colin Crossa97c5d32018-03-28 14:58:31 -0700433 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
434 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700435 // This file isn't used by Soong, but is generated for exporting
436 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Cross4eae06d2023-06-20 22:40:02 -0700437 var transitiveRJars android.Paths
Colin Crossf3b7bad2023-08-02 15:49:00 -0700438 var srcJar android.WritablePath
Colin Crossa97c5d32018-03-28 14:58:31 -0700439
Colin Cross4aaa84a2018-08-21 15:14:37 -0700440 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700441 for _, dir := range resDirs {
Colin Cross014489c2020-06-02 20:09:13 -0700442 a.resourceFiles = append(a.resourceFiles, dir.files...)
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900443 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags, a.filterProduct()).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700444 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700445
Colin Crossa592e3e2019-02-19 16:59:53 -0800446 for i, zip := range resZips {
447 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
Colin Crossa0ba2f52019-06-22 12:59:27 -0700448 aapt2CompileZip(ctx, flata, zip, "", compileFlags)
Colin Crossa592e3e2019-02-19 16:59:53 -0800449 compiledResDirs = append(compiledResDirs, android.Paths{flata})
450 }
451
Colin Cross4aaa84a2018-08-21 15:14:37 -0700452 var compiledRes, compiledOverlay android.Paths
453
Colin Crossab8d1382023-07-14 17:23:41 +0000454 // AAPT2 overlays are in lowest to highest priority order, reverse the topological order
455 // of transitiveStaticLibs.
456 transitiveStaticLibs := android.ReversePaths(staticDeps.resPackages())
457
Colin Cross8f1b0332024-01-25 13:39:06 -0800458 if a.isLibrary && a.useResourceProcessorBusyBox(ctx) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700459 // When building an android_library with ResourceProcessorBusyBox enabled treat static library dependencies
460 // as imports. The resources from dependencies will not be merged into this module's package-res.apk, and
461 // instead modules depending on this module will reference package-res.apk from all transitive static
462 // dependencies.
Colin Cross1d3f5902024-03-05 11:51:54 -0800463 for _, sharedDep := range sharedDeps {
464 if sharedDep.usedResourceProcessor {
465 transitiveRJars = append(transitiveRJars, sharedDep.rJar)
466 }
467 }
Colin Cross4eae06d2023-06-20 22:40:02 -0700468 for _, staticDep := range staticDeps {
469 linkDeps = append(linkDeps, staticDep.resPackage)
470 linkFlags = append(linkFlags, "-I "+staticDep.resPackage.String())
471 if staticDep.usedResourceProcessor {
472 transitiveRJars = append(transitiveRJars, staticDep.rJar)
473 }
474 }
475 } else {
476 // When building an app or building a library without ResourceProcessorBusyBox enabled all static
477 // dependencies are compiled into this module's package-res.apk as overlays.
478 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
479 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700480
Colin Crossbec85302019-02-13 13:15:46 -0800481 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700482 // If we are using static android libraries, every source file becomes an overlay.
483 // This is to emulate old AAPT behavior which simulated library support.
484 for _, compiledResDir := range compiledResDirs {
485 compiledOverlay = append(compiledOverlay, compiledResDir...)
486 }
Colin Crossbec85302019-02-13 13:15:46 -0800487 } else if a.isLibrary {
488 // Otherwise, for a static library we treat all the resources equally with no overlay.
489 for _, compiledResDir := range compiledResDirs {
490 compiledRes = append(compiledRes, compiledResDir...)
491 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700492 } else if len(compiledResDirs) > 0 {
493 // Without static libraries, the first directory is our directory, which can then be
494 // overlaid by the rest.
495 compiledRes = append(compiledRes, compiledResDirs[0]...)
496 for _, compiledResDir := range compiledResDirs[1:] {
497 compiledOverlay = append(compiledOverlay, compiledResDir...)
498 }
499 }
500
Colin Crossa97c5d32018-03-28 14:58:31 -0700501 for _, dir := range overlayDirs {
Inseob Kim34dc4cd2023-11-07 13:37:14 +0900502 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags, a.filterProduct()).Paths()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700503 }
504
Colin Crosse560c4a2019-03-19 16:03:11 -0700505 var splitPackages android.WritablePaths
506 var splits []split
507
508 for _, s := range a.splitNames {
509 suffix := strings.Replace(s, ",", "_", -1)
510 path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
511 linkFlags = append(linkFlags, "--split", path.String()+":"+s)
512 splitPackages = append(splitPackages, path)
513 splits = append(splits, split{
514 name: s,
515 suffix: suffix,
516 path: path,
517 })
518 }
519
Colin Cross8f1b0332024-01-25 13:39:06 -0800520 if !a.useResourceProcessorBusyBox(ctx) {
Colin Crossf3b7bad2023-08-02 15:49:00 -0700521 // the subdir "android" is required to be filtered by package names
522 srcJar = android.PathForModuleGen(ctx, "android", "R.srcjar")
523 }
524
Colin Crossab8d1382023-07-14 17:23:41 +0000525 // No need to specify assets from dependencies to aapt2Link for libraries, all transitive assets will be
526 // provided to the final app aapt2Link step.
527 var transitiveAssets android.Paths
528 if !a.isLibrary {
529 transitiveAssets = android.ReverseSliceInPlace(staticDeps.assets())
530 }
Colin Crossf3b7bad2023-08-02 15:49:00 -0700531 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt,
Jihoon Kang84b25892023-12-01 22:01:06 +0000532 linkFlags, linkDeps, compiledRes, compiledOverlay, transitiveAssets, splitPackages,
533 opts.aconfigTextFiles)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800534 // Extract assets from the resource package output so that they can be used later in aapt2link
535 // for modules that depend on this one.
Colin Crossab8d1382023-07-14 17:23:41 +0000536 if android.PrefixInList(linkFlags, "-A ") {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800537 assets := android.PathForModuleOut(ctx, "assets.zip")
538 ctx.Build(pctx, android.BuildParams{
539 Rule: extractAssetsRule,
540 Input: packageRes,
541 Output: assets,
542 Description: "extract assets from built resource file",
543 })
544 a.assetPackage = android.OptionalPathForPath(assets)
545 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700546
Colin Cross8f1b0332024-01-25 13:39:06 -0800547 if a.useResourceProcessorBusyBox(ctx) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700548 rJar := android.PathForModuleOut(ctx, "busybox/R.jar")
Rico Winda2fa2632024-03-13 13:09:17 +0100549 resourceProcessorBusyBoxGenerateBinaryR(ctx, rTxt, a.mergedManifestFile, rJar, staticDeps, a.isLibrary, a.aaptProperties.Aaptflags,
550 opts.forceNonFinalResourceIDs)
Colin Crossf3b7bad2023-08-02 15:49:00 -0700551 aapt2ExtractExtraPackages(ctx, extraPackages, rJar)
Colin Cross4eae06d2023-06-20 22:40:02 -0700552 transitiveRJars = append(transitiveRJars, rJar)
553 a.rJar = rJar
Colin Crossf3b7bad2023-08-02 15:49:00 -0700554 } else {
555 aapt2ExtractExtraPackages(ctx, extraPackages, srcJar)
Colin Cross4eae06d2023-06-20 22:40:02 -0700556 }
557
Colin Cross312634e2023-11-21 15:13:56 -0800558 transitiveAaptResourcePackages := staticDeps.resPackages().Strings()
559 transitiveAaptResourcePackages = slices.DeleteFunc(transitiveAaptResourcePackages, func(p string) bool {
560 return p == packageRes.String()
561 })
562 transitiveAaptResourcePackagesFile := android.PathForModuleOut(ctx, "transitive-res-packages")
563 android.WriteFileRule(ctx, transitiveAaptResourcePackagesFile, strings.Join(transitiveAaptResourcePackages, "\n"))
564
Colin Cross1d3f5902024-03-05 11:51:54 -0800565 // Reverse the list of R.jar files so that the current module comes first, and direct dependencies come before
566 // transitive dependencies.
567 transitiveRJars = android.ReversePaths(transitiveRJars)
568
Colin Crossa97c5d32018-03-28 14:58:31 -0700569 a.aaptSrcJar = srcJar
Colin Cross4eae06d2023-06-20 22:40:02 -0700570 a.transitiveAaptRJars = transitiveRJars
Colin Cross312634e2023-11-21 15:13:56 -0800571 a.transitiveAaptResourcePackagesFile = transitiveAaptResourcePackagesFile
Colin Crossa97c5d32018-03-28 14:58:31 -0700572 a.exportPackage = packageRes
573 a.manifestPath = manifestPath
574 a.proguardOptionsFile = proguardOptionsFile
Colin Cross66f78822018-05-02 12:58:28 -0700575 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700576 a.rTxt = rTxt
Colin Crosse560c4a2019-03-19 16:03:11 -0700577 a.splits = splits
Colin Crossab8d1382023-07-14 17:23:41 +0000578 a.resourcesNodesDepSet = android.NewDepSetBuilder[*resourcesNode](android.TOPOLOGICAL).
579 Direct(&resourcesNode{
580 resPackage: a.exportPackage,
581 manifest: a.manifestPath,
582 additionalManifests: additionalManifests,
Colin Cross4eae06d2023-06-20 22:40:02 -0700583 rTxt: a.rTxt,
584 rJar: a.rJar,
Colin Crossab8d1382023-07-14 17:23:41 +0000585 assets: a.assetPackage,
Colin Cross4eae06d2023-06-20 22:40:02 -0700586
Colin Cross8f1b0332024-01-25 13:39:06 -0800587 usedResourceProcessor: a.useResourceProcessorBusyBox(ctx),
Colin Crossab8d1382023-07-14 17:23:41 +0000588 }).
589 Transitive(staticResourcesNodesDepSet).Build()
590 a.rroDirsDepSet = android.NewDepSetBuilder[rroDir](android.TOPOLOGICAL).
591 Direct(rroDirs...).
592 Transitive(staticRRODirsDepSet).Build()
593 a.manifestsDepSet = android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).
594 Direct(a.manifestPath).
595 DirectSlice(additionalManifests).
596 Transitive(staticManifestsDepSet).Build()
597}
598
Colin Cross4eae06d2023-06-20 22:40:02 -0700599var resourceProcessorBusyBox = pctx.AndroidStaticRule("resourceProcessorBusyBox",
600 blueprint.RuleParams{
601 Command: "${config.JavaCmd} -cp ${config.ResourceProcessorBusyBox} " +
602 "com.google.devtools.build.android.ResourceProcessorBusyBox --tool=GENERATE_BINARY_R -- @${out}.args && " +
603 "if cmp -s ${out}.tmp ${out} ; then rm ${out}.tmp ; else mv ${out}.tmp ${out}; fi",
604 CommandDeps: []string{"${config.ResourceProcessorBusyBox}"},
605 Rspfile: "${out}.args",
606 RspfileContent: "--primaryRTxt ${rTxt} --primaryManifest ${manifest} --classJarOutput ${out}.tmp ${args}",
607 Restat: true,
608 }, "rTxt", "manifest", "args")
609
610// resourceProcessorBusyBoxGenerateBinaryR converts the R.txt file produced by aapt2 into R.class files
611// using Bazel's ResourceProcessorBusyBox tool, which is faster than compiling the R.java files and
612// supports producing classes for static dependencies that only include resources from that dependency.
613func resourceProcessorBusyBoxGenerateBinaryR(ctx android.ModuleContext, rTxt, manifest android.Path,
Rico Winda2fa2632024-03-13 13:09:17 +0100614 rJar android.WritablePath, transitiveDeps transitiveAarDeps, isLibrary bool, aaptFlags []string,
615 forceNonFinalIds bool) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700616
617 var args []string
618 var deps android.Paths
619
620 if !isLibrary {
621 // When compiling an app, pass all R.txt and AndroidManifest.xml from transitive static library dependencies
622 // to ResourceProcessorBusyBox so that it can regenerate R.class files with the final resource IDs for each
623 // package.
624 args, deps = transitiveDeps.resourceProcessorDeps()
Rico Winda2fa2632024-03-13 13:09:17 +0100625 if forceNonFinalIds {
626 args = append(args, "--finalFields=false")
627 }
Colin Cross4eae06d2023-06-20 22:40:02 -0700628 } else {
629 // When compiling a library don't pass any dependencies as it only needs to generate an R.class file for this
630 // library. Pass --finalFields=false so that the R.class file contains non-final fields so they don't get
631 // inlined into the library before the final IDs are assigned during app compilation.
632 args = append(args, "--finalFields=false")
633 }
634
Colin Crossd3f7d1a2024-01-03 19:42:25 -0800635 for i, arg := range aaptFlags {
636 const AAPT_CUSTOM_PACKAGE = "--custom-package"
637 if strings.HasPrefix(arg, AAPT_CUSTOM_PACKAGE) {
638 pkg := strings.TrimSpace(strings.TrimPrefix(arg, AAPT_CUSTOM_PACKAGE))
639 if pkg == "" && i+1 < len(aaptFlags) {
640 pkg = aaptFlags[i+1]
641 }
642 args = append(args, "--packageForR "+pkg)
643 }
644 }
645
Colin Cross4eae06d2023-06-20 22:40:02 -0700646 deps = append(deps, rTxt, manifest)
647
648 ctx.Build(pctx, android.BuildParams{
649 Rule: resourceProcessorBusyBox,
650 Output: rJar,
651 Implicits: deps,
652 Description: "ResourceProcessorBusyBox",
653 Args: map[string]string{
654 "rTxt": rTxt.String(),
655 "manifest": manifest.String(),
656 "args": strings.Join(args, " "),
657 },
658 })
659}
660
Colin Crossab8d1382023-07-14 17:23:41 +0000661type resourcesNode struct {
662 resPackage android.Path
663 manifest android.Path
664 additionalManifests android.Paths
Colin Cross4eae06d2023-06-20 22:40:02 -0700665 rTxt android.Path
666 rJar android.Path
Colin Crossab8d1382023-07-14 17:23:41 +0000667 assets android.OptionalPath
Colin Cross4eae06d2023-06-20 22:40:02 -0700668
669 usedResourceProcessor bool
Colin Crossab8d1382023-07-14 17:23:41 +0000670}
671
672type transitiveAarDeps []*resourcesNode
673
674func (t transitiveAarDeps) resPackages() android.Paths {
Colin Cross4eae06d2023-06-20 22:40:02 -0700675 paths := make(android.Paths, 0, len(t))
Colin Crossab8d1382023-07-14 17:23:41 +0000676 for _, dep := range t {
677 paths = append(paths, dep.resPackage)
678 }
Colin Cross4eae06d2023-06-20 22:40:02 -0700679 return paths
Colin Crossab8d1382023-07-14 17:23:41 +0000680}
681
682func (t transitiveAarDeps) manifests() android.Paths {
Colin Cross4eae06d2023-06-20 22:40:02 -0700683 paths := make(android.Paths, 0, len(t))
Colin Crossab8d1382023-07-14 17:23:41 +0000684 for _, dep := range t {
685 paths = append(paths, dep.manifest)
686 paths = append(paths, dep.additionalManifests...)
687 }
Colin Cross4eae06d2023-06-20 22:40:02 -0700688 return paths
689}
690
691func (t transitiveAarDeps) resourceProcessorDeps() (args []string, deps android.Paths) {
692 for _, dep := range t {
693 args = append(args, "--library="+dep.rTxt.String()+","+dep.manifest.String())
694 deps = append(deps, dep.rTxt, dep.manifest)
695 }
696 return args, deps
Colin Crossab8d1382023-07-14 17:23:41 +0000697}
698
699func (t transitiveAarDeps) assets() android.Paths {
Colin Cross4eae06d2023-06-20 22:40:02 -0700700 paths := make(android.Paths, 0, len(t))
Colin Crossab8d1382023-07-14 17:23:41 +0000701 for _, dep := range t {
702 if dep.assets.Valid() {
703 paths = append(paths, dep.assets.Path())
704 }
705 }
706 return paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700707}
708
709// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Jiakai Zhang36937082024-04-15 11:15:50 +0000710func aaptLibs(ctx android.ModuleContext, sdkContext android.SdkContext,
711 classLoaderContexts dexpreopt.ClassLoaderContextMap, usesLibrary *usesLibrary) (
Colin Cross8676c8c2023-10-12 15:58:57 -0700712 staticResourcesNodes, sharedResourcesNodes *android.DepSet[*resourcesNode], staticRRODirs *android.DepSet[rroDir],
Colin Crossab8d1382023-07-14 17:23:41 +0000713 staticManifests *android.DepSet[android.Path], sharedLibs android.Paths, flags []string) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700714
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100715 if classLoaderContexts == nil {
Ulya Trafimovich18554242020-11-03 15:55:11 +0000716 // Not all callers need to compute class loader context, those who don't just pass nil.
717 // Create a temporary class loader context here (it will be computed, but not used).
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100718 classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000719 }
720
Colin Cross83bb3162018-06-25 15:48:06 -0700721 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700722 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700723 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700724 }
725
Colin Cross8676c8c2023-10-12 15:58:57 -0700726 var staticResourcesNodeDepSets []*android.DepSet[*resourcesNode]
727 var sharedResourcesNodeDepSets []*android.DepSet[*resourcesNode]
Colin Crossab8d1382023-07-14 17:23:41 +0000728 rroDirsDepSetBuilder := android.NewDepSetBuilder[rroDir](android.TOPOLOGICAL)
729 manifestsDepSetBuilder := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL)
730
Colin Crossa97c5d32018-03-28 14:58:31 -0700731 ctx.VisitDirectDeps(func(module android.Module) {
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000732 depTag := ctx.OtherModuleDependencyTag(module)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000733
Colin Crossa97c5d32018-03-28 14:58:31 -0700734 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700735 aarDep, _ := module.(AndroidLibraryDependency)
736 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700737 exportPackage = aarDep.ExportPackage()
738 }
739
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000740 switch depTag {
Colin Cross4b964c02018-10-15 16:18:06 -0700741 case instrumentationForTag:
742 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Liz Kammeref28a4c2022-09-23 16:50:56 -0400743 case sdkLibTag, libTag:
Colin Cross5446e882019-05-22 10:46:27 -0700744 if exportPackage != nil {
Colin Cross8676c8c2023-10-12 15:58:57 -0700745 sharedResourcesNodeDepSets = append(sharedResourcesNodeDepSets, aarDep.ResourcesNodeDepSet())
Colin Cross5446e882019-05-22 10:46:27 -0700746 sharedLibs = append(sharedLibs, exportPackage)
747 }
Colin Cross5446e882019-05-22 10:46:27 -0700748 case frameworkResTag:
Colin Crossa97c5d32018-03-28 14:58:31 -0700749 if exportPackage != nil {
750 sharedLibs = append(sharedLibs, exportPackage)
751 }
752 case staticLibTag:
753 if exportPackage != nil {
Colin Cross8676c8c2023-10-12 15:58:57 -0700754 staticResourcesNodeDepSets = append(staticResourcesNodeDepSets, aarDep.ResourcesNodeDepSet())
Colin Crossab8d1382023-07-14 17:23:41 +0000755 rroDirsDepSetBuilder.Transitive(aarDep.RRODirsDepSet())
756 manifestsDepSetBuilder.Transitive(aarDep.ManifestsDepSet())
Colin Crossa97c5d32018-03-28 14:58:31 -0700757 }
758 }
Ulya Trafimovich18554242020-11-03 15:55:11 +0000759
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +0000760 addCLCFromDep(ctx, module, classLoaderContexts)
Jiakai Zhang36937082024-04-15 11:15:50 +0000761 if usesLibrary != nil {
762 addMissingOptionalUsesLibsFromDep(ctx, module, usesLibrary)
763 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700764 })
765
Colin Crossab8d1382023-07-14 17:23:41 +0000766 // AAPT2 overlays are in lowest to highest priority order, the topological order will be reversed later.
767 // Reverse the dependency order now going into the depset so that it comes out in order after the second
768 // reverse later.
769 // NOTE: this is legacy and probably incorrect behavior, for most other cases (e.g. conflicting classes in
770 // dependencies) the highest priority dependency is listed first, but for resources the highest priority
Colin Cross9055e212024-03-23 04:43:41 +0000771 // dependency has to be listed last. This is also inconsistent with the way manifests from the same
772 // transitive dependencies are merged.
Colin Crossab8d1382023-07-14 17:23:41 +0000773 staticResourcesNodes = android.NewDepSet(android.TOPOLOGICAL, nil,
Colin Cross8676c8c2023-10-12 15:58:57 -0700774 android.ReverseSliceInPlace(staticResourcesNodeDepSets))
775 sharedResourcesNodes = android.NewDepSet(android.TOPOLOGICAL, nil,
776 android.ReverseSliceInPlace(sharedResourcesNodeDepSets))
Colin Crossa97c5d32018-03-28 14:58:31 -0700777
Colin Crossab8d1382023-07-14 17:23:41 +0000778 staticRRODirs = rroDirsDepSetBuilder.Build()
779 staticManifests = manifestsDepSetBuilder.Build()
780
781 if len(staticResourcesNodes.ToList()) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700782 flags = append(flags, "--auto-add-overlay")
783 }
784
785 for _, sharedLib := range sharedLibs {
786 flags = append(flags, "-I "+sharedLib.String())
787 }
788
Colin Cross8676c8c2023-10-12 15:58:57 -0700789 return staticResourcesNodes, sharedResourcesNodes, staticRRODirs, staticManifests, sharedLibs, flags
Colin Crossa97c5d32018-03-28 14:58:31 -0700790}
791
792type AndroidLibrary struct {
793 Library
794 aapt
795
796 androidLibraryProperties androidLibraryProperties
797
798 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700799}
800
Saeid Farivar Asanjan1fca3012021-09-14 18:40:19 +0000801var _ android.OutputFileProducer = (*AndroidLibrary)(nil)
802
803// For OutputFileProducer interface
804func (a *AndroidLibrary) OutputFiles(tag string) (android.Paths, error) {
805 switch tag {
806 case ".aar":
807 return []android.Path{a.aarFile}, nil
808 default:
809 return a.Library.OutputFiles(tag)
810 }
811}
812
Colin Crossa97c5d32018-03-28 14:58:31 -0700813var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
814
815func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiakai Zhangf98da192024-04-15 11:15:41 +0000816 a.usesLibrary.deps(ctx, false)
Colin Crossa97c5d32018-03-28 14:58:31 -0700817 a.Module.deps(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900818 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
Paul Duffin250e6192019-06-07 10:44:37 +0100819 if sdkDep.hasFrameworkLibs() {
820 a.aapt.deps(ctx, sdkDep)
Colin Crossa97c5d32018-03-28 14:58:31 -0700821 }
Jihoon Kang9049c272024-03-19 21:57:36 +0000822
823 for _, aconfig_declaration := range a.aaptProperties.Flags_packages {
824 ctx.AddDependency(ctx.Module(), aconfigDeclarationTag, aconfig_declaration)
825 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700826}
827
828func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800829 a.aapt.isLibrary = true
Ulya Trafimovich42c7f0d2021-08-17 16:20:29 +0100830 a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Spandan Das0727ba72024-02-13 16:37:43 +0000831 if a.usesLibrary.shouldDisableDexpreopt {
832 a.dexpreopter.disableDexpreopt()
833 }
Alixf7a10272023-09-27 16:47:56 +0000834 a.aapt.buildActions(ctx,
835 aaptBuildActionOptions{
836 sdkContext: android.SdkContext(a),
837 classLoaderContexts: a.classLoaderContexts,
838 enforceDefaultTargetSdkVersion: false,
Jihoon Kang9049c272024-03-19 21:57:36 +0000839 aconfigTextFiles: getAconfigFilePaths(ctx),
Jiakai Zhang36937082024-04-15 11:15:50 +0000840 usesLibrary: &a.usesLibrary,
Alixf7a10272023-09-27 16:47:56 +0000841 },
842 )
Colin Crossa97c5d32018-03-28 14:58:31 -0700843
Colin Crossff694a82023-12-13 15:54:49 -0800844 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
845 a.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -0700846
yangbill2af0b6e2024-03-15 09:29:29 +0000847 a.stem = proptools.StringDefault(a.overridableProperties.Stem, ctx.ModuleName())
Jihoon Kang1bfb6f22023-07-01 00:13:47 +0000848
Colin Cross4eae06d2023-06-20 22:40:02 -0700849 ctx.CheckbuildFile(a.aapt.proguardOptionsFile)
850 ctx.CheckbuildFile(a.aapt.exportPackage)
Colin Cross8f1b0332024-01-25 13:39:06 -0800851 if a.useResourceProcessorBusyBox(ctx) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700852 ctx.CheckbuildFile(a.aapt.rJar)
Colin Crossf3b7bad2023-08-02 15:49:00 -0700853 } else {
854 ctx.CheckbuildFile(a.aapt.aaptSrcJar)
Colin Cross4eae06d2023-06-20 22:40:02 -0700855 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700856
857 // apps manifests are handled by aapt, don't let Module see them
858 a.properties.Manifest = nil
859
Colin Cross014489c2020-06-02 20:09:13 -0700860 a.linter.mergedManifest = a.aapt.mergedManifestFile
861 a.linter.manifest = a.aapt.manifestPath
862 a.linter.resources = a.aapt.resourceFiles
863
Sam Delmericoc8e040c2023-10-31 17:27:02 +0000864 proguardSpecInfo := a.collectProguardSpecInfo(ctx)
Colin Cross40213022023-12-13 15:19:49 -0800865 android.SetProvider(ctx, ProguardSpecInfoProvider, proguardSpecInfo)
Colin Cross312634e2023-11-21 15:13:56 -0800866 exportedProguardFlagsFiles := proguardSpecInfo.ProguardFlagsFiles.ToList()
867 a.extraProguardFlagsFiles = append(a.extraProguardFlagsFiles, exportedProguardFlagsFiles...)
868 a.extraProguardFlagsFiles = append(a.extraProguardFlagsFiles, a.proguardOptionsFile)
869
870 combinedExportedProguardFlagFile := android.PathForModuleOut(ctx, "export_proguard_flags")
871 writeCombinedProguardFlagsFile(ctx, combinedExportedProguardFlagFile, exportedProguardFlagsFiles)
872 a.combinedExportedProguardFlagsFile = combinedExportedProguardFlagFile
Colin Crossa97c5d32018-03-28 14:58:31 -0700873
Colin Cross4eae06d2023-06-20 22:40:02 -0700874 var extraSrcJars android.Paths
875 var extraCombinedJars android.Paths
876 var extraClasspathJars android.Paths
Colin Cross8f1b0332024-01-25 13:39:06 -0800877 if a.useResourceProcessorBusyBox(ctx) {
Colin Cross4eae06d2023-06-20 22:40:02 -0700878 // When building a library with ResourceProcessorBusyBox enabled ResourceProcessorBusyBox for this
879 // library and each of the transitive static android_library dependencies has already created an
880 // R.class file for the appropriate package. Add all of those R.class files to the classpath.
881 extraClasspathJars = a.transitiveAaptRJars
882 } else {
883 // When building a library without ResourceProcessorBusyBox the aapt2 rule creates R.srcjar containing
884 // R.java files for the library's package and the packages from all transitive static android_library
885 // dependencies. Compile the srcjar alongside the rest of the sources.
886 extraSrcJars = android.Paths{a.aapt.aaptSrcJar}
887 }
888
889 a.Module.compile(ctx, extraSrcJars, extraClasspathJars, extraCombinedJars)
Colin Crossa97c5d32018-03-28 14:58:31 -0700890
Colin Crossf57c5782019-01-25 13:20:38 -0800891 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700892 var res android.Paths
893 if a.androidLibraryProperties.BuildAAR {
894 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
895 ctx.CheckbuildFile(a.aarFile)
896 }
Colin Cross89c31582018-04-30 15:55:11 -0700897
Sam Delmerico82602492022-06-10 17:05:42 +0000898 prebuiltJniPackages := android.Paths{}
899 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800900 if info, ok := android.OtherModuleProvider(ctx, module, JniPackageProvider); ok {
Sam Delmerico82602492022-06-10 17:05:42 +0000901 prebuiltJniPackages = append(prebuiltJniPackages, info.JniPackages...)
902 }
903 })
904 if len(prebuiltJniPackages) > 0 {
Colin Cross40213022023-12-13 15:19:49 -0800905 android.SetProvider(ctx, JniPackageProvider, JniPackageInfo{
Sam Delmerico82602492022-06-10 17:05:42 +0000906 JniPackages: prebuiltJniPackages,
907 })
908 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700909}
910
Colin Cross95b53b82023-10-17 13:21:02 -0700911func (a *AndroidLibrary) IDEInfo(dpInfo *android.IdeInfo) {
912 a.Library.IDEInfo(dpInfo)
913 a.aapt.IDEInfo(dpInfo)
914}
915
916func (a *aapt) IDEInfo(dpInfo *android.IdeInfo) {
Colin Cross8f1b0332024-01-25 13:39:06 -0800917 if a.rJar != nil {
Colin Cross95b53b82023-10-17 13:21:02 -0700918 dpInfo.Jars = append(dpInfo.Jars, a.rJar.String())
919 }
920}
921
Colin Cross1b16b0e2019-02-12 14:41:32 -0800922// android_library builds and links sources into a `.jar` file for the device along with Android resources.
923//
924// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
Sam Delmerico82602492022-06-10 17:05:42 +0000925// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
Colin Cross1b16b0e2019-02-12 14:41:32 -0800926// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
927// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700928func AndroidLibraryFactory() android.Module {
929 module := &AndroidLibrary{}
930
Colin Crossce6734e2020-06-15 16:09:53 -0700931 module.Module.addHostAndDeviceProperties()
Colin Crossa97c5d32018-03-28 14:58:31 -0700932 module.AddProperties(
Colin Crossa97c5d32018-03-28 14:58:31 -0700933 &module.aaptProperties,
Ronald Braunsteincdc66f42024-04-12 11:23:19 -0700934 &module.androidLibraryProperties,
935 &module.sourceProperties)
Colin Crossa97c5d32018-03-28 14:58:31 -0700936
937 module.androidLibraryProperties.BuildAAR = true
Colin Cross014489c2020-06-02 20:09:13 -0700938 module.Module.linter.library = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700939
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900940 android.InitApexModule(module)
Colin Cross48de9a42018-10-02 13:53:33 -0700941 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700942 return module
943}
944
Colin Crossfabb6082018-02-20 17:22:23 -0800945//
946// AAR (android library) prebuilts
947//
Colin Crossfabb6082018-02-20 17:22:23 -0800948
Vinh Trance0781f2022-04-13 01:30:44 +0000949// Properties for android_library_import
Colin Crossfabb6082018-02-20 17:22:23 -0800950type AARImportProperties struct {
Vinh Trance0781f2022-04-13 01:30:44 +0000951 // ARR (android library prebuilt) filepath. Exactly one ARR is required.
Colin Cross27b922f2019-03-04 22:35:41 -0800952 Aars []string `android:"path"`
Vinh Trance0781f2022-04-13 01:30:44 +0000953 // If not blank, set to the version of the sdk to compile against.
954 // Defaults to private.
955 // Values are of one of the following forms:
956 // 1) numerical API level, "current", "none", or "core_platform"
957 // 2) An SDK kind with an API level: "<sdk kind>_<API level>"
958 // See build/soong/android/sdk_version.go for the complete and up to date list of SDK kinds.
959 // If the SDK kind is empty, it will be set to public
960 Sdk_version *string
961 // If not blank, set the minimum version of the sdk that the compiled artifacts will run against.
962 // Defaults to sdk_version if not set. See sdk_version for possible values.
Colin Cross479884c2018-07-10 13:39:30 -0700963 Min_sdk_version *string
Vinh Trance0781f2022-04-13 01:30:44 +0000964 // List of java static libraries that the included ARR (android library prebuilts) has dependencies to.
Colin Crossa97c5d32018-03-28 14:58:31 -0700965 Static_libs []string
Vinh Trance0781f2022-04-13 01:30:44 +0000966 // List of java libraries that the included ARR (android library prebuilts) has dependencies to.
967 Libs []string
968 // If set to true, run Jetifier against .aar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -0700969 Jetifier *bool
Sam Delmerico82602492022-06-10 17:05:42 +0000970 // If true, extract JNI libs from AAR archive. These libs will be accessible to android_app modules and
971 // will be passed transitively through android_libraries to an android_app.
972 //TODO(b/241138093) evaluate whether we can have this flag default to true for Bazel conversion
973 Extract_jni *bool
Colin Cross21ed4692024-04-24 20:23:38 +0000974
975 // If set, overrides the manifest extracted from the AAR with the provided path.
976 Manifest *string `android:"path"`
Colin Crossfabb6082018-02-20 17:22:23 -0800977}
978
979type AARImport struct {
980 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700981 android.DefaultableModuleBase
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900982 android.ApexModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800983 prebuilt android.Prebuilt
984
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900985 // Functionality common to Module and Import.
986 embeddableInModuleAndImport
987
Sam Delmerico9f9c0a22022-11-29 11:19:37 -0500988 providesTransitiveHeaderJars
989
Colin Crossfabb6082018-02-20 17:22:23 -0800990 properties AARImportProperties
991
Colin Cross9055e212024-03-23 04:43:41 +0000992 headerJarFile android.WritablePath
993 implementationJarFile android.WritablePath
Colin Cross607bbd62024-04-12 13:44:45 -0700994 implementationAndResourcesJarFile android.WritablePath
Colin Cross312634e2023-11-21 15:13:56 -0800995 proguardFlags android.WritablePath
996 exportPackage android.WritablePath
997 transitiveAaptResourcePackagesFile android.Path
998 extraAaptPackagesFile android.WritablePath
Colin Cross21ed4692024-04-24 20:23:38 +0000999 manifest android.Path
Colin Cross312634e2023-11-21 15:13:56 -08001000 assetsPackage android.WritablePath
1001 rTxt android.WritablePath
1002 rJar android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -07001003
Colin Crossab8d1382023-07-14 17:23:41 +00001004 resourcesNodesDepSet *android.DepSet[*resourcesNode]
1005 manifestsDepSet *android.DepSet[android.Path]
Colin Cross56a83212020-09-15 18:30:11 -07001006
1007 hideApexVariantFromMake bool
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001008
Sam Delmerico82602492022-06-10 17:05:42 +00001009 aarPath android.Path
1010 jniPackages android.Paths
Jiyong Park92315372021-04-02 08:45:46 +09001011
1012 sdkVersion android.SdkSpec
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001013 minSdkVersion android.ApiLevel
LaMont Jonesafe7baf2024-01-09 22:47:39 +00001014
Colin Cross9055e212024-03-23 04:43:41 +00001015 usesLibrary
1016 classLoaderContexts dexpreopt.ClassLoaderContextMap
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001017}
1018
1019var _ android.OutputFileProducer = (*AARImport)(nil)
1020
1021// For OutputFileProducer interface
1022func (a *AARImport) OutputFiles(tag string) (android.Paths, error) {
1023 switch tag {
1024 case ".aar":
1025 return []android.Path{a.aarPath}, nil
1026 case "":
Colin Cross607bbd62024-04-12 13:44:45 -07001027 return []android.Path{a.implementationAndResourcesJarFile}, nil
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001028 default:
1029 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1030 }
Colin Crossfabb6082018-02-20 17:22:23 -08001031}
1032
Jiyong Park92315372021-04-02 08:45:46 +09001033func (a *AARImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1034 return android.SdkSpecFrom(ctx, String(a.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07001035}
1036
Jiyong Parkf1691d22021-03-29 20:11:58 +09001037func (a *AARImport) SystemModules() string {
Paul Duffine25c6442019-10-11 13:50:28 +01001038 return ""
1039}
1040
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001041func (a *AARImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
Colin Cross479884c2018-07-10 13:39:30 -07001042 if a.properties.Min_sdk_version != nil {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001043 return android.ApiLevelFrom(ctx, *a.properties.Min_sdk_version)
Colin Cross479884c2018-07-10 13:39:30 -07001044 }
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001045 return a.SdkVersion(ctx).ApiLevel
Colin Cross83bb3162018-06-25 15:48:06 -07001046}
1047
Spandan Dasa26eda72023-03-02 00:56:06 +00001048func (a *AARImport) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
1049 return android.SdkSpecFrom(ctx, "").ApiLevel
William Loh5a082f92022-05-17 20:21:50 +00001050}
1051
Spandan Dasca70fc42023-03-01 23:38:49 +00001052func (a *AARImport) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
1053 return a.SdkVersion(ctx).ApiLevel
Dan Willemsen419290a2018-10-31 15:28:47 -07001054}
1055
Colin Cross1e743852019-10-28 11:37:20 -07001056func (a *AARImport) javaVersion() string {
1057 return ""
1058}
1059
Colin Crossa97c5d32018-03-28 14:58:31 -07001060var _ AndroidLibraryDependency = (*AARImport)(nil)
1061
1062func (a *AARImport) ExportPackage() android.Path {
1063 return a.exportPackage
1064}
Colin Crossab8d1382023-07-14 17:23:41 +00001065func (a *AARImport) ResourcesNodeDepSet() *android.DepSet[*resourcesNode] {
1066 return a.resourcesNodesDepSet
Colin Crossc1c37552019-01-31 11:42:41 -08001067}
1068
Colin Crossab8d1382023-07-14 17:23:41 +00001069func (a *AARImport) RRODirsDepSet() *android.DepSet[rroDir] {
1070 return android.NewDepSet[rroDir](android.TOPOLOGICAL, nil, nil)
Colin Cross66f78822018-05-02 12:58:28 -07001071}
1072
Colin Crossab8d1382023-07-14 17:23:41 +00001073func (a *AARImport) ManifestsDepSet() *android.DepSet[android.Path] {
1074 return a.manifestsDepSet
Jaewoong Jung6431ca72020-01-15 14:15:10 -08001075}
1076
Jaewoong Jungc779cd42020-10-06 18:56:10 -07001077// RRO enforcement is not available on aar_import since its RRO dirs are not
1078// exported.
1079func (a *AARImport) SetRROEnforcedForDependent(enforce bool) {
1080}
1081
1082// RRO enforcement is not available on aar_import since its RRO dirs are not
1083// exported.
1084func (a *AARImport) IsRROEnforced(ctx android.BaseModuleContext) bool {
1085 return false
1086}
1087
Colin Crossfabb6082018-02-20 17:22:23 -08001088func (a *AARImport) Prebuilt() *android.Prebuilt {
1089 return &a.prebuilt
1090}
1091
1092func (a *AARImport) Name() string {
1093 return a.prebuilt.Name(a.ModuleBase.Name())
1094}
1095
Jiyong Park618922e2020-01-08 13:35:43 +09001096func (a *AARImport) JacocoReportClassesFile() android.Path {
1097 return nil
1098}
1099
Colin Crossfabb6082018-02-20 17:22:23 -08001100func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Jeongik Cha816a23a2020-07-08 01:09:23 +09001101 if !ctx.Config().AlwaysUsePrebuiltSdks() {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001102 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -07001103 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -07001104 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -08001105 }
1106 }
Colin Crossa97c5d32018-03-28 14:58:31 -07001107
Colin Cross42d48b72018-08-29 14:10:52 -07001108 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
1109 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Cross9055e212024-03-23 04:43:41 +00001110
1111 a.usesLibrary.deps(ctx, false)
Colin Crossfabb6082018-02-20 17:22:23 -08001112}
1113
Sam Delmerico82602492022-06-10 17:05:42 +00001114type JniPackageInfo struct {
1115 // List of zip files containing JNI libraries
1116 // Zip files should have directory structure jni/<arch>/*.so
1117 JniPackages android.Paths
1118}
1119
Colin Crossbc7d76c2023-12-12 16:39:03 -08001120var JniPackageProvider = blueprint.NewProvider[JniPackageInfo]()
Sam Delmerico82602492022-06-10 17:05:42 +00001121
1122// Unzip an AAR and extract the JNI libs for $archString.
1123var extractJNI = pctx.AndroidStaticRule("extractJNI",
1124 blueprint.RuleParams{
1125 Command: `rm -rf $out $outDir && touch $out && ` +
1126 `unzip -qoDD -d $outDir $in "jni/${archString}/*" && ` +
1127 `jni_files=$$(find $outDir/jni -type f) && ` +
1128 // print error message if there are no JNI libs for this arch
1129 `[ -n "$$jni_files" ] || (echo "ERROR: no JNI libs found for arch ${archString}" && exit 1) && ` +
Sam Delmerico80ee45c2023-06-22 15:36:02 -04001130 `${config.SoongZipCmd} -o $out -L 0 -P 'lib/${archString}' ` +
Sam Delmerico82602492022-06-10 17:05:42 +00001131 `-C $outDir/jni/${archString} $$(echo $$jni_files | xargs -n1 printf " -f %s")`,
1132 CommandDeps: []string{"${config.SoongZipCmd}"},
1133 },
1134 "outDir", "archString")
1135
Colin Crossfabb6082018-02-20 17:22:23 -08001136// 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 -07001137// 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 -08001138var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
1139 blueprint.RuleParams{
Dan Willemsen304cfec2019-05-28 14:49:06 -07001140 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
Colin Cross205e9112020-08-06 13:20:17 -07001141 `unzip -qoDD -d $outDir $in && rm -rf $outDir/res && touch $out && ` +
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001142 `${config.Zip2ZipCmd} -i $in -o $assetsPackage 'assets/**/*' && ` +
Colin Cross205e9112020-08-06 13:20:17 -07001143 `${config.MergeZipsCmd} $combinedClassesJar $$(ls $outDir/classes.jar 2> /dev/null) $$(ls $outDir/libs/*.jar 2> /dev/null)`,
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001144 CommandDeps: []string{"${config.MergeZipsCmd}", "${config.Zip2ZipCmd}"},
Colin Crossfabb6082018-02-20 17:22:23 -08001145 },
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001146 "outDir", "combinedClassesJar", "assetsPackage")
Colin Crossfabb6082018-02-20 17:22:23 -08001147
1148func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1149 if len(a.properties.Aars) != 1 {
1150 ctx.PropertyErrorf("aars", "exactly one aar is required")
1151 return
1152 }
1153
Jiyong Park92315372021-04-02 08:45:46 +09001154 a.sdkVersion = a.SdkVersion(ctx)
1155 a.minSdkVersion = a.MinSdkVersion(ctx)
1156
Colin Crossff694a82023-12-13 15:54:49 -08001157 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1158 a.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07001159
Nan Zhang4c819fb2018-08-27 18:31:46 -07001160 aarName := ctx.ModuleName() + ".aar"
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001161 a.aarPath = android.PathForModuleSrc(ctx, a.properties.Aars[0])
1162
Colin Cross1001a792019-03-21 22:21:39 -07001163 if Bool(a.properties.Jetifier) {
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001164 inputFile := a.aarPath
1165 a.aarPath = android.PathForModuleOut(ctx, "jetifier", aarName)
1166 TransformJetifier(ctx, a.aarPath.(android.WritablePath), inputFile)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001167 }
Colin Crossfabb6082018-02-20 17:22:23 -08001168
Colin Cross607bbd62024-04-12 13:44:45 -07001169 jarName := ctx.ModuleName() + ".jar"
Colin Crossfabb6082018-02-20 17:22:23 -08001170 extractedAARDir := android.PathForModuleOut(ctx, "aar")
Colin Cross607bbd62024-04-12 13:44:45 -07001171 classpathFile := extractedAARDir.Join(ctx, jarName)
Colin Cross21ed4692024-04-24 20:23:38 +00001172
1173 extractedManifest := extractedAARDir.Join(ctx, "AndroidManifest.xml")
1174 providedManifest := android.OptionalPathForModuleSrc(ctx, a.properties.Manifest)
1175 if providedManifest.Valid() {
1176 a.manifest = providedManifest.Path()
1177 } else {
1178 a.manifest = extractedManifest
1179 }
1180
Colin Crossbb77d8e2024-02-15 14:43:47 -08001181 a.rTxt = extractedAARDir.Join(ctx, "R.txt")
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001182 a.assetsPackage = android.PathForModuleOut(ctx, "assets.zip")
Sam Delmerico95d70942023-08-02 18:00:35 -04001183 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Crosscde55342024-03-27 14:11:51 -07001184 transitiveProguardFlags, transitiveUnconditionalExportedFlags := collectDepProguardSpecInfo(ctx)
Colin Cross40213022023-12-13 15:19:49 -08001185 android.SetProvider(ctx, ProguardSpecInfoProvider, ProguardSpecInfo{
Sam Delmerico95d70942023-08-02 18:00:35 -04001186 ProguardFlagsFiles: android.NewDepSet[android.Path](
1187 android.POSTORDER,
1188 android.Paths{a.proguardFlags},
Colin Crosscde55342024-03-27 14:11:51 -07001189 transitiveProguardFlags,
1190 ),
1191 UnconditionallyExportedProguardFlags: android.NewDepSet[android.Path](
1192 android.POSTORDER,
Sam Delmerico95d70942023-08-02 18:00:35 -04001193 nil,
Colin Crosscde55342024-03-27 14:11:51 -07001194 transitiveUnconditionalExportedFlags,
Sam Delmerico95d70942023-08-02 18:00:35 -04001195 ),
1196 })
Colin Crossfabb6082018-02-20 17:22:23 -08001197
1198 ctx.Build(pctx, android.BuildParams{
1199 Rule: unzipAAR,
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001200 Input: a.aarPath,
Colin Cross21ed4692024-04-24 20:23:38 +00001201 Outputs: android.WritablePaths{classpathFile, a.proguardFlags, extractedManifest, a.assetsPackage, a.rTxt},
Colin Crossfabb6082018-02-20 17:22:23 -08001202 Description: "unzip AAR",
1203 Args: map[string]string{
Colin Cross205e9112020-08-06 13:20:17 -07001204 "outDir": extractedAARDir.String(),
Colin Cross9055e212024-03-23 04:43:41 +00001205 "combinedClassesJar": classpathFile.String(),
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001206 "assetsPackage": a.assetsPackage.String(),
Colin Crossfabb6082018-02-20 17:22:23 -08001207 },
1208 })
1209
Colin Crossa0ba2f52019-06-22 12:59:27 -07001210 // Always set --pseudo-localize, it will be stripped out later for release
1211 // builds that don't want it.
1212 compileFlags := []string{"--pseudo-localize"}
Colin Crossfabb6082018-02-20 17:22:23 -08001213 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
Colin Crossfabb6082018-02-20 17:22:23 -08001214 flata := compiledResDir.Join(ctx, "gen_res.flata")
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +00001215 aapt2CompileZip(ctx, flata, a.aarPath, "res", compileFlags)
Colin Crossfabb6082018-02-20 17:22:23 -08001216
1217 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
Colin Crossfabb6082018-02-20 17:22:23 -08001218 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossbb77d8e2024-02-15 14:43:47 -08001219 aaptRTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -07001220 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -08001221
1222 var linkDeps android.Paths
1223
1224 linkFlags := []string{
1225 "--static-lib",
Colin Cross4eae06d2023-06-20 22:40:02 -07001226 "--merge-only",
Colin Crossfabb6082018-02-20 17:22:23 -08001227 "--auto-add-overlay",
Colin Cross7c4dc5d2024-02-13 14:29:45 -08001228 "--no-static-lib-packages",
Colin Crossfabb6082018-02-20 17:22:23 -08001229 }
1230
Colin Cross10f7c4a2018-05-23 10:59:28 -07001231 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
1232 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -08001233
Colin Cross8676c8c2023-10-12 15:58:57 -07001234 staticResourcesNodesDepSet, sharedResourcesNodesDepSet, staticRRODirsDepSet, staticManifestsDepSet, sharedLibs, libFlags :=
Jiakai Zhang36937082024-04-15 11:15:50 +00001235 aaptLibs(ctx, android.SdkContext(a), nil, nil)
Colin Cross31656952018-05-24 16:11:20 -07001236
Colin Cross8676c8c2023-10-12 15:58:57 -07001237 _ = sharedResourcesNodesDepSet
Colin Crossab8d1382023-07-14 17:23:41 +00001238 _ = staticRRODirsDepSet
Colin Cross8676c8c2023-10-12 15:58:57 -07001239
Colin Crossab8d1382023-07-14 17:23:41 +00001240 staticDeps := transitiveAarDeps(staticResourcesNodesDepSet.ToList())
Colin Crossfabb6082018-02-20 17:22:23 -08001241
Colin Crossab8d1382023-07-14 17:23:41 +00001242 linkDeps = append(linkDeps, sharedLibs...)
Colin Cross4eae06d2023-06-20 22:40:02 -07001243 linkDeps = append(linkDeps, staticDeps.resPackages()...)
Colin Crossa97c5d32018-03-28 14:58:31 -07001244 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -08001245
Colin Cross4eae06d2023-06-20 22:40:02 -07001246 overlayRes := android.Paths{flata}
1247
1248 // Treat static library dependencies of static libraries as imports.
1249 transitiveStaticLibs := staticDeps.resPackages()
1250 linkDeps = append(linkDeps, transitiveStaticLibs...)
1251 for _, staticLib := range transitiveStaticLibs {
1252 linkFlags = append(linkFlags, "-I "+staticLib.String())
1253 }
Colin Crossfabb6082018-02-20 17:22:23 -08001254
Colin Crossab8d1382023-07-14 17:23:41 +00001255 transitiveAssets := android.ReverseSliceInPlace(staticDeps.assets())
Colin Crossbb77d8e2024-02-15 14:43:47 -08001256 aapt2Link(ctx, a.exportPackage, nil, proguardOptionsFile, aaptRTxt,
Jihoon Kang84b25892023-12-01 22:01:06 +00001257 linkFlags, linkDeps, nil, overlayRes, transitiveAssets, nil, nil)
Colin Crossfabb6082018-02-20 17:22:23 -08001258
Colin Cross4eae06d2023-06-20 22:40:02 -07001259 a.rJar = android.PathForModuleOut(ctx, "busybox/R.jar")
Rico Winda2fa2632024-03-13 13:09:17 +01001260 resourceProcessorBusyBoxGenerateBinaryR(ctx, a.rTxt, a.manifest, a.rJar, nil, true, nil, false)
Colin Cross4eae06d2023-06-20 22:40:02 -07001261
Colin Crossf3b7bad2023-08-02 15:49:00 -07001262 aapt2ExtractExtraPackages(ctx, a.extraAaptPackagesFile, a.rJar)
1263
Colin Crossab8d1382023-07-14 17:23:41 +00001264 resourcesNodesDepSetBuilder := android.NewDepSetBuilder[*resourcesNode](android.TOPOLOGICAL)
1265 resourcesNodesDepSetBuilder.Direct(&resourcesNode{
1266 resPackage: a.exportPackage,
1267 manifest: a.manifest,
Colin Cross4eae06d2023-06-20 22:40:02 -07001268 rTxt: a.rTxt,
1269 rJar: a.rJar,
Colin Crossab8d1382023-07-14 17:23:41 +00001270 assets: android.OptionalPathForPath(a.assetsPackage),
Colin Cross4eae06d2023-06-20 22:40:02 -07001271
1272 usedResourceProcessor: true,
Colin Crossab8d1382023-07-14 17:23:41 +00001273 })
1274 resourcesNodesDepSetBuilder.Transitive(staticResourcesNodesDepSet)
1275 a.resourcesNodesDepSet = resourcesNodesDepSetBuilder.Build()
1276
1277 manifestDepSetBuilder := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(a.manifest)
Colin Cross9055e212024-03-23 04:43:41 +00001278 manifestDepSetBuilder.Transitive(staticManifestsDepSet)
Colin Crossab8d1382023-07-14 17:23:41 +00001279 a.manifestsDepSet = manifestDepSetBuilder.Build()
Michael Rosenfeld5ad15572021-12-03 13:25:10 -08001280
Colin Cross312634e2023-11-21 15:13:56 -08001281 transitiveAaptResourcePackages := staticDeps.resPackages().Strings()
1282 transitiveAaptResourcePackages = slices.DeleteFunc(transitiveAaptResourcePackages, func(p string) bool {
1283 return p == a.exportPackage.String()
1284 })
1285 transitiveAaptResourcePackagesFile := android.PathForModuleOut(ctx, "transitive-res-packages")
1286 android.WriteFileRule(ctx, transitiveAaptResourcePackagesFile, strings.Join(transitiveAaptResourcePackages, "\n"))
1287 a.transitiveAaptResourcePackagesFile = transitiveAaptResourcePackagesFile
Colin Cross4eae06d2023-06-20 22:40:02 -07001288
Sam Delmerico9f9c0a22022-11-29 11:19:37 -05001289 a.collectTransitiveHeaderJars(ctx)
Colin Cross9055e212024-03-23 04:43:41 +00001290
1291 a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
1292
1293 var staticJars android.Paths
1294 var staticHeaderJars android.Paths
Colin Cross607bbd62024-04-12 13:44:45 -07001295 var staticResourceJars android.Paths
Colin Cross9055e212024-03-23 04:43:41 +00001296 ctx.VisitDirectDeps(func(module android.Module) {
1297 if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
1298 tag := ctx.OtherModuleDependencyTag(module)
1299 switch tag {
1300 case staticLibTag:
1301 staticJars = append(staticJars, dep.ImplementationJars...)
1302 staticHeaderJars = append(staticHeaderJars, dep.HeaderJars...)
Colin Cross607bbd62024-04-12 13:44:45 -07001303 staticResourceJars = append(staticResourceJars, dep.ResourceJars...)
Colin Cross9055e212024-03-23 04:43:41 +00001304 }
1305 }
1306 addCLCFromDep(ctx, module, a.classLoaderContexts)
Jiakai Zhang36937082024-04-15 11:15:50 +00001307 addMissingOptionalUsesLibsFromDep(ctx, module, &a.usesLibrary)
Colin Cross9055e212024-03-23 04:43:41 +00001308 })
1309
Colin Cross28ac2ff2024-04-02 12:21:34 -07001310 var implementationJarFile android.OutputPath
Colin Cross9055e212024-03-23 04:43:41 +00001311 if len(staticJars) > 0 {
1312 combineJars := append(android.Paths{classpathFile}, staticJars...)
Colin Cross607bbd62024-04-12 13:44:45 -07001313 implementationJarFile = android.PathForModuleOut(ctx, "combined", jarName).OutputPath
Colin Cross28ac2ff2024-04-02 12:21:34 -07001314 TransformJarsToJar(ctx, implementationJarFile, "combine", combineJars, android.OptionalPath{}, false, nil, nil)
Colin Cross9055e212024-03-23 04:43:41 +00001315 } else {
Colin Cross28ac2ff2024-04-02 12:21:34 -07001316 implementationJarFile = classpathFile
Colin Cross9055e212024-03-23 04:43:41 +00001317 }
1318
Colin Cross607bbd62024-04-12 13:44:45 -07001319 var resourceJarFile android.Path
1320 if len(staticResourceJars) > 1 {
1321 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1322 TransformJarsToJar(ctx, combinedJar, "for resources", staticResourceJars, android.OptionalPath{},
1323 false, nil, nil)
1324 resourceJarFile = combinedJar
1325 } else if len(staticResourceJars) == 1 {
1326 resourceJarFile = staticResourceJars[0]
1327 }
1328
1329 // merge implementation jar with resources if necessary
1330 implementationAndResourcesJar := implementationJarFile
1331 if resourceJarFile != nil {
1332 jars := android.Paths{resourceJarFile, implementationAndResourcesJar}
1333 combinedJar := android.PathForModuleOut(ctx, "withres", jarName).OutputPath
1334 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1335 false, nil, nil)
1336 implementationAndResourcesJar = combinedJar
1337 }
1338
1339 a.implementationJarFile = implementationJarFile
Colin Cross28ac2ff2024-04-02 12:21:34 -07001340 // 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 -07001341 a.implementationAndResourcesJarFile = implementationAndResourcesJar.WithoutRel()
Colin Cross28ac2ff2024-04-02 12:21:34 -07001342
Colin Cross9055e212024-03-23 04:43:41 +00001343 if len(staticHeaderJars) > 0 {
1344 combineJars := append(android.Paths{classpathFile}, staticHeaderJars...)
Colin Cross607bbd62024-04-12 13:44:45 -07001345 a.headerJarFile = android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross9055e212024-03-23 04:43:41 +00001346 TransformJarsToJar(ctx, a.headerJarFile, "combine header jars", combineJars, android.OptionalPath{}, false, nil, nil)
1347 } else {
1348 a.headerJarFile = classpathFile
1349 }
1350
Colin Cross40213022023-12-13 15:19:49 -08001351 android.SetProvider(ctx, JavaInfoProvider, JavaInfo{
Colin Cross9055e212024-03-23 04:43:41 +00001352 HeaderJars: android.PathsIfNonNil(a.headerJarFile),
Colin Cross607bbd62024-04-12 13:44:45 -07001353 ResourceJars: android.PathsIfNonNil(resourceJarFile),
Sam Delmerico9f9c0a22022-11-29 11:19:37 -05001354 TransitiveLibsHeaderJars: a.transitiveLibsHeaderJars,
1355 TransitiveStaticLibsHeaderJars: a.transitiveStaticLibsHeaderJars,
Colin Cross607bbd62024-04-12 13:44:45 -07001356 ImplementationAndResourcesJars: android.PathsIfNonNil(a.implementationAndResourcesJarFile),
Colin Cross9055e212024-03-23 04:43:41 +00001357 ImplementationJars: android.PathsIfNonNil(a.implementationJarFile),
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001358 StubsLinkType: Implementation,
Joe Onorato6fe59eb2023-07-16 13:20:33 -07001359 // TransitiveAconfigFiles: // TODO(b/289117800): LOCAL_ACONFIG_FILES for prebuilts
Colin Crossdcf71b22021-02-01 13:59:03 -08001360 })
Sam Delmerico82602492022-06-10 17:05:42 +00001361
1362 if proptools.Bool(a.properties.Extract_jni) {
1363 for _, t := range ctx.MultiTargets() {
1364 arch := t.Arch.Abi[0]
1365 path := android.PathForModuleOut(ctx, arch+"_jni.zip")
1366 a.jniPackages = append(a.jniPackages, path)
1367
1368 outDir := android.PathForModuleOut(ctx, "aarForJni")
1369 aarPath := android.PathForModuleSrc(ctx, a.properties.Aars[0])
1370 ctx.Build(pctx, android.BuildParams{
1371 Rule: extractJNI,
1372 Input: aarPath,
1373 Outputs: android.WritablePaths{path},
1374 Description: "extract JNI from AAR",
1375 Args: map[string]string{
1376 "outDir": outDir.String(),
1377 "archString": arch,
1378 },
1379 })
1380 }
Sam Delmerico82602492022-06-10 17:05:42 +00001381 }
Colin Crosse8eeec92023-12-14 14:50:05 -08001382
Colin Cross40213022023-12-13 15:19:49 -08001383 android.SetProvider(ctx, JniPackageProvider, JniPackageInfo{
Colin Crosse8eeec92023-12-14 14:50:05 -08001384 JniPackages: a.jniPackages,
1385 })
Colin Crossdcf71b22021-02-01 13:59:03 -08001386}
Colin Crossfabb6082018-02-20 17:22:23 -08001387
1388func (a *AARImport) HeaderJars() android.Paths {
Colin Cross9055e212024-03-23 04:43:41 +00001389 return android.Paths{a.headerJarFile}
Colin Crossfabb6082018-02-20 17:22:23 -08001390}
1391
Colin Cross331a1212018-08-15 20:40:52 -07001392func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
Colin Cross607bbd62024-04-12 13:44:45 -07001393 return android.Paths{a.implementationAndResourcesJarFile}
Colin Cross331a1212018-08-15 20:40:52 -07001394}
1395
Colin Cross9055e212024-03-23 04:43:41 +00001396func (a *AARImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
1397 return OptionalDexJarPath{}
Colin Crossf24a22a2019-01-31 14:12:44 -08001398}
1399
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001400func (a *AARImport) DexJarInstallPath() android.Path {
1401 return nil
1402}
1403
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001404func (a *AARImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
Colin Cross9055e212024-03-23 04:43:41 +00001405 return a.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001406}
1407
Colin Cross9055e212024-03-23 04:43:41 +00001408var _ UsesLibraryDependency = (*AARImport)(nil)
1409
Jiyong Park45bf82e2020-12-15 22:29:02 +09001410var _ android.ApexModule = (*AARImport)(nil)
1411
1412// Implements android.ApexModule
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09001413func (a *AARImport) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1414 return a.depIsInSameApex(ctx, dep)
1415}
1416
Jiyong Park45bf82e2020-12-15 22:29:02 +09001417// Implements android.ApexModule
Colin Cross9055e212024-03-23 04:43:41 +00001418func (a *AARImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
Dan Albertc8060532020-07-22 22:32:17 -07001419 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001420 return nil
1421}
1422
Sam Delmericoaf8bb702022-07-25 15:39:32 -04001423var _ android.PrebuiltInterface = (*AARImport)(nil)
Colin Crossfabb6082018-02-20 17:22:23 -08001424
Jiakai Zhangf98da192024-04-15 11:15:41 +00001425func (a *AARImport) UsesLibrary() *usesLibrary {
1426 return &a.usesLibrary
1427}
1428
1429var _ ModuleWithUsesLibrary = (*AARImport)(nil)
1430
Colin Cross1b16b0e2019-02-12 14:41:32 -08001431// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
1432//
1433// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
1434// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -08001435func AARImportFactory() android.Module {
1436 module := &AARImport{}
1437
Colin Cross9055e212024-03-23 04:43:41 +00001438 module.AddProperties(
1439 &module.properties,
1440 &module.usesLibrary.usesLibraryProperties,
1441 )
Colin Crossfabb6082018-02-20 17:22:23 -08001442
1443 android.InitPrebuiltModule(module, &module.properties.Aars)
Jooyung Hanacc7bbe2020-05-20 09:06:00 +09001444 android.InitApexModule(module)
Sam Delmerico82602492022-06-10 17:05:42 +00001445 InitJavaModuleMultiTargets(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -08001446 return module
1447}