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