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