blob: a4e6f910d10c56c521f91e48e1cda35fade7b2a5 [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() {
37 android.RegisterModuleType("android_library_import", AARImportFactory)
38 android.RegisterModuleType("android_library", AndroidLibraryFactory)
39}
40
41//
42// AAR (android library)
43//
44
45type androidLibraryProperties struct {
46 BuildAAR bool `blueprint:"mutated"`
47}
48
49type aaptProperties struct {
50 // flags passed to aapt when creating the apk
51 Aaptflags []string
52
Dan Willemsen72be5902018-10-24 20:24:57 -070053 // include all resource configurations, not just the product-configured
54 // ones.
55 Aapt_include_all_resources *bool
56
Colin Crossa97c5d32018-03-28 14:58:31 -070057 // list of directories relative to the Blueprints file containing assets.
Colin Cross0ddae7f2019-02-07 15:30:01 -080058 // Defaults to ["assets"] if a directory called assets exists. Set to []
59 // to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070060 Asset_dirs []string
61
62 // list of directories relative to the Blueprints file containing
Colin Cross0ddae7f2019-02-07 15:30:01 -080063 // Android resources. Defaults to ["res"] if a directory called res exists.
64 // Set to [] to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070065 Resource_dirs []string
66
Colin Crossa592e3e2019-02-19 16:59:53 -080067 // list of zip files containing Android resources.
Colin Cross27b922f2019-03-04 22:35:41 -080068 Resource_zips []string `android:"path"`
Colin Crossa592e3e2019-02-19 16:59:53 -080069
Colin Crossa97c5d32018-03-28 14:58:31 -070070 // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml".
Colin Cross27b922f2019-03-04 22:35:41 -080071 Manifest *string `android:"path"`
changho.shinb5432b72019-08-08 18:37:17 +090072
73 // paths to additional manifest files to merge with main manifest.
74 Additional_manifests []string `android:"path"`
Colin Crossa97c5d32018-03-28 14:58:31 -070075}
76
77type aapt struct {
Colin Cross90c25c62019-04-19 16:22:57 -070078 aaptSrcJar android.Path
79 exportPackage android.Path
80 manifestPath android.Path
81 transitiveManifestPaths android.Paths
82 proguardOptionsFile android.Path
83 rroDirs []rroDir
84 rTxt android.Path
85 extraAaptPackagesFile android.Path
86 mergedManifestFile android.Path
Jaewoong Jung5b425e22019-06-17 17:40:56 -070087 noticeFile android.OptionalPath
Colin Cross90c25c62019-04-19 16:22:57 -070088 isLibrary bool
Sasha Smundak6ad77252019-05-01 13:16:22 -070089 useEmbeddedNativeLibs bool
Colin Cross90c25c62019-04-19 16:22:57 -070090 useEmbeddedDex bool
91 usesNonSdkApis bool
Colin Cross5446e882019-05-22 10:46:27 -070092 sdkLibraries []string
Jaewoong Jungc27ab662019-05-30 15:51:14 -070093 hasNoCode bool
Colin Crossa97c5d32018-03-28 14:58:31 -070094
Colin Crosse560c4a2019-03-19 16:03:11 -070095 splitNames []string
96 splits []split
97
Colin Crossa97c5d32018-03-28 14:58:31 -070098 aaptProperties aaptProperties
99}
100
Colin Crosse560c4a2019-03-19 16:03:11 -0700101type split struct {
102 name string
103 suffix string
104 path android.Path
105}
106
Colin Crossa97c5d32018-03-28 14:58:31 -0700107func (a *aapt) ExportPackage() android.Path {
108 return a.exportPackage
109}
110
Anton Hansson53c88442019-03-18 15:53:16 +0000111func (a *aapt) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800112 return a.rroDirs
113}
114
Colin Cross90c25c62019-04-19 16:22:57 -0700115func (a *aapt) ExportedManifests() android.Paths {
116 return a.transitiveManifestPaths
Colin Crossc1c37552019-01-31 11:42:41 -0800117}
118
Colin Crossa0ba2f52019-06-22 12:59:27 -0700119func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext sdkContext,
120 manifestPath android.Path) (compileFlags, linkFlags []string, linkDeps android.Paths,
121 resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700122
123 hasVersionCode := false
124 hasVersionName := false
Colin Crossa97c5d32018-03-28 14:58:31 -0700125 for _, f := range a.aaptProperties.Aaptflags {
126 if strings.HasPrefix(f, "--version-code") {
127 hasVersionCode = true
128 } else if strings.HasPrefix(f, "--version-name") {
129 hasVersionName = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700130 }
131 }
132
Colin Crossa97c5d32018-03-28 14:58:31 -0700133 // Flags specified in Android.bp
134 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
135
136 linkFlags = append(linkFlags, "--no-static-lib-packages")
137
138 // Find implicit or explicit asset and resource dirs
139 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
140 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
Colin Cross8a497952019-03-05 22:25:09 -0800141 resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips)
Colin Crossa97c5d32018-03-28 14:58:31 -0700142
Colin Crossa97c5d32018-03-28 14:58:31 -0700143 // Glob directories into lists of paths
144 for _, dir := range resourceDirs {
145 resDirs = append(resDirs, globbedResourceDir{
146 dir: dir,
147 files: androidResourceGlob(ctx, dir),
148 })
149 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, dir)
150 overlayDirs = append(overlayDirs, resOverlayDirs...)
151 rroDirs = append(rroDirs, resRRODirs...)
152 }
153
154 var assetFiles android.Paths
155 for _, dir := range assetDirs {
156 assetFiles = append(assetFiles, androidResourceGlob(ctx, dir)...)
157 }
158
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700159 assetDirStrings := assetDirs.Strings()
160 if a.noticeFile.Valid() {
161 assetDirStrings = append(assetDirStrings, filepath.Dir(a.noticeFile.Path().String()))
162 assetFiles = append(assetFiles, a.noticeFile.Path())
163 }
164
Colin Crossa97c5d32018-03-28 14:58:31 -0700165 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
166 linkDeps = append(linkDeps, manifestPath)
167
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700168 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirStrings, "-A "))
Colin Crossa97c5d32018-03-28 14:58:31 -0700169 linkDeps = append(linkDeps, assetFiles...)
170
Colin Crossa97c5d32018-03-28 14:58:31 -0700171 // SDK version flags
Colin Cross83bb3162018-06-25 15:48:06 -0700172 minSdkVersion := sdkVersionOrDefault(ctx, sdkContext.minSdkVersion())
Colin Crossa97c5d32018-03-28 14:58:31 -0700173
Colin Cross83bb3162018-06-25 15:48:06 -0700174 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
175 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700176
Colin Crossa97c5d32018-03-28 14:58:31 -0700177 // Version code
178 if !hasVersionCode {
179 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion())
180 }
181
182 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700183 var versionName string
184 if ctx.ModuleName() == "framework-res" {
185 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
186 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700187 // if it contains the build number. Use the PlatformVersionName instead.
188 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700189 } else {
190 versionName = ctx.Config().AppsDefaultVersionName()
191 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800192 versionName = proptools.NinjaEscape(versionName)
Colin Crossa97c5d32018-03-28 14:58:31 -0700193 linkFlags = append(linkFlags, "--version-name ", versionName)
194 }
195
Colin Crossa0ba2f52019-06-22 12:59:27 -0700196 linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"})
197
198 // Always set --pseudo-localize, it will be stripped out later for release
199 // builds that don't want it.
200 compileFlags = append(compileFlags, "--pseudo-localize")
201
202 return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
Colin Crossa97c5d32018-03-28 14:58:31 -0700203}
204
Paul Duffin250e6192019-06-07 10:44:37 +0100205func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) {
Colin Cross42308aa2018-11-14 21:44:17 -0800206 if sdkDep.frameworkResModule != "" {
207 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700208 }
209}
210
Colin Cross83bb3162018-06-25 15:48:06 -0700211func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, extraLinkFlags ...string) {
Colin Cross5446e882019-05-22 10:46:27 -0700212
213 transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, libDeps, libFlags, sdkLibraries :=
214 aaptLibs(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700215
Colin Cross31656952018-05-24 16:11:20 -0700216 // App manifest file
217 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
218 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
219
Colin Cross5446e882019-05-22 10:46:27 -0700220 manifestPath := manifestFixer(ctx, manifestSrcPath, sdkContext, sdkLibraries,
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700221 a.isLibrary, a.useEmbeddedNativeLibs, a.usesNonSdkApis, a.useEmbeddedDex, a.hasNoCode)
Colin Cross90c25c62019-04-19 16:22:57 -0700222
223 a.transitiveManifestPaths = append(android.Paths{manifestPath}, transitiveStaticLibManifests...)
224
225 if len(transitiveStaticLibManifests) > 0 {
changho.shinb5432b72019-08-08 18:37:17 +0900226 // Merge additional manifest files with app manifest.
227 additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)
228 additionalManifests = append(additionalManifests, transitiveStaticLibManifests...)
229
230 a.mergedManifestFile = manifestMerger(ctx, manifestPath, additionalManifests, a.isLibrary)
Colin Cross90c25c62019-04-19 16:22:57 -0700231 if !a.isLibrary {
232 // Only use the merged manifest for applications. For libraries, the transitive closure of manifests
233 // will be propagated to the final application and merged there. The merged manifest for libraries is
234 // only passed to Make, which can't handle transitive dependencies.
235 manifestPath = a.mergedManifestFile
236 }
237 } else {
238 a.mergedManifestFile = manifestPath
239 }
Colin Cross31656952018-05-24 16:11:20 -0700240
Colin Crossa0ba2f52019-06-22 12:59:27 -0700241 compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700242
Colin Crossc1c37552019-01-31 11:42:41 -0800243 rroDirs = append(rroDirs, staticRRODirs...)
Colin Cross31656952018-05-24 16:11:20 -0700244 linkFlags = append(linkFlags, libFlags...)
245 linkDeps = append(linkDeps, libDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700246 linkFlags = append(linkFlags, extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700247 if a.isLibrary {
248 linkFlags = append(linkFlags, "--static-lib")
249 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700250
251 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
252 srcJar := android.PathForModuleGen(ctx, "R.jar")
253 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
254 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700255 // This file isn't used by Soong, but is generated for exporting
256 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Crossa97c5d32018-03-28 14:58:31 -0700257
Colin Cross4aaa84a2018-08-21 15:14:37 -0700258 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700259 for _, dir := range resDirs {
Colin Crossa0ba2f52019-06-22 12:59:27 -0700260 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700261 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700262
Colin Crossa592e3e2019-02-19 16:59:53 -0800263 for i, zip := range resZips {
264 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
Colin Crossa0ba2f52019-06-22 12:59:27 -0700265 aapt2CompileZip(ctx, flata, zip, "", compileFlags)
Colin Crossa592e3e2019-02-19 16:59:53 -0800266 compiledResDirs = append(compiledResDirs, android.Paths{flata})
267 }
268
Colin Cross4aaa84a2018-08-21 15:14:37 -0700269 var compiledRes, compiledOverlay android.Paths
270
271 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
272
Colin Crossbec85302019-02-13 13:15:46 -0800273 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700274 // If we are using static android libraries, every source file becomes an overlay.
275 // This is to emulate old AAPT behavior which simulated library support.
276 for _, compiledResDir := range compiledResDirs {
277 compiledOverlay = append(compiledOverlay, compiledResDir...)
278 }
Colin Crossbec85302019-02-13 13:15:46 -0800279 } else if a.isLibrary {
280 // Otherwise, for a static library we treat all the resources equally with no overlay.
281 for _, compiledResDir := range compiledResDirs {
282 compiledRes = append(compiledRes, compiledResDir...)
283 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700284 } else if len(compiledResDirs) > 0 {
285 // Without static libraries, the first directory is our directory, which can then be
286 // overlaid by the rest.
287 compiledRes = append(compiledRes, compiledResDirs[0]...)
288 for _, compiledResDir := range compiledResDirs[1:] {
289 compiledOverlay = append(compiledOverlay, compiledResDir...)
290 }
291 }
292
Colin Crossa97c5d32018-03-28 14:58:31 -0700293 for _, dir := range overlayDirs {
Colin Crossa0ba2f52019-06-22 12:59:27 -0700294 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700295 }
296
Colin Crosse560c4a2019-03-19 16:03:11 -0700297 var splitPackages android.WritablePaths
298 var splits []split
299
300 for _, s := range a.splitNames {
301 suffix := strings.Replace(s, ",", "_", -1)
302 path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
303 linkFlags = append(linkFlags, "--split", path.String()+":"+s)
304 splitPackages = append(splitPackages, path)
305 splits = append(splits, split{
306 name: s,
307 suffix: suffix,
308 path: path,
309 })
310 }
311
Colin Cross66f78822018-05-02 12:58:28 -0700312 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
Colin Crosse560c4a2019-03-19 16:03:11 -0700313 linkFlags, linkDeps, compiledRes, compiledOverlay, splitPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700314
315 a.aaptSrcJar = srcJar
316 a.exportPackage = packageRes
317 a.manifestPath = manifestPath
318 a.proguardOptionsFile = proguardOptionsFile
319 a.rroDirs = rroDirs
Colin Cross66f78822018-05-02 12:58:28 -0700320 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700321 a.rTxt = rTxt
Colin Crosse560c4a2019-03-19 16:03:11 -0700322 a.splits = splits
Colin Crossa97c5d32018-03-28 14:58:31 -0700323}
324
325// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Colin Cross90c25c62019-04-19 16:22:57 -0700326func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, transitiveStaticLibManifests android.Paths,
Colin Cross5446e882019-05-22 10:46:27 -0700327 staticRRODirs []rroDir, deps android.Paths, flags []string, sdkLibraries []string) {
Colin Cross66f78822018-05-02 12:58:28 -0700328
Colin Crossa97c5d32018-03-28 14:58:31 -0700329 var sharedLibs android.Paths
330
Colin Cross83bb3162018-06-25 15:48:06 -0700331 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700332 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700333 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700334 }
335
336 ctx.VisitDirectDeps(func(module android.Module) {
337 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700338 aarDep, _ := module.(AndroidLibraryDependency)
339 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700340 exportPackage = aarDep.ExportPackage()
341 }
342
343 switch ctx.OtherModuleDependencyTag(module) {
Colin Cross4b964c02018-10-15 16:18:06 -0700344 case instrumentationForTag:
345 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Colin Cross5446e882019-05-22 10:46:27 -0700346 case libTag:
347 if exportPackage != nil {
348 sharedLibs = append(sharedLibs, exportPackage)
349 }
350
351 if _, ok := module.(SdkLibraryDependency); ok {
352 sdkLibraries = append(sdkLibraries, ctx.OtherModuleName(module))
353 }
354
355 case frameworkResTag:
Colin Crossa97c5d32018-03-28 14:58:31 -0700356 if exportPackage != nil {
357 sharedLibs = append(sharedLibs, exportPackage)
358 }
359 case staticLibTag:
360 if exportPackage != nil {
Colin Cross66f78822018-05-02 12:58:28 -0700361 transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
Colin Crossbec85302019-02-13 13:15:46 -0800362 transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
Colin Cross90c25c62019-04-19 16:22:57 -0700363 transitiveStaticLibManifests = append(transitiveStaticLibManifests, aarDep.ExportedManifests()...)
Colin Cross5446e882019-05-22 10:46:27 -0700364 sdkLibraries = append(sdkLibraries, aarDep.ExportedSdkLibs()...)
Anton Hansson53c88442019-03-18 15:53:16 +0000365
366 outer:
367 for _, d := range aarDep.ExportedRRODirs() {
368 for _, e := range staticRRODirs {
369 if d.path == e.path {
370 continue outer
371 }
372 }
373 staticRRODirs = append(staticRRODirs, d)
374 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700375 }
376 }
377 })
378
379 deps = append(deps, sharedLibs...)
Colin Cross66f78822018-05-02 12:58:28 -0700380 deps = append(deps, transitiveStaticLibs...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700381
Colin Cross66f78822018-05-02 12:58:28 -0700382 if len(transitiveStaticLibs) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700383 flags = append(flags, "--auto-add-overlay")
384 }
385
386 for _, sharedLib := range sharedLibs {
387 flags = append(flags, "-I "+sharedLib.String())
388 }
389
Colin Cross66f78822018-05-02 12:58:28 -0700390 transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
Colin Cross90c25c62019-04-19 16:22:57 -0700391 transitiveStaticLibManifests = android.FirstUniquePaths(transitiveStaticLibManifests)
Colin Cross5446e882019-05-22 10:46:27 -0700392 sdkLibraries = android.FirstUniqueStrings(sdkLibraries)
Colin Cross66f78822018-05-02 12:58:28 -0700393
Colin Cross5446e882019-05-22 10:46:27 -0700394 return transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, deps, flags, sdkLibraries
Colin Crossa97c5d32018-03-28 14:58:31 -0700395}
396
397type AndroidLibrary struct {
398 Library
399 aapt
400
401 androidLibraryProperties androidLibraryProperties
402
403 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700404
405 exportedProguardFlagFiles android.Paths
Colin Cross66f78822018-05-02 12:58:28 -0700406 exportedStaticPackages android.Paths
Colin Cross89c31582018-04-30 15:55:11 -0700407}
408
409func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
410 return a.exportedProguardFlagFiles
Colin Crossa97c5d32018-03-28 14:58:31 -0700411}
412
Colin Cross66f78822018-05-02 12:58:28 -0700413func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
414 return a.exportedStaticPackages
415}
416
Colin Crossa97c5d32018-03-28 14:58:31 -0700417var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
418
419func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
420 a.Module.deps(ctx)
Paul Duffin250e6192019-06-07 10:44:37 +0100421 sdkDep := decodeSdkDep(ctx, sdkContext(a))
422 if sdkDep.hasFrameworkLibs() {
423 a.aapt.deps(ctx, sdkDep)
Colin Crossa97c5d32018-03-28 14:58:31 -0700424 }
425}
426
427func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800428 a.aapt.isLibrary = true
Colin Cross5446e882019-05-22 10:46:27 -0700429 a.aapt.sdkLibraries = a.exportedSdkLibs
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700430 a.aapt.buildActions(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700431
432 ctx.CheckbuildFile(a.proguardOptionsFile)
433 ctx.CheckbuildFile(a.exportPackage)
434 ctx.CheckbuildFile(a.aaptSrcJar)
435
436 // apps manifests are handled by aapt, don't let Module see them
437 a.properties.Manifest = nil
438
439 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
440 a.proguardOptionsFile)
441
442 a.Module.compile(ctx, a.aaptSrcJar)
443
Colin Crossf57c5782019-01-25 13:20:38 -0800444 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700445 var res android.Paths
446 if a.androidLibraryProperties.BuildAAR {
447 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
448 ctx.CheckbuildFile(a.aarFile)
449 }
Colin Cross89c31582018-04-30 15:55:11 -0700450
451 ctx.VisitDirectDeps(func(m android.Module) {
452 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
453 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
Colin Cross66f78822018-05-02 12:58:28 -0700454 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
455 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
Colin Cross89c31582018-04-30 15:55:11 -0700456 }
457 })
458
459 a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
Colin Cross66f78822018-05-02 12:58:28 -0700460 a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700461}
462
Colin Cross1b16b0e2019-02-12 14:41:32 -0800463// android_library builds and links sources into a `.jar` file for the device along with Android resources.
464//
465// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
466// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
467// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
468// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700469func AndroidLibraryFactory() android.Module {
470 module := &AndroidLibrary{}
471
472 module.AddProperties(
473 &module.Module.properties,
474 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800475 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700476 &module.Module.protoProperties,
477 &module.aaptProperties,
478 &module.androidLibraryProperties)
479
480 module.androidLibraryProperties.BuildAAR = true
481
Colin Cross48de9a42018-10-02 13:53:33 -0700482 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700483 return module
484}
485
Colin Crossfabb6082018-02-20 17:22:23 -0800486//
487// AAR (android library) prebuilts
488//
Colin Crossfabb6082018-02-20 17:22:23 -0800489
490type AARImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -0800491 Aars []string `android:"path"`
Colin Crossfabb6082018-02-20 17:22:23 -0800492
Colin Cross479884c2018-07-10 13:39:30 -0700493 Sdk_version *string
494 Min_sdk_version *string
Colin Crossa97c5d32018-03-28 14:58:31 -0700495
496 Static_libs []string
497 Libs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -0700498
499 // if set to true, run Jetifier against .aar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -0700500 Jetifier *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800501}
502
503type AARImport struct {
504 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700505 android.DefaultableModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800506 prebuilt android.Prebuilt
507
508 properties AARImportProperties
509
Colin Cross66f78822018-05-02 12:58:28 -0700510 classpathFile android.WritablePath
511 proguardFlags android.WritablePath
512 exportPackage android.WritablePath
513 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700514 manifest android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700515
516 exportedStaticPackages android.Paths
Colin Crossfabb6082018-02-20 17:22:23 -0800517}
518
Colin Cross83bb3162018-06-25 15:48:06 -0700519func (a *AARImport) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900520 return proptools.StringDefault(a.properties.Sdk_version, defaultSdkVersion(a))
Colin Cross83bb3162018-06-25 15:48:06 -0700521}
522
523func (a *AARImport) minSdkVersion() string {
Colin Cross479884c2018-07-10 13:39:30 -0700524 if a.properties.Min_sdk_version != nil {
525 return *a.properties.Min_sdk_version
526 }
Colin Cross83bb3162018-06-25 15:48:06 -0700527 return a.sdkVersion()
528}
529
Dan Willemsen419290a2018-10-31 15:28:47 -0700530func (a *AARImport) targetSdkVersion() string {
531 return a.sdkVersion()
532}
533
Colin Crossa97c5d32018-03-28 14:58:31 -0700534var _ AndroidLibraryDependency = (*AARImport)(nil)
535
536func (a *AARImport) ExportPackage() android.Path {
537 return a.exportPackage
538}
539
Colin Cross89c31582018-04-30 15:55:11 -0700540func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
541 return android.Paths{a.proguardFlags}
542}
543
Anton Hansson53c88442019-03-18 15:53:16 +0000544func (a *AARImport) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800545 return nil
546}
547
Colin Cross66f78822018-05-02 12:58:28 -0700548func (a *AARImport) ExportedStaticPackages() android.Paths {
549 return a.exportedStaticPackages
550}
551
Colin Cross90c25c62019-04-19 16:22:57 -0700552func (a *AARImport) ExportedManifests() android.Paths {
553 return android.Paths{a.manifest}
Colin Cross31656952018-05-24 16:11:20 -0700554}
555
Colin Crossfabb6082018-02-20 17:22:23 -0800556func (a *AARImport) Prebuilt() *android.Prebuilt {
557 return &a.prebuilt
558}
559
560func (a *AARImport) Name() string {
561 return a.prebuilt.Name(a.ModuleBase.Name())
562}
563
564func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross10932872019-04-18 14:27:12 -0700565 if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Colin Cross83bb3162018-06-25 15:48:06 -0700566 sdkDep := decodeSdkDep(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700567 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700568 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800569 }
570 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700571
Colin Cross42d48b72018-08-29 14:10:52 -0700572 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
573 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800574}
575
576// 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 -0700577// 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 -0800578var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
579 blueprint.RuleParams{
Dan Willemsen304cfec2019-05-28 14:49:06 -0700580 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
581 `unzip -qo -d $outDir $in && rm -rf $outDir/res && touch $out`,
Colin Crossfabb6082018-02-20 17:22:23 -0800582 },
Dan Willemsen304cfec2019-05-28 14:49:06 -0700583 "outDir")
Colin Crossfabb6082018-02-20 17:22:23 -0800584
585func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
586 if len(a.properties.Aars) != 1 {
587 ctx.PropertyErrorf("aars", "exactly one aar is required")
588 return
589 }
590
Nan Zhang4c819fb2018-08-27 18:31:46 -0700591 aarName := ctx.ModuleName() + ".aar"
592 var aar android.Path
593 aar = android.PathForModuleSrc(ctx, a.properties.Aars[0])
Colin Cross1001a792019-03-21 22:21:39 -0700594 if Bool(a.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -0700595 inputFile := aar
596 aar = android.PathForModuleOut(ctx, "jetifier", aarName)
597 TransformJetifier(ctx, aar.(android.WritablePath), inputFile)
598 }
Colin Crossfabb6082018-02-20 17:22:23 -0800599
600 extractedAARDir := android.PathForModuleOut(ctx, "aar")
Colin Crossfabb6082018-02-20 17:22:23 -0800601 a.classpathFile = extractedAARDir.Join(ctx, "classes.jar")
602 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700603 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossfabb6082018-02-20 17:22:23 -0800604
605 ctx.Build(pctx, android.BuildParams{
606 Rule: unzipAAR,
607 Input: aar,
Colin Cross10f7c4a2018-05-23 10:59:28 -0700608 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest},
Colin Crossfabb6082018-02-20 17:22:23 -0800609 Description: "unzip AAR",
610 Args: map[string]string{
Dan Willemsen304cfec2019-05-28 14:49:06 -0700611 "outDir": extractedAARDir.String(),
Colin Crossfabb6082018-02-20 17:22:23 -0800612 },
613 })
614
Colin Crossa0ba2f52019-06-22 12:59:27 -0700615 // Always set --pseudo-localize, it will be stripped out later for release
616 // builds that don't want it.
617 compileFlags := []string{"--pseudo-localize"}
Colin Crossfabb6082018-02-20 17:22:23 -0800618 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
Colin Crossfabb6082018-02-20 17:22:23 -0800619 flata := compiledResDir.Join(ctx, "gen_res.flata")
Colin Crossa0ba2f52019-06-22 12:59:27 -0700620 aapt2CompileZip(ctx, flata, aar, "res", compileFlags)
Colin Crossfabb6082018-02-20 17:22:23 -0800621
622 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
623 srcJar := android.PathForModuleGen(ctx, "R.jar")
624 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700625 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700626 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800627
628 var linkDeps android.Paths
629
630 linkFlags := []string{
631 "--static-lib",
632 "--no-static-lib-packages",
633 "--auto-add-overlay",
634 }
635
Colin Cross10f7c4a2018-05-23 10:59:28 -0700636 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
637 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800638
Colin Cross5446e882019-05-22 10:46:27 -0700639 transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags, sdkLibraries :=
640 aaptLibs(ctx, sdkContext(a))
Colin Cross31656952018-05-24 16:11:20 -0700641
642 _ = staticLibManifests
Colin Crossc1c37552019-01-31 11:42:41 -0800643 _ = staticRRODirs
Colin Cross5446e882019-05-22 10:46:27 -0700644 _ = sdkLibraries
Colin Crossfabb6082018-02-20 17:22:23 -0800645
Colin Crossa97c5d32018-03-28 14:58:31 -0700646 linkDeps = append(linkDeps, libDeps...)
647 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800648
Colin Cross66f78822018-05-02 12:58:28 -0700649 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800650
Colin Cross66f78822018-05-02 12:58:28 -0700651 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Colin Crosse560c4a2019-03-19 16:03:11 -0700652 linkFlags, linkDeps, nil, overlayRes, nil)
Colin Crossfabb6082018-02-20 17:22:23 -0800653}
654
655var _ Dependency = (*AARImport)(nil)
656
657func (a *AARImport) HeaderJars() android.Paths {
658 return android.Paths{a.classpathFile}
659}
660
661func (a *AARImport) ImplementationJars() android.Paths {
662 return android.Paths{a.classpathFile}
663}
664
Colin Cross331a1212018-08-15 20:40:52 -0700665func (a *AARImport) ResourceJars() android.Paths {
666 return nil
667}
668
669func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
670 return android.Paths{a.classpathFile}
671}
672
Colin Crossf24a22a2019-01-31 14:12:44 -0800673func (a *AARImport) DexJar() android.Path {
674 return nil
675}
676
Colin Crossfabb6082018-02-20 17:22:23 -0800677func (a *AARImport) AidlIncludeDirs() android.Paths {
678 return nil
679}
680
Jiyong Park1be96912018-05-28 18:02:19 +0900681func (a *AARImport) ExportedSdkLibs() []string {
682 return nil
683}
684
Colin Cross0c4ce212019-05-03 15:28:19 -0700685func (a *AARImport) SrcJarArgs() ([]string, android.Paths) {
686 return nil, nil
687}
688
Colin Crossfabb6082018-02-20 17:22:23 -0800689var _ android.PrebuiltInterface = (*Import)(nil)
690
Colin Cross1b16b0e2019-02-12 14:41:32 -0800691// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
692//
693// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
694// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -0800695func AARImportFactory() android.Module {
696 module := &AARImport{}
697
698 module.AddProperties(&module.properties)
699
700 android.InitPrebuiltModule(module, &module.properties.Aars)
Colin Cross48de9a42018-10-02 13:53:33 -0700701 InitJavaModule(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800702 return module
703}