Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 19 | "fmt" |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 20 | "strings" |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 21 | |
| 22 | "github.com/google/blueprint" |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 24 | ) |
| 25 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 26 | type AndroidLibraryDependency interface { |
| 27 | Dependency |
| 28 | ExportPackage() android.Path |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 29 | ExportedProguardFlagFiles() android.Paths |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 30 | ExportedRRODirs() []rroDir |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 31 | ExportedStaticPackages() android.Paths |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 32 | ExportedManifests() android.Paths |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | func init() { |
| 36 | android.RegisterModuleType("android_library_import", AARImportFactory) |
| 37 | android.RegisterModuleType("android_library", AndroidLibraryFactory) |
| 38 | } |
| 39 | |
| 40 | // |
| 41 | // AAR (android library) |
| 42 | // |
| 43 | |
| 44 | type androidLibraryProperties struct { |
| 45 | BuildAAR bool `blueprint:"mutated"` |
| 46 | } |
| 47 | |
| 48 | type aaptProperties struct { |
| 49 | // flags passed to aapt when creating the apk |
| 50 | Aaptflags []string |
| 51 | |
Dan Willemsen | 72be590 | 2018-10-24 20:24:57 -0700 | [diff] [blame] | 52 | // include all resource configurations, not just the product-configured |
| 53 | // ones. |
| 54 | Aapt_include_all_resources *bool |
| 55 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 56 | // list of directories relative to the Blueprints file containing assets. |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 57 | // Defaults to ["assets"] if a directory called assets exists. Set to [] |
| 58 | // to disable the default. |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 59 | Asset_dirs []string |
| 60 | |
| 61 | // list of directories relative to the Blueprints file containing |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 62 | // Android resources. Defaults to ["res"] if a directory called res exists. |
| 63 | // Set to [] to disable the default. |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 64 | Resource_dirs []string |
| 65 | |
Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 66 | // list of zip files containing Android resources. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 67 | Resource_zips []string `android:"path"` |
Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 68 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 69 | // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml". |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 70 | Manifest *string `android:"path"` |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | type aapt struct { |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 74 | aaptSrcJar android.Path |
| 75 | exportPackage android.Path |
| 76 | manifestPath android.Path |
| 77 | transitiveManifestPaths android.Paths |
| 78 | proguardOptionsFile android.Path |
| 79 | rroDirs []rroDir |
| 80 | rTxt android.Path |
| 81 | extraAaptPackagesFile android.Path |
| 82 | mergedManifestFile android.Path |
| 83 | isLibrary bool |
Sasha Smundak | 6ad7725 | 2019-05-01 13:16:22 -0700 | [diff] [blame] | 84 | useEmbeddedNativeLibs bool |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 85 | useEmbeddedDex bool |
| 86 | usesNonSdkApis bool |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 87 | sdkLibraries []string |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 88 | hasNoCode bool |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 89 | |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 90 | splitNames []string |
| 91 | splits []split |
| 92 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 93 | aaptProperties aaptProperties |
| 94 | } |
| 95 | |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 96 | type split struct { |
| 97 | name string |
| 98 | suffix string |
| 99 | path android.Path |
| 100 | } |
| 101 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 102 | func (a *aapt) ExportPackage() android.Path { |
| 103 | return a.exportPackage |
| 104 | } |
| 105 | |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 106 | func (a *aapt) ExportedRRODirs() []rroDir { |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 107 | return a.rroDirs |
| 108 | } |
| 109 | |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 110 | func (a *aapt) ExportedManifests() android.Paths { |
| 111 | return a.transitiveManifestPaths |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame^] | 114 | func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext sdkContext, |
| 115 | manifestPath android.Path) (compileFlags, linkFlags []string, linkDeps android.Paths, |
| 116 | resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 117 | |
| 118 | hasVersionCode := false |
| 119 | hasVersionName := false |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 120 | for _, f := range a.aaptProperties.Aaptflags { |
| 121 | if strings.HasPrefix(f, "--version-code") { |
| 122 | hasVersionCode = true |
| 123 | } else if strings.HasPrefix(f, "--version-name") { |
| 124 | hasVersionName = true |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 128 | // Flags specified in Android.bp |
| 129 | linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...) |
| 130 | |
| 131 | linkFlags = append(linkFlags, "--no-static-lib-packages") |
| 132 | |
| 133 | // Find implicit or explicit asset and resource dirs |
| 134 | assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets") |
| 135 | resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res") |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 136 | resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 137 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 138 | // Glob directories into lists of paths |
| 139 | for _, dir := range resourceDirs { |
| 140 | resDirs = append(resDirs, globbedResourceDir{ |
| 141 | dir: dir, |
| 142 | files: androidResourceGlob(ctx, dir), |
| 143 | }) |
| 144 | resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, dir) |
| 145 | overlayDirs = append(overlayDirs, resOverlayDirs...) |
| 146 | rroDirs = append(rroDirs, resRRODirs...) |
| 147 | } |
| 148 | |
| 149 | var assetFiles android.Paths |
| 150 | for _, dir := range assetDirs { |
| 151 | assetFiles = append(assetFiles, androidResourceGlob(ctx, dir)...) |
| 152 | } |
| 153 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 154 | linkFlags = append(linkFlags, "--manifest "+manifestPath.String()) |
| 155 | linkDeps = append(linkDeps, manifestPath) |
| 156 | |
| 157 | linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirs.Strings(), "-A ")) |
| 158 | linkDeps = append(linkDeps, assetFiles...) |
| 159 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 160 | // SDK version flags |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 161 | minSdkVersion := sdkVersionOrDefault(ctx, sdkContext.minSdkVersion()) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 162 | |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 163 | linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion) |
| 164 | linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 165 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 166 | // Version code |
| 167 | if !hasVersionCode { |
| 168 | linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion()) |
| 169 | } |
| 170 | |
| 171 | if !hasVersionName { |
Colin Cross | 402d5e0 | 2018-04-25 14:54:06 -0700 | [diff] [blame] | 172 | var versionName string |
| 173 | if ctx.ModuleName() == "framework-res" { |
| 174 | // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the |
| 175 | // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things |
Colin Cross | bfd347d | 2018-05-09 11:11:35 -0700 | [diff] [blame] | 176 | // if it contains the build number. Use the PlatformVersionName instead. |
| 177 | versionName = ctx.Config().PlatformVersionName() |
Colin Cross | 402d5e0 | 2018-04-25 14:54:06 -0700 | [diff] [blame] | 178 | } else { |
| 179 | versionName = ctx.Config().AppsDefaultVersionName() |
| 180 | } |
Colin Cross | 0b9f31f | 2019-02-28 11:00:01 -0800 | [diff] [blame] | 181 | versionName = proptools.NinjaEscape(versionName) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 182 | linkFlags = append(linkFlags, "--version-name ", versionName) |
| 183 | } |
| 184 | |
Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame^] | 185 | linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"}) |
| 186 | |
| 187 | // Always set --pseudo-localize, it will be stripped out later for release |
| 188 | // builds that don't want it. |
| 189 | compileFlags = append(compileFlags, "--pseudo-localize") |
| 190 | |
| 191 | return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Paul Duffin | 250e619 | 2019-06-07 10:44:37 +0100 | [diff] [blame] | 194 | func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) { |
Colin Cross | 42308aa | 2018-11-14 21:44:17 -0800 | [diff] [blame] | 195 | if sdkDep.frameworkResModule != "" { |
| 196 | ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 200 | func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, extraLinkFlags ...string) { |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 201 | |
| 202 | transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, libDeps, libFlags, sdkLibraries := |
| 203 | aaptLibs(ctx, sdkContext) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 204 | |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 205 | // App manifest file |
| 206 | manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml") |
| 207 | manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile) |
| 208 | |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 209 | manifestPath := manifestFixer(ctx, manifestSrcPath, sdkContext, sdkLibraries, |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 210 | a.isLibrary, a.useEmbeddedNativeLibs, a.usesNonSdkApis, a.useEmbeddedDex, a.hasNoCode) |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 211 | |
| 212 | a.transitiveManifestPaths = append(android.Paths{manifestPath}, transitiveStaticLibManifests...) |
| 213 | |
| 214 | if len(transitiveStaticLibManifests) > 0 { |
Colin Cross | aa1c6f1 | 2019-04-19 16:25:38 -0700 | [diff] [blame] | 215 | a.mergedManifestFile = manifestMerger(ctx, manifestPath, transitiveStaticLibManifests, a.isLibrary) |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 216 | if !a.isLibrary { |
| 217 | // Only use the merged manifest for applications. For libraries, the transitive closure of manifests |
| 218 | // will be propagated to the final application and merged there. The merged manifest for libraries is |
| 219 | // only passed to Make, which can't handle transitive dependencies. |
| 220 | manifestPath = a.mergedManifestFile |
| 221 | } |
| 222 | } else { |
| 223 | a.mergedManifestFile = manifestPath |
| 224 | } |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 225 | |
Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame^] | 226 | compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, sdkContext, manifestPath) |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 227 | |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 228 | rroDirs = append(rroDirs, staticRRODirs...) |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 229 | linkFlags = append(linkFlags, libFlags...) |
| 230 | linkDeps = append(linkDeps, libDeps...) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 231 | linkFlags = append(linkFlags, extraLinkFlags...) |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 232 | if a.isLibrary { |
| 233 | linkFlags = append(linkFlags, "--static-lib") |
| 234 | } |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 235 | |
| 236 | packageRes := android.PathForModuleOut(ctx, "package-res.apk") |
| 237 | srcJar := android.PathForModuleGen(ctx, "R.jar") |
| 238 | proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options") |
| 239 | rTxt := android.PathForModuleOut(ctx, "R.txt") |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 240 | // This file isn't used by Soong, but is generated for exporting |
| 241 | extraPackages := android.PathForModuleOut(ctx, "extra_packages") |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 242 | |
Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 243 | var compiledResDirs []android.Paths |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 244 | for _, dir := range resDirs { |
Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame^] | 245 | compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths()) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 246 | } |
Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 247 | |
Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 248 | for i, zip := range resZips { |
| 249 | flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i)) |
Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame^] | 250 | aapt2CompileZip(ctx, flata, zip, "", compileFlags) |
Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 251 | compiledResDirs = append(compiledResDirs, android.Paths{flata}) |
| 252 | } |
| 253 | |
Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 254 | var compiledRes, compiledOverlay android.Paths |
| 255 | |
| 256 | compiledOverlay = append(compiledOverlay, transitiveStaticLibs...) |
| 257 | |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 258 | if len(transitiveStaticLibs) > 0 { |
Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 259 | // If we are using static android libraries, every source file becomes an overlay. |
| 260 | // This is to emulate old AAPT behavior which simulated library support. |
| 261 | for _, compiledResDir := range compiledResDirs { |
| 262 | compiledOverlay = append(compiledOverlay, compiledResDir...) |
| 263 | } |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 264 | } else if a.isLibrary { |
| 265 | // Otherwise, for a static library we treat all the resources equally with no overlay. |
| 266 | for _, compiledResDir := range compiledResDirs { |
| 267 | compiledRes = append(compiledRes, compiledResDir...) |
| 268 | } |
Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 269 | } else if len(compiledResDirs) > 0 { |
| 270 | // Without static libraries, the first directory is our directory, which can then be |
| 271 | // overlaid by the rest. |
| 272 | compiledRes = append(compiledRes, compiledResDirs[0]...) |
| 273 | for _, compiledResDir := range compiledResDirs[1:] { |
| 274 | compiledOverlay = append(compiledOverlay, compiledResDir...) |
| 275 | } |
| 276 | } |
| 277 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 278 | for _, dir := range overlayDirs { |
Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame^] | 279 | compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags).Paths()...) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 282 | var splitPackages android.WritablePaths |
| 283 | var splits []split |
| 284 | |
| 285 | for _, s := range a.splitNames { |
| 286 | suffix := strings.Replace(s, ",", "_", -1) |
| 287 | path := android.PathForModuleOut(ctx, "package_"+suffix+".apk") |
| 288 | linkFlags = append(linkFlags, "--split", path.String()+":"+s) |
| 289 | splitPackages = append(splitPackages, path) |
| 290 | splits = append(splits, split{ |
| 291 | name: s, |
| 292 | suffix: suffix, |
| 293 | path: path, |
| 294 | }) |
| 295 | } |
| 296 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 297 | aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages, |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 298 | linkFlags, linkDeps, compiledRes, compiledOverlay, splitPackages) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 299 | |
| 300 | a.aaptSrcJar = srcJar |
| 301 | a.exportPackage = packageRes |
| 302 | a.manifestPath = manifestPath |
| 303 | a.proguardOptionsFile = proguardOptionsFile |
| 304 | a.rroDirs = rroDirs |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 305 | a.extraAaptPackagesFile = extraPackages |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 306 | a.rTxt = rTxt |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 307 | a.splits = splits |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // aaptLibs collects libraries from dependencies and sdk_version and converts them into paths |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 311 | func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, transitiveStaticLibManifests android.Paths, |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 312 | staticRRODirs []rroDir, deps android.Paths, flags []string, sdkLibraries []string) { |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 313 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 314 | var sharedLibs android.Paths |
| 315 | |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 316 | sdkDep := decodeSdkDep(ctx, sdkContext) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 317 | if sdkDep.useFiles { |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 318 | sharedLibs = append(sharedLibs, sdkDep.jars...) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | ctx.VisitDirectDeps(func(module android.Module) { |
| 322 | var exportPackage android.Path |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 323 | aarDep, _ := module.(AndroidLibraryDependency) |
| 324 | if aarDep != nil { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 325 | exportPackage = aarDep.ExportPackage() |
| 326 | } |
| 327 | |
| 328 | switch ctx.OtherModuleDependencyTag(module) { |
Colin Cross | 4b964c0 | 2018-10-15 16:18:06 -0700 | [diff] [blame] | 329 | case instrumentationForTag: |
| 330 | // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2. |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 331 | case libTag: |
| 332 | if exportPackage != nil { |
| 333 | sharedLibs = append(sharedLibs, exportPackage) |
| 334 | } |
| 335 | |
| 336 | if _, ok := module.(SdkLibraryDependency); ok { |
| 337 | sdkLibraries = append(sdkLibraries, ctx.OtherModuleName(module)) |
| 338 | } |
| 339 | |
| 340 | case frameworkResTag: |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 341 | if exportPackage != nil { |
| 342 | sharedLibs = append(sharedLibs, exportPackage) |
| 343 | } |
| 344 | case staticLibTag: |
| 345 | if exportPackage != nil { |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 346 | transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...) |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 347 | transitiveStaticLibs = append(transitiveStaticLibs, exportPackage) |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 348 | transitiveStaticLibManifests = append(transitiveStaticLibManifests, aarDep.ExportedManifests()...) |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 349 | sdkLibraries = append(sdkLibraries, aarDep.ExportedSdkLibs()...) |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 350 | |
| 351 | outer: |
| 352 | for _, d := range aarDep.ExportedRRODirs() { |
| 353 | for _, e := range staticRRODirs { |
| 354 | if d.path == e.path { |
| 355 | continue outer |
| 356 | } |
| 357 | } |
| 358 | staticRRODirs = append(staticRRODirs, d) |
| 359 | } |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | }) |
| 363 | |
| 364 | deps = append(deps, sharedLibs...) |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 365 | deps = append(deps, transitiveStaticLibs...) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 366 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 367 | if len(transitiveStaticLibs) > 0 { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 368 | flags = append(flags, "--auto-add-overlay") |
| 369 | } |
| 370 | |
| 371 | for _, sharedLib := range sharedLibs { |
| 372 | flags = append(flags, "-I "+sharedLib.String()) |
| 373 | } |
| 374 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 375 | transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs) |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 376 | transitiveStaticLibManifests = android.FirstUniquePaths(transitiveStaticLibManifests) |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 377 | sdkLibraries = android.FirstUniqueStrings(sdkLibraries) |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 378 | |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 379 | return transitiveStaticLibs, transitiveStaticLibManifests, staticRRODirs, deps, flags, sdkLibraries |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | type AndroidLibrary struct { |
| 383 | Library |
| 384 | aapt |
| 385 | |
| 386 | androidLibraryProperties androidLibraryProperties |
| 387 | |
| 388 | aarFile android.WritablePath |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 389 | |
| 390 | exportedProguardFlagFiles android.Paths |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 391 | exportedStaticPackages android.Paths |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths { |
| 395 | return a.exportedProguardFlagFiles |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 398 | func (a *AndroidLibrary) ExportedStaticPackages() android.Paths { |
| 399 | return a.exportedStaticPackages |
| 400 | } |
| 401 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 402 | var _ AndroidLibraryDependency = (*AndroidLibrary)(nil) |
| 403 | |
| 404 | func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 405 | a.Module.deps(ctx) |
Paul Duffin | 250e619 | 2019-06-07 10:44:37 +0100 | [diff] [blame] | 406 | sdkDep := decodeSdkDep(ctx, sdkContext(a)) |
| 407 | if sdkDep.hasFrameworkLibs() { |
| 408 | a.aapt.deps(ctx, sdkDep) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 413 | a.aapt.isLibrary = true |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 414 | a.aapt.sdkLibraries = a.exportedSdkLibs |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 415 | a.aapt.buildActions(ctx, sdkContext(a)) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 416 | |
| 417 | ctx.CheckbuildFile(a.proguardOptionsFile) |
| 418 | ctx.CheckbuildFile(a.exportPackage) |
| 419 | ctx.CheckbuildFile(a.aaptSrcJar) |
| 420 | |
| 421 | // apps manifests are handled by aapt, don't let Module see them |
| 422 | a.properties.Manifest = nil |
| 423 | |
| 424 | a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, |
| 425 | a.proguardOptionsFile) |
| 426 | |
| 427 | a.Module.compile(ctx, a.aaptSrcJar) |
| 428 | |
Colin Cross | f57c578 | 2019-01-25 13:20:38 -0800 | [diff] [blame] | 429 | a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar") |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 430 | var res android.Paths |
| 431 | if a.androidLibraryProperties.BuildAAR { |
| 432 | BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res) |
| 433 | ctx.CheckbuildFile(a.aarFile) |
| 434 | } |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 435 | |
| 436 | ctx.VisitDirectDeps(func(m android.Module) { |
| 437 | if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag { |
| 438 | a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...) |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 439 | a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage()) |
| 440 | a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...) |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 441 | } |
| 442 | }) |
| 443 | |
| 444 | a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles) |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 445 | a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 448 | // android_library builds and links sources into a `.jar` file for the device along with Android resources. |
| 449 | // |
| 450 | // An android_library has a single variant that produces a `.jar` file containing `.class` files that were |
| 451 | // compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled |
| 452 | // with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of |
| 453 | // an android_app module. |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 454 | func AndroidLibraryFactory() android.Module { |
| 455 | module := &AndroidLibrary{} |
| 456 | |
| 457 | module.AddProperties( |
| 458 | &module.Module.properties, |
| 459 | &module.Module.deviceProperties, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 460 | &module.Module.dexpreoptProperties, |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 461 | &module.Module.protoProperties, |
| 462 | &module.aaptProperties, |
| 463 | &module.androidLibraryProperties) |
| 464 | |
| 465 | module.androidLibraryProperties.BuildAAR = true |
| 466 | |
Colin Cross | 48de9a4 | 2018-10-02 13:53:33 -0700 | [diff] [blame] | 467 | InitJavaModule(module, android.DeviceSupported) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 468 | return module |
| 469 | } |
| 470 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 471 | // |
| 472 | // AAR (android library) prebuilts |
| 473 | // |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 474 | |
| 475 | type AARImportProperties struct { |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 476 | Aars []string `android:"path"` |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 477 | |
Colin Cross | 479884c | 2018-07-10 13:39:30 -0700 | [diff] [blame] | 478 | Sdk_version *string |
| 479 | Min_sdk_version *string |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 480 | |
| 481 | Static_libs []string |
| 482 | Libs []string |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 483 | |
| 484 | // if set to true, run Jetifier against .aar file. Defaults to false. |
Colin Cross | 1001a79 | 2019-03-21 22:21:39 -0700 | [diff] [blame] | 485 | Jetifier *bool |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | type AARImport struct { |
| 489 | android.ModuleBase |
Colin Cross | 48de9a4 | 2018-10-02 13:53:33 -0700 | [diff] [blame] | 490 | android.DefaultableModuleBase |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 491 | prebuilt android.Prebuilt |
| 492 | |
| 493 | properties AARImportProperties |
| 494 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 495 | classpathFile android.WritablePath |
| 496 | proguardFlags android.WritablePath |
| 497 | exportPackage android.WritablePath |
| 498 | extraAaptPackagesFile android.WritablePath |
Colin Cross | 10f7c4a | 2018-05-23 10:59:28 -0700 | [diff] [blame] | 499 | manifest android.WritablePath |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 500 | |
| 501 | exportedStaticPackages android.Paths |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 504 | func (a *AARImport) sdkVersion() string { |
| 505 | return String(a.properties.Sdk_version) |
| 506 | } |
| 507 | |
| 508 | func (a *AARImport) minSdkVersion() string { |
Colin Cross | 479884c | 2018-07-10 13:39:30 -0700 | [diff] [blame] | 509 | if a.properties.Min_sdk_version != nil { |
| 510 | return *a.properties.Min_sdk_version |
| 511 | } |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 512 | return a.sdkVersion() |
| 513 | } |
| 514 | |
Dan Willemsen | 419290a | 2018-10-31 15:28:47 -0700 | [diff] [blame] | 515 | func (a *AARImport) targetSdkVersion() string { |
| 516 | return a.sdkVersion() |
| 517 | } |
| 518 | |
Paul Duffin | 250e619 | 2019-06-07 10:44:37 +0100 | [diff] [blame] | 519 | func (a *AARImport) noFrameworkLibs() bool { |
| 520 | return false |
| 521 | } |
| 522 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 523 | var _ AndroidLibraryDependency = (*AARImport)(nil) |
| 524 | |
| 525 | func (a *AARImport) ExportPackage() android.Path { |
| 526 | return a.exportPackage |
| 527 | } |
| 528 | |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 529 | func (a *AARImport) ExportedProguardFlagFiles() android.Paths { |
| 530 | return android.Paths{a.proguardFlags} |
| 531 | } |
| 532 | |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 533 | func (a *AARImport) ExportedRRODirs() []rroDir { |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 534 | return nil |
| 535 | } |
| 536 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 537 | func (a *AARImport) ExportedStaticPackages() android.Paths { |
| 538 | return a.exportedStaticPackages |
| 539 | } |
| 540 | |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 541 | func (a *AARImport) ExportedManifests() android.Paths { |
| 542 | return android.Paths{a.manifest} |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 545 | func (a *AARImport) Prebuilt() *android.Prebuilt { |
| 546 | return &a.prebuilt |
| 547 | } |
| 548 | |
| 549 | func (a *AARImport) Name() string { |
| 550 | return a.prebuilt.Name(a.ModuleBase.Name()) |
| 551 | } |
| 552 | |
| 553 | func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 554 | if !ctx.Config().UnbundledBuildUsePrebuiltSdks() { |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 555 | sdkDep := decodeSdkDep(ctx, sdkContext(a)) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 556 | if sdkDep.useModule && sdkDep.frameworkResModule != "" { |
Colin Cross | 42d48b7 | 2018-08-29 14:10:52 -0700 | [diff] [blame] | 557 | ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 558 | } |
| 559 | } |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 560 | |
Colin Cross | 42d48b7 | 2018-08-29 14:10:52 -0700 | [diff] [blame] | 561 | ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...) |
| 562 | ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | // Unzip an AAR into its constituent files and directories. Any files in Outputs that don't exist in the AAR will be |
Dan Willemsen | 304cfec | 2019-05-28 14:49:06 -0700 | [diff] [blame] | 566 | // touched to create an empty file. The res directory is not extracted, as it will be extracted in its own rule. |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 567 | var unzipAAR = pctx.AndroidStaticRule("unzipAAR", |
| 568 | blueprint.RuleParams{ |
Dan Willemsen | 304cfec | 2019-05-28 14:49:06 -0700 | [diff] [blame] | 569 | Command: `rm -rf $outDir && mkdir -p $outDir && ` + |
| 570 | `unzip -qo -d $outDir $in && rm -rf $outDir/res && touch $out`, |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 571 | }, |
Dan Willemsen | 304cfec | 2019-05-28 14:49:06 -0700 | [diff] [blame] | 572 | "outDir") |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 573 | |
| 574 | func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 575 | if len(a.properties.Aars) != 1 { |
| 576 | ctx.PropertyErrorf("aars", "exactly one aar is required") |
| 577 | return |
| 578 | } |
| 579 | |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 580 | aarName := ctx.ModuleName() + ".aar" |
| 581 | var aar android.Path |
| 582 | aar = android.PathForModuleSrc(ctx, a.properties.Aars[0]) |
Colin Cross | 1001a79 | 2019-03-21 22:21:39 -0700 | [diff] [blame] | 583 | if Bool(a.properties.Jetifier) { |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 584 | inputFile := aar |
| 585 | aar = android.PathForModuleOut(ctx, "jetifier", aarName) |
| 586 | TransformJetifier(ctx, aar.(android.WritablePath), inputFile) |
| 587 | } |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 588 | |
| 589 | extractedAARDir := android.PathForModuleOut(ctx, "aar") |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 590 | a.classpathFile = extractedAARDir.Join(ctx, "classes.jar") |
| 591 | a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt") |
Colin Cross | 10f7c4a | 2018-05-23 10:59:28 -0700 | [diff] [blame] | 592 | a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml") |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 593 | |
| 594 | ctx.Build(pctx, android.BuildParams{ |
| 595 | Rule: unzipAAR, |
| 596 | Input: aar, |
Colin Cross | 10f7c4a | 2018-05-23 10:59:28 -0700 | [diff] [blame] | 597 | Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest}, |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 598 | Description: "unzip AAR", |
| 599 | Args: map[string]string{ |
Dan Willemsen | 304cfec | 2019-05-28 14:49:06 -0700 | [diff] [blame] | 600 | "outDir": extractedAARDir.String(), |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 601 | }, |
| 602 | }) |
| 603 | |
Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame^] | 604 | // Always set --pseudo-localize, it will be stripped out later for release |
| 605 | // builds that don't want it. |
| 606 | compileFlags := []string{"--pseudo-localize"} |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 607 | compiledResDir := android.PathForModuleOut(ctx, "flat-res") |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 608 | flata := compiledResDir.Join(ctx, "gen_res.flata") |
Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame^] | 609 | aapt2CompileZip(ctx, flata, aar, "res", compileFlags) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 610 | |
| 611 | a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk") |
| 612 | srcJar := android.PathForModuleGen(ctx, "R.jar") |
| 613 | proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options") |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 614 | rTxt := android.PathForModuleOut(ctx, "R.txt") |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 615 | a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages") |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 616 | |
| 617 | var linkDeps android.Paths |
| 618 | |
| 619 | linkFlags := []string{ |
| 620 | "--static-lib", |
| 621 | "--no-static-lib-packages", |
| 622 | "--auto-add-overlay", |
| 623 | } |
| 624 | |
Colin Cross | 10f7c4a | 2018-05-23 10:59:28 -0700 | [diff] [blame] | 625 | linkFlags = append(linkFlags, "--manifest "+a.manifest.String()) |
| 626 | linkDeps = append(linkDeps, a.manifest) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 627 | |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 628 | transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags, sdkLibraries := |
| 629 | aaptLibs(ctx, sdkContext(a)) |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 630 | |
| 631 | _ = staticLibManifests |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 632 | _ = staticRRODirs |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 633 | _ = sdkLibraries |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 634 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 635 | linkDeps = append(linkDeps, libDeps...) |
| 636 | linkFlags = append(linkFlags, libFlags...) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 637 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 638 | overlayRes := append(android.Paths{flata}, transitiveStaticLibs...) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 639 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 640 | aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile, |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 641 | linkFlags, linkDeps, nil, overlayRes, nil) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | var _ Dependency = (*AARImport)(nil) |
| 645 | |
| 646 | func (a *AARImport) HeaderJars() android.Paths { |
| 647 | return android.Paths{a.classpathFile} |
| 648 | } |
| 649 | |
| 650 | func (a *AARImport) ImplementationJars() android.Paths { |
| 651 | return android.Paths{a.classpathFile} |
| 652 | } |
| 653 | |
Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 654 | func (a *AARImport) ResourceJars() android.Paths { |
| 655 | return nil |
| 656 | } |
| 657 | |
| 658 | func (a *AARImport) ImplementationAndResourcesJars() android.Paths { |
| 659 | return android.Paths{a.classpathFile} |
| 660 | } |
| 661 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 662 | func (a *AARImport) DexJar() android.Path { |
| 663 | return nil |
| 664 | } |
| 665 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 666 | func (a *AARImport) AidlIncludeDirs() android.Paths { |
| 667 | return nil |
| 668 | } |
| 669 | |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 670 | func (a *AARImport) ExportedSdkLibs() []string { |
| 671 | return nil |
| 672 | } |
| 673 | |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 674 | func (a *AARImport) SrcJarArgs() ([]string, android.Paths) { |
| 675 | return nil, nil |
| 676 | } |
| 677 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 678 | var _ android.PrebuiltInterface = (*Import)(nil) |
| 679 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 680 | // android_library_import imports an `.aar` file into the build graph as if it was built with android_library. |
| 681 | // |
| 682 | // This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of |
| 683 | // an android_app module. |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 684 | func AARImportFactory() android.Module { |
| 685 | module := &AARImport{} |
| 686 | |
| 687 | module.AddProperties(&module.properties) |
| 688 | |
| 689 | android.InitPrebuiltModule(module, &module.properties.Aars) |
Colin Cross | 48de9a4 | 2018-10-02 13:53:33 -0700 | [diff] [blame] | 690 | InitJavaModule(module, android.DeviceSupported) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 691 | return module |
| 692 | } |