| 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 ( | 
| Cole Faust | 6265120c | 2024-09-05 16:27:56 -0700 | [diff] [blame] | 18 | "crypto/sha256" | 
| Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 19 | "fmt" | 
| Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 20 | "path/filepath" | 
| Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame] | 21 | "slices" | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 22 | "strings" | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 23 |  | 
| Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 24 | "android/soong/android" | 
| Ulya Trafimovich | 31e444e | 2020-08-14 17:32:16 +0100 | [diff] [blame] | 25 | "android/soong/dexpreopt" | 
| Jihoon Kang | fe914ed | 2024-02-12 22:49:21 +0000 | [diff] [blame] | 26 |  | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 27 | "github.com/google/blueprint" | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 28 | "github.com/google/blueprint/depset" | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 29 | "github.com/google/blueprint/proptools" | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 30 | ) | 
|  | 31 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 32 | type AndroidLibraryDependency interface { | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 33 | ExportPackage() android.Path | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 34 | ResourcesNodeDepSet() depset.DepSet[*resourcesNode] | 
|  | 35 | RRODirsDepSet() depset.DepSet[rroDir] | 
|  | 36 | ManifestsDepSet() depset.DepSet[android.Path] | 
| 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) { | 
| Colin Cross | c444162 | 2024-09-18 14:55:49 -0700 | [diff] [blame] | 49 | ctx.Transition("propagate_rro_enforcement", &propagateRROEnforcementTransitionMutator{}) | 
| Paul Duffin | 04ba70d | 2021-03-22 13:56:43 +0000 | [diff] [blame] | 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 |  | 
| Jiakai Zhang | ba82e28 | 2023-10-13 18:08:59 +0100 | [diff] [blame] | 69 | // list of files to use as assets. | 
|  | 70 | Assets []string `android:"path"` | 
|  | 71 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 72 | // list of directories relative to the Blueprints file containing assets. | 
| Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 73 | // Defaults to ["assets"] if a directory called assets exists.  Set to [] | 
|  | 74 | // to disable the default. | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 75 | Asset_dirs []string | 
|  | 76 |  | 
|  | 77 | // list of directories relative to the Blueprints file containing | 
| Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 78 | // Android resources.  Defaults to ["res"] if a directory called res exists. | 
|  | 79 | // Set to [] to disable the default. | 
| Cole Faust | d65add2 | 2024-07-31 16:42:33 -0700 | [diff] [blame] | 80 | Resource_dirs []string `android:"path"` | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 81 |  | 
| Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 82 | // list of zip files containing Android resources. | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 83 | Resource_zips []string `android:"path"` | 
| Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 84 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 85 | // path to AndroidManifest.xml.  If unset, defaults to "AndroidManifest.xml". | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 86 | Manifest *string `android:"path"` | 
| changho.shin | b5432b7 | 2019-08-08 18:37:17 +0900 | [diff] [blame] | 87 |  | 
|  | 88 | // paths to additional manifest files to merge with main manifest. | 
|  | 89 | Additional_manifests []string `android:"path"` | 
| Sasha Smundak | 541056c | 2019-10-28 15:50:06 -0700 | [diff] [blame] | 90 |  | 
|  | 91 | // do not include AndroidManifest from dependent libraries | 
|  | 92 | Dont_merge_manifests *bool | 
| Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 93 |  | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 94 | // If use_resource_processor is set, use Bazel's resource processor instead of aapt2 to generate R.class files. | 
|  | 95 | // The resource processor produces more optimal R.class files that only list resources in the package of the | 
|  | 96 | // library that provided them, as opposed to aapt2 which produces R.java files for every package containing | 
|  | 97 | // every resource.  Using the resource processor can provide significant build time speedups, but requires | 
|  | 98 | // fixing the module to use the correct package to reference each resource, and to avoid having any other | 
|  | 99 | // libraries in the tree that use the same package name.  Defaults to false, but will default to true in the | 
|  | 100 | // future. | 
|  | 101 | Use_resource_processor *bool | 
|  | 102 |  | 
| Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 103 | // true if RRO is enforced for any of the dependent modules | 
|  | 104 | RROEnforcedForDependent bool `blueprint:"mutated"` | 
| Inseob Kim | 34dc4cd | 2023-11-07 13:37:14 +0900 | [diff] [blame] | 105 |  | 
|  | 106 | // Filter only specified product and ignore other products | 
|  | 107 | Filter_product *string `blueprint:"mutated"` | 
| Jihoon Kang | 9049c27 | 2024-03-19 21:57:36 +0000 | [diff] [blame] | 108 |  | 
|  | 109 | // Names of aconfig_declarations modules that specify aconfig flags that the module depends on. | 
|  | 110 | Flags_packages []string | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 111 | } | 
|  | 112 |  | 
|  | 113 | type aapt struct { | 
| Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame] | 114 | aaptSrcJar                         android.Path | 
|  | 115 | transitiveAaptRJars                android.Paths | 
|  | 116 | transitiveAaptResourcePackagesFile android.Path | 
|  | 117 | exportPackage                      android.Path | 
|  | 118 | manifestPath                       android.Path | 
|  | 119 | proguardOptionsFile                android.Path | 
|  | 120 | rTxt                               android.Path | 
|  | 121 | rJar                               android.Path | 
|  | 122 | extraAaptPackagesFile              android.Path | 
|  | 123 | mergedManifestFile                 android.Path | 
|  | 124 | noticeFile                         android.OptionalPath | 
|  | 125 | assetPackage                       android.OptionalPath | 
|  | 126 | isLibrary                          bool | 
|  | 127 | defaultManifestVersion             string | 
|  | 128 | useEmbeddedNativeLibs              bool | 
|  | 129 | useEmbeddedDex                     bool | 
|  | 130 | usesNonSdkApis                     bool | 
|  | 131 | hasNoCode                          bool | 
|  | 132 | LoggingParent                      string | 
|  | 133 | resourceFiles                      android.Paths | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 134 |  | 
| Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 135 | splitNames []string | 
|  | 136 | splits     []split | 
|  | 137 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 138 | aaptProperties aaptProperties | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 139 |  | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 140 | resourcesNodesDepSet depset.DepSet[*resourcesNode] | 
|  | 141 | rroDirsDepSet        depset.DepSet[rroDir] | 
|  | 142 | manifestsDepSet      depset.DepSet[android.Path] | 
| Alix | 96ea8845 | 2023-08-31 15:48:23 +0000 | [diff] [blame] | 143 |  | 
|  | 144 | manifestValues struct { | 
|  | 145 | applicationId string | 
|  | 146 | } | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 147 | } | 
|  | 148 |  | 
| Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 149 | type split struct { | 
|  | 150 | name   string | 
|  | 151 | suffix string | 
|  | 152 | path   android.Path | 
|  | 153 | } | 
|  | 154 |  | 
| Colin Cross | c444162 | 2024-09-18 14:55:49 -0700 | [diff] [blame] | 155 | // Propagate RRO enforcement flag to static lib dependencies transitively.  If EnforceRROGlobally is set then | 
|  | 156 | // all modules will use the "" variant.  If specific modules have RRO enforced, then modules (usually apps) with | 
|  | 157 | // RRO enabled will use the "" variation for themselves, but use the "rro" variant of direct and transitive static | 
|  | 158 | // android_library dependencies. | 
|  | 159 | type propagateRROEnforcementTransitionMutator struct{} | 
|  | 160 |  | 
|  | 161 | func (p propagateRROEnforcementTransitionMutator) Split(ctx android.BaseModuleContext) []string { | 
|  | 162 | // Never split modules, apps with or without RRO enabled use the "" variant, static android_library dependencies | 
|  | 163 | // will use create the "rro" variant from incoming tranisitons. | 
|  | 164 | return []string{""} | 
|  | 165 | } | 
|  | 166 |  | 
|  | 167 | func (p propagateRROEnforcementTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string { | 
|  | 168 | // Non-static dependencies are not involved in RRO and always use the empty variant. | 
|  | 169 | if ctx.DepTag() != staticLibTag { | 
|  | 170 | return "" | 
|  | 171 | } | 
|  | 172 |  | 
| Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 173 | m := ctx.Module() | 
| Colin Cross | c444162 | 2024-09-18 14:55:49 -0700 | [diff] [blame] | 174 | if _, ok := m.(AndroidLibraryDependency); ok { | 
|  | 175 | // If RRO is enforced globally don't bother using "rro" variants, the empty variant will have RRO enabled. | 
|  | 176 | if ctx.Config().EnforceRROGlobally() { | 
|  | 177 | return "" | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | // If RRO is enabled for this module use the "rro" variants of static dependencies.  IncomingTransition will | 
|  | 181 | // rewrite this back to "" if the dependency is not an android_library. | 
|  | 182 | if ctx.Config().EnforceRROForModule(ctx.Module().Name()) { | 
|  | 183 | return "rro" | 
|  | 184 | } | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | return sourceVariation | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | func (p propagateRROEnforcementTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string { | 
|  | 191 | // Propagate the "rro" variant to android_library modules, but use the empty variant for everything else. | 
|  | 192 | if incomingVariation == "rro" { | 
|  | 193 | m := ctx.Module() | 
|  | 194 | if _, ok := m.(AndroidLibraryDependency); ok { | 
|  | 195 | return "rro" | 
|  | 196 | } | 
|  | 197 | return "" | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 | return "" | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | func (p propagateRROEnforcementTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) { | 
|  | 204 | m := ctx.Module() | 
|  | 205 | if d, ok := m.(AndroidLibraryDependency); ok { | 
|  | 206 | if variation == "rro" { | 
|  | 207 | // This is the "rro" variant of a module that has both variants, mark this one as RRO enabled and | 
|  | 208 | // hide it from make to avoid collisions with the non-RRO empty variant. | 
|  | 209 | d.SetRROEnforcedForDependent(true) | 
|  | 210 | m.HideFromMake() | 
|  | 211 | } else if ctx.Config().EnforceRROGlobally() { | 
|  | 212 | // RRO is enabled globally, mark it enabled for this module, but there is only one variant so no | 
|  | 213 | // need to hide it from make. | 
|  | 214 | d.SetRROEnforcedForDependent(true) | 
|  | 215 | } | 
| Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 216 | } | 
|  | 217 | } | 
|  | 218 |  | 
| Colin Cross | 8f1b033 | 2024-01-25 13:39:06 -0800 | [diff] [blame] | 219 | func (a *aapt) useResourceProcessorBusyBox(ctx android.BaseModuleContext) bool { | 
| Colin Cross | eee4ab1 | 2024-03-27 11:54:10 -0700 | [diff] [blame] | 220 | return BoolDefault(a.aaptProperties.Use_resource_processor, ctx.Config().UseResourceProcessorByDefault()) && | 
|  | 221 | // TODO(b/331641946): remove this when ResourceProcessorBusyBox supports generating shared libraries. | 
| Spandan Das | a62d956 | 2024-08-06 00:13:31 +0000 | [diff] [blame] | 222 | !slices.Contains(a.aaptProperties.Aaptflags, "--shared-lib") && | 
|  | 223 | // Use the legacy resource processor in kythe builds. | 
|  | 224 | // The legacy resource processor creates an R.srcjar, which kythe can use for generating crossrefs. | 
|  | 225 | // TODO(b/354854007): Re-enable BusyBox in kythe builds | 
|  | 226 | !ctx.Config().EmitXrefRules() | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 227 | } | 
|  | 228 |  | 
| Inseob Kim | 34dc4cd | 2023-11-07 13:37:14 +0900 | [diff] [blame] | 229 | func (a *aapt) filterProduct() string { | 
|  | 230 | return String(a.aaptProperties.Filter_product) | 
|  | 231 | } | 
|  | 232 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 233 | func (a *aapt) ExportPackage() android.Path { | 
|  | 234 | return a.exportPackage | 
|  | 235 | } | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 236 | func (a *aapt) ResourcesNodeDepSet() depset.DepSet[*resourcesNode] { | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 237 | return a.resourcesNodesDepSet | 
| Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 238 | } | 
|  | 239 |  | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 240 | func (a *aapt) RRODirsDepSet() depset.DepSet[rroDir] { | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 241 | return a.rroDirsDepSet | 
| Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 242 | } | 
|  | 243 |  | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 244 | func (a *aapt) ManifestsDepSet() depset.DepSet[android.Path] { | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 245 | return a.manifestsDepSet | 
| Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 246 | } | 
|  | 247 |  | 
| Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 248 | func (a *aapt) SetRROEnforcedForDependent(enforce bool) { | 
|  | 249 | a.aaptProperties.RROEnforcedForDependent = enforce | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | func (a *aapt) IsRROEnforced(ctx android.BaseModuleContext) bool { | 
|  | 253 | // True if RRO is enforced for this module or... | 
|  | 254 | return ctx.Config().EnforceRROForModule(ctx.ModuleName()) || | 
| Jeongik Cha | cee5ba9 | 2021-02-19 12:11:51 +0900 | [diff] [blame] | 255 | // if RRO is enforced for any of its dependents. | 
|  | 256 | a.aaptProperties.RROEnforcedForDependent | 
| Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 257 | } | 
|  | 258 |  | 
| Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 259 | func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext android.SdkContext, | 
| Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame] | 260 | manifestPath android.Path) (compileFlags, linkFlags []string, linkDeps android.Paths, | 
|  | 261 | resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) { | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 262 |  | 
| Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 263 | hasVersionCode := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-code") | 
|  | 264 | hasVersionName := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-name") | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 265 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 266 | // Flags specified in Android.bp | 
|  | 267 | linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...) | 
|  | 268 |  | 
| Eric Miao | 40eab20 | 2023-03-30 16:57:17 +0000 | [diff] [blame] | 269 | linkFlags = append(linkFlags, "--enable-compact-entries") | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 270 |  | 
|  | 271 | // Find implicit or explicit asset and resource dirs | 
| Jiakai Zhang | ba82e28 | 2023-10-13 18:08:59 +0100 | [diff] [blame] | 272 | assets := android.PathsRelativeToModuleSourceDir(android.SourceInput{ | 
|  | 273 | Context:     ctx, | 
|  | 274 | Paths:       a.aaptProperties.Assets, | 
|  | 275 | IncludeDirs: false, | 
|  | 276 | }) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 277 | assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets") | 
|  | 278 | resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res") | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 279 | resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 280 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 281 | // Glob directories into lists of paths | 
|  | 282 | for _, dir := range resourceDirs { | 
|  | 283 | resDirs = append(resDirs, globbedResourceDir{ | 
|  | 284 | dir:   dir, | 
|  | 285 | files: androidResourceGlob(ctx, dir), | 
|  | 286 | }) | 
| Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 287 | resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, a, dir) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 288 | overlayDirs = append(overlayDirs, resOverlayDirs...) | 
|  | 289 | rroDirs = append(rroDirs, resRRODirs...) | 
|  | 290 | } | 
|  | 291 |  | 
| Cole Faust | 6265120c | 2024-09-05 16:27:56 -0700 | [diff] [blame] | 292 | assetDirsHasher := sha256.New() | 
| Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame] | 293 | var assetDeps android.Paths | 
| Cole Faust | 6265120c | 2024-09-05 16:27:56 -0700 | [diff] [blame] | 294 | for _, dir := range assetDirs { | 
| Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame] | 295 | // Add a dependency on every file in the asset directory.  This ensures the aapt2 | 
|  | 296 | // rule will be rerun if one of the files in the asset directory is modified. | 
| Cole Faust | 6265120c | 2024-09-05 16:27:56 -0700 | [diff] [blame] | 297 | dirContents := androidResourceGlob(ctx, dir) | 
|  | 298 | assetDeps = append(assetDeps, dirContents...) | 
| Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame] | 299 |  | 
| Cole Faust | 6265120c | 2024-09-05 16:27:56 -0700 | [diff] [blame] | 300 | // Add a hash of all the files in the asset directory to the command line. | 
| Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame] | 301 | // This ensures the aapt2 rule will be run if a file is removed from the asset directory, | 
|  | 302 | // or a file is added whose timestamp is older than the output of aapt2. | 
| Cole Faust | 6265120c | 2024-09-05 16:27:56 -0700 | [diff] [blame] | 303 | for _, path := range dirContents.Strings() { | 
|  | 304 | assetDirsHasher.Write([]byte(path)) | 
|  | 305 | } | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 306 | } | 
|  | 307 |  | 
| Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 308 | assetDirStrings := assetDirs.Strings() | 
|  | 309 | if a.noticeFile.Valid() { | 
|  | 310 | assetDirStrings = append(assetDirStrings, filepath.Dir(a.noticeFile.Path().String())) | 
| Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame] | 311 | assetDeps = append(assetDeps, a.noticeFile.Path()) | 
| Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 312 | } | 
| Jiakai Zhang | ba82e28 | 2023-10-13 18:08:59 +0100 | [diff] [blame] | 313 | if len(assets) > 0 { | 
|  | 314 | // aapt2 doesn't support adding individual asset files. Create a temp directory to hold asset | 
|  | 315 | // files and pass it to aapt2. | 
|  | 316 | tmpAssetDir := android.PathForModuleOut(ctx, "tmp_asset_dir") | 
|  | 317 |  | 
|  | 318 | rule := android.NewRuleBuilder(pctx, ctx) | 
|  | 319 | rule.Command(). | 
|  | 320 | Text("rm -rf").Text(tmpAssetDir.String()). | 
|  | 321 | Text("&&"). | 
|  | 322 | Text("mkdir -p").Text(tmpAssetDir.String()) | 
|  | 323 |  | 
|  | 324 | for _, asset := range assets { | 
|  | 325 | output := tmpAssetDir.Join(ctx, asset.Rel()) | 
|  | 326 | assetDeps = append(assetDeps, output) | 
|  | 327 | rule.Command().Text("mkdir -p").Text(filepath.Dir(output.String())) | 
|  | 328 | rule.Command().Text("cp").Input(asset).Output(output) | 
|  | 329 | } | 
|  | 330 |  | 
|  | 331 | rule.Build("tmp_asset_dir", "tmp_asset_dir") | 
|  | 332 |  | 
|  | 333 | assetDirStrings = append(assetDirStrings, tmpAssetDir.String()) | 
|  | 334 | } | 
| Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 335 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 336 | linkFlags = append(linkFlags, "--manifest "+manifestPath.String()) | 
|  | 337 | linkDeps = append(linkDeps, manifestPath) | 
|  | 338 |  | 
| Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 339 | linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirStrings, "-A ")) | 
| Cole Faust | 6265120c | 2024-09-05 16:27:56 -0700 | [diff] [blame] | 340 | linkFlags = append(linkFlags, fmt.Sprintf("$$(: %x)", assetDirsHasher.Sum(nil))) | 
| Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame] | 341 | linkDeps = append(linkDeps, assetDeps...) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 342 |  | 
| Spandan Das | 50885c0 | 2023-02-23 21:31:33 +0000 | [diff] [blame] | 343 | // Returns the effective version for {min|target}_sdk_version | 
| Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 344 | effectiveVersionString := func(sdkVersion android.SdkSpec, minSdkVersion android.ApiLevel) string { | 
| Spandan Das | 50885c0 | 2023-02-23 21:31:33 +0000 | [diff] [blame] | 345 | // If {min|target}_sdk_version is current, use sdk_version to determine the effective level | 
|  | 346 | // This is necessary for vendor modules. | 
|  | 347 | // The effective version does not _only_ depend on {min|target}_sdk_version(level), | 
|  | 348 | // but also on the sdk_version (kind+level) | 
| Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 349 | if minSdkVersion.IsCurrent() { | 
| Spandan Das | 50885c0 | 2023-02-23 21:31:33 +0000 | [diff] [blame] | 350 | ret, err := sdkVersion.EffectiveVersionString(ctx) | 
|  | 351 | if err != nil { | 
|  | 352 | ctx.ModuleErrorf("invalid sdk_version: %s", err) | 
|  | 353 | } | 
|  | 354 | return ret | 
|  | 355 | } | 
|  | 356 | ret, err := minSdkVersion.EffectiveVersionString(ctx) | 
|  | 357 | if err != nil { | 
|  | 358 | ctx.ModuleErrorf("invalid min_sdk_version: %s", err) | 
|  | 359 | } | 
|  | 360 | return ret | 
| Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 361 | } | 
| Spandan Das | 50885c0 | 2023-02-23 21:31:33 +0000 | [diff] [blame] | 362 | // SDK version flags | 
|  | 363 | sdkVersion := sdkContext.SdkVersion(ctx) | 
|  | 364 | minSdkVersion := effectiveVersionString(sdkVersion, sdkContext.MinSdkVersion(ctx)) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 365 |  | 
| Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 366 | linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion) | 
| Spandan Das | 6450b55 | 2023-02-23 19:27:07 +0000 | [diff] [blame] | 367 | // Use minSdkVersion for target-sdk-version, even if `target_sdk_version` is set | 
|  | 368 | // This behavior has been copied from Make. | 
| Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 369 | linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 370 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 371 | // Version code | 
|  | 372 | if !hasVersionCode { | 
| Dan Albert | 4f378d7 | 2020-07-23 17:32:15 -0700 | [diff] [blame] | 373 | linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion().String()) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 374 | } | 
|  | 375 |  | 
|  | 376 | if !hasVersionName { | 
| Colin Cross | 402d5e0 | 2018-04-25 14:54:06 -0700 | [diff] [blame] | 377 | var versionName string | 
|  | 378 | if ctx.ModuleName() == "framework-res" { | 
|  | 379 | // Some builds set AppsDefaultVersionName() to include the build number ("O-123456").  aapt2 copies the | 
|  | 380 | // 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] | 381 | // if it contains the build number.  Use the PlatformVersionName instead. | 
|  | 382 | versionName = ctx.Config().PlatformVersionName() | 
| Colin Cross | 402d5e0 | 2018-04-25 14:54:06 -0700 | [diff] [blame] | 383 | } else { | 
|  | 384 | versionName = ctx.Config().AppsDefaultVersionName() | 
|  | 385 | } | 
| Colin Cross | 0b9f31f | 2019-02-28 11:00:01 -0800 | [diff] [blame] | 386 | versionName = proptools.NinjaEscape(versionName) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 387 | linkFlags = append(linkFlags, "--version-name ", versionName) | 
|  | 388 | } | 
|  | 389 |  | 
| Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame] | 390 | linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"}) | 
|  | 391 |  | 
|  | 392 | // Always set --pseudo-localize, it will be stripped out later for release | 
|  | 393 | // builds that don't want it. | 
|  | 394 | compileFlags = append(compileFlags, "--pseudo-localize") | 
|  | 395 |  | 
|  | 396 | return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 397 | } | 
|  | 398 |  | 
| Paul Duffin | 250e619 | 2019-06-07 10:44:37 +0100 | [diff] [blame] | 399 | func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) { | 
| Colin Cross | 42308aa | 2018-11-14 21:44:17 -0800 | [diff] [blame] | 400 | if sdkDep.frameworkResModule != "" { | 
|  | 401 | ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 402 | } | 
|  | 403 | } | 
|  | 404 |  | 
| Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 405 | var extractAssetsRule = pctx.AndroidStaticRule("extractAssets", | 
|  | 406 | blueprint.RuleParams{ | 
|  | 407 | Command:     `${config.Zip2ZipCmd} -i ${in} -o ${out} "assets/**/*"`, | 
|  | 408 | CommandDeps: []string{"${config.Zip2ZipCmd}"}, | 
|  | 409 | }) | 
|  | 410 |  | 
| Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 411 | type aaptBuildActionOptions struct { | 
|  | 412 | sdkContext                     android.SdkContext | 
|  | 413 | classLoaderContexts            dexpreopt.ClassLoaderContextMap | 
|  | 414 | excludedLibs                   []string | 
|  | 415 | enforceDefaultTargetSdkVersion bool | 
| Rico Wind | a2fa263 | 2024-03-13 13:09:17 +0100 | [diff] [blame] | 416 | forceNonFinalResourceIDs       bool | 
| Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 417 | extraLinkFlags                 []string | 
| Jihoon Kang | 84b2589 | 2023-12-01 22:01:06 +0000 | [diff] [blame] | 418 | aconfigTextFiles               android.Paths | 
| Jiakai Zhang | 3693708 | 2024-04-15 11:15:50 +0000 | [diff] [blame] | 419 | usesLibrary                    *usesLibrary | 
| Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 420 | } | 
|  | 421 |  | 
|  | 422 | func (a *aapt) buildActions(ctx android.ModuleContext, opts aaptBuildActionOptions) { | 
| Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 423 |  | 
| Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 424 | staticResourcesNodesDepSet, sharedResourcesNodesDepSet, staticRRODirsDepSet, staticManifestsDepSet, sharedExportPackages, libFlags := | 
| Jiakai Zhang | 3693708 | 2024-04-15 11:15:50 +0000 | [diff] [blame] | 425 | aaptLibs(ctx, opts.sdkContext, opts.classLoaderContexts, opts.usesLibrary) | 
| Ulya Trafimovich | 31e444e | 2020-08-14 17:32:16 +0100 | [diff] [blame] | 426 |  | 
| Paul Duffin | 0653057 | 2022-02-03 17:54:15 +0000 | [diff] [blame] | 427 | // Exclude any libraries from the supplied list. | 
| Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 428 | opts.classLoaderContexts = opts.classLoaderContexts.ExcludeLibs(opts.excludedLibs) | 
| Paul Duffin | 0653057 | 2022-02-03 17:54:15 +0000 | [diff] [blame] | 429 |  | 
| Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 430 | // App manifest file | 
|  | 431 | manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml") | 
|  | 432 | manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile) | 
|  | 433 |  | 
| Gurpreet Singh | 7deabfa | 2022-02-10 13:28:35 +0000 | [diff] [blame] | 434 | manifestPath := ManifestFixer(ctx, manifestSrcPath, ManifestFixerParams{ | 
| Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 435 | SdkContext:                     opts.sdkContext, | 
|  | 436 | ClassLoaderContexts:            opts.classLoaderContexts, | 
| Harshit Mahajan | 5b8b730 | 2022-06-10 11:24:05 +0000 | [diff] [blame] | 437 | IsLibrary:                      a.isLibrary, | 
|  | 438 | DefaultManifestVersion:         a.defaultManifestVersion, | 
|  | 439 | UseEmbeddedNativeLibs:          a.useEmbeddedNativeLibs, | 
|  | 440 | UsesNonSdkApis:                 a.usesNonSdkApis, | 
|  | 441 | UseEmbeddedDex:                 a.useEmbeddedDex, | 
|  | 442 | HasNoCode:                      a.hasNoCode, | 
|  | 443 | LoggingParent:                  a.LoggingParent, | 
| Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 444 | EnforceDefaultTargetSdkVersion: opts.enforceDefaultTargetSdkVersion, | 
| Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 445 | }) | 
| Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 446 |  | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 447 | staticDeps := transitiveAarDeps(staticResourcesNodesDepSet.ToList()) | 
| Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 448 | sharedDeps := transitiveAarDeps(sharedResourcesNodesDepSet.ToList()) | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 449 |  | 
| Luca Stefani | fd89882 | 2019-09-10 22:13:31 +0200 | [diff] [blame] | 450 | // Add additional manifest files to transitive manifests. | 
|  | 451 | additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests) | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 452 | transitiveManifestPaths := append(android.Paths{manifestPath}, additionalManifests...) | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 453 | transitiveManifestPaths = append(transitiveManifestPaths, staticManifestsDepSet.ToList()...) | 
| Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 454 |  | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 455 | if len(transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) { | 
| Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 456 | manifestMergerParams := ManifestMergerParams{ | 
|  | 457 | staticLibManifests: transitiveManifestPaths[1:], | 
| Alix | 96ea8845 | 2023-08-31 15:48:23 +0000 | [diff] [blame] | 458 | isLibrary:          a.isLibrary, | 
|  | 459 | packageName:        a.manifestValues.applicationId, | 
|  | 460 | } | 
| Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 461 | a.mergedManifestFile = manifestMerger(ctx, transitiveManifestPaths[0], manifestMergerParams) | 
| Colin Cross | a6182ab | 2024-08-21 10:47:44 -0700 | [diff] [blame] | 462 | ctx.CheckbuildFile(a.mergedManifestFile) | 
| Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 463 | if !a.isLibrary { | 
|  | 464 | // Only use the merged manifest for applications.  For libraries, the transitive closure of manifests | 
|  | 465 | // will be propagated to the final application and merged there.  The merged manifest for libraries is | 
|  | 466 | // only passed to Make, which can't handle transitive dependencies. | 
|  | 467 | manifestPath = a.mergedManifestFile | 
|  | 468 | } | 
|  | 469 | } else { | 
|  | 470 | a.mergedManifestFile = manifestPath | 
|  | 471 | } | 
| Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 472 |  | 
| Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 473 | compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, opts.sdkContext, manifestPath) | 
| Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 474 |  | 
|  | 475 | linkFlags = append(linkFlags, libFlags...) | 
| Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 476 | linkDeps = append(linkDeps, sharedExportPackages...) | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 477 | linkDeps = append(linkDeps, staticDeps.resPackages()...) | 
| Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 478 | linkFlags = append(linkFlags, opts.extraLinkFlags...) | 
| Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 479 | if a.isLibrary { | 
|  | 480 | linkFlags = append(linkFlags, "--static-lib") | 
|  | 481 | } | 
| Rico Wind | 7152e82 | 2024-04-19 08:14:15 +0200 | [diff] [blame] | 482 | if opts.forceNonFinalResourceIDs { | 
|  | 483 | linkFlags = append(linkFlags, "--non-final-ids") | 
|  | 484 | } | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 485 |  | 
| Colin Cross | 7c4dc5d | 2024-02-13 14:29:45 -0800 | [diff] [blame] | 486 | linkFlags = append(linkFlags, "--no-static-lib-packages") | 
| Spandan Das | 457e506 | 2024-10-05 01:00:34 +0000 | [diff] [blame] | 487 | if a.isLibrary { | 
|  | 488 | // Pass --merge-only to skip resource references validation until the final | 
|  | 489 | // app link step when when all static libraries are present. | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 490 | linkFlags = append(linkFlags, "--merge-only") | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 491 | } | 
|  | 492 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 493 | packageRes := android.PathForModuleOut(ctx, "package-res.apk") | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 494 | proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options") | 
|  | 495 | rTxt := android.PathForModuleOut(ctx, "R.txt") | 
| Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 496 | // This file isn't used by Soong, but is generated for exporting | 
|  | 497 | extraPackages := android.PathForModuleOut(ctx, "extra_packages") | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 498 | var transitiveRJars android.Paths | 
| Colin Cross | f3b7bad | 2023-08-02 15:49:00 -0700 | [diff] [blame] | 499 | var srcJar android.WritablePath | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 500 |  | 
| Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 501 | var compiledResDirs []android.Paths | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 502 | for _, dir := range resDirs { | 
| Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 503 | a.resourceFiles = append(a.resourceFiles, dir.files...) | 
| Jihoon Kang | 98ea836 | 2024-07-16 18:20:03 +0000 | [diff] [blame] | 504 | compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, | 
|  | 505 | compileFlags, a.filterProduct(), opts.aconfigTextFiles).Paths()) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 506 | } | 
| Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 507 |  | 
| Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 508 | for i, zip := range resZips { | 
|  | 509 | flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i)) | 
| Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame] | 510 | aapt2CompileZip(ctx, flata, zip, "", compileFlags) | 
| Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 511 | compiledResDirs = append(compiledResDirs, android.Paths{flata}) | 
|  | 512 | } | 
|  | 513 |  | 
| Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 514 | var compiledRes, compiledOverlay android.Paths | 
|  | 515 |  | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 516 | // AAPT2 overlays are in lowest to highest priority order, reverse the topological order | 
|  | 517 | // of transitiveStaticLibs. | 
|  | 518 | transitiveStaticLibs := android.ReversePaths(staticDeps.resPackages()) | 
|  | 519 |  | 
| Colin Cross | 8f1b033 | 2024-01-25 13:39:06 -0800 | [diff] [blame] | 520 | if a.isLibrary && a.useResourceProcessorBusyBox(ctx) { | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 521 | // When building an android_library with ResourceProcessorBusyBox enabled treat static library dependencies | 
|  | 522 | // as imports.  The resources from dependencies will not be merged into this module's package-res.apk, and | 
|  | 523 | // instead modules depending on this module will reference package-res.apk from all transitive static | 
|  | 524 | // dependencies. | 
| Colin Cross | 1d3f590 | 2024-03-05 11:51:54 -0800 | [diff] [blame] | 525 | for _, sharedDep := range sharedDeps { | 
|  | 526 | if sharedDep.usedResourceProcessor { | 
|  | 527 | transitiveRJars = append(transitiveRJars, sharedDep.rJar) | 
|  | 528 | } | 
|  | 529 | } | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 530 | for _, staticDep := range staticDeps { | 
|  | 531 | linkDeps = append(linkDeps, staticDep.resPackage) | 
|  | 532 | linkFlags = append(linkFlags, "-I "+staticDep.resPackage.String()) | 
|  | 533 | if staticDep.usedResourceProcessor { | 
|  | 534 | transitiveRJars = append(transitiveRJars, staticDep.rJar) | 
|  | 535 | } | 
|  | 536 | } | 
|  | 537 | } else { | 
|  | 538 | // When building an app or building a library without ResourceProcessorBusyBox enabled all static | 
|  | 539 | // dependencies are compiled into this module's package-res.apk as overlays. | 
|  | 540 | compiledOverlay = append(compiledOverlay, transitiveStaticLibs...) | 
|  | 541 | } | 
| Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 542 |  | 
| Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 543 | if len(transitiveStaticLibs) > 0 { | 
| Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 544 | // If we are using static android libraries, every source file becomes an overlay. | 
|  | 545 | // This is to emulate old AAPT behavior which simulated library support. | 
|  | 546 | for _, compiledResDir := range compiledResDirs { | 
|  | 547 | compiledOverlay = append(compiledOverlay, compiledResDir...) | 
|  | 548 | } | 
| Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 549 | } else if a.isLibrary { | 
|  | 550 | // Otherwise, for a static library we treat all the resources equally with no overlay. | 
|  | 551 | for _, compiledResDir := range compiledResDirs { | 
|  | 552 | compiledRes = append(compiledRes, compiledResDir...) | 
|  | 553 | } | 
| Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 554 | } else if len(compiledResDirs) > 0 { | 
|  | 555 | // Without static libraries, the first directory is our directory, which can then be | 
|  | 556 | // overlaid by the rest. | 
|  | 557 | compiledRes = append(compiledRes, compiledResDirs[0]...) | 
|  | 558 | for _, compiledResDir := range compiledResDirs[1:] { | 
|  | 559 | compiledOverlay = append(compiledOverlay, compiledResDir...) | 
|  | 560 | } | 
|  | 561 | } | 
|  | 562 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 563 | for _, dir := range overlayDirs { | 
| Jihoon Kang | 98ea836 | 2024-07-16 18:20:03 +0000 | [diff] [blame] | 564 | compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, | 
|  | 565 | compileFlags, a.filterProduct(), opts.aconfigTextFiles).Paths()...) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 566 | } | 
|  | 567 |  | 
| Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 568 | var splitPackages android.WritablePaths | 
|  | 569 | var splits []split | 
|  | 570 |  | 
|  | 571 | for _, s := range a.splitNames { | 
|  | 572 | suffix := strings.Replace(s, ",", "_", -1) | 
|  | 573 | path := android.PathForModuleOut(ctx, "package_"+suffix+".apk") | 
|  | 574 | linkFlags = append(linkFlags, "--split", path.String()+":"+s) | 
|  | 575 | splitPackages = append(splitPackages, path) | 
|  | 576 | splits = append(splits, split{ | 
|  | 577 | name:   s, | 
|  | 578 | suffix: suffix, | 
|  | 579 | path:   path, | 
|  | 580 | }) | 
|  | 581 | } | 
|  | 582 |  | 
| Colin Cross | 8f1b033 | 2024-01-25 13:39:06 -0800 | [diff] [blame] | 583 | if !a.useResourceProcessorBusyBox(ctx) { | 
| Colin Cross | f3b7bad | 2023-08-02 15:49:00 -0700 | [diff] [blame] | 584 | // the subdir "android" is required to be filtered by package names | 
|  | 585 | srcJar = android.PathForModuleGen(ctx, "android", "R.srcjar") | 
|  | 586 | } | 
|  | 587 |  | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 588 | // No need to specify assets from dependencies to aapt2Link for libraries, all transitive assets will be | 
|  | 589 | // provided to the final app aapt2Link step. | 
|  | 590 | var transitiveAssets android.Paths | 
|  | 591 | if !a.isLibrary { | 
|  | 592 | transitiveAssets = android.ReverseSliceInPlace(staticDeps.assets()) | 
|  | 593 | } | 
| Colin Cross | f3b7bad | 2023-08-02 15:49:00 -0700 | [diff] [blame] | 594 | aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, | 
| Jihoon Kang | 84b2589 | 2023-12-01 22:01:06 +0000 | [diff] [blame] | 595 | linkFlags, linkDeps, compiledRes, compiledOverlay, transitiveAssets, splitPackages, | 
|  | 596 | opts.aconfigTextFiles) | 
| Colin Cross | a6182ab | 2024-08-21 10:47:44 -0700 | [diff] [blame] | 597 | ctx.CheckbuildFile(packageRes) | 
|  | 598 |  | 
| Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 599 | // Extract assets from the resource package output so that they can be used later in aapt2link | 
|  | 600 | // for modules that depend on this one. | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 601 | if android.PrefixInList(linkFlags, "-A ") { | 
| Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 602 | assets := android.PathForModuleOut(ctx, "assets.zip") | 
|  | 603 | ctx.Build(pctx, android.BuildParams{ | 
|  | 604 | Rule:        extractAssetsRule, | 
|  | 605 | Input:       packageRes, | 
|  | 606 | Output:      assets, | 
|  | 607 | Description: "extract assets from built resource file", | 
|  | 608 | }) | 
|  | 609 | a.assetPackage = android.OptionalPathForPath(assets) | 
|  | 610 | } | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 611 |  | 
| Colin Cross | 8f1b033 | 2024-01-25 13:39:06 -0800 | [diff] [blame] | 612 | if a.useResourceProcessorBusyBox(ctx) { | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 613 | rJar := android.PathForModuleOut(ctx, "busybox/R.jar") | 
| Rico Wind | a2fa263 | 2024-03-13 13:09:17 +0100 | [diff] [blame] | 614 | resourceProcessorBusyBoxGenerateBinaryR(ctx, rTxt, a.mergedManifestFile, rJar, staticDeps, a.isLibrary, a.aaptProperties.Aaptflags, | 
|  | 615 | opts.forceNonFinalResourceIDs) | 
| Colin Cross | f3b7bad | 2023-08-02 15:49:00 -0700 | [diff] [blame] | 616 | aapt2ExtractExtraPackages(ctx, extraPackages, rJar) | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 617 | transitiveRJars = append(transitiveRJars, rJar) | 
|  | 618 | a.rJar = rJar | 
| Colin Cross | f3b7bad | 2023-08-02 15:49:00 -0700 | [diff] [blame] | 619 | } else { | 
|  | 620 | aapt2ExtractExtraPackages(ctx, extraPackages, srcJar) | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 621 | } | 
|  | 622 |  | 
| Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame] | 623 | transitiveAaptResourcePackages := staticDeps.resPackages().Strings() | 
|  | 624 | transitiveAaptResourcePackages = slices.DeleteFunc(transitiveAaptResourcePackages, func(p string) bool { | 
|  | 625 | return p == packageRes.String() | 
|  | 626 | }) | 
|  | 627 | transitiveAaptResourcePackagesFile := android.PathForModuleOut(ctx, "transitive-res-packages") | 
|  | 628 | android.WriteFileRule(ctx, transitiveAaptResourcePackagesFile, strings.Join(transitiveAaptResourcePackages, "\n")) | 
|  | 629 |  | 
| Colin Cross | 1d3f590 | 2024-03-05 11:51:54 -0800 | [diff] [blame] | 630 | // Reverse the list of R.jar files so that the current module comes first, and direct dependencies come before | 
|  | 631 | // transitive dependencies. | 
|  | 632 | transitiveRJars = android.ReversePaths(transitiveRJars) | 
|  | 633 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 634 | a.aaptSrcJar = srcJar | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 635 | a.transitiveAaptRJars = transitiveRJars | 
| Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame] | 636 | a.transitiveAaptResourcePackagesFile = transitiveAaptResourcePackagesFile | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 637 | a.exportPackage = packageRes | 
|  | 638 | a.manifestPath = manifestPath | 
|  | 639 | a.proguardOptionsFile = proguardOptionsFile | 
| Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 640 | a.extraAaptPackagesFile = extraPackages | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 641 | a.rTxt = rTxt | 
| Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 642 | a.splits = splits | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 643 | a.resourcesNodesDepSet = depset.NewBuilder[*resourcesNode](depset.TOPOLOGICAL). | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 644 | Direct(&resourcesNode{ | 
|  | 645 | resPackage:          a.exportPackage, | 
|  | 646 | manifest:            a.manifestPath, | 
|  | 647 | additionalManifests: additionalManifests, | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 648 | rTxt:                a.rTxt, | 
|  | 649 | rJar:                a.rJar, | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 650 | assets:              a.assetPackage, | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 651 |  | 
| Colin Cross | 8f1b033 | 2024-01-25 13:39:06 -0800 | [diff] [blame] | 652 | usedResourceProcessor: a.useResourceProcessorBusyBox(ctx), | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 653 | }). | 
|  | 654 | Transitive(staticResourcesNodesDepSet).Build() | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 655 | a.rroDirsDepSet = depset.NewBuilder[rroDir](depset.TOPOLOGICAL). | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 656 | Direct(rroDirs...). | 
|  | 657 | Transitive(staticRRODirsDepSet).Build() | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 658 | a.manifestsDepSet = depset.NewBuilder[android.Path](depset.TOPOLOGICAL). | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 659 | Direct(a.manifestPath). | 
|  | 660 | DirectSlice(additionalManifests). | 
|  | 661 | Transitive(staticManifestsDepSet).Build() | 
|  | 662 | } | 
|  | 663 |  | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 664 | var resourceProcessorBusyBox = pctx.AndroidStaticRule("resourceProcessorBusyBox", | 
|  | 665 | blueprint.RuleParams{ | 
|  | 666 | Command: "${config.JavaCmd} -cp ${config.ResourceProcessorBusyBox} " + | 
|  | 667 | "com.google.devtools.build.android.ResourceProcessorBusyBox --tool=GENERATE_BINARY_R -- @${out}.args && " + | 
|  | 668 | "if cmp -s ${out}.tmp ${out} ; then rm ${out}.tmp ; else mv ${out}.tmp ${out}; fi", | 
|  | 669 | CommandDeps:    []string{"${config.ResourceProcessorBusyBox}"}, | 
|  | 670 | Rspfile:        "${out}.args", | 
|  | 671 | RspfileContent: "--primaryRTxt ${rTxt} --primaryManifest ${manifest} --classJarOutput ${out}.tmp ${args}", | 
|  | 672 | Restat:         true, | 
|  | 673 | }, "rTxt", "manifest", "args") | 
|  | 674 |  | 
|  | 675 | // resourceProcessorBusyBoxGenerateBinaryR converts the R.txt file produced by aapt2 into R.class files | 
|  | 676 | // using Bazel's ResourceProcessorBusyBox tool, which is faster than compiling the R.java files and | 
|  | 677 | // supports producing classes for static dependencies that only include resources from that dependency. | 
|  | 678 | func resourceProcessorBusyBoxGenerateBinaryR(ctx android.ModuleContext, rTxt, manifest android.Path, | 
| Rico Wind | a2fa263 | 2024-03-13 13:09:17 +0100 | [diff] [blame] | 679 | rJar android.WritablePath, transitiveDeps transitiveAarDeps, isLibrary bool, aaptFlags []string, | 
|  | 680 | forceNonFinalIds bool) { | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 681 |  | 
|  | 682 | var args []string | 
|  | 683 | var deps android.Paths | 
|  | 684 |  | 
|  | 685 | if !isLibrary { | 
|  | 686 | // When compiling an app, pass all R.txt and AndroidManifest.xml from transitive static library dependencies | 
|  | 687 | // to ResourceProcessorBusyBox so that it can regenerate R.class files with the final resource IDs for each | 
|  | 688 | // package. | 
|  | 689 | args, deps = transitiveDeps.resourceProcessorDeps() | 
| Rico Wind | a2fa263 | 2024-03-13 13:09:17 +0100 | [diff] [blame] | 690 | if forceNonFinalIds { | 
|  | 691 | args = append(args, "--finalFields=false") | 
|  | 692 | } | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 693 | } else { | 
|  | 694 | // When compiling a library don't pass any dependencies as it only needs to generate an R.class file for this | 
|  | 695 | // library.  Pass --finalFields=false so that the R.class file contains non-final fields so they don't get | 
|  | 696 | // inlined into the library before the final IDs are assigned during app compilation. | 
|  | 697 | args = append(args, "--finalFields=false") | 
|  | 698 | } | 
|  | 699 |  | 
| Colin Cross | d3f7d1a | 2024-01-03 19:42:25 -0800 | [diff] [blame] | 700 | for i, arg := range aaptFlags { | 
|  | 701 | const AAPT_CUSTOM_PACKAGE = "--custom-package" | 
|  | 702 | if strings.HasPrefix(arg, AAPT_CUSTOM_PACKAGE) { | 
|  | 703 | pkg := strings.TrimSpace(strings.TrimPrefix(arg, AAPT_CUSTOM_PACKAGE)) | 
|  | 704 | if pkg == "" && i+1 < len(aaptFlags) { | 
|  | 705 | pkg = aaptFlags[i+1] | 
|  | 706 | } | 
|  | 707 | args = append(args, "--packageForR "+pkg) | 
|  | 708 | } | 
|  | 709 | } | 
|  | 710 |  | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 711 | deps = append(deps, rTxt, manifest) | 
|  | 712 |  | 
|  | 713 | ctx.Build(pctx, android.BuildParams{ | 
|  | 714 | Rule:        resourceProcessorBusyBox, | 
|  | 715 | Output:      rJar, | 
|  | 716 | Implicits:   deps, | 
|  | 717 | Description: "ResourceProcessorBusyBox", | 
|  | 718 | Args: map[string]string{ | 
|  | 719 | "rTxt":     rTxt.String(), | 
|  | 720 | "manifest": manifest.String(), | 
|  | 721 | "args":     strings.Join(args, " "), | 
|  | 722 | }, | 
|  | 723 | }) | 
|  | 724 | } | 
|  | 725 |  | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 726 | type resourcesNode struct { | 
|  | 727 | resPackage          android.Path | 
|  | 728 | manifest            android.Path | 
|  | 729 | additionalManifests android.Paths | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 730 | rTxt                android.Path | 
|  | 731 | rJar                android.Path | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 732 | assets              android.OptionalPath | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 733 |  | 
|  | 734 | usedResourceProcessor bool | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 735 | } | 
|  | 736 |  | 
|  | 737 | type transitiveAarDeps []*resourcesNode | 
|  | 738 |  | 
|  | 739 | func (t transitiveAarDeps) resPackages() android.Paths { | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 740 | paths := make(android.Paths, 0, len(t)) | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 741 | for _, dep := range t { | 
|  | 742 | paths = append(paths, dep.resPackage) | 
|  | 743 | } | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 744 | return paths | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 745 | } | 
|  | 746 |  | 
|  | 747 | func (t transitiveAarDeps) manifests() android.Paths { | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 748 | paths := make(android.Paths, 0, len(t)) | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 749 | for _, dep := range t { | 
|  | 750 | paths = append(paths, dep.manifest) | 
|  | 751 | paths = append(paths, dep.additionalManifests...) | 
|  | 752 | } | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 753 | return paths | 
|  | 754 | } | 
|  | 755 |  | 
|  | 756 | func (t transitiveAarDeps) resourceProcessorDeps() (args []string, deps android.Paths) { | 
|  | 757 | for _, dep := range t { | 
|  | 758 | args = append(args, "--library="+dep.rTxt.String()+","+dep.manifest.String()) | 
|  | 759 | deps = append(deps, dep.rTxt, dep.manifest) | 
|  | 760 | } | 
|  | 761 | return args, deps | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 762 | } | 
|  | 763 |  | 
|  | 764 | func (t transitiveAarDeps) assets() android.Paths { | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 765 | paths := make(android.Paths, 0, len(t)) | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 766 | for _, dep := range t { | 
|  | 767 | if dep.assets.Valid() { | 
|  | 768 | paths = append(paths, dep.assets.Path()) | 
|  | 769 | } | 
|  | 770 | } | 
|  | 771 | return paths | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 772 | } | 
|  | 773 |  | 
|  | 774 | // aaptLibs collects libraries from dependencies and sdk_version and converts them into paths | 
| Jiakai Zhang | 3693708 | 2024-04-15 11:15:50 +0000 | [diff] [blame] | 775 | func aaptLibs(ctx android.ModuleContext, sdkContext android.SdkContext, | 
|  | 776 | classLoaderContexts dexpreopt.ClassLoaderContextMap, usesLibrary *usesLibrary) ( | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 777 | staticResourcesNodes, sharedResourcesNodes depset.DepSet[*resourcesNode], staticRRODirs depset.DepSet[rroDir], | 
|  | 778 | staticManifests depset.DepSet[android.Path], sharedLibs android.Paths, flags []string) { | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 779 |  | 
| Ulya Trafimovich | b23d28c | 2020-10-08 12:53:58 +0100 | [diff] [blame] | 780 | if classLoaderContexts == nil { | 
| Ulya Trafimovich | 1855424 | 2020-11-03 15:55:11 +0000 | [diff] [blame] | 781 | // Not all callers need to compute class loader context, those who don't just pass nil. | 
|  | 782 | // 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] | 783 | classLoaderContexts = make(dexpreopt.ClassLoaderContextMap) | 
| Ulya Trafimovich | 1855424 | 2020-11-03 15:55:11 +0000 | [diff] [blame] | 784 | } | 
|  | 785 |  | 
| Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 786 | sdkDep := decodeSdkDep(ctx, sdkContext) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 787 | if sdkDep.useFiles { | 
| Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 788 | sharedLibs = append(sharedLibs, sdkDep.jars...) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 789 | } | 
|  | 790 |  | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 791 | var staticResourcesNodeDepSets []depset.DepSet[*resourcesNode] | 
|  | 792 | var sharedResourcesNodeDepSets []depset.DepSet[*resourcesNode] | 
|  | 793 | rroDirsDepSetBuilder := depset.NewBuilder[rroDir](depset.TOPOLOGICAL) | 
|  | 794 | manifestsDepSetBuilder := depset.NewBuilder[android.Path](depset.TOPOLOGICAL) | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 795 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 796 | ctx.VisitDirectDeps(func(module android.Module) { | 
| Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 797 | depTag := ctx.OtherModuleDependencyTag(module) | 
| Ulya Trafimovich | 1855424 | 2020-11-03 15:55:11 +0000 | [diff] [blame] | 798 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 799 | var exportPackage android.Path | 
| Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 800 | aarDep, _ := module.(AndroidLibraryDependency) | 
|  | 801 | if aarDep != nil { | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 802 | exportPackage = aarDep.ExportPackage() | 
|  | 803 | } | 
|  | 804 |  | 
| Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 805 | switch depTag { | 
| Colin Cross | 4b964c0 | 2018-10-15 16:18:06 -0700 | [diff] [blame] | 806 | case instrumentationForTag: | 
|  | 807 | // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2. | 
| Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 808 | case sdkLibTag, libTag: | 
| Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 809 | if exportPackage != nil { | 
| Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 810 | sharedResourcesNodeDepSets = append(sharedResourcesNodeDepSets, aarDep.ResourcesNodeDepSet()) | 
| Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 811 | sharedLibs = append(sharedLibs, exportPackage) | 
|  | 812 | } | 
| Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 813 | case frameworkResTag: | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 814 | if exportPackage != nil { | 
|  | 815 | sharedLibs = append(sharedLibs, exportPackage) | 
|  | 816 | } | 
|  | 817 | case staticLibTag: | 
|  | 818 | if exportPackage != nil { | 
| Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 819 | staticResourcesNodeDepSets = append(staticResourcesNodeDepSets, aarDep.ResourcesNodeDepSet()) | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 820 | rroDirsDepSetBuilder.Transitive(aarDep.RRODirsDepSet()) | 
|  | 821 | manifestsDepSetBuilder.Transitive(aarDep.ManifestsDepSet()) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 822 | } | 
|  | 823 | } | 
| Ulya Trafimovich | 1855424 | 2020-11-03 15:55:11 +0000 | [diff] [blame] | 824 |  | 
| Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 825 | addCLCFromDep(ctx, module, classLoaderContexts) | 
| Jiakai Zhang | 3693708 | 2024-04-15 11:15:50 +0000 | [diff] [blame] | 826 | if usesLibrary != nil { | 
|  | 827 | addMissingOptionalUsesLibsFromDep(ctx, module, usesLibrary) | 
|  | 828 | } | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 829 | }) | 
|  | 830 |  | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 831 | // AAPT2 overlays are in lowest to highest priority order, the topological order will be reversed later. | 
|  | 832 | // Reverse the dependency order now going into the depset so that it comes out in order after the second | 
|  | 833 | // reverse later. | 
|  | 834 | // NOTE: this is legacy and probably incorrect behavior, for most other cases (e.g. conflicting classes in | 
|  | 835 | // dependencies) the highest priority dependency is listed first, but for resources the highest priority | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 836 | // dependency has to be listed last.  This is also inconsistent with the way manifests from the same | 
|  | 837 | // transitive dependencies are merged. | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 838 | staticResourcesNodes = depset.New(depset.TOPOLOGICAL, nil, | 
| Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 839 | android.ReverseSliceInPlace(staticResourcesNodeDepSets)) | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 840 | sharedResourcesNodes = depset.New(depset.TOPOLOGICAL, nil, | 
| Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 841 | android.ReverseSliceInPlace(sharedResourcesNodeDepSets)) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 842 |  | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 843 | staticRRODirs = rroDirsDepSetBuilder.Build() | 
|  | 844 | staticManifests = manifestsDepSetBuilder.Build() | 
|  | 845 |  | 
|  | 846 | if len(staticResourcesNodes.ToList()) > 0 { | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 847 | flags = append(flags, "--auto-add-overlay") | 
|  | 848 | } | 
|  | 849 |  | 
|  | 850 | for _, sharedLib := range sharedLibs { | 
|  | 851 | flags = append(flags, "-I "+sharedLib.String()) | 
|  | 852 | } | 
|  | 853 |  | 
| Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 854 | return staticResourcesNodes, sharedResourcesNodes, staticRRODirs, staticManifests, sharedLibs, flags | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 855 | } | 
|  | 856 |  | 
|  | 857 | type AndroidLibrary struct { | 
|  | 858 | Library | 
|  | 859 | aapt | 
|  | 860 |  | 
|  | 861 | androidLibraryProperties androidLibraryProperties | 
|  | 862 |  | 
|  | 863 | aarFile android.WritablePath | 
| Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 864 | } | 
|  | 865 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 866 | var _ AndroidLibraryDependency = (*AndroidLibrary)(nil) | 
|  | 867 |  | 
|  | 868 | func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) { | 
| Jiakai Zhang | f98da19 | 2024-04-15 11:15:41 +0000 | [diff] [blame] | 869 | a.usesLibrary.deps(ctx, false) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 870 | a.Module.deps(ctx) | 
| Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 871 | sdkDep := decodeSdkDep(ctx, android.SdkContext(a)) | 
| Paul Duffin | 250e619 | 2019-06-07 10:44:37 +0100 | [diff] [blame] | 872 | if sdkDep.hasFrameworkLibs() { | 
|  | 873 | a.aapt.deps(ctx, sdkDep) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 874 | } | 
| Jihoon Kang | 9049c27 | 2024-03-19 21:57:36 +0000 | [diff] [blame] | 875 |  | 
|  | 876 | for _, aconfig_declaration := range a.aaptProperties.Flags_packages { | 
|  | 877 | ctx.AddDependency(ctx.Module(), aconfigDeclarationTag, aconfig_declaration) | 
|  | 878 | } | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 879 | } | 
|  | 880 |  | 
|  | 881 | func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 882 | a.aapt.isLibrary = true | 
| Ulya Trafimovich | 42c7f0d | 2021-08-17 16:20:29 +0100 | [diff] [blame] | 883 | a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx) | 
| Spandan Das | 0727ba7 | 2024-02-13 16:37:43 +0000 | [diff] [blame] | 884 | if a.usesLibrary.shouldDisableDexpreopt { | 
|  | 885 | a.dexpreopter.disableDexpreopt() | 
|  | 886 | } | 
| Jihoon Kang | 9aef777 | 2024-06-14 23:45:06 +0000 | [diff] [blame] | 887 | aconfigTextFilePaths := getAconfigFilePaths(ctx) | 
| Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 888 | a.aapt.buildActions(ctx, | 
|  | 889 | aaptBuildActionOptions{ | 
|  | 890 | sdkContext:                     android.SdkContext(a), | 
|  | 891 | classLoaderContexts:            a.classLoaderContexts, | 
|  | 892 | enforceDefaultTargetSdkVersion: false, | 
| Jihoon Kang | 9aef777 | 2024-06-14 23:45:06 +0000 | [diff] [blame] | 893 | aconfigTextFiles:               aconfigTextFilePaths, | 
| Jiakai Zhang | 3693708 | 2024-04-15 11:15:50 +0000 | [diff] [blame] | 894 | usesLibrary:                    &a.usesLibrary, | 
| Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 895 | }, | 
|  | 896 | ) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 897 |  | 
| Colin Cross | ff694a8 | 2023-12-13 15:54:49 -0800 | [diff] [blame] | 898 | apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider) | 
|  | 899 | a.hideApexVariantFromMake = !apexInfo.IsForPlatform() | 
| Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 900 |  | 
| yangbill | 2af0b6e | 2024-03-15 09:29:29 +0000 | [diff] [blame] | 901 | a.stem = proptools.StringDefault(a.overridableProperties.Stem, ctx.ModuleName()) | 
| Jihoon Kang | 1bfb6f2 | 2023-07-01 00:13:47 +0000 | [diff] [blame] | 902 |  | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 903 | ctx.CheckbuildFile(a.aapt.proguardOptionsFile) | 
|  | 904 | ctx.CheckbuildFile(a.aapt.exportPackage) | 
| Colin Cross | 8f1b033 | 2024-01-25 13:39:06 -0800 | [diff] [blame] | 905 | if a.useResourceProcessorBusyBox(ctx) { | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 906 | ctx.CheckbuildFile(a.aapt.rJar) | 
| Colin Cross | f3b7bad | 2023-08-02 15:49:00 -0700 | [diff] [blame] | 907 | } else { | 
|  | 908 | ctx.CheckbuildFile(a.aapt.aaptSrcJar) | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 909 | } | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 910 |  | 
|  | 911 | // apps manifests are handled by aapt, don't let Module see them | 
|  | 912 | a.properties.Manifest = nil | 
|  | 913 |  | 
| Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 914 | a.linter.mergedManifest = a.aapt.mergedManifestFile | 
|  | 915 | a.linter.manifest = a.aapt.manifestPath | 
|  | 916 | a.linter.resources = a.aapt.resourceFiles | 
|  | 917 |  | 
| Sam Delmerico | c8e040c | 2023-10-31 17:27:02 +0000 | [diff] [blame] | 918 | proguardSpecInfo := a.collectProguardSpecInfo(ctx) | 
| Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 919 | android.SetProvider(ctx, ProguardSpecInfoProvider, proguardSpecInfo) | 
| Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame] | 920 | exportedProguardFlagsFiles := proguardSpecInfo.ProguardFlagsFiles.ToList() | 
|  | 921 | a.extraProguardFlagsFiles = append(a.extraProguardFlagsFiles, exportedProguardFlagsFiles...) | 
|  | 922 | a.extraProguardFlagsFiles = append(a.extraProguardFlagsFiles, a.proguardOptionsFile) | 
|  | 923 |  | 
|  | 924 | combinedExportedProguardFlagFile := android.PathForModuleOut(ctx, "export_proguard_flags") | 
|  | 925 | writeCombinedProguardFlagsFile(ctx, combinedExportedProguardFlagFile, exportedProguardFlagsFiles) | 
|  | 926 | a.combinedExportedProguardFlagsFile = combinedExportedProguardFlagFile | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 927 |  | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 928 | var extraSrcJars android.Paths | 
|  | 929 | var extraCombinedJars android.Paths | 
|  | 930 | var extraClasspathJars android.Paths | 
| Colin Cross | 8f1b033 | 2024-01-25 13:39:06 -0800 | [diff] [blame] | 931 | if a.useResourceProcessorBusyBox(ctx) { | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 932 | // When building a library with ResourceProcessorBusyBox enabled ResourceProcessorBusyBox for this | 
|  | 933 | // library and each of the transitive static android_library dependencies has already created an | 
|  | 934 | // R.class file for the appropriate package.  Add all of those R.class files to the classpath. | 
|  | 935 | extraClasspathJars = a.transitiveAaptRJars | 
|  | 936 | } else { | 
|  | 937 | // When building a library without ResourceProcessorBusyBox the aapt2 rule creates R.srcjar containing | 
|  | 938 | // R.java files for the library's package and the packages from all transitive static android_library | 
|  | 939 | // dependencies.  Compile the srcjar alongside the rest of the sources. | 
|  | 940 | extraSrcJars = android.Paths{a.aapt.aaptSrcJar} | 
|  | 941 | } | 
|  | 942 |  | 
| Colin Cross | fdaa672 | 2024-08-23 11:58:08 -0700 | [diff] [blame] | 943 | a.Module.compile(ctx, extraSrcJars, extraClasspathJars, extraCombinedJars, nil) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 944 |  | 
| Colin Cross | f57c578 | 2019-01-25 13:20:38 -0800 | [diff] [blame] | 945 | a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar") | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 946 | var res android.Paths | 
|  | 947 | if a.androidLibraryProperties.BuildAAR { | 
|  | 948 | BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 949 | } | 
| Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 950 |  | 
| Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 951 | prebuiltJniPackages := android.Paths{} | 
|  | 952 | ctx.VisitDirectDeps(func(module android.Module) { | 
| Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 953 | if info, ok := android.OtherModuleProvider(ctx, module, JniPackageProvider); ok { | 
| Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 954 | prebuiltJniPackages = append(prebuiltJniPackages, info.JniPackages...) | 
|  | 955 | } | 
|  | 956 | }) | 
|  | 957 | if len(prebuiltJniPackages) > 0 { | 
| Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 958 | android.SetProvider(ctx, JniPackageProvider, JniPackageInfo{ | 
| Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 959 | JniPackages: prebuiltJniPackages, | 
|  | 960 | }) | 
|  | 961 | } | 
| Jihoon Kang | 9aef777 | 2024-06-14 23:45:06 +0000 | [diff] [blame] | 962 |  | 
|  | 963 | android.SetProvider(ctx, FlagsPackagesProvider, FlagsPackages{ | 
|  | 964 | AconfigTextFiles: aconfigTextFilePaths, | 
|  | 965 | }) | 
| mrziwang | 9f7b9f4 | 2024-07-10 12:18:06 -0700 | [diff] [blame] | 966 |  | 
|  | 967 | a.setOutputFiles(ctx) | 
|  | 968 | } | 
|  | 969 |  | 
|  | 970 | func (a *AndroidLibrary) setOutputFiles(ctx android.ModuleContext) { | 
|  | 971 | ctx.SetOutputFiles([]android.Path{a.aarFile}, ".aar") | 
|  | 972 | setOutputFiles(ctx, a.Library.Module) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 973 | } | 
|  | 974 |  | 
| Cole Faust | b36d31d | 2024-08-27 16:04:28 -0700 | [diff] [blame] | 975 | func (a *AndroidLibrary) IDEInfo(ctx android.BaseModuleContext, dpInfo *android.IdeInfo) { | 
|  | 976 | a.Library.IDEInfo(ctx, dpInfo) | 
|  | 977 | a.aapt.IDEInfo(ctx, dpInfo) | 
| Colin Cross | 95b53b8 | 2023-10-17 13:21:02 -0700 | [diff] [blame] | 978 | } | 
|  | 979 |  | 
| Cole Faust | b36d31d | 2024-08-27 16:04:28 -0700 | [diff] [blame] | 980 | func (a *aapt) IDEInfo(ctx android.BaseModuleContext, dpInfo *android.IdeInfo) { | 
| Colin Cross | 8f1b033 | 2024-01-25 13:39:06 -0800 | [diff] [blame] | 981 | if a.rJar != nil { | 
| Colin Cross | 95b53b8 | 2023-10-17 13:21:02 -0700 | [diff] [blame] | 982 | dpInfo.Jars = append(dpInfo.Jars, a.rJar.String()) | 
|  | 983 | } | 
|  | 984 | } | 
|  | 985 |  | 
| Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 986 | // android_library builds and links sources into a `.jar` file for the device along with Android resources. | 
|  | 987 | // | 
|  | 988 | // An android_library has a single variant that produces a `.jar` file containing `.class` files that were | 
| Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 989 | // compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled | 
| Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 990 | // with aapt2.  This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of | 
|  | 991 | // an android_app module. | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 992 | func AndroidLibraryFactory() android.Module { | 
|  | 993 | module := &AndroidLibrary{} | 
|  | 994 |  | 
| Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 995 | module.Module.addHostAndDeviceProperties() | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 996 | module.AddProperties( | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 997 | &module.aaptProperties, | 
| Ronald Braunstein | cdc66f4 | 2024-04-12 11:23:19 -0700 | [diff] [blame] | 998 | &module.androidLibraryProperties, | 
|  | 999 | &module.sourceProperties) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 1000 |  | 
|  | 1001 | module.androidLibraryProperties.BuildAAR = true | 
| Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 1002 | module.Module.linter.library = true | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 1003 |  | 
| Jooyung Han | acc7bbe | 2020-05-20 09:06:00 +0900 | [diff] [blame] | 1004 | android.InitApexModule(module) | 
| Colin Cross | 48de9a4 | 2018-10-02 13:53:33 -0700 | [diff] [blame] | 1005 | InitJavaModule(module, android.DeviceSupported) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 1006 | return module | 
|  | 1007 | } | 
|  | 1008 |  | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1009 | // | 
|  | 1010 | // AAR (android library) prebuilts | 
|  | 1011 | // | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1012 |  | 
| Vinh Tran | ce0781f | 2022-04-13 01:30:44 +0000 | [diff] [blame] | 1013 | // Properties for android_library_import | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1014 | type AARImportProperties struct { | 
| Vinh Tran | ce0781f | 2022-04-13 01:30:44 +0000 | [diff] [blame] | 1015 | // ARR (android library prebuilt) filepath. Exactly one ARR is required. | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 1016 | Aars []string `android:"path"` | 
| Vinh Tran | ce0781f | 2022-04-13 01:30:44 +0000 | [diff] [blame] | 1017 | // If not blank, set to the version of the sdk to compile against. | 
|  | 1018 | // Defaults to private. | 
|  | 1019 | // Values are of one of the following forms: | 
|  | 1020 | // 1) numerical API level, "current", "none", or "core_platform" | 
|  | 1021 | // 2) An SDK kind with an API level: "<sdk kind>_<API level>" | 
|  | 1022 | // See build/soong/android/sdk_version.go for the complete and up to date list of SDK kinds. | 
|  | 1023 | // If the SDK kind is empty, it will be set to public | 
|  | 1024 | Sdk_version *string | 
|  | 1025 | // If not blank, set the minimum version of the sdk that the compiled artifacts will run against. | 
|  | 1026 | // Defaults to sdk_version if not set. See sdk_version for possible values. | 
| Colin Cross | 479884c | 2018-07-10 13:39:30 -0700 | [diff] [blame] | 1027 | Min_sdk_version *string | 
| Vinh Tran | ce0781f | 2022-04-13 01:30:44 +0000 | [diff] [blame] | 1028 | // List of java static libraries that the included ARR (android library prebuilts) has dependencies to. | 
| Cole Faust | b749347 | 2024-08-28 11:55:52 -0700 | [diff] [blame] | 1029 | Static_libs proptools.Configurable[[]string] | 
| Vinh Tran | ce0781f | 2022-04-13 01:30:44 +0000 | [diff] [blame] | 1030 | // List of java libraries that the included ARR (android library prebuilts) has dependencies to. | 
|  | 1031 | Libs []string | 
|  | 1032 | // If set to true, run Jetifier against .aar file. Defaults to false. | 
| Colin Cross | 1001a79 | 2019-03-21 22:21:39 -0700 | [diff] [blame] | 1033 | Jetifier *bool | 
| Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 1034 | // If true, extract JNI libs from AAR archive. These libs will be accessible to android_app modules and | 
|  | 1035 | // will be passed transitively through android_libraries to an android_app. | 
|  | 1036 | //TODO(b/241138093) evaluate whether we can have this flag default to true for Bazel conversion | 
|  | 1037 | Extract_jni *bool | 
| Colin Cross | 21ed469 | 2024-04-24 20:23:38 +0000 | [diff] [blame] | 1038 |  | 
|  | 1039 | // If set, overrides the manifest extracted from the AAR with the provided path. | 
|  | 1040 | Manifest *string `android:"path"` | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1041 | } | 
|  | 1042 |  | 
|  | 1043 | type AARImport struct { | 
|  | 1044 | android.ModuleBase | 
| Colin Cross | 48de9a4 | 2018-10-02 13:53:33 -0700 | [diff] [blame] | 1045 | android.DefaultableModuleBase | 
| Jooyung Han | acc7bbe | 2020-05-20 09:06:00 +0900 | [diff] [blame] | 1046 | android.ApexModuleBase | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1047 | prebuilt android.Prebuilt | 
|  | 1048 |  | 
| Jooyung Han | acc7bbe | 2020-05-20 09:06:00 +0900 | [diff] [blame] | 1049 | // Functionality common to Module and Import. | 
|  | 1050 | embeddableInModuleAndImport | 
|  | 1051 |  | 
| Colin Cross | 9ffaf28 | 2024-08-12 13:50:09 -0700 | [diff] [blame] | 1052 | providesTransitiveHeaderJarsForR8 | 
| Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 1053 |  | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1054 | properties AARImportProperties | 
|  | 1055 |  | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1056 | headerJarFile                      android.Path | 
|  | 1057 | implementationJarFile              android.Path | 
|  | 1058 | implementationAndResourcesJarFile  android.Path | 
|  | 1059 | proguardFlags                      android.Path | 
|  | 1060 | exportPackage                      android.Path | 
| Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame] | 1061 | transitiveAaptResourcePackagesFile android.Path | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1062 | extraAaptPackagesFile              android.Path | 
| Colin Cross | 21ed469 | 2024-04-24 20:23:38 +0000 | [diff] [blame] | 1063 | manifest                           android.Path | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1064 | assetsPackage                      android.Path | 
|  | 1065 | rTxt                               android.Path | 
|  | 1066 | rJar                               android.Path | 
| Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 1067 |  | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 1068 | resourcesNodesDepSet depset.DepSet[*resourcesNode] | 
|  | 1069 | manifestsDepSet      depset.DepSet[android.Path] | 
| Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 1070 |  | 
|  | 1071 | hideApexVariantFromMake bool | 
| Saeid Farivar Asanjan | f043696 | 2020-10-05 19:09:09 +0000 | [diff] [blame] | 1072 |  | 
| Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 1073 | aarPath     android.Path | 
|  | 1074 | jniPackages android.Paths | 
| Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 1075 |  | 
|  | 1076 | sdkVersion    android.SdkSpec | 
| Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 1077 | minSdkVersion android.ApiLevel | 
| LaMont Jones | afe7baf | 2024-01-09 22:47:39 +0000 | [diff] [blame] | 1078 |  | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1079 | usesLibrary | 
|  | 1080 | classLoaderContexts dexpreopt.ClassLoaderContextMap | 
| Saeid Farivar Asanjan | f043696 | 2020-10-05 19:09:09 +0000 | [diff] [blame] | 1081 | } | 
|  | 1082 |  | 
| Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 1083 | func (a *AARImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { | 
|  | 1084 | return android.SdkSpecFrom(ctx, String(a.properties.Sdk_version)) | 
| Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 1085 | } | 
|  | 1086 |  | 
| Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 1087 | func (a *AARImport) SystemModules() string { | 
| Paul Duffin | e25c644 | 2019-10-11 13:50:28 +0100 | [diff] [blame] | 1088 | return "" | 
|  | 1089 | } | 
|  | 1090 |  | 
| Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 1091 | func (a *AARImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { | 
| Colin Cross | 479884c | 2018-07-10 13:39:30 -0700 | [diff] [blame] | 1092 | if a.properties.Min_sdk_version != nil { | 
| Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 1093 | return android.ApiLevelFrom(ctx, *a.properties.Min_sdk_version) | 
| Colin Cross | 479884c | 2018-07-10 13:39:30 -0700 | [diff] [blame] | 1094 | } | 
| Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 1095 | return a.SdkVersion(ctx).ApiLevel | 
| Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 1096 | } | 
|  | 1097 |  | 
| Spandan Das | a26eda7 | 2023-03-02 00:56:06 +0000 | [diff] [blame] | 1098 | func (a *AARImport) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel { | 
|  | 1099 | return android.SdkSpecFrom(ctx, "").ApiLevel | 
| William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 1100 | } | 
|  | 1101 |  | 
| Spandan Das | ca70fc4 | 2023-03-01 23:38:49 +0000 | [diff] [blame] | 1102 | func (a *AARImport) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { | 
|  | 1103 | return a.SdkVersion(ctx).ApiLevel | 
| Dan Willemsen | 419290a | 2018-10-31 15:28:47 -0700 | [diff] [blame] | 1104 | } | 
|  | 1105 |  | 
| Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 1106 | func (a *AARImport) javaVersion() string { | 
|  | 1107 | return "" | 
|  | 1108 | } | 
|  | 1109 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 1110 | var _ AndroidLibraryDependency = (*AARImport)(nil) | 
|  | 1111 |  | 
|  | 1112 | func (a *AARImport) ExportPackage() android.Path { | 
|  | 1113 | return a.exportPackage | 
|  | 1114 | } | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 1115 | func (a *AARImport) ResourcesNodeDepSet() depset.DepSet[*resourcesNode] { | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1116 | return a.resourcesNodesDepSet | 
| Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 1117 | } | 
|  | 1118 |  | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 1119 | func (a *AARImport) RRODirsDepSet() depset.DepSet[rroDir] { | 
|  | 1120 | return depset.New[rroDir](depset.TOPOLOGICAL, nil, nil) | 
| Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 1121 | } | 
|  | 1122 |  | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 1123 | func (a *AARImport) ManifestsDepSet() depset.DepSet[android.Path] { | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1124 | return a.manifestsDepSet | 
| Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 1125 | } | 
|  | 1126 |  | 
| Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 1127 | // RRO enforcement is not available on aar_import since its RRO dirs are not | 
|  | 1128 | // exported. | 
|  | 1129 | func (a *AARImport) SetRROEnforcedForDependent(enforce bool) { | 
|  | 1130 | } | 
|  | 1131 |  | 
|  | 1132 | // RRO enforcement is not available on aar_import since its RRO dirs are not | 
|  | 1133 | // exported. | 
|  | 1134 | func (a *AARImport) IsRROEnforced(ctx android.BaseModuleContext) bool { | 
|  | 1135 | return false | 
|  | 1136 | } | 
|  | 1137 |  | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1138 | func (a *AARImport) Prebuilt() *android.Prebuilt { | 
|  | 1139 | return &a.prebuilt | 
|  | 1140 | } | 
|  | 1141 |  | 
|  | 1142 | func (a *AARImport) Name() string { | 
|  | 1143 | return a.prebuilt.Name(a.ModuleBase.Name()) | 
|  | 1144 | } | 
|  | 1145 |  | 
| Jiyong Park | 618922e | 2020-01-08 13:35:43 +0900 | [diff] [blame] | 1146 | func (a *AARImport) JacocoReportClassesFile() android.Path { | 
|  | 1147 | return nil | 
|  | 1148 | } | 
|  | 1149 |  | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1150 | func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) { | 
| Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 1151 | if !ctx.Config().AlwaysUsePrebuiltSdks() { | 
| Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 1152 | sdkDep := decodeSdkDep(ctx, android.SdkContext(a)) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 1153 | if sdkDep.useModule && sdkDep.frameworkResModule != "" { | 
| Colin Cross | 42d48b7 | 2018-08-29 14:10:52 -0700 | [diff] [blame] | 1154 | ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule) | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1155 | } | 
|  | 1156 | } | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 1157 |  | 
| Colin Cross | 42d48b7 | 2018-08-29 14:10:52 -0700 | [diff] [blame] | 1158 | ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...) | 
| Cole Faust | b749347 | 2024-08-28 11:55:52 -0700 | [diff] [blame] | 1159 | ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs.GetOrDefault(ctx, nil)...) | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1160 |  | 
|  | 1161 | a.usesLibrary.deps(ctx, false) | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1162 | } | 
|  | 1163 |  | 
| Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 1164 | type JniPackageInfo struct { | 
|  | 1165 | // List of zip files containing JNI libraries | 
|  | 1166 | // Zip files should have directory structure jni/<arch>/*.so | 
|  | 1167 | JniPackages android.Paths | 
|  | 1168 | } | 
|  | 1169 |  | 
| Colin Cross | bc7d76c | 2023-12-12 16:39:03 -0800 | [diff] [blame] | 1170 | var JniPackageProvider = blueprint.NewProvider[JniPackageInfo]() | 
| Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 1171 |  | 
|  | 1172 | // Unzip an AAR and extract the JNI libs for $archString. | 
|  | 1173 | var extractJNI = pctx.AndroidStaticRule("extractJNI", | 
|  | 1174 | blueprint.RuleParams{ | 
|  | 1175 | Command: `rm -rf $out $outDir && touch $out && ` + | 
|  | 1176 | `unzip -qoDD -d $outDir $in "jni/${archString}/*" && ` + | 
|  | 1177 | `jni_files=$$(find $outDir/jni -type f) && ` + | 
|  | 1178 | // print error message if there are no JNI libs for this arch | 
|  | 1179 | `[ -n "$$jni_files" ] || (echo "ERROR: no JNI libs found for arch ${archString}" && exit 1) && ` + | 
| Sam Delmerico | 80ee45c | 2023-06-22 15:36:02 -0400 | [diff] [blame] | 1180 | `${config.SoongZipCmd} -o $out -L 0 -P 'lib/${archString}' ` + | 
| Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 1181 | `-C $outDir/jni/${archString} $$(echo $$jni_files | xargs -n1 printf " -f %s")`, | 
|  | 1182 | CommandDeps: []string{"${config.SoongZipCmd}"}, | 
|  | 1183 | }, | 
|  | 1184 | "outDir", "archString") | 
|  | 1185 |  | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1186 | // 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] | 1187 | // 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] | 1188 | var unzipAAR = pctx.AndroidStaticRule("unzipAAR", | 
|  | 1189 | blueprint.RuleParams{ | 
| Dan Willemsen | 304cfec | 2019-05-28 14:49:06 -0700 | [diff] [blame] | 1190 | Command: `rm -rf $outDir && mkdir -p $outDir && ` + | 
| Colin Cross | 205e911 | 2020-08-06 13:20:17 -0700 | [diff] [blame] | 1191 | `unzip -qoDD -d $outDir $in && rm -rf $outDir/res && touch $out && ` + | 
| Michael Rosenfeld | 5ad1557 | 2021-12-03 13:25:10 -0800 | [diff] [blame] | 1192 | `${config.Zip2ZipCmd} -i $in -o $assetsPackage 'assets/**/*' && ` + | 
| Colin Cross | 205e911 | 2020-08-06 13:20:17 -0700 | [diff] [blame] | 1193 | `${config.MergeZipsCmd} $combinedClassesJar $$(ls $outDir/classes.jar 2> /dev/null) $$(ls $outDir/libs/*.jar 2> /dev/null)`, | 
| Michael Rosenfeld | 5ad1557 | 2021-12-03 13:25:10 -0800 | [diff] [blame] | 1194 | CommandDeps: []string{"${config.MergeZipsCmd}", "${config.Zip2ZipCmd}"}, | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1195 | }, | 
| Michael Rosenfeld | 5ad1557 | 2021-12-03 13:25:10 -0800 | [diff] [blame] | 1196 | "outDir", "combinedClassesJar", "assetsPackage") | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1197 |  | 
|  | 1198 | func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 1199 | if len(a.properties.Aars) != 1 { | 
|  | 1200 | ctx.PropertyErrorf("aars", "exactly one aar is required") | 
|  | 1201 | return | 
|  | 1202 | } | 
|  | 1203 |  | 
| Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 1204 | a.sdkVersion = a.SdkVersion(ctx) | 
|  | 1205 | a.minSdkVersion = a.MinSdkVersion(ctx) | 
|  | 1206 |  | 
| Colin Cross | ff694a8 | 2023-12-13 15:54:49 -0800 | [diff] [blame] | 1207 | apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider) | 
|  | 1208 | a.hideApexVariantFromMake = !apexInfo.IsForPlatform() | 
| Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 1209 |  | 
| Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 1210 | aarName := ctx.ModuleName() + ".aar" | 
| Saeid Farivar Asanjan | f043696 | 2020-10-05 19:09:09 +0000 | [diff] [blame] | 1211 | a.aarPath = android.PathForModuleSrc(ctx, a.properties.Aars[0]) | 
|  | 1212 |  | 
| Colin Cross | 1001a79 | 2019-03-21 22:21:39 -0700 | [diff] [blame] | 1213 | if Bool(a.properties.Jetifier) { | 
| Saeid Farivar Asanjan | f043696 | 2020-10-05 19:09:09 +0000 | [diff] [blame] | 1214 | inputFile := a.aarPath | 
| Colin Cross | 77965d9 | 2024-08-15 17:11:08 -0700 | [diff] [blame] | 1215 | jetifierPath := android.PathForModuleOut(ctx, "jetifier", aarName) | 
|  | 1216 | TransformJetifier(ctx, jetifierPath, inputFile) | 
|  | 1217 | a.aarPath = jetifierPath | 
| Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 1218 | } | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1219 |  | 
| Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 1220 | jarName := ctx.ModuleName() + ".jar" | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1221 | extractedAARDir := android.PathForModuleOut(ctx, "aar") | 
| Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 1222 | classpathFile := extractedAARDir.Join(ctx, jarName) | 
| Colin Cross | 21ed469 | 2024-04-24 20:23:38 +0000 | [diff] [blame] | 1223 |  | 
|  | 1224 | extractedManifest := extractedAARDir.Join(ctx, "AndroidManifest.xml") | 
|  | 1225 | providedManifest := android.OptionalPathForModuleSrc(ctx, a.properties.Manifest) | 
|  | 1226 | if providedManifest.Valid() { | 
|  | 1227 | a.manifest = providedManifest.Path() | 
|  | 1228 | } else { | 
|  | 1229 | a.manifest = extractedManifest | 
|  | 1230 | } | 
|  | 1231 |  | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1232 | rTxt := extractedAARDir.Join(ctx, "R.txt") | 
|  | 1233 | assetsPackage := android.PathForModuleOut(ctx, "assets.zip") | 
|  | 1234 | proguardFlags := extractedAARDir.Join(ctx, "proguard.txt") | 
| Colin Cross | cde5534 | 2024-03-27 14:11:51 -0700 | [diff] [blame] | 1235 | transitiveProguardFlags, transitiveUnconditionalExportedFlags := collectDepProguardSpecInfo(ctx) | 
| Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 1236 | android.SetProvider(ctx, ProguardSpecInfoProvider, ProguardSpecInfo{ | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 1237 | ProguardFlagsFiles: depset.New[android.Path]( | 
|  | 1238 | depset.POSTORDER, | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1239 | android.Paths{proguardFlags}, | 
| Colin Cross | cde5534 | 2024-03-27 14:11:51 -0700 | [diff] [blame] | 1240 | transitiveProguardFlags, | 
|  | 1241 | ), | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 1242 | UnconditionallyExportedProguardFlags: depset.New[android.Path]( | 
|  | 1243 | depset.POSTORDER, | 
| Sam Delmerico | 95d7094 | 2023-08-02 18:00:35 -0400 | [diff] [blame] | 1244 | nil, | 
| Colin Cross | cde5534 | 2024-03-27 14:11:51 -0700 | [diff] [blame] | 1245 | transitiveUnconditionalExportedFlags, | 
| Sam Delmerico | 95d7094 | 2023-08-02 18:00:35 -0400 | [diff] [blame] | 1246 | ), | 
|  | 1247 | }) | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1248 |  | 
|  | 1249 | ctx.Build(pctx, android.BuildParams{ | 
|  | 1250 | Rule:        unzipAAR, | 
| Saeid Farivar Asanjan | f043696 | 2020-10-05 19:09:09 +0000 | [diff] [blame] | 1251 | Input:       a.aarPath, | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1252 | Outputs:     android.WritablePaths{classpathFile, proguardFlags, extractedManifest, assetsPackage, rTxt}, | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1253 | Description: "unzip AAR", | 
|  | 1254 | Args: map[string]string{ | 
| Colin Cross | 205e911 | 2020-08-06 13:20:17 -0700 | [diff] [blame] | 1255 | "outDir":             extractedAARDir.String(), | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1256 | "combinedClassesJar": classpathFile.String(), | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1257 | "assetsPackage":      assetsPackage.String(), | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1258 | }, | 
|  | 1259 | }) | 
|  | 1260 |  | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1261 | a.proguardFlags = proguardFlags | 
|  | 1262 | a.assetsPackage = assetsPackage | 
|  | 1263 | a.rTxt = rTxt | 
|  | 1264 |  | 
| Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame] | 1265 | // Always set --pseudo-localize, it will be stripped out later for release | 
|  | 1266 | // builds that don't want it. | 
|  | 1267 | compileFlags := []string{"--pseudo-localize"} | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1268 | compiledResDir := android.PathForModuleOut(ctx, "flat-res") | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1269 | flata := compiledResDir.Join(ctx, "gen_res.flata") | 
| Saeid Farivar Asanjan | f043696 | 2020-10-05 19:09:09 +0000 | [diff] [blame] | 1270 | aapt2CompileZip(ctx, flata, a.aarPath, "res", compileFlags) | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1271 |  | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1272 | exportPackage := android.PathForModuleOut(ctx, "package-res.apk") | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1273 | proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options") | 
| Colin Cross | bb77d8e | 2024-02-15 14:43:47 -0800 | [diff] [blame] | 1274 | aaptRTxt := android.PathForModuleOut(ctx, "R.txt") | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1275 | extraAaptPackagesFile := android.PathForModuleOut(ctx, "extra_packages") | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1276 |  | 
|  | 1277 | var linkDeps android.Paths | 
|  | 1278 |  | 
|  | 1279 | linkFlags := []string{ | 
|  | 1280 | "--static-lib", | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1281 | "--merge-only", | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1282 | "--auto-add-overlay", | 
| Colin Cross | 7c4dc5d | 2024-02-13 14:29:45 -0800 | [diff] [blame] | 1283 | "--no-static-lib-packages", | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1284 | } | 
|  | 1285 |  | 
| Colin Cross | 10f7c4a | 2018-05-23 10:59:28 -0700 | [diff] [blame] | 1286 | linkFlags = append(linkFlags, "--manifest "+a.manifest.String()) | 
|  | 1287 | linkDeps = append(linkDeps, a.manifest) | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1288 |  | 
| Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 1289 | staticResourcesNodesDepSet, sharedResourcesNodesDepSet, staticRRODirsDepSet, staticManifestsDepSet, sharedLibs, libFlags := | 
| Jiakai Zhang | 3693708 | 2024-04-15 11:15:50 +0000 | [diff] [blame] | 1290 | aaptLibs(ctx, android.SdkContext(a), nil, nil) | 
| Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 1291 |  | 
| Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 1292 | _ = sharedResourcesNodesDepSet | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1293 | _ = staticRRODirsDepSet | 
| Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 1294 |  | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1295 | staticDeps := transitiveAarDeps(staticResourcesNodesDepSet.ToList()) | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1296 |  | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1297 | linkDeps = append(linkDeps, sharedLibs...) | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1298 | linkDeps = append(linkDeps, staticDeps.resPackages()...) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 1299 | linkFlags = append(linkFlags, libFlags...) | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1300 |  | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1301 | overlayRes := android.Paths{flata} | 
|  | 1302 |  | 
|  | 1303 | // Treat static library dependencies of static libraries as imports. | 
|  | 1304 | transitiveStaticLibs := staticDeps.resPackages() | 
|  | 1305 | linkDeps = append(linkDeps, transitiveStaticLibs...) | 
|  | 1306 | for _, staticLib := range transitiveStaticLibs { | 
|  | 1307 | linkFlags = append(linkFlags, "-I "+staticLib.String()) | 
|  | 1308 | } | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1309 |  | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1310 | transitiveAssets := android.ReverseSliceInPlace(staticDeps.assets()) | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1311 | aapt2Link(ctx, exportPackage, nil, proguardOptionsFile, aaptRTxt, | 
| Jihoon Kang | 84b2589 | 2023-12-01 22:01:06 +0000 | [diff] [blame] | 1312 | linkFlags, linkDeps, nil, overlayRes, transitiveAssets, nil, nil) | 
| Colin Cross | a6182ab | 2024-08-21 10:47:44 -0700 | [diff] [blame] | 1313 | ctx.CheckbuildFile(exportPackage) | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1314 | a.exportPackage = exportPackage | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1315 |  | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1316 | rJar := android.PathForModuleOut(ctx, "busybox/R.jar") | 
|  | 1317 | resourceProcessorBusyBoxGenerateBinaryR(ctx, a.rTxt, a.manifest, rJar, nil, true, nil, false) | 
| Colin Cross | a6182ab | 2024-08-21 10:47:44 -0700 | [diff] [blame] | 1318 | ctx.CheckbuildFile(rJar) | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1319 | a.rJar = rJar | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1320 |  | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1321 | aapt2ExtractExtraPackages(ctx, extraAaptPackagesFile, a.rJar) | 
|  | 1322 | a.extraAaptPackagesFile = extraAaptPackagesFile | 
| Colin Cross | f3b7bad | 2023-08-02 15:49:00 -0700 | [diff] [blame] | 1323 |  | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 1324 | resourcesNodesDepSetBuilder := depset.NewBuilder[*resourcesNode](depset.TOPOLOGICAL) | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1325 | resourcesNodesDepSetBuilder.Direct(&resourcesNode{ | 
|  | 1326 | resPackage: a.exportPackage, | 
|  | 1327 | manifest:   a.manifest, | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1328 | rTxt:       a.rTxt, | 
|  | 1329 | rJar:       a.rJar, | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1330 | assets:     android.OptionalPathForPath(a.assetsPackage), | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1331 |  | 
|  | 1332 | usedResourceProcessor: true, | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1333 | }) | 
|  | 1334 | resourcesNodesDepSetBuilder.Transitive(staticResourcesNodesDepSet) | 
|  | 1335 | a.resourcesNodesDepSet = resourcesNodesDepSetBuilder.Build() | 
|  | 1336 |  | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 1337 | manifestDepSetBuilder := depset.NewBuilder[android.Path](depset.TOPOLOGICAL).Direct(a.manifest) | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1338 | manifestDepSetBuilder.Transitive(staticManifestsDepSet) | 
| Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1339 | a.manifestsDepSet = manifestDepSetBuilder.Build() | 
| Michael Rosenfeld | 5ad1557 | 2021-12-03 13:25:10 -0800 | [diff] [blame] | 1340 |  | 
| Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame] | 1341 | transitiveAaptResourcePackages := staticDeps.resPackages().Strings() | 
|  | 1342 | transitiveAaptResourcePackages = slices.DeleteFunc(transitiveAaptResourcePackages, func(p string) bool { | 
|  | 1343 | return p == a.exportPackage.String() | 
|  | 1344 | }) | 
|  | 1345 | transitiveAaptResourcePackagesFile := android.PathForModuleOut(ctx, "transitive-res-packages") | 
|  | 1346 | android.WriteFileRule(ctx, transitiveAaptResourcePackagesFile, strings.Join(transitiveAaptResourcePackages, "\n")) | 
|  | 1347 | a.transitiveAaptResourcePackagesFile = transitiveAaptResourcePackagesFile | 
| Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1348 |  | 
| Colin Cross | 9ffaf28 | 2024-08-12 13:50:09 -0700 | [diff] [blame] | 1349 | a.collectTransitiveHeaderJarsForR8(ctx) | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1350 |  | 
|  | 1351 | a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx) | 
|  | 1352 |  | 
|  | 1353 | var staticJars android.Paths | 
|  | 1354 | var staticHeaderJars android.Paths | 
| Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 1355 | var staticResourceJars android.Paths | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 1356 | var transitiveStaticLibsHeaderJars []depset.DepSet[android.Path] | 
|  | 1357 | var transitiveStaticLibsImplementationJars []depset.DepSet[android.Path] | 
|  | 1358 | var transitiveStaticLibsResourceJars []depset.DepSet[android.Path] | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 1359 |  | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1360 | ctx.VisitDirectDeps(func(module android.Module) { | 
|  | 1361 | if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok { | 
|  | 1362 | tag := ctx.OtherModuleDependencyTag(module) | 
|  | 1363 | switch tag { | 
|  | 1364 | case staticLibTag: | 
|  | 1365 | staticJars = append(staticJars, dep.ImplementationJars...) | 
|  | 1366 | staticHeaderJars = append(staticHeaderJars, dep.HeaderJars...) | 
| Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 1367 | staticResourceJars = append(staticResourceJars, dep.ResourceJars...) | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 1368 | transitiveStaticLibsHeaderJars = append(transitiveStaticLibsHeaderJars, dep.TransitiveStaticLibsHeaderJars) | 
|  | 1369 | transitiveStaticLibsImplementationJars = append(transitiveStaticLibsImplementationJars, dep.TransitiveStaticLibsImplementationJars) | 
|  | 1370 | transitiveStaticLibsResourceJars = append(transitiveStaticLibsResourceJars, dep.TransitiveStaticLibsResourceJars) | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1371 | } | 
|  | 1372 | } | 
|  | 1373 | addCLCFromDep(ctx, module, a.classLoaderContexts) | 
| Jiakai Zhang | 3693708 | 2024-04-15 11:15:50 +0000 | [diff] [blame] | 1374 | addMissingOptionalUsesLibsFromDep(ctx, module, &a.usesLibrary) | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1375 | }) | 
|  | 1376 |  | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 1377 | completeStaticLibsHeaderJars := depset.New(depset.PREORDER, android.Paths{classpathFile}, transitiveStaticLibsHeaderJars) | 
|  | 1378 | completeStaticLibsImplementationJars := depset.New(depset.PREORDER, android.Paths{classpathFile}, transitiveStaticLibsImplementationJars) | 
|  | 1379 | completeStaticLibsResourceJars := depset.New(depset.PREORDER, nil, transitiveStaticLibsResourceJars) | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 1380 |  | 
| Colin Cross | 77965d9 | 2024-08-15 17:11:08 -0700 | [diff] [blame] | 1381 | var implementationJarFile android.Path | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 1382 | var combineJars android.Paths | 
|  | 1383 | if ctx.Config().UseTransitiveJarsInClasspath() { | 
|  | 1384 | combineJars = completeStaticLibsImplementationJars.ToList() | 
|  | 1385 | } else { | 
|  | 1386 | combineJars = append(android.Paths{classpathFile}, staticJars...) | 
|  | 1387 | } | 
|  | 1388 |  | 
|  | 1389 | if len(combineJars) > 1 { | 
|  | 1390 | implementationJarOutputPath := android.PathForModuleOut(ctx, "combined", jarName) | 
|  | 1391 | TransformJarsToJar(ctx, implementationJarOutputPath, "combine", combineJars, android.OptionalPath{}, false, nil, nil) | 
|  | 1392 | implementationJarFile = implementationJarOutputPath | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1393 | } else { | 
| Colin Cross | 28ac2ff | 2024-04-02 12:21:34 -0700 | [diff] [blame] | 1394 | implementationJarFile = classpathFile | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1395 | } | 
|  | 1396 |  | 
| Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 1397 | var resourceJarFile android.Path | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 1398 | var resourceJars android.Paths | 
|  | 1399 | if ctx.Config().UseTransitiveJarsInClasspath() { | 
|  | 1400 | resourceJars = completeStaticLibsResourceJars.ToList() | 
|  | 1401 | } else { | 
|  | 1402 | resourceJars = staticResourceJars | 
|  | 1403 | } | 
|  | 1404 | if len(resourceJars) > 1 { | 
| Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 1405 | combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName) | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 1406 | TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{}, | 
| Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 1407 | false, nil, nil) | 
|  | 1408 | resourceJarFile = combinedJar | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 1409 | } else if len(resourceJars) == 1 { | 
|  | 1410 | resourceJarFile = resourceJars[0] | 
| Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 1411 | } | 
|  | 1412 |  | 
|  | 1413 | // merge implementation jar with resources if necessary | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 1414 | var implementationAndResourcesJars android.Paths | 
|  | 1415 | if ctx.Config().UseTransitiveJarsInClasspath() { | 
|  | 1416 | implementationAndResourcesJars = append(slices.Clone(resourceJars), combineJars...) | 
|  | 1417 | } else { | 
|  | 1418 | implementationAndResourcesJars = android.PathsIfNonNil(resourceJarFile, implementationJarFile) | 
|  | 1419 | } | 
|  | 1420 | var implementationAndResourcesJar android.Path | 
|  | 1421 | if len(implementationAndResourcesJars) > 1 { | 
| Colin Cross | 77965d9 | 2024-08-15 17:11:08 -0700 | [diff] [blame] | 1422 | combinedJar := android.PathForModuleOut(ctx, "withres", jarName) | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 1423 | TransformJarsToJar(ctx, combinedJar, "for resources", implementationAndResourcesJars, android.OptionalPath{}, | 
| Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 1424 | false, nil, nil) | 
|  | 1425 | implementationAndResourcesJar = combinedJar | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 1426 | } else { | 
|  | 1427 | implementationAndResourcesJar = implementationAndResourcesJars[0] | 
| Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 1428 | } | 
|  | 1429 |  | 
|  | 1430 | a.implementationJarFile = implementationJarFile | 
| Colin Cross | 28ac2ff | 2024-04-02 12:21:34 -0700 | [diff] [blame] | 1431 | // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource | 
| Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 1432 | a.implementationAndResourcesJarFile = implementationAndResourcesJar.WithoutRel() | 
| Colin Cross | 28ac2ff | 2024-04-02 12:21:34 -0700 | [diff] [blame] | 1433 |  | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 1434 | var headerJars android.Paths | 
|  | 1435 | if ctx.Config().UseTransitiveJarsInClasspath() { | 
|  | 1436 | headerJars = completeStaticLibsHeaderJars.ToList() | 
|  | 1437 | } else { | 
|  | 1438 | headerJars = append(android.Paths{classpathFile}, staticHeaderJars...) | 
|  | 1439 | } | 
|  | 1440 | if len(headerJars) > 1 { | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1441 | headerJarFile := android.PathForModuleOut(ctx, "turbine-combined", jarName) | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 1442 | TransformJarsToJar(ctx, headerJarFile, "combine header jars", headerJars, android.OptionalPath{}, false, nil, nil) | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 1443 | a.headerJarFile = headerJarFile | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1444 | } else { | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 1445 | a.headerJarFile = headerJars[0] | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1446 | } | 
|  | 1447 |  | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 1448 | if ctx.Config().UseTransitiveJarsInClasspath() { | 
|  | 1449 | ctx.CheckbuildFile(classpathFile) | 
|  | 1450 | } else { | 
|  | 1451 | ctx.CheckbuildFile(a.headerJarFile) | 
|  | 1452 | ctx.CheckbuildFile(a.implementationJarFile) | 
|  | 1453 | } | 
| Colin Cross | a6182ab | 2024-08-21 10:47:44 -0700 | [diff] [blame] | 1454 |  | 
| Colin Cross | 7727c7f | 2024-07-18 15:36:32 -0700 | [diff] [blame] | 1455 | android.SetProvider(ctx, JavaInfoProvider, &JavaInfo{ | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 1456 | HeaderJars:                             android.PathsIfNonNil(a.headerJarFile), | 
|  | 1457 | LocalHeaderJars:                        android.PathsIfNonNil(classpathFile), | 
|  | 1458 | TransitiveStaticLibsHeaderJars:         completeStaticLibsHeaderJars, | 
|  | 1459 | TransitiveStaticLibsImplementationJars: completeStaticLibsImplementationJars, | 
|  | 1460 | TransitiveStaticLibsResourceJars:       completeStaticLibsResourceJars, | 
|  | 1461 | ResourceJars:                           android.PathsIfNonNil(resourceJarFile), | 
|  | 1462 | TransitiveLibsHeaderJarsForR8:          a.transitiveLibsHeaderJarsForR8, | 
|  | 1463 | TransitiveStaticLibsHeaderJarsForR8:    a.transitiveStaticLibsHeaderJarsForR8, | 
|  | 1464 | ImplementationAndResourcesJars:         android.PathsIfNonNil(a.implementationAndResourcesJarFile), | 
|  | 1465 | ImplementationJars:                     android.PathsIfNonNil(a.implementationJarFile), | 
|  | 1466 | StubsLinkType:                          Implementation, | 
| Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 1467 | // TransitiveAconfigFiles: // TODO(b/289117800): LOCAL_ACONFIG_FILES for prebuilts | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 1468 | }) | 
| Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 1469 |  | 
|  | 1470 | if proptools.Bool(a.properties.Extract_jni) { | 
|  | 1471 | for _, t := range ctx.MultiTargets() { | 
|  | 1472 | arch := t.Arch.Abi[0] | 
|  | 1473 | path := android.PathForModuleOut(ctx, arch+"_jni.zip") | 
|  | 1474 | a.jniPackages = append(a.jniPackages, path) | 
|  | 1475 |  | 
|  | 1476 | outDir := android.PathForModuleOut(ctx, "aarForJni") | 
|  | 1477 | aarPath := android.PathForModuleSrc(ctx, a.properties.Aars[0]) | 
|  | 1478 | ctx.Build(pctx, android.BuildParams{ | 
|  | 1479 | Rule:        extractJNI, | 
|  | 1480 | Input:       aarPath, | 
|  | 1481 | Outputs:     android.WritablePaths{path}, | 
|  | 1482 | Description: "extract JNI from AAR", | 
|  | 1483 | Args: map[string]string{ | 
|  | 1484 | "outDir":     outDir.String(), | 
|  | 1485 | "archString": arch, | 
|  | 1486 | }, | 
|  | 1487 | }) | 
|  | 1488 | } | 
| Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 1489 | } | 
| Colin Cross | e8eeec9 | 2023-12-14 14:50:05 -0800 | [diff] [blame] | 1490 |  | 
| Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 1491 | android.SetProvider(ctx, JniPackageProvider, JniPackageInfo{ | 
| Colin Cross | e8eeec9 | 2023-12-14 14:50:05 -0800 | [diff] [blame] | 1492 | JniPackages: a.jniPackages, | 
|  | 1493 | }) | 
| mrziwang | 68786d8 | 2024-07-09 10:41:55 -0700 | [diff] [blame] | 1494 |  | 
|  | 1495 | ctx.SetOutputFiles([]android.Path{a.implementationAndResourcesJarFile}, "") | 
|  | 1496 | ctx.SetOutputFiles([]android.Path{a.aarPath}, ".aar") | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 1497 | } | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1498 |  | 
|  | 1499 | func (a *AARImport) HeaderJars() android.Paths { | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1500 | return android.Paths{a.headerJarFile} | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1501 | } | 
|  | 1502 |  | 
| Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 1503 | func (a *AARImport) ImplementationAndResourcesJars() android.Paths { | 
| Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 1504 | return android.Paths{a.implementationAndResourcesJarFile} | 
| Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 1505 | } | 
|  | 1506 |  | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1507 | func (a *AARImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath { | 
|  | 1508 | return OptionalDexJarPath{} | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 1509 | } | 
|  | 1510 |  | 
| Ulya Trafimovich | 9f3052c | 2020-06-09 14:31:19 +0100 | [diff] [blame] | 1511 | func (a *AARImport) DexJarInstallPath() android.Path { | 
|  | 1512 | return nil | 
|  | 1513 | } | 
|  | 1514 |  | 
| Ulya Trafimovich | b23d28c | 2020-10-08 12:53:58 +0100 | [diff] [blame] | 1515 | func (a *AARImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap { | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1516 | return a.classLoaderContexts | 
| Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1517 | } | 
|  | 1518 |  | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1519 | var _ UsesLibraryDependency = (*AARImport)(nil) | 
|  | 1520 |  | 
| Jiyong Park | 45bf82e | 2020-12-15 22:29:02 +0900 | [diff] [blame] | 1521 | var _ android.ApexModule = (*AARImport)(nil) | 
|  | 1522 |  | 
|  | 1523 | // Implements android.ApexModule | 
| Jooyung Han | acc7bbe | 2020-05-20 09:06:00 +0900 | [diff] [blame] | 1524 | func (a *AARImport) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool { | 
|  | 1525 | return a.depIsInSameApex(ctx, dep) | 
|  | 1526 | } | 
|  | 1527 |  | 
| Jiyong Park | 45bf82e | 2020-12-15 22:29:02 +0900 | [diff] [blame] | 1528 | // Implements android.ApexModule | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1529 | func (a *AARImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext, | 
| Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 1530 | sdkVersion android.ApiLevel) error { | 
| Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 1531 | return nil | 
|  | 1532 | } | 
|  | 1533 |  | 
| Sam Delmerico | af8bb70 | 2022-07-25 15:39:32 -0400 | [diff] [blame] | 1534 | var _ android.PrebuiltInterface = (*AARImport)(nil) | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1535 |  | 
| Jiakai Zhang | f98da19 | 2024-04-15 11:15:41 +0000 | [diff] [blame] | 1536 | func (a *AARImport) UsesLibrary() *usesLibrary { | 
|  | 1537 | return &a.usesLibrary | 
|  | 1538 | } | 
|  | 1539 |  | 
|  | 1540 | var _ ModuleWithUsesLibrary = (*AARImport)(nil) | 
|  | 1541 |  | 
| Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 1542 | // android_library_import imports an `.aar` file into the build graph as if it was built with android_library. | 
|  | 1543 | // | 
|  | 1544 | // This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of | 
|  | 1545 | // an android_app module. | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1546 | func AARImportFactory() android.Module { | 
|  | 1547 | module := &AARImport{} | 
|  | 1548 |  | 
| Colin Cross | 9055e21 | 2024-03-23 04:43:41 +0000 | [diff] [blame] | 1549 | module.AddProperties( | 
|  | 1550 | &module.properties, | 
|  | 1551 | &module.usesLibrary.usesLibraryProperties, | 
|  | 1552 | ) | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1553 |  | 
|  | 1554 | android.InitPrebuiltModule(module, &module.properties.Aars) | 
| Jooyung Han | acc7bbe | 2020-05-20 09:06:00 +0900 | [diff] [blame] | 1555 | android.InitApexModule(module) | 
| Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 1556 | InitJavaModuleMultiTargets(module, android.DeviceSupported) | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1557 | return module | 
|  | 1558 | } | 
| Spandan Das | b08bf77 | 2024-08-07 21:55:42 +0000 | [diff] [blame] | 1559 |  | 
| Cole Faust | b36d31d | 2024-08-27 16:04:28 -0700 | [diff] [blame] | 1560 | func (a *AARImport) IDEInfo(ctx android.BaseModuleContext, dpInfo *android.IdeInfo) { | 
| Spandan Das | b08bf77 | 2024-08-07 21:55:42 +0000 | [diff] [blame] | 1561 | dpInfo.Jars = append(dpInfo.Jars, a.headerJarFile.String(), a.rJar.String()) | 
|  | 1562 | } |