blob: b849b98b16d8c40a13bb21a569415dbb8d27394d [file] [log] [blame]
Colin Cross30e076a2015-04-13 13:58:27 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
Jaewoong Jungf9b44652020-12-21 12:29:12 -080017// This file contains the module implementations for android_app, android_test, and some more
18// related module types, including their override variants.
Colin Cross30e076a2015-04-13 13:58:27 -070019
20import (
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070021 "path/filepath"
Jaewoong Jung5b425e22019-06-17 17:40:56 -070022 "sort"
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070023 "strings"
Colin Cross30e076a2015-04-13 13:58:27 -070024
Colin Cross50ddcc42019-05-16 12:28:22 -070025 "github.com/google/blueprint"
26 "github.com/google/blueprint/proptools"
27
Colin Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Colin Crossa4f08812018-10-02 22:03:40 -070029 "android/soong/cc"
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +010030 "android/soong/dexpreopt"
Colin Cross303e21f2018-08-07 16:49:25 -070031 "android/soong/tradefed"
Colin Cross30e076a2015-04-13 13:58:27 -070032)
33
Colin Cross3bc7ffa2017-11-22 16:19:37 -080034func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000035 RegisterAppBuildComponents(android.InitRegistrationContext)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080036}
37
Paul Duffinf9b1da02019-12-18 19:51:55 +000038func RegisterAppBuildComponents(ctx android.RegistrationContext) {
39 ctx.RegisterModuleType("android_app", AndroidAppFactory)
40 ctx.RegisterModuleType("android_test", AndroidTestFactory)
41 ctx.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
42 ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
43 ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
44 ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000045}
46
Colin Cross30e076a2015-04-13 13:58:27 -070047// AndroidManifest.xml merging
48// package splits
49
Colin Crossfabb6082018-02-20 17:22:23 -080050type appProperties struct {
Colin Crossbd01e2a2018-10-04 15:21:03 -070051 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
Colin Cross7d5136f2015-05-11 13:39:40 -070052 Additional_certificates []string
53
54 // If set, create package-export.apk, which other packages can
55 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
Nan Zhangea568a42017-11-08 21:20:04 -080056 Export_package_resources *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070057
Colin Cross16056062017-12-13 22:46:28 -080058 // Specifies that this app should be installed to the priv-app directory,
59 // where the system will grant it additional privileges not available to
60 // normal apps.
61 Privileged *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070062
63 // list of resource labels to generate individual resource packages
64 Package_splits []string
Jason Monkd4122be2018-08-10 09:33:36 -040065
66 // Names of modules to be overridden. Listed modules can only be other binaries
67 // (in Make or Soong).
68 // This does not completely prevent installation of the overridden binaries, but if both
69 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
70 // from PRODUCT_PACKAGES.
71 Overrides []string
Colin Crossa4f08812018-10-02 22:03:40 -070072
73 // list of native libraries that will be provided in or alongside the resulting jar
74 Jni_libs []string `android:"arch_variant"`
75
Colin Cross7204cf02020-05-06 17:51:39 -070076 // if true, use JNI libraries that link against platform APIs even if this module sets
Colin Crossee87c602020-02-19 16:57:15 -080077 // sdk_version.
78 Jni_uses_platform_apis *bool
79
Colin Cross7204cf02020-05-06 17:51:39 -070080 // if true, use JNI libraries that link against SDK APIs even if this module does not set
81 // sdk_version.
82 Jni_uses_sdk_apis *bool
83
Jaewoong Jungbc625cd2019-05-06 15:48:44 -070084 // STL library to use for JNI libraries.
85 Stl *string `android:"arch_variant"`
86
Colin Crosse4246ab2019-02-05 21:55:21 -080087 // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest
88 // flag so that they are used from inside the APK at runtime. Defaults to true for android_test modules unless
Jiyong Park52cd06f2019-11-11 10:14:32 +090089 // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to true for
90 // android_app modules that are embedded to APEXes, defaults to false for other module types where the native
91 // libraries are generally preinstalled outside the APK.
Colin Crosse4246ab2019-02-05 21:55:21 -080092 Use_embedded_native_libs *bool
Colin Cross46abdad2019-02-07 13:07:08 -080093
94 // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that
95 // they are used from inside the APK at runtime.
96 Use_embedded_dex *bool
Colin Cross47fa9d32019-03-26 10:51:39 -070097
98 // Forces native libraries to always be packaged into the APK,
99 // Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed.
100 // True for android_test* modules.
101 AlwaysPackageNativeLibs bool `blueprint:"mutated"`
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700102
103 // If set, find and merge all NOTICE files that this module and its dependencies have and store
104 // it in the APK as an asset.
105 Embed_notices *bool
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700106
107 // cc.Coverage related properties
108 PreventInstall bool `blueprint:"mutated"`
109 HideFromMake bool `blueprint:"mutated"`
110 IsCoverageVariant bool `blueprint:"mutated"`
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100111
112 // Whether this app is considered mainline updatable or not. When set to true, this will enforce
Artur Satayevf40fc852020-04-16 13:43:02 +0100113 // additional rules to make sure an app can safely be updated. Default is false.
114 // Prefer using other specific properties if build behaviour must be changed; avoid using this
115 // flag for anything but neverallow rules (unless the behaviour change is invisible to owners).
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100116 Updatable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700117}
118
Jaewoong Jung525443a2019-02-28 15:35:54 -0800119// android_app properties that can be overridden by override_android_app
120type overridableAppProperties struct {
121 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
122 // or an android_app_certificate module name in the form ":module".
123 Certificate *string
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700124
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800125 // Name of the signing certificate lineage file or filegroup module.
126 Lineage *string `android:"path"`
Liz Kammere2b27f42020-05-07 13:24:05 -0700127
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700128 // the package name of this app. The package name in the manifest file is used if one was not given.
129 Package_name *string
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800130
131 // the logging parent of this app.
132 Logging_parent *string
Liz Kammer9f9fd022020-06-18 19:44:06 +0000133
134 // Whether to rename the package in resources to the override name rather than the base name. Defaults to true.
135 Rename_resources_package *bool
Jaewoong Jung525443a2019-02-28 15:35:54 -0800136}
137
Colin Cross30e076a2015-04-13 13:58:27 -0700138type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -0700139 Library
140 aapt
Jaewoong Jung525443a2019-02-28 15:35:54 -0800141 android.OverridableModuleBase
Colin Crossa97c5d32018-03-28 14:58:31 -0700142
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900143 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -0700144
Colin Crossfabb6082018-02-20 17:22:23 -0800145 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -0700146
Jaewoong Jung525443a2019-02-28 15:35:54 -0800147 overridableAppProperties overridableAppProperties
148
Colin Cross403cc152020-07-06 14:15:24 -0700149 jniLibs []jniLib
150 installPathForJNISymbols android.Path
151 embeddedJniLibs bool
152 jniCoverageOutputs android.Paths
Colin Crossf6237212018-10-29 23:14:58 -0700153
154 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800155
156 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
157 installApkName string
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800158
Colin Cross70dda7e2019-10-01 22:05:35 -0700159 installDir android.InstallPath
Jaewoong Jung0949f312019-09-11 10:25:18 -0700160
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700161 onDeviceDir string
162
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800163 additionalAaptFlags []string
Jaewoong Jung98772792019-07-01 17:15:13 -0700164
165 noticeOutputs android.NoticeOutputs
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900166
167 overriddenManifestPackageName string
Artur Satayev1111b842020-04-27 19:05:28 +0100168
169 android.ApexBundleDepsInfo
Colin Crosse1731a52017-12-14 11:22:55 -0800170}
171
Martin Stjernholm6d415272020-01-31 17:10:36 +0000172func (a *AndroidApp) IsInstallable() bool {
173 return Bool(a.properties.Installable)
174}
175
Colin Cross89c31582018-04-30 15:55:11 -0700176func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
177 return nil
178}
179
Colin Cross66f78822018-05-02 12:58:28 -0700180func (a *AndroidApp) ExportedStaticPackages() android.Paths {
181 return nil
182}
183
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900184func (a *AndroidApp) OutputFile() android.Path {
185 return a.outputFile
186}
187
Colin Cross503c1d02020-01-28 14:00:53 -0800188func (a *AndroidApp) Certificate() Certificate {
189 return a.certificate
190}
191
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700192func (a *AndroidApp) JniCoverageOutputs() android.Paths {
193 return a.jniCoverageOutputs
194}
195
Colin Crossa97c5d32018-03-28 14:58:31 -0700196var _ AndroidLibraryDependency = (*AndroidApp)(nil)
197
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900198type Certificate struct {
Colin Cross503c1d02020-01-28 14:00:53 -0800199 Pem, Key android.Path
200 presigned bool
201}
202
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700203var PresignedCertificate = Certificate{presigned: true}
Colin Cross503c1d02020-01-28 14:00:53 -0800204
205func (c Certificate) AndroidMkString() string {
206 if c.presigned {
207 return "PRESIGNED"
208 } else {
209 return c.Pem.String()
210 }
Colin Cross30e076a2015-04-13 13:58:27 -0700211}
212
Colin Cross46c9b8b2017-06-22 16:51:17 -0700213func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
214 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700215
Jiyong Park6a927c42020-01-21 02:03:43 +0900216 if String(a.appProperties.Stl) == "c++_shared" && !a.sdkVersion().specified() {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700217 ctx.PropertyErrorf("stl", "sdk_version must be set in order to use c++_shared")
218 }
219
Paul Duffin250e6192019-06-07 10:44:37 +0100220 sdkDep := decodeSdkDep(ctx, sdkContext(a))
221 if sdkDep.hasFrameworkLibs() {
222 a.aapt.deps(ctx, sdkDep)
Colin Cross30e076a2015-04-13 13:58:27 -0700223 }
Colin Crossa4f08812018-10-02 22:03:40 -0700224
Colin Cross3c007702020-05-08 11:20:24 -0700225 usesSDK := a.sdkVersion().specified() && a.sdkVersion().kind != sdkCorePlatform
226
227 if usesSDK && Bool(a.appProperties.Jni_uses_sdk_apis) {
228 ctx.PropertyErrorf("jni_uses_sdk_apis",
229 "can only be set for modules that do not set sdk_version")
230 } else if !usesSDK && Bool(a.appProperties.Jni_uses_platform_apis) {
231 ctx.PropertyErrorf("jni_uses_platform_apis",
232 "can only be set for modules that set sdk_version")
233 }
234
Colin Crossa4f08812018-10-02 22:03:40 -0700235 for _, jniTarget := range ctx.MultiTargets() {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700236 variation := append(jniTarget.Variations(),
237 blueprint.Variation{Mutator: "link", Variation: "shared"})
Colin Crossc511bc52020-04-07 16:50:32 +0000238
239 // If the app builds against an Android SDK use the SDK variant of JNI dependencies
240 // unless jni_uses_platform_apis is set.
Colin Crossc2d24052020-05-13 11:05:02 -0700241 // Don't require the SDK variant for apps that are shipped on vendor, etc., as they already
242 // have stable APIs through the VNDK.
243 if (usesSDK && !a.RequiresStableAPIs(ctx) &&
244 !Bool(a.appProperties.Jni_uses_platform_apis)) ||
Colin Cross7204cf02020-05-06 17:51:39 -0700245 Bool(a.appProperties.Jni_uses_sdk_apis) {
Colin Crossc511bc52020-04-07 16:50:32 +0000246 variation = append(variation, blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
247 }
Colin Crossde78d132020-10-09 18:59:49 -0700248 ctx.AddFarVariationDependencies(variation, jniLibTag, a.appProperties.Jni_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700249 }
Colin Cross50ddcc42019-05-16 12:28:22 -0700250
Paul Duffin250e6192019-06-07 10:44:37 +0100251 a.usesLibrary.deps(ctx, sdkDep.hasFrameworkLibs())
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700252}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700253
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700254func (a *AndroidApp) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800255 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700256 if cert != "" {
257 ctx.AddDependency(ctx.Module(), certificateTag, cert)
258 }
259
260 for _, cert := range a.appProperties.Additional_certificates {
261 cert = android.SrcIsModule(cert)
262 if cert != "" {
263 ctx.AddDependency(ctx.Module(), certificateTag, cert)
264 } else {
265 ctx.PropertyErrorf("additional_certificates",
266 `must be names of android_app_certificate modules in the form ":module"`)
267 }
268 }
Colin Cross30e076a2015-04-13 13:58:27 -0700269}
270
Jeongik Cha538c0d02019-07-11 15:54:27 +0900271func (a *AndroidTestHelperApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
272 a.generateAndroidBuildActions(ctx)
273}
274
Colin Cross46c9b8b2017-06-22 16:51:17 -0700275func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100276 a.checkAppSdkVersions(ctx)
Colin Crossae5caf52018-05-22 11:11:52 -0700277 a.generateAndroidBuildActions(ctx)
278}
279
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100280func (a *AndroidApp) checkAppSdkVersions(ctx android.ModuleContext) {
Artur Satayev849f8442020-04-28 14:57:42 +0100281 if a.Updatable() {
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100282 if !a.sdkVersion().stable() {
283 ctx.PropertyErrorf("sdk_version", "Updatable apps must use stable SDKs, found %v", a.sdkVersion())
284 }
Artur Satayevf40fc852020-04-16 13:43:02 +0100285 if String(a.deviceProperties.Min_sdk_version) == "" {
286 ctx.PropertyErrorf("updatable", "updatable apps must set min_sdk_version.")
287 }
Jooyung Han749dc692020-04-15 11:03:39 +0900288
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900289 if minSdkVersion, err := a.minSdkVersion().effectiveVersion(ctx); err == nil {
290 a.checkJniLibsSdkVersion(ctx, minSdkVersion)
Dan Albertc8060532020-07-22 22:32:17 -0700291 android.CheckMinSdkVersion(a, ctx, minSdkVersion.ApiLevel(ctx))
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900292 } else {
293 ctx.PropertyErrorf("min_sdk_version", "%s", err.Error())
294 }
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100295 }
296
297 a.checkPlatformAPI(ctx)
298 a.checkSdkVersions(ctx)
299}
300
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900301// If an updatable APK sets min_sdk_version, min_sdk_vesion of JNI libs should match with it.
302// This check is enforced for "updatable" APKs (including APK-in-APEX).
303// b/155209650: until min_sdk_version is properly supported, use sdk_version instead.
304// because, sdk_version is overridden by min_sdk_version (if set as smaller)
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -0800305// and sdkLinkType is checked with dependencies so we can be sure that the whole dependency tree
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900306// will meet the requirements.
307func (a *AndroidApp) checkJniLibsSdkVersion(ctx android.ModuleContext, minSdkVersion sdkVersion) {
308 // It's enough to check direct JNI deps' sdk_version because all transitive deps from JNI deps are checked in cc.checkLinkType()
309 ctx.VisitDirectDeps(func(m android.Module) {
310 if !IsJniDepTag(ctx.OtherModuleDependencyTag(m)) {
311 return
312 }
313 dep, _ := m.(*cc.Module)
Jooyung Han652d5b32020-05-20 17:12:13 +0900314 // The domain of cc.sdk_version is "current" and <number>
315 // We can rely on sdkSpec to convert it to <number> so that "current" is handled
316 // properly regardless of sdk finalization.
317 jniSdkVersion, err := sdkSpecFrom(dep.SdkVersion()).effectiveVersion(ctx)
318 if err != nil || minSdkVersion < jniSdkVersion {
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900319 ctx.OtherModuleErrorf(dep, "sdk_version(%v) is higher than min_sdk_version(%v) of the containing android_app(%v)",
320 dep.SdkVersion(), minSdkVersion, ctx.ModuleName())
321 return
322 }
323
324 })
325}
326
Sasha Smundak6ad77252019-05-01 13:16:22 -0700327// Returns true if the native libraries should be stored in the APK uncompressed and the
Colin Crosse4246ab2019-02-05 21:55:21 -0800328// extractNativeLibs application flag should be set to false in the manifest.
Sasha Smundak6ad77252019-05-01 13:16:22 -0700329func (a *AndroidApp) useEmbeddedNativeLibs(ctx android.ModuleContext) bool {
Jiyong Park6a927c42020-01-21 02:03:43 +0900330 minSdkVersion, err := a.minSdkVersion().effectiveVersion(ctx)
Colin Crosse4246ab2019-02-05 21:55:21 -0800331 if err != nil {
332 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
333 }
334
Colin Cross56a83212020-09-15 18:30:11 -0700335 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Jiyong Park52cd06f2019-11-11 10:14:32 +0900336 return (minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)) ||
Colin Cross56a83212020-09-15 18:30:11 -0700337 !apexInfo.IsForPlatform()
Colin Crosse4246ab2019-02-05 21:55:21 -0800338}
339
Colin Cross43f08db2018-11-12 10:13:39 -0800340// Returns whether this module should have the dex file stored uncompressed in the APK.
341func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800342 if Bool(a.appProperties.Use_embedded_dex) {
343 return true
344 }
345
Colin Cross53a87f52019-06-25 13:35:30 -0700346 // Uncompress dex in APKs of privileged apps (even for unbundled builds, they may
347 // be preinstalled as prebuilts).
Jiyong Parkf7487312019-10-17 12:54:30 +0900348 if ctx.Config().UncompressPrivAppDex() && a.Privileged() {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000349 return true
350 }
351
Colin Cross53a87f52019-06-25 13:35:30 -0700352 if ctx.Config().UnbundledBuild() {
353 return false
354 }
355
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700356 return shouldUncompressDex(ctx, &a.dexpreopter)
Colin Cross5a0dcd52018-10-05 14:20:06 -0700357}
358
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700359func (a *AndroidApp) shouldEmbedJnis(ctx android.BaseModuleContext) bool {
Colin Cross56a83212020-09-15 18:30:11 -0700360 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700361 return ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) ||
Colin Cross56a83212020-09-15 18:30:11 -0700362 !apexInfo.IsForPlatform() || a.appProperties.AlwaysPackageNativeLibs
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700363}
364
Liz Kammer9f9fd022020-06-18 19:44:06 +0000365func generateAaptRenamePackageFlags(packageName string, renameResourcesPackage bool) []string {
366 aaptFlags := []string{"--rename-manifest-package " + packageName}
367 if renameResourcesPackage {
368 // Required to rename the package name in the resources table.
369 aaptFlags = append(aaptFlags, "--rename-resources-package "+packageName)
370 }
371 return aaptFlags
372}
373
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900374func (a *AndroidApp) OverriddenManifestPackageName() string {
375 return a.overriddenManifestPackageName
376}
377
Liz Kammer9f9fd022020-06-18 19:44:06 +0000378func (a *AndroidApp) renameResourcesPackage() bool {
379 return proptools.BoolDefault(a.overridableAppProperties.Rename_resources_package, true)
380}
381
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800382func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
David Brazdild25060a2019-02-18 18:24:16 +0000383 a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis)
384
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700385 // Ask manifest_fixer to add or update the application element indicating this app has no code.
386 a.aapt.hasNoCode = !a.hasCode(ctx)
387
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800388 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800389
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800390 // Add TARGET_AAPT_CHARACTERISTICS values to AAPT link flags if they exist and --product flags were not provided.
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800391 hasProduct := android.PrefixInList(a.aaptProperties.Aaptflags, "--product")
Colin Crosse78dcd32018-04-19 15:25:19 -0700392 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800393 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700394 }
395
Dan Willemsen72be5902018-10-24 20:24:57 -0700396 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
397 // Product AAPT config
398 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800399 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700400 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700401
Dan Willemsen72be5902018-10-24 20:24:57 -0700402 // Product AAPT preferred config
403 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800404 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700405 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700406 }
407
Jiyong Park7f67f482019-01-05 12:57:48 +0900408 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700409 if overridden || a.overridableAppProperties.Package_name != nil {
410 // The product override variable has a priority over the package_name property.
411 if !overridden {
412 manifestPackageName = *a.overridableAppProperties.Package_name
413 }
Liz Kammer9f9fd022020-06-18 19:44:06 +0000414 aaptLinkFlags = append(aaptLinkFlags, generateAaptRenamePackageFlags(manifestPackageName, a.renameResourcesPackage())...)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900415 a.overriddenManifestPackageName = manifestPackageName
Jiyong Park7f67f482019-01-05 12:57:48 +0900416 }
417
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800418 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
419
Colin Crosse560c4a2019-03-19 16:03:11 -0700420 a.aapt.splitNames = a.appProperties.Package_splits
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800421 a.aapt.LoggingParent = String(a.overridableAppProperties.Logging_parent)
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100422 a.aapt.buildActions(ctx, sdkContext(a), a.classLoaderContexts, aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700423
Colin Cross46c9b8b2017-06-22 16:51:17 -0700424 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700425 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800426}
Colin Cross30e076a2015-04-13 13:58:27 -0700427
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800428func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700429 var staticLibProguardFlagFiles android.Paths
430 ctx.VisitDirectDeps(func(m android.Module) {
431 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
432 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
433 }
434 })
435
436 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
437
438 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
439 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800440}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800441
Colin Cross403cc152020-07-06 14:15:24 -0700442func (a *AndroidApp) installPath(ctx android.ModuleContext) android.InstallPath {
Colin Cross43f08db2018-11-12 10:13:39 -0800443 var installDir string
444 if ctx.ModuleName() == "framework-res" {
445 // framework-res.apk is installed as system/framework/framework-res.apk
446 installDir = "framework"
Jiyong Parkf7487312019-10-17 12:54:30 +0900447 } else if a.Privileged() {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800448 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800449 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800450 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800451 }
Colin Cross403cc152020-07-06 14:15:24 -0700452
453 return android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
454}
455
Ulya Trafimovich18554242020-11-03 15:55:11 +0000456func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross403cc152020-07-06 14:15:24 -0700457 a.dexpreopter.installPath = a.installPath(ctx)
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000458 a.dexpreopter.isApp = true
Liz Kammera7a64f32020-07-09 15:16:41 -0700459 if a.dexProperties.Uncompress_dex == nil {
David Srbeckye033cba2020-05-20 22:20:28 +0100460 // If the value was not force-set by the user, use reasonable default based on the module.
Liz Kammera7a64f32020-07-09 15:16:41 -0700461 a.dexProperties.Uncompress_dex = proptools.BoolPtr(a.shouldUncompressDex(ctx))
David Srbeckye033cba2020-05-20 22:20:28 +0100462 }
Liz Kammera7a64f32020-07-09 15:16:41 -0700463 a.dexpreopter.uncompressedDex = *a.dexProperties.Uncompress_dex
Colin Cross50ddcc42019-05-16 12:28:22 -0700464 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100465 a.dexpreopter.classLoaderContexts = a.classLoaderContexts
Colin Cross50ddcc42019-05-16 12:28:22 -0700466 a.dexpreopter.manifestFile = a.mergedManifestFile
467
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800468 if ctx.ModuleName() != "framework-res" {
469 a.Module.compile(ctx, a.aaptSrcJar)
470 }
Colin Cross30e076a2015-04-13 13:58:27 -0700471
Colin Crossb014f072021-02-26 14:54:36 -0800472 return a.dexJarFile
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800473}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800474
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800475func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700476 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700477 if len(jniLibs) > 0 {
Colin Cross403cc152020-07-06 14:15:24 -0700478 a.jniLibs = jniLibs
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700479 if a.shouldEmbedJnis(ctx) {
Colin Crossa4f08812018-10-02 22:03:40 -0700480 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Colin Cross403cc152020-07-06 14:15:24 -0700481 a.installPathForJNISymbols = a.installPath(ctx).ToMakePath()
Sasha Smundak6ad77252019-05-01 13:16:22 -0700482 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.useEmbeddedNativeLibs(ctx))
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700483 for _, jni := range jniLibs {
484 if jni.coverageFile.Valid() {
Jaewoong Jung46984ee2020-04-07 13:07:55 -0700485 // Only collect coverage for the first target arch if this is a multilib target.
486 // TODO(jungjw): Ideally, we want to collect both reports, but that would cause coverage
487 // data file path collisions since the current coverage file path format doesn't contain
488 // arch-related strings. This is fine for now though; the code coverage team doesn't use
489 // multi-arch targets such as test_suite_* for coverage collections yet.
490 //
491 // Work with the team to come up with a new format that handles multilib modules properly
492 // and change this.
493 if len(ctx.Config().Targets[android.Android]) == 1 ||
Jaewoong Jung642916f2020-10-09 17:25:15 -0700494 ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType == jni.target.Arch.ArchType {
Jaewoong Jung46984ee2020-04-07 13:07:55 -0700495 a.jniCoverageOutputs = append(a.jniCoverageOutputs, jni.coverageFile.Path())
496 }
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700497 }
498 }
Colin Cross403cc152020-07-06 14:15:24 -0700499 a.embeddedJniLibs = true
Colin Crossa4f08812018-10-02 22:03:40 -0700500 }
501 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800502 return jniJarFile
503}
Colin Crossa4f08812018-10-02 22:03:40 -0700504
Colin Cross403cc152020-07-06 14:15:24 -0700505func (a *AndroidApp) JNISymbolsInstalls(installPath string) android.RuleBuilderInstalls {
506 var jniSymbols android.RuleBuilderInstalls
507 for _, jniLib := range a.jniLibs {
508 if jniLib.unstrippedFile != nil {
509 jniSymbols = append(jniSymbols, android.RuleBuilderInstall{
510 From: jniLib.unstrippedFile,
511 To: filepath.Join(installPath, targetToJniDir(jniLib.target), jniLib.unstrippedFile.Base()),
512 })
513 }
514 }
515 return jniSymbols
516}
517
Jaewoong Jung0949f312019-09-11 10:25:18 -0700518func (a *AndroidApp) noticeBuildActions(ctx android.ModuleContext) {
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700519 // Collect NOTICE files from all dependencies.
520 seenModules := make(map[android.Module]bool)
521 noticePathSet := make(map[android.Path]bool)
522
523 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
524 // Have we already seen this?
525 if _, ok := seenModules[child]; ok {
526 return false
527 }
528 seenModules[child] = true
529
530 // Skip host modules.
Jiyong Park1613e552020-09-14 19:43:17 +0900531 if child.Target().Os.Class == android.Host {
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700532 return false
533 }
534
Bob Badoura75b0572020-02-18 20:21:55 -0800535 paths := child.(android.Module).NoticeFiles()
536 if len(paths) > 0 {
537 for _, path := range paths {
538 noticePathSet[path] = true
539 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700540 }
541 return true
542 })
543
544 // If the app has one, add it too.
Bob Badoura75b0572020-02-18 20:21:55 -0800545 if len(a.NoticeFiles()) > 0 {
546 for _, path := range a.NoticeFiles() {
547 noticePathSet[path] = true
548 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700549 }
550
551 if len(noticePathSet) == 0 {
Jaewoong Jung98772792019-07-01 17:15:13 -0700552 return
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700553 }
554 var noticePaths []android.Path
555 for path := range noticePathSet {
556 noticePaths = append(noticePaths, path)
557 }
558 sort.Slice(noticePaths, func(i, j int) bool {
559 return noticePaths[i].String() < noticePaths[j].String()
560 })
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700561
Jaewoong Jung0949f312019-09-11 10:25:18 -0700562 a.noticeOutputs = android.BuildNoticeOutput(ctx, a.installDir, a.installApkName+".apk", noticePaths)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700563}
564
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700565// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
566// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
567func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate {
568 if android.SrcIsModule(certPropValue) == "" {
569 var mainCert Certificate
570 if certPropValue != "" {
571 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
572 mainCert = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -0800573 Pem: defaultDir.Join(ctx, certPropValue+".x509.pem"),
574 Key: defaultDir.Join(ctx, certPropValue+".pk8"),
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700575 }
576 } else {
577 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Colin Cross503c1d02020-01-28 14:00:53 -0800578 mainCert = Certificate{
579 Pem: pem,
580 Key: key,
581 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700582 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700583 certificates = append([]Certificate{mainCert}, certificates...)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700584 }
585
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700586 if !m.Platform() {
587 certPath := certificates[0].Pem.String()
Jeongik Chac9464142019-01-07 12:07:27 +0900588 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
589 if strings.HasPrefix(certPath, systemCertPath) {
590 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
Colin Cross440e0d02020-06-11 11:32:11 -0700591 allowed := ctx.Config().EnforceSystemCertificateAllowList()
Jeongik Chac9464142019-01-07 12:07:27 +0900592
Colin Cross440e0d02020-06-11 11:32:11 -0700593 if enforceSystemCert && !inList(m.Name(), allowed) {
Jeongik Chac9464142019-01-07 12:07:27 +0900594 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
595 }
596 }
597 }
598
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700599 return certificates
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800600}
601
Jooyung Han39ee1192020-03-23 20:21:11 +0900602func (a *AndroidApp) InstallApkName() string {
603 return a.installApkName
604}
605
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800606func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross50ddcc42019-05-16 12:28:22 -0700607 var apkDeps android.Paths
608
Colin Cross56a83212020-09-15 18:30:11 -0700609 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
610 a.hideApexVariantFromMake = true
611 }
612
Jeongik Cha538c0d02019-07-11 15:54:27 +0900613 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
614 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
615
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800616 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800617 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800618
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700619 if ctx.ModuleName() == "framework-res" {
620 // framework-res.apk is installed as system/framework/framework-res.apk
Jaewoong Jung0949f312019-09-11 10:25:18 -0700621 a.installDir = android.PathForModuleInstall(ctx, "framework")
Jiyong Parkf7487312019-10-17 12:54:30 +0900622 } else if a.Privileged() {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700623 a.installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
624 } else if ctx.InstallInTestcases() {
Jaewoong Jung326a9412019-11-21 10:41:00 -0800625 a.installDir = android.PathForModuleInstall(ctx, a.installApkName, ctx.DeviceConfig().DeviceArch())
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700626 } else {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700627 a.installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700628 }
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700629 a.onDeviceDir = android.InstallPathToOnDevicePath(ctx, a.installDir)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700630
Jaewoong Jung0949f312019-09-11 10:25:18 -0700631 a.noticeBuildActions(ctx)
Jaewoong Jung98772792019-07-01 17:15:13 -0700632 if Bool(a.appProperties.Embed_notices) || ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") {
633 a.aapt.noticeFile = a.noticeOutputs.HtmlGzOutput
634 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700635
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100636 a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000637
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800638 // Process all building blocks, from AAPT to certificates.
639 a.aaptBuildActions(ctx)
640
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100641 // The decision to enforce <uses-library> checks is made before adding implicit SDK libraries.
642 a.usesLibrary.freezeEnforceUsesLibraries()
643
644 // Add implicit SDK libraries to <uses-library> list.
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100645 for _, usesLib := range a.classLoaderContexts.UsesLibs() {
Ulya Trafimovich663dc532020-09-10 12:48:53 +0100646 a.usesLibrary.addLib(usesLib, inList(usesLib, dexpreopt.OptionalCompatUsesLibs))
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100647 }
648
649 // Check that the <uses-library> list is coherent with the manifest.
Colin Cross50ddcc42019-05-16 12:28:22 -0700650 if a.usesLibrary.enforceUsesLibraries() {
651 manifestCheckFile := a.usesLibrary.verifyUsesLibrariesManifest(ctx, a.mergedManifestFile)
652 apkDeps = append(apkDeps, manifestCheckFile)
653 }
654
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800655 a.proguardBuildActions(ctx)
656
Colin Cross014489c2020-06-02 20:09:13 -0700657 a.linter.mergedManifest = a.aapt.mergedManifestFile
658 a.linter.manifest = a.aapt.manifestPath
659 a.linter.resources = a.aapt.resourceFiles
Colin Crossc0efd1d2020-07-03 11:56:24 -0700660 a.linter.buildModuleReportZip = ctx.Config().UnbundledBuildApps()
Colin Cross014489c2020-06-02 20:09:13 -0700661
Ulya Trafimovich18554242020-11-03 15:55:11 +0000662 dexJarFile := a.dexBuildActions(ctx)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800663
Colin Crossc2d24052020-05-13 11:05:02 -0700664 jniLibs, certificateDeps := collectAppDeps(ctx, a, a.shouldEmbedJnis(ctx), !Bool(a.appProperties.Jni_uses_platform_apis))
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800665 jniJarFile := a.jniBuildActions(jniLibs, ctx)
666
667 if ctx.Failed() {
668 return
669 }
670
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700671 certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx)
672 a.certificate = certificates[0]
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800673
674 // Build a final signed app package.
Jaewoong Jung5a498812019-11-07 14:14:38 -0800675 packageFile := android.PathForModuleOut(ctx, a.installApkName+".apk")
Songchun Fan17d69e32020-03-24 20:32:24 -0700676 v4SigningRequested := Bool(a.Module.deviceProperties.V4_signature)
677 var v4SignatureFile android.WritablePath = nil
678 if v4SigningRequested {
679 v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+".apk.idsig")
680 }
Liz Kammere2b27f42020-05-07 13:24:05 -0700681 var lineageFile android.Path
682 if lineage := String(a.overridableAppProperties.Lineage); lineage != "" {
683 lineageFile = android.PathForModuleSrc(ctx, lineage)
684 }
Liz Kammer70dd74d2020-05-07 13:24:05 -0700685 CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates, apkDeps, v4SignatureFile, lineageFile)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800686 a.outputFile = packageFile
Songchun Fan17d69e32020-03-24 20:32:24 -0700687 if v4SigningRequested {
688 a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
689 }
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800690
Colin Crosse560c4a2019-03-19 16:03:11 -0700691 for _, split := range a.aapt.splits {
692 // Sign the split APKs
Jaewoong Jung5a498812019-11-07 14:14:38 -0800693 packageFile := android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk")
Songchun Fan17d69e32020-03-24 20:32:24 -0700694 if v4SigningRequested {
695 v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk.idsig")
696 }
Liz Kammer70dd74d2020-05-07 13:24:05 -0700697 CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates, apkDeps, v4SignatureFile, lineageFile)
Colin Crosse560c4a2019-03-19 16:03:11 -0700698 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
Songchun Fan17d69e32020-03-24 20:32:24 -0700699 if v4SigningRequested {
700 a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
701 }
Colin Crosse560c4a2019-03-19 16:03:11 -0700702 }
703
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800704 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700705 bundleFile := android.PathForModuleOut(ctx, "base.zip")
706 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
707 a.bundleFile = bundleFile
708
Colin Cross56a83212020-09-15 18:30:11 -0700709 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
710
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800711 // Install the app package.
Colin Cross56a83212020-09-15 18:30:11 -0700712 if (Bool(a.Module.properties.Installable) || ctx.Host()) && apexInfo.IsForPlatform() {
Jiyong Park8ba50f92019-11-13 15:01:01 +0900713 ctx.InstallFile(a.installDir, a.outputFile.Base(), a.outputFile)
714 for _, extra := range a.extraOutputFiles {
715 ctx.InstallFile(a.installDir, extra.Base(), extra)
716 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800717 }
Artur Satayev1111b842020-04-27 19:05:28 +0100718
719 a.buildAppDependencyInfo(ctx)
Colin Cross30e076a2015-04-13 13:58:27 -0700720}
721
Colin Crossc2d24052020-05-13 11:05:02 -0700722type appDepsInterface interface {
723 sdkVersion() sdkSpec
724 minSdkVersion() sdkSpec
725 RequiresStableAPIs(ctx android.BaseModuleContext) bool
726}
727
728func collectAppDeps(ctx android.ModuleContext, app appDepsInterface,
729 shouldCollectRecursiveNativeDeps bool,
Colin Cross094cde42020-02-15 10:38:00 -0800730 checkNativeSdkVersion bool) ([]jniLib, []Certificate) {
Colin Crossc2d24052020-05-13 11:05:02 -0700731
Colin Crossa4f08812018-10-02 22:03:40 -0700732 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900733 var certificates []Certificate
Peter Collingbournead84f972019-12-17 16:46:18 -0800734 seenModulePaths := make(map[string]bool)
Colin Crossa4f08812018-10-02 22:03:40 -0700735
Colin Crossc2d24052020-05-13 11:05:02 -0700736 if checkNativeSdkVersion {
737 checkNativeSdkVersion = app.sdkVersion().specified() &&
738 app.sdkVersion().kind != sdkCorePlatform && !app.RequiresStableAPIs(ctx)
739 }
740
Peter Collingbournead84f972019-12-17 16:46:18 -0800741 ctx.WalkDeps(func(module android.Module, parent android.Module) bool {
Colin Crossa4f08812018-10-02 22:03:40 -0700742 otherName := ctx.OtherModuleName(module)
743 tag := ctx.OtherModuleDependencyTag(module)
744
Colin Crossf0913fb2020-07-29 12:59:39 -0700745 if IsJniDepTag(tag) || cc.IsSharedDepTag(tag) {
Colin Crossa4f08812018-10-02 22:03:40 -0700746 if dep, ok := module.(*cc.Module); ok {
Colin Cross95f1ca02020-10-29 20:47:22 -0700747 if dep.IsNdk(ctx.Config()) || dep.IsStubs() {
Peter Collingbournead84f972019-12-17 16:46:18 -0800748 return false
749 }
750
Colin Crossa4f08812018-10-02 22:03:40 -0700751 lib := dep.OutputFile()
Peter Collingbournead84f972019-12-17 16:46:18 -0800752 path := lib.Path()
753 if seenModulePaths[path.String()] {
754 return false
755 }
756 seenModulePaths[path.String()] = true
757
Colin Crossc2d24052020-05-13 11:05:02 -0700758 if checkNativeSdkVersion && dep.SdkVersion() == "" {
759 ctx.PropertyErrorf("jni_libs", "JNI dependency %q uses platform APIs, but this module does not",
760 otherName)
Colin Cross094cde42020-02-15 10:38:00 -0800761 }
762
Colin Crossa4f08812018-10-02 22:03:40 -0700763 if lib.Valid() {
764 jniLibs = append(jniLibs, jniLib{
Colin Cross403cc152020-07-06 14:15:24 -0700765 name: ctx.OtherModuleName(module),
766 path: path,
767 target: module.Target(),
768 coverageFile: dep.CoverageOutputFile(),
769 unstrippedFile: dep.UnstrippedOutputFile(),
Colin Crossa4f08812018-10-02 22:03:40 -0700770 })
771 } else {
772 ctx.ModuleErrorf("dependency %q missing output file", otherName)
773 }
774 } else {
775 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
Colin Crossa4f08812018-10-02 22:03:40 -0700776 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800777
778 return shouldCollectRecursiveNativeDeps
779 }
780
781 if tag == certificateTag {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700782 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900783 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700784 } else {
785 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
786 }
Colin Crossa4f08812018-10-02 22:03:40 -0700787 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800788
789 return false
Colin Crossa4f08812018-10-02 22:03:40 -0700790 })
791
Colin Crossbd01e2a2018-10-04 15:21:03 -0700792 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700793}
794
Jooyung Han749dc692020-04-15 11:03:39 +0900795func (a *AndroidApp) WalkPayloadDeps(ctx android.ModuleContext, do android.PayloadDepsCallback) {
Artur Satayev1111b842020-04-27 19:05:28 +0100796 ctx.WalkDeps(func(child, parent android.Module) bool {
797 isExternal := !a.DepIsInSameApex(ctx, child)
798 if am, ok := child.(android.ApexModule); ok {
Jooyung Han749dc692020-04-15 11:03:39 +0900799 if !do(ctx, parent, am, isExternal) {
800 return false
801 }
Artur Satayev1111b842020-04-27 19:05:28 +0100802 }
803 return !isExternal
804 })
805}
806
807func (a *AndroidApp) buildAppDependencyInfo(ctx android.ModuleContext) {
808 if ctx.Host() {
809 return
810 }
811
812 depsInfo := android.DepNameToDepInfoMap{}
Jooyung Han749dc692020-04-15 11:03:39 +0900813 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
Artur Satayev1111b842020-04-27 19:05:28 +0100814 depName := to.Name()
Artur Satayev533b98c2021-03-11 18:03:42 +0000815
816 // Skip dependencies that are only available to APEXes; they are developed with updatability
817 // in mind and don't need manual approval.
818 if to.(android.ApexModule).NotAvailableForPlatform() {
819 return true
820 }
821
Artur Satayev1111b842020-04-27 19:05:28 +0100822 if info, exist := depsInfo[depName]; exist {
823 info.From = append(info.From, from.Name())
824 info.IsExternal = info.IsExternal && externalDep
825 depsInfo[depName] = info
826 } else {
827 toMinSdkVersion := "(no version)"
828 if m, ok := to.(interface{ MinSdkVersion() string }); ok {
829 if v := m.MinSdkVersion(); v != "" {
830 toMinSdkVersion = v
831 }
832 }
833 depsInfo[depName] = android.ApexModuleDepInfo{
834 To: depName,
835 From: []string{from.Name()},
836 IsExternal: externalDep,
837 MinSdkVersion: toMinSdkVersion,
838 }
839 }
Jooyung Han749dc692020-04-15 11:03:39 +0900840 return true
Artur Satayev1111b842020-04-27 19:05:28 +0100841 })
842
843 a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, a.MinSdkVersion(), depsInfo)
844}
845
Artur Satayev849f8442020-04-28 14:57:42 +0100846func (a *AndroidApp) Updatable() bool {
Colin Cross56a83212020-09-15 18:30:11 -0700847 return Bool(a.appProperties.Updatable)
Artur Satayev849f8442020-04-28 14:57:42 +0100848}
849
Colin Cross0ea8ba82019-06-06 14:33:29 -0700850func (a *AndroidApp) getCertString(ctx android.BaseModuleContext) string {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800851 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
852 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000853 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800854 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800855 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800856}
857
Jiyong Park0f80c182020-01-31 02:49:53 +0900858func (a *AndroidApp) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
859 if IsJniDepTag(ctx.OtherModuleDependencyTag(dep)) {
860 return true
861 }
862 return a.Library.DepIsInSameApex(ctx, dep)
863}
864
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900865// For OutputFileProducer interface
866func (a *AndroidApp) OutputFiles(tag string) (android.Paths, error) {
867 switch tag {
868 case ".aapt.srcjar":
869 return []android.Path{a.aaptSrcJar}, nil
Anton Hansson092aca42020-08-13 19:37:22 +0100870 case ".export-package.apk":
871 return []android.Path{a.exportPackage}, nil
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900872 }
873 return a.Library.OutputFiles(tag)
874}
875
Jiyong Parkf7487312019-10-17 12:54:30 +0900876func (a *AndroidApp) Privileged() bool {
877 return Bool(a.appProperties.Privileged)
878}
879
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700880func (a *AndroidApp) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
Colin Cross1a6acd42020-06-16 17:51:46 -0700881 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700882}
883
884func (a *AndroidApp) PreventInstall() {
885 a.appProperties.PreventInstall = true
886}
887
888func (a *AndroidApp) HideFromMake() {
889 a.appProperties.HideFromMake = true
890}
891
892func (a *AndroidApp) MarkAsCoverageVariant(coverage bool) {
893 a.appProperties.IsCoverageVariant = coverage
894}
895
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400896func (a *AndroidApp) EnableCoverageIfNeeded() {}
897
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700898var _ cc.Coverage = (*AndroidApp)(nil)
899
Colin Cross1b16b0e2019-02-12 14:41:32 -0800900// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700901func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700902 module := &AndroidApp{}
903
Liz Kammera7a64f32020-07-09 15:16:41 -0700904 module.Module.dexProperties.Optimize.EnabledByDefault = true
905 module.Module.dexProperties.Optimize.Shrink = proptools.BoolPtr(true)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800906
Colin Crossae5caf52018-05-22 11:11:52 -0700907 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700908 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700909
Colin Crossce6734e2020-06-15 16:09:53 -0700910 module.addHostAndDeviceProperties()
Colin Cross36242852017-06-23 15:06:31 -0700911 module.AddProperties(
Colin Crossa97c5d32018-03-28 14:58:31 -0700912 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800913 &module.appProperties,
Ulya Trafimovich21a73752020-09-01 17:33:48 +0100914 &module.overridableAppProperties)
Colin Cross36242852017-06-23 15:06:31 -0700915
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000916 module.usesLibrary.enforce = true
917
Colin Crossa4f08812018-10-02 22:03:40 -0700918 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
919 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800920 android.InitOverridableModule(module, &module.appProperties.Overrides)
Jiyong Park52cd06f2019-11-11 10:14:32 +0900921 android.InitApexModule(module)
Colin Crossa4f08812018-10-02 22:03:40 -0700922
Colin Cross36242852017-06-23 15:06:31 -0700923 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700924}
Colin Crossae5caf52018-05-22 11:11:52 -0700925
926type appTestProperties struct {
Liz Kammer6b0c5522020-04-28 16:10:55 -0700927 // The name of the android_app module that the tests will run against.
Colin Crossae5caf52018-05-22 11:11:52 -0700928 Instrumentation_for *string
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700929
930 // if specified, the instrumentation target package name in the manifest is overwritten by it.
931 Instrumentation_target_package *string
Colin Crossae5caf52018-05-22 11:11:52 -0700932}
933
934type AndroidTest struct {
935 AndroidApp
936
937 appTestProperties appTestProperties
938
939 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700940
Dan Shi95d19422020-08-15 12:24:26 -0700941 testConfig android.Path
942 extraTestConfigs android.Paths
943 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700944}
945
Jaewoong Jung0949f312019-09-11 10:25:18 -0700946func (a *AndroidTest) InstallInTestcases() bool {
947 return true
948}
949
Colin Crossae5caf52018-05-22 11:11:52 -0700950func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
easoncylee5bcff5d2020-04-30 14:57:06 +0800951 var configs []tradefed.Config
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700952 if a.appTestProperties.Instrumentation_target_package != nil {
953 a.additionalAaptFlags = append(a.additionalAaptFlags,
954 "--rename-instrumentation-target-package "+*a.appTestProperties.Instrumentation_target_package)
955 } else if a.appTestProperties.Instrumentation_for != nil {
956 // Check if the instrumentation target package is overridden.
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800957 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
958 if overridden {
959 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
960 }
961 }
Colin Crossae5caf52018-05-22 11:11:52 -0700962 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700963
easoncylee5bcff5d2020-04-30 14:57:06 +0800964 for _, module := range a.testProperties.Test_mainline_modules {
965 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
966 }
967
Jaewoong Jung39982342020-01-14 10:27:18 -0800968 testConfig := tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config,
easoncylee5bcff5d2020-04-30 14:57:06 +0800969 a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites, a.testProperties.Auto_gen_config, configs)
Jaewoong Jung39982342020-01-14 10:27:18 -0800970 a.testConfig = a.FixTestConfig(ctx, testConfig)
Dan Shi95d19422020-08-15 12:24:26 -0700971 a.extraTestConfigs = android.PathsForModuleSrc(ctx, a.testProperties.Test_options.Extra_test_configs)
Colin Cross8a497952019-03-05 22:25:09 -0800972 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700973}
974
Jaewoong Jung39982342020-01-14 10:27:18 -0800975func (a *AndroidTest) FixTestConfig(ctx android.ModuleContext, testConfig android.Path) android.Path {
976 if testConfig == nil {
977 return nil
978 }
979
980 fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", "AndroidTest.xml")
Colin Crossf1a035e2020-11-16 17:32:30 -0800981 rule := android.NewRuleBuilder(pctx, ctx)
982 command := rule.Command().BuiltTool("test_config_fixer").Input(testConfig).Output(fixedConfig)
Jaewoong Jung39982342020-01-14 10:27:18 -0800983 fixNeeded := false
984
985 if ctx.ModuleName() != a.installApkName {
986 fixNeeded = true
987 command.FlagWithArg("--test-file-name ", a.installApkName+".apk")
988 }
989
990 if a.overridableAppProperties.Package_name != nil {
991 fixNeeded = true
992 command.FlagWithInput("--manifest ", a.manifestPath).
993 FlagWithArg("--package-name ", *a.overridableAppProperties.Package_name)
994 }
995
996 if fixNeeded {
Colin Crossf1a035e2020-11-16 17:32:30 -0800997 rule.Build("fix_test_config", "fix test config")
Jaewoong Jung39982342020-01-14 10:27:18 -0800998 return fixedConfig
999 }
1000 return testConfig
1001}
1002
Colin Cross303e21f2018-08-07 16:49:25 -07001003func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -07001004 a.AndroidApp.DepsMutator(ctx)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07001005}
1006
1007func (a *AndroidTest) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
1008 a.AndroidApp.OverridablePropertiesDepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -07001009 if a.appTestProperties.Instrumentation_for != nil {
1010 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
1011 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
1012 // use instrumentationForTag instead of libTag.
1013 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
1014 }
Colin Crossae5caf52018-05-22 11:11:52 -07001015}
1016
Colin Cross1b16b0e2019-02-12 14:41:32 -08001017// android_test compiles test sources and Android resources into an Android application package `.apk` file and
1018// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -07001019func AndroidTestFactory() android.Module {
1020 module := &AndroidTest{}
1021
Liz Kammera7a64f32020-07-09 15:16:41 -07001022 module.Module.dexProperties.Optimize.EnabledByDefault = true
Colin Cross5067db92018-09-17 16:46:35 -07001023
1024 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001025 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -08001026 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -07001027 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -08001028 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001029 module.Module.linter.test = true
Colin Crossae5caf52018-05-22 11:11:52 -07001030
Colin Crossce6734e2020-06-15 16:09:53 -07001031 module.addHostAndDeviceProperties()
Colin Crossae5caf52018-05-22 11:11:52 -07001032 module.AddProperties(
Colin Crossae5caf52018-05-22 11:11:52 -07001033 &module.aaptProperties,
1034 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -07001035 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -08001036 &module.overridableAppProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -07001037 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -07001038
Colin Crossa4f08812018-10-02 22:03:40 -07001039 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1040 android.InitDefaultableModule(module)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07001041 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossae5caf52018-05-22 11:11:52 -07001042 return module
1043}
Colin Crossbd01e2a2018-10-04 15:21:03 -07001044
Colin Cross252fc6f2018-10-04 15:22:03 -07001045type appTestHelperAppProperties struct {
1046 // list of compatibility suites (for example "cts", "vts") that the module should be
1047 // installed into.
1048 Test_suites []string `android:"arch_variant"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001049
1050 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1051 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1052 // explicitly.
1053 Auto_gen_config *bool
Colin Cross252fc6f2018-10-04 15:22:03 -07001054}
1055
1056type AndroidTestHelperApp struct {
1057 AndroidApp
1058
1059 appTestHelperAppProperties appTestHelperAppProperties
1060}
1061
Jaewoong Jung326a9412019-11-21 10:41:00 -08001062func (a *AndroidTestHelperApp) InstallInTestcases() bool {
1063 return true
1064}
1065
Colin Cross1b16b0e2019-02-12 14:41:32 -08001066// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
1067// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
1068// test.
Colin Cross252fc6f2018-10-04 15:22:03 -07001069func AndroidTestHelperAppFactory() android.Module {
1070 module := &AndroidTestHelperApp{}
1071
Liz Kammera7a64f32020-07-09 15:16:41 -07001072 module.Module.dexProperties.Optimize.EnabledByDefault = true
Colin Cross252fc6f2018-10-04 15:22:03 -07001073
1074 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -08001075 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -07001076 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -08001077 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001078 module.Module.linter.test = true
Colin Cross252fc6f2018-10-04 15:22:03 -07001079
Colin Crossce6734e2020-06-15 16:09:53 -07001080 module.addHostAndDeviceProperties()
Colin Cross252fc6f2018-10-04 15:22:03 -07001081 module.AddProperties(
Colin Cross252fc6f2018-10-04 15:22:03 -07001082 &module.aaptProperties,
1083 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -08001084 &module.appTestHelperAppProperties,
Ulya Trafimovich21a73752020-09-01 17:33:48 +01001085 &module.overridableAppProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -07001086
1087 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1088 android.InitDefaultableModule(module)
Anton Hansson3d2b6b42020-01-10 15:06:01 +00001089 android.InitApexModule(module)
Colin Cross252fc6f2018-10-04 15:22:03 -07001090 return module
1091}
1092
Colin Crossbd01e2a2018-10-04 15:21:03 -07001093type AndroidAppCertificate struct {
1094 android.ModuleBase
1095 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001096 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -07001097}
1098
1099type AndroidAppCertificateProperties struct {
1100 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
1101 Certificate *string
1102}
1103
Colin Cross1b16b0e2019-02-12 14:41:32 -08001104// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
1105// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -07001106func AndroidAppCertificateFactory() android.Module {
1107 module := &AndroidAppCertificate{}
1108 module.AddProperties(&module.properties)
1109 android.InitAndroidModule(module)
1110 return module
1111}
1112
Colin Crossbd01e2a2018-10-04 15:21:03 -07001113func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1114 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001115 c.Certificate = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -08001116 Pem: android.PathForModuleSrc(ctx, cert+".x509.pem"),
1117 Key: android.PathForModuleSrc(ctx, cert+".pk8"),
Colin Crossbd01e2a2018-10-04 15:21:03 -07001118 }
1119}
Jaewoong Jung525443a2019-02-28 15:35:54 -08001120
1121type OverrideAndroidApp struct {
1122 android.ModuleBase
1123 android.OverrideModuleBase
1124}
1125
Sasha Smundak613cbb12020-06-05 10:27:23 -07001126func (i *OverrideAndroidApp) GenerateAndroidBuildActions(_ android.ModuleContext) {
Jaewoong Jung525443a2019-02-28 15:35:54 -08001127 // All the overrides happen in the base module.
1128 // TODO(jungjw): Check the base module type.
1129}
1130
1131// override_android_app is used to create an android_app module based on another android_app by overriding
1132// some of its properties.
1133func OverrideAndroidAppModuleFactory() android.Module {
1134 m := &OverrideAndroidApp{}
1135 m.AddProperties(&overridableAppProperties{})
1136
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001137 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung525443a2019-02-28 15:35:54 -08001138 android.InitOverrideModule(m)
1139 return m
1140}
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001141
Jaewoong Jung26dedd32019-06-06 08:45:58 -07001142type OverrideAndroidTest struct {
1143 android.ModuleBase
1144 android.OverrideModuleBase
1145}
1146
Sasha Smundak613cbb12020-06-05 10:27:23 -07001147func (i *OverrideAndroidTest) GenerateAndroidBuildActions(_ android.ModuleContext) {
Jaewoong Jung26dedd32019-06-06 08:45:58 -07001148 // All the overrides happen in the base module.
1149 // TODO(jungjw): Check the base module type.
1150}
1151
1152// override_android_test is used to create an android_app module based on another android_test by overriding
1153// some of its properties.
1154func OverrideAndroidTestModuleFactory() android.Module {
1155 m := &OverrideAndroidTest{}
1156 m.AddProperties(&overridableAppProperties{})
1157 m.AddProperties(&appTestProperties{})
1158
1159 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
1160 android.InitOverrideModule(m)
1161 return m
1162}
1163
Colin Cross50ddcc42019-05-16 12:28:22 -07001164type UsesLibraryProperties struct {
1165 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file.
1166 Uses_libs []string
1167
1168 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file with
1169 // required=false.
1170 Optional_uses_libs []string
1171
1172 // If true, the list of uses_libs and optional_uses_libs modules must match the AndroidManifest.xml file. Defaults
1173 // to true if either uses_libs or optional_uses_libs is set. Will unconditionally default to true in the future.
1174 Enforce_uses_libs *bool
Ulya Trafimovich21a73752020-09-01 17:33:48 +01001175
Ulya Trafimovich54027b52020-09-09 14:08:23 +01001176 // Optional name of the <uses-library> provided by this module. This is needed for non-SDK
1177 // libraries, because SDK ones are automatically picked up by Soong. The <uses-library> name
1178 // normally is the same as the module name, but there are exceptions.
1179 Provides_uses_lib *string
Colin Cross50ddcc42019-05-16 12:28:22 -07001180}
1181
1182// usesLibrary provides properties and helper functions for AndroidApp and AndroidAppImport to verify that the
1183// <uses-library> tags that end up in the manifest of an APK match the ones known to the build system through the
1184// uses_libs and optional_uses_libs properties. The build system's values are used by dexpreopt to preopt apps
1185// with knowledge of their shared libraries.
1186type usesLibrary struct {
1187 usesLibraryProperties UsesLibraryProperties
Ulya Trafimovich22890c42021-01-05 12:04:17 +00001188
1189 // Whether to enforce verify_uses_library check.
1190 enforce bool
Colin Cross50ddcc42019-05-16 12:28:22 -07001191}
1192
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +01001193func (u *usesLibrary) addLib(lib string, optional bool) {
1194 if !android.InList(lib, u.usesLibraryProperties.Uses_libs) && !android.InList(lib, u.usesLibraryProperties.Optional_uses_libs) {
1195 if optional {
1196 u.usesLibraryProperties.Optional_uses_libs = append(u.usesLibraryProperties.Optional_uses_libs, lib)
1197 } else {
1198 u.usesLibraryProperties.Uses_libs = append(u.usesLibraryProperties.Uses_libs, lib)
1199 }
1200 }
1201}
1202
Paul Duffin250e6192019-06-07 10:44:37 +01001203func (u *usesLibrary) deps(ctx android.BottomUpMutatorContext, hasFrameworkLibs bool) {
Colin Cross3245b2c2019-06-07 13:18:09 -07001204 if !ctx.Config().UnbundledBuild() {
1205 ctx.AddVariationDependencies(nil, usesLibTag, u.usesLibraryProperties.Uses_libs...)
1206 ctx.AddVariationDependencies(nil, usesLibTag, u.presentOptionalUsesLibs(ctx)...)
Paul Duffin250e6192019-06-07 10:44:37 +01001207 // Only add these extra dependencies if the module depends on framework libs. This avoids
1208 // creating a cyclic dependency:
1209 // e.g. framework-res -> org.apache.http.legacy -> ... -> framework-res.
1210 if hasFrameworkLibs {
Ulya Trafimovich5f364b62020-06-30 12:39:01 +01001211 // Dexpreopt needs paths to the dex jars of these libraries in order to construct
1212 // class loader context for dex2oat. Add them as a dependency with a special tag.
Ulya Trafimovichb5218112020-10-07 15:11:32 +01001213 ctx.AddVariationDependencies(nil, usesLibCompat29Tag, dexpreopt.CompatUsesLibs29...)
1214 ctx.AddVariationDependencies(nil, usesLibCompat28Tag, dexpreopt.OptionalCompatUsesLibs28...)
1215 ctx.AddVariationDependencies(nil, usesLibCompat30Tag, dexpreopt.OptionalCompatUsesLibs30...)
Colin Cross3245b2c2019-06-07 13:18:09 -07001216 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001217 }
1218}
1219
1220// presentOptionalUsesLibs returns optional_uses_libs after filtering out MissingUsesLibraries, which don't exist in the
1221// build.
1222func (u *usesLibrary) presentOptionalUsesLibs(ctx android.BaseModuleContext) []string {
1223 optionalUsesLibs, _ := android.FilterList(u.usesLibraryProperties.Optional_uses_libs, ctx.Config().MissingUsesLibraries())
1224 return optionalUsesLibs
1225}
1226
Ulya Trafimovicheea486a2021-02-26 11:38:21 +00001227// Helper function to replace string in a list.
1228func replaceInList(list []string, oldstr, newstr string) {
1229 for i, str := range list {
1230 if str == oldstr {
1231 list[i] = newstr
1232 }
1233 }
1234}
1235
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001236// Returns a map of module names of shared library dependencies to the paths
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +01001237// to their dex jars on host and on device.
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001238func (u *usesLibrary) classLoaderContextForUsesLibDeps(ctx android.ModuleContext) dexpreopt.ClassLoaderContextMap {
1239 clcMap := make(dexpreopt.ClassLoaderContextMap)
Colin Cross50ddcc42019-05-16 12:28:22 -07001240
1241 if !ctx.Config().UnbundledBuild() {
Ulya Trafimovichb5218112020-10-07 15:11:32 +01001242 ctx.VisitDirectDeps(func(m android.Module) {
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001243 if tag, ok := ctx.OtherModuleDependencyTag(m).(usesLibraryDependencyTag); ok {
Ulya Trafimovichb5218112020-10-07 15:11:32 +01001244 dep := ctx.OtherModuleName(m)
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00001245 if lib, ok := m.(UsesLibraryDependency); ok {
Ulya Trafimovicheea486a2021-02-26 11:38:21 +00001246 libName := dep
1247 if ulib, ok := m.(ProvidesUsesLib); ok && ulib.ProvidesUsesLib() != nil {
1248 libName = *ulib.ProvidesUsesLib()
1249 // Replace module name with library name in `uses_libs`/`optional_uses_libs`
1250 // in order to pass verify_uses_libraries check (which compares these
1251 // properties against library names written in the manifest).
1252 replaceInList(u.usesLibraryProperties.Uses_libs, dep, libName)
1253 replaceInList(u.usesLibraryProperties.Optional_uses_libs, dep, libName)
1254 }
1255 clcMap.AddContext(ctx, tag.sdkVersion, libName,
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001256 lib.DexJarBuildPath(), lib.DexJarInstallPath(), lib.ClassLoaderContexts())
Ulya Trafimovichb5218112020-10-07 15:11:32 +01001257 } else if ctx.Config().AllowMissingDependencies() {
1258 ctx.AddMissingDependencies([]string{dep})
1259 } else {
1260 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must be a java library", dep)
1261 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001262 }
1263 })
1264 }
1265
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001266 return clcMap
Colin Cross50ddcc42019-05-16 12:28:22 -07001267}
1268
1269// enforceUsesLibraries returns true of <uses-library> tags should be checked against uses_libs and optional_uses_libs
1270// properties. Defaults to true if either of uses_libs or optional_uses_libs is specified. Will default to true
1271// unconditionally in the future.
1272func (u *usesLibrary) enforceUsesLibraries() bool {
1273 defaultEnforceUsesLibs := len(u.usesLibraryProperties.Uses_libs) > 0 ||
1274 len(u.usesLibraryProperties.Optional_uses_libs) > 0
Ulya Trafimovich22890c42021-01-05 12:04:17 +00001275 return BoolDefault(u.usesLibraryProperties.Enforce_uses_libs, u.enforce || defaultEnforceUsesLibs)
Colin Cross50ddcc42019-05-16 12:28:22 -07001276}
1277
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +01001278// Freeze the value of `enforce_uses_libs` based on the current values of `uses_libs` and `optional_uses_libs`.
1279func (u *usesLibrary) freezeEnforceUsesLibraries() {
1280 enforce := u.enforceUsesLibraries()
1281 u.usesLibraryProperties.Enforce_uses_libs = &enforce
1282}
1283
Ulya Trafimovich0aba2522021-03-03 16:38:37 +00001284// verifyUsesLibraries checks the <uses-library> tags in the manifest against the ones specified
1285// in the `uses_libs`/`optional_uses_libs` properties. The input can be either an XML manifest, or
1286// an APK with the manifest embedded in it (manifest_check will know which one it is by the file
1287// extension: APKs are supposed to end with '.apk').
1288func (u *usesLibrary) verifyUsesLibraries(ctx android.ModuleContext, inputFile android.Path,
Ulya Trafimovicha76d6602021-03-16 15:34:50 +00001289 outputFile android.WritablePath) android.Path {
Ulya Trafimovich0aba2522021-03-03 16:38:37 +00001290
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +00001291 statusFile := dexpreopt.UsesLibrariesStatusFile(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -07001292
Ulya Trafimovich6e55ef12021-03-04 12:37:50 +00001293 // Disable verify_uses_libraries check if dexpreopt is globally disabled. Without dexpreopt the
1294 // check is not necessary, and although it is good to have, it is difficult to maintain on
1295 // non-linux build platforms where dexpreopt is generally disabled (the check may fail due to
1296 // various unrelated reasons, such as a failure to get manifest from an APK).
Ulya Trafimovich39dd0a42021-03-29 14:57:34 +01001297 global := dexpreopt.GetGlobalConfig(ctx)
1298 if global.DisablePreopt || global.OnlyPreoptBootImageAndSystemServer {
Ulya Trafimovicha76d6602021-03-16 15:34:50 +00001299 return inputFile
Ulya Trafimovich6e55ef12021-03-04 12:37:50 +00001300 }
1301
Colin Crossf1a035e2020-11-16 17:32:30 -08001302 rule := android.NewRuleBuilder(pctx, ctx)
1303 cmd := rule.Command().BuiltTool("manifest_check").
Colin Cross50ddcc42019-05-16 12:28:22 -07001304 Flag("--enforce-uses-libraries").
Ulya Trafimovich0aba2522021-03-03 16:38:37 +00001305 Input(inputFile).
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +00001306 FlagWithOutput("--enforce-uses-libraries-status ", statusFile).
Ulya Trafimovich0aba2522021-03-03 16:38:37 +00001307 FlagWithInput("--aapt ", ctx.Config().HostToolPath(ctx, "aapt"))
1308
1309 if outputFile != nil {
1310 cmd.FlagWithOutput("-o ", outputFile)
1311 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001312
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +00001313 if dexpreopt.GetGlobalConfig(ctx).RelaxUsesLibraryCheck {
1314 cmd.Flag("--enforce-uses-libraries-relax")
1315 }
1316
Colin Cross50ddcc42019-05-16 12:28:22 -07001317 for _, lib := range u.usesLibraryProperties.Uses_libs {
1318 cmd.FlagWithArg("--uses-library ", lib)
1319 }
1320
1321 for _, lib := range u.usesLibraryProperties.Optional_uses_libs {
1322 cmd.FlagWithArg("--optional-uses-library ", lib)
1323 }
1324
Colin Crossf1a035e2020-11-16 17:32:30 -08001325 rule.Build("verify_uses_libraries", "verify <uses-library>")
Ulya Trafimovicha76d6602021-03-16 15:34:50 +00001326 return outputFile
Ulya Trafimovich0aba2522021-03-03 16:38:37 +00001327}
Colin Cross50ddcc42019-05-16 12:28:22 -07001328
Ulya Trafimovich0aba2522021-03-03 16:38:37 +00001329// verifyUsesLibrariesManifest checks the <uses-library> tags in an AndroidManifest.xml against
1330// the build system and returns the path to a copy of the manifest.
1331func (u *usesLibrary) verifyUsesLibrariesManifest(ctx android.ModuleContext, manifest android.Path) android.Path {
1332 outputFile := android.PathForModuleOut(ctx, "manifest_check", "AndroidManifest.xml")
Ulya Trafimovicha76d6602021-03-16 15:34:50 +00001333 return u.verifyUsesLibraries(ctx, manifest, outputFile)
Colin Cross50ddcc42019-05-16 12:28:22 -07001334}
1335
Ulya Trafimovich0aba2522021-03-03 16:38:37 +00001336// verifyUsesLibrariesAPK checks the <uses-library> tags in the manifest of an APK against the build
1337// system and returns the path to a copy of the APK.
Colin Cross50ddcc42019-05-16 12:28:22 -07001338func (u *usesLibrary) verifyUsesLibrariesAPK(ctx android.ModuleContext, apk android.Path) android.Path {
Ulya Trafimovich0aba2522021-03-03 16:38:37 +00001339 u.verifyUsesLibraries(ctx, apk, nil) // for APKs manifest_check does not write output file
Colin Cross50ddcc42019-05-16 12:28:22 -07001340 outputFile := android.PathForModuleOut(ctx, "verify_uses_libraries", apk.Base())
Colin Cross50ddcc42019-05-16 12:28:22 -07001341 return outputFile
1342}