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