blob: 3ab07cc90e89301a382f280a5342bd067d7add3f [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 Cross66f78822018-05-02 12:58:28 -070029 ExportedStaticPackages() android.Paths
Colin Cross31656952018-05-24 16:11:20 -070030 ExportedManifest() android.Path
Colin Crossa97c5d32018-03-28 14:58:31 -070031}
32
33func init() {
34 android.RegisterModuleType("android_library_import", AARImportFactory)
35 android.RegisterModuleType("android_library", AndroidLibraryFactory)
36}
37
38//
39// AAR (android library)
40//
41
42type androidLibraryProperties struct {
43 BuildAAR bool `blueprint:"mutated"`
44}
45
46type aaptProperties struct {
47 // flags passed to aapt when creating the apk
48 Aaptflags []string
49
Dan Willemsen72be5902018-10-24 20:24:57 -070050 // include all resource configurations, not just the product-configured
51 // ones.
52 Aapt_include_all_resources *bool
53
Colin Crossa97c5d32018-03-28 14:58:31 -070054 // list of directories relative to the Blueprints file containing assets.
55 // Defaults to "assets"
56 Asset_dirs []string
57
58 // list of directories relative to the Blueprints file containing
59 // Android resources
60 Resource_dirs []string
61
62 // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml".
63 Manifest *string
64}
65
66type aapt struct {
Colin Cross66f78822018-05-02 12:58:28 -070067 aaptSrcJar android.Path
68 exportPackage android.Path
69 manifestPath android.Path
70 proguardOptionsFile android.Path
71 rroDirs android.Paths
72 rTxt android.Path
73 extraAaptPackagesFile android.Path
Colin Cross1b6a3cf2018-07-24 14:51:30 -070074 isLibrary bool
Colin Crossa97c5d32018-03-28 14:58:31 -070075
76 aaptProperties aaptProperties
77}
78
79func (a *aapt) ExportPackage() android.Path {
80 return a.exportPackage
81}
82
Colin Cross31656952018-05-24 16:11:20 -070083func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext sdkContext, manifestPath android.Path) (flags []string,
84 deps android.Paths, resDirs, overlayDirs []globbedResourceDir, rroDirs android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -070085
86 hasVersionCode := false
87 hasVersionName := false
Colin Crossa97c5d32018-03-28 14:58:31 -070088 for _, f := range a.aaptProperties.Aaptflags {
89 if strings.HasPrefix(f, "--version-code") {
90 hasVersionCode = true
91 } else if strings.HasPrefix(f, "--version-name") {
92 hasVersionName = true
Colin Crossa97c5d32018-03-28 14:58:31 -070093 }
94 }
95
96 var linkFlags []string
97
98 // Flags specified in Android.bp
99 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
100
101 linkFlags = append(linkFlags, "--no-static-lib-packages")
102
103 // Find implicit or explicit asset and resource dirs
104 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
105 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
106
107 var linkDeps android.Paths
108
109 // Glob directories into lists of paths
110 for _, dir := range resourceDirs {
111 resDirs = append(resDirs, globbedResourceDir{
112 dir: dir,
113 files: androidResourceGlob(ctx, dir),
114 })
115 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, dir)
116 overlayDirs = append(overlayDirs, resOverlayDirs...)
117 rroDirs = append(rroDirs, resRRODirs...)
118 }
119
120 var assetFiles android.Paths
121 for _, dir := range assetDirs {
122 assetFiles = append(assetFiles, androidResourceGlob(ctx, dir)...)
123 }
124
Colin Crossa97c5d32018-03-28 14:58:31 -0700125 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
126 linkDeps = append(linkDeps, manifestPath)
127
128 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirs.Strings(), "-A "))
129 linkDeps = append(linkDeps, assetFiles...)
130
Colin Crossa97c5d32018-03-28 14:58:31 -0700131 // SDK version flags
Colin Cross83bb3162018-06-25 15:48:06 -0700132 minSdkVersion := sdkVersionOrDefault(ctx, sdkContext.minSdkVersion())
Colin Crossa97c5d32018-03-28 14:58:31 -0700133
Colin Cross83bb3162018-06-25 15:48:06 -0700134 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
135 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700136
Colin Crossa97c5d32018-03-28 14:58:31 -0700137 // Version code
138 if !hasVersionCode {
139 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion())
140 }
141
142 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700143 var versionName string
144 if ctx.ModuleName() == "framework-res" {
145 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
146 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700147 // if it contains the build number. Use the PlatformVersionName instead.
148 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700149 } else {
150 versionName = ctx.Config().AppsDefaultVersionName()
151 }
152 versionName = proptools.NinjaEscape([]string{versionName})[0]
Colin Crossa97c5d32018-03-28 14:58:31 -0700153 linkFlags = append(linkFlags, "--version-name ", versionName)
154 }
155
Colin Cross31656952018-05-24 16:11:20 -0700156 return linkFlags, linkDeps, resDirs, overlayDirs, rroDirs
Colin Crossa97c5d32018-03-28 14:58:31 -0700157}
158
Colin Cross83bb3162018-06-25 15:48:06 -0700159func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkContext sdkContext) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700160 if !ctx.Config().UnbundledBuild() {
Colin Cross83bb3162018-06-25 15:48:06 -0700161 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700162 if sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700163 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700164 }
165 }
166}
167
Colin Cross83bb3162018-06-25 15:48:06 -0700168func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, extraLinkFlags ...string) {
Colin Cross31656952018-05-24 16:11:20 -0700169 transitiveStaticLibs, staticLibManifests, libDeps, libFlags := aaptLibs(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700170
Colin Cross31656952018-05-24 16:11:20 -0700171 // App manifest file
172 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
173 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
174
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700175 manifestPath := manifestMerger(ctx, manifestSrcPath, sdkContext, staticLibManifests, a.isLibrary)
Colin Cross31656952018-05-24 16:11:20 -0700176
177 linkFlags, linkDeps, resDirs, overlayDirs, rroDirs := a.aapt2Flags(ctx, sdkContext, manifestPath)
178
179 linkFlags = append(linkFlags, libFlags...)
180 linkDeps = append(linkDeps, libDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700181 linkFlags = append(linkFlags, extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700182 if a.isLibrary {
183 linkFlags = append(linkFlags, "--static-lib")
184 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700185
186 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
187 srcJar := android.PathForModuleGen(ctx, "R.jar")
188 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
189 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700190 // This file isn't used by Soong, but is generated for exporting
191 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Crossa97c5d32018-03-28 14:58:31 -0700192
Colin Cross4aaa84a2018-08-21 15:14:37 -0700193 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700194 for _, dir := range resDirs {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700195 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700196 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700197
198 var compiledRes, compiledOverlay android.Paths
199
200 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
201
202 if a.isLibrary {
203 // For a static library we treat all the resources equally with no overlay.
204 for _, compiledResDir := range compiledResDirs {
205 compiledRes = append(compiledRes, compiledResDir...)
206 }
207 } else if len(transitiveStaticLibs) > 0 {
208 // If we are using static android libraries, every source file becomes an overlay.
209 // This is to emulate old AAPT behavior which simulated library support.
210 for _, compiledResDir := range compiledResDirs {
211 compiledOverlay = append(compiledOverlay, compiledResDir...)
212 }
213 } else if len(compiledResDirs) > 0 {
214 // Without static libraries, the first directory is our directory, which can then be
215 // overlaid by the rest.
216 compiledRes = append(compiledRes, compiledResDirs[0]...)
217 for _, compiledResDir := range compiledResDirs[1:] {
218 compiledOverlay = append(compiledOverlay, compiledResDir...)
219 }
220 }
221
Colin Crossa97c5d32018-03-28 14:58:31 -0700222 for _, dir := range overlayDirs {
223 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files).Paths()...)
224 }
225
Colin Cross66f78822018-05-02 12:58:28 -0700226 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
Colin Crossa97c5d32018-03-28 14:58:31 -0700227 linkFlags, linkDeps, compiledRes, compiledOverlay)
228
229 a.aaptSrcJar = srcJar
230 a.exportPackage = packageRes
231 a.manifestPath = manifestPath
232 a.proguardOptionsFile = proguardOptionsFile
233 a.rroDirs = rroDirs
Colin Cross66f78822018-05-02 12:58:28 -0700234 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700235 a.rTxt = rTxt
236}
237
238// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Colin Cross31656952018-05-24 16:11:20 -0700239func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, staticLibManifests,
240 deps android.Paths, flags []string) {
Colin Cross66f78822018-05-02 12:58:28 -0700241
Colin Crossa97c5d32018-03-28 14:58:31 -0700242 var sharedLibs android.Paths
243
Colin Cross83bb3162018-06-25 15:48:06 -0700244 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700245 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700246 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700247 }
248
249 ctx.VisitDirectDeps(func(module android.Module) {
250 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700251 aarDep, _ := module.(AndroidLibraryDependency)
252 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700253 exportPackage = aarDep.ExportPackage()
254 }
255
256 switch ctx.OtherModuleDependencyTag(module) {
Colin Cross4b964c02018-10-15 16:18:06 -0700257 case instrumentationForTag:
258 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Colin Crossa97c5d32018-03-28 14:58:31 -0700259 case libTag, frameworkResTag:
260 if exportPackage != nil {
261 sharedLibs = append(sharedLibs, exportPackage)
262 }
263 case staticLibTag:
264 if exportPackage != nil {
Colin Cross66f78822018-05-02 12:58:28 -0700265 transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
266 transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
Colin Cross31656952018-05-24 16:11:20 -0700267 staticLibManifests = append(staticLibManifests, aarDep.ExportedManifest())
Colin Crossa97c5d32018-03-28 14:58:31 -0700268 }
269 }
270 })
271
272 deps = append(deps, sharedLibs...)
Colin Cross66f78822018-05-02 12:58:28 -0700273 deps = append(deps, transitiveStaticLibs...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700274
Colin Cross66f78822018-05-02 12:58:28 -0700275 if len(transitiveStaticLibs) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700276 flags = append(flags, "--auto-add-overlay")
277 }
278
279 for _, sharedLib := range sharedLibs {
280 flags = append(flags, "-I "+sharedLib.String())
281 }
282
Colin Cross66f78822018-05-02 12:58:28 -0700283 transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
284
Colin Cross31656952018-05-24 16:11:20 -0700285 return transitiveStaticLibs, staticLibManifests, deps, flags
Colin Crossa97c5d32018-03-28 14:58:31 -0700286}
287
288type AndroidLibrary struct {
289 Library
290 aapt
291
292 androidLibraryProperties androidLibraryProperties
293
294 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700295
296 exportedProguardFlagFiles android.Paths
Colin Cross66f78822018-05-02 12:58:28 -0700297 exportedStaticPackages android.Paths
Colin Cross89c31582018-04-30 15:55:11 -0700298}
299
300func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
301 return a.exportedProguardFlagFiles
Colin Crossa97c5d32018-03-28 14:58:31 -0700302}
303
Colin Cross66f78822018-05-02 12:58:28 -0700304func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
305 return a.exportedStaticPackages
306}
307
Colin Cross31656952018-05-24 16:11:20 -0700308func (a *AndroidLibrary) ExportedManifest() android.Path {
309 return a.manifestPath
310}
311
Colin Crossa97c5d32018-03-28 14:58:31 -0700312var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
313
314func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
315 a.Module.deps(ctx)
316 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700317 a.aapt.deps(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700318 }
319}
320
321func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700322 a.isLibrary = true
323 a.aapt.buildActions(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700324
325 ctx.CheckbuildFile(a.proguardOptionsFile)
326 ctx.CheckbuildFile(a.exportPackage)
327 ctx.CheckbuildFile(a.aaptSrcJar)
328
329 // apps manifests are handled by aapt, don't let Module see them
330 a.properties.Manifest = nil
331
332 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
333 a.proguardOptionsFile)
334
335 a.Module.compile(ctx, a.aaptSrcJar)
336
337 a.aarFile = android.PathForOutput(ctx, ctx.ModuleName()+".aar")
338 var res android.Paths
339 if a.androidLibraryProperties.BuildAAR {
340 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
341 ctx.CheckbuildFile(a.aarFile)
342 }
Colin Cross89c31582018-04-30 15:55:11 -0700343
344 ctx.VisitDirectDeps(func(m android.Module) {
345 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
346 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
Colin Cross66f78822018-05-02 12:58:28 -0700347 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
348 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
Colin Cross89c31582018-04-30 15:55:11 -0700349 }
350 })
351
352 a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
Colin Cross66f78822018-05-02 12:58:28 -0700353 a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700354}
355
356func AndroidLibraryFactory() android.Module {
357 module := &AndroidLibrary{}
358
359 module.AddProperties(
360 &module.Module.properties,
361 &module.Module.deviceProperties,
362 &module.Module.protoProperties,
363 &module.aaptProperties,
364 &module.androidLibraryProperties)
365
366 module.androidLibraryProperties.BuildAAR = true
367
Colin Cross48de9a42018-10-02 13:53:33 -0700368 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700369 return module
370}
371
Colin Crossfabb6082018-02-20 17:22:23 -0800372//
373// AAR (android library) prebuilts
374//
Colin Crossfabb6082018-02-20 17:22:23 -0800375
376type AARImportProperties struct {
377 Aars []string
378
Colin Cross479884c2018-07-10 13:39:30 -0700379 Sdk_version *string
380 Min_sdk_version *string
Colin Crossa97c5d32018-03-28 14:58:31 -0700381
382 Static_libs []string
383 Libs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -0700384
385 // if set to true, run Jetifier against .aar file. Defaults to false.
386 Jetifier_enabled *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800387}
388
389type AARImport struct {
390 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700391 android.DefaultableModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800392 prebuilt android.Prebuilt
393
394 properties AARImportProperties
395
Colin Cross66f78822018-05-02 12:58:28 -0700396 classpathFile android.WritablePath
397 proguardFlags android.WritablePath
398 exportPackage android.WritablePath
399 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700400 manifest android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700401
402 exportedStaticPackages android.Paths
Colin Crossfabb6082018-02-20 17:22:23 -0800403}
404
Colin Cross83bb3162018-06-25 15:48:06 -0700405func (a *AARImport) sdkVersion() string {
406 return String(a.properties.Sdk_version)
407}
408
409func (a *AARImport) minSdkVersion() string {
Colin Cross479884c2018-07-10 13:39:30 -0700410 if a.properties.Min_sdk_version != nil {
411 return *a.properties.Min_sdk_version
412 }
Colin Cross83bb3162018-06-25 15:48:06 -0700413 return a.sdkVersion()
414}
415
Colin Crossa97c5d32018-03-28 14:58:31 -0700416var _ AndroidLibraryDependency = (*AARImport)(nil)
417
418func (a *AARImport) ExportPackage() android.Path {
419 return a.exportPackage
420}
421
Colin Cross89c31582018-04-30 15:55:11 -0700422func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
423 return android.Paths{a.proguardFlags}
424}
425
Colin Cross66f78822018-05-02 12:58:28 -0700426func (a *AARImport) ExportedStaticPackages() android.Paths {
427 return a.exportedStaticPackages
428}
429
Colin Cross31656952018-05-24 16:11:20 -0700430func (a *AARImport) ExportedManifest() android.Path {
431 return a.manifest
432}
433
Colin Crossfabb6082018-02-20 17:22:23 -0800434func (a *AARImport) Prebuilt() *android.Prebuilt {
435 return &a.prebuilt
436}
437
438func (a *AARImport) Name() string {
439 return a.prebuilt.Name(a.ModuleBase.Name())
440}
441
442func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossfabb6082018-02-20 17:22:23 -0800443 if !ctx.Config().UnbundledBuild() {
Colin Cross83bb3162018-06-25 15:48:06 -0700444 sdkDep := decodeSdkDep(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700445 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700446 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800447 }
448 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700449
Colin Cross42d48b72018-08-29 14:10:52 -0700450 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
451 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800452}
453
454// Unzip an AAR into its constituent files and directories. Any files in Outputs that don't exist in the AAR will be
455// touched to create an empty file, and any directories in $expectedDirs will be created.
456var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
457 blueprint.RuleParams{
458 Command: `rm -rf $outDir && mkdir -p $outDir $expectedDirs && ` +
459 `unzip -qo -d $outDir $in && touch $out`,
460 },
461 "expectedDirs", "outDir")
462
463func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
464 if len(a.properties.Aars) != 1 {
465 ctx.PropertyErrorf("aars", "exactly one aar is required")
466 return
467 }
468
Nan Zhang4c819fb2018-08-27 18:31:46 -0700469 aarName := ctx.ModuleName() + ".aar"
470 var aar android.Path
471 aar = android.PathForModuleSrc(ctx, a.properties.Aars[0])
472 if Bool(a.properties.Jetifier_enabled) {
473 inputFile := aar
474 aar = android.PathForModuleOut(ctx, "jetifier", aarName)
475 TransformJetifier(ctx, aar.(android.WritablePath), inputFile)
476 }
Colin Crossfabb6082018-02-20 17:22:23 -0800477
478 extractedAARDir := android.PathForModuleOut(ctx, "aar")
479 extractedResDir := extractedAARDir.Join(ctx, "res")
480 a.classpathFile = extractedAARDir.Join(ctx, "classes.jar")
481 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700482 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossfabb6082018-02-20 17:22:23 -0800483
484 ctx.Build(pctx, android.BuildParams{
485 Rule: unzipAAR,
486 Input: aar,
Colin Cross10f7c4a2018-05-23 10:59:28 -0700487 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest},
Colin Crossfabb6082018-02-20 17:22:23 -0800488 Description: "unzip AAR",
489 Args: map[string]string{
490 "expectedDirs": extractedResDir.String(),
491 "outDir": extractedAARDir.String(),
492 },
493 })
494
495 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
496 aaptCompileDeps := android.Paths{a.classpathFile}
497 aaptCompileDirs := android.Paths{extractedResDir}
498 flata := compiledResDir.Join(ctx, "gen_res.flata")
499 aapt2CompileDirs(ctx, flata, aaptCompileDirs, aaptCompileDeps)
500
501 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
502 srcJar := android.PathForModuleGen(ctx, "R.jar")
503 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700504 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700505 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800506
507 var linkDeps android.Paths
508
509 linkFlags := []string{
510 "--static-lib",
511 "--no-static-lib-packages",
512 "--auto-add-overlay",
513 }
514
Colin Cross10f7c4a2018-05-23 10:59:28 -0700515 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
516 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800517
Colin Cross31656952018-05-24 16:11:20 -0700518 transitiveStaticLibs, staticLibManifests, libDeps, libFlags := aaptLibs(ctx, sdkContext(a))
519
520 _ = staticLibManifests
Colin Crossfabb6082018-02-20 17:22:23 -0800521
Colin Crossa97c5d32018-03-28 14:58:31 -0700522 linkDeps = append(linkDeps, libDeps...)
523 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800524
Colin Cross66f78822018-05-02 12:58:28 -0700525 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800526
Colin Cross66f78822018-05-02 12:58:28 -0700527 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Colin Crossa97c5d32018-03-28 14:58:31 -0700528 linkFlags, linkDeps, nil, overlayRes)
Colin Crossfabb6082018-02-20 17:22:23 -0800529}
530
531var _ Dependency = (*AARImport)(nil)
532
533func (a *AARImport) HeaderJars() android.Paths {
534 return android.Paths{a.classpathFile}
535}
536
537func (a *AARImport) ImplementationJars() android.Paths {
538 return android.Paths{a.classpathFile}
539}
540
Colin Cross331a1212018-08-15 20:40:52 -0700541func (a *AARImport) ResourceJars() android.Paths {
542 return nil
543}
544
545func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
546 return android.Paths{a.classpathFile}
547}
548
Colin Crossfabb6082018-02-20 17:22:23 -0800549func (a *AARImport) AidlIncludeDirs() android.Paths {
550 return nil
551}
552
Jiyong Park1be96912018-05-28 18:02:19 +0900553func (a *AARImport) ExportedSdkLibs() []string {
554 return nil
555}
556
Colin Crossfabb6082018-02-20 17:22:23 -0800557var _ android.PrebuiltInterface = (*Import)(nil)
558
559func AARImportFactory() android.Module {
560 module := &AARImport{}
561
562 module.AddProperties(&module.properties)
563
564 android.InitPrebuiltModule(module, &module.properties.Aars)
Colin Cross48de9a42018-10-02 13:53:33 -0700565 InitJavaModule(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800566 return module
567}