blob: dfe9809ee7b8c34a428fdcedb37143e59bd3574d [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)
48 ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory)
49 ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -080050 ctx.RegisterModuleType("runtime_resource_overlay", RuntimeResourceOverlayFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000051}
52
Colin Cross30e076a2015-04-13 13:58:27 -070053// AndroidManifest.xml merging
54// package splits
55
Colin Crossfabb6082018-02-20 17:22:23 -080056type appProperties struct {
Colin Crossbd01e2a2018-10-04 15:21:03 -070057 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
Colin Cross7d5136f2015-05-11 13:39:40 -070058 Additional_certificates []string
59
60 // If set, create package-export.apk, which other packages can
61 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
Nan Zhangea568a42017-11-08 21:20:04 -080062 Export_package_resources *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070063
Colin Cross16056062017-12-13 22:46:28 -080064 // Specifies that this app should be installed to the priv-app directory,
65 // where the system will grant it additional privileges not available to
66 // normal apps.
67 Privileged *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070068
69 // list of resource labels to generate individual resource packages
70 Package_splits []string
Jason Monkd4122be2018-08-10 09:33:36 -040071
72 // Names of modules to be overridden. Listed modules can only be other binaries
73 // (in Make or Soong).
74 // This does not completely prevent installation of the overridden binaries, but if both
75 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
76 // from PRODUCT_PACKAGES.
77 Overrides []string
Colin Crossa4f08812018-10-02 22:03:40 -070078
79 // list of native libraries that will be provided in or alongside the resulting jar
80 Jni_libs []string `android:"arch_variant"`
81
Colin Crossee87c602020-02-19 16:57:15 -080082 // if true, allow JNI libraries that link against platform APIs even if this module sets
83 // sdk_version.
84 Jni_uses_platform_apis *bool
85
Jaewoong Jungbc625cd2019-05-06 15:48:44 -070086 // STL library to use for JNI libraries.
87 Stl *string `android:"arch_variant"`
88
Colin Crosse4246ab2019-02-05 21:55:21 -080089 // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest
90 // 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 +090091 // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to true for
92 // android_app modules that are embedded to APEXes, defaults to false for other module types where the native
93 // libraries are generally preinstalled outside the APK.
Colin Crosse4246ab2019-02-05 21:55:21 -080094 Use_embedded_native_libs *bool
Colin Cross46abdad2019-02-07 13:07:08 -080095
96 // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that
97 // they are used from inside the APK at runtime.
98 Use_embedded_dex *bool
Colin Cross47fa9d32019-03-26 10:51:39 -070099
100 // Forces native libraries to always be packaged into the APK,
101 // Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed.
102 // True for android_test* modules.
103 AlwaysPackageNativeLibs bool `blueprint:"mutated"`
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700104
105 // If set, find and merge all NOTICE files that this module and its dependencies have and store
106 // it in the APK as an asset.
107 Embed_notices *bool
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700108
109 // cc.Coverage related properties
110 PreventInstall bool `blueprint:"mutated"`
111 HideFromMake bool `blueprint:"mutated"`
112 IsCoverageVariant bool `blueprint:"mutated"`
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100113
114 // Whether this app is considered mainline updatable or not. When set to true, this will enforce
Artur Satayevf40fc852020-04-16 13:43:02 +0100115 // additional rules to make sure an app can safely be updated. Default is false.
116 // Prefer using other specific properties if build behaviour must be changed; avoid using this
117 // flag for anything but neverallow rules (unless the behaviour change is invisible to owners).
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100118 Updatable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700119}
120
Jaewoong Jung525443a2019-02-28 15:35:54 -0800121// android_app properties that can be overridden by override_android_app
122type overridableAppProperties struct {
123 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
124 // or an android_app_certificate module name in the form ":module".
125 Certificate *string
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700126
127 // the package name of this app. The package name in the manifest file is used if one was not given.
128 Package_name *string
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800129
130 // the logging parent of this app.
131 Logging_parent *string
Jaewoong Jung525443a2019-02-28 15:35:54 -0800132}
133
Colin Cross30e076a2015-04-13 13:58:27 -0700134type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -0700135 Library
136 aapt
Jaewoong Jung525443a2019-02-28 15:35:54 -0800137 android.OverridableModuleBase
Colin Crossa97c5d32018-03-28 14:58:31 -0700138
Colin Cross50ddcc42019-05-16 12:28:22 -0700139 usesLibrary usesLibrary
140
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900141 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -0700142
Colin Crossfabb6082018-02-20 17:22:23 -0800143 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -0700144
Jaewoong Jung525443a2019-02-28 15:35:54 -0800145 overridableAppProperties overridableAppProperties
146
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700147 installJniLibs []jniLib
148 jniCoverageOutputs android.Paths
Colin Crossf6237212018-10-29 23:14:58 -0700149
150 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800151
152 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
153 installApkName string
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800154
Colin Cross70dda7e2019-10-01 22:05:35 -0700155 installDir android.InstallPath
Jaewoong Jung0949f312019-09-11 10:25:18 -0700156
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700157 onDeviceDir string
158
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800159 additionalAaptFlags []string
Jaewoong Jung98772792019-07-01 17:15:13 -0700160
161 noticeOutputs android.NoticeOutputs
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900162
163 overriddenManifestPackageName string
Colin Crosse1731a52017-12-14 11:22:55 -0800164}
165
Martin Stjernholm6d415272020-01-31 17:10:36 +0000166func (a *AndroidApp) IsInstallable() bool {
167 return Bool(a.properties.Installable)
168}
169
Colin Cross89c31582018-04-30 15:55:11 -0700170func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
171 return nil
172}
173
Colin Cross66f78822018-05-02 12:58:28 -0700174func (a *AndroidApp) ExportedStaticPackages() android.Paths {
175 return nil
176}
177
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900178func (a *AndroidApp) OutputFile() android.Path {
179 return a.outputFile
180}
181
Colin Cross503c1d02020-01-28 14:00:53 -0800182func (a *AndroidApp) Certificate() Certificate {
183 return a.certificate
184}
185
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700186func (a *AndroidApp) JniCoverageOutputs() android.Paths {
187 return a.jniCoverageOutputs
188}
189
Colin Crossa97c5d32018-03-28 14:58:31 -0700190var _ AndroidLibraryDependency = (*AndroidApp)(nil)
191
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900192type Certificate struct {
Colin Cross503c1d02020-01-28 14:00:53 -0800193 Pem, Key android.Path
194 presigned bool
195}
196
197var presignedCertificate = Certificate{presigned: true}
198
199func (c Certificate) AndroidMkString() string {
200 if c.presigned {
201 return "PRESIGNED"
202 } else {
203 return c.Pem.String()
204 }
Colin Cross30e076a2015-04-13 13:58:27 -0700205}
206
Colin Cross46c9b8b2017-06-22 16:51:17 -0700207func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
208 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700209
Jiyong Park6a927c42020-01-21 02:03:43 +0900210 if String(a.appProperties.Stl) == "c++_shared" && !a.sdkVersion().specified() {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700211 ctx.PropertyErrorf("stl", "sdk_version must be set in order to use c++_shared")
212 }
213
Paul Duffin250e6192019-06-07 10:44:37 +0100214 sdkDep := decodeSdkDep(ctx, sdkContext(a))
215 if sdkDep.hasFrameworkLibs() {
216 a.aapt.deps(ctx, sdkDep)
Colin Cross30e076a2015-04-13 13:58:27 -0700217 }
Colin Crossa4f08812018-10-02 22:03:40 -0700218
Peter Collingbournead84f972019-12-17 16:46:18 -0800219 tag := &jniDependencyTag{}
Colin Crossa4f08812018-10-02 22:03:40 -0700220 for _, jniTarget := range ctx.MultiTargets() {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700221 variation := append(jniTarget.Variations(),
222 blueprint.Variation{Mutator: "link", Variation: "shared"})
Colin Crossc511bc52020-04-07 16:50:32 +0000223
224 // If the app builds against an Android SDK use the SDK variant of JNI dependencies
225 // unless jni_uses_platform_apis is set.
226 if a.sdkVersion().specified() && a.sdkVersion().kind != sdkCorePlatform &&
227 !Bool(a.appProperties.Jni_uses_platform_apis) {
228 variation = append(variation, blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
229 }
Colin Crossa4f08812018-10-02 22:03:40 -0700230 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
231 }
Colin Cross50ddcc42019-05-16 12:28:22 -0700232
Paul Duffin250e6192019-06-07 10:44:37 +0100233 a.usesLibrary.deps(ctx, sdkDep.hasFrameworkLibs())
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700234}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700235
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700236func (a *AndroidApp) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800237 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700238 if cert != "" {
239 ctx.AddDependency(ctx.Module(), certificateTag, cert)
240 }
241
242 for _, cert := range a.appProperties.Additional_certificates {
243 cert = android.SrcIsModule(cert)
244 if cert != "" {
245 ctx.AddDependency(ctx.Module(), certificateTag, cert)
246 } else {
247 ctx.PropertyErrorf("additional_certificates",
248 `must be names of android_app_certificate modules in the form ":module"`)
249 }
250 }
Colin Cross30e076a2015-04-13 13:58:27 -0700251}
252
Jeongik Cha538c0d02019-07-11 15:54:27 +0900253func (a *AndroidTestHelperApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
254 a.generateAndroidBuildActions(ctx)
255}
256
Colin Cross46c9b8b2017-06-22 16:51:17 -0700257func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100258 a.checkAppSdkVersions(ctx)
Colin Crossae5caf52018-05-22 11:11:52 -0700259 a.generateAndroidBuildActions(ctx)
260}
261
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100262func (a *AndroidApp) checkAppSdkVersions(ctx android.ModuleContext) {
263 if Bool(a.appProperties.Updatable) {
264 if !a.sdkVersion().stable() {
265 ctx.PropertyErrorf("sdk_version", "Updatable apps must use stable SDKs, found %v", a.sdkVersion())
266 }
Artur Satayevf40fc852020-04-16 13:43:02 +0100267 if String(a.deviceProperties.Min_sdk_version) == "" {
268 ctx.PropertyErrorf("updatable", "updatable apps must set min_sdk_version.")
269 }
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100270 }
271
272 a.checkPlatformAPI(ctx)
273 a.checkSdkVersions(ctx)
274}
275
Sasha Smundak6ad77252019-05-01 13:16:22 -0700276// Returns true if the native libraries should be stored in the APK uncompressed and the
Colin Crosse4246ab2019-02-05 21:55:21 -0800277// extractNativeLibs application flag should be set to false in the manifest.
Sasha Smundak6ad77252019-05-01 13:16:22 -0700278func (a *AndroidApp) useEmbeddedNativeLibs(ctx android.ModuleContext) bool {
Jiyong Park6a927c42020-01-21 02:03:43 +0900279 minSdkVersion, err := a.minSdkVersion().effectiveVersion(ctx)
Colin Crosse4246ab2019-02-05 21:55:21 -0800280 if err != nil {
281 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
282 }
283
Jiyong Park52cd06f2019-11-11 10:14:32 +0900284 return (minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)) ||
285 !a.IsForPlatform()
Colin Crosse4246ab2019-02-05 21:55:21 -0800286}
287
Colin Cross43f08db2018-11-12 10:13:39 -0800288// Returns whether this module should have the dex file stored uncompressed in the APK.
289func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800290 if Bool(a.appProperties.Use_embedded_dex) {
291 return true
292 }
293
Colin Cross53a87f52019-06-25 13:35:30 -0700294 // Uncompress dex in APKs of privileged apps (even for unbundled builds, they may
295 // be preinstalled as prebuilts).
Jiyong Parkf7487312019-10-17 12:54:30 +0900296 if ctx.Config().UncompressPrivAppDex() && a.Privileged() {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000297 return true
298 }
299
Colin Cross53a87f52019-06-25 13:35:30 -0700300 if ctx.Config().UnbundledBuild() {
301 return false
302 }
303
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700304 return shouldUncompressDex(ctx, &a.dexpreopter)
Colin Cross5a0dcd52018-10-05 14:20:06 -0700305}
306
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700307func (a *AndroidApp) shouldEmbedJnis(ctx android.BaseModuleContext) bool {
308 return ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) ||
Jiyong Park52cd06f2019-11-11 10:14:32 +0900309 !a.IsForPlatform() || a.appProperties.AlwaysPackageNativeLibs
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700310}
311
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900312func (a *AndroidApp) OverriddenManifestPackageName() string {
313 return a.overriddenManifestPackageName
314}
315
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800316func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
David Brazdild25060a2019-02-18 18:24:16 +0000317 a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis)
318
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700319 // Ask manifest_fixer to add or update the application element indicating this app has no code.
320 a.aapt.hasNoCode = !a.hasCode(ctx)
321
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800322 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800323
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800324 // 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 -0800325 hasProduct := android.PrefixInList(a.aaptProperties.Aaptflags, "--product")
Colin Crosse78dcd32018-04-19 15:25:19 -0700326 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800327 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700328 }
329
Dan Willemsen72be5902018-10-24 20:24:57 -0700330 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
331 // Product AAPT config
332 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800333 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700334 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700335
Dan Willemsen72be5902018-10-24 20:24:57 -0700336 // Product AAPT preferred config
337 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800338 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700339 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700340 }
341
Jiyong Park7f67f482019-01-05 12:57:48 +0900342 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700343 if overridden || a.overridableAppProperties.Package_name != nil {
344 // The product override variable has a priority over the package_name property.
345 if !overridden {
346 manifestPackageName = *a.overridableAppProperties.Package_name
347 }
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800348 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900349 a.overriddenManifestPackageName = manifestPackageName
Jiyong Park7f67f482019-01-05 12:57:48 +0900350 }
351
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800352 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
353
Colin Crosse560c4a2019-03-19 16:03:11 -0700354 a.aapt.splitNames = a.appProperties.Package_splits
Colin Cross50ddcc42019-05-16 12:28:22 -0700355 a.aapt.sdkLibraries = a.exportedSdkLibs
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800356 a.aapt.LoggingParent = String(a.overridableAppProperties.Logging_parent)
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800357 a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700358
Colin Cross46c9b8b2017-06-22 16:51:17 -0700359 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700360 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800361}
Colin Cross30e076a2015-04-13 13:58:27 -0700362
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800363func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700364 var staticLibProguardFlagFiles android.Paths
365 ctx.VisitDirectDeps(func(m android.Module) {
366 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
367 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
368 }
369 })
370
371 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
372
373 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
374 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800375}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800376
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800377func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross43f08db2018-11-12 10:13:39 -0800378
379 var installDir string
380 if ctx.ModuleName() == "framework-res" {
381 // framework-res.apk is installed as system/framework/framework-res.apk
382 installDir = "framework"
Jiyong Parkf7487312019-10-17 12:54:30 +0900383 } else if a.Privileged() {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800384 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800385 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800386 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800387 }
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800388 a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000389 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -0700390
391 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
392 a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
393 a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
394 a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
395 a.dexpreopter.manifestFile = a.mergedManifestFile
396
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000397 a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex
Colin Cross5a0dcd52018-10-05 14:20:06 -0700398
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800399 if ctx.ModuleName() != "framework-res" {
400 a.Module.compile(ctx, a.aaptSrcJar)
401 }
Colin Cross30e076a2015-04-13 13:58:27 -0700402
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800403 return a.maybeStrippedDexJarFile
404}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800405
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800406func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700407 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700408 if len(jniLibs) > 0 {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700409 if a.shouldEmbedJnis(ctx) {
Colin Crossa4f08812018-10-02 22:03:40 -0700410 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Sasha Smundak6ad77252019-05-01 13:16:22 -0700411 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.useEmbeddedNativeLibs(ctx))
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700412 for _, jni := range jniLibs {
413 if jni.coverageFile.Valid() {
Jaewoong Jung46984ee2020-04-07 13:07:55 -0700414 // Only collect coverage for the first target arch if this is a multilib target.
415 // TODO(jungjw): Ideally, we want to collect both reports, but that would cause coverage
416 // data file path collisions since the current coverage file path format doesn't contain
417 // arch-related strings. This is fine for now though; the code coverage team doesn't use
418 // multi-arch targets such as test_suite_* for coverage collections yet.
419 //
420 // Work with the team to come up with a new format that handles multilib modules properly
421 // and change this.
422 if len(ctx.Config().Targets[android.Android]) == 1 ||
423 ctx.Config().Targets[android.Android][0].Arch.ArchType == jni.target.Arch.ArchType {
424 a.jniCoverageOutputs = append(a.jniCoverageOutputs, jni.coverageFile.Path())
425 }
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700426 }
427 }
Colin Crossa4f08812018-10-02 22:03:40 -0700428 } else {
429 a.installJniLibs = jniLibs
430 }
431 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800432 return jniJarFile
433}
Colin Crossa4f08812018-10-02 22:03:40 -0700434
Jaewoong Jung0949f312019-09-11 10:25:18 -0700435func (a *AndroidApp) noticeBuildActions(ctx android.ModuleContext) {
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700436 // Collect NOTICE files from all dependencies.
437 seenModules := make(map[android.Module]bool)
438 noticePathSet := make(map[android.Path]bool)
439
440 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
441 // Have we already seen this?
442 if _, ok := seenModules[child]; ok {
443 return false
444 }
445 seenModules[child] = true
446
447 // Skip host modules.
448 if child.Target().Os.Class == android.Host || child.Target().Os.Class == android.HostCross {
449 return false
450 }
451
Bob Badoura75b0572020-02-18 20:21:55 -0800452 paths := child.(android.Module).NoticeFiles()
453 if len(paths) > 0 {
454 for _, path := range paths {
455 noticePathSet[path] = true
456 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700457 }
458 return true
459 })
460
461 // If the app has one, add it too.
Bob Badoura75b0572020-02-18 20:21:55 -0800462 if len(a.NoticeFiles()) > 0 {
463 for _, path := range a.NoticeFiles() {
464 noticePathSet[path] = true
465 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700466 }
467
468 if len(noticePathSet) == 0 {
Jaewoong Jung98772792019-07-01 17:15:13 -0700469 return
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700470 }
471 var noticePaths []android.Path
472 for path := range noticePathSet {
473 noticePaths = append(noticePaths, path)
474 }
475 sort.Slice(noticePaths, func(i, j int) bool {
476 return noticePaths[i].String() < noticePaths[j].String()
477 })
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700478
Jaewoong Jung0949f312019-09-11 10:25:18 -0700479 a.noticeOutputs = android.BuildNoticeOutput(ctx, a.installDir, a.installApkName+".apk", noticePaths)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700480}
481
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700482// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
483// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
484func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate {
485 if android.SrcIsModule(certPropValue) == "" {
486 var mainCert Certificate
487 if certPropValue != "" {
488 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
489 mainCert = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -0800490 Pem: defaultDir.Join(ctx, certPropValue+".x509.pem"),
491 Key: defaultDir.Join(ctx, certPropValue+".pk8"),
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700492 }
493 } else {
494 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Colin Cross503c1d02020-01-28 14:00:53 -0800495 mainCert = Certificate{
496 Pem: pem,
497 Key: key,
498 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700499 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700500 certificates = append([]Certificate{mainCert}, certificates...)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700501 }
502
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700503 if !m.Platform() {
504 certPath := certificates[0].Pem.String()
Jeongik Chac9464142019-01-07 12:07:27 +0900505 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
506 if strings.HasPrefix(certPath, systemCertPath) {
507 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
508 whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
509
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700510 if enforceSystemCert && !inList(m.Name(), whitelist) {
Jeongik Chac9464142019-01-07 12:07:27 +0900511 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
512 }
513 }
514 }
515
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700516 return certificates
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800517}
518
Jooyung Han39ee1192020-03-23 20:21:11 +0900519func (a *AndroidApp) InstallApkName() string {
520 return a.installApkName
521}
522
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800523func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross50ddcc42019-05-16 12:28:22 -0700524 var apkDeps android.Paths
525
Jeongik Cha538c0d02019-07-11 15:54:27 +0900526 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
527 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
528
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800529 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800530 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800531
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700532 if ctx.ModuleName() == "framework-res" {
533 // framework-res.apk is installed as system/framework/framework-res.apk
Jaewoong Jung0949f312019-09-11 10:25:18 -0700534 a.installDir = android.PathForModuleInstall(ctx, "framework")
Jiyong Parkf7487312019-10-17 12:54:30 +0900535 } else if a.Privileged() {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700536 a.installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
537 } else if ctx.InstallInTestcases() {
Jaewoong Jung326a9412019-11-21 10:41:00 -0800538 a.installDir = android.PathForModuleInstall(ctx, a.installApkName, ctx.DeviceConfig().DeviceArch())
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700539 } else {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700540 a.installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700541 }
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700542 a.onDeviceDir = android.InstallPathToOnDevicePath(ctx, a.installDir)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700543
Jaewoong Jung0949f312019-09-11 10:25:18 -0700544 a.noticeBuildActions(ctx)
Jaewoong Jung98772792019-07-01 17:15:13 -0700545 if Bool(a.appProperties.Embed_notices) || ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") {
546 a.aapt.noticeFile = a.noticeOutputs.HtmlGzOutput
547 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700548
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800549 // Process all building blocks, from AAPT to certificates.
550 a.aaptBuildActions(ctx)
551
Colin Cross50ddcc42019-05-16 12:28:22 -0700552 if a.usesLibrary.enforceUsesLibraries() {
553 manifestCheckFile := a.usesLibrary.verifyUsesLibrariesManifest(ctx, a.mergedManifestFile)
554 apkDeps = append(apkDeps, manifestCheckFile)
555 }
556
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800557 a.proguardBuildActions(ctx)
558
559 dexJarFile := a.dexBuildActions(ctx)
560
Colin Cross094cde42020-02-15 10:38:00 -0800561 jniLibs, certificateDeps := collectAppDeps(ctx, a.shouldEmbedJnis(ctx), !Bool(a.appProperties.Jni_uses_platform_apis))
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800562 jniJarFile := a.jniBuildActions(jniLibs, ctx)
563
564 if ctx.Failed() {
565 return
566 }
567
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700568 certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx)
569 a.certificate = certificates[0]
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800570
571 // Build a final signed app package.
Jaewoong Jung5a498812019-11-07 14:14:38 -0800572 packageFile := android.PathForModuleOut(ctx, a.installApkName+".apk")
Songchun Fan17d69e32020-03-24 20:32:24 -0700573 v4SigningRequested := Bool(a.Module.deviceProperties.V4_signature)
574 var v4SignatureFile android.WritablePath = nil
575 if v4SigningRequested {
576 v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+".apk.idsig")
577 }
578 CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates, apkDeps, v4SignatureFile)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800579 a.outputFile = packageFile
Songchun Fan17d69e32020-03-24 20:32:24 -0700580 if v4SigningRequested {
581 a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
582 }
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800583
Colin Crosse560c4a2019-03-19 16:03:11 -0700584 for _, split := range a.aapt.splits {
585 // Sign the split APKs
Jaewoong Jung5a498812019-11-07 14:14:38 -0800586 packageFile := android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk")
Songchun Fan17d69e32020-03-24 20:32:24 -0700587 if v4SigningRequested {
588 v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk.idsig")
589 }
590 CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates, apkDeps, v4SignatureFile)
Colin Crosse560c4a2019-03-19 16:03:11 -0700591 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
Songchun Fan17d69e32020-03-24 20:32:24 -0700592 if v4SigningRequested {
593 a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
594 }
Colin Crosse560c4a2019-03-19 16:03:11 -0700595 }
596
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800597 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700598 bundleFile := android.PathForModuleOut(ctx, "base.zip")
599 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
600 a.bundleFile = bundleFile
601
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800602 // Install the app package.
Jiyong Park8ba50f92019-11-13 15:01:01 +0900603 if (Bool(a.Module.properties.Installable) || ctx.Host()) && a.IsForPlatform() {
604 ctx.InstallFile(a.installDir, a.outputFile.Base(), a.outputFile)
605 for _, extra := range a.extraOutputFiles {
606 ctx.InstallFile(a.installDir, extra.Base(), extra)
607 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800608 }
Colin Cross30e076a2015-04-13 13:58:27 -0700609}
610
Colin Cross094cde42020-02-15 10:38:00 -0800611func collectAppDeps(ctx android.ModuleContext, shouldCollectRecursiveNativeDeps bool,
612 checkNativeSdkVersion bool) ([]jniLib, []Certificate) {
Colin Crossa4f08812018-10-02 22:03:40 -0700613 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900614 var certificates []Certificate
Peter Collingbournead84f972019-12-17 16:46:18 -0800615 seenModulePaths := make(map[string]bool)
Colin Crossa4f08812018-10-02 22:03:40 -0700616
Peter Collingbournead84f972019-12-17 16:46:18 -0800617 ctx.WalkDeps(func(module android.Module, parent android.Module) bool {
Colin Crossa4f08812018-10-02 22:03:40 -0700618 otherName := ctx.OtherModuleName(module)
619 tag := ctx.OtherModuleDependencyTag(module)
620
Peter Collingbournead84f972019-12-17 16:46:18 -0800621 if IsJniDepTag(tag) || tag == cc.SharedDepTag {
Colin Crossa4f08812018-10-02 22:03:40 -0700622 if dep, ok := module.(*cc.Module); ok {
Peter Collingbournead84f972019-12-17 16:46:18 -0800623 if dep.IsNdk() || dep.IsStubs() {
624 return false
625 }
626
Colin Crossa4f08812018-10-02 22:03:40 -0700627 lib := dep.OutputFile()
Peter Collingbournead84f972019-12-17 16:46:18 -0800628 path := lib.Path()
629 if seenModulePaths[path.String()] {
630 return false
631 }
632 seenModulePaths[path.String()] = true
633
Colin Cross094cde42020-02-15 10:38:00 -0800634 if checkNativeSdkVersion {
635 if app, ok := ctx.Module().(interface{ sdkVersion() sdkSpec }); ok {
636 if app.sdkVersion().specified() &&
637 app.sdkVersion().kind != sdkCorePlatform &&
638 dep.SdkVersion() == "" {
639 ctx.PropertyErrorf("jni_libs",
640 "JNI dependency %q uses platform APIs, but this module does not",
641 otherName)
642 }
643 }
644 }
645
Colin Crossa4f08812018-10-02 22:03:40 -0700646 if lib.Valid() {
647 jniLibs = append(jniLibs, jniLib{
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700648 name: ctx.OtherModuleName(module),
649 path: path,
650 target: module.Target(),
651 coverageFile: dep.CoverageOutputFile(),
Colin Crossa4f08812018-10-02 22:03:40 -0700652 })
653 } else {
654 ctx.ModuleErrorf("dependency %q missing output file", otherName)
655 }
656 } else {
657 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
Colin Crossa4f08812018-10-02 22:03:40 -0700658 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800659
660 return shouldCollectRecursiveNativeDeps
661 }
662
663 if tag == certificateTag {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700664 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900665 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700666 } else {
667 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
668 }
Colin Crossa4f08812018-10-02 22:03:40 -0700669 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800670
671 return false
Colin Crossa4f08812018-10-02 22:03:40 -0700672 })
673
Colin Crossbd01e2a2018-10-04 15:21:03 -0700674 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700675}
676
Colin Cross0ea8ba82019-06-06 14:33:29 -0700677func (a *AndroidApp) getCertString(ctx android.BaseModuleContext) string {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800678 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
679 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000680 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800681 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800682 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800683}
684
Jiyong Park0f80c182020-01-31 02:49:53 +0900685func (a *AndroidApp) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
686 if IsJniDepTag(ctx.OtherModuleDependencyTag(dep)) {
687 return true
688 }
689 return a.Library.DepIsInSameApex(ctx, dep)
690}
691
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900692// For OutputFileProducer interface
693func (a *AndroidApp) OutputFiles(tag string) (android.Paths, error) {
694 switch tag {
695 case ".aapt.srcjar":
696 return []android.Path{a.aaptSrcJar}, nil
697 }
698 return a.Library.OutputFiles(tag)
699}
700
Jiyong Parkf7487312019-10-17 12:54:30 +0900701func (a *AndroidApp) Privileged() bool {
702 return Bool(a.appProperties.Privileged)
703}
704
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700705func (a *AndroidApp) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
706 return ctx.Device() && (ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled())
707}
708
709func (a *AndroidApp) PreventInstall() {
710 a.appProperties.PreventInstall = true
711}
712
713func (a *AndroidApp) HideFromMake() {
714 a.appProperties.HideFromMake = true
715}
716
717func (a *AndroidApp) MarkAsCoverageVariant(coverage bool) {
718 a.appProperties.IsCoverageVariant = coverage
719}
720
721var _ cc.Coverage = (*AndroidApp)(nil)
722
Colin Cross1b16b0e2019-02-12 14:41:32 -0800723// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700724func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700725 module := &AndroidApp{}
726
Sasha Smundak2057f822019-04-16 17:16:58 -0700727 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross66dbc0b2017-12-28 12:23:20 -0800728 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
729
Colin Crossae5caf52018-05-22 11:11:52 -0700730 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700731 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700732
Colin Cross36242852017-06-23 15:06:31 -0700733 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700734 &module.Module.properties,
735 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800736 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700737 &module.Module.protoProperties,
738 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800739 &module.appProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700740 &module.overridableAppProperties,
741 &module.usesLibrary.usesLibraryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700742
Colin Crossa9d8bee2018-10-02 13:59:46 -0700743 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
744 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
745 })
746
Colin Crossa4f08812018-10-02 22:03:40 -0700747 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
748 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800749 android.InitOverridableModule(module, &module.appProperties.Overrides)
Jiyong Park52cd06f2019-11-11 10:14:32 +0900750 android.InitApexModule(module)
Colin Crossa4f08812018-10-02 22:03:40 -0700751
Colin Cross36242852017-06-23 15:06:31 -0700752 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700753}
Colin Crossae5caf52018-05-22 11:11:52 -0700754
755type appTestProperties struct {
Liz Kammer6b0c5522020-04-28 16:10:55 -0700756 // The name of the android_app module that the tests will run against.
Colin Crossae5caf52018-05-22 11:11:52 -0700757 Instrumentation_for *string
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700758
759 // if specified, the instrumentation target package name in the manifest is overwritten by it.
760 Instrumentation_target_package *string
Colin Crossae5caf52018-05-22 11:11:52 -0700761}
762
763type AndroidTest struct {
764 AndroidApp
765
766 appTestProperties appTestProperties
767
768 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700769
770 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700771 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700772}
773
Jaewoong Jung0949f312019-09-11 10:25:18 -0700774func (a *AndroidTest) InstallInTestcases() bool {
775 return true
776}
777
Colin Crossae5caf52018-05-22 11:11:52 -0700778func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700779 if a.appTestProperties.Instrumentation_target_package != nil {
780 a.additionalAaptFlags = append(a.additionalAaptFlags,
781 "--rename-instrumentation-target-package "+*a.appTestProperties.Instrumentation_target_package)
782 } else if a.appTestProperties.Instrumentation_for != nil {
783 // Check if the instrumentation target package is overridden.
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800784 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
785 if overridden {
786 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
787 }
788 }
Colin Crossae5caf52018-05-22 11:11:52 -0700789 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700790
Jaewoong Jung39982342020-01-14 10:27:18 -0800791 testConfig := tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700792 a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites, a.testProperties.Auto_gen_config)
Jaewoong Jung39982342020-01-14 10:27:18 -0800793 a.testConfig = a.FixTestConfig(ctx, testConfig)
Colin Cross8a497952019-03-05 22:25:09 -0800794 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700795}
796
Jaewoong Jung39982342020-01-14 10:27:18 -0800797func (a *AndroidTest) FixTestConfig(ctx android.ModuleContext, testConfig android.Path) android.Path {
798 if testConfig == nil {
799 return nil
800 }
801
802 fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", "AndroidTest.xml")
803 rule := android.NewRuleBuilder()
804 command := rule.Command().BuiltTool(ctx, "test_config_fixer").Input(testConfig).Output(fixedConfig)
805 fixNeeded := false
806
807 if ctx.ModuleName() != a.installApkName {
808 fixNeeded = true
809 command.FlagWithArg("--test-file-name ", a.installApkName+".apk")
810 }
811
812 if a.overridableAppProperties.Package_name != nil {
813 fixNeeded = true
814 command.FlagWithInput("--manifest ", a.manifestPath).
815 FlagWithArg("--package-name ", *a.overridableAppProperties.Package_name)
816 }
817
818 if fixNeeded {
819 rule.Build(pctx, ctx, "fix_test_config", "fix test config")
820 return fixedConfig
821 }
822 return testConfig
823}
824
Colin Cross303e21f2018-08-07 16:49:25 -0700825func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -0700826 a.AndroidApp.DepsMutator(ctx)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700827}
828
829func (a *AndroidTest) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
830 a.AndroidApp.OverridablePropertiesDepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -0700831 if a.appTestProperties.Instrumentation_for != nil {
832 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
833 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
834 // use instrumentationForTag instead of libTag.
835 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
836 }
Colin Crossae5caf52018-05-22 11:11:52 -0700837}
838
Colin Cross1b16b0e2019-02-12 14:41:32 -0800839// android_test compiles test sources and Android resources into an Android application package `.apk` file and
840// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -0700841func AndroidTestFactory() android.Module {
842 module := &AndroidTest{}
843
Sasha Smundak2057f822019-04-16 17:16:58 -0700844 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross5067db92018-09-17 16:46:35 -0700845
846 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700847 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800848 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700849 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800850 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -0700851
852 module.AddProperties(
853 &module.Module.properties,
854 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800855 &module.Module.dexpreoptProperties,
Colin Crossae5caf52018-05-22 11:11:52 -0700856 &module.Module.protoProperties,
857 &module.aaptProperties,
858 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700859 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800860 &module.overridableAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700861 &module.usesLibrary.usesLibraryProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700862 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700863
Colin Crossa4f08812018-10-02 22:03:40 -0700864 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
865 android.InitDefaultableModule(module)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700866 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossae5caf52018-05-22 11:11:52 -0700867 return module
868}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700869
Colin Cross252fc6f2018-10-04 15:22:03 -0700870type appTestHelperAppProperties struct {
871 // list of compatibility suites (for example "cts", "vts") that the module should be
872 // installed into.
873 Test_suites []string `android:"arch_variant"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700874
875 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
876 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
877 // explicitly.
878 Auto_gen_config *bool
Colin Cross252fc6f2018-10-04 15:22:03 -0700879}
880
881type AndroidTestHelperApp struct {
882 AndroidApp
883
884 appTestHelperAppProperties appTestHelperAppProperties
885}
886
Jaewoong Jung326a9412019-11-21 10:41:00 -0800887func (a *AndroidTestHelperApp) InstallInTestcases() bool {
888 return true
889}
890
Colin Cross1b16b0e2019-02-12 14:41:32 -0800891// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
892// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
893// test.
Colin Cross252fc6f2018-10-04 15:22:03 -0700894func AndroidTestHelperAppFactory() android.Module {
895 module := &AndroidTestHelperApp{}
896
Sasha Smundak2057f822019-04-16 17:16:58 -0700897 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700898
899 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800900 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700901 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800902 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700903
904 module.AddProperties(
905 &module.Module.properties,
906 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800907 &module.Module.dexpreoptProperties,
Colin Cross252fc6f2018-10-04 15:22:03 -0700908 &module.Module.protoProperties,
909 &module.aaptProperties,
910 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800911 &module.appTestHelperAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700912 &module.overridableAppProperties,
913 &module.usesLibrary.usesLibraryProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -0700914
915 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
916 android.InitDefaultableModule(module)
Anton Hansson3d2b6b42020-01-10 15:06:01 +0000917 android.InitApexModule(module)
Colin Cross252fc6f2018-10-04 15:22:03 -0700918 return module
919}
920
Colin Crossbd01e2a2018-10-04 15:21:03 -0700921type AndroidAppCertificate struct {
922 android.ModuleBase
923 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900924 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -0700925}
926
927type AndroidAppCertificateProperties struct {
928 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
929 Certificate *string
930}
931
Colin Cross1b16b0e2019-02-12 14:41:32 -0800932// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
933// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -0700934func AndroidAppCertificateFactory() android.Module {
935 module := &AndroidAppCertificate{}
936 module.AddProperties(&module.properties)
937 android.InitAndroidModule(module)
938 return module
939}
940
Colin Crossbd01e2a2018-10-04 15:21:03 -0700941func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
942 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900943 c.Certificate = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -0800944 Pem: android.PathForModuleSrc(ctx, cert+".x509.pem"),
945 Key: android.PathForModuleSrc(ctx, cert+".pk8"),
Colin Crossbd01e2a2018-10-04 15:21:03 -0700946 }
947}
Jaewoong Jung525443a2019-02-28 15:35:54 -0800948
949type OverrideAndroidApp struct {
950 android.ModuleBase
951 android.OverrideModuleBase
952}
953
954func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
955 // All the overrides happen in the base module.
956 // TODO(jungjw): Check the base module type.
957}
958
959// override_android_app is used to create an android_app module based on another android_app by overriding
960// some of its properties.
961func OverrideAndroidAppModuleFactory() android.Module {
962 m := &OverrideAndroidApp{}
963 m.AddProperties(&overridableAppProperties{})
964
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700965 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800966 android.InitOverrideModule(m)
967 return m
968}
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700969
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700970type OverrideAndroidTest struct {
971 android.ModuleBase
972 android.OverrideModuleBase
973}
974
975func (i *OverrideAndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
976 // All the overrides happen in the base module.
977 // TODO(jungjw): Check the base module type.
978}
979
980// override_android_test is used to create an android_app module based on another android_test by overriding
981// some of its properties.
982func OverrideAndroidTestModuleFactory() android.Module {
983 m := &OverrideAndroidTest{}
984 m.AddProperties(&overridableAppProperties{})
985 m.AddProperties(&appTestProperties{})
986
987 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
988 android.InitOverrideModule(m)
989 return m
990}
991
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700992type AndroidAppImport struct {
993 android.ModuleBase
994 android.DefaultableModuleBase
Jiyong Park592a6a42020-04-21 22:34:28 +0900995 android.ApexModuleBase
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700996 prebuilt android.Prebuilt
997
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700998 properties AndroidAppImportProperties
999 dpiVariants interface{}
1000 archVariants interface{}
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001001
1002 outputFile android.Path
Colin Cross503c1d02020-01-28 14:00:53 -08001003 certificate Certificate
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001004
1005 dexpreopter
Colin Cross50ddcc42019-05-16 12:28:22 -07001006
1007 usesLibrary usesLibrary
Jaewoong Jung8aae22e2019-07-17 10:21:49 -07001008
Colin Cross70dda7e2019-10-01 22:05:35 -07001009 installPath android.InstallPath
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001010}
1011
1012type AndroidAppImportProperties struct {
1013 // A prebuilt apk to import
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001014 Apk *string
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001015
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001016 // The name of a certificate in the default certificate directory or an android_app_certificate
1017 // module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001018 Certificate *string
1019
1020 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
1021 // be set for presigned modules.
1022 Presigned *bool
1023
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001024 // Sign with the default system dev certificate. Must be used judiciously. Most imported apps
1025 // need to either specify a specific certificate or be presigned.
1026 Default_dev_cert *bool
1027
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001028 // Specifies that this app should be installed to the priv-app directory,
1029 // where the system will grant it additional privileges not available to
1030 // normal apps.
1031 Privileged *bool
1032
1033 // Names of modules to be overridden. Listed modules can only be other binaries
1034 // (in Make or Soong).
1035 // This does not completely prevent installation of the overridden binaries, but if both
1036 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1037 // from PRODUCT_PACKAGES.
1038 Overrides []string
Jaewoong Jung8aae22e2019-07-17 10:21:49 -07001039
1040 // Optional name for the installed app. If unspecified, it is derived from the module name.
1041 Filename *string
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001042}
1043
Martin Stjernholm6d415272020-01-31 17:10:36 +00001044func (a *AndroidAppImport) IsInstallable() bool {
1045 return true
1046}
1047
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001048// Updates properties with variant-specific values.
1049func (a *AndroidAppImport) processVariants(ctx android.LoadHookContext) {
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001050 config := ctx.Config()
1051
1052 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants")
1053 // Try DPI variant matches in the reverse-priority order so that the highest priority match
1054 // overwrites everything else.
1055 // TODO(jungjw): Can we optimize this by making it priority order?
1056 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001057 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001058 }
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001059 if config.ProductAAPTPreferredConfig() != "" {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001060 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001061 }
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001062
1063 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName("Arch")
1064 archType := ctx.Config().Targets[android.Android][0].Arch.ArchType
1065 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001066}
1067
Colin Cross1184b642019-12-30 18:43:07 -08001068func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001069 dst interface{}, variantGroup reflect.Value, variant string) {
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001070 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
1071 if !src.IsValid() {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001072 return
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001073 }
1074
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001075 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
1076 if err != nil {
1077 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1078 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1079 } else {
1080 panic(err)
1081 }
1082 }
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001083}
1084
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001085func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
1086 cert := android.SrcIsModule(String(a.properties.Certificate))
1087 if cert != "" {
1088 ctx.AddDependency(ctx.Module(), certificateTag, cert)
1089 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001090
Paul Duffin250e6192019-06-07 10:44:37 +01001091 a.usesLibrary.deps(ctx, true)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001092}
1093
1094func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
1095 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001096 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
1097 // with them may invalidate pre-existing signature data.
1098 if ctx.InstallInTestcases() && Bool(a.properties.Presigned) {
1099 ctx.Build(pctx, android.BuildParams{
1100 Rule: android.Cp,
1101 Output: outputPath,
1102 Input: inputPath,
1103 })
1104 return
1105 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001106 rule := android.NewRuleBuilder()
1107 rule.Command().
1108 Textf(`if (zipinfo %s 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
Colin Crossee94d6a2019-07-08 17:08:34 -07001109 BuiltTool(ctx, "zip2zip").
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001110 FlagWithInput("-i ", inputPath).
1111 FlagWithOutput("-o ", outputPath).
1112 FlagWithArg("-0 ", "'lib/**/*.so'").
1113 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
1114 rule.Build(pctx, ctx, "uncompress-embedded-jni-libs", "Uncompress embedded JIN libs")
1115}
1116
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001117// Returns whether this module should have the dex file stored uncompressed in the APK.
1118func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
1119 if ctx.Config().UnbundledBuild() {
1120 return false
1121 }
1122
1123 // Uncompress dex in APKs of privileged apps
Jiyong Parkf7487312019-10-17 12:54:30 +09001124 if ctx.Config().UncompressPrivAppDex() && a.Privileged() {
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001125 return true
1126 }
1127
1128 return shouldUncompressDex(ctx, &a.dexpreopter)
1129}
1130
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001131func (a *AndroidAppImport) uncompressDex(
1132 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
1133 rule := android.NewRuleBuilder()
1134 rule.Command().
1135 Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
Colin Crossee94d6a2019-07-08 17:08:34 -07001136 BuiltTool(ctx, "zip2zip").
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001137 FlagWithInput("-i ", inputPath).
1138 FlagWithOutput("-o ", outputPath).
1139 FlagWithArg("-0 ", "'classes*.dex'").
1140 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
1141 rule.Build(pctx, ctx, "uncompress-dex", "Uncompress dex files")
1142}
1143
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001144func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001145 a.generateAndroidBuildActions(ctx)
1146}
1147
Jooyung Han39ee1192020-03-23 20:21:11 +09001148func (a *AndroidAppImport) InstallApkName() string {
1149 return a.BaseModuleName()
1150}
1151
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001152func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001153 numCertPropsSet := 0
1154 if String(a.properties.Certificate) != "" {
1155 numCertPropsSet++
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001156 }
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001157 if Bool(a.properties.Presigned) {
1158 numCertPropsSet++
1159 }
1160 if Bool(a.properties.Default_dev_cert) {
1161 numCertPropsSet++
1162 }
1163 if numCertPropsSet != 1 {
1164 ctx.ModuleErrorf("One and only one of certficate, presigned, and default_dev_cert properties must be set")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001165 }
1166
Colin Cross094cde42020-02-15 10:38:00 -08001167 _, certificates := collectAppDeps(ctx, false, false)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001168
1169 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001170 // TODO: LOCAL_PACKAGE_SPLITS
1171
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001172 srcApk := a.prebuilt.SingleSourcePath(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -07001173
1174 if a.usesLibrary.enforceUsesLibraries() {
1175 srcApk = a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk)
1176 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001177
1178 // TODO: Install or embed JNI libraries
1179
1180 // Uncompress JNI libraries in the apk
1181 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
1182 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
1183
Kyeongkab.Namc4997142019-11-22 11:38:16 +09001184 var installDir android.InstallPath
1185 if Bool(a.properties.Privileged) {
1186 installDir = android.PathForModuleInstall(ctx, "priv-app", a.BaseModuleName())
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001187 } else if ctx.InstallInTestcases() {
1188 installDir = android.PathForModuleInstall(ctx, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch())
Kyeongkab.Namc4997142019-11-22 11:38:16 +09001189 } else {
1190 installDir = android.PathForModuleInstall(ctx, "app", a.BaseModuleName())
1191 }
1192
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001193 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001194 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001195 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -07001196
1197 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
1198 a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
1199 a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
1200 a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
1201
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001202 dexOutput := a.dexpreopter.dexpreopt(ctx, jnisUncompressed)
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001203 if a.dexpreopter.uncompressedDex {
1204 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
1205 a.uncompressDex(ctx, dexOutput, dexUncompressed.OutputPath)
1206 dexOutput = dexUncompressed
1207 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001208
Jooyung Han39ee1192020-03-23 20:21:11 +09001209 apkFilename := proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk")
1210
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001211 // Sign or align the package
1212 // TODO: Handle EXTERNAL
1213 if !Bool(a.properties.Presigned) {
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001214 // If the certificate property is empty at this point, default_dev_cert must be set to true.
1215 // Which makes processMainCert's behavior for the empty cert string WAI.
1216 certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001217 if len(certificates) != 1 {
1218 ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates)
1219 }
Colin Cross503c1d02020-01-28 14:00:53 -08001220 a.certificate = certificates[0]
Jooyung Han39ee1192020-03-23 20:21:11 +09001221 signed := android.PathForModuleOut(ctx, "signed", apkFilename)
Songchun Fan17d69e32020-03-24 20:32:24 -07001222 SignAppPackage(ctx, signed, dexOutput, certificates, nil)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001223 a.outputFile = signed
1224 } else {
Jooyung Han39ee1192020-03-23 20:21:11 +09001225 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001226 TransformZipAlign(ctx, alignedApk, dexOutput)
1227 a.outputFile = alignedApk
Colin Cross503c1d02020-01-28 14:00:53 -08001228 a.certificate = presignedCertificate
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001229 }
1230
1231 // TODO: Optionally compress the output apk.
1232
Jiyong Park592a6a42020-04-21 22:34:28 +09001233 if a.IsForPlatform() {
1234 a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
1235 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001236
1237 // TODO: androidmk converter jni libs
1238}
1239
1240func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
1241 return &a.prebuilt
1242}
1243
1244func (a *AndroidAppImport) Name() string {
1245 return a.prebuilt.Name(a.ModuleBase.Name())
1246}
1247
Dario Frenicde2a032019-10-27 00:29:22 +01001248func (a *AndroidAppImport) OutputFile() android.Path {
1249 return a.outputFile
1250}
1251
Jiyong Park618922e2020-01-08 13:35:43 +09001252func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
1253 return nil
1254}
1255
Colin Cross503c1d02020-01-28 14:00:53 -08001256func (a *AndroidAppImport) Certificate() Certificate {
1257 return a.certificate
1258}
1259
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001260var dpiVariantGroupType reflect.Type
1261var archVariantGroupType reflect.Type
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001262
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001263func initAndroidAppImportVariantGroupTypes() {
1264 dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants")
1265
1266 archNames := make([]string, len(android.ArchTypeList()))
1267 for i, archType := range android.ArchTypeList() {
1268 archNames[i] = archType.Name
1269 }
1270 archVariantGroupType = createVariantGroupType(archNames, "Arch")
1271}
1272
1273// Populates all variant struct properties at creation time.
1274func (a *AndroidAppImport) populateAllVariantStructs() {
1275 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
1276 a.AddProperties(a.dpiVariants)
1277
1278 a.archVariants = reflect.New(archVariantGroupType).Interface()
1279 a.AddProperties(a.archVariants)
1280}
1281
Jiyong Parkf7487312019-10-17 12:54:30 +09001282func (a *AndroidAppImport) Privileged() bool {
1283 return Bool(a.properties.Privileged)
1284}
1285
Jiyong Park592a6a42020-04-21 22:34:28 +09001286func (a *AndroidAppImport) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1287 // android_app_import might have extra dependencies via uses_libs property.
1288 // Don't track the dependency as we don't automatically add those libraries
1289 // to the classpath. It should be explicitly added to java_libs property of APEX
1290 return false
1291}
1292
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001293func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
1294 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
1295
1296 variantFields := make([]reflect.StructField, len(variants))
1297 for i, variant := range variants {
1298 variantFields[i] = reflect.StructField{
1299 Name: proptools.FieldNameForProperty(variant),
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001300 Type: props,
1301 }
1302 }
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001303
1304 variantGroupStruct := reflect.StructOf(variantFields)
1305 return reflect.StructOf([]reflect.StructField{
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001306 {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001307 Name: variantGroupName,
1308 Type: variantGroupStruct,
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001309 },
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001310 })
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001311}
1312
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001313// android_app_import imports a prebuilt apk with additional processing specified in the module.
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001314// DPI-specific apk source files can be specified using dpi_variants. Example:
1315//
1316// android_app_import {
1317// name: "example_import",
1318// apk: "prebuilts/example.apk",
1319// dpi_variants: {
1320// mdpi: {
1321// apk: "prebuilts/example_mdpi.apk",
1322// },
1323// xhdpi: {
1324// apk: "prebuilts/example_xhdpi.apk",
1325// },
1326// },
1327// certificate: "PRESIGNED",
1328// }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001329func AndroidAppImportFactory() android.Module {
1330 module := &AndroidAppImport{}
1331 module.AddProperties(&module.properties)
1332 module.AddProperties(&module.dexpreoptProperties)
Colin Cross50ddcc42019-05-16 12:28:22 -07001333 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001334 module.populateAllVariantStructs()
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001335 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001336 module.processVariants(ctx)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001337 })
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001338
Jiyong Park592a6a42020-04-21 22:34:28 +09001339 android.InitApexModule(module)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001340 InitJavaModule(module, android.DeviceSupported)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001341 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001342
1343 return module
1344}
Colin Cross50ddcc42019-05-16 12:28:22 -07001345
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001346type AndroidTestImport struct {
1347 AndroidAppImport
1348
1349 testProperties testProperties
1350
1351 data android.Paths
1352}
1353
1354func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1355 a.generateAndroidBuildActions(ctx)
1356
1357 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
1358}
1359
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001360func (a *AndroidTestImport) InstallInTestcases() bool {
1361 return true
1362}
1363
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001364// android_test_import imports a prebuilt test apk with additional processing specified in the
1365// module. DPI or arch variant configurations can be made as with android_app_import.
1366func AndroidTestImportFactory() android.Module {
1367 module := &AndroidTestImport{}
1368 module.AddProperties(&module.properties)
1369 module.AddProperties(&module.dexpreoptProperties)
1370 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
1371 module.AddProperties(&module.testProperties)
1372 module.populateAllVariantStructs()
1373 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
1374 module.processVariants(ctx)
1375 })
1376
Jiyong Park592a6a42020-04-21 22:34:28 +09001377 android.InitApexModule(module)
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001378 InitJavaModule(module, android.DeviceSupported)
1379 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
1380
1381 return module
1382}
1383
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001384type RuntimeResourceOverlay struct {
1385 android.ModuleBase
1386 android.DefaultableModuleBase
1387 aapt
1388
1389 properties RuntimeResourceOverlayProperties
1390
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08001391 certificate Certificate
1392
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001393 outputFile android.Path
1394 installDir android.InstallPath
1395}
1396
1397type RuntimeResourceOverlayProperties struct {
1398 // the name of a certificate in the default certificate directory or an android_app_certificate
1399 // module name in the form ":module".
1400 Certificate *string
1401
1402 // optional theme name. If specified, the overlay package will be applied
1403 // only when the ro.boot.vendor.overlay.theme system property is set to the same value.
1404 Theme *string
1405
1406 // if not blank, set to the version of the sdk to compile against.
1407 // Defaults to compiling against the current platform.
1408 Sdk_version *string
1409
1410 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
1411 // Defaults to sdk_version if not set.
1412 Min_sdk_version *string
Jaewoong Jungfe3c7f62020-04-09 16:15:30 -07001413
1414 // list of android_library modules whose resources are extracted and linked against statically
1415 Static_libs []string
1416
1417 // list of android_app modules whose resources are extracted and linked against
1418 Resource_libs []string
Jaewoong Jungad0177b2020-04-24 15:22:40 -07001419
1420 // Names of modules to be overridden. Listed modules can only be other overlays
1421 // (in Make or Soong).
1422 // This does not completely prevent installation of the overridden overlays, but if both
1423 // overlays would be installed by default (in PRODUCT_PACKAGES) the other overlay will be removed
1424 // from PRODUCT_PACKAGES.
1425 Overrides []string
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001426}
1427
Jiyong Park69aeba92020-04-24 21:16:36 +09001428// RuntimeResourceOverlayModule interface is used by the apex package to gather information from
1429// a RuntimeResourceOverlay module.
1430type RuntimeResourceOverlayModule interface {
1431 android.Module
1432 OutputFile() android.Path
1433 Certificate() Certificate
1434 Theme() string
1435}
1436
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001437func (r *RuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
1438 sdkDep := decodeSdkDep(ctx, sdkContext(r))
1439 if sdkDep.hasFrameworkLibs() {
1440 r.aapt.deps(ctx, sdkDep)
1441 }
1442
1443 cert := android.SrcIsModule(String(r.properties.Certificate))
1444 if cert != "" {
1445 ctx.AddDependency(ctx.Module(), certificateTag, cert)
1446 }
Jaewoong Jungfe3c7f62020-04-09 16:15:30 -07001447
1448 ctx.AddVariationDependencies(nil, staticLibTag, r.properties.Static_libs...)
1449 ctx.AddVariationDependencies(nil, libTag, r.properties.Resource_libs...)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001450}
1451
1452func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1453 // Compile and link resources
1454 r.aapt.hasNoCode = true
Jaewoong Jungf0f747c2020-01-24 10:30:02 -08001455 // Do not remove resources without default values nor dedupe resource configurations with the same value
1456 r.aapt.buildActions(ctx, r, "--no-resource-deduping", "--no-resource-removal")
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001457
1458 // Sign the built package
Colin Cross094cde42020-02-15 10:38:00 -08001459 _, certificates := collectAppDeps(ctx, false, false)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001460 certificates = processMainCert(r.ModuleBase, String(r.properties.Certificate), certificates, ctx)
1461 signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
Songchun Fan17d69e32020-03-24 20:32:24 -07001462 SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil)
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08001463 r.certificate = certificates[0]
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001464
1465 r.outputFile = signed
1466 r.installDir = android.PathForModuleInstall(ctx, "overlay", String(r.properties.Theme))
1467 ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile)
1468}
1469
Jiyong Park6a927c42020-01-21 02:03:43 +09001470func (r *RuntimeResourceOverlay) sdkVersion() sdkSpec {
1471 return sdkSpecFrom(String(r.properties.Sdk_version))
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001472}
1473
1474func (r *RuntimeResourceOverlay) systemModules() string {
1475 return ""
1476}
1477
Jiyong Park6a927c42020-01-21 02:03:43 +09001478func (r *RuntimeResourceOverlay) minSdkVersion() sdkSpec {
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001479 if r.properties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +09001480 return sdkSpecFrom(*r.properties.Min_sdk_version)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001481 }
1482 return r.sdkVersion()
1483}
1484
Jiyong Park6a927c42020-01-21 02:03:43 +09001485func (r *RuntimeResourceOverlay) targetSdkVersion() sdkSpec {
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001486 return r.sdkVersion()
1487}
1488
Jiyong Park69aeba92020-04-24 21:16:36 +09001489func (r *RuntimeResourceOverlay) Certificate() Certificate {
1490 return r.certificate
1491}
1492
1493func (r *RuntimeResourceOverlay) OutputFile() android.Path {
1494 return r.outputFile
1495}
1496
1497func (r *RuntimeResourceOverlay) Theme() string {
1498 return String(r.properties.Theme)
1499}
1500
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001501// runtime_resource_overlay generates a resource-only apk file that can overlay application and
1502// system resources at run time.
1503func RuntimeResourceOverlayFactory() android.Module {
1504 module := &RuntimeResourceOverlay{}
1505 module.AddProperties(
1506 &module.properties,
1507 &module.aaptProperties)
1508
1509 InitJavaModule(module, android.DeviceSupported)
1510
1511 return module
1512}
1513
Colin Cross50ddcc42019-05-16 12:28:22 -07001514type UsesLibraryProperties struct {
1515 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file.
1516 Uses_libs []string
1517
1518 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file with
1519 // required=false.
1520 Optional_uses_libs []string
1521
1522 // If true, the list of uses_libs and optional_uses_libs modules must match the AndroidManifest.xml file. Defaults
1523 // to true if either uses_libs or optional_uses_libs is set. Will unconditionally default to true in the future.
1524 Enforce_uses_libs *bool
1525}
1526
1527// usesLibrary provides properties and helper functions for AndroidApp and AndroidAppImport to verify that the
1528// <uses-library> tags that end up in the manifest of an APK match the ones known to the build system through the
1529// uses_libs and optional_uses_libs properties. The build system's values are used by dexpreopt to preopt apps
1530// with knowledge of their shared libraries.
1531type usesLibrary struct {
1532 usesLibraryProperties UsesLibraryProperties
1533}
1534
Paul Duffin250e6192019-06-07 10:44:37 +01001535func (u *usesLibrary) deps(ctx android.BottomUpMutatorContext, hasFrameworkLibs bool) {
Colin Cross3245b2c2019-06-07 13:18:09 -07001536 if !ctx.Config().UnbundledBuild() {
1537 ctx.AddVariationDependencies(nil, usesLibTag, u.usesLibraryProperties.Uses_libs...)
1538 ctx.AddVariationDependencies(nil, usesLibTag, u.presentOptionalUsesLibs(ctx)...)
Paul Duffin250e6192019-06-07 10:44:37 +01001539 // Only add these extra dependencies if the module depends on framework libs. This avoids
1540 // creating a cyclic dependency:
1541 // e.g. framework-res -> org.apache.http.legacy -> ... -> framework-res.
1542 if hasFrameworkLibs {
Colin Cross3245b2c2019-06-07 13:18:09 -07001543 // dexpreopt/dexpreopt.go needs the paths to the dex jars of these libraries in case construct_context.sh needs
1544 // to pass them to dex2oat. Add them as a dependency so we can determine the path to the dex jar of each
1545 // library to dexpreopt.
1546 ctx.AddVariationDependencies(nil, usesLibTag,
1547 "org.apache.http.legacy",
1548 "android.hidl.base-V1.0-java",
1549 "android.hidl.manager-V1.0-java")
1550 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001551 }
1552}
1553
1554// presentOptionalUsesLibs returns optional_uses_libs after filtering out MissingUsesLibraries, which don't exist in the
1555// build.
1556func (u *usesLibrary) presentOptionalUsesLibs(ctx android.BaseModuleContext) []string {
1557 optionalUsesLibs, _ := android.FilterList(u.usesLibraryProperties.Optional_uses_libs, ctx.Config().MissingUsesLibraries())
1558 return optionalUsesLibs
1559}
1560
1561// usesLibraryPaths returns a map of module names of shared library dependencies to the paths to their dex jars.
1562func (u *usesLibrary) usesLibraryPaths(ctx android.ModuleContext) map[string]android.Path {
1563 usesLibPaths := make(map[string]android.Path)
1564
1565 if !ctx.Config().UnbundledBuild() {
1566 ctx.VisitDirectDepsWithTag(usesLibTag, func(m android.Module) {
1567 if lib, ok := m.(Dependency); ok {
1568 if dexJar := lib.DexJar(); dexJar != nil {
1569 usesLibPaths[ctx.OtherModuleName(m)] = dexJar
1570 } else {
1571 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must produce a dex jar, does it have installable: true?",
1572 ctx.OtherModuleName(m))
1573 }
1574 } else if ctx.Config().AllowMissingDependencies() {
1575 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(m)})
1576 } else {
1577 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must be a java library",
1578 ctx.OtherModuleName(m))
1579 }
1580 })
1581 }
1582
1583 return usesLibPaths
1584}
1585
1586// enforceUsesLibraries returns true of <uses-library> tags should be checked against uses_libs and optional_uses_libs
1587// properties. Defaults to true if either of uses_libs or optional_uses_libs is specified. Will default to true
1588// unconditionally in the future.
1589func (u *usesLibrary) enforceUsesLibraries() bool {
1590 defaultEnforceUsesLibs := len(u.usesLibraryProperties.Uses_libs) > 0 ||
1591 len(u.usesLibraryProperties.Optional_uses_libs) > 0
1592 return BoolDefault(u.usesLibraryProperties.Enforce_uses_libs, defaultEnforceUsesLibs)
1593}
1594
1595// verifyUsesLibrariesManifest checks the <uses-library> tags in an AndroidManifest.xml against the ones specified
1596// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the manifest.
1597func (u *usesLibrary) verifyUsesLibrariesManifest(ctx android.ModuleContext, manifest android.Path) android.Path {
1598 outputFile := android.PathForModuleOut(ctx, "manifest_check", "AndroidManifest.xml")
1599
1600 rule := android.NewRuleBuilder()
Colin Crossee94d6a2019-07-08 17:08:34 -07001601 cmd := rule.Command().BuiltTool(ctx, "manifest_check").
Colin Cross50ddcc42019-05-16 12:28:22 -07001602 Flag("--enforce-uses-libraries").
1603 Input(manifest).
1604 FlagWithOutput("-o ", outputFile)
1605
1606 for _, lib := range u.usesLibraryProperties.Uses_libs {
1607 cmd.FlagWithArg("--uses-library ", lib)
1608 }
1609
1610 for _, lib := range u.usesLibraryProperties.Optional_uses_libs {
1611 cmd.FlagWithArg("--optional-uses-library ", lib)
1612 }
1613
1614 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1615
1616 return outputFile
1617}
1618
1619// verifyUsesLibrariesAPK checks the <uses-library> tags in the manifest of an APK against the ones specified
1620// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the APK.
1621func (u *usesLibrary) verifyUsesLibrariesAPK(ctx android.ModuleContext, apk android.Path) android.Path {
1622 outputFile := android.PathForModuleOut(ctx, "verify_uses_libraries", apk.Base())
1623
1624 rule := android.NewRuleBuilder()
1625 aapt := ctx.Config().HostToolPath(ctx, "aapt")
1626 rule.Command().
1627 Textf("aapt_binary=%s", aapt.String()).Implicit(aapt).
1628 Textf(`uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Uses_libs, " ")).
1629 Textf(`optional_uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Optional_uses_libs, " ")).
1630 Tool(android.PathForSource(ctx, "build/make/core/verify_uses_libraries.sh")).Input(apk)
1631 rule.Command().Text("cp -f").Input(apk).Output(outputFile)
1632
1633 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1634
1635 return outputFile
1636}