blob: 4468dc54354f30686a3439b1f295c14c1f0265b3 [file] [log] [blame]
Colin Cross30e076a2015-04-13 13:58:27 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Android apps.
18
19import (
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070020 "path/filepath"
21 "reflect"
Jaewoong Jung5b425e22019-06-17 17:40:56 -070022 "sort"
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070023 "strings"
Colin Cross30e076a2015-04-13 13:58:27 -070024
Colin Cross50ddcc42019-05-16 12:28:22 -070025 "github.com/google/blueprint"
26 "github.com/google/blueprint/proptools"
27
Colin Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Colin Crossa4f08812018-10-02 22:03:40 -070029 "android/soong/cc"
Colin Cross303e21f2018-08-07 16:49:25 -070030 "android/soong/tradefed"
Colin Cross30e076a2015-04-13 13:58:27 -070031)
32
Jaewoong Jung3e18b192019-06-11 12:25:34 -070033var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070034
Colin Cross3bc7ffa2017-11-22 16:19:37 -080035func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000036 RegisterAppBuildComponents(android.InitRegistrationContext)
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -070037
38 initAndroidAppImportVariantGroupTypes()
Colin Cross3bc7ffa2017-11-22 16:19:37 -080039}
40
Paul Duffinf9b1da02019-12-18 19:51:55 +000041func RegisterAppBuildComponents(ctx android.RegistrationContext) {
42 ctx.RegisterModuleType("android_app", AndroidAppFactory)
43 ctx.RegisterModuleType("android_test", AndroidTestFactory)
44 ctx.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
45 ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
46 ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
47 ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory)
Roshan Piusb8307962020-04-27 09:42:27 -070048 ctx.RegisterModuleType("override_runtime_resource_overlay", OverrideRuntimeResourceOverlayModuleFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000049 ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory)
50 ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -080051 ctx.RegisterModuleType("runtime_resource_overlay", RuntimeResourceOverlayFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000052}
53
Colin Cross30e076a2015-04-13 13:58:27 -070054// AndroidManifest.xml merging
55// package splits
56
Colin Crossfabb6082018-02-20 17:22:23 -080057type appProperties struct {
Colin Crossbd01e2a2018-10-04 15:21:03 -070058 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
Colin Cross7d5136f2015-05-11 13:39:40 -070059 Additional_certificates []string
60
61 // If set, create package-export.apk, which other packages can
62 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
Nan Zhangea568a42017-11-08 21:20:04 -080063 Export_package_resources *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070064
Colin Cross16056062017-12-13 22:46:28 -080065 // Specifies that this app should be installed to the priv-app directory,
66 // where the system will grant it additional privileges not available to
67 // normal apps.
68 Privileged *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070069
70 // list of resource labels to generate individual resource packages
71 Package_splits []string
Jason Monkd4122be2018-08-10 09:33:36 -040072
73 // Names of modules to be overridden. Listed modules can only be other binaries
74 // (in Make or Soong).
75 // This does not completely prevent installation of the overridden binaries, but if both
76 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
77 // from PRODUCT_PACKAGES.
78 Overrides []string
Colin Crossa4f08812018-10-02 22:03:40 -070079
80 // list of native libraries that will be provided in or alongside the resulting jar
81 Jni_libs []string `android:"arch_variant"`
82
Colin Crossee87c602020-02-19 16:57:15 -080083 // if true, allow JNI libraries that link against platform APIs even if this module sets
84 // sdk_version.
85 Jni_uses_platform_apis *bool
86
Jaewoong Jungbc625cd2019-05-06 15:48:44 -070087 // STL library to use for JNI libraries.
88 Stl *string `android:"arch_variant"`
89
Colin Crosse4246ab2019-02-05 21:55:21 -080090 // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest
91 // flag so that they are used from inside the APK at runtime. Defaults to true for android_test modules unless
Jiyong Park52cd06f2019-11-11 10:14:32 +090092 // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to true for
93 // android_app modules that are embedded to APEXes, defaults to false for other module types where the native
94 // libraries are generally preinstalled outside the APK.
Colin Crosse4246ab2019-02-05 21:55:21 -080095 Use_embedded_native_libs *bool
Colin Cross46abdad2019-02-07 13:07:08 -080096
97 // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that
98 // they are used from inside the APK at runtime.
99 Use_embedded_dex *bool
Colin Cross47fa9d32019-03-26 10:51:39 -0700100
101 // Forces native libraries to always be packaged into the APK,
102 // Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed.
103 // True for android_test* modules.
104 AlwaysPackageNativeLibs bool `blueprint:"mutated"`
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700105
106 // If set, find and merge all NOTICE files that this module and its dependencies have and store
107 // it in the APK as an asset.
108 Embed_notices *bool
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700109
110 // cc.Coverage related properties
111 PreventInstall bool `blueprint:"mutated"`
112 HideFromMake bool `blueprint:"mutated"`
113 IsCoverageVariant bool `blueprint:"mutated"`
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100114
115 // Whether this app is considered mainline updatable or not. When set to true, this will enforce
116 // additional rules for making sure that the APK is truly updatable. Default is false.
117 Updatable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700118}
119
Jaewoong Jung525443a2019-02-28 15:35:54 -0800120// android_app properties that can be overridden by override_android_app
121type overridableAppProperties struct {
122 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
123 // or an android_app_certificate module name in the form ":module".
124 Certificate *string
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700125
126 // the package name of this app. The package name in the manifest file is used if one was not given.
127 Package_name *string
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800128
129 // the logging parent of this app.
130 Logging_parent *string
Jaewoong Jung525443a2019-02-28 15:35:54 -0800131}
132
Roshan Piusb8307962020-04-27 09:42:27 -0700133// runtime_resource_overlay properties that can be overridden by override_runtime_resource_overlay
134type OverridableRuntimeResourceOverlayProperties struct {
135 // the package name of this app. The package name in the manifest file is used if one was not given.
136 Package_name *string
137
138 // the target package name of this overlay app. The target package name in the manifest file is used if one was not given.
139 Target_package_name *string
140}
141
Colin Cross30e076a2015-04-13 13:58:27 -0700142type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -0700143 Library
144 aapt
Jaewoong Jung525443a2019-02-28 15:35:54 -0800145 android.OverridableModuleBase
Colin Crossa97c5d32018-03-28 14:58:31 -0700146
Colin Cross50ddcc42019-05-16 12:28:22 -0700147 usesLibrary usesLibrary
148
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900149 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -0700150
Colin Crossfabb6082018-02-20 17:22:23 -0800151 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -0700152
Jaewoong Jung525443a2019-02-28 15:35:54 -0800153 overridableAppProperties overridableAppProperties
154
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700155 installJniLibs []jniLib
156 jniCoverageOutputs android.Paths
Colin Crossf6237212018-10-29 23:14:58 -0700157
158 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800159
160 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
161 installApkName string
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800162
Colin Cross70dda7e2019-10-01 22:05:35 -0700163 installDir android.InstallPath
Jaewoong Jung0949f312019-09-11 10:25:18 -0700164
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700165 onDeviceDir string
166
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800167 additionalAaptFlags []string
Jaewoong Jung98772792019-07-01 17:15:13 -0700168
169 noticeOutputs android.NoticeOutputs
Jiyong Parkaf8998c2020-02-28 16:51:07 +0900170
171 overriddenManifestPackageName string
Colin Crosse1731a52017-12-14 11:22:55 -0800172}
173
Martin Stjernholm6d415272020-01-31 17:10:36 +0000174func (a *AndroidApp) IsInstallable() bool {
175 return Bool(a.properties.Installable)
176}
177
Colin Cross89c31582018-04-30 15:55:11 -0700178func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
179 return nil
180}
181
Colin Cross66f78822018-05-02 12:58:28 -0700182func (a *AndroidApp) ExportedStaticPackages() android.Paths {
183 return nil
184}
185
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900186func (a *AndroidApp) OutputFile() android.Path {
187 return a.outputFile
188}
189
Colin Cross503c1d02020-01-28 14:00:53 -0800190func (a *AndroidApp) Certificate() Certificate {
191 return a.certificate
192}
193
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700194func (a *AndroidApp) JniCoverageOutputs() android.Paths {
195 return a.jniCoverageOutputs
196}
197
Colin Crossa97c5d32018-03-28 14:58:31 -0700198var _ AndroidLibraryDependency = (*AndroidApp)(nil)
199
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900200type Certificate struct {
Colin Cross503c1d02020-01-28 14:00:53 -0800201 Pem, Key android.Path
202 presigned bool
203}
204
205var presignedCertificate = Certificate{presigned: true}
206
207func (c Certificate) AndroidMkString() string {
208 if c.presigned {
209 return "PRESIGNED"
210 } else {
211 return c.Pem.String()
212 }
Colin Cross30e076a2015-04-13 13:58:27 -0700213}
214
Colin Cross46c9b8b2017-06-22 16:51:17 -0700215func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
216 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700217
Jiyong Park6a927c42020-01-21 02:03:43 +0900218 if String(a.appProperties.Stl) == "c++_shared" && !a.sdkVersion().specified() {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700219 ctx.PropertyErrorf("stl", "sdk_version must be set in order to use c++_shared")
220 }
221
Paul Duffin250e6192019-06-07 10:44:37 +0100222 sdkDep := decodeSdkDep(ctx, sdkContext(a))
223 if sdkDep.hasFrameworkLibs() {
224 a.aapt.deps(ctx, sdkDep)
Colin Cross30e076a2015-04-13 13:58:27 -0700225 }
Colin Crossa4f08812018-10-02 22:03:40 -0700226
Peter Collingbournead84f972019-12-17 16:46:18 -0800227 tag := &jniDependencyTag{}
Colin Crossa4f08812018-10-02 22:03:40 -0700228 for _, jniTarget := range ctx.MultiTargets() {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700229 variation := append(jniTarget.Variations(),
230 blueprint.Variation{Mutator: "link", Variation: "shared"})
Colin Crossa4f08812018-10-02 22:03:40 -0700231 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
232 }
Colin Cross50ddcc42019-05-16 12:28:22 -0700233
Paul Duffin250e6192019-06-07 10:44:37 +0100234 a.usesLibrary.deps(ctx, sdkDep.hasFrameworkLibs())
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700235}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700236
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700237func (a *AndroidApp) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800238 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700239 if cert != "" {
240 ctx.AddDependency(ctx.Module(), certificateTag, cert)
241 }
242
243 for _, cert := range a.appProperties.Additional_certificates {
244 cert = android.SrcIsModule(cert)
245 if cert != "" {
246 ctx.AddDependency(ctx.Module(), certificateTag, cert)
247 } else {
248 ctx.PropertyErrorf("additional_certificates",
249 `must be names of android_app_certificate modules in the form ":module"`)
250 }
251 }
Colin Cross30e076a2015-04-13 13:58:27 -0700252}
253
Jeongik Cha538c0d02019-07-11 15:54:27 +0900254func (a *AndroidTestHelperApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
255 a.generateAndroidBuildActions(ctx)
256}
257
Colin Cross46c9b8b2017-06-22 16:51:17 -0700258func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100259 a.checkAppSdkVersions(ctx)
Colin Crossae5caf52018-05-22 11:11:52 -0700260 a.generateAndroidBuildActions(ctx)
261}
262
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100263func (a *AndroidApp) checkAppSdkVersions(ctx android.ModuleContext) {
264 if Bool(a.appProperties.Updatable) {
265 if !a.sdkVersion().stable() {
266 ctx.PropertyErrorf("sdk_version", "Updatable apps must use stable SDKs, found %v", a.sdkVersion())
267 }
268 }
269
270 a.checkPlatformAPI(ctx)
271 a.checkSdkVersions(ctx)
272}
273
Sasha Smundak6ad77252019-05-01 13:16:22 -0700274// Returns true if the native libraries should be stored in the APK uncompressed and the
Colin Crosse4246ab2019-02-05 21:55:21 -0800275// extractNativeLibs application flag should be set to false in the manifest.
Sasha Smundak6ad77252019-05-01 13:16:22 -0700276func (a *AndroidApp) useEmbeddedNativeLibs(ctx android.ModuleContext) bool {
Jiyong Park6a927c42020-01-21 02:03:43 +0900277 minSdkVersion, err := a.minSdkVersion().effectiveVersion(ctx)
Colin Crosse4246ab2019-02-05 21:55:21 -0800278 if err != nil {
279 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
280 }
281
Jiyong Park52cd06f2019-11-11 10:14:32 +0900282 return (minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)) ||
283 !a.IsForPlatform()
Colin Crosse4246ab2019-02-05 21:55:21 -0800284}
285
Colin Cross43f08db2018-11-12 10:13:39 -0800286// Returns whether this module should have the dex file stored uncompressed in the APK.
287func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800288 if Bool(a.appProperties.Use_embedded_dex) {
289 return true
290 }
291
Colin Cross53a87f52019-06-25 13:35:30 -0700292 // Uncompress dex in APKs of privileged apps (even for unbundled builds, they may
293 // be preinstalled as prebuilts).
Jiyong Parkf7487312019-10-17 12:54:30 +0900294 if ctx.Config().UncompressPrivAppDex() && a.Privileged() {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000295 return true
296 }
297
Colin Cross53a87f52019-06-25 13:35:30 -0700298 if ctx.Config().UnbundledBuild() {
299 return false
300 }
301
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700302 return shouldUncompressDex(ctx, &a.dexpreopter)
Colin Cross5a0dcd52018-10-05 14:20:06 -0700303}
304
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700305func (a *AndroidApp) shouldEmbedJnis(ctx android.BaseModuleContext) bool {
306 return ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) ||
Jiyong Park52cd06f2019-11-11 10:14:32 +0900307 !a.IsForPlatform() || a.appProperties.AlwaysPackageNativeLibs
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700308}
309
Jiyong Parkaf8998c2020-02-28 16:51:07 +0900310func (a *AndroidApp) OverriddenManifestPackageName() string {
311 return a.overriddenManifestPackageName
312}
313
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800314func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
David Brazdild25060a2019-02-18 18:24:16 +0000315 a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis)
316
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700317 // Ask manifest_fixer to add or update the application element indicating this app has no code.
318 a.aapt.hasNoCode = !a.hasCode(ctx)
319
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800320 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800321
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800322 // 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 -0800323 hasProduct := android.PrefixInList(a.aaptProperties.Aaptflags, "--product")
Colin Crosse78dcd32018-04-19 15:25:19 -0700324 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800325 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700326 }
327
Dan Willemsen72be5902018-10-24 20:24:57 -0700328 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
329 // Product AAPT config
330 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800331 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700332 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700333
Dan Willemsen72be5902018-10-24 20:24:57 -0700334 // Product AAPT preferred config
335 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800336 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700337 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700338 }
339
Jiyong Park7f67f482019-01-05 12:57:48 +0900340 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700341 if overridden || a.overridableAppProperties.Package_name != nil {
342 // The product override variable has a priority over the package_name property.
343 if !overridden {
344 manifestPackageName = *a.overridableAppProperties.Package_name
345 }
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800346 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
Jiyong Parkaf8998c2020-02-28 16:51:07 +0900347 a.overriddenManifestPackageName = manifestPackageName
Jiyong Park7f67f482019-01-05 12:57:48 +0900348 }
349
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800350 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
351
Colin Crosse560c4a2019-03-19 16:03:11 -0700352 a.aapt.splitNames = a.appProperties.Package_splits
Colin Cross50ddcc42019-05-16 12:28:22 -0700353 a.aapt.sdkLibraries = a.exportedSdkLibs
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800354 a.aapt.LoggingParent = String(a.overridableAppProperties.Logging_parent)
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800355 a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700356
Colin Cross46c9b8b2017-06-22 16:51:17 -0700357 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700358 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800359}
Colin Cross30e076a2015-04-13 13:58:27 -0700360
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800361func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700362 var staticLibProguardFlagFiles android.Paths
363 ctx.VisitDirectDeps(func(m android.Module) {
364 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
365 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
366 }
367 })
368
369 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
370
371 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
372 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800373}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800374
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800375func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross43f08db2018-11-12 10:13:39 -0800376
377 var installDir string
378 if ctx.ModuleName() == "framework-res" {
379 // framework-res.apk is installed as system/framework/framework-res.apk
380 installDir = "framework"
Jiyong Parkf7487312019-10-17 12:54:30 +0900381 } else if a.Privileged() {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800382 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800383 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800384 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800385 }
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800386 a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000387 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -0700388
389 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
390 a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
391 a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
392 a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
393 a.dexpreopter.manifestFile = a.mergedManifestFile
394
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000395 a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex
Colin Cross5a0dcd52018-10-05 14:20:06 -0700396
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800397 if ctx.ModuleName() != "framework-res" {
398 a.Module.compile(ctx, a.aaptSrcJar)
399 }
Colin Cross30e076a2015-04-13 13:58:27 -0700400
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800401 return a.maybeStrippedDexJarFile
402}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800403
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800404func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700405 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700406 if len(jniLibs) > 0 {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700407 if a.shouldEmbedJnis(ctx) {
Colin Crossa4f08812018-10-02 22:03:40 -0700408 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Sasha Smundak6ad77252019-05-01 13:16:22 -0700409 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.useEmbeddedNativeLibs(ctx))
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700410 for _, jni := range jniLibs {
411 if jni.coverageFile.Valid() {
Jaewoong Junge62e5942020-04-07 13:07:55 -0700412 // Only collect coverage for the first target arch if this is a multilib target.
413 // TODO(jungjw): Ideally, we want to collect both reports, but that would cause coverage
414 // data file path collisions since the current coverage file path format doesn't contain
415 // arch-related strings. This is fine for now though; the code coverage team doesn't use
416 // multi-arch targets such as test_suite_* for coverage collections yet.
417 //
418 // Work with the team to come up with a new format that handles multilib modules properly
419 // and change this.
420 if len(ctx.Config().Targets[android.Android]) == 1 ||
421 ctx.Config().Targets[android.Android][0].Arch.ArchType == jni.target.Arch.ArchType {
422 a.jniCoverageOutputs = append(a.jniCoverageOutputs, jni.coverageFile.Path())
423 }
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700424 }
425 }
Colin Crossa4f08812018-10-02 22:03:40 -0700426 } else {
427 a.installJniLibs = jniLibs
428 }
429 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800430 return jniJarFile
431}
Colin Crossa4f08812018-10-02 22:03:40 -0700432
Jaewoong Jung0949f312019-09-11 10:25:18 -0700433func (a *AndroidApp) noticeBuildActions(ctx android.ModuleContext) {
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700434 // Collect NOTICE files from all dependencies.
435 seenModules := make(map[android.Module]bool)
436 noticePathSet := make(map[android.Path]bool)
437
438 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
439 // Have we already seen this?
440 if _, ok := seenModules[child]; ok {
441 return false
442 }
443 seenModules[child] = true
444
445 // Skip host modules.
446 if child.Target().Os.Class == android.Host || child.Target().Os.Class == android.HostCross {
447 return false
448 }
449
450 path := child.(android.Module).NoticeFile()
451 if path.Valid() {
452 noticePathSet[path.Path()] = true
453 }
454 return true
455 })
456
457 // If the app has one, add it too.
458 if a.NoticeFile().Valid() {
459 noticePathSet[a.NoticeFile().Path()] = true
460 }
461
462 if len(noticePathSet) == 0 {
Jaewoong Jung98772792019-07-01 17:15:13 -0700463 return
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700464 }
465 var noticePaths []android.Path
466 for path := range noticePathSet {
467 noticePaths = append(noticePaths, path)
468 }
469 sort.Slice(noticePaths, func(i, j int) bool {
470 return noticePaths[i].String() < noticePaths[j].String()
471 })
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700472
Jaewoong Jung0949f312019-09-11 10:25:18 -0700473 a.noticeOutputs = android.BuildNoticeOutput(ctx, a.installDir, a.installApkName+".apk", noticePaths)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700474}
475
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700476// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
477// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
478func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate {
479 if android.SrcIsModule(certPropValue) == "" {
480 var mainCert Certificate
481 if certPropValue != "" {
482 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
483 mainCert = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -0800484 Pem: defaultDir.Join(ctx, certPropValue+".x509.pem"),
485 Key: defaultDir.Join(ctx, certPropValue+".pk8"),
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700486 }
487 } else {
488 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Colin Cross503c1d02020-01-28 14:00:53 -0800489 mainCert = Certificate{
490 Pem: pem,
491 Key: key,
492 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700493 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700494 certificates = append([]Certificate{mainCert}, certificates...)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700495 }
496
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700497 if !m.Platform() {
498 certPath := certificates[0].Pem.String()
Jeongik Chac9464142019-01-07 12:07:27 +0900499 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
500 if strings.HasPrefix(certPath, systemCertPath) {
501 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
502 whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
503
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700504 if enforceSystemCert && !inList(m.Name(), whitelist) {
Jeongik Chac9464142019-01-07 12:07:27 +0900505 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
506 }
507 }
508 }
509
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700510 return certificates
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800511}
512
Jooyung Han65cd0f02020-03-23 20:21:11 +0900513func (a *AndroidApp) InstallApkName() string {
514 return a.installApkName
515}
516
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800517func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross50ddcc42019-05-16 12:28:22 -0700518 var apkDeps android.Paths
519
Jeongik Cha538c0d02019-07-11 15:54:27 +0900520 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
521 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
522
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800523 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800524 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800525
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700526 if ctx.ModuleName() == "framework-res" {
527 // framework-res.apk is installed as system/framework/framework-res.apk
Jaewoong Jung0949f312019-09-11 10:25:18 -0700528 a.installDir = android.PathForModuleInstall(ctx, "framework")
Jiyong Parkf7487312019-10-17 12:54:30 +0900529 } else if a.Privileged() {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700530 a.installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
531 } else if ctx.InstallInTestcases() {
Jaewoong Jung326a9412019-11-21 10:41:00 -0800532 a.installDir = android.PathForModuleInstall(ctx, a.installApkName, ctx.DeviceConfig().DeviceArch())
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700533 } else {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700534 a.installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700535 }
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700536 a.onDeviceDir = android.InstallPathToOnDevicePath(ctx, a.installDir)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700537
Jaewoong Jung0949f312019-09-11 10:25:18 -0700538 a.noticeBuildActions(ctx)
Jaewoong Jung98772792019-07-01 17:15:13 -0700539 if Bool(a.appProperties.Embed_notices) || ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") {
540 a.aapt.noticeFile = a.noticeOutputs.HtmlGzOutput
541 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700542
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800543 // Process all building blocks, from AAPT to certificates.
544 a.aaptBuildActions(ctx)
545
Colin Cross50ddcc42019-05-16 12:28:22 -0700546 if a.usesLibrary.enforceUsesLibraries() {
547 manifestCheckFile := a.usesLibrary.verifyUsesLibrariesManifest(ctx, a.mergedManifestFile)
548 apkDeps = append(apkDeps, manifestCheckFile)
549 }
550
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800551 a.proguardBuildActions(ctx)
552
553 dexJarFile := a.dexBuildActions(ctx)
554
Peter Collingbournead84f972019-12-17 16:46:18 -0800555 jniLibs, certificateDeps := collectAppDeps(ctx, a.shouldEmbedJnis(ctx))
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800556 jniJarFile := a.jniBuildActions(jniLibs, ctx)
557
558 if ctx.Failed() {
559 return
560 }
561
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700562 certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx)
563 a.certificate = certificates[0]
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800564
565 // Build a final signed app package.
Jaewoong Jung5a498812019-11-07 14:14:38 -0800566 packageFile := android.PathForModuleOut(ctx, a.installApkName+".apk")
Songchun Fan688de9a2020-03-24 20:32:24 -0700567 v4SigningRequested := Bool(a.Module.deviceProperties.V4_signature)
568 var v4SignatureFile android.WritablePath = nil
569 if v4SigningRequested {
570 v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+".apk.idsig")
571 }
572 CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates, apkDeps, v4SignatureFile)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800573 a.outputFile = packageFile
Songchun Fan688de9a2020-03-24 20:32:24 -0700574 if v4SigningRequested {
575 a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
576 }
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800577
Colin Crosse560c4a2019-03-19 16:03:11 -0700578 for _, split := range a.aapt.splits {
579 // Sign the split APKs
Jaewoong Jung5a498812019-11-07 14:14:38 -0800580 packageFile := android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk")
Songchun Fan688de9a2020-03-24 20:32:24 -0700581 if v4SigningRequested {
582 v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk.idsig")
583 }
584 CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates, apkDeps, v4SignatureFile)
Colin Crosse560c4a2019-03-19 16:03:11 -0700585 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
Songchun Fan688de9a2020-03-24 20:32:24 -0700586 if v4SigningRequested {
587 a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
588 }
Colin Crosse560c4a2019-03-19 16:03:11 -0700589 }
590
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800591 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700592 bundleFile := android.PathForModuleOut(ctx, "base.zip")
593 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
594 a.bundleFile = bundleFile
595
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800596 // Install the app package.
Jiyong Park8ba50f92019-11-13 15:01:01 +0900597 if (Bool(a.Module.properties.Installable) || ctx.Host()) && a.IsForPlatform() {
598 ctx.InstallFile(a.installDir, a.outputFile.Base(), a.outputFile)
599 for _, extra := range a.extraOutputFiles {
600 ctx.InstallFile(a.installDir, extra.Base(), extra)
601 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800602 }
Colin Cross30e076a2015-04-13 13:58:27 -0700603}
604
Peter Collingbournead84f972019-12-17 16:46:18 -0800605func collectAppDeps(ctx android.ModuleContext, shouldCollectRecursiveNativeDeps bool) ([]jniLib, []Certificate) {
Colin Crossa4f08812018-10-02 22:03:40 -0700606 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900607 var certificates []Certificate
Peter Collingbournead84f972019-12-17 16:46:18 -0800608 seenModulePaths := make(map[string]bool)
Colin Crossa4f08812018-10-02 22:03:40 -0700609
Peter Collingbournead84f972019-12-17 16:46:18 -0800610 ctx.WalkDeps(func(module android.Module, parent android.Module) bool {
Colin Crossa4f08812018-10-02 22:03:40 -0700611 otherName := ctx.OtherModuleName(module)
612 tag := ctx.OtherModuleDependencyTag(module)
613
Peter Collingbournead84f972019-12-17 16:46:18 -0800614 if IsJniDepTag(tag) || tag == cc.SharedDepTag {
Colin Crossa4f08812018-10-02 22:03:40 -0700615 if dep, ok := module.(*cc.Module); ok {
Peter Collingbournead84f972019-12-17 16:46:18 -0800616 if dep.IsNdk() || dep.IsStubs() {
617 return false
618 }
619
Colin Crossa4f08812018-10-02 22:03:40 -0700620 lib := dep.OutputFile()
Peter Collingbournead84f972019-12-17 16:46:18 -0800621 path := lib.Path()
622 if seenModulePaths[path.String()] {
623 return false
624 }
625 seenModulePaths[path.String()] = true
626
Colin Crossa4f08812018-10-02 22:03:40 -0700627 if lib.Valid() {
628 jniLibs = append(jniLibs, jniLib{
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700629 name: ctx.OtherModuleName(module),
630 path: path,
631 target: module.Target(),
632 coverageFile: dep.CoverageOutputFile(),
Colin Crossa4f08812018-10-02 22:03:40 -0700633 })
634 } else {
635 ctx.ModuleErrorf("dependency %q missing output file", otherName)
636 }
637 } else {
638 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
Colin Crossa4f08812018-10-02 22:03:40 -0700639 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800640
641 return shouldCollectRecursiveNativeDeps
642 }
643
644 if tag == certificateTag {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700645 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900646 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700647 } else {
648 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
649 }
Colin Crossa4f08812018-10-02 22:03:40 -0700650 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800651
652 return false
Colin Crossa4f08812018-10-02 22:03:40 -0700653 })
654
Colin Crossbd01e2a2018-10-04 15:21:03 -0700655 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700656}
657
Colin Cross0ea8ba82019-06-06 14:33:29 -0700658func (a *AndroidApp) getCertString(ctx android.BaseModuleContext) string {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800659 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
660 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000661 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800662 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800663 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800664}
665
Jiyong Park0f80c182020-01-31 02:49:53 +0900666func (a *AndroidApp) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
667 if IsJniDepTag(ctx.OtherModuleDependencyTag(dep)) {
668 return true
669 }
670 return a.Library.DepIsInSameApex(ctx, dep)
671}
672
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900673// For OutputFileProducer interface
674func (a *AndroidApp) OutputFiles(tag string) (android.Paths, error) {
675 switch tag {
676 case ".aapt.srcjar":
677 return []android.Path{a.aaptSrcJar}, nil
678 }
679 return a.Library.OutputFiles(tag)
680}
681
Jiyong Parkf7487312019-10-17 12:54:30 +0900682func (a *AndroidApp) Privileged() bool {
683 return Bool(a.appProperties.Privileged)
684}
685
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700686func (a *AndroidApp) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
687 return ctx.Device() && (ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled())
688}
689
690func (a *AndroidApp) PreventInstall() {
691 a.appProperties.PreventInstall = true
692}
693
694func (a *AndroidApp) HideFromMake() {
695 a.appProperties.HideFromMake = true
696}
697
698func (a *AndroidApp) MarkAsCoverageVariant(coverage bool) {
699 a.appProperties.IsCoverageVariant = coverage
700}
701
702var _ cc.Coverage = (*AndroidApp)(nil)
703
Colin Cross1b16b0e2019-02-12 14:41:32 -0800704// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700705func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700706 module := &AndroidApp{}
707
Sasha Smundak2057f822019-04-16 17:16:58 -0700708 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross66dbc0b2017-12-28 12:23:20 -0800709 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
710
Colin Crossae5caf52018-05-22 11:11:52 -0700711 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700712 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700713
Colin Cross36242852017-06-23 15:06:31 -0700714 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700715 &module.Module.properties,
716 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800717 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700718 &module.Module.protoProperties,
719 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800720 &module.appProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700721 &module.overridableAppProperties,
722 &module.usesLibrary.usesLibraryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700723
Colin Crossa9d8bee2018-10-02 13:59:46 -0700724 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
725 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
726 })
727
Colin Crossa4f08812018-10-02 22:03:40 -0700728 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
729 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800730 android.InitOverridableModule(module, &module.appProperties.Overrides)
Jiyong Park52cd06f2019-11-11 10:14:32 +0900731 android.InitApexModule(module)
Colin Crossa4f08812018-10-02 22:03:40 -0700732
Colin Cross36242852017-06-23 15:06:31 -0700733 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700734}
Colin Crossae5caf52018-05-22 11:11:52 -0700735
736type appTestProperties struct {
737 Instrumentation_for *string
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700738
739 // if specified, the instrumentation target package name in the manifest is overwritten by it.
740 Instrumentation_target_package *string
Colin Crossae5caf52018-05-22 11:11:52 -0700741}
742
743type AndroidTest struct {
744 AndroidApp
745
746 appTestProperties appTestProperties
747
748 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700749
750 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700751 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700752}
753
Jaewoong Jung0949f312019-09-11 10:25:18 -0700754func (a *AndroidTest) InstallInTestcases() bool {
755 return true
756}
757
Colin Crossae5caf52018-05-22 11:11:52 -0700758func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700759 if a.appTestProperties.Instrumentation_target_package != nil {
760 a.additionalAaptFlags = append(a.additionalAaptFlags,
761 "--rename-instrumentation-target-package "+*a.appTestProperties.Instrumentation_target_package)
762 } else if a.appTestProperties.Instrumentation_for != nil {
763 // Check if the instrumentation target package is overridden.
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800764 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
765 if overridden {
766 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
767 }
768 }
Colin Crossae5caf52018-05-22 11:11:52 -0700769 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700770
Jaewoong Jung39982342020-01-14 10:27:18 -0800771 testConfig := tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700772 a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites, a.testProperties.Auto_gen_config)
Jaewoong Jung39982342020-01-14 10:27:18 -0800773 a.testConfig = a.FixTestConfig(ctx, testConfig)
Colin Cross8a497952019-03-05 22:25:09 -0800774 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700775}
776
Jaewoong Jung39982342020-01-14 10:27:18 -0800777func (a *AndroidTest) FixTestConfig(ctx android.ModuleContext, testConfig android.Path) android.Path {
778 if testConfig == nil {
779 return nil
780 }
781
782 fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", "AndroidTest.xml")
783 rule := android.NewRuleBuilder()
784 command := rule.Command().BuiltTool(ctx, "test_config_fixer").Input(testConfig).Output(fixedConfig)
785 fixNeeded := false
786
787 if ctx.ModuleName() != a.installApkName {
788 fixNeeded = true
789 command.FlagWithArg("--test-file-name ", a.installApkName+".apk")
790 }
791
792 if a.overridableAppProperties.Package_name != nil {
793 fixNeeded = true
794 command.FlagWithInput("--manifest ", a.manifestPath).
795 FlagWithArg("--package-name ", *a.overridableAppProperties.Package_name)
796 }
797
798 if fixNeeded {
799 rule.Build(pctx, ctx, "fix_test_config", "fix test config")
800 return fixedConfig
801 }
802 return testConfig
803}
804
Colin Cross303e21f2018-08-07 16:49:25 -0700805func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -0700806 a.AndroidApp.DepsMutator(ctx)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700807}
808
809func (a *AndroidTest) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
810 a.AndroidApp.OverridablePropertiesDepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -0700811 if a.appTestProperties.Instrumentation_for != nil {
812 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
813 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
814 // use instrumentationForTag instead of libTag.
815 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
816 }
Colin Crossae5caf52018-05-22 11:11:52 -0700817}
818
Colin Cross1b16b0e2019-02-12 14:41:32 -0800819// android_test compiles test sources and Android resources into an Android application package `.apk` file and
820// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -0700821func AndroidTestFactory() android.Module {
822 module := &AndroidTest{}
823
Sasha Smundak2057f822019-04-16 17:16:58 -0700824 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross5067db92018-09-17 16:46:35 -0700825
826 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700827 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800828 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700829 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800830 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -0700831
832 module.AddProperties(
833 &module.Module.properties,
834 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800835 &module.Module.dexpreoptProperties,
Colin Crossae5caf52018-05-22 11:11:52 -0700836 &module.Module.protoProperties,
837 &module.aaptProperties,
838 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700839 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800840 &module.overridableAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700841 &module.usesLibrary.usesLibraryProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700842 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700843
Colin Crossa4f08812018-10-02 22:03:40 -0700844 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
845 android.InitDefaultableModule(module)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700846 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossae5caf52018-05-22 11:11:52 -0700847 return module
848}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700849
Colin Cross252fc6f2018-10-04 15:22:03 -0700850type appTestHelperAppProperties struct {
851 // list of compatibility suites (for example "cts", "vts") that the module should be
852 // installed into.
853 Test_suites []string `android:"arch_variant"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700854
855 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
856 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
857 // explicitly.
858 Auto_gen_config *bool
Colin Cross252fc6f2018-10-04 15:22:03 -0700859}
860
861type AndroidTestHelperApp struct {
862 AndroidApp
863
864 appTestHelperAppProperties appTestHelperAppProperties
865}
866
Jaewoong Jung326a9412019-11-21 10:41:00 -0800867func (a *AndroidTestHelperApp) InstallInTestcases() bool {
868 return true
869}
870
Colin Cross1b16b0e2019-02-12 14:41:32 -0800871// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
872// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
873// test.
Colin Cross252fc6f2018-10-04 15:22:03 -0700874func AndroidTestHelperAppFactory() android.Module {
875 module := &AndroidTestHelperApp{}
876
Sasha Smundak2057f822019-04-16 17:16:58 -0700877 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700878
879 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800880 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700881 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800882 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700883
884 module.AddProperties(
885 &module.Module.properties,
886 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800887 &module.Module.dexpreoptProperties,
Colin Cross252fc6f2018-10-04 15:22:03 -0700888 &module.Module.protoProperties,
889 &module.aaptProperties,
890 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800891 &module.appTestHelperAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700892 &module.overridableAppProperties,
893 &module.usesLibrary.usesLibraryProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -0700894
895 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
896 android.InitDefaultableModule(module)
Anton Hansson3d2b6b42020-01-10 15:06:01 +0000897 android.InitApexModule(module)
Colin Cross252fc6f2018-10-04 15:22:03 -0700898 return module
899}
900
Colin Crossbd01e2a2018-10-04 15:21:03 -0700901type AndroidAppCertificate struct {
902 android.ModuleBase
903 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900904 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -0700905}
906
907type AndroidAppCertificateProperties struct {
908 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
909 Certificate *string
910}
911
Colin Cross1b16b0e2019-02-12 14:41:32 -0800912// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
913// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -0700914func AndroidAppCertificateFactory() android.Module {
915 module := &AndroidAppCertificate{}
916 module.AddProperties(&module.properties)
917 android.InitAndroidModule(module)
918 return module
919}
920
Colin Crossbd01e2a2018-10-04 15:21:03 -0700921func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
922 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900923 c.Certificate = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -0800924 Pem: android.PathForModuleSrc(ctx, cert+".x509.pem"),
925 Key: android.PathForModuleSrc(ctx, cert+".pk8"),
Colin Crossbd01e2a2018-10-04 15:21:03 -0700926 }
927}
Jaewoong Jung525443a2019-02-28 15:35:54 -0800928
929type OverrideAndroidApp struct {
930 android.ModuleBase
931 android.OverrideModuleBase
932}
933
934func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
935 // All the overrides happen in the base module.
936 // TODO(jungjw): Check the base module type.
937}
938
939// override_android_app is used to create an android_app module based on another android_app by overriding
940// some of its properties.
941func OverrideAndroidAppModuleFactory() android.Module {
942 m := &OverrideAndroidApp{}
943 m.AddProperties(&overridableAppProperties{})
944
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700945 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800946 android.InitOverrideModule(m)
947 return m
948}
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700949
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700950type OverrideAndroidTest struct {
951 android.ModuleBase
952 android.OverrideModuleBase
953}
954
955func (i *OverrideAndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
956 // All the overrides happen in the base module.
957 // TODO(jungjw): Check the base module type.
958}
959
960// override_android_test is used to create an android_app module based on another android_test by overriding
961// some of its properties.
962func OverrideAndroidTestModuleFactory() android.Module {
963 m := &OverrideAndroidTest{}
964 m.AddProperties(&overridableAppProperties{})
965 m.AddProperties(&appTestProperties{})
966
967 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
968 android.InitOverrideModule(m)
969 return m
970}
971
Roshan Piusb8307962020-04-27 09:42:27 -0700972type OverrideRuntimeResourceOverlay struct {
973 android.ModuleBase
974 android.OverrideModuleBase
975}
976
977func (i *OverrideRuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
978 // All the overrides happen in the base module.
979 // TODO(jungjw): Check the base module type.
980}
981
982// override_runtime_resource_overlay is used to create a module based on another
983// runtime_resource_overlay module by overriding some of its properties.
984func OverrideRuntimeResourceOverlayModuleFactory() android.Module {
985 m := &OverrideRuntimeResourceOverlay{}
986 m.AddProperties(&OverridableRuntimeResourceOverlayProperties{})
987
988 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
989 android.InitOverrideModule(m)
990 return m
991}
992
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700993type AndroidAppImport struct {
994 android.ModuleBase
995 android.DefaultableModuleBase
996 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 Han65cd0f02020-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
Peter Collingbournead84f972019-12-17 16:46:18 -08001167 _, certificates := collectAppDeps(ctx, 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 Han65cd0f02020-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 Han65cd0f02020-03-23 20:21:11 +09001221 signed := android.PathForModuleOut(ctx, "signed", apkFilename)
Songchun Fan688de9a2020-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 Han65cd0f02020-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
Jooyung Han65cd0f02020-03-23 20:21:11 +09001233 a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001234
1235 // TODO: androidmk converter jni libs
1236}
1237
1238func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
1239 return &a.prebuilt
1240}
1241
1242func (a *AndroidAppImport) Name() string {
1243 return a.prebuilt.Name(a.ModuleBase.Name())
1244}
1245
Dario Frenicde2a032019-10-27 00:29:22 +01001246func (a *AndroidAppImport) OutputFile() android.Path {
1247 return a.outputFile
1248}
1249
Jiyong Park618922e2020-01-08 13:35:43 +09001250func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
1251 return nil
1252}
1253
Colin Cross503c1d02020-01-28 14:00:53 -08001254func (a *AndroidAppImport) Certificate() Certificate {
1255 return a.certificate
1256}
1257
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001258var dpiVariantGroupType reflect.Type
1259var archVariantGroupType reflect.Type
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001260
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001261func initAndroidAppImportVariantGroupTypes() {
1262 dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants")
1263
1264 archNames := make([]string, len(android.ArchTypeList()))
1265 for i, archType := range android.ArchTypeList() {
1266 archNames[i] = archType.Name
1267 }
1268 archVariantGroupType = createVariantGroupType(archNames, "Arch")
1269}
1270
1271// Populates all variant struct properties at creation time.
1272func (a *AndroidAppImport) populateAllVariantStructs() {
1273 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
1274 a.AddProperties(a.dpiVariants)
1275
1276 a.archVariants = reflect.New(archVariantGroupType).Interface()
1277 a.AddProperties(a.archVariants)
1278}
1279
Jiyong Parkf7487312019-10-17 12:54:30 +09001280func (a *AndroidAppImport) Privileged() bool {
1281 return Bool(a.properties.Privileged)
1282}
1283
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001284func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
1285 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
1286
1287 variantFields := make([]reflect.StructField, len(variants))
1288 for i, variant := range variants {
1289 variantFields[i] = reflect.StructField{
1290 Name: proptools.FieldNameForProperty(variant),
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001291 Type: props,
1292 }
1293 }
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001294
1295 variantGroupStruct := reflect.StructOf(variantFields)
1296 return reflect.StructOf([]reflect.StructField{
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001297 {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001298 Name: variantGroupName,
1299 Type: variantGroupStruct,
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001300 },
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001301 })
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001302}
1303
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001304// android_app_import imports a prebuilt apk with additional processing specified in the module.
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001305// DPI-specific apk source files can be specified using dpi_variants. Example:
1306//
1307// android_app_import {
1308// name: "example_import",
1309// apk: "prebuilts/example.apk",
1310// dpi_variants: {
1311// mdpi: {
1312// apk: "prebuilts/example_mdpi.apk",
1313// },
1314// xhdpi: {
1315// apk: "prebuilts/example_xhdpi.apk",
1316// },
1317// },
1318// certificate: "PRESIGNED",
1319// }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001320func AndroidAppImportFactory() android.Module {
1321 module := &AndroidAppImport{}
1322 module.AddProperties(&module.properties)
1323 module.AddProperties(&module.dexpreoptProperties)
Colin Cross50ddcc42019-05-16 12:28:22 -07001324 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001325 module.populateAllVariantStructs()
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001326 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001327 module.processVariants(ctx)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001328 })
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001329
1330 InitJavaModule(module, android.DeviceSupported)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001331 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001332
1333 return module
1334}
Colin Cross50ddcc42019-05-16 12:28:22 -07001335
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001336type AndroidTestImport struct {
1337 AndroidAppImport
1338
1339 testProperties testProperties
1340
1341 data android.Paths
1342}
1343
1344func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1345 a.generateAndroidBuildActions(ctx)
1346
1347 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
1348}
1349
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001350func (a *AndroidTestImport) InstallInTestcases() bool {
1351 return true
1352}
1353
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001354// android_test_import imports a prebuilt test apk with additional processing specified in the
1355// module. DPI or arch variant configurations can be made as with android_app_import.
1356func AndroidTestImportFactory() android.Module {
1357 module := &AndroidTestImport{}
1358 module.AddProperties(&module.properties)
1359 module.AddProperties(&module.dexpreoptProperties)
1360 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
1361 module.AddProperties(&module.testProperties)
1362 module.populateAllVariantStructs()
1363 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
1364 module.processVariants(ctx)
1365 })
1366
1367 InitJavaModule(module, android.DeviceSupported)
1368 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
1369
1370 return module
1371}
1372
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001373type RuntimeResourceOverlay struct {
1374 android.ModuleBase
1375 android.DefaultableModuleBase
Roshan Piusb8307962020-04-27 09:42:27 -07001376 android.OverridableModuleBase
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001377 aapt
1378
Roshan Piusb8307962020-04-27 09:42:27 -07001379 properties RuntimeResourceOverlayProperties
1380 overridableProperties OverridableRuntimeResourceOverlayProperties
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001381
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08001382 certificate Certificate
1383
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001384 outputFile android.Path
1385 installDir android.InstallPath
1386}
1387
1388type RuntimeResourceOverlayProperties struct {
1389 // the name of a certificate in the default certificate directory or an android_app_certificate
1390 // module name in the form ":module".
1391 Certificate *string
1392
1393 // optional theme name. If specified, the overlay package will be applied
1394 // only when the ro.boot.vendor.overlay.theme system property is set to the same value.
1395 Theme *string
1396
1397 // if not blank, set to the version of the sdk to compile against.
1398 // Defaults to compiling against the current platform.
1399 Sdk_version *string
1400
1401 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
1402 // Defaults to sdk_version if not set.
1403 Min_sdk_version *string
Jaewoong Jungca095d72020-04-09 16:15:30 -07001404
1405 // list of android_library modules whose resources are extracted and linked against statically
1406 Static_libs []string
1407
1408 // list of android_app modules whose resources are extracted and linked against
1409 Resource_libs []string
Jaewoong Jungbfc6ac02020-04-24 15:22:40 -07001410
1411 // Names of modules to be overridden. Listed modules can only be other overlays
1412 // (in Make or Soong).
1413 // This does not completely prevent installation of the overridden overlays, but if both
1414 // overlays would be installed by default (in PRODUCT_PACKAGES) the other overlay will be removed
1415 // from PRODUCT_PACKAGES.
1416 Overrides []string
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001417}
1418
1419func (r *RuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
1420 sdkDep := decodeSdkDep(ctx, sdkContext(r))
1421 if sdkDep.hasFrameworkLibs() {
1422 r.aapt.deps(ctx, sdkDep)
1423 }
1424
1425 cert := android.SrcIsModule(String(r.properties.Certificate))
1426 if cert != "" {
1427 ctx.AddDependency(ctx.Module(), certificateTag, cert)
1428 }
Jaewoong Jungca095d72020-04-09 16:15:30 -07001429
1430 ctx.AddVariationDependencies(nil, staticLibTag, r.properties.Static_libs...)
1431 ctx.AddVariationDependencies(nil, libTag, r.properties.Resource_libs...)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001432}
1433
1434func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1435 // Compile and link resources
1436 r.aapt.hasNoCode = true
Jaewoong Jungf0f747c2020-01-24 10:30:02 -08001437 // Do not remove resources without default values nor dedupe resource configurations with the same value
Roshan Piusb8307962020-04-27 09:42:27 -07001438 aaptLinkFlags := []string{"--no-resource-deduping", "--no-resource-removal"}
1439 // Allow the override of "package name" and "overlay target package name"
1440 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1441 if overridden || r.overridableProperties.Package_name != nil {
1442 // The product override variable has a priority over the package_name property.
1443 if !overridden {
1444 manifestPackageName = *r.overridableProperties.Package_name
1445 }
1446 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
1447 }
1448 if r.overridableProperties.Target_package_name != nil {
1449 aaptLinkFlags = append(aaptLinkFlags,
1450 "--rename-overlay-target-package "+*r.overridableProperties.Target_package_name)
1451 }
1452 r.aapt.buildActions(ctx, r, aaptLinkFlags...)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001453
1454 // Sign the built package
1455 _, certificates := collectAppDeps(ctx, false)
1456 certificates = processMainCert(r.ModuleBase, String(r.properties.Certificate), certificates, ctx)
1457 signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
Songchun Fan688de9a2020-03-24 20:32:24 -07001458 SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil)
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08001459 r.certificate = certificates[0]
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001460
1461 r.outputFile = signed
1462 r.installDir = android.PathForModuleInstall(ctx, "overlay", String(r.properties.Theme))
1463 ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile)
1464}
1465
Jiyong Park6a927c42020-01-21 02:03:43 +09001466func (r *RuntimeResourceOverlay) sdkVersion() sdkSpec {
1467 return sdkSpecFrom(String(r.properties.Sdk_version))
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001468}
1469
1470func (r *RuntimeResourceOverlay) systemModules() string {
1471 return ""
1472}
1473
Jiyong Park6a927c42020-01-21 02:03:43 +09001474func (r *RuntimeResourceOverlay) minSdkVersion() sdkSpec {
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001475 if r.properties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +09001476 return sdkSpecFrom(*r.properties.Min_sdk_version)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001477 }
1478 return r.sdkVersion()
1479}
1480
Jiyong Park6a927c42020-01-21 02:03:43 +09001481func (r *RuntimeResourceOverlay) targetSdkVersion() sdkSpec {
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001482 return r.sdkVersion()
1483}
1484
1485// runtime_resource_overlay generates a resource-only apk file that can overlay application and
1486// system resources at run time.
1487func RuntimeResourceOverlayFactory() android.Module {
1488 module := &RuntimeResourceOverlay{}
1489 module.AddProperties(
1490 &module.properties,
Roshan Piusb8307962020-04-27 09:42:27 -07001491 &module.aaptProperties,
1492 &module.overridableProperties)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001493
Roshan Piusb8307962020-04-27 09:42:27 -07001494 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1495 android.InitDefaultableModule(module)
1496 android.InitOverridableModule(module, &module.properties.Overrides)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001497 return module
1498}
1499
Colin Cross50ddcc42019-05-16 12:28:22 -07001500type UsesLibraryProperties struct {
1501 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file.
1502 Uses_libs []string
1503
1504 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file with
1505 // required=false.
1506 Optional_uses_libs []string
1507
1508 // If true, the list of uses_libs and optional_uses_libs modules must match the AndroidManifest.xml file. Defaults
1509 // to true if either uses_libs or optional_uses_libs is set. Will unconditionally default to true in the future.
1510 Enforce_uses_libs *bool
1511}
1512
1513// usesLibrary provides properties and helper functions for AndroidApp and AndroidAppImport to verify that the
1514// <uses-library> tags that end up in the manifest of an APK match the ones known to the build system through the
1515// uses_libs and optional_uses_libs properties. The build system's values are used by dexpreopt to preopt apps
1516// with knowledge of their shared libraries.
1517type usesLibrary struct {
1518 usesLibraryProperties UsesLibraryProperties
1519}
1520
Paul Duffin250e6192019-06-07 10:44:37 +01001521func (u *usesLibrary) deps(ctx android.BottomUpMutatorContext, hasFrameworkLibs bool) {
Colin Cross3245b2c2019-06-07 13:18:09 -07001522 if !ctx.Config().UnbundledBuild() {
1523 ctx.AddVariationDependencies(nil, usesLibTag, u.usesLibraryProperties.Uses_libs...)
1524 ctx.AddVariationDependencies(nil, usesLibTag, u.presentOptionalUsesLibs(ctx)...)
Paul Duffin250e6192019-06-07 10:44:37 +01001525 // Only add these extra dependencies if the module depends on framework libs. This avoids
1526 // creating a cyclic dependency:
1527 // e.g. framework-res -> org.apache.http.legacy -> ... -> framework-res.
1528 if hasFrameworkLibs {
Colin Cross3245b2c2019-06-07 13:18:09 -07001529 // dexpreopt/dexpreopt.go needs the paths to the dex jars of these libraries in case construct_context.sh needs
1530 // to pass them to dex2oat. Add them as a dependency so we can determine the path to the dex jar of each
1531 // library to dexpreopt.
1532 ctx.AddVariationDependencies(nil, usesLibTag,
1533 "org.apache.http.legacy",
1534 "android.hidl.base-V1.0-java",
1535 "android.hidl.manager-V1.0-java")
1536 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001537 }
1538}
1539
1540// presentOptionalUsesLibs returns optional_uses_libs after filtering out MissingUsesLibraries, which don't exist in the
1541// build.
1542func (u *usesLibrary) presentOptionalUsesLibs(ctx android.BaseModuleContext) []string {
1543 optionalUsesLibs, _ := android.FilterList(u.usesLibraryProperties.Optional_uses_libs, ctx.Config().MissingUsesLibraries())
1544 return optionalUsesLibs
1545}
1546
1547// usesLibraryPaths returns a map of module names of shared library dependencies to the paths to their dex jars.
1548func (u *usesLibrary) usesLibraryPaths(ctx android.ModuleContext) map[string]android.Path {
1549 usesLibPaths := make(map[string]android.Path)
1550
1551 if !ctx.Config().UnbundledBuild() {
1552 ctx.VisitDirectDepsWithTag(usesLibTag, func(m android.Module) {
1553 if lib, ok := m.(Dependency); ok {
1554 if dexJar := lib.DexJar(); dexJar != nil {
1555 usesLibPaths[ctx.OtherModuleName(m)] = dexJar
1556 } else {
1557 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must produce a dex jar, does it have installable: true?",
1558 ctx.OtherModuleName(m))
1559 }
1560 } else if ctx.Config().AllowMissingDependencies() {
1561 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(m)})
1562 } else {
1563 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must be a java library",
1564 ctx.OtherModuleName(m))
1565 }
1566 })
1567 }
1568
1569 return usesLibPaths
1570}
1571
1572// enforceUsesLibraries returns true of <uses-library> tags should be checked against uses_libs and optional_uses_libs
1573// properties. Defaults to true if either of uses_libs or optional_uses_libs is specified. Will default to true
1574// unconditionally in the future.
1575func (u *usesLibrary) enforceUsesLibraries() bool {
1576 defaultEnforceUsesLibs := len(u.usesLibraryProperties.Uses_libs) > 0 ||
1577 len(u.usesLibraryProperties.Optional_uses_libs) > 0
1578 return BoolDefault(u.usesLibraryProperties.Enforce_uses_libs, defaultEnforceUsesLibs)
1579}
1580
1581// verifyUsesLibrariesManifest checks the <uses-library> tags in an AndroidManifest.xml against the ones specified
1582// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the manifest.
1583func (u *usesLibrary) verifyUsesLibrariesManifest(ctx android.ModuleContext, manifest android.Path) android.Path {
1584 outputFile := android.PathForModuleOut(ctx, "manifest_check", "AndroidManifest.xml")
1585
1586 rule := android.NewRuleBuilder()
Colin Crossee94d6a2019-07-08 17:08:34 -07001587 cmd := rule.Command().BuiltTool(ctx, "manifest_check").
Colin Cross50ddcc42019-05-16 12:28:22 -07001588 Flag("--enforce-uses-libraries").
1589 Input(manifest).
1590 FlagWithOutput("-o ", outputFile)
1591
1592 for _, lib := range u.usesLibraryProperties.Uses_libs {
1593 cmd.FlagWithArg("--uses-library ", lib)
1594 }
1595
1596 for _, lib := range u.usesLibraryProperties.Optional_uses_libs {
1597 cmd.FlagWithArg("--optional-uses-library ", lib)
1598 }
1599
1600 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1601
1602 return outputFile
1603}
1604
1605// verifyUsesLibrariesAPK checks the <uses-library> tags in the manifest of an APK against the ones specified
1606// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the APK.
1607func (u *usesLibrary) verifyUsesLibrariesAPK(ctx android.ModuleContext, apk android.Path) android.Path {
1608 outputFile := android.PathForModuleOut(ctx, "verify_uses_libraries", apk.Base())
1609
1610 rule := android.NewRuleBuilder()
1611 aapt := ctx.Config().HostToolPath(ctx, "aapt")
1612 rule.Command().
1613 Textf("aapt_binary=%s", aapt.String()).Implicit(aapt).
1614 Textf(`uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Uses_libs, " ")).
1615 Textf(`optional_uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Optional_uses_libs, " ")).
1616 Tool(android.PathForSource(ctx, "build/make/core/verify_uses_libraries.sh")).Input(apk)
1617 rule.Command().Text("cp -f").Input(apk).Output(outputFile)
1618
1619 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1620
1621 return outputFile
1622}