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 | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 27 | func init() { |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 28 | android.RegisterModuleType("android_app", AndroidAppFactory) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 29 | } |
| 30 | |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 31 | // AndroidManifest.xml merging |
| 32 | // package splits |
| 33 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 34 | type appProperties struct { |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 35 | // path to a certificate, or the name of a certificate in the default |
| 36 | // certificate directory, or blank to use the default product certificate |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 37 | Certificate *string |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 38 | |
| 39 | // paths to extra certificates to sign the apk with |
| 40 | Additional_certificates []string |
| 41 | |
| 42 | // If set, create package-export.apk, which other packages can |
| 43 | // use to get PRODUCT-agnostic resource data like IDs and type definitions. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 44 | Export_package_resources *bool |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 45 | |
Colin Cross | 1605606 | 2017-12-13 22:46:28 -0800 | [diff] [blame] | 46 | // Specifies that this app should be installed to the priv-app directory, |
| 47 | // where the system will grant it additional privileges not available to |
| 48 | // normal apps. |
| 49 | Privileged *bool |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 50 | |
| 51 | // list of resource labels to generate individual resource packages |
| 52 | Package_splits []string |
| 53 | |
| 54 | Instrumentation_for *string |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 57 | type AndroidApp struct { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 58 | Library |
| 59 | aapt |
| 60 | |
| 61 | certificate certificate |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 62 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 63 | appProperties appProperties |
Colin Cross | e1731a5 | 2017-12-14 11:22:55 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame^] | 66 | func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths { |
| 67 | return nil |
| 68 | } |
| 69 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 70 | var _ AndroidLibraryDependency = (*AndroidApp)(nil) |
| 71 | |
Colin Cross | e1731a5 | 2017-12-14 11:22:55 -0800 | [diff] [blame] | 72 | type certificate struct { |
| 73 | pem, key android.Path |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 76 | func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 77 | a.Module.deps(ctx) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 78 | if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 79 | a.aapt.deps(ctx, String(a.deviceProperties.Sdk_version)) |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 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 | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 84 | var linkFlags []string |
| 85 | if String(a.appProperties.Instrumentation_for) != "" { |
| 86 | linkFlags = append(linkFlags, |
| 87 | "--rename-instrumentation-target-package", |
| 88 | String(a.appProperties.Instrumentation_for)) |
| 89 | } else { |
| 90 | a.properties.Instrument = true |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Colin Cross | e78dcd3 | 2018-04-19 15:25:19 -0700 | [diff] [blame] | 93 | hasProduct := false |
| 94 | for _, f := range a.aaptProperties.Aaptflags { |
| 95 | if strings.HasPrefix(f, "--product") { |
| 96 | hasProduct = true |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Product characteristics |
| 101 | if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 { |
| 102 | linkFlags = append(linkFlags, "--product", ctx.Config().ProductAAPTCharacteristics()) |
| 103 | } |
| 104 | |
| 105 | // Product AAPT config |
| 106 | for _, aaptConfig := range ctx.Config().ProductAAPTConfig() { |
| 107 | linkFlags = append(linkFlags, "-c", aaptConfig) |
| 108 | } |
| 109 | |
| 110 | // Product AAPT preferred config |
| 111 | if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 { |
| 112 | linkFlags = append(linkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig()) |
| 113 | } |
| 114 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 115 | // TODO: LOCAL_PACKAGE_OVERRIDES |
| 116 | // $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \ |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 117 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 118 | a.aapt.buildActions(ctx, String(a.deviceProperties.Sdk_version), linkFlags...) |
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 | |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame^] | 123 | var staticLibProguardFlagFiles android.Paths |
| 124 | ctx.VisitDirectDeps(func(m android.Module) { |
| 125 | if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag { |
| 126 | staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...) |
| 127 | } |
| 128 | }) |
| 129 | |
| 130 | staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles) |
| 131 | |
| 132 | a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...) |
| 133 | a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile) |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 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 |
| 170 | |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 171 | if ctx.ModuleName() == "framework-res" { |
| 172 | // framework-res.apk is installed as system/framework/framework-res.apk |
| 173 | ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".apk", a.outputFile) |
Colin Cross | 1605606 | 2017-12-13 22:46:28 -0800 | [diff] [blame] | 174 | } else if Bool(a.appProperties.Privileged) { |
| 175 | ctx.InstallFile(android.PathForModuleInstall(ctx, "priv-app"), ctx.ModuleName()+".apk", a.outputFile) |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 176 | } else { |
| 177 | ctx.InstallFile(android.PathForModuleInstall(ctx, "app"), ctx.ModuleName()+".apk", a.outputFile) |
| 178 | } |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 181 | func AndroidAppFactory() android.Module { |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 182 | module := &AndroidApp{} |
| 183 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 184 | module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true) |
| 185 | module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true) |
| 186 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 187 | module.AddProperties( |
Colin Cross | 540eff8 | 2017-06-22 17:01:52 -0700 | [diff] [blame] | 188 | &module.Module.properties, |
| 189 | &module.Module.deviceProperties, |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 190 | &module.Module.protoProperties, |
| 191 | &module.aaptProperties, |
Colin Cross | 540eff8 | 2017-06-22 17:01:52 -0700 | [diff] [blame] | 192 | &module.appProperties) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 193 | |
| 194 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 195 | return module |
Colin Cross | 30e076a | 2015-04-13 13:58:27 -0700 | [diff] [blame] | 196 | } |