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