blob: f9992d885ce5414141c3b7e9e5bf0831be7463e7 [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 Jung37ca4a12020-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"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700113}
114
Jaewoong Jung525443a2019-02-28 15:35:54 -0800115// android_app properties that can be overridden by override_android_app
116type overridableAppProperties struct {
117 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
118 // or an android_app_certificate module name in the form ":module".
119 Certificate *string
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700120
121 // the package name of this app. The package name in the manifest file is used if one was not given.
122 Package_name *string
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800123
124 // the logging parent of this app.
125 Logging_parent *string
Jaewoong Jung525443a2019-02-28 15:35:54 -0800126}
127
Colin Cross30e076a2015-04-13 13:58:27 -0700128type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -0700129 Library
130 aapt
Jaewoong Jung525443a2019-02-28 15:35:54 -0800131 android.OverridableModuleBase
Colin Crossa97c5d32018-03-28 14:58:31 -0700132
Colin Cross50ddcc42019-05-16 12:28:22 -0700133 usesLibrary usesLibrary
134
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900135 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -0700136
Colin Crossfabb6082018-02-20 17:22:23 -0800137 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -0700138
Jaewoong Jung525443a2019-02-28 15:35:54 -0800139 overridableAppProperties overridableAppProperties
140
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700141 installJniLibs []jniLib
142 jniCoverageOutputs android.Paths
Colin Crossf6237212018-10-29 23:14:58 -0700143
144 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800145
146 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
147 installApkName string
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800148
Colin Cross70dda7e2019-10-01 22:05:35 -0700149 installDir android.InstallPath
Jaewoong Jung0949f312019-09-11 10:25:18 -0700150
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700151 onDeviceDir string
152
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800153 additionalAaptFlags []string
Jaewoong Jung98772792019-07-01 17:15:13 -0700154
155 noticeOutputs android.NoticeOutputs
Jiyong Parkaf8998c2020-02-28 16:51:07 +0900156
157 overriddenManifestPackageName string
Colin Crosse1731a52017-12-14 11:22:55 -0800158}
159
Martin Stjernholm6d415272020-01-31 17:10:36 +0000160func (a *AndroidApp) IsInstallable() bool {
161 return Bool(a.properties.Installable)
162}
163
Colin Cross89c31582018-04-30 15:55:11 -0700164func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
165 return nil
166}
167
Colin Cross66f78822018-05-02 12:58:28 -0700168func (a *AndroidApp) ExportedStaticPackages() android.Paths {
169 return nil
170}
171
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900172func (a *AndroidApp) OutputFile() android.Path {
173 return a.outputFile
174}
175
Colin Cross503c1d02020-01-28 14:00:53 -0800176func (a *AndroidApp) Certificate() Certificate {
177 return a.certificate
178}
179
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700180func (a *AndroidApp) JniCoverageOutputs() android.Paths {
181 return a.jniCoverageOutputs
182}
183
Colin Crossa97c5d32018-03-28 14:58:31 -0700184var _ AndroidLibraryDependency = (*AndroidApp)(nil)
185
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900186type Certificate struct {
Colin Cross503c1d02020-01-28 14:00:53 -0800187 Pem, Key android.Path
188 presigned bool
189}
190
191var presignedCertificate = Certificate{presigned: true}
192
193func (c Certificate) AndroidMkString() string {
194 if c.presigned {
195 return "PRESIGNED"
196 } else {
197 return c.Pem.String()
198 }
Colin Cross30e076a2015-04-13 13:58:27 -0700199}
200
Colin Cross46c9b8b2017-06-22 16:51:17 -0700201func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
202 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700203
Jiyong Park6a927c42020-01-21 02:03:43 +0900204 if String(a.appProperties.Stl) == "c++_shared" && !a.sdkVersion().specified() {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700205 ctx.PropertyErrorf("stl", "sdk_version must be set in order to use c++_shared")
206 }
207
Paul Duffin250e6192019-06-07 10:44:37 +0100208 sdkDep := decodeSdkDep(ctx, sdkContext(a))
209 if sdkDep.hasFrameworkLibs() {
210 a.aapt.deps(ctx, sdkDep)
Colin Cross30e076a2015-04-13 13:58:27 -0700211 }
Colin Crossa4f08812018-10-02 22:03:40 -0700212
Peter Collingbournead84f972019-12-17 16:46:18 -0800213 tag := &jniDependencyTag{}
Colin Crossa4f08812018-10-02 22:03:40 -0700214 for _, jniTarget := range ctx.MultiTargets() {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700215 variation := append(jniTarget.Variations(),
216 blueprint.Variation{Mutator: "link", Variation: "shared"})
Colin Crossa4f08812018-10-02 22:03:40 -0700217 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
218 }
Colin Cross50ddcc42019-05-16 12:28:22 -0700219
Paul Duffin250e6192019-06-07 10:44:37 +0100220 a.usesLibrary.deps(ctx, sdkDep.hasFrameworkLibs())
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700221}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700222
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700223func (a *AndroidApp) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800224 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700225 if cert != "" {
226 ctx.AddDependency(ctx.Module(), certificateTag, cert)
227 }
228
229 for _, cert := range a.appProperties.Additional_certificates {
230 cert = android.SrcIsModule(cert)
231 if cert != "" {
232 ctx.AddDependency(ctx.Module(), certificateTag, cert)
233 } else {
234 ctx.PropertyErrorf("additional_certificates",
235 `must be names of android_app_certificate modules in the form ":module"`)
236 }
237 }
Colin Cross30e076a2015-04-13 13:58:27 -0700238}
239
Jeongik Cha538c0d02019-07-11 15:54:27 +0900240func (a *AndroidTestHelperApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
241 a.generateAndroidBuildActions(ctx)
242}
243
Colin Cross46c9b8b2017-06-22 16:51:17 -0700244func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900245 a.checkPlatformAPI(ctx)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900246 a.checkSdkVersion(ctx)
Colin Crossae5caf52018-05-22 11:11:52 -0700247 a.generateAndroidBuildActions(ctx)
248}
249
Sasha Smundak6ad77252019-05-01 13:16:22 -0700250// Returns true if the native libraries should be stored in the APK uncompressed and the
Colin Crosse4246ab2019-02-05 21:55:21 -0800251// extractNativeLibs application flag should be set to false in the manifest.
Sasha Smundak6ad77252019-05-01 13:16:22 -0700252func (a *AndroidApp) useEmbeddedNativeLibs(ctx android.ModuleContext) bool {
Jiyong Park6a927c42020-01-21 02:03:43 +0900253 minSdkVersion, err := a.minSdkVersion().effectiveVersion(ctx)
Colin Crosse4246ab2019-02-05 21:55:21 -0800254 if err != nil {
255 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
256 }
257
Jiyong Park52cd06f2019-11-11 10:14:32 +0900258 return (minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)) ||
259 !a.IsForPlatform()
Colin Crosse4246ab2019-02-05 21:55:21 -0800260}
261
Colin Cross43f08db2018-11-12 10:13:39 -0800262// Returns whether this module should have the dex file stored uncompressed in the APK.
263func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800264 if Bool(a.appProperties.Use_embedded_dex) {
265 return true
266 }
267
Colin Cross53a87f52019-06-25 13:35:30 -0700268 // Uncompress dex in APKs of privileged apps (even for unbundled builds, they may
269 // be preinstalled as prebuilts).
Jiyong Parkf7487312019-10-17 12:54:30 +0900270 if ctx.Config().UncompressPrivAppDex() && a.Privileged() {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000271 return true
272 }
273
Colin Cross53a87f52019-06-25 13:35:30 -0700274 if ctx.Config().UnbundledBuild() {
275 return false
276 }
277
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700278 return shouldUncompressDex(ctx, &a.dexpreopter)
Colin Cross5a0dcd52018-10-05 14:20:06 -0700279}
280
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700281func (a *AndroidApp) shouldEmbedJnis(ctx android.BaseModuleContext) bool {
282 return ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) ||
Jiyong Park52cd06f2019-11-11 10:14:32 +0900283 !a.IsForPlatform() || a.appProperties.AlwaysPackageNativeLibs
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700284}
285
Jiyong Parkaf8998c2020-02-28 16:51:07 +0900286func (a *AndroidApp) OverriddenManifestPackageName() string {
287 return a.overriddenManifestPackageName
288}
289
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800290func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
David Brazdild25060a2019-02-18 18:24:16 +0000291 a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis)
292
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700293 // Ask manifest_fixer to add or update the application element indicating this app has no code.
294 a.aapt.hasNoCode = !a.hasCode(ctx)
295
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800296 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800297
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800298 // 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 -0800299 hasProduct := android.PrefixInList(a.aaptProperties.Aaptflags, "--product")
Colin Crosse78dcd32018-04-19 15:25:19 -0700300 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800301 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700302 }
303
Dan Willemsen72be5902018-10-24 20:24:57 -0700304 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
305 // Product AAPT config
306 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800307 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700308 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700309
Dan Willemsen72be5902018-10-24 20:24:57 -0700310 // Product AAPT preferred config
311 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800312 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700313 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700314 }
315
Jiyong Park7f67f482019-01-05 12:57:48 +0900316 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700317 if overridden || a.overridableAppProperties.Package_name != nil {
318 // The product override variable has a priority over the package_name property.
319 if !overridden {
320 manifestPackageName = *a.overridableAppProperties.Package_name
321 }
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800322 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
Jiyong Parkaf8998c2020-02-28 16:51:07 +0900323 a.overriddenManifestPackageName = manifestPackageName
Jiyong Park7f67f482019-01-05 12:57:48 +0900324 }
325
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800326 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
327
Colin Crosse560c4a2019-03-19 16:03:11 -0700328 a.aapt.splitNames = a.appProperties.Package_splits
Colin Cross50ddcc42019-05-16 12:28:22 -0700329 a.aapt.sdkLibraries = a.exportedSdkLibs
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800330 a.aapt.LoggingParent = String(a.overridableAppProperties.Logging_parent)
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800331 a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700332
Colin Cross46c9b8b2017-06-22 16:51:17 -0700333 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700334 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800335}
Colin Cross30e076a2015-04-13 13:58:27 -0700336
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800337func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700338 var staticLibProguardFlagFiles android.Paths
339 ctx.VisitDirectDeps(func(m android.Module) {
340 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
341 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
342 }
343 })
344
345 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
346
347 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
348 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800349}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800350
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800351func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross43f08db2018-11-12 10:13:39 -0800352
353 var installDir string
354 if ctx.ModuleName() == "framework-res" {
355 // framework-res.apk is installed as system/framework/framework-res.apk
356 installDir = "framework"
Jiyong Parkf7487312019-10-17 12:54:30 +0900357 } else if a.Privileged() {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800358 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800359 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800360 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800361 }
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800362 a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000363 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -0700364
365 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
366 a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
367 a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
368 a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
369 a.dexpreopter.manifestFile = a.mergedManifestFile
370
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000371 a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex
Colin Cross5a0dcd52018-10-05 14:20:06 -0700372
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800373 if ctx.ModuleName() != "framework-res" {
374 a.Module.compile(ctx, a.aaptSrcJar)
375 }
Colin Cross30e076a2015-04-13 13:58:27 -0700376
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800377 return a.maybeStrippedDexJarFile
378}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800379
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800380func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700381 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700382 if len(jniLibs) > 0 {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700383 if a.shouldEmbedJnis(ctx) {
Colin Crossa4f08812018-10-02 22:03:40 -0700384 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Sasha Smundak6ad77252019-05-01 13:16:22 -0700385 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.useEmbeddedNativeLibs(ctx))
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700386 for _, jni := range jniLibs {
387 if jni.coverageFile.Valid() {
388 a.jniCoverageOutputs = append(a.jniCoverageOutputs, jni.coverageFile.Path())
389 }
390 }
Colin Crossa4f08812018-10-02 22:03:40 -0700391 } else {
392 a.installJniLibs = jniLibs
393 }
394 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800395 return jniJarFile
396}
Colin Crossa4f08812018-10-02 22:03:40 -0700397
Jaewoong Jung0949f312019-09-11 10:25:18 -0700398func (a *AndroidApp) noticeBuildActions(ctx android.ModuleContext) {
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700399 // Collect NOTICE files from all dependencies.
400 seenModules := make(map[android.Module]bool)
401 noticePathSet := make(map[android.Path]bool)
402
403 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
404 // Have we already seen this?
405 if _, ok := seenModules[child]; ok {
406 return false
407 }
408 seenModules[child] = true
409
410 // Skip host modules.
411 if child.Target().Os.Class == android.Host || child.Target().Os.Class == android.HostCross {
412 return false
413 }
414
415 path := child.(android.Module).NoticeFile()
416 if path.Valid() {
417 noticePathSet[path.Path()] = true
418 }
419 return true
420 })
421
422 // If the app has one, add it too.
423 if a.NoticeFile().Valid() {
424 noticePathSet[a.NoticeFile().Path()] = true
425 }
426
427 if len(noticePathSet) == 0 {
Jaewoong Jung98772792019-07-01 17:15:13 -0700428 return
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700429 }
430 var noticePaths []android.Path
431 for path := range noticePathSet {
432 noticePaths = append(noticePaths, path)
433 }
434 sort.Slice(noticePaths, func(i, j int) bool {
435 return noticePaths[i].String() < noticePaths[j].String()
436 })
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700437
Jaewoong Jung0949f312019-09-11 10:25:18 -0700438 a.noticeOutputs = android.BuildNoticeOutput(ctx, a.installDir, a.installApkName+".apk", noticePaths)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700439}
440
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700441// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
442// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
443func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate {
444 if android.SrcIsModule(certPropValue) == "" {
445 var mainCert Certificate
446 if certPropValue != "" {
447 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
448 mainCert = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -0800449 Pem: defaultDir.Join(ctx, certPropValue+".x509.pem"),
450 Key: defaultDir.Join(ctx, certPropValue+".pk8"),
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700451 }
452 } else {
453 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Colin Cross503c1d02020-01-28 14:00:53 -0800454 mainCert = Certificate{
455 Pem: pem,
456 Key: key,
457 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700458 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700459 certificates = append([]Certificate{mainCert}, certificates...)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700460 }
461
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700462 if !m.Platform() {
463 certPath := certificates[0].Pem.String()
Jeongik Chac9464142019-01-07 12:07:27 +0900464 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
465 if strings.HasPrefix(certPath, systemCertPath) {
466 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
467 whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
468
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700469 if enforceSystemCert && !inList(m.Name(), whitelist) {
Jeongik Chac9464142019-01-07 12:07:27 +0900470 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
471 }
472 }
473 }
474
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700475 return certificates
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800476}
477
478func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross50ddcc42019-05-16 12:28:22 -0700479 var apkDeps android.Paths
480
Jeongik Cha538c0d02019-07-11 15:54:27 +0900481 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
482 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
483
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800484 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800485 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800486
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700487 if ctx.ModuleName() == "framework-res" {
488 // framework-res.apk is installed as system/framework/framework-res.apk
Jaewoong Jung0949f312019-09-11 10:25:18 -0700489 a.installDir = android.PathForModuleInstall(ctx, "framework")
Jiyong Parkf7487312019-10-17 12:54:30 +0900490 } else if a.Privileged() {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700491 a.installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
492 } else if ctx.InstallInTestcases() {
Jaewoong Jung326a9412019-11-21 10:41:00 -0800493 a.installDir = android.PathForModuleInstall(ctx, a.installApkName, ctx.DeviceConfig().DeviceArch())
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700494 } else {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700495 a.installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700496 }
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700497 a.onDeviceDir = android.InstallPathToOnDevicePath(ctx, a.installDir)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700498
Jaewoong Jung0949f312019-09-11 10:25:18 -0700499 a.noticeBuildActions(ctx)
Jaewoong Jung98772792019-07-01 17:15:13 -0700500 if Bool(a.appProperties.Embed_notices) || ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") {
501 a.aapt.noticeFile = a.noticeOutputs.HtmlGzOutput
502 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700503
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800504 // Process all building blocks, from AAPT to certificates.
505 a.aaptBuildActions(ctx)
506
Colin Cross50ddcc42019-05-16 12:28:22 -0700507 if a.usesLibrary.enforceUsesLibraries() {
508 manifestCheckFile := a.usesLibrary.verifyUsesLibrariesManifest(ctx, a.mergedManifestFile)
509 apkDeps = append(apkDeps, manifestCheckFile)
510 }
511
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800512 a.proguardBuildActions(ctx)
513
514 dexJarFile := a.dexBuildActions(ctx)
515
Peter Collingbournead84f972019-12-17 16:46:18 -0800516 jniLibs, certificateDeps := collectAppDeps(ctx, a.shouldEmbedJnis(ctx))
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800517 jniJarFile := a.jniBuildActions(jniLibs, ctx)
518
519 if ctx.Failed() {
520 return
521 }
522
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700523 certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx)
524 a.certificate = certificates[0]
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800525
526 // Build a final signed app package.
Jaewoong Jung5a498812019-11-07 14:14:38 -0800527 packageFile := android.PathForModuleOut(ctx, a.installApkName+".apk")
Songchun Fan688de9a2020-03-24 20:32:24 -0700528 v4SigningRequested := Bool(a.Module.deviceProperties.V4_signature)
529 var v4SignatureFile android.WritablePath = nil
530 if v4SigningRequested {
531 v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+".apk.idsig")
532 }
533 CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates, apkDeps, v4SignatureFile)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800534 a.outputFile = packageFile
Songchun Fan688de9a2020-03-24 20:32:24 -0700535 if v4SigningRequested {
536 a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
537 }
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800538
Colin Crosse560c4a2019-03-19 16:03:11 -0700539 for _, split := range a.aapt.splits {
540 // Sign the split APKs
Jaewoong Jung5a498812019-11-07 14:14:38 -0800541 packageFile := android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk")
Songchun Fan688de9a2020-03-24 20:32:24 -0700542 if v4SigningRequested {
543 v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk.idsig")
544 }
545 CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates, apkDeps, v4SignatureFile)
Colin Crosse560c4a2019-03-19 16:03:11 -0700546 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
Songchun Fan688de9a2020-03-24 20:32:24 -0700547 if v4SigningRequested {
548 a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
549 }
Colin Crosse560c4a2019-03-19 16:03:11 -0700550 }
551
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800552 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700553 bundleFile := android.PathForModuleOut(ctx, "base.zip")
554 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
555 a.bundleFile = bundleFile
556
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800557 // Install the app package.
Jiyong Park8ba50f92019-11-13 15:01:01 +0900558 if (Bool(a.Module.properties.Installable) || ctx.Host()) && a.IsForPlatform() {
559 ctx.InstallFile(a.installDir, a.outputFile.Base(), a.outputFile)
560 for _, extra := range a.extraOutputFiles {
561 ctx.InstallFile(a.installDir, extra.Base(), extra)
562 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800563 }
Colin Cross30e076a2015-04-13 13:58:27 -0700564}
565
Peter Collingbournead84f972019-12-17 16:46:18 -0800566func collectAppDeps(ctx android.ModuleContext, shouldCollectRecursiveNativeDeps bool) ([]jniLib, []Certificate) {
Colin Crossa4f08812018-10-02 22:03:40 -0700567 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900568 var certificates []Certificate
Peter Collingbournead84f972019-12-17 16:46:18 -0800569 seenModulePaths := make(map[string]bool)
Colin Crossa4f08812018-10-02 22:03:40 -0700570
Peter Collingbournead84f972019-12-17 16:46:18 -0800571 ctx.WalkDeps(func(module android.Module, parent android.Module) bool {
Colin Crossa4f08812018-10-02 22:03:40 -0700572 otherName := ctx.OtherModuleName(module)
573 tag := ctx.OtherModuleDependencyTag(module)
574
Peter Collingbournead84f972019-12-17 16:46:18 -0800575 if IsJniDepTag(tag) || tag == cc.SharedDepTag {
Colin Crossa4f08812018-10-02 22:03:40 -0700576 if dep, ok := module.(*cc.Module); ok {
Peter Collingbournead84f972019-12-17 16:46:18 -0800577 if dep.IsNdk() || dep.IsStubs() {
578 return false
579 }
580
Colin Crossa4f08812018-10-02 22:03:40 -0700581 lib := dep.OutputFile()
Peter Collingbournead84f972019-12-17 16:46:18 -0800582 path := lib.Path()
583 if seenModulePaths[path.String()] {
584 return false
585 }
586 seenModulePaths[path.String()] = true
587
Colin Crossa4f08812018-10-02 22:03:40 -0700588 if lib.Valid() {
589 jniLibs = append(jniLibs, jniLib{
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700590 name: ctx.OtherModuleName(module),
591 path: path,
592 target: module.Target(),
593 coverageFile: dep.CoverageOutputFile(),
Colin Crossa4f08812018-10-02 22:03:40 -0700594 })
595 } else {
596 ctx.ModuleErrorf("dependency %q missing output file", otherName)
597 }
598 } else {
599 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
Colin Crossa4f08812018-10-02 22:03:40 -0700600 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800601
602 return shouldCollectRecursiveNativeDeps
603 }
604
605 if tag == certificateTag {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700606 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900607 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700608 } else {
609 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
610 }
Colin Crossa4f08812018-10-02 22:03:40 -0700611 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800612
613 return false
Colin Crossa4f08812018-10-02 22:03:40 -0700614 })
615
Colin Crossbd01e2a2018-10-04 15:21:03 -0700616 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700617}
618
Colin Cross0ea8ba82019-06-06 14:33:29 -0700619func (a *AndroidApp) getCertString(ctx android.BaseModuleContext) string {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800620 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
621 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000622 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800623 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800624 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800625}
626
Jiyong Park0f80c182020-01-31 02:49:53 +0900627func (a *AndroidApp) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
628 if IsJniDepTag(ctx.OtherModuleDependencyTag(dep)) {
629 return true
630 }
631 return a.Library.DepIsInSameApex(ctx, dep)
632}
633
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900634// For OutputFileProducer interface
635func (a *AndroidApp) OutputFiles(tag string) (android.Paths, error) {
636 switch tag {
637 case ".aapt.srcjar":
638 return []android.Path{a.aaptSrcJar}, nil
639 }
640 return a.Library.OutputFiles(tag)
641}
642
Jiyong Parkf7487312019-10-17 12:54:30 +0900643func (a *AndroidApp) Privileged() bool {
644 return Bool(a.appProperties.Privileged)
645}
646
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700647func (a *AndroidApp) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
648 return ctx.Device() && (ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled())
649}
650
651func (a *AndroidApp) PreventInstall() {
652 a.appProperties.PreventInstall = true
653}
654
655func (a *AndroidApp) HideFromMake() {
656 a.appProperties.HideFromMake = true
657}
658
659func (a *AndroidApp) MarkAsCoverageVariant(coverage bool) {
660 a.appProperties.IsCoverageVariant = coverage
661}
662
663var _ cc.Coverage = (*AndroidApp)(nil)
664
Colin Cross1b16b0e2019-02-12 14:41:32 -0800665// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700666func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700667 module := &AndroidApp{}
668
Sasha Smundak2057f822019-04-16 17:16:58 -0700669 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross66dbc0b2017-12-28 12:23:20 -0800670 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
671
Colin Crossae5caf52018-05-22 11:11:52 -0700672 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700673 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700674
Colin Cross36242852017-06-23 15:06:31 -0700675 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700676 &module.Module.properties,
677 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800678 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700679 &module.Module.protoProperties,
680 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800681 &module.appProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700682 &module.overridableAppProperties,
683 &module.usesLibrary.usesLibraryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700684
Colin Crossa9d8bee2018-10-02 13:59:46 -0700685 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
686 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
687 })
688
Colin Crossa4f08812018-10-02 22:03:40 -0700689 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
690 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800691 android.InitOverridableModule(module, &module.appProperties.Overrides)
Jiyong Park52cd06f2019-11-11 10:14:32 +0900692 android.InitApexModule(module)
Colin Crossa4f08812018-10-02 22:03:40 -0700693
Colin Cross36242852017-06-23 15:06:31 -0700694 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700695}
Colin Crossae5caf52018-05-22 11:11:52 -0700696
697type appTestProperties struct {
698 Instrumentation_for *string
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700699
700 // if specified, the instrumentation target package name in the manifest is overwritten by it.
701 Instrumentation_target_package *string
Colin Crossae5caf52018-05-22 11:11:52 -0700702}
703
704type AndroidTest struct {
705 AndroidApp
706
707 appTestProperties appTestProperties
708
709 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700710
711 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700712 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700713}
714
Jaewoong Jung0949f312019-09-11 10:25:18 -0700715func (a *AndroidTest) InstallInTestcases() bool {
716 return true
717}
718
Colin Crossae5caf52018-05-22 11:11:52 -0700719func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700720 if a.appTestProperties.Instrumentation_target_package != nil {
721 a.additionalAaptFlags = append(a.additionalAaptFlags,
722 "--rename-instrumentation-target-package "+*a.appTestProperties.Instrumentation_target_package)
723 } else if a.appTestProperties.Instrumentation_for != nil {
724 // Check if the instrumentation target package is overridden.
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800725 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
726 if overridden {
727 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
728 }
729 }
Colin Crossae5caf52018-05-22 11:11:52 -0700730 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700731
Jaewoong Jung39982342020-01-14 10:27:18 -0800732 testConfig := tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700733 a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites, a.testProperties.Auto_gen_config)
Jaewoong Jung39982342020-01-14 10:27:18 -0800734 a.testConfig = a.FixTestConfig(ctx, testConfig)
Colin Cross8a497952019-03-05 22:25:09 -0800735 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700736}
737
Jaewoong Jung39982342020-01-14 10:27:18 -0800738func (a *AndroidTest) FixTestConfig(ctx android.ModuleContext, testConfig android.Path) android.Path {
739 if testConfig == nil {
740 return nil
741 }
742
743 fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", "AndroidTest.xml")
744 rule := android.NewRuleBuilder()
745 command := rule.Command().BuiltTool(ctx, "test_config_fixer").Input(testConfig).Output(fixedConfig)
746 fixNeeded := false
747
748 if ctx.ModuleName() != a.installApkName {
749 fixNeeded = true
750 command.FlagWithArg("--test-file-name ", a.installApkName+".apk")
751 }
752
753 if a.overridableAppProperties.Package_name != nil {
754 fixNeeded = true
755 command.FlagWithInput("--manifest ", a.manifestPath).
756 FlagWithArg("--package-name ", *a.overridableAppProperties.Package_name)
757 }
758
759 if fixNeeded {
760 rule.Build(pctx, ctx, "fix_test_config", "fix test config")
761 return fixedConfig
762 }
763 return testConfig
764}
765
Colin Cross303e21f2018-08-07 16:49:25 -0700766func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -0700767 a.AndroidApp.DepsMutator(ctx)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700768}
769
770func (a *AndroidTest) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
771 a.AndroidApp.OverridablePropertiesDepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -0700772 if a.appTestProperties.Instrumentation_for != nil {
773 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
774 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
775 // use instrumentationForTag instead of libTag.
776 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
777 }
Colin Crossae5caf52018-05-22 11:11:52 -0700778}
779
Colin Cross1b16b0e2019-02-12 14:41:32 -0800780// android_test compiles test sources and Android resources into an Android application package `.apk` file and
781// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -0700782func AndroidTestFactory() android.Module {
783 module := &AndroidTest{}
784
Sasha Smundak2057f822019-04-16 17:16:58 -0700785 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross5067db92018-09-17 16:46:35 -0700786
787 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700788 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800789 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700790 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800791 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -0700792
793 module.AddProperties(
794 &module.Module.properties,
795 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800796 &module.Module.dexpreoptProperties,
Colin Crossae5caf52018-05-22 11:11:52 -0700797 &module.Module.protoProperties,
798 &module.aaptProperties,
799 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700800 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800801 &module.overridableAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700802 &module.usesLibrary.usesLibraryProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700803 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700804
Colin Crossa4f08812018-10-02 22:03:40 -0700805 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
806 android.InitDefaultableModule(module)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700807 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossae5caf52018-05-22 11:11:52 -0700808 return module
809}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700810
Colin Cross252fc6f2018-10-04 15:22:03 -0700811type appTestHelperAppProperties struct {
812 // list of compatibility suites (for example "cts", "vts") that the module should be
813 // installed into.
814 Test_suites []string `android:"arch_variant"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700815
816 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
817 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
818 // explicitly.
819 Auto_gen_config *bool
Colin Cross252fc6f2018-10-04 15:22:03 -0700820}
821
822type AndroidTestHelperApp struct {
823 AndroidApp
824
825 appTestHelperAppProperties appTestHelperAppProperties
826}
827
Jaewoong Jung326a9412019-11-21 10:41:00 -0800828func (a *AndroidTestHelperApp) InstallInTestcases() bool {
829 return true
830}
831
Colin Cross1b16b0e2019-02-12 14:41:32 -0800832// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
833// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
834// test.
Colin Cross252fc6f2018-10-04 15:22:03 -0700835func AndroidTestHelperAppFactory() android.Module {
836 module := &AndroidTestHelperApp{}
837
Sasha Smundak2057f822019-04-16 17:16:58 -0700838 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700839
840 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800841 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700842 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800843 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700844
845 module.AddProperties(
846 &module.Module.properties,
847 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800848 &module.Module.dexpreoptProperties,
Colin Cross252fc6f2018-10-04 15:22:03 -0700849 &module.Module.protoProperties,
850 &module.aaptProperties,
851 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800852 &module.appTestHelperAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700853 &module.overridableAppProperties,
854 &module.usesLibrary.usesLibraryProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -0700855
856 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
857 android.InitDefaultableModule(module)
Anton Hansson3d2b6b42020-01-10 15:06:01 +0000858 android.InitApexModule(module)
Colin Cross252fc6f2018-10-04 15:22:03 -0700859 return module
860}
861
Colin Crossbd01e2a2018-10-04 15:21:03 -0700862type AndroidAppCertificate struct {
863 android.ModuleBase
864 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900865 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -0700866}
867
868type AndroidAppCertificateProperties struct {
869 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
870 Certificate *string
871}
872
Colin Cross1b16b0e2019-02-12 14:41:32 -0800873// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
874// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -0700875func AndroidAppCertificateFactory() android.Module {
876 module := &AndroidAppCertificate{}
877 module.AddProperties(&module.properties)
878 android.InitAndroidModule(module)
879 return module
880}
881
Colin Crossbd01e2a2018-10-04 15:21:03 -0700882func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
883 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900884 c.Certificate = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -0800885 Pem: android.PathForModuleSrc(ctx, cert+".x509.pem"),
886 Key: android.PathForModuleSrc(ctx, cert+".pk8"),
Colin Crossbd01e2a2018-10-04 15:21:03 -0700887 }
888}
Jaewoong Jung525443a2019-02-28 15:35:54 -0800889
890type OverrideAndroidApp struct {
891 android.ModuleBase
892 android.OverrideModuleBase
893}
894
895func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
896 // All the overrides happen in the base module.
897 // TODO(jungjw): Check the base module type.
898}
899
900// override_android_app is used to create an android_app module based on another android_app by overriding
901// some of its properties.
902func OverrideAndroidAppModuleFactory() android.Module {
903 m := &OverrideAndroidApp{}
904 m.AddProperties(&overridableAppProperties{})
905
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700906 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800907 android.InitOverrideModule(m)
908 return m
909}
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700910
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700911type OverrideAndroidTest struct {
912 android.ModuleBase
913 android.OverrideModuleBase
914}
915
916func (i *OverrideAndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
917 // All the overrides happen in the base module.
918 // TODO(jungjw): Check the base module type.
919}
920
921// override_android_test is used to create an android_app module based on another android_test by overriding
922// some of its properties.
923func OverrideAndroidTestModuleFactory() android.Module {
924 m := &OverrideAndroidTest{}
925 m.AddProperties(&overridableAppProperties{})
926 m.AddProperties(&appTestProperties{})
927
928 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
929 android.InitOverrideModule(m)
930 return m
931}
932
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700933type AndroidAppImport struct {
934 android.ModuleBase
935 android.DefaultableModuleBase
936 prebuilt android.Prebuilt
937
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700938 properties AndroidAppImportProperties
939 dpiVariants interface{}
940 archVariants interface{}
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700941
942 outputFile android.Path
Colin Cross503c1d02020-01-28 14:00:53 -0800943 certificate Certificate
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700944
945 dexpreopter
Colin Cross50ddcc42019-05-16 12:28:22 -0700946
947 usesLibrary usesLibrary
Jaewoong Jung8aae22e2019-07-17 10:21:49 -0700948
Colin Cross70dda7e2019-10-01 22:05:35 -0700949 installPath android.InstallPath
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700950}
951
952type AndroidAppImportProperties struct {
953 // A prebuilt apk to import
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700954 Apk *string
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700955
Jaewoong Jung961d4fd2019-08-22 14:25:58 -0700956 // The name of a certificate in the default certificate directory or an android_app_certificate
957 // module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700958 Certificate *string
959
960 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
961 // be set for presigned modules.
962 Presigned *bool
963
Jaewoong Jung961d4fd2019-08-22 14:25:58 -0700964 // Sign with the default system dev certificate. Must be used judiciously. Most imported apps
965 // need to either specify a specific certificate or be presigned.
966 Default_dev_cert *bool
967
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700968 // Specifies that this app should be installed to the priv-app directory,
969 // where the system will grant it additional privileges not available to
970 // normal apps.
971 Privileged *bool
972
973 // Names of modules to be overridden. Listed modules can only be other binaries
974 // (in Make or Soong).
975 // This does not completely prevent installation of the overridden binaries, but if both
976 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
977 // from PRODUCT_PACKAGES.
978 Overrides []string
Jaewoong Jung8aae22e2019-07-17 10:21:49 -0700979
980 // Optional name for the installed app. If unspecified, it is derived from the module name.
981 Filename *string
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700982}
983
Martin Stjernholm6d415272020-01-31 17:10:36 +0000984func (a *AndroidAppImport) IsInstallable() bool {
985 return true
986}
987
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700988// Updates properties with variant-specific values.
989func (a *AndroidAppImport) processVariants(ctx android.LoadHookContext) {
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700990 config := ctx.Config()
991
992 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants")
993 // Try DPI variant matches in the reverse-priority order so that the highest priority match
994 // overwrites everything else.
995 // TODO(jungjw): Can we optimize this by making it priority order?
996 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700997 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700998 }
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700999 if config.ProductAAPTPreferredConfig() != "" {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001000 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001001 }
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001002
1003 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName("Arch")
1004 archType := ctx.Config().Targets[android.Android][0].Arch.ArchType
1005 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001006}
1007
Colin Cross1184b642019-12-30 18:43:07 -08001008func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001009 dst interface{}, variantGroup reflect.Value, variant string) {
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001010 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
1011 if !src.IsValid() {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001012 return
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001013 }
1014
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001015 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
1016 if err != nil {
1017 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1018 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1019 } else {
1020 panic(err)
1021 }
1022 }
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001023}
1024
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001025func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
1026 cert := android.SrcIsModule(String(a.properties.Certificate))
1027 if cert != "" {
1028 ctx.AddDependency(ctx.Module(), certificateTag, cert)
1029 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001030
Paul Duffin250e6192019-06-07 10:44:37 +01001031 a.usesLibrary.deps(ctx, true)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001032}
1033
1034func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
1035 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001036 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
1037 // with them may invalidate pre-existing signature data.
1038 if ctx.InstallInTestcases() && Bool(a.properties.Presigned) {
1039 ctx.Build(pctx, android.BuildParams{
1040 Rule: android.Cp,
1041 Output: outputPath,
1042 Input: inputPath,
1043 })
1044 return
1045 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001046 rule := android.NewRuleBuilder()
1047 rule.Command().
1048 Textf(`if (zipinfo %s 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
Colin Crossee94d6a2019-07-08 17:08:34 -07001049 BuiltTool(ctx, "zip2zip").
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001050 FlagWithInput("-i ", inputPath).
1051 FlagWithOutput("-o ", outputPath).
1052 FlagWithArg("-0 ", "'lib/**/*.so'").
1053 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
1054 rule.Build(pctx, ctx, "uncompress-embedded-jni-libs", "Uncompress embedded JIN libs")
1055}
1056
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001057// Returns whether this module should have the dex file stored uncompressed in the APK.
1058func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
1059 if ctx.Config().UnbundledBuild() {
1060 return false
1061 }
1062
1063 // Uncompress dex in APKs of privileged apps
Jiyong Parkf7487312019-10-17 12:54:30 +09001064 if ctx.Config().UncompressPrivAppDex() && a.Privileged() {
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001065 return true
1066 }
1067
1068 return shouldUncompressDex(ctx, &a.dexpreopter)
1069}
1070
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001071func (a *AndroidAppImport) uncompressDex(
1072 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
1073 rule := android.NewRuleBuilder()
1074 rule.Command().
1075 Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
Colin Crossee94d6a2019-07-08 17:08:34 -07001076 BuiltTool(ctx, "zip2zip").
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001077 FlagWithInput("-i ", inputPath).
1078 FlagWithOutput("-o ", outputPath).
1079 FlagWithArg("-0 ", "'classes*.dex'").
1080 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
1081 rule.Build(pctx, ctx, "uncompress-dex", "Uncompress dex files")
1082}
1083
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001084func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001085 a.generateAndroidBuildActions(ctx)
1086}
1087
1088func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001089 numCertPropsSet := 0
1090 if String(a.properties.Certificate) != "" {
1091 numCertPropsSet++
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001092 }
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001093 if Bool(a.properties.Presigned) {
1094 numCertPropsSet++
1095 }
1096 if Bool(a.properties.Default_dev_cert) {
1097 numCertPropsSet++
1098 }
1099 if numCertPropsSet != 1 {
1100 ctx.ModuleErrorf("One and only one of certficate, presigned, and default_dev_cert properties must be set")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001101 }
1102
Peter Collingbournead84f972019-12-17 16:46:18 -08001103 _, certificates := collectAppDeps(ctx, false)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001104
1105 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001106 // TODO: LOCAL_PACKAGE_SPLITS
1107
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001108 srcApk := a.prebuilt.SingleSourcePath(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -07001109
1110 if a.usesLibrary.enforceUsesLibraries() {
1111 srcApk = a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk)
1112 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001113
1114 // TODO: Install or embed JNI libraries
1115
1116 // Uncompress JNI libraries in the apk
1117 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
1118 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
1119
Kyeongkab.Namc4997142019-11-22 11:38:16 +09001120 var installDir android.InstallPath
1121 if Bool(a.properties.Privileged) {
1122 installDir = android.PathForModuleInstall(ctx, "priv-app", a.BaseModuleName())
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001123 } else if ctx.InstallInTestcases() {
1124 installDir = android.PathForModuleInstall(ctx, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch())
Kyeongkab.Namc4997142019-11-22 11:38:16 +09001125 } else {
1126 installDir = android.PathForModuleInstall(ctx, "app", a.BaseModuleName())
1127 }
1128
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001129 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001130 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001131 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -07001132
1133 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
1134 a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
1135 a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
1136 a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
1137
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001138 dexOutput := a.dexpreopter.dexpreopt(ctx, jnisUncompressed)
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001139 if a.dexpreopter.uncompressedDex {
1140 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
1141 a.uncompressDex(ctx, dexOutput, dexUncompressed.OutputPath)
1142 dexOutput = dexUncompressed
1143 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001144
1145 // Sign or align the package
1146 // TODO: Handle EXTERNAL
1147 if !Bool(a.properties.Presigned) {
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001148 // If the certificate property is empty at this point, default_dev_cert must be set to true.
1149 // Which makes processMainCert's behavior for the empty cert string WAI.
1150 certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001151 if len(certificates) != 1 {
1152 ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates)
1153 }
Colin Cross503c1d02020-01-28 14:00:53 -08001154 a.certificate = certificates[0]
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001155 signed := android.PathForModuleOut(ctx, "signed", ctx.ModuleName()+".apk")
Songchun Fan688de9a2020-03-24 20:32:24 -07001156 SignAppPackage(ctx, signed, dexOutput, certificates, nil)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001157 a.outputFile = signed
1158 } else {
1159 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", ctx.ModuleName()+".apk")
1160 TransformZipAlign(ctx, alignedApk, dexOutput)
1161 a.outputFile = alignedApk
Colin Cross503c1d02020-01-28 14:00:53 -08001162 a.certificate = presignedCertificate
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001163 }
1164
1165 // TODO: Optionally compress the output apk.
1166
Jaewoong Jung8aae22e2019-07-17 10:21:49 -07001167 a.installPath = ctx.InstallFile(installDir,
1168 proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk"), a.outputFile)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001169
1170 // TODO: androidmk converter jni libs
1171}
1172
1173func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
1174 return &a.prebuilt
1175}
1176
1177func (a *AndroidAppImport) Name() string {
1178 return a.prebuilt.Name(a.ModuleBase.Name())
1179}
1180
Dario Frenicde2a032019-10-27 00:29:22 +01001181func (a *AndroidAppImport) OutputFile() android.Path {
1182 return a.outputFile
1183}
1184
Jiyong Park618922e2020-01-08 13:35:43 +09001185func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
1186 return nil
1187}
1188
Colin Cross503c1d02020-01-28 14:00:53 -08001189func (a *AndroidAppImport) Certificate() Certificate {
1190 return a.certificate
1191}
1192
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001193var dpiVariantGroupType reflect.Type
1194var archVariantGroupType reflect.Type
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001195
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001196func initAndroidAppImportVariantGroupTypes() {
1197 dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants")
1198
1199 archNames := make([]string, len(android.ArchTypeList()))
1200 for i, archType := range android.ArchTypeList() {
1201 archNames[i] = archType.Name
1202 }
1203 archVariantGroupType = createVariantGroupType(archNames, "Arch")
1204}
1205
1206// Populates all variant struct properties at creation time.
1207func (a *AndroidAppImport) populateAllVariantStructs() {
1208 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
1209 a.AddProperties(a.dpiVariants)
1210
1211 a.archVariants = reflect.New(archVariantGroupType).Interface()
1212 a.AddProperties(a.archVariants)
1213}
1214
Jiyong Parkf7487312019-10-17 12:54:30 +09001215func (a *AndroidAppImport) Privileged() bool {
1216 return Bool(a.properties.Privileged)
1217}
1218
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001219func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
1220 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
1221
1222 variantFields := make([]reflect.StructField, len(variants))
1223 for i, variant := range variants {
1224 variantFields[i] = reflect.StructField{
1225 Name: proptools.FieldNameForProperty(variant),
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001226 Type: props,
1227 }
1228 }
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001229
1230 variantGroupStruct := reflect.StructOf(variantFields)
1231 return reflect.StructOf([]reflect.StructField{
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001232 {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001233 Name: variantGroupName,
1234 Type: variantGroupStruct,
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001235 },
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001236 })
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001237}
1238
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001239// android_app_import imports a prebuilt apk with additional processing specified in the module.
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001240// DPI-specific apk source files can be specified using dpi_variants. Example:
1241//
1242// android_app_import {
1243// name: "example_import",
1244// apk: "prebuilts/example.apk",
1245// dpi_variants: {
1246// mdpi: {
1247// apk: "prebuilts/example_mdpi.apk",
1248// },
1249// xhdpi: {
1250// apk: "prebuilts/example_xhdpi.apk",
1251// },
1252// },
1253// certificate: "PRESIGNED",
1254// }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001255func AndroidAppImportFactory() android.Module {
1256 module := &AndroidAppImport{}
1257 module.AddProperties(&module.properties)
1258 module.AddProperties(&module.dexpreoptProperties)
Colin Cross50ddcc42019-05-16 12:28:22 -07001259 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001260 module.populateAllVariantStructs()
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001261 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001262 module.processVariants(ctx)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001263 })
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001264
1265 InitJavaModule(module, android.DeviceSupported)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001266 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001267
1268 return module
1269}
Colin Cross50ddcc42019-05-16 12:28:22 -07001270
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001271type AndroidTestImport struct {
1272 AndroidAppImport
1273
1274 testProperties testProperties
1275
1276 data android.Paths
1277}
1278
1279func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1280 a.generateAndroidBuildActions(ctx)
1281
1282 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
1283}
1284
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001285func (a *AndroidTestImport) InstallInTestcases() bool {
1286 return true
1287}
1288
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001289// android_test_import imports a prebuilt test apk with additional processing specified in the
1290// module. DPI or arch variant configurations can be made as with android_app_import.
1291func AndroidTestImportFactory() android.Module {
1292 module := &AndroidTestImport{}
1293 module.AddProperties(&module.properties)
1294 module.AddProperties(&module.dexpreoptProperties)
1295 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
1296 module.AddProperties(&module.testProperties)
1297 module.populateAllVariantStructs()
1298 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
1299 module.processVariants(ctx)
1300 })
1301
1302 InitJavaModule(module, android.DeviceSupported)
1303 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
1304
1305 return module
1306}
1307
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001308type RuntimeResourceOverlay struct {
1309 android.ModuleBase
1310 android.DefaultableModuleBase
1311 aapt
1312
1313 properties RuntimeResourceOverlayProperties
1314
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08001315 certificate Certificate
1316
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001317 outputFile android.Path
1318 installDir android.InstallPath
1319}
1320
1321type RuntimeResourceOverlayProperties struct {
1322 // the name of a certificate in the default certificate directory or an android_app_certificate
1323 // module name in the form ":module".
1324 Certificate *string
1325
1326 // optional theme name. If specified, the overlay package will be applied
1327 // only when the ro.boot.vendor.overlay.theme system property is set to the same value.
1328 Theme *string
1329
1330 // if not blank, set to the version of the sdk to compile against.
1331 // Defaults to compiling against the current platform.
1332 Sdk_version *string
1333
1334 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
1335 // Defaults to sdk_version if not set.
1336 Min_sdk_version *string
1337}
1338
1339func (r *RuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
1340 sdkDep := decodeSdkDep(ctx, sdkContext(r))
1341 if sdkDep.hasFrameworkLibs() {
1342 r.aapt.deps(ctx, sdkDep)
1343 }
1344
1345 cert := android.SrcIsModule(String(r.properties.Certificate))
1346 if cert != "" {
1347 ctx.AddDependency(ctx.Module(), certificateTag, cert)
1348 }
1349}
1350
1351func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1352 // Compile and link resources
1353 r.aapt.hasNoCode = true
Jaewoong Jungf0f747c2020-01-24 10:30:02 -08001354 // Do not remove resources without default values nor dedupe resource configurations with the same value
1355 r.aapt.buildActions(ctx, r, "--no-resource-deduping", "--no-resource-removal")
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001356
1357 // Sign the built package
1358 _, certificates := collectAppDeps(ctx, false)
1359 certificates = processMainCert(r.ModuleBase, String(r.properties.Certificate), certificates, ctx)
1360 signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
Songchun Fan688de9a2020-03-24 20:32:24 -07001361 SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil)
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08001362 r.certificate = certificates[0]
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001363
1364 r.outputFile = signed
1365 r.installDir = android.PathForModuleInstall(ctx, "overlay", String(r.properties.Theme))
1366 ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile)
1367}
1368
Jiyong Park6a927c42020-01-21 02:03:43 +09001369func (r *RuntimeResourceOverlay) sdkVersion() sdkSpec {
1370 return sdkSpecFrom(String(r.properties.Sdk_version))
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001371}
1372
1373func (r *RuntimeResourceOverlay) systemModules() string {
1374 return ""
1375}
1376
Jiyong Park6a927c42020-01-21 02:03:43 +09001377func (r *RuntimeResourceOverlay) minSdkVersion() sdkSpec {
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001378 if r.properties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +09001379 return sdkSpecFrom(*r.properties.Min_sdk_version)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001380 }
1381 return r.sdkVersion()
1382}
1383
Jiyong Park6a927c42020-01-21 02:03:43 +09001384func (r *RuntimeResourceOverlay) targetSdkVersion() sdkSpec {
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001385 return r.sdkVersion()
1386}
1387
1388// runtime_resource_overlay generates a resource-only apk file that can overlay application and
1389// system resources at run time.
1390func RuntimeResourceOverlayFactory() android.Module {
1391 module := &RuntimeResourceOverlay{}
1392 module.AddProperties(
1393 &module.properties,
1394 &module.aaptProperties)
1395
1396 InitJavaModule(module, android.DeviceSupported)
1397
1398 return module
1399}
1400
Colin Cross50ddcc42019-05-16 12:28:22 -07001401type UsesLibraryProperties struct {
1402 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file.
1403 Uses_libs []string
1404
1405 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file with
1406 // required=false.
1407 Optional_uses_libs []string
1408
1409 // If true, the list of uses_libs and optional_uses_libs modules must match the AndroidManifest.xml file. Defaults
1410 // to true if either uses_libs or optional_uses_libs is set. Will unconditionally default to true in the future.
1411 Enforce_uses_libs *bool
1412}
1413
1414// usesLibrary provides properties and helper functions for AndroidApp and AndroidAppImport to verify that the
1415// <uses-library> tags that end up in the manifest of an APK match the ones known to the build system through the
1416// uses_libs and optional_uses_libs properties. The build system's values are used by dexpreopt to preopt apps
1417// with knowledge of their shared libraries.
1418type usesLibrary struct {
1419 usesLibraryProperties UsesLibraryProperties
1420}
1421
Paul Duffin250e6192019-06-07 10:44:37 +01001422func (u *usesLibrary) deps(ctx android.BottomUpMutatorContext, hasFrameworkLibs bool) {
Colin Cross3245b2c2019-06-07 13:18:09 -07001423 if !ctx.Config().UnbundledBuild() {
1424 ctx.AddVariationDependencies(nil, usesLibTag, u.usesLibraryProperties.Uses_libs...)
1425 ctx.AddVariationDependencies(nil, usesLibTag, u.presentOptionalUsesLibs(ctx)...)
Paul Duffin250e6192019-06-07 10:44:37 +01001426 // Only add these extra dependencies if the module depends on framework libs. This avoids
1427 // creating a cyclic dependency:
1428 // e.g. framework-res -> org.apache.http.legacy -> ... -> framework-res.
1429 if hasFrameworkLibs {
Colin Cross3245b2c2019-06-07 13:18:09 -07001430 // dexpreopt/dexpreopt.go needs the paths to the dex jars of these libraries in case construct_context.sh needs
1431 // to pass them to dex2oat. Add them as a dependency so we can determine the path to the dex jar of each
1432 // library to dexpreopt.
1433 ctx.AddVariationDependencies(nil, usesLibTag,
1434 "org.apache.http.legacy",
1435 "android.hidl.base-V1.0-java",
1436 "android.hidl.manager-V1.0-java")
1437 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001438 }
1439}
1440
1441// presentOptionalUsesLibs returns optional_uses_libs after filtering out MissingUsesLibraries, which don't exist in the
1442// build.
1443func (u *usesLibrary) presentOptionalUsesLibs(ctx android.BaseModuleContext) []string {
1444 optionalUsesLibs, _ := android.FilterList(u.usesLibraryProperties.Optional_uses_libs, ctx.Config().MissingUsesLibraries())
1445 return optionalUsesLibs
1446}
1447
1448// usesLibraryPaths returns a map of module names of shared library dependencies to the paths to their dex jars.
1449func (u *usesLibrary) usesLibraryPaths(ctx android.ModuleContext) map[string]android.Path {
1450 usesLibPaths := make(map[string]android.Path)
1451
1452 if !ctx.Config().UnbundledBuild() {
1453 ctx.VisitDirectDepsWithTag(usesLibTag, func(m android.Module) {
1454 if lib, ok := m.(Dependency); ok {
1455 if dexJar := lib.DexJar(); dexJar != nil {
1456 usesLibPaths[ctx.OtherModuleName(m)] = dexJar
1457 } else {
1458 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must produce a dex jar, does it have installable: true?",
1459 ctx.OtherModuleName(m))
1460 }
1461 } else if ctx.Config().AllowMissingDependencies() {
1462 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(m)})
1463 } else {
1464 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must be a java library",
1465 ctx.OtherModuleName(m))
1466 }
1467 })
1468 }
1469
1470 return usesLibPaths
1471}
1472
1473// enforceUsesLibraries returns true of <uses-library> tags should be checked against uses_libs and optional_uses_libs
1474// properties. Defaults to true if either of uses_libs or optional_uses_libs is specified. Will default to true
1475// unconditionally in the future.
1476func (u *usesLibrary) enforceUsesLibraries() bool {
1477 defaultEnforceUsesLibs := len(u.usesLibraryProperties.Uses_libs) > 0 ||
1478 len(u.usesLibraryProperties.Optional_uses_libs) > 0
1479 return BoolDefault(u.usesLibraryProperties.Enforce_uses_libs, defaultEnforceUsesLibs)
1480}
1481
1482// verifyUsesLibrariesManifest checks the <uses-library> tags in an AndroidManifest.xml against the ones specified
1483// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the manifest.
1484func (u *usesLibrary) verifyUsesLibrariesManifest(ctx android.ModuleContext, manifest android.Path) android.Path {
1485 outputFile := android.PathForModuleOut(ctx, "manifest_check", "AndroidManifest.xml")
1486
1487 rule := android.NewRuleBuilder()
Colin Crossee94d6a2019-07-08 17:08:34 -07001488 cmd := rule.Command().BuiltTool(ctx, "manifest_check").
Colin Cross50ddcc42019-05-16 12:28:22 -07001489 Flag("--enforce-uses-libraries").
1490 Input(manifest).
1491 FlagWithOutput("-o ", outputFile)
1492
1493 for _, lib := range u.usesLibraryProperties.Uses_libs {
1494 cmd.FlagWithArg("--uses-library ", lib)
1495 }
1496
1497 for _, lib := range u.usesLibraryProperties.Optional_uses_libs {
1498 cmd.FlagWithArg("--optional-uses-library ", lib)
1499 }
1500
1501 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1502
1503 return outputFile
1504}
1505
1506// verifyUsesLibrariesAPK checks the <uses-library> tags in the manifest of an APK against the ones specified
1507// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the APK.
1508func (u *usesLibrary) verifyUsesLibrariesAPK(ctx android.ModuleContext, apk android.Path) android.Path {
1509 outputFile := android.PathForModuleOut(ctx, "verify_uses_libraries", apk.Base())
1510
1511 rule := android.NewRuleBuilder()
1512 aapt := ctx.Config().HostToolPath(ctx, "aapt")
1513 rule.Command().
1514 Textf("aapt_binary=%s", aapt.String()).Implicit(aapt).
1515 Textf(`uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Uses_libs, " ")).
1516 Textf(`optional_uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Optional_uses_libs, " ")).
1517 Tool(android.PathForSource(ctx, "build/make/core/verify_uses_libraries.sh")).Input(apk)
1518 rule.Command().Text("cp -f").Input(apk).Output(outputFile)
1519
1520 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1521
1522 return outputFile
1523}