| 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 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 28 | func init() { | 
|  | 29 | android.RegisterPreSingletonType("overlay", OverlaySingletonFactory) | 
| Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 30 | android.RegisterModuleType("android_app", AndroidAppFactory) | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 31 | } | 
|  | 32 |  | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 33 | // AAR prebuilts | 
|  | 34 | // AndroidManifest.xml merging | 
|  | 35 | // package splits | 
|  | 36 |  | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 37 | type androidAppProperties struct { | 
|  | 38 | // path to a certificate, or the name of a certificate in the default | 
|  | 39 | // certificate directory, or blank to use the default product certificate | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 40 | Certificate *string | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 41 |  | 
|  | 42 | // paths to extra certificates to sign the apk with | 
|  | 43 | Additional_certificates []string | 
|  | 44 |  | 
|  | 45 | // If set, create package-export.apk, which other packages can | 
|  | 46 | // use to get PRODUCT-agnostic resource data like IDs and type definitions. | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 47 | Export_package_resources *bool | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 48 |  | 
|  | 49 | // flags passed to aapt when creating the apk | 
|  | 50 | Aaptflags []string | 
|  | 51 |  | 
|  | 52 | // list of resource labels to generate individual resource packages | 
|  | 53 | Package_splits []string | 
|  | 54 |  | 
|  | 55 | // list of directories relative to the Blueprints file containing assets. | 
|  | 56 | // Defaults to "assets" | 
|  | 57 | Asset_dirs []string | 
|  | 58 |  | 
|  | 59 | // list of directories relative to the Blueprints file containing | 
| Colin Cross | 86a63ff | 2017-09-27 17:33:10 -0700 | [diff] [blame] | 60 | // Android resources | 
|  | 61 | Resource_dirs []string | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 62 |  | 
|  | 63 | Instrumentation_for *string | 
| Colin Cross | 1605606 | 2017-12-13 22:46:28 -0800 | [diff] [blame] | 64 |  | 
|  | 65 | // Specifies that this app should be installed to the priv-app directory, | 
|  | 66 | // where the system will grant it additional privileges not available to | 
|  | 67 | // normal apps. | 
|  | 68 | Privileged *bool | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 69 | } | 
|  | 70 |  | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 71 | type AndroidApp struct { | 
| Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 72 | Module | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 73 |  | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 74 | appProperties androidAppProperties | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 75 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 76 | aaptSrcJar    android.Path | 
|  | 77 | exportPackage android.Path | 
| Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 78 | rroDirs       android.Paths | 
|  | 79 | manifestPath  android.Path | 
| Colin Cross | e1731a5 | 2017-12-14 11:22:55 -0800 | [diff] [blame] | 80 | certificate   certificate | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | type certificate struct { | 
|  | 84 | pem, key android.Path | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
| Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 87 | func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) { | 
|  | 88 | a.Module.deps(ctx) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 89 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 90 | if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) { | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 91 | switch String(a.deviceProperties.Sdk_version) { // TODO: Res_sdk_version? | 
| Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 92 | case "current", "system_current", "test_current", "": | 
| Colin Cross | be1da47 | 2017-07-07 15:59:46 -0700 | [diff] [blame] | 93 | ctx.AddDependency(ctx.Module(), frameworkResTag, "framework-res") | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 94 | default: | 
|  | 95 | // We'll already have a dependency on an sdk prebuilt android.jar | 
|  | 96 | } | 
|  | 97 | } | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
| Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 100 | func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 101 | linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, manifestPath := a.aapt2Flags(ctx) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 102 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 103 | packageRes := android.PathForModuleOut(ctx, "package-res.apk") | 
|  | 104 | srcJar := android.PathForModuleGen(ctx, "R.jar") | 
|  | 105 | proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options") | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 106 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 107 | var compiledRes, compiledOverlay android.Paths | 
|  | 108 | for _, dir := range resDirs { | 
|  | 109 | compiledRes = append(compiledRes, aapt2Compile(ctx, dir.dir, dir.files).Paths()...) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 110 | } | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 111 | for _, dir := range overlayDirs { | 
|  | 112 | compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files).Paths()...) | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, | 
|  | 116 | linkFlags, linkDeps, compiledRes, compiledOverlay) | 
|  | 117 |  | 
|  | 118 | a.exportPackage = packageRes | 
|  | 119 | a.aaptSrcJar = srcJar | 
|  | 120 |  | 
|  | 121 | ctx.CheckbuildFile(proguardOptionsFile) | 
|  | 122 | ctx.CheckbuildFile(a.exportPackage) | 
|  | 123 | ctx.CheckbuildFile(a.aaptSrcJar) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 124 |  | 
| Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 125 | // apps manifests are handled by aapt, don't let Module see them | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 126 | a.properties.Manifest = nil | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 127 |  | 
| Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 128 | if String(a.appProperties.Instrumentation_for) == "" { | 
|  | 129 | a.properties.Instrument = true | 
|  | 130 | } | 
|  | 131 |  | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 132 | a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, | 
|  | 133 | proguardOptionsFile) | 
|  | 134 |  | 
| Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 135 | if ctx.ModuleName() != "framework-res" { | 
|  | 136 | a.Module.compile(ctx, a.aaptSrcJar) | 
|  | 137 | } | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 138 |  | 
| Colin Cross | e1731a5 | 2017-12-14 11:22:55 -0800 | [diff] [blame] | 139 | c := String(a.appProperties.Certificate) | 
|  | 140 | switch { | 
|  | 141 | case c == "": | 
|  | 142 | pem, key := ctx.Config().DefaultAppCertificate(ctx) | 
|  | 143 | a.certificate = certificate{pem, key} | 
|  | 144 | case strings.ContainsRune(c, '/'): | 
|  | 145 | a.certificate = certificate{ | 
|  | 146 | android.PathForSource(ctx, c+".x509.pem"), | 
|  | 147 | android.PathForSource(ctx, c+".pk8"), | 
|  | 148 | } | 
|  | 149 | default: | 
|  | 150 | defaultDir := ctx.Config().DefaultAppCertificateDir(ctx) | 
|  | 151 | a.certificate = certificate{ | 
|  | 152 | defaultDir.Join(ctx, c+".x509.pem"), | 
|  | 153 | defaultDir.Join(ctx, c+".pk8"), | 
|  | 154 | } | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 155 | } | 
|  | 156 |  | 
| Colin Cross | e1731a5 | 2017-12-14 11:22:55 -0800 | [diff] [blame] | 157 | certificates := []certificate{a.certificate} | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 158 | for _, c := range a.appProperties.Additional_certificates { | 
| Colin Cross | e1731a5 | 2017-12-14 11:22:55 -0800 | [diff] [blame] | 159 | certificates = append(certificates, certificate{ | 
|  | 160 | android.PathForSource(ctx, c+".x509.pem"), | 
|  | 161 | android.PathForSource(ctx, c+".pk8"), | 
|  | 162 | }) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 163 | } | 
|  | 164 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 165 | packageFile := android.PathForModuleOut(ctx, "package.apk") | 
|  | 166 |  | 
|  | 167 | CreateAppPackage(ctx, packageFile, a.exportPackage, a.outputFile, certificates) | 
|  | 168 |  | 
|  | 169 | a.outputFile = packageFile | 
| Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 170 | a.rroDirs = rroDirs | 
|  | 171 | a.manifestPath = manifestPath | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 172 |  | 
| Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 173 | if ctx.ModuleName() == "framework-res" { | 
|  | 174 | // framework-res.apk is installed as system/framework/framework-res.apk | 
|  | 175 | ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".apk", a.outputFile) | 
| Colin Cross | 1605606 | 2017-12-13 22:46:28 -0800 | [diff] [blame] | 176 | } else if Bool(a.appProperties.Privileged) { | 
|  | 177 | ctx.InstallFile(android.PathForModuleInstall(ctx, "priv-app"), ctx.ModuleName()+".apk", a.outputFile) | 
| Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 178 | } else { | 
|  | 179 | ctx.InstallFile(android.PathForModuleInstall(ctx, "app"), ctx.ModuleName()+".apk", a.outputFile) | 
|  | 180 | } | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 181 | } | 
|  | 182 |  | 
|  | 183 | var aaptIgnoreFilenames = []string{ | 
|  | 184 | ".svn", | 
|  | 185 | ".git", | 
|  | 186 | ".ds_store", | 
|  | 187 | "*.scc", | 
|  | 188 | ".*", | 
|  | 189 | "CVS", | 
|  | 190 | "thumbs.db", | 
|  | 191 | "picasa.ini", | 
|  | 192 | "*~", | 
|  | 193 | } | 
|  | 194 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 195 | type globbedResourceDir struct { | 
|  | 196 | dir   android.Path | 
|  | 197 | files android.Paths | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 | func (a *AndroidApp) aapt2Flags(ctx android.ModuleContext) (flags []string, deps android.Paths, | 
| Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 201 | resDirs, overlayDirs []globbedResourceDir, rroDirs android.Paths, manifestPath android.Path) { | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 202 |  | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 203 | hasVersionCode := false | 
|  | 204 | hasVersionName := false | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 205 | hasProduct := false | 
|  | 206 | for _, f := range a.appProperties.Aaptflags { | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 207 | if strings.HasPrefix(f, "--version-code") { | 
|  | 208 | hasVersionCode = true | 
|  | 209 | } else if strings.HasPrefix(f, "--version-name") { | 
|  | 210 | hasVersionName = true | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 211 | } else if strings.HasPrefix(f, "--product") { | 
|  | 212 | hasProduct = true | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 213 | } | 
|  | 214 | } | 
|  | 215 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 216 | var linkFlags []string | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 217 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 218 | // Flags specified in Android.bp | 
|  | 219 | linkFlags = append(linkFlags, a.appProperties.Aaptflags...) | 
|  | 220 |  | 
|  | 221 | linkFlags = append(linkFlags, "--no-static-lib-packages") | 
|  | 222 |  | 
|  | 223 | // Find implicit or explicit asset and resource dirs | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 224 | assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.appProperties.Asset_dirs, "assets") | 
| Colin Cross | 86a63ff | 2017-09-27 17:33:10 -0700 | [diff] [blame] | 225 | resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.appProperties.Resource_dirs, "res") | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 226 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 227 | var linkDeps android.Paths | 
|  | 228 |  | 
|  | 229 | // Glob directories into lists of paths | 
|  | 230 | for _, dir := range resourceDirs { | 
|  | 231 | resDirs = append(resDirs, globbedResourceDir{ | 
|  | 232 | dir:   dir, | 
|  | 233 | files: resourceGlob(ctx, dir), | 
|  | 234 | }) | 
| Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 235 | resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, dir) | 
|  | 236 | overlayDirs = append(overlayDirs, resOverlayDirs...) | 
|  | 237 | rroDirs = append(rroDirs, resRRODirs...) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 238 | } | 
|  | 239 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 240 | var assetFiles android.Paths | 
|  | 241 | for _, dir := range assetDirs { | 
|  | 242 | assetFiles = append(assetFiles, resourceGlob(ctx, dir)...) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 243 | } | 
|  | 244 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 245 | // App manifest file | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 246 | var manifestFile string | 
|  | 247 | if a.properties.Manifest == nil { | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 248 | manifestFile = "AndroidManifest.xml" | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 249 | } else { | 
|  | 250 | manifestFile = *a.properties.Manifest | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 251 | } | 
|  | 252 |  | 
| Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 253 | manifestPath = android.PathForModuleSrc(ctx, manifestFile) | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 254 | linkFlags = append(linkFlags, "--manifest "+manifestPath.String()) | 
|  | 255 | linkDeps = append(linkDeps, manifestPath) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 256 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 257 | linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirs.Strings(), "-A ")) | 
|  | 258 | linkDeps = append(linkDeps, assetFiles...) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 259 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 260 | // Include dirs | 
| Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 261 | ctx.VisitDirectDeps(func(module android.Module) { | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 262 | var depFiles android.Paths | 
| Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 263 | if javaDep, ok := module.(Dependency); ok { | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 264 | // TODO: shared android libraries | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 265 | if ctx.OtherModuleName(module) == "framework-res" { | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 266 | depFiles = android.Paths{javaDep.(*AndroidApp).exportPackage} | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 267 | } | 
|  | 268 | } | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 269 |  | 
|  | 270 | for _, dep := range depFiles { | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 271 | linkFlags = append(linkFlags, "-I "+dep.String()) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 272 | } | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 273 | linkDeps = append(linkDeps, depFiles...) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 274 | }) | 
|  | 275 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 276 | // SDK version flags | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 277 | sdkVersion := String(a.deviceProperties.Sdk_version) | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 278 | switch sdkVersion { | 
|  | 279 | case "", "current", "system_current", "test_current": | 
| Colin Cross | 42f3a76 | 2017-12-02 16:14:26 -0800 | [diff] [blame] | 280 | sdkVersion = proptools.NinjaEscape([]string{ctx.Config().AppsDefaultVersionName()})[0] | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 281 | } | 
|  | 282 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 283 | linkFlags = append(linkFlags, "--min-sdk-version "+sdkVersion) | 
|  | 284 | linkFlags = append(linkFlags, "--target-sdk-version "+sdkVersion) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 285 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 286 | // Product characteristics | 
| Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 287 | if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 { | 
|  | 288 | linkFlags = append(linkFlags, "--product", ctx.Config().ProductAAPTCharacteristics()) | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 289 | } | 
|  | 290 |  | 
|  | 291 | // Product AAPT config | 
| Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 292 | for _, aaptConfig := range ctx.Config().ProductAAPTConfig() { | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 293 | linkFlags = append(linkFlags, "-c", aaptConfig) | 
|  | 294 | } | 
|  | 295 |  | 
|  | 296 | // Product AAPT preferred config | 
| Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 297 | if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 { | 
|  | 298 | linkFlags = append(linkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig()) | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 299 | } | 
|  | 300 |  | 
|  | 301 | // Version code | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 302 | if !hasVersionCode { | 
| Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 303 | linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion()) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 304 | } | 
|  | 305 |  | 
|  | 306 | if !hasVersionName { | 
| Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 307 | versionName := proptools.NinjaEscape([]string{ctx.Config().AppsDefaultVersionName()})[0] | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 308 | linkFlags = append(linkFlags, "--version-name ", versionName) | 
|  | 309 | } | 
|  | 310 |  | 
|  | 311 | if String(a.appProperties.Instrumentation_for) != "" { | 
|  | 312 | linkFlags = append(linkFlags, | 
|  | 313 | "--rename-instrumentation-target-package", | 
|  | 314 | String(a.appProperties.Instrumentation_for)) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 315 | } | 
|  | 316 |  | 
|  | 317 | // TODO: LOCAL_PACKAGE_OVERRIDES | 
|  | 318 | //    $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \ | 
|  | 319 |  | 
| Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 320 | return linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, manifestPath | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 321 | } | 
|  | 322 |  | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 323 | func AndroidAppFactory() android.Module { | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 324 | module := &AndroidApp{} | 
|  | 325 |  | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 326 | module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true) | 
|  | 327 | module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true) | 
|  | 328 |  | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 329 | module.AddProperties( | 
| Colin Cross | 540eff8 | 2017-06-22 17:01:52 -0700 | [diff] [blame] | 330 | &module.Module.properties, | 
|  | 331 | &module.Module.deviceProperties, | 
|  | 332 | &module.appProperties) | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 333 |  | 
|  | 334 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) | 
|  | 335 | return module | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 336 | } | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 337 |  | 
|  | 338 | func resourceGlob(ctx android.ModuleContext, dir android.Path) android.Paths { | 
|  | 339 | var ret android.Paths | 
|  | 340 | files := ctx.Glob(filepath.Join(dir.String(), "**/*"), aaptIgnoreFilenames) | 
|  | 341 | for _, f := range files { | 
|  | 342 | if isDir, err := ctx.Fs().IsDir(f.String()); err != nil { | 
|  | 343 | ctx.ModuleErrorf("error in IsDir(%s): %s", f.String(), err.Error()) | 
|  | 344 | return nil | 
|  | 345 | } else if !isDir { | 
|  | 346 | ret = append(ret, f) | 
|  | 347 | } | 
|  | 348 | } | 
|  | 349 | return ret | 
|  | 350 | } | 
|  | 351 |  | 
|  | 352 | type overlayGlobResult struct { | 
|  | 353 | dir   string | 
|  | 354 | paths android.DirectorySortedPaths | 
| Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 355 |  | 
|  | 356 | // Set to true of the product has selected that values in this overlay should not be moved to | 
|  | 357 | // Runtime Resource Overlay (RRO) packages. | 
|  | 358 | excludeFromRRO bool | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 359 | } | 
|  | 360 |  | 
|  | 361 | const overlayDataKey = "overlayDataKey" | 
|  | 362 |  | 
| Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 363 | func overlayResourceGlob(ctx android.ModuleContext, dir android.Path) (res []globbedResourceDir, | 
|  | 364 | rroDirs android.Paths) { | 
|  | 365 |  | 
| Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 366 | overlayData := ctx.Config().Get(overlayDataKey).([]overlayGlobResult) | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 367 |  | 
| Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 368 | // Runtime resource overlays (RRO) may be turned on by the product config for some modules | 
|  | 369 | rroEnabled := false | 
|  | 370 | enforceRROTargets := ctx.Config().ProductVariables.EnforceRROTargets | 
|  | 371 | if enforceRROTargets != nil { | 
|  | 372 | if len(*enforceRROTargets) == 1 && (*enforceRROTargets)[0] == "*" { | 
|  | 373 | rroEnabled = true | 
|  | 374 | } else if inList(ctx.ModuleName(), *enforceRROTargets) { | 
|  | 375 | rroEnabled = true | 
|  | 376 | } | 
|  | 377 | } | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 378 |  | 
|  | 379 | for _, data := range overlayData { | 
|  | 380 | files := data.paths.PathsInDirectory(filepath.Join(data.dir, dir.String())) | 
|  | 381 | if len(files) > 0 { | 
| Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 382 | overlayModuleDir := android.PathForSource(ctx, data.dir, dir.String()) | 
|  | 383 | // If enforce RRO is enabled for this module and this overlay is not in the | 
|  | 384 | // exclusion list, ignore the overlay.  The list of ignored overlays will be | 
|  | 385 | // passed to Make to be turned into an RRO package. | 
|  | 386 | if rroEnabled && !data.excludeFromRRO { | 
|  | 387 | rroDirs = append(rroDirs, overlayModuleDir) | 
|  | 388 | } else { | 
|  | 389 | res = append(res, globbedResourceDir{ | 
|  | 390 | dir:   overlayModuleDir, | 
|  | 391 | files: files, | 
|  | 392 | }) | 
|  | 393 | } | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 394 | } | 
|  | 395 | } | 
|  | 396 |  | 
| Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 397 | return res, rroDirs | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 398 | } | 
|  | 399 |  | 
|  | 400 | func OverlaySingletonFactory() android.Singleton { | 
|  | 401 | return overlaySingleton{} | 
|  | 402 | } | 
|  | 403 |  | 
|  | 404 | type overlaySingleton struct{} | 
|  | 405 |  | 
|  | 406 | func (overlaySingleton) GenerateBuildActions(ctx android.SingletonContext) { | 
| Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 407 |  | 
|  | 408 | // Specific overlays may be excluded from Runtime Resource Overlays by the product config | 
|  | 409 | var rroExcludedOverlays []string | 
|  | 410 | if ctx.Config().ProductVariables.EnforceRROExcludedOverlays != nil { | 
|  | 411 | rroExcludedOverlays = *ctx.Config().ProductVariables.EnforceRROExcludedOverlays | 
|  | 412 | } | 
|  | 413 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 414 | var overlayData []overlayGlobResult | 
| Colin Cross | 68a7023 | 2018-01-04 14:50:13 -0800 | [diff] [blame] | 415 | overlayDirs := ctx.Config().ResourceOverlays() | 
|  | 416 | for i := range overlayDirs { | 
|  | 417 | // Iterate backwards through the list of overlay directories so that the later, lower-priority | 
|  | 418 | // directories in the list show up earlier in the command line to aapt2. | 
|  | 419 | overlay := overlayDirs[len(overlayDirs)-1-i] | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 420 | var result overlayGlobResult | 
|  | 421 | result.dir = overlay | 
| Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 422 |  | 
|  | 423 | // Mark overlays that will not have Runtime Resource Overlays enforced on them | 
|  | 424 | for _, exclude := range rroExcludedOverlays { | 
|  | 425 | if strings.HasPrefix(overlay, exclude) { | 
|  | 426 | result.excludeFromRRO = true | 
|  | 427 | } | 
|  | 428 | } | 
|  | 429 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 430 | files, err := ctx.GlobWithDeps(filepath.Join(overlay, "**/*"), aaptIgnoreFilenames) | 
|  | 431 | if err != nil { | 
|  | 432 | ctx.Errorf("failed to glob resource dir %q: %s", overlay, err.Error()) | 
|  | 433 | continue | 
|  | 434 | } | 
|  | 435 | var paths android.Paths | 
|  | 436 | for _, f := range files { | 
|  | 437 | if isDir, err := ctx.Fs().IsDir(f); err != nil { | 
|  | 438 | ctx.Errorf("error in IsDir(%s): %s", f, err.Error()) | 
|  | 439 | return | 
|  | 440 | } else if !isDir { | 
|  | 441 | paths = append(paths, android.PathForSource(ctx, f)) | 
|  | 442 | } | 
|  | 443 | } | 
|  | 444 | result.paths = android.PathsToDirectorySortedPaths(paths) | 
|  | 445 | overlayData = append(overlayData, result) | 
|  | 446 | } | 
|  | 447 |  | 
| Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 448 | ctx.Config().Once(overlayDataKey, func() interface{} { | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 449 | return overlayData | 
|  | 450 | }) | 
|  | 451 | } |