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