1// 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 (
20	"strings"
21
22	"github.com/google/blueprint/proptools"
23
24	"android/soong/android"
25)
26
27func init() {
28	android.RegisterModuleType("android_app", AndroidAppFactory)
29}
30
31// AndroidManifest.xml merging
32// package splits
33
34type appProperties struct {
35	// path to a certificate, or the name of a certificate in the default
36	// certificate directory, or blank to use the default product certificate
37	Certificate *string
38
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.
44	Export_package_resources *bool
45
46	// 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
50
51	// list of resource labels to generate individual resource packages
52	Package_splits []string
53
54	Instrumentation_for *string
55}
56
57type AndroidApp struct {
58	Library
59	aapt
60
61	certificate certificate
62
63	appProperties appProperties
64}
65
66func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
67	return nil
68}
69
70func (a *AndroidApp) ExportedStaticPackages() android.Paths {
71	return nil
72}
73
74var _ AndroidLibraryDependency = (*AndroidApp)(nil)
75
76type certificate struct {
77	pem, key android.Path
78}
79
80func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
81	a.Module.deps(ctx)
82	if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
83		a.aapt.deps(ctx, String(a.deviceProperties.Sdk_version))
84	}
85}
86
87func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
88	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
95	}
96
97	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
119	// TODO: LOCAL_PACKAGE_OVERRIDES
120	//    $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
121
122	a.aapt.buildActions(ctx, String(a.deviceProperties.Sdk_version), linkFlags...)
123
124	// apps manifests are handled by aapt, don't let Module see them
125	a.properties.Manifest = nil
126
127	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)
138
139	if ctx.ModuleName() != "framework-res" {
140		a.Module.compile(ctx, a.aaptSrcJar)
141	}
142
143	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		}
159	}
160
161	certificates := []certificate{a.certificate}
162	for _, c := range a.appProperties.Additional_certificates {
163		certificates = append(certificates, certificate{
164			android.PathForSource(ctx, c+".x509.pem"),
165			android.PathForSource(ctx, c+".pk8"),
166		})
167	}
168
169	packageFile := android.PathForModuleOut(ctx, "package.apk")
170
171	CreateAppPackage(ctx, packageFile, a.exportPackage, a.outputFile, certificates)
172
173	a.outputFile = packageFile
174
175	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)
178	} else if Bool(a.appProperties.Privileged) {
179		ctx.InstallFile(android.PathForModuleInstall(ctx, "priv-app"), ctx.ModuleName()+".apk", a.outputFile)
180	} else {
181		ctx.InstallFile(android.PathForModuleInstall(ctx, "app"), ctx.ModuleName()+".apk", a.outputFile)
182	}
183}
184
185func AndroidAppFactory() android.Module {
186	module := &AndroidApp{}
187
188	module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
189	module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
190
191	module.AddProperties(
192		&module.Module.properties,
193		&module.Module.deviceProperties,
194		&module.Module.protoProperties,
195		&module.aaptProperties,
196		&module.appProperties)
197
198	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
199	return module
200}
201