blob: 1ba65dc7564d7c7a8534a172c57725fe1e73f48b [file] [log] [blame]
Colin Crossfabb6082018-02-20 17:22:23 -08001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Colin Crossa592e3e2019-02-19 16:59:53 -080018 "fmt"
Jaewoong Jung5b425e22019-06-17 17:40:56 -070019 "path/filepath"
Colin Crossa97c5d32018-03-28 14:58:31 -070020 "strings"
Colin Crossfabb6082018-02-20 17:22:23 -080021
Jaewoong Jung9befb0c2020-01-18 10:33:43 -080022 "android/soong/android"
23
Colin Crossfabb6082018-02-20 17:22:23 -080024 "github.com/google/blueprint"
Colin Crossa97c5d32018-03-28 14:58:31 -070025 "github.com/google/blueprint/proptools"
Colin Crossfabb6082018-02-20 17:22:23 -080026)
27
Colin Crossa97c5d32018-03-28 14:58:31 -070028type AndroidLibraryDependency interface {
29 Dependency
30 ExportPackage() android.Path
Colin Cross89c31582018-04-30 15:55:11 -070031 ExportedProguardFlagFiles() android.Paths
Anton Hansson53c88442019-03-18 15:53:16 +000032 ExportedRRODirs() []rroDir
Colin Cross66f78822018-05-02 12:58:28 -070033 ExportedStaticPackages() android.Paths
Colin Cross90c25c62019-04-19 16:22:57 -070034 ExportedManifests() android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -070035}
36
37func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000038 RegisterAARBuildComponents(android.InitRegistrationContext)
39}
40
41func RegisterAARBuildComponents(ctx android.RegistrationContext) {
42 ctx.RegisterModuleType("android_library_import", AARImportFactory)
43 ctx.RegisterModuleType("android_library", AndroidLibraryFactory)
Colin Crossa97c5d32018-03-28 14:58:31 -070044}
45
46//
47// AAR (android library)
48//
49
50type androidLibraryProperties struct {
51 BuildAAR bool `blueprint:"mutated"`
52}
53
54type aaptProperties struct {
55 // flags passed to aapt when creating the apk
56 Aaptflags []string
57
Dan Willemsen72be5902018-10-24 20:24:57 -070058 // include all resource configurations, not just the product-configured
59 // ones.
60 Aapt_include_all_resources *bool
61
Colin Crossa97c5d32018-03-28 14:58:31 -070062 // list of directories relative to the Blueprints file containing assets.
Colin Cross0ddae7f2019-02-07 15:30:01 -080063 // Defaults to ["assets"] if a directory called assets exists. Set to []
64 // to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070065 Asset_dirs []string
66
67 // list of directories relative to the Blueprints file containing
Colin Cross0ddae7f2019-02-07 15:30:01 -080068 // Android resources. Defaults to ["res"] if a directory called res exists.
69 // Set to [] to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070070 Resource_dirs []string
71
Colin Crossa592e3e2019-02-19 16:59:53 -080072 // list of zip files containing Android resources.
Colin Cross27b922f2019-03-04 22:35:41 -080073 Resource_zips []string `android:"path"`
Colin Crossa592e3e2019-02-19 16:59:53 -080074
Colin Crossa97c5d32018-03-28 14:58:31 -070075 // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml".
Colin Cross27b922f2019-03-04 22:35:41 -080076 Manifest *string `android:"path"`
changho.shinb5432b72019-08-08 18:37:17 +090077
78 // paths to additional manifest files to merge with main manifest.
79 Additional_manifests []string `android:"path"`
Sasha Smundak541056c2019-10-28 15:50:06 -070080
81 // do not include AndroidManifest from dependent libraries
82 Dont_merge_manifests *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070083}
84
85type aapt struct {
Colin Cross90c25c62019-04-19 16:22:57 -070086 aaptSrcJar android.Path
87 exportPackage android.Path
88 manifestPath android.Path
89 transitiveManifestPaths android.Paths
90 proguardOptionsFile android.Path
91 rroDirs []rroDir
92 rTxt android.Path
93 extraAaptPackagesFile android.Path
94 mergedManifestFile android.Path
Jaewoong Jung5b425e22019-06-17 17:40:56 -070095 noticeFile android.OptionalPath
Colin Cross90c25c62019-04-19 16:22:57 -070096 isLibrary bool
Sasha Smundak6ad77252019-05-01 13:16:22 -070097 useEmbeddedNativeLibs bool
Colin Cross90c25c62019-04-19 16:22:57 -070098 useEmbeddedDex bool
99 usesNonSdkApis bool
Colin Cross5446e882019-05-22 10:46:27 -0700100 sdkLibraries []string
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700101 hasNoCode bool
Colin Crossa97c5d32018-03-28 14:58:31 -0700102
Colin Crosse560c4a2019-03-19 16:03:11 -0700103 splitNames []string
104 splits []split
105
Colin Crossa97c5d32018-03-28 14:58:31 -0700106 aaptProperties aaptProperties
107}
108
Colin Crosse560c4a2019-03-19 16:03:11 -0700109type split struct {
110 name string
111 suffix string
112 path android.Path
113}
114
Colin Crossa97c5d32018-03-28 14:58:31 -0700115func (a *aapt) ExportPackage() android.Path {
116 return a.exportPackage
117}
118
Anton Hansson53c88442019-03-18 15:53:16 +0000119func (a *aapt) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800120 return a.rroDirs
121}
122
Colin Cross90c25c62019-04-19 16:22:57 -0700123func (a *aapt) ExportedManifests() android.Paths {
124 return a.transitiveManifestPaths
Colin Crossc1c37552019-01-31 11:42:41 -0800125}
126
Colin Crossa0ba2f52019-06-22 12:59:27 -0700127func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext sdkContext,
128 manifestPath android.Path) (compileFlags, linkFlags []string, linkDeps android.Paths,
129 resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700130
131 hasVersionCode := false
132 hasVersionName := false
Colin Crossa97c5d32018-03-28 14:58:31 -0700133 for _, f := range a.aaptProperties.Aaptflags {
134 if strings.HasPrefix(f, "--version-code") {
135 hasVersionCode = true
136 } else if strings.HasPrefix(f, "--version-name") {
137 hasVersionName = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700138 }
139 }
140
Colin Crossa97c5d32018-03-28 14:58:31 -0700141 // Flags specified in Android.bp
142 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
143
144 linkFlags = append(linkFlags, "--no-static-lib-packages")
145
146 // Find implicit or explicit asset and resource dirs
147 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
148 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
Colin Cross8a497952019-03-05 22:25:09 -0800149 resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips)
Colin Crossa97c5d32018-03-28 14:58:31 -0700150
Colin Crossa97c5d32018-03-28 14:58:31 -0700151 // Glob directories into lists of paths
152 for _, dir := range resourceDirs {
153 resDirs = append(resDirs, globbedResourceDir{
154 dir: dir,
155 files: androidResourceGlob(ctx, dir),
156 })
157 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, dir)
158 overlayDirs = append(overlayDirs, resOverlayDirs...)
159 rroDirs = append(rroDirs, resRRODirs...)
160 }
161
162 var assetFiles android.Paths
163 for _, dir := range assetDirs {
164 assetFiles = append(assetFiles, androidResourceGlob(ctx, dir)...)
165 }
166
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700167 assetDirStrings := assetDirs.Strings()
168 if a.noticeFile.Valid() {
169 assetDirStrings = append(assetDirStrings, filepath.Dir(a.noticeFile.Path().String()))
170 assetFiles = append(assetFiles, a.noticeFile.Path())
171 }
172
Colin Crossa97c5d32018-03-28 14:58:31 -0700173 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
174 linkDeps = append(linkDeps, manifestPath)
175
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700176 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirStrings, "-A "))
Colin Crossa97c5d32018-03-28 14:58:31 -0700177 linkDeps = append(linkDeps, assetFiles...)
178
Colin Crossa97c5d32018-03-28 14:58:31 -0700179 // SDK version flags
Colin Cross83bb3162018-06-25 15:48:06 -0700180 minSdkVersion := sdkVersionOrDefault(ctx, sdkContext.minSdkVersion())
Colin Crossa97c5d32018-03-28 14:58:31 -0700181
Colin Cross83bb3162018-06-25 15:48:06 -0700182 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
183 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700184
Colin Crossa97c5d32018-03-28 14:58:31 -0700185 // Version code
186 if !hasVersionCode {
187 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion())
188 }
189
190 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700191 var versionName string
192 if ctx.ModuleName() == "framework-res" {
193 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
194 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700195 // if it contains the build number. Use the PlatformVersionName instead.
196 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700197 } else {
198 versionName = ctx.Config().AppsDefaultVersionName()
199 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800200 versionName = proptools.NinjaEscape(versionName)
Colin Crossa97c5d32018-03-28 14:58:31 -0700201 linkFlags = append(linkFlags, "--version-name ", versionName)
202 }
203
Colin Crossa0ba2f52019-06-22 12:59:27 -0700204 linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"})
205
206 // Always set --pseudo-localize, it will be stripped out later for release
207 // builds that don't want it.
208 compileFlags = append(compileFlags, "--pseudo-localize")
209
210 return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
Colin Crossa97c5d32018-03-28 14:58:31 -0700211}
212
Paul Duffin250e6192019-06-07 10:44:37 +0100213func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) {
Colin Cross42308aa2018-11-14 21:44:17 -0800214 if sdkDep.frameworkResModule != "" {
215 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700216 }
217}
218
Colin Cross83bb3162018-06-25 15:48:06 -0700219func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, extraLinkFlags ...string) {
Colin Cross5446e882019-05-22 10:46:27 -0700220
221 transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, libDeps, libFlags, sdkLibraries :=
222 aaptLibs(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700223
Colin Cross31656952018-05-24 16:11:20 -0700224 // App manifest file
225 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
226 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
227
Colin Cross5446e882019-05-22 10:46:27 -0700228 manifestPath := manifestFixer(ctx, manifestSrcPath, sdkContext, sdkLibraries,
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700229 a.isLibrary, a.useEmbeddedNativeLibs, a.usesNonSdkApis, a.useEmbeddedDex, a.hasNoCode)
Colin Cross90c25c62019-04-19 16:22:57 -0700230
Luca Stefanifd898822019-09-10 22:13:31 +0200231 // Add additional manifest files to transitive manifests.
232 additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)
233 a.transitiveManifestPaths = append(android.Paths{manifestPath}, additionalManifests...)
234 a.transitiveManifestPaths = append(a.transitiveManifestPaths, transitiveStaticLibManifests...)
Colin Cross90c25c62019-04-19 16:22:57 -0700235
Sasha Smundak541056c2019-10-28 15:50:06 -0700236 if len(a.transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) {
Luca Stefanifd898822019-09-10 22:13:31 +0200237 a.mergedManifestFile = manifestMerger(ctx, a.transitiveManifestPaths[0], a.transitiveManifestPaths[1:], a.isLibrary)
Colin Cross90c25c62019-04-19 16:22:57 -0700238 if !a.isLibrary {
239 // Only use the merged manifest for applications. For libraries, the transitive closure of manifests
240 // will be propagated to the final application and merged there. The merged manifest for libraries is
241 // only passed to Make, which can't handle transitive dependencies.
242 manifestPath = a.mergedManifestFile
243 }
244 } else {
245 a.mergedManifestFile = manifestPath
246 }
Colin Cross31656952018-05-24 16:11:20 -0700247
Colin Crossa0ba2f52019-06-22 12:59:27 -0700248 compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700249
Colin Crossc1c37552019-01-31 11:42:41 -0800250 rroDirs = append(rroDirs, staticRRODirs...)
Colin Cross31656952018-05-24 16:11:20 -0700251 linkFlags = append(linkFlags, libFlags...)
252 linkDeps = append(linkDeps, libDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700253 linkFlags = append(linkFlags, extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700254 if a.isLibrary {
255 linkFlags = append(linkFlags, "--static-lib")
256 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700257
258 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900259 // the subdir "android" is required to be filtered by package names
260 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700261 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
262 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700263 // This file isn't used by Soong, but is generated for exporting
264 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Crossa97c5d32018-03-28 14:58:31 -0700265
Colin Cross4aaa84a2018-08-21 15:14:37 -0700266 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700267 for _, dir := range resDirs {
Colin Crossa0ba2f52019-06-22 12:59:27 -0700268 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700269 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700270
Colin Crossa592e3e2019-02-19 16:59:53 -0800271 for i, zip := range resZips {
272 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
Colin Crossa0ba2f52019-06-22 12:59:27 -0700273 aapt2CompileZip(ctx, flata, zip, "", compileFlags)
Colin Crossa592e3e2019-02-19 16:59:53 -0800274 compiledResDirs = append(compiledResDirs, android.Paths{flata})
275 }
276
Colin Cross4aaa84a2018-08-21 15:14:37 -0700277 var compiledRes, compiledOverlay android.Paths
278
279 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
280
Colin Crossbec85302019-02-13 13:15:46 -0800281 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700282 // If we are using static android libraries, every source file becomes an overlay.
283 // This is to emulate old AAPT behavior which simulated library support.
284 for _, compiledResDir := range compiledResDirs {
285 compiledOverlay = append(compiledOverlay, compiledResDir...)
286 }
Colin Crossbec85302019-02-13 13:15:46 -0800287 } else if a.isLibrary {
288 // Otherwise, for a static library we treat all the resources equally with no overlay.
289 for _, compiledResDir := range compiledResDirs {
290 compiledRes = append(compiledRes, compiledResDir...)
291 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700292 } else if len(compiledResDirs) > 0 {
293 // Without static libraries, the first directory is our directory, which can then be
294 // overlaid by the rest.
295 compiledRes = append(compiledRes, compiledResDirs[0]...)
296 for _, compiledResDir := range compiledResDirs[1:] {
297 compiledOverlay = append(compiledOverlay, compiledResDir...)
298 }
299 }
300
Colin Crossa97c5d32018-03-28 14:58:31 -0700301 for _, dir := range overlayDirs {
Colin Crossa0ba2f52019-06-22 12:59:27 -0700302 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700303 }
304
Colin Crosse560c4a2019-03-19 16:03:11 -0700305 var splitPackages android.WritablePaths
306 var splits []split
307
308 for _, s := range a.splitNames {
309 suffix := strings.Replace(s, ",", "_", -1)
310 path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
311 linkFlags = append(linkFlags, "--split", path.String()+":"+s)
312 splitPackages = append(splitPackages, path)
313 splits = append(splits, split{
314 name: s,
315 suffix: suffix,
316 path: path,
317 })
318 }
319
Colin Cross66f78822018-05-02 12:58:28 -0700320 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
Colin Crosse560c4a2019-03-19 16:03:11 -0700321 linkFlags, linkDeps, compiledRes, compiledOverlay, splitPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700322
323 a.aaptSrcJar = srcJar
324 a.exportPackage = packageRes
325 a.manifestPath = manifestPath
326 a.proguardOptionsFile = proguardOptionsFile
327 a.rroDirs = rroDirs
Colin Cross66f78822018-05-02 12:58:28 -0700328 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700329 a.rTxt = rTxt
Colin Crosse560c4a2019-03-19 16:03:11 -0700330 a.splits = splits
Colin Crossa97c5d32018-03-28 14:58:31 -0700331}
332
333// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Colin Cross90c25c62019-04-19 16:22:57 -0700334func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, transitiveStaticLibManifests android.Paths,
Colin Cross5446e882019-05-22 10:46:27 -0700335 staticRRODirs []rroDir, deps android.Paths, flags []string, sdkLibraries []string) {
Colin Cross66f78822018-05-02 12:58:28 -0700336
Colin Crossa97c5d32018-03-28 14:58:31 -0700337 var sharedLibs android.Paths
338
Colin Cross83bb3162018-06-25 15:48:06 -0700339 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700340 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700341 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700342 }
343
344 ctx.VisitDirectDeps(func(module android.Module) {
345 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700346 aarDep, _ := module.(AndroidLibraryDependency)
347 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700348 exportPackage = aarDep.ExportPackage()
349 }
350
351 switch ctx.OtherModuleDependencyTag(module) {
Colin Cross4b964c02018-10-15 16:18:06 -0700352 case instrumentationForTag:
353 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Colin Cross5446e882019-05-22 10:46:27 -0700354 case libTag:
355 if exportPackage != nil {
356 sharedLibs = append(sharedLibs, exportPackage)
357 }
358
359 if _, ok := module.(SdkLibraryDependency); ok {
360 sdkLibraries = append(sdkLibraries, ctx.OtherModuleName(module))
361 }
362
363 case frameworkResTag:
Colin Crossa97c5d32018-03-28 14:58:31 -0700364 if exportPackage != nil {
365 sharedLibs = append(sharedLibs, exportPackage)
366 }
367 case staticLibTag:
368 if exportPackage != nil {
Colin Cross66f78822018-05-02 12:58:28 -0700369 transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
Colin Crossbec85302019-02-13 13:15:46 -0800370 transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
Colin Cross90c25c62019-04-19 16:22:57 -0700371 transitiveStaticLibManifests = append(transitiveStaticLibManifests, aarDep.ExportedManifests()...)
Colin Cross5446e882019-05-22 10:46:27 -0700372 sdkLibraries = append(sdkLibraries, aarDep.ExportedSdkLibs()...)
Anton Hansson53c88442019-03-18 15:53:16 +0000373
374 outer:
375 for _, d := range aarDep.ExportedRRODirs() {
376 for _, e := range staticRRODirs {
377 if d.path == e.path {
378 continue outer
379 }
380 }
381 staticRRODirs = append(staticRRODirs, d)
382 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700383 }
384 }
385 })
386
387 deps = append(deps, sharedLibs...)
Colin Cross66f78822018-05-02 12:58:28 -0700388 deps = append(deps, transitiveStaticLibs...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700389
Colin Cross66f78822018-05-02 12:58:28 -0700390 if len(transitiveStaticLibs) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700391 flags = append(flags, "--auto-add-overlay")
392 }
393
394 for _, sharedLib := range sharedLibs {
395 flags = append(flags, "-I "+sharedLib.String())
396 }
397
Colin Cross66f78822018-05-02 12:58:28 -0700398 transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
Colin Cross90c25c62019-04-19 16:22:57 -0700399 transitiveStaticLibManifests = android.FirstUniquePaths(transitiveStaticLibManifests)
Colin Cross5446e882019-05-22 10:46:27 -0700400 sdkLibraries = android.FirstUniqueStrings(sdkLibraries)
Colin Cross66f78822018-05-02 12:58:28 -0700401
Colin Cross5446e882019-05-22 10:46:27 -0700402 return transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, deps, flags, sdkLibraries
Colin Crossa97c5d32018-03-28 14:58:31 -0700403}
404
405type AndroidLibrary struct {
406 Library
407 aapt
408
409 androidLibraryProperties androidLibraryProperties
410
411 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700412
413 exportedProguardFlagFiles android.Paths
Colin Cross66f78822018-05-02 12:58:28 -0700414 exportedStaticPackages android.Paths
Colin Cross89c31582018-04-30 15:55:11 -0700415}
416
417func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
418 return a.exportedProguardFlagFiles
Colin Crossa97c5d32018-03-28 14:58:31 -0700419}
420
Colin Cross66f78822018-05-02 12:58:28 -0700421func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
422 return a.exportedStaticPackages
423}
424
Colin Crossa97c5d32018-03-28 14:58:31 -0700425var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
426
427func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
428 a.Module.deps(ctx)
Paul Duffin250e6192019-06-07 10:44:37 +0100429 sdkDep := decodeSdkDep(ctx, sdkContext(a))
430 if sdkDep.hasFrameworkLibs() {
431 a.aapt.deps(ctx, sdkDep)
Colin Crossa97c5d32018-03-28 14:58:31 -0700432 }
433}
434
435func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800436 a.aapt.isLibrary = true
Colin Cross5446e882019-05-22 10:46:27 -0700437 a.aapt.sdkLibraries = a.exportedSdkLibs
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700438 a.aapt.buildActions(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700439
440 ctx.CheckbuildFile(a.proguardOptionsFile)
441 ctx.CheckbuildFile(a.exportPackage)
442 ctx.CheckbuildFile(a.aaptSrcJar)
443
444 // apps manifests are handled by aapt, don't let Module see them
445 a.properties.Manifest = nil
446
447 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
448 a.proguardOptionsFile)
449
450 a.Module.compile(ctx, a.aaptSrcJar)
451
Colin Crossf57c5782019-01-25 13:20:38 -0800452 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700453 var res android.Paths
454 if a.androidLibraryProperties.BuildAAR {
455 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
456 ctx.CheckbuildFile(a.aarFile)
457 }
Colin Cross89c31582018-04-30 15:55:11 -0700458
459 ctx.VisitDirectDeps(func(m android.Module) {
460 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
461 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
Colin Cross66f78822018-05-02 12:58:28 -0700462 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
463 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
Colin Cross89c31582018-04-30 15:55:11 -0700464 }
465 })
466
467 a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
Colin Cross66f78822018-05-02 12:58:28 -0700468 a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700469}
470
Colin Cross1b16b0e2019-02-12 14:41:32 -0800471// android_library builds and links sources into a `.jar` file for the device along with Android resources.
472//
473// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
474// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
475// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
476// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700477func AndroidLibraryFactory() android.Module {
478 module := &AndroidLibrary{}
479
480 module.AddProperties(
481 &module.Module.properties,
482 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800483 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700484 &module.Module.protoProperties,
485 &module.aaptProperties,
486 &module.androidLibraryProperties)
487
488 module.androidLibraryProperties.BuildAAR = true
489
Colin Cross48de9a42018-10-02 13:53:33 -0700490 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700491 return module
492}
493
Colin Crossfabb6082018-02-20 17:22:23 -0800494//
495// AAR (android library) prebuilts
496//
Colin Crossfabb6082018-02-20 17:22:23 -0800497
498type AARImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -0800499 Aars []string `android:"path"`
Colin Crossfabb6082018-02-20 17:22:23 -0800500
Colin Cross479884c2018-07-10 13:39:30 -0700501 Sdk_version *string
502 Min_sdk_version *string
Colin Crossa97c5d32018-03-28 14:58:31 -0700503
504 Static_libs []string
505 Libs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -0700506
507 // if set to true, run Jetifier against .aar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -0700508 Jetifier *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800509}
510
511type AARImport struct {
512 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700513 android.DefaultableModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800514 prebuilt android.Prebuilt
515
516 properties AARImportProperties
517
Colin Cross66f78822018-05-02 12:58:28 -0700518 classpathFile android.WritablePath
519 proguardFlags android.WritablePath
520 exportPackage android.WritablePath
521 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700522 manifest android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700523
524 exportedStaticPackages android.Paths
Colin Crossfabb6082018-02-20 17:22:23 -0800525}
526
Colin Cross83bb3162018-06-25 15:48:06 -0700527func (a *AARImport) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900528 return String(a.properties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700529}
530
Paul Duffine25c6442019-10-11 13:50:28 +0100531func (a *AARImport) systemModules() string {
532 return ""
533}
534
Colin Cross83bb3162018-06-25 15:48:06 -0700535func (a *AARImport) minSdkVersion() string {
Colin Cross479884c2018-07-10 13:39:30 -0700536 if a.properties.Min_sdk_version != nil {
537 return *a.properties.Min_sdk_version
538 }
Colin Cross83bb3162018-06-25 15:48:06 -0700539 return a.sdkVersion()
540}
541
Dan Willemsen419290a2018-10-31 15:28:47 -0700542func (a *AARImport) targetSdkVersion() string {
543 return a.sdkVersion()
544}
545
Colin Cross1e743852019-10-28 11:37:20 -0700546func (a *AARImport) javaVersion() string {
547 return ""
548}
549
Colin Crossa97c5d32018-03-28 14:58:31 -0700550var _ AndroidLibraryDependency = (*AARImport)(nil)
551
552func (a *AARImport) ExportPackage() android.Path {
553 return a.exportPackage
554}
555
Colin Cross89c31582018-04-30 15:55:11 -0700556func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
557 return android.Paths{a.proguardFlags}
558}
559
Anton Hansson53c88442019-03-18 15:53:16 +0000560func (a *AARImport) ExportedRRODirs() []rroDir {
Colin Crossc1c37552019-01-31 11:42:41 -0800561 return nil
562}
563
Colin Cross66f78822018-05-02 12:58:28 -0700564func (a *AARImport) ExportedStaticPackages() android.Paths {
565 return a.exportedStaticPackages
566}
567
Colin Cross90c25c62019-04-19 16:22:57 -0700568func (a *AARImport) ExportedManifests() android.Paths {
569 return android.Paths{a.manifest}
Colin Cross31656952018-05-24 16:11:20 -0700570}
571
Colin Crossfabb6082018-02-20 17:22:23 -0800572func (a *AARImport) Prebuilt() *android.Prebuilt {
573 return &a.prebuilt
574}
575
576func (a *AARImport) Name() string {
577 return a.prebuilt.Name(a.ModuleBase.Name())
578}
579
Jiyong Park618922e2020-01-08 13:35:43 +0900580func (a *AARImport) JacocoReportClassesFile() android.Path {
581 return nil
582}
583
Colin Crossfabb6082018-02-20 17:22:23 -0800584func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross10932872019-04-18 14:27:12 -0700585 if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
Colin Cross83bb3162018-06-25 15:48:06 -0700586 sdkDep := decodeSdkDep(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700587 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700588 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800589 }
590 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700591
Colin Cross42d48b72018-08-29 14:10:52 -0700592 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
593 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800594}
595
596// 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 -0700597// 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 -0800598var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
599 blueprint.RuleParams{
Dan Willemsen304cfec2019-05-28 14:49:06 -0700600 Command: `rm -rf $outDir && mkdir -p $outDir && ` +
601 `unzip -qo -d $outDir $in && rm -rf $outDir/res && touch $out`,
Colin Crossfabb6082018-02-20 17:22:23 -0800602 },
Dan Willemsen304cfec2019-05-28 14:49:06 -0700603 "outDir")
Colin Crossfabb6082018-02-20 17:22:23 -0800604
605func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
606 if len(a.properties.Aars) != 1 {
607 ctx.PropertyErrorf("aars", "exactly one aar is required")
608 return
609 }
610
Nan Zhang4c819fb2018-08-27 18:31:46 -0700611 aarName := ctx.ModuleName() + ".aar"
612 var aar android.Path
613 aar = android.PathForModuleSrc(ctx, a.properties.Aars[0])
Colin Cross1001a792019-03-21 22:21:39 -0700614 if Bool(a.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -0700615 inputFile := aar
616 aar = android.PathForModuleOut(ctx, "jetifier", aarName)
617 TransformJetifier(ctx, aar.(android.WritablePath), inputFile)
618 }
Colin Crossfabb6082018-02-20 17:22:23 -0800619
620 extractedAARDir := android.PathForModuleOut(ctx, "aar")
Colin Crossfabb6082018-02-20 17:22:23 -0800621 a.classpathFile = extractedAARDir.Join(ctx, "classes.jar")
622 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700623 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossfabb6082018-02-20 17:22:23 -0800624
625 ctx.Build(pctx, android.BuildParams{
626 Rule: unzipAAR,
627 Input: aar,
Colin Cross10f7c4a2018-05-23 10:59:28 -0700628 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest},
Colin Crossfabb6082018-02-20 17:22:23 -0800629 Description: "unzip AAR",
630 Args: map[string]string{
Dan Willemsen304cfec2019-05-28 14:49:06 -0700631 "outDir": extractedAARDir.String(),
Colin Crossfabb6082018-02-20 17:22:23 -0800632 },
633 })
634
Colin Crossa0ba2f52019-06-22 12:59:27 -0700635 // Always set --pseudo-localize, it will be stripped out later for release
636 // builds that don't want it.
637 compileFlags := []string{"--pseudo-localize"}
Colin Crossfabb6082018-02-20 17:22:23 -0800638 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
Colin Crossfabb6082018-02-20 17:22:23 -0800639 flata := compiledResDir.Join(ctx, "gen_res.flata")
Colin Crossa0ba2f52019-06-22 12:59:27 -0700640 aapt2CompileZip(ctx, flata, aar, "res", compileFlags)
Colin Crossfabb6082018-02-20 17:22:23 -0800641
642 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900643 // the subdir "android" is required to be filtered by package names
644 srcJar := android.PathForModuleGen(ctx, "android", "R.srcjar")
Colin Crossfabb6082018-02-20 17:22:23 -0800645 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700646 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700647 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800648
649 var linkDeps android.Paths
650
651 linkFlags := []string{
652 "--static-lib",
653 "--no-static-lib-packages",
654 "--auto-add-overlay",
655 }
656
Colin Cross10f7c4a2018-05-23 10:59:28 -0700657 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
658 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800659
Colin Cross5446e882019-05-22 10:46:27 -0700660 transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags, sdkLibraries :=
661 aaptLibs(ctx, sdkContext(a))
Colin Cross31656952018-05-24 16:11:20 -0700662
663 _ = staticLibManifests
Colin Crossc1c37552019-01-31 11:42:41 -0800664 _ = staticRRODirs
Colin Cross5446e882019-05-22 10:46:27 -0700665 _ = sdkLibraries
Colin Crossfabb6082018-02-20 17:22:23 -0800666
Colin Crossa97c5d32018-03-28 14:58:31 -0700667 linkDeps = append(linkDeps, libDeps...)
668 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800669
Colin Cross66f78822018-05-02 12:58:28 -0700670 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800671
Colin Cross66f78822018-05-02 12:58:28 -0700672 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Colin Crosse560c4a2019-03-19 16:03:11 -0700673 linkFlags, linkDeps, nil, overlayRes, nil)
Colin Crossfabb6082018-02-20 17:22:23 -0800674}
675
676var _ Dependency = (*AARImport)(nil)
677
678func (a *AARImport) HeaderJars() android.Paths {
679 return android.Paths{a.classpathFile}
680}
681
682func (a *AARImport) ImplementationJars() android.Paths {
683 return android.Paths{a.classpathFile}
684}
685
Colin Cross331a1212018-08-15 20:40:52 -0700686func (a *AARImport) ResourceJars() android.Paths {
687 return nil
688}
689
690func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
691 return android.Paths{a.classpathFile}
692}
693
Colin Crossf24a22a2019-01-31 14:12:44 -0800694func (a *AARImport) DexJar() android.Path {
695 return nil
696}
697
Colin Crossfabb6082018-02-20 17:22:23 -0800698func (a *AARImport) AidlIncludeDirs() android.Paths {
699 return nil
700}
701
Jiyong Park1be96912018-05-28 18:02:19 +0900702func (a *AARImport) ExportedSdkLibs() []string {
703 return nil
704}
705
Artur Satayev9cf46692019-11-26 18:08:34 +0000706func (d *AARImport) ExportedPlugins() (android.Paths, []string) {
707 return nil, nil
708}
709
Colin Cross0c4ce212019-05-03 15:28:19 -0700710func (a *AARImport) SrcJarArgs() ([]string, android.Paths) {
711 return nil, nil
712}
713
Colin Crossfabb6082018-02-20 17:22:23 -0800714var _ android.PrebuiltInterface = (*Import)(nil)
715
Colin Cross1b16b0e2019-02-12 14:41:32 -0800716// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
717//
718// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
719// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -0800720func AARImportFactory() android.Module {
721 module := &AARImport{}
722
723 module.AddProperties(&module.properties)
724
725 android.InitPrebuiltModule(module, &module.properties.Aars)
Colin Cross48de9a42018-10-02 13:53:33 -0700726 InitJavaModule(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800727 return module
728}