blob: 05e66250142adf62d5e7a94ea0b558a8ac8b28f9 [file] [log] [blame]
Colin Crossfabb6082018-02-20 17:22:23 -08001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Colin Crossa592e3e2019-02-19 16:59:53 -080018 "fmt"
Jaewoong Jung5b425e22019-06-17 17:40:56 -070019 "path/filepath"
Colin Crossa97c5d32018-03-28 14:58:31 -070020 "strings"
Colin Crossfabb6082018-02-20 17:22:23 -080021
Jaewoong Jung9befb0c2020-01-18 10:33:43 -080022 "android/soong/android"
23
Colin Crossfabb6082018-02-20 17:22:23 -080024 "github.com/google/blueprint"
Colin Crossa97c5d32018-03-28 14:58:31 -070025 "github.com/google/blueprint/proptools"
Colin Crossfabb6082018-02-20 17:22:23 -080026)
27
Colin Crossa97c5d32018-03-28 14:58:31 -070028type AndroidLibraryDependency interface {
29 Dependency
30 ExportPackage() android.Path
Colin Cross89c31582018-04-30 15:55:11 -070031 ExportedProguardFlagFiles() android.Paths
Anton Hansson53c88442019-03-18 15:53:16 +000032 ExportedRRODirs() []rroDir
Colin Cross66f78822018-05-02 12:58:28 -070033 ExportedStaticPackages() android.Paths
Colin Cross90c25c62019-04-19 16:22:57 -070034 ExportedManifests() android.Paths
Jaewoong Jung6431ca72020-01-15 14:15:10 -080035 ExportedAssets() android.OptionalPath
Colin Crossa97c5d32018-03-28 14:58:31 -070036}
37
38func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000039 RegisterAARBuildComponents(android.InitRegistrationContext)
40}
41
42func RegisterAARBuildComponents(ctx android.RegistrationContext) {
43 ctx.RegisterModuleType("android_library_import", AARImportFactory)
44 ctx.RegisterModuleType("android_library", AndroidLibraryFactory)
Colin Crossa97c5d32018-03-28 14:58:31 -070045}
46
47//
48// AAR (android library)
49//
50
51type androidLibraryProperties struct {
52 BuildAAR bool `blueprint:"mutated"`
53}
54
55type aaptProperties struct {
56 // flags passed to aapt when creating the apk
57 Aaptflags []string
58
Dan Willemsen72be5902018-10-24 20:24:57 -070059 // include all resource configurations, not just the product-configured
60 // ones.
61 Aapt_include_all_resources *bool
62
Colin Crossa97c5d32018-03-28 14:58:31 -070063 // list of directories relative to the Blueprints file containing assets.
Colin Cross0ddae7f2019-02-07 15:30:01 -080064 // Defaults to ["assets"] if a directory called assets exists. Set to []
65 // to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070066 Asset_dirs []string
67
68 // list of directories relative to the Blueprints file containing
Colin Cross0ddae7f2019-02-07 15:30:01 -080069 // Android resources. Defaults to ["res"] if a directory called res exists.
70 // Set to [] to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070071 Resource_dirs []string
72
Colin Crossa592e3e2019-02-19 16:59:53 -080073 // list of zip files containing Android resources.
Colin Cross27b922f2019-03-04 22:35:41 -080074 Resource_zips []string `android:"path"`
Colin Crossa592e3e2019-02-19 16:59:53 -080075
Colin Crossa97c5d32018-03-28 14:58:31 -070076 // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml".
Colin Cross27b922f2019-03-04 22:35:41 -080077 Manifest *string `android:"path"`
changho.shinb5432b72019-08-08 18:37:17 +090078
79 // paths to additional manifest files to merge with main manifest.
80 Additional_manifests []string `android:"path"`
Sasha Smundak541056c2019-10-28 15:50:06 -070081
82 // do not include AndroidManifest from dependent libraries
83 Dont_merge_manifests *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070084}
85
86type aapt struct {
Colin Cross90c25c62019-04-19 16:22:57 -070087 aaptSrcJar android.Path
88 exportPackage android.Path
89 manifestPath android.Path
90 transitiveManifestPaths android.Paths
91 proguardOptionsFile android.Path
92 rroDirs []rroDir
93 rTxt android.Path
94 extraAaptPackagesFile android.Path
95 mergedManifestFile android.Path
Jaewoong Jung5b425e22019-06-17 17:40:56 -070096 noticeFile android.OptionalPath
Jaewoong Jung6431ca72020-01-15 14:15:10 -080097 assetPackage android.OptionalPath
Colin Cross90c25c62019-04-19 16:22:57 -070098 isLibrary bool
Sasha Smundak6ad77252019-05-01 13:16:22 -070099 useEmbeddedNativeLibs bool
Colin Cross90c25c62019-04-19 16:22:57 -0700100 useEmbeddedDex bool
101 usesNonSdkApis bool
Colin Cross5446e882019-05-22 10:46:27 -0700102 sdkLibraries []string
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700103 hasNoCode bool
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800104 LoggingParent string
Colin Crossa97c5d32018-03-28 14:58:31 -0700105
Colin Crosse560c4a2019-03-19 16:03:11 -0700106 splitNames []string
107 splits []split
108
Colin Crossa97c5d32018-03-28 14:58:31 -0700109 aaptProperties aaptProperties
110}
111
Colin Crosse560c4a2019-03-19 16:03:11 -0700112type split struct {
113 name string
114 suffix string
115 path android.Path
116}
117
Colin Crossa97c5d32018-03-28 14:58:31 -0700118func (a *aapt) ExportPackage() android.Path {
119 return a.exportPackage
120}
121
Anton Hansson53c88442019-03-18 15:53:16 +0000122func (a *aapt) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800123 return a.rroDirs
124}
125
Colin Cross90c25c62019-04-19 16:22:57 -0700126func (a *aapt) ExportedManifests() android.Paths {
127 return a.transitiveManifestPaths
Colin Crossc1c37552019-01-31 11:42:41 -0800128}
129
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800130func (a *aapt) ExportedAssets() android.OptionalPath {
131 return a.assetPackage
132}
133
Colin Crossa0ba2f52019-06-22 12:59:27 -0700134func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext sdkContext,
135 manifestPath android.Path) (compileFlags, linkFlags []string, linkDeps android.Paths,
136 resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700137
138 hasVersionCode := false
139 hasVersionName := false
Colin Crossa97c5d32018-03-28 14:58:31 -0700140 for _, f := range a.aaptProperties.Aaptflags {
141 if strings.HasPrefix(f, "--version-code") {
142 hasVersionCode = true
143 } else if strings.HasPrefix(f, "--version-name") {
144 hasVersionName = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700145 }
146 }
147
Colin Crossa97c5d32018-03-28 14:58:31 -0700148 // Flags specified in Android.bp
149 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
150
151 linkFlags = append(linkFlags, "--no-static-lib-packages")
152
153 // Find implicit or explicit asset and resource dirs
154 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
155 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
Colin Cross8a497952019-03-05 22:25:09 -0800156 resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips)
Colin Crossa97c5d32018-03-28 14:58:31 -0700157
Colin Crossa97c5d32018-03-28 14:58:31 -0700158 // Glob directories into lists of paths
159 for _, dir := range resourceDirs {
160 resDirs = append(resDirs, globbedResourceDir{
161 dir: dir,
162 files: androidResourceGlob(ctx, dir),
163 })
164 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, dir)
165 overlayDirs = append(overlayDirs, resOverlayDirs...)
166 rroDirs = append(rroDirs, resRRODirs...)
167 }
168
169 var assetFiles android.Paths
170 for _, dir := range assetDirs {
171 assetFiles = append(assetFiles, androidResourceGlob(ctx, dir)...)
172 }
173
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700174 assetDirStrings := assetDirs.Strings()
175 if a.noticeFile.Valid() {
176 assetDirStrings = append(assetDirStrings, filepath.Dir(a.noticeFile.Path().String()))
177 assetFiles = append(assetFiles, a.noticeFile.Path())
178 }
179
Colin Crossa97c5d32018-03-28 14:58:31 -0700180 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
181 linkDeps = append(linkDeps, manifestPath)
182
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700183 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirStrings, "-A "))
Colin Crossa97c5d32018-03-28 14:58:31 -0700184 linkDeps = append(linkDeps, assetFiles...)
185
Colin Crossa97c5d32018-03-28 14:58:31 -0700186 // SDK version flags
Jiyong Park6a927c42020-01-21 02:03:43 +0900187 minSdkVersion, err := sdkContext.minSdkVersion().effectiveVersionString(ctx)
188 if err != nil {
189 ctx.ModuleErrorf("invalid minSdkVersion: %s", err)
190 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700191
Colin Cross83bb3162018-06-25 15:48:06 -0700192 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
193 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700194
Colin Crossa97c5d32018-03-28 14:58:31 -0700195 // Version code
196 if !hasVersionCode {
197 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion())
198 }
199
200 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700201 var versionName string
202 if ctx.ModuleName() == "framework-res" {
203 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
204 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700205 // if it contains the build number. Use the PlatformVersionName instead.
206 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700207 } else {
208 versionName = ctx.Config().AppsDefaultVersionName()
209 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800210 versionName = proptools.NinjaEscape(versionName)
Colin Crossa97c5d32018-03-28 14:58:31 -0700211 linkFlags = append(linkFlags, "--version-name ", versionName)
212 }
213
Colin Crossa0ba2f52019-06-22 12:59:27 -0700214 linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"})
215
216 // Always set --pseudo-localize, it will be stripped out later for release
217 // builds that don't want it.
218 compileFlags = append(compileFlags, "--pseudo-localize")
219
220 return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
Colin Crossa97c5d32018-03-28 14:58:31 -0700221}
222
Paul Duffin250e6192019-06-07 10:44:37 +0100223func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) {
Colin Cross42308aa2018-11-14 21:44:17 -0800224 if sdkDep.frameworkResModule != "" {
225 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700226 }
227}
228
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800229var extractAssetsRule = pctx.AndroidStaticRule("extractAssets",
230 blueprint.RuleParams{
231 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "assets/**/*"`,
232 CommandDeps: []string{"${config.Zip2ZipCmd}"},
233 })
234
Colin Cross83bb3162018-06-25 15:48:06 -0700235func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, extraLinkFlags ...string) {
Colin Cross5446e882019-05-22 10:46:27 -0700236
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800237 transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, assetPackages, libDeps, libFlags, sdkLibraries :=
Colin Cross5446e882019-05-22 10:46:27 -0700238 aaptLibs(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700239
Colin Cross31656952018-05-24 16:11:20 -0700240 // App manifest file
241 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
242 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
243
Colin Cross5446e882019-05-22 10:46:27 -0700244 manifestPath := manifestFixer(ctx, manifestSrcPath, sdkContext, sdkLibraries,
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800245 a.isLibrary, a.useEmbeddedNativeLibs, a.usesNonSdkApis, a.useEmbeddedDex, a.hasNoCode,
246 a.LoggingParent)
Colin Cross90c25c62019-04-19 16:22:57 -0700247
Luca Stefanifd898822019-09-10 22:13:31 +0200248 // Add additional manifest files to transitive manifests.
249 additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)
250 a.transitiveManifestPaths = append(android.Paths{manifestPath}, additionalManifests...)
251 a.transitiveManifestPaths = append(a.transitiveManifestPaths, transitiveStaticLibManifests...)
Colin Cross90c25c62019-04-19 16:22:57 -0700252
Sasha Smundak541056c2019-10-28 15:50:06 -0700253 if len(a.transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) {
Luca Stefanifd898822019-09-10 22:13:31 +0200254 a.mergedManifestFile = manifestMerger(ctx, a.transitiveManifestPaths[0], a.transitiveManifestPaths[1:], a.isLibrary)
Colin Cross90c25c62019-04-19 16:22:57 -0700255 if !a.isLibrary {
256 // Only use the merged manifest for applications. For libraries, the transitive closure of manifests
257 // will be propagated to the final application and merged there. The merged manifest for libraries is
258 // only passed to Make, which can't handle transitive dependencies.
259 manifestPath = a.mergedManifestFile
260 }
261 } else {
262 a.mergedManifestFile = manifestPath
263 }
Colin Cross31656952018-05-24 16:11:20 -0700264
Colin Crossa0ba2f52019-06-22 12:59:27 -0700265 compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700266
Colin Crossc1c37552019-01-31 11:42:41 -0800267 rroDirs = append(rroDirs, staticRRODirs...)
Colin Cross31656952018-05-24 16:11:20 -0700268 linkFlags = append(linkFlags, libFlags...)
269 linkDeps = append(linkDeps, libDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700270 linkFlags = append(linkFlags, extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700271 if a.isLibrary {
272 linkFlags = append(linkFlags, "--static-lib")
273 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700274
275 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900276 // the subdir "android" is required to be filtered by package names
277 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700278 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
279 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700280 // This file isn't used by Soong, but is generated for exporting
281 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Crossa97c5d32018-03-28 14:58:31 -0700282
Colin Cross4aaa84a2018-08-21 15:14:37 -0700283 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700284 for _, dir := range resDirs {
Colin Crossa0ba2f52019-06-22 12:59:27 -0700285 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700286 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700287
Colin Crossa592e3e2019-02-19 16:59:53 -0800288 for i, zip := range resZips {
289 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
Colin Crossa0ba2f52019-06-22 12:59:27 -0700290 aapt2CompileZip(ctx, flata, zip, "", compileFlags)
Colin Crossa592e3e2019-02-19 16:59:53 -0800291 compiledResDirs = append(compiledResDirs, android.Paths{flata})
292 }
293
Colin Cross4aaa84a2018-08-21 15:14:37 -0700294 var compiledRes, compiledOverlay android.Paths
295
296 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
297
Colin Crossbec85302019-02-13 13:15:46 -0800298 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700299 // If we are using static android libraries, every source file becomes an overlay.
300 // This is to emulate old AAPT behavior which simulated library support.
301 for _, compiledResDir := range compiledResDirs {
302 compiledOverlay = append(compiledOverlay, compiledResDir...)
303 }
Colin Crossbec85302019-02-13 13:15:46 -0800304 } else if a.isLibrary {
305 // Otherwise, for a static library we treat all the resources equally with no overlay.
306 for _, compiledResDir := range compiledResDirs {
307 compiledRes = append(compiledRes, compiledResDir...)
308 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700309 } else if len(compiledResDirs) > 0 {
310 // Without static libraries, the first directory is our directory, which can then be
311 // overlaid by the rest.
312 compiledRes = append(compiledRes, compiledResDirs[0]...)
313 for _, compiledResDir := range compiledResDirs[1:] {
314 compiledOverlay = append(compiledOverlay, compiledResDir...)
315 }
316 }
317
Colin Crossa97c5d32018-03-28 14:58:31 -0700318 for _, dir := range overlayDirs {
Colin Crossa0ba2f52019-06-22 12:59:27 -0700319 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700320 }
321
Colin Crosse560c4a2019-03-19 16:03:11 -0700322 var splitPackages android.WritablePaths
323 var splits []split
324
325 for _, s := range a.splitNames {
326 suffix := strings.Replace(s, ",", "_", -1)
327 path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
328 linkFlags = append(linkFlags, "--split", path.String()+":"+s)
329 splitPackages = append(splitPackages, path)
330 splits = append(splits, split{
331 name: s,
332 suffix: suffix,
333 path: path,
334 })
335 }
336
Colin Cross66f78822018-05-02 12:58:28 -0700337 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800338 linkFlags, linkDeps, compiledRes, compiledOverlay, assetPackages, splitPackages)
339
340 // Extract assets from the resource package output so that they can be used later in aapt2link
341 // for modules that depend on this one.
342 if android.PrefixedStringInList(linkFlags, "-A ") || len(assetPackages) > 0 {
343 assets := android.PathForModuleOut(ctx, "assets.zip")
344 ctx.Build(pctx, android.BuildParams{
345 Rule: extractAssetsRule,
346 Input: packageRes,
347 Output: assets,
348 Description: "extract assets from built resource file",
349 })
350 a.assetPackage = android.OptionalPathForPath(assets)
351 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700352
353 a.aaptSrcJar = srcJar
354 a.exportPackage = packageRes
355 a.manifestPath = manifestPath
356 a.proguardOptionsFile = proguardOptionsFile
357 a.rroDirs = rroDirs
Colin Cross66f78822018-05-02 12:58:28 -0700358 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700359 a.rTxt = rTxt
Colin Crosse560c4a2019-03-19 16:03:11 -0700360 a.splits = splits
Colin Crossa97c5d32018-03-28 14:58:31 -0700361}
362
363// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Colin Cross90c25c62019-04-19 16:22:57 -0700364func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, transitiveStaticLibManifests android.Paths,
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800365 staticRRODirs []rroDir, assets, deps android.Paths, flags []string, sdkLibraries []string) {
Colin Cross66f78822018-05-02 12:58:28 -0700366
Colin Crossa97c5d32018-03-28 14:58:31 -0700367 var sharedLibs android.Paths
368
Colin Cross83bb3162018-06-25 15:48:06 -0700369 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700370 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700371 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700372 }
373
374 ctx.VisitDirectDeps(func(module android.Module) {
375 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700376 aarDep, _ := module.(AndroidLibraryDependency)
377 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700378 exportPackage = aarDep.ExportPackage()
379 }
380
381 switch ctx.OtherModuleDependencyTag(module) {
Colin Cross4b964c02018-10-15 16:18:06 -0700382 case instrumentationForTag:
383 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Colin Cross5446e882019-05-22 10:46:27 -0700384 case libTag:
385 if exportPackage != nil {
386 sharedLibs = append(sharedLibs, exportPackage)
387 }
388
389 if _, ok := module.(SdkLibraryDependency); ok {
390 sdkLibraries = append(sdkLibraries, ctx.OtherModuleName(module))
391 }
392
393 case frameworkResTag:
Colin Crossa97c5d32018-03-28 14:58:31 -0700394 if exportPackage != nil {
395 sharedLibs = append(sharedLibs, exportPackage)
396 }
397 case staticLibTag:
398 if exportPackage != nil {
Colin Cross66f78822018-05-02 12:58:28 -0700399 transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
Colin Crossbec85302019-02-13 13:15:46 -0800400 transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
Colin Cross90c25c62019-04-19 16:22:57 -0700401 transitiveStaticLibManifests = append(transitiveStaticLibManifests, aarDep.ExportedManifests()...)
Colin Cross5446e882019-05-22 10:46:27 -0700402 sdkLibraries = append(sdkLibraries, aarDep.ExportedSdkLibs()...)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800403 if aarDep.ExportedAssets().Valid() {
404 assets = append(assets, aarDep.ExportedAssets().Path())
405 }
Anton Hansson53c88442019-03-18 15:53:16 +0000406
407 outer:
408 for _, d := range aarDep.ExportedRRODirs() {
409 for _, e := range staticRRODirs {
410 if d.path == e.path {
411 continue outer
412 }
413 }
414 staticRRODirs = append(staticRRODirs, d)
415 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700416 }
417 }
418 })
419
420 deps = append(deps, sharedLibs...)
Colin Cross66f78822018-05-02 12:58:28 -0700421 deps = append(deps, transitiveStaticLibs...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700422
Colin Cross66f78822018-05-02 12:58:28 -0700423 if len(transitiveStaticLibs) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700424 flags = append(flags, "--auto-add-overlay")
425 }
426
427 for _, sharedLib := range sharedLibs {
428 flags = append(flags, "-I "+sharedLib.String())
429 }
430
Colin Cross66f78822018-05-02 12:58:28 -0700431 transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
Colin Cross90c25c62019-04-19 16:22:57 -0700432 transitiveStaticLibManifests = android.FirstUniquePaths(transitiveStaticLibManifests)
Colin Cross5446e882019-05-22 10:46:27 -0700433 sdkLibraries = android.FirstUniqueStrings(sdkLibraries)
Colin Cross66f78822018-05-02 12:58:28 -0700434
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800435 return transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, assets, deps, flags, sdkLibraries
Colin Crossa97c5d32018-03-28 14:58:31 -0700436}
437
438type AndroidLibrary struct {
439 Library
440 aapt
441
442 androidLibraryProperties androidLibraryProperties
443
444 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700445
446 exportedProguardFlagFiles android.Paths
Colin Cross66f78822018-05-02 12:58:28 -0700447 exportedStaticPackages android.Paths
Colin Cross89c31582018-04-30 15:55:11 -0700448}
449
450func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
451 return a.exportedProguardFlagFiles
Colin Crossa97c5d32018-03-28 14:58:31 -0700452}
453
Colin Cross66f78822018-05-02 12:58:28 -0700454func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
455 return a.exportedStaticPackages
456}
457
Colin Crossa97c5d32018-03-28 14:58:31 -0700458var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
459
460func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
461 a.Module.deps(ctx)
Paul Duffin250e6192019-06-07 10:44:37 +0100462 sdkDep := decodeSdkDep(ctx, sdkContext(a))
463 if sdkDep.hasFrameworkLibs() {
464 a.aapt.deps(ctx, sdkDep)
Colin Crossa97c5d32018-03-28 14:58:31 -0700465 }
466}
467
468func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800469 a.aapt.isLibrary = true
Colin Cross5446e882019-05-22 10:46:27 -0700470 a.aapt.sdkLibraries = a.exportedSdkLibs
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700471 a.aapt.buildActions(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700472
473 ctx.CheckbuildFile(a.proguardOptionsFile)
474 ctx.CheckbuildFile(a.exportPackage)
475 ctx.CheckbuildFile(a.aaptSrcJar)
476
477 // apps manifests are handled by aapt, don't let Module see them
478 a.properties.Manifest = nil
479
480 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
481 a.proguardOptionsFile)
482
483 a.Module.compile(ctx, a.aaptSrcJar)
484
Colin Crossf57c5782019-01-25 13:20:38 -0800485 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700486 var res android.Paths
487 if a.androidLibraryProperties.BuildAAR {
488 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
489 ctx.CheckbuildFile(a.aarFile)
490 }
Colin Cross89c31582018-04-30 15:55:11 -0700491
492 ctx.VisitDirectDeps(func(m android.Module) {
493 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
494 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
Colin Cross66f78822018-05-02 12:58:28 -0700495 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
496 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
Colin Cross89c31582018-04-30 15:55:11 -0700497 }
498 })
499
500 a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
Colin Cross66f78822018-05-02 12:58:28 -0700501 a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700502}
503
Colin Cross1b16b0e2019-02-12 14:41:32 -0800504// android_library builds and links sources into a `.jar` file for the device along with Android resources.
505//
506// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
507// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
508// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
509// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700510func AndroidLibraryFactory() android.Module {
511 module := &AndroidLibrary{}
512
513 module.AddProperties(
514 &module.Module.properties,
515 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800516 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700517 &module.Module.protoProperties,
518 &module.aaptProperties,
519 &module.androidLibraryProperties)
520
521 module.androidLibraryProperties.BuildAAR = true
522
Colin Cross48de9a42018-10-02 13:53:33 -0700523 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700524 return module
525}
526
Colin Crossfabb6082018-02-20 17:22:23 -0800527//
528// AAR (android library) prebuilts
529//
Colin Crossfabb6082018-02-20 17:22:23 -0800530
531type AARImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -0800532 Aars []string `android:"path"`
Colin Crossfabb6082018-02-20 17:22:23 -0800533
Colin Cross479884c2018-07-10 13:39:30 -0700534 Sdk_version *string
535 Min_sdk_version *string
Colin Crossa97c5d32018-03-28 14:58:31 -0700536
537 Static_libs []string
538 Libs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -0700539
540 // if set to true, run Jetifier against .aar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -0700541 Jetifier *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800542}
543
544type AARImport struct {
545 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700546 android.DefaultableModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800547 prebuilt android.Prebuilt
548
549 properties AARImportProperties
550
Colin Cross66f78822018-05-02 12:58:28 -0700551 classpathFile android.WritablePath
552 proguardFlags android.WritablePath
553 exportPackage android.WritablePath
554 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700555 manifest android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700556
557 exportedStaticPackages android.Paths
Colin Crossfabb6082018-02-20 17:22:23 -0800558}
559
Jiyong Park6a927c42020-01-21 02:03:43 +0900560func (a *AARImport) sdkVersion() sdkSpec {
561 return sdkSpecFrom(String(a.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700562}
563
Paul Duffine25c6442019-10-11 13:50:28 +0100564func (a *AARImport) systemModules() string {
565 return ""
566}
567
Jiyong Park6a927c42020-01-21 02:03:43 +0900568func (a *AARImport) minSdkVersion() sdkSpec {
Colin Cross479884c2018-07-10 13:39:30 -0700569 if a.properties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900570 return sdkSpecFrom(*a.properties.Min_sdk_version)
Colin Cross479884c2018-07-10 13:39:30 -0700571 }
Colin Cross83bb3162018-06-25 15:48:06 -0700572 return a.sdkVersion()
573}
574
Jiyong Park6a927c42020-01-21 02:03:43 +0900575func (a *AARImport) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700576 return a.sdkVersion()
577}
578
Colin Cross1e743852019-10-28 11:37:20 -0700579func (a *AARImport) javaVersion() string {
580 return ""
581}
582
Colin Crossa97c5d32018-03-28 14:58:31 -0700583var _ AndroidLibraryDependency = (*AARImport)(nil)
584
585func (a *AARImport) ExportPackage() android.Path {
586 return a.exportPackage
587}
588
Colin Cross89c31582018-04-30 15:55:11 -0700589func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
590 return android.Paths{a.proguardFlags}
591}
592
Anton Hansson53c88442019-03-18 15:53:16 +0000593func (a *AARImport) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800594 return nil
595}
596
Colin Cross66f78822018-05-02 12:58:28 -0700597func (a *AARImport) ExportedStaticPackages() android.Paths {
598 return a.exportedStaticPackages
599}
600
Colin Cross90c25c62019-04-19 16:22:57 -0700601func (a *AARImport) ExportedManifests() android.Paths {
602 return android.Paths{a.manifest}
Colin Cross31656952018-05-24 16:11:20 -0700603}
604
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800605// TODO(jungjw): Decide whether we want to implement this.
606func (a *AARImport) ExportedAssets() android.OptionalPath {
607 return android.OptionalPath{}
608}
609
Colin Crossfabb6082018-02-20 17:22:23 -0800610func (a *AARImport) Prebuilt() *android.Prebuilt {
611 return &a.prebuilt
612}
613
614func (a *AARImport) Name() string {
615 return a.prebuilt.Name(a.ModuleBase.Name())
616}
617
Jiyong Park618922e2020-01-08 13:35:43 +0900618func (a *AARImport) JacocoReportClassesFile() android.Path {
619 return nil
620}
621
Colin Crossfabb6082018-02-20 17:22:23 -0800622func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross10932872019-04-18 14:27:12 -0700623 if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Colin Cross83bb3162018-06-25 15:48:06 -0700624 sdkDep := decodeSdkDep(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700625 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700626 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800627 }
628 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700629
Colin Cross42d48b72018-08-29 14:10:52 -0700630 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
631 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800632}
633
634// 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 -0700635// 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 -0800636var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
637 blueprint.RuleParams{
Dan Willemsen304cfec2019-05-28 14:49:06 -0700638 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
639 `unzip -qo -d $outDir $in && rm -rf $outDir/res && touch $out`,
Colin Crossfabb6082018-02-20 17:22:23 -0800640 },
Dan Willemsen304cfec2019-05-28 14:49:06 -0700641 "outDir")
Colin Crossfabb6082018-02-20 17:22:23 -0800642
643func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
644 if len(a.properties.Aars) != 1 {
645 ctx.PropertyErrorf("aars", "exactly one aar is required")
646 return
647 }
648
Nan Zhang4c819fb2018-08-27 18:31:46 -0700649 aarName := ctx.ModuleName() + ".aar"
650 var aar android.Path
651 aar = android.PathForModuleSrc(ctx, a.properties.Aars[0])
Colin Cross1001a792019-03-21 22:21:39 -0700652 if Bool(a.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -0700653 inputFile := aar
654 aar = android.PathForModuleOut(ctx, "jetifier", aarName)
655 TransformJetifier(ctx, aar.(android.WritablePath), inputFile)
656 }
Colin Crossfabb6082018-02-20 17:22:23 -0800657
658 extractedAARDir := android.PathForModuleOut(ctx, "aar")
Colin Crossfabb6082018-02-20 17:22:23 -0800659 a.classpathFile = extractedAARDir.Join(ctx, "classes.jar")
660 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700661 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossfabb6082018-02-20 17:22:23 -0800662
663 ctx.Build(pctx, android.BuildParams{
664 Rule: unzipAAR,
665 Input: aar,
Colin Cross10f7c4a2018-05-23 10:59:28 -0700666 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest},
Colin Crossfabb6082018-02-20 17:22:23 -0800667 Description: "unzip AAR",
668 Args: map[string]string{
Dan Willemsen304cfec2019-05-28 14:49:06 -0700669 "outDir": extractedAARDir.String(),
Colin Crossfabb6082018-02-20 17:22:23 -0800670 },
671 })
672
Colin Crossa0ba2f52019-06-22 12:59:27 -0700673 // Always set --pseudo-localize, it will be stripped out later for release
674 // builds that don't want it.
675 compileFlags := []string{"--pseudo-localize"}
Colin Crossfabb6082018-02-20 17:22:23 -0800676 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
Colin Crossfabb6082018-02-20 17:22:23 -0800677 flata := compiledResDir.Join(ctx, "gen_res.flata")
Colin Crossa0ba2f52019-06-22 12:59:27 -0700678 aapt2CompileZip(ctx, flata, aar, "res", compileFlags)
Colin Crossfabb6082018-02-20 17:22:23 -0800679
680 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900681 // the subdir "android" is required to be filtered by package names
682 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossfabb6082018-02-20 17:22:23 -0800683 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700684 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700685 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800686
687 var linkDeps android.Paths
688
689 linkFlags := []string{
690 "--static-lib",
691 "--no-static-lib-packages",
692 "--auto-add-overlay",
693 }
694
Colin Cross10f7c4a2018-05-23 10:59:28 -0700695 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
696 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800697
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800698 transitiveStaticLibs, staticLibManifests, staticRRODirs, transitiveAssets, libDeps, libFlags, sdkLibraries :=
Colin Cross5446e882019-05-22 10:46:27 -0700699 aaptLibs(ctx, sdkContext(a))
Colin Cross31656952018-05-24 16:11:20 -0700700
701 _ = staticLibManifests
Colin Crossc1c37552019-01-31 11:42:41 -0800702 _ = staticRRODirs
Colin Cross5446e882019-05-22 10:46:27 -0700703 _ = sdkLibraries
Colin Crossfabb6082018-02-20 17:22:23 -0800704
Colin Crossa97c5d32018-03-28 14:58:31 -0700705 linkDeps = append(linkDeps, libDeps...)
706 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800707
Colin Cross66f78822018-05-02 12:58:28 -0700708 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800709
Colin Cross66f78822018-05-02 12:58:28 -0700710 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800711 linkFlags, linkDeps, nil, overlayRes, transitiveAssets, nil)
Colin Crossfabb6082018-02-20 17:22:23 -0800712}
713
714var _ Dependency = (*AARImport)(nil)
715
716func (a *AARImport) HeaderJars() android.Paths {
717 return android.Paths{a.classpathFile}
718}
719
720func (a *AARImport) ImplementationJars() android.Paths {
721 return android.Paths{a.classpathFile}
722}
723
Colin Cross331a1212018-08-15 20:40:52 -0700724func (a *AARImport) ResourceJars() android.Paths {
725 return nil
726}
727
728func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
729 return android.Paths{a.classpathFile}
730}
731
Colin Crossf24a22a2019-01-31 14:12:44 -0800732func (a *AARImport) DexJar() android.Path {
733 return nil
734}
735
Colin Crossfabb6082018-02-20 17:22:23 -0800736func (a *AARImport) AidlIncludeDirs() android.Paths {
737 return nil
738}
739
Jiyong Park1be96912018-05-28 18:02:19 +0900740func (a *AARImport) ExportedSdkLibs() []string {
741 return nil
742}
743
Artur Satayev9cf46692019-11-26 18:08:34 +0000744func (d *AARImport) ExportedPlugins() (android.Paths, []string) {
745 return nil, nil
746}
747
Colin Cross0c4ce212019-05-03 15:28:19 -0700748func (a *AARImport) SrcJarArgs() ([]string, android.Paths) {
749 return nil, nil
750}
751
Colin Crossfabb6082018-02-20 17:22:23 -0800752var _ android.PrebuiltInterface = (*Import)(nil)
753
Colin Cross1b16b0e2019-02-12 14:41:32 -0800754// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
755//
756// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
757// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -0800758func AARImportFactory() android.Module {
759 module := &AARImport{}
760
761 module.AddProperties(&module.properties)
762
763 android.InitPrebuiltModule(module, &module.properties.Aars)
Colin Cross48de9a42018-10-02 13:53:33 -0700764 InitJavaModule(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800765 return module
766}