blob: 60fbe293b5aa5f05d418ea0b8b3c728017cec591 [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
Colin Cross1b16b0e2019-02-12 14:41:32 -0800370// android_library builds and links sources into a `.jar` file for the device along with Android resources.
371//
372// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
373// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
374// with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
375// an android_app module.
Colin Crossa97c5d32018-03-28 14:58:31 -0700376func AndroidLibraryFactory() android.Module {
377 module := &AndroidLibrary{}
378
379 module.AddProperties(
380 &module.Module.properties,
381 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800382 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700383 &module.Module.protoProperties,
384 &module.aaptProperties,
385 &module.androidLibraryProperties)
386
387 module.androidLibraryProperties.BuildAAR = true
388
Colin Cross48de9a42018-10-02 13:53:33 -0700389 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700390 return module
391}
392
Colin Crossfabb6082018-02-20 17:22:23 -0800393//
394// AAR (android library) prebuilts
395//
Colin Crossfabb6082018-02-20 17:22:23 -0800396
397type AARImportProperties struct {
398 Aars []string
399
Colin Cross479884c2018-07-10 13:39:30 -0700400 Sdk_version *string
401 Min_sdk_version *string
Colin Crossa97c5d32018-03-28 14:58:31 -0700402
403 Static_libs []string
404 Libs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -0700405
406 // if set to true, run Jetifier against .aar file. Defaults to false.
407 Jetifier_enabled *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800408}
409
410type AARImport struct {
411 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700412 android.DefaultableModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800413 prebuilt android.Prebuilt
414
415 properties AARImportProperties
416
Colin Cross66f78822018-05-02 12:58:28 -0700417 classpathFile android.WritablePath
418 proguardFlags android.WritablePath
419 exportPackage android.WritablePath
420 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700421 manifest android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700422
423 exportedStaticPackages android.Paths
Colin Crossfabb6082018-02-20 17:22:23 -0800424}
425
Colin Cross83bb3162018-06-25 15:48:06 -0700426func (a *AARImport) sdkVersion() string {
427 return String(a.properties.Sdk_version)
428}
429
430func (a *AARImport) minSdkVersion() string {
Colin Cross479884c2018-07-10 13:39:30 -0700431 if a.properties.Min_sdk_version != nil {
432 return *a.properties.Min_sdk_version
433 }
Colin Cross83bb3162018-06-25 15:48:06 -0700434 return a.sdkVersion()
435}
436
Dan Willemsen419290a2018-10-31 15:28:47 -0700437func (a *AARImport) targetSdkVersion() string {
438 return a.sdkVersion()
439}
440
Colin Crossa97c5d32018-03-28 14:58:31 -0700441var _ AndroidLibraryDependency = (*AARImport)(nil)
442
443func (a *AARImport) ExportPackage() android.Path {
444 return a.exportPackage
445}
446
Colin Cross89c31582018-04-30 15:55:11 -0700447func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
448 return android.Paths{a.proguardFlags}
449}
450
Colin Crossc1c37552019-01-31 11:42:41 -0800451func (a *AARImport) ExportedRRODirs() android.Paths {
452 return nil
453}
454
Colin Cross66f78822018-05-02 12:58:28 -0700455func (a *AARImport) ExportedStaticPackages() android.Paths {
456 return a.exportedStaticPackages
457}
458
Colin Cross31656952018-05-24 16:11:20 -0700459func (a *AARImport) ExportedManifest() android.Path {
460 return a.manifest
461}
462
Colin Crossfabb6082018-02-20 17:22:23 -0800463func (a *AARImport) Prebuilt() *android.Prebuilt {
464 return &a.prebuilt
465}
466
467func (a *AARImport) Name() string {
468 return a.prebuilt.Name(a.ModuleBase.Name())
469}
470
471func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1f367bf2018-12-18 22:46:24 -0800472 if !ctx.Config().UnbundledBuildPrebuiltSdks() {
Colin Cross83bb3162018-06-25 15:48:06 -0700473 sdkDep := decodeSdkDep(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700474 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700475 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800476 }
477 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700478
Colin Cross42d48b72018-08-29 14:10:52 -0700479 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
480 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800481}
482
483// Unzip an AAR into its constituent files and directories. Any files in Outputs that don't exist in the AAR will be
484// touched to create an empty file, and any directories in $expectedDirs will be created.
485var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
486 blueprint.RuleParams{
487 Command: `rm -rf $outDir && mkdir -p $outDir $expectedDirs && ` +
488 `unzip -qo -d $outDir $in && touch $out`,
489 },
490 "expectedDirs", "outDir")
491
492func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
493 if len(a.properties.Aars) != 1 {
494 ctx.PropertyErrorf("aars", "exactly one aar is required")
495 return
496 }
497
Nan Zhang4c819fb2018-08-27 18:31:46 -0700498 aarName := ctx.ModuleName() + ".aar"
499 var aar android.Path
500 aar = android.PathForModuleSrc(ctx, a.properties.Aars[0])
501 if Bool(a.properties.Jetifier_enabled) {
502 inputFile := aar
503 aar = android.PathForModuleOut(ctx, "jetifier", aarName)
504 TransformJetifier(ctx, aar.(android.WritablePath), inputFile)
505 }
Colin Crossfabb6082018-02-20 17:22:23 -0800506
507 extractedAARDir := android.PathForModuleOut(ctx, "aar")
508 extractedResDir := extractedAARDir.Join(ctx, "res")
509 a.classpathFile = extractedAARDir.Join(ctx, "classes.jar")
510 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700511 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossfabb6082018-02-20 17:22:23 -0800512
513 ctx.Build(pctx, android.BuildParams{
514 Rule: unzipAAR,
515 Input: aar,
Colin Cross10f7c4a2018-05-23 10:59:28 -0700516 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest},
Colin Crossfabb6082018-02-20 17:22:23 -0800517 Description: "unzip AAR",
518 Args: map[string]string{
519 "expectedDirs": extractedResDir.String(),
520 "outDir": extractedAARDir.String(),
521 },
522 })
523
524 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
525 aaptCompileDeps := android.Paths{a.classpathFile}
526 aaptCompileDirs := android.Paths{extractedResDir}
527 flata := compiledResDir.Join(ctx, "gen_res.flata")
528 aapt2CompileDirs(ctx, flata, aaptCompileDirs, aaptCompileDeps)
529
530 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
531 srcJar := android.PathForModuleGen(ctx, "R.jar")
532 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700533 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700534 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800535
536 var linkDeps android.Paths
537
538 linkFlags := []string{
539 "--static-lib",
540 "--no-static-lib-packages",
541 "--auto-add-overlay",
542 }
543
Colin Cross10f7c4a2018-05-23 10:59:28 -0700544 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
545 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800546
Colin Crossc1c37552019-01-31 11:42:41 -0800547 transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags := aaptLibs(ctx, sdkContext(a))
Colin Cross31656952018-05-24 16:11:20 -0700548
549 _ = staticLibManifests
Colin Crossc1c37552019-01-31 11:42:41 -0800550 _ = staticRRODirs
Colin Crossfabb6082018-02-20 17:22:23 -0800551
Colin Crossa97c5d32018-03-28 14:58:31 -0700552 linkDeps = append(linkDeps, libDeps...)
553 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800554
Colin Cross66f78822018-05-02 12:58:28 -0700555 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800556
Colin Cross66f78822018-05-02 12:58:28 -0700557 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Colin Crossa97c5d32018-03-28 14:58:31 -0700558 linkFlags, linkDeps, nil, overlayRes)
Colin Crossfabb6082018-02-20 17:22:23 -0800559}
560
561var _ Dependency = (*AARImport)(nil)
562
563func (a *AARImport) HeaderJars() android.Paths {
564 return android.Paths{a.classpathFile}
565}
566
567func (a *AARImport) ImplementationJars() android.Paths {
568 return android.Paths{a.classpathFile}
569}
570
Colin Cross331a1212018-08-15 20:40:52 -0700571func (a *AARImport) ResourceJars() android.Paths {
572 return nil
573}
574
575func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
576 return android.Paths{a.classpathFile}
577}
578
Colin Crossf24a22a2019-01-31 14:12:44 -0800579func (a *AARImport) DexJar() android.Path {
580 return nil
581}
582
Colin Crossfabb6082018-02-20 17:22:23 -0800583func (a *AARImport) AidlIncludeDirs() android.Paths {
584 return nil
585}
586
Jiyong Park1be96912018-05-28 18:02:19 +0900587func (a *AARImport) ExportedSdkLibs() []string {
588 return nil
589}
590
Colin Crossfabb6082018-02-20 17:22:23 -0800591var _ android.PrebuiltInterface = (*Import)(nil)
592
Colin Cross1b16b0e2019-02-12 14:41:32 -0800593// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
594//
595// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
596// an android_app module.
Colin Crossfabb6082018-02-20 17:22:23 -0800597func AARImportFactory() android.Module {
598 module := &AARImport{}
599
600 module.AddProperties(&module.properties)
601
602 android.InitPrebuiltModule(module, &module.properties.Aars)
Colin Cross48de9a42018-10-02 13:53:33 -0700603 InitJavaModule(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800604 return module
605}