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