blob: b96f1272bab97d7839d292b8b19add53f965b369 [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 Crossa97c5d32018-03-28 14:58:31 -070019 "strings"
Colin Crossfabb6082018-02-20 17:22:23 -080020
21 "github.com/google/blueprint"
Colin Crossa97c5d32018-03-28 14:58:31 -070022 "github.com/google/blueprint/proptools"
Colin Crossfabb6082018-02-20 17:22:23 -080023)
24
Colin Crossa97c5d32018-03-28 14:58:31 -070025type AndroidLibraryDependency interface {
26 Dependency
27 ExportPackage() android.Path
Colin Cross89c31582018-04-30 15:55:11 -070028 ExportedProguardFlagFiles() android.Paths
Colin Crossc1c37552019-01-31 11:42:41 -080029 ExportedRRODirs() android.Paths
Colin Cross66f78822018-05-02 12:58:28 -070030 ExportedStaticPackages() android.Paths
Colin Cross31656952018-05-24 16:11:20 -070031 ExportedManifest() android.Path
Colin Crossa97c5d32018-03-28 14:58:31 -070032}
33
34func init() {
35 android.RegisterModuleType("android_library_import", AARImportFactory)
36 android.RegisterModuleType("android_library", AndroidLibraryFactory)
37}
38
39//
40// AAR (android library)
41//
42
43type androidLibraryProperties struct {
44 BuildAAR bool `blueprint:"mutated"`
45}
46
47type aaptProperties struct {
48 // flags passed to aapt when creating the apk
49 Aaptflags []string
50
Dan Willemsen72be5902018-10-24 20:24:57 -070051 // include all resource configurations, not just the product-configured
52 // ones.
53 Aapt_include_all_resources *bool
54
Colin Crossa97c5d32018-03-28 14:58:31 -070055 // list of directories relative to the Blueprints file containing assets.
56 // Defaults to "assets"
57 Asset_dirs []string
58
59 // list of directories relative to the Blueprints file containing
60 // Android resources
61 Resource_dirs []string
62
63 // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml".
64 Manifest *string
65}
66
67type aapt struct {
Colin Cross66f78822018-05-02 12:58:28 -070068 aaptSrcJar android.Path
69 exportPackage android.Path
70 manifestPath android.Path
71 proguardOptionsFile android.Path
72 rroDirs android.Paths
73 rTxt android.Path
74 extraAaptPackagesFile android.Path
Colin Cross1b6a3cf2018-07-24 14:51:30 -070075 isLibrary bool
Colin Crossa97c5d32018-03-28 14:58:31 -070076
77 aaptProperties aaptProperties
78}
79
80func (a *aapt) ExportPackage() android.Path {
81 return a.exportPackage
82}
83
Colin Crossc1c37552019-01-31 11:42:41 -080084func (a *aapt) ExportedRRODirs() android.Paths {
85 return a.rroDirs
86}
87
88func (a *aapt) ExportedManifest() android.Path {
89 return a.manifestPath
90}
91
Colin Cross31656952018-05-24 16:11:20 -070092func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext sdkContext, manifestPath android.Path) (flags []string,
93 deps android.Paths, resDirs, overlayDirs []globbedResourceDir, rroDirs android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -070094
95 hasVersionCode := false
96 hasVersionName := false
Colin Crossa97c5d32018-03-28 14:58:31 -070097 for _, f := range a.aaptProperties.Aaptflags {
98 if strings.HasPrefix(f, "--version-code") {
99 hasVersionCode = true
100 } else if strings.HasPrefix(f, "--version-name") {
101 hasVersionName = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700102 }
103 }
104
105 var linkFlags []string
106
107 // Flags specified in Android.bp
108 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
109
110 linkFlags = append(linkFlags, "--no-static-lib-packages")
111
112 // Find implicit or explicit asset and resource dirs
113 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
114 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
115
116 var linkDeps android.Paths
117
118 // Glob directories into lists of paths
119 for _, dir := range resourceDirs {
120 resDirs = append(resDirs, globbedResourceDir{
121 dir: dir,
122 files: androidResourceGlob(ctx, dir),
123 })
124 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, dir)
125 overlayDirs = append(overlayDirs, resOverlayDirs...)
126 rroDirs = append(rroDirs, resRRODirs...)
127 }
128
129 var assetFiles android.Paths
130 for _, dir := range assetDirs {
131 assetFiles = append(assetFiles, androidResourceGlob(ctx, dir)...)
132 }
133
Colin Crossa97c5d32018-03-28 14:58:31 -0700134 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
135 linkDeps = append(linkDeps, manifestPath)
136
137 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirs.Strings(), "-A "))
138 linkDeps = append(linkDeps, assetFiles...)
139
Colin Crossa97c5d32018-03-28 14:58:31 -0700140 // SDK version flags
Colin Cross83bb3162018-06-25 15:48:06 -0700141 minSdkVersion := sdkVersionOrDefault(ctx, sdkContext.minSdkVersion())
Colin Crossa97c5d32018-03-28 14:58:31 -0700142
Colin Cross83bb3162018-06-25 15:48:06 -0700143 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
144 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700145
Colin Crossa97c5d32018-03-28 14:58:31 -0700146 // Version code
147 if !hasVersionCode {
148 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion())
149 }
150
151 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700152 var versionName string
153 if ctx.ModuleName() == "framework-res" {
154 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
155 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700156 // if it contains the build number. Use the PlatformVersionName instead.
157 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700158 } else {
159 versionName = ctx.Config().AppsDefaultVersionName()
160 }
161 versionName = proptools.NinjaEscape([]string{versionName})[0]
Colin Crossa97c5d32018-03-28 14:58:31 -0700162 linkFlags = append(linkFlags, "--version-name ", versionName)
163 }
164
Colin Cross31656952018-05-24 16:11:20 -0700165 return linkFlags, linkDeps, resDirs, overlayDirs, rroDirs
Colin Crossa97c5d32018-03-28 14:58:31 -0700166}
167
Colin Cross83bb3162018-06-25 15:48:06 -0700168func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkContext sdkContext) {
Colin Cross42308aa2018-11-14 21:44:17 -0800169 sdkDep := decodeSdkDep(ctx, sdkContext)
170 if sdkDep.frameworkResModule != "" {
171 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700172 }
173}
174
Colin Cross83bb3162018-06-25 15:48:06 -0700175func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, extraLinkFlags ...string) {
Colin Crossc1c37552019-01-31 11:42:41 -0800176 transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags := aaptLibs(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700177
Colin Cross31656952018-05-24 16:11:20 -0700178 // App manifest file
179 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
180 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
181
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700182 manifestPath := manifestMerger(ctx, manifestSrcPath, sdkContext, staticLibManifests, a.isLibrary)
Colin Cross31656952018-05-24 16:11:20 -0700183
184 linkFlags, linkDeps, resDirs, overlayDirs, rroDirs := a.aapt2Flags(ctx, sdkContext, manifestPath)
185
Colin Crossc1c37552019-01-31 11:42:41 -0800186 rroDirs = append(rroDirs, staticRRODirs...)
187
Colin Cross31656952018-05-24 16:11:20 -0700188 linkFlags = append(linkFlags, libFlags...)
189 linkDeps = append(linkDeps, libDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700190 linkFlags = append(linkFlags, extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700191 if a.isLibrary {
192 linkFlags = append(linkFlags, "--static-lib")
193 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700194
195 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
196 srcJar := android.PathForModuleGen(ctx, "R.jar")
197 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
198 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700199 // This file isn't used by Soong, but is generated for exporting
200 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Crossa97c5d32018-03-28 14:58:31 -0700201
Colin Cross4aaa84a2018-08-21 15:14:37 -0700202 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700203 for _, dir := range resDirs {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700204 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700205 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700206
207 var compiledRes, compiledOverlay android.Paths
208
209 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
210
211 if a.isLibrary {
212 // For a static library we treat all the resources equally with no overlay.
213 for _, compiledResDir := range compiledResDirs {
214 compiledRes = append(compiledRes, compiledResDir...)
215 }
216 } else if len(transitiveStaticLibs) > 0 {
217 // If we are using static android libraries, every source file becomes an overlay.
218 // This is to emulate old AAPT behavior which simulated library support.
219 for _, compiledResDir := range compiledResDirs {
220 compiledOverlay = append(compiledOverlay, compiledResDir...)
221 }
222 } else if len(compiledResDirs) > 0 {
223 // Without static libraries, the first directory is our directory, which can then be
224 // overlaid by the rest.
225 compiledRes = append(compiledRes, compiledResDirs[0]...)
226 for _, compiledResDir := range compiledResDirs[1:] {
227 compiledOverlay = append(compiledOverlay, compiledResDir...)
228 }
229 }
230
Colin Crossa97c5d32018-03-28 14:58:31 -0700231 for _, dir := range overlayDirs {
232 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files).Paths()...)
233 }
234
Colin Cross66f78822018-05-02 12:58:28 -0700235 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
Colin Crossa97c5d32018-03-28 14:58:31 -0700236 linkFlags, linkDeps, compiledRes, compiledOverlay)
237
238 a.aaptSrcJar = srcJar
239 a.exportPackage = packageRes
240 a.manifestPath = manifestPath
241 a.proguardOptionsFile = proguardOptionsFile
242 a.rroDirs = rroDirs
Colin Cross66f78822018-05-02 12:58:28 -0700243 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700244 a.rTxt = rTxt
245}
246
247// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Colin Cross31656952018-05-24 16:11:20 -0700248func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, staticLibManifests,
Colin Crossc1c37552019-01-31 11:42:41 -0800249 staticRRODirs, deps android.Paths, flags []string) {
Colin Cross66f78822018-05-02 12:58:28 -0700250
Colin Crossa97c5d32018-03-28 14:58:31 -0700251 var sharedLibs android.Paths
252
Colin Cross83bb3162018-06-25 15:48:06 -0700253 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700254 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700255 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700256 }
257
258 ctx.VisitDirectDeps(func(module android.Module) {
259 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700260 aarDep, _ := module.(AndroidLibraryDependency)
261 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700262 exportPackage = aarDep.ExportPackage()
263 }
264
265 switch ctx.OtherModuleDependencyTag(module) {
Colin Cross4b964c02018-10-15 16:18:06 -0700266 case instrumentationForTag:
267 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Colin Crossa97c5d32018-03-28 14:58:31 -0700268 case libTag, frameworkResTag:
269 if exportPackage != nil {
270 sharedLibs = append(sharedLibs, exportPackage)
271 }
272 case staticLibTag:
273 if exportPackage != nil {
Colin Cross66f78822018-05-02 12:58:28 -0700274 transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
275 transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
Colin Cross31656952018-05-24 16:11:20 -0700276 staticLibManifests = append(staticLibManifests, aarDep.ExportedManifest())
Colin Crossc1c37552019-01-31 11:42:41 -0800277 staticRRODirs = append(staticRRODirs, aarDep.ExportedRRODirs()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700278 }
279 }
280 })
281
282 deps = append(deps, sharedLibs...)
Colin Cross66f78822018-05-02 12:58:28 -0700283 deps = append(deps, transitiveStaticLibs...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700284
Colin Cross66f78822018-05-02 12:58:28 -0700285 if len(transitiveStaticLibs) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700286 flags = append(flags, "--auto-add-overlay")
287 }
288
289 for _, sharedLib := range sharedLibs {
290 flags = append(flags, "-I "+sharedLib.String())
291 }
292
Colin Cross66f78822018-05-02 12:58:28 -0700293 transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
Colin Crossc1c37552019-01-31 11:42:41 -0800294 staticRRODirs = android.FirstUniquePaths(staticRRODirs)
Colin Cross66f78822018-05-02 12:58:28 -0700295
Colin Crossc1c37552019-01-31 11:42:41 -0800296 return transitiveStaticLibs, staticLibManifests, staticRRODirs, deps, flags
Colin Crossa97c5d32018-03-28 14:58:31 -0700297}
298
299type AndroidLibrary struct {
300 Library
301 aapt
302
303 androidLibraryProperties androidLibraryProperties
304
305 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700306
307 exportedProguardFlagFiles android.Paths
Colin Cross66f78822018-05-02 12:58:28 -0700308 exportedStaticPackages android.Paths
Colin Cross89c31582018-04-30 15:55:11 -0700309}
310
311func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
312 return a.exportedProguardFlagFiles
Colin Crossa97c5d32018-03-28 14:58:31 -0700313}
314
Colin Cross66f78822018-05-02 12:58:28 -0700315func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
316 return a.exportedStaticPackages
317}
318
Colin Crossa97c5d32018-03-28 14:58:31 -0700319var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
320
321func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
322 a.Module.deps(ctx)
323 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700324 a.aapt.deps(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700325 }
326}
327
328func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700329 a.isLibrary = true
330 a.aapt.buildActions(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700331
332 ctx.CheckbuildFile(a.proguardOptionsFile)
333 ctx.CheckbuildFile(a.exportPackage)
334 ctx.CheckbuildFile(a.aaptSrcJar)
335
336 // apps manifests are handled by aapt, don't let Module see them
337 a.properties.Manifest = nil
338
339 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
340 a.proguardOptionsFile)
341
342 a.Module.compile(ctx, a.aaptSrcJar)
343
Colin Crossf57c5782019-01-25 13:20:38 -0800344 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700345 var res android.Paths
346 if a.androidLibraryProperties.BuildAAR {
347 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
348 ctx.CheckbuildFile(a.aarFile)
349 }
Colin Cross89c31582018-04-30 15:55:11 -0700350
351 ctx.VisitDirectDeps(func(m android.Module) {
352 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
353 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
Colin Cross66f78822018-05-02 12:58:28 -0700354 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
355 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
Colin Cross89c31582018-04-30 15:55:11 -0700356 }
357 })
358
359 a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
Colin Cross66f78822018-05-02 12:58:28 -0700360 a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700361}
362
363func AndroidLibraryFactory() android.Module {
364 module := &AndroidLibrary{}
365
366 module.AddProperties(
367 &module.Module.properties,
368 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800369 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700370 &module.Module.protoProperties,
371 &module.aaptProperties,
372 &module.androidLibraryProperties)
373
374 module.androidLibraryProperties.BuildAAR = true
375
Colin Cross48de9a42018-10-02 13:53:33 -0700376 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700377 return module
378}
379
Colin Crossfabb6082018-02-20 17:22:23 -0800380//
381// AAR (android library) prebuilts
382//
Colin Crossfabb6082018-02-20 17:22:23 -0800383
384type AARImportProperties struct {
385 Aars []string
386
Colin Cross479884c2018-07-10 13:39:30 -0700387 Sdk_version *string
388 Min_sdk_version *string
Colin Crossa97c5d32018-03-28 14:58:31 -0700389
390 Static_libs []string
391 Libs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -0700392
393 // if set to true, run Jetifier against .aar file. Defaults to false.
394 Jetifier_enabled *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800395}
396
397type AARImport struct {
398 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700399 android.DefaultableModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800400 prebuilt android.Prebuilt
401
402 properties AARImportProperties
403
Colin Cross66f78822018-05-02 12:58:28 -0700404 classpathFile android.WritablePath
405 proguardFlags android.WritablePath
406 exportPackage android.WritablePath
407 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700408 manifest android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700409
410 exportedStaticPackages android.Paths
Colin Crossfabb6082018-02-20 17:22:23 -0800411}
412
Colin Cross83bb3162018-06-25 15:48:06 -0700413func (a *AARImport) sdkVersion() string {
414 return String(a.properties.Sdk_version)
415}
416
417func (a *AARImport) minSdkVersion() string {
Colin Cross479884c2018-07-10 13:39:30 -0700418 if a.properties.Min_sdk_version != nil {
419 return *a.properties.Min_sdk_version
420 }
Colin Cross83bb3162018-06-25 15:48:06 -0700421 return a.sdkVersion()
422}
423
Dan Willemsen419290a2018-10-31 15:28:47 -0700424func (a *AARImport) targetSdkVersion() string {
425 return a.sdkVersion()
426}
427
Colin Crossa97c5d32018-03-28 14:58:31 -0700428var _ AndroidLibraryDependency = (*AARImport)(nil)
429
430func (a *AARImport) ExportPackage() android.Path {
431 return a.exportPackage
432}
433
Colin Cross89c31582018-04-30 15:55:11 -0700434func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
435 return android.Paths{a.proguardFlags}
436}
437
Colin Crossc1c37552019-01-31 11:42:41 -0800438func (a *AARImport) ExportedRRODirs() android.Paths {
439 return nil
440}
441
Colin Cross66f78822018-05-02 12:58:28 -0700442func (a *AARImport) ExportedStaticPackages() android.Paths {
443 return a.exportedStaticPackages
444}
445
Colin Cross31656952018-05-24 16:11:20 -0700446func (a *AARImport) ExportedManifest() android.Path {
447 return a.manifest
448}
449
Colin Crossfabb6082018-02-20 17:22:23 -0800450func (a *AARImport) Prebuilt() *android.Prebuilt {
451 return &a.prebuilt
452}
453
454func (a *AARImport) Name() string {
455 return a.prebuilt.Name(a.ModuleBase.Name())
456}
457
458func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1f367bf2018-12-18 22:46:24 -0800459 if !ctx.Config().UnbundledBuildPrebuiltSdks() {
Colin Cross83bb3162018-06-25 15:48:06 -0700460 sdkDep := decodeSdkDep(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700461 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700462 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800463 }
464 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700465
Colin Cross42d48b72018-08-29 14:10:52 -0700466 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
467 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800468}
469
470// Unzip an AAR into its constituent files and directories. Any files in Outputs that don't exist in the AAR will be
471// touched to create an empty file, and any directories in $expectedDirs will be created.
472var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
473 blueprint.RuleParams{
474 Command: `rm -rf $outDir && mkdir -p $outDir $expectedDirs && ` +
475 `unzip -qo -d $outDir $in && touch $out`,
476 },
477 "expectedDirs", "outDir")
478
479func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
480 if len(a.properties.Aars) != 1 {
481 ctx.PropertyErrorf("aars", "exactly one aar is required")
482 return
483 }
484
Nan Zhang4c819fb2018-08-27 18:31:46 -0700485 aarName := ctx.ModuleName() + ".aar"
486 var aar android.Path
487 aar = android.PathForModuleSrc(ctx, a.properties.Aars[0])
488 if Bool(a.properties.Jetifier_enabled) {
489 inputFile := aar
490 aar = android.PathForModuleOut(ctx, "jetifier", aarName)
491 TransformJetifier(ctx, aar.(android.WritablePath), inputFile)
492 }
Colin Crossfabb6082018-02-20 17:22:23 -0800493
494 extractedAARDir := android.PathForModuleOut(ctx, "aar")
495 extractedResDir := extractedAARDir.Join(ctx, "res")
496 a.classpathFile = extractedAARDir.Join(ctx, "classes.jar")
497 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700498 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossfabb6082018-02-20 17:22:23 -0800499
500 ctx.Build(pctx, android.BuildParams{
501 Rule: unzipAAR,
502 Input: aar,
Colin Cross10f7c4a2018-05-23 10:59:28 -0700503 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest},
Colin Crossfabb6082018-02-20 17:22:23 -0800504 Description: "unzip AAR",
505 Args: map[string]string{
506 "expectedDirs": extractedResDir.String(),
507 "outDir": extractedAARDir.String(),
508 },
509 })
510
511 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
512 aaptCompileDeps := android.Paths{a.classpathFile}
513 aaptCompileDirs := android.Paths{extractedResDir}
514 flata := compiledResDir.Join(ctx, "gen_res.flata")
515 aapt2CompileDirs(ctx, flata, aaptCompileDirs, aaptCompileDeps)
516
517 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
518 srcJar := android.PathForModuleGen(ctx, "R.jar")
519 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700520 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700521 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800522
523 var linkDeps android.Paths
524
525 linkFlags := []string{
526 "--static-lib",
527 "--no-static-lib-packages",
528 "--auto-add-overlay",
529 }
530
Colin Cross10f7c4a2018-05-23 10:59:28 -0700531 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
532 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800533
Colin Crossc1c37552019-01-31 11:42:41 -0800534 transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags := aaptLibs(ctx, sdkContext(a))
Colin Cross31656952018-05-24 16:11:20 -0700535
536 _ = staticLibManifests
Colin Crossc1c37552019-01-31 11:42:41 -0800537 _ = staticRRODirs
Colin Crossfabb6082018-02-20 17:22:23 -0800538
Colin Crossa97c5d32018-03-28 14:58:31 -0700539 linkDeps = append(linkDeps, libDeps...)
540 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800541
Colin Cross66f78822018-05-02 12:58:28 -0700542 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800543
Colin Cross66f78822018-05-02 12:58:28 -0700544 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Colin Crossa97c5d32018-03-28 14:58:31 -0700545 linkFlags, linkDeps, nil, overlayRes)
Colin Crossfabb6082018-02-20 17:22:23 -0800546}
547
548var _ Dependency = (*AARImport)(nil)
549
550func (a *AARImport) HeaderJars() android.Paths {
551 return android.Paths{a.classpathFile}
552}
553
554func (a *AARImport) ImplementationJars() android.Paths {
555 return android.Paths{a.classpathFile}
556}
557
Colin Cross331a1212018-08-15 20:40:52 -0700558func (a *AARImport) ResourceJars() android.Paths {
559 return nil
560}
561
562func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
563 return android.Paths{a.classpathFile}
564}
565
Colin Crossfabb6082018-02-20 17:22:23 -0800566func (a *AARImport) AidlIncludeDirs() android.Paths {
567 return nil
568}
569
Jiyong Park1be96912018-05-28 18:02:19 +0900570func (a *AARImport) ExportedSdkLibs() []string {
571 return nil
572}
573
Colin Crossfabb6082018-02-20 17:22:23 -0800574var _ android.PrebuiltInterface = (*Import)(nil)
575
576func AARImportFactory() android.Module {
577 module := &AARImport{}
578
579 module.AddProperties(&module.properties)
580
581 android.InitPrebuiltModule(module, &module.properties.Aars)
Colin Cross48de9a42018-10-02 13:53:33 -0700582 InitJavaModule(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800583 return module
584}