blob: ae064e5b6ff3396aaccca9598e17af8a537fad59 [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"
Jaewoong Jung5b425e22019-06-17 17:40:56 -070020 "path/filepath"
Colin Crossa97c5d32018-03-28 14:58:31 -070021 "strings"
Colin Crossfabb6082018-02-20 17:22:23 -080022
23 "github.com/google/blueprint"
Colin Crossa97c5d32018-03-28 14:58:31 -070024 "github.com/google/blueprint/proptools"
Colin Crossfabb6082018-02-20 17:22:23 -080025)
26
Colin Crossa97c5d32018-03-28 14:58:31 -070027type AndroidLibraryDependency interface {
28 Dependency
29 ExportPackage() android.Path
Colin Cross89c31582018-04-30 15:55:11 -070030 ExportedProguardFlagFiles() android.Paths
Anton Hansson53c88442019-03-18 15:53:16 +000031 ExportedRRODirs() []rroDir
Colin Cross66f78822018-05-02 12:58:28 -070032 ExportedStaticPackages() android.Paths
Colin Cross90c25c62019-04-19 16:22:57 -070033 ExportedManifests() android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -070034}
35
36func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000037 RegisterAARBuildComponents(android.InitRegistrationContext)
38}
39
40func RegisterAARBuildComponents(ctx android.RegistrationContext) {
41 ctx.RegisterModuleType("android_library_import", AARImportFactory)
42 ctx.RegisterModuleType("android_library", AndroidLibraryFactory)
Colin Crossa97c5d32018-03-28 14:58:31 -070043}
44
45//
46// AAR (android library)
47//
48
49type androidLibraryProperties struct {
50 BuildAAR bool `blueprint:"mutated"`
51}
52
53type aaptProperties struct {
54 // flags passed to aapt when creating the apk
55 Aaptflags []string
56
Dan Willemsen72be5902018-10-24 20:24:57 -070057 // include all resource configurations, not just the product-configured
58 // ones.
59 Aapt_include_all_resources *bool
60
Colin Crossa97c5d32018-03-28 14:58:31 -070061 // list of directories relative to the Blueprints file containing assets.
Colin Cross0ddae7f2019-02-07 15:30:01 -080062 // Defaults to ["assets"] if a directory called assets exists. Set to []
63 // to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070064 Asset_dirs []string
65
66 // list of directories relative to the Blueprints file containing
Colin Cross0ddae7f2019-02-07 15:30:01 -080067 // Android resources. Defaults to ["res"] if a directory called res exists.
68 // Set to [] to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070069 Resource_dirs []string
70
Colin Crossa592e3e2019-02-19 16:59:53 -080071 // list of zip files containing Android resources.
Colin Cross27b922f2019-03-04 22:35:41 -080072 Resource_zips []string `android:"path"`
Colin Crossa592e3e2019-02-19 16:59:53 -080073
Colin Crossa97c5d32018-03-28 14:58:31 -070074 // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml".
Colin Cross27b922f2019-03-04 22:35:41 -080075 Manifest *string `android:"path"`
changho.shinb5432b72019-08-08 18:37:17 +090076
77 // paths to additional manifest files to merge with main manifest.
78 Additional_manifests []string `android:"path"`
Sasha Smundak541056c2019-10-28 15:50:06 -070079
80 // do not include AndroidManifest from dependent libraries
81 Dont_merge_manifests *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070082}
83
84type aapt struct {
Colin Cross90c25c62019-04-19 16:22:57 -070085 aaptSrcJar android.Path
86 exportPackage android.Path
87 manifestPath android.Path
88 transitiveManifestPaths android.Paths
89 proguardOptionsFile android.Path
90 rroDirs []rroDir
91 rTxt android.Path
92 extraAaptPackagesFile android.Path
93 mergedManifestFile android.Path
Jaewoong Jung5b425e22019-06-17 17:40:56 -070094 noticeFile android.OptionalPath
Colin Cross90c25c62019-04-19 16:22:57 -070095 isLibrary bool
Sasha Smundak6ad77252019-05-01 13:16:22 -070096 useEmbeddedNativeLibs bool
Colin Cross90c25c62019-04-19 16:22:57 -070097 useEmbeddedDex bool
98 usesNonSdkApis bool
Colin Cross5446e882019-05-22 10:46:27 -070099 sdkLibraries []string
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700100 hasNoCode bool
Colin Crossa97c5d32018-03-28 14:58:31 -0700101
Colin Crosse560c4a2019-03-19 16:03:11 -0700102 splitNames []string
103 splits []split
104
Colin Crossa97c5d32018-03-28 14:58:31 -0700105 aaptProperties aaptProperties
106}
107
Colin Crosse560c4a2019-03-19 16:03:11 -0700108type split struct {
109 name string
110 suffix string
111 path android.Path
112}
113
Colin Crossa97c5d32018-03-28 14:58:31 -0700114func (a *aapt) ExportPackage() android.Path {
115 return a.exportPackage
116}
117
Anton Hansson53c88442019-03-18 15:53:16 +0000118func (a *aapt) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800119 return a.rroDirs
120}
121
Colin Cross90c25c62019-04-19 16:22:57 -0700122func (a *aapt) ExportedManifests() android.Paths {
123 return a.transitiveManifestPaths
Colin Crossc1c37552019-01-31 11:42:41 -0800124}
125
Colin Crossa0ba2f52019-06-22 12:59:27 -0700126func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext sdkContext,
127 manifestPath android.Path) (compileFlags, linkFlags []string, linkDeps android.Paths,
128 resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700129
130 hasVersionCode := false
131 hasVersionName := false
Colin Crossa97c5d32018-03-28 14:58:31 -0700132 for _, f := range a.aaptProperties.Aaptflags {
133 if strings.HasPrefix(f, "--version-code") {
134 hasVersionCode = true
135 } else if strings.HasPrefix(f, "--version-name") {
136 hasVersionName = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700137 }
138 }
139
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
Colin Cross83bb3162018-06-25 15:48:06 -0700179 minSdkVersion := sdkVersionOrDefault(ctx, sdkContext.minSdkVersion())
Colin Crossa97c5d32018-03-28 14:58:31 -0700180
Colin Cross83bb3162018-06-25 15:48:06 -0700181 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
182 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700183
Colin Crossa97c5d32018-03-28 14:58:31 -0700184 // Version code
185 if !hasVersionCode {
186 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion())
187 }
188
189 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700190 var versionName string
191 if ctx.ModuleName() == "framework-res" {
192 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
193 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700194 // if it contains the build number. Use the PlatformVersionName instead.
195 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700196 } else {
197 versionName = ctx.Config().AppsDefaultVersionName()
198 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800199 versionName = proptools.NinjaEscape(versionName)
Colin Crossa97c5d32018-03-28 14:58:31 -0700200 linkFlags = append(linkFlags, "--version-name ", versionName)
201 }
202
Colin Crossa0ba2f52019-06-22 12:59:27 -0700203 linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"})
204
205 // Always set --pseudo-localize, it will be stripped out later for release
206 // builds that don't want it.
207 compileFlags = append(compileFlags, "--pseudo-localize")
208
209 return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
Colin Crossa97c5d32018-03-28 14:58:31 -0700210}
211
Paul Duffin250e6192019-06-07 10:44:37 +0100212func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) {
Colin Cross42308aa2018-11-14 21:44:17 -0800213 if sdkDep.frameworkResModule != "" {
214 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700215 }
216}
217
Colin Cross83bb3162018-06-25 15:48:06 -0700218func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, extraLinkFlags ...string) {
Colin Cross5446e882019-05-22 10:46:27 -0700219
220 transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, libDeps, libFlags, sdkLibraries :=
221 aaptLibs(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700222
Colin Cross31656952018-05-24 16:11:20 -0700223 // App manifest file
224 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
225 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
226
Colin Cross5446e882019-05-22 10:46:27 -0700227 manifestPath := manifestFixer(ctx, manifestSrcPath, sdkContext, sdkLibraries,
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700228 a.isLibrary, a.useEmbeddedNativeLibs, a.usesNonSdkApis, a.useEmbeddedDex, a.hasNoCode)
Colin Cross90c25c62019-04-19 16:22:57 -0700229
Luca Stefanifd898822019-09-10 22:13:31 +0200230 // Add additional manifest files to transitive manifests.
231 additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)
232 a.transitiveManifestPaths = append(android.Paths{manifestPath}, additionalManifests...)
233 a.transitiveManifestPaths = append(a.transitiveManifestPaths, transitiveStaticLibManifests...)
Colin Cross90c25c62019-04-19 16:22:57 -0700234
Sasha Smundak541056c2019-10-28 15:50:06 -0700235 if len(a.transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) {
Luca Stefanifd898822019-09-10 22:13:31 +0200236 a.mergedManifestFile = manifestMerger(ctx, a.transitiveManifestPaths[0], a.transitiveManifestPaths[1:], a.isLibrary)
Colin Cross90c25c62019-04-19 16:22:57 -0700237 if !a.isLibrary {
238 // Only use the merged manifest for applications. For libraries, the transitive closure of manifests
239 // will be propagated to the final application and merged there. The merged manifest for libraries is
240 // only passed to Make, which can't handle transitive dependencies.
241 manifestPath = a.mergedManifestFile
242 }
243 } else {
244 a.mergedManifestFile = manifestPath
245 }
Colin Cross31656952018-05-24 16:11:20 -0700246
Colin Crossa0ba2f52019-06-22 12:59:27 -0700247 compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700248
Colin Crossc1c37552019-01-31 11:42:41 -0800249 rroDirs = append(rroDirs, staticRRODirs...)
Colin Cross31656952018-05-24 16:11:20 -0700250 linkFlags = append(linkFlags, libFlags...)
251 linkDeps = append(linkDeps, libDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700252 linkFlags = append(linkFlags, extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700253 if a.isLibrary {
254 linkFlags = append(linkFlags, "--static-lib")
255 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700256
257 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900258 // the subdir "android" is required to be filtered by package names
259 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700260 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
261 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700262 // This file isn't used by Soong, but is generated for exporting
263 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Crossa97c5d32018-03-28 14:58:31 -0700264
Colin Cross4aaa84a2018-08-21 15:14:37 -0700265 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700266 for _, dir := range resDirs {
Colin Crossa0ba2f52019-06-22 12:59:27 -0700267 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700268 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700269
Colin Crossa592e3e2019-02-19 16:59:53 -0800270 for i, zip := range resZips {
271 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
Colin Crossa0ba2f52019-06-22 12:59:27 -0700272 aapt2CompileZip(ctx, flata, zip, "", compileFlags)
Colin Crossa592e3e2019-02-19 16:59:53 -0800273 compiledResDirs = append(compiledResDirs, android.Paths{flata})
274 }
275
Colin Cross4aaa84a2018-08-21 15:14:37 -0700276 var compiledRes, compiledOverlay android.Paths
277
278 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
279
Colin Crossbec85302019-02-13 13:15:46 -0800280 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700281 // If we are using static android libraries, every source file becomes an overlay.
282 // This is to emulate old AAPT behavior which simulated library support.
283 for _, compiledResDir := range compiledResDirs {
284 compiledOverlay = append(compiledOverlay, compiledResDir...)
285 }
Colin Crossbec85302019-02-13 13:15:46 -0800286 } else if a.isLibrary {
287 // Otherwise, for a static library we treat all the resources equally with no overlay.
288 for _, compiledResDir := range compiledResDirs {
289 compiledRes = append(compiledRes, compiledResDir...)
290 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700291 } else if len(compiledResDirs) > 0 {
292 // Without static libraries, the first directory is our directory, which can then be
293 // overlaid by the rest.
294 compiledRes = append(compiledRes, compiledResDirs[0]...)
295 for _, compiledResDir := range compiledResDirs[1:] {
296 compiledOverlay = append(compiledOverlay, compiledResDir...)
297 }
298 }
299
Colin Crossa97c5d32018-03-28 14:58:31 -0700300 for _, dir := range overlayDirs {
Colin Crossa0ba2f52019-06-22 12:59:27 -0700301 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700302 }
303
Colin Crosse560c4a2019-03-19 16:03:11 -0700304 var splitPackages android.WritablePaths
305 var splits []split
306
307 for _, s := range a.splitNames {
308 suffix := strings.Replace(s, ",", "_", -1)
309 path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
310 linkFlags = append(linkFlags, "--split", path.String()+":"+s)
311 splitPackages = append(splitPackages, path)
312 splits = append(splits, split{
313 name: s,
314 suffix: suffix,
315 path: path,
316 })
317 }
318
Colin Cross66f78822018-05-02 12:58:28 -0700319 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
Colin Crosse560c4a2019-03-19 16:03:11 -0700320 linkFlags, linkDeps, compiledRes, compiledOverlay, splitPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700321
322 a.aaptSrcJar = srcJar
323 a.exportPackage = packageRes
324 a.manifestPath = manifestPath
325 a.proguardOptionsFile = proguardOptionsFile
326 a.rroDirs = rroDirs
Colin Cross66f78822018-05-02 12:58:28 -0700327 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700328 a.rTxt = rTxt
Colin Crosse560c4a2019-03-19 16:03:11 -0700329 a.splits = splits
Colin Crossa97c5d32018-03-28 14:58:31 -0700330}
331
332// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Colin Cross90c25c62019-04-19 16:22:57 -0700333func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, transitiveStaticLibManifests android.Paths,
Colin Cross5446e882019-05-22 10:46:27 -0700334 staticRRODirs []rroDir, deps android.Paths, flags []string, sdkLibraries []string) {
Colin Cross66f78822018-05-02 12:58:28 -0700335
Colin Crossa97c5d32018-03-28 14:58:31 -0700336 var sharedLibs android.Paths
337
Colin Cross83bb3162018-06-25 15:48:06 -0700338 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700339 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700340 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700341 }
342
343 ctx.VisitDirectDeps(func(module android.Module) {
344 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700345 aarDep, _ := module.(AndroidLibraryDependency)
346 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700347 exportPackage = aarDep.ExportPackage()
348 }
349
350 switch ctx.OtherModuleDependencyTag(module) {
Colin Cross4b964c02018-10-15 16:18:06 -0700351 case instrumentationForTag:
352 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Colin Cross5446e882019-05-22 10:46:27 -0700353 case libTag:
354 if exportPackage != nil {
355 sharedLibs = append(sharedLibs, exportPackage)
356 }
357
358 if _, ok := module.(SdkLibraryDependency); ok {
359 sdkLibraries = append(sdkLibraries, ctx.OtherModuleName(module))
360 }
361
362 case frameworkResTag:
Colin Crossa97c5d32018-03-28 14:58:31 -0700363 if exportPackage != nil {
364 sharedLibs = append(sharedLibs, exportPackage)
365 }
366 case staticLibTag:
367 if exportPackage != nil {
Colin Cross66f78822018-05-02 12:58:28 -0700368 transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
Colin Crossbec85302019-02-13 13:15:46 -0800369 transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
Colin Cross90c25c62019-04-19 16:22:57 -0700370 transitiveStaticLibManifests = append(transitiveStaticLibManifests, aarDep.ExportedManifests()...)
Colin Cross5446e882019-05-22 10:46:27 -0700371 sdkLibraries = append(sdkLibraries, aarDep.ExportedSdkLibs()...)
Anton Hansson53c88442019-03-18 15:53:16 +0000372
373 outer:
374 for _, d := range aarDep.ExportedRRODirs() {
375 for _, e := range staticRRODirs {
376 if d.path == e.path {
377 continue outer
378 }
379 }
380 staticRRODirs = append(staticRRODirs, d)
381 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700382 }
383 }
384 })
385
386 deps = append(deps, sharedLibs...)
Colin Cross66f78822018-05-02 12:58:28 -0700387 deps = append(deps, transitiveStaticLibs...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700388
Colin Cross66f78822018-05-02 12:58:28 -0700389 if len(transitiveStaticLibs) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700390 flags = append(flags, "--auto-add-overlay")
391 }
392
393 for _, sharedLib := range sharedLibs {
394 flags = append(flags, "-I "+sharedLib.String())
395 }
396
Colin Cross66f78822018-05-02 12:58:28 -0700397 transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
Colin Cross90c25c62019-04-19 16:22:57 -0700398 transitiveStaticLibManifests = android.FirstUniquePaths(transitiveStaticLibManifests)
Colin Cross5446e882019-05-22 10:46:27 -0700399 sdkLibraries = android.FirstUniqueStrings(sdkLibraries)
Colin Cross66f78822018-05-02 12:58:28 -0700400
Colin Cross5446e882019-05-22 10:46:27 -0700401 return transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, deps, flags, sdkLibraries
Colin Crossa97c5d32018-03-28 14:58:31 -0700402}
403
404type AndroidLibrary struct {
405 Library
406 aapt
407
408 androidLibraryProperties androidLibraryProperties
409
410 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700411
412 exportedProguardFlagFiles android.Paths
Colin Cross66f78822018-05-02 12:58:28 -0700413 exportedStaticPackages android.Paths
Colin Cross89c31582018-04-30 15:55:11 -0700414}
415
416func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
417 return a.exportedProguardFlagFiles
Colin Crossa97c5d32018-03-28 14:58:31 -0700418}
419
Colin Cross66f78822018-05-02 12:58:28 -0700420func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
421 return a.exportedStaticPackages
422}
423
Colin Crossa97c5d32018-03-28 14:58:31 -0700424var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
425
426func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
427 a.Module.deps(ctx)
Paul Duffin250e6192019-06-07 10:44:37 +0100428 sdkDep := decodeSdkDep(ctx, sdkContext(a))
429 if sdkDep.hasFrameworkLibs() {
430 a.aapt.deps(ctx, sdkDep)
Colin Crossa97c5d32018-03-28 14:58:31 -0700431 }
432}
433
434func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800435 a.aapt.isLibrary = true
Colin Cross5446e882019-05-22 10:46:27 -0700436 a.aapt.sdkLibraries = a.exportedSdkLibs
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700437 a.aapt.buildActions(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700438
439 ctx.CheckbuildFile(a.proguardOptionsFile)
440 ctx.CheckbuildFile(a.exportPackage)
441 ctx.CheckbuildFile(a.aaptSrcJar)
442
443 // apps manifests are handled by aapt, don't let Module see them
444 a.properties.Manifest = nil
445
446 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
447 a.proguardOptionsFile)
448
449 a.Module.compile(ctx, a.aaptSrcJar)
450
Colin Crossf57c5782019-01-25 13:20:38 -0800451 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700452 var res android.Paths
453 if a.androidLibraryProperties.BuildAAR {
454 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
455 ctx.CheckbuildFile(a.aarFile)
456 }
Colin Cross89c31582018-04-30 15:55:11 -0700457
458 ctx.VisitDirectDeps(func(m android.Module) {
459 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
460 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
Colin Cross66f78822018-05-02 12:58:28 -0700461 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
462 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
Colin Cross89c31582018-04-30 15:55:11 -0700463 }
464 })
465
466 a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
Colin Cross66f78822018-05-02 12:58:28 -0700467 a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700468}
469
Colin Cross1b16b0e2019-02-12 14:41:32 -0800470// android_library builds and links sources into a `.jar` file for the device along with Android resources.
471//
472// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
473// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
474// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
475// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700476func AndroidLibraryFactory() android.Module {
477 module := &AndroidLibrary{}
478
479 module.AddProperties(
480 &module.Module.properties,
481 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800482 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700483 &module.Module.protoProperties,
484 &module.aaptProperties,
485 &module.androidLibraryProperties)
486
487 module.androidLibraryProperties.BuildAAR = true
488
Colin Cross48de9a42018-10-02 13:53:33 -0700489 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700490 return module
491}
492
Colin Crossfabb6082018-02-20 17:22:23 -0800493//
494// AAR (android library) prebuilts
495//
Colin Crossfabb6082018-02-20 17:22:23 -0800496
497type AARImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -0800498 Aars []string `android:"path"`
Colin Crossfabb6082018-02-20 17:22:23 -0800499
Colin Cross479884c2018-07-10 13:39:30 -0700500 Sdk_version *string
501 Min_sdk_version *string
Colin Crossa97c5d32018-03-28 14:58:31 -0700502
503 Static_libs []string
504 Libs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -0700505
506 // if set to true, run Jetifier against .aar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -0700507 Jetifier *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800508}
509
510type AARImport struct {
511 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700512 android.DefaultableModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800513 prebuilt android.Prebuilt
514
515 properties AARImportProperties
516
Colin Cross66f78822018-05-02 12:58:28 -0700517 classpathFile android.WritablePath
518 proguardFlags android.WritablePath
519 exportPackage android.WritablePath
520 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700521 manifest android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700522
523 exportedStaticPackages android.Paths
Colin Crossfabb6082018-02-20 17:22:23 -0800524}
525
Colin Cross83bb3162018-06-25 15:48:06 -0700526func (a *AARImport) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900527 return String(a.properties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700528}
529
Paul Duffine25c6442019-10-11 13:50:28 +0100530func (a *AARImport) systemModules() string {
531 return ""
532}
533
Colin Cross83bb3162018-06-25 15:48:06 -0700534func (a *AARImport) minSdkVersion() string {
Colin Cross479884c2018-07-10 13:39:30 -0700535 if a.properties.Min_sdk_version != nil {
536 return *a.properties.Min_sdk_version
537 }
Colin Cross83bb3162018-06-25 15:48:06 -0700538 return a.sdkVersion()
539}
540
Dan Willemsen419290a2018-10-31 15:28:47 -0700541func (a *AARImport) targetSdkVersion() string {
542 return a.sdkVersion()
543}
544
Colin Cross1e743852019-10-28 11:37:20 -0700545func (a *AARImport) javaVersion() string {
546 return ""
547}
548
Colin Crossa97c5d32018-03-28 14:58:31 -0700549var _ AndroidLibraryDependency = (*AARImport)(nil)
550
551func (a *AARImport) ExportPackage() android.Path {
552 return a.exportPackage
553}
554
Colin Cross89c31582018-04-30 15:55:11 -0700555func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
556 return android.Paths{a.proguardFlags}
557}
558
Anton Hansson53c88442019-03-18 15:53:16 +0000559func (a *AARImport) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800560 return nil
561}
562
Colin Cross66f78822018-05-02 12:58:28 -0700563func (a *AARImport) ExportedStaticPackages() android.Paths {
564 return a.exportedStaticPackages
565}
566
Colin Cross90c25c62019-04-19 16:22:57 -0700567func (a *AARImport) ExportedManifests() android.Paths {
568 return android.Paths{a.manifest}
Colin Cross31656952018-05-24 16:11:20 -0700569}
570
Colin Crossfabb6082018-02-20 17:22:23 -0800571func (a *AARImport) Prebuilt() *android.Prebuilt {
572 return &a.prebuilt
573}
574
575func (a *AARImport) Name() string {
576 return a.prebuilt.Name(a.ModuleBase.Name())
577}
578
Jiyong Park618922e2020-01-08 13:35:43 +0900579func (a *AARImport) JacocoReportClassesFile() android.Path {
580 return nil
581}
582
Colin Crossfabb6082018-02-20 17:22:23 -0800583func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross10932872019-04-18 14:27:12 -0700584 if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Colin Cross83bb3162018-06-25 15:48:06 -0700585 sdkDep := decodeSdkDep(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700586 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700587 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800588 }
589 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700590
Colin Cross42d48b72018-08-29 14:10:52 -0700591 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
592 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800593}
594
595// 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 -0700596// 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 -0800597var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
598 blueprint.RuleParams{
Dan Willemsen304cfec2019-05-28 14:49:06 -0700599 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
600 `unzip -qo -d $outDir $in && rm -rf $outDir/res && touch $out`,
Colin Crossfabb6082018-02-20 17:22:23 -0800601 },
Dan Willemsen304cfec2019-05-28 14:49:06 -0700602 "outDir")
Colin Crossfabb6082018-02-20 17:22:23 -0800603
604func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
605 if len(a.properties.Aars) != 1 {
606 ctx.PropertyErrorf("aars", "exactly one aar is required")
607 return
608 }
609
Nan Zhang4c819fb2018-08-27 18:31:46 -0700610 aarName := ctx.ModuleName() + ".aar"
611 var aar android.Path
612 aar = android.PathForModuleSrc(ctx, a.properties.Aars[0])
Colin Cross1001a792019-03-21 22:21:39 -0700613 if Bool(a.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -0700614 inputFile := aar
615 aar = android.PathForModuleOut(ctx, "jetifier", aarName)
616 TransformJetifier(ctx, aar.(android.WritablePath), inputFile)
617 }
Colin Crossfabb6082018-02-20 17:22:23 -0800618
619 extractedAARDir := android.PathForModuleOut(ctx, "aar")
Colin Crossfabb6082018-02-20 17:22:23 -0800620 a.classpathFile = extractedAARDir.Join(ctx, "classes.jar")
621 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700622 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossfabb6082018-02-20 17:22:23 -0800623
624 ctx.Build(pctx, android.BuildParams{
625 Rule: unzipAAR,
626 Input: aar,
Colin Cross10f7c4a2018-05-23 10:59:28 -0700627 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest},
Colin Crossfabb6082018-02-20 17:22:23 -0800628 Description: "unzip AAR",
629 Args: map[string]string{
Dan Willemsen304cfec2019-05-28 14:49:06 -0700630 "outDir": extractedAARDir.String(),
Colin Crossfabb6082018-02-20 17:22:23 -0800631 },
632 })
633
Colin Crossa0ba2f52019-06-22 12:59:27 -0700634 // Always set --pseudo-localize, it will be stripped out later for release
635 // builds that don't want it.
636 compileFlags := []string{"--pseudo-localize"}
Colin Crossfabb6082018-02-20 17:22:23 -0800637 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
Colin Crossfabb6082018-02-20 17:22:23 -0800638 flata := compiledResDir.Join(ctx, "gen_res.flata")
Colin Crossa0ba2f52019-06-22 12:59:27 -0700639 aapt2CompileZip(ctx, flata, aar, "res", compileFlags)
Colin Crossfabb6082018-02-20 17:22:23 -0800640
641 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900642 // the subdir "android" is required to be filtered by package names
643 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossfabb6082018-02-20 17:22:23 -0800644 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700645 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700646 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800647
648 var linkDeps android.Paths
649
650 linkFlags := []string{
651 "--static-lib",
652 "--no-static-lib-packages",
653 "--auto-add-overlay",
654 }
655
Colin Cross10f7c4a2018-05-23 10:59:28 -0700656 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
657 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800658
Colin Cross5446e882019-05-22 10:46:27 -0700659 transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags, sdkLibraries :=
660 aaptLibs(ctx, sdkContext(a))
Colin Cross31656952018-05-24 16:11:20 -0700661
662 _ = staticLibManifests
Colin Crossc1c37552019-01-31 11:42:41 -0800663 _ = staticRRODirs
Colin Cross5446e882019-05-22 10:46:27 -0700664 _ = sdkLibraries
Colin Crossfabb6082018-02-20 17:22:23 -0800665
Colin Crossa97c5d32018-03-28 14:58:31 -0700666 linkDeps = append(linkDeps, libDeps...)
667 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800668
Colin Cross66f78822018-05-02 12:58:28 -0700669 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800670
Colin Cross66f78822018-05-02 12:58:28 -0700671 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Colin Crosse560c4a2019-03-19 16:03:11 -0700672 linkFlags, linkDeps, nil, overlayRes, nil)
Colin Crossfabb6082018-02-20 17:22:23 -0800673}
674
675var _ Dependency = (*AARImport)(nil)
676
677func (a *AARImport) HeaderJars() android.Paths {
678 return android.Paths{a.classpathFile}
679}
680
681func (a *AARImport) ImplementationJars() android.Paths {
682 return android.Paths{a.classpathFile}
683}
684
Colin Cross331a1212018-08-15 20:40:52 -0700685func (a *AARImport) ResourceJars() android.Paths {
686 return nil
687}
688
689func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
690 return android.Paths{a.classpathFile}
691}
692
Colin Crossf24a22a2019-01-31 14:12:44 -0800693func (a *AARImport) DexJar() android.Path {
694 return nil
695}
696
Colin Crossfabb6082018-02-20 17:22:23 -0800697func (a *AARImport) AidlIncludeDirs() android.Paths {
698 return nil
699}
700
Jiyong Park1be96912018-05-28 18:02:19 +0900701func (a *AARImport) ExportedSdkLibs() []string {
702 return nil
703}
704
Artur Satayev9cf46692019-11-26 18:08:34 +0000705func (d *AARImport) ExportedPlugins() (android.Paths, []string) {
706 return nil, nil
707}
708
Colin Cross0c4ce212019-05-03 15:28:19 -0700709func (a *AARImport) SrcJarArgs() ([]string, android.Paths) {
710 return nil, nil
711}
712
Colin Crossfabb6082018-02-20 17:22:23 -0800713var _ android.PrebuiltInterface = (*Import)(nil)
714
Colin Cross1b16b0e2019-02-12 14:41:32 -0800715// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
716//
717// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
718// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -0800719func AARImportFactory() android.Module {
720 module := &AARImport{}
721
722 module.AddProperties(&module.properties)
723
724 android.InitPrebuiltModule(module, &module.properties.Aars)
Colin Cross48de9a42018-10-02 13:53:33 -0700725 InitJavaModule(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800726 return module
727}