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