blob: 6aaae07867d83c5af0de3f582ec2465002cf5874 [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"
Sasha Smundak4de27a52020-04-23 09:49:59 -070023 "strconv"
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070024 "strings"
Colin Cross30e076a2015-04-13 13:58:27 -070025
Colin Cross50ddcc42019-05-16 12:28:22 -070026 "github.com/google/blueprint"
27 "github.com/google/blueprint/proptools"
28
Colin Cross635c3b02016-05-18 15:37:25 -070029 "android/soong/android"
Colin Crossa4f08812018-10-02 22:03:40 -070030 "android/soong/cc"
Colin Cross303e21f2018-08-07 16:49:25 -070031 "android/soong/tradefed"
Colin Cross30e076a2015-04-13 13:58:27 -070032)
33
Jaewoong Jung3e18b192019-06-11 12:25:34 -070034var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070035
Colin Cross3bc7ffa2017-11-22 16:19:37 -080036func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000037 RegisterAppBuildComponents(android.InitRegistrationContext)
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -070038
39 initAndroidAppImportVariantGroupTypes()
Colin Cross3bc7ffa2017-11-22 16:19:37 -080040}
41
Paul Duffinf9b1da02019-12-18 19:51:55 +000042func RegisterAppBuildComponents(ctx android.RegistrationContext) {
43 ctx.RegisterModuleType("android_app", AndroidAppFactory)
44 ctx.RegisterModuleType("android_test", AndroidTestFactory)
45 ctx.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
46 ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
47 ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
48 ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory)
Roshan Piusb8307962020-04-27 09:42:27 -070049 ctx.RegisterModuleType("override_runtime_resource_overlay", OverrideRuntimeResourceOverlayModuleFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000050 ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory)
51 ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -080052 ctx.RegisterModuleType("runtime_resource_overlay", RuntimeResourceOverlayFactory)
Sasha Smundak4de27a52020-04-23 09:49:59 -070053 ctx.RegisterModuleType("android_app_set", AndroidApkSetFactory)
54}
55
56type AndroidAppSetProperties struct {
57 // APK Set path
58 Set *string
59
60 // Specifies that this app should be installed to the priv-app directory,
61 // where the system will grant it additional privileges not available to
62 // normal apps.
63 Privileged *bool
64
65 // APKs in this set use prerelease SDK version
66 Prerelease *bool
67
68 // Names of modules to be overridden. Listed modules can only be other apps
69 // (in Make or Soong).
70 Overrides []string
71}
72
73type AndroidAppSet struct {
74 android.ModuleBase
75 android.DefaultableModuleBase
76 prebuilt android.Prebuilt
77
78 properties AndroidAppSetProperties
79 packedOutput android.WritablePath
80 masterFile string
81}
82
83func (as *AndroidAppSet) Name() string {
84 return as.prebuilt.Name(as.ModuleBase.Name())
85}
86
87func (as *AndroidAppSet) IsInstallable() bool {
88 return true
89}
90
91func (as *AndroidAppSet) Prebuilt() *android.Prebuilt {
92 return &as.prebuilt
93}
94
95func (as *AndroidAppSet) Privileged() bool {
96 return Bool(as.properties.Privileged)
97}
98
Sasha Smundakc4f0ff12020-05-27 16:36:07 -070099func (as *AndroidAppSet) OutputFile() android.Path {
100 return as.packedOutput
101}
102
103func (as *AndroidAppSet) MasterFile() string {
104 return as.masterFile
105}
106
Jaewoong Jung8cf307e2020-05-14 14:15:24 -0700107var TargetCpuAbi = map[string]string{
Sasha Smundak4de27a52020-04-23 09:49:59 -0700108 "arm": "ARMEABI_V7A",
109 "arm64": "ARM64_V8A",
110 "x86": "X86",
111 "x86_64": "X86_64",
112}
113
Jaewoong Jung8cf307e2020-05-14 14:15:24 -0700114func SupportedAbis(ctx android.ModuleContext) []string {
Jaewoong Jung829b7132020-06-10 12:23:32 -0700115 abiName := func(targetIdx int, deviceArch string) string {
Jaewoong Jung8cf307e2020-05-14 14:15:24 -0700116 if abi, found := TargetCpuAbi[deviceArch]; found {
Sasha Smundak4de27a52020-04-23 09:49:59 -0700117 return abi
118 }
Jaewoong Jung829b7132020-06-10 12:23:32 -0700119 ctx.ModuleErrorf("Target %d has invalid Arch: %s", targetIdx, deviceArch)
Sasha Smundak4de27a52020-04-23 09:49:59 -0700120 return "BAD_ABI"
121 }
122
Jaewoong Jung829b7132020-06-10 12:23:32 -0700123 var result []string
124 for i, target := range ctx.Config().Targets[android.Android] {
125 result = append(result, abiName(i, target.Arch.ArchType.String()))
Sasha Smundak4de27a52020-04-23 09:49:59 -0700126 }
127 return result
128}
129
130func (as *AndroidAppSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Sasha Smundakc4f0ff12020-05-27 16:36:07 -0700131 as.packedOutput = android.PathForModuleOut(ctx, ctx.ModuleName()+".zip")
Sasha Smundak4de27a52020-04-23 09:49:59 -0700132 // We are assuming here that the master file in the APK
133 // set has `.apk` suffix. If it doesn't the build will fail.
134 // APK sets containing APEX files are handled elsewhere.
Sasha Smundak854c14f2020-06-16 10:28:22 -0700135 as.masterFile = as.BaseModuleName() + ".apk"
Sasha Smundak4de27a52020-04-23 09:49:59 -0700136 screenDensities := "all"
137 if dpis := ctx.Config().ProductAAPTPrebuiltDPI(); len(dpis) > 0 {
138 screenDensities = strings.ToUpper(strings.Join(dpis, ","))
139 }
140 // TODO(asmundak): handle locales.
141 // TODO(asmundak): do we support device features
142 ctx.Build(pctx,
143 android.BuildParams{
144 Rule: extractMatchingApks,
145 Description: "Extract APKs from APK set",
146 Output: as.packedOutput,
147 Inputs: android.Paths{as.prebuilt.SingleSourcePath(ctx)},
148 Args: map[string]string{
Jaewoong Jung8cf307e2020-05-14 14:15:24 -0700149 "abis": strings.Join(SupportedAbis(ctx), ","),
Sasha Smundak4de27a52020-04-23 09:49:59 -0700150 "allow-prereleased": strconv.FormatBool(proptools.Bool(as.properties.Prerelease)),
151 "screen-densities": screenDensities,
152 "sdk-version": ctx.Config().PlatformSdkVersion(),
153 "stem": ctx.ModuleName(),
154 },
155 })
Sasha Smundak4de27a52020-04-23 09:49:59 -0700156}
157
158// android_app_set extracts a set of APKs based on the target device
159// configuration and installs this set as "split APKs".
Sasha Smundakc4f0ff12020-05-27 16:36:07 -0700160// The extracted set always contains 'master' APK whose name is
161// _module_name_.apk and every split APK matching target device.
162// The extraction of the density-specific splits depends on
163// PRODUCT_AAPT_PREBUILT_DPI variable. If present (its value should
164// be a list density names: LDPI, MDPI, HDPI, etc.), only listed
165// splits will be extracted. Otherwise all density-specific splits
166// will be extracted.
Sasha Smundak4de27a52020-04-23 09:49:59 -0700167func AndroidApkSetFactory() android.Module {
168 module := &AndroidAppSet{}
169 module.AddProperties(&module.properties)
170 InitJavaModule(module, android.DeviceSupported)
171 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Set")
172 return module
Paul Duffinf9b1da02019-12-18 19:51:55 +0000173}
174
Colin Cross30e076a2015-04-13 13:58:27 -0700175// AndroidManifest.xml merging
176// package splits
177
Colin Crossfabb6082018-02-20 17:22:23 -0800178type appProperties struct {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700179 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
Colin Cross7d5136f2015-05-11 13:39:40 -0700180 Additional_certificates []string
181
182 // If set, create package-export.apk, which other packages can
183 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
Nan Zhangea568a42017-11-08 21:20:04 -0800184 Export_package_resources *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700185
Colin Cross16056062017-12-13 22:46:28 -0800186 // Specifies that this app should be installed to the priv-app directory,
187 // where the system will grant it additional privileges not available to
188 // normal apps.
189 Privileged *bool
Colin Crossa97c5d32018-03-28 14:58:31 -0700190
191 // list of resource labels to generate individual resource packages
192 Package_splits []string
Jason Monkd4122be2018-08-10 09:33:36 -0400193
194 // Names of modules to be overridden. Listed modules can only be other binaries
195 // (in Make or Soong).
196 // This does not completely prevent installation of the overridden binaries, but if both
197 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
198 // from PRODUCT_PACKAGES.
199 Overrides []string
Colin Crossa4f08812018-10-02 22:03:40 -0700200
201 // list of native libraries that will be provided in or alongside the resulting jar
202 Jni_libs []string `android:"arch_variant"`
203
Colin Cross76583a42020-05-06 17:51:39 -0700204 // if true, use JNI libraries that link against platform APIs even if this module sets
Colin Crossee87c602020-02-19 16:57:15 -0800205 // sdk_version.
206 Jni_uses_platform_apis *bool
207
Colin Cross76583a42020-05-06 17:51:39 -0700208 // if true, use JNI libraries that link against SDK APIs even if this module does not set
209 // sdk_version.
210 Jni_uses_sdk_apis *bool
211
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700212 // STL library to use for JNI libraries.
213 Stl *string `android:"arch_variant"`
214
Colin Crosse4246ab2019-02-05 21:55:21 -0800215 // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest
216 // 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 +0900217 // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to true for
218 // android_app modules that are embedded to APEXes, defaults to false for other module types where the native
219 // libraries are generally preinstalled outside the APK.
Colin Crosse4246ab2019-02-05 21:55:21 -0800220 Use_embedded_native_libs *bool
Colin Cross46abdad2019-02-07 13:07:08 -0800221
222 // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that
223 // they are used from inside the APK at runtime.
224 Use_embedded_dex *bool
Colin Cross47fa9d32019-03-26 10:51:39 -0700225
226 // Forces native libraries to always be packaged into the APK,
227 // Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed.
228 // True for android_test* modules.
229 AlwaysPackageNativeLibs bool `blueprint:"mutated"`
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700230
231 // If set, find and merge all NOTICE files that this module and its dependencies have and store
232 // it in the APK as an asset.
233 Embed_notices *bool
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700234
235 // cc.Coverage related properties
236 PreventInstall bool `blueprint:"mutated"`
237 HideFromMake bool `blueprint:"mutated"`
238 IsCoverageVariant bool `blueprint:"mutated"`
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100239
240 // Whether this app is considered mainline updatable or not. When set to true, this will enforce
Artur Satayev11962102020-04-16 13:43:02 +0100241 // additional rules to make sure an app can safely be updated. Default is false.
242 // Prefer using other specific properties if build behaviour must be changed; avoid using this
243 // flag for anything but neverallow rules (unless the behaviour change is invisible to owners).
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100244 Updatable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700245}
246
Jaewoong Jung525443a2019-02-28 15:35:54 -0800247// android_app properties that can be overridden by override_android_app
248type overridableAppProperties struct {
249 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
250 // or an android_app_certificate module name in the form ":module".
251 Certificate *string
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700252
Liz Kammer70dd74d2020-05-07 13:24:05 -0700253 // Name of the signing certificate lineage file.
254 Lineage *string
255
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700256 // the package name of this app. The package name in the manifest file is used if one was not given.
257 Package_name *string
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800258
259 // the logging parent of this app.
260 Logging_parent *string
Jaewoong Jung525443a2019-02-28 15:35:54 -0800261}
262
Roshan Piusb8307962020-04-27 09:42:27 -0700263// runtime_resource_overlay properties that can be overridden by override_runtime_resource_overlay
264type OverridableRuntimeResourceOverlayProperties struct {
265 // the package name of this app. The package name in the manifest file is used if one was not given.
266 Package_name *string
267
268 // the target package name of this overlay app. The target package name in the manifest file is used if one was not given.
269 Target_package_name *string
270}
271
Colin Cross30e076a2015-04-13 13:58:27 -0700272type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -0700273 Library
274 aapt
Jaewoong Jung525443a2019-02-28 15:35:54 -0800275 android.OverridableModuleBase
Colin Crossa97c5d32018-03-28 14:58:31 -0700276
Colin Cross50ddcc42019-05-16 12:28:22 -0700277 usesLibrary usesLibrary
278
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900279 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -0700280
Colin Crossfabb6082018-02-20 17:22:23 -0800281 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -0700282
Jaewoong Jung525443a2019-02-28 15:35:54 -0800283 overridableAppProperties overridableAppProperties
284
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700285 installJniLibs []jniLib
286 jniCoverageOutputs android.Paths
Colin Crossf6237212018-10-29 23:14:58 -0700287
288 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800289
290 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
291 installApkName string
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800292
Colin Cross70dda7e2019-10-01 22:05:35 -0700293 installDir android.InstallPath
Jaewoong Jung0949f312019-09-11 10:25:18 -0700294
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700295 onDeviceDir string
296
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800297 additionalAaptFlags []string
Jaewoong Jung98772792019-07-01 17:15:13 -0700298
299 noticeOutputs android.NoticeOutputs
Jiyong Parkaf8998c2020-02-28 16:51:07 +0900300
301 overriddenManifestPackageName string
Artur Satayevd9b503a2020-04-27 19:05:28 +0100302
303 android.ApexBundleDepsInfo
Colin Crosse1731a52017-12-14 11:22:55 -0800304}
305
Martin Stjernholm6d415272020-01-31 17:10:36 +0000306func (a *AndroidApp) IsInstallable() bool {
307 return Bool(a.properties.Installable)
308}
309
Colin Cross89c31582018-04-30 15:55:11 -0700310func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
311 return nil
312}
313
Colin Cross66f78822018-05-02 12:58:28 -0700314func (a *AndroidApp) ExportedStaticPackages() android.Paths {
315 return nil
316}
317
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900318func (a *AndroidApp) OutputFile() android.Path {
319 return a.outputFile
320}
321
Colin Cross503c1d02020-01-28 14:00:53 -0800322func (a *AndroidApp) Certificate() Certificate {
323 return a.certificate
324}
325
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700326func (a *AndroidApp) JniCoverageOutputs() android.Paths {
327 return a.jniCoverageOutputs
328}
329
Colin Crossa97c5d32018-03-28 14:58:31 -0700330var _ AndroidLibraryDependency = (*AndroidApp)(nil)
331
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900332type Certificate struct {
Colin Cross503c1d02020-01-28 14:00:53 -0800333 Pem, Key android.Path
334 presigned bool
335}
336
Sasha Smundakc4f0ff12020-05-27 16:36:07 -0700337var PresignedCertificate = Certificate{presigned: true}
Colin Cross503c1d02020-01-28 14:00:53 -0800338
339func (c Certificate) AndroidMkString() string {
340 if c.presigned {
341 return "PRESIGNED"
342 } else {
343 return c.Pem.String()
344 }
Colin Cross30e076a2015-04-13 13:58:27 -0700345}
346
Colin Cross46c9b8b2017-06-22 16:51:17 -0700347func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
348 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700349
Jiyong Park6a927c42020-01-21 02:03:43 +0900350 if String(a.appProperties.Stl) == "c++_shared" && !a.sdkVersion().specified() {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700351 ctx.PropertyErrorf("stl", "sdk_version must be set in order to use c++_shared")
352 }
353
Paul Duffin250e6192019-06-07 10:44:37 +0100354 sdkDep := decodeSdkDep(ctx, sdkContext(a))
355 if sdkDep.hasFrameworkLibs() {
356 a.aapt.deps(ctx, sdkDep)
Colin Cross30e076a2015-04-13 13:58:27 -0700357 }
Colin Crossa4f08812018-10-02 22:03:40 -0700358
Colin Cross1dd9c442020-05-08 11:20:24 -0700359 usesSDK := a.sdkVersion().specified() && a.sdkVersion().kind != sdkCorePlatform
360
361 if usesSDK && Bool(a.appProperties.Jni_uses_sdk_apis) {
362 ctx.PropertyErrorf("jni_uses_sdk_apis",
363 "can only be set for modules that do not set sdk_version")
364 } else if !usesSDK && Bool(a.appProperties.Jni_uses_platform_apis) {
365 ctx.PropertyErrorf("jni_uses_platform_apis",
366 "can only be set for modules that set sdk_version")
367 }
368
Peter Collingbournead84f972019-12-17 16:46:18 -0800369 tag := &jniDependencyTag{}
Colin Crossa4f08812018-10-02 22:03:40 -0700370 for _, jniTarget := range ctx.MultiTargets() {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700371 variation := append(jniTarget.Variations(),
372 blueprint.Variation{Mutator: "link", Variation: "shared"})
Colin Cross01fd7cc2020-02-19 16:54:04 -0800373
374 // If the app builds against an Android SDK use the SDK variant of JNI dependencies
375 // unless jni_uses_platform_apis is set.
Colin Crosseb032962020-05-13 11:05:02 -0700376 // Don't require the SDK variant for apps that are shipped on vendor, etc., as they already
377 // have stable APIs through the VNDK.
378 if (usesSDK && !a.RequiresStableAPIs(ctx) &&
379 !Bool(a.appProperties.Jni_uses_platform_apis)) ||
Colin Cross76583a42020-05-06 17:51:39 -0700380 Bool(a.appProperties.Jni_uses_sdk_apis) {
Colin Cross01fd7cc2020-02-19 16:54:04 -0800381 variation = append(variation, blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
382 }
Colin Crossa4f08812018-10-02 22:03:40 -0700383 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
384 }
Colin Cross50ddcc42019-05-16 12:28:22 -0700385
Paul Duffin250e6192019-06-07 10:44:37 +0100386 a.usesLibrary.deps(ctx, sdkDep.hasFrameworkLibs())
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700387}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700388
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700389func (a *AndroidApp) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800390 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700391 if cert != "" {
392 ctx.AddDependency(ctx.Module(), certificateTag, cert)
393 }
394
395 for _, cert := range a.appProperties.Additional_certificates {
396 cert = android.SrcIsModule(cert)
397 if cert != "" {
398 ctx.AddDependency(ctx.Module(), certificateTag, cert)
399 } else {
400 ctx.PropertyErrorf("additional_certificates",
401 `must be names of android_app_certificate modules in the form ":module"`)
402 }
403 }
Colin Cross30e076a2015-04-13 13:58:27 -0700404}
405
Jeongik Cha538c0d02019-07-11 15:54:27 +0900406func (a *AndroidTestHelperApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
407 a.generateAndroidBuildActions(ctx)
408}
409
Colin Cross46c9b8b2017-06-22 16:51:17 -0700410func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100411 a.checkAppSdkVersions(ctx)
Colin Crossae5caf52018-05-22 11:11:52 -0700412 a.generateAndroidBuildActions(ctx)
413}
414
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100415func (a *AndroidApp) checkAppSdkVersions(ctx android.ModuleContext) {
Artur Satayev2b4b7bb2020-04-28 14:57:42 +0100416 if a.Updatable() {
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100417 if !a.sdkVersion().stable() {
418 ctx.PropertyErrorf("sdk_version", "Updatable apps must use stable SDKs, found %v", a.sdkVersion())
419 }
Artur Satayev11962102020-04-16 13:43:02 +0100420 if String(a.deviceProperties.Min_sdk_version) == "" {
421 ctx.PropertyErrorf("updatable", "updatable apps must set min_sdk_version.")
422 }
Jooyung Hanaf7f91f2020-04-29 14:01:06 +0900423 if minSdkVersion, err := a.minSdkVersion().effectiveVersion(ctx); err == nil {
424 a.checkJniLibsSdkVersion(ctx, minSdkVersion)
425 } else {
426 ctx.PropertyErrorf("min_sdk_version", "%s", err.Error())
427 }
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100428 }
429
430 a.checkPlatformAPI(ctx)
431 a.checkSdkVersions(ctx)
432}
433
Jooyung Hanaf7f91f2020-04-29 14:01:06 +0900434// If an updatable APK sets min_sdk_version, min_sdk_vesion of JNI libs should match with it.
435// This check is enforced for "updatable" APKs (including APK-in-APEX).
436// b/155209650: until min_sdk_version is properly supported, use sdk_version instead.
437// because, sdk_version is overridden by min_sdk_version (if set as smaller)
438// and linkType is checked with dependencies so we can be sure that the whole dependency tree
439// will meet the requirements.
440func (a *AndroidApp) checkJniLibsSdkVersion(ctx android.ModuleContext, minSdkVersion sdkVersion) {
441 // It's enough to check direct JNI deps' sdk_version because all transitive deps from JNI deps are checked in cc.checkLinkType()
442 ctx.VisitDirectDeps(func(m android.Module) {
443 if !IsJniDepTag(ctx.OtherModuleDependencyTag(m)) {
444 return
445 }
446 dep, _ := m.(*cc.Module)
Jooyung Han9d2c0f72020-05-20 17:12:13 +0900447 // The domain of cc.sdk_version is "current" and <number>
448 // We can rely on sdkSpec to convert it to <number> so that "current" is handled
449 // properly regardless of sdk finalization.
450 jniSdkVersion, err := sdkSpecFrom(dep.SdkVersion()).effectiveVersion(ctx)
451 if err != nil || minSdkVersion < jniSdkVersion {
Jooyung Hanaf7f91f2020-04-29 14:01:06 +0900452 ctx.OtherModuleErrorf(dep, "sdk_version(%v) is higher than min_sdk_version(%v) of the containing android_app(%v)",
453 dep.SdkVersion(), minSdkVersion, ctx.ModuleName())
454 return
455 }
456
457 })
458}
459
Sasha Smundak6ad77252019-05-01 13:16:22 -0700460// Returns true if the native libraries should be stored in the APK uncompressed and the
Colin Crosse4246ab2019-02-05 21:55:21 -0800461// extractNativeLibs application flag should be set to false in the manifest.
Sasha Smundak6ad77252019-05-01 13:16:22 -0700462func (a *AndroidApp) useEmbeddedNativeLibs(ctx android.ModuleContext) bool {
Jiyong Park6a927c42020-01-21 02:03:43 +0900463 minSdkVersion, err := a.minSdkVersion().effectiveVersion(ctx)
Colin Crosse4246ab2019-02-05 21:55:21 -0800464 if err != nil {
465 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
466 }
467
Jiyong Park52cd06f2019-11-11 10:14:32 +0900468 return (minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)) ||
469 !a.IsForPlatform()
Colin Crosse4246ab2019-02-05 21:55:21 -0800470}
471
Colin Cross43f08db2018-11-12 10:13:39 -0800472// Returns whether this module should have the dex file stored uncompressed in the APK.
473func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800474 if Bool(a.appProperties.Use_embedded_dex) {
475 return true
476 }
477
Colin Cross53a87f52019-06-25 13:35:30 -0700478 // Uncompress dex in APKs of privileged apps (even for unbundled builds, they may
479 // be preinstalled as prebuilts).
Jiyong Parkf7487312019-10-17 12:54:30 +0900480 if ctx.Config().UncompressPrivAppDex() && a.Privileged() {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000481 return true
482 }
483
Colin Cross53a87f52019-06-25 13:35:30 -0700484 if ctx.Config().UnbundledBuild() {
485 return false
486 }
487
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700488 return shouldUncompressDex(ctx, &a.dexpreopter)
Colin Cross5a0dcd52018-10-05 14:20:06 -0700489}
490
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700491func (a *AndroidApp) shouldEmbedJnis(ctx android.BaseModuleContext) bool {
492 return ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) ||
Jiyong Park52cd06f2019-11-11 10:14:32 +0900493 !a.IsForPlatform() || a.appProperties.AlwaysPackageNativeLibs
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700494}
495
Jiyong Parkaf8998c2020-02-28 16:51:07 +0900496func (a *AndroidApp) OverriddenManifestPackageName() string {
497 return a.overriddenManifestPackageName
498}
499
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800500func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
David Brazdild25060a2019-02-18 18:24:16 +0000501 a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis)
502
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700503 // Ask manifest_fixer to add or update the application element indicating this app has no code.
504 a.aapt.hasNoCode = !a.hasCode(ctx)
505
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800506 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800507
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800508 // 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 -0800509 hasProduct := android.PrefixInList(a.aaptProperties.Aaptflags, "--product")
Colin Crosse78dcd32018-04-19 15:25:19 -0700510 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800511 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700512 }
513
Dan Willemsen72be5902018-10-24 20:24:57 -0700514 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
515 // Product AAPT config
516 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800517 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700518 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700519
Dan Willemsen72be5902018-10-24 20:24:57 -0700520 // Product AAPT preferred config
521 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800522 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700523 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700524 }
525
Jiyong Park7f67f482019-01-05 12:57:48 +0900526 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700527 if overridden || a.overridableAppProperties.Package_name != nil {
528 // The product override variable has a priority over the package_name property.
529 if !overridden {
530 manifestPackageName = *a.overridableAppProperties.Package_name
531 }
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800532 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
Jiyong Parkaf8998c2020-02-28 16:51:07 +0900533 a.overriddenManifestPackageName = manifestPackageName
Jiyong Park7f67f482019-01-05 12:57:48 +0900534 }
535
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800536 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
537
Colin Crosse560c4a2019-03-19 16:03:11 -0700538 a.aapt.splitNames = a.appProperties.Package_splits
Colin Cross50ddcc42019-05-16 12:28:22 -0700539 a.aapt.sdkLibraries = a.exportedSdkLibs
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800540 a.aapt.LoggingParent = String(a.overridableAppProperties.Logging_parent)
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800541 a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700542
Colin Cross46c9b8b2017-06-22 16:51:17 -0700543 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700544 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800545}
Colin Cross30e076a2015-04-13 13:58:27 -0700546
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800547func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700548 var staticLibProguardFlagFiles android.Paths
549 ctx.VisitDirectDeps(func(m android.Module) {
550 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
551 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
552 }
553 })
554
555 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
556
557 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
558 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800559}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800560
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800561func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross43f08db2018-11-12 10:13:39 -0800562
563 var installDir string
564 if ctx.ModuleName() == "framework-res" {
565 // framework-res.apk is installed as system/framework/framework-res.apk
566 installDir = "framework"
Jiyong Parkf7487312019-10-17 12:54:30 +0900567 } else if a.Privileged() {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800568 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800569 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800570 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800571 }
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800572 a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
David Srbecky98c71222020-05-20 22:20:28 +0100573 if a.deviceProperties.Uncompress_dex == nil {
574 // If the value was not force-set by the user, use reasonable default based on the module.
575 a.deviceProperties.Uncompress_dex = proptools.BoolPtr(a.shouldUncompressDex(ctx))
576 }
577 a.dexpreopter.uncompressedDex = *a.deviceProperties.Uncompress_dex
Colin Cross50ddcc42019-05-16 12:28:22 -0700578 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
579 a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
580 a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
581 a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
582 a.dexpreopter.manifestFile = a.mergedManifestFile
583
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800584 if ctx.ModuleName() != "framework-res" {
585 a.Module.compile(ctx, a.aaptSrcJar)
586 }
Colin Cross30e076a2015-04-13 13:58:27 -0700587
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800588 return a.maybeStrippedDexJarFile
589}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800590
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800591func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700592 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700593 if len(jniLibs) > 0 {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700594 if a.shouldEmbedJnis(ctx) {
Colin Crossa4f08812018-10-02 22:03:40 -0700595 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Sasha Smundak6ad77252019-05-01 13:16:22 -0700596 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.useEmbeddedNativeLibs(ctx))
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700597 for _, jni := range jniLibs {
598 if jni.coverageFile.Valid() {
Jaewoong Junge62e5942020-04-07 13:07:55 -0700599 // Only collect coverage for the first target arch if this is a multilib target.
600 // TODO(jungjw): Ideally, we want to collect both reports, but that would cause coverage
601 // data file path collisions since the current coverage file path format doesn't contain
602 // arch-related strings. This is fine for now though; the code coverage team doesn't use
603 // multi-arch targets such as test_suite_* for coverage collections yet.
604 //
605 // Work with the team to come up with a new format that handles multilib modules properly
606 // and change this.
607 if len(ctx.Config().Targets[android.Android]) == 1 ||
608 ctx.Config().Targets[android.Android][0].Arch.ArchType == jni.target.Arch.ArchType {
609 a.jniCoverageOutputs = append(a.jniCoverageOutputs, jni.coverageFile.Path())
610 }
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700611 }
612 }
Colin Crossa4f08812018-10-02 22:03:40 -0700613 } else {
614 a.installJniLibs = jniLibs
615 }
616 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800617 return jniJarFile
618}
Colin Crossa4f08812018-10-02 22:03:40 -0700619
Jaewoong Jung0949f312019-09-11 10:25:18 -0700620func (a *AndroidApp) noticeBuildActions(ctx android.ModuleContext) {
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700621 // Collect NOTICE files from all dependencies.
622 seenModules := make(map[android.Module]bool)
623 noticePathSet := make(map[android.Path]bool)
624
625 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
626 // Have we already seen this?
627 if _, ok := seenModules[child]; ok {
628 return false
629 }
630 seenModules[child] = true
631
632 // Skip host modules.
633 if child.Target().Os.Class == android.Host || child.Target().Os.Class == android.HostCross {
634 return false
635 }
636
637 path := child.(android.Module).NoticeFile()
638 if path.Valid() {
639 noticePathSet[path.Path()] = true
640 }
641 return true
642 })
643
644 // If the app has one, add it too.
645 if a.NoticeFile().Valid() {
646 noticePathSet[a.NoticeFile().Path()] = true
647 }
648
649 if len(noticePathSet) == 0 {
Jaewoong Jung98772792019-07-01 17:15:13 -0700650 return
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700651 }
652 var noticePaths []android.Path
653 for path := range noticePathSet {
654 noticePaths = append(noticePaths, path)
655 }
656 sort.Slice(noticePaths, func(i, j int) bool {
657 return noticePaths[i].String() < noticePaths[j].String()
658 })
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700659
Jaewoong Jung0949f312019-09-11 10:25:18 -0700660 a.noticeOutputs = android.BuildNoticeOutput(ctx, a.installDir, a.installApkName+".apk", noticePaths)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700661}
662
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700663// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
664// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
665func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate {
666 if android.SrcIsModule(certPropValue) == "" {
667 var mainCert Certificate
668 if certPropValue != "" {
669 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
670 mainCert = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -0800671 Pem: defaultDir.Join(ctx, certPropValue+".x509.pem"),
672 Key: defaultDir.Join(ctx, certPropValue+".pk8"),
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700673 }
674 } else {
675 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Colin Cross503c1d02020-01-28 14:00:53 -0800676 mainCert = Certificate{
677 Pem: pem,
678 Key: key,
679 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700680 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700681 certificates = append([]Certificate{mainCert}, certificates...)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700682 }
683
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700684 if !m.Platform() {
685 certPath := certificates[0].Pem.String()
Jeongik Chac9464142019-01-07 12:07:27 +0900686 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
687 if strings.HasPrefix(certPath, systemCertPath) {
688 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
Colin Cross95f7b342020-06-11 11:32:11 -0700689 allowed := ctx.Config().EnforceSystemCertificateAllowList()
Jeongik Chac9464142019-01-07 12:07:27 +0900690
Colin Cross95f7b342020-06-11 11:32:11 -0700691 if enforceSystemCert && !inList(m.Name(), allowed) {
Jeongik Chac9464142019-01-07 12:07:27 +0900692 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
693 }
694 }
695 }
696
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700697 return certificates
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800698}
699
Jooyung Han65cd0f02020-03-23 20:21:11 +0900700func (a *AndroidApp) InstallApkName() string {
701 return a.installApkName
702}
703
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800704func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross50ddcc42019-05-16 12:28:22 -0700705 var apkDeps android.Paths
706
Jeongik Cha538c0d02019-07-11 15:54:27 +0900707 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
708 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
709
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800710 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800711 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800712
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700713 if ctx.ModuleName() == "framework-res" {
714 // framework-res.apk is installed as system/framework/framework-res.apk
Jaewoong Jung0949f312019-09-11 10:25:18 -0700715 a.installDir = android.PathForModuleInstall(ctx, "framework")
Jiyong Parkf7487312019-10-17 12:54:30 +0900716 } else if a.Privileged() {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700717 a.installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
718 } else if ctx.InstallInTestcases() {
Jaewoong Jung326a9412019-11-21 10:41:00 -0800719 a.installDir = android.PathForModuleInstall(ctx, a.installApkName, ctx.DeviceConfig().DeviceArch())
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700720 } else {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700721 a.installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700722 }
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700723 a.onDeviceDir = android.InstallPathToOnDevicePath(ctx, a.installDir)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700724
Jaewoong Jung0949f312019-09-11 10:25:18 -0700725 a.noticeBuildActions(ctx)
Jaewoong Jung98772792019-07-01 17:15:13 -0700726 if Bool(a.appProperties.Embed_notices) || ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") {
727 a.aapt.noticeFile = a.noticeOutputs.HtmlGzOutput
728 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700729
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800730 // Process all building blocks, from AAPT to certificates.
731 a.aaptBuildActions(ctx)
732
Colin Cross50ddcc42019-05-16 12:28:22 -0700733 if a.usesLibrary.enforceUsesLibraries() {
734 manifestCheckFile := a.usesLibrary.verifyUsesLibrariesManifest(ctx, a.mergedManifestFile)
735 apkDeps = append(apkDeps, manifestCheckFile)
736 }
737
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800738 a.proguardBuildActions(ctx)
739
740 dexJarFile := a.dexBuildActions(ctx)
741
Colin Crosseb032962020-05-13 11:05:02 -0700742 jniLibs, certificateDeps := collectAppDeps(ctx, a, a.shouldEmbedJnis(ctx), !Bool(a.appProperties.Jni_uses_platform_apis))
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800743 jniJarFile := a.jniBuildActions(jniLibs, ctx)
744
745 if ctx.Failed() {
746 return
747 }
748
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700749 certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx)
750 a.certificate = certificates[0]
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800751
752 // Build a final signed app package.
Jaewoong Jung5a498812019-11-07 14:14:38 -0800753 packageFile := android.PathForModuleOut(ctx, a.installApkName+".apk")
Songchun Fan688de9a2020-03-24 20:32:24 -0700754 v4SigningRequested := Bool(a.Module.deviceProperties.V4_signature)
755 var v4SignatureFile android.WritablePath = nil
756 if v4SigningRequested {
757 v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+".apk.idsig")
758 }
Liz Kammer70dd74d2020-05-07 13:24:05 -0700759 var lineageFile android.Path
760 if lineage := String(a.overridableAppProperties.Lineage); lineage != "" {
761 lineageFile = android.PathForModuleSrc(ctx, lineage)
762 }
763 CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates, apkDeps, v4SignatureFile, lineageFile)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800764 a.outputFile = packageFile
Songchun Fan688de9a2020-03-24 20:32:24 -0700765 if v4SigningRequested {
766 a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
767 }
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800768
Colin Crosse560c4a2019-03-19 16:03:11 -0700769 for _, split := range a.aapt.splits {
770 // Sign the split APKs
Jaewoong Jung5a498812019-11-07 14:14:38 -0800771 packageFile := android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk")
Songchun Fan688de9a2020-03-24 20:32:24 -0700772 if v4SigningRequested {
773 v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk.idsig")
774 }
Liz Kammer70dd74d2020-05-07 13:24:05 -0700775 CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates, apkDeps, v4SignatureFile, lineageFile)
Colin Crosse560c4a2019-03-19 16:03:11 -0700776 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
Songchun Fan688de9a2020-03-24 20:32:24 -0700777 if v4SigningRequested {
778 a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
779 }
Colin Crosse560c4a2019-03-19 16:03:11 -0700780 }
781
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800782 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700783 bundleFile := android.PathForModuleOut(ctx, "base.zip")
784 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
785 a.bundleFile = bundleFile
786
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800787 // Install the app package.
Jiyong Park8ba50f92019-11-13 15:01:01 +0900788 if (Bool(a.Module.properties.Installable) || ctx.Host()) && a.IsForPlatform() {
789 ctx.InstallFile(a.installDir, a.outputFile.Base(), a.outputFile)
790 for _, extra := range a.extraOutputFiles {
791 ctx.InstallFile(a.installDir, extra.Base(), extra)
792 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800793 }
Artur Satayevd9b503a2020-04-27 19:05:28 +0100794
795 a.buildAppDependencyInfo(ctx)
Colin Cross30e076a2015-04-13 13:58:27 -0700796}
797
Colin Crosseb032962020-05-13 11:05:02 -0700798type appDepsInterface interface {
799 sdkVersion() sdkSpec
800 minSdkVersion() sdkSpec
801 RequiresStableAPIs(ctx android.BaseModuleContext) bool
802}
803
804func collectAppDeps(ctx android.ModuleContext, app appDepsInterface,
805 shouldCollectRecursiveNativeDeps bool,
Colin Cross1c93c292020-02-15 10:38:00 -0800806 checkNativeSdkVersion bool) ([]jniLib, []Certificate) {
Colin Crosseb032962020-05-13 11:05:02 -0700807
Colin Crossa4f08812018-10-02 22:03:40 -0700808 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900809 var certificates []Certificate
Peter Collingbournead84f972019-12-17 16:46:18 -0800810 seenModulePaths := make(map[string]bool)
Colin Crossa4f08812018-10-02 22:03:40 -0700811
Colin Crosseb032962020-05-13 11:05:02 -0700812 if checkNativeSdkVersion {
813 checkNativeSdkVersion = app.sdkVersion().specified() &&
814 app.sdkVersion().kind != sdkCorePlatform && !app.RequiresStableAPIs(ctx)
815 }
816
Peter Collingbournead84f972019-12-17 16:46:18 -0800817 ctx.WalkDeps(func(module android.Module, parent android.Module) bool {
Colin Crossa4f08812018-10-02 22:03:40 -0700818 otherName := ctx.OtherModuleName(module)
819 tag := ctx.OtherModuleDependencyTag(module)
820
Peter Collingbournead84f972019-12-17 16:46:18 -0800821 if IsJniDepTag(tag) || tag == cc.SharedDepTag {
Colin Crossa4f08812018-10-02 22:03:40 -0700822 if dep, ok := module.(*cc.Module); ok {
Peter Collingbournead84f972019-12-17 16:46:18 -0800823 if dep.IsNdk() || dep.IsStubs() {
824 return false
825 }
826
Colin Crossa4f08812018-10-02 22:03:40 -0700827 lib := dep.OutputFile()
Peter Collingbournead84f972019-12-17 16:46:18 -0800828 path := lib.Path()
829 if seenModulePaths[path.String()] {
830 return false
831 }
832 seenModulePaths[path.String()] = true
833
Colin Crosseb032962020-05-13 11:05:02 -0700834 if checkNativeSdkVersion && dep.SdkVersion() == "" {
835 ctx.PropertyErrorf("jni_libs", "JNI dependency %q uses platform APIs, but this module does not",
836 otherName)
Colin Cross1c93c292020-02-15 10:38:00 -0800837 }
838
Colin Crossa4f08812018-10-02 22:03:40 -0700839 if lib.Valid() {
840 jniLibs = append(jniLibs, jniLib{
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700841 name: ctx.OtherModuleName(module),
842 path: path,
843 target: module.Target(),
844 coverageFile: dep.CoverageOutputFile(),
Colin Crossa4f08812018-10-02 22:03:40 -0700845 })
846 } else {
847 ctx.ModuleErrorf("dependency %q missing output file", otherName)
848 }
849 } else {
850 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
Colin Crossa4f08812018-10-02 22:03:40 -0700851 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800852
853 return shouldCollectRecursiveNativeDeps
854 }
855
856 if tag == certificateTag {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700857 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900858 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700859 } else {
860 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
861 }
Colin Crossa4f08812018-10-02 22:03:40 -0700862 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800863
864 return false
Colin Crossa4f08812018-10-02 22:03:40 -0700865 })
866
Colin Crossbd01e2a2018-10-04 15:21:03 -0700867 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700868}
869
Artur Satayevd9b503a2020-04-27 19:05:28 +0100870func (a *AndroidApp) walkPayloadDeps(ctx android.ModuleContext,
871 do func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool)) {
872
873 ctx.WalkDeps(func(child, parent android.Module) bool {
874 isExternal := !a.DepIsInSameApex(ctx, child)
875 if am, ok := child.(android.ApexModule); ok {
876 do(ctx, parent, am, isExternal)
877 }
878 return !isExternal
879 })
880}
881
882func (a *AndroidApp) buildAppDependencyInfo(ctx android.ModuleContext) {
883 if ctx.Host() {
884 return
885 }
886
887 depsInfo := android.DepNameToDepInfoMap{}
888 a.walkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) {
889 depName := to.Name()
890 if info, exist := depsInfo[depName]; exist {
891 info.From = append(info.From, from.Name())
892 info.IsExternal = info.IsExternal && externalDep
893 depsInfo[depName] = info
894 } else {
895 toMinSdkVersion := "(no version)"
896 if m, ok := to.(interface{ MinSdkVersion() string }); ok {
897 if v := m.MinSdkVersion(); v != "" {
898 toMinSdkVersion = v
899 }
900 }
901 depsInfo[depName] = android.ApexModuleDepInfo{
902 To: depName,
903 From: []string{from.Name()},
904 IsExternal: externalDep,
905 MinSdkVersion: toMinSdkVersion,
906 }
907 }
908 })
909
910 a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, a.MinSdkVersion(), depsInfo)
911}
912
Artur Satayev2b4b7bb2020-04-28 14:57:42 +0100913func (a *AndroidApp) Updatable() bool {
914 return Bool(a.appProperties.Updatable) || a.ApexModuleBase.Updatable()
915}
916
Colin Cross0ea8ba82019-06-06 14:33:29 -0700917func (a *AndroidApp) getCertString(ctx android.BaseModuleContext) string {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800918 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
919 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000920 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800921 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800922 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800923}
924
Jiyong Park0f80c182020-01-31 02:49:53 +0900925func (a *AndroidApp) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
926 if IsJniDepTag(ctx.OtherModuleDependencyTag(dep)) {
927 return true
928 }
929 return a.Library.DepIsInSameApex(ctx, dep)
930}
931
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900932// For OutputFileProducer interface
933func (a *AndroidApp) OutputFiles(tag string) (android.Paths, error) {
934 switch tag {
935 case ".aapt.srcjar":
936 return []android.Path{a.aaptSrcJar}, nil
937 }
938 return a.Library.OutputFiles(tag)
939}
940
Jiyong Parkf7487312019-10-17 12:54:30 +0900941func (a *AndroidApp) Privileged() bool {
942 return Bool(a.appProperties.Privileged)
943}
944
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700945func (a *AndroidApp) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
946 return ctx.Device() && (ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled())
947}
948
949func (a *AndroidApp) PreventInstall() {
950 a.appProperties.PreventInstall = true
951}
952
953func (a *AndroidApp) HideFromMake() {
954 a.appProperties.HideFromMake = true
955}
956
957func (a *AndroidApp) MarkAsCoverageVariant(coverage bool) {
958 a.appProperties.IsCoverageVariant = coverage
959}
960
961var _ cc.Coverage = (*AndroidApp)(nil)
962
Colin Cross1b16b0e2019-02-12 14:41:32 -0800963// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700964func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700965 module := &AndroidApp{}
966
Sasha Smundak2057f822019-04-16 17:16:58 -0700967 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross66dbc0b2017-12-28 12:23:20 -0800968 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
969
Colin Crossae5caf52018-05-22 11:11:52 -0700970 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700971 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700972
Colin Cross1c14b4e2020-06-15 16:09:53 -0700973 module.addHostAndDeviceProperties()
Colin Cross36242852017-06-23 15:06:31 -0700974 module.AddProperties(
Colin Crossa97c5d32018-03-28 14:58:31 -0700975 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800976 &module.appProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700977 &module.overridableAppProperties,
978 &module.usesLibrary.usesLibraryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700979
Colin Crossa9d8bee2018-10-02 13:59:46 -0700980 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
981 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
982 })
983
Colin Crossa4f08812018-10-02 22:03:40 -0700984 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
985 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800986 android.InitOverridableModule(module, &module.appProperties.Overrides)
Jiyong Park52cd06f2019-11-11 10:14:32 +0900987 android.InitApexModule(module)
Colin Crossa4f08812018-10-02 22:03:40 -0700988
Colin Cross36242852017-06-23 15:06:31 -0700989 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700990}
Colin Crossae5caf52018-05-22 11:11:52 -0700991
992type appTestProperties struct {
993 Instrumentation_for *string
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700994
995 // if specified, the instrumentation target package name in the manifest is overwritten by it.
996 Instrumentation_target_package *string
Colin Crossae5caf52018-05-22 11:11:52 -0700997}
998
999type AndroidTest struct {
1000 AndroidApp
1001
1002 appTestProperties appTestProperties
1003
1004 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001005
1006 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001007 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -07001008}
1009
Jaewoong Jung0949f312019-09-11 10:25:18 -07001010func (a *AndroidTest) InstallInTestcases() bool {
1011 return true
1012}
1013
Colin Crossae5caf52018-05-22 11:11:52 -07001014func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
easoncyleeba606252020-04-30 14:57:06 +08001015 var configs []tradefed.Config
Jaewoong Jung26dedd32019-06-06 08:45:58 -07001016 if a.appTestProperties.Instrumentation_target_package != nil {
1017 a.additionalAaptFlags = append(a.additionalAaptFlags,
1018 "--rename-instrumentation-target-package "+*a.appTestProperties.Instrumentation_target_package)
1019 } else if a.appTestProperties.Instrumentation_for != nil {
1020 // Check if the instrumentation target package is overridden.
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001021 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
1022 if overridden {
1023 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
1024 }
1025 }
Colin Crossae5caf52018-05-22 11:11:52 -07001026 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001027
easoncyleeba606252020-04-30 14:57:06 +08001028 for _, module := range a.testProperties.Test_mainline_modules {
1029 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
1030 }
1031
Jaewoong Jung39982342020-01-14 10:27:18 -08001032 testConfig := tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config,
easoncyleeba606252020-04-30 14:57:06 +08001033 a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites, a.testProperties.Auto_gen_config, configs)
Jaewoong Jung39982342020-01-14 10:27:18 -08001034 a.testConfig = a.FixTestConfig(ctx, testConfig)
Colin Cross8a497952019-03-05 22:25:09 -08001035 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001036}
1037
Jaewoong Jung39982342020-01-14 10:27:18 -08001038func (a *AndroidTest) FixTestConfig(ctx android.ModuleContext, testConfig android.Path) android.Path {
1039 if testConfig == nil {
1040 return nil
1041 }
1042
1043 fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", "AndroidTest.xml")
1044 rule := android.NewRuleBuilder()
1045 command := rule.Command().BuiltTool(ctx, "test_config_fixer").Input(testConfig).Output(fixedConfig)
1046 fixNeeded := false
1047
1048 if ctx.ModuleName() != a.installApkName {
1049 fixNeeded = true
1050 command.FlagWithArg("--test-file-name ", a.installApkName+".apk")
1051 }
1052
1053 if a.overridableAppProperties.Package_name != nil {
1054 fixNeeded = true
1055 command.FlagWithInput("--manifest ", a.manifestPath).
1056 FlagWithArg("--package-name ", *a.overridableAppProperties.Package_name)
1057 }
1058
1059 if fixNeeded {
1060 rule.Build(pctx, ctx, "fix_test_config", "fix test config")
1061 return fixedConfig
1062 }
1063 return testConfig
1064}
1065
Colin Cross303e21f2018-08-07 16:49:25 -07001066func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -07001067 a.AndroidApp.DepsMutator(ctx)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07001068}
1069
1070func (a *AndroidTest) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
1071 a.AndroidApp.OverridablePropertiesDepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -07001072 if a.appTestProperties.Instrumentation_for != nil {
1073 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
1074 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
1075 // use instrumentationForTag instead of libTag.
1076 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
1077 }
Colin Crossae5caf52018-05-22 11:11:52 -07001078}
1079
Colin Cross1b16b0e2019-02-12 14:41:32 -08001080// android_test compiles test sources and Android resources into an Android application package `.apk` file and
1081// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -07001082func AndroidTestFactory() android.Module {
1083 module := &AndroidTest{}
1084
Sasha Smundak2057f822019-04-16 17:16:58 -07001085 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross5067db92018-09-17 16:46:35 -07001086
1087 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001088 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -08001089 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -07001090 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -08001091 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -07001092
Colin Cross1c14b4e2020-06-15 16:09:53 -07001093 module.addHostAndDeviceProperties()
Colin Crossae5caf52018-05-22 11:11:52 -07001094 module.AddProperties(
Colin Crossae5caf52018-05-22 11:11:52 -07001095 &module.aaptProperties,
1096 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -07001097 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -08001098 &module.overridableAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -07001099 &module.usesLibrary.usesLibraryProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -07001100 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -07001101
Colin Crossa4f08812018-10-02 22:03:40 -07001102 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1103 android.InitDefaultableModule(module)
Jaewoong Jung26dedd32019-06-06 08:45:58 -07001104 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossae5caf52018-05-22 11:11:52 -07001105 return module
1106}
Colin Crossbd01e2a2018-10-04 15:21:03 -07001107
Colin Cross252fc6f2018-10-04 15:22:03 -07001108type appTestHelperAppProperties struct {
1109 // list of compatibility suites (for example "cts", "vts") that the module should be
1110 // installed into.
1111 Test_suites []string `android:"arch_variant"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001112
1113 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1114 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1115 // explicitly.
1116 Auto_gen_config *bool
Colin Cross252fc6f2018-10-04 15:22:03 -07001117}
1118
1119type AndroidTestHelperApp struct {
1120 AndroidApp
1121
1122 appTestHelperAppProperties appTestHelperAppProperties
1123}
1124
Jaewoong Jung326a9412019-11-21 10:41:00 -08001125func (a *AndroidTestHelperApp) InstallInTestcases() bool {
1126 return true
1127}
1128
Colin Cross1b16b0e2019-02-12 14:41:32 -08001129// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
1130// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
1131// test.
Colin Cross252fc6f2018-10-04 15:22:03 -07001132func AndroidTestHelperAppFactory() android.Module {
1133 module := &AndroidTestHelperApp{}
1134
Sasha Smundak2057f822019-04-16 17:16:58 -07001135 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross252fc6f2018-10-04 15:22:03 -07001136
1137 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -08001138 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -07001139 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -08001140 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -07001141
Colin Cross1c14b4e2020-06-15 16:09:53 -07001142 module.addHostAndDeviceProperties()
Colin Cross252fc6f2018-10-04 15:22:03 -07001143 module.AddProperties(
Colin Cross252fc6f2018-10-04 15:22:03 -07001144 &module.aaptProperties,
1145 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -08001146 &module.appTestHelperAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -07001147 &module.overridableAppProperties,
1148 &module.usesLibrary.usesLibraryProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -07001149
1150 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1151 android.InitDefaultableModule(module)
Anton Hansson3d2b6b42020-01-10 15:06:01 +00001152 android.InitApexModule(module)
Colin Cross252fc6f2018-10-04 15:22:03 -07001153 return module
1154}
1155
Colin Crossbd01e2a2018-10-04 15:21:03 -07001156type AndroidAppCertificate struct {
1157 android.ModuleBase
1158 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001159 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -07001160}
1161
1162type AndroidAppCertificateProperties struct {
1163 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
1164 Certificate *string
1165}
1166
Colin Cross1b16b0e2019-02-12 14:41:32 -08001167// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
1168// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -07001169func AndroidAppCertificateFactory() android.Module {
1170 module := &AndroidAppCertificate{}
1171 module.AddProperties(&module.properties)
1172 android.InitAndroidModule(module)
1173 return module
1174}
1175
Colin Crossbd01e2a2018-10-04 15:21:03 -07001176func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1177 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001178 c.Certificate = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -08001179 Pem: android.PathForModuleSrc(ctx, cert+".x509.pem"),
1180 Key: android.PathForModuleSrc(ctx, cert+".pk8"),
Colin Crossbd01e2a2018-10-04 15:21:03 -07001181 }
1182}
Jaewoong Jung525443a2019-02-28 15:35:54 -08001183
1184type OverrideAndroidApp struct {
1185 android.ModuleBase
1186 android.OverrideModuleBase
1187}
1188
Sasha Smundakc4f0ff12020-05-27 16:36:07 -07001189func (i *OverrideAndroidApp) GenerateAndroidBuildActions(_ android.ModuleContext) {
Jaewoong Jung525443a2019-02-28 15:35:54 -08001190 // All the overrides happen in the base module.
1191 // TODO(jungjw): Check the base module type.
1192}
1193
1194// override_android_app is used to create an android_app module based on another android_app by overriding
1195// some of its properties.
1196func OverrideAndroidAppModuleFactory() android.Module {
1197 m := &OverrideAndroidApp{}
1198 m.AddProperties(&overridableAppProperties{})
1199
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001200 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung525443a2019-02-28 15:35:54 -08001201 android.InitOverrideModule(m)
1202 return m
1203}
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001204
Jaewoong Jung26dedd32019-06-06 08:45:58 -07001205type OverrideAndroidTest struct {
1206 android.ModuleBase
1207 android.OverrideModuleBase
1208}
1209
Sasha Smundakc4f0ff12020-05-27 16:36:07 -07001210func (i *OverrideAndroidTest) GenerateAndroidBuildActions(_ android.ModuleContext) {
Jaewoong Jung26dedd32019-06-06 08:45:58 -07001211 // All the overrides happen in the base module.
1212 // TODO(jungjw): Check the base module type.
1213}
1214
1215// override_android_test is used to create an android_app module based on another android_test by overriding
1216// some of its properties.
1217func OverrideAndroidTestModuleFactory() android.Module {
1218 m := &OverrideAndroidTest{}
1219 m.AddProperties(&overridableAppProperties{})
1220 m.AddProperties(&appTestProperties{})
1221
1222 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
1223 android.InitOverrideModule(m)
1224 return m
1225}
1226
Roshan Piusb8307962020-04-27 09:42:27 -07001227type OverrideRuntimeResourceOverlay struct {
1228 android.ModuleBase
1229 android.OverrideModuleBase
1230}
1231
Sasha Smundakc4f0ff12020-05-27 16:36:07 -07001232func (i *OverrideRuntimeResourceOverlay) GenerateAndroidBuildActions(_ android.ModuleContext) {
Roshan Piusb8307962020-04-27 09:42:27 -07001233 // All the overrides happen in the base module.
1234 // TODO(jungjw): Check the base module type.
1235}
1236
1237// override_runtime_resource_overlay is used to create a module based on another
1238// runtime_resource_overlay module by overriding some of its properties.
1239func OverrideRuntimeResourceOverlayModuleFactory() android.Module {
1240 m := &OverrideRuntimeResourceOverlay{}
1241 m.AddProperties(&OverridableRuntimeResourceOverlayProperties{})
1242
1243 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
1244 android.InitOverrideModule(m)
1245 return m
1246}
1247
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001248type AndroidAppImport struct {
1249 android.ModuleBase
1250 android.DefaultableModuleBase
1251 prebuilt android.Prebuilt
1252
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001253 properties AndroidAppImportProperties
1254 dpiVariants interface{}
1255 archVariants interface{}
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001256
1257 outputFile android.Path
Colin Cross503c1d02020-01-28 14:00:53 -08001258 certificate Certificate
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001259
1260 dexpreopter
Colin Cross50ddcc42019-05-16 12:28:22 -07001261
1262 usesLibrary usesLibrary
Jaewoong Jung8aae22e2019-07-17 10:21:49 -07001263
Liz Kammer7e20dda2020-05-20 14:36:30 -07001264 preprocessed bool
1265
Colin Cross70dda7e2019-10-01 22:05:35 -07001266 installPath android.InstallPath
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001267}
1268
1269type AndroidAppImportProperties struct {
1270 // A prebuilt apk to import
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001271 Apk *string
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001272
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001273 // The name of a certificate in the default certificate directory or an android_app_certificate
1274 // module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001275 Certificate *string
1276
1277 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
1278 // be set for presigned modules.
1279 Presigned *bool
1280
Liz Kammer2bc57f62020-05-13 15:49:21 -07001281 // Name of the signing certificate lineage file.
1282 Lineage *string
1283
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001284 // Sign with the default system dev certificate. Must be used judiciously. Most imported apps
1285 // need to either specify a specific certificate or be presigned.
1286 Default_dev_cert *bool
1287
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001288 // Specifies that this app should be installed to the priv-app directory,
1289 // where the system will grant it additional privileges not available to
1290 // normal apps.
1291 Privileged *bool
1292
1293 // Names of modules to be overridden. Listed modules can only be other binaries
1294 // (in Make or Soong).
1295 // This does not completely prevent installation of the overridden binaries, but if both
1296 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1297 // from PRODUCT_PACKAGES.
1298 Overrides []string
Jaewoong Jung8aae22e2019-07-17 10:21:49 -07001299
1300 // Optional name for the installed app. If unspecified, it is derived from the module name.
1301 Filename *string
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001302}
1303
Martin Stjernholm6d415272020-01-31 17:10:36 +00001304func (a *AndroidAppImport) IsInstallable() bool {
1305 return true
1306}
1307
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001308// Updates properties with variant-specific values.
1309func (a *AndroidAppImport) processVariants(ctx android.LoadHookContext) {
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001310 config := ctx.Config()
1311
1312 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants")
1313 // Try DPI variant matches in the reverse-priority order so that the highest priority match
1314 // overwrites everything else.
1315 // TODO(jungjw): Can we optimize this by making it priority order?
1316 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001317 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001318 }
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001319 if config.ProductAAPTPreferredConfig() != "" {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001320 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001321 }
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001322
1323 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName("Arch")
1324 archType := ctx.Config().Targets[android.Android][0].Arch.ArchType
1325 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001326}
1327
Colin Cross1184b642019-12-30 18:43:07 -08001328func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001329 dst interface{}, variantGroup reflect.Value, variant string) {
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001330 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
1331 if !src.IsValid() {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001332 return
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001333 }
1334
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001335 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
1336 if err != nil {
1337 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1338 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1339 } else {
1340 panic(err)
1341 }
1342 }
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001343}
1344
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001345func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
1346 cert := android.SrcIsModule(String(a.properties.Certificate))
1347 if cert != "" {
1348 ctx.AddDependency(ctx.Module(), certificateTag, cert)
1349 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001350
Paul Duffin250e6192019-06-07 10:44:37 +01001351 a.usesLibrary.deps(ctx, true)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001352}
1353
1354func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
1355 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001356 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
1357 // with them may invalidate pre-existing signature data.
Liz Kammer7e20dda2020-05-20 14:36:30 -07001358 if ctx.InstallInTestcases() && (Bool(a.properties.Presigned) || a.preprocessed) {
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001359 ctx.Build(pctx, android.BuildParams{
1360 Rule: android.Cp,
1361 Output: outputPath,
1362 Input: inputPath,
1363 })
1364 return
1365 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001366 rule := android.NewRuleBuilder()
1367 rule.Command().
1368 Textf(`if (zipinfo %s 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
Colin Crossee94d6a2019-07-08 17:08:34 -07001369 BuiltTool(ctx, "zip2zip").
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001370 FlagWithInput("-i ", inputPath).
1371 FlagWithOutput("-o ", outputPath).
1372 FlagWithArg("-0 ", "'lib/**/*.so'").
1373 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
1374 rule.Build(pctx, ctx, "uncompress-embedded-jni-libs", "Uncompress embedded JIN libs")
1375}
1376
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001377// Returns whether this module should have the dex file stored uncompressed in the APK.
1378func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
Liz Kammer7e20dda2020-05-20 14:36:30 -07001379 if ctx.Config().UnbundledBuild() || a.preprocessed {
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001380 return false
1381 }
1382
1383 // Uncompress dex in APKs of privileged apps
Jiyong Parkf7487312019-10-17 12:54:30 +09001384 if ctx.Config().UncompressPrivAppDex() && a.Privileged() {
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001385 return true
1386 }
1387
1388 return shouldUncompressDex(ctx, &a.dexpreopter)
1389}
1390
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001391func (a *AndroidAppImport) uncompressDex(
1392 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
1393 rule := android.NewRuleBuilder()
1394 rule.Command().
1395 Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
Colin Crossee94d6a2019-07-08 17:08:34 -07001396 BuiltTool(ctx, "zip2zip").
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001397 FlagWithInput("-i ", inputPath).
1398 FlagWithOutput("-o ", outputPath).
1399 FlagWithArg("-0 ", "'classes*.dex'").
1400 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
1401 rule.Build(pctx, ctx, "uncompress-dex", "Uncompress dex files")
1402}
1403
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001404func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001405 a.generateAndroidBuildActions(ctx)
1406}
1407
Jooyung Han65cd0f02020-03-23 20:21:11 +09001408func (a *AndroidAppImport) InstallApkName() string {
1409 return a.BaseModuleName()
1410}
1411
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001412func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001413 numCertPropsSet := 0
1414 if String(a.properties.Certificate) != "" {
1415 numCertPropsSet++
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001416 }
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001417 if Bool(a.properties.Presigned) {
1418 numCertPropsSet++
1419 }
1420 if Bool(a.properties.Default_dev_cert) {
1421 numCertPropsSet++
1422 }
1423 if numCertPropsSet != 1 {
1424 ctx.ModuleErrorf("One and only one of certficate, presigned, and default_dev_cert properties must be set")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001425 }
1426
Colin Crosseb032962020-05-13 11:05:02 -07001427 _, certificates := collectAppDeps(ctx, a, false, false)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001428
1429 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001430 // TODO: LOCAL_PACKAGE_SPLITS
1431
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001432 srcApk := a.prebuilt.SingleSourcePath(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -07001433
1434 if a.usesLibrary.enforceUsesLibraries() {
1435 srcApk = a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk)
1436 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001437
1438 // TODO: Install or embed JNI libraries
1439
1440 // Uncompress JNI libraries in the apk
1441 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
1442 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
1443
Kyeongkab.Namc4997142019-11-22 11:38:16 +09001444 var installDir android.InstallPath
1445 if Bool(a.properties.Privileged) {
1446 installDir = android.PathForModuleInstall(ctx, "priv-app", a.BaseModuleName())
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001447 } else if ctx.InstallInTestcases() {
1448 installDir = android.PathForModuleInstall(ctx, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch())
Kyeongkab.Namc4997142019-11-22 11:38:16 +09001449 } else {
1450 installDir = android.PathForModuleInstall(ctx, "app", a.BaseModuleName())
1451 }
1452
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001453 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001454 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001455 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -07001456
1457 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
1458 a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
1459 a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
1460 a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
1461
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001462 dexOutput := a.dexpreopter.dexpreopt(ctx, jnisUncompressed)
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001463 if a.dexpreopter.uncompressedDex {
1464 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
1465 a.uncompressDex(ctx, dexOutput, dexUncompressed.OutputPath)
1466 dexOutput = dexUncompressed
1467 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001468
Jooyung Han65cd0f02020-03-23 20:21:11 +09001469 apkFilename := proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk")
1470
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001471 // TODO: Handle EXTERNAL
Liz Kammer7e20dda2020-05-20 14:36:30 -07001472
1473 // Sign or align the package if package has not been preprocessed
1474 if a.preprocessed {
1475 a.outputFile = srcApk
Sasha Smundakc4f0ff12020-05-27 16:36:07 -07001476 a.certificate = PresignedCertificate
Liz Kammer7e20dda2020-05-20 14:36:30 -07001477 } else if !Bool(a.properties.Presigned) {
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001478 // If the certificate property is empty at this point, default_dev_cert must be set to true.
1479 // Which makes processMainCert's behavior for the empty cert string WAI.
1480 certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001481 if len(certificates) != 1 {
1482 ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates)
1483 }
Colin Cross503c1d02020-01-28 14:00:53 -08001484 a.certificate = certificates[0]
Jooyung Han65cd0f02020-03-23 20:21:11 +09001485 signed := android.PathForModuleOut(ctx, "signed", apkFilename)
Liz Kammer2bc57f62020-05-13 15:49:21 -07001486 var lineageFile android.Path
1487 if lineage := String(a.properties.Lineage); lineage != "" {
1488 lineageFile = android.PathForModuleSrc(ctx, lineage)
1489 }
1490 SignAppPackage(ctx, signed, dexOutput, certificates, nil, lineageFile)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001491 a.outputFile = signed
1492 } else {
Jooyung Han65cd0f02020-03-23 20:21:11 +09001493 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001494 TransformZipAlign(ctx, alignedApk, dexOutput)
1495 a.outputFile = alignedApk
Sasha Smundakc4f0ff12020-05-27 16:36:07 -07001496 a.certificate = PresignedCertificate
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001497 }
1498
1499 // TODO: Optionally compress the output apk.
1500
Jooyung Han65cd0f02020-03-23 20:21:11 +09001501 a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001502
1503 // TODO: androidmk converter jni libs
1504}
1505
1506func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
1507 return &a.prebuilt
1508}
1509
1510func (a *AndroidAppImport) Name() string {
1511 return a.prebuilt.Name(a.ModuleBase.Name())
1512}
1513
Dario Frenicde2a032019-10-27 00:29:22 +01001514func (a *AndroidAppImport) OutputFile() android.Path {
1515 return a.outputFile
1516}
1517
Jiyong Park618922e2020-01-08 13:35:43 +09001518func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
1519 return nil
1520}
1521
Colin Cross503c1d02020-01-28 14:00:53 -08001522func (a *AndroidAppImport) Certificate() Certificate {
1523 return a.certificate
1524}
1525
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001526var dpiVariantGroupType reflect.Type
1527var archVariantGroupType reflect.Type
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001528
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001529func initAndroidAppImportVariantGroupTypes() {
1530 dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants")
1531
1532 archNames := make([]string, len(android.ArchTypeList()))
1533 for i, archType := range android.ArchTypeList() {
1534 archNames[i] = archType.Name
1535 }
1536 archVariantGroupType = createVariantGroupType(archNames, "Arch")
1537}
1538
1539// Populates all variant struct properties at creation time.
1540func (a *AndroidAppImport) populateAllVariantStructs() {
1541 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
1542 a.AddProperties(a.dpiVariants)
1543
1544 a.archVariants = reflect.New(archVariantGroupType).Interface()
1545 a.AddProperties(a.archVariants)
1546}
1547
Jiyong Parkf7487312019-10-17 12:54:30 +09001548func (a *AndroidAppImport) Privileged() bool {
1549 return Bool(a.properties.Privileged)
1550}
1551
Colin Crosseb032962020-05-13 11:05:02 -07001552func (a *AndroidAppImport) sdkVersion() sdkSpec {
1553 return sdkSpecFrom("")
1554}
1555
1556func (a *AndroidAppImport) minSdkVersion() sdkSpec {
1557 return sdkSpecFrom("")
1558}
1559
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001560func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
1561 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
1562
1563 variantFields := make([]reflect.StructField, len(variants))
1564 for i, variant := range variants {
1565 variantFields[i] = reflect.StructField{
1566 Name: proptools.FieldNameForProperty(variant),
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001567 Type: props,
1568 }
1569 }
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001570
1571 variantGroupStruct := reflect.StructOf(variantFields)
1572 return reflect.StructOf([]reflect.StructField{
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001573 {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001574 Name: variantGroupName,
1575 Type: variantGroupStruct,
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001576 },
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001577 })
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001578}
1579
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001580// android_app_import imports a prebuilt apk with additional processing specified in the module.
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001581// DPI-specific apk source files can be specified using dpi_variants. Example:
1582//
1583// android_app_import {
1584// name: "example_import",
1585// apk: "prebuilts/example.apk",
1586// dpi_variants: {
1587// mdpi: {
1588// apk: "prebuilts/example_mdpi.apk",
1589// },
1590// xhdpi: {
1591// apk: "prebuilts/example_xhdpi.apk",
1592// },
1593// },
1594// certificate: "PRESIGNED",
1595// }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001596func AndroidAppImportFactory() android.Module {
1597 module := &AndroidAppImport{}
1598 module.AddProperties(&module.properties)
1599 module.AddProperties(&module.dexpreoptProperties)
Colin Cross50ddcc42019-05-16 12:28:22 -07001600 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001601 module.populateAllVariantStructs()
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001602 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001603 module.processVariants(ctx)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001604 })
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001605
Jaewoong Jung0feed892020-05-26 20:10:08 -07001606 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1607 android.InitDefaultableModule(module)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001608 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001609
1610 return module
1611}
Colin Cross50ddcc42019-05-16 12:28:22 -07001612
Liz Kammer7e20dda2020-05-20 14:36:30 -07001613type androidTestImportProperties struct {
1614 // Whether the prebuilt apk can be installed without additional processing. Default is false.
1615 Preprocessed *bool
1616}
1617
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001618type AndroidTestImport struct {
1619 AndroidAppImport
1620
1621 testProperties testProperties
1622
Liz Kammer7e20dda2020-05-20 14:36:30 -07001623 testImportProperties androidTestImportProperties
1624
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001625 data android.Paths
1626}
1627
1628func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammer7e20dda2020-05-20 14:36:30 -07001629 a.preprocessed = Bool(a.testImportProperties.Preprocessed)
1630
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001631 a.generateAndroidBuildActions(ctx)
1632
1633 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
1634}
1635
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001636func (a *AndroidTestImport) InstallInTestcases() bool {
1637 return true
1638}
1639
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001640// android_test_import imports a prebuilt test apk with additional processing specified in the
1641// module. DPI or arch variant configurations can be made as with android_app_import.
1642func AndroidTestImportFactory() android.Module {
1643 module := &AndroidTestImport{}
1644 module.AddProperties(&module.properties)
1645 module.AddProperties(&module.dexpreoptProperties)
1646 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
1647 module.AddProperties(&module.testProperties)
Liz Kammer7e20dda2020-05-20 14:36:30 -07001648 module.AddProperties(&module.testImportProperties)
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001649 module.populateAllVariantStructs()
1650 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
1651 module.processVariants(ctx)
1652 })
1653
Colin Crossf30c4532020-05-06 22:29:10 -07001654 module.dexpreopter.isTest = true
1655
Jaewoong Junga689ffe2020-05-01 15:50:08 -07001656 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1657 android.InitDefaultableModule(module)
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001658 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
1659
1660 return module
1661}
1662
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001663type RuntimeResourceOverlay struct {
1664 android.ModuleBase
1665 android.DefaultableModuleBase
Roshan Piusb8307962020-04-27 09:42:27 -07001666 android.OverridableModuleBase
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001667 aapt
1668
Roshan Piusb8307962020-04-27 09:42:27 -07001669 properties RuntimeResourceOverlayProperties
1670 overridableProperties OverridableRuntimeResourceOverlayProperties
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001671
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08001672 certificate Certificate
1673
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001674 outputFile android.Path
1675 installDir android.InstallPath
1676}
1677
1678type RuntimeResourceOverlayProperties struct {
1679 // the name of a certificate in the default certificate directory or an android_app_certificate
1680 // module name in the form ":module".
1681 Certificate *string
1682
Liz Kammer7fe241f2020-05-19 16:15:25 -07001683 // Name of the signing certificate lineage file.
1684 Lineage *string
1685
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001686 // optional theme name. If specified, the overlay package will be applied
1687 // only when the ro.boot.vendor.overlay.theme system property is set to the same value.
1688 Theme *string
1689
1690 // if not blank, set to the version of the sdk to compile against.
1691 // Defaults to compiling against the current platform.
1692 Sdk_version *string
1693
1694 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
1695 // Defaults to sdk_version if not set.
1696 Min_sdk_version *string
Jaewoong Jungca095d72020-04-09 16:15:30 -07001697
1698 // list of android_library modules whose resources are extracted and linked against statically
1699 Static_libs []string
1700
1701 // list of android_app modules whose resources are extracted and linked against
1702 Resource_libs []string
Jaewoong Jungbfc6ac02020-04-24 15:22:40 -07001703
1704 // Names of modules to be overridden. Listed modules can only be other overlays
1705 // (in Make or Soong).
1706 // This does not completely prevent installation of the overridden overlays, but if both
1707 // overlays would be installed by default (in PRODUCT_PACKAGES) the other overlay will be removed
1708 // from PRODUCT_PACKAGES.
1709 Overrides []string
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001710}
1711
1712func (r *RuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
1713 sdkDep := decodeSdkDep(ctx, sdkContext(r))
1714 if sdkDep.hasFrameworkLibs() {
1715 r.aapt.deps(ctx, sdkDep)
1716 }
1717
1718 cert := android.SrcIsModule(String(r.properties.Certificate))
1719 if cert != "" {
1720 ctx.AddDependency(ctx.Module(), certificateTag, cert)
1721 }
Jaewoong Jungca095d72020-04-09 16:15:30 -07001722
1723 ctx.AddVariationDependencies(nil, staticLibTag, r.properties.Static_libs...)
1724 ctx.AddVariationDependencies(nil, libTag, r.properties.Resource_libs...)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001725}
1726
1727func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1728 // Compile and link resources
1729 r.aapt.hasNoCode = true
Jaewoong Jungf0f747c2020-01-24 10:30:02 -08001730 // Do not remove resources without default values nor dedupe resource configurations with the same value
Roshan Piusb8307962020-04-27 09:42:27 -07001731 aaptLinkFlags := []string{"--no-resource-deduping", "--no-resource-removal"}
1732 // Allow the override of "package name" and "overlay target package name"
1733 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1734 if overridden || r.overridableProperties.Package_name != nil {
1735 // The product override variable has a priority over the package_name property.
1736 if !overridden {
1737 manifestPackageName = *r.overridableProperties.Package_name
1738 }
1739 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
1740 }
1741 if r.overridableProperties.Target_package_name != nil {
1742 aaptLinkFlags = append(aaptLinkFlags,
1743 "--rename-overlay-target-package "+*r.overridableProperties.Target_package_name)
1744 }
1745 r.aapt.buildActions(ctx, r, aaptLinkFlags...)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001746
1747 // Sign the built package
Colin Crosseb032962020-05-13 11:05:02 -07001748 _, certificates := collectAppDeps(ctx, r, false, false)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001749 certificates = processMainCert(r.ModuleBase, String(r.properties.Certificate), certificates, ctx)
1750 signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
Liz Kammer7fe241f2020-05-19 16:15:25 -07001751 var lineageFile android.Path
1752 if lineage := String(r.properties.Lineage); lineage != "" {
1753 lineageFile = android.PathForModuleSrc(ctx, lineage)
1754 }
1755 SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil, lineageFile)
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08001756 r.certificate = certificates[0]
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001757
1758 r.outputFile = signed
1759 r.installDir = android.PathForModuleInstall(ctx, "overlay", String(r.properties.Theme))
1760 ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile)
1761}
1762
Jiyong Park6a927c42020-01-21 02:03:43 +09001763func (r *RuntimeResourceOverlay) sdkVersion() sdkSpec {
1764 return sdkSpecFrom(String(r.properties.Sdk_version))
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001765}
1766
1767func (r *RuntimeResourceOverlay) systemModules() string {
1768 return ""
1769}
1770
Jiyong Park6a927c42020-01-21 02:03:43 +09001771func (r *RuntimeResourceOverlay) minSdkVersion() sdkSpec {
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001772 if r.properties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +09001773 return sdkSpecFrom(*r.properties.Min_sdk_version)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001774 }
1775 return r.sdkVersion()
1776}
1777
Jiyong Park6a927c42020-01-21 02:03:43 +09001778func (r *RuntimeResourceOverlay) targetSdkVersion() sdkSpec {
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001779 return r.sdkVersion()
1780}
1781
1782// runtime_resource_overlay generates a resource-only apk file that can overlay application and
1783// system resources at run time.
1784func RuntimeResourceOverlayFactory() android.Module {
1785 module := &RuntimeResourceOverlay{}
1786 module.AddProperties(
1787 &module.properties,
Roshan Piusb8307962020-04-27 09:42:27 -07001788 &module.aaptProperties,
1789 &module.overridableProperties)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001790
Roshan Piusb8307962020-04-27 09:42:27 -07001791 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1792 android.InitDefaultableModule(module)
1793 android.InitOverridableModule(module, &module.properties.Overrides)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001794 return module
1795}
1796
Colin Cross50ddcc42019-05-16 12:28:22 -07001797type UsesLibraryProperties struct {
1798 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file.
1799 Uses_libs []string
1800
1801 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file with
1802 // required=false.
1803 Optional_uses_libs []string
1804
1805 // If true, the list of uses_libs and optional_uses_libs modules must match the AndroidManifest.xml file. Defaults
1806 // to true if either uses_libs or optional_uses_libs is set. Will unconditionally default to true in the future.
1807 Enforce_uses_libs *bool
1808}
1809
1810// usesLibrary provides properties and helper functions for AndroidApp and AndroidAppImport to verify that the
1811// <uses-library> tags that end up in the manifest of an APK match the ones known to the build system through the
1812// uses_libs and optional_uses_libs properties. The build system's values are used by dexpreopt to preopt apps
1813// with knowledge of their shared libraries.
1814type usesLibrary struct {
1815 usesLibraryProperties UsesLibraryProperties
1816}
1817
Paul Duffin250e6192019-06-07 10:44:37 +01001818func (u *usesLibrary) deps(ctx android.BottomUpMutatorContext, hasFrameworkLibs bool) {
Colin Cross3245b2c2019-06-07 13:18:09 -07001819 if !ctx.Config().UnbundledBuild() {
1820 ctx.AddVariationDependencies(nil, usesLibTag, u.usesLibraryProperties.Uses_libs...)
1821 ctx.AddVariationDependencies(nil, usesLibTag, u.presentOptionalUsesLibs(ctx)...)
Paul Duffin250e6192019-06-07 10:44:37 +01001822 // Only add these extra dependencies if the module depends on framework libs. This avoids
1823 // creating a cyclic dependency:
1824 // e.g. framework-res -> org.apache.http.legacy -> ... -> framework-res.
1825 if hasFrameworkLibs {
Colin Cross3245b2c2019-06-07 13:18:09 -07001826 // dexpreopt/dexpreopt.go needs the paths to the dex jars of these libraries in case construct_context.sh needs
1827 // to pass them to dex2oat. Add them as a dependency so we can determine the path to the dex jar of each
1828 // library to dexpreopt.
1829 ctx.AddVariationDependencies(nil, usesLibTag,
1830 "org.apache.http.legacy",
1831 "android.hidl.base-V1.0-java",
1832 "android.hidl.manager-V1.0-java")
1833 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001834 }
1835}
1836
1837// presentOptionalUsesLibs returns optional_uses_libs after filtering out MissingUsesLibraries, which don't exist in the
1838// build.
1839func (u *usesLibrary) presentOptionalUsesLibs(ctx android.BaseModuleContext) []string {
1840 optionalUsesLibs, _ := android.FilterList(u.usesLibraryProperties.Optional_uses_libs, ctx.Config().MissingUsesLibraries())
1841 return optionalUsesLibs
1842}
1843
1844// usesLibraryPaths returns a map of module names of shared library dependencies to the paths to their dex jars.
1845func (u *usesLibrary) usesLibraryPaths(ctx android.ModuleContext) map[string]android.Path {
1846 usesLibPaths := make(map[string]android.Path)
1847
1848 if !ctx.Config().UnbundledBuild() {
1849 ctx.VisitDirectDepsWithTag(usesLibTag, func(m android.Module) {
1850 if lib, ok := m.(Dependency); ok {
1851 if dexJar := lib.DexJar(); dexJar != nil {
1852 usesLibPaths[ctx.OtherModuleName(m)] = dexJar
1853 } else {
1854 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must produce a dex jar, does it have installable: true?",
1855 ctx.OtherModuleName(m))
1856 }
1857 } else if ctx.Config().AllowMissingDependencies() {
1858 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(m)})
1859 } else {
1860 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must be a java library",
1861 ctx.OtherModuleName(m))
1862 }
1863 })
1864 }
1865
1866 return usesLibPaths
1867}
1868
1869// enforceUsesLibraries returns true of <uses-library> tags should be checked against uses_libs and optional_uses_libs
1870// properties. Defaults to true if either of uses_libs or optional_uses_libs is specified. Will default to true
1871// unconditionally in the future.
1872func (u *usesLibrary) enforceUsesLibraries() bool {
1873 defaultEnforceUsesLibs := len(u.usesLibraryProperties.Uses_libs) > 0 ||
1874 len(u.usesLibraryProperties.Optional_uses_libs) > 0
1875 return BoolDefault(u.usesLibraryProperties.Enforce_uses_libs, defaultEnforceUsesLibs)
1876}
1877
1878// verifyUsesLibrariesManifest checks the <uses-library> tags in an AndroidManifest.xml against the ones specified
1879// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the manifest.
1880func (u *usesLibrary) verifyUsesLibrariesManifest(ctx android.ModuleContext, manifest android.Path) android.Path {
1881 outputFile := android.PathForModuleOut(ctx, "manifest_check", "AndroidManifest.xml")
1882
1883 rule := android.NewRuleBuilder()
Colin Crossee94d6a2019-07-08 17:08:34 -07001884 cmd := rule.Command().BuiltTool(ctx, "manifest_check").
Colin Cross50ddcc42019-05-16 12:28:22 -07001885 Flag("--enforce-uses-libraries").
1886 Input(manifest).
1887 FlagWithOutput("-o ", outputFile)
1888
1889 for _, lib := range u.usesLibraryProperties.Uses_libs {
1890 cmd.FlagWithArg("--uses-library ", lib)
1891 }
1892
1893 for _, lib := range u.usesLibraryProperties.Optional_uses_libs {
1894 cmd.FlagWithArg("--optional-uses-library ", lib)
1895 }
1896
1897 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1898
1899 return outputFile
1900}
1901
1902// verifyUsesLibrariesAPK checks the <uses-library> tags in the manifest of an APK against the ones specified
1903// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the APK.
1904func (u *usesLibrary) verifyUsesLibrariesAPK(ctx android.ModuleContext, apk android.Path) android.Path {
1905 outputFile := android.PathForModuleOut(ctx, "verify_uses_libraries", apk.Base())
1906
1907 rule := android.NewRuleBuilder()
1908 aapt := ctx.Config().HostToolPath(ctx, "aapt")
1909 rule.Command().
1910 Textf("aapt_binary=%s", aapt.String()).Implicit(aapt).
1911 Textf(`uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Uses_libs, " ")).
1912 Textf(`optional_uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Optional_uses_libs, " ")).
1913 Tool(android.PathForSource(ctx, "build/make/core/verify_uses_libraries.sh")).Input(apk)
1914 rule.Command().Text("cp -f").Input(apk).Output(outputFile)
1915
1916 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1917
1918 return outputFile
1919}