| 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 | "strings" | 
|  | 21 |  | 
| Colin Cross | 76b5f0c | 2017-08-29 16:02:06 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 23 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 24 | "android/soong/android" | 
| Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 25 | "android/soong/tradefed" | 
| 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() { | 
| Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 29 | android.RegisterModuleType("android_app", AndroidAppFactory) | 
| Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 30 | android.RegisterModuleType("android_test", AndroidTestFactory) | 
| 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 | // AndroidManifest.xml merging | 
|  | 34 | // package splits | 
|  | 35 |  | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 36 | type appProperties struct { | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 37 | // path to a certificate, or the name of a certificate in the default | 
|  | 38 | // certificate directory, or blank to use the default product certificate | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 39 | Certificate *string | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 40 |  | 
|  | 41 | // paths to extra certificates to sign the apk with | 
|  | 42 | Additional_certificates []string | 
|  | 43 |  | 
|  | 44 | // If set, create package-export.apk, which other packages can | 
|  | 45 | // use to get PRODUCT-agnostic resource data like IDs and type definitions. | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 46 | Export_package_resources *bool | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 47 |  | 
| Colin Cross | 1605606 | 2017-12-13 22:46:28 -0800 | [diff] [blame] | 48 | // Specifies that this app should be installed to the priv-app directory, | 
|  | 49 | // where the system will grant it additional privileges not available to | 
|  | 50 | // normal apps. | 
|  | 51 | Privileged *bool | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 52 |  | 
|  | 53 | // list of resource labels to generate individual resource packages | 
|  | 54 | Package_splits []string | 
| Jason Monk | d4122be | 2018-08-10 09:33:36 -0400 | [diff] [blame] | 55 |  | 
|  | 56 | // Names of modules to be overridden. Listed modules can only be other binaries | 
|  | 57 | // (in Make or Soong). | 
|  | 58 | // This does not completely prevent installation of the overridden binaries, but if both | 
|  | 59 | // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed | 
|  | 60 | // from PRODUCT_PACKAGES. | 
|  | 61 | Overrides []string | 
| Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 64 | type AndroidApp struct { | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 65 | Library | 
|  | 66 | aapt | 
|  | 67 |  | 
|  | 68 | certificate certificate | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 69 |  | 
| Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 70 | appProperties appProperties | 
| Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 71 |  | 
|  | 72 | extraLinkFlags []string | 
| Colin Cross | e1731a5 | 2017-12-14 11:22:55 -0800 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
| Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 75 | func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths { | 
|  | 76 | return nil | 
|  | 77 | } | 
|  | 78 |  | 
| Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 79 | func (a *AndroidApp) ExportedStaticPackages() android.Paths { | 
|  | 80 | return nil | 
|  | 81 | } | 
|  | 82 |  | 
| Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 83 | func (a *AndroidApp) ExportedManifest() android.Path { | 
|  | 84 | return a.manifestPath | 
|  | 85 | } | 
|  | 86 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 87 | var _ AndroidLibraryDependency = (*AndroidApp)(nil) | 
|  | 88 |  | 
| Colin Cross | e1731a5 | 2017-12-14 11:22:55 -0800 | [diff] [blame] | 89 | type certificate struct { | 
|  | 90 | pem, key android.Path | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
| Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 93 | func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) { | 
|  | 94 | a.Module.deps(ctx) | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 95 | if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) { | 
| Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 96 | a.aapt.deps(ctx, sdkContext(a)) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 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 | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 101 | a.generateAndroidBuildActions(ctx) | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 105 | linkFlags := append([]string(nil), a.extraLinkFlags...) | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 106 |  | 
| Colin Cross | e78dcd3 | 2018-04-19 15:25:19 -0700 | [diff] [blame] | 107 | hasProduct := false | 
|  | 108 | for _, f := range a.aaptProperties.Aaptflags { | 
|  | 109 | if strings.HasPrefix(f, "--product") { | 
|  | 110 | hasProduct = true | 
|  | 111 | } | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | // Product characteristics | 
|  | 115 | if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 { | 
|  | 116 | linkFlags = append(linkFlags, "--product", ctx.Config().ProductAAPTCharacteristics()) | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | // Product AAPT config | 
|  | 120 | for _, aaptConfig := range ctx.Config().ProductAAPTConfig() { | 
|  | 121 | linkFlags = append(linkFlags, "-c", aaptConfig) | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | // Product AAPT preferred config | 
|  | 125 | if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 { | 
|  | 126 | linkFlags = append(linkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig()) | 
|  | 127 | } | 
|  | 128 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 129 | // TODO: LOCAL_PACKAGE_OVERRIDES | 
|  | 130 | //    $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \ | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 131 |  | 
| Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 132 | a.aapt.buildActions(ctx, sdkContext(a), linkFlags...) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 133 |  | 
| Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 134 | // apps manifests are handled by aapt, don't let Module see them | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 135 | a.properties.Manifest = nil | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 136 |  | 
| Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 137 | var staticLibProguardFlagFiles android.Paths | 
|  | 138 | ctx.VisitDirectDeps(func(m android.Module) { | 
|  | 139 | if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag { | 
|  | 140 | staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...) | 
|  | 141 | } | 
|  | 142 | }) | 
|  | 143 |  | 
|  | 144 | staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles) | 
|  | 145 |  | 
|  | 146 | a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...) | 
|  | 147 | a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile) | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 148 |  | 
| Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 149 | if ctx.ModuleName() != "framework-res" { | 
|  | 150 | a.Module.compile(ctx, a.aaptSrcJar) | 
|  | 151 | } | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 152 |  | 
| Colin Cross | e1731a5 | 2017-12-14 11:22:55 -0800 | [diff] [blame] | 153 | c := String(a.appProperties.Certificate) | 
|  | 154 | switch { | 
|  | 155 | case c == "": | 
|  | 156 | pem, key := ctx.Config().DefaultAppCertificate(ctx) | 
|  | 157 | a.certificate = certificate{pem, key} | 
|  | 158 | case strings.ContainsRune(c, '/'): | 
|  | 159 | a.certificate = certificate{ | 
|  | 160 | android.PathForSource(ctx, c+".x509.pem"), | 
|  | 161 | android.PathForSource(ctx, c+".pk8"), | 
|  | 162 | } | 
|  | 163 | default: | 
|  | 164 | defaultDir := ctx.Config().DefaultAppCertificateDir(ctx) | 
|  | 165 | a.certificate = certificate{ | 
|  | 166 | defaultDir.Join(ctx, c+".x509.pem"), | 
|  | 167 | defaultDir.Join(ctx, c+".pk8"), | 
|  | 168 | } | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 169 | } | 
|  | 170 |  | 
| Colin Cross | e1731a5 | 2017-12-14 11:22:55 -0800 | [diff] [blame] | 171 | certificates := []certificate{a.certificate} | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 172 | for _, c := range a.appProperties.Additional_certificates { | 
| Colin Cross | e1731a5 | 2017-12-14 11:22:55 -0800 | [diff] [blame] | 173 | certificates = append(certificates, certificate{ | 
|  | 174 | android.PathForSource(ctx, c+".x509.pem"), | 
|  | 175 | android.PathForSource(ctx, c+".pk8"), | 
|  | 176 | }) | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 177 | } | 
|  | 178 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 179 | packageFile := android.PathForModuleOut(ctx, "package.apk") | 
|  | 180 |  | 
|  | 181 | CreateAppPackage(ctx, packageFile, a.exportPackage, a.outputFile, certificates) | 
|  | 182 |  | 
|  | 183 | a.outputFile = packageFile | 
|  | 184 |  | 
| Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 185 | if ctx.ModuleName() == "framework-res" { | 
|  | 186 | // framework-res.apk is installed as system/framework/framework-res.apk | 
|  | 187 | ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".apk", a.outputFile) | 
| Colin Cross | 1605606 | 2017-12-13 22:46:28 -0800 | [diff] [blame] | 188 | } else if Bool(a.appProperties.Privileged) { | 
|  | 189 | ctx.InstallFile(android.PathForModuleInstall(ctx, "priv-app"), ctx.ModuleName()+".apk", a.outputFile) | 
| Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 190 | } else { | 
|  | 191 | ctx.InstallFile(android.PathForModuleInstall(ctx, "app"), ctx.ModuleName()+".apk", a.outputFile) | 
|  | 192 | } | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 193 | } | 
|  | 194 |  | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 195 | func AndroidAppFactory() android.Module { | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 196 | module := &AndroidApp{} | 
|  | 197 |  | 
| Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 198 | module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true) | 
|  | 199 | module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true) | 
|  | 200 |  | 
| Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 201 | module.Module.properties.Instrument = true | 
| Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 202 | module.Module.properties.Installable = proptools.BoolPtr(true) | 
| Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 203 |  | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 204 | module.AddProperties( | 
| Colin Cross | 540eff8 | 2017-06-22 17:01:52 -0700 | [diff] [blame] | 205 | &module.Module.properties, | 
|  | 206 | &module.Module.deviceProperties, | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 207 | &module.Module.protoProperties, | 
|  | 208 | &module.aaptProperties, | 
| Colin Cross | 540eff8 | 2017-06-22 17:01:52 -0700 | [diff] [blame] | 209 | &module.appProperties) | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 210 |  | 
| Colin Cross | b98c0f4 | 2018-08-20 13:28:12 -0700 | [diff] [blame] | 211 | InitJavaModule(module, android.DeviceSupported) | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 212 | return module | 
| Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 213 | } | 
| Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 214 |  | 
|  | 215 | type appTestProperties struct { | 
|  | 216 | Instrumentation_for *string | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | type AndroidTest struct { | 
|  | 220 | AndroidApp | 
|  | 221 |  | 
|  | 222 | appTestProperties appTestProperties | 
|  | 223 |  | 
|  | 224 | testProperties testProperties | 
| Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 225 |  | 
|  | 226 | testConfig android.Path | 
| Colin Cross | d96ca35 | 2018-08-10 16:06:24 -0700 | [diff] [blame] | 227 | data       android.Paths | 
| Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 228 | } | 
|  | 229 |  | 
|  | 230 | func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 231 | if String(a.appTestProperties.Instrumentation_for) != "" { | 
|  | 232 | a.AndroidApp.extraLinkFlags = append(a.AndroidApp.extraLinkFlags, | 
|  | 233 | "--rename-instrumentation-target-package", | 
|  | 234 | String(a.appTestProperties.Instrumentation_for)) | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | a.generateAndroidBuildActions(ctx) | 
| Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 238 |  | 
|  | 239 | a.testConfig = tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config, a.manifestPath) | 
| Colin Cross | d96ca35 | 2018-08-10 16:06:24 -0700 | [diff] [blame] | 240 | a.data = ctx.ExpandSources(a.testProperties.Data, nil) | 
| Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 241 | } | 
|  | 242 |  | 
|  | 243 | func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) { | 
|  | 244 | android.ExtractSourceDeps(ctx, a.testProperties.Test_config) | 
| Colin Cross | d96ca35 | 2018-08-10 16:06:24 -0700 | [diff] [blame] | 245 | android.ExtractSourcesDeps(ctx, a.testProperties.Data) | 
| Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 246 | a.AndroidApp.DepsMutator(ctx) | 
| Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 247 | } | 
|  | 248 |  | 
|  | 249 | func AndroidTestFactory() android.Module { | 
|  | 250 | module := &AndroidTest{} | 
|  | 251 |  | 
|  | 252 | module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true) | 
| Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 253 | module.Module.properties.Installable = proptools.BoolPtr(true) | 
| Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 254 |  | 
|  | 255 | module.AddProperties( | 
|  | 256 | &module.Module.properties, | 
|  | 257 | &module.Module.deviceProperties, | 
|  | 258 | &module.Module.protoProperties, | 
|  | 259 | &module.aaptProperties, | 
|  | 260 | &module.appProperties, | 
| Dan Willemsen | f5531d2 | 2018-07-16 17:21:19 -0700 | [diff] [blame] | 261 | &module.appTestProperties, | 
|  | 262 | &module.testProperties) | 
| Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 263 |  | 
| Colin Cross | b98c0f4 | 2018-08-20 13:28:12 -0700 | [diff] [blame] | 264 | InitJavaModule(module, android.DeviceSupported) | 
| Colin Cross | ae5caf5 | 2018-05-22 11:11:52 -0700 | [diff] [blame] | 265 | return module | 
|  | 266 | } |