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 ( |
| 18 | "android/soong/android" |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 19 | "strings" |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 20 | |
| 21 | "github.com/google/blueprint" |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 23 | ) |
| 24 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 25 | type AndroidLibraryDependency interface { |
| 26 | Dependency |
| 27 | ExportPackage() android.Path |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 28 | ExportedProguardFlagFiles() android.Paths |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 29 | ExportedRRODirs() android.Paths |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 30 | ExportedStaticPackages() android.Paths |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 31 | ExportedManifest() android.Path |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | func init() { |
| 35 | android.RegisterModuleType("android_library_import", AARImportFactory) |
| 36 | android.RegisterModuleType("android_library", AndroidLibraryFactory) |
| 37 | } |
| 38 | |
| 39 | // |
| 40 | // AAR (android library) |
| 41 | // |
| 42 | |
| 43 | type androidLibraryProperties struct { |
| 44 | BuildAAR bool `blueprint:"mutated"` |
| 45 | } |
| 46 | |
| 47 | type aaptProperties struct { |
| 48 | // flags passed to aapt when creating the apk |
| 49 | Aaptflags []string |
| 50 | |
Dan Willemsen | 72be590 | 2018-10-24 20:24:57 -0700 | [diff] [blame] | 51 | // include all resource configurations, not just the product-configured |
| 52 | // ones. |
| 53 | Aapt_include_all_resources *bool |
| 54 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 55 | // list of directories relative to the Blueprints file containing assets. |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 56 | // Defaults to ["assets"] if a directory called assets exists. Set to [] |
| 57 | // to disable the default. |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 58 | Asset_dirs []string |
| 59 | |
| 60 | // list of directories relative to the Blueprints file containing |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 61 | // Android resources. Defaults to ["res"] if a directory called res exists. |
| 62 | // Set to [] to disable the default. |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 63 | Resource_dirs []string |
| 64 | |
| 65 | // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml". |
| 66 | Manifest *string |
| 67 | } |
| 68 | |
| 69 | type aapt struct { |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 70 | aaptSrcJar android.Path |
| 71 | exportPackage android.Path |
| 72 | manifestPath android.Path |
| 73 | proguardOptionsFile android.Path |
| 74 | rroDirs android.Paths |
| 75 | rTxt android.Path |
| 76 | extraAaptPackagesFile android.Path |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 77 | isLibrary bool |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 78 | uncompressedJNI bool |
Colin Cross | 46abdad | 2019-02-07 13:07:08 -0800 | [diff] [blame] | 79 | useEmbeddedDex bool |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 80 | |
| 81 | aaptProperties aaptProperties |
| 82 | } |
| 83 | |
| 84 | func (a *aapt) ExportPackage() android.Path { |
| 85 | return a.exportPackage |
| 86 | } |
| 87 | |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 88 | func (a *aapt) ExportedRRODirs() android.Paths { |
| 89 | return a.rroDirs |
| 90 | } |
| 91 | |
| 92 | func (a *aapt) ExportedManifest() android.Path { |
| 93 | return a.manifestPath |
| 94 | } |
| 95 | |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 96 | func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext sdkContext, manifestPath android.Path) (flags []string, |
| 97 | deps android.Paths, resDirs, overlayDirs []globbedResourceDir, rroDirs android.Paths) { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 98 | |
| 99 | hasVersionCode := false |
| 100 | hasVersionName := false |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 101 | for _, f := range a.aaptProperties.Aaptflags { |
| 102 | if strings.HasPrefix(f, "--version-code") { |
| 103 | hasVersionCode = true |
| 104 | } else if strings.HasPrefix(f, "--version-name") { |
| 105 | hasVersionName = true |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | var linkFlags []string |
| 110 | |
| 111 | // Flags specified in Android.bp |
| 112 | linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...) |
| 113 | |
| 114 | linkFlags = append(linkFlags, "--no-static-lib-packages") |
| 115 | |
| 116 | // Find implicit or explicit asset and resource dirs |
| 117 | assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets") |
| 118 | resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res") |
| 119 | |
| 120 | var linkDeps android.Paths |
| 121 | |
| 122 | // Glob directories into lists of paths |
| 123 | for _, dir := range resourceDirs { |
| 124 | resDirs = append(resDirs, globbedResourceDir{ |
| 125 | dir: dir, |
| 126 | files: androidResourceGlob(ctx, dir), |
| 127 | }) |
| 128 | resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, dir) |
| 129 | overlayDirs = append(overlayDirs, resOverlayDirs...) |
| 130 | rroDirs = append(rroDirs, resRRODirs...) |
| 131 | } |
| 132 | |
| 133 | var assetFiles android.Paths |
| 134 | for _, dir := range assetDirs { |
| 135 | assetFiles = append(assetFiles, androidResourceGlob(ctx, dir)...) |
| 136 | } |
| 137 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 138 | linkFlags = append(linkFlags, "--manifest "+manifestPath.String()) |
| 139 | linkDeps = append(linkDeps, manifestPath) |
| 140 | |
| 141 | linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirs.Strings(), "-A ")) |
| 142 | linkDeps = append(linkDeps, assetFiles...) |
| 143 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 144 | // SDK version flags |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 145 | minSdkVersion := sdkVersionOrDefault(ctx, sdkContext.minSdkVersion()) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 146 | |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 147 | linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion) |
| 148 | linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 149 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 150 | // Version code |
| 151 | if !hasVersionCode { |
| 152 | linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion()) |
| 153 | } |
| 154 | |
| 155 | if !hasVersionName { |
Colin Cross | 402d5e0 | 2018-04-25 14:54:06 -0700 | [diff] [blame] | 156 | var versionName string |
| 157 | if ctx.ModuleName() == "framework-res" { |
| 158 | // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the |
| 159 | // 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] | 160 | // if it contains the build number. Use the PlatformVersionName instead. |
| 161 | versionName = ctx.Config().PlatformVersionName() |
Colin Cross | 402d5e0 | 2018-04-25 14:54:06 -0700 | [diff] [blame] | 162 | } else { |
| 163 | versionName = ctx.Config().AppsDefaultVersionName() |
| 164 | } |
| 165 | versionName = proptools.NinjaEscape([]string{versionName})[0] |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 166 | linkFlags = append(linkFlags, "--version-name ", versionName) |
| 167 | } |
| 168 | |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 169 | return linkFlags, linkDeps, resDirs, overlayDirs, rroDirs |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 172 | func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkContext sdkContext) { |
Colin Cross | 42308aa | 2018-11-14 21:44:17 -0800 | [diff] [blame] | 173 | sdkDep := decodeSdkDep(ctx, sdkContext) |
| 174 | if sdkDep.frameworkResModule != "" { |
| 175 | ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 179 | func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, extraLinkFlags ...string) { |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 180 | transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags := aaptLibs(ctx, sdkContext) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 181 | |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 182 | // App manifest file |
| 183 | manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml") |
| 184 | manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile) |
| 185 | |
Colin Cross | 46abdad | 2019-02-07 13:07:08 -0800 | [diff] [blame] | 186 | manifestPath := manifestMerger(ctx, manifestSrcPath, sdkContext, staticLibManifests, a.isLibrary, |
| 187 | a.uncompressedJNI, a.useEmbeddedDex) |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 188 | |
| 189 | linkFlags, linkDeps, resDirs, overlayDirs, rroDirs := a.aapt2Flags(ctx, sdkContext, manifestPath) |
| 190 | |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 191 | rroDirs = append(rroDirs, staticRRODirs...) |
Anton Hansson | 129b9ce | 2019-02-07 12:21:42 +0000 | [diff] [blame] | 192 | // TODO(b/124035856): stop de-duping when there are no more dupe resource dirs. |
| 193 | rroDirs = android.FirstUniquePaths(rroDirs) |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 194 | |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 195 | linkFlags = append(linkFlags, libFlags...) |
| 196 | linkDeps = append(linkDeps, libDeps...) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 197 | linkFlags = append(linkFlags, extraLinkFlags...) |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 198 | if a.isLibrary { |
| 199 | linkFlags = append(linkFlags, "--static-lib") |
| 200 | } |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 201 | |
| 202 | packageRes := android.PathForModuleOut(ctx, "package-res.apk") |
| 203 | srcJar := android.PathForModuleGen(ctx, "R.jar") |
| 204 | proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options") |
| 205 | rTxt := android.PathForModuleOut(ctx, "R.txt") |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 206 | // This file isn't used by Soong, but is generated for exporting |
| 207 | extraPackages := android.PathForModuleOut(ctx, "extra_packages") |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 208 | |
Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 209 | var compiledResDirs []android.Paths |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 210 | for _, dir := range resDirs { |
Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 211 | compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files).Paths()) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 212 | } |
Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 213 | |
| 214 | var compiledRes, compiledOverlay android.Paths |
| 215 | |
| 216 | compiledOverlay = append(compiledOverlay, transitiveStaticLibs...) |
| 217 | |
| 218 | if a.isLibrary { |
| 219 | // For a static library we treat all the resources equally with no overlay. |
| 220 | for _, compiledResDir := range compiledResDirs { |
| 221 | compiledRes = append(compiledRes, compiledResDir...) |
| 222 | } |
| 223 | } else if len(transitiveStaticLibs) > 0 { |
| 224 | // If we are using static android libraries, every source file becomes an overlay. |
| 225 | // This is to emulate old AAPT behavior which simulated library support. |
| 226 | for _, compiledResDir := range compiledResDirs { |
| 227 | compiledOverlay = append(compiledOverlay, compiledResDir...) |
| 228 | } |
| 229 | } else if len(compiledResDirs) > 0 { |
| 230 | // Without static libraries, the first directory is our directory, which can then be |
| 231 | // overlaid by the rest. |
| 232 | compiledRes = append(compiledRes, compiledResDirs[0]...) |
| 233 | for _, compiledResDir := range compiledResDirs[1:] { |
| 234 | compiledOverlay = append(compiledOverlay, compiledResDir...) |
| 235 | } |
| 236 | } |
| 237 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 238 | for _, dir := range overlayDirs { |
| 239 | compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files).Paths()...) |
| 240 | } |
| 241 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 242 | aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages, |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 243 | linkFlags, linkDeps, compiledRes, compiledOverlay) |
| 244 | |
| 245 | a.aaptSrcJar = srcJar |
| 246 | a.exportPackage = packageRes |
| 247 | a.manifestPath = manifestPath |
| 248 | a.proguardOptionsFile = proguardOptionsFile |
| 249 | a.rroDirs = rroDirs |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 250 | a.extraAaptPackagesFile = extraPackages |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 251 | a.rTxt = rTxt |
| 252 | } |
| 253 | |
| 254 | // aaptLibs collects libraries from dependencies and sdk_version and converts them into paths |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 255 | func aaptLibs(ctx android.ModuleContext, sdkContext sdkContext) (transitiveStaticLibs, staticLibManifests, |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 256 | staticRRODirs, deps android.Paths, flags []string) { |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 257 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 258 | var sharedLibs android.Paths |
| 259 | |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 260 | sdkDep := decodeSdkDep(ctx, sdkContext) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 261 | if sdkDep.useFiles { |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 262 | sharedLibs = append(sharedLibs, sdkDep.jars...) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | ctx.VisitDirectDeps(func(module android.Module) { |
| 266 | var exportPackage android.Path |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 267 | aarDep, _ := module.(AndroidLibraryDependency) |
| 268 | if aarDep != nil { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 269 | exportPackage = aarDep.ExportPackage() |
| 270 | } |
| 271 | |
| 272 | switch ctx.OtherModuleDependencyTag(module) { |
Colin Cross | 4b964c0 | 2018-10-15 16:18:06 -0700 | [diff] [blame] | 273 | case instrumentationForTag: |
| 274 | // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2. |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 275 | case libTag, frameworkResTag: |
| 276 | if exportPackage != nil { |
| 277 | sharedLibs = append(sharedLibs, exportPackage) |
| 278 | } |
| 279 | case staticLibTag: |
| 280 | if exportPackage != nil { |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 281 | transitiveStaticLibs = append(transitiveStaticLibs, exportPackage) |
| 282 | transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...) |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 283 | staticLibManifests = append(staticLibManifests, aarDep.ExportedManifest()) |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 284 | staticRRODirs = append(staticRRODirs, aarDep.ExportedRRODirs()...) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | }) |
| 288 | |
| 289 | deps = append(deps, sharedLibs...) |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 290 | deps = append(deps, transitiveStaticLibs...) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 291 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 292 | if len(transitiveStaticLibs) > 0 { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 293 | flags = append(flags, "--auto-add-overlay") |
| 294 | } |
| 295 | |
| 296 | for _, sharedLib := range sharedLibs { |
| 297 | flags = append(flags, "-I "+sharedLib.String()) |
| 298 | } |
| 299 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 300 | transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs) |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 301 | staticRRODirs = android.FirstUniquePaths(staticRRODirs) |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 302 | |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 303 | return transitiveStaticLibs, staticLibManifests, staticRRODirs, deps, flags |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | type AndroidLibrary struct { |
| 307 | Library |
| 308 | aapt |
| 309 | |
| 310 | androidLibraryProperties androidLibraryProperties |
| 311 | |
| 312 | aarFile android.WritablePath |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 313 | |
| 314 | exportedProguardFlagFiles android.Paths |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 315 | exportedStaticPackages android.Paths |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths { |
| 319 | return a.exportedProguardFlagFiles |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 322 | func (a *AndroidLibrary) ExportedStaticPackages() android.Paths { |
| 323 | return a.exportedStaticPackages |
| 324 | } |
| 325 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 326 | var _ AndroidLibraryDependency = (*AndroidLibrary)(nil) |
| 327 | |
| 328 | func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 329 | a.Module.deps(ctx) |
| 330 | if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) { |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 331 | a.aapt.deps(ctx, sdkContext(a)) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 336 | a.aapt.isLibrary = true |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 337 | a.aapt.buildActions(ctx, sdkContext(a)) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 338 | |
| 339 | ctx.CheckbuildFile(a.proguardOptionsFile) |
| 340 | ctx.CheckbuildFile(a.exportPackage) |
| 341 | ctx.CheckbuildFile(a.aaptSrcJar) |
| 342 | |
| 343 | // apps manifests are handled by aapt, don't let Module see them |
| 344 | a.properties.Manifest = nil |
| 345 | |
| 346 | a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, |
| 347 | a.proguardOptionsFile) |
| 348 | |
| 349 | a.Module.compile(ctx, a.aaptSrcJar) |
| 350 | |
Colin Cross | f57c578 | 2019-01-25 13:20:38 -0800 | [diff] [blame] | 351 | a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar") |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 352 | var res android.Paths |
| 353 | if a.androidLibraryProperties.BuildAAR { |
| 354 | BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res) |
| 355 | ctx.CheckbuildFile(a.aarFile) |
| 356 | } |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 357 | |
| 358 | ctx.VisitDirectDeps(func(m android.Module) { |
| 359 | if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag { |
| 360 | a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...) |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 361 | a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage()) |
| 362 | a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...) |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 363 | } |
| 364 | }) |
| 365 | |
| 366 | a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles) |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 367 | a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame^] | 370 | // android_library builds and links sources into a `.jar` file for the device along with Android resources. |
| 371 | // |
| 372 | // An android_library has a single variant that produces a `.jar` file containing `.class` files that were |
| 373 | // compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled |
| 374 | // with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of |
| 375 | // an android_app module. |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 376 | func AndroidLibraryFactory() android.Module { |
| 377 | module := &AndroidLibrary{} |
| 378 | |
| 379 | module.AddProperties( |
| 380 | &module.Module.properties, |
| 381 | &module.Module.deviceProperties, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 382 | &module.Module.dexpreoptProperties, |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 383 | &module.Module.protoProperties, |
| 384 | &module.aaptProperties, |
| 385 | &module.androidLibraryProperties) |
| 386 | |
| 387 | module.androidLibraryProperties.BuildAAR = true |
| 388 | |
Colin Cross | 48de9a4 | 2018-10-02 13:53:33 -0700 | [diff] [blame] | 389 | InitJavaModule(module, android.DeviceSupported) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 390 | return module |
| 391 | } |
| 392 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 393 | // |
| 394 | // AAR (android library) prebuilts |
| 395 | // |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 396 | |
| 397 | type AARImportProperties struct { |
| 398 | Aars []string |
| 399 | |
Colin Cross | 479884c | 2018-07-10 13:39:30 -0700 | [diff] [blame] | 400 | Sdk_version *string |
| 401 | Min_sdk_version *string |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 402 | |
| 403 | Static_libs []string |
| 404 | Libs []string |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 405 | |
| 406 | // if set to true, run Jetifier against .aar file. Defaults to false. |
| 407 | Jetifier_enabled *bool |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | type AARImport struct { |
| 411 | android.ModuleBase |
Colin Cross | 48de9a4 | 2018-10-02 13:53:33 -0700 | [diff] [blame] | 412 | android.DefaultableModuleBase |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 413 | prebuilt android.Prebuilt |
| 414 | |
| 415 | properties AARImportProperties |
| 416 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 417 | classpathFile android.WritablePath |
| 418 | proguardFlags android.WritablePath |
| 419 | exportPackage android.WritablePath |
| 420 | extraAaptPackagesFile android.WritablePath |
Colin Cross | 10f7c4a | 2018-05-23 10:59:28 -0700 | [diff] [blame] | 421 | manifest android.WritablePath |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 422 | |
| 423 | exportedStaticPackages android.Paths |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 426 | func (a *AARImport) sdkVersion() string { |
| 427 | return String(a.properties.Sdk_version) |
| 428 | } |
| 429 | |
| 430 | func (a *AARImport) minSdkVersion() string { |
Colin Cross | 479884c | 2018-07-10 13:39:30 -0700 | [diff] [blame] | 431 | if a.properties.Min_sdk_version != nil { |
| 432 | return *a.properties.Min_sdk_version |
| 433 | } |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 434 | return a.sdkVersion() |
| 435 | } |
| 436 | |
Dan Willemsen | 419290a | 2018-10-31 15:28:47 -0700 | [diff] [blame] | 437 | func (a *AARImport) targetSdkVersion() string { |
| 438 | return a.sdkVersion() |
| 439 | } |
| 440 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 441 | var _ AndroidLibraryDependency = (*AARImport)(nil) |
| 442 | |
| 443 | func (a *AARImport) ExportPackage() android.Path { |
| 444 | return a.exportPackage |
| 445 | } |
| 446 | |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 447 | func (a *AARImport) ExportedProguardFlagFiles() android.Paths { |
| 448 | return android.Paths{a.proguardFlags} |
| 449 | } |
| 450 | |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 451 | func (a *AARImport) ExportedRRODirs() android.Paths { |
| 452 | return nil |
| 453 | } |
| 454 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 455 | func (a *AARImport) ExportedStaticPackages() android.Paths { |
| 456 | return a.exportedStaticPackages |
| 457 | } |
| 458 | |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 459 | func (a *AARImport) ExportedManifest() android.Path { |
| 460 | return a.manifest |
| 461 | } |
| 462 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 463 | func (a *AARImport) Prebuilt() *android.Prebuilt { |
| 464 | return &a.prebuilt |
| 465 | } |
| 466 | |
| 467 | func (a *AARImport) Name() string { |
| 468 | return a.prebuilt.Name(a.ModuleBase.Name()) |
| 469 | } |
| 470 | |
| 471 | func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 1f367bf | 2018-12-18 22:46:24 -0800 | [diff] [blame] | 472 | if !ctx.Config().UnbundledBuildPrebuiltSdks() { |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 473 | sdkDep := decodeSdkDep(ctx, sdkContext(a)) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 474 | if sdkDep.useModule && sdkDep.frameworkResModule != "" { |
Colin Cross | 42d48b7 | 2018-08-29 14:10:52 -0700 | [diff] [blame] | 475 | ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 476 | } |
| 477 | } |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 478 | |
Colin Cross | 42d48b7 | 2018-08-29 14:10:52 -0700 | [diff] [blame] | 479 | ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...) |
| 480 | ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | // Unzip an AAR into its constituent files and directories. Any files in Outputs that don't exist in the AAR will be |
| 484 | // touched to create an empty file, and any directories in $expectedDirs will be created. |
| 485 | var unzipAAR = pctx.AndroidStaticRule("unzipAAR", |
| 486 | blueprint.RuleParams{ |
| 487 | Command: `rm -rf $outDir && mkdir -p $outDir $expectedDirs && ` + |
| 488 | `unzip -qo -d $outDir $in && touch $out`, |
| 489 | }, |
| 490 | "expectedDirs", "outDir") |
| 491 | |
| 492 | func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 493 | if len(a.properties.Aars) != 1 { |
| 494 | ctx.PropertyErrorf("aars", "exactly one aar is required") |
| 495 | return |
| 496 | } |
| 497 | |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 498 | aarName := ctx.ModuleName() + ".aar" |
| 499 | var aar android.Path |
| 500 | aar = android.PathForModuleSrc(ctx, a.properties.Aars[0]) |
| 501 | if Bool(a.properties.Jetifier_enabled) { |
| 502 | inputFile := aar |
| 503 | aar = android.PathForModuleOut(ctx, "jetifier", aarName) |
| 504 | TransformJetifier(ctx, aar.(android.WritablePath), inputFile) |
| 505 | } |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 506 | |
| 507 | extractedAARDir := android.PathForModuleOut(ctx, "aar") |
| 508 | extractedResDir := extractedAARDir.Join(ctx, "res") |
| 509 | a.classpathFile = extractedAARDir.Join(ctx, "classes.jar") |
| 510 | a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt") |
Colin Cross | 10f7c4a | 2018-05-23 10:59:28 -0700 | [diff] [blame] | 511 | a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml") |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 512 | |
| 513 | ctx.Build(pctx, android.BuildParams{ |
| 514 | Rule: unzipAAR, |
| 515 | Input: aar, |
Colin Cross | 10f7c4a | 2018-05-23 10:59:28 -0700 | [diff] [blame] | 516 | Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest}, |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 517 | Description: "unzip AAR", |
| 518 | Args: map[string]string{ |
| 519 | "expectedDirs": extractedResDir.String(), |
| 520 | "outDir": extractedAARDir.String(), |
| 521 | }, |
| 522 | }) |
| 523 | |
| 524 | compiledResDir := android.PathForModuleOut(ctx, "flat-res") |
| 525 | aaptCompileDeps := android.Paths{a.classpathFile} |
| 526 | aaptCompileDirs := android.Paths{extractedResDir} |
| 527 | flata := compiledResDir.Join(ctx, "gen_res.flata") |
| 528 | aapt2CompileDirs(ctx, flata, aaptCompileDirs, aaptCompileDeps) |
| 529 | |
| 530 | a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk") |
| 531 | srcJar := android.PathForModuleGen(ctx, "R.jar") |
| 532 | proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options") |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 533 | rTxt := android.PathForModuleOut(ctx, "R.txt") |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 534 | a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages") |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 535 | |
| 536 | var linkDeps android.Paths |
| 537 | |
| 538 | linkFlags := []string{ |
| 539 | "--static-lib", |
| 540 | "--no-static-lib-packages", |
| 541 | "--auto-add-overlay", |
| 542 | } |
| 543 | |
Colin Cross | 10f7c4a | 2018-05-23 10:59:28 -0700 | [diff] [blame] | 544 | linkFlags = append(linkFlags, "--manifest "+a.manifest.String()) |
| 545 | linkDeps = append(linkDeps, a.manifest) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 546 | |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 547 | transitiveStaticLibs, staticLibManifests, staticRRODirs, libDeps, libFlags := aaptLibs(ctx, sdkContext(a)) |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 548 | |
| 549 | _ = staticLibManifests |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 550 | _ = staticRRODirs |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 551 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 552 | linkDeps = append(linkDeps, libDeps...) |
| 553 | linkFlags = append(linkFlags, libFlags...) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 554 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 555 | overlayRes := append(android.Paths{flata}, transitiveStaticLibs...) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 556 | |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 557 | aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile, |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 558 | linkFlags, linkDeps, nil, overlayRes) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | var _ Dependency = (*AARImport)(nil) |
| 562 | |
| 563 | func (a *AARImport) HeaderJars() android.Paths { |
| 564 | return android.Paths{a.classpathFile} |
| 565 | } |
| 566 | |
| 567 | func (a *AARImport) ImplementationJars() android.Paths { |
| 568 | return android.Paths{a.classpathFile} |
| 569 | } |
| 570 | |
Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 571 | func (a *AARImport) ResourceJars() android.Paths { |
| 572 | return nil |
| 573 | } |
| 574 | |
| 575 | func (a *AARImport) ImplementationAndResourcesJars() android.Paths { |
| 576 | return android.Paths{a.classpathFile} |
| 577 | } |
| 578 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 579 | func (a *AARImport) DexJar() android.Path { |
| 580 | return nil |
| 581 | } |
| 582 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 583 | func (a *AARImport) AidlIncludeDirs() android.Paths { |
| 584 | return nil |
| 585 | } |
| 586 | |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 587 | func (a *AARImport) ExportedSdkLibs() []string { |
| 588 | return nil |
| 589 | } |
| 590 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 591 | var _ android.PrebuiltInterface = (*Import)(nil) |
| 592 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame^] | 593 | // android_library_import imports an `.aar` file into the build graph as if it was built with android_library. |
| 594 | // |
| 595 | // This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of |
| 596 | // an android_app module. |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 597 | func AARImportFactory() android.Module { |
| 598 | module := &AARImport{} |
| 599 | |
| 600 | module.AddProperties(&module.properties) |
| 601 | |
| 602 | android.InitPrebuiltModule(module, &module.properties.Aars) |
Colin Cross | 48de9a4 | 2018-10-02 13:53:33 -0700 | [diff] [blame] | 603 | InitJavaModule(module, android.DeviceSupported) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 604 | return module |
| 605 | } |