blob: a59ecb8ca75ecc8af8dabea0353101fa8f47e0d3 [file] [log] [blame]
Colin Crossfabb6082018-02-20 17:22:23 -08001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Colin Crossa592e3e2019-02-19 16:59:53 -080018 "fmt"
Jaewoong Jung5b425e22019-06-17 17:40:56 -070019 "path/filepath"
Colin Crossc20dc852020-11-10 12:27:45 -080020 "strconv"
Colin Crossa97c5d32018-03-28 14:58:31 -070021 "strings"
Colin Crossfabb6082018-02-20 17:22:23 -080022
Jaewoong Jung9befb0c2020-01-18 10:33:43 -080023 "android/soong/android"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010024 "android/soong/dexpreopt"
Jaewoong Jung9befb0c2020-01-18 10:33:43 -080025
Colin Crossfabb6082018-02-20 17:22:23 -080026 "github.com/google/blueprint"
Colin Crossa97c5d32018-03-28 14:58:31 -070027 "github.com/google/blueprint/proptools"
Colin Crossfabb6082018-02-20 17:22:23 -080028)
29
Colin Crossa97c5d32018-03-28 14:58:31 -070030type AndroidLibraryDependency interface {
Colin Crossa97c5d32018-03-28 14:58:31 -070031 ExportPackage() android.Path
Colin Cross89c31582018-04-30 15:55:11 -070032 ExportedProguardFlagFiles() android.Paths
Anton Hansson53c88442019-03-18 15:53:16 +000033 ExportedRRODirs() []rroDir
Colin Cross66f78822018-05-02 12:58:28 -070034 ExportedStaticPackages() android.Paths
Colin Cross90c25c62019-04-19 16:22:57 -070035 ExportedManifests() android.Paths
Jaewoong Jung6431ca72020-01-15 14:15:10 -080036 ExportedAssets() android.OptionalPath
Jaewoong Jungc779cd42020-10-06 18:56:10 -070037 SetRROEnforcedForDependent(enforce bool)
38 IsRROEnforced(ctx android.BaseModuleContext) bool
Colin Crossa97c5d32018-03-28 14:58:31 -070039}
40
41func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000042 RegisterAARBuildComponents(android.InitRegistrationContext)
43}
44
45func RegisterAARBuildComponents(ctx android.RegistrationContext) {
46 ctx.RegisterModuleType("android_library_import", AARImportFactory)
47 ctx.RegisterModuleType("android_library", AndroidLibraryFactory)
Paul Duffin04ba70d2021-03-22 13:56:43 +000048 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
49 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
50 })
Colin Crossa97c5d32018-03-28 14:58:31 -070051}
52
53//
54// AAR (android library)
55//
56
57type androidLibraryProperties struct {
58 BuildAAR bool `blueprint:"mutated"`
59}
60
61type aaptProperties struct {
62 // flags passed to aapt when creating the apk
63 Aaptflags []string
64
Dan Willemsen72be5902018-10-24 20:24:57 -070065 // include all resource configurations, not just the product-configured
66 // ones.
67 Aapt_include_all_resources *bool
68
Colin Crossa97c5d32018-03-28 14:58:31 -070069 // list of directories relative to the Blueprints file containing assets.
Colin Cross0ddae7f2019-02-07 15:30:01 -080070 // Defaults to ["assets"] if a directory called assets exists. Set to []
71 // to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070072 Asset_dirs []string
73
74 // list of directories relative to the Blueprints file containing
Colin Cross0ddae7f2019-02-07 15:30:01 -080075 // Android resources. Defaults to ["res"] if a directory called res exists.
76 // Set to [] to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070077 Resource_dirs []string
78
Colin Crossa592e3e2019-02-19 16:59:53 -080079 // list of zip files containing Android resources.
Colin Cross27b922f2019-03-04 22:35:41 -080080 Resource_zips []string `android:"path"`
Colin Crossa592e3e2019-02-19 16:59:53 -080081
Colin Crossa97c5d32018-03-28 14:58:31 -070082 // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml".
Colin Cross27b922f2019-03-04 22:35:41 -080083 Manifest *string `android:"path"`
changho.shinb5432b72019-08-08 18:37:17 +090084
85 // paths to additional manifest files to merge with main manifest.
86 Additional_manifests []string `android:"path"`
Sasha Smundak541056c2019-10-28 15:50:06 -070087
88 // do not include AndroidManifest from dependent libraries
89 Dont_merge_manifests *bool
Jaewoong Jungc779cd42020-10-06 18:56:10 -070090
91 // true if RRO is enforced for any of the dependent modules
92 RROEnforcedForDependent bool `blueprint:"mutated"`
Colin Crossa97c5d32018-03-28 14:58:31 -070093}
94
95type aapt struct {
Colin Cross90c25c62019-04-19 16:22:57 -070096 aaptSrcJar android.Path
97 exportPackage android.Path
98 manifestPath android.Path
99 transitiveManifestPaths android.Paths
100 proguardOptionsFile android.Path
101 rroDirs []rroDir
102 rTxt android.Path
103 extraAaptPackagesFile android.Path
104 mergedManifestFile android.Path
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700105 noticeFile android.OptionalPath
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800106 assetPackage android.OptionalPath
Colin Cross90c25c62019-04-19 16:22:57 -0700107 isLibrary bool
Alexei Nicoara69cf0f32022-07-27 14:59:18 +0100108 defaultManifestVersion string
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()) ||
Jeongik Chacee5ba92021-02-19 12:11:51 +0900163 // if RRO is enforced for any of its dependents.
164 a.aaptProperties.RROEnforcedForDependent
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700165}
166
Jiyong Parkf1691d22021-03-29 20:11:58 +0900167func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext android.SdkContext,
Colin Crossa0ba2f52019-06-22 12:59:27 -0700168 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 Park92315372021-04-02 08:45:46 +0900222 minSdkVersion, err := sdkContext.MinSdkVersion(ctx).EffectiveVersionString(ctx)
Jiyong Park6a927c42020-01-21 02:03:43 +0900223 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
Jiyong Parkf1691d22021-03-29 20:11:58 +0900270func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext android.SdkContext,
Paul Duffin06530572022-02-03 17:54:15 +0000271 classLoaderContexts dexpreopt.ClassLoaderContextMap, excludedLibs []string,
272 extraLinkFlags ...string) {
Colin Cross5446e882019-05-22 10:46:27 -0700273
Ulya Trafimovich18554242020-11-03 15:55:11 +0000274 transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, assetPackages, libDeps, libFlags :=
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100275 aaptLibs(ctx, sdkContext, classLoaderContexts)
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100276
Paul Duffin06530572022-02-03 17:54:15 +0000277 // Exclude any libraries from the supplied list.
278 classLoaderContexts = classLoaderContexts.ExcludeLibs(excludedLibs)
279
Colin Cross31656952018-05-24 16:11:20 -0700280 // App manifest file
281 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
282 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
283
Gurpreet Singh7deabfa2022-02-10 13:28:35 +0000284 manifestPath := ManifestFixer(ctx, manifestSrcPath, ManifestFixerParams{
Alexei Nicoara69cf0f32022-07-27 14:59:18 +0100285 SdkContext: sdkContext,
286 ClassLoaderContexts: classLoaderContexts,
287 IsLibrary: a.isLibrary,
288 DefaultManifestVersion: a.defaultManifestVersion,
289 UseEmbeddedNativeLibs: a.useEmbeddedNativeLibs,
290 UsesNonSdkApis: a.usesNonSdkApis,
291 UseEmbeddedDex: a.useEmbeddedDex,
292 HasNoCode: a.hasNoCode,
293 LoggingParent: a.LoggingParent,
Gurpreet Singh75d65f32022-01-24 17:44:05 +0000294 })
Colin Cross90c25c62019-04-19 16:22:57 -0700295
Luca Stefanifd898822019-09-10 22:13:31 +0200296 // Add additional manifest files to transitive manifests.
297 additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)
298 a.transitiveManifestPaths = append(android.Paths{manifestPath}, additionalManifests...)
299 a.transitiveManifestPaths = append(a.transitiveManifestPaths, transitiveStaticLibManifests...)
Colin Cross90c25c62019-04-19 16:22:57 -0700300
Sasha Smundak541056c2019-10-28 15:50:06 -0700301 if len(a.transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) {
Luca Stefanifd898822019-09-10 22:13:31 +0200302 a.mergedManifestFile = manifestMerger(ctx, a.transitiveManifestPaths[0], a.transitiveManifestPaths[1:], a.isLibrary)
Colin Cross90c25c62019-04-19 16:22:57 -0700303 if !a.isLibrary {
304 // Only use the merged manifest for applications. For libraries, the transitive closure of manifests
305 // will be propagated to the final application and merged there. The merged manifest for libraries is
306 // only passed to Make, which can't handle transitive dependencies.
307 manifestPath = a.mergedManifestFile
308 }
309 } else {
310 a.mergedManifestFile = manifestPath
311 }
Colin Cross31656952018-05-24 16:11:20 -0700312
Colin Crossa0ba2f52019-06-22 12:59:27 -0700313 compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700314
Colin Crossc1c37552019-01-31 11:42:41 -0800315 rroDirs = append(rroDirs, staticRRODirs...)
Colin Cross31656952018-05-24 16:11:20 -0700316 linkFlags = append(linkFlags, libFlags...)
317 linkDeps = append(linkDeps, libDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700318 linkFlags = append(linkFlags, extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700319 if a.isLibrary {
320 linkFlags = append(linkFlags, "--static-lib")
321 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700322
323 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900324 // the subdir "android" is required to be filtered by package names
325 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700326 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
327 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700328 // This file isn't used by Soong, but is generated for exporting
329 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Crossa97c5d32018-03-28 14:58:31 -0700330
Colin Cross4aaa84a2018-08-21 15:14:37 -0700331 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700332 for _, dir := range resDirs {
Colin Cross014489c2020-06-02 20:09:13 -0700333 a.resourceFiles = append(a.resourceFiles, dir.files...)
Colin Crossa0ba2f52019-06-22 12:59:27 -0700334 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700335 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700336
Colin Crossa592e3e2019-02-19 16:59:53 -0800337 for i, zip := range resZips {
338 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
Colin Crossa0ba2f52019-06-22 12:59:27 -0700339 aapt2CompileZip(ctx, flata, zip, "", compileFlags)
Colin Crossa592e3e2019-02-19 16:59:53 -0800340 compiledResDirs = append(compiledResDirs, android.Paths{flata})
341 }
342
Colin Cross4aaa84a2018-08-21 15:14:37 -0700343 var compiledRes, compiledOverlay android.Paths
344
345 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
346
Colin Crossbec85302019-02-13 13:15:46 -0800347 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700348 // If we are using static android libraries, every source file becomes an overlay.
349 // This is to emulate old AAPT behavior which simulated library support.
350 for _, compiledResDir := range compiledResDirs {
351 compiledOverlay = append(compiledOverlay, compiledResDir...)
352 }
Colin Crossbec85302019-02-13 13:15:46 -0800353 } else if a.isLibrary {
354 // Otherwise, for a static library we treat all the resources equally with no overlay.
355 for _, compiledResDir := range compiledResDirs {
356 compiledRes = append(compiledRes, compiledResDir...)
357 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700358 } else if len(compiledResDirs) > 0 {
359 // Without static libraries, the first directory is our directory, which can then be
360 // overlaid by the rest.
361 compiledRes = append(compiledRes, compiledResDirs[0]...)
362 for _, compiledResDir := range compiledResDirs[1:] {
363 compiledOverlay = append(compiledOverlay, compiledResDir...)
364 }
365 }
366
Colin Crossa97c5d32018-03-28 14:58:31 -0700367 for _, dir := range overlayDirs {
Colin Crossa0ba2f52019-06-22 12:59:27 -0700368 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700369 }
370
Colin Crosse560c4a2019-03-19 16:03:11 -0700371 var splitPackages android.WritablePaths
372 var splits []split
373
374 for _, s := range a.splitNames {
375 suffix := strings.Replace(s, ",", "_", -1)
376 path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
377 linkFlags = append(linkFlags, "--split", path.String()+":"+s)
378 splitPackages = append(splitPackages, path)
379 splits = append(splits, split{
380 name: s,
381 suffix: suffix,
382 path: path,
383 })
384 }
385
Colin Cross66f78822018-05-02 12:58:28 -0700386 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800387 linkFlags, linkDeps, compiledRes, compiledOverlay, assetPackages, splitPackages)
388
389 // Extract assets from the resource package output so that they can be used later in aapt2link
390 // for modules that depend on this one.
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800391 if android.PrefixInList(linkFlags, "-A ") || len(assetPackages) > 0 {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800392 assets := android.PathForModuleOut(ctx, "assets.zip")
393 ctx.Build(pctx, android.BuildParams{
394 Rule: extractAssetsRule,
395 Input: packageRes,
396 Output: assets,
397 Description: "extract assets from built resource file",
398 })
399 a.assetPackage = android.OptionalPathForPath(assets)
400 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700401
402 a.aaptSrcJar = srcJar
403 a.exportPackage = packageRes
404 a.manifestPath = manifestPath
405 a.proguardOptionsFile = proguardOptionsFile
406 a.rroDirs = rroDirs
Colin Cross66f78822018-05-02 12:58:28 -0700407 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700408 a.rTxt = rTxt
Colin Crosse560c4a2019-03-19 16:03:11 -0700409 a.splits = splits
Colin Crossa97c5d32018-03-28 14:58:31 -0700410}
411
412// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Jiyong Parkf1691d22021-03-29 20:11:58 +0900413func aaptLibs(ctx android.ModuleContext, sdkContext android.SdkContext, classLoaderContexts dexpreopt.ClassLoaderContextMap) (
Ulya Trafimovich18554242020-11-03 15:55:11 +0000414 transitiveStaticLibs, transitiveStaticLibManifests android.Paths, staticRRODirs []rroDir, assets, deps android.Paths, flags []string) {
Colin Cross66f78822018-05-02 12:58:28 -0700415
Colin Crossa97c5d32018-03-28 14:58:31 -0700416 var sharedLibs android.Paths
417
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100418 if classLoaderContexts == nil {
Ulya Trafimovich18554242020-11-03 15:55:11 +0000419 // Not all callers need to compute class loader context, those who don't just pass nil.
420 // Create a temporary class loader context here (it will be computed, but not used).
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100421 classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000422 }
423
Colin Cross83bb3162018-06-25 15:48:06 -0700424 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700425 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700426 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700427 }
428
429 ctx.VisitDirectDeps(func(module android.Module) {
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000430 depTag := ctx.OtherModuleDependencyTag(module)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000431
Colin Crossa97c5d32018-03-28 14:58:31 -0700432 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700433 aarDep, _ := module.(AndroidLibraryDependency)
434 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700435 exportPackage = aarDep.ExportPackage()
436 }
437
Ulya Trafimovich65b03192020-12-03 16:50:22 +0000438 switch depTag {
Colin Cross4b964c02018-10-15 16:18:06 -0700439 case instrumentationForTag:
440 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Colin Cross5446e882019-05-22 10:46:27 -0700441 case libTag:
442 if exportPackage != nil {
443 sharedLibs = append(sharedLibs, exportPackage)
444 }
Colin Cross5446e882019-05-22 10:46:27 -0700445 case frameworkResTag:
Colin Crossa97c5d32018-03-28 14:58:31 -0700446 if exportPackage != nil {
447 sharedLibs = append(sharedLibs, exportPackage)
448 }
449 case staticLibTag:
450 if exportPackage != nil {
Colin Cross66f78822018-05-02 12:58:28 -0700451 transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
Colin Crossbec85302019-02-13 13:15:46 -0800452 transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
Colin Cross90c25c62019-04-19 16:22:57 -0700453 transitiveStaticLibManifests = append(transitiveStaticLibManifests, aarDep.ExportedManifests()...)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800454 if aarDep.ExportedAssets().Valid() {
455 assets = append(assets, aarDep.ExportedAssets().Path())
456 }
Anton Hansson53c88442019-03-18 15:53:16 +0000457
Jeongik Chacee5ba92021-02-19 12:11:51 +0900458 outer:
459 for _, d := range aarDep.ExportedRRODirs() {
460 for _, e := range staticRRODirs {
461 if d.path == e.path {
462 continue outer
Anton Hansson53c88442019-03-18 15:53:16 +0000463 }
464 }
Jeongik Chacee5ba92021-02-19 12:11:51 +0900465 staticRRODirs = append(staticRRODirs, d)
Anton Hansson53c88442019-03-18 15:53:16 +0000466 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700467 }
468 }
Ulya Trafimovich18554242020-11-03 15:55:11 +0000469
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +0000470 addCLCFromDep(ctx, module, classLoaderContexts)
Colin Crossa97c5d32018-03-28 14:58:31 -0700471 })
472
473 deps = append(deps, sharedLibs...)
Colin Cross66f78822018-05-02 12:58:28 -0700474 deps = append(deps, transitiveStaticLibs...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700475
Colin Cross66f78822018-05-02 12:58:28 -0700476 if len(transitiveStaticLibs) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700477 flags = append(flags, "--auto-add-overlay")
478 }
479
480 for _, sharedLib := range sharedLibs {
481 flags = append(flags, "-I "+sharedLib.String())
482 }
483
Colin Cross66f78822018-05-02 12:58:28 -0700484 transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
Colin Cross90c25c62019-04-19 16:22:57 -0700485 transitiveStaticLibManifests = android.FirstUniquePaths(transitiveStaticLibManifests)
Colin Cross66f78822018-05-02 12:58:28 -0700486
Ulya Trafimovich18554242020-11-03 15:55:11 +0000487 return transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, assets, deps, flags
Colin Crossa97c5d32018-03-28 14:58:31 -0700488}
489
490type AndroidLibrary struct {
491 Library
492 aapt
493
494 androidLibraryProperties androidLibraryProperties
495
496 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700497
498 exportedProguardFlagFiles android.Paths
Colin Cross66f78822018-05-02 12:58:28 -0700499 exportedStaticPackages android.Paths
Colin Cross89c31582018-04-30 15:55:11 -0700500}
501
Saeid Farivar Asanjan1fca3012021-09-14 18:40:19 +0000502var _ android.OutputFileProducer = (*AndroidLibrary)(nil)
503
504// For OutputFileProducer interface
505func (a *AndroidLibrary) OutputFiles(tag string) (android.Paths, error) {
506 switch tag {
507 case ".aar":
508 return []android.Path{a.aarFile}, nil
509 default:
510 return a.Library.OutputFiles(tag)
511 }
512}
513
Colin Cross89c31582018-04-30 15:55:11 -0700514func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
515 return a.exportedProguardFlagFiles
Colin Crossa97c5d32018-03-28 14:58:31 -0700516}
517
Colin Cross66f78822018-05-02 12:58:28 -0700518func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
519 return a.exportedStaticPackages
520}
521
Colin Crossa97c5d32018-03-28 14:58:31 -0700522var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
523
524func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
525 a.Module.deps(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +0900526 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
Paul Duffin250e6192019-06-07 10:44:37 +0100527 if sdkDep.hasFrameworkLibs() {
528 a.aapt.deps(ctx, sdkDep)
Colin Crossa97c5d32018-03-28 14:58:31 -0700529 }
Ulya Trafimovich42c7f0d2021-08-17 16:20:29 +0100530 a.usesLibrary.deps(ctx, sdkDep.hasFrameworkLibs())
Colin Crossa97c5d32018-03-28 14:58:31 -0700531}
532
533func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800534 a.aapt.isLibrary = true
Ulya Trafimovich42c7f0d2021-08-17 16:20:29 +0100535 a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Paul Duffin06530572022-02-03 17:54:15 +0000536 a.aapt.buildActions(ctx, android.SdkContext(a), a.classLoaderContexts, nil)
Colin Crossa97c5d32018-03-28 14:58:31 -0700537
Colin Cross56a83212020-09-15 18:30:11 -0700538 a.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
539
Colin Crossa97c5d32018-03-28 14:58:31 -0700540 ctx.CheckbuildFile(a.proguardOptionsFile)
541 ctx.CheckbuildFile(a.exportPackage)
542 ctx.CheckbuildFile(a.aaptSrcJar)
543
544 // apps manifests are handled by aapt, don't let Module see them
545 a.properties.Manifest = nil
546
Colin Cross014489c2020-06-02 20:09:13 -0700547 a.linter.mergedManifest = a.aapt.mergedManifestFile
548 a.linter.manifest = a.aapt.manifestPath
549 a.linter.resources = a.aapt.resourceFiles
550
Colin Crossa97c5d32018-03-28 14:58:31 -0700551 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
552 a.proguardOptionsFile)
553
554 a.Module.compile(ctx, a.aaptSrcJar)
555
Colin Crossf57c5782019-01-25 13:20:38 -0800556 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700557 var res android.Paths
558 if a.androidLibraryProperties.BuildAAR {
559 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
560 ctx.CheckbuildFile(a.aarFile)
561 }
Colin Cross89c31582018-04-30 15:55:11 -0700562
Cole Faust9a631312020-10-22 21:05:24 +0000563 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles,
564 android.PathsForModuleSrc(ctx, a.dexProperties.Optimize.Proguard_flags_files)...)
Colin Cross89c31582018-04-30 15:55:11 -0700565 ctx.VisitDirectDeps(func(m android.Module) {
566 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
567 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
Colin Cross66f78822018-05-02 12:58:28 -0700568 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
569 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
Colin Cross89c31582018-04-30 15:55:11 -0700570 }
571 })
572
573 a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
Colin Cross66f78822018-05-02 12:58:28 -0700574 a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
Sam Delmerico82602492022-06-10 17:05:42 +0000575
576 prebuiltJniPackages := android.Paths{}
577 ctx.VisitDirectDeps(func(module android.Module) {
578 if info, ok := ctx.OtherModuleProvider(module, JniPackageProvider).(JniPackageInfo); ok {
579 prebuiltJniPackages = append(prebuiltJniPackages, info.JniPackages...)
580 }
581 })
582 if len(prebuiltJniPackages) > 0 {
583 ctx.SetProvider(JniPackageProvider, JniPackageInfo{
584 JniPackages: prebuiltJniPackages,
585 })
586 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700587}
588
Colin Cross1b16b0e2019-02-12 14:41:32 -0800589// android_library builds and links sources into a `.jar` file for the device along with Android resources.
590//
591// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
Sam Delmerico82602492022-06-10 17:05:42 +0000592// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
Colin Cross1b16b0e2019-02-12 14:41:32 -0800593// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
594// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700595func AndroidLibraryFactory() android.Module {
596 module := &AndroidLibrary{}
597
Colin Crossce6734e2020-06-15 16:09:53 -0700598 module.Module.addHostAndDeviceProperties()
Colin Crossa97c5d32018-03-28 14:58:31 -0700599 module.AddProperties(
Colin Crossa97c5d32018-03-28 14:58:31 -0700600 &module.aaptProperties,
601 &module.androidLibraryProperties)
602
603 module.androidLibraryProperties.BuildAAR = true
Colin Cross014489c2020-06-02 20:09:13 -0700604 module.Module.linter.library = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700605
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900606 android.InitApexModule(module)
Colin Cross48de9a42018-10-02 13:53:33 -0700607 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700608 return module
609}
610
Colin Crossfabb6082018-02-20 17:22:23 -0800611//
612// AAR (android library) prebuilts
613//
Colin Crossfabb6082018-02-20 17:22:23 -0800614
Vinh Trance0781f2022-04-13 01:30:44 +0000615// Properties for android_library_import
Colin Crossfabb6082018-02-20 17:22:23 -0800616type AARImportProperties struct {
Vinh Trance0781f2022-04-13 01:30:44 +0000617 // ARR (android library prebuilt) filepath. Exactly one ARR is required.
Colin Cross27b922f2019-03-04 22:35:41 -0800618 Aars []string `android:"path"`
Vinh Trance0781f2022-04-13 01:30:44 +0000619 // If not blank, set to the version of the sdk to compile against.
620 // Defaults to private.
621 // Values are of one of the following forms:
622 // 1) numerical API level, "current", "none", or "core_platform"
623 // 2) An SDK kind with an API level: "<sdk kind>_<API level>"
624 // See build/soong/android/sdk_version.go for the complete and up to date list of SDK kinds.
625 // If the SDK kind is empty, it will be set to public
626 Sdk_version *string
627 // If not blank, set the minimum version of the sdk that the compiled artifacts will run against.
628 // Defaults to sdk_version if not set. See sdk_version for possible values.
Colin Cross479884c2018-07-10 13:39:30 -0700629 Min_sdk_version *string
Vinh Trance0781f2022-04-13 01:30:44 +0000630 // List of java static libraries that the included ARR (android library prebuilts) has dependencies to.
Colin Crossa97c5d32018-03-28 14:58:31 -0700631 Static_libs []string
Vinh Trance0781f2022-04-13 01:30:44 +0000632 // List of java libraries that the included ARR (android library prebuilts) has dependencies to.
633 Libs []string
634 // If set to true, run Jetifier against .aar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -0700635 Jetifier *bool
Sam Delmerico82602492022-06-10 17:05:42 +0000636 // If true, extract JNI libs from AAR archive. These libs will be accessible to android_app modules and
637 // will be passed transitively through android_libraries to an android_app.
638 //TODO(b/241138093) evaluate whether we can have this flag default to true for Bazel conversion
639 Extract_jni *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800640}
641
642type AARImport struct {
643 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700644 android.DefaultableModuleBase
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900645 android.ApexModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800646 prebuilt android.Prebuilt
647
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900648 // Functionality common to Module and Import.
649 embeddableInModuleAndImport
650
Colin Crossfabb6082018-02-20 17:22:23 -0800651 properties AARImportProperties
652
Colin Cross66f78822018-05-02 12:58:28 -0700653 classpathFile android.WritablePath
654 proguardFlags android.WritablePath
655 exportPackage android.WritablePath
656 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700657 manifest android.WritablePath
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800658 assetsPackage android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700659
660 exportedStaticPackages android.Paths
Colin Cross56a83212020-09-15 18:30:11 -0700661
662 hideApexVariantFromMake bool
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000663
Sam Delmerico82602492022-06-10 17:05:42 +0000664 aarPath android.Path
665 jniPackages android.Paths
Jiyong Park92315372021-04-02 08:45:46 +0900666
667 sdkVersion android.SdkSpec
668 minSdkVersion android.SdkSpec
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000669}
670
671var _ android.OutputFileProducer = (*AARImport)(nil)
672
673// For OutputFileProducer interface
674func (a *AARImport) OutputFiles(tag string) (android.Paths, error) {
675 switch tag {
676 case ".aar":
677 return []android.Path{a.aarPath}, nil
678 case "":
679 return []android.Path{a.classpathFile}, nil
680 default:
681 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
682 }
Colin Crossfabb6082018-02-20 17:22:23 -0800683}
684
Jiyong Park92315372021-04-02 08:45:46 +0900685func (a *AARImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
686 return android.SdkSpecFrom(ctx, String(a.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700687}
688
Jiyong Parkf1691d22021-03-29 20:11:58 +0900689func (a *AARImport) SystemModules() string {
Paul Duffine25c6442019-10-11 13:50:28 +0100690 return ""
691}
692
Jiyong Park92315372021-04-02 08:45:46 +0900693func (a *AARImport) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Colin Cross479884c2018-07-10 13:39:30 -0700694 if a.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +0900695 return android.SdkSpecFrom(ctx, *a.properties.Min_sdk_version)
Colin Cross479884c2018-07-10 13:39:30 -0700696 }
Jiyong Park92315372021-04-02 08:45:46 +0900697 return a.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -0700698}
699
William Loh5a082f92022-05-17 20:21:50 +0000700func (a *AARImport) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.SdkSpec {
701 return android.SdkSpecFrom(ctx, "")
702}
703
Jiyong Park92315372021-04-02 08:45:46 +0900704func (a *AARImport) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
705 return a.SdkVersion(ctx)
Dan Willemsen419290a2018-10-31 15:28:47 -0700706}
707
Colin Cross1e743852019-10-28 11:37:20 -0700708func (a *AARImport) javaVersion() string {
709 return ""
710}
711
Colin Crossa97c5d32018-03-28 14:58:31 -0700712var _ AndroidLibraryDependency = (*AARImport)(nil)
713
714func (a *AARImport) ExportPackage() android.Path {
715 return a.exportPackage
716}
717
Colin Cross89c31582018-04-30 15:55:11 -0700718func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
719 return android.Paths{a.proguardFlags}
720}
721
Anton Hansson53c88442019-03-18 15:53:16 +0000722func (a *AARImport) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800723 return nil
724}
725
Colin Cross66f78822018-05-02 12:58:28 -0700726func (a *AARImport) ExportedStaticPackages() android.Paths {
727 return a.exportedStaticPackages
728}
729
Colin Cross90c25c62019-04-19 16:22:57 -0700730func (a *AARImport) ExportedManifests() android.Paths {
731 return android.Paths{a.manifest}
Colin Cross31656952018-05-24 16:11:20 -0700732}
733
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800734func (a *AARImport) ExportedAssets() android.OptionalPath {
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800735 return android.OptionalPathForPath(a.assetsPackage)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800736}
737
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700738// RRO enforcement is not available on aar_import since its RRO dirs are not
739// exported.
740func (a *AARImport) SetRROEnforcedForDependent(enforce bool) {
741}
742
743// RRO enforcement is not available on aar_import since its RRO dirs are not
744// exported.
745func (a *AARImport) IsRROEnforced(ctx android.BaseModuleContext) bool {
746 return false
747}
748
Colin Crossfabb6082018-02-20 17:22:23 -0800749func (a *AARImport) Prebuilt() *android.Prebuilt {
750 return &a.prebuilt
751}
752
753func (a *AARImport) Name() string {
754 return a.prebuilt.Name(a.ModuleBase.Name())
755}
756
Jiyong Park618922e2020-01-08 13:35:43 +0900757func (a *AARImport) JacocoReportClassesFile() android.Path {
758 return nil
759}
760
Colin Crossfabb6082018-02-20 17:22:23 -0800761func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Jeongik Cha816a23a2020-07-08 01:09:23 +0900762 if !ctx.Config().AlwaysUsePrebuiltSdks() {
Jiyong Parkf1691d22021-03-29 20:11:58 +0900763 sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700764 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700765 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800766 }
767 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700768
Colin Cross42d48b72018-08-29 14:10:52 -0700769 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
770 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800771}
772
Sam Delmerico82602492022-06-10 17:05:42 +0000773type JniPackageInfo struct {
774 // List of zip files containing JNI libraries
775 // Zip files should have directory structure jni/<arch>/*.so
776 JniPackages android.Paths
777}
778
779var JniPackageProvider = blueprint.NewProvider(JniPackageInfo{})
780
781// Unzip an AAR and extract the JNI libs for $archString.
782var extractJNI = pctx.AndroidStaticRule("extractJNI",
783 blueprint.RuleParams{
784 Command: `rm -rf $out $outDir && touch $out && ` +
785 `unzip -qoDD -d $outDir $in "jni/${archString}/*" && ` +
786 `jni_files=$$(find $outDir/jni -type f) && ` +
787 // print error message if there are no JNI libs for this arch
788 `[ -n "$$jni_files" ] || (echo "ERROR: no JNI libs found for arch ${archString}" && exit 1) && ` +
789 `${config.SoongZipCmd} -o $out -P 'lib/${archString}' ` +
790 `-C $outDir/jni/${archString} $$(echo $$jni_files | xargs -n1 printf " -f %s")`,
791 CommandDeps: []string{"${config.SoongZipCmd}"},
792 },
793 "outDir", "archString")
794
Colin Crossfabb6082018-02-20 17:22:23 -0800795// 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 -0700796// 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 -0800797var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
798 blueprint.RuleParams{
Dan Willemsen304cfec2019-05-28 14:49:06 -0700799 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
Colin Cross205e9112020-08-06 13:20:17 -0700800 `unzip -qoDD -d $outDir $in && rm -rf $outDir/res && touch $out && ` +
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800801 `${config.Zip2ZipCmd} -i $in -o $assetsPackage 'assets/**/*' && ` +
Colin Cross205e9112020-08-06 13:20:17 -0700802 `${config.MergeZipsCmd} $combinedClassesJar $$(ls $outDir/classes.jar 2> /dev/null) $$(ls $outDir/libs/*.jar 2> /dev/null)`,
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800803 CommandDeps: []string{"${config.MergeZipsCmd}", "${config.Zip2ZipCmd}"},
Colin Crossfabb6082018-02-20 17:22:23 -0800804 },
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800805 "outDir", "combinedClassesJar", "assetsPackage")
Colin Crossfabb6082018-02-20 17:22:23 -0800806
807func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
808 if len(a.properties.Aars) != 1 {
809 ctx.PropertyErrorf("aars", "exactly one aar is required")
810 return
811 }
812
Jiyong Park92315372021-04-02 08:45:46 +0900813 a.sdkVersion = a.SdkVersion(ctx)
814 a.minSdkVersion = a.MinSdkVersion(ctx)
815
Colin Cross56a83212020-09-15 18:30:11 -0700816 a.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
817
Nan Zhang4c819fb2018-08-27 18:31:46 -0700818 aarName := ctx.ModuleName() + ".aar"
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000819 a.aarPath = android.PathForModuleSrc(ctx, a.properties.Aars[0])
820
Colin Cross1001a792019-03-21 22:21:39 -0700821 if Bool(a.properties.Jetifier) {
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000822 inputFile := a.aarPath
823 a.aarPath = android.PathForModuleOut(ctx, "jetifier", aarName)
824 TransformJetifier(ctx, a.aarPath.(android.WritablePath), inputFile)
Nan Zhang4c819fb2018-08-27 18:31:46 -0700825 }
Colin Crossfabb6082018-02-20 17:22:23 -0800826
827 extractedAARDir := android.PathForModuleOut(ctx, "aar")
Colin Cross205e9112020-08-06 13:20:17 -0700828 a.classpathFile = extractedAARDir.Join(ctx, "classes-combined.jar")
Colin Crossfabb6082018-02-20 17:22:23 -0800829 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700830 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800831 a.assetsPackage = android.PathForModuleOut(ctx, "assets.zip")
Colin Crossfabb6082018-02-20 17:22:23 -0800832
833 ctx.Build(pctx, android.BuildParams{
834 Rule: unzipAAR,
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000835 Input: a.aarPath,
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800836 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest, a.assetsPackage},
Colin Crossfabb6082018-02-20 17:22:23 -0800837 Description: "unzip AAR",
838 Args: map[string]string{
Colin Cross205e9112020-08-06 13:20:17 -0700839 "outDir": extractedAARDir.String(),
840 "combinedClassesJar": a.classpathFile.String(),
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800841 "assetsPackage": a.assetsPackage.String(),
Colin Crossfabb6082018-02-20 17:22:23 -0800842 },
843 })
844
Colin Crossa0ba2f52019-06-22 12:59:27 -0700845 // Always set --pseudo-localize, it will be stripped out later for release
846 // builds that don't want it.
847 compileFlags := []string{"--pseudo-localize"}
Colin Crossfabb6082018-02-20 17:22:23 -0800848 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
Colin Crossfabb6082018-02-20 17:22:23 -0800849 flata := compiledResDir.Join(ctx, "gen_res.flata")
Saeid Farivar Asanjanf0436962020-10-05 19:09:09 +0000850 aapt2CompileZip(ctx, flata, a.aarPath, "res", compileFlags)
Colin Crossfabb6082018-02-20 17:22:23 -0800851
852 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900853 // the subdir "android" is required to be filtered by package names
854 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossfabb6082018-02-20 17:22:23 -0800855 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700856 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700857 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800858
859 var linkDeps android.Paths
860
861 linkFlags := []string{
862 "--static-lib",
863 "--no-static-lib-packages",
864 "--auto-add-overlay",
865 }
866
Colin Cross10f7c4a2018-05-23 10:59:28 -0700867 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
868 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800869
Ulya Trafimovich18554242020-11-03 15:55:11 +0000870 transitiveStaticLibs, staticLibManifests, staticRRODirs, transitiveAssets, libDeps, libFlags :=
Jiyong Parkf1691d22021-03-29 20:11:58 +0900871 aaptLibs(ctx, android.SdkContext(a), nil)
Colin Cross31656952018-05-24 16:11:20 -0700872
873 _ = staticLibManifests
Colin Crossc1c37552019-01-31 11:42:41 -0800874 _ = staticRRODirs
Colin Crossfabb6082018-02-20 17:22:23 -0800875
Colin Crossa97c5d32018-03-28 14:58:31 -0700876 linkDeps = append(linkDeps, libDeps...)
877 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800878
Colin Cross66f78822018-05-02 12:58:28 -0700879 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800880
Colin Cross66f78822018-05-02 12:58:28 -0700881 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800882 linkFlags, linkDeps, nil, overlayRes, transitiveAssets, nil)
Colin Crossfabb6082018-02-20 17:22:23 -0800883
Michael Rosenfeld5ad15572021-12-03 13:25:10 -0800884 // Merge this import's assets with its dependencies' assets (if there are any).
885 if len(transitiveAssets) > 0 {
886 mergedAssets := android.PathForModuleOut(ctx, "merged-assets.zip")
887 inputZips := append(android.Paths{a.assetsPackage}, transitiveAssets...)
888 ctx.Build(pctx, android.BuildParams{
889 Rule: mergeAssetsRule,
890 Inputs: inputZips,
891 Output: mergedAssets,
892 Description: "merge assets from dependencies and self",
893 })
894 a.assetsPackage = mergedAssets
895 }
896
Colin Crossdcf71b22021-02-01 13:59:03 -0800897 ctx.SetProvider(JavaInfoProvider, JavaInfo{
898 HeaderJars: android.PathsIfNonNil(a.classpathFile),
899 ImplementationAndResourcesJars: android.PathsIfNonNil(a.classpathFile),
900 ImplementationJars: android.PathsIfNonNil(a.classpathFile),
901 })
Sam Delmerico82602492022-06-10 17:05:42 +0000902
903 if proptools.Bool(a.properties.Extract_jni) {
904 for _, t := range ctx.MultiTargets() {
905 arch := t.Arch.Abi[0]
906 path := android.PathForModuleOut(ctx, arch+"_jni.zip")
907 a.jniPackages = append(a.jniPackages, path)
908
909 outDir := android.PathForModuleOut(ctx, "aarForJni")
910 aarPath := android.PathForModuleSrc(ctx, a.properties.Aars[0])
911 ctx.Build(pctx, android.BuildParams{
912 Rule: extractJNI,
913 Input: aarPath,
914 Outputs: android.WritablePaths{path},
915 Description: "extract JNI from AAR",
916 Args: map[string]string{
917 "outDir": outDir.String(),
918 "archString": arch,
919 },
920 })
921 }
922
923 ctx.SetProvider(JniPackageProvider, JniPackageInfo{
924 JniPackages: a.jniPackages,
925 })
926 }
Colin Crossdcf71b22021-02-01 13:59:03 -0800927}
Colin Crossfabb6082018-02-20 17:22:23 -0800928
929func (a *AARImport) HeaderJars() android.Paths {
930 return android.Paths{a.classpathFile}
931}
932
Colin Cross331a1212018-08-15 20:40:52 -0700933func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
934 return android.Paths{a.classpathFile}
935}
936
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000937func (a *AARImport) DexJarBuildPath() android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -0800938 return nil
939}
940
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100941func (a *AARImport) DexJarInstallPath() android.Path {
942 return nil
943}
944
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100945func (a *AARImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
Jiyong Park1be96912018-05-28 18:02:19 +0900946 return nil
947}
948
Jiyong Park45bf82e2020-12-15 22:29:02 +0900949var _ android.ApexModule = (*AARImport)(nil)
950
951// Implements android.ApexModule
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900952func (a *AARImport) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
953 return a.depIsInSameApex(ctx, dep)
954}
955
Jiyong Park45bf82e2020-12-15 22:29:02 +0900956// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -0700957func (g *AARImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
958 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900959 return nil
960}
961
Sam Delmericoaf8bb702022-07-25 15:39:32 -0400962var _ android.PrebuiltInterface = (*AARImport)(nil)
Colin Crossfabb6082018-02-20 17:22:23 -0800963
Colin Cross1b16b0e2019-02-12 14:41:32 -0800964// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
965//
966// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
967// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -0800968func AARImportFactory() android.Module {
969 module := &AARImport{}
970
971 module.AddProperties(&module.properties)
972
973 android.InitPrebuiltModule(module, &module.properties.Aars)
Jooyung Hanacc7bbe2020-05-20 09:06:00 +0900974 android.InitApexModule(module)
Sam Delmerico82602492022-06-10 17:05:42 +0000975 InitJavaModuleMultiTargets(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800976 return module
977}