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