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