blob: b9088c3358805e961d69d8702430ac0daa195163 [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 Crossa4f08812018-10-02 22:03:40 -070022 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070023 "github.com/google/blueprint/proptools"
Colin Cross30e076a2015-04-13 13:58:27 -070024
Colin Cross635c3b02016-05-18 15:37:25 -070025 "android/soong/android"
Colin Crossa4f08812018-10-02 22:03:40 -070026 "android/soong/cc"
Colin Cross303e21f2018-08-07 16:49:25 -070027 "android/soong/tradefed"
Colin Cross30e076a2015-04-13 13:58:27 -070028)
29
Colin Cross3bc7ffa2017-11-22 16:19:37 -080030func init() {
Colin Cross5ab4e6d2017-11-22 16:20:45 -080031 android.RegisterModuleType("android_app", AndroidAppFactory)
Colin Crossae5caf52018-05-22 11:11:52 -070032 android.RegisterModuleType("android_test", AndroidTestFactory)
Colin Crossbd01e2a2018-10-04 15:21:03 -070033 android.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080034}
35
Colin Cross30e076a2015-04-13 13:58:27 -070036// AndroidManifest.xml merging
37// package splits
38
Colin Crossfabb6082018-02-20 17:22:23 -080039type appProperties struct {
Colin Crossbd01e2a2018-10-04 15:21:03 -070040 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
41 // or an android_app_certificate module name in the form ":module".
Nan Zhangea568a42017-11-08 21:20:04 -080042 Certificate *string
Colin Cross7d5136f2015-05-11 13:39:40 -070043
Colin Crossbd01e2a2018-10-04 15:21:03 -070044 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
Colin Cross7d5136f2015-05-11 13:39:40 -070045 Additional_certificates []string
46
47 // If set, create package-export.apk, which other packages can
48 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
Nan Zhangea568a42017-11-08 21:20:04 -080049 Export_package_resources *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070050
Colin Cross16056062017-12-13 22:46:28 -080051 // Specifies that this app should be installed to the priv-app directory,
52 // where the system will grant it additional privileges not available to
53 // normal apps.
54 Privileged *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070055
56 // list of resource labels to generate individual resource packages
57 Package_splits []string
Jason Monkd4122be2018-08-10 09:33:36 -040058
59 // Names of modules to be overridden. Listed modules can only be other binaries
60 // (in Make or Soong).
61 // This does not completely prevent installation of the overridden binaries, but if both
62 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
63 // from PRODUCT_PACKAGES.
64 Overrides []string
Colin Crossa4f08812018-10-02 22:03:40 -070065
66 // list of native libraries that will be provided in or alongside the resulting jar
67 Jni_libs []string `android:"arch_variant"`
68
69 EmbedJNI bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -070070}
71
Colin Cross30e076a2015-04-13 13:58:27 -070072type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -070073 Library
74 aapt
75
76 certificate certificate
Colin Cross30e076a2015-04-13 13:58:27 -070077
Colin Crossfabb6082018-02-20 17:22:23 -080078 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -070079
80 extraLinkFlags []string
Colin Crossa4f08812018-10-02 22:03:40 -070081
82 installJniLibs []jniLib
Colin Crosse1731a52017-12-14 11:22:55 -080083}
84
Colin Cross89c31582018-04-30 15:55:11 -070085func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
86 return nil
87}
88
Colin Cross66f78822018-05-02 12:58:28 -070089func (a *AndroidApp) ExportedStaticPackages() android.Paths {
90 return nil
91}
92
Colin Cross31656952018-05-24 16:11:20 -070093func (a *AndroidApp) ExportedManifest() android.Path {
94 return a.manifestPath
95}
96
Colin Crossa97c5d32018-03-28 14:58:31 -070097var _ AndroidLibraryDependency = (*AndroidApp)(nil)
98
Colin Crosse1731a52017-12-14 11:22:55 -080099type certificate struct {
100 pem, key android.Path
Colin Cross30e076a2015-04-13 13:58:27 -0700101}
102
Colin Cross46c9b8b2017-06-22 16:51:17 -0700103func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
104 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700105
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800106 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700107 a.aapt.deps(ctx, sdkContext(a))
Colin Cross30e076a2015-04-13 13:58:27 -0700108 }
Colin Crossa4f08812018-10-02 22:03:40 -0700109
110 for _, jniTarget := range ctx.MultiTargets() {
111 variation := []blueprint.Variation{
112 {Mutator: "arch", Variation: jniTarget.String()},
113 {Mutator: "link", Variation: "shared"},
114 }
115 tag := &jniDependencyTag{
116 target: jniTarget,
117 }
118 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
119 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700120
121 cert := android.SrcIsModule(String(a.appProperties.Certificate))
122 if cert != "" {
123 ctx.AddDependency(ctx.Module(), certificateTag, cert)
124 }
125
126 for _, cert := range a.appProperties.Additional_certificates {
127 cert = android.SrcIsModule(cert)
128 if cert != "" {
129 ctx.AddDependency(ctx.Module(), certificateTag, cert)
130 } else {
131 ctx.PropertyErrorf("additional_certificates",
132 `must be names of android_app_certificate modules in the form ":module"`)
133 }
134 }
Colin Cross30e076a2015-04-13 13:58:27 -0700135}
136
Colin Cross46c9b8b2017-06-22 16:51:17 -0700137func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossae5caf52018-05-22 11:11:52 -0700138 a.generateAndroidBuildActions(ctx)
139}
140
141func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
142 linkFlags := append([]string(nil), a.extraLinkFlags...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800143
Colin Crosse78dcd32018-04-19 15:25:19 -0700144 hasProduct := false
145 for _, f := range a.aaptProperties.Aaptflags {
146 if strings.HasPrefix(f, "--product") {
147 hasProduct = true
148 }
149 }
150
151 // Product characteristics
152 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
153 linkFlags = append(linkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
154 }
155
156 // Product AAPT config
157 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
158 linkFlags = append(linkFlags, "-c", aaptConfig)
159 }
160
161 // Product AAPT preferred config
162 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
163 linkFlags = append(linkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
164 }
165
Colin Crossa97c5d32018-03-28 14:58:31 -0700166 // TODO: LOCAL_PACKAGE_OVERRIDES
167 // $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800168
Colin Cross83bb3162018-06-25 15:48:06 -0700169 a.aapt.buildActions(ctx, sdkContext(a), linkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700170
Colin Cross46c9b8b2017-06-22 16:51:17 -0700171 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700172 a.properties.Manifest = nil
Colin Cross30e076a2015-04-13 13:58:27 -0700173
Colin Cross89c31582018-04-30 15:55:11 -0700174 var staticLibProguardFlagFiles android.Paths
175 ctx.VisitDirectDeps(func(m android.Module) {
176 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
177 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
178 }
179 })
180
181 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
182
183 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
184 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800185
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800186 if ctx.ModuleName() != "framework-res" {
187 a.Module.compile(ctx, a.aaptSrcJar)
188 }
Colin Cross30e076a2015-04-13 13:58:27 -0700189
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800190 packageFile := android.PathForModuleOut(ctx, "package.apk")
191
Colin Crossbd01e2a2018-10-04 15:21:03 -0700192 var certificates []certificate
193
Colin Crossa4f08812018-10-02 22:03:40 -0700194 var jniJarFile android.WritablePath
Colin Crossbd01e2a2018-10-04 15:21:03 -0700195 jniLibs, certificateDeps := a.collectAppDeps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700196 if len(jniLibs) > 0 {
197 embedJni := ctx.Config().UnbundledBuild() || a.appProperties.EmbedJNI
198 if embedJni {
199 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
200 TransformJniLibsToJar(ctx, jniJarFile, jniLibs)
201 } else {
202 a.installJniLibs = jniLibs
203 }
204 }
205
Colin Crossbd01e2a2018-10-04 15:21:03 -0700206 if ctx.Failed() {
207 return
208 }
209
210 cert := String(a.appProperties.Certificate)
211 certModule := android.SrcIsModule(cert)
212 if certModule != "" {
213 a.certificate = certificateDeps[0]
214 certificateDeps = certificateDeps[1:]
215 } else if cert != "" {
216 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
217 a.certificate = certificate{
218 defaultDir.Join(ctx, cert+".x509.pem"),
219 defaultDir.Join(ctx, cert+".pk8"),
220 }
221 } else {
222 pem, key := ctx.Config().DefaultAppCertificate(ctx)
223 a.certificate = certificate{pem, key}
224 }
225
226 certificates = append([]certificate{a.certificate}, certificateDeps...)
227
Colin Crossa4f08812018-10-02 22:03:40 -0700228 CreateAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, a.outputFile, certificates)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800229
230 a.outputFile = packageFile
231
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800232 if ctx.ModuleName() == "framework-res" {
233 // framework-res.apk is installed as system/framework/framework-res.apk
234 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".apk", a.outputFile)
Colin Cross16056062017-12-13 22:46:28 -0800235 } else if Bool(a.appProperties.Privileged) {
236 ctx.InstallFile(android.PathForModuleInstall(ctx, "priv-app"), ctx.ModuleName()+".apk", a.outputFile)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800237 } else {
238 ctx.InstallFile(android.PathForModuleInstall(ctx, "app"), ctx.ModuleName()+".apk", a.outputFile)
239 }
Colin Cross30e076a2015-04-13 13:58:27 -0700240}
241
Colin Crossbd01e2a2018-10-04 15:21:03 -0700242func (a *AndroidApp) collectAppDeps(ctx android.ModuleContext) ([]jniLib, []certificate) {
Colin Crossa4f08812018-10-02 22:03:40 -0700243 var jniLibs []jniLib
Colin Crossbd01e2a2018-10-04 15:21:03 -0700244 var certificates []certificate
Colin Crossa4f08812018-10-02 22:03:40 -0700245
246 ctx.VisitDirectDeps(func(module android.Module) {
247 otherName := ctx.OtherModuleName(module)
248 tag := ctx.OtherModuleDependencyTag(module)
249
250 if jniTag, ok := tag.(*jniDependencyTag); ok {
251 if dep, ok := module.(*cc.Module); ok {
252 lib := dep.OutputFile()
253 if lib.Valid() {
254 jniLibs = append(jniLibs, jniLib{
255 name: ctx.OtherModuleName(module),
256 path: lib.Path(),
257 target: jniTag.target,
258 })
259 } else {
260 ctx.ModuleErrorf("dependency %q missing output file", otherName)
261 }
262 } else {
263 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
264
265 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700266 } else if tag == certificateTag {
267 if dep, ok := module.(*AndroidAppCertificate); ok {
268 certificates = append(certificates, dep.certificate)
269 } else {
270 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
271 }
Colin Crossa4f08812018-10-02 22:03:40 -0700272 }
273 })
274
Colin Crossbd01e2a2018-10-04 15:21:03 -0700275 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700276}
277
Colin Cross36242852017-06-23 15:06:31 -0700278func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700279 module := &AndroidApp{}
280
Colin Cross66dbc0b2017-12-28 12:23:20 -0800281 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
282 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
283
Colin Crossae5caf52018-05-22 11:11:52 -0700284 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700285 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700286
Colin Cross36242852017-06-23 15:06:31 -0700287 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700288 &module.Module.properties,
289 &module.Module.deviceProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700290 &module.Module.protoProperties,
291 &module.aaptProperties,
Colin Cross540eff82017-06-22 17:01:52 -0700292 &module.appProperties)
Colin Cross36242852017-06-23 15:06:31 -0700293
Colin Crossa9d8bee2018-10-02 13:59:46 -0700294 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
295 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
296 })
297
Colin Crossa4f08812018-10-02 22:03:40 -0700298 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
299 android.InitDefaultableModule(module)
300
Colin Cross36242852017-06-23 15:06:31 -0700301 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700302}
Colin Crossae5caf52018-05-22 11:11:52 -0700303
304type appTestProperties struct {
305 Instrumentation_for *string
306}
307
308type AndroidTest struct {
309 AndroidApp
310
311 appTestProperties appTestProperties
312
313 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700314
315 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700316 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700317}
318
319func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
320 if String(a.appTestProperties.Instrumentation_for) != "" {
321 a.AndroidApp.extraLinkFlags = append(a.AndroidApp.extraLinkFlags,
322 "--rename-instrumentation-target-package",
323 String(a.appTestProperties.Instrumentation_for))
324 }
325
326 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700327
Jack He33338892018-09-19 02:21:28 -0700328 a.testConfig = tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config, a.testProperties.Test_config_template, a.manifestPath)
Colin Crossd96ca352018-08-10 16:06:24 -0700329 a.data = ctx.ExpandSources(a.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -0700330}
331
332func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
333 android.ExtractSourceDeps(ctx, a.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -0700334 android.ExtractSourceDeps(ctx, a.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -0700335 android.ExtractSourcesDeps(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700336 a.AndroidApp.DepsMutator(ctx)
Colin Crossae5caf52018-05-22 11:11:52 -0700337}
338
339func AndroidTestFactory() android.Module {
340 module := &AndroidTest{}
341
342 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
Colin Cross5067db92018-09-17 16:46:35 -0700343
344 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700345 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossa4f08812018-10-02 22:03:40 -0700346 module.appProperties.EmbedJNI = true
Colin Crossae5caf52018-05-22 11:11:52 -0700347
348 module.AddProperties(
349 &module.Module.properties,
350 &module.Module.deviceProperties,
351 &module.Module.protoProperties,
352 &module.aaptProperties,
353 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700354 &module.appTestProperties,
355 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700356
Colin Crossa4f08812018-10-02 22:03:40 -0700357 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
358 android.InitDefaultableModule(module)
Colin Crossae5caf52018-05-22 11:11:52 -0700359 return module
360}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700361
362type AndroidAppCertificate struct {
363 android.ModuleBase
364 properties AndroidAppCertificateProperties
365 certificate certificate
366}
367
368type AndroidAppCertificateProperties struct {
369 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
370 Certificate *string
371}
372
373func AndroidAppCertificateFactory() android.Module {
374 module := &AndroidAppCertificate{}
375 module.AddProperties(&module.properties)
376 android.InitAndroidModule(module)
377 return module
378}
379
380func (c *AndroidAppCertificate) DepsMutator(ctx android.BottomUpMutatorContext) {
381}
382
383func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
384 cert := String(c.properties.Certificate)
385 c.certificate = certificate{
386 android.PathForModuleSrc(ctx, cert+".x509.pem"),
387 android.PathForModuleSrc(ctx, cert+".pk8"),
388 }
389}