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