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