blob: 51aad8da05a9d5e6ee93350d70b0893447256bdf [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 Crossc20dc852020-11-10 12:27:45 -080020 "strconv"
Colin Crossa97c5d32018-03-28 14:58:31 -070021 "strings"
Colin Crossfabb6082018-02-20 17:22:23 -080022
Jaewoong Jung9befb0c2020-01-18 10:33:43 -080023 "android/soong/android"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010024 "android/soong/dexpreopt"
Jaewoong Jung9befb0c2020-01-18 10:33:43 -080025
Colin Crossfabb6082018-02-20 17:22:23 -080026 "github.com/google/blueprint"
Colin Crossa97c5d32018-03-28 14:58:31 -070027 "github.com/google/blueprint/proptools"
Colin Crossfabb6082018-02-20 17:22:23 -080028)
29
Colin Crossa97c5d32018-03-28 14:58:31 -070030type AndroidLibraryDependency interface {
Colin Crossa97c5d32018-03-28 14:58:31 -070031 ExportPackage() android.Path
Colin Cross89c31582018-04-30 15:55:11 -070032 ExportedProguardFlagFiles() android.Paths
Anton Hansson53c88442019-03-18 15:53:16 +000033 ExportedRRODirs() []rroDir
Colin Cross66f78822018-05-02 12:58:28 -070034 ExportedStaticPackages() android.Paths
Colin Cross90c25c62019-04-19 16:22:57 -070035 ExportedManifests() android.Paths
Jaewoong Jung6431ca72020-01-15 14:15:10 -080036 ExportedAssets() android.OptionalPath
Jaewoong Jungc779cd42020-10-06 18:56:10 -070037 SetRROEnforcedForDependent(enforce bool)
38 IsRROEnforced(ctx android.BaseModuleContext) bool
Colin Crossa97c5d32018-03-28 14:58:31 -070039}
40
41func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000042 RegisterAARBuildComponents(android.InitRegistrationContext)
43}
44
45func RegisterAARBuildComponents(ctx android.RegistrationContext) {
46 ctx.RegisterModuleType("android_library_import", AARImportFactory)
47 ctx.RegisterModuleType("android_library", AndroidLibraryFactory)
Paul Duffin04ba70d2021-03-22 13:56:43 +000048 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
49 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
50 })
Colin Crossa97c5d32018-03-28 14:58:31 -070051}
52
53//
54// AAR (android library)
55//
56
57type androidLibraryProperties struct {
58 BuildAAR bool `blueprint:"mutated"`
59}
60
61type aaptProperties struct {
62 // flags passed to aapt when creating the apk
63 Aaptflags []string
64
Dan Willemsen72be5902018-10-24 20:24:57 -070065 // include all resource configurations, not just the product-configured
66 // ones.
67 Aapt_include_all_resources *bool
68
Colin Crossa97c5d32018-03-28 14:58:31 -070069 // list of directories relative to the Blueprints file containing assets.
Colin Cross0ddae7f2019-02-07 15:30:01 -080070 // Defaults to ["assets"] if a directory called assets exists. Set to []
71 // to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070072 Asset_dirs []string
73
74 // list of directories relative to the Blueprints file containing
Colin Cross0ddae7f2019-02-07 15:30:01 -080075 // Android resources. Defaults to ["res"] if a directory called res exists.
76 // Set to [] to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070077 Resource_dirs []string
78
Colin Crossa592e3e2019-02-19 16:59:53 -080079 // list of zip files containing Android resources.
Colin Cross27b922f2019-03-04 22:35:41 -080080 Resource_zips []string `android:"path"`
Colin Crossa592e3e2019-02-19 16:59:53 -080081
Colin Crossa97c5d32018-03-28 14:58:31 -070082 // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml".
Colin Cross27b922f2019-03-04 22:35:41 -080083 Manifest *string `android:"path"`
changho.shinb5432b72019-08-08 18:37:17 +090084
85 // paths to additional manifest files to merge with main manifest.
86 Additional_manifests []string `android:"path"`
Sasha Smundak541056c2019-10-28 15:50:06 -070087
88 // do not include AndroidManifest from dependent libraries
89 Dont_merge_manifests *bool
Jaewoong Jungc779cd42020-10-06 18:56:10 -070090
91 // true if RRO is enforced for any of the dependent modules
92 RROEnforcedForDependent bool `blueprint:"mutated"`
Colin Crossa97c5d32018-03-28 14:58:31 -070093}
94
95type aapt struct {
Colin Cross90c25c62019-04-19 16:22:57 -070096 aaptSrcJar android.Path
97 exportPackage android.Path
98 manifestPath android.Path
99 transitiveManifestPaths android.Paths
100 proguardOptionsFile android.Path
101 rroDirs []rroDir
102 rTxt android.Path
103 extraAaptPackagesFile android.Path
104 mergedManifestFile android.Path
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700105 noticeFile android.OptionalPath
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800106 assetPackage android.OptionalPath
Colin Cross90c25c62019-04-19 16:22:57 -0700107 isLibrary bool
Sasha Smundak6ad77252019-05-01 13:16:22 -0700108 useEmbeddedNativeLibs bool
Colin Cross90c25c62019-04-19 16:22:57 -0700109 useEmbeddedDex bool
110 usesNonSdkApis bool
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700111 hasNoCode bool
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800112 LoggingParent string
Colin Cross014489c2020-06-02 20:09:13 -0700113 resourceFiles android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700114
Colin Crosse560c4a2019-03-19 16:03:11 -0700115 splitNames []string
116 splits []split
117
Colin Crossa97c5d32018-03-28 14:58:31 -0700118 aaptProperties aaptProperties
119}
120
Colin Crosse560c4a2019-03-19 16:03:11 -0700121type split struct {
122 name string
123 suffix string
124 path android.Path
125}
126
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700127// Propagate RRO enforcement flag to static lib dependencies transitively.
128func propagateRROEnforcementMutator(ctx android.TopDownMutatorContext) {
129 m := ctx.Module()
130 if d, ok := m.(AndroidLibraryDependency); ok && d.IsRROEnforced(ctx) {
131 ctx.VisitDirectDepsWithTag(staticLibTag, func(d android.Module) {
132 if a, ok := d.(AndroidLibraryDependency); ok {
133 a.SetRROEnforcedForDependent(true)
134 }
135 })
136 }
137}
138
Colin Crossa97c5d32018-03-28 14:58:31 -0700139func (a *aapt) ExportPackage() android.Path {
140 return a.exportPackage
141}
142
Anton Hansson53c88442019-03-18 15:53:16 +0000143func (a *aapt) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800144 return a.rroDirs
145}
146
Colin Cross90c25c62019-04-19 16:22:57 -0700147func (a *aapt) ExportedManifests() android.Paths {
148 return a.transitiveManifestPaths
Colin Crossc1c37552019-01-31 11:42:41 -0800149}
150
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800151func (a *aapt) ExportedAssets() android.OptionalPath {
152 return a.assetPackage
153}
154
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700155func (a *aapt) SetRROEnforcedForDependent(enforce bool) {
156 a.aaptProperties.RROEnforcedForDependent = enforce
157}
158
159func (a *aapt) IsRROEnforced(ctx android.BaseModuleContext) bool {
160 // True if RRO is enforced for this module or...
161 return ctx.Config().EnforceRROForModule(ctx.ModuleName()) ||
Jeongik Chacee5ba92021-02-19 12:11:51 +0900162 // if RRO is enforced for any of its dependents.
163 a.aaptProperties.RROEnforcedForDependent
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700164}
165
Jiyong Parkf1691d22021-03-29 20:11:58 +0900166func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext android.SdkContext,
Colin Crossa0ba2f52019-06-22 12:59:27 -0700167 manifestPath android.Path) (compileFlags, linkFlags []string, linkDeps android.Paths,
168 resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700169
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800170 hasVersionCode := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-code")
171 hasVersionName := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-name")
Colin Crossa97c5d32018-03-28 14:58:31 -0700172
Colin Crossa97c5d32018-03-28 14:58:31 -0700173 // Flags specified in Android.bp
174 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
175
176 linkFlags = append(linkFlags, "--no-static-lib-packages")
177
178 // Find implicit or explicit asset and resource dirs
179 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
180 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
Colin Cross8a497952019-03-05 22:25:09 -0800181 resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips)
Colin Crossa97c5d32018-03-28 14:58:31 -0700182
Colin Crossa97c5d32018-03-28 14:58:31 -0700183 // Glob directories into lists of paths
184 for _, dir := range resourceDirs {
185 resDirs = append(resDirs, globbedResourceDir{
186 dir: dir,
187 files: androidResourceGlob(ctx, dir),
188 })
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700189 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, a, dir)
Colin Crossa97c5d32018-03-28 14:58:31 -0700190 overlayDirs = append(overlayDirs, resOverlayDirs...)
191 rroDirs = append(rroDirs, resRRODirs...)
192 }
193
Colin Crossc20dc852020-11-10 12:27:45 -0800194 var assetDeps android.Paths
195 for i, dir := range assetDirs {
196 // Add a dependency on every file in the asset directory. This ensures the aapt2
197 // rule will be rerun if one of the files in the asset directory is modified.
198 assetDeps = append(assetDeps, androidResourceGlob(ctx, dir)...)
199
200 // Add a dependency on a file that contains a list of all the files in the asset directory.
201 // This ensures the aapt2 rule will be run if a file is removed from the asset directory,
202 // or a file is added whose timestamp is older than the output of aapt2.
203 assetFileListFile := android.PathForModuleOut(ctx, "asset_dir_globs", strconv.Itoa(i)+".glob")
204 androidResourceGlobList(ctx, dir, assetFileListFile)
205 assetDeps = append(assetDeps, assetFileListFile)
Colin Crossa97c5d32018-03-28 14:58:31 -0700206 }
207
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700208 assetDirStrings := assetDirs.Strings()
209 if a.noticeFile.Valid() {
210 assetDirStrings = append(assetDirStrings, filepath.Dir(a.noticeFile.Path().String()))
Colin Crossc20dc852020-11-10 12:27:45 -0800211 assetDeps = append(assetDeps, a.noticeFile.Path())
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700212 }
213
Colin Crossa97c5d32018-03-28 14:58:31 -0700214 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
215 linkDeps = append(linkDeps, manifestPath)
216
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700217 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirStrings, "-A "))
Colin Crossc20dc852020-11-10 12:27:45 -0800218 linkDeps = append(linkDeps, assetDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700219
Colin Crossa97c5d32018-03-28 14:58:31 -0700220 // SDK version flags
Jiyong Park92315372021-04-02 08:45:46 +0900221 minSdkVersion, err := sdkContext.MinSdkVersion(ctx).EffectiveVersionString(ctx)
Jiyong Park6a927c42020-01-21 02:03:43 +0900222 if err != nil {
223 ctx.ModuleErrorf("invalid minSdkVersion: %s", err)
224 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700225
Colin Cross83bb3162018-06-25 15:48:06 -0700226 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
227 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700228
Colin Crossa97c5d32018-03-28 14:58:31 -0700229 // Version code
230 if !hasVersionCode {
Dan Albert4f378d72020-07-23 17:32:15 -0700231 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion().String())
Colin Crossa97c5d32018-03-28 14:58:31 -0700232 }
233
234 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700235 var versionName string
236 if ctx.ModuleName() == "framework-res" {
237 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
238 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700239 // if it contains the build number. Use the PlatformVersionName instead.
240 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700241 } else {
242 versionName = ctx.Config().AppsDefaultVersionName()
243 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800244 versionName = proptools.NinjaEscape(versionName)
Colin Crossa97c5d32018-03-28 14:58:31 -0700245 linkFlags = append(linkFlags, "--version-name ", versionName)
246 }
247
Colin Crossa0ba2f52019-06-22 12:59:27 -0700248 linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"})
249
250 // Always set --pseudo-localize, it will be stripped out later for release
251 // builds that don't want it.
252 compileFlags = append(compileFlags, "--pseudo-localize")
253
254 return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
Colin Crossa97c5d32018-03-28 14:58:31 -0700255}
256
Paul Duffin250e6192019-06-07 10:44:37 +0100257func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) {
Colin Cross42308aa2018-11-14 21:44:17 -0800258 if sdkDep.frameworkResModule != "" {
259 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700260 }
261}
262
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800263var extractAssetsRule = pctx.AndroidStaticRule("extractAssets",
264 blueprint.RuleParams{
265 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "assets/**/*"`,
266 CommandDeps: []string{"${config.Zip2ZipCmd}"},
267 })
268
Jiyong Parkf1691d22021-03-29 20:11:58 +0900269func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext android.SdkContext,
Paul Duffin06530572022-02-03 17:54:15 +0000270 classLoaderContexts dexpreopt.ClassLoaderContextMap, excludedLibs []string,
271 extraLinkFlags ...string) {
Colin Cross5446e882019-05-22 10:46:27 -0700272
Ulya Trafimovich18554242020-11-03 15:55:11 +0000273 transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, assetPackages, libDeps, libFlags :=
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100274 aaptLibs(ctx, sdkContext, classLoaderContexts)
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100275
Paul Duffin06530572022-02-03 17:54:15 +0000276 // Exclude any libraries from the supplied list.
277 classLoaderContexts = classLoaderContexts.ExcludeLibs(excludedLibs)
278
Colin Cross31656952018-05-24 16:11:20 -0700279 // App manifest file
280 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
281 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
282
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000283 manifestPath := ManifestFixer(ManifestFixerParams{
284 Ctx: ctx,
285 Manifest: manifestSrcPath,
286 SdkContext: sdkContext,
287 ClassLoaderContexts: classLoaderContexts,
288 IsLibrary: a.isLibrary,
289 UseEmbeddedNativeLibs: a.useEmbeddedNativeLibs,
290 UsesNonSdkApis: a.usesNonSdkApis,
291 UseEmbeddedDex: a.useEmbeddedDex,
292 HasNoCode: a.hasNoCode,
293 TestOnly: false,
294 LoggingParent: a.LoggingParent,
295 })
Colin Cross90c25c62019-04-19 16:22:57 -0700296
Luca Stefanifd898822019-09-10 22:13:31 +0200297 // Add additional manifest files to transitive manifests.
298 additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)
299 a.transitiveManifestPaths = append(android.Paths{manifestPath}, additionalManifests...)
300 a.transitiveManifestPaths = append(a.transitiveManifestPaths, transitiveStaticLibManifests...)
Colin Cross90c25c62019-04-19 16:22:57 -0700301
Sasha Smundak541056c2019-10-28 15:50:06 -0700302 if len(a.transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) {
Luca Stefanifd898822019-09-10 22:13:31 +0200303 a.mergedManifestFile = manifestMerger(ctx, a.transitiveManifestPaths[0], a.transitiveManifestPaths[1:], a.isLibrary)
Colin Cross90c25c62019-04-19 16:22:57 -0700304 if !a.isLibrary {
305 // Only use the merged manifest for applications. For libraries, the transitive closure of manifests
306 // will be propagated to the final application and merged there. The merged manifest for libraries is
307 // only passed to Make, which can't handle transitive dependencies.
308 manifestPath = a.mergedManifestFile
309 }
310 } else {
311 a.mergedManifestFile = manifestPath
312 }
Colin Cross31656952018-05-24 16:11:20 -0700313
Colin Crossa0ba2f52019-06-22 12:59:27 -0700314 compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700315
Colin Crossc1c37552019-01-31 11:42:41 -0800316 rroDirs = append(rroDirs, staticRRODirs...)
Colin Cross31656952018-05-24 16:11:20 -0700317 linkFlags = append(linkFlags, libFlags...)
318 linkDeps = append(linkDeps, libDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700319 linkFlags = append(linkFlags, extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700320 if a.isLibrary {
321 linkFlags = append(linkFlags, "--static-lib")
322 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700323
324 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900325 // the subdir "android" is required to be filtered by package names
326 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700327 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
328 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700329 // This file isn't used by Soong, but is generated for exporting
330 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Crossa97c5d32018-03-28 14:58:31 -0700331
Colin Cross4aaa84a2018-08-21 15:14:37 -0700332 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700333 for _, dir := range resDirs {
Colin Cross014489c2020-06-02 20:09:13 -0700334 a.resourceFiles = append(a.resourceFiles, dir.files...)
Colin Crossa0ba2f52019-06-22 12:59:27 -0700335 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700336 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700337
Colin Crossa592e3e2019-02-19 16:59:53 -0800338 for i, zip := range resZips {
339 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
Colin Crossa0ba2f52019-06-22 12:59:27 -0700340 aapt2CompileZip(ctx, flata, zip, "", compileFlags)
Colin Crossa592e3e2019-02-19 16:59:53 -0800341 compiledResDirs = append(compiledResDirs, android.Paths{flata})
342 }
343
Colin Cross4aaa84a2018-08-21 15:14:37 -0700344 var compiledRes, compiledOverlay android.Paths
345
346 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
347
Colin Crossbec85302019-02-13 13:15:46 -0800348 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700349 // If we are using static android libraries, every source file becomes an overlay.
350 // This is to emulate old AAPT behavior which simulated library support.
351 for _, compiledResDir := range compiledResDirs {
352 compiledOverlay = append(compiledOverlay, compiledResDir...)
353 }
Colin Crossbec85302019-02-13 13:15:46 -0800354 } else if a.isLibrary {
355 // Otherwise, for a static library we treat all the resources equally with no overlay.
356 for _, compiledResDir := range compiledResDirs {
357 compiledRes = append(compiledRes, compiledResDir...)
358 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700359 } else if len(compiledResDirs) > 0 {
360 // Without static libraries, the first directory is our directory, which can then be
361 // overlaid by the rest.
362 compiledRes = append(compiledRes, compiledResDirs[0]...)
363 for _, compiledResDir := range compiledResDirs[1:] {
364 compiledOverlay = append(compiledOverlay, compiledResDir...)
365 }
366 }
367
Colin Crossa97c5d32018-03-28 14:58:31 -0700368 for _, dir := range overlayDirs {
Colin Crossa0ba2f52019-06-22 12:59:27 -0700369 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700370 }
371
Colin Crosse560c4a2019-03-19 16:03:11 -0700372 var splitPackages android.WritablePaths
373 var splits []split
374
375 for _, s := range a.splitNames {
376 suffix := strings.Replace(s, ",", "_", -1)
377 path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
378 linkFlags = append(linkFlags, "--split", path.String()+":"+s)
379 splitPackages = append(splitPackages, path)
380 splits = append(splits, split{
381 name: s,
382 suffix: suffix,
383 path: path,
384 })
385 }
386
Colin Cross66f78822018-05-02 12:58:28 -0700387 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800388 linkFlags, linkDeps, compiledRes, compiledOverlay, assetPackages, splitPackages)
389
390 // Extract assets from the resource package output so that they can be used later in aapt2link
391 // for modules that depend on this one.
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800392 if android.PrefixInList(linkFlags, "-A ") || len(assetPackages) > 0 {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800393 assets := android.PathForModuleOut(ctx, "assets.zip")
394 ctx.Build(pctx, android.BuildParams{
395 Rule: extractAssetsRule,
396 Input: packageRes,
397 Output: assets,
398 Description: "extract assets from built resource file",
399 })
400 a.assetPackage = android.OptionalPathForPath(assets)
401 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700402
403 a.aaptSrcJar = srcJar
404 a.exportPackage = packageRes
405 a.manifestPath = manifestPath
406 a.proguardOptionsFile = proguardOptionsFile
407 a.rroDirs = rroDirs
Colin Cross66f78822018-05-02 12:58:28 -0700408 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700409 a.rTxt = rTxt
Colin Crosse560c4a2019-03-19 16:03:11 -0700410 a.splits = splits
Colin Crossa97c5d32018-03-28 14:58:31 -0700411}
412
413// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Jiyong Parkf1691d22021-03-29 20:11:58 +0900414func aaptLibs(ctx android.ModuleContext, sdkContext android.SdkContext, classLoaderContexts dexpreopt.ClassLoaderContextMap) (
Ulya Trafimovich18554242020-11-03 15:55:11 +0000415 transitiveStaticLibs, transitiveStaticLibManifests android.Paths, staticRRODirs []rroDir, assets, deps android.Paths, flags []string) {
Colin Cross66f78822018-05-02 12:58:28 -0700416
Colin Crossa97c5d32018-03-28 14:58:31 -0700417 var sharedLibs android.Paths
418
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100419 if classLoaderContexts == nil {
Ulya Trafimovich18554242020-11-03 15:55:11 +0000420 // Not all callers need to compute class loader context, those who don't just pass nil.
421 // Create a temporary class loader context here (it will be computed, but not used).
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100422 classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000423 }
424
Colin Cross83bb3162018-06-25 15:48:06 -0700425 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700426 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700427 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700428 }
429
430 ctx.VisitDirectDeps(func(module android.Module) {
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000431 depTag := ctx.OtherModuleDependencyTag(module)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000432
Colin Crossa97c5d32018-03-28 14:58:31 -0700433 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700434 aarDep, _ := module.(AndroidLibraryDependency)
435 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700436 exportPackage = aarDep.ExportPackage()
437 }
438
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000439 switch depTag {
Colin Cross4b964c02018-10-15 16:18:06 -0700440 case instrumentationForTag:
441 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Colin Cross5446e882019-05-22 10:46:27 -0700442 case libTag:
443 if exportPackage != nil {
444 sharedLibs = append(sharedLibs, exportPackage)
445 }
Colin Cross5446e882019-05-22 10:46:27 -0700446 case frameworkResTag:
Colin Crossa97c5d32018-03-28 14:58:31 -0700447 if exportPackage != nil {
448 sharedLibs = append(sharedLibs, exportPackage)
449 }
450 case staticLibTag:
451 if exportPackage != nil {
Colin Cross66f78822018-05-02 12:58:28 -0700452 transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
Colin Crossbec85302019-02-13 13:15:46 -0800453 transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
Colin Cross90c25c62019-04-19 16:22:57 -0700454 transitiveStaticLibManifests = append(transitiveStaticLibManifests, aarDep.ExportedManifests()...)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800455 if aarDep.ExportedAssets().Valid() {
456 assets = append(assets, aarDep.ExportedAssets().Path())
457 }
Anton Hansson53c88442019-03-18 15:53:16 +0000458
Jeongik Chacee5ba92021-02-19 12:11:51 +0900459 outer:
460 for _, d := range aarDep.ExportedRRODirs() {
461 for _, e := range staticRRODirs {
462 if d.path == e.path {
463 continue outer
Anton Hansson53c88442019-03-18 15:53:16 +0000464 }
465 }
Jeongik Chacee5ba92021-02-19 12:11:51 +0900466 staticRRODirs = append(staticRRODirs, d)
Anton Hansson53c88442019-03-18 15:53:16 +0000467 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700468 }
469 }
Ulya Trafimovich18554242020-11-03 15:55:11 +0000470
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +0000471 addCLCFromDep(ctx, module, classLoaderContexts)
Colin Crossa97c5d32018-03-28 14:58:31 -0700472 })
473
474 deps = append(deps, sharedLibs...)
Colin Cross66f78822018-05-02 12:58:28 -0700475 deps = append(deps, transitiveStaticLibs...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700476
Colin Cross66f78822018-05-02 12:58:28 -0700477 if len(transitiveStaticLibs) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700478 flags = append(flags, "--auto-add-overlay")
479 }
480
481 for _, sharedLib := range sharedLibs {
482 flags = append(flags, "-I "+sharedLib.String())
483 }
484
Colin Cross66f78822018-05-02 12:58:28 -0700485 transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
Colin Cross90c25c62019-04-19 16:22:57 -0700486 transitiveStaticLibManifests = android.FirstUniquePaths(transitiveStaticLibManifests)
Colin Cross66f78822018-05-02 12:58:28 -0700487
Ulya Trafimovich18554242020-11-03 15:55:11 +0000488 return transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, assets, deps, flags
Colin Crossa97c5d32018-03-28 14:58:31 -0700489}
490
491type AndroidLibrary struct {
492 Library
493 aapt
494
495 androidLibraryProperties androidLibraryProperties
496
497 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700498
499 exportedProguardFlagFiles android.Paths
Colin Cross66f78822018-05-02 12:58:28 -0700500 exportedStaticPackages android.Paths
Colin Cross89c31582018-04-30 15:55:11 -0700501}
502
Saeid Farivar Asanjan1fca3012021-09-14 18:40:19 +0000503var _ android.OutputFileProducer = (*AndroidLibrary)(nil)
504
505// For OutputFileProducer interface
506func (a *AndroidLibrary) OutputFiles(tag string) (android.Paths, error) {
507 switch tag {
508 case ".aar":
509 return []android.Path{a.aarFile}, nil
510 default:
511 return a.Library.OutputFiles(tag)
512 }
513}
514
Colin Cross89c31582018-04-30 15:55:11 -0700515func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
516 return a.exportedProguardFlagFiles
Colin Crossa97c5d32018-03-28 14:58:31 -0700517}
518
Colin Cross66f78822018-05-02 12:58:28 -0700519func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
520 return a.exportedStaticPackages
521}
522
Colin Crossa97c5d32018-03-28 14:58:31 -0700523var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
524
525func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
526 a.Module.deps(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900527 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
Paul Duffin250e6192019-06-07 10:44:37 +0100528 if sdkDep.hasFrameworkLibs() {
529 a.aapt.deps(ctx, sdkDep)
Colin Crossa97c5d32018-03-28 14:58:31 -0700530 }
Ulya Trafimovich42c7f0d2021-08-17 16:20:29 +0100531 a.usesLibrary.deps(ctx, sdkDep.hasFrameworkLibs())
Colin Crossa97c5d32018-03-28 14:58:31 -0700532}
533
534func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800535 a.aapt.isLibrary = true
Ulya Trafimovich42c7f0d2021-08-17 16:20:29 +0100536 a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Paul Duffin06530572022-02-03 17:54:15 +0000537 a.aapt.buildActions(ctx, android.SdkContext(a), a.classLoaderContexts, nil)
Colin Crossa97c5d32018-03-28 14:58:31 -0700538
Colin Cross56a83212020-09-15 18:30:11 -0700539 a.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
540
Colin Crossa97c5d32018-03-28 14:58:31 -0700541 ctx.CheckbuildFile(a.proguardOptionsFile)
542 ctx.CheckbuildFile(a.exportPackage)
543 ctx.CheckbuildFile(a.aaptSrcJar)
544
545 // apps manifests are handled by aapt, don't let Module see them
546 a.properties.Manifest = nil
547
Colin Cross014489c2020-06-02 20:09:13 -0700548 a.linter.mergedManifest = a.aapt.mergedManifestFile
549 a.linter.manifest = a.aapt.manifestPath
550 a.linter.resources = a.aapt.resourceFiles
551
Colin Crossa97c5d32018-03-28 14:58:31 -0700552 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
553 a.proguardOptionsFile)
554
555 a.Module.compile(ctx, a.aaptSrcJar)
556
Colin Crossf57c5782019-01-25 13:20:38 -0800557 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700558 var res android.Paths
559 if a.androidLibraryProperties.BuildAAR {
560 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
561 ctx.CheckbuildFile(a.aarFile)
562 }
Colin Cross89c31582018-04-30 15:55:11 -0700563
Cole Faust9a631312020-10-22 21:05:24 +0000564 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles,
565 android.PathsForModuleSrc(ctx, a.dexProperties.Optimize.Proguard_flags_files)...)
Colin Cross89c31582018-04-30 15:55:11 -0700566 ctx.VisitDirectDeps(func(m android.Module) {
567 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
568 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
Colin Cross66f78822018-05-02 12:58:28 -0700569 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
570 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
Colin Cross89c31582018-04-30 15:55:11 -0700571 }
572 })
573
574 a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
Colin Cross66f78822018-05-02 12:58:28 -0700575 a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700576}
577
Colin Cross1b16b0e2019-02-12 14:41:32 -0800578// android_library builds and links sources into a `.jar` file for the device along with Android resources.
579//
580// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
581// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
582// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
583// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700584func AndroidLibraryFactory() android.Module {
585 module := &AndroidLibrary{}
586
Colin Crossce6734e2020-06-15 16:09:53 -0700587 module.Module.addHostAndDeviceProperties()
Colin Crossa97c5d32018-03-28 14:58:31 -0700588 module.AddProperties(
Colin Crossa97c5d32018-03-28 14:58:31 -0700589 &module.aaptProperties,
590 &module.androidLibraryProperties)
591
592 module.androidLibraryProperties.BuildAAR = true
Colin Cross014489c2020-06-02 20:09:13 -0700593 module.Module.linter.library = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700594
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900595 android.InitApexModule(module)
Colin Cross48de9a42018-10-02 13:53:33 -0700596 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700597 return module
598}
599
Colin Crossfabb6082018-02-20 17:22:23 -0800600//
601// AAR (android library) prebuilts
602//
Colin Crossfabb6082018-02-20 17:22:23 -0800603
604type AARImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -0800605 Aars []string `android:"path"`
Colin Crossfabb6082018-02-20 17:22:23 -0800606
Colin Cross479884c2018-07-10 13:39:30 -0700607 Sdk_version *string
608 Min_sdk_version *string
Colin Crossa97c5d32018-03-28 14:58:31 -0700609
610 Static_libs []string
611 Libs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -0700612
613 // if set to true, run Jetifier against .aar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -0700614 Jetifier *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800615}
616
617type AARImport struct {
618 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700619 android.DefaultableModuleBase
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900620 android.ApexModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800621 prebuilt android.Prebuilt
622
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900623 // Functionality common to Module and Import.
624 embeddableInModuleAndImport
625
Colin Crossfabb6082018-02-20 17:22:23 -0800626 properties AARImportProperties
627
Colin Cross66f78822018-05-02 12:58:28 -0700628 classpathFile android.WritablePath
629 proguardFlags android.WritablePath
630 exportPackage android.WritablePath
631 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700632 manifest android.WritablePath
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800633 assetsPackage android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700634
635 exportedStaticPackages android.Paths
Colin Cross56a83212020-09-15 18:30:11 -0700636
637 hideApexVariantFromMake bool
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000638
639 aarPath android.Path
Jiyong Park92315372021-04-02 08:45:46 +0900640
641 sdkVersion android.SdkSpec
642 minSdkVersion android.SdkSpec
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000643}
644
645var _ android.OutputFileProducer = (*AARImport)(nil)
646
647// For OutputFileProducer interface
648func (a *AARImport) OutputFiles(tag string) (android.Paths, error) {
649 switch tag {
650 case ".aar":
651 return []android.Path{a.aarPath}, nil
652 case "":
653 return []android.Path{a.classpathFile}, nil
654 default:
655 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
656 }
Colin Crossfabb6082018-02-20 17:22:23 -0800657}
658
Jiyong Park92315372021-04-02 08:45:46 +0900659func (a *AARImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
660 return android.SdkSpecFrom(ctx, String(a.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700661}
662
Jiyong Parkf1691d22021-03-29 20:11:58 +0900663func (a *AARImport) SystemModules() string {
Paul Duffine25c6442019-10-11 13:50:28 +0100664 return ""
665}
666
Jiyong Park92315372021-04-02 08:45:46 +0900667func (a *AARImport) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Colin Cross479884c2018-07-10 13:39:30 -0700668 if a.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +0900669 return android.SdkSpecFrom(ctx, *a.properties.Min_sdk_version)
Colin Cross479884c2018-07-10 13:39:30 -0700670 }
Jiyong Park92315372021-04-02 08:45:46 +0900671 return a.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -0700672}
673
Jiyong Park92315372021-04-02 08:45:46 +0900674func (a *AARImport) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
675 return a.SdkVersion(ctx)
Dan Willemsen419290a2018-10-31 15:28:47 -0700676}
677
Colin Cross1e743852019-10-28 11:37:20 -0700678func (a *AARImport) javaVersion() string {
679 return ""
680}
681
Colin Crossa97c5d32018-03-28 14:58:31 -0700682var _ AndroidLibraryDependency = (*AARImport)(nil)
683
684func (a *AARImport) ExportPackage() android.Path {
685 return a.exportPackage
686}
687
Colin Cross89c31582018-04-30 15:55:11 -0700688func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
689 return android.Paths{a.proguardFlags}
690}
691
Anton Hansson53c88442019-03-18 15:53:16 +0000692func (a *AARImport) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800693 return nil
694}
695
Colin Cross66f78822018-05-02 12:58:28 -0700696func (a *AARImport) ExportedStaticPackages() android.Paths {
697 return a.exportedStaticPackages
698}
699
Colin Cross90c25c62019-04-19 16:22:57 -0700700func (a *AARImport) ExportedManifests() android.Paths {
701 return android.Paths{a.manifest}
Colin Cross31656952018-05-24 16:11:20 -0700702}
703
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800704func (a *AARImport) ExportedAssets() android.OptionalPath {
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800705 return android.OptionalPathForPath(a.assetsPackage)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800706}
707
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700708// RRO enforcement is not available on aar_import since its RRO dirs are not
709// exported.
710func (a *AARImport) SetRROEnforcedForDependent(enforce bool) {
711}
712
713// RRO enforcement is not available on aar_import since its RRO dirs are not
714// exported.
715func (a *AARImport) IsRROEnforced(ctx android.BaseModuleContext) bool {
716 return false
717}
718
Colin Crossfabb6082018-02-20 17:22:23 -0800719func (a *AARImport) Prebuilt() *android.Prebuilt {
720 return &a.prebuilt
721}
722
723func (a *AARImport) Name() string {
724 return a.prebuilt.Name(a.ModuleBase.Name())
725}
726
Jiyong Park618922e2020-01-08 13:35:43 +0900727func (a *AARImport) JacocoReportClassesFile() android.Path {
728 return nil
729}
730
Colin Crossfabb6082018-02-20 17:22:23 -0800731func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Jeongik Cha816a23a2020-07-08 01:09:23 +0900732 if !ctx.Config().AlwaysUsePrebuiltSdks() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900733 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700734 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700735 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800736 }
737 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700738
Colin Cross42d48b72018-08-29 14:10:52 -0700739 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
740 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800741}
742
743// 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 -0700744// 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 -0800745var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
746 blueprint.RuleParams{
Dan Willemsen304cfec2019-05-28 14:49:06 -0700747 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
Colin Cross205e9112020-08-06 13:20:17 -0700748 `unzip -qoDD -d $outDir $in && rm -rf $outDir/res && touch $out && ` +
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800749 `${config.Zip2ZipCmd} -i $in -o $assetsPackage 'assets/**/*' && ` +
Colin Cross205e9112020-08-06 13:20:17 -0700750 `${config.MergeZipsCmd} $combinedClassesJar $$(ls $outDir/classes.jar 2> /dev/null) $$(ls $outDir/libs/*.jar 2> /dev/null)`,
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800751 CommandDeps: []string{"${config.MergeZipsCmd}", "${config.Zip2ZipCmd}"},
Colin Crossfabb6082018-02-20 17:22:23 -0800752 },
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800753 "outDir", "combinedClassesJar", "assetsPackage")
Colin Crossfabb6082018-02-20 17:22:23 -0800754
755func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
756 if len(a.properties.Aars) != 1 {
757 ctx.PropertyErrorf("aars", "exactly one aar is required")
758 return
759 }
760
Jiyong Park92315372021-04-02 08:45:46 +0900761 a.sdkVersion = a.SdkVersion(ctx)
762 a.minSdkVersion = a.MinSdkVersion(ctx)
763
Colin Cross56a83212020-09-15 18:30:11 -0700764 a.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
765
Nan Zhang4c819fb2018-08-27 18:31:46 -0700766 aarName := ctx.ModuleName() + ".aar"
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000767 a.aarPath = android.PathForModuleSrc(ctx, a.properties.Aars[0])
768
Colin Cross1001a792019-03-21 22:21:39 -0700769 if Bool(a.properties.Jetifier) {
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000770 inputFile := a.aarPath
771 a.aarPath = android.PathForModuleOut(ctx, "jetifier", aarName)
772 TransformJetifier(ctx, a.aarPath.(android.WritablePath), inputFile)
Nan Zhang4c819fb2018-08-27 18:31:46 -0700773 }
Colin Crossfabb6082018-02-20 17:22:23 -0800774
775 extractedAARDir := android.PathForModuleOut(ctx, "aar")
Colin Cross205e9112020-08-06 13:20:17 -0700776 a.classpathFile = extractedAARDir.Join(ctx, "classes-combined.jar")
Colin Crossfabb6082018-02-20 17:22:23 -0800777 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700778 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800779 a.assetsPackage = android.PathForModuleOut(ctx, "assets.zip")
Colin Crossfabb6082018-02-20 17:22:23 -0800780
781 ctx.Build(pctx, android.BuildParams{
782 Rule: unzipAAR,
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000783 Input: a.aarPath,
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800784 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest, a.assetsPackage},
Colin Crossfabb6082018-02-20 17:22:23 -0800785 Description: "unzip AAR",
786 Args: map[string]string{
Colin Cross205e9112020-08-06 13:20:17 -0700787 "outDir": extractedAARDir.String(),
788 "combinedClassesJar": a.classpathFile.String(),
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800789 "assetsPackage": a.assetsPackage.String(),
Colin Crossfabb6082018-02-20 17:22:23 -0800790 },
791 })
792
Colin Crossa0ba2f52019-06-22 12:59:27 -0700793 // Always set --pseudo-localize, it will be stripped out later for release
794 // builds that don't want it.
795 compileFlags := []string{"--pseudo-localize"}
Colin Crossfabb6082018-02-20 17:22:23 -0800796 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
Colin Crossfabb6082018-02-20 17:22:23 -0800797 flata := compiledResDir.Join(ctx, "gen_res.flata")
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000798 aapt2CompileZip(ctx, flata, a.aarPath, "res", compileFlags)
Colin Crossfabb6082018-02-20 17:22:23 -0800799
800 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900801 // the subdir "android" is required to be filtered by package names
802 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossfabb6082018-02-20 17:22:23 -0800803 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700804 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700805 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800806
807 var linkDeps android.Paths
808
809 linkFlags := []string{
810 "--static-lib",
811 "--no-static-lib-packages",
812 "--auto-add-overlay",
813 }
814
Colin Cross10f7c4a2018-05-23 10:59:28 -0700815 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
816 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800817
Ulya Trafimovich18554242020-11-03 15:55:11 +0000818 transitiveStaticLibs, staticLibManifests, staticRRODirs, transitiveAssets, libDeps, libFlags :=
Jiyong Parkf1691d22021-03-29 20:11:58 +0900819 aaptLibs(ctx, android.SdkContext(a), nil)
Colin Cross31656952018-05-24 16:11:20 -0700820
821 _ = staticLibManifests
Colin Crossc1c37552019-01-31 11:42:41 -0800822 _ = staticRRODirs
Colin Crossfabb6082018-02-20 17:22:23 -0800823
Colin Crossa97c5d32018-03-28 14:58:31 -0700824 linkDeps = append(linkDeps, libDeps...)
825 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800826
Colin Cross66f78822018-05-02 12:58:28 -0700827 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800828
Colin Cross66f78822018-05-02 12:58:28 -0700829 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800830 linkFlags, linkDeps, nil, overlayRes, transitiveAssets, nil)
Colin Crossfabb6082018-02-20 17:22:23 -0800831
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800832 // Merge this import's assets with its dependencies' assets (if there are any).
833 if len(transitiveAssets) > 0 {
834 mergedAssets := android.PathForModuleOut(ctx, "merged-assets.zip")
835 inputZips := append(android.Paths{a.assetsPackage}, transitiveAssets...)
836 ctx.Build(pctx, android.BuildParams{
837 Rule: mergeAssetsRule,
838 Inputs: inputZips,
839 Output: mergedAssets,
840 Description: "merge assets from dependencies and self",
841 })
842 a.assetsPackage = mergedAssets
843 }
844
Colin Crossdcf71b22021-02-01 13:59:03 -0800845 ctx.SetProvider(JavaInfoProvider, JavaInfo{
846 HeaderJars: android.PathsIfNonNil(a.classpathFile),
847 ImplementationAndResourcesJars: android.PathsIfNonNil(a.classpathFile),
848 ImplementationJars: android.PathsIfNonNil(a.classpathFile),
849 })
850}
Colin Crossfabb6082018-02-20 17:22:23 -0800851
852func (a *AARImport) HeaderJars() android.Paths {
853 return android.Paths{a.classpathFile}
854}
855
Colin Cross331a1212018-08-15 20:40:52 -0700856func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
857 return android.Paths{a.classpathFile}
858}
859
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000860func (a *AARImport) DexJarBuildPath() android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800861 return nil
862}
863
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100864func (a *AARImport) DexJarInstallPath() android.Path {
865 return nil
866}
867
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100868func (a *AARImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
Jiyong Park1be96912018-05-28 18:02:19 +0900869 return nil
870}
871
Jiyong Park45bf82e2020-12-15 22:29:02 +0900872var _ android.ApexModule = (*AARImport)(nil)
873
874// Implements android.ApexModule
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900875func (a *AARImport) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
876 return a.depIsInSameApex(ctx, dep)
877}
878
Jiyong Park45bf82e2020-12-15 22:29:02 +0900879// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -0700880func (g *AARImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
881 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900882 return nil
883}
884
Colin Crossfabb6082018-02-20 17:22:23 -0800885var _ android.PrebuiltInterface = (*Import)(nil)
886
Colin Cross1b16b0e2019-02-12 14:41:32 -0800887// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
888//
889// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
890// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -0800891func AARImportFactory() android.Module {
892 module := &AARImport{}
893
894 module.AddProperties(&module.properties)
895
896 android.InitPrebuiltModule(module, &module.properties.Aars)
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900897 android.InitApexModule(module)
Colin Cross48de9a42018-10-02 13:53:33 -0700898 InitJavaModule(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800899 return module
900}