blob: 3e24ebc8996b1ebac43e31533fafc071442cf86e [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 Crossa97c5d32018-03-28 14:58:31 -070066var _ AndroidLibraryDependency = (*AndroidApp)(nil)
67
Colin Crosse1731a52017-12-14 11:22:55 -080068type certificate struct {
69 pem, key android.Path
Colin Cross30e076a2015-04-13 13:58:27 -070070}
71
Colin Cross46c9b8b2017-06-22 16:51:17 -070072func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
73 a.Module.deps(ctx)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080074 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Crossa97c5d32018-03-28 14:58:31 -070075 a.aapt.deps(ctx, String(a.deviceProperties.Sdk_version))
Colin Cross30e076a2015-04-13 13:58:27 -070076 }
Colin Cross30e076a2015-04-13 13:58:27 -070077}
78
Colin Cross46c9b8b2017-06-22 16:51:17 -070079func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossa97c5d32018-03-28 14:58:31 -070080 var linkFlags []string
81 if String(a.appProperties.Instrumentation_for) != "" {
82 linkFlags = append(linkFlags,
83 "--rename-instrumentation-target-package",
84 String(a.appProperties.Instrumentation_for))
85 } else {
86 a.properties.Instrument = true
Colin Cross3bc7ffa2017-11-22 16:19:37 -080087 }
88
Colin Crossa97c5d32018-03-28 14:58:31 -070089 // TODO: LOCAL_PACKAGE_OVERRIDES
90 // $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
Colin Cross3bc7ffa2017-11-22 16:19:37 -080091
Colin Crossa97c5d32018-03-28 14:58:31 -070092 a.aapt.buildActions(ctx, String(a.deviceProperties.Sdk_version), linkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -070093
Colin Cross46c9b8b2017-06-22 16:51:17 -070094 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -070095 a.properties.Manifest = nil
Colin Cross30e076a2015-04-13 13:58:27 -070096
Colin Cross66dbc0b2017-12-28 12:23:20 -080097 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles,
Colin Crossa97c5d32018-03-28 14:58:31 -070098 a.proguardOptionsFile)
Colin Cross66dbc0b2017-12-28 12:23:20 -080099
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800100 if ctx.ModuleName() != "framework-res" {
101 a.Module.compile(ctx, a.aaptSrcJar)
102 }
Colin Cross30e076a2015-04-13 13:58:27 -0700103
Colin Crosse1731a52017-12-14 11:22:55 -0800104 c := String(a.appProperties.Certificate)
105 switch {
106 case c == "":
107 pem, key := ctx.Config().DefaultAppCertificate(ctx)
108 a.certificate = certificate{pem, key}
109 case strings.ContainsRune(c, '/'):
110 a.certificate = certificate{
111 android.PathForSource(ctx, c+".x509.pem"),
112 android.PathForSource(ctx, c+".pk8"),
113 }
114 default:
115 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
116 a.certificate = certificate{
117 defaultDir.Join(ctx, c+".x509.pem"),
118 defaultDir.Join(ctx, c+".pk8"),
119 }
Colin Cross30e076a2015-04-13 13:58:27 -0700120 }
121
Colin Crosse1731a52017-12-14 11:22:55 -0800122 certificates := []certificate{a.certificate}
Colin Cross30e076a2015-04-13 13:58:27 -0700123 for _, c := range a.appProperties.Additional_certificates {
Colin Crosse1731a52017-12-14 11:22:55 -0800124 certificates = append(certificates, certificate{
125 android.PathForSource(ctx, c+".x509.pem"),
126 android.PathForSource(ctx, c+".pk8"),
127 })
Colin Cross30e076a2015-04-13 13:58:27 -0700128 }
129
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800130 packageFile := android.PathForModuleOut(ctx, "package.apk")
131
132 CreateAppPackage(ctx, packageFile, a.exportPackage, a.outputFile, certificates)
133
134 a.outputFile = packageFile
135
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800136 if ctx.ModuleName() == "framework-res" {
137 // framework-res.apk is installed as system/framework/framework-res.apk
138 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".apk", a.outputFile)
Colin Cross16056062017-12-13 22:46:28 -0800139 } else if Bool(a.appProperties.Privileged) {
140 ctx.InstallFile(android.PathForModuleInstall(ctx, "priv-app"), ctx.ModuleName()+".apk", a.outputFile)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800141 } else {
142 ctx.InstallFile(android.PathForModuleInstall(ctx, "app"), ctx.ModuleName()+".apk", a.outputFile)
143 }
Colin Cross30e076a2015-04-13 13:58:27 -0700144}
145
Colin Cross36242852017-06-23 15:06:31 -0700146func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700147 module := &AndroidApp{}
148
Colin Cross66dbc0b2017-12-28 12:23:20 -0800149 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
150 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
151
Colin Cross36242852017-06-23 15:06:31 -0700152 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700153 &module.Module.properties,
154 &module.Module.deviceProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700155 &module.Module.protoProperties,
156 &module.aaptProperties,
Colin Cross540eff82017-06-22 17:01:52 -0700157 &module.appProperties)
Colin Cross36242852017-06-23 15:06:31 -0700158
159 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
160 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700161}