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