blob: 3b6b34e270a1cded5338e515b48569378a767ff9 [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 Crossa97c5d32018-03-28 14:58:31 -070020 "strings"
Colin Crossfabb6082018-02-20 17:22:23 -080021
Jaewoong Jung9befb0c2020-01-18 10:33:43 -080022 "android/soong/android"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010023 "android/soong/dexpreopt"
Jaewoong Jung9befb0c2020-01-18 10:33:43 -080024
Colin Crossfabb6082018-02-20 17:22:23 -080025 "github.com/google/blueprint"
Colin Crossa97c5d32018-03-28 14:58:31 -070026 "github.com/google/blueprint/proptools"
Colin Crossfabb6082018-02-20 17:22:23 -080027)
28
Colin Crossa97c5d32018-03-28 14:58:31 -070029type AndroidLibraryDependency interface {
30 Dependency
31 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)
Jaewoong Jungc779cd42020-10-06 18:56:10 -070043
44 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
45 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
46 })
Paul Duffinf9b1da02019-12-18 19:51:55 +000047}
48
49func RegisterAARBuildComponents(ctx android.RegistrationContext) {
50 ctx.RegisterModuleType("android_library_import", AARImportFactory)
51 ctx.RegisterModuleType("android_library", AndroidLibraryFactory)
Colin Crossa97c5d32018-03-28 14:58:31 -070052}
53
54//
55// AAR (android library)
56//
57
58type androidLibraryProperties struct {
59 BuildAAR bool `blueprint:"mutated"`
60}
61
62type aaptProperties struct {
63 // flags passed to aapt when creating the apk
64 Aaptflags []string
65
Dan Willemsen72be5902018-10-24 20:24:57 -070066 // include all resource configurations, not just the product-configured
67 // ones.
68 Aapt_include_all_resources *bool
69
Colin Crossa97c5d32018-03-28 14:58:31 -070070 // list of directories relative to the Blueprints file containing assets.
Colin Cross0ddae7f2019-02-07 15:30:01 -080071 // Defaults to ["assets"] if a directory called assets exists. Set to []
72 // to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070073 Asset_dirs []string
74
75 // list of directories relative to the Blueprints file containing
Colin Cross0ddae7f2019-02-07 15:30:01 -080076 // Android resources. Defaults to ["res"] if a directory called res exists.
77 // Set to [] to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070078 Resource_dirs []string
79
Colin Crossa592e3e2019-02-19 16:59:53 -080080 // list of zip files containing Android resources.
Colin Cross27b922f2019-03-04 22:35:41 -080081 Resource_zips []string `android:"path"`
Colin Crossa592e3e2019-02-19 16:59:53 -080082
Colin Crossa97c5d32018-03-28 14:58:31 -070083 // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml".
Colin Cross27b922f2019-03-04 22:35:41 -080084 Manifest *string `android:"path"`
changho.shinb5432b72019-08-08 18:37:17 +090085
86 // paths to additional manifest files to merge with main manifest.
87 Additional_manifests []string `android:"path"`
Sasha Smundak541056c2019-10-28 15:50:06 -070088
89 // do not include AndroidManifest from dependent libraries
90 Dont_merge_manifests *bool
Jaewoong Jungc779cd42020-10-06 18:56:10 -070091
92 // true if RRO is enforced for any of the dependent modules
93 RROEnforcedForDependent bool `blueprint:"mutated"`
Colin Crossa97c5d32018-03-28 14:58:31 -070094}
95
96type aapt struct {
Colin Cross90c25c62019-04-19 16:22:57 -070097 aaptSrcJar android.Path
98 exportPackage android.Path
99 manifestPath android.Path
100 transitiveManifestPaths android.Paths
101 proguardOptionsFile android.Path
102 rroDirs []rroDir
103 rTxt android.Path
104 extraAaptPackagesFile android.Path
105 mergedManifestFile android.Path
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700106 noticeFile android.OptionalPath
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800107 assetPackage android.OptionalPath
Colin Cross90c25c62019-04-19 16:22:57 -0700108 isLibrary bool
Sasha Smundak6ad77252019-05-01 13:16:22 -0700109 useEmbeddedNativeLibs bool
Colin Cross90c25c62019-04-19 16:22:57 -0700110 useEmbeddedDex bool
111 usesNonSdkApis bool
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700112 hasNoCode bool
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800113 LoggingParent string
Colin Cross014489c2020-06-02 20:09:13 -0700114 resourceFiles android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700115
Colin Crosse560c4a2019-03-19 16:03:11 -0700116 splitNames []string
117 splits []split
118
Colin Crossa97c5d32018-03-28 14:58:31 -0700119 aaptProperties aaptProperties
120}
121
Colin Crosse560c4a2019-03-19 16:03:11 -0700122type split struct {
123 name string
124 suffix string
125 path android.Path
126}
127
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700128// Propagate RRO enforcement flag to static lib dependencies transitively.
129func propagateRROEnforcementMutator(ctx android.TopDownMutatorContext) {
130 m := ctx.Module()
131 if d, ok := m.(AndroidLibraryDependency); ok && d.IsRROEnforced(ctx) {
132 ctx.VisitDirectDepsWithTag(staticLibTag, func(d android.Module) {
133 if a, ok := d.(AndroidLibraryDependency); ok {
134 a.SetRROEnforcedForDependent(true)
135 }
136 })
137 }
138}
139
Colin Crossa97c5d32018-03-28 14:58:31 -0700140func (a *aapt) ExportPackage() android.Path {
141 return a.exportPackage
142}
143
Anton Hansson53c88442019-03-18 15:53:16 +0000144func (a *aapt) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800145 return a.rroDirs
146}
147
Colin Cross90c25c62019-04-19 16:22:57 -0700148func (a *aapt) ExportedManifests() android.Paths {
149 return a.transitiveManifestPaths
Colin Crossc1c37552019-01-31 11:42:41 -0800150}
151
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800152func (a *aapt) ExportedAssets() android.OptionalPath {
153 return a.assetPackage
154}
155
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700156func (a *aapt) SetRROEnforcedForDependent(enforce bool) {
157 a.aaptProperties.RROEnforcedForDependent = enforce
158}
159
160func (a *aapt) IsRROEnforced(ctx android.BaseModuleContext) bool {
161 // True if RRO is enforced for this module or...
162 return ctx.Config().EnforceRROForModule(ctx.ModuleName()) ||
163 // if RRO is enforced for any of its dependents, and this module is not exempted.
164 (a.aaptProperties.RROEnforcedForDependent && !ctx.Config().EnforceRROExemptedForModule(ctx.ModuleName()))
165}
166
Colin Crossa0ba2f52019-06-22 12:59:27 -0700167func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext sdkContext,
168 manifestPath android.Path) (compileFlags, linkFlags []string, linkDeps android.Paths,
169 resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700170
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800171 hasVersionCode := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-code")
172 hasVersionName := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-name")
Colin Crossa97c5d32018-03-28 14:58:31 -0700173
Colin Crossa97c5d32018-03-28 14:58:31 -0700174 // Flags specified in Android.bp
175 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
176
177 linkFlags = append(linkFlags, "--no-static-lib-packages")
178
179 // Find implicit or explicit asset and resource dirs
180 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
181 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
Colin Cross8a497952019-03-05 22:25:09 -0800182 resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips)
Colin Crossa97c5d32018-03-28 14:58:31 -0700183
Colin Crossa97c5d32018-03-28 14:58:31 -0700184 // Glob directories into lists of paths
185 for _, dir := range resourceDirs {
186 resDirs = append(resDirs, globbedResourceDir{
187 dir: dir,
188 files: androidResourceGlob(ctx, dir),
189 })
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700190 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, a, dir)
Colin Crossa97c5d32018-03-28 14:58:31 -0700191 overlayDirs = append(overlayDirs, resOverlayDirs...)
192 rroDirs = append(rroDirs, resRRODirs...)
193 }
194
195 var assetFiles android.Paths
196 for _, dir := range assetDirs {
197 assetFiles = append(assetFiles, androidResourceGlob(ctx, dir)...)
198 }
199
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700200 assetDirStrings := assetDirs.Strings()
201 if a.noticeFile.Valid() {
202 assetDirStrings = append(assetDirStrings, filepath.Dir(a.noticeFile.Path().String()))
203 assetFiles = append(assetFiles, a.noticeFile.Path())
204 }
205
Colin Crossa97c5d32018-03-28 14:58:31 -0700206 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
207 linkDeps = append(linkDeps, manifestPath)
208
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700209 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirStrings, "-A "))
Colin Crossa97c5d32018-03-28 14:58:31 -0700210 linkDeps = append(linkDeps, assetFiles...)
211
Colin Crossa97c5d32018-03-28 14:58:31 -0700212 // SDK version flags
Jiyong Park6a927c42020-01-21 02:03:43 +0900213 minSdkVersion, err := sdkContext.minSdkVersion().effectiveVersionString(ctx)
214 if err != nil {
215 ctx.ModuleErrorf("invalid minSdkVersion: %s", err)
216 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700217
Colin Cross83bb3162018-06-25 15:48:06 -0700218 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
219 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700220
Colin Crossa97c5d32018-03-28 14:58:31 -0700221 // Version code
222 if !hasVersionCode {
Dan Albert4f378d72020-07-23 17:32:15 -0700223 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion().String())
Colin Crossa97c5d32018-03-28 14:58:31 -0700224 }
225
226 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700227 var versionName string
228 if ctx.ModuleName() == "framework-res" {
229 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
230 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700231 // if it contains the build number. Use the PlatformVersionName instead.
232 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700233 } else {
234 versionName = ctx.Config().AppsDefaultVersionName()
235 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800236 versionName = proptools.NinjaEscape(versionName)
Colin Crossa97c5d32018-03-28 14:58:31 -0700237 linkFlags = append(linkFlags, "--version-name ", versionName)
238 }
239
Colin Crossa0ba2f52019-06-22 12:59:27 -0700240 linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"})
241
242 // Always set --pseudo-localize, it will be stripped out later for release
243 // builds that don't want it.
244 compileFlags = append(compileFlags, "--pseudo-localize")
245
246 return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
Colin Crossa97c5d32018-03-28 14:58:31 -0700247}
248
Paul Duffin250e6192019-06-07 10:44:37 +0100249func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) {
Colin Cross42308aa2018-11-14 21:44:17 -0800250 if sdkDep.frameworkResModule != "" {
251 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700252 }
253}
254
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800255var extractAssetsRule = pctx.AndroidStaticRule("extractAssets",
256 blueprint.RuleParams{
257 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "assets/**/*"`,
258 CommandDeps: []string{"${config.Zip2ZipCmd}"},
259 })
260
Ulya Trafimovich18554242020-11-03 15:55:11 +0000261func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext,
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100262 classLoaderContexts dexpreopt.ClassLoaderContextMap, extraLinkFlags ...string) {
Colin Cross5446e882019-05-22 10:46:27 -0700263
Ulya Trafimovich18554242020-11-03 15:55:11 +0000264 transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, assetPackages, libDeps, libFlags :=
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100265 aaptLibs(ctx, sdkContext, classLoaderContexts)
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100266
Colin Cross31656952018-05-24 16:11:20 -0700267 // App manifest file
268 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
269 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
270
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100271 manifestPath := manifestFixer(ctx, manifestSrcPath, sdkContext, classLoaderContexts,
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800272 a.isLibrary, a.useEmbeddedNativeLibs, a.usesNonSdkApis, a.useEmbeddedDex, a.hasNoCode,
273 a.LoggingParent)
Colin Cross90c25c62019-04-19 16:22:57 -0700274
Luca Stefanifd898822019-09-10 22:13:31 +0200275 // Add additional manifest files to transitive manifests.
276 additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)
277 a.transitiveManifestPaths = append(android.Paths{manifestPath}, additionalManifests...)
278 a.transitiveManifestPaths = append(a.transitiveManifestPaths, transitiveStaticLibManifests...)
Colin Cross90c25c62019-04-19 16:22:57 -0700279
Sasha Smundak541056c2019-10-28 15:50:06 -0700280 if len(a.transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) {
Luca Stefanifd898822019-09-10 22:13:31 +0200281 a.mergedManifestFile = manifestMerger(ctx, a.transitiveManifestPaths[0], a.transitiveManifestPaths[1:], a.isLibrary)
Colin Cross90c25c62019-04-19 16:22:57 -0700282 if !a.isLibrary {
283 // Only use the merged manifest for applications. For libraries, the transitive closure of manifests
284 // will be propagated to the final application and merged there. The merged manifest for libraries is
285 // only passed to Make, which can't handle transitive dependencies.
286 manifestPath = a.mergedManifestFile
287 }
288 } else {
289 a.mergedManifestFile = manifestPath
290 }
Colin Cross31656952018-05-24 16:11:20 -0700291
Colin Crossa0ba2f52019-06-22 12:59:27 -0700292 compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700293
Colin Crossc1c37552019-01-31 11:42:41 -0800294 rroDirs = append(rroDirs, staticRRODirs...)
Colin Cross31656952018-05-24 16:11:20 -0700295 linkFlags = append(linkFlags, libFlags...)
296 linkDeps = append(linkDeps, libDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700297 linkFlags = append(linkFlags, extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700298 if a.isLibrary {
299 linkFlags = append(linkFlags, "--static-lib")
300 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700301
302 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900303 // the subdir "android" is required to be filtered by package names
304 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700305 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
306 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700307 // This file isn't used by Soong, but is generated for exporting
308 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Crossa97c5d32018-03-28 14:58:31 -0700309
Colin Cross4aaa84a2018-08-21 15:14:37 -0700310 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700311 for _, dir := range resDirs {
Colin Cross014489c2020-06-02 20:09:13 -0700312 a.resourceFiles = append(a.resourceFiles, dir.files...)
Colin Crossa0ba2f52019-06-22 12:59:27 -0700313 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700314 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700315
Colin Crossa592e3e2019-02-19 16:59:53 -0800316 for i, zip := range resZips {
317 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
Colin Crossa0ba2f52019-06-22 12:59:27 -0700318 aapt2CompileZip(ctx, flata, zip, "", compileFlags)
Colin Crossa592e3e2019-02-19 16:59:53 -0800319 compiledResDirs = append(compiledResDirs, android.Paths{flata})
320 }
321
Colin Cross4aaa84a2018-08-21 15:14:37 -0700322 var compiledRes, compiledOverlay android.Paths
323
324 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
325
Colin Crossbec85302019-02-13 13:15:46 -0800326 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700327 // If we are using static android libraries, every source file becomes an overlay.
328 // This is to emulate old AAPT behavior which simulated library support.
329 for _, compiledResDir := range compiledResDirs {
330 compiledOverlay = append(compiledOverlay, compiledResDir...)
331 }
Colin Crossbec85302019-02-13 13:15:46 -0800332 } else if a.isLibrary {
333 // Otherwise, for a static library we treat all the resources equally with no overlay.
334 for _, compiledResDir := range compiledResDirs {
335 compiledRes = append(compiledRes, compiledResDir...)
336 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700337 } else if len(compiledResDirs) > 0 {
338 // Without static libraries, the first directory is our directory, which can then be
339 // overlaid by the rest.
340 compiledRes = append(compiledRes, compiledResDirs[0]...)
341 for _, compiledResDir := range compiledResDirs[1:] {
342 compiledOverlay = append(compiledOverlay, compiledResDir...)
343 }
344 }
345
Colin Crossa97c5d32018-03-28 14:58:31 -0700346 for _, dir := range overlayDirs {
Colin Crossa0ba2f52019-06-22 12:59:27 -0700347 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700348 }
349
Colin Crosse560c4a2019-03-19 16:03:11 -0700350 var splitPackages android.WritablePaths
351 var splits []split
352
353 for _, s := range a.splitNames {
354 suffix := strings.Replace(s, ",", "_", -1)
355 path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
356 linkFlags = append(linkFlags, "--split", path.String()+":"+s)
357 splitPackages = append(splitPackages, path)
358 splits = append(splits, split{
359 name: s,
360 suffix: suffix,
361 path: path,
362 })
363 }
364
Colin Cross66f78822018-05-02 12:58:28 -0700365 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800366 linkFlags, linkDeps, compiledRes, compiledOverlay, assetPackages, splitPackages)
367
368 // Extract assets from the resource package output so that they can be used later in aapt2link
369 // for modules that depend on this one.
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800370 if android.PrefixInList(linkFlags, "-A ") || len(assetPackages) > 0 {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800371 assets := android.PathForModuleOut(ctx, "assets.zip")
372 ctx.Build(pctx, android.BuildParams{
373 Rule: extractAssetsRule,
374 Input: packageRes,
375 Output: assets,
376 Description: "extract assets from built resource file",
377 })
378 a.assetPackage = android.OptionalPathForPath(assets)
379 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700380
381 a.aaptSrcJar = srcJar
382 a.exportPackage = packageRes
383 a.manifestPath = manifestPath
384 a.proguardOptionsFile = proguardOptionsFile
385 a.rroDirs = rroDirs
Colin Cross66f78822018-05-02 12:58:28 -0700386 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700387 a.rTxt = rTxt
Colin Crosse560c4a2019-03-19 16:03:11 -0700388 a.splits = splits
Colin Crossa97c5d32018-03-28 14:58:31 -0700389}
390
391// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100392func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext, classLoaderContexts dexpreopt.ClassLoaderContextMap) (
Ulya Trafimovich18554242020-11-03 15:55:11 +0000393 transitiveStaticLibs, transitiveStaticLibManifests android.Paths, staticRRODirs []rroDir, assets, deps android.Paths, flags []string) {
Colin Cross66f78822018-05-02 12:58:28 -0700394
Colin Crossa97c5d32018-03-28 14:58:31 -0700395 var sharedLibs android.Paths
396
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100397 if classLoaderContexts == nil {
Ulya Trafimovich18554242020-11-03 15:55:11 +0000398 // Not all callers need to compute class loader context, those who don't just pass nil.
399 // Create a temporary class loader context here (it will be computed, but not used).
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100400 classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000401 }
402
Colin Cross83bb3162018-06-25 15:48:06 -0700403 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700404 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700405 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700406 }
407
408 ctx.VisitDirectDeps(func(module android.Module) {
Ulya Trafimovich18554242020-11-03 15:55:11 +0000409 depName := ctx.OtherModuleName(module)
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000410 depTag := ctx.OtherModuleDependencyTag(module)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000411
Colin Crossa97c5d32018-03-28 14:58:31 -0700412 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700413 aarDep, _ := module.(AndroidLibraryDependency)
414 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700415 exportPackage = aarDep.ExportPackage()
416 }
417
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000418 switch depTag {
Colin Cross4b964c02018-10-15 16:18:06 -0700419 case instrumentationForTag:
420 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Colin Cross5446e882019-05-22 10:46:27 -0700421 case libTag:
422 if exportPackage != nil {
423 sharedLibs = append(sharedLibs, exportPackage)
424 }
425
Paul Duffin859fe962020-05-15 10:20:31 +0100426 // If the module is (or possibly could be) a component of a java_sdk_library
427 // (including the java_sdk_library) itself then append any implicit sdk library
428 // names to the list of sdk libraries to be added to the manifest.
429 if component, ok := module.(SdkLibraryComponentDependency); ok {
Ulya Trafimovich78a71552020-11-25 14:20:52 +0000430 classLoaderContexts.MaybeAddContext(ctx, component.OptionalImplicitSdkLibrary(),
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100431 component.DexJarBuildPath(), component.DexJarInstallPath())
Colin Cross5446e882019-05-22 10:46:27 -0700432 }
433
434 case frameworkResTag:
Colin Crossa97c5d32018-03-28 14:58:31 -0700435 if exportPackage != nil {
436 sharedLibs = append(sharedLibs, exportPackage)
437 }
438 case staticLibTag:
439 if exportPackage != nil {
Colin Cross66f78822018-05-02 12:58:28 -0700440 transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
Colin Crossbec85302019-02-13 13:15:46 -0800441 transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
Colin Cross90c25c62019-04-19 16:22:57 -0700442 transitiveStaticLibManifests = append(transitiveStaticLibManifests, aarDep.ExportedManifests()...)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800443 if aarDep.ExportedAssets().Valid() {
444 assets = append(assets, aarDep.ExportedAssets().Path())
445 }
Anton Hansson53c88442019-03-18 15:53:16 +0000446
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700447 if !ctx.Config().EnforceRROExemptedForModule(ctx.ModuleName()) {
448 outer:
449 for _, d := range aarDep.ExportedRRODirs() {
450 for _, e := range staticRRODirs {
451 if d.path == e.path {
452 continue outer
453 }
Anton Hansson53c88442019-03-18 15:53:16 +0000454 }
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700455 staticRRODirs = append(staticRRODirs, d)
Anton Hansson53c88442019-03-18 15:53:16 +0000456 }
Anton Hansson53c88442019-03-18 15:53:16 +0000457 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700458 }
459 }
Ulya Trafimovich18554242020-11-03 15:55:11 +0000460
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000461 // Merge dep's CLC after processing the dep itself (which may add its own <uses-library>).
462 maybeAddCLCFromDep(module, depTag, depName, classLoaderContexts)
Colin Crossa97c5d32018-03-28 14:58:31 -0700463 })
464
465 deps = append(deps, sharedLibs...)
Colin Cross66f78822018-05-02 12:58:28 -0700466 deps = append(deps, transitiveStaticLibs...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700467
Colin Cross66f78822018-05-02 12:58:28 -0700468 if len(transitiveStaticLibs) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700469 flags = append(flags, "--auto-add-overlay")
470 }
471
472 for _, sharedLib := range sharedLibs {
473 flags = append(flags, "-I "+sharedLib.String())
474 }
475
Colin Cross66f78822018-05-02 12:58:28 -0700476 transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
Colin Cross90c25c62019-04-19 16:22:57 -0700477 transitiveStaticLibManifests = android.FirstUniquePaths(transitiveStaticLibManifests)
Colin Cross66f78822018-05-02 12:58:28 -0700478
Ulya Trafimovich18554242020-11-03 15:55:11 +0000479 return transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, assets, deps, flags
Colin Crossa97c5d32018-03-28 14:58:31 -0700480}
481
482type AndroidLibrary struct {
483 Library
484 aapt
485
486 androidLibraryProperties androidLibraryProperties
487
488 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700489
490 exportedProguardFlagFiles android.Paths
Colin Cross66f78822018-05-02 12:58:28 -0700491 exportedStaticPackages android.Paths
Colin Cross89c31582018-04-30 15:55:11 -0700492}
493
494func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
495 return a.exportedProguardFlagFiles
Colin Crossa97c5d32018-03-28 14:58:31 -0700496}
497
Colin Cross66f78822018-05-02 12:58:28 -0700498func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
499 return a.exportedStaticPackages
500}
501
Colin Crossa97c5d32018-03-28 14:58:31 -0700502var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
503
504func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
505 a.Module.deps(ctx)
Paul Duffin250e6192019-06-07 10:44:37 +0100506 sdkDep := decodeSdkDep(ctx, sdkContext(a))
507 if sdkDep.hasFrameworkLibs() {
508 a.aapt.deps(ctx, sdkDep)
Colin Crossa97c5d32018-03-28 14:58:31 -0700509 }
510}
511
512func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800513 a.aapt.isLibrary = true
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100514 a.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
515 a.aapt.buildActions(ctx, sdkContext(a), a.classLoaderContexts)
Colin Crossa97c5d32018-03-28 14:58:31 -0700516
Colin Cross56a83212020-09-15 18:30:11 -0700517 a.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
518
Colin Crossa97c5d32018-03-28 14:58:31 -0700519 ctx.CheckbuildFile(a.proguardOptionsFile)
520 ctx.CheckbuildFile(a.exportPackage)
521 ctx.CheckbuildFile(a.aaptSrcJar)
522
523 // apps manifests are handled by aapt, don't let Module see them
524 a.properties.Manifest = nil
525
Colin Cross014489c2020-06-02 20:09:13 -0700526 a.linter.mergedManifest = a.aapt.mergedManifestFile
527 a.linter.manifest = a.aapt.manifestPath
528 a.linter.resources = a.aapt.resourceFiles
529
Colin Crossa97c5d32018-03-28 14:58:31 -0700530 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
531 a.proguardOptionsFile)
532
533 a.Module.compile(ctx, a.aaptSrcJar)
534
Colin Crossf57c5782019-01-25 13:20:38 -0800535 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700536 var res android.Paths
537 if a.androidLibraryProperties.BuildAAR {
538 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
539 ctx.CheckbuildFile(a.aarFile)
540 }
Colin Cross89c31582018-04-30 15:55:11 -0700541
Cole Faust9a631312020-10-22 21:05:24 +0000542 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles,
543 android.PathsForModuleSrc(ctx, a.dexProperties.Optimize.Proguard_flags_files)...)
Colin Cross89c31582018-04-30 15:55:11 -0700544 ctx.VisitDirectDeps(func(m android.Module) {
545 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
546 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
Colin Cross66f78822018-05-02 12:58:28 -0700547 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
548 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
Colin Cross89c31582018-04-30 15:55:11 -0700549 }
550 })
551
552 a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
Colin Cross66f78822018-05-02 12:58:28 -0700553 a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700554}
555
Colin Cross1b16b0e2019-02-12 14:41:32 -0800556// android_library builds and links sources into a `.jar` file for the device along with Android resources.
557//
558// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
559// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
560// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
561// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700562func AndroidLibraryFactory() android.Module {
563 module := &AndroidLibrary{}
564
Colin Crossce6734e2020-06-15 16:09:53 -0700565 module.Module.addHostAndDeviceProperties()
Colin Crossa97c5d32018-03-28 14:58:31 -0700566 module.AddProperties(
Colin Crossa97c5d32018-03-28 14:58:31 -0700567 &module.aaptProperties,
568 &module.androidLibraryProperties)
569
570 module.androidLibraryProperties.BuildAAR = true
Colin Cross014489c2020-06-02 20:09:13 -0700571 module.Module.linter.library = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700572
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900573 android.InitApexModule(module)
Colin Cross48de9a42018-10-02 13:53:33 -0700574 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700575 return module
576}
577
Colin Crossfabb6082018-02-20 17:22:23 -0800578//
579// AAR (android library) prebuilts
580//
Colin Crossfabb6082018-02-20 17:22:23 -0800581
582type AARImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -0800583 Aars []string `android:"path"`
Colin Crossfabb6082018-02-20 17:22:23 -0800584
Colin Cross479884c2018-07-10 13:39:30 -0700585 Sdk_version *string
586 Min_sdk_version *string
Colin Crossa97c5d32018-03-28 14:58:31 -0700587
588 Static_libs []string
589 Libs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -0700590
591 // if set to true, run Jetifier against .aar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -0700592 Jetifier *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800593}
594
595type AARImport struct {
596 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700597 android.DefaultableModuleBase
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900598 android.ApexModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800599 prebuilt android.Prebuilt
600
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900601 // Functionality common to Module and Import.
602 embeddableInModuleAndImport
603
Colin Crossfabb6082018-02-20 17:22:23 -0800604 properties AARImportProperties
605
Colin Cross66f78822018-05-02 12:58:28 -0700606 classpathFile android.WritablePath
607 proguardFlags android.WritablePath
608 exportPackage android.WritablePath
609 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700610 manifest android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700611
612 exportedStaticPackages android.Paths
Colin Cross56a83212020-09-15 18:30:11 -0700613
614 hideApexVariantFromMake bool
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000615
616 aarPath android.Path
617}
618
619var _ android.OutputFileProducer = (*AARImport)(nil)
620
621// For OutputFileProducer interface
622func (a *AARImport) OutputFiles(tag string) (android.Paths, error) {
623 switch tag {
624 case ".aar":
625 return []android.Path{a.aarPath}, nil
626 case "":
627 return []android.Path{a.classpathFile}, nil
628 default:
629 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
630 }
Colin Crossfabb6082018-02-20 17:22:23 -0800631}
632
Jiyong Park6a927c42020-01-21 02:03:43 +0900633func (a *AARImport) sdkVersion() sdkSpec {
634 return sdkSpecFrom(String(a.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700635}
636
Paul Duffine25c6442019-10-11 13:50:28 +0100637func (a *AARImport) systemModules() string {
638 return ""
639}
640
Jiyong Park6a927c42020-01-21 02:03:43 +0900641func (a *AARImport) minSdkVersion() sdkSpec {
Colin Cross479884c2018-07-10 13:39:30 -0700642 if a.properties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900643 return sdkSpecFrom(*a.properties.Min_sdk_version)
Colin Cross479884c2018-07-10 13:39:30 -0700644 }
Colin Cross83bb3162018-06-25 15:48:06 -0700645 return a.sdkVersion()
646}
647
Jiyong Park6a927c42020-01-21 02:03:43 +0900648func (a *AARImport) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700649 return a.sdkVersion()
650}
651
Colin Cross1e743852019-10-28 11:37:20 -0700652func (a *AARImport) javaVersion() string {
653 return ""
654}
655
Colin Crossa97c5d32018-03-28 14:58:31 -0700656var _ AndroidLibraryDependency = (*AARImport)(nil)
657
658func (a *AARImport) ExportPackage() android.Path {
659 return a.exportPackage
660}
661
Colin Cross89c31582018-04-30 15:55:11 -0700662func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
663 return android.Paths{a.proguardFlags}
664}
665
Anton Hansson53c88442019-03-18 15:53:16 +0000666func (a *AARImport) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800667 return nil
668}
669
Colin Cross66f78822018-05-02 12:58:28 -0700670func (a *AARImport) ExportedStaticPackages() android.Paths {
671 return a.exportedStaticPackages
672}
673
Colin Cross90c25c62019-04-19 16:22:57 -0700674func (a *AARImport) ExportedManifests() android.Paths {
675 return android.Paths{a.manifest}
Colin Cross31656952018-05-24 16:11:20 -0700676}
677
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800678// TODO(jungjw): Decide whether we want to implement this.
679func (a *AARImport) ExportedAssets() android.OptionalPath {
680 return android.OptionalPath{}
681}
682
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700683// RRO enforcement is not available on aar_import since its RRO dirs are not
684// exported.
685func (a *AARImport) SetRROEnforcedForDependent(enforce bool) {
686}
687
688// RRO enforcement is not available on aar_import since its RRO dirs are not
689// exported.
690func (a *AARImport) IsRROEnforced(ctx android.BaseModuleContext) bool {
691 return false
692}
693
Colin Crossfabb6082018-02-20 17:22:23 -0800694func (a *AARImport) Prebuilt() *android.Prebuilt {
695 return &a.prebuilt
696}
697
698func (a *AARImport) Name() string {
699 return a.prebuilt.Name(a.ModuleBase.Name())
700}
701
Jiyong Park618922e2020-01-08 13:35:43 +0900702func (a *AARImport) JacocoReportClassesFile() android.Path {
703 return nil
704}
705
Colin Crossfabb6082018-02-20 17:22:23 -0800706func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Jeongik Cha816a23a2020-07-08 01:09:23 +0900707 if !ctx.Config().AlwaysUsePrebuiltSdks() {
Colin Cross83bb3162018-06-25 15:48:06 -0700708 sdkDep := decodeSdkDep(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700709 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700710 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800711 }
712 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700713
Colin Cross42d48b72018-08-29 14:10:52 -0700714 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
715 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800716}
717
718// 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 -0700719// 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 -0800720var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
721 blueprint.RuleParams{
Dan Willemsen304cfec2019-05-28 14:49:06 -0700722 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
Colin Cross205e9112020-08-06 13:20:17 -0700723 `unzip -qoDD -d $outDir $in && rm -rf $outDir/res && touch $out && ` +
724 `${config.MergeZipsCmd} $combinedClassesJar $$(ls $outDir/classes.jar 2> /dev/null) $$(ls $outDir/libs/*.jar 2> /dev/null)`,
725 CommandDeps: []string{"${config.MergeZipsCmd}"},
Colin Crossfabb6082018-02-20 17:22:23 -0800726 },
Colin Cross205e9112020-08-06 13:20:17 -0700727 "outDir", "combinedClassesJar")
Colin Crossfabb6082018-02-20 17:22:23 -0800728
729func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
730 if len(a.properties.Aars) != 1 {
731 ctx.PropertyErrorf("aars", "exactly one aar is required")
732 return
733 }
734
Colin Cross56a83212020-09-15 18:30:11 -0700735 a.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
736
Nan Zhang4c819fb2018-08-27 18:31:46 -0700737 aarName := ctx.ModuleName() + ".aar"
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000738 a.aarPath = android.PathForModuleSrc(ctx, a.properties.Aars[0])
739
Colin Cross1001a792019-03-21 22:21:39 -0700740 if Bool(a.properties.Jetifier) {
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000741 inputFile := a.aarPath
742 a.aarPath = android.PathForModuleOut(ctx, "jetifier", aarName)
743 TransformJetifier(ctx, a.aarPath.(android.WritablePath), inputFile)
Nan Zhang4c819fb2018-08-27 18:31:46 -0700744 }
Colin Crossfabb6082018-02-20 17:22:23 -0800745
746 extractedAARDir := android.PathForModuleOut(ctx, "aar")
Colin Cross205e9112020-08-06 13:20:17 -0700747 a.classpathFile = extractedAARDir.Join(ctx, "classes-combined.jar")
Colin Crossfabb6082018-02-20 17:22:23 -0800748 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700749 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossfabb6082018-02-20 17:22:23 -0800750
751 ctx.Build(pctx, android.BuildParams{
752 Rule: unzipAAR,
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000753 Input: a.aarPath,
Colin Cross10f7c4a2018-05-23 10:59:28 -0700754 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest},
Colin Crossfabb6082018-02-20 17:22:23 -0800755 Description: "unzip AAR",
756 Args: map[string]string{
Colin Cross205e9112020-08-06 13:20:17 -0700757 "outDir": extractedAARDir.String(),
758 "combinedClassesJar": a.classpathFile.String(),
Colin Crossfabb6082018-02-20 17:22:23 -0800759 },
760 })
761
Colin Crossa0ba2f52019-06-22 12:59:27 -0700762 // Always set --pseudo-localize, it will be stripped out later for release
763 // builds that don't want it.
764 compileFlags := []string{"--pseudo-localize"}
Colin Crossfabb6082018-02-20 17:22:23 -0800765 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
Colin Crossfabb6082018-02-20 17:22:23 -0800766 flata := compiledResDir.Join(ctx, "gen_res.flata")
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000767 aapt2CompileZip(ctx, flata, a.aarPath, "res", compileFlags)
Colin Crossfabb6082018-02-20 17:22:23 -0800768
769 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900770 // the subdir "android" is required to be filtered by package names
771 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossfabb6082018-02-20 17:22:23 -0800772 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700773 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700774 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800775
776 var linkDeps android.Paths
777
778 linkFlags := []string{
779 "--static-lib",
780 "--no-static-lib-packages",
781 "--auto-add-overlay",
782 }
783
Colin Cross10f7c4a2018-05-23 10:59:28 -0700784 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
785 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800786
Ulya Trafimovich18554242020-11-03 15:55:11 +0000787 transitiveStaticLibs, staticLibManifests, staticRRODirs, transitiveAssets, libDeps, libFlags :=
788 aaptLibs(ctx, sdkContext(a), nil)
Colin Cross31656952018-05-24 16:11:20 -0700789
790 _ = staticLibManifests
Colin Crossc1c37552019-01-31 11:42:41 -0800791 _ = staticRRODirs
Colin Crossfabb6082018-02-20 17:22:23 -0800792
Colin Crossa97c5d32018-03-28 14:58:31 -0700793 linkDeps = append(linkDeps, libDeps...)
794 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800795
Colin Cross66f78822018-05-02 12:58:28 -0700796 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800797
Colin Cross66f78822018-05-02 12:58:28 -0700798 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800799 linkFlags, linkDeps, nil, overlayRes, transitiveAssets, nil)
Colin Crossfabb6082018-02-20 17:22:23 -0800800}
801
802var _ Dependency = (*AARImport)(nil)
803
804func (a *AARImport) HeaderJars() android.Paths {
805 return android.Paths{a.classpathFile}
806}
807
808func (a *AARImport) ImplementationJars() android.Paths {
809 return android.Paths{a.classpathFile}
810}
811
Colin Cross331a1212018-08-15 20:40:52 -0700812func (a *AARImport) ResourceJars() android.Paths {
813 return nil
814}
815
816func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
817 return android.Paths{a.classpathFile}
818}
819
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000820func (a *AARImport) DexJarBuildPath() android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800821 return nil
822}
823
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100824func (a *AARImport) DexJarInstallPath() android.Path {
825 return nil
826}
827
Colin Crossfabb6082018-02-20 17:22:23 -0800828func (a *AARImport) AidlIncludeDirs() android.Paths {
829 return nil
830}
831
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100832func (a *AARImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
Jiyong Park1be96912018-05-28 18:02:19 +0900833 return nil
834}
835
Colin Crossc9fe10f2020-11-19 18:06:03 -0800836func (d *AARImport) ExportedPlugins() (android.Paths, []string, bool) {
837 return nil, nil, false
Artur Satayev9cf46692019-11-26 18:08:34 +0000838}
839
Colin Cross0c4ce212019-05-03 15:28:19 -0700840func (a *AARImport) SrcJarArgs() ([]string, android.Paths) {
841 return nil, nil
842}
843
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900844func (a *AARImport) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
845 return a.depIsInSameApex(ctx, dep)
846}
847
Dan Albertc8060532020-07-22 22:32:17 -0700848func (g *AARImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
849 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900850 return nil
851}
852
Colin Crossfabb6082018-02-20 17:22:23 -0800853var _ android.PrebuiltInterface = (*Import)(nil)
854
Colin Cross1b16b0e2019-02-12 14:41:32 -0800855// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
856//
857// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
858// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -0800859func AARImportFactory() android.Module {
860 module := &AARImport{}
861
862 module.AddProperties(&module.properties)
863
864 android.InitPrebuiltModule(module, &module.properties.Aars)
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900865 android.InitApexModule(module)
Colin Cross48de9a42018-10-02 13:53:33 -0700866 InitJavaModule(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800867 return module
868}