Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 1 | // Copyright 2015 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 | // This file contains the module types for compiling Android apps. |
| 18 | |
| 19 | import ( |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 20 | "path/filepath" |
| 21 | "strings" |
| 22 | |
| 23 | "github.com/google/blueprint" |
Colin Cross | 76b5f0c | 2017-08-29 16:02:06 -0700 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 25 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 26 | "android/soong/android" |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | // AAR prebuilts |
| 30 | // AndroidManifest.xml merging |
| 31 | // package splits |
| 32 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 33 | type androidAppProperties struct { |
| 34 | // path to a certificate, or the name of a certificate in the default |
| 35 | // certificate directory, or blank to use the default product certificate |
| 36 | Certificate string |
| 37 | |
| 38 | // paths to extra certificates to sign the apk with |
| 39 | Additional_certificates []string |
| 40 | |
| 41 | // If set, create package-export.apk, which other packages can |
| 42 | // use to get PRODUCT-agnostic resource data like IDs and type definitions. |
| 43 | Export_package_resources bool |
| 44 | |
| 45 | // flags passed to aapt when creating the apk |
| 46 | Aaptflags []string |
| 47 | |
| 48 | // list of resource labels to generate individual resource packages |
| 49 | Package_splits []string |
| 50 | |
| 51 | // list of directories relative to the Blueprints file containing assets. |
| 52 | // Defaults to "assets" |
| 53 | Asset_dirs []string |
| 54 | |
| 55 | // list of directories relative to the Blueprints file containing |
Colin Cross | 86a63ff | 2017-09-27 17:33:10 -0700 | [diff] [blame^] | 56 | // Android resources |
| 57 | Resource_dirs []string |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 60 | type AndroidApp struct { |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 61 | Module |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 62 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 63 | appProperties androidAppProperties |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 64 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 65 | aaptJavaFileList android.Path |
| 66 | exportPackage android.Path |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 69 | func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 70 | a.Module.deps(ctx) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 71 | |
Colin Cross | 76b5f0c | 2017-08-29 16:02:06 -0700 | [diff] [blame] | 72 | if !proptools.Bool(a.properties.No_standard_libs) { |
Colin Cross | 540eff8 | 2017-06-22 17:01:52 -0700 | [diff] [blame] | 73 | switch a.deviceProperties.Sdk_version { // TODO: Res_sdk_version? |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 74 | case "current", "system_current", "": |
Colin Cross | be1da47 | 2017-07-07 15:59:46 -0700 | [diff] [blame] | 75 | ctx.AddDependency(ctx.Module(), frameworkResTag, "framework-res") |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 76 | default: |
| 77 | // We'll already have a dependency on an sdk prebuilt android.jar |
| 78 | } |
| 79 | } |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 82 | func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 83 | aaptFlags, aaptDeps, hasResources := a.aaptFlags(ctx) |
| 84 | |
| 85 | if hasResources { |
| 86 | // First generate R.java so we can build the .class files |
| 87 | aaptRJavaFlags := append([]string(nil), aaptFlags...) |
| 88 | |
| 89 | publicResourcesFile, proguardOptionsFile, aaptJavaFileList := |
| 90 | CreateResourceJavaFiles(ctx, aaptRJavaFlags, aaptDeps) |
| 91 | a.aaptJavaFileList = aaptJavaFileList |
| 92 | a.ExtraSrcLists = append(a.ExtraSrcLists, aaptJavaFileList) |
| 93 | |
| 94 | if a.appProperties.Export_package_resources { |
| 95 | aaptPackageFlags := append([]string(nil), aaptFlags...) |
| 96 | var hasProduct bool |
| 97 | for _, f := range aaptPackageFlags { |
| 98 | if strings.HasPrefix(f, "--product") { |
| 99 | hasProduct = true |
| 100 | break |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if !hasProduct { |
| 105 | aaptPackageFlags = append(aaptPackageFlags, |
| 106 | "--product "+ctx.AConfig().ProductAaptCharacteristics()) |
| 107 | } |
| 108 | a.exportPackage = CreateExportPackage(ctx, aaptPackageFlags, aaptDeps) |
| 109 | ctx.CheckbuildFile(a.exportPackage) |
| 110 | } |
| 111 | ctx.CheckbuildFile(publicResourcesFile) |
| 112 | ctx.CheckbuildFile(proguardOptionsFile) |
| 113 | ctx.CheckbuildFile(aaptJavaFileList) |
| 114 | } |
| 115 | |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 116 | // apps manifests are handled by aapt, don't let Module see them |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 117 | a.properties.Manifest = nil |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 118 | |
| 119 | //if !ctx.ContainsProperty("proguard.enabled") { |
| 120 | // a.properties.Proguard.Enabled = true |
| 121 | //} |
| 122 | |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 123 | a.Module.compile(ctx) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 124 | |
| 125 | aaptPackageFlags := append([]string(nil), aaptFlags...) |
| 126 | var hasProduct bool |
| 127 | for _, f := range aaptPackageFlags { |
| 128 | if strings.HasPrefix(f, "--product") { |
| 129 | hasProduct = true |
| 130 | break |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if !hasProduct { |
| 135 | aaptPackageFlags = append(aaptPackageFlags, |
| 136 | "--product "+ctx.AConfig().ProductAaptCharacteristics()) |
| 137 | } |
| 138 | |
| 139 | certificate := a.appProperties.Certificate |
| 140 | if certificate == "" { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 141 | certificate = ctx.AConfig().DefaultAppCertificate(ctx).String() |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 142 | } else if dir, _ := filepath.Split(certificate); dir == "" { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 143 | certificate = filepath.Join(ctx.AConfig().DefaultAppCertificateDir(ctx).String(), certificate) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 144 | } else { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 145 | certificate = filepath.Join(android.PathForSource(ctx).String(), certificate) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | certificates := []string{certificate} |
| 149 | for _, c := range a.appProperties.Additional_certificates { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 150 | certificates = append(certificates, filepath.Join(android.PathForSource(ctx).String(), c)) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | a.outputFile = CreateAppPackage(ctx, aaptPackageFlags, a.outputFile, certificates) |
Colin Cross | 5c51792 | 2017-08-31 12:29:17 -0700 | [diff] [blame] | 154 | ctx.InstallFile(android.PathForModuleInstall(ctx, "app"), ctx.ModuleName()+".apk", a.outputFile) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | var aaptIgnoreFilenames = []string{ |
| 158 | ".svn", |
| 159 | ".git", |
| 160 | ".ds_store", |
| 161 | "*.scc", |
| 162 | ".*", |
| 163 | "CVS", |
| 164 | "thumbs.db", |
| 165 | "picasa.ini", |
| 166 | "*~", |
| 167 | } |
| 168 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 169 | func (a *AndroidApp) aaptFlags(ctx android.ModuleContext) ([]string, android.Paths, bool) { |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 170 | aaptFlags := a.appProperties.Aaptflags |
| 171 | hasVersionCode := false |
| 172 | hasVersionName := false |
| 173 | for _, f := range aaptFlags { |
| 174 | if strings.HasPrefix(f, "--version-code") { |
| 175 | hasVersionCode = true |
| 176 | } else if strings.HasPrefix(f, "--version-name") { |
| 177 | hasVersionName = true |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if true /* is not a test */ { |
| 182 | aaptFlags = append(aaptFlags, "-z") |
| 183 | } |
| 184 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 185 | assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.appProperties.Asset_dirs, "assets") |
Colin Cross | 86a63ff | 2017-09-27 17:33:10 -0700 | [diff] [blame^] | 186 | resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.appProperties.Resource_dirs, "res") |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 187 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 188 | var overlayResourceDirs android.Paths |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 189 | // For every resource directory, check if there is an overlay directory with the same path. |
| 190 | // If found, it will be prepended to the list of resource directories. |
| 191 | for _, overlayDir := range ctx.AConfig().ResourceOverlays() { |
| 192 | for _, resourceDir := range resourceDirs { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 193 | overlay := overlayDir.OverlayPath(ctx, resourceDir) |
| 194 | if overlay.Valid() { |
| 195 | overlayResourceDirs = append(overlayResourceDirs, overlay.Path()) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if len(overlayResourceDirs) > 0 { |
| 201 | resourceDirs = append(overlayResourceDirs, resourceDirs...) |
| 202 | } |
| 203 | |
| 204 | // aapt needs to rerun if any files are added or modified in the assets or resource directories, |
| 205 | // use glob to create a filelist. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 206 | var aaptDeps android.Paths |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 207 | var hasResources bool |
| 208 | for _, d := range resourceDirs { |
Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 209 | newDeps := ctx.Glob(filepath.Join(d.String(), "**/*"), aaptIgnoreFilenames) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 210 | aaptDeps = append(aaptDeps, newDeps...) |
| 211 | if len(newDeps) > 0 { |
| 212 | hasResources = true |
| 213 | } |
| 214 | } |
| 215 | for _, d := range assetDirs { |
Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 216 | newDeps := ctx.Glob(filepath.Join(d.String(), "**/*"), aaptIgnoreFilenames) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 217 | aaptDeps = append(aaptDeps, newDeps...) |
| 218 | } |
| 219 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 220 | var manifestFile string |
| 221 | if a.properties.Manifest == nil { |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 222 | manifestFile = "AndroidManifest.xml" |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 223 | } else { |
| 224 | manifestFile = *a.properties.Manifest |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 227 | manifestPath := android.PathForModuleSrc(ctx, manifestFile) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 228 | aaptDeps = append(aaptDeps, manifestPath) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 229 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 230 | aaptFlags = append(aaptFlags, "-M "+manifestPath.String()) |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 231 | aaptFlags = append(aaptFlags, android.JoinWithPrefix(assetDirs.Strings(), "-A ")) |
| 232 | aaptFlags = append(aaptFlags, android.JoinWithPrefix(resourceDirs.Strings(), "-S ")) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 233 | |
| 234 | ctx.VisitDirectDeps(func(module blueprint.Module) { |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 235 | var depFiles android.Paths |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 236 | if javaDep, ok := module.(Dependency); ok { |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 237 | if ctx.OtherModuleName(module) == "framework-res" { |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 238 | depFiles = android.Paths{javaDep.(*AndroidApp).exportPackage} |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 241 | |
| 242 | for _, dep := range depFiles { |
| 243 | aaptFlags = append(aaptFlags, "-I "+dep.String()) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 244 | } |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 245 | aaptDeps = append(aaptDeps, depFiles...) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 246 | }) |
| 247 | |
Colin Cross | 540eff8 | 2017-06-22 17:01:52 -0700 | [diff] [blame] | 248 | sdkVersion := a.deviceProperties.Sdk_version |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 249 | if sdkVersion == "" { |
| 250 | sdkVersion = ctx.AConfig().PlatformSdkVersion() |
| 251 | } |
| 252 | |
| 253 | aaptFlags = append(aaptFlags, "--min-sdk-version "+sdkVersion) |
| 254 | aaptFlags = append(aaptFlags, "--target-sdk-version "+sdkVersion) |
| 255 | |
| 256 | if !hasVersionCode { |
| 257 | aaptFlags = append(aaptFlags, "--version-code "+ctx.AConfig().PlatformSdkVersion()) |
| 258 | } |
| 259 | |
| 260 | if !hasVersionName { |
| 261 | aaptFlags = append(aaptFlags, |
| 262 | "--version-name "+ctx.AConfig().PlatformVersion()+"-"+ctx.AConfig().BuildNumber()) |
| 263 | } |
| 264 | |
| 265 | // TODO: LOCAL_PACKAGE_OVERRIDES |
| 266 | // $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \ |
| 267 | |
| 268 | // TODO: LOCAL_INSTRUMENTATION_FOR |
| 269 | // $(addprefix --rename-instrumentation-target-package , $(PRIVATE_MANIFEST_INSTRUMENTATION_FOR)) |
| 270 | |
| 271 | return aaptFlags, aaptDeps, hasResources |
| 272 | } |
| 273 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 274 | func AndroidAppFactory() android.Module { |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 275 | module := &AndroidApp{} |
| 276 | |
Colin Cross | 540eff8 | 2017-06-22 17:01:52 -0700 | [diff] [blame] | 277 | module.deviceProperties.Dex = true |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 278 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 279 | module.AddProperties( |
Colin Cross | 540eff8 | 2017-06-22 17:01:52 -0700 | [diff] [blame] | 280 | &module.Module.properties, |
| 281 | &module.Module.deviceProperties, |
| 282 | &module.appProperties) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 283 | |
| 284 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 285 | return module |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 286 | } |