blob: d21b62aecc4d9f05271b23a14e9cffbcd9b02462 [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 Cross252fc6f2018-10-04 15:22:03 -070033 android.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
Colin Crossbd01e2a2018-10-04 15:21:03 -070034 android.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080035}
36
Colin Cross30e076a2015-04-13 13:58:27 -070037// AndroidManifest.xml merging
38// package splits
39
Colin Crossfabb6082018-02-20 17:22:23 -080040type appProperties struct {
Colin Crossbd01e2a2018-10-04 15:21:03 -070041 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
42 // or an android_app_certificate module name in the form ":module".
Nan Zhangea568a42017-11-08 21:20:04 -080043 Certificate *string
Colin Cross7d5136f2015-05-11 13:39:40 -070044
Colin Crossbd01e2a2018-10-04 15:21:03 -070045 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
Colin Cross7d5136f2015-05-11 13:39:40 -070046 Additional_certificates []string
47
48 // If set, create package-export.apk, which other packages can
49 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
Nan Zhangea568a42017-11-08 21:20:04 -080050 Export_package_resources *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070051
Colin Cross16056062017-12-13 22:46:28 -080052 // Specifies that this app should be installed to the priv-app directory,
53 // where the system will grant it additional privileges not available to
54 // normal apps.
55 Privileged *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070056
57 // list of resource labels to generate individual resource packages
58 Package_splits []string
Jason Monkd4122be2018-08-10 09:33:36 -040059
60 // Names of modules to be overridden. Listed modules can only be other binaries
61 // (in Make or Soong).
62 // This does not completely prevent installation of the overridden binaries, but if both
63 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
64 // from PRODUCT_PACKAGES.
65 Overrides []string
Colin Crossa4f08812018-10-02 22:03:40 -070066
67 // list of native libraries that will be provided in or alongside the resulting jar
68 Jni_libs []string `android:"arch_variant"`
69
70 EmbedJNI bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -070071}
72
Colin Cross30e076a2015-04-13 13:58:27 -070073type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -070074 Library
75 aapt
76
77 certificate certificate
Colin Cross30e076a2015-04-13 13:58:27 -070078
Colin Crossfabb6082018-02-20 17:22:23 -080079 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -070080
81 extraLinkFlags []string
Colin Crossa4f08812018-10-02 22:03:40 -070082
83 installJniLibs []jniLib
Colin Crosse1731a52017-12-14 11:22:55 -080084}
85
Colin Cross89c31582018-04-30 15:55:11 -070086func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
87 return nil
88}
89
Colin Cross66f78822018-05-02 12:58:28 -070090func (a *AndroidApp) ExportedStaticPackages() android.Paths {
91 return nil
92}
93
Colin Cross31656952018-05-24 16:11:20 -070094func (a *AndroidApp) ExportedManifest() android.Path {
95 return a.manifestPath
96}
97
Colin Crossa97c5d32018-03-28 14:58:31 -070098var _ AndroidLibraryDependency = (*AndroidApp)(nil)
99
Colin Crosse1731a52017-12-14 11:22:55 -0800100type certificate struct {
101 pem, key android.Path
Colin Cross30e076a2015-04-13 13:58:27 -0700102}
103
Colin Cross46c9b8b2017-06-22 16:51:17 -0700104func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
105 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700106
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800107 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700108 a.aapt.deps(ctx, sdkContext(a))
Colin Cross30e076a2015-04-13 13:58:27 -0700109 }
Colin Crossa4f08812018-10-02 22:03:40 -0700110
111 for _, jniTarget := range ctx.MultiTargets() {
112 variation := []blueprint.Variation{
113 {Mutator: "arch", Variation: jniTarget.String()},
114 {Mutator: "link", Variation: "shared"},
115 }
116 tag := &jniDependencyTag{
117 target: jniTarget,
118 }
119 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
120 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700121
122 cert := android.SrcIsModule(String(a.appProperties.Certificate))
123 if cert != "" {
124 ctx.AddDependency(ctx.Module(), certificateTag, cert)
125 }
126
127 for _, cert := range a.appProperties.Additional_certificates {
128 cert = android.SrcIsModule(cert)
129 if cert != "" {
130 ctx.AddDependency(ctx.Module(), certificateTag, cert)
131 } else {
132 ctx.PropertyErrorf("additional_certificates",
133 `must be names of android_app_certificate modules in the form ":module"`)
134 }
135 }
Colin Cross30e076a2015-04-13 13:58:27 -0700136}
137
Colin Cross46c9b8b2017-06-22 16:51:17 -0700138func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossae5caf52018-05-22 11:11:52 -0700139 a.generateAndroidBuildActions(ctx)
140}
141
142func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
143 linkFlags := append([]string(nil), a.extraLinkFlags...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800144
Colin Crosse78dcd32018-04-19 15:25:19 -0700145 hasProduct := false
146 for _, f := range a.aaptProperties.Aaptflags {
147 if strings.HasPrefix(f, "--product") {
148 hasProduct = true
149 }
150 }
151
152 // Product characteristics
153 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
154 linkFlags = append(linkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
155 }
156
157 // Product AAPT config
158 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
159 linkFlags = append(linkFlags, "-c", aaptConfig)
160 }
161
162 // Product AAPT preferred config
163 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
164 linkFlags = append(linkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
165 }
166
Colin Crossa97c5d32018-03-28 14:58:31 -0700167 // TODO: LOCAL_PACKAGE_OVERRIDES
168 // $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800169
Colin Cross83bb3162018-06-25 15:48:06 -0700170 a.aapt.buildActions(ctx, sdkContext(a), linkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700171
Colin Cross46c9b8b2017-06-22 16:51:17 -0700172 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700173 a.properties.Manifest = nil
Colin Cross30e076a2015-04-13 13:58:27 -0700174
Colin Cross89c31582018-04-30 15:55:11 -0700175 var staticLibProguardFlagFiles android.Paths
176 ctx.VisitDirectDeps(func(m android.Module) {
177 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
178 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
179 }
180 })
181
182 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
183
184 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
185 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800186
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800187 if ctx.ModuleName() != "framework-res" {
188 a.Module.compile(ctx, a.aaptSrcJar)
189 }
Colin Cross30e076a2015-04-13 13:58:27 -0700190
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800191 packageFile := android.PathForModuleOut(ctx, "package.apk")
192
Colin Crossbd01e2a2018-10-04 15:21:03 -0700193 var certificates []certificate
194
Colin Crossa4f08812018-10-02 22:03:40 -0700195 var jniJarFile android.WritablePath
Colin Crossbd01e2a2018-10-04 15:21:03 -0700196 jniLibs, certificateDeps := a.collectAppDeps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700197 if len(jniLibs) > 0 {
198 embedJni := ctx.Config().UnbundledBuild() || a.appProperties.EmbedJNI
199 if embedJni {
200 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
201 TransformJniLibsToJar(ctx, jniJarFile, jniLibs)
202 } else {
203 a.installJniLibs = jniLibs
204 }
205 }
206
Colin Crossbd01e2a2018-10-04 15:21:03 -0700207 if ctx.Failed() {
208 return
209 }
210
211 cert := String(a.appProperties.Certificate)
212 certModule := android.SrcIsModule(cert)
213 if certModule != "" {
214 a.certificate = certificateDeps[0]
215 certificateDeps = certificateDeps[1:]
216 } else if cert != "" {
217 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
218 a.certificate = certificate{
219 defaultDir.Join(ctx, cert+".x509.pem"),
220 defaultDir.Join(ctx, cert+".pk8"),
221 }
222 } else {
223 pem, key := ctx.Config().DefaultAppCertificate(ctx)
224 a.certificate = certificate{pem, key}
225 }
226
227 certificates = append([]certificate{a.certificate}, certificateDeps...)
228
Colin Crossa4f08812018-10-02 22:03:40 -0700229 CreateAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, a.outputFile, certificates)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800230
231 a.outputFile = packageFile
232
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800233 if ctx.ModuleName() == "framework-res" {
234 // framework-res.apk is installed as system/framework/framework-res.apk
235 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".apk", a.outputFile)
Colin Cross16056062017-12-13 22:46:28 -0800236 } else if Bool(a.appProperties.Privileged) {
237 ctx.InstallFile(android.PathForModuleInstall(ctx, "priv-app"), ctx.ModuleName()+".apk", a.outputFile)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800238 } else {
239 ctx.InstallFile(android.PathForModuleInstall(ctx, "app"), ctx.ModuleName()+".apk", a.outputFile)
240 }
Colin Cross30e076a2015-04-13 13:58:27 -0700241}
242
Colin Crossbd01e2a2018-10-04 15:21:03 -0700243func (a *AndroidApp) collectAppDeps(ctx android.ModuleContext) ([]jniLib, []certificate) {
Colin Crossa4f08812018-10-02 22:03:40 -0700244 var jniLibs []jniLib
Colin Crossbd01e2a2018-10-04 15:21:03 -0700245 var certificates []certificate
Colin Crossa4f08812018-10-02 22:03:40 -0700246
247 ctx.VisitDirectDeps(func(module android.Module) {
248 otherName := ctx.OtherModuleName(module)
249 tag := ctx.OtherModuleDependencyTag(module)
250
251 if jniTag, ok := tag.(*jniDependencyTag); ok {
252 if dep, ok := module.(*cc.Module); ok {
253 lib := dep.OutputFile()
254 if lib.Valid() {
255 jniLibs = append(jniLibs, jniLib{
256 name: ctx.OtherModuleName(module),
257 path: lib.Path(),
258 target: jniTag.target,
259 })
260 } else {
261 ctx.ModuleErrorf("dependency %q missing output file", otherName)
262 }
263 } else {
264 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
265
266 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700267 } else if tag == certificateTag {
268 if dep, ok := module.(*AndroidAppCertificate); ok {
269 certificates = append(certificates, dep.certificate)
270 } else {
271 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
272 }
Colin Crossa4f08812018-10-02 22:03:40 -0700273 }
274 })
275
Colin Crossbd01e2a2018-10-04 15:21:03 -0700276 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700277}
278
Colin Cross36242852017-06-23 15:06:31 -0700279func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700280 module := &AndroidApp{}
281
Colin Cross66dbc0b2017-12-28 12:23:20 -0800282 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
283 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
284
Colin Crossae5caf52018-05-22 11:11:52 -0700285 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700286 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700287
Colin Cross36242852017-06-23 15:06:31 -0700288 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700289 &module.Module.properties,
290 &module.Module.deviceProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700291 &module.Module.protoProperties,
292 &module.aaptProperties,
Colin Cross540eff82017-06-22 17:01:52 -0700293 &module.appProperties)
Colin Cross36242852017-06-23 15:06:31 -0700294
Colin Crossa9d8bee2018-10-02 13:59:46 -0700295 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
296 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
297 })
298
Colin Crossa4f08812018-10-02 22:03:40 -0700299 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
300 android.InitDefaultableModule(module)
301
Colin Cross36242852017-06-23 15:06:31 -0700302 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700303}
Colin Crossae5caf52018-05-22 11:11:52 -0700304
305type appTestProperties struct {
306 Instrumentation_for *string
307}
308
309type AndroidTest struct {
310 AndroidApp
311
312 appTestProperties appTestProperties
313
314 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700315
316 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700317 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700318}
319
320func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
321 if String(a.appTestProperties.Instrumentation_for) != "" {
322 a.AndroidApp.extraLinkFlags = append(a.AndroidApp.extraLinkFlags,
323 "--rename-instrumentation-target-package",
324 String(a.appTestProperties.Instrumentation_for))
325 }
326
327 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700328
Jack He33338892018-09-19 02:21:28 -0700329 a.testConfig = tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config, a.testProperties.Test_config_template, a.manifestPath)
Colin Crossd96ca352018-08-10 16:06:24 -0700330 a.data = ctx.ExpandSources(a.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -0700331}
332
333func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
334 android.ExtractSourceDeps(ctx, a.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -0700335 android.ExtractSourceDeps(ctx, a.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -0700336 android.ExtractSourcesDeps(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700337 a.AndroidApp.DepsMutator(ctx)
Colin Crossae5caf52018-05-22 11:11:52 -0700338}
339
340func AndroidTestFactory() android.Module {
341 module := &AndroidTest{}
342
343 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
Colin Cross5067db92018-09-17 16:46:35 -0700344
345 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700346 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossa4f08812018-10-02 22:03:40 -0700347 module.appProperties.EmbedJNI = true
Colin Crossae5caf52018-05-22 11:11:52 -0700348
349 module.AddProperties(
350 &module.Module.properties,
351 &module.Module.deviceProperties,
352 &module.Module.protoProperties,
353 &module.aaptProperties,
354 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700355 &module.appTestProperties,
356 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700357
Colin Crossa4f08812018-10-02 22:03:40 -0700358 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
359 android.InitDefaultableModule(module)
Colin Crossae5caf52018-05-22 11:11:52 -0700360 return module
361}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700362
Colin Cross252fc6f2018-10-04 15:22:03 -0700363type appTestHelperAppProperties struct {
364 // list of compatibility suites (for example "cts", "vts") that the module should be
365 // installed into.
366 Test_suites []string `android:"arch_variant"`
367}
368
369type AndroidTestHelperApp struct {
370 AndroidApp
371
372 appTestHelperAppProperties appTestHelperAppProperties
373}
374
375func AndroidTestHelperAppFactory() android.Module {
376 module := &AndroidTestHelperApp{}
377
378 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
379
380 module.Module.properties.Installable = proptools.BoolPtr(true)
381 module.appProperties.EmbedJNI = true
382
383 module.AddProperties(
384 &module.Module.properties,
385 &module.Module.deviceProperties,
386 &module.Module.protoProperties,
387 &module.aaptProperties,
388 &module.appProperties,
389 &module.appTestHelperAppProperties)
390
391 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
392 android.InitDefaultableModule(module)
393 return module
394}
395
Colin Crossbd01e2a2018-10-04 15:21:03 -0700396type AndroidAppCertificate struct {
397 android.ModuleBase
398 properties AndroidAppCertificateProperties
399 certificate certificate
400}
401
402type AndroidAppCertificateProperties struct {
403 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
404 Certificate *string
405}
406
407func AndroidAppCertificateFactory() android.Module {
408 module := &AndroidAppCertificate{}
409 module.AddProperties(&module.properties)
410 android.InitAndroidModule(module)
411 return module
412}
413
414func (c *AndroidAppCertificate) DepsMutator(ctx android.BottomUpMutatorContext) {
415}
416
417func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
418 cert := String(c.properties.Certificate)
419 c.certificate = certificate{
420 android.PathForModuleSrc(ctx, cert+".x509.pem"),
421 android.PathForModuleSrc(ctx, cert+".pk8"),
422 }
423}