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