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