blob: 951ce166b72098ea339bc3fa2c4e15273ef5e60d [file] [log] [blame]
Colin Cross30e076a2015-04-13 13:58:27 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Android apps.
18
19import (
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070020 "path/filepath"
21 "reflect"
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"
Colin Cross303e21f2018-08-07 16:49:25 -070030 "android/soong/tradefed"
Colin Cross30e076a2015-04-13 13:58:27 -070031)
32
Jaewoong Jung3e18b192019-06-11 12:25:34 -070033var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070034
Colin Cross3bc7ffa2017-11-22 16:19:37 -080035func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000036 RegisterAppBuildComponents(android.InitRegistrationContext)
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -070037
38 initAndroidAppImportVariantGroupTypes()
Colin Cross3bc7ffa2017-11-22 16:19:37 -080039}
40
Paul Duffinf9b1da02019-12-18 19:51:55 +000041func RegisterAppBuildComponents(ctx android.RegistrationContext) {
42 ctx.RegisterModuleType("android_app", AndroidAppFactory)
43 ctx.RegisterModuleType("android_test", AndroidTestFactory)
44 ctx.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
45 ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
46 ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
47 ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory)
Roshan Piusb8307962020-04-27 09:42:27 -070048 ctx.RegisterModuleType("override_runtime_resource_overlay", OverrideRuntimeResourceOverlayModuleFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000049 ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory)
50 ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -080051 ctx.RegisterModuleType("runtime_resource_overlay", RuntimeResourceOverlayFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000052}
53
Colin Cross30e076a2015-04-13 13:58:27 -070054// AndroidManifest.xml merging
55// package splits
56
Colin Crossfabb6082018-02-20 17:22:23 -080057type appProperties struct {
Colin Crossbd01e2a2018-10-04 15:21:03 -070058 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
Colin Cross7d5136f2015-05-11 13:39:40 -070059 Additional_certificates []string
60
61 // If set, create package-export.apk, which other packages can
62 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
Nan Zhangea568a42017-11-08 21:20:04 -080063 Export_package_resources *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070064
Colin Cross16056062017-12-13 22:46:28 -080065 // Specifies that this app should be installed to the priv-app directory,
66 // where the system will grant it additional privileges not available to
67 // normal apps.
68 Privileged *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070069
70 // list of resource labels to generate individual resource packages
71 Package_splits []string
Jason Monkd4122be2018-08-10 09:33:36 -040072
73 // Names of modules to be overridden. Listed modules can only be other binaries
74 // (in Make or Soong).
75 // This does not completely prevent installation of the overridden binaries, but if both
76 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
77 // from PRODUCT_PACKAGES.
78 Overrides []string
Colin Crossa4f08812018-10-02 22:03:40 -070079
80 // list of native libraries that will be provided in or alongside the resulting jar
81 Jni_libs []string `android:"arch_variant"`
82
Colin Crossee87c602020-02-19 16:57:15 -080083 // if true, allow JNI libraries that link against platform APIs even if this module sets
84 // sdk_version.
85 Jni_uses_platform_apis *bool
86
Jaewoong Jungbc625cd2019-05-06 15:48:44 -070087 // STL library to use for JNI libraries.
88 Stl *string `android:"arch_variant"`
89
Colin Crosse4246ab2019-02-05 21:55:21 -080090 // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest
91 // 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 +090092 // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to true for
93 // android_app modules that are embedded to APEXes, defaults to false for other module types where the native
94 // libraries are generally preinstalled outside the APK.
Colin Crosse4246ab2019-02-05 21:55:21 -080095 Use_embedded_native_libs *bool
Colin Cross46abdad2019-02-07 13:07:08 -080096
97 // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that
98 // they are used from inside the APK at runtime.
99 Use_embedded_dex *bool
Colin Cross47fa9d32019-03-26 10:51:39 -0700100
101 // Forces native libraries to always be packaged into the APK,
102 // Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed.
103 // True for android_test* modules.
104 AlwaysPackageNativeLibs bool `blueprint:"mutated"`
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700105
106 // If set, find and merge all NOTICE files that this module and its dependencies have and store
107 // it in the APK as an asset.
108 Embed_notices *bool
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700109
110 // cc.Coverage related properties
111 PreventInstall bool `blueprint:"mutated"`
112 HideFromMake bool `blueprint:"mutated"`
113 IsCoverageVariant bool `blueprint:"mutated"`
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100114
115 // Whether this app is considered mainline updatable or not. When set to true, this will enforce
Artur Satayev11962102020-04-16 13:43:02 +0100116 // additional rules to make sure an app can safely be updated. Default is false.
117 // Prefer using other specific properties if build behaviour must be changed; avoid using this
118 // flag for anything but neverallow rules (unless the behaviour change is invisible to owners).
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100119 Updatable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700120}
121
Jaewoong Jung525443a2019-02-28 15:35:54 -0800122// android_app properties that can be overridden by override_android_app
123type overridableAppProperties struct {
124 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
125 // or an android_app_certificate module name in the form ":module".
126 Certificate *string
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700127
128 // 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
Jaewoong Jung525443a2019-02-28 15:35:54 -0800133}
134
Roshan Piusb8307962020-04-27 09:42:27 -0700135// runtime_resource_overlay properties that can be overridden by override_runtime_resource_overlay
136type OverridableRuntimeResourceOverlayProperties struct {
137 // the package name of this app. The package name in the manifest file is used if one was not given.
138 Package_name *string
139
140 // the target package name of this overlay app. The target package name in the manifest file is used if one was not given.
141 Target_package_name *string
142}
143
Colin Cross30e076a2015-04-13 13:58:27 -0700144type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -0700145 Library
146 aapt
Jaewoong Jung525443a2019-02-28 15:35:54 -0800147 android.OverridableModuleBase
Colin Crossa97c5d32018-03-28 14:58:31 -0700148
Colin Cross50ddcc42019-05-16 12:28:22 -0700149 usesLibrary usesLibrary
150
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900151 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -0700152
Colin Crossfabb6082018-02-20 17:22:23 -0800153 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -0700154
Jaewoong Jung525443a2019-02-28 15:35:54 -0800155 overridableAppProperties overridableAppProperties
156
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700157 installJniLibs []jniLib
158 jniCoverageOutputs android.Paths
Colin Crossf6237212018-10-29 23:14:58 -0700159
160 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800161
162 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
163 installApkName string
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800164
Colin Cross70dda7e2019-10-01 22:05:35 -0700165 installDir android.InstallPath
Jaewoong Jung0949f312019-09-11 10:25:18 -0700166
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700167 onDeviceDir string
168
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800169 additionalAaptFlags []string
Jaewoong Jung98772792019-07-01 17:15:13 -0700170
171 noticeOutputs android.NoticeOutputs
Jiyong Parkaf8998c2020-02-28 16:51:07 +0900172
173 overriddenManifestPackageName string
Colin Crosse1731a52017-12-14 11:22:55 -0800174}
175
Martin Stjernholm6d415272020-01-31 17:10:36 +0000176func (a *AndroidApp) IsInstallable() bool {
177 return Bool(a.properties.Installable)
178}
179
Colin Cross89c31582018-04-30 15:55:11 -0700180func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
181 return nil
182}
183
Colin Cross66f78822018-05-02 12:58:28 -0700184func (a *AndroidApp) ExportedStaticPackages() android.Paths {
185 return nil
186}
187
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900188func (a *AndroidApp) OutputFile() android.Path {
189 return a.outputFile
190}
191
Colin Cross503c1d02020-01-28 14:00:53 -0800192func (a *AndroidApp) Certificate() Certificate {
193 return a.certificate
194}
195
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700196func (a *AndroidApp) JniCoverageOutputs() android.Paths {
197 return a.jniCoverageOutputs
198}
199
Colin Crossa97c5d32018-03-28 14:58:31 -0700200var _ AndroidLibraryDependency = (*AndroidApp)(nil)
201
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900202type Certificate struct {
Colin Cross503c1d02020-01-28 14:00:53 -0800203 Pem, Key android.Path
204 presigned bool
205}
206
207var presignedCertificate = Certificate{presigned: true}
208
209func (c Certificate) AndroidMkString() string {
210 if c.presigned {
211 return "PRESIGNED"
212 } else {
213 return c.Pem.String()
214 }
Colin Cross30e076a2015-04-13 13:58:27 -0700215}
216
Colin Cross46c9b8b2017-06-22 16:51:17 -0700217func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
218 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700219
Jiyong Park6a927c42020-01-21 02:03:43 +0900220 if String(a.appProperties.Stl) == "c++_shared" && !a.sdkVersion().specified() {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700221 ctx.PropertyErrorf("stl", "sdk_version must be set in order to use c++_shared")
222 }
223
Paul Duffin250e6192019-06-07 10:44:37 +0100224 sdkDep := decodeSdkDep(ctx, sdkContext(a))
225 if sdkDep.hasFrameworkLibs() {
226 a.aapt.deps(ctx, sdkDep)
Colin Cross30e076a2015-04-13 13:58:27 -0700227 }
Colin Crossa4f08812018-10-02 22:03:40 -0700228
Peter Collingbournead84f972019-12-17 16:46:18 -0800229 tag := &jniDependencyTag{}
Colin Crossa4f08812018-10-02 22:03:40 -0700230 for _, jniTarget := range ctx.MultiTargets() {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700231 variation := append(jniTarget.Variations(),
232 blueprint.Variation{Mutator: "link", Variation: "shared"})
Colin Crossa4f08812018-10-02 22:03:40 -0700233 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
234 }
Colin Cross50ddcc42019-05-16 12:28:22 -0700235
Paul Duffin250e6192019-06-07 10:44:37 +0100236 a.usesLibrary.deps(ctx, sdkDep.hasFrameworkLibs())
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700237}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700238
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700239func (a *AndroidApp) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800240 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700241 if cert != "" {
242 ctx.AddDependency(ctx.Module(), certificateTag, cert)
243 }
244
245 for _, cert := range a.appProperties.Additional_certificates {
246 cert = android.SrcIsModule(cert)
247 if cert != "" {
248 ctx.AddDependency(ctx.Module(), certificateTag, cert)
249 } else {
250 ctx.PropertyErrorf("additional_certificates",
251 `must be names of android_app_certificate modules in the form ":module"`)
252 }
253 }
Colin Cross30e076a2015-04-13 13:58:27 -0700254}
255
Jeongik Cha538c0d02019-07-11 15:54:27 +0900256func (a *AndroidTestHelperApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
257 a.generateAndroidBuildActions(ctx)
258}
259
Colin Cross46c9b8b2017-06-22 16:51:17 -0700260func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100261 a.checkAppSdkVersions(ctx)
Colin Crossae5caf52018-05-22 11:11:52 -0700262 a.generateAndroidBuildActions(ctx)
263}
264
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100265func (a *AndroidApp) checkAppSdkVersions(ctx android.ModuleContext) {
266 if Bool(a.appProperties.Updatable) {
267 if !a.sdkVersion().stable() {
268 ctx.PropertyErrorf("sdk_version", "Updatable apps must use stable SDKs, found %v", a.sdkVersion())
269 }
Artur Satayev11962102020-04-16 13:43:02 +0100270 if String(a.deviceProperties.Min_sdk_version) == "" {
271 ctx.PropertyErrorf("updatable", "updatable apps must set min_sdk_version.")
272 }
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100273 }
274
275 a.checkPlatformAPI(ctx)
276 a.checkSdkVersions(ctx)
277}
278
Sasha Smundak6ad77252019-05-01 13:16:22 -0700279// Returns true if the native libraries should be stored in the APK uncompressed and the
Colin Crosse4246ab2019-02-05 21:55:21 -0800280// extractNativeLibs application flag should be set to false in the manifest.
Sasha Smundak6ad77252019-05-01 13:16:22 -0700281func (a *AndroidApp) useEmbeddedNativeLibs(ctx android.ModuleContext) bool {
Jiyong Park6a927c42020-01-21 02:03:43 +0900282 minSdkVersion, err := a.minSdkVersion().effectiveVersion(ctx)
Colin Crosse4246ab2019-02-05 21:55:21 -0800283 if err != nil {
284 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
285 }
286
Jiyong Park52cd06f2019-11-11 10:14:32 +0900287 return (minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)) ||
288 !a.IsForPlatform()
Colin Crosse4246ab2019-02-05 21:55:21 -0800289}
290
Colin Cross43f08db2018-11-12 10:13:39 -0800291// Returns whether this module should have the dex file stored uncompressed in the APK.
292func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800293 if Bool(a.appProperties.Use_embedded_dex) {
294 return true
295 }
296
Colin Cross53a87f52019-06-25 13:35:30 -0700297 // Uncompress dex in APKs of privileged apps (even for unbundled builds, they may
298 // be preinstalled as prebuilts).
Jiyong Parkf7487312019-10-17 12:54:30 +0900299 if ctx.Config().UncompressPrivAppDex() && a.Privileged() {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000300 return true
301 }
302
Colin Cross53a87f52019-06-25 13:35:30 -0700303 if ctx.Config().UnbundledBuild() {
304 return false
305 }
306
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700307 return shouldUncompressDex(ctx, &a.dexpreopter)
Colin Cross5a0dcd52018-10-05 14:20:06 -0700308}
309
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700310func (a *AndroidApp) shouldEmbedJnis(ctx android.BaseModuleContext) bool {
311 return ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) ||
Jiyong Park52cd06f2019-11-11 10:14:32 +0900312 !a.IsForPlatform() || a.appProperties.AlwaysPackageNativeLibs
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700313}
314
Jiyong Parkaf8998c2020-02-28 16:51:07 +0900315func (a *AndroidApp) OverriddenManifestPackageName() string {
316 return a.overriddenManifestPackageName
317}
318
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800319func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
David Brazdild25060a2019-02-18 18:24:16 +0000320 a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis)
321
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700322 // Ask manifest_fixer to add or update the application element indicating this app has no code.
323 a.aapt.hasNoCode = !a.hasCode(ctx)
324
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800325 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800326
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800327 // 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 -0800328 hasProduct := android.PrefixInList(a.aaptProperties.Aaptflags, "--product")
Colin Crosse78dcd32018-04-19 15:25:19 -0700329 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800330 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700331 }
332
Dan Willemsen72be5902018-10-24 20:24:57 -0700333 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
334 // Product AAPT config
335 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800336 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700337 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700338
Dan Willemsen72be5902018-10-24 20:24:57 -0700339 // Product AAPT preferred config
340 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800341 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700342 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700343 }
344
Jiyong Park7f67f482019-01-05 12:57:48 +0900345 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700346 if overridden || a.overridableAppProperties.Package_name != nil {
347 // The product override variable has a priority over the package_name property.
348 if !overridden {
349 manifestPackageName = *a.overridableAppProperties.Package_name
350 }
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800351 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
Jiyong Parkaf8998c2020-02-28 16:51:07 +0900352 a.overriddenManifestPackageName = manifestPackageName
Jiyong Park7f67f482019-01-05 12:57:48 +0900353 }
354
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800355 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
356
Colin Crosse560c4a2019-03-19 16:03:11 -0700357 a.aapt.splitNames = a.appProperties.Package_splits
Colin Cross50ddcc42019-05-16 12:28:22 -0700358 a.aapt.sdkLibraries = a.exportedSdkLibs
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800359 a.aapt.LoggingParent = String(a.overridableAppProperties.Logging_parent)
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800360 a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700361
Colin Cross46c9b8b2017-06-22 16:51:17 -0700362 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700363 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800364}
Colin Cross30e076a2015-04-13 13:58:27 -0700365
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800366func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700367 var staticLibProguardFlagFiles android.Paths
368 ctx.VisitDirectDeps(func(m android.Module) {
369 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
370 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
371 }
372 })
373
374 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
375
376 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
377 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800378}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800379
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800380func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross43f08db2018-11-12 10:13:39 -0800381
382 var installDir string
383 if ctx.ModuleName() == "framework-res" {
384 // framework-res.apk is installed as system/framework/framework-res.apk
385 installDir = "framework"
Jiyong Parkf7487312019-10-17 12:54:30 +0900386 } else if a.Privileged() {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800387 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800388 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800389 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800390 }
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800391 a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000392 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -0700393
394 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
395 a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
396 a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
397 a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
398 a.dexpreopter.manifestFile = a.mergedManifestFile
399
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000400 a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex
Colin Cross5a0dcd52018-10-05 14:20:06 -0700401
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800402 if ctx.ModuleName() != "framework-res" {
403 a.Module.compile(ctx, a.aaptSrcJar)
404 }
Colin Cross30e076a2015-04-13 13:58:27 -0700405
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800406 return a.maybeStrippedDexJarFile
407}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800408
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800409func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700410 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700411 if len(jniLibs) > 0 {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700412 if a.shouldEmbedJnis(ctx) {
Colin Crossa4f08812018-10-02 22:03:40 -0700413 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Sasha Smundak6ad77252019-05-01 13:16:22 -0700414 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.useEmbeddedNativeLibs(ctx))
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700415 for _, jni := range jniLibs {
416 if jni.coverageFile.Valid() {
Jaewoong Junge62e5942020-04-07 13:07:55 -0700417 // Only collect coverage for the first target arch if this is a multilib target.
418 // TODO(jungjw): Ideally, we want to collect both reports, but that would cause coverage
419 // data file path collisions since the current coverage file path format doesn't contain
420 // arch-related strings. This is fine for now though; the code coverage team doesn't use
421 // multi-arch targets such as test_suite_* for coverage collections yet.
422 //
423 // Work with the team to come up with a new format that handles multilib modules properly
424 // and change this.
425 if len(ctx.Config().Targets[android.Android]) == 1 ||
426 ctx.Config().Targets[android.Android][0].Arch.ArchType == jni.target.Arch.ArchType {
427 a.jniCoverageOutputs = append(a.jniCoverageOutputs, jni.coverageFile.Path())
428 }
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700429 }
430 }
Colin Crossa4f08812018-10-02 22:03:40 -0700431 } else {
432 a.installJniLibs = jniLibs
433 }
434 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800435 return jniJarFile
436}
Colin Crossa4f08812018-10-02 22:03:40 -0700437
Jaewoong Jung0949f312019-09-11 10:25:18 -0700438func (a *AndroidApp) noticeBuildActions(ctx android.ModuleContext) {
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700439 // Collect NOTICE files from all dependencies.
440 seenModules := make(map[android.Module]bool)
441 noticePathSet := make(map[android.Path]bool)
442
443 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
444 // Have we already seen this?
445 if _, ok := seenModules[child]; ok {
446 return false
447 }
448 seenModules[child] = true
449
450 // Skip host modules.
451 if child.Target().Os.Class == android.Host || child.Target().Os.Class == android.HostCross {
452 return false
453 }
454
455 path := child.(android.Module).NoticeFile()
456 if path.Valid() {
457 noticePathSet[path.Path()] = true
458 }
459 return true
460 })
461
462 // If the app has one, add it too.
463 if a.NoticeFile().Valid() {
464 noticePathSet[a.NoticeFile().Path()] = true
465 }
466
467 if len(noticePathSet) == 0 {
Jaewoong Jung98772792019-07-01 17:15:13 -0700468 return
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700469 }
470 var noticePaths []android.Path
471 for path := range noticePathSet {
472 noticePaths = append(noticePaths, path)
473 }
474 sort.Slice(noticePaths, func(i, j int) bool {
475 return noticePaths[i].String() < noticePaths[j].String()
476 })
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700477
Jaewoong Jung0949f312019-09-11 10:25:18 -0700478 a.noticeOutputs = android.BuildNoticeOutput(ctx, a.installDir, a.installApkName+".apk", noticePaths)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700479}
480
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700481// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
482// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
483func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate {
484 if android.SrcIsModule(certPropValue) == "" {
485 var mainCert Certificate
486 if certPropValue != "" {
487 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
488 mainCert = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -0800489 Pem: defaultDir.Join(ctx, certPropValue+".x509.pem"),
490 Key: defaultDir.Join(ctx, certPropValue+".pk8"),
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700491 }
492 } else {
493 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Colin Cross503c1d02020-01-28 14:00:53 -0800494 mainCert = Certificate{
495 Pem: pem,
496 Key: key,
497 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700498 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700499 certificates = append([]Certificate{mainCert}, certificates...)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700500 }
501
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700502 if !m.Platform() {
503 certPath := certificates[0].Pem.String()
Jeongik Chac9464142019-01-07 12:07:27 +0900504 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
505 if strings.HasPrefix(certPath, systemCertPath) {
506 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
507 whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
508
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700509 if enforceSystemCert && !inList(m.Name(), whitelist) {
Jeongik Chac9464142019-01-07 12:07:27 +0900510 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
511 }
512 }
513 }
514
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700515 return certificates
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800516}
517
Jooyung Han65cd0f02020-03-23 20:21:11 +0900518func (a *AndroidApp) InstallApkName() string {
519 return a.installApkName
520}
521
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800522func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross50ddcc42019-05-16 12:28:22 -0700523 var apkDeps android.Paths
524
Jeongik Cha538c0d02019-07-11 15:54:27 +0900525 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
526 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
527
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800528 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800529 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800530
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700531 if ctx.ModuleName() == "framework-res" {
532 // framework-res.apk is installed as system/framework/framework-res.apk
Jaewoong Jung0949f312019-09-11 10:25:18 -0700533 a.installDir = android.PathForModuleInstall(ctx, "framework")
Jiyong Parkf7487312019-10-17 12:54:30 +0900534 } else if a.Privileged() {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700535 a.installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
536 } else if ctx.InstallInTestcases() {
Jaewoong Jung326a9412019-11-21 10:41:00 -0800537 a.installDir = android.PathForModuleInstall(ctx, a.installApkName, ctx.DeviceConfig().DeviceArch())
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700538 } else {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700539 a.installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700540 }
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700541 a.onDeviceDir = android.InstallPathToOnDevicePath(ctx, a.installDir)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700542
Jaewoong Jung0949f312019-09-11 10:25:18 -0700543 a.noticeBuildActions(ctx)
Jaewoong Jung98772792019-07-01 17:15:13 -0700544 if Bool(a.appProperties.Embed_notices) || ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") {
545 a.aapt.noticeFile = a.noticeOutputs.HtmlGzOutput
546 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700547
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800548 // Process all building blocks, from AAPT to certificates.
549 a.aaptBuildActions(ctx)
550
Colin Cross50ddcc42019-05-16 12:28:22 -0700551 if a.usesLibrary.enforceUsesLibraries() {
552 manifestCheckFile := a.usesLibrary.verifyUsesLibrariesManifest(ctx, a.mergedManifestFile)
553 apkDeps = append(apkDeps, manifestCheckFile)
554 }
555
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800556 a.proguardBuildActions(ctx)
557
558 dexJarFile := a.dexBuildActions(ctx)
559
Peter Collingbournead84f972019-12-17 16:46:18 -0800560 jniLibs, certificateDeps := collectAppDeps(ctx, a.shouldEmbedJnis(ctx))
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800561 jniJarFile := a.jniBuildActions(jniLibs, ctx)
562
563 if ctx.Failed() {
564 return
565 }
566
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700567 certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx)
568 a.certificate = certificates[0]
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800569
570 // Build a final signed app package.
Jaewoong Jung5a498812019-11-07 14:14:38 -0800571 packageFile := android.PathForModuleOut(ctx, a.installApkName+".apk")
Songchun Fan688de9a2020-03-24 20:32:24 -0700572 v4SigningRequested := Bool(a.Module.deviceProperties.V4_signature)
573 var v4SignatureFile android.WritablePath = nil
574 if v4SigningRequested {
575 v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+".apk.idsig")
576 }
577 CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates, apkDeps, v4SignatureFile)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800578 a.outputFile = packageFile
Songchun Fan688de9a2020-03-24 20:32:24 -0700579 if v4SigningRequested {
580 a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
581 }
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800582
Colin Crosse560c4a2019-03-19 16:03:11 -0700583 for _, split := range a.aapt.splits {
584 // Sign the split APKs
Jaewoong Jung5a498812019-11-07 14:14:38 -0800585 packageFile := android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk")
Songchun Fan688de9a2020-03-24 20:32:24 -0700586 if v4SigningRequested {
587 v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk.idsig")
588 }
589 CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates, apkDeps, v4SignatureFile)
Colin Crosse560c4a2019-03-19 16:03:11 -0700590 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
Songchun Fan688de9a2020-03-24 20:32:24 -0700591 if v4SigningRequested {
592 a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
593 }
Colin Crosse560c4a2019-03-19 16:03:11 -0700594 }
595
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800596 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700597 bundleFile := android.PathForModuleOut(ctx, "base.zip")
598 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
599 a.bundleFile = bundleFile
600
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800601 // Install the app package.
Jiyong Park8ba50f92019-11-13 15:01:01 +0900602 if (Bool(a.Module.properties.Installable) || ctx.Host()) && a.IsForPlatform() {
603 ctx.InstallFile(a.installDir, a.outputFile.Base(), a.outputFile)
604 for _, extra := range a.extraOutputFiles {
605 ctx.InstallFile(a.installDir, extra.Base(), extra)
606 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800607 }
Colin Cross30e076a2015-04-13 13:58:27 -0700608}
609
Peter Collingbournead84f972019-12-17 16:46:18 -0800610func collectAppDeps(ctx android.ModuleContext, shouldCollectRecursiveNativeDeps bool) ([]jniLib, []Certificate) {
Colin Crossa4f08812018-10-02 22:03:40 -0700611 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900612 var certificates []Certificate
Peter Collingbournead84f972019-12-17 16:46:18 -0800613 seenModulePaths := make(map[string]bool)
Colin Crossa4f08812018-10-02 22:03:40 -0700614
Peter Collingbournead84f972019-12-17 16:46:18 -0800615 ctx.WalkDeps(func(module android.Module, parent android.Module) bool {
Colin Crossa4f08812018-10-02 22:03:40 -0700616 otherName := ctx.OtherModuleName(module)
617 tag := ctx.OtherModuleDependencyTag(module)
618
Peter Collingbournead84f972019-12-17 16:46:18 -0800619 if IsJniDepTag(tag) || tag == cc.SharedDepTag {
Colin Crossa4f08812018-10-02 22:03:40 -0700620 if dep, ok := module.(*cc.Module); ok {
Peter Collingbournead84f972019-12-17 16:46:18 -0800621 if dep.IsNdk() || dep.IsStubs() {
622 return false
623 }
624
Colin Crossa4f08812018-10-02 22:03:40 -0700625 lib := dep.OutputFile()
Peter Collingbournead84f972019-12-17 16:46:18 -0800626 path := lib.Path()
627 if seenModulePaths[path.String()] {
628 return false
629 }
630 seenModulePaths[path.String()] = true
631
Colin Crossa4f08812018-10-02 22:03:40 -0700632 if lib.Valid() {
633 jniLibs = append(jniLibs, jniLib{
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700634 name: ctx.OtherModuleName(module),
635 path: path,
636 target: module.Target(),
637 coverageFile: dep.CoverageOutputFile(),
Colin Crossa4f08812018-10-02 22:03:40 -0700638 })
639 } else {
640 ctx.ModuleErrorf("dependency %q missing output file", otherName)
641 }
642 } else {
643 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
Colin Crossa4f08812018-10-02 22:03:40 -0700644 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800645
646 return shouldCollectRecursiveNativeDeps
647 }
648
649 if tag == certificateTag {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700650 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900651 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700652 } else {
653 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
654 }
Colin Crossa4f08812018-10-02 22:03:40 -0700655 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800656
657 return false
Colin Crossa4f08812018-10-02 22:03:40 -0700658 })
659
Colin Crossbd01e2a2018-10-04 15:21:03 -0700660 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700661}
662
Colin Cross0ea8ba82019-06-06 14:33:29 -0700663func (a *AndroidApp) getCertString(ctx android.BaseModuleContext) string {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800664 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
665 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000666 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800667 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800668 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800669}
670
Jiyong Park0f80c182020-01-31 02:49:53 +0900671func (a *AndroidApp) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
672 if IsJniDepTag(ctx.OtherModuleDependencyTag(dep)) {
673 return true
674 }
675 return a.Library.DepIsInSameApex(ctx, dep)
676}
677
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900678// For OutputFileProducer interface
679func (a *AndroidApp) OutputFiles(tag string) (android.Paths, error) {
680 switch tag {
681 case ".aapt.srcjar":
682 return []android.Path{a.aaptSrcJar}, nil
683 }
684 return a.Library.OutputFiles(tag)
685}
686
Jiyong Parkf7487312019-10-17 12:54:30 +0900687func (a *AndroidApp) Privileged() bool {
688 return Bool(a.appProperties.Privileged)
689}
690
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700691func (a *AndroidApp) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
692 return ctx.Device() && (ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled())
693}
694
695func (a *AndroidApp) PreventInstall() {
696 a.appProperties.PreventInstall = true
697}
698
699func (a *AndroidApp) HideFromMake() {
700 a.appProperties.HideFromMake = true
701}
702
703func (a *AndroidApp) MarkAsCoverageVariant(coverage bool) {
704 a.appProperties.IsCoverageVariant = coverage
705}
706
707var _ cc.Coverage = (*AndroidApp)(nil)
708
Colin Cross1b16b0e2019-02-12 14:41:32 -0800709// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700710func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700711 module := &AndroidApp{}
712
Sasha Smundak2057f822019-04-16 17:16:58 -0700713 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross66dbc0b2017-12-28 12:23:20 -0800714 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
715
Colin Crossae5caf52018-05-22 11:11:52 -0700716 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700717 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700718
Colin Cross36242852017-06-23 15:06:31 -0700719 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700720 &module.Module.properties,
721 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800722 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700723 &module.Module.protoProperties,
724 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800725 &module.appProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700726 &module.overridableAppProperties,
727 &module.usesLibrary.usesLibraryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700728
Colin Crossa9d8bee2018-10-02 13:59:46 -0700729 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
730 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
731 })
732
Colin Crossa4f08812018-10-02 22:03:40 -0700733 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
734 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800735 android.InitOverridableModule(module, &module.appProperties.Overrides)
Jiyong Park52cd06f2019-11-11 10:14:32 +0900736 android.InitApexModule(module)
Colin Crossa4f08812018-10-02 22:03:40 -0700737
Colin Cross36242852017-06-23 15:06:31 -0700738 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700739}
Colin Crossae5caf52018-05-22 11:11:52 -0700740
741type appTestProperties struct {
742 Instrumentation_for *string
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700743
744 // if specified, the instrumentation target package name in the manifest is overwritten by it.
745 Instrumentation_target_package *string
Colin Crossae5caf52018-05-22 11:11:52 -0700746}
747
748type AndroidTest struct {
749 AndroidApp
750
751 appTestProperties appTestProperties
752
753 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700754
755 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700756 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700757}
758
Jaewoong Jung0949f312019-09-11 10:25:18 -0700759func (a *AndroidTest) InstallInTestcases() bool {
760 return true
761}
762
Colin Crossae5caf52018-05-22 11:11:52 -0700763func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700764 if a.appTestProperties.Instrumentation_target_package != nil {
765 a.additionalAaptFlags = append(a.additionalAaptFlags,
766 "--rename-instrumentation-target-package "+*a.appTestProperties.Instrumentation_target_package)
767 } else if a.appTestProperties.Instrumentation_for != nil {
768 // Check if the instrumentation target package is overridden.
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800769 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
770 if overridden {
771 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
772 }
773 }
Colin Crossae5caf52018-05-22 11:11:52 -0700774 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700775
Jaewoong Jung39982342020-01-14 10:27:18 -0800776 testConfig := tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700777 a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites, a.testProperties.Auto_gen_config)
Jaewoong Jung39982342020-01-14 10:27:18 -0800778 a.testConfig = a.FixTestConfig(ctx, testConfig)
Colin Cross8a497952019-03-05 22:25:09 -0800779 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700780}
781
Jaewoong Jung39982342020-01-14 10:27:18 -0800782func (a *AndroidTest) FixTestConfig(ctx android.ModuleContext, testConfig android.Path) android.Path {
783 if testConfig == nil {
784 return nil
785 }
786
787 fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", "AndroidTest.xml")
788 rule := android.NewRuleBuilder()
789 command := rule.Command().BuiltTool(ctx, "test_config_fixer").Input(testConfig).Output(fixedConfig)
790 fixNeeded := false
791
792 if ctx.ModuleName() != a.installApkName {
793 fixNeeded = true
794 command.FlagWithArg("--test-file-name ", a.installApkName+".apk")
795 }
796
797 if a.overridableAppProperties.Package_name != nil {
798 fixNeeded = true
799 command.FlagWithInput("--manifest ", a.manifestPath).
800 FlagWithArg("--package-name ", *a.overridableAppProperties.Package_name)
801 }
802
803 if fixNeeded {
804 rule.Build(pctx, ctx, "fix_test_config", "fix test config")
805 return fixedConfig
806 }
807 return testConfig
808}
809
Colin Cross303e21f2018-08-07 16:49:25 -0700810func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -0700811 a.AndroidApp.DepsMutator(ctx)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700812}
813
814func (a *AndroidTest) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
815 a.AndroidApp.OverridablePropertiesDepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -0700816 if a.appTestProperties.Instrumentation_for != nil {
817 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
818 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
819 // use instrumentationForTag instead of libTag.
820 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
821 }
Colin Crossae5caf52018-05-22 11:11:52 -0700822}
823
Colin Cross1b16b0e2019-02-12 14:41:32 -0800824// android_test compiles test sources and Android resources into an Android application package `.apk` file and
825// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -0700826func AndroidTestFactory() android.Module {
827 module := &AndroidTest{}
828
Sasha Smundak2057f822019-04-16 17:16:58 -0700829 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross5067db92018-09-17 16:46:35 -0700830
831 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700832 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800833 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700834 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800835 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -0700836
837 module.AddProperties(
838 &module.Module.properties,
839 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800840 &module.Module.dexpreoptProperties,
Colin Crossae5caf52018-05-22 11:11:52 -0700841 &module.Module.protoProperties,
842 &module.aaptProperties,
843 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700844 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800845 &module.overridableAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700846 &module.usesLibrary.usesLibraryProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700847 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700848
Colin Crossa4f08812018-10-02 22:03:40 -0700849 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
850 android.InitDefaultableModule(module)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700851 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossae5caf52018-05-22 11:11:52 -0700852 return module
853}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700854
Colin Cross252fc6f2018-10-04 15:22:03 -0700855type appTestHelperAppProperties struct {
856 // list of compatibility suites (for example "cts", "vts") that the module should be
857 // installed into.
858 Test_suites []string `android:"arch_variant"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700859
860 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
861 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
862 // explicitly.
863 Auto_gen_config *bool
Colin Cross252fc6f2018-10-04 15:22:03 -0700864}
865
866type AndroidTestHelperApp struct {
867 AndroidApp
868
869 appTestHelperAppProperties appTestHelperAppProperties
870}
871
Jaewoong Jung326a9412019-11-21 10:41:00 -0800872func (a *AndroidTestHelperApp) InstallInTestcases() bool {
873 return true
874}
875
Colin Cross1b16b0e2019-02-12 14:41:32 -0800876// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
877// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
878// test.
Colin Cross252fc6f2018-10-04 15:22:03 -0700879func AndroidTestHelperAppFactory() android.Module {
880 module := &AndroidTestHelperApp{}
881
Sasha Smundak2057f822019-04-16 17:16:58 -0700882 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700883
884 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800885 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700886 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800887 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700888
889 module.AddProperties(
890 &module.Module.properties,
891 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800892 &module.Module.dexpreoptProperties,
Colin Cross252fc6f2018-10-04 15:22:03 -0700893 &module.Module.protoProperties,
894 &module.aaptProperties,
895 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800896 &module.appTestHelperAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700897 &module.overridableAppProperties,
898 &module.usesLibrary.usesLibraryProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -0700899
900 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
901 android.InitDefaultableModule(module)
Anton Hansson3d2b6b42020-01-10 15:06:01 +0000902 android.InitApexModule(module)
Colin Cross252fc6f2018-10-04 15:22:03 -0700903 return module
904}
905
Colin Crossbd01e2a2018-10-04 15:21:03 -0700906type AndroidAppCertificate struct {
907 android.ModuleBase
908 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900909 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -0700910}
911
912type AndroidAppCertificateProperties struct {
913 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
914 Certificate *string
915}
916
Colin Cross1b16b0e2019-02-12 14:41:32 -0800917// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
918// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -0700919func AndroidAppCertificateFactory() android.Module {
920 module := &AndroidAppCertificate{}
921 module.AddProperties(&module.properties)
922 android.InitAndroidModule(module)
923 return module
924}
925
Colin Crossbd01e2a2018-10-04 15:21:03 -0700926func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
927 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900928 c.Certificate = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -0800929 Pem: android.PathForModuleSrc(ctx, cert+".x509.pem"),
930 Key: android.PathForModuleSrc(ctx, cert+".pk8"),
Colin Crossbd01e2a2018-10-04 15:21:03 -0700931 }
932}
Jaewoong Jung525443a2019-02-28 15:35:54 -0800933
934type OverrideAndroidApp struct {
935 android.ModuleBase
936 android.OverrideModuleBase
937}
938
939func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
940 // All the overrides happen in the base module.
941 // TODO(jungjw): Check the base module type.
942}
943
944// override_android_app is used to create an android_app module based on another android_app by overriding
945// some of its properties.
946func OverrideAndroidAppModuleFactory() android.Module {
947 m := &OverrideAndroidApp{}
948 m.AddProperties(&overridableAppProperties{})
949
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700950 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800951 android.InitOverrideModule(m)
952 return m
953}
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700954
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700955type OverrideAndroidTest struct {
956 android.ModuleBase
957 android.OverrideModuleBase
958}
959
960func (i *OverrideAndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
961 // All the overrides happen in the base module.
962 // TODO(jungjw): Check the base module type.
963}
964
965// override_android_test is used to create an android_app module based on another android_test by overriding
966// some of its properties.
967func OverrideAndroidTestModuleFactory() android.Module {
968 m := &OverrideAndroidTest{}
969 m.AddProperties(&overridableAppProperties{})
970 m.AddProperties(&appTestProperties{})
971
972 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
973 android.InitOverrideModule(m)
974 return m
975}
976
Roshan Piusb8307962020-04-27 09:42:27 -0700977type OverrideRuntimeResourceOverlay struct {
978 android.ModuleBase
979 android.OverrideModuleBase
980}
981
982func (i *OverrideRuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
983 // All the overrides happen in the base module.
984 // TODO(jungjw): Check the base module type.
985}
986
987// override_runtime_resource_overlay is used to create a module based on another
988// runtime_resource_overlay module by overriding some of its properties.
989func OverrideRuntimeResourceOverlayModuleFactory() android.Module {
990 m := &OverrideRuntimeResourceOverlay{}
991 m.AddProperties(&OverridableRuntimeResourceOverlayProperties{})
992
993 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
994 android.InitOverrideModule(m)
995 return m
996}
997
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700998type AndroidAppImport struct {
999 android.ModuleBase
1000 android.DefaultableModuleBase
1001 prebuilt android.Prebuilt
1002
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001003 properties AndroidAppImportProperties
1004 dpiVariants interface{}
1005 archVariants interface{}
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001006
1007 outputFile android.Path
Colin Cross503c1d02020-01-28 14:00:53 -08001008 certificate Certificate
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001009
1010 dexpreopter
Colin Cross50ddcc42019-05-16 12:28:22 -07001011
1012 usesLibrary usesLibrary
Jaewoong Jung8aae22e2019-07-17 10:21:49 -07001013
Colin Cross70dda7e2019-10-01 22:05:35 -07001014 installPath android.InstallPath
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001015}
1016
1017type AndroidAppImportProperties struct {
1018 // A prebuilt apk to import
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001019 Apk *string
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001020
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001021 // The name of a certificate in the default certificate directory or an android_app_certificate
1022 // module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001023 Certificate *string
1024
1025 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
1026 // be set for presigned modules.
1027 Presigned *bool
1028
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001029 // Sign with the default system dev certificate. Must be used judiciously. Most imported apps
1030 // need to either specify a specific certificate or be presigned.
1031 Default_dev_cert *bool
1032
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001033 // Specifies that this app should be installed to the priv-app directory,
1034 // where the system will grant it additional privileges not available to
1035 // normal apps.
1036 Privileged *bool
1037
1038 // Names of modules to be overridden. Listed modules can only be other binaries
1039 // (in Make or Soong).
1040 // This does not completely prevent installation of the overridden binaries, but if both
1041 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1042 // from PRODUCT_PACKAGES.
1043 Overrides []string
Jaewoong Jung8aae22e2019-07-17 10:21:49 -07001044
1045 // Optional name for the installed app. If unspecified, it is derived from the module name.
1046 Filename *string
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001047}
1048
Martin Stjernholm6d415272020-01-31 17:10:36 +00001049func (a *AndroidAppImport) IsInstallable() bool {
1050 return true
1051}
1052
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001053// Updates properties with variant-specific values.
1054func (a *AndroidAppImport) processVariants(ctx android.LoadHookContext) {
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001055 config := ctx.Config()
1056
1057 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants")
1058 // Try DPI variant matches in the reverse-priority order so that the highest priority match
1059 // overwrites everything else.
1060 // TODO(jungjw): Can we optimize this by making it priority order?
1061 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001062 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001063 }
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001064 if config.ProductAAPTPreferredConfig() != "" {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001065 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001066 }
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001067
1068 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName("Arch")
1069 archType := ctx.Config().Targets[android.Android][0].Arch.ArchType
1070 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001071}
1072
Colin Cross1184b642019-12-30 18:43:07 -08001073func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001074 dst interface{}, variantGroup reflect.Value, variant string) {
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001075 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
1076 if !src.IsValid() {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001077 return
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001078 }
1079
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001080 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
1081 if err != nil {
1082 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1083 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1084 } else {
1085 panic(err)
1086 }
1087 }
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001088}
1089
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001090func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
1091 cert := android.SrcIsModule(String(a.properties.Certificate))
1092 if cert != "" {
1093 ctx.AddDependency(ctx.Module(), certificateTag, cert)
1094 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001095
Paul Duffin250e6192019-06-07 10:44:37 +01001096 a.usesLibrary.deps(ctx, true)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001097}
1098
1099func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
1100 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001101 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
1102 // with them may invalidate pre-existing signature data.
1103 if ctx.InstallInTestcases() && Bool(a.properties.Presigned) {
1104 ctx.Build(pctx, android.BuildParams{
1105 Rule: android.Cp,
1106 Output: outputPath,
1107 Input: inputPath,
1108 })
1109 return
1110 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001111 rule := android.NewRuleBuilder()
1112 rule.Command().
1113 Textf(`if (zipinfo %s 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
Colin Crossee94d6a2019-07-08 17:08:34 -07001114 BuiltTool(ctx, "zip2zip").
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001115 FlagWithInput("-i ", inputPath).
1116 FlagWithOutput("-o ", outputPath).
1117 FlagWithArg("-0 ", "'lib/**/*.so'").
1118 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
1119 rule.Build(pctx, ctx, "uncompress-embedded-jni-libs", "Uncompress embedded JIN libs")
1120}
1121
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001122// Returns whether this module should have the dex file stored uncompressed in the APK.
1123func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
1124 if ctx.Config().UnbundledBuild() {
1125 return false
1126 }
1127
1128 // Uncompress dex in APKs of privileged apps
Jiyong Parkf7487312019-10-17 12:54:30 +09001129 if ctx.Config().UncompressPrivAppDex() && a.Privileged() {
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001130 return true
1131 }
1132
1133 return shouldUncompressDex(ctx, &a.dexpreopter)
1134}
1135
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001136func (a *AndroidAppImport) uncompressDex(
1137 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
1138 rule := android.NewRuleBuilder()
1139 rule.Command().
1140 Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
Colin Crossee94d6a2019-07-08 17:08:34 -07001141 BuiltTool(ctx, "zip2zip").
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001142 FlagWithInput("-i ", inputPath).
1143 FlagWithOutput("-o ", outputPath).
1144 FlagWithArg("-0 ", "'classes*.dex'").
1145 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
1146 rule.Build(pctx, ctx, "uncompress-dex", "Uncompress dex files")
1147}
1148
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001149func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001150 a.generateAndroidBuildActions(ctx)
1151}
1152
Jooyung Han65cd0f02020-03-23 20:21:11 +09001153func (a *AndroidAppImport) InstallApkName() string {
1154 return a.BaseModuleName()
1155}
1156
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001157func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001158 numCertPropsSet := 0
1159 if String(a.properties.Certificate) != "" {
1160 numCertPropsSet++
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001161 }
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001162 if Bool(a.properties.Presigned) {
1163 numCertPropsSet++
1164 }
1165 if Bool(a.properties.Default_dev_cert) {
1166 numCertPropsSet++
1167 }
1168 if numCertPropsSet != 1 {
1169 ctx.ModuleErrorf("One and only one of certficate, presigned, and default_dev_cert properties must be set")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001170 }
1171
Peter Collingbournead84f972019-12-17 16:46:18 -08001172 _, certificates := collectAppDeps(ctx, false)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001173
1174 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001175 // TODO: LOCAL_PACKAGE_SPLITS
1176
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001177 srcApk := a.prebuilt.SingleSourcePath(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -07001178
1179 if a.usesLibrary.enforceUsesLibraries() {
1180 srcApk = a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk)
1181 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001182
1183 // TODO: Install or embed JNI libraries
1184
1185 // Uncompress JNI libraries in the apk
1186 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
1187 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
1188
Kyeongkab.Namc4997142019-11-22 11:38:16 +09001189 var installDir android.InstallPath
1190 if Bool(a.properties.Privileged) {
1191 installDir = android.PathForModuleInstall(ctx, "priv-app", a.BaseModuleName())
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001192 } else if ctx.InstallInTestcases() {
1193 installDir = android.PathForModuleInstall(ctx, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch())
Kyeongkab.Namc4997142019-11-22 11:38:16 +09001194 } else {
1195 installDir = android.PathForModuleInstall(ctx, "app", a.BaseModuleName())
1196 }
1197
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001198 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001199 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001200 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -07001201
1202 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
1203 a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
1204 a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
1205 a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
1206
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001207 dexOutput := a.dexpreopter.dexpreopt(ctx, jnisUncompressed)
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001208 if a.dexpreopter.uncompressedDex {
1209 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
1210 a.uncompressDex(ctx, dexOutput, dexUncompressed.OutputPath)
1211 dexOutput = dexUncompressed
1212 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001213
Jooyung Han65cd0f02020-03-23 20:21:11 +09001214 apkFilename := proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk")
1215
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001216 // Sign or align the package
1217 // TODO: Handle EXTERNAL
1218 if !Bool(a.properties.Presigned) {
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001219 // If the certificate property is empty at this point, default_dev_cert must be set to true.
1220 // Which makes processMainCert's behavior for the empty cert string WAI.
1221 certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001222 if len(certificates) != 1 {
1223 ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates)
1224 }
Colin Cross503c1d02020-01-28 14:00:53 -08001225 a.certificate = certificates[0]
Jooyung Han65cd0f02020-03-23 20:21:11 +09001226 signed := android.PathForModuleOut(ctx, "signed", apkFilename)
Songchun Fan688de9a2020-03-24 20:32:24 -07001227 SignAppPackage(ctx, signed, dexOutput, certificates, nil)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001228 a.outputFile = signed
1229 } else {
Jooyung Han65cd0f02020-03-23 20:21:11 +09001230 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001231 TransformZipAlign(ctx, alignedApk, dexOutput)
1232 a.outputFile = alignedApk
Colin Cross503c1d02020-01-28 14:00:53 -08001233 a.certificate = presignedCertificate
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001234 }
1235
1236 // TODO: Optionally compress the output apk.
1237
Jooyung Han65cd0f02020-03-23 20:21:11 +09001238 a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001239
1240 // TODO: androidmk converter jni libs
1241}
1242
1243func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
1244 return &a.prebuilt
1245}
1246
1247func (a *AndroidAppImport) Name() string {
1248 return a.prebuilt.Name(a.ModuleBase.Name())
1249}
1250
Dario Frenicde2a032019-10-27 00:29:22 +01001251func (a *AndroidAppImport) OutputFile() android.Path {
1252 return a.outputFile
1253}
1254
Jiyong Park618922e2020-01-08 13:35:43 +09001255func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
1256 return nil
1257}
1258
Colin Cross503c1d02020-01-28 14:00:53 -08001259func (a *AndroidAppImport) Certificate() Certificate {
1260 return a.certificate
1261}
1262
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001263var dpiVariantGroupType reflect.Type
1264var archVariantGroupType reflect.Type
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001265
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001266func initAndroidAppImportVariantGroupTypes() {
1267 dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants")
1268
1269 archNames := make([]string, len(android.ArchTypeList()))
1270 for i, archType := range android.ArchTypeList() {
1271 archNames[i] = archType.Name
1272 }
1273 archVariantGroupType = createVariantGroupType(archNames, "Arch")
1274}
1275
1276// Populates all variant struct properties at creation time.
1277func (a *AndroidAppImport) populateAllVariantStructs() {
1278 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
1279 a.AddProperties(a.dpiVariants)
1280
1281 a.archVariants = reflect.New(archVariantGroupType).Interface()
1282 a.AddProperties(a.archVariants)
1283}
1284
Jiyong Parkf7487312019-10-17 12:54:30 +09001285func (a *AndroidAppImport) Privileged() bool {
1286 return Bool(a.properties.Privileged)
1287}
1288
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001289func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
1290 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
1291
1292 variantFields := make([]reflect.StructField, len(variants))
1293 for i, variant := range variants {
1294 variantFields[i] = reflect.StructField{
1295 Name: proptools.FieldNameForProperty(variant),
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001296 Type: props,
1297 }
1298 }
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001299
1300 variantGroupStruct := reflect.StructOf(variantFields)
1301 return reflect.StructOf([]reflect.StructField{
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001302 {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001303 Name: variantGroupName,
1304 Type: variantGroupStruct,
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001305 },
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001306 })
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001307}
1308
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001309// android_app_import imports a prebuilt apk with additional processing specified in the module.
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001310// DPI-specific apk source files can be specified using dpi_variants. Example:
1311//
1312// android_app_import {
1313// name: "example_import",
1314// apk: "prebuilts/example.apk",
1315// dpi_variants: {
1316// mdpi: {
1317// apk: "prebuilts/example_mdpi.apk",
1318// },
1319// xhdpi: {
1320// apk: "prebuilts/example_xhdpi.apk",
1321// },
1322// },
1323// certificate: "PRESIGNED",
1324// }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001325func AndroidAppImportFactory() android.Module {
1326 module := &AndroidAppImport{}
1327 module.AddProperties(&module.properties)
1328 module.AddProperties(&module.dexpreoptProperties)
Colin Cross50ddcc42019-05-16 12:28:22 -07001329 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001330 module.populateAllVariantStructs()
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001331 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001332 module.processVariants(ctx)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001333 })
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001334
1335 InitJavaModule(module, android.DeviceSupported)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001336 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001337
1338 return module
1339}
Colin Cross50ddcc42019-05-16 12:28:22 -07001340
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001341type AndroidTestImport struct {
1342 AndroidAppImport
1343
1344 testProperties testProperties
1345
1346 data android.Paths
1347}
1348
1349func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1350 a.generateAndroidBuildActions(ctx)
1351
1352 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
1353}
1354
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001355func (a *AndroidTestImport) InstallInTestcases() bool {
1356 return true
1357}
1358
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001359// android_test_import imports a prebuilt test apk with additional processing specified in the
1360// module. DPI or arch variant configurations can be made as with android_app_import.
1361func AndroidTestImportFactory() android.Module {
1362 module := &AndroidTestImport{}
1363 module.AddProperties(&module.properties)
1364 module.AddProperties(&module.dexpreoptProperties)
1365 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
1366 module.AddProperties(&module.testProperties)
1367 module.populateAllVariantStructs()
1368 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
1369 module.processVariants(ctx)
1370 })
1371
1372 InitJavaModule(module, android.DeviceSupported)
1373 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
1374
1375 return module
1376}
1377
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001378type RuntimeResourceOverlay struct {
1379 android.ModuleBase
1380 android.DefaultableModuleBase
Roshan Piusb8307962020-04-27 09:42:27 -07001381 android.OverridableModuleBase
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001382 aapt
1383
Roshan Piusb8307962020-04-27 09:42:27 -07001384 properties RuntimeResourceOverlayProperties
1385 overridableProperties OverridableRuntimeResourceOverlayProperties
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001386
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08001387 certificate Certificate
1388
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001389 outputFile android.Path
1390 installDir android.InstallPath
1391}
1392
1393type RuntimeResourceOverlayProperties struct {
1394 // the name of a certificate in the default certificate directory or an android_app_certificate
1395 // module name in the form ":module".
1396 Certificate *string
1397
1398 // optional theme name. If specified, the overlay package will be applied
1399 // only when the ro.boot.vendor.overlay.theme system property is set to the same value.
1400 Theme *string
1401
1402 // if not blank, set to the version of the sdk to compile against.
1403 // Defaults to compiling against the current platform.
1404 Sdk_version *string
1405
1406 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
1407 // Defaults to sdk_version if not set.
1408 Min_sdk_version *string
Jaewoong Jungca095d72020-04-09 16:15:30 -07001409
1410 // list of android_library modules whose resources are extracted and linked against statically
1411 Static_libs []string
1412
1413 // list of android_app modules whose resources are extracted and linked against
1414 Resource_libs []string
Jaewoong Jungbfc6ac02020-04-24 15:22:40 -07001415
1416 // Names of modules to be overridden. Listed modules can only be other overlays
1417 // (in Make or Soong).
1418 // This does not completely prevent installation of the overridden overlays, but if both
1419 // overlays would be installed by default (in PRODUCT_PACKAGES) the other overlay will be removed
1420 // from PRODUCT_PACKAGES.
1421 Overrides []string
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001422}
1423
1424func (r *RuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
1425 sdkDep := decodeSdkDep(ctx, sdkContext(r))
1426 if sdkDep.hasFrameworkLibs() {
1427 r.aapt.deps(ctx, sdkDep)
1428 }
1429
1430 cert := android.SrcIsModule(String(r.properties.Certificate))
1431 if cert != "" {
1432 ctx.AddDependency(ctx.Module(), certificateTag, cert)
1433 }
Jaewoong Jungca095d72020-04-09 16:15:30 -07001434
1435 ctx.AddVariationDependencies(nil, staticLibTag, r.properties.Static_libs...)
1436 ctx.AddVariationDependencies(nil, libTag, r.properties.Resource_libs...)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001437}
1438
1439func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1440 // Compile and link resources
1441 r.aapt.hasNoCode = true
Jaewoong Jungf0f747c2020-01-24 10:30:02 -08001442 // Do not remove resources without default values nor dedupe resource configurations with the same value
Roshan Piusb8307962020-04-27 09:42:27 -07001443 aaptLinkFlags := []string{"--no-resource-deduping", "--no-resource-removal"}
1444 // Allow the override of "package name" and "overlay target package name"
1445 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1446 if overridden || r.overridableProperties.Package_name != nil {
1447 // The product override variable has a priority over the package_name property.
1448 if !overridden {
1449 manifestPackageName = *r.overridableProperties.Package_name
1450 }
1451 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
1452 }
1453 if r.overridableProperties.Target_package_name != nil {
1454 aaptLinkFlags = append(aaptLinkFlags,
1455 "--rename-overlay-target-package "+*r.overridableProperties.Target_package_name)
1456 }
1457 r.aapt.buildActions(ctx, r, aaptLinkFlags...)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001458
1459 // Sign the built package
1460 _, certificates := collectAppDeps(ctx, false)
1461 certificates = processMainCert(r.ModuleBase, String(r.properties.Certificate), certificates, ctx)
1462 signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
Songchun Fan688de9a2020-03-24 20:32:24 -07001463 SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil)
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08001464 r.certificate = certificates[0]
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001465
1466 r.outputFile = signed
1467 r.installDir = android.PathForModuleInstall(ctx, "overlay", String(r.properties.Theme))
1468 ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile)
1469}
1470
Jiyong Park6a927c42020-01-21 02:03:43 +09001471func (r *RuntimeResourceOverlay) sdkVersion() sdkSpec {
1472 return sdkSpecFrom(String(r.properties.Sdk_version))
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001473}
1474
1475func (r *RuntimeResourceOverlay) systemModules() string {
1476 return ""
1477}
1478
Jiyong Park6a927c42020-01-21 02:03:43 +09001479func (r *RuntimeResourceOverlay) minSdkVersion() sdkSpec {
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001480 if r.properties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +09001481 return sdkSpecFrom(*r.properties.Min_sdk_version)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001482 }
1483 return r.sdkVersion()
1484}
1485
Jiyong Park6a927c42020-01-21 02:03:43 +09001486func (r *RuntimeResourceOverlay) targetSdkVersion() sdkSpec {
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001487 return r.sdkVersion()
1488}
1489
1490// runtime_resource_overlay generates a resource-only apk file that can overlay application and
1491// system resources at run time.
1492func RuntimeResourceOverlayFactory() android.Module {
1493 module := &RuntimeResourceOverlay{}
1494 module.AddProperties(
1495 &module.properties,
Roshan Piusb8307962020-04-27 09:42:27 -07001496 &module.aaptProperties,
1497 &module.overridableProperties)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001498
Roshan Piusb8307962020-04-27 09:42:27 -07001499 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1500 android.InitDefaultableModule(module)
1501 android.InitOverridableModule(module, &module.properties.Overrides)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001502 return module
1503}
1504
Colin Cross50ddcc42019-05-16 12:28:22 -07001505type UsesLibraryProperties struct {
1506 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file.
1507 Uses_libs []string
1508
1509 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file with
1510 // required=false.
1511 Optional_uses_libs []string
1512
1513 // If true, the list of uses_libs and optional_uses_libs modules must match the AndroidManifest.xml file. Defaults
1514 // to true if either uses_libs or optional_uses_libs is set. Will unconditionally default to true in the future.
1515 Enforce_uses_libs *bool
1516}
1517
1518// usesLibrary provides properties and helper functions for AndroidApp and AndroidAppImport to verify that the
1519// <uses-library> tags that end up in the manifest of an APK match the ones known to the build system through the
1520// uses_libs and optional_uses_libs properties. The build system's values are used by dexpreopt to preopt apps
1521// with knowledge of their shared libraries.
1522type usesLibrary struct {
1523 usesLibraryProperties UsesLibraryProperties
1524}
1525
Paul Duffin250e6192019-06-07 10:44:37 +01001526func (u *usesLibrary) deps(ctx android.BottomUpMutatorContext, hasFrameworkLibs bool) {
Colin Cross3245b2c2019-06-07 13:18:09 -07001527 if !ctx.Config().UnbundledBuild() {
1528 ctx.AddVariationDependencies(nil, usesLibTag, u.usesLibraryProperties.Uses_libs...)
1529 ctx.AddVariationDependencies(nil, usesLibTag, u.presentOptionalUsesLibs(ctx)...)
Paul Duffin250e6192019-06-07 10:44:37 +01001530 // Only add these extra dependencies if the module depends on framework libs. This avoids
1531 // creating a cyclic dependency:
1532 // e.g. framework-res -> org.apache.http.legacy -> ... -> framework-res.
1533 if hasFrameworkLibs {
Colin Cross3245b2c2019-06-07 13:18:09 -07001534 // dexpreopt/dexpreopt.go needs the paths to the dex jars of these libraries in case construct_context.sh needs
1535 // to pass them to dex2oat. Add them as a dependency so we can determine the path to the dex jar of each
1536 // library to dexpreopt.
1537 ctx.AddVariationDependencies(nil, usesLibTag,
1538 "org.apache.http.legacy",
1539 "android.hidl.base-V1.0-java",
1540 "android.hidl.manager-V1.0-java")
1541 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001542 }
1543}
1544
1545// presentOptionalUsesLibs returns optional_uses_libs after filtering out MissingUsesLibraries, which don't exist in the
1546// build.
1547func (u *usesLibrary) presentOptionalUsesLibs(ctx android.BaseModuleContext) []string {
1548 optionalUsesLibs, _ := android.FilterList(u.usesLibraryProperties.Optional_uses_libs, ctx.Config().MissingUsesLibraries())
1549 return optionalUsesLibs
1550}
1551
1552// usesLibraryPaths returns a map of module names of shared library dependencies to the paths to their dex jars.
1553func (u *usesLibrary) usesLibraryPaths(ctx android.ModuleContext) map[string]android.Path {
1554 usesLibPaths := make(map[string]android.Path)
1555
1556 if !ctx.Config().UnbundledBuild() {
1557 ctx.VisitDirectDepsWithTag(usesLibTag, func(m android.Module) {
1558 if lib, ok := m.(Dependency); ok {
1559 if dexJar := lib.DexJar(); dexJar != nil {
1560 usesLibPaths[ctx.OtherModuleName(m)] = dexJar
1561 } else {
1562 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must produce a dex jar, does it have installable: true?",
1563 ctx.OtherModuleName(m))
1564 }
1565 } else if ctx.Config().AllowMissingDependencies() {
1566 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(m)})
1567 } else {
1568 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must be a java library",
1569 ctx.OtherModuleName(m))
1570 }
1571 })
1572 }
1573
1574 return usesLibPaths
1575}
1576
1577// enforceUsesLibraries returns true of <uses-library> tags should be checked against uses_libs and optional_uses_libs
1578// properties. Defaults to true if either of uses_libs or optional_uses_libs is specified. Will default to true
1579// unconditionally in the future.
1580func (u *usesLibrary) enforceUsesLibraries() bool {
1581 defaultEnforceUsesLibs := len(u.usesLibraryProperties.Uses_libs) > 0 ||
1582 len(u.usesLibraryProperties.Optional_uses_libs) > 0
1583 return BoolDefault(u.usesLibraryProperties.Enforce_uses_libs, defaultEnforceUsesLibs)
1584}
1585
1586// verifyUsesLibrariesManifest checks the <uses-library> tags in an AndroidManifest.xml against the ones specified
1587// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the manifest.
1588func (u *usesLibrary) verifyUsesLibrariesManifest(ctx android.ModuleContext, manifest android.Path) android.Path {
1589 outputFile := android.PathForModuleOut(ctx, "manifest_check", "AndroidManifest.xml")
1590
1591 rule := android.NewRuleBuilder()
Colin Crossee94d6a2019-07-08 17:08:34 -07001592 cmd := rule.Command().BuiltTool(ctx, "manifest_check").
Colin Cross50ddcc42019-05-16 12:28:22 -07001593 Flag("--enforce-uses-libraries").
1594 Input(manifest).
1595 FlagWithOutput("-o ", outputFile)
1596
1597 for _, lib := range u.usesLibraryProperties.Uses_libs {
1598 cmd.FlagWithArg("--uses-library ", lib)
1599 }
1600
1601 for _, lib := range u.usesLibraryProperties.Optional_uses_libs {
1602 cmd.FlagWithArg("--optional-uses-library ", lib)
1603 }
1604
1605 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1606
1607 return outputFile
1608}
1609
1610// verifyUsesLibrariesAPK checks the <uses-library> tags in the manifest of an APK against the ones specified
1611// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the APK.
1612func (u *usesLibrary) verifyUsesLibrariesAPK(ctx android.ModuleContext, apk android.Path) android.Path {
1613 outputFile := android.PathForModuleOut(ctx, "verify_uses_libraries", apk.Base())
1614
1615 rule := android.NewRuleBuilder()
1616 aapt := ctx.Config().HostToolPath(ctx, "aapt")
1617 rule.Command().
1618 Textf("aapt_binary=%s", aapt.String()).Implicit(aapt).
1619 Textf(`uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Uses_libs, " ")).
1620 Textf(`optional_uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Optional_uses_libs, " ")).
1621 Tool(android.PathForSource(ctx, "build/make/core/verify_uses_libraries.sh")).Input(apk)
1622 rule.Command().Text("cp -f").Input(apk).Output(outputFile)
1623
1624 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1625
1626 return outputFile
1627}