blob: 1fdce129cbc9e681198eb0747fac2da84a67e125 [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 Crossa97c5d32018-03-28 14:58:31 -070070var _ AndroidLibraryDependency = (*AndroidApp)(nil)
71
Colin Crosse1731a52017-12-14 11:22:55 -080072type certificate struct {
73 pem, key android.Path
Colin Cross30e076a2015-04-13 13:58:27 -070074}
75
Colin Cross46c9b8b2017-06-22 16:51:17 -070076func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
77 a.Module.deps(ctx)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080078 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Crossa97c5d32018-03-28 14:58:31 -070079 a.aapt.deps(ctx, String(a.deviceProperties.Sdk_version))
Colin Cross30e076a2015-04-13 13:58:27 -070080 }
Colin Cross30e076a2015-04-13 13:58:27 -070081}
82
Colin Cross46c9b8b2017-06-22 16:51:17 -070083func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossa97c5d32018-03-28 14:58:31 -070084 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 Cross3bc7ffa2017-11-22 16:19:37 -080091 }
92
Colin Crosse78dcd32018-04-19 15:25:19 -070093 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 Crossa97c5d32018-03-28 14:58:31 -0700115 // TODO: LOCAL_PACKAGE_OVERRIDES
116 // $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800117
Colin Crossa97c5d32018-03-28 14:58:31 -0700118 a.aapt.buildActions(ctx, String(a.deviceProperties.Sdk_version), linkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700119
Colin Cross46c9b8b2017-06-22 16:51:17 -0700120 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700121 a.properties.Manifest = nil
Colin Cross30e076a2015-04-13 13:58:27 -0700122
Colin Cross89c31582018-04-30 15:55:11 -0700123 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 Cross66dbc0b2017-12-28 12:23:20 -0800134
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800135 if ctx.ModuleName() != "framework-res" {
136 a.Module.compile(ctx, a.aaptSrcJar)
137 }
Colin Cross30e076a2015-04-13 13:58:27 -0700138
Colin Crosse1731a52017-12-14 11:22:55 -0800139 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 Cross30e076a2015-04-13 13:58:27 -0700155 }
156
Colin Crosse1731a52017-12-14 11:22:55 -0800157 certificates := []certificate{a.certificate}
Colin Cross30e076a2015-04-13 13:58:27 -0700158 for _, c := range a.appProperties.Additional_certificates {
Colin Crosse1731a52017-12-14 11:22:55 -0800159 certificates = append(certificates, certificate{
160 android.PathForSource(ctx, c+".x509.pem"),
161 android.PathForSource(ctx, c+".pk8"),
162 })
Colin Cross30e076a2015-04-13 13:58:27 -0700163 }
164
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800165 packageFile := android.PathForModuleOut(ctx, "package.apk")
166
167 CreateAppPackage(ctx, packageFile, a.exportPackage, a.outputFile, certificates)
168
169 a.outputFile = packageFile
170
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800171 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 Cross16056062017-12-13 22:46:28 -0800174 } else if Bool(a.appProperties.Privileged) {
175 ctx.InstallFile(android.PathForModuleInstall(ctx, "priv-app"), ctx.ModuleName()+".apk", a.outputFile)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800176 } else {
177 ctx.InstallFile(android.PathForModuleInstall(ctx, "app"), ctx.ModuleName()+".apk", a.outputFile)
178 }
Colin Cross30e076a2015-04-13 13:58:27 -0700179}
180
Colin Cross36242852017-06-23 15:06:31 -0700181func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700182 module := &AndroidApp{}
183
Colin Cross66dbc0b2017-12-28 12:23:20 -0800184 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
185 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
186
Colin Cross36242852017-06-23 15:06:31 -0700187 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700188 &module.Module.properties,
189 &module.Module.deviceProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700190 &module.Module.protoProperties,
191 &module.aaptProperties,
Colin Cross540eff82017-06-22 17:01:52 -0700192 &module.appProperties)
Colin Cross36242852017-06-23 15:06:31 -0700193
194 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
195 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700196}