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