blob: 574472c1b4f8c100c09a85dcacbffb46ee2ee664 [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
Liz Kammere2b27f42020-05-07 13:24:05 -0700125 // Name of the signing certificate lineage file.
126 Lineage *string
127
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)
305// and linkType is checked with dependencies so we can be sure that the whole dependency tree
306// 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)
Liz Kammera7a64f32020-07-09 15:16:41 -0700458 if a.dexProperties.Uncompress_dex == nil {
David Srbeckye033cba2020-05-20 22:20:28 +0100459 // If the value was not force-set by the user, use reasonable default based on the module.
Liz Kammera7a64f32020-07-09 15:16:41 -0700460 a.dexProperties.Uncompress_dex = proptools.BoolPtr(a.shouldUncompressDex(ctx))
David Srbeckye033cba2020-05-20 22:20:28 +0100461 }
Liz Kammera7a64f32020-07-09 15:16:41 -0700462 a.dexpreopter.uncompressedDex = *a.dexProperties.Uncompress_dex
Colin Cross50ddcc42019-05-16 12:28:22 -0700463 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100464 a.dexpreopter.classLoaderContexts = a.classLoaderContexts
Colin Cross50ddcc42019-05-16 12:28:22 -0700465 a.dexpreopter.manifestFile = a.mergedManifestFile
466
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800467 if ctx.ModuleName() != "framework-res" {
468 a.Module.compile(ctx, a.aaptSrcJar)
469 }
Colin Cross30e076a2015-04-13 13:58:27 -0700470
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800471 return a.maybeStrippedDexJarFile
472}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800473
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800474func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700475 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700476 if len(jniLibs) > 0 {
Colin Cross403cc152020-07-06 14:15:24 -0700477 a.jniLibs = jniLibs
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700478 if a.shouldEmbedJnis(ctx) {
Colin Crossa4f08812018-10-02 22:03:40 -0700479 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Colin Cross403cc152020-07-06 14:15:24 -0700480 a.installPathForJNISymbols = a.installPath(ctx).ToMakePath()
Sasha Smundak6ad77252019-05-01 13:16:22 -0700481 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.useEmbeddedNativeLibs(ctx))
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700482 for _, jni := range jniLibs {
483 if jni.coverageFile.Valid() {
Jaewoong Jung46984ee2020-04-07 13:07:55 -0700484 // Only collect coverage for the first target arch if this is a multilib target.
485 // TODO(jungjw): Ideally, we want to collect both reports, but that would cause coverage
486 // data file path collisions since the current coverage file path format doesn't contain
487 // arch-related strings. This is fine for now though; the code coverage team doesn't use
488 // multi-arch targets such as test_suite_* for coverage collections yet.
489 //
490 // Work with the team to come up with a new format that handles multilib modules properly
491 // and change this.
492 if len(ctx.Config().Targets[android.Android]) == 1 ||
Jaewoong Jung642916f2020-10-09 17:25:15 -0700493 ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType == jni.target.Arch.ArchType {
Jaewoong Jung46984ee2020-04-07 13:07:55 -0700494 a.jniCoverageOutputs = append(a.jniCoverageOutputs, jni.coverageFile.Path())
495 }
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700496 }
497 }
Colin Cross403cc152020-07-06 14:15:24 -0700498 a.embeddedJniLibs = true
Colin Crossa4f08812018-10-02 22:03:40 -0700499 }
500 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800501 return jniJarFile
502}
Colin Crossa4f08812018-10-02 22:03:40 -0700503
Colin Cross403cc152020-07-06 14:15:24 -0700504func (a *AndroidApp) JNISymbolsInstalls(installPath string) android.RuleBuilderInstalls {
505 var jniSymbols android.RuleBuilderInstalls
506 for _, jniLib := range a.jniLibs {
507 if jniLib.unstrippedFile != nil {
508 jniSymbols = append(jniSymbols, android.RuleBuilderInstall{
509 From: jniLib.unstrippedFile,
510 To: filepath.Join(installPath, targetToJniDir(jniLib.target), jniLib.unstrippedFile.Base()),
511 })
512 }
513 }
514 return jniSymbols
515}
516
Jaewoong Jung0949f312019-09-11 10:25:18 -0700517func (a *AndroidApp) noticeBuildActions(ctx android.ModuleContext) {
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700518 // Collect NOTICE files from all dependencies.
519 seenModules := make(map[android.Module]bool)
520 noticePathSet := make(map[android.Path]bool)
521
522 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
523 // Have we already seen this?
524 if _, ok := seenModules[child]; ok {
525 return false
526 }
527 seenModules[child] = true
528
529 // Skip host modules.
Jiyong Park1613e552020-09-14 19:43:17 +0900530 if child.Target().Os.Class == android.Host {
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700531 return false
532 }
533
Bob Badoura75b0572020-02-18 20:21:55 -0800534 paths := child.(android.Module).NoticeFiles()
535 if len(paths) > 0 {
536 for _, path := range paths {
537 noticePathSet[path] = true
538 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700539 }
540 return true
541 })
542
543 // If the app has one, add it too.
Bob Badoura75b0572020-02-18 20:21:55 -0800544 if len(a.NoticeFiles()) > 0 {
545 for _, path := range a.NoticeFiles() {
546 noticePathSet[path] = true
547 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700548 }
549
550 if len(noticePathSet) == 0 {
Jaewoong Jung98772792019-07-01 17:15:13 -0700551 return
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700552 }
553 var noticePaths []android.Path
554 for path := range noticePathSet {
555 noticePaths = append(noticePaths, path)
556 }
557 sort.Slice(noticePaths, func(i, j int) bool {
558 return noticePaths[i].String() < noticePaths[j].String()
559 })
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700560
Jaewoong Jung0949f312019-09-11 10:25:18 -0700561 a.noticeOutputs = android.BuildNoticeOutput(ctx, a.installDir, a.installApkName+".apk", noticePaths)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700562}
563
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700564// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
565// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
566func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate {
567 if android.SrcIsModule(certPropValue) == "" {
568 var mainCert Certificate
569 if certPropValue != "" {
570 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
571 mainCert = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -0800572 Pem: defaultDir.Join(ctx, certPropValue+".x509.pem"),
573 Key: defaultDir.Join(ctx, certPropValue+".pk8"),
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700574 }
575 } else {
576 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Colin Cross503c1d02020-01-28 14:00:53 -0800577 mainCert = Certificate{
578 Pem: pem,
579 Key: key,
580 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700581 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700582 certificates = append([]Certificate{mainCert}, certificates...)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700583 }
584
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700585 if !m.Platform() {
586 certPath := certificates[0].Pem.String()
Jeongik Chac9464142019-01-07 12:07:27 +0900587 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
588 if strings.HasPrefix(certPath, systemCertPath) {
589 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
Colin Cross440e0d02020-06-11 11:32:11 -0700590 allowed := ctx.Config().EnforceSystemCertificateAllowList()
Jeongik Chac9464142019-01-07 12:07:27 +0900591
Colin Cross440e0d02020-06-11 11:32:11 -0700592 if enforceSystemCert && !inList(m.Name(), allowed) {
Jeongik Chac9464142019-01-07 12:07:27 +0900593 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
594 }
595 }
596 }
597
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700598 return certificates
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800599}
600
Jooyung Han39ee1192020-03-23 20:21:11 +0900601func (a *AndroidApp) InstallApkName() string {
602 return a.installApkName
603}
604
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800605func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross50ddcc42019-05-16 12:28:22 -0700606 var apkDeps android.Paths
607
Colin Cross56a83212020-09-15 18:30:11 -0700608 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
609 a.hideApexVariantFromMake = true
610 }
611
Jeongik Cha538c0d02019-07-11 15:54:27 +0900612 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
613 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
614
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800615 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800616 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800617
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700618 if ctx.ModuleName() == "framework-res" {
619 // framework-res.apk is installed as system/framework/framework-res.apk
Jaewoong Jung0949f312019-09-11 10:25:18 -0700620 a.installDir = android.PathForModuleInstall(ctx, "framework")
Jiyong Parkf7487312019-10-17 12:54:30 +0900621 } else if a.Privileged() {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700622 a.installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
623 } else if ctx.InstallInTestcases() {
Jaewoong Jung326a9412019-11-21 10:41:00 -0800624 a.installDir = android.PathForModuleInstall(ctx, a.installApkName, ctx.DeviceConfig().DeviceArch())
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700625 } else {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700626 a.installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700627 }
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700628 a.onDeviceDir = android.InstallPathToOnDevicePath(ctx, a.installDir)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700629
Jaewoong Jung0949f312019-09-11 10:25:18 -0700630 a.noticeBuildActions(ctx)
Jaewoong Jung98772792019-07-01 17:15:13 -0700631 if Bool(a.appProperties.Embed_notices) || ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") {
632 a.aapt.noticeFile = a.noticeOutputs.HtmlGzOutput
633 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700634
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100635 a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Ulya Trafimovich18554242020-11-03 15:55:11 +0000636
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800637 // Process all building blocks, from AAPT to certificates.
638 a.aaptBuildActions(ctx)
639
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100640 // The decision to enforce <uses-library> checks is made before adding implicit SDK libraries.
641 a.usesLibrary.freezeEnforceUsesLibraries()
642
643 // Add implicit SDK libraries to <uses-library> list.
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100644 for _, usesLib := range a.classLoaderContexts.UsesLibs() {
Ulya Trafimovich663dc532020-09-10 12:48:53 +0100645 a.usesLibrary.addLib(usesLib, inList(usesLib, dexpreopt.OptionalCompatUsesLibs))
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100646 }
647
648 // Check that the <uses-library> list is coherent with the manifest.
Colin Cross50ddcc42019-05-16 12:28:22 -0700649 if a.usesLibrary.enforceUsesLibraries() {
650 manifestCheckFile := a.usesLibrary.verifyUsesLibrariesManifest(ctx, a.mergedManifestFile)
651 apkDeps = append(apkDeps, manifestCheckFile)
652 }
653
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800654 a.proguardBuildActions(ctx)
655
Colin Cross014489c2020-06-02 20:09:13 -0700656 a.linter.mergedManifest = a.aapt.mergedManifestFile
657 a.linter.manifest = a.aapt.manifestPath
658 a.linter.resources = a.aapt.resourceFiles
Colin Crossc0efd1d2020-07-03 11:56:24 -0700659 a.linter.buildModuleReportZip = ctx.Config().UnbundledBuildApps()
Colin Cross014489c2020-06-02 20:09:13 -0700660
Ulya Trafimovich18554242020-11-03 15:55:11 +0000661 dexJarFile := a.dexBuildActions(ctx)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800662
Colin Crossc2d24052020-05-13 11:05:02 -0700663 jniLibs, certificateDeps := collectAppDeps(ctx, a, a.shouldEmbedJnis(ctx), !Bool(a.appProperties.Jni_uses_platform_apis))
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800664 jniJarFile := a.jniBuildActions(jniLibs, ctx)
665
666 if ctx.Failed() {
667 return
668 }
669
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700670 certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx)
671 a.certificate = certificates[0]
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800672
673 // Build a final signed app package.
Jaewoong Jung5a498812019-11-07 14:14:38 -0800674 packageFile := android.PathForModuleOut(ctx, a.installApkName+".apk")
Songchun Fan17d69e32020-03-24 20:32:24 -0700675 v4SigningRequested := Bool(a.Module.deviceProperties.V4_signature)
676 var v4SignatureFile android.WritablePath = nil
677 if v4SigningRequested {
678 v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+".apk.idsig")
679 }
Liz Kammere2b27f42020-05-07 13:24:05 -0700680 var lineageFile android.Path
681 if lineage := String(a.overridableAppProperties.Lineage); lineage != "" {
682 lineageFile = android.PathForModuleSrc(ctx, lineage)
683 }
Liz Kammer70dd74d2020-05-07 13:24:05 -0700684 CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates, apkDeps, v4SignatureFile, lineageFile)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800685 a.outputFile = packageFile
Songchun Fan17d69e32020-03-24 20:32:24 -0700686 if v4SigningRequested {
687 a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
688 }
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800689
Colin Crosse560c4a2019-03-19 16:03:11 -0700690 for _, split := range a.aapt.splits {
691 // Sign the split APKs
Jaewoong Jung5a498812019-11-07 14:14:38 -0800692 packageFile := android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk")
Songchun Fan17d69e32020-03-24 20:32:24 -0700693 if v4SigningRequested {
694 v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk.idsig")
695 }
Liz Kammer70dd74d2020-05-07 13:24:05 -0700696 CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates, apkDeps, v4SignatureFile, lineageFile)
Colin Crosse560c4a2019-03-19 16:03:11 -0700697 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
Songchun Fan17d69e32020-03-24 20:32:24 -0700698 if v4SigningRequested {
699 a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
700 }
Colin Crosse560c4a2019-03-19 16:03:11 -0700701 }
702
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800703 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700704 bundleFile := android.PathForModuleOut(ctx, "base.zip")
705 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
706 a.bundleFile = bundleFile
707
Colin Cross56a83212020-09-15 18:30:11 -0700708 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
709
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800710 // Install the app package.
Colin Cross56a83212020-09-15 18:30:11 -0700711 if (Bool(a.Module.properties.Installable) || ctx.Host()) && apexInfo.IsForPlatform() {
Jiyong Park8ba50f92019-11-13 15:01:01 +0900712 ctx.InstallFile(a.installDir, a.outputFile.Base(), a.outputFile)
713 for _, extra := range a.extraOutputFiles {
714 ctx.InstallFile(a.installDir, extra.Base(), extra)
715 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800716 }
Artur Satayev1111b842020-04-27 19:05:28 +0100717
718 a.buildAppDependencyInfo(ctx)
Colin Cross30e076a2015-04-13 13:58:27 -0700719}
720
Colin Crossc2d24052020-05-13 11:05:02 -0700721type appDepsInterface interface {
722 sdkVersion() sdkSpec
723 minSdkVersion() sdkSpec
724 RequiresStableAPIs(ctx android.BaseModuleContext) bool
725}
726
727func collectAppDeps(ctx android.ModuleContext, app appDepsInterface,
728 shouldCollectRecursiveNativeDeps bool,
Colin Cross094cde42020-02-15 10:38:00 -0800729 checkNativeSdkVersion bool) ([]jniLib, []Certificate) {
Colin Crossc2d24052020-05-13 11:05:02 -0700730
Colin Crossa4f08812018-10-02 22:03:40 -0700731 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900732 var certificates []Certificate
Peter Collingbournead84f972019-12-17 16:46:18 -0800733 seenModulePaths := make(map[string]bool)
Colin Crossa4f08812018-10-02 22:03:40 -0700734
Colin Crossc2d24052020-05-13 11:05:02 -0700735 if checkNativeSdkVersion {
736 checkNativeSdkVersion = app.sdkVersion().specified() &&
737 app.sdkVersion().kind != sdkCorePlatform && !app.RequiresStableAPIs(ctx)
738 }
739
Peter Collingbournead84f972019-12-17 16:46:18 -0800740 ctx.WalkDeps(func(module android.Module, parent android.Module) bool {
Colin Crossa4f08812018-10-02 22:03:40 -0700741 otherName := ctx.OtherModuleName(module)
742 tag := ctx.OtherModuleDependencyTag(module)
743
Colin Crossf0913fb2020-07-29 12:59:39 -0700744 if IsJniDepTag(tag) || cc.IsSharedDepTag(tag) {
Colin Crossa4f08812018-10-02 22:03:40 -0700745 if dep, ok := module.(*cc.Module); ok {
Colin Cross95f1ca02020-10-29 20:47:22 -0700746 if dep.IsNdk(ctx.Config()) || dep.IsStubs() {
Peter Collingbournead84f972019-12-17 16:46:18 -0800747 return false
748 }
749
Colin Crossa4f08812018-10-02 22:03:40 -0700750 lib := dep.OutputFile()
Peter Collingbournead84f972019-12-17 16:46:18 -0800751 path := lib.Path()
752 if seenModulePaths[path.String()] {
753 return false
754 }
755 seenModulePaths[path.String()] = true
756
Colin Crossc2d24052020-05-13 11:05:02 -0700757 if checkNativeSdkVersion && dep.SdkVersion() == "" {
758 ctx.PropertyErrorf("jni_libs", "JNI dependency %q uses platform APIs, but this module does not",
759 otherName)
Colin Cross094cde42020-02-15 10:38:00 -0800760 }
761
Colin Crossa4f08812018-10-02 22:03:40 -0700762 if lib.Valid() {
763 jniLibs = append(jniLibs, jniLib{
Colin Cross403cc152020-07-06 14:15:24 -0700764 name: ctx.OtherModuleName(module),
765 path: path,
766 target: module.Target(),
767 coverageFile: dep.CoverageOutputFile(),
768 unstrippedFile: dep.UnstrippedOutputFile(),
Colin Crossa4f08812018-10-02 22:03:40 -0700769 })
770 } else {
771 ctx.ModuleErrorf("dependency %q missing output file", otherName)
772 }
773 } else {
774 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
Colin Crossa4f08812018-10-02 22:03:40 -0700775 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800776
777 return shouldCollectRecursiveNativeDeps
778 }
779
780 if tag == certificateTag {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700781 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900782 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700783 } else {
784 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
785 }
Colin Crossa4f08812018-10-02 22:03:40 -0700786 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800787
788 return false
Colin Crossa4f08812018-10-02 22:03:40 -0700789 })
790
Colin Crossbd01e2a2018-10-04 15:21:03 -0700791 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700792}
793
Jooyung Han749dc692020-04-15 11:03:39 +0900794func (a *AndroidApp) WalkPayloadDeps(ctx android.ModuleContext, do android.PayloadDepsCallback) {
Artur Satayev1111b842020-04-27 19:05:28 +0100795 ctx.WalkDeps(func(child, parent android.Module) bool {
796 isExternal := !a.DepIsInSameApex(ctx, child)
797 if am, ok := child.(android.ApexModule); ok {
Jooyung Han749dc692020-04-15 11:03:39 +0900798 if !do(ctx, parent, am, isExternal) {
799 return false
800 }
Artur Satayev1111b842020-04-27 19:05:28 +0100801 }
802 return !isExternal
803 })
804}
805
806func (a *AndroidApp) buildAppDependencyInfo(ctx android.ModuleContext) {
807 if ctx.Host() {
808 return
809 }
810
811 depsInfo := android.DepNameToDepInfoMap{}
Jooyung Han749dc692020-04-15 11:03:39 +0900812 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool {
Artur Satayev1111b842020-04-27 19:05:28 +0100813 depName := to.Name()
814 if info, exist := depsInfo[depName]; exist {
815 info.From = append(info.From, from.Name())
816 info.IsExternal = info.IsExternal && externalDep
817 depsInfo[depName] = info
818 } else {
819 toMinSdkVersion := "(no version)"
820 if m, ok := to.(interface{ MinSdkVersion() string }); ok {
821 if v := m.MinSdkVersion(); v != "" {
822 toMinSdkVersion = v
823 }
824 }
825 depsInfo[depName] = android.ApexModuleDepInfo{
826 To: depName,
827 From: []string{from.Name()},
828 IsExternal: externalDep,
829 MinSdkVersion: toMinSdkVersion,
830 }
831 }
Jooyung Han749dc692020-04-15 11:03:39 +0900832 return true
Artur Satayev1111b842020-04-27 19:05:28 +0100833 })
834
835 a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, a.MinSdkVersion(), depsInfo)
836}
837
Artur Satayev849f8442020-04-28 14:57:42 +0100838func (a *AndroidApp) Updatable() bool {
Colin Cross56a83212020-09-15 18:30:11 -0700839 return Bool(a.appProperties.Updatable)
Artur Satayev849f8442020-04-28 14:57:42 +0100840}
841
Colin Cross0ea8ba82019-06-06 14:33:29 -0700842func (a *AndroidApp) getCertString(ctx android.BaseModuleContext) string {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800843 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
844 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000845 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800846 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800847 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800848}
849
Jiyong Park0f80c182020-01-31 02:49:53 +0900850func (a *AndroidApp) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
851 if IsJniDepTag(ctx.OtherModuleDependencyTag(dep)) {
852 return true
853 }
854 return a.Library.DepIsInSameApex(ctx, dep)
855}
856
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900857// For OutputFileProducer interface
858func (a *AndroidApp) OutputFiles(tag string) (android.Paths, error) {
859 switch tag {
860 case ".aapt.srcjar":
861 return []android.Path{a.aaptSrcJar}, nil
Anton Hansson092aca42020-08-13 19:37:22 +0100862 case ".export-package.apk":
863 return []android.Path{a.exportPackage}, nil
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900864 }
865 return a.Library.OutputFiles(tag)
866}
867
Jiyong Parkf7487312019-10-17 12:54:30 +0900868func (a *AndroidApp) Privileged() bool {
869 return Bool(a.appProperties.Privileged)
870}
871
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700872func (a *AndroidApp) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
Colin Cross1a6acd42020-06-16 17:51:46 -0700873 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700874}
875
876func (a *AndroidApp) PreventInstall() {
877 a.appProperties.PreventInstall = true
878}
879
880func (a *AndroidApp) HideFromMake() {
881 a.appProperties.HideFromMake = true
882}
883
884func (a *AndroidApp) MarkAsCoverageVariant(coverage bool) {
885 a.appProperties.IsCoverageVariant = coverage
886}
887
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400888func (a *AndroidApp) EnableCoverageIfNeeded() {}
889
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700890var _ cc.Coverage = (*AndroidApp)(nil)
891
Colin Cross1b16b0e2019-02-12 14:41:32 -0800892// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700893func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700894 module := &AndroidApp{}
895
Liz Kammera7a64f32020-07-09 15:16:41 -0700896 module.Module.dexProperties.Optimize.EnabledByDefault = true
897 module.Module.dexProperties.Optimize.Shrink = proptools.BoolPtr(true)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800898
Colin Crossae5caf52018-05-22 11:11:52 -0700899 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700900 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700901
Colin Crossce6734e2020-06-15 16:09:53 -0700902 module.addHostAndDeviceProperties()
Colin Cross36242852017-06-23 15:06:31 -0700903 module.AddProperties(
Colin Crossa97c5d32018-03-28 14:58:31 -0700904 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800905 &module.appProperties,
Ulya Trafimovich21a73752020-09-01 17:33:48 +0100906 &module.overridableAppProperties)
Colin Cross36242852017-06-23 15:06:31 -0700907
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000908 module.usesLibrary.enforce = true
909
Colin Crossa4f08812018-10-02 22:03:40 -0700910 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
911 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800912 android.InitOverridableModule(module, &module.appProperties.Overrides)
Jiyong Park52cd06f2019-11-11 10:14:32 +0900913 android.InitApexModule(module)
Colin Crossa4f08812018-10-02 22:03:40 -0700914
Colin Cross36242852017-06-23 15:06:31 -0700915 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700916}
Colin Crossae5caf52018-05-22 11:11:52 -0700917
918type appTestProperties struct {
Liz Kammer6b0c5522020-04-28 16:10:55 -0700919 // The name of the android_app module that the tests will run against.
Colin Crossae5caf52018-05-22 11:11:52 -0700920 Instrumentation_for *string
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700921
922 // if specified, the instrumentation target package name in the manifest is overwritten by it.
923 Instrumentation_target_package *string
Colin Crossae5caf52018-05-22 11:11:52 -0700924}
925
926type AndroidTest struct {
927 AndroidApp
928
929 appTestProperties appTestProperties
930
931 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700932
Dan Shi95d19422020-08-15 12:24:26 -0700933 testConfig android.Path
934 extraTestConfigs android.Paths
935 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700936}
937
Jaewoong Jung0949f312019-09-11 10:25:18 -0700938func (a *AndroidTest) InstallInTestcases() bool {
939 return true
940}
941
Colin Crossae5caf52018-05-22 11:11:52 -0700942func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
easoncylee5bcff5d2020-04-30 14:57:06 +0800943 var configs []tradefed.Config
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700944 if a.appTestProperties.Instrumentation_target_package != nil {
945 a.additionalAaptFlags = append(a.additionalAaptFlags,
946 "--rename-instrumentation-target-package "+*a.appTestProperties.Instrumentation_target_package)
947 } else if a.appTestProperties.Instrumentation_for != nil {
948 // Check if the instrumentation target package is overridden.
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800949 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
950 if overridden {
951 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
952 }
953 }
Colin Crossae5caf52018-05-22 11:11:52 -0700954 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700955
easoncylee5bcff5d2020-04-30 14:57:06 +0800956 for _, module := range a.testProperties.Test_mainline_modules {
957 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
958 }
959
Jaewoong Jung39982342020-01-14 10:27:18 -0800960 testConfig := tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config,
easoncylee5bcff5d2020-04-30 14:57:06 +0800961 a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites, a.testProperties.Auto_gen_config, configs)
Jaewoong Jung39982342020-01-14 10:27:18 -0800962 a.testConfig = a.FixTestConfig(ctx, testConfig)
Dan Shi95d19422020-08-15 12:24:26 -0700963 a.extraTestConfigs = android.PathsForModuleSrc(ctx, a.testProperties.Test_options.Extra_test_configs)
Colin Cross8a497952019-03-05 22:25:09 -0800964 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700965}
966
Jaewoong Jung39982342020-01-14 10:27:18 -0800967func (a *AndroidTest) FixTestConfig(ctx android.ModuleContext, testConfig android.Path) android.Path {
968 if testConfig == nil {
969 return nil
970 }
971
972 fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", "AndroidTest.xml")
Colin Crossf1a035e2020-11-16 17:32:30 -0800973 rule := android.NewRuleBuilder(pctx, ctx)
974 command := rule.Command().BuiltTool("test_config_fixer").Input(testConfig).Output(fixedConfig)
Jaewoong Jung39982342020-01-14 10:27:18 -0800975 fixNeeded := false
976
977 if ctx.ModuleName() != a.installApkName {
978 fixNeeded = true
979 command.FlagWithArg("--test-file-name ", a.installApkName+".apk")
980 }
981
982 if a.overridableAppProperties.Package_name != nil {
983 fixNeeded = true
984 command.FlagWithInput("--manifest ", a.manifestPath).
985 FlagWithArg("--package-name ", *a.overridableAppProperties.Package_name)
986 }
987
988 if fixNeeded {
Colin Crossf1a035e2020-11-16 17:32:30 -0800989 rule.Build("fix_test_config", "fix test config")
Jaewoong Jung39982342020-01-14 10:27:18 -0800990 return fixedConfig
991 }
992 return testConfig
993}
994
Colin Cross303e21f2018-08-07 16:49:25 -0700995func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -0700996 a.AndroidApp.DepsMutator(ctx)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700997}
998
999func (a *AndroidTest) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
1000 a.AndroidApp.OverridablePropertiesDepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -07001001 if a.appTestProperties.Instrumentation_for != nil {
1002 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
1003 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
1004 // use instrumentationForTag instead of libTag.
1005 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
1006 }
Colin Crossae5caf52018-05-22 11:11:52 -07001007}
1008
Colin Cross1b16b0e2019-02-12 14:41:32 -08001009// android_test compiles test sources and Android resources into an Android application package `.apk` file and
1010// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -07001011func AndroidTestFactory() android.Module {
1012 module := &AndroidTest{}
1013
Liz Kammera7a64f32020-07-09 15:16:41 -07001014 module.Module.dexProperties.Optimize.EnabledByDefault = true
Colin Cross5067db92018-09-17 16:46:35 -07001015
1016 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001017 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -08001018 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -07001019 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -08001020 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001021 module.Module.linter.test = true
Colin Crossae5caf52018-05-22 11:11:52 -07001022
Colin Crossce6734e2020-06-15 16:09:53 -07001023 module.addHostAndDeviceProperties()
Colin Crossae5caf52018-05-22 11:11:52 -07001024 module.AddProperties(
Colin Crossae5caf52018-05-22 11:11:52 -07001025 &module.aaptProperties,
1026 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -07001027 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -08001028 &module.overridableAppProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -07001029 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -07001030
Colin Crossa4f08812018-10-02 22:03:40 -07001031 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1032 android.InitDefaultableModule(module)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07001033 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossae5caf52018-05-22 11:11:52 -07001034 return module
1035}
Colin Crossbd01e2a2018-10-04 15:21:03 -07001036
Colin Cross252fc6f2018-10-04 15:22:03 -07001037type appTestHelperAppProperties struct {
1038 // list of compatibility suites (for example "cts", "vts") that the module should be
1039 // installed into.
1040 Test_suites []string `android:"arch_variant"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001041
1042 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1043 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1044 // explicitly.
1045 Auto_gen_config *bool
Colin Cross252fc6f2018-10-04 15:22:03 -07001046}
1047
1048type AndroidTestHelperApp struct {
1049 AndroidApp
1050
1051 appTestHelperAppProperties appTestHelperAppProperties
1052}
1053
Jaewoong Jung326a9412019-11-21 10:41:00 -08001054func (a *AndroidTestHelperApp) InstallInTestcases() bool {
1055 return true
1056}
1057
Colin Cross1b16b0e2019-02-12 14:41:32 -08001058// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
1059// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
1060// test.
Colin Cross252fc6f2018-10-04 15:22:03 -07001061func AndroidTestHelperAppFactory() android.Module {
1062 module := &AndroidTestHelperApp{}
1063
Liz Kammera7a64f32020-07-09 15:16:41 -07001064 module.Module.dexProperties.Optimize.EnabledByDefault = true
Colin Cross252fc6f2018-10-04 15:22:03 -07001065
1066 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -08001067 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -07001068 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -08001069 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001070 module.Module.linter.test = true
Colin Cross252fc6f2018-10-04 15:22:03 -07001071
Colin Crossce6734e2020-06-15 16:09:53 -07001072 module.addHostAndDeviceProperties()
Colin Cross252fc6f2018-10-04 15:22:03 -07001073 module.AddProperties(
Colin Cross252fc6f2018-10-04 15:22:03 -07001074 &module.aaptProperties,
1075 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -08001076 &module.appTestHelperAppProperties,
Ulya Trafimovich21a73752020-09-01 17:33:48 +01001077 &module.overridableAppProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -07001078
1079 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1080 android.InitDefaultableModule(module)
Anton Hansson3d2b6b42020-01-10 15:06:01 +00001081 android.InitApexModule(module)
Colin Cross252fc6f2018-10-04 15:22:03 -07001082 return module
1083}
1084
Colin Crossbd01e2a2018-10-04 15:21:03 -07001085type AndroidAppCertificate struct {
1086 android.ModuleBase
1087 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001088 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -07001089}
1090
1091type AndroidAppCertificateProperties struct {
1092 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
1093 Certificate *string
1094}
1095
Colin Cross1b16b0e2019-02-12 14:41:32 -08001096// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
1097// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -07001098func AndroidAppCertificateFactory() android.Module {
1099 module := &AndroidAppCertificate{}
1100 module.AddProperties(&module.properties)
1101 android.InitAndroidModule(module)
1102 return module
1103}
1104
Colin Crossbd01e2a2018-10-04 15:21:03 -07001105func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1106 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001107 c.Certificate = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -08001108 Pem: android.PathForModuleSrc(ctx, cert+".x509.pem"),
1109 Key: android.PathForModuleSrc(ctx, cert+".pk8"),
Colin Crossbd01e2a2018-10-04 15:21:03 -07001110 }
1111}
Jaewoong Jung525443a2019-02-28 15:35:54 -08001112
1113type OverrideAndroidApp struct {
1114 android.ModuleBase
1115 android.OverrideModuleBase
1116}
1117
Sasha Smundak613cbb12020-06-05 10:27:23 -07001118func (i *OverrideAndroidApp) GenerateAndroidBuildActions(_ android.ModuleContext) {
Jaewoong Jung525443a2019-02-28 15:35:54 -08001119 // All the overrides happen in the base module.
1120 // TODO(jungjw): Check the base module type.
1121}
1122
1123// override_android_app is used to create an android_app module based on another android_app by overriding
1124// some of its properties.
1125func OverrideAndroidAppModuleFactory() android.Module {
1126 m := &OverrideAndroidApp{}
1127 m.AddProperties(&overridableAppProperties{})
1128
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001129 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung525443a2019-02-28 15:35:54 -08001130 android.InitOverrideModule(m)
1131 return m
1132}
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001133
Jaewoong Jung26dedd32019-06-06 08:45:58 -07001134type OverrideAndroidTest struct {
1135 android.ModuleBase
1136 android.OverrideModuleBase
1137}
1138
Sasha Smundak613cbb12020-06-05 10:27:23 -07001139func (i *OverrideAndroidTest) GenerateAndroidBuildActions(_ android.ModuleContext) {
Jaewoong Jung26dedd32019-06-06 08:45:58 -07001140 // All the overrides happen in the base module.
1141 // TODO(jungjw): Check the base module type.
1142}
1143
1144// override_android_test is used to create an android_app module based on another android_test by overriding
1145// some of its properties.
1146func OverrideAndroidTestModuleFactory() android.Module {
1147 m := &OverrideAndroidTest{}
1148 m.AddProperties(&overridableAppProperties{})
1149 m.AddProperties(&appTestProperties{})
1150
1151 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
1152 android.InitOverrideModule(m)
1153 return m
1154}
1155
Colin Cross50ddcc42019-05-16 12:28:22 -07001156type UsesLibraryProperties struct {
1157 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file.
1158 Uses_libs []string
1159
1160 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file with
1161 // required=false.
1162 Optional_uses_libs []string
1163
1164 // If true, the list of uses_libs and optional_uses_libs modules must match the AndroidManifest.xml file. Defaults
1165 // to true if either uses_libs or optional_uses_libs is set. Will unconditionally default to true in the future.
1166 Enforce_uses_libs *bool
Ulya Trafimovich21a73752020-09-01 17:33:48 +01001167
Ulya Trafimovich54027b52020-09-09 14:08:23 +01001168 // Optional name of the <uses-library> provided by this module. This is needed for non-SDK
1169 // libraries, because SDK ones are automatically picked up by Soong. The <uses-library> name
1170 // normally is the same as the module name, but there are exceptions.
1171 Provides_uses_lib *string
Colin Cross50ddcc42019-05-16 12:28:22 -07001172}
1173
1174// usesLibrary provides properties and helper functions for AndroidApp and AndroidAppImport to verify that the
1175// <uses-library> tags that end up in the manifest of an APK match the ones known to the build system through the
1176// uses_libs and optional_uses_libs properties. The build system's values are used by dexpreopt to preopt apps
1177// with knowledge of their shared libraries.
1178type usesLibrary struct {
1179 usesLibraryProperties UsesLibraryProperties
Ulya Trafimovich22890c42021-01-05 12:04:17 +00001180
1181 // Whether to enforce verify_uses_library check.
1182 enforce bool
Colin Cross50ddcc42019-05-16 12:28:22 -07001183}
1184
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +01001185func (u *usesLibrary) addLib(lib string, optional bool) {
1186 if !android.InList(lib, u.usesLibraryProperties.Uses_libs) && !android.InList(lib, u.usesLibraryProperties.Optional_uses_libs) {
1187 if optional {
1188 u.usesLibraryProperties.Optional_uses_libs = append(u.usesLibraryProperties.Optional_uses_libs, lib)
1189 } else {
1190 u.usesLibraryProperties.Uses_libs = append(u.usesLibraryProperties.Uses_libs, lib)
1191 }
1192 }
1193}
1194
Paul Duffin250e6192019-06-07 10:44:37 +01001195func (u *usesLibrary) deps(ctx android.BottomUpMutatorContext, hasFrameworkLibs bool) {
Colin Cross3245b2c2019-06-07 13:18:09 -07001196 if !ctx.Config().UnbundledBuild() {
1197 ctx.AddVariationDependencies(nil, usesLibTag, u.usesLibraryProperties.Uses_libs...)
1198 ctx.AddVariationDependencies(nil, usesLibTag, u.presentOptionalUsesLibs(ctx)...)
Paul Duffin250e6192019-06-07 10:44:37 +01001199 // Only add these extra dependencies if the module depends on framework libs. This avoids
1200 // creating a cyclic dependency:
1201 // e.g. framework-res -> org.apache.http.legacy -> ... -> framework-res.
1202 if hasFrameworkLibs {
Ulya Trafimovich5f364b62020-06-30 12:39:01 +01001203 // Dexpreopt needs paths to the dex jars of these libraries in order to construct
1204 // class loader context for dex2oat. Add them as a dependency with a special tag.
Ulya Trafimovichb5218112020-10-07 15:11:32 +01001205 ctx.AddVariationDependencies(nil, usesLibCompat29Tag, dexpreopt.CompatUsesLibs29...)
1206 ctx.AddVariationDependencies(nil, usesLibCompat28Tag, dexpreopt.OptionalCompatUsesLibs28...)
1207 ctx.AddVariationDependencies(nil, usesLibCompat30Tag, dexpreopt.OptionalCompatUsesLibs30...)
Colin Cross3245b2c2019-06-07 13:18:09 -07001208 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001209 }
1210}
1211
1212// presentOptionalUsesLibs returns optional_uses_libs after filtering out MissingUsesLibraries, which don't exist in the
1213// build.
1214func (u *usesLibrary) presentOptionalUsesLibs(ctx android.BaseModuleContext) []string {
1215 optionalUsesLibs, _ := android.FilterList(u.usesLibraryProperties.Optional_uses_libs, ctx.Config().MissingUsesLibraries())
1216 return optionalUsesLibs
1217}
1218
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001219// Returns a map of module names of shared library dependencies to the paths
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +01001220// to their dex jars on host and on device.
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001221func (u *usesLibrary) classLoaderContextForUsesLibDeps(ctx android.ModuleContext) dexpreopt.ClassLoaderContextMap {
1222 clcMap := make(dexpreopt.ClassLoaderContextMap)
Colin Cross50ddcc42019-05-16 12:28:22 -07001223
1224 if !ctx.Config().UnbundledBuild() {
Ulya Trafimovichb5218112020-10-07 15:11:32 +01001225 ctx.VisitDirectDeps(func(m android.Module) {
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001226 if tag, ok := ctx.OtherModuleDependencyTag(m).(usesLibraryDependencyTag); ok {
Ulya Trafimovichb5218112020-10-07 15:11:32 +01001227 dep := ctx.OtherModuleName(m)
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00001228 if lib, ok := m.(UsesLibraryDependency); ok {
Ulya Trafimovich78a71552020-11-25 14:20:52 +00001229 clcMap.AddContextForSdk(ctx, tag.sdkVersion, dep,
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001230 lib.DexJarBuildPath(), lib.DexJarInstallPath(), lib.ClassLoaderContexts())
Ulya Trafimovichb5218112020-10-07 15:11:32 +01001231 } else if ctx.Config().AllowMissingDependencies() {
1232 ctx.AddMissingDependencies([]string{dep})
1233 } else {
1234 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must be a java library", dep)
1235 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001236 }
1237 })
1238 }
1239
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001240 return clcMap
Colin Cross50ddcc42019-05-16 12:28:22 -07001241}
1242
1243// enforceUsesLibraries returns true of <uses-library> tags should be checked against uses_libs and optional_uses_libs
1244// properties. Defaults to true if either of uses_libs or optional_uses_libs is specified. Will default to true
1245// unconditionally in the future.
1246func (u *usesLibrary) enforceUsesLibraries() bool {
1247 defaultEnforceUsesLibs := len(u.usesLibraryProperties.Uses_libs) > 0 ||
1248 len(u.usesLibraryProperties.Optional_uses_libs) > 0
Ulya Trafimovich22890c42021-01-05 12:04:17 +00001249 return BoolDefault(u.usesLibraryProperties.Enforce_uses_libs, u.enforce || defaultEnforceUsesLibs)
Colin Cross50ddcc42019-05-16 12:28:22 -07001250}
1251
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +01001252// Freeze the value of `enforce_uses_libs` based on the current values of `uses_libs` and `optional_uses_libs`.
1253func (u *usesLibrary) freezeEnforceUsesLibraries() {
1254 enforce := u.enforceUsesLibraries()
1255 u.usesLibraryProperties.Enforce_uses_libs = &enforce
1256}
1257
Colin Cross50ddcc42019-05-16 12:28:22 -07001258// verifyUsesLibrariesManifest checks the <uses-library> tags in an AndroidManifest.xml against the ones specified
1259// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the manifest.
1260func (u *usesLibrary) verifyUsesLibrariesManifest(ctx android.ModuleContext, manifest android.Path) android.Path {
1261 outputFile := android.PathForModuleOut(ctx, "manifest_check", "AndroidManifest.xml")
1262
Colin Crossf1a035e2020-11-16 17:32:30 -08001263 rule := android.NewRuleBuilder(pctx, ctx)
1264 cmd := rule.Command().BuiltTool("manifest_check").
Colin Cross50ddcc42019-05-16 12:28:22 -07001265 Flag("--enforce-uses-libraries").
1266 Input(manifest).
1267 FlagWithOutput("-o ", outputFile)
1268
1269 for _, lib := range u.usesLibraryProperties.Uses_libs {
1270 cmd.FlagWithArg("--uses-library ", lib)
1271 }
1272
1273 for _, lib := range u.usesLibraryProperties.Optional_uses_libs {
1274 cmd.FlagWithArg("--optional-uses-library ", lib)
1275 }
1276
Colin Crossf1a035e2020-11-16 17:32:30 -08001277 rule.Build("verify_uses_libraries", "verify <uses-library>")
Colin Cross50ddcc42019-05-16 12:28:22 -07001278
1279 return outputFile
1280}
1281
1282// verifyUsesLibrariesAPK checks the <uses-library> tags in the manifest of an APK against the ones specified
1283// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the APK.
1284func (u *usesLibrary) verifyUsesLibrariesAPK(ctx android.ModuleContext, apk android.Path) android.Path {
1285 outputFile := android.PathForModuleOut(ctx, "verify_uses_libraries", apk.Base())
1286
Colin Crossf1a035e2020-11-16 17:32:30 -08001287 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -07001288 aapt := ctx.Config().HostToolPath(ctx, "aapt")
1289 rule.Command().
1290 Textf("aapt_binary=%s", aapt.String()).Implicit(aapt).
1291 Textf(`uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Uses_libs, " ")).
1292 Textf(`optional_uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Optional_uses_libs, " ")).
1293 Tool(android.PathForSource(ctx, "build/make/core/verify_uses_libraries.sh")).Input(apk)
1294 rule.Command().Text("cp -f").Input(apk).Output(outputFile)
1295
Colin Crossf1a035e2020-11-16 17:32:30 -08001296 rule.Build("verify_uses_libraries", "verify <uses-library>")
Colin Cross50ddcc42019-05-16 12:28:22 -07001297
1298 return outputFile
1299}