blob: 65a7c2a5e3c7ea6dcd505b54fa118c788a04664c [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 (
18 "android/soong/android"
Colin Crossa592e3e2019-02-19 16:59:53 -080019 "fmt"
Colin Crossa97c5d32018-03-28 14:58:31 -070020 "strings"
Colin Crossfabb6082018-02-20 17:22:23 -080021
22 "github.com/google/blueprint"
Colin Crossa97c5d32018-03-28 14:58:31 -070023 "github.com/google/blueprint/proptools"
Colin Crossfabb6082018-02-20 17:22:23 -080024)
25
Colin Crossa97c5d32018-03-28 14:58:31 -070026type AndroidLibraryDependency interface {
27 Dependency
28 ExportPackage() android.Path
Colin Cross89c31582018-04-30 15:55:11 -070029 ExportedProguardFlagFiles() android.Paths
Anton Hansson53c88442019-03-18 15:53:16 +000030 ExportedRRODirs() []rroDir
Colin Cross66f78822018-05-02 12:58:28 -070031 ExportedStaticPackages() android.Paths
Colin Cross90c25c62019-04-19 16:22:57 -070032 ExportedManifests() android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -070033}
34
35func init() {
36 android.RegisterModuleType("android_library_import", AARImportFactory)
37 android.RegisterModuleType("android_library", AndroidLibraryFactory)
38}
39
40//
41// AAR (android library)
42//
43
44type androidLibraryProperties struct {
45 BuildAAR bool `blueprint:"mutated"`
46}
47
48type aaptProperties struct {
49 // flags passed to aapt when creating the apk
50 Aaptflags []string
51
Dan Willemsen72be5902018-10-24 20:24:57 -070052 // include all resource configurations, not just the product-configured
53 // ones.
54 Aapt_include_all_resources *bool
55
Colin Crossa97c5d32018-03-28 14:58:31 -070056 // list of directories relative to the Blueprints file containing assets.
Colin Cross0ddae7f2019-02-07 15:30:01 -080057 // Defaults to ["assets"] if a directory called assets exists. Set to []
58 // to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070059 Asset_dirs []string
60
61 // list of directories relative to the Blueprints file containing
Colin Cross0ddae7f2019-02-07 15:30:01 -080062 // Android resources. Defaults to ["res"] if a directory called res exists.
63 // Set to [] to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070064 Resource_dirs []string
65
Colin Crossa592e3e2019-02-19 16:59:53 -080066 // list of zip files containing Android resources.
Colin Cross27b922f2019-03-04 22:35:41 -080067 Resource_zips []string `android:"path"`
Colin Crossa592e3e2019-02-19 16:59:53 -080068
Colin Crossa97c5d32018-03-28 14:58:31 -070069 // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml".
Colin Cross27b922f2019-03-04 22:35:41 -080070 Manifest *string `android:"path"`
Colin Crossa97c5d32018-03-28 14:58:31 -070071}
72
73type aapt struct {
Colin Cross90c25c62019-04-19 16:22:57 -070074 aaptSrcJar android.Path
75 exportPackage android.Path
76 manifestPath android.Path
77 transitiveManifestPaths android.Paths
78 proguardOptionsFile android.Path
79 rroDirs []rroDir
80 rTxt android.Path
81 extraAaptPackagesFile android.Path
82 mergedManifestFile android.Path
83 isLibrary bool
Sasha Smundak6ad77252019-05-01 13:16:22 -070084 useEmbeddedNativeLibs bool
Colin Cross90c25c62019-04-19 16:22:57 -070085 useEmbeddedDex bool
86 usesNonSdkApis bool
Colin Cross5446e882019-05-22 10:46:27 -070087 sdkLibraries []string
Colin Crossa97c5d32018-03-28 14:58:31 -070088
Colin Crosse560c4a2019-03-19 16:03:11 -070089 splitNames []string
90 splits []split
91
Colin Crossa97c5d32018-03-28 14:58:31 -070092 aaptProperties aaptProperties
93}
94
Colin Crosse560c4a2019-03-19 16:03:11 -070095type split struct {
96 name string
97 suffix string
98 path android.Path
99}
100
Colin Crossa97c5d32018-03-28 14:58:31 -0700101func (a *aapt) ExportPackage() android.Path {
102 return a.exportPackage
103}
104
Anton Hansson53c88442019-03-18 15:53:16 +0000105func (a *aapt) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800106 return a.rroDirs
107}
108
Colin Cross90c25c62019-04-19 16:22:57 -0700109func (a *aapt) ExportedManifests() android.Paths {
110 return a.transitiveManifestPaths
Colin Crossc1c37552019-01-31 11:42:41 -0800111}
112
Colin Cross31656952018-05-24 16:11:20 -0700113func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext sdkContext, manifestPath android.Path) (flags []string,
Anton Hansson53c88442019-03-18 15:53:16 +0000114 deps android.Paths, resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700115
116 hasVersionCode := false
117 hasVersionName := false
Colin Crossa97c5d32018-03-28 14:58:31 -0700118 for _, f := range a.aaptProperties.Aaptflags {
119 if strings.HasPrefix(f, "--version-code") {
120 hasVersionCode = true
121 } else if strings.HasPrefix(f, "--version-name") {
122 hasVersionName = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700123 }
124 }
125
126 var linkFlags []string
127
128 // Flags specified in Android.bp
129 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
130
131 linkFlags = append(linkFlags, "--no-static-lib-packages")
132
133 // Find implicit or explicit asset and resource dirs
134 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
135 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
Colin Cross8a497952019-03-05 22:25:09 -0800136 resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips)
Colin Crossa97c5d32018-03-28 14:58:31 -0700137
138 var linkDeps android.Paths
139
140 // Glob directories into lists of paths
141 for _, dir := range resourceDirs {
142 resDirs = append(resDirs, globbedResourceDir{
143 dir: dir,
144 files: androidResourceGlob(ctx, dir),
145 })
146 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, dir)
147 overlayDirs = append(overlayDirs, resOverlayDirs...)
148 rroDirs = append(rroDirs, resRRODirs...)
149 }
150
151 var assetFiles android.Paths
152 for _, dir := range assetDirs {
153 assetFiles = append(assetFiles, androidResourceGlob(ctx, dir)...)
154 }
155
Colin Crossa97c5d32018-03-28 14:58:31 -0700156 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
157 linkDeps = append(linkDeps, manifestPath)
158
159 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirs.Strings(), "-A "))
160 linkDeps = append(linkDeps, assetFiles...)
161
Colin Crossa97c5d32018-03-28 14:58:31 -0700162 // SDK version flags
Colin Cross83bb3162018-06-25 15:48:06 -0700163 minSdkVersion := sdkVersionOrDefault(ctx, sdkContext.minSdkVersion())
Colin Crossa97c5d32018-03-28 14:58:31 -0700164
Colin Cross83bb3162018-06-25 15:48:06 -0700165 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
166 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700167
Colin Crossa97c5d32018-03-28 14:58:31 -0700168 // Version code
169 if !hasVersionCode {
170 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion())
171 }
172
173 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700174 var versionName string
175 if ctx.ModuleName() == "framework-res" {
176 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
177 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700178 // if it contains the build number. Use the PlatformVersionName instead.
179 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700180 } else {
181 versionName = ctx.Config().AppsDefaultVersionName()
182 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800183 versionName = proptools.NinjaEscape(versionName)
Colin Crossa97c5d32018-03-28 14:58:31 -0700184 linkFlags = append(linkFlags, "--version-name ", versionName)
185 }
186
Colin Crossa592e3e2019-02-19 16:59:53 -0800187 return linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
Colin Crossa97c5d32018-03-28 14:58:31 -0700188}
189
Colin Cross83bb3162018-06-25 15:48:06 -0700190func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkContext sdkContext) {
Colin Cross42308aa2018-11-14 21:44:17 -0800191 sdkDep := decodeSdkDep(ctx, sdkContext)
192 if sdkDep.frameworkResModule != "" {
193 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700194 }
195}
196
Colin Cross83bb3162018-06-25 15:48:06 -0700197func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, extraLinkFlags ...string) {
Colin Cross5446e882019-05-22 10:46:27 -0700198
199 transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, libDeps, libFlags, sdkLibraries :=
200 aaptLibs(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700201
Colin Cross31656952018-05-24 16:11:20 -0700202 // App manifest file
203 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
204 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
205
Colin Cross5446e882019-05-22 10:46:27 -0700206 manifestPath := manifestFixer(ctx, manifestSrcPath, sdkContext, sdkLibraries,
Sasha Smundak6ad77252019-05-01 13:16:22 -0700207 a.isLibrary, a.useEmbeddedNativeLibs, a.usesNonSdkApis, a.useEmbeddedDex)
Colin Cross90c25c62019-04-19 16:22:57 -0700208
209 a.transitiveManifestPaths = append(android.Paths{manifestPath}, transitiveStaticLibManifests...)
210
211 if len(transitiveStaticLibManifests) > 0 {
Colin Crossaa1c6f12019-04-19 16:25:38 -0700212 a.mergedManifestFile = manifestMerger(ctx, manifestPath, transitiveStaticLibManifests, a.isLibrary)
Colin Cross90c25c62019-04-19 16:22:57 -0700213 if !a.isLibrary {
214 // Only use the merged manifest for applications. For libraries, the transitive closure of manifests
215 // will be propagated to the final application and merged there. The merged manifest for libraries is
216 // only passed to Make, which can't handle transitive dependencies.
217 manifestPath = a.mergedManifestFile
218 }
219 } else {
220 a.mergedManifestFile = manifestPath
221 }
Colin Cross31656952018-05-24 16:11:20 -0700222
Colin Crossa592e3e2019-02-19 16:59:53 -0800223 linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700224
Colin Crossc1c37552019-01-31 11:42:41 -0800225 rroDirs = append(rroDirs, staticRRODirs...)
Colin Cross31656952018-05-24 16:11:20 -0700226 linkFlags = append(linkFlags, libFlags...)
227 linkDeps = append(linkDeps, libDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700228 linkFlags = append(linkFlags, extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700229 if a.isLibrary {
230 linkFlags = append(linkFlags, "--static-lib")
231 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700232
233 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
234 srcJar := android.PathForModuleGen(ctx, "R.jar")
235 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
236 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700237 // This file isn't used by Soong, but is generated for exporting
238 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Crossa97c5d32018-03-28 14:58:31 -0700239
Colin Cross4aaa84a2018-08-21 15:14:37 -0700240 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700241 for _, dir := range resDirs {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700242 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700243 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700244
Colin Crossa592e3e2019-02-19 16:59:53 -0800245 for i, zip := range resZips {
246 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
247 aapt2CompileZip(ctx, flata, zip)
248 compiledResDirs = append(compiledResDirs, android.Paths{flata})
249 }
250
Colin Cross4aaa84a2018-08-21 15:14:37 -0700251 var compiledRes, compiledOverlay android.Paths
252
253 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
254
Colin Crossbec85302019-02-13 13:15:46 -0800255 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700256 // If we are using static android libraries, every source file becomes an overlay.
257 // This is to emulate old AAPT behavior which simulated library support.
258 for _, compiledResDir := range compiledResDirs {
259 compiledOverlay = append(compiledOverlay, compiledResDir...)
260 }
Colin Crossbec85302019-02-13 13:15:46 -0800261 } else if a.isLibrary {
262 // Otherwise, for a static library we treat all the resources equally with no overlay.
263 for _, compiledResDir := range compiledResDirs {
264 compiledRes = append(compiledRes, compiledResDir...)
265 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700266 } else if len(compiledResDirs) > 0 {
267 // Without static libraries, the first directory is our directory, which can then be
268 // overlaid by the rest.
269 compiledRes = append(compiledRes, compiledResDirs[0]...)
270 for _, compiledResDir := range compiledResDirs[1:] {
271 compiledOverlay = append(compiledOverlay, compiledResDir...)
272 }
273 }
274
Colin Crossa97c5d32018-03-28 14:58:31 -0700275 for _, dir := range overlayDirs {
276 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files).Paths()...)
277 }
278
Colin Crosse560c4a2019-03-19 16:03:11 -0700279 var splitPackages android.WritablePaths
280 var splits []split
281
282 for _, s := range a.splitNames {
283 suffix := strings.Replace(s, ",", "_", -1)
284 path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
285 linkFlags = append(linkFlags, "--split", path.String()+":"+s)
286 splitPackages = append(splitPackages, path)
287 splits = append(splits, split{
288 name: s,
289 suffix: suffix,
290 path: path,
291 })
292 }
293
Colin Cross66f78822018-05-02 12:58:28 -0700294 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
Colin Crosse560c4a2019-03-19 16:03:11 -0700295 linkFlags, linkDeps, compiledRes, compiledOverlay, splitPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700296
297 a.aaptSrcJar = srcJar
298 a.exportPackage = packageRes
299 a.manifestPath = manifestPath
300 a.proguardOptionsFile = proguardOptionsFile
301 a.rroDirs = rroDirs
Colin Cross66f78822018-05-02 12:58:28 -0700302 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700303 a.rTxt = rTxt
Colin Crosse560c4a2019-03-19 16:03:11 -0700304 a.splits = splits
Colin Crossa97c5d32018-03-28 14:58:31 -0700305}
306
307// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Colin Cross90c25c62019-04-19 16:22:57 -0700308func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, transitiveStaticLibManifests android.Paths,
Colin Cross5446e882019-05-22 10:46:27 -0700309 staticRRODirs []rroDir, deps android.Paths, flags []string, sdkLibraries []string) {
Colin Cross66f78822018-05-02 12:58:28 -0700310
Colin Crossa97c5d32018-03-28 14:58:31 -0700311 var sharedLibs android.Paths
312
Colin Cross83bb3162018-06-25 15:48:06 -0700313 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700314 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700315 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700316 }
317
318 ctx.VisitDirectDeps(func(module android.Module) {
319 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700320 aarDep, _ := module.(AndroidLibraryDependency)
321 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700322 exportPackage = aarDep.ExportPackage()
323 }
324
325 switch ctx.OtherModuleDependencyTag(module) {
Colin Cross4b964c02018-10-15 16:18:06 -0700326 case instrumentationForTag:
327 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Colin Cross5446e882019-05-22 10:46:27 -0700328 case libTag:
329 if exportPackage != nil {
330 sharedLibs = append(sharedLibs, exportPackage)
331 }
332
333 if _, ok := module.(SdkLibraryDependency); ok {
334 sdkLibraries = append(sdkLibraries, ctx.OtherModuleName(module))
335 }
336
337 case frameworkResTag:
Colin Crossa97c5d32018-03-28 14:58:31 -0700338 if exportPackage != nil {
339 sharedLibs = append(sharedLibs, exportPackage)
340 }
341 case staticLibTag:
342 if exportPackage != nil {
Colin Cross66f78822018-05-02 12:58:28 -0700343 transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
Colin Crossbec85302019-02-13 13:15:46 -0800344 transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
Colin Cross90c25c62019-04-19 16:22:57 -0700345 transitiveStaticLibManifests = append(transitiveStaticLibManifests, aarDep.ExportedManifests()...)
Colin Cross5446e882019-05-22 10:46:27 -0700346 sdkLibraries = append(sdkLibraries, aarDep.ExportedSdkLibs()...)
Anton Hansson53c88442019-03-18 15:53:16 +0000347
348 outer:
349 for _, d := range aarDep.ExportedRRODirs() {
350 for _, e := range staticRRODirs {
351 if d.path == e.path {
352 continue outer
353 }
354 }
355 staticRRODirs = append(staticRRODirs, d)
356 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700357 }
358 }
359 })
360
361 deps = append(deps, sharedLibs...)
Colin Cross66f78822018-05-02 12:58:28 -0700362 deps = append(deps, transitiveStaticLibs...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700363
Colin Cross66f78822018-05-02 12:58:28 -0700364 if len(transitiveStaticLibs) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700365 flags = append(flags, "--auto-add-overlay")
366 }
367
368 for _, sharedLib := range sharedLibs {
369 flags = append(flags, "-I "+sharedLib.String())
370 }
371
Colin Cross66f78822018-05-02 12:58:28 -0700372 transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
Colin Cross90c25c62019-04-19 16:22:57 -0700373 transitiveStaticLibManifests = android.FirstUniquePaths(transitiveStaticLibManifests)
Colin Cross5446e882019-05-22 10:46:27 -0700374 sdkLibraries = android.FirstUniqueStrings(sdkLibraries)
Colin Cross66f78822018-05-02 12:58:28 -0700375
Colin Cross5446e882019-05-22 10:46:27 -0700376 return transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, deps, flags, sdkLibraries
Colin Crossa97c5d32018-03-28 14:58:31 -0700377}
378
379type AndroidLibrary struct {
380 Library
381 aapt
382
383 androidLibraryProperties androidLibraryProperties
384
385 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700386
387 exportedProguardFlagFiles android.Paths
Colin Cross66f78822018-05-02 12:58:28 -0700388 exportedStaticPackages android.Paths
Colin Cross89c31582018-04-30 15:55:11 -0700389}
390
391func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
392 return a.exportedProguardFlagFiles
Colin Crossa97c5d32018-03-28 14:58:31 -0700393}
394
Colin Cross66f78822018-05-02 12:58:28 -0700395func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
396 return a.exportedStaticPackages
397}
398
Colin Crossa97c5d32018-03-28 14:58:31 -0700399var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
400
401func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
402 a.Module.deps(ctx)
403 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700404 a.aapt.deps(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700405 }
406}
407
408func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800409 a.aapt.isLibrary = true
Colin Cross5446e882019-05-22 10:46:27 -0700410 a.aapt.sdkLibraries = a.exportedSdkLibs
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700411 a.aapt.buildActions(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700412
413 ctx.CheckbuildFile(a.proguardOptionsFile)
414 ctx.CheckbuildFile(a.exportPackage)
415 ctx.CheckbuildFile(a.aaptSrcJar)
416
417 // apps manifests are handled by aapt, don't let Module see them
418 a.properties.Manifest = nil
419
420 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
421 a.proguardOptionsFile)
422
423 a.Module.compile(ctx, a.aaptSrcJar)
424
Colin Crossf57c5782019-01-25 13:20:38 -0800425 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700426 var res android.Paths
427 if a.androidLibraryProperties.BuildAAR {
428 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
429 ctx.CheckbuildFile(a.aarFile)
430 }
Colin Cross89c31582018-04-30 15:55:11 -0700431
432 ctx.VisitDirectDeps(func(m android.Module) {
433 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
434 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
Colin Cross66f78822018-05-02 12:58:28 -0700435 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
436 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
Colin Cross89c31582018-04-30 15:55:11 -0700437 }
438 })
439
440 a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
Colin Cross66f78822018-05-02 12:58:28 -0700441 a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700442}
443
Colin Cross1b16b0e2019-02-12 14:41:32 -0800444// android_library builds and links sources into a `.jar` file for the device along with Android resources.
445//
446// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
447// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
448// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
449// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700450func AndroidLibraryFactory() android.Module {
451 module := &AndroidLibrary{}
452
453 module.AddProperties(
454 &module.Module.properties,
455 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800456 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700457 &module.Module.protoProperties,
458 &module.aaptProperties,
459 &module.androidLibraryProperties)
460
461 module.androidLibraryProperties.BuildAAR = true
462
Colin Cross48de9a42018-10-02 13:53:33 -0700463 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700464 return module
465}
466
Colin Crossfabb6082018-02-20 17:22:23 -0800467//
468// AAR (android library) prebuilts
469//
Colin Crossfabb6082018-02-20 17:22:23 -0800470
471type AARImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -0800472 Aars []string `android:"path"`
Colin Crossfabb6082018-02-20 17:22:23 -0800473
Colin Cross479884c2018-07-10 13:39:30 -0700474 Sdk_version *string
475 Min_sdk_version *string
Colin Crossa97c5d32018-03-28 14:58:31 -0700476
477 Static_libs []string
478 Libs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -0700479
480 // if set to true, run Jetifier against .aar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -0700481 Jetifier *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800482}
483
484type AARImport struct {
485 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700486 android.DefaultableModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800487 prebuilt android.Prebuilt
488
489 properties AARImportProperties
490
Colin Cross66f78822018-05-02 12:58:28 -0700491 classpathFile android.WritablePath
492 proguardFlags android.WritablePath
493 exportPackage android.WritablePath
494 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700495 manifest android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700496
497 exportedStaticPackages android.Paths
Colin Crossfabb6082018-02-20 17:22:23 -0800498}
499
Colin Cross83bb3162018-06-25 15:48:06 -0700500func (a *AARImport) sdkVersion() string {
501 return String(a.properties.Sdk_version)
502}
503
504func (a *AARImport) minSdkVersion() string {
Colin Cross479884c2018-07-10 13:39:30 -0700505 if a.properties.Min_sdk_version != nil {
506 return *a.properties.Min_sdk_version
507 }
Colin Cross83bb3162018-06-25 15:48:06 -0700508 return a.sdkVersion()
509}
510
Dan Willemsen419290a2018-10-31 15:28:47 -0700511func (a *AARImport) targetSdkVersion() string {
512 return a.sdkVersion()
513}
514
Colin Crossa97c5d32018-03-28 14:58:31 -0700515var _ AndroidLibraryDependency = (*AARImport)(nil)
516
517func (a *AARImport) ExportPackage() android.Path {
518 return a.exportPackage
519}
520
Colin Cross89c31582018-04-30 15:55:11 -0700521func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
522 return android.Paths{a.proguardFlags}
523}
524
Anton Hansson53c88442019-03-18 15:53:16 +0000525func (a *AARImport) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800526 return nil
527}
528
Colin Cross66f78822018-05-02 12:58:28 -0700529func (a *AARImport) ExportedStaticPackages() android.Paths {
530 return a.exportedStaticPackages
531}
532
Colin Cross90c25c62019-04-19 16:22:57 -0700533func (a *AARImport) ExportedManifests() android.Paths {
534 return android.Paths{a.manifest}
Colin Cross31656952018-05-24 16:11:20 -0700535}
536
Colin Crossfabb6082018-02-20 17:22:23 -0800537func (a *AARImport) Prebuilt() *android.Prebuilt {
538 return &a.prebuilt
539}
540
541func (a *AARImport) Name() string {
542 return a.prebuilt.Name(a.ModuleBase.Name())
543}
544
545func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross10932872019-04-18 14:27:12 -0700546 if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Colin Cross83bb3162018-06-25 15:48:06 -0700547 sdkDep := decodeSdkDep(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700548 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700549 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800550 }
551 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700552
Colin Cross42d48b72018-08-29 14:10:52 -0700553 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
554 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800555}
556
557// Unzip an AAR into its constituent files and directories. Any files in Outputs that don't exist in the AAR will be
558// touched to create an empty file, and any directories in $expectedDirs will be created.
559var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
560 blueprint.RuleParams{
561 Command: `rm -rf $outDir && mkdir -p $outDir $expectedDirs && ` +
562 `unzip -qo -d $outDir $in && touch $out`,
563 },
564 "expectedDirs", "outDir")
565
566func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
567 if len(a.properties.Aars) != 1 {
568 ctx.PropertyErrorf("aars", "exactly one aar is required")
569 return
570 }
571
Nan Zhang4c819fb2018-08-27 18:31:46 -0700572 aarName := ctx.ModuleName() + ".aar"
573 var aar android.Path
574 aar = android.PathForModuleSrc(ctx, a.properties.Aars[0])
Colin Cross1001a792019-03-21 22:21:39 -0700575 if Bool(a.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -0700576 inputFile := aar
577 aar = android.PathForModuleOut(ctx, "jetifier", aarName)
578 TransformJetifier(ctx, aar.(android.WritablePath), inputFile)
579 }
Colin Crossfabb6082018-02-20 17:22:23 -0800580
581 extractedAARDir := android.PathForModuleOut(ctx, "aar")
582 extractedResDir := extractedAARDir.Join(ctx, "res")
583 a.classpathFile = extractedAARDir.Join(ctx, "classes.jar")
584 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700585 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossfabb6082018-02-20 17:22:23 -0800586
587 ctx.Build(pctx, android.BuildParams{
588 Rule: unzipAAR,
589 Input: aar,
Colin Cross10f7c4a2018-05-23 10:59:28 -0700590 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest},
Colin Crossfabb6082018-02-20 17:22:23 -0800591 Description: "unzip AAR",
592 Args: map[string]string{
593 "expectedDirs": extractedResDir.String(),
594 "outDir": extractedAARDir.String(),
595 },
596 })
597
598 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
599 aaptCompileDeps := android.Paths{a.classpathFile}
600 aaptCompileDirs := android.Paths{extractedResDir}
601 flata := compiledResDir.Join(ctx, "gen_res.flata")
602 aapt2CompileDirs(ctx, flata, aaptCompileDirs, aaptCompileDeps)
603
604 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
605 srcJar := android.PathForModuleGen(ctx, "R.jar")
606 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700607 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700608 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800609
610 var linkDeps android.Paths
611
612 linkFlags := []string{
613 "--static-lib",
614 "--no-static-lib-packages",
615 "--auto-add-overlay",
616 }
617
Colin Cross10f7c4a2018-05-23 10:59:28 -0700618 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
619 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800620
Colin Cross5446e882019-05-22 10:46:27 -0700621 transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags, sdkLibraries :=
622 aaptLibs(ctx, sdkContext(a))
Colin Cross31656952018-05-24 16:11:20 -0700623
624 _ = staticLibManifests
Colin Crossc1c37552019-01-31 11:42:41 -0800625 _ = staticRRODirs
Colin Cross5446e882019-05-22 10:46:27 -0700626 _ = sdkLibraries
Colin Crossfabb6082018-02-20 17:22:23 -0800627
Colin Crossa97c5d32018-03-28 14:58:31 -0700628 linkDeps = append(linkDeps, libDeps...)
629 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800630
Colin Cross66f78822018-05-02 12:58:28 -0700631 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800632
Colin Cross66f78822018-05-02 12:58:28 -0700633 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Colin Crosse560c4a2019-03-19 16:03:11 -0700634 linkFlags, linkDeps, nil, overlayRes, nil)
Colin Crossfabb6082018-02-20 17:22:23 -0800635}
636
637var _ Dependency = (*AARImport)(nil)
638
639func (a *AARImport) HeaderJars() android.Paths {
640 return android.Paths{a.classpathFile}
641}
642
643func (a *AARImport) ImplementationJars() android.Paths {
644 return android.Paths{a.classpathFile}
645}
646
Colin Cross331a1212018-08-15 20:40:52 -0700647func (a *AARImport) ResourceJars() android.Paths {
648 return nil
649}
650
651func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
652 return android.Paths{a.classpathFile}
653}
654
Colin Crossf24a22a2019-01-31 14:12:44 -0800655func (a *AARImport) DexJar() android.Path {
656 return nil
657}
658
Colin Crossfabb6082018-02-20 17:22:23 -0800659func (a *AARImport) AidlIncludeDirs() android.Paths {
660 return nil
661}
662
Jiyong Park1be96912018-05-28 18:02:19 +0900663func (a *AARImport) ExportedSdkLibs() []string {
664 return nil
665}
666
Colin Cross0c4ce212019-05-03 15:28:19 -0700667func (a *AARImport) SrcJarArgs() ([]string, android.Paths) {
668 return nil, nil
669}
670
Colin Crossfabb6082018-02-20 17:22:23 -0800671var _ android.PrebuiltInterface = (*Import)(nil)
672
Colin Cross1b16b0e2019-02-12 14:41:32 -0800673// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
674//
675// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
676// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -0800677func AARImportFactory() android.Module {
678 module := &AARImport{}
679
680 module.AddProperties(&module.properties)
681
682 android.InitPrebuiltModule(module, &module.properties.Aars)
Colin Cross48de9a42018-10-02 13:53:33 -0700683 InitJavaModule(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800684 return module
685}