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