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