blob: 96eb5c0fc422e6e0eed37b4b520828827df904ca [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"`
Sasha Smundak541056c2019-10-28 15:50:06 -070075
76 // do not include AndroidManifest from dependent libraries
77 Dont_merge_manifests *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070078}
79
80type aapt struct {
Colin Cross90c25c62019-04-19 16:22:57 -070081 aaptSrcJar android.Path
82 exportPackage android.Path
83 manifestPath android.Path
84 transitiveManifestPaths android.Paths
85 proguardOptionsFile android.Path
86 rroDirs []rroDir
87 rTxt android.Path
88 extraAaptPackagesFile android.Path
89 mergedManifestFile android.Path
Jaewoong Jung5b425e22019-06-17 17:40:56 -070090 noticeFile android.OptionalPath
Colin Cross90c25c62019-04-19 16:22:57 -070091 isLibrary bool
Sasha Smundak6ad77252019-05-01 13:16:22 -070092 useEmbeddedNativeLibs bool
Colin Cross90c25c62019-04-19 16:22:57 -070093 useEmbeddedDex bool
94 usesNonSdkApis bool
Colin Cross5446e882019-05-22 10:46:27 -070095 sdkLibraries []string
Jaewoong Jungc27ab662019-05-30 15:51:14 -070096 hasNoCode bool
Colin Crossa97c5d32018-03-28 14:58:31 -070097
Colin Crosse560c4a2019-03-19 16:03:11 -070098 splitNames []string
99 splits []split
100
Colin Crossa97c5d32018-03-28 14:58:31 -0700101 aaptProperties aaptProperties
102}
103
Colin Crosse560c4a2019-03-19 16:03:11 -0700104type split struct {
105 name string
106 suffix string
107 path android.Path
108}
109
Colin Crossa97c5d32018-03-28 14:58:31 -0700110func (a *aapt) ExportPackage() android.Path {
111 return a.exportPackage
112}
113
Anton Hansson53c88442019-03-18 15:53:16 +0000114func (a *aapt) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800115 return a.rroDirs
116}
117
Colin Cross90c25c62019-04-19 16:22:57 -0700118func (a *aapt) ExportedManifests() android.Paths {
119 return a.transitiveManifestPaths
Colin Crossc1c37552019-01-31 11:42:41 -0800120}
121
Colin Crossa0ba2f52019-06-22 12:59:27 -0700122func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext sdkContext,
123 manifestPath android.Path) (compileFlags, linkFlags []string, linkDeps android.Paths,
124 resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700125
126 hasVersionCode := false
127 hasVersionName := false
Colin Crossa97c5d32018-03-28 14:58:31 -0700128 for _, f := range a.aaptProperties.Aaptflags {
129 if strings.HasPrefix(f, "--version-code") {
130 hasVersionCode = true
131 } else if strings.HasPrefix(f, "--version-name") {
132 hasVersionName = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700133 }
134 }
135
Colin Crossa97c5d32018-03-28 14:58:31 -0700136 // Flags specified in Android.bp
137 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
138
139 linkFlags = append(linkFlags, "--no-static-lib-packages")
140
141 // Find implicit or explicit asset and resource dirs
142 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
143 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
Colin Cross8a497952019-03-05 22:25:09 -0800144 resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips)
Colin Crossa97c5d32018-03-28 14:58:31 -0700145
Colin Crossa97c5d32018-03-28 14:58:31 -0700146 // Glob directories into lists of paths
147 for _, dir := range resourceDirs {
148 resDirs = append(resDirs, globbedResourceDir{
149 dir: dir,
150 files: androidResourceGlob(ctx, dir),
151 })
152 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, dir)
153 overlayDirs = append(overlayDirs, resOverlayDirs...)
154 rroDirs = append(rroDirs, resRRODirs...)
155 }
156
157 var assetFiles android.Paths
158 for _, dir := range assetDirs {
159 assetFiles = append(assetFiles, androidResourceGlob(ctx, dir)...)
160 }
161
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700162 assetDirStrings := assetDirs.Strings()
163 if a.noticeFile.Valid() {
164 assetDirStrings = append(assetDirStrings, filepath.Dir(a.noticeFile.Path().String()))
165 assetFiles = append(assetFiles, a.noticeFile.Path())
166 }
167
Colin Crossa97c5d32018-03-28 14:58:31 -0700168 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
169 linkDeps = append(linkDeps, manifestPath)
170
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700171 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirStrings, "-A "))
Colin Crossa97c5d32018-03-28 14:58:31 -0700172 linkDeps = append(linkDeps, assetFiles...)
173
Colin Crossa97c5d32018-03-28 14:58:31 -0700174 // SDK version flags
Colin Cross83bb3162018-06-25 15:48:06 -0700175 minSdkVersion := sdkVersionOrDefault(ctx, sdkContext.minSdkVersion())
Colin Crossa97c5d32018-03-28 14:58:31 -0700176
Colin Cross83bb3162018-06-25 15:48:06 -0700177 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
178 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700179
Colin Crossa97c5d32018-03-28 14:58:31 -0700180 // Version code
181 if !hasVersionCode {
182 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion())
183 }
184
185 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700186 var versionName string
187 if ctx.ModuleName() == "framework-res" {
188 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
189 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700190 // if it contains the build number. Use the PlatformVersionName instead.
191 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700192 } else {
193 versionName = ctx.Config().AppsDefaultVersionName()
194 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800195 versionName = proptools.NinjaEscape(versionName)
Colin Crossa97c5d32018-03-28 14:58:31 -0700196 linkFlags = append(linkFlags, "--version-name ", versionName)
197 }
198
Colin Crossa0ba2f52019-06-22 12:59:27 -0700199 linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"})
200
201 // Always set --pseudo-localize, it will be stripped out later for release
202 // builds that don't want it.
203 compileFlags = append(compileFlags, "--pseudo-localize")
204
205 return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
Colin Crossa97c5d32018-03-28 14:58:31 -0700206}
207
Paul Duffin250e6192019-06-07 10:44:37 +0100208func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) {
Colin Cross42308aa2018-11-14 21:44:17 -0800209 if sdkDep.frameworkResModule != "" {
210 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700211 }
212}
213
Colin Cross83bb3162018-06-25 15:48:06 -0700214func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, extraLinkFlags ...string) {
Colin Cross5446e882019-05-22 10:46:27 -0700215
216 transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, libDeps, libFlags, sdkLibraries :=
217 aaptLibs(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700218
Colin Cross31656952018-05-24 16:11:20 -0700219 // App manifest file
220 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
221 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
222
Colin Cross5446e882019-05-22 10:46:27 -0700223 manifestPath := manifestFixer(ctx, manifestSrcPath, sdkContext, sdkLibraries,
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700224 a.isLibrary, a.useEmbeddedNativeLibs, a.usesNonSdkApis, a.useEmbeddedDex, a.hasNoCode)
Colin Cross90c25c62019-04-19 16:22:57 -0700225
Luca Stefanifd898822019-09-10 22:13:31 +0200226 // Add additional manifest files to transitive manifests.
227 additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)
228 a.transitiveManifestPaths = append(android.Paths{manifestPath}, additionalManifests...)
229 a.transitiveManifestPaths = append(a.transitiveManifestPaths, transitiveStaticLibManifests...)
Colin Cross90c25c62019-04-19 16:22:57 -0700230
Sasha Smundak541056c2019-10-28 15:50:06 -0700231 if len(a.transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) {
Luca Stefanifd898822019-09-10 22:13:31 +0200232 a.mergedManifestFile = manifestMerger(ctx, a.transitiveManifestPaths[0], a.transitiveManifestPaths[1:], a.isLibrary)
Colin Cross90c25c62019-04-19 16:22:57 -0700233 if !a.isLibrary {
234 // Only use the merged manifest for applications. For libraries, the transitive closure of manifests
235 // will be propagated to the final application and merged there. The merged manifest for libraries is
236 // only passed to Make, which can't handle transitive dependencies.
237 manifestPath = a.mergedManifestFile
238 }
239 } else {
240 a.mergedManifestFile = manifestPath
241 }
Colin Cross31656952018-05-24 16:11:20 -0700242
Colin Crossa0ba2f52019-06-22 12:59:27 -0700243 compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700244
Colin Crossc1c37552019-01-31 11:42:41 -0800245 rroDirs = append(rroDirs, staticRRODirs...)
Colin Cross31656952018-05-24 16:11:20 -0700246 linkFlags = append(linkFlags, libFlags...)
247 linkDeps = append(linkDeps, libDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700248 linkFlags = append(linkFlags, extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700249 if a.isLibrary {
250 linkFlags = append(linkFlags, "--static-lib")
251 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700252
253 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900254 // the subdir "android" is required to be filtered by package names
255 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700256 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
257 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700258 // This file isn't used by Soong, but is generated for exporting
259 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Crossa97c5d32018-03-28 14:58:31 -0700260
Colin Cross4aaa84a2018-08-21 15:14:37 -0700261 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700262 for _, dir := range resDirs {
Colin Crossa0ba2f52019-06-22 12:59:27 -0700263 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700264 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700265
Colin Crossa592e3e2019-02-19 16:59:53 -0800266 for i, zip := range resZips {
267 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
Colin Crossa0ba2f52019-06-22 12:59:27 -0700268 aapt2CompileZip(ctx, flata, zip, "", compileFlags)
Colin Crossa592e3e2019-02-19 16:59:53 -0800269 compiledResDirs = append(compiledResDirs, android.Paths{flata})
270 }
271
Colin Cross4aaa84a2018-08-21 15:14:37 -0700272 var compiledRes, compiledOverlay android.Paths
273
274 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
275
Colin Crossbec85302019-02-13 13:15:46 -0800276 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700277 // If we are using static android libraries, every source file becomes an overlay.
278 // This is to emulate old AAPT behavior which simulated library support.
279 for _, compiledResDir := range compiledResDirs {
280 compiledOverlay = append(compiledOverlay, compiledResDir...)
281 }
Colin Crossbec85302019-02-13 13:15:46 -0800282 } else if a.isLibrary {
283 // Otherwise, for a static library we treat all the resources equally with no overlay.
284 for _, compiledResDir := range compiledResDirs {
285 compiledRes = append(compiledRes, compiledResDir...)
286 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700287 } else if len(compiledResDirs) > 0 {
288 // Without static libraries, the first directory is our directory, which can then be
289 // overlaid by the rest.
290 compiledRes = append(compiledRes, compiledResDirs[0]...)
291 for _, compiledResDir := range compiledResDirs[1:] {
292 compiledOverlay = append(compiledOverlay, compiledResDir...)
293 }
294 }
295
Colin Crossa97c5d32018-03-28 14:58:31 -0700296 for _, dir := range overlayDirs {
Colin Crossa0ba2f52019-06-22 12:59:27 -0700297 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700298 }
299
Colin Crosse560c4a2019-03-19 16:03:11 -0700300 var splitPackages android.WritablePaths
301 var splits []split
302
303 for _, s := range a.splitNames {
304 suffix := strings.Replace(s, ",", "_", -1)
305 path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
306 linkFlags = append(linkFlags, "--split", path.String()+":"+s)
307 splitPackages = append(splitPackages, path)
308 splits = append(splits, split{
309 name: s,
310 suffix: suffix,
311 path: path,
312 })
313 }
314
Colin Cross66f78822018-05-02 12:58:28 -0700315 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
Colin Crosse560c4a2019-03-19 16:03:11 -0700316 linkFlags, linkDeps, compiledRes, compiledOverlay, splitPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700317
318 a.aaptSrcJar = srcJar
319 a.exportPackage = packageRes
320 a.manifestPath = manifestPath
321 a.proguardOptionsFile = proguardOptionsFile
322 a.rroDirs = rroDirs
Colin Cross66f78822018-05-02 12:58:28 -0700323 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700324 a.rTxt = rTxt
Colin Crosse560c4a2019-03-19 16:03:11 -0700325 a.splits = splits
Colin Crossa97c5d32018-03-28 14:58:31 -0700326}
327
328// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Colin Cross90c25c62019-04-19 16:22:57 -0700329func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, transitiveStaticLibManifests android.Paths,
Colin Cross5446e882019-05-22 10:46:27 -0700330 staticRRODirs []rroDir, deps android.Paths, flags []string, sdkLibraries []string) {
Colin Cross66f78822018-05-02 12:58:28 -0700331
Colin Crossa97c5d32018-03-28 14:58:31 -0700332 var sharedLibs android.Paths
333
Colin Cross83bb3162018-06-25 15:48:06 -0700334 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700335 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700336 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700337 }
338
339 ctx.VisitDirectDeps(func(module android.Module) {
340 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700341 aarDep, _ := module.(AndroidLibraryDependency)
342 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700343 exportPackage = aarDep.ExportPackage()
344 }
345
346 switch ctx.OtherModuleDependencyTag(module) {
Colin Cross4b964c02018-10-15 16:18:06 -0700347 case instrumentationForTag:
348 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Colin Cross5446e882019-05-22 10:46:27 -0700349 case libTag:
350 if exportPackage != nil {
351 sharedLibs = append(sharedLibs, exportPackage)
352 }
353
354 if _, ok := module.(SdkLibraryDependency); ok {
355 sdkLibraries = append(sdkLibraries, ctx.OtherModuleName(module))
356 }
357
358 case frameworkResTag:
Colin Crossa97c5d32018-03-28 14:58:31 -0700359 if exportPackage != nil {
360 sharedLibs = append(sharedLibs, exportPackage)
361 }
362 case staticLibTag:
363 if exportPackage != nil {
Colin Cross66f78822018-05-02 12:58:28 -0700364 transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
Colin Crossbec85302019-02-13 13:15:46 -0800365 transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
Colin Cross90c25c62019-04-19 16:22:57 -0700366 transitiveStaticLibManifests = append(transitiveStaticLibManifests, aarDep.ExportedManifests()...)
Colin Cross5446e882019-05-22 10:46:27 -0700367 sdkLibraries = append(sdkLibraries, aarDep.ExportedSdkLibs()...)
Anton Hansson53c88442019-03-18 15:53:16 +0000368
369 outer:
370 for _, d := range aarDep.ExportedRRODirs() {
371 for _, e := range staticRRODirs {
372 if d.path == e.path {
373 continue outer
374 }
375 }
376 staticRRODirs = append(staticRRODirs, d)
377 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700378 }
379 }
380 })
381
382 deps = append(deps, sharedLibs...)
Colin Cross66f78822018-05-02 12:58:28 -0700383 deps = append(deps, transitiveStaticLibs...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700384
Colin Cross66f78822018-05-02 12:58:28 -0700385 if len(transitiveStaticLibs) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700386 flags = append(flags, "--auto-add-overlay")
387 }
388
389 for _, sharedLib := range sharedLibs {
390 flags = append(flags, "-I "+sharedLib.String())
391 }
392
Colin Cross66f78822018-05-02 12:58:28 -0700393 transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
Colin Cross90c25c62019-04-19 16:22:57 -0700394 transitiveStaticLibManifests = android.FirstUniquePaths(transitiveStaticLibManifests)
Colin Cross5446e882019-05-22 10:46:27 -0700395 sdkLibraries = android.FirstUniqueStrings(sdkLibraries)
Colin Cross66f78822018-05-02 12:58:28 -0700396
Colin Cross5446e882019-05-22 10:46:27 -0700397 return transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, deps, flags, sdkLibraries
Colin Crossa97c5d32018-03-28 14:58:31 -0700398}
399
400type AndroidLibrary struct {
401 Library
402 aapt
403
404 androidLibraryProperties androidLibraryProperties
405
406 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700407
408 exportedProguardFlagFiles android.Paths
Colin Cross66f78822018-05-02 12:58:28 -0700409 exportedStaticPackages android.Paths
Colin Cross89c31582018-04-30 15:55:11 -0700410}
411
412func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
413 return a.exportedProguardFlagFiles
Colin Crossa97c5d32018-03-28 14:58:31 -0700414}
415
Colin Cross66f78822018-05-02 12:58:28 -0700416func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
417 return a.exportedStaticPackages
418}
419
Colin Crossa97c5d32018-03-28 14:58:31 -0700420var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
421
422func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
423 a.Module.deps(ctx)
Paul Duffin250e6192019-06-07 10:44:37 +0100424 sdkDep := decodeSdkDep(ctx, sdkContext(a))
425 if sdkDep.hasFrameworkLibs() {
426 a.aapt.deps(ctx, sdkDep)
Colin Crossa97c5d32018-03-28 14:58:31 -0700427 }
428}
429
430func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800431 a.aapt.isLibrary = true
Colin Cross5446e882019-05-22 10:46:27 -0700432 a.aapt.sdkLibraries = a.exportedSdkLibs
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700433 a.aapt.buildActions(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700434
435 ctx.CheckbuildFile(a.proguardOptionsFile)
436 ctx.CheckbuildFile(a.exportPackage)
437 ctx.CheckbuildFile(a.aaptSrcJar)
438
439 // apps manifests are handled by aapt, don't let Module see them
440 a.properties.Manifest = nil
441
442 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
443 a.proguardOptionsFile)
444
445 a.Module.compile(ctx, a.aaptSrcJar)
446
Colin Crossf57c5782019-01-25 13:20:38 -0800447 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700448 var res android.Paths
449 if a.androidLibraryProperties.BuildAAR {
450 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
451 ctx.CheckbuildFile(a.aarFile)
452 }
Colin Cross89c31582018-04-30 15:55:11 -0700453
454 ctx.VisitDirectDeps(func(m android.Module) {
455 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
456 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
Colin Cross66f78822018-05-02 12:58:28 -0700457 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
458 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
Colin Cross89c31582018-04-30 15:55:11 -0700459 }
460 })
461
462 a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
Colin Cross66f78822018-05-02 12:58:28 -0700463 a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700464}
465
Colin Cross1b16b0e2019-02-12 14:41:32 -0800466// android_library builds and links sources into a `.jar` file for the device along with Android resources.
467//
468// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
469// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
470// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
471// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700472func AndroidLibraryFactory() android.Module {
473 module := &AndroidLibrary{}
474
475 module.AddProperties(
476 &module.Module.properties,
477 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800478 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700479 &module.Module.protoProperties,
480 &module.aaptProperties,
481 &module.androidLibraryProperties)
482
483 module.androidLibraryProperties.BuildAAR = true
484
Colin Cross48de9a42018-10-02 13:53:33 -0700485 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700486 return module
487}
488
Colin Crossfabb6082018-02-20 17:22:23 -0800489//
490// AAR (android library) prebuilts
491//
Colin Crossfabb6082018-02-20 17:22:23 -0800492
493type AARImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -0800494 Aars []string `android:"path"`
Colin Crossfabb6082018-02-20 17:22:23 -0800495
Colin Cross479884c2018-07-10 13:39:30 -0700496 Sdk_version *string
497 Min_sdk_version *string
Colin Crossa97c5d32018-03-28 14:58:31 -0700498
499 Static_libs []string
500 Libs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -0700501
502 // if set to true, run Jetifier against .aar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -0700503 Jetifier *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800504}
505
506type AARImport struct {
507 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700508 android.DefaultableModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800509 prebuilt android.Prebuilt
510
511 properties AARImportProperties
512
Colin Cross66f78822018-05-02 12:58:28 -0700513 classpathFile android.WritablePath
514 proguardFlags android.WritablePath
515 exportPackage android.WritablePath
516 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700517 manifest android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700518
519 exportedStaticPackages android.Paths
Colin Crossfabb6082018-02-20 17:22:23 -0800520}
521
Colin Cross83bb3162018-06-25 15:48:06 -0700522func (a *AARImport) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900523 return proptools.StringDefault(a.properties.Sdk_version, defaultSdkVersion(a))
Colin Cross83bb3162018-06-25 15:48:06 -0700524}
525
Paul Duffine25c6442019-10-11 13:50:28 +0100526func (a *AARImport) systemModules() string {
527 return ""
528}
529
Colin Cross83bb3162018-06-25 15:48:06 -0700530func (a *AARImport) minSdkVersion() string {
Colin Cross479884c2018-07-10 13:39:30 -0700531 if a.properties.Min_sdk_version != nil {
532 return *a.properties.Min_sdk_version
533 }
Colin Cross83bb3162018-06-25 15:48:06 -0700534 return a.sdkVersion()
535}
536
Dan Willemsen419290a2018-10-31 15:28:47 -0700537func (a *AARImport) targetSdkVersion() string {
538 return a.sdkVersion()
539}
540
Colin Crossa97c5d32018-03-28 14:58:31 -0700541var _ AndroidLibraryDependency = (*AARImport)(nil)
542
543func (a *AARImport) ExportPackage() android.Path {
544 return a.exportPackage
545}
546
Colin Cross89c31582018-04-30 15:55:11 -0700547func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
548 return android.Paths{a.proguardFlags}
549}
550
Anton Hansson53c88442019-03-18 15:53:16 +0000551func (a *AARImport) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800552 return nil
553}
554
Colin Cross66f78822018-05-02 12:58:28 -0700555func (a *AARImport) ExportedStaticPackages() android.Paths {
556 return a.exportedStaticPackages
557}
558
Colin Cross90c25c62019-04-19 16:22:57 -0700559func (a *AARImport) ExportedManifests() android.Paths {
560 return android.Paths{a.manifest}
Colin Cross31656952018-05-24 16:11:20 -0700561}
562
Colin Crossfabb6082018-02-20 17:22:23 -0800563func (a *AARImport) Prebuilt() *android.Prebuilt {
564 return &a.prebuilt
565}
566
567func (a *AARImport) Name() string {
568 return a.prebuilt.Name(a.ModuleBase.Name())
569}
570
571func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross10932872019-04-18 14:27:12 -0700572 if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Colin Cross83bb3162018-06-25 15:48:06 -0700573 sdkDep := decodeSdkDep(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700574 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700575 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800576 }
577 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700578
Colin Cross42d48b72018-08-29 14:10:52 -0700579 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
580 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800581}
582
583// 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 -0700584// 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 -0800585var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
586 blueprint.RuleParams{
Dan Willemsen304cfec2019-05-28 14:49:06 -0700587 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
588 `unzip -qo -d $outDir $in && rm -rf $outDir/res && touch $out`,
Colin Crossfabb6082018-02-20 17:22:23 -0800589 },
Dan Willemsen304cfec2019-05-28 14:49:06 -0700590 "outDir")
Colin Crossfabb6082018-02-20 17:22:23 -0800591
592func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
593 if len(a.properties.Aars) != 1 {
594 ctx.PropertyErrorf("aars", "exactly one aar is required")
595 return
596 }
597
Nan Zhang4c819fb2018-08-27 18:31:46 -0700598 aarName := ctx.ModuleName() + ".aar"
599 var aar android.Path
600 aar = android.PathForModuleSrc(ctx, a.properties.Aars[0])
Colin Cross1001a792019-03-21 22:21:39 -0700601 if Bool(a.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -0700602 inputFile := aar
603 aar = android.PathForModuleOut(ctx, "jetifier", aarName)
604 TransformJetifier(ctx, aar.(android.WritablePath), inputFile)
605 }
Colin Crossfabb6082018-02-20 17:22:23 -0800606
607 extractedAARDir := android.PathForModuleOut(ctx, "aar")
Colin Crossfabb6082018-02-20 17:22:23 -0800608 a.classpathFile = extractedAARDir.Join(ctx, "classes.jar")
609 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700610 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossfabb6082018-02-20 17:22:23 -0800611
612 ctx.Build(pctx, android.BuildParams{
613 Rule: unzipAAR,
614 Input: aar,
Colin Cross10f7c4a2018-05-23 10:59:28 -0700615 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest},
Colin Crossfabb6082018-02-20 17:22:23 -0800616 Description: "unzip AAR",
617 Args: map[string]string{
Dan Willemsen304cfec2019-05-28 14:49:06 -0700618 "outDir": extractedAARDir.String(),
Colin Crossfabb6082018-02-20 17:22:23 -0800619 },
620 })
621
Colin Crossa0ba2f52019-06-22 12:59:27 -0700622 // Always set --pseudo-localize, it will be stripped out later for release
623 // builds that don't want it.
624 compileFlags := []string{"--pseudo-localize"}
Colin Crossfabb6082018-02-20 17:22:23 -0800625 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
Colin Crossfabb6082018-02-20 17:22:23 -0800626 flata := compiledResDir.Join(ctx, "gen_res.flata")
Colin Crossa0ba2f52019-06-22 12:59:27 -0700627 aapt2CompileZip(ctx, flata, aar, "res", compileFlags)
Colin Crossfabb6082018-02-20 17:22:23 -0800628
629 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900630 // the subdir "android" is required to be filtered by package names
631 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossfabb6082018-02-20 17:22:23 -0800632 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700633 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700634 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800635
636 var linkDeps android.Paths
637
638 linkFlags := []string{
639 "--static-lib",
640 "--no-static-lib-packages",
641 "--auto-add-overlay",
642 }
643
Colin Cross10f7c4a2018-05-23 10:59:28 -0700644 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
645 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800646
Colin Cross5446e882019-05-22 10:46:27 -0700647 transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags, sdkLibraries :=
648 aaptLibs(ctx, sdkContext(a))
Colin Cross31656952018-05-24 16:11:20 -0700649
650 _ = staticLibManifests
Colin Crossc1c37552019-01-31 11:42:41 -0800651 _ = staticRRODirs
Colin Cross5446e882019-05-22 10:46:27 -0700652 _ = sdkLibraries
Colin Crossfabb6082018-02-20 17:22:23 -0800653
Colin Crossa97c5d32018-03-28 14:58:31 -0700654 linkDeps = append(linkDeps, libDeps...)
655 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800656
Colin Cross66f78822018-05-02 12:58:28 -0700657 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800658
Colin Cross66f78822018-05-02 12:58:28 -0700659 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Colin Crosse560c4a2019-03-19 16:03:11 -0700660 linkFlags, linkDeps, nil, overlayRes, nil)
Colin Crossfabb6082018-02-20 17:22:23 -0800661}
662
663var _ Dependency = (*AARImport)(nil)
664
665func (a *AARImport) HeaderJars() android.Paths {
666 return android.Paths{a.classpathFile}
667}
668
669func (a *AARImport) ImplementationJars() android.Paths {
670 return android.Paths{a.classpathFile}
671}
672
Colin Cross331a1212018-08-15 20:40:52 -0700673func (a *AARImport) ResourceJars() android.Paths {
674 return nil
675}
676
677func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
678 return android.Paths{a.classpathFile}
679}
680
Colin Crossf24a22a2019-01-31 14:12:44 -0800681func (a *AARImport) DexJar() android.Path {
682 return nil
683}
684
Colin Crossfabb6082018-02-20 17:22:23 -0800685func (a *AARImport) AidlIncludeDirs() android.Paths {
686 return nil
687}
688
Jiyong Park1be96912018-05-28 18:02:19 +0900689func (a *AARImport) ExportedSdkLibs() []string {
690 return nil
691}
692
Colin Cross0c4ce212019-05-03 15:28:19 -0700693func (a *AARImport) SrcJarArgs() ([]string, android.Paths) {
694 return nil, nil
695}
696
Colin Crossfabb6082018-02-20 17:22:23 -0800697var _ android.PrebuiltInterface = (*Import)(nil)
698
Colin Cross1b16b0e2019-02-12 14:41:32 -0800699// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
700//
701// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
702// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -0800703func AARImportFactory() android.Module {
704 module := &AARImport{}
705
706 module.AddProperties(&module.properties)
707
708 android.InitPrebuiltModule(module, &module.properties.Aars)
Colin Cross48de9a42018-10-02 13:53:33 -0700709 InitJavaModule(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800710 return module
711}