blob: c30632ea9435ae6ca58297b7858733708dc29883 [file] [log] [blame]
Colin Crossfabb6082018-02-20 17:22:23 -08001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
18 "android/soong/android"
Colin Crossa592e3e2019-02-19 16:59:53 -080019 "fmt"
Colin Crossa97c5d32018-03-28 14:58:31 -070020 "strings"
Colin Crossfabb6082018-02-20 17:22:23 -080021
22 "github.com/google/blueprint"
Colin Crossa97c5d32018-03-28 14:58:31 -070023 "github.com/google/blueprint/proptools"
Colin Crossfabb6082018-02-20 17:22:23 -080024)
25
Colin Crossa97c5d32018-03-28 14:58:31 -070026type AndroidLibraryDependency interface {
27 Dependency
28 ExportPackage() android.Path
Colin Cross89c31582018-04-30 15:55:11 -070029 ExportedProguardFlagFiles() android.Paths
Colin Crossc1c37552019-01-31 11:42:41 -080030 ExportedRRODirs() android.Paths
Colin Cross66f78822018-05-02 12:58:28 -070031 ExportedStaticPackages() android.Paths
Colin Cross31656952018-05-24 16:11:20 -070032 ExportedManifest() android.Path
Colin Crossa97c5d32018-03-28 14:58:31 -070033}
34
35func init() {
36 android.RegisterModuleType("android_library_import", AARImportFactory)
37 android.RegisterModuleType("android_library", AndroidLibraryFactory)
38}
39
40//
41// AAR (android library)
42//
43
44type androidLibraryProperties struct {
45 BuildAAR bool `blueprint:"mutated"`
46}
47
48type aaptProperties struct {
49 // flags passed to aapt when creating the apk
50 Aaptflags []string
51
Dan Willemsen72be5902018-10-24 20:24:57 -070052 // include all resource configurations, not just the product-configured
53 // ones.
54 Aapt_include_all_resources *bool
55
Colin Crossa97c5d32018-03-28 14:58:31 -070056 // list of directories relative to the Blueprints file containing assets.
Colin Cross0ddae7f2019-02-07 15:30:01 -080057 // Defaults to ["assets"] if a directory called assets exists. Set to []
58 // to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070059 Asset_dirs []string
60
61 // list of directories relative to the Blueprints file containing
Colin Cross0ddae7f2019-02-07 15:30:01 -080062 // Android resources. Defaults to ["res"] if a directory called res exists.
63 // Set to [] to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070064 Resource_dirs []string
65
Colin Crossa592e3e2019-02-19 16:59:53 -080066 // list of zip files containing Android resources.
67 Resource_zips []string
68
Colin Crossa97c5d32018-03-28 14:58:31 -070069 // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml".
70 Manifest *string
71}
72
73type aapt struct {
Colin Cross66f78822018-05-02 12:58:28 -070074 aaptSrcJar android.Path
75 exportPackage android.Path
76 manifestPath android.Path
77 proguardOptionsFile android.Path
78 rroDirs android.Paths
79 rTxt android.Path
80 extraAaptPackagesFile android.Path
Colin Cross1b6a3cf2018-07-24 14:51:30 -070081 isLibrary bool
Colin Crosse4246ab2019-02-05 21:55:21 -080082 uncompressedJNI bool
Colin Cross46abdad2019-02-07 13:07:08 -080083 useEmbeddedDex bool
David Brazdild25060a2019-02-18 18:24:16 +000084 usesNonSdkApis bool
Colin Crossa97c5d32018-03-28 14:58:31 -070085
86 aaptProperties aaptProperties
87}
88
89func (a *aapt) ExportPackage() android.Path {
90 return a.exportPackage
91}
92
Colin Crossc1c37552019-01-31 11:42:41 -080093func (a *aapt) ExportedRRODirs() android.Paths {
94 return a.rroDirs
95}
96
97func (a *aapt) ExportedManifest() android.Path {
98 return a.manifestPath
99}
100
Colin Cross31656952018-05-24 16:11:20 -0700101func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext sdkContext, manifestPath android.Path) (flags []string,
Colin Crossa592e3e2019-02-19 16:59:53 -0800102 deps android.Paths, resDirs, overlayDirs []globbedResourceDir, rroDirs, resZips android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700103
104 hasVersionCode := false
105 hasVersionName := false
Colin Crossa97c5d32018-03-28 14:58:31 -0700106 for _, f := range a.aaptProperties.Aaptflags {
107 if strings.HasPrefix(f, "--version-code") {
108 hasVersionCode = true
109 } else if strings.HasPrefix(f, "--version-name") {
110 hasVersionName = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700111 }
112 }
113
114 var linkFlags []string
115
116 // Flags specified in Android.bp
117 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
118
119 linkFlags = append(linkFlags, "--no-static-lib-packages")
120
121 // Find implicit or explicit asset and resource dirs
122 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
123 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
Colin Crossa592e3e2019-02-19 16:59:53 -0800124 resourceZips := ctx.ExpandSources(a.aaptProperties.Resource_zips, nil)
Colin Crossa97c5d32018-03-28 14:58:31 -0700125
126 var linkDeps android.Paths
127
128 // Glob directories into lists of paths
129 for _, dir := range resourceDirs {
130 resDirs = append(resDirs, globbedResourceDir{
131 dir: dir,
132 files: androidResourceGlob(ctx, dir),
133 })
134 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, dir)
135 overlayDirs = append(overlayDirs, resOverlayDirs...)
136 rroDirs = append(rroDirs, resRRODirs...)
137 }
138
139 var assetFiles android.Paths
140 for _, dir := range assetDirs {
141 assetFiles = append(assetFiles, androidResourceGlob(ctx, dir)...)
142 }
143
Colin Crossa97c5d32018-03-28 14:58:31 -0700144 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
145 linkDeps = append(linkDeps, manifestPath)
146
147 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirs.Strings(), "-A "))
148 linkDeps = append(linkDeps, assetFiles...)
149
Colin Crossa97c5d32018-03-28 14:58:31 -0700150 // SDK version flags
Colin Cross83bb3162018-06-25 15:48:06 -0700151 minSdkVersion := sdkVersionOrDefault(ctx, sdkContext.minSdkVersion())
Colin Crossa97c5d32018-03-28 14:58:31 -0700152
Colin Cross83bb3162018-06-25 15:48:06 -0700153 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
154 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700155
Colin Crossa97c5d32018-03-28 14:58:31 -0700156 // Version code
157 if !hasVersionCode {
158 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion())
159 }
160
161 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700162 var versionName string
163 if ctx.ModuleName() == "framework-res" {
164 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
165 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700166 // if it contains the build number. Use the PlatformVersionName instead.
167 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700168 } else {
169 versionName = ctx.Config().AppsDefaultVersionName()
170 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800171 versionName = proptools.NinjaEscape(versionName)
Colin Crossa97c5d32018-03-28 14:58:31 -0700172 linkFlags = append(linkFlags, "--version-name ", versionName)
173 }
174
Colin Crossa592e3e2019-02-19 16:59:53 -0800175 return linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
Colin Crossa97c5d32018-03-28 14:58:31 -0700176}
177
Colin Cross83bb3162018-06-25 15:48:06 -0700178func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkContext sdkContext) {
Colin Cross42308aa2018-11-14 21:44:17 -0800179 sdkDep := decodeSdkDep(ctx, sdkContext)
180 if sdkDep.frameworkResModule != "" {
181 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700182 }
Colin Crossa592e3e2019-02-19 16:59:53 -0800183
184 android.ExtractSourcesDeps(ctx, a.aaptProperties.Resource_zips)
Colin Crossa97c5d32018-03-28 14:58:31 -0700185}
186
Colin Cross83bb3162018-06-25 15:48:06 -0700187func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, extraLinkFlags ...string) {
Colin Crossc1c37552019-01-31 11:42:41 -0800188 transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags := aaptLibs(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700189
Colin Cross31656952018-05-24 16:11:20 -0700190 // App manifest file
191 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
192 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
193
Colin Cross46abdad2019-02-07 13:07:08 -0800194 manifestPath := manifestMerger(ctx, manifestSrcPath, sdkContext, staticLibManifests, a.isLibrary,
David Brazdild25060a2019-02-18 18:24:16 +0000195 a.uncompressedJNI, a.useEmbeddedDex, a.usesNonSdkApis)
Colin Cross31656952018-05-24 16:11:20 -0700196
Colin Crossa592e3e2019-02-19 16:59:53 -0800197 linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, sdkContext, manifestPath)
Colin Cross31656952018-05-24 16:11:20 -0700198
Colin Crossc1c37552019-01-31 11:42:41 -0800199 rroDirs = append(rroDirs, staticRRODirs...)
Colin Cross31656952018-05-24 16:11:20 -0700200 linkFlags = append(linkFlags, libFlags...)
201 linkDeps = append(linkDeps, libDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700202 linkFlags = append(linkFlags, extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700203 if a.isLibrary {
204 linkFlags = append(linkFlags, "--static-lib")
205 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700206
207 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
208 srcJar := android.PathForModuleGen(ctx, "R.jar")
209 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
210 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700211 // This file isn't used by Soong, but is generated for exporting
212 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Crossa97c5d32018-03-28 14:58:31 -0700213
Colin Cross4aaa84a2018-08-21 15:14:37 -0700214 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700215 for _, dir := range resDirs {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700216 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700217 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700218
Colin Crossa592e3e2019-02-19 16:59:53 -0800219 for i, zip := range resZips {
220 flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
221 aapt2CompileZip(ctx, flata, zip)
222 compiledResDirs = append(compiledResDirs, android.Paths{flata})
223 }
224
Colin Cross4aaa84a2018-08-21 15:14:37 -0700225 var compiledRes, compiledOverlay android.Paths
226
227 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
228
Colin Crossbec85302019-02-13 13:15:46 -0800229 if len(transitiveStaticLibs) > 0 {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700230 // If we are using static android libraries, every source file becomes an overlay.
231 // This is to emulate old AAPT behavior which simulated library support.
232 for _, compiledResDir := range compiledResDirs {
233 compiledOverlay = append(compiledOverlay, compiledResDir...)
234 }
Colin Crossbec85302019-02-13 13:15:46 -0800235 } else if a.isLibrary {
236 // Otherwise, for a static library we treat all the resources equally with no overlay.
237 for _, compiledResDir := range compiledResDirs {
238 compiledRes = append(compiledRes, compiledResDir...)
239 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700240 } else if len(compiledResDirs) > 0 {
241 // Without static libraries, the first directory is our directory, which can then be
242 // overlaid by the rest.
243 compiledRes = append(compiledRes, compiledResDirs[0]...)
244 for _, compiledResDir := range compiledResDirs[1:] {
245 compiledOverlay = append(compiledOverlay, compiledResDir...)
246 }
247 }
248
Colin Crossa97c5d32018-03-28 14:58:31 -0700249 for _, dir := range overlayDirs {
250 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files).Paths()...)
251 }
252
Colin Cross66f78822018-05-02 12:58:28 -0700253 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
Colin Crossa97c5d32018-03-28 14:58:31 -0700254 linkFlags, linkDeps, compiledRes, compiledOverlay)
255
256 a.aaptSrcJar = srcJar
257 a.exportPackage = packageRes
258 a.manifestPath = manifestPath
259 a.proguardOptionsFile = proguardOptionsFile
260 a.rroDirs = rroDirs
Colin Cross66f78822018-05-02 12:58:28 -0700261 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700262 a.rTxt = rTxt
263}
264
265// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Colin Cross31656952018-05-24 16:11:20 -0700266func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, staticLibManifests,
Colin Crossc1c37552019-01-31 11:42:41 -0800267 staticRRODirs, deps android.Paths, flags []string) {
Colin Cross66f78822018-05-02 12:58:28 -0700268
Colin Crossa97c5d32018-03-28 14:58:31 -0700269 var sharedLibs android.Paths
270
Colin Cross83bb3162018-06-25 15:48:06 -0700271 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700272 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700273 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700274 }
275
276 ctx.VisitDirectDeps(func(module android.Module) {
277 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700278 aarDep, _ := module.(AndroidLibraryDependency)
279 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700280 exportPackage = aarDep.ExportPackage()
281 }
282
283 switch ctx.OtherModuleDependencyTag(module) {
Colin Cross4b964c02018-10-15 16:18:06 -0700284 case instrumentationForTag:
285 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Colin Crossa97c5d32018-03-28 14:58:31 -0700286 case libTag, frameworkResTag:
287 if exportPackage != nil {
288 sharedLibs = append(sharedLibs, exportPackage)
289 }
290 case staticLibTag:
291 if exportPackage != nil {
Colin Cross66f78822018-05-02 12:58:28 -0700292 transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
Colin Crossbec85302019-02-13 13:15:46 -0800293 transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
Colin Cross31656952018-05-24 16:11:20 -0700294 staticLibManifests = append(staticLibManifests, aarDep.ExportedManifest())
Colin Crossc1c37552019-01-31 11:42:41 -0800295 staticRRODirs = append(staticRRODirs, aarDep.ExportedRRODirs()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700296 }
297 }
298 })
299
300 deps = append(deps, sharedLibs...)
Colin Cross66f78822018-05-02 12:58:28 -0700301 deps = append(deps, transitiveStaticLibs...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700302
Colin Cross66f78822018-05-02 12:58:28 -0700303 if len(transitiveStaticLibs) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700304 flags = append(flags, "--auto-add-overlay")
305 }
306
307 for _, sharedLib := range sharedLibs {
308 flags = append(flags, "-I "+sharedLib.String())
309 }
310
Colin Cross66f78822018-05-02 12:58:28 -0700311 transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
Colin Crossc1c37552019-01-31 11:42:41 -0800312 staticRRODirs = android.FirstUniquePaths(staticRRODirs)
Colin Cross66f78822018-05-02 12:58:28 -0700313
Colin Crossc1c37552019-01-31 11:42:41 -0800314 return transitiveStaticLibs, staticLibManifests, staticRRODirs, deps, flags
Colin Crossa97c5d32018-03-28 14:58:31 -0700315}
316
317type AndroidLibrary struct {
318 Library
319 aapt
320
321 androidLibraryProperties androidLibraryProperties
322
323 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700324
325 exportedProguardFlagFiles android.Paths
Colin Cross66f78822018-05-02 12:58:28 -0700326 exportedStaticPackages android.Paths
Colin Cross89c31582018-04-30 15:55:11 -0700327}
328
329func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
330 return a.exportedProguardFlagFiles
Colin Crossa97c5d32018-03-28 14:58:31 -0700331}
332
Colin Cross66f78822018-05-02 12:58:28 -0700333func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
334 return a.exportedStaticPackages
335}
336
Colin Crossa97c5d32018-03-28 14:58:31 -0700337var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
338
339func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
340 a.Module.deps(ctx)
341 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700342 a.aapt.deps(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700343 }
344}
345
346func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800347 a.aapt.isLibrary = true
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700348 a.aapt.buildActions(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700349
350 ctx.CheckbuildFile(a.proguardOptionsFile)
351 ctx.CheckbuildFile(a.exportPackage)
352 ctx.CheckbuildFile(a.aaptSrcJar)
353
354 // apps manifests are handled by aapt, don't let Module see them
355 a.properties.Manifest = nil
356
357 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
358 a.proguardOptionsFile)
359
360 a.Module.compile(ctx, a.aaptSrcJar)
361
Colin Crossf57c5782019-01-25 13:20:38 -0800362 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700363 var res android.Paths
364 if a.androidLibraryProperties.BuildAAR {
365 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
366 ctx.CheckbuildFile(a.aarFile)
367 }
Colin Cross89c31582018-04-30 15:55:11 -0700368
369 ctx.VisitDirectDeps(func(m android.Module) {
370 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
371 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
Colin Cross66f78822018-05-02 12:58:28 -0700372 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
373 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
Colin Cross89c31582018-04-30 15:55:11 -0700374 }
375 })
376
377 a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
Colin Cross66f78822018-05-02 12:58:28 -0700378 a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700379}
380
Colin Cross1b16b0e2019-02-12 14:41:32 -0800381// android_library builds and links sources into a `.jar` file for the device along with Android resources.
382//
383// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
384// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
385// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
386// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700387func AndroidLibraryFactory() android.Module {
388 module := &AndroidLibrary{}
389
390 module.AddProperties(
391 &module.Module.properties,
392 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800393 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700394 &module.Module.protoProperties,
395 &module.aaptProperties,
396 &module.androidLibraryProperties)
397
398 module.androidLibraryProperties.BuildAAR = true
399
Colin Cross48de9a42018-10-02 13:53:33 -0700400 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700401 return module
402}
403
Colin Crossfabb6082018-02-20 17:22:23 -0800404//
405// AAR (android library) prebuilts
406//
Colin Crossfabb6082018-02-20 17:22:23 -0800407
408type AARImportProperties struct {
409 Aars []string
410
Colin Cross479884c2018-07-10 13:39:30 -0700411 Sdk_version *string
412 Min_sdk_version *string
Colin Crossa97c5d32018-03-28 14:58:31 -0700413
414 Static_libs []string
415 Libs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -0700416
417 // if set to true, run Jetifier against .aar file. Defaults to false.
418 Jetifier_enabled *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800419}
420
421type AARImport struct {
422 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700423 android.DefaultableModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800424 prebuilt android.Prebuilt
425
426 properties AARImportProperties
427
Colin Cross66f78822018-05-02 12:58:28 -0700428 classpathFile android.WritablePath
429 proguardFlags android.WritablePath
430 exportPackage android.WritablePath
431 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700432 manifest android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700433
434 exportedStaticPackages android.Paths
Colin Crossfabb6082018-02-20 17:22:23 -0800435}
436
Colin Cross83bb3162018-06-25 15:48:06 -0700437func (a *AARImport) sdkVersion() string {
438 return String(a.properties.Sdk_version)
439}
440
441func (a *AARImport) minSdkVersion() string {
Colin Cross479884c2018-07-10 13:39:30 -0700442 if a.properties.Min_sdk_version != nil {
443 return *a.properties.Min_sdk_version
444 }
Colin Cross83bb3162018-06-25 15:48:06 -0700445 return a.sdkVersion()
446}
447
Dan Willemsen419290a2018-10-31 15:28:47 -0700448func (a *AARImport) targetSdkVersion() string {
449 return a.sdkVersion()
450}
451
Colin Crossa97c5d32018-03-28 14:58:31 -0700452var _ AndroidLibraryDependency = (*AARImport)(nil)
453
454func (a *AARImport) ExportPackage() android.Path {
455 return a.exportPackage
456}
457
Colin Cross89c31582018-04-30 15:55:11 -0700458func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
459 return android.Paths{a.proguardFlags}
460}
461
Colin Crossc1c37552019-01-31 11:42:41 -0800462func (a *AARImport) ExportedRRODirs() android.Paths {
463 return nil
464}
465
Colin Cross66f78822018-05-02 12:58:28 -0700466func (a *AARImport) ExportedStaticPackages() android.Paths {
467 return a.exportedStaticPackages
468}
469
Colin Cross31656952018-05-24 16:11:20 -0700470func (a *AARImport) ExportedManifest() android.Path {
471 return a.manifest
472}
473
Colin Crossfabb6082018-02-20 17:22:23 -0800474func (a *AARImport) Prebuilt() *android.Prebuilt {
475 return &a.prebuilt
476}
477
478func (a *AARImport) Name() string {
479 return a.prebuilt.Name(a.ModuleBase.Name())
480}
481
482func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1f367bf2018-12-18 22:46:24 -0800483 if !ctx.Config().UnbundledBuildPrebuiltSdks() {
Colin Cross83bb3162018-06-25 15:48:06 -0700484 sdkDep := decodeSdkDep(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700485 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700486 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800487 }
488 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700489
Colin Cross42d48b72018-08-29 14:10:52 -0700490 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
491 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800492}
493
494// Unzip an AAR into its constituent files and directories. Any files in Outputs that don't exist in the AAR will be
495// touched to create an empty file, and any directories in $expectedDirs will be created.
496var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
497 blueprint.RuleParams{
498 Command: `rm -rf $outDir && mkdir -p $outDir $expectedDirs && ` +
499 `unzip -qo -d $outDir $in && touch $out`,
500 },
501 "expectedDirs", "outDir")
502
503func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
504 if len(a.properties.Aars) != 1 {
505 ctx.PropertyErrorf("aars", "exactly one aar is required")
506 return
507 }
508
Nan Zhang4c819fb2018-08-27 18:31:46 -0700509 aarName := ctx.ModuleName() + ".aar"
510 var aar android.Path
511 aar = android.PathForModuleSrc(ctx, a.properties.Aars[0])
512 if Bool(a.properties.Jetifier_enabled) {
513 inputFile := aar
514 aar = android.PathForModuleOut(ctx, "jetifier", aarName)
515 TransformJetifier(ctx, aar.(android.WritablePath), inputFile)
516 }
Colin Crossfabb6082018-02-20 17:22:23 -0800517
518 extractedAARDir := android.PathForModuleOut(ctx, "aar")
519 extractedResDir := extractedAARDir.Join(ctx, "res")
520 a.classpathFile = extractedAARDir.Join(ctx, "classes.jar")
521 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700522 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossfabb6082018-02-20 17:22:23 -0800523
524 ctx.Build(pctx, android.BuildParams{
525 Rule: unzipAAR,
526 Input: aar,
Colin Cross10f7c4a2018-05-23 10:59:28 -0700527 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest},
Colin Crossfabb6082018-02-20 17:22:23 -0800528 Description: "unzip AAR",
529 Args: map[string]string{
530 "expectedDirs": extractedResDir.String(),
531 "outDir": extractedAARDir.String(),
532 },
533 })
534
535 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
536 aaptCompileDeps := android.Paths{a.classpathFile}
537 aaptCompileDirs := android.Paths{extractedResDir}
538 flata := compiledResDir.Join(ctx, "gen_res.flata")
539 aapt2CompileDirs(ctx, flata, aaptCompileDirs, aaptCompileDeps)
540
541 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
542 srcJar := android.PathForModuleGen(ctx, "R.jar")
543 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700544 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700545 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800546
547 var linkDeps android.Paths
548
549 linkFlags := []string{
550 "--static-lib",
551 "--no-static-lib-packages",
552 "--auto-add-overlay",
553 }
554
Colin Cross10f7c4a2018-05-23 10:59:28 -0700555 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
556 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800557
Colin Crossc1c37552019-01-31 11:42:41 -0800558 transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags := aaptLibs(ctx, sdkContext(a))
Colin Cross31656952018-05-24 16:11:20 -0700559
560 _ = staticLibManifests
Colin Crossc1c37552019-01-31 11:42:41 -0800561 _ = staticRRODirs
Colin Crossfabb6082018-02-20 17:22:23 -0800562
Colin Crossa97c5d32018-03-28 14:58:31 -0700563 linkDeps = append(linkDeps, libDeps...)
564 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800565
Colin Cross66f78822018-05-02 12:58:28 -0700566 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800567
Colin Cross66f78822018-05-02 12:58:28 -0700568 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Colin Crossa97c5d32018-03-28 14:58:31 -0700569 linkFlags, linkDeps, nil, overlayRes)
Colin Crossfabb6082018-02-20 17:22:23 -0800570}
571
572var _ Dependency = (*AARImport)(nil)
573
574func (a *AARImport) HeaderJars() android.Paths {
575 return android.Paths{a.classpathFile}
576}
577
578func (a *AARImport) ImplementationJars() android.Paths {
579 return android.Paths{a.classpathFile}
580}
581
Colin Cross331a1212018-08-15 20:40:52 -0700582func (a *AARImport) ResourceJars() android.Paths {
583 return nil
584}
585
586func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
587 return android.Paths{a.classpathFile}
588}
589
Colin Crossf24a22a2019-01-31 14:12:44 -0800590func (a *AARImport) DexJar() android.Path {
591 return nil
592}
593
Colin Crossfabb6082018-02-20 17:22:23 -0800594func (a *AARImport) AidlIncludeDirs() android.Paths {
595 return nil
596}
597
Jiyong Park1be96912018-05-28 18:02:19 +0900598func (a *AARImport) ExportedSdkLibs() []string {
599 return nil
600}
601
Colin Crossfabb6082018-02-20 17:22:23 -0800602var _ android.PrebuiltInterface = (*Import)(nil)
603
Colin Cross1b16b0e2019-02-12 14:41:32 -0800604// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
605//
606// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
607// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -0800608func AARImportFactory() android.Module {
609 module := &AARImport{}
610
611 module.AddProperties(&module.properties)
612
613 android.InitPrebuiltModule(module, &module.properties.Aars)
Colin Cross48de9a42018-10-02 13:53:33 -0700614 InitJavaModule(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800615 return module
616}