blob: a06d19164dcb6fbf388481778765857c61d2e195 [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
50 // list of directories relative to the Blueprints file containing assets.
51 // Defaults to "assets"
52 Asset_dirs []string
53
54 // list of directories relative to the Blueprints file containing
55 // Android resources
56 Resource_dirs []string
57
58 // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml".
59 Manifest *string
60}
61
62type aapt struct {
Colin Cross66f78822018-05-02 12:58:28 -070063 aaptSrcJar android.Path
64 exportPackage android.Path
65 manifestPath android.Path
66 proguardOptionsFile android.Path
67 rroDirs android.Paths
68 rTxt android.Path
69 extraAaptPackagesFile android.Path
Colin Cross1b6a3cf2018-07-24 14:51:30 -070070 isLibrary bool
Colin Crossa97c5d32018-03-28 14:58:31 -070071
72 aaptProperties aaptProperties
73}
74
75func (a *aapt) ExportPackage() android.Path {
76 return a.exportPackage
77}
78
Colin Cross31656952018-05-24 16:11:20 -070079func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext sdkContext, manifestPath android.Path) (flags []string,
80 deps android.Paths, resDirs, overlayDirs []globbedResourceDir, rroDirs android.Paths) {
Colin Crossa97c5d32018-03-28 14:58:31 -070081
82 hasVersionCode := false
83 hasVersionName := false
Colin Crossa97c5d32018-03-28 14:58:31 -070084 for _, f := range a.aaptProperties.Aaptflags {
85 if strings.HasPrefix(f, "--version-code") {
86 hasVersionCode = true
87 } else if strings.HasPrefix(f, "--version-name") {
88 hasVersionName = true
Colin Crossa97c5d32018-03-28 14:58:31 -070089 }
90 }
91
92 var linkFlags []string
93
94 // Flags specified in Android.bp
95 linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)
96
97 linkFlags = append(linkFlags, "--no-static-lib-packages")
98
99 // Find implicit or explicit asset and resource dirs
100 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
101 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res")
102
103 var linkDeps android.Paths
104
105 // Glob directories into lists of paths
106 for _, dir := range resourceDirs {
107 resDirs = append(resDirs, globbedResourceDir{
108 dir: dir,
109 files: androidResourceGlob(ctx, dir),
110 })
111 resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, dir)
112 overlayDirs = append(overlayDirs, resOverlayDirs...)
113 rroDirs = append(rroDirs, resRRODirs...)
114 }
115
116 var assetFiles android.Paths
117 for _, dir := range assetDirs {
118 assetFiles = append(assetFiles, androidResourceGlob(ctx, dir)...)
119 }
120
Colin Crossa97c5d32018-03-28 14:58:31 -0700121 linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
122 linkDeps = append(linkDeps, manifestPath)
123
124 linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirs.Strings(), "-A "))
125 linkDeps = append(linkDeps, assetFiles...)
126
Colin Crossa97c5d32018-03-28 14:58:31 -0700127 // SDK version flags
Colin Cross83bb3162018-06-25 15:48:06 -0700128 minSdkVersion := sdkVersionOrDefault(ctx, sdkContext.minSdkVersion())
Colin Crossa97c5d32018-03-28 14:58:31 -0700129
Colin Cross83bb3162018-06-25 15:48:06 -0700130 linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
131 linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)
Colin Crossa97c5d32018-03-28 14:58:31 -0700132
Colin Crossa97c5d32018-03-28 14:58:31 -0700133 // Version code
134 if !hasVersionCode {
135 linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion())
136 }
137
138 if !hasVersionName {
Colin Cross402d5e02018-04-25 14:54:06 -0700139 var versionName string
140 if ctx.ModuleName() == "framework-res" {
141 // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the
142 // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
Colin Crossbfd347d2018-05-09 11:11:35 -0700143 // if it contains the build number. Use the PlatformVersionName instead.
144 versionName = ctx.Config().PlatformVersionName()
Colin Cross402d5e02018-04-25 14:54:06 -0700145 } else {
146 versionName = ctx.Config().AppsDefaultVersionName()
147 }
148 versionName = proptools.NinjaEscape([]string{versionName})[0]
Colin Crossa97c5d32018-03-28 14:58:31 -0700149 linkFlags = append(linkFlags, "--version-name ", versionName)
150 }
151
Colin Cross31656952018-05-24 16:11:20 -0700152 return linkFlags, linkDeps, resDirs, overlayDirs, rroDirs
Colin Crossa97c5d32018-03-28 14:58:31 -0700153}
154
Colin Cross83bb3162018-06-25 15:48:06 -0700155func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkContext sdkContext) {
Colin Crossa97c5d32018-03-28 14:58:31 -0700156 if !ctx.Config().UnbundledBuild() {
Colin Cross83bb3162018-06-25 15:48:06 -0700157 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700158 if sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700159 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossa97c5d32018-03-28 14:58:31 -0700160 }
161 }
162}
163
Colin Cross83bb3162018-06-25 15:48:06 -0700164func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, extraLinkFlags ...string) {
Colin Cross31656952018-05-24 16:11:20 -0700165 transitiveStaticLibs, staticLibManifests, libDeps, libFlags := aaptLibs(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700166
Colin Cross31656952018-05-24 16:11:20 -0700167 // App manifest file
168 manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
169 manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile)
170
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700171 manifestPath := manifestMerger(ctx, manifestSrcPath, sdkContext, staticLibManifests, a.isLibrary)
Colin Cross31656952018-05-24 16:11:20 -0700172
173 linkFlags, linkDeps, resDirs, overlayDirs, rroDirs := a.aapt2Flags(ctx, sdkContext, manifestPath)
174
175 linkFlags = append(linkFlags, libFlags...)
176 linkDeps = append(linkDeps, libDeps...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700177 linkFlags = append(linkFlags, extraLinkFlags...)
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700178 if a.isLibrary {
179 linkFlags = append(linkFlags, "--static-lib")
180 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700181
182 packageRes := android.PathForModuleOut(ctx, "package-res.apk")
183 srcJar := android.PathForModuleGen(ctx, "R.jar")
184 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
185 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700186 // This file isn't used by Soong, but is generated for exporting
187 extraPackages := android.PathForModuleOut(ctx, "extra_packages")
Colin Crossa97c5d32018-03-28 14:58:31 -0700188
Colin Cross4aaa84a2018-08-21 15:14:37 -0700189 var compiledResDirs []android.Paths
Colin Crossa97c5d32018-03-28 14:58:31 -0700190 for _, dir := range resDirs {
Colin Cross4aaa84a2018-08-21 15:14:37 -0700191 compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files).Paths())
Colin Crossa97c5d32018-03-28 14:58:31 -0700192 }
Colin Cross4aaa84a2018-08-21 15:14:37 -0700193
194 var compiledRes, compiledOverlay android.Paths
195
196 compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
197
198 if a.isLibrary {
199 // For a static library we treat all the resources equally with no overlay.
200 for _, compiledResDir := range compiledResDirs {
201 compiledRes = append(compiledRes, compiledResDir...)
202 }
203 } else if len(transitiveStaticLibs) > 0 {
204 // If we are using static android libraries, every source file becomes an overlay.
205 // This is to emulate old AAPT behavior which simulated library support.
206 for _, compiledResDir := range compiledResDirs {
207 compiledOverlay = append(compiledOverlay, compiledResDir...)
208 }
209 } else if len(compiledResDirs) > 0 {
210 // Without static libraries, the first directory is our directory, which can then be
211 // overlaid by the rest.
212 compiledRes = append(compiledRes, compiledResDirs[0]...)
213 for _, compiledResDir := range compiledResDirs[1:] {
214 compiledOverlay = append(compiledOverlay, compiledResDir...)
215 }
216 }
217
Colin Crossa97c5d32018-03-28 14:58:31 -0700218 for _, dir := range overlayDirs {
219 compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files).Paths()...)
220 }
221
Colin Cross66f78822018-05-02 12:58:28 -0700222 aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
Colin Crossa97c5d32018-03-28 14:58:31 -0700223 linkFlags, linkDeps, compiledRes, compiledOverlay)
224
225 a.aaptSrcJar = srcJar
226 a.exportPackage = packageRes
227 a.manifestPath = manifestPath
228 a.proguardOptionsFile = proguardOptionsFile
229 a.rroDirs = rroDirs
Colin Cross66f78822018-05-02 12:58:28 -0700230 a.extraAaptPackagesFile = extraPackages
Colin Crossa97c5d32018-03-28 14:58:31 -0700231 a.rTxt = rTxt
232}
233
234// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
Colin Cross31656952018-05-24 16:11:20 -0700235func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, staticLibManifests,
236 deps android.Paths, flags []string) {
Colin Cross66f78822018-05-02 12:58:28 -0700237
Colin Crossa97c5d32018-03-28 14:58:31 -0700238 var sharedLibs android.Paths
239
Colin Cross83bb3162018-06-25 15:48:06 -0700240 sdkDep := decodeSdkDep(ctx, sdkContext)
Colin Crossa97c5d32018-03-28 14:58:31 -0700241 if sdkDep.useFiles {
Colin Cross86a60ae2018-05-29 14:44:55 -0700242 sharedLibs = append(sharedLibs, sdkDep.jars...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700243 }
244
245 ctx.VisitDirectDeps(func(module android.Module) {
246 var exportPackage android.Path
Colin Cross66f78822018-05-02 12:58:28 -0700247 aarDep, _ := module.(AndroidLibraryDependency)
248 if aarDep != nil {
Colin Crossa97c5d32018-03-28 14:58:31 -0700249 exportPackage = aarDep.ExportPackage()
250 }
251
252 switch ctx.OtherModuleDependencyTag(module) {
Colin Cross4b964c02018-10-15 16:18:06 -0700253 case instrumentationForTag:
254 // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
Colin Crossa97c5d32018-03-28 14:58:31 -0700255 case libTag, frameworkResTag:
256 if exportPackage != nil {
257 sharedLibs = append(sharedLibs, exportPackage)
258 }
259 case staticLibTag:
260 if exportPackage != nil {
Colin Cross66f78822018-05-02 12:58:28 -0700261 transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
262 transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
Colin Cross31656952018-05-24 16:11:20 -0700263 staticLibManifests = append(staticLibManifests, aarDep.ExportedManifest())
Colin Crossa97c5d32018-03-28 14:58:31 -0700264 }
265 }
266 })
267
268 deps = append(deps, sharedLibs...)
Colin Cross66f78822018-05-02 12:58:28 -0700269 deps = append(deps, transitiveStaticLibs...)
Colin Crossa97c5d32018-03-28 14:58:31 -0700270
Colin Cross66f78822018-05-02 12:58:28 -0700271 if len(transitiveStaticLibs) > 0 {
Colin Crossa97c5d32018-03-28 14:58:31 -0700272 flags = append(flags, "--auto-add-overlay")
273 }
274
275 for _, sharedLib := range sharedLibs {
276 flags = append(flags, "-I "+sharedLib.String())
277 }
278
Colin Cross66f78822018-05-02 12:58:28 -0700279 transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
280
Colin Cross31656952018-05-24 16:11:20 -0700281 return transitiveStaticLibs, staticLibManifests, deps, flags
Colin Crossa97c5d32018-03-28 14:58:31 -0700282}
283
284type AndroidLibrary struct {
285 Library
286 aapt
287
288 androidLibraryProperties androidLibraryProperties
289
290 aarFile android.WritablePath
Colin Cross89c31582018-04-30 15:55:11 -0700291
292 exportedProguardFlagFiles android.Paths
Colin Cross66f78822018-05-02 12:58:28 -0700293 exportedStaticPackages android.Paths
Colin Cross89c31582018-04-30 15:55:11 -0700294}
295
296func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
297 return a.exportedProguardFlagFiles
Colin Crossa97c5d32018-03-28 14:58:31 -0700298}
299
Colin Cross66f78822018-05-02 12:58:28 -0700300func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
301 return a.exportedStaticPackages
302}
303
Colin Cross31656952018-05-24 16:11:20 -0700304func (a *AndroidLibrary) ExportedManifest() android.Path {
305 return a.manifestPath
306}
307
Colin Crossa97c5d32018-03-28 14:58:31 -0700308var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
309
310func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
311 a.Module.deps(ctx)
312 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700313 a.aapt.deps(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700314 }
315}
316
317func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross1b6a3cf2018-07-24 14:51:30 -0700318 a.isLibrary = true
319 a.aapt.buildActions(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700320
321 ctx.CheckbuildFile(a.proguardOptionsFile)
322 ctx.CheckbuildFile(a.exportPackage)
323 ctx.CheckbuildFile(a.aaptSrcJar)
324
325 // apps manifests are handled by aapt, don't let Module see them
326 a.properties.Manifest = nil
327
328 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
329 a.proguardOptionsFile)
330
331 a.Module.compile(ctx, a.aaptSrcJar)
332
333 a.aarFile = android.PathForOutput(ctx, ctx.ModuleName()+".aar")
334 var res android.Paths
335 if a.androidLibraryProperties.BuildAAR {
336 BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
337 ctx.CheckbuildFile(a.aarFile)
338 }
Colin Cross89c31582018-04-30 15:55:11 -0700339
340 ctx.VisitDirectDeps(func(m android.Module) {
341 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
342 a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
Colin Cross66f78822018-05-02 12:58:28 -0700343 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
344 a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
Colin Cross89c31582018-04-30 15:55:11 -0700345 }
346 })
347
348 a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
Colin Cross66f78822018-05-02 12:58:28 -0700349 a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
Colin Crossa97c5d32018-03-28 14:58:31 -0700350}
351
352func AndroidLibraryFactory() android.Module {
353 module := &AndroidLibrary{}
354
355 module.AddProperties(
356 &module.Module.properties,
357 &module.Module.deviceProperties,
358 &module.Module.protoProperties,
359 &module.aaptProperties,
360 &module.androidLibraryProperties)
361
362 module.androidLibraryProperties.BuildAAR = true
363
Colin Cross48de9a42018-10-02 13:53:33 -0700364 InitJavaModule(module, android.DeviceSupported)
Colin Crossa97c5d32018-03-28 14:58:31 -0700365 return module
366}
367
Colin Crossfabb6082018-02-20 17:22:23 -0800368//
369// AAR (android library) prebuilts
370//
Colin Crossfabb6082018-02-20 17:22:23 -0800371
372type AARImportProperties struct {
373 Aars []string
374
Colin Cross479884c2018-07-10 13:39:30 -0700375 Sdk_version *string
376 Min_sdk_version *string
Colin Crossa97c5d32018-03-28 14:58:31 -0700377
378 Static_libs []string
379 Libs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -0700380
381 // if set to true, run Jetifier against .aar file. Defaults to false.
382 Jetifier_enabled *bool
Colin Crossfabb6082018-02-20 17:22:23 -0800383}
384
385type AARImport struct {
386 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -0700387 android.DefaultableModuleBase
Colin Crossfabb6082018-02-20 17:22:23 -0800388 prebuilt android.Prebuilt
389
390 properties AARImportProperties
391
Colin Cross66f78822018-05-02 12:58:28 -0700392 classpathFile android.WritablePath
393 proguardFlags android.WritablePath
394 exportPackage android.WritablePath
395 extraAaptPackagesFile android.WritablePath
Colin Cross10f7c4a2018-05-23 10:59:28 -0700396 manifest android.WritablePath
Colin Cross66f78822018-05-02 12:58:28 -0700397
398 exportedStaticPackages android.Paths
Colin Crossfabb6082018-02-20 17:22:23 -0800399}
400
Colin Cross83bb3162018-06-25 15:48:06 -0700401func (a *AARImport) sdkVersion() string {
402 return String(a.properties.Sdk_version)
403}
404
405func (a *AARImport) minSdkVersion() string {
Colin Cross479884c2018-07-10 13:39:30 -0700406 if a.properties.Min_sdk_version != nil {
407 return *a.properties.Min_sdk_version
408 }
Colin Cross83bb3162018-06-25 15:48:06 -0700409 return a.sdkVersion()
410}
411
Colin Crossa97c5d32018-03-28 14:58:31 -0700412var _ AndroidLibraryDependency = (*AARImport)(nil)
413
414func (a *AARImport) ExportPackage() android.Path {
415 return a.exportPackage
416}
417
Colin Cross89c31582018-04-30 15:55:11 -0700418func (a *AARImport) ExportedProguardFlagFiles() android.Paths {
419 return android.Paths{a.proguardFlags}
420}
421
Colin Cross66f78822018-05-02 12:58:28 -0700422func (a *AARImport) ExportedStaticPackages() android.Paths {
423 return a.exportedStaticPackages
424}
425
Colin Cross31656952018-05-24 16:11:20 -0700426func (a *AARImport) ExportedManifest() android.Path {
427 return a.manifest
428}
429
Colin Crossfabb6082018-02-20 17:22:23 -0800430func (a *AARImport) Prebuilt() *android.Prebuilt {
431 return &a.prebuilt
432}
433
434func (a *AARImport) Name() string {
435 return a.prebuilt.Name(a.ModuleBase.Name())
436}
437
438func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossfabb6082018-02-20 17:22:23 -0800439 if !ctx.Config().UnbundledBuild() {
Colin Cross83bb3162018-06-25 15:48:06 -0700440 sdkDep := decodeSdkDep(ctx, sdkContext(a))
Colin Crossa97c5d32018-03-28 14:58:31 -0700441 if sdkDep.useModule && sdkDep.frameworkResModule != "" {
Colin Cross42d48b72018-08-29 14:10:52 -0700442 ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
Colin Crossfabb6082018-02-20 17:22:23 -0800443 }
444 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700445
Colin Cross42d48b72018-08-29 14:10:52 -0700446 ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
447 ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800448}
449
450// Unzip an AAR into its constituent files and directories. Any files in Outputs that don't exist in the AAR will be
451// touched to create an empty file, and any directories in $expectedDirs will be created.
452var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
453 blueprint.RuleParams{
454 Command: `rm -rf $outDir && mkdir -p $outDir $expectedDirs && ` +
455 `unzip -qo -d $outDir $in && touch $out`,
456 },
457 "expectedDirs", "outDir")
458
459func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
460 if len(a.properties.Aars) != 1 {
461 ctx.PropertyErrorf("aars", "exactly one aar is required")
462 return
463 }
464
Nan Zhang4c819fb2018-08-27 18:31:46 -0700465 aarName := ctx.ModuleName() + ".aar"
466 var aar android.Path
467 aar = android.PathForModuleSrc(ctx, a.properties.Aars[0])
468 if Bool(a.properties.Jetifier_enabled) {
469 inputFile := aar
470 aar = android.PathForModuleOut(ctx, "jetifier", aarName)
471 TransformJetifier(ctx, aar.(android.WritablePath), inputFile)
472 }
Colin Crossfabb6082018-02-20 17:22:23 -0800473
474 extractedAARDir := android.PathForModuleOut(ctx, "aar")
475 extractedResDir := extractedAARDir.Join(ctx, "res")
476 a.classpathFile = extractedAARDir.Join(ctx, "classes.jar")
477 a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
Colin Cross10f7c4a2018-05-23 10:59:28 -0700478 a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
Colin Crossfabb6082018-02-20 17:22:23 -0800479
480 ctx.Build(pctx, android.BuildParams{
481 Rule: unzipAAR,
482 Input: aar,
Colin Cross10f7c4a2018-05-23 10:59:28 -0700483 Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest},
Colin Crossfabb6082018-02-20 17:22:23 -0800484 Description: "unzip AAR",
485 Args: map[string]string{
486 "expectedDirs": extractedResDir.String(),
487 "outDir": extractedAARDir.String(),
488 },
489 })
490
491 compiledResDir := android.PathForModuleOut(ctx, "flat-res")
492 aaptCompileDeps := android.Paths{a.classpathFile}
493 aaptCompileDirs := android.Paths{extractedResDir}
494 flata := compiledResDir.Join(ctx, "gen_res.flata")
495 aapt2CompileDirs(ctx, flata, aaptCompileDirs, aaptCompileDeps)
496
497 a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
498 srcJar := android.PathForModuleGen(ctx, "R.jar")
499 proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
Colin Crossa97c5d32018-03-28 14:58:31 -0700500 rTxt := android.PathForModuleOut(ctx, "R.txt")
Colin Cross66f78822018-05-02 12:58:28 -0700501 a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
Colin Crossfabb6082018-02-20 17:22:23 -0800502
503 var linkDeps android.Paths
504
505 linkFlags := []string{
506 "--static-lib",
507 "--no-static-lib-packages",
508 "--auto-add-overlay",
509 }
510
Colin Cross10f7c4a2018-05-23 10:59:28 -0700511 linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
512 linkDeps = append(linkDeps, a.manifest)
Colin Crossfabb6082018-02-20 17:22:23 -0800513
Colin Cross31656952018-05-24 16:11:20 -0700514 transitiveStaticLibs, staticLibManifests, libDeps, libFlags := aaptLibs(ctx, sdkContext(a))
515
516 _ = staticLibManifests
Colin Crossfabb6082018-02-20 17:22:23 -0800517
Colin Crossa97c5d32018-03-28 14:58:31 -0700518 linkDeps = append(linkDeps, libDeps...)
519 linkFlags = append(linkFlags, libFlags...)
Colin Crossfabb6082018-02-20 17:22:23 -0800520
Colin Cross66f78822018-05-02 12:58:28 -0700521 overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
Colin Crossfabb6082018-02-20 17:22:23 -0800522
Colin Cross66f78822018-05-02 12:58:28 -0700523 aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
Colin Crossa97c5d32018-03-28 14:58:31 -0700524 linkFlags, linkDeps, nil, overlayRes)
Colin Crossfabb6082018-02-20 17:22:23 -0800525}
526
527var _ Dependency = (*AARImport)(nil)
528
529func (a *AARImport) HeaderJars() android.Paths {
530 return android.Paths{a.classpathFile}
531}
532
533func (a *AARImport) ImplementationJars() android.Paths {
534 return android.Paths{a.classpathFile}
535}
536
Colin Cross331a1212018-08-15 20:40:52 -0700537func (a *AARImport) ResourceJars() android.Paths {
538 return nil
539}
540
541func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
542 return android.Paths{a.classpathFile}
543}
544
Colin Crossfabb6082018-02-20 17:22:23 -0800545func (a *AARImport) AidlIncludeDirs() android.Paths {
546 return nil
547}
548
Jiyong Park1be96912018-05-28 18:02:19 +0900549func (a *AARImport) ExportedSdkLibs() []string {
550 return nil
551}
552
Colin Crossfabb6082018-02-20 17:22:23 -0800553var _ android.PrebuiltInterface = (*Import)(nil)
554
555func AARImportFactory() android.Module {
556 module := &AARImport{}
557
558 module.AddProperties(&module.properties)
559
560 android.InitPrebuiltModule(module, &module.properties.Aars)
Colin Cross48de9a42018-10-02 13:53:33 -0700561 InitJavaModule(module, android.DeviceSupported)
Colin Crossfabb6082018-02-20 17:22:23 -0800562 return module
563}