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