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