blob: 7495bdad2bd2bac816b0200cee131e7285befd9a [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.
Colin Cross0ddae7f2019-02-07 15:30:01 -080056 // Defaults to ["assets"] if a directory called assets exists. Set to []
57 // to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070058 Asset_dirs []string
59
60 // list of directories relative to the Blueprints file containing
Colin Cross0ddae7f2019-02-07 15:30:01 -080061 // Android resources. Defaults to ["res"] if a directory called res exists.
62 // Set to [] to disable the default.
Colin Crossa97c5d32018-03-28 14:58:31 -070063 Resource_dirs []string
64
65 // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml".
66 Manifest *string
67}
68
69type aapt struct {
Colin Cross66f78822018-05-02 12:58:28 -070070 aaptSrcJar android.Path
71 exportPackage android.Path
72 manifestPath android.Path
73 proguardOptionsFile android.Path
74 rroDirs android.Paths
75 rTxt android.Path
76 extraAaptPackagesFile android.Path
Colin Cross1b6a3cf2018-07-24 14:51:30 -070077 isLibrary bool
Colin Crosse4246ab2019-02-05 21:55:21 -080078 uncompressedJNI bool
Colin Cross46abdad2019-02-07 13:07:08 -080079 useEmbeddedDex bool
Colin Crossa97c5d32018-03-28 14:58:31 -070080
81 aaptProperties aaptProperties
82}
83
84func (a *aapt) ExportPackage() android.Path {
85 return a.exportPackage
86}
87
Colin Crossc1c37552019-01-31 11:42:41 -080088func (a *aapt) ExportedRRODirs() android.Paths {
89 return a.rroDirs
90}
91
92func (a *aapt) ExportedManifest() android.Path {
93 return a.manifestPath
94}
95
Colin Cross31656952018-05-24 16:11:20 -070096func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext sdkContext, manifestPath android.Path) (flags []string,
97 deps android.Paths, resDirs, overlayDirs []globbedResourceDir, rroDirs android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -070098
99 hasVersionCode := false
100 hasVersionName := false
Colin Crossa97c5d32018-03-28 14:58:31 -0700101 for _, f := range a.aaptProperties.Aaptflags {
102 if strings.HasPrefix(f, "--version-code") {
103 hasVersionCode = true
104 } else if strings.HasPrefix(f, "--version-name") {
105 hasVersionName = true
Colin Crossa97c5d32018-03-28 14:58:31 -0700106 }
107 }
108
109 var linkFlags []string
110
111 // Flags specified in Android.bp
112 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
113
114 linkFlags = append(linkFlags, "--no-static-lib-packages")
115
116 // Find implicit or explicit asset and resource dirs
117 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
118 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
119
120 var linkDeps android.Paths
121
122 // Glob directories into lists of paths
123 for _, dir := range resourceDirs {
124 resDirs = append(resDirs, globbedResourceDir{
125 dir: dir,
126 files: androidResourceGlob(ctx, dir),
127 })
128 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, dir)
129 overlayDirs = append(overlayDirs, resOverlayDirs...)
130 rroDirs = append(rroDirs, resRRODirs...)
131 }
132
133 var assetFiles android.Paths
134 for _, dir := range assetDirs {
135 assetFiles = append(assetFiles, androidResourceGlob(ctx, dir)...)
136 }
137
Colin Crossa97c5d32018-03-28 14:58:31 -0700138 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
139 linkDeps = append(linkDeps, manifestPath)
140
141 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirs.Strings(), "-A "))
142 linkDeps = append(linkDeps, assetFiles...)
143
Colin Crossa97c5d32018-03-28 14:58:31 -0700144 // SDK version flags
Colin Cross83bb3162018-06-25 15:48:06 -0700145 minSdkVersion := sdkVersionOrDefault(ctx, sdkContext.minSdkVersion())
Colin Crossa97c5d32018-03-28 14:58:31 -0700146
Colin Cross83bb3162018-06-25 15:48:06 -0700147 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
148 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700149
Colin Crossa97c5d32018-03-28 14:58:31 -0700150 // Version code
151 if !hasVersionCode {
152 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion())
153 }
154
155 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700156 var versionName string
157 if ctx.ModuleName() == "framework-res" {
158 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
159 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700160 // if it contains the build number. Use the PlatformVersionName instead.
161 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700162 } else {
163 versionName = ctx.Config().AppsDefaultVersionName()
164 }
165 versionName = proptools.NinjaEscape([]string{versionName})[0]
Colin Crossa97c5d32018-03-28 14:58:31 -0700166 linkFlags = append(linkFlags, "--version-name ", versionName)
167 }
168
Colin Cross31656952018-05-24 16:11:20 -0700169 return linkFlags, linkDeps, resDirs, overlayDirs, rroDirs
Colin Crossa97c5d32018-03-28 14:58:31 -0700170}
171
Colin Cross83bb3162018-06-25 15:48:06 -0700172func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkContext sdkContext) {
Colin Cross42308aa2018-11-14 21:44:17 -0800173 sdkDep := decodeSdkDep(ctx, sdkContext)
174 if sdkDep.frameworkResModule != "" {
175 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700176 }
177}
178
Colin Cross83bb3162018-06-25 15:48:06 -0700179func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, extraLinkFlags ...string) {
Colin Crossc1c37552019-01-31 11:42:41 -0800180 transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags := aaptLibs(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700181
Colin Cross31656952018-05-24 16:11:20 -0700182 // App manifest file
183 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
184 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
185
Colin Cross46abdad2019-02-07 13:07:08 -0800186 manifestPath := manifestMerger(ctx, manifestSrcPath, sdkContext, staticLibManifests, a.isLibrary,
187 a.uncompressedJNI, a.useEmbeddedDex)
Colin Cross31656952018-05-24 16:11:20 -0700188
189 linkFlags, linkDeps, resDirs, overlayDirs, rroDirs := a.aapt2Flags(ctx, sdkContext, manifestPath)
190
Colin Crossc1c37552019-01-31 11:42:41 -0800191 rroDirs = append(rroDirs, staticRRODirs...)
Anton Hansson129b9ce2019-02-07 12:21:42 +0000192 // TODO(b/124035856): stop de-duping when there are no more dupe resource dirs.
193 rroDirs = android.FirstUniquePaths(rroDirs)
Colin Crossc1c37552019-01-31 11:42:41 -0800194
Colin Cross31656952018-05-24 16:11:20 -0700195 linkFlags = append(linkFlags, libFlags...)
196 linkDeps = append(linkDeps, libDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700197 linkFlags = append(linkFlags, extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700198 if a.isLibrary {
199 linkFlags = append(linkFlags, "--static-lib")
200 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700201
202 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
203 srcJar := android.PathForModuleGen(ctx, "R.jar")
204 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
205 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700206 // This file isn't used by Soong, but is generated for exporting
207 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Crossa97c5d32018-03-28 14:58:31 -0700208
Colin Cross4aaa84a2018-08-21 15:14:37 -0700209 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700210 for _, dir := range resDirs {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700211 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700212 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700213
214 var compiledRes, compiledOverlay android.Paths
215
216 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
217
218 if a.isLibrary {
219 // For a static library we treat all the resources equally with no overlay.
220 for _, compiledResDir := range compiledResDirs {
221 compiledRes = append(compiledRes, compiledResDir...)
222 }
223 } else if len(transitiveStaticLibs) > 0 {
224 // If we are using static android libraries, every source file becomes an overlay.
225 // This is to emulate old AAPT behavior which simulated library support.
226 for _, compiledResDir := range compiledResDirs {
227 compiledOverlay = append(compiledOverlay, compiledResDir...)
228 }
229 } else if len(compiledResDirs) > 0 {
230 // Without static libraries, the first directory is our directory, which can then be
231 // overlaid by the rest.
232 compiledRes = append(compiledRes, compiledResDirs[0]...)
233 for _, compiledResDir := range compiledResDirs[1:] {
234 compiledOverlay = append(compiledOverlay, compiledResDir...)
235 }
236 }
237
Colin Crossa97c5d32018-03-28 14:58:31 -0700238 for _, dir := range overlayDirs {
239 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files).Paths()...)
240 }
241
Colin Cross66f78822018-05-02 12:58:28 -0700242 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
Colin Crossa97c5d32018-03-28 14:58:31 -0700243 linkFlags, linkDeps, compiledRes, compiledOverlay)
244
245 a.aaptSrcJar = srcJar
246 a.exportPackage = packageRes
247 a.manifestPath = manifestPath
248 a.proguardOptionsFile = proguardOptionsFile
249 a.rroDirs = rroDirs
Colin Cross66f78822018-05-02 12:58:28 -0700250 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700251 a.rTxt = rTxt
252}
253
254// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Colin Cross31656952018-05-24 16:11:20 -0700255func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, staticLibManifests,
Colin Crossc1c37552019-01-31 11:42:41 -0800256 staticRRODirs, deps android.Paths, flags []string) {
Colin Cross66f78822018-05-02 12:58:28 -0700257
Colin Crossa97c5d32018-03-28 14:58:31 -0700258 var sharedLibs android.Paths
259
Colin Cross83bb3162018-06-25 15:48:06 -0700260 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700261 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700262 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700263 }
264
265 ctx.VisitDirectDeps(func(module android.Module) {
266 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700267 aarDep, _ := module.(AndroidLibraryDependency)
268 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700269 exportPackage = aarDep.ExportPackage()
270 }
271
272 switch ctx.OtherModuleDependencyTag(module) {
Colin Cross4b964c02018-10-15 16:18:06 -0700273 case instrumentationForTag:
274 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Colin Crossa97c5d32018-03-28 14:58:31 -0700275 case libTag, frameworkResTag:
276 if exportPackage != nil {
277 sharedLibs = append(sharedLibs, exportPackage)
278 }
279 case staticLibTag:
280 if exportPackage != nil {
Colin Cross66f78822018-05-02 12:58:28 -0700281 transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
282 transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
Colin Cross31656952018-05-24 16:11:20 -0700283 staticLibManifests = append(staticLibManifests, aarDep.ExportedManifest())
Colin Crossc1c37552019-01-31 11:42:41 -0800284 staticRRODirs = append(staticRRODirs, aarDep.ExportedRRODirs()...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700285 }
286 }
287 })
288
289 deps = append(deps, sharedLibs...)
Colin Cross66f78822018-05-02 12:58:28 -0700290 deps = append(deps, transitiveStaticLibs...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700291
Colin Cross66f78822018-05-02 12:58:28 -0700292 if len(transitiveStaticLibs) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700293 flags = append(flags, "--auto-add-overlay")
294 }
295
296 for _, sharedLib := range sharedLibs {
297 flags = append(flags, "-I "+sharedLib.String())
298 }
299
Colin Cross66f78822018-05-02 12:58:28 -0700300 transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
Colin Crossc1c37552019-01-31 11:42:41 -0800301 staticRRODirs = android.FirstUniquePaths(staticRRODirs)
Colin Cross66f78822018-05-02 12:58:28 -0700302
Colin Crossc1c37552019-01-31 11:42:41 -0800303 return transitiveStaticLibs, staticLibManifests, staticRRODirs, deps, flags
Colin Crossa97c5d32018-03-28 14:58:31 -0700304}
305
306type AndroidLibrary struct {
307 Library
308 aapt
309
310 androidLibraryProperties androidLibraryProperties
311
312 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700313
314 exportedProguardFlagFiles android.Paths
Colin Cross66f78822018-05-02 12:58:28 -0700315 exportedStaticPackages android.Paths
Colin Cross89c31582018-04-30 15:55:11 -0700316}
317
318func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
319 return a.exportedProguardFlagFiles
Colin Crossa97c5d32018-03-28 14:58:31 -0700320}
321
Colin Cross66f78822018-05-02 12:58:28 -0700322func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
323 return a.exportedStaticPackages
324}
325
Colin Crossa97c5d32018-03-28 14:58:31 -0700326var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
327
328func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
329 a.Module.deps(ctx)
330 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700331 a.aapt.deps(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700332 }
333}
334
335func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800336 a.aapt.isLibrary = true
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700337 a.aapt.buildActions(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700338
339 ctx.CheckbuildFile(a.proguardOptionsFile)
340 ctx.CheckbuildFile(a.exportPackage)
341 ctx.CheckbuildFile(a.aaptSrcJar)
342
343 // apps manifests are handled by aapt, don't let Module see them
344 a.properties.Manifest = nil
345
346 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
347 a.proguardOptionsFile)
348
349 a.Module.compile(ctx, a.aaptSrcJar)
350
Colin Crossf57c5782019-01-25 13:20:38 -0800351 a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
Colin Crossa97c5d32018-03-28 14:58:31 -0700352 var res android.Paths
353 if a.androidLibraryProperties.BuildAAR {
354 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
355 ctx.CheckbuildFile(a.aarFile)
356 }
Colin Cross89c31582018-04-30 15:55:11 -0700357
358 ctx.VisitDirectDeps(func(m android.Module) {
359 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
360 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
Colin Cross66f78822018-05-02 12:58:28 -0700361 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
362 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
Colin Cross89c31582018-04-30 15:55:11 -0700363 }
364 })
365
366 a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
Colin Cross66f78822018-05-02 12:58:28 -0700367 a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700368}
369
370func AndroidLibraryFactory() android.Module {
371 module := &AndroidLibrary{}
372
373 module.AddProperties(
374 &module.Module.properties,
375 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800376 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700377 &module.Module.protoProperties,
378 &module.aaptProperties,
379 &module.androidLibraryProperties)
380
381 module.androidLibraryProperties.BuildAAR = true
382
Colin Cross48de9a42018-10-02 13:53:33 -0700383 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700384 return module
385}
386
Colin Crossfabb6082018-02-20 17:22:23 -0800387//
388// AAR (android library) prebuilts
389//
Colin Crossfabb6082018-02-20 17:22:23 -0800390
391type AARImportProperties struct {
392 Aars []string
393
Colin Cross479884c2018-07-10 13:39:30 -0700394 Sdk_version *string
395 Min_sdk_version *string
Colin Crossa97c5d32018-03-28 14:58:31 -0700396
397 Static_libs []string
398 Libs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -0700399
400 // if set to true, run Jetifier against .aar file. Defaults to false.
401 Jetifier_enabled *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800402}
403
404type AARImport struct {
405 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700406 android.DefaultableModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800407 prebuilt android.Prebuilt
408
409 properties AARImportProperties
410
Colin Cross66f78822018-05-02 12:58:28 -0700411 classpathFile android.WritablePath
412 proguardFlags android.WritablePath
413 exportPackage android.WritablePath
414 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700415 manifest android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700416
417 exportedStaticPackages android.Paths
Colin Crossfabb6082018-02-20 17:22:23 -0800418}
419
Colin Cross83bb3162018-06-25 15:48:06 -0700420func (a *AARImport) sdkVersion() string {
421 return String(a.properties.Sdk_version)
422}
423
424func (a *AARImport) minSdkVersion() string {
Colin Cross479884c2018-07-10 13:39:30 -0700425 if a.properties.Min_sdk_version != nil {
426 return *a.properties.Min_sdk_version
427 }
Colin Cross83bb3162018-06-25 15:48:06 -0700428 return a.sdkVersion()
429}
430
Dan Willemsen419290a2018-10-31 15:28:47 -0700431func (a *AARImport) targetSdkVersion() string {
432 return a.sdkVersion()
433}
434
Colin Crossa97c5d32018-03-28 14:58:31 -0700435var _ AndroidLibraryDependency = (*AARImport)(nil)
436
437func (a *AARImport) ExportPackage() android.Path {
438 return a.exportPackage
439}
440
Colin Cross89c31582018-04-30 15:55:11 -0700441func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
442 return android.Paths{a.proguardFlags}
443}
444
Colin Crossc1c37552019-01-31 11:42:41 -0800445func (a *AARImport) ExportedRRODirs() android.Paths {
446 return nil
447}
448
Colin Cross66f78822018-05-02 12:58:28 -0700449func (a *AARImport) ExportedStaticPackages() android.Paths {
450 return a.exportedStaticPackages
451}
452
Colin Cross31656952018-05-24 16:11:20 -0700453func (a *AARImport) ExportedManifest() android.Path {
454 return a.manifest
455}
456
Colin Crossfabb6082018-02-20 17:22:23 -0800457func (a *AARImport) Prebuilt() *android.Prebuilt {
458 return &a.prebuilt
459}
460
461func (a *AARImport) Name() string {
462 return a.prebuilt.Name(a.ModuleBase.Name())
463}
464
465func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1f367bf2018-12-18 22:46:24 -0800466 if !ctx.Config().UnbundledBuildPrebuiltSdks() {
Colin Cross83bb3162018-06-25 15:48:06 -0700467 sdkDep := decodeSdkDep(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700468 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700469 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800470 }
471 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700472
Colin Cross42d48b72018-08-29 14:10:52 -0700473 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
474 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800475}
476
477// Unzip an AAR into its constituent files and directories. Any files in Outputs that don't exist in the AAR will be
478// touched to create an empty file, and any directories in $expectedDirs will be created.
479var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
480 blueprint.RuleParams{
481 Command: `rm -rf $outDir && mkdir -p $outDir $expectedDirs && ` +
482 `unzip -qo -d $outDir $in && touch $out`,
483 },
484 "expectedDirs", "outDir")
485
486func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
487 if len(a.properties.Aars) != 1 {
488 ctx.PropertyErrorf("aars", "exactly one aar is required")
489 return
490 }
491
Nan Zhang4c819fb2018-08-27 18:31:46 -0700492 aarName := ctx.ModuleName() + ".aar"
493 var aar android.Path
494 aar = android.PathForModuleSrc(ctx, a.properties.Aars[0])
495 if Bool(a.properties.Jetifier_enabled) {
496 inputFile := aar
497 aar = android.PathForModuleOut(ctx, "jetifier", aarName)
498 TransformJetifier(ctx, aar.(android.WritablePath), inputFile)
499 }
Colin Crossfabb6082018-02-20 17:22:23 -0800500
501 extractedAARDir := android.PathForModuleOut(ctx, "aar")
502 extractedResDir := extractedAARDir.Join(ctx, "res")
503 a.classpathFile = extractedAARDir.Join(ctx, "classes.jar")
504 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700505 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossfabb6082018-02-20 17:22:23 -0800506
507 ctx.Build(pctx, android.BuildParams{
508 Rule: unzipAAR,
509 Input: aar,
Colin Cross10f7c4a2018-05-23 10:59:28 -0700510 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest},
Colin Crossfabb6082018-02-20 17:22:23 -0800511 Description: "unzip AAR",
512 Args: map[string]string{
513 "expectedDirs": extractedResDir.String(),
514 "outDir": extractedAARDir.String(),
515 },
516 })
517
518 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
519 aaptCompileDeps := android.Paths{a.classpathFile}
520 aaptCompileDirs := android.Paths{extractedResDir}
521 flata := compiledResDir.Join(ctx, "gen_res.flata")
522 aapt2CompileDirs(ctx, flata, aaptCompileDirs, aaptCompileDeps)
523
524 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
525 srcJar := android.PathForModuleGen(ctx, "R.jar")
526 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700527 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700528 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800529
530 var linkDeps android.Paths
531
532 linkFlags := []string{
533 "--static-lib",
534 "--no-static-lib-packages",
535 "--auto-add-overlay",
536 }
537
Colin Cross10f7c4a2018-05-23 10:59:28 -0700538 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
539 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800540
Colin Crossc1c37552019-01-31 11:42:41 -0800541 transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags := aaptLibs(ctx, sdkContext(a))
Colin Cross31656952018-05-24 16:11:20 -0700542
543 _ = staticLibManifests
Colin Crossc1c37552019-01-31 11:42:41 -0800544 _ = staticRRODirs
Colin Crossfabb6082018-02-20 17:22:23 -0800545
Colin Crossa97c5d32018-03-28 14:58:31 -0700546 linkDeps = append(linkDeps, libDeps...)
547 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800548
Colin Cross66f78822018-05-02 12:58:28 -0700549 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800550
Colin Cross66f78822018-05-02 12:58:28 -0700551 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Colin Crossa97c5d32018-03-28 14:58:31 -0700552 linkFlags, linkDeps, nil, overlayRes)
Colin Crossfabb6082018-02-20 17:22:23 -0800553}
554
555var _ Dependency = (*AARImport)(nil)
556
557func (a *AARImport) HeaderJars() android.Paths {
558 return android.Paths{a.classpathFile}
559}
560
561func (a *AARImport) ImplementationJars() android.Paths {
562 return android.Paths{a.classpathFile}
563}
564
Colin Cross331a1212018-08-15 20:40:52 -0700565func (a *AARImport) ResourceJars() android.Paths {
566 return nil
567}
568
569func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
570 return android.Paths{a.classpathFile}
571}
572
Colin Crossf24a22a2019-01-31 14:12:44 -0800573func (a *AARImport) DexJar() android.Path {
574 return nil
575}
576
Colin Crossfabb6082018-02-20 17:22:23 -0800577func (a *AARImport) AidlIncludeDirs() android.Paths {
578 return nil
579}
580
Jiyong Park1be96912018-05-28 18:02:19 +0900581func (a *AARImport) ExportedSdkLibs() []string {
582 return nil
583}
584
Colin Crossfabb6082018-02-20 17:22:23 -0800585var _ android.PrebuiltInterface = (*Import)(nil)
586
587func AARImportFactory() android.Module {
588 module := &AARImport{}
589
590 module.AddProperties(&module.properties)
591
592 android.InitPrebuiltModule(module, &module.properties.Aars)
Colin Cross48de9a42018-10-02 13:53:33 -0700593 InitJavaModule(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800594 return module
595}