blob: 5577cdfd2501beeb421d36fba156d06b0913d2ca [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 {
756 Instrumentation_for *string
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700757
758 // if specified, the instrumentation target package name in the manifest is overwritten by it.
759 Instrumentation_target_package *string
Colin Crossae5caf52018-05-22 11:11:52 -0700760}
761
762type AndroidTest struct {
763 AndroidApp
764
765 appTestProperties appTestProperties
766
767 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700768
769 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700770 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700771}
772
Jaewoong Jung0949f312019-09-11 10:25:18 -0700773func (a *AndroidTest) InstallInTestcases() bool {
774 return true
775}
776
Colin Crossae5caf52018-05-22 11:11:52 -0700777func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700778 if a.appTestProperties.Instrumentation_target_package != nil {
779 a.additionalAaptFlags = append(a.additionalAaptFlags,
780 "--rename-instrumentation-target-package "+*a.appTestProperties.Instrumentation_target_package)
781 } else if a.appTestProperties.Instrumentation_for != nil {
782 // Check if the instrumentation target package is overridden.
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800783 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
784 if overridden {
785 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
786 }
787 }
Colin Crossae5caf52018-05-22 11:11:52 -0700788 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700789
Jaewoong Jung39982342020-01-14 10:27:18 -0800790 testConfig := tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700791 a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites, a.testProperties.Auto_gen_config)
Jaewoong Jung39982342020-01-14 10:27:18 -0800792 a.testConfig = a.FixTestConfig(ctx, testConfig)
Colin Cross8a497952019-03-05 22:25:09 -0800793 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700794}
795
Jaewoong Jung39982342020-01-14 10:27:18 -0800796func (a *AndroidTest) FixTestConfig(ctx android.ModuleContext, testConfig android.Path) android.Path {
797 if testConfig == nil {
798 return nil
799 }
800
801 fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", "AndroidTest.xml")
802 rule := android.NewRuleBuilder()
803 command := rule.Command().BuiltTool(ctx, "test_config_fixer").Input(testConfig).Output(fixedConfig)
804 fixNeeded := false
805
806 if ctx.ModuleName() != a.installApkName {
807 fixNeeded = true
808 command.FlagWithArg("--test-file-name ", a.installApkName+".apk")
809 }
810
811 if a.overridableAppProperties.Package_name != nil {
812 fixNeeded = true
813 command.FlagWithInput("--manifest ", a.manifestPath).
814 FlagWithArg("--package-name ", *a.overridableAppProperties.Package_name)
815 }
816
817 if fixNeeded {
818 rule.Build(pctx, ctx, "fix_test_config", "fix test config")
819 return fixedConfig
820 }
821 return testConfig
822}
823
Colin Cross303e21f2018-08-07 16:49:25 -0700824func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -0700825 a.AndroidApp.DepsMutator(ctx)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700826}
827
828func (a *AndroidTest) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
829 a.AndroidApp.OverridablePropertiesDepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -0700830 if a.appTestProperties.Instrumentation_for != nil {
831 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
832 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
833 // use instrumentationForTag instead of libTag.
834 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
835 }
Colin Crossae5caf52018-05-22 11:11:52 -0700836}
837
Colin Cross1b16b0e2019-02-12 14:41:32 -0800838// android_test compiles test sources and Android resources into an Android application package `.apk` file and
839// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -0700840func AndroidTestFactory() android.Module {
841 module := &AndroidTest{}
842
Sasha Smundak2057f822019-04-16 17:16:58 -0700843 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross5067db92018-09-17 16:46:35 -0700844
845 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700846 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800847 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700848 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800849 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -0700850
851 module.AddProperties(
852 &module.Module.properties,
853 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800854 &module.Module.dexpreoptProperties,
Colin Crossae5caf52018-05-22 11:11:52 -0700855 &module.Module.protoProperties,
856 &module.aaptProperties,
857 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700858 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800859 &module.overridableAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700860 &module.usesLibrary.usesLibraryProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700861 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700862
Colin Crossa4f08812018-10-02 22:03:40 -0700863 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
864 android.InitDefaultableModule(module)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700865 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossae5caf52018-05-22 11:11:52 -0700866 return module
867}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700868
Colin Cross252fc6f2018-10-04 15:22:03 -0700869type appTestHelperAppProperties struct {
870 // list of compatibility suites (for example "cts", "vts") that the module should be
871 // installed into.
872 Test_suites []string `android:"arch_variant"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700873
874 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
875 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
876 // explicitly.
877 Auto_gen_config *bool
Colin Cross252fc6f2018-10-04 15:22:03 -0700878}
879
880type AndroidTestHelperApp struct {
881 AndroidApp
882
883 appTestHelperAppProperties appTestHelperAppProperties
884}
885
Jaewoong Jung326a9412019-11-21 10:41:00 -0800886func (a *AndroidTestHelperApp) InstallInTestcases() bool {
887 return true
888}
889
Colin Cross1b16b0e2019-02-12 14:41:32 -0800890// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
891// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
892// test.
Colin Cross252fc6f2018-10-04 15:22:03 -0700893func AndroidTestHelperAppFactory() android.Module {
894 module := &AndroidTestHelperApp{}
895
Sasha Smundak2057f822019-04-16 17:16:58 -0700896 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700897
898 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800899 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700900 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800901 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700902
903 module.AddProperties(
904 &module.Module.properties,
905 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800906 &module.Module.dexpreoptProperties,
Colin Cross252fc6f2018-10-04 15:22:03 -0700907 &module.Module.protoProperties,
908 &module.aaptProperties,
909 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800910 &module.appTestHelperAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700911 &module.overridableAppProperties,
912 &module.usesLibrary.usesLibraryProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -0700913
914 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
915 android.InitDefaultableModule(module)
Anton Hansson3d2b6b42020-01-10 15:06:01 +0000916 android.InitApexModule(module)
Colin Cross252fc6f2018-10-04 15:22:03 -0700917 return module
918}
919
Colin Crossbd01e2a2018-10-04 15:21:03 -0700920type AndroidAppCertificate struct {
921 android.ModuleBase
922 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900923 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -0700924}
925
926type AndroidAppCertificateProperties struct {
927 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
928 Certificate *string
929}
930
Colin Cross1b16b0e2019-02-12 14:41:32 -0800931// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
932// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -0700933func AndroidAppCertificateFactory() android.Module {
934 module := &AndroidAppCertificate{}
935 module.AddProperties(&module.properties)
936 android.InitAndroidModule(module)
937 return module
938}
939
Colin Crossbd01e2a2018-10-04 15:21:03 -0700940func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
941 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900942 c.Certificate = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -0800943 Pem: android.PathForModuleSrc(ctx, cert+".x509.pem"),
944 Key: android.PathForModuleSrc(ctx, cert+".pk8"),
Colin Crossbd01e2a2018-10-04 15:21:03 -0700945 }
946}
Jaewoong Jung525443a2019-02-28 15:35:54 -0800947
948type OverrideAndroidApp struct {
949 android.ModuleBase
950 android.OverrideModuleBase
951}
952
953func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
954 // All the overrides happen in the base module.
955 // TODO(jungjw): Check the base module type.
956}
957
958// override_android_app is used to create an android_app module based on another android_app by overriding
959// some of its properties.
960func OverrideAndroidAppModuleFactory() android.Module {
961 m := &OverrideAndroidApp{}
962 m.AddProperties(&overridableAppProperties{})
963
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700964 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800965 android.InitOverrideModule(m)
966 return m
967}
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700968
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700969type OverrideAndroidTest struct {
970 android.ModuleBase
971 android.OverrideModuleBase
972}
973
974func (i *OverrideAndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
975 // All the overrides happen in the base module.
976 // TODO(jungjw): Check the base module type.
977}
978
979// override_android_test is used to create an android_app module based on another android_test by overriding
980// some of its properties.
981func OverrideAndroidTestModuleFactory() android.Module {
982 m := &OverrideAndroidTest{}
983 m.AddProperties(&overridableAppProperties{})
984 m.AddProperties(&appTestProperties{})
985
986 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
987 android.InitOverrideModule(m)
988 return m
989}
990
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700991type AndroidAppImport struct {
992 android.ModuleBase
993 android.DefaultableModuleBase
994 prebuilt android.Prebuilt
995
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700996 properties AndroidAppImportProperties
997 dpiVariants interface{}
998 archVariants interface{}
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700999
1000 outputFile android.Path
Colin Cross503c1d02020-01-28 14:00:53 -08001001 certificate Certificate
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001002
1003 dexpreopter
Colin Cross50ddcc42019-05-16 12:28:22 -07001004
1005 usesLibrary usesLibrary
Jaewoong Jung8aae22e2019-07-17 10:21:49 -07001006
Colin Cross70dda7e2019-10-01 22:05:35 -07001007 installPath android.InstallPath
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001008}
1009
1010type AndroidAppImportProperties struct {
1011 // A prebuilt apk to import
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001012 Apk *string
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001013
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001014 // The name of a certificate in the default certificate directory or an android_app_certificate
1015 // module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001016 Certificate *string
1017
1018 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
1019 // be set for presigned modules.
1020 Presigned *bool
1021
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001022 // Sign with the default system dev certificate. Must be used judiciously. Most imported apps
1023 // need to either specify a specific certificate or be presigned.
1024 Default_dev_cert *bool
1025
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001026 // Specifies that this app should be installed to the priv-app directory,
1027 // where the system will grant it additional privileges not available to
1028 // normal apps.
1029 Privileged *bool
1030
1031 // Names of modules to be overridden. Listed modules can only be other binaries
1032 // (in Make or Soong).
1033 // This does not completely prevent installation of the overridden binaries, but if both
1034 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1035 // from PRODUCT_PACKAGES.
1036 Overrides []string
Jaewoong Jung8aae22e2019-07-17 10:21:49 -07001037
1038 // Optional name for the installed app. If unspecified, it is derived from the module name.
1039 Filename *string
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001040}
1041
Martin Stjernholm6d415272020-01-31 17:10:36 +00001042func (a *AndroidAppImport) IsInstallable() bool {
1043 return true
1044}
1045
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001046// Updates properties with variant-specific values.
1047func (a *AndroidAppImport) processVariants(ctx android.LoadHookContext) {
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001048 config := ctx.Config()
1049
1050 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants")
1051 // Try DPI variant matches in the reverse-priority order so that the highest priority match
1052 // overwrites everything else.
1053 // TODO(jungjw): Can we optimize this by making it priority order?
1054 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001055 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001056 }
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001057 if config.ProductAAPTPreferredConfig() != "" {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001058 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001059 }
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001060
1061 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName("Arch")
1062 archType := ctx.Config().Targets[android.Android][0].Arch.ArchType
1063 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001064}
1065
Colin Cross1184b642019-12-30 18:43:07 -08001066func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001067 dst interface{}, variantGroup reflect.Value, variant string) {
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001068 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
1069 if !src.IsValid() {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001070 return
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001071 }
1072
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001073 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
1074 if err != nil {
1075 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1076 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1077 } else {
1078 panic(err)
1079 }
1080 }
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001081}
1082
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001083func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
1084 cert := android.SrcIsModule(String(a.properties.Certificate))
1085 if cert != "" {
1086 ctx.AddDependency(ctx.Module(), certificateTag, cert)
1087 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001088
Paul Duffin250e6192019-06-07 10:44:37 +01001089 a.usesLibrary.deps(ctx, true)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001090}
1091
1092func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
1093 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001094 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
1095 // with them may invalidate pre-existing signature data.
1096 if ctx.InstallInTestcases() && Bool(a.properties.Presigned) {
1097 ctx.Build(pctx, android.BuildParams{
1098 Rule: android.Cp,
1099 Output: outputPath,
1100 Input: inputPath,
1101 })
1102 return
1103 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001104 rule := android.NewRuleBuilder()
1105 rule.Command().
1106 Textf(`if (zipinfo %s 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
Colin Crossee94d6a2019-07-08 17:08:34 -07001107 BuiltTool(ctx, "zip2zip").
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001108 FlagWithInput("-i ", inputPath).
1109 FlagWithOutput("-o ", outputPath).
1110 FlagWithArg("-0 ", "'lib/**/*.so'").
1111 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
1112 rule.Build(pctx, ctx, "uncompress-embedded-jni-libs", "Uncompress embedded JIN libs")
1113}
1114
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001115// Returns whether this module should have the dex file stored uncompressed in the APK.
1116func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
1117 if ctx.Config().UnbundledBuild() {
1118 return false
1119 }
1120
1121 // Uncompress dex in APKs of privileged apps
Jiyong Parkf7487312019-10-17 12:54:30 +09001122 if ctx.Config().UncompressPrivAppDex() && a.Privileged() {
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001123 return true
1124 }
1125
1126 return shouldUncompressDex(ctx, &a.dexpreopter)
1127}
1128
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001129func (a *AndroidAppImport) uncompressDex(
1130 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
1131 rule := android.NewRuleBuilder()
1132 rule.Command().
1133 Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
Colin Crossee94d6a2019-07-08 17:08:34 -07001134 BuiltTool(ctx, "zip2zip").
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001135 FlagWithInput("-i ", inputPath).
1136 FlagWithOutput("-o ", outputPath).
1137 FlagWithArg("-0 ", "'classes*.dex'").
1138 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
1139 rule.Build(pctx, ctx, "uncompress-dex", "Uncompress dex files")
1140}
1141
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001142func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001143 a.generateAndroidBuildActions(ctx)
1144}
1145
Jooyung Han39ee1192020-03-23 20:21:11 +09001146func (a *AndroidAppImport) InstallApkName() string {
1147 return a.BaseModuleName()
1148}
1149
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001150func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001151 numCertPropsSet := 0
1152 if String(a.properties.Certificate) != "" {
1153 numCertPropsSet++
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001154 }
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001155 if Bool(a.properties.Presigned) {
1156 numCertPropsSet++
1157 }
1158 if Bool(a.properties.Default_dev_cert) {
1159 numCertPropsSet++
1160 }
1161 if numCertPropsSet != 1 {
1162 ctx.ModuleErrorf("One and only one of certficate, presigned, and default_dev_cert properties must be set")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001163 }
1164
Colin Cross094cde42020-02-15 10:38:00 -08001165 _, certificates := collectAppDeps(ctx, false, false)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001166
1167 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001168 // TODO: LOCAL_PACKAGE_SPLITS
1169
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001170 srcApk := a.prebuilt.SingleSourcePath(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -07001171
1172 if a.usesLibrary.enforceUsesLibraries() {
1173 srcApk = a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk)
1174 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001175
1176 // TODO: Install or embed JNI libraries
1177
1178 // Uncompress JNI libraries in the apk
1179 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
1180 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
1181
Kyeongkab.Namc4997142019-11-22 11:38:16 +09001182 var installDir android.InstallPath
1183 if Bool(a.properties.Privileged) {
1184 installDir = android.PathForModuleInstall(ctx, "priv-app", a.BaseModuleName())
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001185 } else if ctx.InstallInTestcases() {
1186 installDir = android.PathForModuleInstall(ctx, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch())
Kyeongkab.Namc4997142019-11-22 11:38:16 +09001187 } else {
1188 installDir = android.PathForModuleInstall(ctx, "app", a.BaseModuleName())
1189 }
1190
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001191 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001192 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001193 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -07001194
1195 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
1196 a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
1197 a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
1198 a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
1199
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001200 dexOutput := a.dexpreopter.dexpreopt(ctx, jnisUncompressed)
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001201 if a.dexpreopter.uncompressedDex {
1202 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
1203 a.uncompressDex(ctx, dexOutput, dexUncompressed.OutputPath)
1204 dexOutput = dexUncompressed
1205 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001206
Jooyung Han39ee1192020-03-23 20:21:11 +09001207 apkFilename := proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk")
1208
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001209 // Sign or align the package
1210 // TODO: Handle EXTERNAL
1211 if !Bool(a.properties.Presigned) {
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001212 // If the certificate property is empty at this point, default_dev_cert must be set to true.
1213 // Which makes processMainCert's behavior for the empty cert string WAI.
1214 certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001215 if len(certificates) != 1 {
1216 ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates)
1217 }
Colin Cross503c1d02020-01-28 14:00:53 -08001218 a.certificate = certificates[0]
Jooyung Han39ee1192020-03-23 20:21:11 +09001219 signed := android.PathForModuleOut(ctx, "signed", apkFilename)
Songchun Fan17d69e32020-03-24 20:32:24 -07001220 SignAppPackage(ctx, signed, dexOutput, certificates, nil)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001221 a.outputFile = signed
1222 } else {
Jooyung Han39ee1192020-03-23 20:21:11 +09001223 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001224 TransformZipAlign(ctx, alignedApk, dexOutput)
1225 a.outputFile = alignedApk
Colin Cross503c1d02020-01-28 14:00:53 -08001226 a.certificate = presignedCertificate
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001227 }
1228
1229 // TODO: Optionally compress the output apk.
1230
Jooyung Han39ee1192020-03-23 20:21:11 +09001231 a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001232
1233 // TODO: androidmk converter jni libs
1234}
1235
1236func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
1237 return &a.prebuilt
1238}
1239
1240func (a *AndroidAppImport) Name() string {
1241 return a.prebuilt.Name(a.ModuleBase.Name())
1242}
1243
Dario Frenicde2a032019-10-27 00:29:22 +01001244func (a *AndroidAppImport) OutputFile() android.Path {
1245 return a.outputFile
1246}
1247
Jiyong Park618922e2020-01-08 13:35:43 +09001248func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
1249 return nil
1250}
1251
Colin Cross503c1d02020-01-28 14:00:53 -08001252func (a *AndroidAppImport) Certificate() Certificate {
1253 return a.certificate
1254}
1255
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001256var dpiVariantGroupType reflect.Type
1257var archVariantGroupType reflect.Type
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001258
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001259func initAndroidAppImportVariantGroupTypes() {
1260 dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants")
1261
1262 archNames := make([]string, len(android.ArchTypeList()))
1263 for i, archType := range android.ArchTypeList() {
1264 archNames[i] = archType.Name
1265 }
1266 archVariantGroupType = createVariantGroupType(archNames, "Arch")
1267}
1268
1269// Populates all variant struct properties at creation time.
1270func (a *AndroidAppImport) populateAllVariantStructs() {
1271 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
1272 a.AddProperties(a.dpiVariants)
1273
1274 a.archVariants = reflect.New(archVariantGroupType).Interface()
1275 a.AddProperties(a.archVariants)
1276}
1277
Jiyong Parkf7487312019-10-17 12:54:30 +09001278func (a *AndroidAppImport) Privileged() bool {
1279 return Bool(a.properties.Privileged)
1280}
1281
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001282func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
1283 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
1284
1285 variantFields := make([]reflect.StructField, len(variants))
1286 for i, variant := range variants {
1287 variantFields[i] = reflect.StructField{
1288 Name: proptools.FieldNameForProperty(variant),
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001289 Type: props,
1290 }
1291 }
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001292
1293 variantGroupStruct := reflect.StructOf(variantFields)
1294 return reflect.StructOf([]reflect.StructField{
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001295 {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001296 Name: variantGroupName,
1297 Type: variantGroupStruct,
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001298 },
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001299 })
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001300}
1301
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001302// android_app_import imports a prebuilt apk with additional processing specified in the module.
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001303// DPI-specific apk source files can be specified using dpi_variants. Example:
1304//
1305// android_app_import {
1306// name: "example_import",
1307// apk: "prebuilts/example.apk",
1308// dpi_variants: {
1309// mdpi: {
1310// apk: "prebuilts/example_mdpi.apk",
1311// },
1312// xhdpi: {
1313// apk: "prebuilts/example_xhdpi.apk",
1314// },
1315// },
1316// certificate: "PRESIGNED",
1317// }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001318func AndroidAppImportFactory() android.Module {
1319 module := &AndroidAppImport{}
1320 module.AddProperties(&module.properties)
1321 module.AddProperties(&module.dexpreoptProperties)
Colin Cross50ddcc42019-05-16 12:28:22 -07001322 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001323 module.populateAllVariantStructs()
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001324 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001325 module.processVariants(ctx)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001326 })
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001327
1328 InitJavaModule(module, android.DeviceSupported)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001329 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001330
1331 return module
1332}
Colin Cross50ddcc42019-05-16 12:28:22 -07001333
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001334type AndroidTestImport struct {
1335 AndroidAppImport
1336
1337 testProperties testProperties
1338
1339 data android.Paths
1340}
1341
1342func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1343 a.generateAndroidBuildActions(ctx)
1344
1345 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
1346}
1347
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001348func (a *AndroidTestImport) InstallInTestcases() bool {
1349 return true
1350}
1351
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001352// android_test_import imports a prebuilt test apk with additional processing specified in the
1353// module. DPI or arch variant configurations can be made as with android_app_import.
1354func AndroidTestImportFactory() android.Module {
1355 module := &AndroidTestImport{}
1356 module.AddProperties(&module.properties)
1357 module.AddProperties(&module.dexpreoptProperties)
1358 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
1359 module.AddProperties(&module.testProperties)
1360 module.populateAllVariantStructs()
1361 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
1362 module.processVariants(ctx)
1363 })
1364
1365 InitJavaModule(module, android.DeviceSupported)
1366 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
1367
1368 return module
1369}
1370
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001371type RuntimeResourceOverlay struct {
1372 android.ModuleBase
1373 android.DefaultableModuleBase
1374 aapt
1375
1376 properties RuntimeResourceOverlayProperties
1377
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08001378 certificate Certificate
1379
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001380 outputFile android.Path
1381 installDir android.InstallPath
1382}
1383
1384type RuntimeResourceOverlayProperties struct {
1385 // the name of a certificate in the default certificate directory or an android_app_certificate
1386 // module name in the form ":module".
1387 Certificate *string
1388
1389 // optional theme name. If specified, the overlay package will be applied
1390 // only when the ro.boot.vendor.overlay.theme system property is set to the same value.
1391 Theme *string
1392
1393 // if not blank, set to the version of the sdk to compile against.
1394 // Defaults to compiling against the current platform.
1395 Sdk_version *string
1396
1397 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
1398 // Defaults to sdk_version if not set.
1399 Min_sdk_version *string
Jaewoong Jungfe3c7f62020-04-09 16:15:30 -07001400
1401 // list of android_library modules whose resources are extracted and linked against statically
1402 Static_libs []string
1403
1404 // list of android_app modules whose resources are extracted and linked against
1405 Resource_libs []string
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001406}
1407
1408func (r *RuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
1409 sdkDep := decodeSdkDep(ctx, sdkContext(r))
1410 if sdkDep.hasFrameworkLibs() {
1411 r.aapt.deps(ctx, sdkDep)
1412 }
1413
1414 cert := android.SrcIsModule(String(r.properties.Certificate))
1415 if cert != "" {
1416 ctx.AddDependency(ctx.Module(), certificateTag, cert)
1417 }
Jaewoong Jungfe3c7f62020-04-09 16:15:30 -07001418
1419 ctx.AddVariationDependencies(nil, staticLibTag, r.properties.Static_libs...)
1420 ctx.AddVariationDependencies(nil, libTag, r.properties.Resource_libs...)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001421}
1422
1423func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1424 // Compile and link resources
1425 r.aapt.hasNoCode = true
Jaewoong Jungf0f747c2020-01-24 10:30:02 -08001426 // Do not remove resources without default values nor dedupe resource configurations with the same value
1427 r.aapt.buildActions(ctx, r, "--no-resource-deduping", "--no-resource-removal")
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001428
1429 // Sign the built package
Colin Cross094cde42020-02-15 10:38:00 -08001430 _, certificates := collectAppDeps(ctx, false, false)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001431 certificates = processMainCert(r.ModuleBase, String(r.properties.Certificate), certificates, ctx)
1432 signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
Songchun Fan17d69e32020-03-24 20:32:24 -07001433 SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil)
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08001434 r.certificate = certificates[0]
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001435
1436 r.outputFile = signed
1437 r.installDir = android.PathForModuleInstall(ctx, "overlay", String(r.properties.Theme))
1438 ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile)
1439}
1440
Jiyong Park6a927c42020-01-21 02:03:43 +09001441func (r *RuntimeResourceOverlay) sdkVersion() sdkSpec {
1442 return sdkSpecFrom(String(r.properties.Sdk_version))
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001443}
1444
1445func (r *RuntimeResourceOverlay) systemModules() string {
1446 return ""
1447}
1448
Jiyong Park6a927c42020-01-21 02:03:43 +09001449func (r *RuntimeResourceOverlay) minSdkVersion() sdkSpec {
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001450 if r.properties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +09001451 return sdkSpecFrom(*r.properties.Min_sdk_version)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001452 }
1453 return r.sdkVersion()
1454}
1455
Jiyong Park6a927c42020-01-21 02:03:43 +09001456func (r *RuntimeResourceOverlay) targetSdkVersion() sdkSpec {
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001457 return r.sdkVersion()
1458}
1459
1460// runtime_resource_overlay generates a resource-only apk file that can overlay application and
1461// system resources at run time.
1462func RuntimeResourceOverlayFactory() android.Module {
1463 module := &RuntimeResourceOverlay{}
1464 module.AddProperties(
1465 &module.properties,
1466 &module.aaptProperties)
1467
1468 InitJavaModule(module, android.DeviceSupported)
1469
1470 return module
1471}
1472
Colin Cross50ddcc42019-05-16 12:28:22 -07001473type UsesLibraryProperties struct {
1474 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file.
1475 Uses_libs []string
1476
1477 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file with
1478 // required=false.
1479 Optional_uses_libs []string
1480
1481 // If true, the list of uses_libs and optional_uses_libs modules must match the AndroidManifest.xml file. Defaults
1482 // to true if either uses_libs or optional_uses_libs is set. Will unconditionally default to true in the future.
1483 Enforce_uses_libs *bool
1484}
1485
1486// usesLibrary provides properties and helper functions for AndroidApp and AndroidAppImport to verify that the
1487// <uses-library> tags that end up in the manifest of an APK match the ones known to the build system through the
1488// uses_libs and optional_uses_libs properties. The build system's values are used by dexpreopt to preopt apps
1489// with knowledge of their shared libraries.
1490type usesLibrary struct {
1491 usesLibraryProperties UsesLibraryProperties
1492}
1493
Paul Duffin250e6192019-06-07 10:44:37 +01001494func (u *usesLibrary) deps(ctx android.BottomUpMutatorContext, hasFrameworkLibs bool) {
Colin Cross3245b2c2019-06-07 13:18:09 -07001495 if !ctx.Config().UnbundledBuild() {
1496 ctx.AddVariationDependencies(nil, usesLibTag, u.usesLibraryProperties.Uses_libs...)
1497 ctx.AddVariationDependencies(nil, usesLibTag, u.presentOptionalUsesLibs(ctx)...)
Paul Duffin250e6192019-06-07 10:44:37 +01001498 // Only add these extra dependencies if the module depends on framework libs. This avoids
1499 // creating a cyclic dependency:
1500 // e.g. framework-res -> org.apache.http.legacy -> ... -> framework-res.
1501 if hasFrameworkLibs {
Colin Cross3245b2c2019-06-07 13:18:09 -07001502 // dexpreopt/dexpreopt.go needs the paths to the dex jars of these libraries in case construct_context.sh needs
1503 // to pass them to dex2oat. Add them as a dependency so we can determine the path to the dex jar of each
1504 // library to dexpreopt.
1505 ctx.AddVariationDependencies(nil, usesLibTag,
1506 "org.apache.http.legacy",
1507 "android.hidl.base-V1.0-java",
1508 "android.hidl.manager-V1.0-java")
1509 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001510 }
1511}
1512
1513// presentOptionalUsesLibs returns optional_uses_libs after filtering out MissingUsesLibraries, which don't exist in the
1514// build.
1515func (u *usesLibrary) presentOptionalUsesLibs(ctx android.BaseModuleContext) []string {
1516 optionalUsesLibs, _ := android.FilterList(u.usesLibraryProperties.Optional_uses_libs, ctx.Config().MissingUsesLibraries())
1517 return optionalUsesLibs
1518}
1519
1520// usesLibraryPaths returns a map of module names of shared library dependencies to the paths to their dex jars.
1521func (u *usesLibrary) usesLibraryPaths(ctx android.ModuleContext) map[string]android.Path {
1522 usesLibPaths := make(map[string]android.Path)
1523
1524 if !ctx.Config().UnbundledBuild() {
1525 ctx.VisitDirectDepsWithTag(usesLibTag, func(m android.Module) {
1526 if lib, ok := m.(Dependency); ok {
1527 if dexJar := lib.DexJar(); dexJar != nil {
1528 usesLibPaths[ctx.OtherModuleName(m)] = dexJar
1529 } else {
1530 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must produce a dex jar, does it have installable: true?",
1531 ctx.OtherModuleName(m))
1532 }
1533 } else if ctx.Config().AllowMissingDependencies() {
1534 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(m)})
1535 } else {
1536 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must be a java library",
1537 ctx.OtherModuleName(m))
1538 }
1539 })
1540 }
1541
1542 return usesLibPaths
1543}
1544
1545// enforceUsesLibraries returns true of <uses-library> tags should be checked against uses_libs and optional_uses_libs
1546// properties. Defaults to true if either of uses_libs or optional_uses_libs is specified. Will default to true
1547// unconditionally in the future.
1548func (u *usesLibrary) enforceUsesLibraries() bool {
1549 defaultEnforceUsesLibs := len(u.usesLibraryProperties.Uses_libs) > 0 ||
1550 len(u.usesLibraryProperties.Optional_uses_libs) > 0
1551 return BoolDefault(u.usesLibraryProperties.Enforce_uses_libs, defaultEnforceUsesLibs)
1552}
1553
1554// verifyUsesLibrariesManifest checks the <uses-library> tags in an AndroidManifest.xml against the ones specified
1555// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the manifest.
1556func (u *usesLibrary) verifyUsesLibrariesManifest(ctx android.ModuleContext, manifest android.Path) android.Path {
1557 outputFile := android.PathForModuleOut(ctx, "manifest_check", "AndroidManifest.xml")
1558
1559 rule := android.NewRuleBuilder()
Colin Crossee94d6a2019-07-08 17:08:34 -07001560 cmd := rule.Command().BuiltTool(ctx, "manifest_check").
Colin Cross50ddcc42019-05-16 12:28:22 -07001561 Flag("--enforce-uses-libraries").
1562 Input(manifest).
1563 FlagWithOutput("-o ", outputFile)
1564
1565 for _, lib := range u.usesLibraryProperties.Uses_libs {
1566 cmd.FlagWithArg("--uses-library ", lib)
1567 }
1568
1569 for _, lib := range u.usesLibraryProperties.Optional_uses_libs {
1570 cmd.FlagWithArg("--optional-uses-library ", lib)
1571 }
1572
1573 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1574
1575 return outputFile
1576}
1577
1578// verifyUsesLibrariesAPK checks the <uses-library> tags in the manifest of an APK against the ones specified
1579// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the APK.
1580func (u *usesLibrary) verifyUsesLibrariesAPK(ctx android.ModuleContext, apk android.Path) android.Path {
1581 outputFile := android.PathForModuleOut(ctx, "verify_uses_libraries", apk.Base())
1582
1583 rule := android.NewRuleBuilder()
1584 aapt := ctx.Config().HostToolPath(ctx, "aapt")
1585 rule.Command().
1586 Textf("aapt_binary=%s", aapt.String()).Implicit(aapt).
1587 Textf(`uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Uses_libs, " ")).
1588 Textf(`optional_uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Optional_uses_libs, " ")).
1589 Tool(android.PathForSource(ctx, "build/make/core/verify_uses_libraries.sh")).Input(apk)
1590 rule.Command().Text("cp -f").Input(apk).Output(outputFile)
1591
1592 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1593
1594 return outputFile
1595}