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