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