blob: ae0592a6a5226b984e5c767c9c3c2a0dbef44d34 [file] [log] [blame]
Colin Cross30e076a2015-04-13 13:58:27 -07001// 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
15package java
16
17// This file contains the module types for compiling Android apps.
18
19import (
Colin Cross30e076a2015-04-13 13:58:27 -070020 "strings"
21
Colin Cross76b5f0c2017-08-29 16:02:06 -070022 "github.com/google/blueprint/proptools"
Colin Cross30e076a2015-04-13 13:58:27 -070023
Colin Cross635c3b02016-05-18 15:37:25 -070024 "android/soong/android"
Colin Cross30e076a2015-04-13 13:58:27 -070025)
26
Colin Cross3bc7ffa2017-11-22 16:19:37 -080027func init() {
Colin Cross5ab4e6d2017-11-22 16:20:45 -080028 android.RegisterModuleType("android_app", AndroidAppFactory)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080029}
30
Colin Cross30e076a2015-04-13 13:58:27 -070031// AndroidManifest.xml merging
32// package splits
33
Colin Crossfabb6082018-02-20 17:22:23 -080034type appProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070035 // 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 Zhangea568a42017-11-08 21:20:04 -080037 Certificate *string
Colin Cross7d5136f2015-05-11 13:39:40 -070038
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 Zhangea568a42017-11-08 21:20:04 -080044 Export_package_resources *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070045
Colin Cross16056062017-12-13 22:46:28 -080046 // 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 Crossa97c5d32018-03-28 14:58:31 -070050
51 // list of resource labels to generate individual resource packages
52 Package_splits []string
53
54 Instrumentation_for *string
Colin Cross7d5136f2015-05-11 13:39:40 -070055}
56
Colin Cross30e076a2015-04-13 13:58:27 -070057type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -070058 Library
59 aapt
60
61 certificate certificate
Colin Cross30e076a2015-04-13 13:58:27 -070062
Colin Crossfabb6082018-02-20 17:22:23 -080063 appProperties appProperties
Colin Crosse1731a52017-12-14 11:22:55 -080064}
65
Colin Cross89c31582018-04-30 15:55:11 -070066func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
67 return nil
68}
69
Colin Cross66f78822018-05-02 12:58:28 -070070func (a *AndroidApp) ExportedStaticPackages() android.Paths {
71 return nil
72}
73
Colin Crossa97c5d32018-03-28 14:58:31 -070074var _ AndroidLibraryDependency = (*AndroidApp)(nil)
75
Colin Crosse1731a52017-12-14 11:22:55 -080076type certificate struct {
77 pem, key android.Path
Colin Cross30e076a2015-04-13 13:58:27 -070078}
79
Colin Cross46c9b8b2017-06-22 16:51:17 -070080func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
81 a.Module.deps(ctx)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080082 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Crossa97c5d32018-03-28 14:58:31 -070083 a.aapt.deps(ctx, String(a.deviceProperties.Sdk_version))
Colin Cross30e076a2015-04-13 13:58:27 -070084 }
Colin Cross30e076a2015-04-13 13:58:27 -070085}
86
Colin Cross46c9b8b2017-06-22 16:51:17 -070087func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossa97c5d32018-03-28 14:58:31 -070088 var linkFlags []string
89 if String(a.appProperties.Instrumentation_for) != "" {
90 linkFlags = append(linkFlags,
91 "--rename-instrumentation-target-package",
92 String(a.appProperties.Instrumentation_for))
93 } else {
94 a.properties.Instrument = true
Colin Cross3bc7ffa2017-11-22 16:19:37 -080095 }
96
Colin Crosse78dcd32018-04-19 15:25:19 -070097 hasProduct := false
98 for _, f := range a.aaptProperties.Aaptflags {
99 if strings.HasPrefix(f, "--product") {
100 hasProduct = true
101 }
102 }
103
104 // Product characteristics
105 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
106 linkFlags = append(linkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
107 }
108
109 // Product AAPT config
110 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
111 linkFlags = append(linkFlags, "-c", aaptConfig)
112 }
113
114 // Product AAPT preferred config
115 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
116 linkFlags = append(linkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
117 }
118
Colin Crossa97c5d32018-03-28 14:58:31 -0700119 // TODO: LOCAL_PACKAGE_OVERRIDES
120 // $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800121
Colin Crossa97c5d32018-03-28 14:58:31 -0700122 a.aapt.buildActions(ctx, String(a.deviceProperties.Sdk_version), linkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700123
Colin Cross46c9b8b2017-06-22 16:51:17 -0700124 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700125 a.properties.Manifest = nil
Colin Cross30e076a2015-04-13 13:58:27 -0700126
Colin Cross89c31582018-04-30 15:55:11 -0700127 var staticLibProguardFlagFiles android.Paths
128 ctx.VisitDirectDeps(func(m android.Module) {
129 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
130 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
131 }
132 })
133
134 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
135
136 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
137 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800138
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800139 if ctx.ModuleName() != "framework-res" {
140 a.Module.compile(ctx, a.aaptSrcJar)
141 }
Colin Cross30e076a2015-04-13 13:58:27 -0700142
Colin Crosse1731a52017-12-14 11:22:55 -0800143 c := String(a.appProperties.Certificate)
144 switch {
145 case c == "":
146 pem, key := ctx.Config().DefaultAppCertificate(ctx)
147 a.certificate = certificate{pem, key}
148 case strings.ContainsRune(c, '/'):
149 a.certificate = certificate{
150 android.PathForSource(ctx, c+".x509.pem"),
151 android.PathForSource(ctx, c+".pk8"),
152 }
153 default:
154 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
155 a.certificate = certificate{
156 defaultDir.Join(ctx, c+".x509.pem"),
157 defaultDir.Join(ctx, c+".pk8"),
158 }
Colin Cross30e076a2015-04-13 13:58:27 -0700159 }
160
Colin Crosse1731a52017-12-14 11:22:55 -0800161 certificates := []certificate{a.certificate}
Colin Cross30e076a2015-04-13 13:58:27 -0700162 for _, c := range a.appProperties.Additional_certificates {
Colin Crosse1731a52017-12-14 11:22:55 -0800163 certificates = append(certificates, certificate{
164 android.PathForSource(ctx, c+".x509.pem"),
165 android.PathForSource(ctx, c+".pk8"),
166 })
Colin Cross30e076a2015-04-13 13:58:27 -0700167 }
168
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800169 packageFile := android.PathForModuleOut(ctx, "package.apk")
170
171 CreateAppPackage(ctx, packageFile, a.exportPackage, a.outputFile, certificates)
172
173 a.outputFile = packageFile
174
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800175 if ctx.ModuleName() == "framework-res" {
176 // framework-res.apk is installed as system/framework/framework-res.apk
177 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".apk", a.outputFile)
Colin Cross16056062017-12-13 22:46:28 -0800178 } else if Bool(a.appProperties.Privileged) {
179 ctx.InstallFile(android.PathForModuleInstall(ctx, "priv-app"), ctx.ModuleName()+".apk", a.outputFile)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800180 } else {
181 ctx.InstallFile(android.PathForModuleInstall(ctx, "app"), ctx.ModuleName()+".apk", a.outputFile)
182 }
Colin Cross30e076a2015-04-13 13:58:27 -0700183}
184
Colin Cross36242852017-06-23 15:06:31 -0700185func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700186 module := &AndroidApp{}
187
Colin Cross66dbc0b2017-12-28 12:23:20 -0800188 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
189 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
190
Colin Cross36242852017-06-23 15:06:31 -0700191 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700192 &module.Module.properties,
193 &module.Module.deviceProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700194 &module.Module.protoProperties,
195 &module.aaptProperties,
Colin Cross540eff82017-06-22 17:01:52 -0700196 &module.appProperties)
Colin Cross36242852017-06-23 15:06:31 -0700197
198 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
199 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700200}