xref: /aosp_15_r20/build/soong/java/app.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2015 Google Inc. All rights reserved.
2*333d2b36SAndroid Build Coastguard Worker//
3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker//
7*333d2b36SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker//
9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker// limitations under the License.
14*333d2b36SAndroid Build Coastguard Worker
15*333d2b36SAndroid Build Coastguard Workerpackage java
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Worker// This file contains the module implementations for android_app, android_test, and some more
18*333d2b36SAndroid Build Coastguard Worker// related module types, including their override variants.
19*333d2b36SAndroid Build Coastguard Worker
20*333d2b36SAndroid Build Coastguard Workerimport (
21*333d2b36SAndroid Build Coastguard Worker	"fmt"
22*333d2b36SAndroid Build Coastguard Worker	"path/filepath"
23*333d2b36SAndroid Build Coastguard Worker	"strings"
24*333d2b36SAndroid Build Coastguard Worker
25*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint"
26*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint/depset"
27*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint/proptools"
28*333d2b36SAndroid Build Coastguard Worker
29*333d2b36SAndroid Build Coastguard Worker	"android/soong/android"
30*333d2b36SAndroid Build Coastguard Worker	"android/soong/cc"
31*333d2b36SAndroid Build Coastguard Worker	"android/soong/dexpreopt"
32*333d2b36SAndroid Build Coastguard Worker	"android/soong/tradefed"
33*333d2b36SAndroid Build Coastguard Worker)
34*333d2b36SAndroid Build Coastguard Worker
35*333d2b36SAndroid Build Coastguard Workerfunc init() {
36*333d2b36SAndroid Build Coastguard Worker	RegisterAppBuildComponents(android.InitRegistrationContext)
37*333d2b36SAndroid Build Coastguard Worker	pctx.HostBinToolVariable("ModifyAllowlistCmd", "modify_permissions_allowlist")
38*333d2b36SAndroid Build Coastguard Worker}
39*333d2b36SAndroid Build Coastguard Worker
40*333d2b36SAndroid Build Coastguard Workervar (
41*333d2b36SAndroid Build Coastguard Worker	modifyAllowlist = pctx.AndroidStaticRule("modifyAllowlist",
42*333d2b36SAndroid Build Coastguard Worker		blueprint.RuleParams{
43*333d2b36SAndroid Build Coastguard Worker			Command:     "${ModifyAllowlistCmd} $in $packageName $out",
44*333d2b36SAndroid Build Coastguard Worker			CommandDeps: []string{"${ModifyAllowlistCmd}"},
45*333d2b36SAndroid Build Coastguard Worker		}, "packageName")
46*333d2b36SAndroid Build Coastguard Worker)
47*333d2b36SAndroid Build Coastguard Worker
48*333d2b36SAndroid Build Coastguard Workertype FlagsPackages struct {
49*333d2b36SAndroid Build Coastguard Worker	// Paths to the aconfig dump output text files that are consumed by aapt2
50*333d2b36SAndroid Build Coastguard Worker	AconfigTextFiles android.Paths
51*333d2b36SAndroid Build Coastguard Worker}
52*333d2b36SAndroid Build Coastguard Worker
53*333d2b36SAndroid Build Coastguard Workervar FlagsPackagesProvider = blueprint.NewProvider[FlagsPackages]()
54*333d2b36SAndroid Build Coastguard Worker
55*333d2b36SAndroid Build Coastguard Workerfunc RegisterAppBuildComponents(ctx android.RegistrationContext) {
56*333d2b36SAndroid Build Coastguard Worker	ctx.RegisterModuleType("android_app", AndroidAppFactory)
57*333d2b36SAndroid Build Coastguard Worker	ctx.RegisterModuleType("android_test", AndroidTestFactory)
58*333d2b36SAndroid Build Coastguard Worker	ctx.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
59*333d2b36SAndroid Build Coastguard Worker	ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
60*333d2b36SAndroid Build Coastguard Worker	ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
61*333d2b36SAndroid Build Coastguard Worker	ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory)
62*333d2b36SAndroid Build Coastguard Worker}
63*333d2b36SAndroid Build Coastguard Worker
64*333d2b36SAndroid Build Coastguard Workertype AppInfo struct {
65*333d2b36SAndroid Build Coastguard Worker	// Updatable is set to the value of the updatable property
66*333d2b36SAndroid Build Coastguard Worker	Updatable bool
67*333d2b36SAndroid Build Coastguard Worker
68*333d2b36SAndroid Build Coastguard Worker	// TestHelperApp is true if the module is a android_test_helper_app
69*333d2b36SAndroid Build Coastguard Worker	TestHelperApp bool
70*333d2b36SAndroid Build Coastguard Worker
71*333d2b36SAndroid Build Coastguard Worker	// EmbeddedJNILibs is the list of paths to JNI libraries that were embedded in the APK.
72*333d2b36SAndroid Build Coastguard Worker	EmbeddedJNILibs android.Paths
73*333d2b36SAndroid Build Coastguard Worker}
74*333d2b36SAndroid Build Coastguard Worker
75*333d2b36SAndroid Build Coastguard Workervar AppInfoProvider = blueprint.NewProvider[*AppInfo]()
76*333d2b36SAndroid Build Coastguard Worker
77*333d2b36SAndroid Build Coastguard Worker// AndroidManifest.xml merging
78*333d2b36SAndroid Build Coastguard Worker// package splits
79*333d2b36SAndroid Build Coastguard Worker
80*333d2b36SAndroid Build Coastguard Workertype appProperties struct {
81*333d2b36SAndroid Build Coastguard Worker	// Names of extra android_app_certificate modules to sign the apk with in the form ":module".
82*333d2b36SAndroid Build Coastguard Worker	Additional_certificates []string
83*333d2b36SAndroid Build Coastguard Worker
84*333d2b36SAndroid Build Coastguard Worker	// If set, create package-export.apk, which other packages can
85*333d2b36SAndroid Build Coastguard Worker	// use to get PRODUCT-agnostic resource data like IDs and type definitions.
86*333d2b36SAndroid Build Coastguard Worker	Export_package_resources *bool
87*333d2b36SAndroid Build Coastguard Worker
88*333d2b36SAndroid Build Coastguard Worker	// Specifies that this app should be installed to the priv-app directory,
89*333d2b36SAndroid Build Coastguard Worker	// where the system will grant it additional privileges not available to
90*333d2b36SAndroid Build Coastguard Worker	// normal apps.
91*333d2b36SAndroid Build Coastguard Worker	Privileged *bool
92*333d2b36SAndroid Build Coastguard Worker
93*333d2b36SAndroid Build Coastguard Worker	// list of resource labels to generate individual resource packages
94*333d2b36SAndroid Build Coastguard Worker	Package_splits []string
95*333d2b36SAndroid Build Coastguard Worker
96*333d2b36SAndroid Build Coastguard Worker	// list of native libraries that will be provided in or alongside the resulting jar
97*333d2b36SAndroid Build Coastguard Worker	Jni_libs proptools.Configurable[[]string] `android:"arch_variant"`
98*333d2b36SAndroid Build Coastguard Worker
99*333d2b36SAndroid Build Coastguard Worker	// if true, use JNI libraries that link against platform APIs even if this module sets
100*333d2b36SAndroid Build Coastguard Worker	// sdk_version.
101*333d2b36SAndroid Build Coastguard Worker	Jni_uses_platform_apis *bool
102*333d2b36SAndroid Build Coastguard Worker
103*333d2b36SAndroid Build Coastguard Worker	// if true, use JNI libraries that link against SDK APIs even if this module does not set
104*333d2b36SAndroid Build Coastguard Worker	// sdk_version.
105*333d2b36SAndroid Build Coastguard Worker	Jni_uses_sdk_apis *bool
106*333d2b36SAndroid Build Coastguard Worker
107*333d2b36SAndroid Build Coastguard Worker	// STL library to use for JNI libraries.
108*333d2b36SAndroid Build Coastguard Worker	Stl *string `android:"arch_variant"`
109*333d2b36SAndroid Build Coastguard Worker
110*333d2b36SAndroid Build Coastguard Worker	// Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest
111*333d2b36SAndroid Build Coastguard Worker	// flag so that they are used from inside the APK at runtime.  Defaults to true for android_test modules unless
112*333d2b36SAndroid Build Coastguard Worker	// sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to true for
113*333d2b36SAndroid Build Coastguard Worker	// android_app modules that are embedded to APEXes, defaults to false for other module types where the native
114*333d2b36SAndroid Build Coastguard Worker	// libraries are generally preinstalled outside the APK.
115*333d2b36SAndroid Build Coastguard Worker	Use_embedded_native_libs *bool
116*333d2b36SAndroid Build Coastguard Worker
117*333d2b36SAndroid Build Coastguard Worker	// Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that
118*333d2b36SAndroid Build Coastguard Worker	// they are used from inside the APK at runtime.
119*333d2b36SAndroid Build Coastguard Worker	Use_embedded_dex *bool
120*333d2b36SAndroid Build Coastguard Worker
121*333d2b36SAndroid Build Coastguard Worker	// Forces native libraries to always be packaged into the APK,
122*333d2b36SAndroid Build Coastguard Worker	// Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed.
123*333d2b36SAndroid Build Coastguard Worker	// True for android_test* modules.
124*333d2b36SAndroid Build Coastguard Worker	AlwaysPackageNativeLibs bool `blueprint:"mutated"`
125*333d2b36SAndroid Build Coastguard Worker
126*333d2b36SAndroid Build Coastguard Worker	// If set, find and merge all NOTICE files that this module and its dependencies have and store
127*333d2b36SAndroid Build Coastguard Worker	// it in the APK as an asset.
128*333d2b36SAndroid Build Coastguard Worker	Embed_notices *bool
129*333d2b36SAndroid Build Coastguard Worker
130*333d2b36SAndroid Build Coastguard Worker	// cc.Coverage related properties
131*333d2b36SAndroid Build Coastguard Worker	PreventInstall    bool `blueprint:"mutated"`
132*333d2b36SAndroid Build Coastguard Worker	IsCoverageVariant bool `blueprint:"mutated"`
133*333d2b36SAndroid Build Coastguard Worker
134*333d2b36SAndroid Build Coastguard Worker	// It can be set to test the behaviour of default target sdk version.
135*333d2b36SAndroid Build Coastguard Worker	// Only required when updatable: false. It is an error if updatable: true and this is false.
136*333d2b36SAndroid Build Coastguard Worker	Enforce_default_target_sdk_version *bool
137*333d2b36SAndroid Build Coastguard Worker
138*333d2b36SAndroid Build Coastguard Worker	// If set, the targetSdkVersion for the target is set to the latest default API level.
139*333d2b36SAndroid Build Coastguard Worker	// This would be by default false, unless updatable: true or
140*333d2b36SAndroid Build Coastguard Worker	// enforce_default_target_sdk_version: true in which case this defaults to true.
141*333d2b36SAndroid Build Coastguard Worker	EnforceDefaultTargetSdkVersion bool `blueprint:"mutated"`
142*333d2b36SAndroid Build Coastguard Worker
143*333d2b36SAndroid Build Coastguard Worker	// Whether this app is considered mainline updatable or not. When set to true, this will enforce
144*333d2b36SAndroid Build Coastguard Worker	// additional rules to make sure an app can safely be updated. Default is false.
145*333d2b36SAndroid Build Coastguard Worker	// Prefer using other specific properties if build behaviour must be changed; avoid using this
146*333d2b36SAndroid Build Coastguard Worker	// flag for anything but neverallow rules (unless the behaviour change is invisible to owners).
147*333d2b36SAndroid Build Coastguard Worker	Updatable *bool
148*333d2b36SAndroid Build Coastguard Worker
149*333d2b36SAndroid Build Coastguard Worker	// Specifies the file that contains the allowlist for this app.
150*333d2b36SAndroid Build Coastguard Worker	Privapp_allowlist *string `android:"path"`
151*333d2b36SAndroid Build Coastguard Worker
152*333d2b36SAndroid Build Coastguard Worker	// If set, create an RRO package which contains only resources having PRODUCT_CHARACTERISTICS
153*333d2b36SAndroid Build Coastguard Worker	// and install the RRO package to /product partition, instead of passing --product argument
154*333d2b36SAndroid Build Coastguard Worker	// to aapt2. Default is false.
155*333d2b36SAndroid Build Coastguard Worker	// Setting this will make this APK identical to all targets, regardless of
156*333d2b36SAndroid Build Coastguard Worker	// PRODUCT_CHARACTERISTICS.
157*333d2b36SAndroid Build Coastguard Worker	Generate_product_characteristics_rro *bool
158*333d2b36SAndroid Build Coastguard Worker
159*333d2b36SAndroid Build Coastguard Worker	ProductCharacteristicsRROPackageName        *string `blueprint:"mutated"`
160*333d2b36SAndroid Build Coastguard Worker	ProductCharacteristicsRROManifestModuleName *string `blueprint:"mutated"`
161*333d2b36SAndroid Build Coastguard Worker}
162*333d2b36SAndroid Build Coastguard Worker
163*333d2b36SAndroid Build Coastguard Worker// android_app properties that can be overridden by override_android_app
164*333d2b36SAndroid Build Coastguard Workertype overridableAppProperties struct {
165*333d2b36SAndroid Build Coastguard Worker	// The name of a certificate in the default certificate directory, blank to use the default product certificate,
166*333d2b36SAndroid Build Coastguard Worker	// or an android_app_certificate module name in the form ":module".
167*333d2b36SAndroid Build Coastguard Worker	Certificate proptools.Configurable[string] `android:"replace_instead_of_append"`
168*333d2b36SAndroid Build Coastguard Worker
169*333d2b36SAndroid Build Coastguard Worker	// Name of the signing certificate lineage file or filegroup module.
170*333d2b36SAndroid Build Coastguard Worker	Lineage *string `android:"path"`
171*333d2b36SAndroid Build Coastguard Worker
172*333d2b36SAndroid Build Coastguard Worker	// For overriding the --rotation-min-sdk-version property of apksig
173*333d2b36SAndroid Build Coastguard Worker	RotationMinSdkVersion *string
174*333d2b36SAndroid Build Coastguard Worker
175*333d2b36SAndroid Build Coastguard Worker	// the package name of this app. The package name in the manifest file is used if one was not given.
176*333d2b36SAndroid Build Coastguard Worker	Package_name proptools.Configurable[string]
177*333d2b36SAndroid Build Coastguard Worker
178*333d2b36SAndroid Build Coastguard Worker	// the logging parent of this app.
179*333d2b36SAndroid Build Coastguard Worker	Logging_parent *string
180*333d2b36SAndroid Build Coastguard Worker
181*333d2b36SAndroid Build Coastguard Worker	// Whether to rename the package in resources to the override name rather than the base name. Defaults to true.
182*333d2b36SAndroid Build Coastguard Worker	Rename_resources_package *bool
183*333d2b36SAndroid Build Coastguard Worker
184*333d2b36SAndroid Build Coastguard Worker	// Names of modules to be overridden. Listed modules can only be other binaries
185*333d2b36SAndroid Build Coastguard Worker	// (in Make or Soong).
186*333d2b36SAndroid Build Coastguard Worker	// This does not completely prevent installation of the overridden binaries, but if both
187*333d2b36SAndroid Build Coastguard Worker	// binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
188*333d2b36SAndroid Build Coastguard Worker	// from PRODUCT_PACKAGES.
189*333d2b36SAndroid Build Coastguard Worker	Overrides []string
190*333d2b36SAndroid Build Coastguard Worker}
191*333d2b36SAndroid Build Coastguard Worker
192*333d2b36SAndroid Build Coastguard Workertype AndroidApp struct {
193*333d2b36SAndroid Build Coastguard Worker	Library
194*333d2b36SAndroid Build Coastguard Worker	aapt
195*333d2b36SAndroid Build Coastguard Worker	android.OverridableModuleBase
196*333d2b36SAndroid Build Coastguard Worker
197*333d2b36SAndroid Build Coastguard Worker	certificate Certificate
198*333d2b36SAndroid Build Coastguard Worker
199*333d2b36SAndroid Build Coastguard Worker	appProperties appProperties
200*333d2b36SAndroid Build Coastguard Worker
201*333d2b36SAndroid Build Coastguard Worker	overridableAppProperties overridableAppProperties
202*333d2b36SAndroid Build Coastguard Worker
203*333d2b36SAndroid Build Coastguard Worker	jniLibs                  []jniLib
204*333d2b36SAndroid Build Coastguard Worker	installPathForJNISymbols android.Path
205*333d2b36SAndroid Build Coastguard Worker	embeddedJniLibs          bool
206*333d2b36SAndroid Build Coastguard Worker	jniCoverageOutputs       android.Paths
207*333d2b36SAndroid Build Coastguard Worker
208*333d2b36SAndroid Build Coastguard Worker	bundleFile android.Path
209*333d2b36SAndroid Build Coastguard Worker
210*333d2b36SAndroid Build Coastguard Worker	// the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
211*333d2b36SAndroid Build Coastguard Worker	installApkName string
212*333d2b36SAndroid Build Coastguard Worker
213*333d2b36SAndroid Build Coastguard Worker	installDir android.InstallPath
214*333d2b36SAndroid Build Coastguard Worker
215*333d2b36SAndroid Build Coastguard Worker	onDeviceDir string
216*333d2b36SAndroid Build Coastguard Worker
217*333d2b36SAndroid Build Coastguard Worker	additionalAaptFlags []string
218*333d2b36SAndroid Build Coastguard Worker
219*333d2b36SAndroid Build Coastguard Worker	overriddenManifestPackageName string
220*333d2b36SAndroid Build Coastguard Worker
221*333d2b36SAndroid Build Coastguard Worker	android.ApexBundleDepsInfo
222*333d2b36SAndroid Build Coastguard Worker
223*333d2b36SAndroid Build Coastguard Worker	javaApiUsedByOutputFile android.ModuleOutPath
224*333d2b36SAndroid Build Coastguard Worker
225*333d2b36SAndroid Build Coastguard Worker	privAppAllowlist android.OptionalPath
226*333d2b36SAndroid Build Coastguard Worker
227*333d2b36SAndroid Build Coastguard Worker	requiredModuleNames []string
228*333d2b36SAndroid Build Coastguard Worker}
229*333d2b36SAndroid Build Coastguard Worker
230*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) IsInstallable() bool {
231*333d2b36SAndroid Build Coastguard Worker	return Bool(a.properties.Installable)
232*333d2b36SAndroid Build Coastguard Worker}
233*333d2b36SAndroid Build Coastguard Worker
234*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) ResourcesNodeDepSet() depset.DepSet[*resourcesNode] {
235*333d2b36SAndroid Build Coastguard Worker	return a.aapt.resourcesNodesDepSet
236*333d2b36SAndroid Build Coastguard Worker}
237*333d2b36SAndroid Build Coastguard Worker
238*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) OutputFile() android.Path {
239*333d2b36SAndroid Build Coastguard Worker	return a.outputFile
240*333d2b36SAndroid Build Coastguard Worker}
241*333d2b36SAndroid Build Coastguard Worker
242*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) Certificate() Certificate {
243*333d2b36SAndroid Build Coastguard Worker	return a.certificate
244*333d2b36SAndroid Build Coastguard Worker}
245*333d2b36SAndroid Build Coastguard Worker
246*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) JniCoverageOutputs() android.Paths {
247*333d2b36SAndroid Build Coastguard Worker	return a.jniCoverageOutputs
248*333d2b36SAndroid Build Coastguard Worker}
249*333d2b36SAndroid Build Coastguard Worker
250*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) PrivAppAllowlist() android.OptionalPath {
251*333d2b36SAndroid Build Coastguard Worker	return a.privAppAllowlist
252*333d2b36SAndroid Build Coastguard Worker}
253*333d2b36SAndroid Build Coastguard Worker
254*333d2b36SAndroid Build Coastguard Workervar _ AndroidLibraryDependency = (*AndroidApp)(nil)
255*333d2b36SAndroid Build Coastguard Worker
256*333d2b36SAndroid Build Coastguard Workertype Certificate struct {
257*333d2b36SAndroid Build Coastguard Worker	Pem, Key  android.Path
258*333d2b36SAndroid Build Coastguard Worker	presigned bool
259*333d2b36SAndroid Build Coastguard Worker}
260*333d2b36SAndroid Build Coastguard Worker
261*333d2b36SAndroid Build Coastguard Workervar PresignedCertificate = Certificate{presigned: true}
262*333d2b36SAndroid Build Coastguard Worker
263*333d2b36SAndroid Build Coastguard Workerfunc (c Certificate) AndroidMkString() string {
264*333d2b36SAndroid Build Coastguard Worker	if c.presigned {
265*333d2b36SAndroid Build Coastguard Worker		return "PRESIGNED"
266*333d2b36SAndroid Build Coastguard Worker	} else {
267*333d2b36SAndroid Build Coastguard Worker		return c.Pem.String()
268*333d2b36SAndroid Build Coastguard Worker	}
269*333d2b36SAndroid Build Coastguard Worker}
270*333d2b36SAndroid Build Coastguard Worker
271*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
272*333d2b36SAndroid Build Coastguard Worker	if String(a.appProperties.Stl) == "c++_shared" && !a.SdkVersion(ctx).Specified() {
273*333d2b36SAndroid Build Coastguard Worker		ctx.PropertyErrorf("stl", "sdk_version must be set in order to use c++_shared")
274*333d2b36SAndroid Build Coastguard Worker	}
275*333d2b36SAndroid Build Coastguard Worker
276*333d2b36SAndroid Build Coastguard Worker	sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
277*333d2b36SAndroid Build Coastguard Worker	a.usesLibrary.deps(ctx, sdkDep.hasFrameworkLibs())
278*333d2b36SAndroid Build Coastguard Worker	a.Module.deps(ctx)
279*333d2b36SAndroid Build Coastguard Worker	if sdkDep.hasFrameworkLibs() {
280*333d2b36SAndroid Build Coastguard Worker		a.aapt.deps(ctx, sdkDep)
281*333d2b36SAndroid Build Coastguard Worker	}
282*333d2b36SAndroid Build Coastguard Worker
283*333d2b36SAndroid Build Coastguard Worker	usesSDK := a.SdkVersion(ctx).Specified() && a.SdkVersion(ctx).Kind != android.SdkCorePlatform
284*333d2b36SAndroid Build Coastguard Worker
285*333d2b36SAndroid Build Coastguard Worker	if usesSDK && Bool(a.appProperties.Jni_uses_sdk_apis) {
286*333d2b36SAndroid Build Coastguard Worker		ctx.PropertyErrorf("jni_uses_sdk_apis",
287*333d2b36SAndroid Build Coastguard Worker			"can only be set for modules that do not set sdk_version")
288*333d2b36SAndroid Build Coastguard Worker	} else if !usesSDK && Bool(a.appProperties.Jni_uses_platform_apis) {
289*333d2b36SAndroid Build Coastguard Worker		ctx.PropertyErrorf("jni_uses_platform_apis",
290*333d2b36SAndroid Build Coastguard Worker			"can only be set for modules that set sdk_version")
291*333d2b36SAndroid Build Coastguard Worker	}
292*333d2b36SAndroid Build Coastguard Worker
293*333d2b36SAndroid Build Coastguard Worker	for _, jniTarget := range ctx.MultiTargets() {
294*333d2b36SAndroid Build Coastguard Worker		variation := append(jniTarget.Variations(),
295*333d2b36SAndroid Build Coastguard Worker			blueprint.Variation{Mutator: "link", Variation: "shared"})
296*333d2b36SAndroid Build Coastguard Worker
297*333d2b36SAndroid Build Coastguard Worker		// Test whether to use the SDK variant or the non-SDK variant of JNI dependencies.
298*333d2b36SAndroid Build Coastguard Worker		// Many factors are considered here.
299*333d2b36SAndroid Build Coastguard Worker		// 1. Basically, the selection follows whether the app has sdk_version set or not.
300*333d2b36SAndroid Build Coastguard Worker		jniUsesSdkVariant := usesSDK
301*333d2b36SAndroid Build Coastguard Worker		// 2. However, jni_uses_platform_apis and jni_uses_sdk_apis can override it
302*333d2b36SAndroid Build Coastguard Worker		if Bool(a.appProperties.Jni_uses_sdk_apis) {
303*333d2b36SAndroid Build Coastguard Worker			jniUsesSdkVariant = true
304*333d2b36SAndroid Build Coastguard Worker		}
305*333d2b36SAndroid Build Coastguard Worker		if Bool(a.appProperties.Jni_uses_platform_apis) {
306*333d2b36SAndroid Build Coastguard Worker			jniUsesSdkVariant = false
307*333d2b36SAndroid Build Coastguard Worker		}
308*333d2b36SAndroid Build Coastguard Worker		// 3. Then the use of SDK variant is again prohibited for the following cases:
309*333d2b36SAndroid Build Coastguard Worker		// 3.1. the app is shipped on unbundled partitions like vendor. Since the entire
310*333d2b36SAndroid Build Coastguard Worker		// partition (not only the app) is considered unbudled, there's no need to use the
311*333d2b36SAndroid Build Coastguard Worker		// SDK variant.
312*333d2b36SAndroid Build Coastguard Worker		// 3.2. the app doesn't support embedding the JNI libs
313*333d2b36SAndroid Build Coastguard Worker		if a.RequiresStableAPIs(ctx) || !a.shouldEmbedJnis(ctx) {
314*333d2b36SAndroid Build Coastguard Worker			jniUsesSdkVariant = false
315*333d2b36SAndroid Build Coastguard Worker		}
316*333d2b36SAndroid Build Coastguard Worker		if jniUsesSdkVariant {
317*333d2b36SAndroid Build Coastguard Worker			variation = append(variation, blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
318*333d2b36SAndroid Build Coastguard Worker		}
319*333d2b36SAndroid Build Coastguard Worker
320*333d2b36SAndroid Build Coastguard Worker		// Use the installable dep tag when the JNIs are not embedded
321*333d2b36SAndroid Build Coastguard Worker		var tag dependencyTag
322*333d2b36SAndroid Build Coastguard Worker		if a.shouldEmbedJnis(ctx) {
323*333d2b36SAndroid Build Coastguard Worker			tag = jniLibTag
324*333d2b36SAndroid Build Coastguard Worker		} else {
325*333d2b36SAndroid Build Coastguard Worker			tag = jniInstallTag
326*333d2b36SAndroid Build Coastguard Worker		}
327*333d2b36SAndroid Build Coastguard Worker		ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs.GetOrDefault(ctx, nil)...)
328*333d2b36SAndroid Build Coastguard Worker	}
329*333d2b36SAndroid Build Coastguard Worker	for _, aconfig_declaration := range a.aaptProperties.Flags_packages {
330*333d2b36SAndroid Build Coastguard Worker		ctx.AddDependency(ctx.Module(), aconfigDeclarationTag, aconfig_declaration)
331*333d2b36SAndroid Build Coastguard Worker	}
332*333d2b36SAndroid Build Coastguard Worker}
333*333d2b36SAndroid Build Coastguard Worker
334*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
335*333d2b36SAndroid Build Coastguard Worker	cert := android.SrcIsModule(a.getCertString(ctx))
336*333d2b36SAndroid Build Coastguard Worker	if cert != "" {
337*333d2b36SAndroid Build Coastguard Worker		ctx.AddDependency(ctx.Module(), certificateTag, cert)
338*333d2b36SAndroid Build Coastguard Worker	}
339*333d2b36SAndroid Build Coastguard Worker
340*333d2b36SAndroid Build Coastguard Worker	if a.appProperties.Privapp_allowlist != nil && !Bool(a.appProperties.Privileged) {
341*333d2b36SAndroid Build Coastguard Worker		// There are a few uids that are explicitly considered privileged regardless of their
342*333d2b36SAndroid Build Coastguard Worker		// app's location. Bluetooth is one such app. It should arguably be moved to priv-app,
343*333d2b36SAndroid Build Coastguard Worker		// but for now, allow it not to be in priv-app.
344*333d2b36SAndroid Build Coastguard Worker		privilegedBecauseOfUid := ctx.ModuleName() == "Bluetooth"
345*333d2b36SAndroid Build Coastguard Worker		if !privilegedBecauseOfUid {
346*333d2b36SAndroid Build Coastguard Worker			ctx.PropertyErrorf("privapp_allowlist", "privileged must be set in order to use privapp_allowlist (with a few exceptions)")
347*333d2b36SAndroid Build Coastguard Worker		}
348*333d2b36SAndroid Build Coastguard Worker	}
349*333d2b36SAndroid Build Coastguard Worker
350*333d2b36SAndroid Build Coastguard Worker	for _, cert := range a.appProperties.Additional_certificates {
351*333d2b36SAndroid Build Coastguard Worker		cert = android.SrcIsModule(cert)
352*333d2b36SAndroid Build Coastguard Worker		if cert != "" {
353*333d2b36SAndroid Build Coastguard Worker			ctx.AddDependency(ctx.Module(), certificateTag, cert)
354*333d2b36SAndroid Build Coastguard Worker		} else {
355*333d2b36SAndroid Build Coastguard Worker			ctx.PropertyErrorf("additional_certificates",
356*333d2b36SAndroid Build Coastguard Worker				`must be names of android_app_certificate modules in the form ":module"`)
357*333d2b36SAndroid Build Coastguard Worker		}
358*333d2b36SAndroid Build Coastguard Worker	}
359*333d2b36SAndroid Build Coastguard Worker}
360*333d2b36SAndroid Build Coastguard Worker
361*333d2b36SAndroid Build Coastguard Worker// TODO(b/156476221): Remove this allowlist
362*333d2b36SAndroid Build Coastguard Workervar (
363*333d2b36SAndroid Build Coastguard Worker	missingMinSdkVersionMtsAllowlist = []string{
364*333d2b36SAndroid Build Coastguard Worker		"CellBroadcastReceiverGoogleUnitTests",
365*333d2b36SAndroid Build Coastguard Worker		"CellBroadcastReceiverUnitTests",
366*333d2b36SAndroid Build Coastguard Worker		"CtsBatterySavingTestCases",
367*333d2b36SAndroid Build Coastguard Worker		"CtsDeviceAndProfileOwnerApp23",
368*333d2b36SAndroid Build Coastguard Worker		"CtsDeviceAndProfileOwnerApp30",
369*333d2b36SAndroid Build Coastguard Worker		"CtsIntentSenderApp",
370*333d2b36SAndroid Build Coastguard Worker		"CtsJobSchedulerTestCases",
371*333d2b36SAndroid Build Coastguard Worker		"CtsMimeMapTestCases",
372*333d2b36SAndroid Build Coastguard Worker		"CtsTareTestCases",
373*333d2b36SAndroid Build Coastguard Worker		"LibStatsPullTests",
374*333d2b36SAndroid Build Coastguard Worker		"MediaProviderClientTests",
375*333d2b36SAndroid Build Coastguard Worker		"TeleServiceTests",
376*333d2b36SAndroid Build Coastguard Worker		"TestExternalImsServiceApp",
377*333d2b36SAndroid Build Coastguard Worker		"TestSmsRetrieverApp",
378*333d2b36SAndroid Build Coastguard Worker		"TetheringPrivilegedTests",
379*333d2b36SAndroid Build Coastguard Worker	}
380*333d2b36SAndroid Build Coastguard Worker)
381*333d2b36SAndroid Build Coastguard Worker
382*333d2b36SAndroid Build Coastguard Workerfunc checkMinSdkVersionMts(ctx android.ModuleContext, minSdkVersion android.ApiLevel) {
383*333d2b36SAndroid Build Coastguard Worker	if includedInMts(ctx.Module()) && !minSdkVersion.Specified() && !android.InList(ctx.ModuleName(), missingMinSdkVersionMtsAllowlist) {
384*333d2b36SAndroid Build Coastguard Worker		ctx.PropertyErrorf("min_sdk_version", "min_sdk_version is a required property for tests included in MTS")
385*333d2b36SAndroid Build Coastguard Worker	}
386*333d2b36SAndroid Build Coastguard Worker}
387*333d2b36SAndroid Build Coastguard Worker
388*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidTestHelperApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
389*333d2b36SAndroid Build Coastguard Worker	checkMinSdkVersionMts(ctx, a.MinSdkVersion(ctx))
390*333d2b36SAndroid Build Coastguard Worker	applicationId := a.appTestHelperAppProperties.Manifest_values.ApplicationId
391*333d2b36SAndroid Build Coastguard Worker	if applicationId != nil {
392*333d2b36SAndroid Build Coastguard Worker		packageName := a.overridableAppProperties.Package_name.Get(ctx)
393*333d2b36SAndroid Build Coastguard Worker		if packageName.IsPresent() {
394*333d2b36SAndroid Build Coastguard Worker			ctx.PropertyErrorf("manifest_values.applicationId", "property is not supported when property package_name is set.")
395*333d2b36SAndroid Build Coastguard Worker		}
396*333d2b36SAndroid Build Coastguard Worker		a.aapt.manifestValues.applicationId = *applicationId
397*333d2b36SAndroid Build Coastguard Worker	}
398*333d2b36SAndroid Build Coastguard Worker	a.generateAndroidBuildActions(ctx)
399*333d2b36SAndroid Build Coastguard Worker	android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{
400*333d2b36SAndroid Build Coastguard Worker		TestOnly: true,
401*333d2b36SAndroid Build Coastguard Worker	})
402*333d2b36SAndroid Build Coastguard Worker	android.SetProvider(ctx, AppInfoProvider, &AppInfo{
403*333d2b36SAndroid Build Coastguard Worker		Updatable:     Bool(a.appProperties.Updatable),
404*333d2b36SAndroid Build Coastguard Worker		TestHelperApp: true,
405*333d2b36SAndroid Build Coastguard Worker	})
406*333d2b36SAndroid Build Coastguard Worker}
407*333d2b36SAndroid Build Coastguard Worker
408*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
409*333d2b36SAndroid Build Coastguard Worker	a.checkAppSdkVersions(ctx)
410*333d2b36SAndroid Build Coastguard Worker	a.checkEmbedJnis(ctx)
411*333d2b36SAndroid Build Coastguard Worker	a.generateAndroidBuildActions(ctx)
412*333d2b36SAndroid Build Coastguard Worker	a.generateJavaUsedByApex(ctx)
413*333d2b36SAndroid Build Coastguard Worker
414*333d2b36SAndroid Build Coastguard Worker	var embeddedJniLibs []android.Path
415*333d2b36SAndroid Build Coastguard Worker
416*333d2b36SAndroid Build Coastguard Worker	if a.embeddedJniLibs {
417*333d2b36SAndroid Build Coastguard Worker		for _, jni := range a.jniLibs {
418*333d2b36SAndroid Build Coastguard Worker			embeddedJniLibs = append(embeddedJniLibs, jni.path)
419*333d2b36SAndroid Build Coastguard Worker		}
420*333d2b36SAndroid Build Coastguard Worker	}
421*333d2b36SAndroid Build Coastguard Worker	android.SetProvider(ctx, AppInfoProvider, &AppInfo{
422*333d2b36SAndroid Build Coastguard Worker		Updatable:       Bool(a.appProperties.Updatable),
423*333d2b36SAndroid Build Coastguard Worker		TestHelperApp:   false,
424*333d2b36SAndroid Build Coastguard Worker		EmbeddedJNILibs: embeddedJniLibs,
425*333d2b36SAndroid Build Coastguard Worker	})
426*333d2b36SAndroid Build Coastguard Worker
427*333d2b36SAndroid Build Coastguard Worker	a.requiredModuleNames = a.getRequiredModuleNames(ctx)
428*333d2b36SAndroid Build Coastguard Worker}
429*333d2b36SAndroid Build Coastguard Worker
430*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) getRequiredModuleNames(ctx android.ModuleContext) []string {
431*333d2b36SAndroid Build Coastguard Worker	var required []string
432*333d2b36SAndroid Build Coastguard Worker	if proptools.Bool(a.appProperties.Generate_product_characteristics_rro) {
433*333d2b36SAndroid Build Coastguard Worker		required = []string{a.productCharacteristicsRROPackageName()}
434*333d2b36SAndroid Build Coastguard Worker	}
435*333d2b36SAndroid Build Coastguard Worker	// Install the vendor overlay variant if this app is installed.
436*333d2b36SAndroid Build Coastguard Worker	if len(filterRRO(a.rroDirsDepSet, device)) > 0 {
437*333d2b36SAndroid Build Coastguard Worker		required = append(required, AutogeneratedRroModuleName(ctx, ctx.Module().Name(), "vendor"))
438*333d2b36SAndroid Build Coastguard Worker	}
439*333d2b36SAndroid Build Coastguard Worker	// Install the product overlay variant if this app is installed.
440*333d2b36SAndroid Build Coastguard Worker	if len(filterRRO(a.rroDirsDepSet, product)) > 0 {
441*333d2b36SAndroid Build Coastguard Worker		required = append(required, AutogeneratedRroModuleName(ctx, ctx.Module().Name(), "product"))
442*333d2b36SAndroid Build Coastguard Worker	}
443*333d2b36SAndroid Build Coastguard Worker	return required
444*333d2b36SAndroid Build Coastguard Worker}
445*333d2b36SAndroid Build Coastguard Worker
446*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) checkAppSdkVersions(ctx android.ModuleContext) {
447*333d2b36SAndroid Build Coastguard Worker	if a.Updatable() {
448*333d2b36SAndroid Build Coastguard Worker		if !a.SdkVersion(ctx).Stable() {
449*333d2b36SAndroid Build Coastguard Worker			ctx.PropertyErrorf("sdk_version", "Updatable apps must use stable SDKs, found %v", a.SdkVersion(ctx))
450*333d2b36SAndroid Build Coastguard Worker		}
451*333d2b36SAndroid Build Coastguard Worker		if String(a.overridableProperties.Min_sdk_version) == "" {
452*333d2b36SAndroid Build Coastguard Worker			ctx.PropertyErrorf("updatable", "updatable apps must set min_sdk_version.")
453*333d2b36SAndroid Build Coastguard Worker		}
454*333d2b36SAndroid Build Coastguard Worker
455*333d2b36SAndroid Build Coastguard Worker		if minSdkVersion, err := a.MinSdkVersion(ctx).EffectiveVersion(ctx); err == nil {
456*333d2b36SAndroid Build Coastguard Worker			a.checkJniLibsSdkVersion(ctx, minSdkVersion)
457*333d2b36SAndroid Build Coastguard Worker			android.CheckMinSdkVersion(ctx, minSdkVersion, a.WalkPayloadDeps)
458*333d2b36SAndroid Build Coastguard Worker		} else {
459*333d2b36SAndroid Build Coastguard Worker			ctx.PropertyErrorf("min_sdk_version", "%s", err.Error())
460*333d2b36SAndroid Build Coastguard Worker		}
461*333d2b36SAndroid Build Coastguard Worker
462*333d2b36SAndroid Build Coastguard Worker		if !BoolDefault(a.appProperties.Enforce_default_target_sdk_version, true) {
463*333d2b36SAndroid Build Coastguard Worker			ctx.PropertyErrorf("enforce_default_target_sdk_version", "Updatable apps must enforce default target sdk version")
464*333d2b36SAndroid Build Coastguard Worker		}
465*333d2b36SAndroid Build Coastguard Worker		// TODO(b/227460469) after all the modules removes the target sdk version, throw an error if the target sdk version is explicitly set.
466*333d2b36SAndroid Build Coastguard Worker		if a.deviceProperties.Target_sdk_version == nil {
467*333d2b36SAndroid Build Coastguard Worker			a.SetEnforceDefaultTargetSdkVersion(true)
468*333d2b36SAndroid Build Coastguard Worker		}
469*333d2b36SAndroid Build Coastguard Worker	}
470*333d2b36SAndroid Build Coastguard Worker
471*333d2b36SAndroid Build Coastguard Worker	a.checkPlatformAPI(ctx)
472*333d2b36SAndroid Build Coastguard Worker	a.checkSdkVersions(ctx)
473*333d2b36SAndroid Build Coastguard Worker}
474*333d2b36SAndroid Build Coastguard Worker
475*333d2b36SAndroid Build Coastguard Worker// Ensures that use_embedded_native_libs are set for apk-in-apex
476*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) checkEmbedJnis(ctx android.BaseModuleContext) {
477*333d2b36SAndroid Build Coastguard Worker	apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
478*333d2b36SAndroid Build Coastguard Worker	apkInApex := !apexInfo.IsForPlatform()
479*333d2b36SAndroid Build Coastguard Worker	hasJnis := len(a.appProperties.Jni_libs.GetOrDefault(ctx, nil)) > 0
480*333d2b36SAndroid Build Coastguard Worker
481*333d2b36SAndroid Build Coastguard Worker	if apkInApex && hasJnis && !Bool(a.appProperties.Use_embedded_native_libs) {
482*333d2b36SAndroid Build Coastguard Worker		ctx.ModuleErrorf("APK in APEX should have use_embedded_native_libs: true")
483*333d2b36SAndroid Build Coastguard Worker	}
484*333d2b36SAndroid Build Coastguard Worker}
485*333d2b36SAndroid Build Coastguard Worker
486*333d2b36SAndroid Build Coastguard Worker// If an updatable APK sets min_sdk_version, min_sdk_vesion of JNI libs should match with it.
487*333d2b36SAndroid Build Coastguard Worker// This check is enforced for "updatable" APKs (including APK-in-APEX).
488*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) checkJniLibsSdkVersion(ctx android.ModuleContext, minSdkVersion android.ApiLevel) {
489*333d2b36SAndroid Build Coastguard Worker	// It's enough to check direct JNI deps' sdk_version because all transitive deps from JNI deps are checked in cc.checkLinkType()
490*333d2b36SAndroid Build Coastguard Worker	ctx.VisitDirectDeps(func(m android.Module) {
491*333d2b36SAndroid Build Coastguard Worker		if !IsJniDepTag(ctx.OtherModuleDependencyTag(m)) {
492*333d2b36SAndroid Build Coastguard Worker			return
493*333d2b36SAndroid Build Coastguard Worker		}
494*333d2b36SAndroid Build Coastguard Worker		dep, _ := m.(*cc.Module)
495*333d2b36SAndroid Build Coastguard Worker		// The domain of cc.sdk_version is "current" and <number>
496*333d2b36SAndroid Build Coastguard Worker		// We can rely on android.SdkSpec to convert it to <number> so that "current" is
497*333d2b36SAndroid Build Coastguard Worker		// handled properly regardless of sdk finalization.
498*333d2b36SAndroid Build Coastguard Worker		jniSdkVersion, err := android.SdkSpecFrom(ctx, dep.MinSdkVersion()).EffectiveVersion(ctx)
499*333d2b36SAndroid Build Coastguard Worker		if err != nil || minSdkVersion.LessThan(jniSdkVersion) {
500*333d2b36SAndroid Build Coastguard Worker			ctx.OtherModuleErrorf(dep, "min_sdk_version(%v) is higher than min_sdk_version(%v) of the containing android_app(%v)",
501*333d2b36SAndroid Build Coastguard Worker				dep.MinSdkVersion(), minSdkVersion, ctx.ModuleName())
502*333d2b36SAndroid Build Coastguard Worker			return
503*333d2b36SAndroid Build Coastguard Worker		}
504*333d2b36SAndroid Build Coastguard Worker
505*333d2b36SAndroid Build Coastguard Worker	})
506*333d2b36SAndroid Build Coastguard Worker}
507*333d2b36SAndroid Build Coastguard Worker
508*333d2b36SAndroid Build Coastguard Worker// Returns true if the native libraries should be stored in the APK uncompressed and the
509*333d2b36SAndroid Build Coastguard Worker// extractNativeLibs application flag should be set to false in the manifest.
510*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) useEmbeddedNativeLibs(ctx android.ModuleContext) bool {
511*333d2b36SAndroid Build Coastguard Worker	minSdkVersion, err := a.MinSdkVersion(ctx).EffectiveVersion(ctx)
512*333d2b36SAndroid Build Coastguard Worker	if err != nil {
513*333d2b36SAndroid Build Coastguard Worker		ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.MinSdkVersion(ctx), err)
514*333d2b36SAndroid Build Coastguard Worker	}
515*333d2b36SAndroid Build Coastguard Worker
516*333d2b36SAndroid Build Coastguard Worker	apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
517*333d2b36SAndroid Build Coastguard Worker	return (minSdkVersion.FinalOrFutureInt() >= 23 && Bool(a.appProperties.Use_embedded_native_libs)) ||
518*333d2b36SAndroid Build Coastguard Worker		!apexInfo.IsForPlatform()
519*333d2b36SAndroid Build Coastguard Worker}
520*333d2b36SAndroid Build Coastguard Worker
521*333d2b36SAndroid Build Coastguard Worker// Returns whether this module should have the dex file stored uncompressed in the APK.
522*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
523*333d2b36SAndroid Build Coastguard Worker	if Bool(a.appProperties.Use_embedded_dex) {
524*333d2b36SAndroid Build Coastguard Worker		return true
525*333d2b36SAndroid Build Coastguard Worker	}
526*333d2b36SAndroid Build Coastguard Worker
527*333d2b36SAndroid Build Coastguard Worker	// Uncompress dex in APKs of privileged apps (even for unbundled builds, they may
528*333d2b36SAndroid Build Coastguard Worker	// be preinstalled as prebuilts).
529*333d2b36SAndroid Build Coastguard Worker	if ctx.Config().UncompressPrivAppDex() && a.Privileged() {
530*333d2b36SAndroid Build Coastguard Worker		return true
531*333d2b36SAndroid Build Coastguard Worker	}
532*333d2b36SAndroid Build Coastguard Worker
533*333d2b36SAndroid Build Coastguard Worker	if ctx.Config().UnbundledBuild() {
534*333d2b36SAndroid Build Coastguard Worker		return false
535*333d2b36SAndroid Build Coastguard Worker	}
536*333d2b36SAndroid Build Coastguard Worker
537*333d2b36SAndroid Build Coastguard Worker	return shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &a.dexpreopter)
538*333d2b36SAndroid Build Coastguard Worker}
539*333d2b36SAndroid Build Coastguard Worker
540*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) shouldEmbedJnis(ctx android.BaseModuleContext) bool {
541*333d2b36SAndroid Build Coastguard Worker	return ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) ||
542*333d2b36SAndroid Build Coastguard Worker		Bool(a.appProperties.Updatable) ||
543*333d2b36SAndroid Build Coastguard Worker		a.appProperties.AlwaysPackageNativeLibs
544*333d2b36SAndroid Build Coastguard Worker}
545*333d2b36SAndroid Build Coastguard Worker
546*333d2b36SAndroid Build Coastguard Workerfunc generateAaptRenamePackageFlags(packageName string, renameResourcesPackage bool) []string {
547*333d2b36SAndroid Build Coastguard Worker	aaptFlags := []string{"--rename-manifest-package " + packageName}
548*333d2b36SAndroid Build Coastguard Worker	if renameResourcesPackage {
549*333d2b36SAndroid Build Coastguard Worker		// Required to rename the package name in the resources table.
550*333d2b36SAndroid Build Coastguard Worker		aaptFlags = append(aaptFlags, "--rename-resources-package "+packageName)
551*333d2b36SAndroid Build Coastguard Worker	}
552*333d2b36SAndroid Build Coastguard Worker	return aaptFlags
553*333d2b36SAndroid Build Coastguard Worker}
554*333d2b36SAndroid Build Coastguard Worker
555*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) OverriddenManifestPackageName() string {
556*333d2b36SAndroid Build Coastguard Worker	return a.overriddenManifestPackageName
557*333d2b36SAndroid Build Coastguard Worker}
558*333d2b36SAndroid Build Coastguard Worker
559*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) renameResourcesPackage() bool {
560*333d2b36SAndroid Build Coastguard Worker	return proptools.BoolDefault(a.overridableAppProperties.Rename_resources_package, true)
561*333d2b36SAndroid Build Coastguard Worker}
562*333d2b36SAndroid Build Coastguard Worker
563*333d2b36SAndroid Build Coastguard Workerfunc getAconfigFilePaths(ctx android.ModuleContext) (aconfigTextFilePaths android.Paths) {
564*333d2b36SAndroid Build Coastguard Worker	ctx.VisitDirectDeps(func(dep android.Module) {
565*333d2b36SAndroid Build Coastguard Worker		tag := ctx.OtherModuleDependencyTag(dep)
566*333d2b36SAndroid Build Coastguard Worker		switch tag {
567*333d2b36SAndroid Build Coastguard Worker		case staticLibTag:
568*333d2b36SAndroid Build Coastguard Worker			if flagPackages, ok := android.OtherModuleProvider(ctx, dep, FlagsPackagesProvider); ok {
569*333d2b36SAndroid Build Coastguard Worker				aconfigTextFilePaths = append(aconfigTextFilePaths, flagPackages.AconfigTextFiles...)
570*333d2b36SAndroid Build Coastguard Worker			}
571*333d2b36SAndroid Build Coastguard Worker
572*333d2b36SAndroid Build Coastguard Worker		case aconfigDeclarationTag:
573*333d2b36SAndroid Build Coastguard Worker			if provider, ok := android.OtherModuleProvider(ctx, dep, android.AconfigDeclarationsProviderKey); ok {
574*333d2b36SAndroid Build Coastguard Worker				aconfigTextFilePaths = append(aconfigTextFilePaths, provider.IntermediateDumpOutputPath)
575*333d2b36SAndroid Build Coastguard Worker			} else {
576*333d2b36SAndroid Build Coastguard Worker				ctx.ModuleErrorf("Only aconfig_declarations module type is allowed for "+
577*333d2b36SAndroid Build Coastguard Worker					"flags_packages property, but %s is not aconfig_declarations module type",
578*333d2b36SAndroid Build Coastguard Worker					dep.Name(),
579*333d2b36SAndroid Build Coastguard Worker				)
580*333d2b36SAndroid Build Coastguard Worker			}
581*333d2b36SAndroid Build Coastguard Worker		}
582*333d2b36SAndroid Build Coastguard Worker	})
583*333d2b36SAndroid Build Coastguard Worker
584*333d2b36SAndroid Build Coastguard Worker	return android.FirstUniquePaths(aconfigTextFilePaths)
585*333d2b36SAndroid Build Coastguard Worker}
586*333d2b36SAndroid Build Coastguard Worker
587*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
588*333d2b36SAndroid Build Coastguard Worker	usePlatformAPI := proptools.Bool(a.Module.deviceProperties.Platform_apis)
589*333d2b36SAndroid Build Coastguard Worker	if ctx.Module().(android.SdkContext).SdkVersion(ctx).Kind == android.SdkModule {
590*333d2b36SAndroid Build Coastguard Worker		usePlatformAPI = true
591*333d2b36SAndroid Build Coastguard Worker	}
592*333d2b36SAndroid Build Coastguard Worker	a.aapt.usesNonSdkApis = usePlatformAPI
593*333d2b36SAndroid Build Coastguard Worker
594*333d2b36SAndroid Build Coastguard Worker	// Ask manifest_fixer to add or update the application element indicating this app has no code.
595*333d2b36SAndroid Build Coastguard Worker	a.aapt.hasNoCode = !a.hasCode(ctx)
596*333d2b36SAndroid Build Coastguard Worker
597*333d2b36SAndroid Build Coastguard Worker	aaptLinkFlags := []string{}
598*333d2b36SAndroid Build Coastguard Worker
599*333d2b36SAndroid Build Coastguard Worker	// Add TARGET_AAPT_CHARACTERISTICS values to AAPT link flags if they exist and --product flags were not provided.
600*333d2b36SAndroid Build Coastguard Worker	autogenerateRRO := proptools.Bool(a.appProperties.Generate_product_characteristics_rro)
601*333d2b36SAndroid Build Coastguard Worker	hasProduct := android.PrefixInList(a.aaptProperties.Aaptflags, "--product")
602*333d2b36SAndroid Build Coastguard Worker	characteristics := ctx.Config().ProductAAPTCharacteristics()
603*333d2b36SAndroid Build Coastguard Worker	if !autogenerateRRO && !hasProduct && len(characteristics) > 0 && characteristics != "default" {
604*333d2b36SAndroid Build Coastguard Worker		aaptLinkFlags = append(aaptLinkFlags, "--product", characteristics)
605*333d2b36SAndroid Build Coastguard Worker	}
606*333d2b36SAndroid Build Coastguard Worker
607*333d2b36SAndroid Build Coastguard Worker	if !Bool(a.aaptProperties.Aapt_include_all_resources) {
608*333d2b36SAndroid Build Coastguard Worker		// Product AAPT config
609*333d2b36SAndroid Build Coastguard Worker		for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
610*333d2b36SAndroid Build Coastguard Worker			aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
611*333d2b36SAndroid Build Coastguard Worker		}
612*333d2b36SAndroid Build Coastguard Worker
613*333d2b36SAndroid Build Coastguard Worker		// Product AAPT preferred config
614*333d2b36SAndroid Build Coastguard Worker		if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
615*333d2b36SAndroid Build Coastguard Worker			aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
616*333d2b36SAndroid Build Coastguard Worker		}
617*333d2b36SAndroid Build Coastguard Worker	}
618*333d2b36SAndroid Build Coastguard Worker
619*333d2b36SAndroid Build Coastguard Worker	manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
620*333d2b36SAndroid Build Coastguard Worker	packageNameProp := a.overridableAppProperties.Package_name.Get(ctx)
621*333d2b36SAndroid Build Coastguard Worker	if overridden || packageNameProp.IsPresent() {
622*333d2b36SAndroid Build Coastguard Worker		// The product override variable has a priority over the package_name property.
623*333d2b36SAndroid Build Coastguard Worker		if !overridden {
624*333d2b36SAndroid Build Coastguard Worker			manifestPackageName = packageNameProp.Get()
625*333d2b36SAndroid Build Coastguard Worker		}
626*333d2b36SAndroid Build Coastguard Worker		aaptLinkFlags = append(aaptLinkFlags, generateAaptRenamePackageFlags(manifestPackageName, a.renameResourcesPackage())...)
627*333d2b36SAndroid Build Coastguard Worker		a.overriddenManifestPackageName = manifestPackageName
628*333d2b36SAndroid Build Coastguard Worker	}
629*333d2b36SAndroid Build Coastguard Worker
630*333d2b36SAndroid Build Coastguard Worker	aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
631*333d2b36SAndroid Build Coastguard Worker
632*333d2b36SAndroid Build Coastguard Worker	a.aapt.splitNames = a.appProperties.Package_splits
633*333d2b36SAndroid Build Coastguard Worker	a.aapt.LoggingParent = String(a.overridableAppProperties.Logging_parent)
634*333d2b36SAndroid Build Coastguard Worker	if a.Updatable() {
635*333d2b36SAndroid Build Coastguard Worker		if override := ctx.Config().Getenv("OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION"); override != "" {
636*333d2b36SAndroid Build Coastguard Worker			a.aapt.defaultManifestVersion = override
637*333d2b36SAndroid Build Coastguard Worker		} else {
638*333d2b36SAndroid Build Coastguard Worker			a.aapt.defaultManifestVersion = ctx.Config().ReleaseDefaultUpdatableModuleVersion()
639*333d2b36SAndroid Build Coastguard Worker		}
640*333d2b36SAndroid Build Coastguard Worker	}
641*333d2b36SAndroid Build Coastguard Worker
642*333d2b36SAndroid Build Coastguard Worker	// Use non final ids if we are doing optimized shrinking and are using R8.
643*333d2b36SAndroid Build Coastguard Worker	nonFinalIds := a.dexProperties.optimizedResourceShrinkingEnabled(ctx) && a.dexer.effectiveOptimizeEnabled()
644*333d2b36SAndroid Build Coastguard Worker
645*333d2b36SAndroid Build Coastguard Worker	aconfigTextFilePaths := getAconfigFilePaths(ctx)
646*333d2b36SAndroid Build Coastguard Worker
647*333d2b36SAndroid Build Coastguard Worker	a.aapt.buildActions(ctx,
648*333d2b36SAndroid Build Coastguard Worker		aaptBuildActionOptions{
649*333d2b36SAndroid Build Coastguard Worker			sdkContext:                     android.SdkContext(a),
650*333d2b36SAndroid Build Coastguard Worker			classLoaderContexts:            a.classLoaderContexts,
651*333d2b36SAndroid Build Coastguard Worker			excludedLibs:                   a.usesLibraryProperties.Exclude_uses_libs,
652*333d2b36SAndroid Build Coastguard Worker			enforceDefaultTargetSdkVersion: a.enforceDefaultTargetSdkVersion(),
653*333d2b36SAndroid Build Coastguard Worker			forceNonFinalResourceIDs:       nonFinalIds,
654*333d2b36SAndroid Build Coastguard Worker			extraLinkFlags:                 aaptLinkFlags,
655*333d2b36SAndroid Build Coastguard Worker			aconfigTextFiles:               aconfigTextFilePaths,
656*333d2b36SAndroid Build Coastguard Worker			usesLibrary:                    &a.usesLibrary,
657*333d2b36SAndroid Build Coastguard Worker		},
658*333d2b36SAndroid Build Coastguard Worker	)
659*333d2b36SAndroid Build Coastguard Worker
660*333d2b36SAndroid Build Coastguard Worker	// apps manifests are handled by aapt, don't let Module see them
661*333d2b36SAndroid Build Coastguard Worker	a.properties.Manifest = nil
662*333d2b36SAndroid Build Coastguard Worker
663*333d2b36SAndroid Build Coastguard Worker	android.SetProvider(ctx, FlagsPackagesProvider, FlagsPackages{
664*333d2b36SAndroid Build Coastguard Worker		AconfigTextFiles: aconfigTextFilePaths,
665*333d2b36SAndroid Build Coastguard Worker	})
666*333d2b36SAndroid Build Coastguard Worker}
667*333d2b36SAndroid Build Coastguard Worker
668*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
669*333d2b36SAndroid Build Coastguard Worker	var staticLibProguardFlagFiles android.Paths
670*333d2b36SAndroid Build Coastguard Worker	ctx.VisitDirectDeps(func(m android.Module) {
671*333d2b36SAndroid Build Coastguard Worker		depProguardInfo, _ := android.OtherModuleProvider(ctx, m, ProguardSpecInfoProvider)
672*333d2b36SAndroid Build Coastguard Worker		staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, depProguardInfo.UnconditionallyExportedProguardFlags.ToList()...)
673*333d2b36SAndroid Build Coastguard Worker		if ctx.OtherModuleDependencyTag(m) == staticLibTag {
674*333d2b36SAndroid Build Coastguard Worker			staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, depProguardInfo.ProguardFlagsFiles.ToList()...)
675*333d2b36SAndroid Build Coastguard Worker		}
676*333d2b36SAndroid Build Coastguard Worker	})
677*333d2b36SAndroid Build Coastguard Worker
678*333d2b36SAndroid Build Coastguard Worker	staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
679*333d2b36SAndroid Build Coastguard Worker
680*333d2b36SAndroid Build Coastguard Worker	a.Module.extraProguardFlagsFiles = append(a.Module.extraProguardFlagsFiles, staticLibProguardFlagFiles...)
681*333d2b36SAndroid Build Coastguard Worker	if !(a.dexProperties.optimizedResourceShrinkingEnabled(ctx)) {
682*333d2b36SAndroid Build Coastguard Worker		// When using the optimized shrinking the R8 enqueuer will traverse the xml files that become
683*333d2b36SAndroid Build Coastguard Worker		// live for code references and (transitively) mark these as live.
684*333d2b36SAndroid Build Coastguard Worker		// In this case we explicitly don't wan't the aapt2 generated keep files (which would keep the now
685*333d2b36SAndroid Build Coastguard Worker		// dead code alive)
686*333d2b36SAndroid Build Coastguard Worker		a.Module.extraProguardFlagsFiles = append(a.Module.extraProguardFlagsFiles, a.proguardOptionsFile)
687*333d2b36SAndroid Build Coastguard Worker	}
688*333d2b36SAndroid Build Coastguard Worker}
689*333d2b36SAndroid Build Coastguard Worker
690*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) installPath(ctx android.ModuleContext) android.InstallPath {
691*333d2b36SAndroid Build Coastguard Worker	var installDir string
692*333d2b36SAndroid Build Coastguard Worker	if ctx.ModuleName() == "framework-res" {
693*333d2b36SAndroid Build Coastguard Worker		// framework-res.apk is installed as system/framework/framework-res.apk
694*333d2b36SAndroid Build Coastguard Worker		installDir = "framework"
695*333d2b36SAndroid Build Coastguard Worker	} else if a.Privileged() {
696*333d2b36SAndroid Build Coastguard Worker		installDir = filepath.Join("priv-app", a.installApkName)
697*333d2b36SAndroid Build Coastguard Worker	} else {
698*333d2b36SAndroid Build Coastguard Worker		installDir = filepath.Join("app", a.installApkName)
699*333d2b36SAndroid Build Coastguard Worker	}
700*333d2b36SAndroid Build Coastguard Worker
701*333d2b36SAndroid Build Coastguard Worker	return android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
702*333d2b36SAndroid Build Coastguard Worker}
703*333d2b36SAndroid Build Coastguard Worker
704*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) (android.Path, android.Path) {
705*333d2b36SAndroid Build Coastguard Worker	a.dexpreopter.installPath = a.installPath(ctx)
706*333d2b36SAndroid Build Coastguard Worker	a.dexpreopter.isApp = true
707*333d2b36SAndroid Build Coastguard Worker	if a.dexProperties.Uncompress_dex == nil {
708*333d2b36SAndroid Build Coastguard Worker		// If the value was not force-set by the user, use reasonable default based on the module.
709*333d2b36SAndroid Build Coastguard Worker		a.dexProperties.Uncompress_dex = proptools.BoolPtr(a.shouldUncompressDex(ctx))
710*333d2b36SAndroid Build Coastguard Worker	}
711*333d2b36SAndroid Build Coastguard Worker	a.dexpreopter.uncompressedDex = *a.dexProperties.Uncompress_dex
712*333d2b36SAndroid Build Coastguard Worker	a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries(ctx)
713*333d2b36SAndroid Build Coastguard Worker	a.dexpreopter.classLoaderContexts = a.classLoaderContexts
714*333d2b36SAndroid Build Coastguard Worker	a.dexpreopter.manifestFile = a.mergedManifestFile
715*333d2b36SAndroid Build Coastguard Worker	a.dexpreopter.preventInstall = a.appProperties.PreventInstall
716*333d2b36SAndroid Build Coastguard Worker
717*333d2b36SAndroid Build Coastguard Worker	var packageResources = a.exportPackage
718*333d2b36SAndroid Build Coastguard Worker
719*333d2b36SAndroid Build Coastguard Worker	if ctx.ModuleName() != "framework-res" {
720*333d2b36SAndroid Build Coastguard Worker		if a.dexProperties.resourceShrinkingEnabled(ctx) {
721*333d2b36SAndroid Build Coastguard Worker			protoFile := android.PathForModuleOut(ctx, packageResources.Base()+".proto.apk")
722*333d2b36SAndroid Build Coastguard Worker			aapt2Convert(ctx, protoFile, packageResources, "proto")
723*333d2b36SAndroid Build Coastguard Worker			a.dexer.resourcesInput = android.OptionalPathForPath(protoFile)
724*333d2b36SAndroid Build Coastguard Worker		}
725*333d2b36SAndroid Build Coastguard Worker
726*333d2b36SAndroid Build Coastguard Worker		var extraSrcJars android.Paths
727*333d2b36SAndroid Build Coastguard Worker		var extraClasspathJars android.Paths
728*333d2b36SAndroid Build Coastguard Worker		var extraCombinedJars android.Paths
729*333d2b36SAndroid Build Coastguard Worker		if a.useResourceProcessorBusyBox(ctx) {
730*333d2b36SAndroid Build Coastguard Worker			// When building an app with ResourceProcessorBusyBox enabled ResourceProcessorBusyBox has already
731*333d2b36SAndroid Build Coastguard Worker			// created R.class files that provide IDs for resources in busybox/R.jar.  Pass that file in the
732*333d2b36SAndroid Build Coastguard Worker			// classpath when compiling everything else, and add it to the final classes jar.
733*333d2b36SAndroid Build Coastguard Worker			extraClasspathJars = android.Paths{a.aapt.rJar}
734*333d2b36SAndroid Build Coastguard Worker			extraCombinedJars = android.Paths{a.aapt.rJar}
735*333d2b36SAndroid Build Coastguard Worker		} else {
736*333d2b36SAndroid Build Coastguard Worker			// When building an app without ResourceProcessorBusyBox the aapt2 rule creates R.srcjar containing
737*333d2b36SAndroid Build Coastguard Worker			// R.java files for the app's package and the packages from all transitive static android_library
738*333d2b36SAndroid Build Coastguard Worker			// dependencies.  Compile the srcjar alongside the rest of the sources.
739*333d2b36SAndroid Build Coastguard Worker			extraSrcJars = android.Paths{a.aapt.aaptSrcJar}
740*333d2b36SAndroid Build Coastguard Worker		}
741*333d2b36SAndroid Build Coastguard Worker
742*333d2b36SAndroid Build Coastguard Worker		a.Module.compile(ctx, extraSrcJars, extraClasspathJars, extraCombinedJars, nil)
743*333d2b36SAndroid Build Coastguard Worker		if a.dexProperties.resourceShrinkingEnabled(ctx) {
744*333d2b36SAndroid Build Coastguard Worker			binaryResources := android.PathForModuleOut(ctx, packageResources.Base()+".binary.out.apk")
745*333d2b36SAndroid Build Coastguard Worker			aapt2Convert(ctx, binaryResources, a.dexer.resourcesOutput.Path(), "binary")
746*333d2b36SAndroid Build Coastguard Worker			packageResources = binaryResources
747*333d2b36SAndroid Build Coastguard Worker		}
748*333d2b36SAndroid Build Coastguard Worker	}
749*333d2b36SAndroid Build Coastguard Worker
750*333d2b36SAndroid Build Coastguard Worker	return a.dexJarFile.PathOrNil(), packageResources
751*333d2b36SAndroid Build Coastguard Worker}
752*333d2b36SAndroid Build Coastguard Worker
753*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) jniBuildActions(jniLibs []jniLib, prebuiltJniPackages android.Paths, ctx android.ModuleContext) android.WritablePath {
754*333d2b36SAndroid Build Coastguard Worker	var jniJarFile android.WritablePath
755*333d2b36SAndroid Build Coastguard Worker	if len(jniLibs) > 0 || len(prebuiltJniPackages) > 0 {
756*333d2b36SAndroid Build Coastguard Worker		a.jniLibs = jniLibs
757*333d2b36SAndroid Build Coastguard Worker		if a.shouldEmbedJnis(ctx) {
758*333d2b36SAndroid Build Coastguard Worker			jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
759*333d2b36SAndroid Build Coastguard Worker			a.installPathForJNISymbols = a.installPath(ctx)
760*333d2b36SAndroid Build Coastguard Worker			TransformJniLibsToJar(ctx, jniJarFile, jniLibs, prebuiltJniPackages, a.useEmbeddedNativeLibs(ctx))
761*333d2b36SAndroid Build Coastguard Worker			for _, jni := range jniLibs {
762*333d2b36SAndroid Build Coastguard Worker				if jni.coverageFile.Valid() {
763*333d2b36SAndroid Build Coastguard Worker					// Only collect coverage for the first target arch if this is a multilib target.
764*333d2b36SAndroid Build Coastguard Worker					// TODO(jungjw): Ideally, we want to collect both reports, but that would cause coverage
765*333d2b36SAndroid Build Coastguard Worker					// data file path collisions since the current coverage file path format doesn't contain
766*333d2b36SAndroid Build Coastguard Worker					// arch-related strings. This is fine for now though; the code coverage team doesn't use
767*333d2b36SAndroid Build Coastguard Worker					// multi-arch targets such as test_suite_* for coverage collections yet.
768*333d2b36SAndroid Build Coastguard Worker					//
769*333d2b36SAndroid Build Coastguard Worker					// Work with the team to come up with a new format that handles multilib modules properly
770*333d2b36SAndroid Build Coastguard Worker					// and change this.
771*333d2b36SAndroid Build Coastguard Worker					if len(ctx.Config().Targets[android.Android]) == 1 ||
772*333d2b36SAndroid Build Coastguard Worker						ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType == jni.target.Arch.ArchType {
773*333d2b36SAndroid Build Coastguard Worker						a.jniCoverageOutputs = append(a.jniCoverageOutputs, jni.coverageFile.Path())
774*333d2b36SAndroid Build Coastguard Worker					}
775*333d2b36SAndroid Build Coastguard Worker				}
776*333d2b36SAndroid Build Coastguard Worker			}
777*333d2b36SAndroid Build Coastguard Worker			a.embeddedJniLibs = true
778*333d2b36SAndroid Build Coastguard Worker		}
779*333d2b36SAndroid Build Coastguard Worker	}
780*333d2b36SAndroid Build Coastguard Worker	return jniJarFile
781*333d2b36SAndroid Build Coastguard Worker}
782*333d2b36SAndroid Build Coastguard Worker
783*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) JNISymbolsInstalls(installPath string) android.RuleBuilderInstalls {
784*333d2b36SAndroid Build Coastguard Worker	var jniSymbols android.RuleBuilderInstalls
785*333d2b36SAndroid Build Coastguard Worker	for _, jniLib := range a.jniLibs {
786*333d2b36SAndroid Build Coastguard Worker		if jniLib.unstrippedFile != nil {
787*333d2b36SAndroid Build Coastguard Worker			jniSymbols = append(jniSymbols, android.RuleBuilderInstall{
788*333d2b36SAndroid Build Coastguard Worker				From: jniLib.unstrippedFile,
789*333d2b36SAndroid Build Coastguard Worker				To:   filepath.Join(installPath, targetToJniDir(jniLib.target), jniLib.unstrippedFile.Base()),
790*333d2b36SAndroid Build Coastguard Worker			})
791*333d2b36SAndroid Build Coastguard Worker		}
792*333d2b36SAndroid Build Coastguard Worker	}
793*333d2b36SAndroid Build Coastguard Worker	return jniSymbols
794*333d2b36SAndroid Build Coastguard Worker}
795*333d2b36SAndroid Build Coastguard Worker
796*333d2b36SAndroid Build Coastguard Worker// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
797*333d2b36SAndroid Build Coastguard Worker// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
798*333d2b36SAndroid Build Coastguard Workerfunc processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate,
799*333d2b36SAndroid Build Coastguard Worker	ctx android.ModuleContext) (mainCertificate Certificate, allCertificates []Certificate) {
800*333d2b36SAndroid Build Coastguard Worker	if android.SrcIsModule(certPropValue) == "" {
801*333d2b36SAndroid Build Coastguard Worker		var mainCert Certificate
802*333d2b36SAndroid Build Coastguard Worker		if certPropValue != "" {
803*333d2b36SAndroid Build Coastguard Worker			defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
804*333d2b36SAndroid Build Coastguard Worker			mainCert = Certificate{
805*333d2b36SAndroid Build Coastguard Worker				Pem: defaultDir.Join(ctx, certPropValue+".x509.pem"),
806*333d2b36SAndroid Build Coastguard Worker				Key: defaultDir.Join(ctx, certPropValue+".pk8"),
807*333d2b36SAndroid Build Coastguard Worker			}
808*333d2b36SAndroid Build Coastguard Worker		} else {
809*333d2b36SAndroid Build Coastguard Worker			pem, key := ctx.Config().DefaultAppCertificate(ctx)
810*333d2b36SAndroid Build Coastguard Worker			mainCert = Certificate{
811*333d2b36SAndroid Build Coastguard Worker				Pem: pem,
812*333d2b36SAndroid Build Coastguard Worker				Key: key,
813*333d2b36SAndroid Build Coastguard Worker			}
814*333d2b36SAndroid Build Coastguard Worker		}
815*333d2b36SAndroid Build Coastguard Worker		certificates = append([]Certificate{mainCert}, certificates...)
816*333d2b36SAndroid Build Coastguard Worker	}
817*333d2b36SAndroid Build Coastguard Worker
818*333d2b36SAndroid Build Coastguard Worker	if len(certificates) > 0 {
819*333d2b36SAndroid Build Coastguard Worker		mainCertificate = certificates[0]
820*333d2b36SAndroid Build Coastguard Worker	} else {
821*333d2b36SAndroid Build Coastguard Worker		// This can be reached with an empty certificate list if AllowMissingDependencies is set
822*333d2b36SAndroid Build Coastguard Worker		// and the certificate property for this module is a module reference to a missing module.
823*333d2b36SAndroid Build Coastguard Worker		if !ctx.Config().AllowMissingDependencies() && len(ctx.GetMissingDependencies()) > 0 {
824*333d2b36SAndroid Build Coastguard Worker			panic("Should only get here if AllowMissingDependencies set and there are missing dependencies")
825*333d2b36SAndroid Build Coastguard Worker		}
826*333d2b36SAndroid Build Coastguard Worker		// Set a certificate to avoid panics later when accessing it.
827*333d2b36SAndroid Build Coastguard Worker		mainCertificate = Certificate{
828*333d2b36SAndroid Build Coastguard Worker			Key: android.PathForModuleOut(ctx, "missing.pk8"),
829*333d2b36SAndroid Build Coastguard Worker			Pem: android.PathForModuleOut(ctx, "missing.x509.pem"),
830*333d2b36SAndroid Build Coastguard Worker		}
831*333d2b36SAndroid Build Coastguard Worker	}
832*333d2b36SAndroid Build Coastguard Worker
833*333d2b36SAndroid Build Coastguard Worker	if !m.Platform() {
834*333d2b36SAndroid Build Coastguard Worker		certPath := mainCertificate.Pem.String()
835*333d2b36SAndroid Build Coastguard Worker		systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
836*333d2b36SAndroid Build Coastguard Worker		if strings.HasPrefix(certPath, systemCertPath) {
837*333d2b36SAndroid Build Coastguard Worker			enforceSystemCert := ctx.Config().EnforceSystemCertificate()
838*333d2b36SAndroid Build Coastguard Worker			allowed := ctx.Config().EnforceSystemCertificateAllowList()
839*333d2b36SAndroid Build Coastguard Worker
840*333d2b36SAndroid Build Coastguard Worker			if enforceSystemCert && !inList(m.Name(), allowed) {
841*333d2b36SAndroid Build Coastguard Worker				ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
842*333d2b36SAndroid Build Coastguard Worker			}
843*333d2b36SAndroid Build Coastguard Worker		}
844*333d2b36SAndroid Build Coastguard Worker	}
845*333d2b36SAndroid Build Coastguard Worker
846*333d2b36SAndroid Build Coastguard Worker	return mainCertificate, certificates
847*333d2b36SAndroid Build Coastguard Worker}
848*333d2b36SAndroid Build Coastguard Worker
849*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) InstallApkName() string {
850*333d2b36SAndroid Build Coastguard Worker	return a.installApkName
851*333d2b36SAndroid Build Coastguard Worker}
852*333d2b36SAndroid Build Coastguard Worker
853*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) createPrivappAllowlist(ctx android.ModuleContext) android.Path {
854*333d2b36SAndroid Build Coastguard Worker	if a.appProperties.Privapp_allowlist == nil {
855*333d2b36SAndroid Build Coastguard Worker		return nil
856*333d2b36SAndroid Build Coastguard Worker	}
857*333d2b36SAndroid Build Coastguard Worker
858*333d2b36SAndroid Build Coastguard Worker	isOverrideApp := a.GetOverriddenBy() != ""
859*333d2b36SAndroid Build Coastguard Worker	if !isOverrideApp {
860*333d2b36SAndroid Build Coastguard Worker		// if this is not an override, we don't need to rewrite the existing privapp allowlist
861*333d2b36SAndroid Build Coastguard Worker		return android.PathForModuleSrc(ctx, *a.appProperties.Privapp_allowlist)
862*333d2b36SAndroid Build Coastguard Worker	}
863*333d2b36SAndroid Build Coastguard Worker
864*333d2b36SAndroid Build Coastguard Worker	packageNameProp := a.overridableAppProperties.Package_name.Get(ctx)
865*333d2b36SAndroid Build Coastguard Worker	if packageNameProp.IsEmpty() {
866*333d2b36SAndroid Build Coastguard Worker		ctx.PropertyErrorf("privapp_allowlist", "package_name must be set to use privapp_allowlist")
867*333d2b36SAndroid Build Coastguard Worker	}
868*333d2b36SAndroid Build Coastguard Worker
869*333d2b36SAndroid Build Coastguard Worker	packageName := packageNameProp.Get()
870*333d2b36SAndroid Build Coastguard Worker	fileName := "privapp_allowlist_" + packageName + ".xml"
871*333d2b36SAndroid Build Coastguard Worker	outPath := android.PathForModuleOut(ctx, fileName)
872*333d2b36SAndroid Build Coastguard Worker	ctx.Build(pctx, android.BuildParams{
873*333d2b36SAndroid Build Coastguard Worker		Rule:   modifyAllowlist,
874*333d2b36SAndroid Build Coastguard Worker		Input:  android.PathForModuleSrc(ctx, *a.appProperties.Privapp_allowlist),
875*333d2b36SAndroid Build Coastguard Worker		Output: outPath,
876*333d2b36SAndroid Build Coastguard Worker		Args: map[string]string{
877*333d2b36SAndroid Build Coastguard Worker			"packageName": packageName,
878*333d2b36SAndroid Build Coastguard Worker		},
879*333d2b36SAndroid Build Coastguard Worker	})
880*333d2b36SAndroid Build Coastguard Worker	return outPath
881*333d2b36SAndroid Build Coastguard Worker}
882*333d2b36SAndroid Build Coastguard Worker
883*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
884*333d2b36SAndroid Build Coastguard Worker	var apkDeps android.Paths
885*333d2b36SAndroid Build Coastguard Worker
886*333d2b36SAndroid Build Coastguard Worker	apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
887*333d2b36SAndroid Build Coastguard Worker	if !apexInfo.IsForPlatform() {
888*333d2b36SAndroid Build Coastguard Worker		a.hideApexVariantFromMake = true
889*333d2b36SAndroid Build Coastguard Worker	}
890*333d2b36SAndroid Build Coastguard Worker
891*333d2b36SAndroid Build Coastguard Worker	a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
892*333d2b36SAndroid Build Coastguard Worker	a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
893*333d2b36SAndroid Build Coastguard Worker
894*333d2b36SAndroid Build Coastguard Worker	// Unlike installApkName, a.stem should respect base module name for override_android_app.
895*333d2b36SAndroid Build Coastguard Worker	// Therefore, use ctx.ModuleName() instead of a.Name().
896*333d2b36SAndroid Build Coastguard Worker	a.stem = proptools.StringDefault(a.overridableProperties.Stem, ctx.ModuleName())
897*333d2b36SAndroid Build Coastguard Worker
898*333d2b36SAndroid Build Coastguard Worker	// Check if the install APK name needs to be overridden.
899*333d2b36SAndroid Build Coastguard Worker	// Both android_app and override_android_app module are expected to possess
900*333d2b36SAndroid Build Coastguard Worker	// its module bound apk path. However, override_android_app inherits ctx.ModuleName()
901*333d2b36SAndroid Build Coastguard Worker	// from the base module. Therefore, use a.Name() which represents
902*333d2b36SAndroid Build Coastguard Worker	// the module name for both android_app and override_android_app.
903*333d2b36SAndroid Build Coastguard Worker	a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(
904*333d2b36SAndroid Build Coastguard Worker		proptools.StringDefault(a.overridableProperties.Stem, a.Name()))
905*333d2b36SAndroid Build Coastguard Worker
906*333d2b36SAndroid Build Coastguard Worker	if ctx.ModuleName() == "framework-res" {
907*333d2b36SAndroid Build Coastguard Worker		// framework-res.apk is installed as system/framework/framework-res.apk
908*333d2b36SAndroid Build Coastguard Worker		a.installDir = android.PathForModuleInstall(ctx, "framework")
909*333d2b36SAndroid Build Coastguard Worker	} else if a.Privileged() {
910*333d2b36SAndroid Build Coastguard Worker		a.installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
911*333d2b36SAndroid Build Coastguard Worker	} else if ctx.InstallInTestcases() {
912*333d2b36SAndroid Build Coastguard Worker		a.installDir = android.PathForModuleInstall(ctx, a.installApkName, ctx.DeviceConfig().DeviceArch())
913*333d2b36SAndroid Build Coastguard Worker	} else {
914*333d2b36SAndroid Build Coastguard Worker		a.installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
915*333d2b36SAndroid Build Coastguard Worker	}
916*333d2b36SAndroid Build Coastguard Worker	a.onDeviceDir = android.InstallPathToOnDevicePath(ctx, a.installDir)
917*333d2b36SAndroid Build Coastguard Worker
918*333d2b36SAndroid Build Coastguard Worker	a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
919*333d2b36SAndroid Build Coastguard Worker	if a.usesLibrary.shouldDisableDexpreopt {
920*333d2b36SAndroid Build Coastguard Worker		a.dexpreopter.disableDexpreopt()
921*333d2b36SAndroid Build Coastguard Worker	}
922*333d2b36SAndroid Build Coastguard Worker
923*333d2b36SAndroid Build Coastguard Worker	var noticeAssetPath android.WritablePath
924*333d2b36SAndroid Build Coastguard Worker	if Bool(a.appProperties.Embed_notices) || ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") {
925*333d2b36SAndroid Build Coastguard Worker		// The rule to create the notice file can't be generated yet, as the final output path
926*333d2b36SAndroid Build Coastguard Worker		// for the apk isn't known yet.  Add the path where the notice file will be generated to the
927*333d2b36SAndroid Build Coastguard Worker		// aapt rules now before calling aaptBuildActions, the rule to create the notice file will
928*333d2b36SAndroid Build Coastguard Worker		// be generated later.
929*333d2b36SAndroid Build Coastguard Worker		noticeAssetPath = android.PathForModuleOut(ctx, "NOTICE", "NOTICE.html.gz")
930*333d2b36SAndroid Build Coastguard Worker		a.aapt.noticeFile = android.OptionalPathForPath(noticeAssetPath)
931*333d2b36SAndroid Build Coastguard Worker	}
932*333d2b36SAndroid Build Coastguard Worker
933*333d2b36SAndroid Build Coastguard Worker	// For apps targeting latest target_sdk_version
934*333d2b36SAndroid Build Coastguard Worker	if Bool(a.appProperties.Enforce_default_target_sdk_version) {
935*333d2b36SAndroid Build Coastguard Worker		a.SetEnforceDefaultTargetSdkVersion(true)
936*333d2b36SAndroid Build Coastguard Worker	}
937*333d2b36SAndroid Build Coastguard Worker
938*333d2b36SAndroid Build Coastguard Worker	// Process all building blocks, from AAPT to certificates.
939*333d2b36SAndroid Build Coastguard Worker	a.aaptBuildActions(ctx)
940*333d2b36SAndroid Build Coastguard Worker	// The decision to enforce <uses-library> checks is made before adding implicit SDK libraries.
941*333d2b36SAndroid Build Coastguard Worker	a.usesLibrary.freezeEnforceUsesLibraries(ctx)
942*333d2b36SAndroid Build Coastguard Worker
943*333d2b36SAndroid Build Coastguard Worker	// Check that the <uses-library> list is coherent with the manifest.
944*333d2b36SAndroid Build Coastguard Worker	if a.usesLibrary.enforceUsesLibraries(ctx) {
945*333d2b36SAndroid Build Coastguard Worker		manifestCheckFile := a.usesLibrary.verifyUsesLibrariesManifest(
946*333d2b36SAndroid Build Coastguard Worker			ctx, a.mergedManifestFile, &a.classLoaderContexts)
947*333d2b36SAndroid Build Coastguard Worker		apkDeps = append(apkDeps, manifestCheckFile)
948*333d2b36SAndroid Build Coastguard Worker	}
949*333d2b36SAndroid Build Coastguard Worker
950*333d2b36SAndroid Build Coastguard Worker	a.proguardBuildActions(ctx)
951*333d2b36SAndroid Build Coastguard Worker
952*333d2b36SAndroid Build Coastguard Worker	a.linter.mergedManifest = a.aapt.mergedManifestFile
953*333d2b36SAndroid Build Coastguard Worker	a.linter.manifest = a.aapt.manifestPath
954*333d2b36SAndroid Build Coastguard Worker	a.linter.resources = a.aapt.resourceFiles
955*333d2b36SAndroid Build Coastguard Worker	a.linter.buildModuleReportZip = ctx.Config().UnbundledBuildApps()
956*333d2b36SAndroid Build Coastguard Worker
957*333d2b36SAndroid Build Coastguard Worker	dexJarFile, packageResources := a.dexBuildActions(ctx)
958*333d2b36SAndroid Build Coastguard Worker
959*333d2b36SAndroid Build Coastguard Worker	// No need to check the SDK version of the JNI deps unless we embed them
960*333d2b36SAndroid Build Coastguard Worker	checkNativeSdkVersion := a.shouldEmbedJnis(ctx) && !Bool(a.appProperties.Jni_uses_platform_apis)
961*333d2b36SAndroid Build Coastguard Worker	jniLibs, prebuiltJniPackages, certificates := collectAppDeps(ctx, a, a.shouldEmbedJnis(ctx), checkNativeSdkVersion)
962*333d2b36SAndroid Build Coastguard Worker	jniJarFile := a.jniBuildActions(jniLibs, prebuiltJniPackages, ctx)
963*333d2b36SAndroid Build Coastguard Worker
964*333d2b36SAndroid Build Coastguard Worker	if ctx.Failed() {
965*333d2b36SAndroid Build Coastguard Worker		return
966*333d2b36SAndroid Build Coastguard Worker	}
967*333d2b36SAndroid Build Coastguard Worker
968*333d2b36SAndroid Build Coastguard Worker	a.certificate, certificates = processMainCert(a.ModuleBase, a.getCertString(ctx), certificates, ctx)
969*333d2b36SAndroid Build Coastguard Worker
970*333d2b36SAndroid Build Coastguard Worker	// Build a final signed app package.
971*333d2b36SAndroid Build Coastguard Worker	packageFile := android.PathForModuleOut(ctx, a.installApkName+".apk")
972*333d2b36SAndroid Build Coastguard Worker	v4SigningRequested := Bool(a.Module.deviceProperties.V4_signature)
973*333d2b36SAndroid Build Coastguard Worker	var v4SignatureFile android.WritablePath = nil
974*333d2b36SAndroid Build Coastguard Worker	if v4SigningRequested {
975*333d2b36SAndroid Build Coastguard Worker		v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+".apk.idsig")
976*333d2b36SAndroid Build Coastguard Worker	}
977*333d2b36SAndroid Build Coastguard Worker	var lineageFile android.Path
978*333d2b36SAndroid Build Coastguard Worker	if lineage := String(a.overridableAppProperties.Lineage); lineage != "" {
979*333d2b36SAndroid Build Coastguard Worker		lineageFile = android.PathForModuleSrc(ctx, lineage)
980*333d2b36SAndroid Build Coastguard Worker	}
981*333d2b36SAndroid Build Coastguard Worker	rotationMinSdkVersion := String(a.overridableAppProperties.RotationMinSdkVersion)
982*333d2b36SAndroid Build Coastguard Worker
983*333d2b36SAndroid Build Coastguard Worker	CreateAndSignAppPackage(ctx, packageFile, packageResources, jniJarFile, dexJarFile, certificates, apkDeps, v4SignatureFile, lineageFile, rotationMinSdkVersion)
984*333d2b36SAndroid Build Coastguard Worker	a.outputFile = packageFile
985*333d2b36SAndroid Build Coastguard Worker	if v4SigningRequested {
986*333d2b36SAndroid Build Coastguard Worker		a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
987*333d2b36SAndroid Build Coastguard Worker	}
988*333d2b36SAndroid Build Coastguard Worker
989*333d2b36SAndroid Build Coastguard Worker	if a.aapt.noticeFile.Valid() {
990*333d2b36SAndroid Build Coastguard Worker		// Generating the notice file rule has to be here after a.outputFile is known.
991*333d2b36SAndroid Build Coastguard Worker		noticeFile := android.PathForModuleOut(ctx, "NOTICE.html.gz")
992*333d2b36SAndroid Build Coastguard Worker		android.BuildNoticeHtmlOutputFromLicenseMetadata(
993*333d2b36SAndroid Build Coastguard Worker			ctx, noticeFile, "", "",
994*333d2b36SAndroid Build Coastguard Worker			[]string{
995*333d2b36SAndroid Build Coastguard Worker				a.installDir.String() + "/",
996*333d2b36SAndroid Build Coastguard Worker				android.PathForModuleInstall(ctx).String() + "/",
997*333d2b36SAndroid Build Coastguard Worker				a.outputFile.String(),
998*333d2b36SAndroid Build Coastguard Worker			})
999*333d2b36SAndroid Build Coastguard Worker		builder := android.NewRuleBuilder(pctx, ctx)
1000*333d2b36SAndroid Build Coastguard Worker		builder.Command().Text("cp").
1001*333d2b36SAndroid Build Coastguard Worker			Input(noticeFile).
1002*333d2b36SAndroid Build Coastguard Worker			Output(noticeAssetPath)
1003*333d2b36SAndroid Build Coastguard Worker		builder.Build("notice_dir", "Building notice dir")
1004*333d2b36SAndroid Build Coastguard Worker	}
1005*333d2b36SAndroid Build Coastguard Worker
1006*333d2b36SAndroid Build Coastguard Worker	for _, split := range a.aapt.splits {
1007*333d2b36SAndroid Build Coastguard Worker		// Sign the split APKs
1008*333d2b36SAndroid Build Coastguard Worker		packageFile := android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk")
1009*333d2b36SAndroid Build Coastguard Worker		if v4SigningRequested {
1010*333d2b36SAndroid Build Coastguard Worker			v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk.idsig")
1011*333d2b36SAndroid Build Coastguard Worker		}
1012*333d2b36SAndroid Build Coastguard Worker		CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates, apkDeps, v4SignatureFile, lineageFile, rotationMinSdkVersion)
1013*333d2b36SAndroid Build Coastguard Worker		a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
1014*333d2b36SAndroid Build Coastguard Worker		if v4SigningRequested {
1015*333d2b36SAndroid Build Coastguard Worker			a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
1016*333d2b36SAndroid Build Coastguard Worker		}
1017*333d2b36SAndroid Build Coastguard Worker	}
1018*333d2b36SAndroid Build Coastguard Worker
1019*333d2b36SAndroid Build Coastguard Worker	// Build an app bundle.
1020*333d2b36SAndroid Build Coastguard Worker	bundleFile := android.PathForModuleOut(ctx, "base.zip")
1021*333d2b36SAndroid Build Coastguard Worker	BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
1022*333d2b36SAndroid Build Coastguard Worker	a.bundleFile = bundleFile
1023*333d2b36SAndroid Build Coastguard Worker
1024*333d2b36SAndroid Build Coastguard Worker	allowlist := a.createPrivappAllowlist(ctx)
1025*333d2b36SAndroid Build Coastguard Worker	if allowlist != nil {
1026*333d2b36SAndroid Build Coastguard Worker		a.privAppAllowlist = android.OptionalPathForPath(allowlist)
1027*333d2b36SAndroid Build Coastguard Worker	}
1028*333d2b36SAndroid Build Coastguard Worker
1029*333d2b36SAndroid Build Coastguard Worker	// Install the app package.
1030*333d2b36SAndroid Build Coastguard Worker	shouldInstallAppPackage := (Bool(a.Module.properties.Installable) || ctx.Host()) && apexInfo.IsForPlatform() && !a.appProperties.PreventInstall
1031*333d2b36SAndroid Build Coastguard Worker	if shouldInstallAppPackage {
1032*333d2b36SAndroid Build Coastguard Worker		if a.privAppAllowlist.Valid() {
1033*333d2b36SAndroid Build Coastguard Worker			allowlistInstallPath := android.PathForModuleInstall(ctx, "etc", "permissions")
1034*333d2b36SAndroid Build Coastguard Worker			allowlistInstallFilename := a.installApkName + ".xml"
1035*333d2b36SAndroid Build Coastguard Worker			ctx.InstallFile(allowlistInstallPath, allowlistInstallFilename, a.privAppAllowlist.Path())
1036*333d2b36SAndroid Build Coastguard Worker		}
1037*333d2b36SAndroid Build Coastguard Worker
1038*333d2b36SAndroid Build Coastguard Worker		var extraInstalledPaths android.InstallPaths
1039*333d2b36SAndroid Build Coastguard Worker		for _, extra := range a.extraOutputFiles {
1040*333d2b36SAndroid Build Coastguard Worker			installed := ctx.InstallFile(a.installDir, extra.Base(), extra)
1041*333d2b36SAndroid Build Coastguard Worker			extraInstalledPaths = append(extraInstalledPaths, installed)
1042*333d2b36SAndroid Build Coastguard Worker		}
1043*333d2b36SAndroid Build Coastguard Worker		// If we don't embed jni libs, make sure that those are installed along with the
1044*333d2b36SAndroid Build Coastguard Worker		// app, and also place symlinks to the installed paths under the lib/<arch>
1045*333d2b36SAndroid Build Coastguard Worker		// directory of the app installation directory. ex:
1046*333d2b36SAndroid Build Coastguard Worker		// /system/app/MyApp/lib/arm64/libfoo.so -> /system/lib64/libfoo.so
1047*333d2b36SAndroid Build Coastguard Worker		if !a.embeddedJniLibs {
1048*333d2b36SAndroid Build Coastguard Worker			for _, jniLib := range jniLibs {
1049*333d2b36SAndroid Build Coastguard Worker				archStr := jniLib.target.Arch.ArchType.String()
1050*333d2b36SAndroid Build Coastguard Worker				symlinkDir := a.installDir.Join(ctx, "lib", archStr)
1051*333d2b36SAndroid Build Coastguard Worker				for _, installedLib := range jniLib.installPaths {
1052*333d2b36SAndroid Build Coastguard Worker					// install the symlink itself
1053*333d2b36SAndroid Build Coastguard Worker					symlinkName := installedLib.Base()
1054*333d2b36SAndroid Build Coastguard Worker					symlinkTarget := android.InstallPathToOnDevicePath(ctx, installedLib)
1055*333d2b36SAndroid Build Coastguard Worker					ctx.InstallAbsoluteSymlink(symlinkDir, symlinkName, symlinkTarget)
1056*333d2b36SAndroid Build Coastguard Worker				}
1057*333d2b36SAndroid Build Coastguard Worker			}
1058*333d2b36SAndroid Build Coastguard Worker		}
1059*333d2b36SAndroid Build Coastguard Worker		ctx.InstallFile(a.installDir, a.outputFile.Base(), a.outputFile, extraInstalledPaths...)
1060*333d2b36SAndroid Build Coastguard Worker	}
1061*333d2b36SAndroid Build Coastguard Worker
1062*333d2b36SAndroid Build Coastguard Worker	ctx.CheckbuildFile(a.outputFile)
1063*333d2b36SAndroid Build Coastguard Worker
1064*333d2b36SAndroid Build Coastguard Worker	a.buildAppDependencyInfo(ctx)
1065*333d2b36SAndroid Build Coastguard Worker
1066*333d2b36SAndroid Build Coastguard Worker	providePrebuiltInfo(ctx,
1067*333d2b36SAndroid Build Coastguard Worker		prebuiltInfoProps{
1068*333d2b36SAndroid Build Coastguard Worker			baseModuleName: a.BaseModuleName(),
1069*333d2b36SAndroid Build Coastguard Worker			isPrebuilt:     false,
1070*333d2b36SAndroid Build Coastguard Worker		},
1071*333d2b36SAndroid Build Coastguard Worker	)
1072*333d2b36SAndroid Build Coastguard Worker
1073*333d2b36SAndroid Build Coastguard Worker	a.setOutputFiles(ctx)
1074*333d2b36SAndroid Build Coastguard Worker}
1075*333d2b36SAndroid Build Coastguard Worker
1076*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) setOutputFiles(ctx android.ModuleContext) {
1077*333d2b36SAndroid Build Coastguard Worker	ctx.SetOutputFiles([]android.Path{a.proguardOptionsFile}, ".aapt.proguardOptionsFile")
1078*333d2b36SAndroid Build Coastguard Worker	if a.aaptSrcJar != nil {
1079*333d2b36SAndroid Build Coastguard Worker		ctx.SetOutputFiles([]android.Path{a.aaptSrcJar}, ".aapt.srcjar")
1080*333d2b36SAndroid Build Coastguard Worker	}
1081*333d2b36SAndroid Build Coastguard Worker	if a.rJar != nil {
1082*333d2b36SAndroid Build Coastguard Worker		ctx.SetOutputFiles([]android.Path{a.rJar}, ".aapt.jar")
1083*333d2b36SAndroid Build Coastguard Worker	}
1084*333d2b36SAndroid Build Coastguard Worker	ctx.SetOutputFiles([]android.Path{a.outputFile}, ".apk")
1085*333d2b36SAndroid Build Coastguard Worker	ctx.SetOutputFiles([]android.Path{a.exportPackage}, ".export-package.apk")
1086*333d2b36SAndroid Build Coastguard Worker	ctx.SetOutputFiles([]android.Path{a.aapt.manifestPath}, ".manifest.xml")
1087*333d2b36SAndroid Build Coastguard Worker	setOutputFiles(ctx, a.Library.Module)
1088*333d2b36SAndroid Build Coastguard Worker}
1089*333d2b36SAndroid Build Coastguard Worker
1090*333d2b36SAndroid Build Coastguard Workertype appDepsInterface interface {
1091*333d2b36SAndroid Build Coastguard Worker	SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec
1092*333d2b36SAndroid Build Coastguard Worker	MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel
1093*333d2b36SAndroid Build Coastguard Worker	RequiresStableAPIs(ctx android.BaseModuleContext) bool
1094*333d2b36SAndroid Build Coastguard Worker}
1095*333d2b36SAndroid Build Coastguard Worker
1096*333d2b36SAndroid Build Coastguard Workerfunc collectAppDeps(ctx android.ModuleContext, app appDepsInterface,
1097*333d2b36SAndroid Build Coastguard Worker	shouldCollectRecursiveNativeDeps bool,
1098*333d2b36SAndroid Build Coastguard Worker	checkNativeSdkVersion bool) ([]jniLib, android.Paths, []Certificate) {
1099*333d2b36SAndroid Build Coastguard Worker
1100*333d2b36SAndroid Build Coastguard Worker	if checkNativeSdkVersion {
1101*333d2b36SAndroid Build Coastguard Worker		checkNativeSdkVersion = app.SdkVersion(ctx).Specified() &&
1102*333d2b36SAndroid Build Coastguard Worker			app.SdkVersion(ctx).Kind != android.SdkCorePlatform && !app.RequiresStableAPIs(ctx)
1103*333d2b36SAndroid Build Coastguard Worker	}
1104*333d2b36SAndroid Build Coastguard Worker	jniLib, prebuiltJniPackages := collectJniDeps(ctx, shouldCollectRecursiveNativeDeps,
1105*333d2b36SAndroid Build Coastguard Worker		checkNativeSdkVersion, func(parent, child android.Module) bool {
1106*333d2b36SAndroid Build Coastguard Worker			apkInApex := ctx.Module().(android.ApexModule).NotInPlatform()
1107*333d2b36SAndroid Build Coastguard Worker			childLinkable, _ := child.(cc.LinkableInterface)
1108*333d2b36SAndroid Build Coastguard Worker			parentLinkable, _ := parent.(cc.LinkableInterface)
1109*333d2b36SAndroid Build Coastguard Worker			useStubsOfDep := childLinkable.IsStubs()
1110*333d2b36SAndroid Build Coastguard Worker			if apkInApex && parentLinkable != nil {
1111*333d2b36SAndroid Build Coastguard Worker				// APK-in-APEX
1112*333d2b36SAndroid Build Coastguard Worker				// If the parent is a linkable interface, use stubs if the dependency edge crosses an apex boundary.
1113*333d2b36SAndroid Build Coastguard Worker				useStubsOfDep = useStubsOfDep || (childLinkable.HasStubsVariants() && cc.ShouldUseStubForApex(ctx, parent, child))
1114*333d2b36SAndroid Build Coastguard Worker			}
1115*333d2b36SAndroid Build Coastguard Worker			return !childLinkable.IsNdk(ctx.Config()) && !useStubsOfDep
1116*333d2b36SAndroid Build Coastguard Worker		})
1117*333d2b36SAndroid Build Coastguard Worker
1118*333d2b36SAndroid Build Coastguard Worker	var certificates []Certificate
1119*333d2b36SAndroid Build Coastguard Worker
1120*333d2b36SAndroid Build Coastguard Worker	var directImplementationDeps android.Paths
1121*333d2b36SAndroid Build Coastguard Worker	var transitiveImplementationDeps []depset.DepSet[android.Path]
1122*333d2b36SAndroid Build Coastguard Worker	ctx.VisitDirectDeps(func(module android.Module) {
1123*333d2b36SAndroid Build Coastguard Worker		otherName := ctx.OtherModuleName(module)
1124*333d2b36SAndroid Build Coastguard Worker		tag := ctx.OtherModuleDependencyTag(module)
1125*333d2b36SAndroid Build Coastguard Worker
1126*333d2b36SAndroid Build Coastguard Worker		if tag == certificateTag {
1127*333d2b36SAndroid Build Coastguard Worker			if dep, ok := module.(*AndroidAppCertificate); ok {
1128*333d2b36SAndroid Build Coastguard Worker				certificates = append(certificates, dep.Certificate)
1129*333d2b36SAndroid Build Coastguard Worker			} else {
1130*333d2b36SAndroid Build Coastguard Worker				ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
1131*333d2b36SAndroid Build Coastguard Worker			}
1132*333d2b36SAndroid Build Coastguard Worker		}
1133*333d2b36SAndroid Build Coastguard Worker
1134*333d2b36SAndroid Build Coastguard Worker		if IsJniDepTag(tag) {
1135*333d2b36SAndroid Build Coastguard Worker			directImplementationDeps = append(directImplementationDeps, android.OutputFileForModule(ctx, module, ""))
1136*333d2b36SAndroid Build Coastguard Worker			if info, ok := android.OtherModuleProvider(ctx, module, cc.ImplementationDepInfoProvider); ok {
1137*333d2b36SAndroid Build Coastguard Worker				transitiveImplementationDeps = append(transitiveImplementationDeps, info.ImplementationDeps)
1138*333d2b36SAndroid Build Coastguard Worker			}
1139*333d2b36SAndroid Build Coastguard Worker		}
1140*333d2b36SAndroid Build Coastguard Worker	})
1141*333d2b36SAndroid Build Coastguard Worker	android.SetProvider(ctx, cc.ImplementationDepInfoProvider, &cc.ImplementationDepInfo{
1142*333d2b36SAndroid Build Coastguard Worker		ImplementationDeps: depset.New(depset.PREORDER, directImplementationDeps, transitiveImplementationDeps),
1143*333d2b36SAndroid Build Coastguard Worker	})
1144*333d2b36SAndroid Build Coastguard Worker
1145*333d2b36SAndroid Build Coastguard Worker	return jniLib, prebuiltJniPackages, certificates
1146*333d2b36SAndroid Build Coastguard Worker}
1147*333d2b36SAndroid Build Coastguard Worker
1148*333d2b36SAndroid Build Coastguard Workerfunc collectJniDeps(ctx android.ModuleContext,
1149*333d2b36SAndroid Build Coastguard Worker	shouldCollectRecursiveNativeDeps bool,
1150*333d2b36SAndroid Build Coastguard Worker	checkNativeSdkVersion bool,
1151*333d2b36SAndroid Build Coastguard Worker	filter func(parent, child android.Module) bool) ([]jniLib, android.Paths) {
1152*333d2b36SAndroid Build Coastguard Worker	var jniLibs []jniLib
1153*333d2b36SAndroid Build Coastguard Worker	var prebuiltJniPackages android.Paths
1154*333d2b36SAndroid Build Coastguard Worker	seenModulePaths := make(map[string]bool)
1155*333d2b36SAndroid Build Coastguard Worker
1156*333d2b36SAndroid Build Coastguard Worker	ctx.WalkDeps(func(module android.Module, parent android.Module) bool {
1157*333d2b36SAndroid Build Coastguard Worker		otherName := ctx.OtherModuleName(module)
1158*333d2b36SAndroid Build Coastguard Worker		tag := ctx.OtherModuleDependencyTag(module)
1159*333d2b36SAndroid Build Coastguard Worker
1160*333d2b36SAndroid Build Coastguard Worker		if IsJniDepTag(tag) || cc.IsSharedDepTag(tag) {
1161*333d2b36SAndroid Build Coastguard Worker			if dep, ok := module.(cc.LinkableInterface); ok {
1162*333d2b36SAndroid Build Coastguard Worker				if filter != nil && !filter(parent, module) {
1163*333d2b36SAndroid Build Coastguard Worker					return false
1164*333d2b36SAndroid Build Coastguard Worker				}
1165*333d2b36SAndroid Build Coastguard Worker
1166*333d2b36SAndroid Build Coastguard Worker				lib := dep.OutputFile()
1167*333d2b36SAndroid Build Coastguard Worker				if lib.Valid() {
1168*333d2b36SAndroid Build Coastguard Worker					path := lib.Path()
1169*333d2b36SAndroid Build Coastguard Worker					if seenModulePaths[path.String()] {
1170*333d2b36SAndroid Build Coastguard Worker						return false
1171*333d2b36SAndroid Build Coastguard Worker					}
1172*333d2b36SAndroid Build Coastguard Worker					seenModulePaths[path.String()] = true
1173*333d2b36SAndroid Build Coastguard Worker
1174*333d2b36SAndroid Build Coastguard Worker					if checkNativeSdkVersion && dep.SdkVersion() == "" {
1175*333d2b36SAndroid Build Coastguard Worker						ctx.PropertyErrorf("jni_libs", "JNI dependency %q uses platform APIs, but this module does not",
1176*333d2b36SAndroid Build Coastguard Worker							otherName)
1177*333d2b36SAndroid Build Coastguard Worker					}
1178*333d2b36SAndroid Build Coastguard Worker
1179*333d2b36SAndroid Build Coastguard Worker					jniLibs = append(jniLibs, jniLib{
1180*333d2b36SAndroid Build Coastguard Worker						name:           ctx.OtherModuleName(module),
1181*333d2b36SAndroid Build Coastguard Worker						path:           path,
1182*333d2b36SAndroid Build Coastguard Worker						target:         module.Target(),
1183*333d2b36SAndroid Build Coastguard Worker						coverageFile:   dep.CoverageOutputFile(),
1184*333d2b36SAndroid Build Coastguard Worker						unstrippedFile: dep.UnstrippedOutputFile(),
1185*333d2b36SAndroid Build Coastguard Worker						partition:      dep.Partition(),
1186*333d2b36SAndroid Build Coastguard Worker						installPaths:   android.OtherModuleProviderOrDefault(ctx, dep, android.InstallFilesProvider).InstallFiles,
1187*333d2b36SAndroid Build Coastguard Worker					})
1188*333d2b36SAndroid Build Coastguard Worker				} else if ctx.Config().AllowMissingDependencies() {
1189*333d2b36SAndroid Build Coastguard Worker					ctx.AddMissingDependencies([]string{otherName})
1190*333d2b36SAndroid Build Coastguard Worker				} else {
1191*333d2b36SAndroid Build Coastguard Worker					ctx.ModuleErrorf("dependency %q missing output file", otherName)
1192*333d2b36SAndroid Build Coastguard Worker				}
1193*333d2b36SAndroid Build Coastguard Worker			} else {
1194*333d2b36SAndroid Build Coastguard Worker				ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
1195*333d2b36SAndroid Build Coastguard Worker			}
1196*333d2b36SAndroid Build Coastguard Worker
1197*333d2b36SAndroid Build Coastguard Worker			return shouldCollectRecursiveNativeDeps
1198*333d2b36SAndroid Build Coastguard Worker		}
1199*333d2b36SAndroid Build Coastguard Worker
1200*333d2b36SAndroid Build Coastguard Worker		if info, ok := android.OtherModuleProvider(ctx, module, JniPackageProvider); ok {
1201*333d2b36SAndroid Build Coastguard Worker			prebuiltJniPackages = append(prebuiltJniPackages, info.JniPackages...)
1202*333d2b36SAndroid Build Coastguard Worker		}
1203*333d2b36SAndroid Build Coastguard Worker
1204*333d2b36SAndroid Build Coastguard Worker		return false
1205*333d2b36SAndroid Build Coastguard Worker	})
1206*333d2b36SAndroid Build Coastguard Worker
1207*333d2b36SAndroid Build Coastguard Worker	return jniLibs, prebuiltJniPackages
1208*333d2b36SAndroid Build Coastguard Worker}
1209*333d2b36SAndroid Build Coastguard Worker
1210*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) WalkPayloadDeps(ctx android.BaseModuleContext, do android.PayloadDepsCallback) {
1211*333d2b36SAndroid Build Coastguard Worker	ctx.WalkDeps(func(child, parent android.Module) bool {
1212*333d2b36SAndroid Build Coastguard Worker		isExternal := !a.DepIsInSameApex(ctx, child)
1213*333d2b36SAndroid Build Coastguard Worker		if am, ok := child.(android.ApexModule); ok {
1214*333d2b36SAndroid Build Coastguard Worker			if !do(ctx, parent, am, isExternal) {
1215*333d2b36SAndroid Build Coastguard Worker				return false
1216*333d2b36SAndroid Build Coastguard Worker			}
1217*333d2b36SAndroid Build Coastguard Worker		}
1218*333d2b36SAndroid Build Coastguard Worker		return !isExternal
1219*333d2b36SAndroid Build Coastguard Worker	})
1220*333d2b36SAndroid Build Coastguard Worker}
1221*333d2b36SAndroid Build Coastguard Worker
1222*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) buildAppDependencyInfo(ctx android.ModuleContext) {
1223*333d2b36SAndroid Build Coastguard Worker	if ctx.Host() {
1224*333d2b36SAndroid Build Coastguard Worker		return
1225*333d2b36SAndroid Build Coastguard Worker	}
1226*333d2b36SAndroid Build Coastguard Worker
1227*333d2b36SAndroid Build Coastguard Worker	depsInfo := android.DepNameToDepInfoMap{}
1228*333d2b36SAndroid Build Coastguard Worker	a.WalkPayloadDeps(ctx, func(ctx android.BaseModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
1229*333d2b36SAndroid Build Coastguard Worker		depName := to.Name()
1230*333d2b36SAndroid Build Coastguard Worker
1231*333d2b36SAndroid Build Coastguard Worker		// Skip dependencies that are only available to APEXes; they are developed with updatability
1232*333d2b36SAndroid Build Coastguard Worker		// in mind and don't need manual approval.
1233*333d2b36SAndroid Build Coastguard Worker		if to.(android.ApexModule).NotAvailableForPlatform() {
1234*333d2b36SAndroid Build Coastguard Worker			return true
1235*333d2b36SAndroid Build Coastguard Worker		}
1236*333d2b36SAndroid Build Coastguard Worker
1237*333d2b36SAndroid Build Coastguard Worker		if info, exist := depsInfo[depName]; exist {
1238*333d2b36SAndroid Build Coastguard Worker			info.From = append(info.From, from.Name())
1239*333d2b36SAndroid Build Coastguard Worker			info.IsExternal = info.IsExternal && externalDep
1240*333d2b36SAndroid Build Coastguard Worker			depsInfo[depName] = info
1241*333d2b36SAndroid Build Coastguard Worker		} else {
1242*333d2b36SAndroid Build Coastguard Worker			toMinSdkVersion := "(no version)"
1243*333d2b36SAndroid Build Coastguard Worker			if m, ok := to.(interface {
1244*333d2b36SAndroid Build Coastguard Worker				MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel
1245*333d2b36SAndroid Build Coastguard Worker			}); ok {
1246*333d2b36SAndroid Build Coastguard Worker				if v := m.MinSdkVersion(ctx); !v.IsNone() {
1247*333d2b36SAndroid Build Coastguard Worker					toMinSdkVersion = v.String()
1248*333d2b36SAndroid Build Coastguard Worker				}
1249*333d2b36SAndroid Build Coastguard Worker			} else if m, ok := to.(interface{ MinSdkVersion() string }); ok {
1250*333d2b36SAndroid Build Coastguard Worker				// TODO(b/175678607) eliminate the use of MinSdkVersion returning
1251*333d2b36SAndroid Build Coastguard Worker				// string
1252*333d2b36SAndroid Build Coastguard Worker				if v := m.MinSdkVersion(); v != "" {
1253*333d2b36SAndroid Build Coastguard Worker					toMinSdkVersion = v
1254*333d2b36SAndroid Build Coastguard Worker				}
1255*333d2b36SAndroid Build Coastguard Worker			}
1256*333d2b36SAndroid Build Coastguard Worker			depsInfo[depName] = android.ApexModuleDepInfo{
1257*333d2b36SAndroid Build Coastguard Worker				To:            depName,
1258*333d2b36SAndroid Build Coastguard Worker				From:          []string{from.Name()},
1259*333d2b36SAndroid Build Coastguard Worker				IsExternal:    externalDep,
1260*333d2b36SAndroid Build Coastguard Worker				MinSdkVersion: toMinSdkVersion,
1261*333d2b36SAndroid Build Coastguard Worker			}
1262*333d2b36SAndroid Build Coastguard Worker		}
1263*333d2b36SAndroid Build Coastguard Worker		return true
1264*333d2b36SAndroid Build Coastguard Worker	})
1265*333d2b36SAndroid Build Coastguard Worker
1266*333d2b36SAndroid Build Coastguard Worker	a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, a.MinSdkVersion(ctx).String(), depsInfo)
1267*333d2b36SAndroid Build Coastguard Worker}
1268*333d2b36SAndroid Build Coastguard Worker
1269*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) enforceDefaultTargetSdkVersion() bool {
1270*333d2b36SAndroid Build Coastguard Worker	return a.appProperties.EnforceDefaultTargetSdkVersion
1271*333d2b36SAndroid Build Coastguard Worker}
1272*333d2b36SAndroid Build Coastguard Worker
1273*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) SetEnforceDefaultTargetSdkVersion(val bool) {
1274*333d2b36SAndroid Build Coastguard Worker	a.appProperties.EnforceDefaultTargetSdkVersion = val
1275*333d2b36SAndroid Build Coastguard Worker}
1276*333d2b36SAndroid Build Coastguard Worker
1277*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) Updatable() bool {
1278*333d2b36SAndroid Build Coastguard Worker	return Bool(a.appProperties.Updatable)
1279*333d2b36SAndroid Build Coastguard Worker}
1280*333d2b36SAndroid Build Coastguard Worker
1281*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) getCertString(ctx android.BaseModuleContext) string {
1282*333d2b36SAndroid Build Coastguard Worker	certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
1283*333d2b36SAndroid Build Coastguard Worker	if overridden {
1284*333d2b36SAndroid Build Coastguard Worker		return ":" + certificate
1285*333d2b36SAndroid Build Coastguard Worker	}
1286*333d2b36SAndroid Build Coastguard Worker	return a.overridableAppProperties.Certificate.GetOrDefault(ctx, "")
1287*333d2b36SAndroid Build Coastguard Worker}
1288*333d2b36SAndroid Build Coastguard Worker
1289*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1290*333d2b36SAndroid Build Coastguard Worker	if IsJniDepTag(ctx.OtherModuleDependencyTag(dep)) {
1291*333d2b36SAndroid Build Coastguard Worker		return true
1292*333d2b36SAndroid Build Coastguard Worker	}
1293*333d2b36SAndroid Build Coastguard Worker	return a.Library.DepIsInSameApex(ctx, dep)
1294*333d2b36SAndroid Build Coastguard Worker}
1295*333d2b36SAndroid Build Coastguard Worker
1296*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) Privileged() bool {
1297*333d2b36SAndroid Build Coastguard Worker	return Bool(a.appProperties.Privileged)
1298*333d2b36SAndroid Build Coastguard Worker}
1299*333d2b36SAndroid Build Coastguard Worker
1300*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
1301*333d2b36SAndroid Build Coastguard Worker	return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
1302*333d2b36SAndroid Build Coastguard Worker}
1303*333d2b36SAndroid Build Coastguard Worker
1304*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) SetPreventInstall() {
1305*333d2b36SAndroid Build Coastguard Worker	a.appProperties.PreventInstall = true
1306*333d2b36SAndroid Build Coastguard Worker}
1307*333d2b36SAndroid Build Coastguard Worker
1308*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) MarkAsCoverageVariant(coverage bool) {
1309*333d2b36SAndroid Build Coastguard Worker	a.appProperties.IsCoverageVariant = coverage
1310*333d2b36SAndroid Build Coastguard Worker}
1311*333d2b36SAndroid Build Coastguard Worker
1312*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) EnableCoverageIfNeeded() {}
1313*333d2b36SAndroid Build Coastguard Worker
1314*333d2b36SAndroid Build Coastguard Workervar _ cc.Coverage = (*AndroidApp)(nil)
1315*333d2b36SAndroid Build Coastguard Worker
1316*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) IDEInfo(ctx android.BaseModuleContext, dpInfo *android.IdeInfo) {
1317*333d2b36SAndroid Build Coastguard Worker	a.Library.IDEInfo(ctx, dpInfo)
1318*333d2b36SAndroid Build Coastguard Worker	a.aapt.IDEInfo(ctx, dpInfo)
1319*333d2b36SAndroid Build Coastguard Worker}
1320*333d2b36SAndroid Build Coastguard Worker
1321*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) productCharacteristicsRROPackageName() string {
1322*333d2b36SAndroid Build Coastguard Worker	return proptools.String(a.appProperties.ProductCharacteristicsRROPackageName)
1323*333d2b36SAndroid Build Coastguard Worker}
1324*333d2b36SAndroid Build Coastguard Worker
1325*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidApp) productCharacteristicsRROManifestModuleName() string {
1326*333d2b36SAndroid Build Coastguard Worker	return proptools.String(a.appProperties.ProductCharacteristicsRROManifestModuleName)
1327*333d2b36SAndroid Build Coastguard Worker}
1328*333d2b36SAndroid Build Coastguard Worker
1329*333d2b36SAndroid Build Coastguard Worker// android_app compiles sources and Android resources into an Android application package `.apk` file.
1330*333d2b36SAndroid Build Coastguard Workerfunc AndroidAppFactory() android.Module {
1331*333d2b36SAndroid Build Coastguard Worker	module := &AndroidApp{}
1332*333d2b36SAndroid Build Coastguard Worker
1333*333d2b36SAndroid Build Coastguard Worker	module.Module.dexProperties.Optimize.EnabledByDefault = true
1334*333d2b36SAndroid Build Coastguard Worker	module.Module.dexProperties.Optimize.Shrink = proptools.BoolPtr(true)
1335*333d2b36SAndroid Build Coastguard Worker	module.Module.dexProperties.Optimize.Proguard_compatibility = proptools.BoolPtr(false)
1336*333d2b36SAndroid Build Coastguard Worker
1337*333d2b36SAndroid Build Coastguard Worker	module.Module.properties.Instrument = true
1338*333d2b36SAndroid Build Coastguard Worker	module.Module.properties.Supports_static_instrumentation = true
1339*333d2b36SAndroid Build Coastguard Worker	module.Module.properties.Installable = proptools.BoolPtr(true)
1340*333d2b36SAndroid Build Coastguard Worker
1341*333d2b36SAndroid Build Coastguard Worker	module.addHostAndDeviceProperties()
1342*333d2b36SAndroid Build Coastguard Worker	module.AddProperties(
1343*333d2b36SAndroid Build Coastguard Worker		&module.aaptProperties,
1344*333d2b36SAndroid Build Coastguard Worker		&module.appProperties,
1345*333d2b36SAndroid Build Coastguard Worker		&module.overridableAppProperties,
1346*333d2b36SAndroid Build Coastguard Worker		&module.Library.sourceProperties)
1347*333d2b36SAndroid Build Coastguard Worker
1348*333d2b36SAndroid Build Coastguard Worker	module.usesLibrary.enforce = true
1349*333d2b36SAndroid Build Coastguard Worker
1350*333d2b36SAndroid Build Coastguard Worker	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1351*333d2b36SAndroid Build Coastguard Worker	android.InitDefaultableModule(module)
1352*333d2b36SAndroid Build Coastguard Worker	android.InitOverridableModule(module, &module.overridableAppProperties.Overrides)
1353*333d2b36SAndroid Build Coastguard Worker	android.InitApexModule(module)
1354*333d2b36SAndroid Build Coastguard Worker
1355*333d2b36SAndroid Build Coastguard Worker	android.AddLoadHook(module, func(ctx android.LoadHookContext) {
1356*333d2b36SAndroid Build Coastguard Worker		a := ctx.Module().(*AndroidApp)
1357*333d2b36SAndroid Build Coastguard Worker
1358*333d2b36SAndroid Build Coastguard Worker		characteristics := ctx.Config().ProductAAPTCharacteristics()
1359*333d2b36SAndroid Build Coastguard Worker		if characteristics == "default" || characteristics == "" {
1360*333d2b36SAndroid Build Coastguard Worker			module.appProperties.Generate_product_characteristics_rro = nil
1361*333d2b36SAndroid Build Coastguard Worker			// no need to create RRO
1362*333d2b36SAndroid Build Coastguard Worker			return
1363*333d2b36SAndroid Build Coastguard Worker		}
1364*333d2b36SAndroid Build Coastguard Worker
1365*333d2b36SAndroid Build Coastguard Worker		if !proptools.Bool(module.appProperties.Generate_product_characteristics_rro) {
1366*333d2b36SAndroid Build Coastguard Worker			return
1367*333d2b36SAndroid Build Coastguard Worker		}
1368*333d2b36SAndroid Build Coastguard Worker
1369*333d2b36SAndroid Build Coastguard Worker		rroPackageName := a.Name() + "__" + strings.ReplaceAll(characteristics, ",", "_") + "__auto_generated_characteristics_rro"
1370*333d2b36SAndroid Build Coastguard Worker		rroManifestName := rroPackageName + "_manifest"
1371*333d2b36SAndroid Build Coastguard Worker
1372*333d2b36SAndroid Build Coastguard Worker		a.appProperties.ProductCharacteristicsRROPackageName = proptools.StringPtr(rroPackageName)
1373*333d2b36SAndroid Build Coastguard Worker		a.appProperties.ProductCharacteristicsRROManifestModuleName = proptools.StringPtr(rroManifestName)
1374*333d2b36SAndroid Build Coastguard Worker
1375*333d2b36SAndroid Build Coastguard Worker		rroManifestProperties := struct {
1376*333d2b36SAndroid Build Coastguard Worker			Name  *string
1377*333d2b36SAndroid Build Coastguard Worker			Tools []string
1378*333d2b36SAndroid Build Coastguard Worker			Out   []string
1379*333d2b36SAndroid Build Coastguard Worker			Srcs  []string
1380*333d2b36SAndroid Build Coastguard Worker			Cmd   *string
1381*333d2b36SAndroid Build Coastguard Worker		}{
1382*333d2b36SAndroid Build Coastguard Worker			Name:  proptools.StringPtr(rroManifestName),
1383*333d2b36SAndroid Build Coastguard Worker			Tools: []string{"characteristics_rro_generator", "aapt2"},
1384*333d2b36SAndroid Build Coastguard Worker			Out:   []string{"AndroidManifest.xml"},
1385*333d2b36SAndroid Build Coastguard Worker			Srcs:  []string{":" + a.Name() + "{.apk}"},
1386*333d2b36SAndroid Build Coastguard Worker			Cmd:   proptools.StringPtr("$(location characteristics_rro_generator) $$($(location aapt2) dump packagename $(in)) $(out)"),
1387*333d2b36SAndroid Build Coastguard Worker		}
1388*333d2b36SAndroid Build Coastguard Worker		ctx.CreateModule(GenRuleFactory, &rroManifestProperties)
1389*333d2b36SAndroid Build Coastguard Worker
1390*333d2b36SAndroid Build Coastguard Worker		rroProperties := struct {
1391*333d2b36SAndroid Build Coastguard Worker			Name           *string
1392*333d2b36SAndroid Build Coastguard Worker			Filter_product *string
1393*333d2b36SAndroid Build Coastguard Worker			Aaptflags      []string
1394*333d2b36SAndroid Build Coastguard Worker			Manifest       *string
1395*333d2b36SAndroid Build Coastguard Worker			Resource_dirs  proptools.Configurable[[]string]
1396*333d2b36SAndroid Build Coastguard Worker			Flags_packages []string
1397*333d2b36SAndroid Build Coastguard Worker		}{
1398*333d2b36SAndroid Build Coastguard Worker			Name:           proptools.StringPtr(rroPackageName),
1399*333d2b36SAndroid Build Coastguard Worker			Filter_product: proptools.StringPtr(characteristics),
1400*333d2b36SAndroid Build Coastguard Worker			Aaptflags:      []string{"--auto-add-overlay"},
1401*333d2b36SAndroid Build Coastguard Worker			Manifest:       proptools.StringPtr(":" + rroManifestName),
1402*333d2b36SAndroid Build Coastguard Worker			Resource_dirs:  a.aaptProperties.Resource_dirs,
1403*333d2b36SAndroid Build Coastguard Worker			Flags_packages: a.aaptProperties.Flags_packages,
1404*333d2b36SAndroid Build Coastguard Worker		}
1405*333d2b36SAndroid Build Coastguard Worker		if !Bool(a.aaptProperties.Aapt_include_all_resources) {
1406*333d2b36SAndroid Build Coastguard Worker			for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
1407*333d2b36SAndroid Build Coastguard Worker				rroProperties.Aaptflags = append(rroProperties.Aaptflags, "-c", aaptConfig)
1408*333d2b36SAndroid Build Coastguard Worker			}
1409*333d2b36SAndroid Build Coastguard Worker		}
1410*333d2b36SAndroid Build Coastguard Worker		ctx.CreateModule(RuntimeResourceOverlayFactory, &rroProperties)
1411*333d2b36SAndroid Build Coastguard Worker
1412*333d2b36SAndroid Build Coastguard Worker	})
1413*333d2b36SAndroid Build Coastguard Worker
1414*333d2b36SAndroid Build Coastguard Worker	module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
1415*333d2b36SAndroid Build Coastguard Worker		createInternalRuntimeOverlays(ctx, module.ModuleBase)
1416*333d2b36SAndroid Build Coastguard Worker	})
1417*333d2b36SAndroid Build Coastguard Worker
1418*333d2b36SAndroid Build Coastguard Worker	return module
1419*333d2b36SAndroid Build Coastguard Worker}
1420*333d2b36SAndroid Build Coastguard Worker
1421*333d2b36SAndroid Build Coastguard Workerfunc AutogeneratedRroModuleName(ctx android.EarlyModuleContext, moduleName, partition string) string {
1422*333d2b36SAndroid Build Coastguard Worker	return fmt.Sprintf("%s__%s__auto_generated_rro_%s", moduleName, ctx.Config().DeviceProduct(), partition)
1423*333d2b36SAndroid Build Coastguard Worker}
1424*333d2b36SAndroid Build Coastguard Worker
1425*333d2b36SAndroid Build Coastguard Workertype createModuleContext interface {
1426*333d2b36SAndroid Build Coastguard Worker	android.EarlyModuleContext
1427*333d2b36SAndroid Build Coastguard Worker	CreateModule(android.ModuleFactory, ...interface{}) android.Module
1428*333d2b36SAndroid Build Coastguard Worker}
1429*333d2b36SAndroid Build Coastguard Worker
1430*333d2b36SAndroid Build Coastguard Workerfunc createInternalRuntimeOverlays(ctx createModuleContext, a android.ModuleBase) {
1431*333d2b36SAndroid Build Coastguard Worker	if !ctx.Config().HasDeviceProduct() {
1432*333d2b36SAndroid Build Coastguard Worker		return
1433*333d2b36SAndroid Build Coastguard Worker	}
1434*333d2b36SAndroid Build Coastguard Worker	// vendor
1435*333d2b36SAndroid Build Coastguard Worker	vendorOverlayProps := struct {
1436*333d2b36SAndroid Build Coastguard Worker		Name                *string
1437*333d2b36SAndroid Build Coastguard Worker		Base                *string
1438*333d2b36SAndroid Build Coastguard Worker		Vendor              *bool
1439*333d2b36SAndroid Build Coastguard Worker		Product_specific    *bool
1440*333d2b36SAndroid Build Coastguard Worker		System_ext_specific *bool
1441*333d2b36SAndroid Build Coastguard Worker		Manifest            *string
1442*333d2b36SAndroid Build Coastguard Worker		Sdk_version         *string
1443*333d2b36SAndroid Build Coastguard Worker		Compile_multilib    *string
1444*333d2b36SAndroid Build Coastguard Worker		Enabled             proptools.Configurable[bool]
1445*333d2b36SAndroid Build Coastguard Worker	}{
1446*333d2b36SAndroid Build Coastguard Worker		Name:                proptools.StringPtr(AutogeneratedRroModuleName(ctx, a.Name(), "vendor")),
1447*333d2b36SAndroid Build Coastguard Worker		Base:                proptools.StringPtr(a.Name()),
1448*333d2b36SAndroid Build Coastguard Worker		Vendor:              proptools.BoolPtr(true),
1449*333d2b36SAndroid Build Coastguard Worker		Product_specific:    proptools.BoolPtr(false),
1450*333d2b36SAndroid Build Coastguard Worker		System_ext_specific: proptools.BoolPtr(false),
1451*333d2b36SAndroid Build Coastguard Worker		Manifest:            proptools.StringPtr(":" + a.Name() + "{.manifest.xml}"),
1452*333d2b36SAndroid Build Coastguard Worker		Sdk_version:         proptools.StringPtr("current"),
1453*333d2b36SAndroid Build Coastguard Worker		Compile_multilib:    proptools.StringPtr("first"),
1454*333d2b36SAndroid Build Coastguard Worker		Enabled:             a.EnabledProperty().Clone(),
1455*333d2b36SAndroid Build Coastguard Worker	}
1456*333d2b36SAndroid Build Coastguard Worker	ctx.CreateModule(AutogenRuntimeResourceOverlayFactory, &vendorOverlayProps)
1457*333d2b36SAndroid Build Coastguard Worker
1458*333d2b36SAndroid Build Coastguard Worker	// product
1459*333d2b36SAndroid Build Coastguard Worker	productOverlayProps := struct {
1460*333d2b36SAndroid Build Coastguard Worker		Name                *string
1461*333d2b36SAndroid Build Coastguard Worker		Base                *string
1462*333d2b36SAndroid Build Coastguard Worker		Vendor              *bool
1463*333d2b36SAndroid Build Coastguard Worker		Proprietary         *bool
1464*333d2b36SAndroid Build Coastguard Worker		Soc_specific        *bool
1465*333d2b36SAndroid Build Coastguard Worker		Product_specific    *bool
1466*333d2b36SAndroid Build Coastguard Worker		System_ext_specific *bool
1467*333d2b36SAndroid Build Coastguard Worker		Manifest            *string
1468*333d2b36SAndroid Build Coastguard Worker		Sdk_version         *string
1469*333d2b36SAndroid Build Coastguard Worker		Compile_multilib    *string
1470*333d2b36SAndroid Build Coastguard Worker		Enabled             proptools.Configurable[bool]
1471*333d2b36SAndroid Build Coastguard Worker	}{
1472*333d2b36SAndroid Build Coastguard Worker		Name:                proptools.StringPtr(AutogeneratedRroModuleName(ctx, a.Name(), "product")),
1473*333d2b36SAndroid Build Coastguard Worker		Base:                proptools.StringPtr(a.Name()),
1474*333d2b36SAndroid Build Coastguard Worker		Vendor:              proptools.BoolPtr(false),
1475*333d2b36SAndroid Build Coastguard Worker		Proprietary:         proptools.BoolPtr(false),
1476*333d2b36SAndroid Build Coastguard Worker		Soc_specific:        proptools.BoolPtr(false),
1477*333d2b36SAndroid Build Coastguard Worker		Product_specific:    proptools.BoolPtr(true),
1478*333d2b36SAndroid Build Coastguard Worker		System_ext_specific: proptools.BoolPtr(false),
1479*333d2b36SAndroid Build Coastguard Worker		Manifest:            proptools.StringPtr(":" + a.Name() + "{.manifest.xml}"),
1480*333d2b36SAndroid Build Coastguard Worker		Sdk_version:         proptools.StringPtr("current"),
1481*333d2b36SAndroid Build Coastguard Worker		Compile_multilib:    proptools.StringPtr("first"),
1482*333d2b36SAndroid Build Coastguard Worker		Enabled:             a.EnabledProperty().Clone(),
1483*333d2b36SAndroid Build Coastguard Worker	}
1484*333d2b36SAndroid Build Coastguard Worker	ctx.CreateModule(AutogenRuntimeResourceOverlayFactory, &productOverlayProps)
1485*333d2b36SAndroid Build Coastguard Worker}
1486*333d2b36SAndroid Build Coastguard Worker
1487*333d2b36SAndroid Build Coastguard Worker// A dictionary of values to be overridden in the manifest.
1488*333d2b36SAndroid Build Coastguard Workertype Manifest_values struct {
1489*333d2b36SAndroid Build Coastguard Worker	// Overrides the value of package_name in the manifest
1490*333d2b36SAndroid Build Coastguard Worker	ApplicationId *string
1491*333d2b36SAndroid Build Coastguard Worker}
1492*333d2b36SAndroid Build Coastguard Worker
1493*333d2b36SAndroid Build Coastguard Workertype appTestProperties struct {
1494*333d2b36SAndroid Build Coastguard Worker	// The name of the android_app module that the tests will run against.
1495*333d2b36SAndroid Build Coastguard Worker	Instrumentation_for *string
1496*333d2b36SAndroid Build Coastguard Worker
1497*333d2b36SAndroid Build Coastguard Worker	// If specified, the instrumentation target package name in the manifest is overwritten by it.
1498*333d2b36SAndroid Build Coastguard Worker	Instrumentation_target_package *string
1499*333d2b36SAndroid Build Coastguard Worker
1500*333d2b36SAndroid Build Coastguard Worker	// If specified, the mainline module package name in the test config is overwritten by it.
1501*333d2b36SAndroid Build Coastguard Worker	Mainline_package_name *string
1502*333d2b36SAndroid Build Coastguard Worker
1503*333d2b36SAndroid Build Coastguard Worker	Manifest_values Manifest_values
1504*333d2b36SAndroid Build Coastguard Worker}
1505*333d2b36SAndroid Build Coastguard Worker
1506*333d2b36SAndroid Build Coastguard Workertype AndroidTest struct {
1507*333d2b36SAndroid Build Coastguard Worker	AndroidApp
1508*333d2b36SAndroid Build Coastguard Worker
1509*333d2b36SAndroid Build Coastguard Worker	appTestProperties appTestProperties
1510*333d2b36SAndroid Build Coastguard Worker
1511*333d2b36SAndroid Build Coastguard Worker	testProperties testProperties
1512*333d2b36SAndroid Build Coastguard Worker
1513*333d2b36SAndroid Build Coastguard Worker	testConfig       android.Path
1514*333d2b36SAndroid Build Coastguard Worker	extraTestConfigs android.Paths
1515*333d2b36SAndroid Build Coastguard Worker	data             android.Paths
1516*333d2b36SAndroid Build Coastguard Worker}
1517*333d2b36SAndroid Build Coastguard Worker
1518*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidTest) InstallInTestcases() bool {
1519*333d2b36SAndroid Build Coastguard Worker	return true
1520*333d2b36SAndroid Build Coastguard Worker}
1521*333d2b36SAndroid Build Coastguard Worker
1522*333d2b36SAndroid Build Coastguard Workertype androidTestApp interface {
1523*333d2b36SAndroid Build Coastguard Worker	includedInTestSuite(searchPrefix string) bool
1524*333d2b36SAndroid Build Coastguard Worker}
1525*333d2b36SAndroid Build Coastguard Worker
1526*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidTest) includedInTestSuite(searchPrefix string) bool {
1527*333d2b36SAndroid Build Coastguard Worker	return android.PrefixInList(a.testProperties.Test_suites, searchPrefix)
1528*333d2b36SAndroid Build Coastguard Worker}
1529*333d2b36SAndroid Build Coastguard Worker
1530*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidTestHelperApp) includedInTestSuite(searchPrefix string) bool {
1531*333d2b36SAndroid Build Coastguard Worker	return android.PrefixInList(a.appTestHelperAppProperties.Test_suites, searchPrefix)
1532*333d2b36SAndroid Build Coastguard Worker}
1533*333d2b36SAndroid Build Coastguard Worker
1534*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1535*333d2b36SAndroid Build Coastguard Worker	checkMinSdkVersionMts(ctx, a.MinSdkVersion(ctx))
1536*333d2b36SAndroid Build Coastguard Worker	var configs []tradefed.Config
1537*333d2b36SAndroid Build Coastguard Worker	if a.appTestProperties.Instrumentation_target_package != nil {
1538*333d2b36SAndroid Build Coastguard Worker		a.additionalAaptFlags = append(a.additionalAaptFlags,
1539*333d2b36SAndroid Build Coastguard Worker			"--rename-instrumentation-target-package "+*a.appTestProperties.Instrumentation_target_package)
1540*333d2b36SAndroid Build Coastguard Worker	} else if a.appTestProperties.Instrumentation_for != nil {
1541*333d2b36SAndroid Build Coastguard Worker		// Check if the instrumentation target package is overridden.
1542*333d2b36SAndroid Build Coastguard Worker		manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
1543*333d2b36SAndroid Build Coastguard Worker		if overridden {
1544*333d2b36SAndroid Build Coastguard Worker			a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
1545*333d2b36SAndroid Build Coastguard Worker		}
1546*333d2b36SAndroid Build Coastguard Worker	}
1547*333d2b36SAndroid Build Coastguard Worker	applicationId := a.appTestProperties.Manifest_values.ApplicationId
1548*333d2b36SAndroid Build Coastguard Worker	if applicationId != nil {
1549*333d2b36SAndroid Build Coastguard Worker		packageNameProp := a.overridableAppProperties.Package_name.Get(ctx)
1550*333d2b36SAndroid Build Coastguard Worker		if packageNameProp.IsPresent() {
1551*333d2b36SAndroid Build Coastguard Worker			ctx.PropertyErrorf("manifest_values.applicationId", "property is not supported when property package_name is set.")
1552*333d2b36SAndroid Build Coastguard Worker		}
1553*333d2b36SAndroid Build Coastguard Worker		a.aapt.manifestValues.applicationId = *applicationId
1554*333d2b36SAndroid Build Coastguard Worker	}
1555*333d2b36SAndroid Build Coastguard Worker	a.generateAndroidBuildActions(ctx)
1556*333d2b36SAndroid Build Coastguard Worker
1557*333d2b36SAndroid Build Coastguard Worker	for _, module := range a.testProperties.Test_mainline_modules {
1558*333d2b36SAndroid Build Coastguard Worker		configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
1559*333d2b36SAndroid Build Coastguard Worker	}
1560*333d2b36SAndroid Build Coastguard Worker
1561*333d2b36SAndroid Build Coastguard Worker	testConfig := tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config,
1562*333d2b36SAndroid Build Coastguard Worker		a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites,
1563*333d2b36SAndroid Build Coastguard Worker		a.testProperties.Auto_gen_config, configs, a.testProperties.Test_options.Test_runner_options)
1564*333d2b36SAndroid Build Coastguard Worker	a.testConfig = a.FixTestConfig(ctx, testConfig)
1565*333d2b36SAndroid Build Coastguard Worker	a.extraTestConfigs = android.PathsForModuleSrc(ctx, a.testProperties.Test_options.Extra_test_configs)
1566*333d2b36SAndroid Build Coastguard Worker	a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
1567*333d2b36SAndroid Build Coastguard Worker	a.data = append(a.data, android.PathsForModuleSrc(ctx, a.testProperties.Device_common_data)...)
1568*333d2b36SAndroid Build Coastguard Worker	a.data = append(a.data, android.PathsForModuleSrc(ctx, a.testProperties.Device_first_data)...)
1569*333d2b36SAndroid Build Coastguard Worker	a.data = append(a.data, android.PathsForModuleSrc(ctx, a.testProperties.Device_first_prefer32_data)...)
1570*333d2b36SAndroid Build Coastguard Worker
1571*333d2b36SAndroid Build Coastguard Worker	android.SetProvider(ctx, tradefed.BaseTestProviderKey, tradefed.BaseTestProviderData{
1572*333d2b36SAndroid Build Coastguard Worker		TestcaseRelDataFiles:    testcaseRel(a.data),
1573*333d2b36SAndroid Build Coastguard Worker		OutputFile:              a.OutputFile(),
1574*333d2b36SAndroid Build Coastguard Worker		TestConfig:              a.testConfig,
1575*333d2b36SAndroid Build Coastguard Worker		HostRequiredModuleNames: a.HostRequiredModuleNames(),
1576*333d2b36SAndroid Build Coastguard Worker		TestSuites:              a.testProperties.Test_suites,
1577*333d2b36SAndroid Build Coastguard Worker		IsHost:                  false,
1578*333d2b36SAndroid Build Coastguard Worker		LocalCertificate:        a.certificate.AndroidMkString(),
1579*333d2b36SAndroid Build Coastguard Worker		IsUnitTest:              Bool(a.testProperties.Test_options.Unit_test),
1580*333d2b36SAndroid Build Coastguard Worker		MkInclude:               "$(BUILD_SYSTEM)/soong_app_prebuilt.mk",
1581*333d2b36SAndroid Build Coastguard Worker		MkAppClass:              "APPS",
1582*333d2b36SAndroid Build Coastguard Worker	})
1583*333d2b36SAndroid Build Coastguard Worker	android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{
1584*333d2b36SAndroid Build Coastguard Worker		TestOnly:       true,
1585*333d2b36SAndroid Build Coastguard Worker		TopLevelTarget: true,
1586*333d2b36SAndroid Build Coastguard Worker	})
1587*333d2b36SAndroid Build Coastguard Worker
1588*333d2b36SAndroid Build Coastguard Worker}
1589*333d2b36SAndroid Build Coastguard Worker
1590*333d2b36SAndroid Build Coastguard Workerfunc testcaseRel(paths android.Paths) []string {
1591*333d2b36SAndroid Build Coastguard Worker	relPaths := []string{}
1592*333d2b36SAndroid Build Coastguard Worker	for _, p := range paths {
1593*333d2b36SAndroid Build Coastguard Worker		relPaths = append(relPaths, p.Rel())
1594*333d2b36SAndroid Build Coastguard Worker	}
1595*333d2b36SAndroid Build Coastguard Worker	return relPaths
1596*333d2b36SAndroid Build Coastguard Worker}
1597*333d2b36SAndroid Build Coastguard Worker
1598*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidTest) FixTestConfig(ctx android.ModuleContext, testConfig android.Path) android.Path {
1599*333d2b36SAndroid Build Coastguard Worker	if testConfig == nil {
1600*333d2b36SAndroid Build Coastguard Worker		return nil
1601*333d2b36SAndroid Build Coastguard Worker	}
1602*333d2b36SAndroid Build Coastguard Worker
1603*333d2b36SAndroid Build Coastguard Worker	fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", "AndroidTest.xml")
1604*333d2b36SAndroid Build Coastguard Worker	rule := android.NewRuleBuilder(pctx, ctx)
1605*333d2b36SAndroid Build Coastguard Worker	command := rule.Command().BuiltTool("test_config_fixer").Input(testConfig).Output(fixedConfig)
1606*333d2b36SAndroid Build Coastguard Worker	fixNeeded := false
1607*333d2b36SAndroid Build Coastguard Worker
1608*333d2b36SAndroid Build Coastguard Worker	// Auto-generated test config uses `ModuleName` as the APK name. So fix it if it is not the case.
1609*333d2b36SAndroid Build Coastguard Worker	if ctx.ModuleName() != a.installApkName {
1610*333d2b36SAndroid Build Coastguard Worker		fixNeeded = true
1611*333d2b36SAndroid Build Coastguard Worker		command.FlagWithArg("--test-file-name ", a.installApkName+".apk")
1612*333d2b36SAndroid Build Coastguard Worker	}
1613*333d2b36SAndroid Build Coastguard Worker
1614*333d2b36SAndroid Build Coastguard Worker	packageNameProp := a.overridableAppProperties.Package_name.Get(ctx)
1615*333d2b36SAndroid Build Coastguard Worker	if packageNameProp.IsPresent() {
1616*333d2b36SAndroid Build Coastguard Worker		fixNeeded = true
1617*333d2b36SAndroid Build Coastguard Worker		command.FlagWithInput("--manifest ", a.manifestPath).
1618*333d2b36SAndroid Build Coastguard Worker			FlagWithArg("--package-name ", packageNameProp.Get())
1619*333d2b36SAndroid Build Coastguard Worker	}
1620*333d2b36SAndroid Build Coastguard Worker
1621*333d2b36SAndroid Build Coastguard Worker	if a.appTestProperties.Mainline_package_name != nil {
1622*333d2b36SAndroid Build Coastguard Worker		fixNeeded = true
1623*333d2b36SAndroid Build Coastguard Worker		command.FlagWithArg("--mainline-package-name ", *a.appTestProperties.Mainline_package_name)
1624*333d2b36SAndroid Build Coastguard Worker	}
1625*333d2b36SAndroid Build Coastguard Worker
1626*333d2b36SAndroid Build Coastguard Worker	if fixNeeded {
1627*333d2b36SAndroid Build Coastguard Worker		rule.Build("fix_test_config", "fix test config")
1628*333d2b36SAndroid Build Coastguard Worker		return fixedConfig
1629*333d2b36SAndroid Build Coastguard Worker	}
1630*333d2b36SAndroid Build Coastguard Worker	return testConfig
1631*333d2b36SAndroid Build Coastguard Worker}
1632*333d2b36SAndroid Build Coastguard Worker
1633*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidTestHelperApp) DepsMutator(ctx android.BottomUpMutatorContext) {
1634*333d2b36SAndroid Build Coastguard Worker	if len(a.ApexProperties.Apex_available) == 0 && ctx.Config().IsEnvTrue("EMMA_API_MAPPER") {
1635*333d2b36SAndroid Build Coastguard Worker		// Instrument the android_test_helper target to log potential API calls at the run time.
1636*333d2b36SAndroid Build Coastguard Worker		// Contact android-xts-infra team before using the environment var EMMA_API_MAPPER.
1637*333d2b36SAndroid Build Coastguard Worker		ctx.AddVariationDependencies(nil, staticLibTag, "apimapper-helper-device-lib")
1638*333d2b36SAndroid Build Coastguard Worker		a.setApiMapper(true)
1639*333d2b36SAndroid Build Coastguard Worker	}
1640*333d2b36SAndroid Build Coastguard Worker	a.AndroidApp.DepsMutator(ctx)
1641*333d2b36SAndroid Build Coastguard Worker}
1642*333d2b36SAndroid Build Coastguard Worker
1643*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
1644*333d2b36SAndroid Build Coastguard Worker	if len(a.ApexProperties.Apex_available) == 0 && ctx.Config().IsEnvTrue("EMMA_API_MAPPER") {
1645*333d2b36SAndroid Build Coastguard Worker		// Instrument the android_test_helper target to log potential API calls at the run time.
1646*333d2b36SAndroid Build Coastguard Worker		// Contact android-xts-infra team before using the environment var EMMA_API_MAPPER.
1647*333d2b36SAndroid Build Coastguard Worker		ctx.AddVariationDependencies(nil, staticLibTag, "apimapper-helper-device-lib")
1648*333d2b36SAndroid Build Coastguard Worker		a.setApiMapper(true)
1649*333d2b36SAndroid Build Coastguard Worker	}
1650*333d2b36SAndroid Build Coastguard Worker	a.AndroidApp.DepsMutator(ctx)
1651*333d2b36SAndroid Build Coastguard Worker}
1652*333d2b36SAndroid Build Coastguard Worker
1653*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidTest) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
1654*333d2b36SAndroid Build Coastguard Worker	a.AndroidApp.OverridablePropertiesDepsMutator(ctx)
1655*333d2b36SAndroid Build Coastguard Worker	if a.appTestProperties.Instrumentation_for != nil {
1656*333d2b36SAndroid Build Coastguard Worker		// The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
1657*333d2b36SAndroid Build Coastguard Worker		// but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
1658*333d2b36SAndroid Build Coastguard Worker		// use instrumentationForTag instead of libTag.
1659*333d2b36SAndroid Build Coastguard Worker		ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
1660*333d2b36SAndroid Build Coastguard Worker	}
1661*333d2b36SAndroid Build Coastguard Worker}
1662*333d2b36SAndroid Build Coastguard Worker
1663*333d2b36SAndroid Build Coastguard Worker// android_test compiles test sources and Android resources into an Android application package `.apk` file and
1664*333d2b36SAndroid Build Coastguard Worker// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1665*333d2b36SAndroid Build Coastguard Workerfunc AndroidTestFactory() android.Module {
1666*333d2b36SAndroid Build Coastguard Worker	module := &AndroidTest{}
1667*333d2b36SAndroid Build Coastguard Worker
1668*333d2b36SAndroid Build Coastguard Worker	module.Module.dexProperties.Optimize.EnabledByDefault = false
1669*333d2b36SAndroid Build Coastguard Worker
1670*333d2b36SAndroid Build Coastguard Worker	module.Module.properties.Instrument = true
1671*333d2b36SAndroid Build Coastguard Worker	module.Module.properties.Supports_static_instrumentation = true
1672*333d2b36SAndroid Build Coastguard Worker	module.Module.properties.Installable = proptools.BoolPtr(true)
1673*333d2b36SAndroid Build Coastguard Worker	module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
1674*333d2b36SAndroid Build Coastguard Worker	module.appProperties.AlwaysPackageNativeLibs = true
1675*333d2b36SAndroid Build Coastguard Worker	module.Module.dexpreopter.isTest = true
1676*333d2b36SAndroid Build Coastguard Worker	module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true)
1677*333d2b36SAndroid Build Coastguard Worker
1678*333d2b36SAndroid Build Coastguard Worker	module.addHostAndDeviceProperties()
1679*333d2b36SAndroid Build Coastguard Worker	module.AddProperties(
1680*333d2b36SAndroid Build Coastguard Worker		&module.aaptProperties,
1681*333d2b36SAndroid Build Coastguard Worker		&module.appProperties,
1682*333d2b36SAndroid Build Coastguard Worker		&module.appTestProperties,
1683*333d2b36SAndroid Build Coastguard Worker		&module.overridableAppProperties,
1684*333d2b36SAndroid Build Coastguard Worker		&module.testProperties)
1685*333d2b36SAndroid Build Coastguard Worker
1686*333d2b36SAndroid Build Coastguard Worker	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1687*333d2b36SAndroid Build Coastguard Worker	android.InitDefaultableModule(module)
1688*333d2b36SAndroid Build Coastguard Worker	android.InitOverridableModule(module, &module.overridableAppProperties.Overrides)
1689*333d2b36SAndroid Build Coastguard Worker
1690*333d2b36SAndroid Build Coastguard Worker	return module
1691*333d2b36SAndroid Build Coastguard Worker}
1692*333d2b36SAndroid Build Coastguard Worker
1693*333d2b36SAndroid Build Coastguard Workertype appTestHelperAppProperties struct {
1694*333d2b36SAndroid Build Coastguard Worker	// list of compatibility suites (for example "cts", "vts") that the module should be
1695*333d2b36SAndroid Build Coastguard Worker	// installed into.
1696*333d2b36SAndroid Build Coastguard Worker	Test_suites []string `android:"arch_variant"`
1697*333d2b36SAndroid Build Coastguard Worker
1698*333d2b36SAndroid Build Coastguard Worker	// Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1699*333d2b36SAndroid Build Coastguard Worker	// doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1700*333d2b36SAndroid Build Coastguard Worker	// explicitly.
1701*333d2b36SAndroid Build Coastguard Worker	Auto_gen_config *bool
1702*333d2b36SAndroid Build Coastguard Worker
1703*333d2b36SAndroid Build Coastguard Worker	// Install the test into a folder named for the module in all test suites.
1704*333d2b36SAndroid Build Coastguard Worker	Per_testcase_directory *bool
1705*333d2b36SAndroid Build Coastguard Worker
1706*333d2b36SAndroid Build Coastguard Worker	Manifest_values Manifest_values
1707*333d2b36SAndroid Build Coastguard Worker}
1708*333d2b36SAndroid Build Coastguard Worker
1709*333d2b36SAndroid Build Coastguard Workertype AndroidTestHelperApp struct {
1710*333d2b36SAndroid Build Coastguard Worker	AndroidApp
1711*333d2b36SAndroid Build Coastguard Worker
1712*333d2b36SAndroid Build Coastguard Worker	appTestHelperAppProperties appTestHelperAppProperties
1713*333d2b36SAndroid Build Coastguard Worker}
1714*333d2b36SAndroid Build Coastguard Worker
1715*333d2b36SAndroid Build Coastguard Workerfunc (a *AndroidTestHelperApp) InstallInTestcases() bool {
1716*333d2b36SAndroid Build Coastguard Worker	return true
1717*333d2b36SAndroid Build Coastguard Worker}
1718*333d2b36SAndroid Build Coastguard Worker
1719*333d2b36SAndroid Build Coastguard Worker// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
1720*333d2b36SAndroid Build Coastguard Worker// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
1721*333d2b36SAndroid Build Coastguard Worker// test.
1722*333d2b36SAndroid Build Coastguard Workerfunc AndroidTestHelperAppFactory() android.Module {
1723*333d2b36SAndroid Build Coastguard Worker	module := &AndroidTestHelperApp{}
1724*333d2b36SAndroid Build Coastguard Worker
1725*333d2b36SAndroid Build Coastguard Worker	// TODO(b/192032291): Disable by default after auditing downstream usage.
1726*333d2b36SAndroid Build Coastguard Worker	module.Module.dexProperties.Optimize.EnabledByDefault = true
1727*333d2b36SAndroid Build Coastguard Worker
1728*333d2b36SAndroid Build Coastguard Worker	module.Module.properties.Installable = proptools.BoolPtr(true)
1729*333d2b36SAndroid Build Coastguard Worker	module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
1730*333d2b36SAndroid Build Coastguard Worker	module.appProperties.AlwaysPackageNativeLibs = true
1731*333d2b36SAndroid Build Coastguard Worker	module.Module.dexpreopter.isTest = true
1732*333d2b36SAndroid Build Coastguard Worker	module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true)
1733*333d2b36SAndroid Build Coastguard Worker
1734*333d2b36SAndroid Build Coastguard Worker	module.addHostAndDeviceProperties()
1735*333d2b36SAndroid Build Coastguard Worker	module.AddProperties(
1736*333d2b36SAndroid Build Coastguard Worker		&module.aaptProperties,
1737*333d2b36SAndroid Build Coastguard Worker		&module.appProperties,
1738*333d2b36SAndroid Build Coastguard Worker		&module.appTestHelperAppProperties,
1739*333d2b36SAndroid Build Coastguard Worker		&module.overridableAppProperties)
1740*333d2b36SAndroid Build Coastguard Worker
1741*333d2b36SAndroid Build Coastguard Worker	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1742*333d2b36SAndroid Build Coastguard Worker	android.InitDefaultableModule(module)
1743*333d2b36SAndroid Build Coastguard Worker	android.InitApexModule(module)
1744*333d2b36SAndroid Build Coastguard Worker	return module
1745*333d2b36SAndroid Build Coastguard Worker}
1746*333d2b36SAndroid Build Coastguard Worker
1747*333d2b36SAndroid Build Coastguard Workertype AndroidAppCertificate struct {
1748*333d2b36SAndroid Build Coastguard Worker	android.ModuleBase
1749*333d2b36SAndroid Build Coastguard Worker
1750*333d2b36SAndroid Build Coastguard Worker	properties  AndroidAppCertificateProperties
1751*333d2b36SAndroid Build Coastguard Worker	Certificate Certificate
1752*333d2b36SAndroid Build Coastguard Worker}
1753*333d2b36SAndroid Build Coastguard Worker
1754*333d2b36SAndroid Build Coastguard Workertype AndroidAppCertificateProperties struct {
1755*333d2b36SAndroid Build Coastguard Worker	// Name of the certificate files.  Extensions .x509.pem and .pk8 will be added to the name.
1756*333d2b36SAndroid Build Coastguard Worker	Certificate *string
1757*333d2b36SAndroid Build Coastguard Worker}
1758*333d2b36SAndroid Build Coastguard Worker
1759*333d2b36SAndroid Build Coastguard Worker// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
1760*333d2b36SAndroid Build Coastguard Worker// the signing key.
1761*333d2b36SAndroid Build Coastguard Workerfunc AndroidAppCertificateFactory() android.Module {
1762*333d2b36SAndroid Build Coastguard Worker	module := &AndroidAppCertificate{}
1763*333d2b36SAndroid Build Coastguard Worker	module.AddProperties(&module.properties)
1764*333d2b36SAndroid Build Coastguard Worker	android.InitAndroidModule(module)
1765*333d2b36SAndroid Build Coastguard Worker	return module
1766*333d2b36SAndroid Build Coastguard Worker}
1767*333d2b36SAndroid Build Coastguard Worker
1768*333d2b36SAndroid Build Coastguard Workerfunc (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1769*333d2b36SAndroid Build Coastguard Worker	cert := String(c.properties.Certificate)
1770*333d2b36SAndroid Build Coastguard Worker	c.Certificate = Certificate{
1771*333d2b36SAndroid Build Coastguard Worker		Pem: android.PathForModuleSrc(ctx, cert+".x509.pem"),
1772*333d2b36SAndroid Build Coastguard Worker		Key: android.PathForModuleSrc(ctx, cert+".pk8"),
1773*333d2b36SAndroid Build Coastguard Worker	}
1774*333d2b36SAndroid Build Coastguard Worker}
1775*333d2b36SAndroid Build Coastguard Worker
1776*333d2b36SAndroid Build Coastguard Workertype OverrideAndroidApp struct {
1777*333d2b36SAndroid Build Coastguard Worker	android.ModuleBase
1778*333d2b36SAndroid Build Coastguard Worker	android.OverrideModuleBase
1779*333d2b36SAndroid Build Coastguard Worker}
1780*333d2b36SAndroid Build Coastguard Worker
1781*333d2b36SAndroid Build Coastguard Workerfunc (i *OverrideAndroidApp) GenerateAndroidBuildActions(_ android.ModuleContext) {
1782*333d2b36SAndroid Build Coastguard Worker	// All the overrides happen in the base module.
1783*333d2b36SAndroid Build Coastguard Worker	// TODO(jungjw): Check the base module type.
1784*333d2b36SAndroid Build Coastguard Worker}
1785*333d2b36SAndroid Build Coastguard Worker
1786*333d2b36SAndroid Build Coastguard Worker// override_android_app is used to create an android_app module based on another android_app by overriding
1787*333d2b36SAndroid Build Coastguard Worker// some of its properties.
1788*333d2b36SAndroid Build Coastguard Workerfunc OverrideAndroidAppModuleFactory() android.Module {
1789*333d2b36SAndroid Build Coastguard Worker	m := &OverrideAndroidApp{}
1790*333d2b36SAndroid Build Coastguard Worker	m.AddProperties(
1791*333d2b36SAndroid Build Coastguard Worker		&OverridableProperties{},
1792*333d2b36SAndroid Build Coastguard Worker		&overridableAppProperties{},
1793*333d2b36SAndroid Build Coastguard Worker	)
1794*333d2b36SAndroid Build Coastguard Worker
1795*333d2b36SAndroid Build Coastguard Worker	android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
1796*333d2b36SAndroid Build Coastguard Worker	android.InitOverrideModule(m)
1797*333d2b36SAndroid Build Coastguard Worker	android.AddLoadHookWithPriority(m, func(ctx android.LoadHookContext) {
1798*333d2b36SAndroid Build Coastguard Worker		createInternalRuntimeOverlays(ctx, m.ModuleBase)
1799*333d2b36SAndroid Build Coastguard Worker	}, 1) // Run after soong config load hoook
1800*333d2b36SAndroid Build Coastguard Worker
1801*333d2b36SAndroid Build Coastguard Worker	return m
1802*333d2b36SAndroid Build Coastguard Worker}
1803*333d2b36SAndroid Build Coastguard Worker
1804*333d2b36SAndroid Build Coastguard Workertype OverrideAndroidTest struct {
1805*333d2b36SAndroid Build Coastguard Worker	android.ModuleBase
1806*333d2b36SAndroid Build Coastguard Worker	android.OverrideModuleBase
1807*333d2b36SAndroid Build Coastguard Worker}
1808*333d2b36SAndroid Build Coastguard Worker
1809*333d2b36SAndroid Build Coastguard Workerfunc (i *OverrideAndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1810*333d2b36SAndroid Build Coastguard Worker	// All the overrides happen in the base module.
1811*333d2b36SAndroid Build Coastguard Worker	// TODO(jungjw): Check the base module type.
1812*333d2b36SAndroid Build Coastguard Worker	android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{
1813*333d2b36SAndroid Build Coastguard Worker		TestOnly:       true,
1814*333d2b36SAndroid Build Coastguard Worker		TopLevelTarget: true,
1815*333d2b36SAndroid Build Coastguard Worker	})
1816*333d2b36SAndroid Build Coastguard Worker}
1817*333d2b36SAndroid Build Coastguard Worker
1818*333d2b36SAndroid Build Coastguard Worker// override_android_test is used to create an android_app module based on another android_test by overriding
1819*333d2b36SAndroid Build Coastguard Worker// some of its properties.
1820*333d2b36SAndroid Build Coastguard Workerfunc OverrideAndroidTestModuleFactory() android.Module {
1821*333d2b36SAndroid Build Coastguard Worker	m := &OverrideAndroidTest{}
1822*333d2b36SAndroid Build Coastguard Worker	m.AddProperties(&overridableAppProperties{})
1823*333d2b36SAndroid Build Coastguard Worker	m.AddProperties(&appTestProperties{})
1824*333d2b36SAndroid Build Coastguard Worker
1825*333d2b36SAndroid Build Coastguard Worker	android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
1826*333d2b36SAndroid Build Coastguard Worker	android.InitOverrideModule(m)
1827*333d2b36SAndroid Build Coastguard Worker	return m
1828*333d2b36SAndroid Build Coastguard Worker}
1829*333d2b36SAndroid Build Coastguard Worker
1830*333d2b36SAndroid Build Coastguard Workertype UsesLibraryProperties struct {
1831*333d2b36SAndroid Build Coastguard Worker	// A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file.
1832*333d2b36SAndroid Build Coastguard Worker	Uses_libs proptools.Configurable[[]string]
1833*333d2b36SAndroid Build Coastguard Worker
1834*333d2b36SAndroid Build Coastguard Worker	// A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file with
1835*333d2b36SAndroid Build Coastguard Worker	// required=false.
1836*333d2b36SAndroid Build Coastguard Worker	Optional_uses_libs proptools.Configurable[[]string]
1837*333d2b36SAndroid Build Coastguard Worker
1838*333d2b36SAndroid Build Coastguard Worker	// If true, the list of uses_libs and optional_uses_libs modules must match the AndroidManifest.xml file.  Defaults
1839*333d2b36SAndroid Build Coastguard Worker	// to true if either uses_libs or optional_uses_libs is set.  Will unconditionally default to true in the future.
1840*333d2b36SAndroid Build Coastguard Worker	Enforce_uses_libs *bool
1841*333d2b36SAndroid Build Coastguard Worker
1842*333d2b36SAndroid Build Coastguard Worker	// Optional name of the <uses-library> provided by this module. This is needed for non-SDK
1843*333d2b36SAndroid Build Coastguard Worker	// libraries, because SDK ones are automatically picked up by Soong. The <uses-library> name
1844*333d2b36SAndroid Build Coastguard Worker	// normally is the same as the module name, but there are exceptions.
1845*333d2b36SAndroid Build Coastguard Worker	Provides_uses_lib *string
1846*333d2b36SAndroid Build Coastguard Worker
1847*333d2b36SAndroid Build Coastguard Worker	// A list of shared library names to exclude from the classpath of the APK. Adding a library here
1848*333d2b36SAndroid Build Coastguard Worker	// will prevent it from being used when precompiling the APK and prevent it from being implicitly
1849*333d2b36SAndroid Build Coastguard Worker	// added to the APK's manifest's <uses-library> elements.
1850*333d2b36SAndroid Build Coastguard Worker	//
1851*333d2b36SAndroid Build Coastguard Worker	// Care must be taken when using this as it could result in runtime errors if the APK actually
1852*333d2b36SAndroid Build Coastguard Worker	// uses classes provided by the library and which are not provided in any other way.
1853*333d2b36SAndroid Build Coastguard Worker	//
1854*333d2b36SAndroid Build Coastguard Worker	// This is primarily intended for use by various CTS tests that check the runtime handling of the
1855*333d2b36SAndroid Build Coastguard Worker	// android.test.base shared library (and related libraries) but which depend on some common
1856*333d2b36SAndroid Build Coastguard Worker	// libraries that depend on the android.test.base library. Without this those tests will end up
1857*333d2b36SAndroid Build Coastguard Worker	// with a <uses-library android:name="android.test.base"/> in their manifest which would either
1858*333d2b36SAndroid Build Coastguard Worker	// render the tests worthless (as they would be testing the wrong behavior), or would break the
1859*333d2b36SAndroid Build Coastguard Worker	// test altogether by providing access to classes that the tests were not expecting. Those tests
1860*333d2b36SAndroid Build Coastguard Worker	// provide the android.test.base statically and use jarjar to rename them so they do not collide
1861*333d2b36SAndroid Build Coastguard Worker	// with the classes provided by the android.test.base library.
1862*333d2b36SAndroid Build Coastguard Worker	Exclude_uses_libs []string
1863*333d2b36SAndroid Build Coastguard Worker
1864*333d2b36SAndroid Build Coastguard Worker	// The module names of optional uses-library libraries that are missing from the source tree.
1865*333d2b36SAndroid Build Coastguard Worker	Missing_optional_uses_libs []string `blueprint:"mutated"`
1866*333d2b36SAndroid Build Coastguard Worker}
1867*333d2b36SAndroid Build Coastguard Worker
1868*333d2b36SAndroid Build Coastguard Worker// usesLibrary provides properties and helper functions for AndroidApp and AndroidAppImport to verify that the
1869*333d2b36SAndroid Build Coastguard Worker// <uses-library> tags that end up in the manifest of an APK match the ones known to the build system through the
1870*333d2b36SAndroid Build Coastguard Worker// uses_libs and optional_uses_libs properties.  The build system's values are used by dexpreopt to preopt apps
1871*333d2b36SAndroid Build Coastguard Worker// with knowledge of their shared libraries.
1872*333d2b36SAndroid Build Coastguard Workertype usesLibrary struct {
1873*333d2b36SAndroid Build Coastguard Worker	usesLibraryProperties UsesLibraryProperties
1874*333d2b36SAndroid Build Coastguard Worker
1875*333d2b36SAndroid Build Coastguard Worker	// Whether to enforce verify_uses_library check.
1876*333d2b36SAndroid Build Coastguard Worker	enforce bool
1877*333d2b36SAndroid Build Coastguard Worker
1878*333d2b36SAndroid Build Coastguard Worker	// Whether dexpreopt should be disabled
1879*333d2b36SAndroid Build Coastguard Worker	shouldDisableDexpreopt bool
1880*333d2b36SAndroid Build Coastguard Worker}
1881*333d2b36SAndroid Build Coastguard Worker
1882*333d2b36SAndroid Build Coastguard Workerfunc (u *usesLibrary) deps(ctx android.BottomUpMutatorContext, addCompatDeps bool) {
1883*333d2b36SAndroid Build Coastguard Worker	if !ctx.Config().UnbundledBuild() || ctx.Config().UnbundledBuildImage() {
1884*333d2b36SAndroid Build Coastguard Worker		ctx.AddVariationDependencies(nil, usesLibReqTag, u.usesLibraryProperties.Uses_libs.GetOrDefault(ctx, nil)...)
1885*333d2b36SAndroid Build Coastguard Worker		presentOptionalUsesLibs := u.presentOptionalUsesLibs(ctx)
1886*333d2b36SAndroid Build Coastguard Worker		ctx.AddVariationDependencies(nil, usesLibOptTag, presentOptionalUsesLibs...)
1887*333d2b36SAndroid Build Coastguard Worker		// Only add these extra dependencies if the module is an app that depends on framework
1888*333d2b36SAndroid Build Coastguard Worker		// libs. This avoids creating a cyclic dependency:
1889*333d2b36SAndroid Build Coastguard Worker		//     e.g. framework-res -> org.apache.http.legacy -> ... -> framework-res.
1890*333d2b36SAndroid Build Coastguard Worker		if addCompatDeps {
1891*333d2b36SAndroid Build Coastguard Worker			// Dexpreopt needs paths to the dex jars of these libraries in order to construct
1892*333d2b36SAndroid Build Coastguard Worker			// class loader context for dex2oat. Add them as a dependency with a special tag.
1893*333d2b36SAndroid Build Coastguard Worker			ctx.AddVariationDependencies(nil, usesLibCompat29ReqTag, dexpreopt.CompatUsesLibs29...)
1894*333d2b36SAndroid Build Coastguard Worker			ctx.AddVariationDependencies(nil, usesLibCompat28OptTag, dexpreopt.OptionalCompatUsesLibs28...)
1895*333d2b36SAndroid Build Coastguard Worker			ctx.AddVariationDependencies(nil, usesLibCompat30OptTag, dexpreopt.OptionalCompatUsesLibs30...)
1896*333d2b36SAndroid Build Coastguard Worker		}
1897*333d2b36SAndroid Build Coastguard Worker		_, diff, _ := android.ListSetDifference(u.usesLibraryProperties.Optional_uses_libs.GetOrDefault(ctx, nil), presentOptionalUsesLibs)
1898*333d2b36SAndroid Build Coastguard Worker		u.usesLibraryProperties.Missing_optional_uses_libs = diff
1899*333d2b36SAndroid Build Coastguard Worker	} else {
1900*333d2b36SAndroid Build Coastguard Worker		ctx.AddVariationDependencies(nil, r8LibraryJarTag, u.usesLibraryProperties.Uses_libs.GetOrDefault(ctx, nil)...)
1901*333d2b36SAndroid Build Coastguard Worker		ctx.AddVariationDependencies(nil, r8LibraryJarTag, u.presentOptionalUsesLibs(ctx)...)
1902*333d2b36SAndroid Build Coastguard Worker	}
1903*333d2b36SAndroid Build Coastguard Worker}
1904*333d2b36SAndroid Build Coastguard Worker
1905*333d2b36SAndroid Build Coastguard Worker// presentOptionalUsesLibs returns optional_uses_libs after filtering out libraries that don't exist in the source tree.
1906*333d2b36SAndroid Build Coastguard Workerfunc (u *usesLibrary) presentOptionalUsesLibs(ctx android.BaseModuleContext) []string {
1907*333d2b36SAndroid Build Coastguard Worker	optionalUsesLibs := android.FilterListPred(u.usesLibraryProperties.Optional_uses_libs.GetOrDefault(ctx, nil), func(s string) bool {
1908*333d2b36SAndroid Build Coastguard Worker		exists := ctx.OtherModuleExists(s)
1909*333d2b36SAndroid Build Coastguard Worker		if !exists && !android.InList(ctx.ModuleName(), ctx.Config().BuildWarningBadOptionalUsesLibsAllowlist()) {
1910*333d2b36SAndroid Build Coastguard Worker			fmt.Printf("Warning: Module '%s' depends on non-existing optional_uses_libs '%s'\n", ctx.ModuleName(), s)
1911*333d2b36SAndroid Build Coastguard Worker		}
1912*333d2b36SAndroid Build Coastguard Worker		return exists
1913*333d2b36SAndroid Build Coastguard Worker	})
1914*333d2b36SAndroid Build Coastguard Worker	return optionalUsesLibs
1915*333d2b36SAndroid Build Coastguard Worker}
1916*333d2b36SAndroid Build Coastguard Worker
1917*333d2b36SAndroid Build Coastguard Worker// Returns a map of module names of shared library dependencies to the paths to their dex jars on
1918*333d2b36SAndroid Build Coastguard Worker// host and on device.
1919*333d2b36SAndroid Build Coastguard Workerfunc (u *usesLibrary) classLoaderContextForUsesLibDeps(ctx android.ModuleContext) dexpreopt.ClassLoaderContextMap {
1920*333d2b36SAndroid Build Coastguard Worker	clcMap := make(dexpreopt.ClassLoaderContextMap)
1921*333d2b36SAndroid Build Coastguard Worker
1922*333d2b36SAndroid Build Coastguard Worker	// Skip when UnbundledBuild() is true, but UnbundledBuildImage() is false. With
1923*333d2b36SAndroid Build Coastguard Worker	// UnbundledBuildImage() it is necessary to generate dexpreopt.config for post-dexpreopting.
1924*333d2b36SAndroid Build Coastguard Worker	if ctx.Config().UnbundledBuild() && !ctx.Config().UnbundledBuildImage() {
1925*333d2b36SAndroid Build Coastguard Worker		return clcMap
1926*333d2b36SAndroid Build Coastguard Worker	}
1927*333d2b36SAndroid Build Coastguard Worker
1928*333d2b36SAndroid Build Coastguard Worker	ctx.VisitDirectDeps(func(m android.Module) {
1929*333d2b36SAndroid Build Coastguard Worker		tag, isUsesLibTag := ctx.OtherModuleDependencyTag(m).(usesLibraryDependencyTag)
1930*333d2b36SAndroid Build Coastguard Worker		if !isUsesLibTag {
1931*333d2b36SAndroid Build Coastguard Worker			return
1932*333d2b36SAndroid Build Coastguard Worker		}
1933*333d2b36SAndroid Build Coastguard Worker
1934*333d2b36SAndroid Build Coastguard Worker		dep := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(m))
1935*333d2b36SAndroid Build Coastguard Worker
1936*333d2b36SAndroid Build Coastguard Worker		// Skip stub libraries. A dependency on the implementation library has been added earlier,
1937*333d2b36SAndroid Build Coastguard Worker		// so it will be added to CLC, but the stub shouldn't be. Stub libraries can be distingushed
1938*333d2b36SAndroid Build Coastguard Worker		// from implementation libraries by their name, which is different as it has a suffix.
1939*333d2b36SAndroid Build Coastguard Worker		if comp, ok := m.(SdkLibraryComponentDependency); ok {
1940*333d2b36SAndroid Build Coastguard Worker			if impl := comp.OptionalSdkLibraryImplementation(); impl != nil && *impl != dep {
1941*333d2b36SAndroid Build Coastguard Worker				return
1942*333d2b36SAndroid Build Coastguard Worker			}
1943*333d2b36SAndroid Build Coastguard Worker		}
1944*333d2b36SAndroid Build Coastguard Worker
1945*333d2b36SAndroid Build Coastguard Worker		if lib, ok := m.(UsesLibraryDependency); ok {
1946*333d2b36SAndroid Build Coastguard Worker			if _, ok := android.OtherModuleProvider(ctx, m, SdkLibraryInfoProvider); ok {
1947*333d2b36SAndroid Build Coastguard Worker				// Skip java_sdk_library dependencies that provide stubs, but not an implementation.
1948*333d2b36SAndroid Build Coastguard Worker				// This will be restricted to optional_uses_libs
1949*333d2b36SAndroid Build Coastguard Worker				if tag == usesLibOptTag && lib.DexJarBuildPath(ctx).PathOrNil() == nil {
1950*333d2b36SAndroid Build Coastguard Worker					u.shouldDisableDexpreopt = true
1951*333d2b36SAndroid Build Coastguard Worker					return
1952*333d2b36SAndroid Build Coastguard Worker				}
1953*333d2b36SAndroid Build Coastguard Worker			}
1954*333d2b36SAndroid Build Coastguard Worker			libName := dep
1955*333d2b36SAndroid Build Coastguard Worker			if ulib, ok := m.(ProvidesUsesLib); ok && ulib.ProvidesUsesLib() != nil {
1956*333d2b36SAndroid Build Coastguard Worker				libName = *ulib.ProvidesUsesLib()
1957*333d2b36SAndroid Build Coastguard Worker			}
1958*333d2b36SAndroid Build Coastguard Worker			clcMap.AddContext(ctx, tag.sdkVersion, libName, tag.optional,
1959*333d2b36SAndroid Build Coastguard Worker				lib.DexJarBuildPath(ctx).PathOrNil(), lib.DexJarInstallPath(),
1960*333d2b36SAndroid Build Coastguard Worker				lib.ClassLoaderContexts())
1961*333d2b36SAndroid Build Coastguard Worker		} else if ctx.Config().AllowMissingDependencies() {
1962*333d2b36SAndroid Build Coastguard Worker			ctx.AddMissingDependencies([]string{dep})
1963*333d2b36SAndroid Build Coastguard Worker		} else {
1964*333d2b36SAndroid Build Coastguard Worker			ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must be a java library", dep)
1965*333d2b36SAndroid Build Coastguard Worker		}
1966*333d2b36SAndroid Build Coastguard Worker	})
1967*333d2b36SAndroid Build Coastguard Worker	return clcMap
1968*333d2b36SAndroid Build Coastguard Worker}
1969*333d2b36SAndroid Build Coastguard Worker
1970*333d2b36SAndroid Build Coastguard Worker// enforceUsesLibraries returns true of <uses-library> tags should be checked against uses_libs and optional_uses_libs
1971*333d2b36SAndroid Build Coastguard Worker// properties.  Defaults to true if either of uses_libs or optional_uses_libs is specified.  Will default to true
1972*333d2b36SAndroid Build Coastguard Worker// unconditionally in the future.
1973*333d2b36SAndroid Build Coastguard Workerfunc (u *usesLibrary) enforceUsesLibraries(ctx android.ModuleContext) bool {
1974*333d2b36SAndroid Build Coastguard Worker	defaultEnforceUsesLibs := len(u.usesLibraryProperties.Uses_libs.GetOrDefault(ctx, nil)) > 0 ||
1975*333d2b36SAndroid Build Coastguard Worker		len(u.usesLibraryProperties.Optional_uses_libs.GetOrDefault(ctx, nil)) > 0
1976*333d2b36SAndroid Build Coastguard Worker	return BoolDefault(u.usesLibraryProperties.Enforce_uses_libs, u.enforce || defaultEnforceUsesLibs)
1977*333d2b36SAndroid Build Coastguard Worker}
1978*333d2b36SAndroid Build Coastguard Worker
1979*333d2b36SAndroid Build Coastguard Worker// Freeze the value of `enforce_uses_libs` based on the current values of `uses_libs` and `optional_uses_libs`.
1980*333d2b36SAndroid Build Coastguard Workerfunc (u *usesLibrary) freezeEnforceUsesLibraries(ctx android.ModuleContext) {
1981*333d2b36SAndroid Build Coastguard Worker	enforce := u.enforceUsesLibraries(ctx)
1982*333d2b36SAndroid Build Coastguard Worker	u.usesLibraryProperties.Enforce_uses_libs = &enforce
1983*333d2b36SAndroid Build Coastguard Worker}
1984*333d2b36SAndroid Build Coastguard Worker
1985*333d2b36SAndroid Build Coastguard Worker// verifyUsesLibraries checks the <uses-library> tags in the manifest against the ones specified
1986*333d2b36SAndroid Build Coastguard Worker// in the `uses_libs`/`optional_uses_libs` properties. The input can be either an XML manifest, or
1987*333d2b36SAndroid Build Coastguard Worker// an APK with the manifest embedded in it (manifest_check will know which one it is by the file
1988*333d2b36SAndroid Build Coastguard Worker// extension: APKs are supposed to end with '.apk').
1989*333d2b36SAndroid Build Coastguard Workerfunc (u *usesLibrary) verifyUsesLibraries(ctx android.ModuleContext, inputFile android.Path,
1990*333d2b36SAndroid Build Coastguard Worker	outputFile android.WritablePath, classLoaderContexts *dexpreopt.ClassLoaderContextMap) android.Path {
1991*333d2b36SAndroid Build Coastguard Worker
1992*333d2b36SAndroid Build Coastguard Worker	statusFile := dexpreopt.UsesLibrariesStatusFile(ctx)
1993*333d2b36SAndroid Build Coastguard Worker
1994*333d2b36SAndroid Build Coastguard Worker	// Disable verify_uses_libraries check if dexpreopt is globally disabled. Without dexpreopt the
1995*333d2b36SAndroid Build Coastguard Worker	// check is not necessary, and although it is good to have, it is difficult to maintain on
1996*333d2b36SAndroid Build Coastguard Worker	// non-linux build platforms where dexpreopt is generally disabled (the check may fail due to
1997*333d2b36SAndroid Build Coastguard Worker	// various unrelated reasons, such as a failure to get manifest from an APK).
1998*333d2b36SAndroid Build Coastguard Worker	global := dexpreopt.GetGlobalConfig(ctx)
1999*333d2b36SAndroid Build Coastguard Worker	if global.DisablePreopt || global.OnlyPreoptArtBootImage {
2000*333d2b36SAndroid Build Coastguard Worker		return inputFile
2001*333d2b36SAndroid Build Coastguard Worker	}
2002*333d2b36SAndroid Build Coastguard Worker
2003*333d2b36SAndroid Build Coastguard Worker	rule := android.NewRuleBuilder(pctx, ctx)
2004*333d2b36SAndroid Build Coastguard Worker	cmd := rule.Command().BuiltTool("manifest_check").
2005*333d2b36SAndroid Build Coastguard Worker		Flag("--enforce-uses-libraries").
2006*333d2b36SAndroid Build Coastguard Worker		Input(inputFile).
2007*333d2b36SAndroid Build Coastguard Worker		FlagWithOutput("--enforce-uses-libraries-status ", statusFile).
2008*333d2b36SAndroid Build Coastguard Worker		FlagWithInput("--aapt ", ctx.Config().HostToolPath(ctx, "aapt2"))
2009*333d2b36SAndroid Build Coastguard Worker
2010*333d2b36SAndroid Build Coastguard Worker	if outputFile != nil {
2011*333d2b36SAndroid Build Coastguard Worker		cmd.FlagWithOutput("-o ", outputFile)
2012*333d2b36SAndroid Build Coastguard Worker	}
2013*333d2b36SAndroid Build Coastguard Worker
2014*333d2b36SAndroid Build Coastguard Worker	if dexpreopt.GetGlobalConfig(ctx).RelaxUsesLibraryCheck {
2015*333d2b36SAndroid Build Coastguard Worker		cmd.Flag("--enforce-uses-libraries-relax")
2016*333d2b36SAndroid Build Coastguard Worker	}
2017*333d2b36SAndroid Build Coastguard Worker
2018*333d2b36SAndroid Build Coastguard Worker	requiredUsesLibs, optionalUsesLibs := classLoaderContexts.UsesLibs()
2019*333d2b36SAndroid Build Coastguard Worker	for _, lib := range requiredUsesLibs {
2020*333d2b36SAndroid Build Coastguard Worker		cmd.FlagWithArg("--uses-library ", lib)
2021*333d2b36SAndroid Build Coastguard Worker	}
2022*333d2b36SAndroid Build Coastguard Worker	for _, lib := range optionalUsesLibs {
2023*333d2b36SAndroid Build Coastguard Worker		cmd.FlagWithArg("--optional-uses-library ", lib)
2024*333d2b36SAndroid Build Coastguard Worker	}
2025*333d2b36SAndroid Build Coastguard Worker
2026*333d2b36SAndroid Build Coastguard Worker	// Also add missing optional uses libs, as the manifest check expects them.
2027*333d2b36SAndroid Build Coastguard Worker	// Note that what we add here are the module names of those missing libs, not library names, while
2028*333d2b36SAndroid Build Coastguard Worker	// the manifest check actually expects library names. However, the case where a library is missing
2029*333d2b36SAndroid Build Coastguard Worker	// and the module name != the library name is too rare for us to handle.
2030*333d2b36SAndroid Build Coastguard Worker	for _, lib := range u.usesLibraryProperties.Missing_optional_uses_libs {
2031*333d2b36SAndroid Build Coastguard Worker		cmd.FlagWithArg("--missing-optional-uses-library ", lib)
2032*333d2b36SAndroid Build Coastguard Worker	}
2033*333d2b36SAndroid Build Coastguard Worker
2034*333d2b36SAndroid Build Coastguard Worker	rule.Build("verify_uses_libraries", "verify <uses-library>")
2035*333d2b36SAndroid Build Coastguard Worker	return outputFile
2036*333d2b36SAndroid Build Coastguard Worker}
2037*333d2b36SAndroid Build Coastguard Worker
2038*333d2b36SAndroid Build Coastguard Worker// verifyUsesLibrariesManifest checks the <uses-library> tags in an AndroidManifest.xml against
2039*333d2b36SAndroid Build Coastguard Worker// the build system and returns the path to a copy of the manifest.
2040*333d2b36SAndroid Build Coastguard Workerfunc (u *usesLibrary) verifyUsesLibrariesManifest(ctx android.ModuleContext, manifest android.Path,
2041*333d2b36SAndroid Build Coastguard Worker	classLoaderContexts *dexpreopt.ClassLoaderContextMap) android.Path {
2042*333d2b36SAndroid Build Coastguard Worker	outputFile := android.PathForModuleOut(ctx, "manifest_check", "AndroidManifest.xml")
2043*333d2b36SAndroid Build Coastguard Worker	return u.verifyUsesLibraries(ctx, manifest, outputFile, classLoaderContexts)
2044*333d2b36SAndroid Build Coastguard Worker}
2045*333d2b36SAndroid Build Coastguard Worker
2046*333d2b36SAndroid Build Coastguard Worker// verifyUsesLibrariesAPK checks the <uses-library> tags in the manifest of an APK against the build
2047*333d2b36SAndroid Build Coastguard Worker// system and returns the path to a copy of the APK.
2048*333d2b36SAndroid Build Coastguard Workerfunc (u *usesLibrary) verifyUsesLibrariesAPK(ctx android.ModuleContext, apk android.Path,
2049*333d2b36SAndroid Build Coastguard Worker	classLoaderContexts *dexpreopt.ClassLoaderContextMap) {
2050*333d2b36SAndroid Build Coastguard Worker	u.verifyUsesLibraries(ctx, apk, nil, classLoaderContexts) // for APKs manifest_check does not write output file
2051*333d2b36SAndroid Build Coastguard Worker}
2052