blob: a0ec71a3f870536269462a821a7f4d9c036c0a42 [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
Colin Crossa97c5d32018-03-28 14:58:31 -0700104
Colin Crosse560c4a2019-03-19 16:03:11 -0700105 splitNames []string
106 splits []split
107
Colin Crossa97c5d32018-03-28 14:58:31 -0700108 aaptProperties aaptProperties
109}
110
Colin Crosse560c4a2019-03-19 16:03:11 -0700111type split struct {
112 name string
113 suffix string
114 path android.Path
115}
116
Colin Crossa97c5d32018-03-28 14:58:31 -0700117func (a *aapt) ExportPackage() android.Path {
118 return a.exportPackage
119}
120
Anton Hansson53c88442019-03-18 15:53:16 +0000121func (a *aapt) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800122 return a.rroDirs
123}
124
Colin Cross90c25c62019-04-19 16:22:57 -0700125func (a *aapt) ExportedManifests() android.Paths {
126 return a.transitiveManifestPaths
Colin Crossc1c37552019-01-31 11:42:41 -0800127}
128
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800129func (a *aapt) ExportedAssets() android.OptionalPath {
130 return a.assetPackage
131}
132
Colin Crossa0ba2f52019-06-22 12:59:27 -0700133func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext sdkContext,
134 manifestPath android.Path) (compileFlags, linkFlags []string, linkDeps android.Paths,
135 resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700136
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800137 hasVersionCode := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-code")
138 hasVersionName := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-name")
Colin Crossa97c5d32018-03-28 14:58:31 -0700139
Colin Crossa97c5d32018-03-28 14:58:31 -0700140 // Flags specified in Android.bp
141 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
142
143 linkFlags = append(linkFlags, "--no-static-lib-packages")
144
145 // Find implicit or explicit asset and resource dirs
146 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
147 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
Colin Cross8a497952019-03-05 22:25:09 -0800148 resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips)
Colin Crossa97c5d32018-03-28 14:58:31 -0700149
Colin Crossa97c5d32018-03-28 14:58:31 -0700150 // Glob directories into lists of paths
151 for _, dir := range resourceDirs {
152 resDirs = append(resDirs, globbedResourceDir{
153 dir: dir,
154 files: androidResourceGlob(ctx, dir),
155 })
156 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, dir)
157 overlayDirs = append(overlayDirs, resOverlayDirs...)
158 rroDirs = append(rroDirs, resRRODirs...)
159 }
160
161 var assetFiles android.Paths
162 for _, dir := range assetDirs {
163 assetFiles = append(assetFiles, androidResourceGlob(ctx, dir)...)
164 }
165
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700166 assetDirStrings := assetDirs.Strings()
167 if a.noticeFile.Valid() {
168 assetDirStrings = append(assetDirStrings, filepath.Dir(a.noticeFile.Path().String()))
169 assetFiles = append(assetFiles, a.noticeFile.Path())
170 }
171
Colin Crossa97c5d32018-03-28 14:58:31 -0700172 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
173 linkDeps = append(linkDeps, manifestPath)
174
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700175 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirStrings, "-A "))
Colin Crossa97c5d32018-03-28 14:58:31 -0700176 linkDeps = append(linkDeps, assetFiles...)
177
Colin Crossa97c5d32018-03-28 14:58:31 -0700178 // SDK version flags
Jiyong Park6a927c42020-01-21 02:03:43 +0900179 minSdkVersion, err := sdkContext.minSdkVersion().effectiveVersionString(ctx)
180 if err != nil {
181 ctx.ModuleErrorf("invalid minSdkVersion: %s", err)
182 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700183
Colin Cross83bb3162018-06-25 15:48:06 -0700184 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
185 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700186
Colin Crossa97c5d32018-03-28 14:58:31 -0700187 // Version code
188 if !hasVersionCode {
189 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion())
190 }
191
192 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700193 var versionName string
194 if ctx.ModuleName() == "framework-res" {
195 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
196 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700197 // if it contains the build number. Use the PlatformVersionName instead.
198 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700199 } else {
200 versionName = ctx.Config().AppsDefaultVersionName()
201 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800202 versionName = proptools.NinjaEscape(versionName)
Colin Crossa97c5d32018-03-28 14:58:31 -0700203 linkFlags = append(linkFlags, "--version-name ", versionName)
204 }
205
Colin Crossa0ba2f52019-06-22 12:59:27 -0700206 linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"})
207
208 // Always set --pseudo-localize, it will be stripped out later for release
209 // builds that don't want it.
210 compileFlags = append(compileFlags, "--pseudo-localize")
211
212 return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
Colin Crossa97c5d32018-03-28 14:58:31 -0700213}
214
Paul Duffin250e6192019-06-07 10:44:37 +0100215func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) {
Colin Cross42308aa2018-11-14 21:44:17 -0800216 if sdkDep.frameworkResModule != "" {
217 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700218 }
219}
220
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800221var extractAssetsRule = pctx.AndroidStaticRule("extractAssets",
222 blueprint.RuleParams{
223 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "assets/**/*"`,
224 CommandDeps: []string{"${config.Zip2ZipCmd}"},
225 })
226
Colin Cross83bb3162018-06-25 15:48:06 -0700227func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, extraLinkFlags ...string) {
Colin Cross5446e882019-05-22 10:46:27 -0700228
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800229 transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, assetPackages, libDeps, libFlags, sdkLibraries :=
Colin Cross5446e882019-05-22 10:46:27 -0700230 aaptLibs(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700231
Colin Cross31656952018-05-24 16:11:20 -0700232 // App manifest file
233 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
234 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
235
Colin Cross5446e882019-05-22 10:46:27 -0700236 manifestPath := manifestFixer(ctx, manifestSrcPath, sdkContext, sdkLibraries,
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700237 a.isLibrary, a.useEmbeddedNativeLibs, a.usesNonSdkApis, a.useEmbeddedDex, a.hasNoCode)
Colin Cross90c25c62019-04-19 16:22:57 -0700238
Luca Stefanifd898822019-09-10 22:13:31 +0200239 // Add additional manifest files to transitive manifests.
240 additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)
241 a.transitiveManifestPaths = append(android.Paths{manifestPath}, additionalManifests...)
242 a.transitiveManifestPaths = append(a.transitiveManifestPaths, transitiveStaticLibManifests...)
Colin Cross90c25c62019-04-19 16:22:57 -0700243
Sasha Smundak541056c2019-10-28 15:50:06 -0700244 if len(a.transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) {
Luca Stefanifd898822019-09-10 22:13:31 +0200245 a.mergedManifestFile = manifestMerger(ctx, a.transitiveManifestPaths[0], a.transitiveManifestPaths[1:], a.isLibrary)
Colin Cross90c25c62019-04-19 16:22:57 -0700246 if !a.isLibrary {
247 // Only use the merged manifest for applications. For libraries, the transitive closure of manifests
248 // will be propagated to the final application and merged there. The merged manifest for libraries is
249 // only passed to Make, which can't handle transitive dependencies.
250 manifestPath = a.mergedManifestFile
251 }
252 } else {
253 a.mergedManifestFile = manifestPath
254 }
Colin Cross31656952018-05-24 16:11:20 -0700255
Colin Crossa0ba2f52019-06-22 12:59:27 -0700256 compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700257
Colin Crossc1c37552019-01-31 11:42:41 -0800258 rroDirs = append(rroDirs, staticRRODirs...)
Colin Cross31656952018-05-24 16:11:20 -0700259 linkFlags = append(linkFlags, libFlags...)
260 linkDeps = append(linkDeps, libDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700261 linkFlags = append(linkFlags, extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700262 if a.isLibrary {
263 linkFlags = append(linkFlags, "--static-lib")
264 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700265
266 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900267 // the subdir "android" is required to be filtered by package names
268 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700269 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
270 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700271 // This file isn't used by Soong, but is generated for exporting
272 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Crossa97c5d32018-03-28 14:58:31 -0700273
Colin Cross4aaa84a2018-08-21 15:14:37 -0700274 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700275 for _, dir := range resDirs {
Colin Crossa0ba2f52019-06-22 12:59:27 -0700276 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700277 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700278
Colin Crossa592e3e2019-02-19 16:59:53 -0800279 for i, zip := range resZips {
280 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
Colin Crossa0ba2f52019-06-22 12:59:27 -0700281 aapt2CompileZip(ctx, flata, zip, "", compileFlags)
Colin Crossa592e3e2019-02-19 16:59:53 -0800282 compiledResDirs = append(compiledResDirs, android.Paths{flata})
283 }
284
Colin Cross4aaa84a2018-08-21 15:14:37 -0700285 var compiledRes, compiledOverlay android.Paths
286
287 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
288
Colin Crossbec85302019-02-13 13:15:46 -0800289 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700290 // If we are using static android libraries, every source file becomes an overlay.
291 // This is to emulate old AAPT behavior which simulated library support.
292 for _, compiledResDir := range compiledResDirs {
293 compiledOverlay = append(compiledOverlay, compiledResDir...)
294 }
Colin Crossbec85302019-02-13 13:15:46 -0800295 } else if a.isLibrary {
296 // Otherwise, for a static library we treat all the resources equally with no overlay.
297 for _, compiledResDir := range compiledResDirs {
298 compiledRes = append(compiledRes, compiledResDir...)
299 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700300 } else if len(compiledResDirs) > 0 {
301 // Without static libraries, the first directory is our directory, which can then be
302 // overlaid by the rest.
303 compiledRes = append(compiledRes, compiledResDirs[0]...)
304 for _, compiledResDir := range compiledResDirs[1:] {
305 compiledOverlay = append(compiledOverlay, compiledResDir...)
306 }
307 }
308
Colin Crossa97c5d32018-03-28 14:58:31 -0700309 for _, dir := range overlayDirs {
Colin Crossa0ba2f52019-06-22 12:59:27 -0700310 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700311 }
312
Colin Crosse560c4a2019-03-19 16:03:11 -0700313 var splitPackages android.WritablePaths
314 var splits []split
315
316 for _, s := range a.splitNames {
317 suffix := strings.Replace(s, ",", "_", -1)
318 path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
319 linkFlags = append(linkFlags, "--split", path.String()+":"+s)
320 splitPackages = append(splitPackages, path)
321 splits = append(splits, split{
322 name: s,
323 suffix: suffix,
324 path: path,
325 })
326 }
327
Colin Cross66f78822018-05-02 12:58:28 -0700328 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800329 linkFlags, linkDeps, compiledRes, compiledOverlay, assetPackages, splitPackages)
330
331 // Extract assets from the resource package output so that they can be used later in aapt2link
332 // for modules that depend on this one.
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800333 if android.PrefixInList(linkFlags, "-A ") || len(assetPackages) > 0 {
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800334 assets := android.PathForModuleOut(ctx, "assets.zip")
335 ctx.Build(pctx, android.BuildParams{
336 Rule: extractAssetsRule,
337 Input: packageRes,
338 Output: assets,
339 Description: "extract assets from built resource file",
340 })
341 a.assetPackage = android.OptionalPathForPath(assets)
342 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700343
344 a.aaptSrcJar = srcJar
345 a.exportPackage = packageRes
346 a.manifestPath = manifestPath
347 a.proguardOptionsFile = proguardOptionsFile
348 a.rroDirs = rroDirs
Colin Cross66f78822018-05-02 12:58:28 -0700349 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700350 a.rTxt = rTxt
Colin Crosse560c4a2019-03-19 16:03:11 -0700351 a.splits = splits
Colin Crossa97c5d32018-03-28 14:58:31 -0700352}
353
354// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Colin Cross90c25c62019-04-19 16:22:57 -0700355func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, transitiveStaticLibManifests android.Paths,
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800356 staticRRODirs []rroDir, assets, deps android.Paths, flags []string, sdkLibraries []string) {
Colin Cross66f78822018-05-02 12:58:28 -0700357
Colin Crossa97c5d32018-03-28 14:58:31 -0700358 var sharedLibs android.Paths
359
Colin Cross83bb3162018-06-25 15:48:06 -0700360 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700361 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700362 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700363 }
364
365 ctx.VisitDirectDeps(func(module android.Module) {
366 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700367 aarDep, _ := module.(AndroidLibraryDependency)
368 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700369 exportPackage = aarDep.ExportPackage()
370 }
371
372 switch ctx.OtherModuleDependencyTag(module) {
Colin Cross4b964c02018-10-15 16:18:06 -0700373 case instrumentationForTag:
374 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Colin Cross5446e882019-05-22 10:46:27 -0700375 case libTag:
376 if exportPackage != nil {
377 sharedLibs = append(sharedLibs, exportPackage)
378 }
379
380 if _, ok := module.(SdkLibraryDependency); ok {
381 sdkLibraries = append(sdkLibraries, ctx.OtherModuleName(module))
382 }
383
384 case frameworkResTag:
Colin Crossa97c5d32018-03-28 14:58:31 -0700385 if exportPackage != nil {
386 sharedLibs = append(sharedLibs, exportPackage)
387 }
388 case staticLibTag:
389 if exportPackage != nil {
Colin Cross66f78822018-05-02 12:58:28 -0700390 transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
Colin Crossbec85302019-02-13 13:15:46 -0800391 transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
Colin Cross90c25c62019-04-19 16:22:57 -0700392 transitiveStaticLibManifests = append(transitiveStaticLibManifests, aarDep.ExportedManifests()...)
Colin Cross5446e882019-05-22 10:46:27 -0700393 sdkLibraries = append(sdkLibraries, aarDep.ExportedSdkLibs()...)
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800394 if aarDep.ExportedAssets().Valid() {
395 assets = append(assets, aarDep.ExportedAssets().Path())
396 }
Anton Hansson53c88442019-03-18 15:53:16 +0000397
398 outer:
399 for _, d := range aarDep.ExportedRRODirs() {
400 for _, e := range staticRRODirs {
401 if d.path == e.path {
402 continue outer
403 }
404 }
405 staticRRODirs = append(staticRRODirs, d)
406 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700407 }
408 }
409 })
410
411 deps = append(deps, sharedLibs...)
Colin Cross66f78822018-05-02 12:58:28 -0700412 deps = append(deps, transitiveStaticLibs...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700413
Colin Cross66f78822018-05-02 12:58:28 -0700414 if len(transitiveStaticLibs) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700415 flags = append(flags, "--auto-add-overlay")
416 }
417
418 for _, sharedLib := range sharedLibs {
419 flags = append(flags, "-I "+sharedLib.String())
420 }
421
Colin Cross66f78822018-05-02 12:58:28 -0700422 transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
Colin Cross90c25c62019-04-19 16:22:57 -0700423 transitiveStaticLibManifests = android.FirstUniquePaths(transitiveStaticLibManifests)
Colin Cross5446e882019-05-22 10:46:27 -0700424 sdkLibraries = android.FirstUniqueStrings(sdkLibraries)
Colin Cross66f78822018-05-02 12:58:28 -0700425
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800426 return transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, assets, deps, flags, sdkLibraries
Colin Crossa97c5d32018-03-28 14:58:31 -0700427}
428
429type AndroidLibrary struct {
430 Library
431 aapt
432
433 androidLibraryProperties androidLibraryProperties
434
435 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700436
437 exportedProguardFlagFiles android.Paths
Colin Cross66f78822018-05-02 12:58:28 -0700438 exportedStaticPackages android.Paths
Colin Cross89c31582018-04-30 15:55:11 -0700439}
440
441func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
442 return a.exportedProguardFlagFiles
Colin Crossa97c5d32018-03-28 14:58:31 -0700443}
444
Colin Cross66f78822018-05-02 12:58:28 -0700445func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
446 return a.exportedStaticPackages
447}
448
Colin Crossa97c5d32018-03-28 14:58:31 -0700449var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
450
451func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
452 a.Module.deps(ctx)
Paul Duffin250e6192019-06-07 10:44:37 +0100453 sdkDep := decodeSdkDep(ctx, sdkContext(a))
454 if sdkDep.hasFrameworkLibs() {
455 a.aapt.deps(ctx, sdkDep)
Colin Crossa97c5d32018-03-28 14:58:31 -0700456 }
457}
458
459func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800460 a.aapt.isLibrary = true
Colin Cross5446e882019-05-22 10:46:27 -0700461 a.aapt.sdkLibraries = a.exportedSdkLibs
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700462 a.aapt.buildActions(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700463
464 ctx.CheckbuildFile(a.proguardOptionsFile)
465 ctx.CheckbuildFile(a.exportPackage)
466 ctx.CheckbuildFile(a.aaptSrcJar)
467
468 // apps manifests are handled by aapt, don't let Module see them
469 a.properties.Manifest = nil
470
471 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
472 a.proguardOptionsFile)
473
474 a.Module.compile(ctx, a.aaptSrcJar)
475
Colin Crossf57c5782019-01-25 13:20:38 -0800476 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700477 var res android.Paths
478 if a.androidLibraryProperties.BuildAAR {
479 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
480 ctx.CheckbuildFile(a.aarFile)
481 }
Colin Cross89c31582018-04-30 15:55:11 -0700482
483 ctx.VisitDirectDeps(func(m android.Module) {
484 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
485 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
Colin Cross66f78822018-05-02 12:58:28 -0700486 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
487 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
Colin Cross89c31582018-04-30 15:55:11 -0700488 }
489 })
490
491 a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
Colin Cross66f78822018-05-02 12:58:28 -0700492 a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700493}
494
Colin Cross1b16b0e2019-02-12 14:41:32 -0800495// android_library builds and links sources into a `.jar` file for the device along with Android resources.
496//
497// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
498// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
499// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
500// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700501func AndroidLibraryFactory() android.Module {
502 module := &AndroidLibrary{}
503
504 module.AddProperties(
505 &module.Module.properties,
506 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800507 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700508 &module.Module.protoProperties,
509 &module.aaptProperties,
510 &module.androidLibraryProperties)
511
512 module.androidLibraryProperties.BuildAAR = true
513
Colin Cross48de9a42018-10-02 13:53:33 -0700514 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700515 return module
516}
517
Colin Crossfabb6082018-02-20 17:22:23 -0800518//
519// AAR (android library) prebuilts
520//
Colin Crossfabb6082018-02-20 17:22:23 -0800521
522type AARImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -0800523 Aars []string `android:"path"`
Colin Crossfabb6082018-02-20 17:22:23 -0800524
Colin Cross479884c2018-07-10 13:39:30 -0700525 Sdk_version *string
526 Min_sdk_version *string
Colin Crossa97c5d32018-03-28 14:58:31 -0700527
528 Static_libs []string
529 Libs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -0700530
531 // if set to true, run Jetifier against .aar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -0700532 Jetifier *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800533}
534
535type AARImport struct {
536 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700537 android.DefaultableModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800538 prebuilt android.Prebuilt
539
540 properties AARImportProperties
541
Colin Cross66f78822018-05-02 12:58:28 -0700542 classpathFile android.WritablePath
543 proguardFlags android.WritablePath
544 exportPackage android.WritablePath
545 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700546 manifest android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700547
548 exportedStaticPackages android.Paths
Colin Crossfabb6082018-02-20 17:22:23 -0800549}
550
Jiyong Park6a927c42020-01-21 02:03:43 +0900551func (a *AARImport) sdkVersion() sdkSpec {
552 return sdkSpecFrom(String(a.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700553}
554
Paul Duffine25c6442019-10-11 13:50:28 +0100555func (a *AARImport) systemModules() string {
556 return ""
557}
558
Jiyong Park6a927c42020-01-21 02:03:43 +0900559func (a *AARImport) minSdkVersion() sdkSpec {
Colin Cross479884c2018-07-10 13:39:30 -0700560 if a.properties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900561 return sdkSpecFrom(*a.properties.Min_sdk_version)
Colin Cross479884c2018-07-10 13:39:30 -0700562 }
Colin Cross83bb3162018-06-25 15:48:06 -0700563 return a.sdkVersion()
564}
565
Jiyong Park6a927c42020-01-21 02:03:43 +0900566func (a *AARImport) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700567 return a.sdkVersion()
568}
569
Colin Cross1e743852019-10-28 11:37:20 -0700570func (a *AARImport) javaVersion() string {
571 return ""
572}
573
Colin Crossa97c5d32018-03-28 14:58:31 -0700574var _ AndroidLibraryDependency = (*AARImport)(nil)
575
576func (a *AARImport) ExportPackage() android.Path {
577 return a.exportPackage
578}
579
Colin Cross89c31582018-04-30 15:55:11 -0700580func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
581 return android.Paths{a.proguardFlags}
582}
583
Anton Hansson53c88442019-03-18 15:53:16 +0000584func (a *AARImport) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800585 return nil
586}
587
Colin Cross66f78822018-05-02 12:58:28 -0700588func (a *AARImport) ExportedStaticPackages() android.Paths {
589 return a.exportedStaticPackages
590}
591
Colin Cross90c25c62019-04-19 16:22:57 -0700592func (a *AARImport) ExportedManifests() android.Paths {
593 return android.Paths{a.manifest}
Colin Cross31656952018-05-24 16:11:20 -0700594}
595
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800596// TODO(jungjw): Decide whether we want to implement this.
597func (a *AARImport) ExportedAssets() android.OptionalPath {
598 return android.OptionalPath{}
599}
600
Colin Crossfabb6082018-02-20 17:22:23 -0800601func (a *AARImport) Prebuilt() *android.Prebuilt {
602 return &a.prebuilt
603}
604
605func (a *AARImport) Name() string {
606 return a.prebuilt.Name(a.ModuleBase.Name())
607}
608
Jiyong Park618922e2020-01-08 13:35:43 +0900609func (a *AARImport) JacocoReportClassesFile() android.Path {
610 return nil
611}
612
Colin Crossfabb6082018-02-20 17:22:23 -0800613func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross10932872019-04-18 14:27:12 -0700614 if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Colin Cross83bb3162018-06-25 15:48:06 -0700615 sdkDep := decodeSdkDep(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700616 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700617 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800618 }
619 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700620
Colin Cross42d48b72018-08-29 14:10:52 -0700621 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
622 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800623}
624
625// 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 -0700626// 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 -0800627var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
628 blueprint.RuleParams{
Dan Willemsen304cfec2019-05-28 14:49:06 -0700629 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
630 `unzip -qo -d $outDir $in && rm -rf $outDir/res && touch $out`,
Colin Crossfabb6082018-02-20 17:22:23 -0800631 },
Dan Willemsen304cfec2019-05-28 14:49:06 -0700632 "outDir")
Colin Crossfabb6082018-02-20 17:22:23 -0800633
634func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
635 if len(a.properties.Aars) != 1 {
636 ctx.PropertyErrorf("aars", "exactly one aar is required")
637 return
638 }
639
Nan Zhang4c819fb2018-08-27 18:31:46 -0700640 aarName := ctx.ModuleName() + ".aar"
641 var aar android.Path
642 aar = android.PathForModuleSrc(ctx, a.properties.Aars[0])
Colin Cross1001a792019-03-21 22:21:39 -0700643 if Bool(a.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -0700644 inputFile := aar
645 aar = android.PathForModuleOut(ctx, "jetifier", aarName)
646 TransformJetifier(ctx, aar.(android.WritablePath), inputFile)
647 }
Colin Crossfabb6082018-02-20 17:22:23 -0800648
649 extractedAARDir := android.PathForModuleOut(ctx, "aar")
Colin Crossfabb6082018-02-20 17:22:23 -0800650 a.classpathFile = extractedAARDir.Join(ctx, "classes.jar")
651 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700652 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossfabb6082018-02-20 17:22:23 -0800653
654 ctx.Build(pctx, android.BuildParams{
655 Rule: unzipAAR,
656 Input: aar,
Colin Cross10f7c4a2018-05-23 10:59:28 -0700657 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest},
Colin Crossfabb6082018-02-20 17:22:23 -0800658 Description: "unzip AAR",
659 Args: map[string]string{
Dan Willemsen304cfec2019-05-28 14:49:06 -0700660 "outDir": extractedAARDir.String(),
Colin Crossfabb6082018-02-20 17:22:23 -0800661 },
662 })
663
Colin Crossa0ba2f52019-06-22 12:59:27 -0700664 // Always set --pseudo-localize, it will be stripped out later for release
665 // builds that don't want it.
666 compileFlags := []string{"--pseudo-localize"}
Colin Crossfabb6082018-02-20 17:22:23 -0800667 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
Colin Crossfabb6082018-02-20 17:22:23 -0800668 flata := compiledResDir.Join(ctx, "gen_res.flata")
Colin Crossa0ba2f52019-06-22 12:59:27 -0700669 aapt2CompileZip(ctx, flata, aar, "res", compileFlags)
Colin Crossfabb6082018-02-20 17:22:23 -0800670
671 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900672 // the subdir "android" is required to be filtered by package names
673 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossfabb6082018-02-20 17:22:23 -0800674 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700675 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700676 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800677
678 var linkDeps android.Paths
679
680 linkFlags := []string{
681 "--static-lib",
682 "--no-static-lib-packages",
683 "--auto-add-overlay",
684 }
685
Colin Cross10f7c4a2018-05-23 10:59:28 -0700686 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
687 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800688
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800689 transitiveStaticLibs, staticLibManifests, staticRRODirs, transitiveAssets, libDeps, libFlags, sdkLibraries :=
Colin Cross5446e882019-05-22 10:46:27 -0700690 aaptLibs(ctx, sdkContext(a))
Colin Cross31656952018-05-24 16:11:20 -0700691
692 _ = staticLibManifests
Colin Crossc1c37552019-01-31 11:42:41 -0800693 _ = staticRRODirs
Colin Cross5446e882019-05-22 10:46:27 -0700694 _ = sdkLibraries
Colin Crossfabb6082018-02-20 17:22:23 -0800695
Colin Crossa97c5d32018-03-28 14:58:31 -0700696 linkDeps = append(linkDeps, libDeps...)
697 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800698
Colin Cross66f78822018-05-02 12:58:28 -0700699 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800700
Colin Cross66f78822018-05-02 12:58:28 -0700701 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800702 linkFlags, linkDeps, nil, overlayRes, transitiveAssets, nil)
Colin Crossfabb6082018-02-20 17:22:23 -0800703}
704
705var _ Dependency = (*AARImport)(nil)
706
707func (a *AARImport) HeaderJars() android.Paths {
708 return android.Paths{a.classpathFile}
709}
710
711func (a *AARImport) ImplementationJars() android.Paths {
712 return android.Paths{a.classpathFile}
713}
714
Colin Cross331a1212018-08-15 20:40:52 -0700715func (a *AARImport) ResourceJars() android.Paths {
716 return nil
717}
718
719func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
720 return android.Paths{a.classpathFile}
721}
722
Colin Crossf24a22a2019-01-31 14:12:44 -0800723func (a *AARImport) DexJar() android.Path {
724 return nil
725}
726
Colin Crossfabb6082018-02-20 17:22:23 -0800727func (a *AARImport) AidlIncludeDirs() android.Paths {
728 return nil
729}
730
Jiyong Park1be96912018-05-28 18:02:19 +0900731func (a *AARImport) ExportedSdkLibs() []string {
732 return nil
733}
734
Artur Satayev9cf46692019-11-26 18:08:34 +0000735func (d *AARImport) ExportedPlugins() (android.Paths, []string) {
736 return nil, nil
737}
738
Colin Cross0c4ce212019-05-03 15:28:19 -0700739func (a *AARImport) SrcJarArgs() ([]string, android.Paths) {
740 return nil, nil
741}
742
Colin Crossfabb6082018-02-20 17:22:23 -0800743var _ android.PrebuiltInterface = (*Import)(nil)
744
Colin Cross1b16b0e2019-02-12 14:41:32 -0800745// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
746//
747// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
748// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -0800749func AARImportFactory() android.Module {
750 module := &AARImport{}
751
752 module.AddProperties(&module.properties)
753
754 android.InitPrebuiltModule(module, &module.properties.Aars)
Colin Cross48de9a42018-10-02 13:53:33 -0700755 InitJavaModule(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800756 return module
757}