blob: ac7ae25187cc0506f5b682b42111652c8e0b36d7 [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)
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
Colin Crossc20dc852020-11-10 12:27:45 -0800195 var assetDeps android.Paths
196 for i, dir := range assetDirs {
197 // Add a dependency on every file in the asset directory. This ensures the aapt2
198 // rule will be rerun if one of the files in the asset directory is modified.
199 assetDeps = append(assetDeps, androidResourceGlob(ctx, dir)...)
200
201 // Add a dependency on a file that contains a list of all the files in the asset directory.
202 // This ensures the aapt2 rule will be run if a file is removed from the asset directory,
203 // or a file is added whose timestamp is older than the output of aapt2.
204 assetFileListFile := android.PathForModuleOut(ctx, "asset_dir_globs", strconv.Itoa(i)+".glob")
205 androidResourceGlobList(ctx, dir, assetFileListFile)
206 assetDeps = append(assetDeps, assetFileListFile)
Colin Crossa97c5d32018-03-28 14:58:31 -0700207 }
208
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700209 assetDirStrings := assetDirs.Strings()
210 if a.noticeFile.Valid() {
211 assetDirStrings = append(assetDirStrings, filepath.Dir(a.noticeFile.Path().String()))
Colin Crossc20dc852020-11-10 12:27:45 -0800212 assetDeps = append(assetDeps, a.noticeFile.Path())
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700213 }
214
Colin Crossa97c5d32018-03-28 14:58:31 -0700215 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
216 linkDeps = append(linkDeps, manifestPath)
217
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700218 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirStrings, "-A "))
Colin Crossc20dc852020-11-10 12:27:45 -0800219 linkDeps = append(linkDeps, assetDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700220
Colin Crossa97c5d32018-03-28 14:58:31 -0700221 // SDK version flags
Jiyong Park6a927c42020-01-21 02:03:43 +0900222 minSdkVersion, err := sdkContext.minSdkVersion().effectiveVersionString(ctx)
223 if err != nil {
224 ctx.ModuleErrorf("invalid minSdkVersion: %s", err)
225 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700226
Colin Cross83bb3162018-06-25 15:48:06 -0700227 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
228 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700229
Colin Crossa97c5d32018-03-28 14:58:31 -0700230 // Version code
231 if !hasVersionCode {
Dan Albert4f378d72020-07-23 17:32:15 -0700232 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion().String())
Colin Crossa97c5d32018-03-28 14:58:31 -0700233 }
234
235 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700236 var versionName string
237 if ctx.ModuleName() == "framework-res" {
238 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
239 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700240 // if it contains the build number. Use the PlatformVersionName instead.
241 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700242 } else {
243 versionName = ctx.Config().AppsDefaultVersionName()
244 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800245 versionName = proptools.NinjaEscape(versionName)
Colin Crossa97c5d32018-03-28 14:58:31 -0700246 linkFlags = append(linkFlags, "--version-name ", versionName)
247 }
248
Colin Crossa0ba2f52019-06-22 12:59:27 -0700249 linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"})
250
251 // Always set --pseudo-localize, it will be stripped out later for release
252 // builds that don't want it.
253 compileFlags = append(compileFlags, "--pseudo-localize")
254
255 return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
Colin Crossa97c5d32018-03-28 14:58:31 -0700256}
257
Paul Duffin250e6192019-06-07 10:44:37 +0100258func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) {
Colin Cross42308aa2018-11-14 21:44:17 -0800259 if sdkDep.frameworkResModule != "" {
260 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700261 }
262}
263
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800264var extractAssetsRule = pctx.AndroidStaticRule("extractAssets",
265 blueprint.RuleParams{
266 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "assets/**/*"`,
267 CommandDeps: []string{"${config.Zip2ZipCmd}"},
268 })
269
Ulya Trafimovich18554242020-11-03 15:55:11 +0000270func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext,
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100271 classLoaderContexts dexpreopt.ClassLoaderContextMap, 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
Colin Cross31656952018-05-24 16:11:20 -0700276 // App manifest file
277 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
278 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
279
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100280 manifestPath := manifestFixer(ctx, manifestSrcPath, sdkContext, classLoaderContexts,
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800281 a.isLibrary, a.useEmbeddedNativeLibs, a.usesNonSdkApis, a.useEmbeddedDex, a.hasNoCode,
282 a.LoggingParent)
Colin Cross90c25c62019-04-19 16:22:57 -0700283
Luca Stefanifd898822019-09-10 22:13:31 +0200284 // Add additional manifest files to transitive manifests.
285 additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)
286 a.transitiveManifestPaths = append(android.Paths{manifestPath}, additionalManifests...)
287 a.transitiveManifestPaths = append(a.transitiveManifestPaths, transitiveStaticLibManifests...)
Colin Cross90c25c62019-04-19 16:22:57 -0700288
Sasha Smundak541056c2019-10-28 15:50:06 -0700289 if len(a.transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) {
Luca Stefanifd898822019-09-10 22:13:31 +0200290 a.mergedManifestFile = manifestMerger(ctx, a.transitiveManifestPaths[0], a.transitiveManifestPaths[1:], a.isLibrary)
Colin Cross90c25c62019-04-19 16:22:57 -0700291 if !a.isLibrary {
292 // Only use the merged manifest for applications. For libraries, the transitive closure of manifests
293 // will be propagated to the final application and merged there. The merged manifest for libraries is
294 // only passed to Make, which can't handle transitive dependencies.
295 manifestPath = a.mergedManifestFile
296 }
297 } else {
298 a.mergedManifestFile = manifestPath
299 }
Colin Cross31656952018-05-24 16:11:20 -0700300
Colin Crossa0ba2f52019-06-22 12:59:27 -0700301 compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700302
Colin Crossc1c37552019-01-31 11:42:41 -0800303 rroDirs = append(rroDirs, staticRRODirs...)
Colin Cross31656952018-05-24 16:11:20 -0700304 linkFlags = append(linkFlags, libFlags...)
305 linkDeps = append(linkDeps, libDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700306 linkFlags = append(linkFlags, extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700307 if a.isLibrary {
308 linkFlags = append(linkFlags, "--static-lib")
309 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700310
311 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900312 // the subdir "android" is required to be filtered by package names
313 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700314 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
315 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700316 // This file isn't used by Soong, but is generated for exporting
317 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Crossa97c5d32018-03-28 14:58:31 -0700318
Colin Cross4aaa84a2018-08-21 15:14:37 -0700319 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700320 for _, dir := range resDirs {
Colin Cross014489c2020-06-02 20:09:13 -0700321 a.resourceFiles = append(a.resourceFiles, dir.files...)
Colin Crossa0ba2f52019-06-22 12:59:27 -0700322 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700323 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700324
Colin Crossa592e3e2019-02-19 16:59:53 -0800325 for i, zip := range resZips {
326 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
Colin Crossa0ba2f52019-06-22 12:59:27 -0700327 aapt2CompileZip(ctx, flata, zip, "", compileFlags)
Colin Crossa592e3e2019-02-19 16:59:53 -0800328 compiledResDirs = append(compiledResDirs, android.Paths{flata})
329 }
330
Colin Cross4aaa84a2018-08-21 15:14:37 -0700331 var compiledRes, compiledOverlay android.Paths
332
333 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
334
Colin Crossbec85302019-02-13 13:15:46 -0800335 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700336 // If we are using static android libraries, every source file becomes an overlay.
337 // This is to emulate old AAPT behavior which simulated library support.
338 for _, compiledResDir := range compiledResDirs {
339 compiledOverlay = append(compiledOverlay, compiledResDir...)
340 }
Colin Crossbec85302019-02-13 13:15:46 -0800341 } else if a.isLibrary {
342 // Otherwise, for a static library we treat all the resources equally with no overlay.
343 for _, compiledResDir := range compiledResDirs {
344 compiledRes = append(compiledRes, compiledResDir...)
345 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700346 } else if len(compiledResDirs) > 0 {
347 // Without static libraries, the first directory is our directory, which can then be
348 // overlaid by the rest.
349 compiledRes = append(compiledRes, compiledResDirs[0]...)
350 for _, compiledResDir := range compiledResDirs[1:] {
351 compiledOverlay = append(compiledOverlay, compiledResDir...)
352 }
353 }
354
Colin Crossa97c5d32018-03-28 14:58:31 -0700355 for _, dir := range overlayDirs {
Colin Crossa0ba2f52019-06-22 12:59:27 -0700356 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700357 }
358
Colin Crosse560c4a2019-03-19 16:03:11 -0700359 var splitPackages android.WritablePaths
360 var splits []split
361
362 for _, s := range a.splitNames {
363 suffix := strings.Replace(s, ",", "_", -1)
364 path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
365 linkFlags = append(linkFlags, "--split", path.String()+":"+s)
366 splitPackages = append(splitPackages, path)
367 splits = append(splits, split{
368 name: s,
369 suffix: suffix,
370 path: path,
371 })
372 }
373
Colin Cross66f78822018-05-02 12:58:28 -0700374 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800375 linkFlags, linkDeps, compiledRes, compiledOverlay, assetPackages, splitPackages)
376
377 // Extract assets from the resource package output so that they can be used later in aapt2link
378 // for modules that depend on this one.
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800379 if android.PrefixInList(linkFlags, "-A ") || len(assetPackages) > 0 {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800380 assets := android.PathForModuleOut(ctx, "assets.zip")
381 ctx.Build(pctx, android.BuildParams{
382 Rule: extractAssetsRule,
383 Input: packageRes,
384 Output: assets,
385 Description: "extract assets from built resource file",
386 })
387 a.assetPackage = android.OptionalPathForPath(assets)
388 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700389
390 a.aaptSrcJar = srcJar
391 a.exportPackage = packageRes
392 a.manifestPath = manifestPath
393 a.proguardOptionsFile = proguardOptionsFile
394 a.rroDirs = rroDirs
Colin Cross66f78822018-05-02 12:58:28 -0700395 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700396 a.rTxt = rTxt
Colin Crosse560c4a2019-03-19 16:03:11 -0700397 a.splits = splits
Colin Crossa97c5d32018-03-28 14:58:31 -0700398}
399
400// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100401func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext, classLoaderContexts dexpreopt.ClassLoaderContextMap) (
Ulya Trafimovich18554242020-11-03 15:55:11 +0000402 transitiveStaticLibs, transitiveStaticLibManifests android.Paths, staticRRODirs []rroDir, assets, deps android.Paths, flags []string) {
Colin Cross66f78822018-05-02 12:58:28 -0700403
Colin Crossa97c5d32018-03-28 14:58:31 -0700404 var sharedLibs android.Paths
405
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100406 if classLoaderContexts == nil {
Ulya Trafimovich18554242020-11-03 15:55:11 +0000407 // Not all callers need to compute class loader context, those who don't just pass nil.
408 // Create a temporary class loader context here (it will be computed, but not used).
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100409 classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000410 }
411
Colin Cross83bb3162018-06-25 15:48:06 -0700412 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700413 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700414 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700415 }
416
417 ctx.VisitDirectDeps(func(module android.Module) {
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000418 depTag := ctx.OtherModuleDependencyTag(module)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000419
Colin Crossa97c5d32018-03-28 14:58:31 -0700420 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700421 aarDep, _ := module.(AndroidLibraryDependency)
422 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700423 exportPackage = aarDep.ExportPackage()
424 }
425
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000426 switch depTag {
Colin Cross4b964c02018-10-15 16:18:06 -0700427 case instrumentationForTag:
428 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Colin Cross5446e882019-05-22 10:46:27 -0700429 case libTag:
430 if exportPackage != nil {
431 sharedLibs = append(sharedLibs, exportPackage)
432 }
Colin Cross5446e882019-05-22 10:46:27 -0700433 case frameworkResTag:
Colin Crossa97c5d32018-03-28 14:58:31 -0700434 if exportPackage != nil {
435 sharedLibs = append(sharedLibs, exportPackage)
436 }
437 case staticLibTag:
438 if exportPackage != nil {
Colin Cross66f78822018-05-02 12:58:28 -0700439 transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
Colin Crossbec85302019-02-13 13:15:46 -0800440 transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
Colin Cross90c25c62019-04-19 16:22:57 -0700441 transitiveStaticLibManifests = append(transitiveStaticLibManifests, aarDep.ExportedManifests()...)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800442 if aarDep.ExportedAssets().Valid() {
443 assets = append(assets, aarDep.ExportedAssets().Path())
444 }
Anton Hansson53c88442019-03-18 15:53:16 +0000445
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700446 if !ctx.Config().EnforceRROExemptedForModule(ctx.ModuleName()) {
447 outer:
448 for _, d := range aarDep.ExportedRRODirs() {
449 for _, e := range staticRRODirs {
450 if d.path == e.path {
451 continue outer
452 }
Anton Hansson53c88442019-03-18 15:53:16 +0000453 }
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700454 staticRRODirs = append(staticRRODirs, d)
Anton Hansson53c88442019-03-18 15:53:16 +0000455 }
Anton Hansson53c88442019-03-18 15:53:16 +0000456 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700457 }
458 }
Ulya Trafimovich18554242020-11-03 15:55:11 +0000459
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +0000460 addCLCFromDep(ctx, module, classLoaderContexts)
Colin Crossa97c5d32018-03-28 14:58:31 -0700461 })
462
463 deps = append(deps, sharedLibs...)
Colin Cross66f78822018-05-02 12:58:28 -0700464 deps = append(deps, transitiveStaticLibs...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700465
Colin Cross66f78822018-05-02 12:58:28 -0700466 if len(transitiveStaticLibs) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700467 flags = append(flags, "--auto-add-overlay")
468 }
469
470 for _, sharedLib := range sharedLibs {
471 flags = append(flags, "-I "+sharedLib.String())
472 }
473
Colin Cross66f78822018-05-02 12:58:28 -0700474 transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
Colin Cross90c25c62019-04-19 16:22:57 -0700475 transitiveStaticLibManifests = android.FirstUniquePaths(transitiveStaticLibManifests)
Colin Cross66f78822018-05-02 12:58:28 -0700476
Ulya Trafimovich18554242020-11-03 15:55:11 +0000477 return transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, assets, deps, flags
Colin Crossa97c5d32018-03-28 14:58:31 -0700478}
479
480type AndroidLibrary struct {
481 Library
482 aapt
483
484 androidLibraryProperties androidLibraryProperties
485
486 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700487
488 exportedProguardFlagFiles android.Paths
Colin Cross66f78822018-05-02 12:58:28 -0700489 exportedStaticPackages android.Paths
Colin Cross89c31582018-04-30 15:55:11 -0700490}
491
492func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
493 return a.exportedProguardFlagFiles
Colin Crossa97c5d32018-03-28 14:58:31 -0700494}
495
Colin Cross66f78822018-05-02 12:58:28 -0700496func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
497 return a.exportedStaticPackages
498}
499
Colin Crossa97c5d32018-03-28 14:58:31 -0700500var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
501
502func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
503 a.Module.deps(ctx)
Paul Duffin250e6192019-06-07 10:44:37 +0100504 sdkDep := decodeSdkDep(ctx, sdkContext(a))
505 if sdkDep.hasFrameworkLibs() {
506 a.aapt.deps(ctx, sdkDep)
Colin Crossa97c5d32018-03-28 14:58:31 -0700507 }
508}
509
510func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800511 a.aapt.isLibrary = true
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100512 a.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
513 a.aapt.buildActions(ctx, sdkContext(a), a.classLoaderContexts)
Colin Crossa97c5d32018-03-28 14:58:31 -0700514
Colin Cross56a83212020-09-15 18:30:11 -0700515 a.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
516
Colin Crossa97c5d32018-03-28 14:58:31 -0700517 ctx.CheckbuildFile(a.proguardOptionsFile)
518 ctx.CheckbuildFile(a.exportPackage)
519 ctx.CheckbuildFile(a.aaptSrcJar)
520
521 // apps manifests are handled by aapt, don't let Module see them
522 a.properties.Manifest = nil
523
Colin Cross014489c2020-06-02 20:09:13 -0700524 a.linter.mergedManifest = a.aapt.mergedManifestFile
525 a.linter.manifest = a.aapt.manifestPath
526 a.linter.resources = a.aapt.resourceFiles
527
Colin Crossa97c5d32018-03-28 14:58:31 -0700528 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
529 a.proguardOptionsFile)
530
531 a.Module.compile(ctx, a.aaptSrcJar)
532
Colin Crossf57c5782019-01-25 13:20:38 -0800533 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700534 var res android.Paths
535 if a.androidLibraryProperties.BuildAAR {
536 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
537 ctx.CheckbuildFile(a.aarFile)
538 }
Colin Cross89c31582018-04-30 15:55:11 -0700539
Cole Faust9a631312020-10-22 21:05:24 +0000540 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles,
541 android.PathsForModuleSrc(ctx, a.dexProperties.Optimize.Proguard_flags_files)...)
Colin Cross89c31582018-04-30 15:55:11 -0700542 ctx.VisitDirectDeps(func(m android.Module) {
543 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
544 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
Colin Cross66f78822018-05-02 12:58:28 -0700545 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
546 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
Colin Cross89c31582018-04-30 15:55:11 -0700547 }
548 })
549
550 a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
Colin Cross66f78822018-05-02 12:58:28 -0700551 a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700552}
553
Colin Cross1b16b0e2019-02-12 14:41:32 -0800554// android_library builds and links sources into a `.jar` file for the device along with Android resources.
555//
556// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
557// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
558// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
559// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700560func AndroidLibraryFactory() android.Module {
561 module := &AndroidLibrary{}
562
Colin Crossce6734e2020-06-15 16:09:53 -0700563 module.Module.addHostAndDeviceProperties()
Colin Crossa97c5d32018-03-28 14:58:31 -0700564 module.AddProperties(
Colin Crossa97c5d32018-03-28 14:58:31 -0700565 &module.aaptProperties,
566 &module.androidLibraryProperties)
567
568 module.androidLibraryProperties.BuildAAR = true
Colin Cross014489c2020-06-02 20:09:13 -0700569 module.Module.linter.library = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700570
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900571 android.InitApexModule(module)
Colin Cross48de9a42018-10-02 13:53:33 -0700572 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700573 return module
574}
575
Colin Crossfabb6082018-02-20 17:22:23 -0800576//
577// AAR (android library) prebuilts
578//
Colin Crossfabb6082018-02-20 17:22:23 -0800579
580type AARImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -0800581 Aars []string `android:"path"`
Colin Crossfabb6082018-02-20 17:22:23 -0800582
Colin Cross479884c2018-07-10 13:39:30 -0700583 Sdk_version *string
584 Min_sdk_version *string
Colin Crossa97c5d32018-03-28 14:58:31 -0700585
586 Static_libs []string
587 Libs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -0700588
589 // if set to true, run Jetifier against .aar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -0700590 Jetifier *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800591}
592
593type AARImport struct {
594 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700595 android.DefaultableModuleBase
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900596 android.ApexModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800597 prebuilt android.Prebuilt
598
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900599 // Functionality common to Module and Import.
600 embeddableInModuleAndImport
601
Colin Crossfabb6082018-02-20 17:22:23 -0800602 properties AARImportProperties
603
Colin Cross66f78822018-05-02 12:58:28 -0700604 classpathFile android.WritablePath
605 proguardFlags android.WritablePath
606 exportPackage android.WritablePath
607 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700608 manifest android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700609
610 exportedStaticPackages android.Paths
Colin Cross56a83212020-09-15 18:30:11 -0700611
612 hideApexVariantFromMake bool
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000613
614 aarPath android.Path
615}
616
617var _ android.OutputFileProducer = (*AARImport)(nil)
618
619// For OutputFileProducer interface
620func (a *AARImport) OutputFiles(tag string) (android.Paths, error) {
621 switch tag {
622 case ".aar":
623 return []android.Path{a.aarPath}, nil
624 case "":
625 return []android.Path{a.classpathFile}, nil
626 default:
627 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
628 }
Colin Crossfabb6082018-02-20 17:22:23 -0800629}
630
Jiyong Park6a927c42020-01-21 02:03:43 +0900631func (a *AARImport) sdkVersion() sdkSpec {
632 return sdkSpecFrom(String(a.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700633}
634
Paul Duffine25c6442019-10-11 13:50:28 +0100635func (a *AARImport) systemModules() string {
636 return ""
637}
638
Jiyong Park6a927c42020-01-21 02:03:43 +0900639func (a *AARImport) minSdkVersion() sdkSpec {
Colin Cross479884c2018-07-10 13:39:30 -0700640 if a.properties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900641 return sdkSpecFrom(*a.properties.Min_sdk_version)
Colin Cross479884c2018-07-10 13:39:30 -0700642 }
Colin Cross83bb3162018-06-25 15:48:06 -0700643 return a.sdkVersion()
644}
645
Jiyong Park6a927c42020-01-21 02:03:43 +0900646func (a *AARImport) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700647 return a.sdkVersion()
648}
649
Colin Cross1e743852019-10-28 11:37:20 -0700650func (a *AARImport) javaVersion() string {
651 return ""
652}
653
Colin Crossa97c5d32018-03-28 14:58:31 -0700654var _ AndroidLibraryDependency = (*AARImport)(nil)
655
656func (a *AARImport) ExportPackage() android.Path {
657 return a.exportPackage
658}
659
Colin Cross89c31582018-04-30 15:55:11 -0700660func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
661 return android.Paths{a.proguardFlags}
662}
663
Anton Hansson53c88442019-03-18 15:53:16 +0000664func (a *AARImport) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800665 return nil
666}
667
Colin Cross66f78822018-05-02 12:58:28 -0700668func (a *AARImport) ExportedStaticPackages() android.Paths {
669 return a.exportedStaticPackages
670}
671
Colin Cross90c25c62019-04-19 16:22:57 -0700672func (a *AARImport) ExportedManifests() android.Paths {
673 return android.Paths{a.manifest}
Colin Cross31656952018-05-24 16:11:20 -0700674}
675
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800676// TODO(jungjw): Decide whether we want to implement this.
677func (a *AARImport) ExportedAssets() android.OptionalPath {
678 return android.OptionalPath{}
679}
680
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700681// RRO enforcement is not available on aar_import since its RRO dirs are not
682// exported.
683func (a *AARImport) SetRROEnforcedForDependent(enforce bool) {
684}
685
686// RRO enforcement is not available on aar_import since its RRO dirs are not
687// exported.
688func (a *AARImport) IsRROEnforced(ctx android.BaseModuleContext) bool {
689 return false
690}
691
Colin Crossfabb6082018-02-20 17:22:23 -0800692func (a *AARImport) Prebuilt() *android.Prebuilt {
693 return &a.prebuilt
694}
695
696func (a *AARImport) Name() string {
697 return a.prebuilt.Name(a.ModuleBase.Name())
698}
699
Jiyong Park618922e2020-01-08 13:35:43 +0900700func (a *AARImport) JacocoReportClassesFile() android.Path {
701 return nil
702}
703
Colin Crossfabb6082018-02-20 17:22:23 -0800704func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Jeongik Cha816a23a2020-07-08 01:09:23 +0900705 if !ctx.Config().AlwaysUsePrebuiltSdks() {
Colin Cross83bb3162018-06-25 15:48:06 -0700706 sdkDep := decodeSdkDep(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700707 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700708 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800709 }
710 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700711
Colin Cross42d48b72018-08-29 14:10:52 -0700712 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
713 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800714}
715
716// 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 -0700717// 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 -0800718var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
719 blueprint.RuleParams{
Dan Willemsen304cfec2019-05-28 14:49:06 -0700720 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
Colin Cross205e9112020-08-06 13:20:17 -0700721 `unzip -qoDD -d $outDir $in && rm -rf $outDir/res && touch $out && ` +
722 `${config.MergeZipsCmd} $combinedClassesJar $$(ls $outDir/classes.jar 2> /dev/null) $$(ls $outDir/libs/*.jar 2> /dev/null)`,
723 CommandDeps: []string{"${config.MergeZipsCmd}"},
Colin Crossfabb6082018-02-20 17:22:23 -0800724 },
Colin Cross205e9112020-08-06 13:20:17 -0700725 "outDir", "combinedClassesJar")
Colin Crossfabb6082018-02-20 17:22:23 -0800726
727func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
728 if len(a.properties.Aars) != 1 {
729 ctx.PropertyErrorf("aars", "exactly one aar is required")
730 return
731 }
732
Colin Cross56a83212020-09-15 18:30:11 -0700733 a.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
734
Nan Zhang4c819fb2018-08-27 18:31:46 -0700735 aarName := ctx.ModuleName() + ".aar"
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000736 a.aarPath = android.PathForModuleSrc(ctx, a.properties.Aars[0])
737
Colin Cross1001a792019-03-21 22:21:39 -0700738 if Bool(a.properties.Jetifier) {
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000739 inputFile := a.aarPath
740 a.aarPath = android.PathForModuleOut(ctx, "jetifier", aarName)
741 TransformJetifier(ctx, a.aarPath.(android.WritablePath), inputFile)
Nan Zhang4c819fb2018-08-27 18:31:46 -0700742 }
Colin Crossfabb6082018-02-20 17:22:23 -0800743
744 extractedAARDir := android.PathForModuleOut(ctx, "aar")
Colin Cross205e9112020-08-06 13:20:17 -0700745 a.classpathFile = extractedAARDir.Join(ctx, "classes-combined.jar")
Colin Crossfabb6082018-02-20 17:22:23 -0800746 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700747 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossfabb6082018-02-20 17:22:23 -0800748
749 ctx.Build(pctx, android.BuildParams{
750 Rule: unzipAAR,
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000751 Input: a.aarPath,
Colin Cross10f7c4a2018-05-23 10:59:28 -0700752 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest},
Colin Crossfabb6082018-02-20 17:22:23 -0800753 Description: "unzip AAR",
754 Args: map[string]string{
Colin Cross205e9112020-08-06 13:20:17 -0700755 "outDir": extractedAARDir.String(),
756 "combinedClassesJar": a.classpathFile.String(),
Colin Crossfabb6082018-02-20 17:22:23 -0800757 },
758 })
759
Colin Crossa0ba2f52019-06-22 12:59:27 -0700760 // Always set --pseudo-localize, it will be stripped out later for release
761 // builds that don't want it.
762 compileFlags := []string{"--pseudo-localize"}
Colin Crossfabb6082018-02-20 17:22:23 -0800763 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
Colin Crossfabb6082018-02-20 17:22:23 -0800764 flata := compiledResDir.Join(ctx, "gen_res.flata")
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000765 aapt2CompileZip(ctx, flata, a.aarPath, "res", compileFlags)
Colin Crossfabb6082018-02-20 17:22:23 -0800766
767 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900768 // the subdir "android" is required to be filtered by package names
769 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossfabb6082018-02-20 17:22:23 -0800770 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700771 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700772 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800773
774 var linkDeps android.Paths
775
776 linkFlags := []string{
777 "--static-lib",
778 "--no-static-lib-packages",
779 "--auto-add-overlay",
780 }
781
Colin Cross10f7c4a2018-05-23 10:59:28 -0700782 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
783 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800784
Ulya Trafimovich18554242020-11-03 15:55:11 +0000785 transitiveStaticLibs, staticLibManifests, staticRRODirs, transitiveAssets, libDeps, libFlags :=
786 aaptLibs(ctx, sdkContext(a), nil)
Colin Cross31656952018-05-24 16:11:20 -0700787
788 _ = staticLibManifests
Colin Crossc1c37552019-01-31 11:42:41 -0800789 _ = staticRRODirs
Colin Crossfabb6082018-02-20 17:22:23 -0800790
Colin Crossa97c5d32018-03-28 14:58:31 -0700791 linkDeps = append(linkDeps, libDeps...)
792 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800793
Colin Cross66f78822018-05-02 12:58:28 -0700794 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800795
Colin Cross66f78822018-05-02 12:58:28 -0700796 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800797 linkFlags, linkDeps, nil, overlayRes, transitiveAssets, nil)
Colin Crossfabb6082018-02-20 17:22:23 -0800798
Colin Crossdcf71b22021-02-01 13:59:03 -0800799 ctx.SetProvider(JavaInfoProvider, JavaInfo{
800 HeaderJars: android.PathsIfNonNil(a.classpathFile),
801 ImplementationAndResourcesJars: android.PathsIfNonNil(a.classpathFile),
802 ImplementationJars: android.PathsIfNonNil(a.classpathFile),
803 })
804}
Colin Crossfabb6082018-02-20 17:22:23 -0800805
806func (a *AARImport) HeaderJars() android.Paths {
807 return android.Paths{a.classpathFile}
808}
809
810func (a *AARImport) ImplementationJars() android.Paths {
811 return android.Paths{a.classpathFile}
812}
813
Colin Cross331a1212018-08-15 20:40:52 -0700814func (a *AARImport) ResourceJars() android.Paths {
815 return nil
816}
817
818func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
819 return android.Paths{a.classpathFile}
820}
821
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000822func (a *AARImport) DexJarBuildPath() android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800823 return nil
824}
825
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100826func (a *AARImport) DexJarInstallPath() android.Path {
827 return nil
828}
829
Colin Crossfabb6082018-02-20 17:22:23 -0800830func (a *AARImport) AidlIncludeDirs() android.Paths {
831 return nil
832}
833
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100834func (a *AARImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
Jiyong Park1be96912018-05-28 18:02:19 +0900835 return nil
836}
837
Colin Crossc9fe10f2020-11-19 18:06:03 -0800838func (d *AARImport) ExportedPlugins() (android.Paths, []string, bool) {
839 return nil, nil, false
Artur Satayev9cf46692019-11-26 18:08:34 +0000840}
841
Colin Cross0c4ce212019-05-03 15:28:19 -0700842func (a *AARImport) SrcJarArgs() ([]string, android.Paths) {
843 return nil, nil
844}
845
Jiyong Park45bf82e2020-12-15 22:29:02 +0900846var _ android.ApexModule = (*AARImport)(nil)
847
848// Implements android.ApexModule
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900849func (a *AARImport) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
850 return a.depIsInSameApex(ctx, dep)
851}
852
Jiyong Park45bf82e2020-12-15 22:29:02 +0900853// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -0700854func (g *AARImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
855 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900856 return nil
857}
858
Colin Crossfabb6082018-02-20 17:22:23 -0800859var _ android.PrebuiltInterface = (*Import)(nil)
860
Colin Cross1b16b0e2019-02-12 14:41:32 -0800861// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
862//
863// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
864// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -0800865func AARImportFactory() android.Module {
866 module := &AARImport{}
867
868 module.AddProperties(&module.properties)
869
870 android.InitPrebuiltModule(module, &module.properties.Aars)
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900871 android.InitApexModule(module)
Colin Cross48de9a42018-10-02 13:53:33 -0700872 InitJavaModule(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800873 return module
874}