blob: a81a061fd1e60b137f5277ee4184724089781ca9 [file] [log] [blame]
Colin Cross30e076a2015-04-13 13:58:27 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Android apps.
18
19import (
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070020 "path/filepath"
21 "reflect"
Jaewoong Jung5b425e22019-06-17 17:40:56 -070022 "sort"
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070023 "strings"
Colin Cross30e076a2015-04-13 13:58:27 -070024
Colin Cross50ddcc42019-05-16 12:28:22 -070025 "github.com/google/blueprint"
26 "github.com/google/blueprint/proptools"
27
Colin Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Colin Crossa4f08812018-10-02 22:03:40 -070029 "android/soong/cc"
Colin Cross303e21f2018-08-07 16:49:25 -070030 "android/soong/tradefed"
Colin Cross30e076a2015-04-13 13:58:27 -070031)
32
Jaewoong Jung3e18b192019-06-11 12:25:34 -070033var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070034
Colin Cross3bc7ffa2017-11-22 16:19:37 -080035func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000036 RegisterAppBuildComponents(android.InitRegistrationContext)
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -070037
38 initAndroidAppImportVariantGroupTypes()
Colin Cross3bc7ffa2017-11-22 16:19:37 -080039}
40
Paul Duffinf9b1da02019-12-18 19:51:55 +000041func RegisterAppBuildComponents(ctx android.RegistrationContext) {
42 ctx.RegisterModuleType("android_app", AndroidAppFactory)
43 ctx.RegisterModuleType("android_test", AndroidTestFactory)
44 ctx.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
45 ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
46 ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
47 ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory)
Roshan Pius4df2bc72020-04-27 09:42:27 -070048 ctx.RegisterModuleType("override_runtime_resource_overlay", OverrideRuntimeResourceOverlayModuleFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000049 ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory)
50 ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -080051 ctx.RegisterModuleType("runtime_resource_overlay", RuntimeResourceOverlayFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000052}
53
Colin Cross30e076a2015-04-13 13:58:27 -070054// AndroidManifest.xml merging
55// package splits
56
Colin Crossfabb6082018-02-20 17:22:23 -080057type appProperties struct {
Colin Crossbd01e2a2018-10-04 15:21:03 -070058 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
Colin Cross7d5136f2015-05-11 13:39:40 -070059 Additional_certificates []string
60
61 // If set, create package-export.apk, which other packages can
62 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
Nan Zhangea568a42017-11-08 21:20:04 -080063 Export_package_resources *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070064
Colin Cross16056062017-12-13 22:46:28 -080065 // Specifies that this app should be installed to the priv-app directory,
66 // where the system will grant it additional privileges not available to
67 // normal apps.
68 Privileged *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070069
70 // list of resource labels to generate individual resource packages
71 Package_splits []string
Jason Monkd4122be2018-08-10 09:33:36 -040072
73 // Names of modules to be overridden. Listed modules can only be other binaries
74 // (in Make or Soong).
75 // This does not completely prevent installation of the overridden binaries, but if both
76 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
77 // from PRODUCT_PACKAGES.
78 Overrides []string
Colin Crossa4f08812018-10-02 22:03:40 -070079
80 // list of native libraries that will be provided in or alongside the resulting jar
81 Jni_libs []string `android:"arch_variant"`
82
Colin Cross7204cf02020-05-06 17:51:39 -070083 // if true, use JNI libraries that link against platform APIs even if this module sets
Colin Crossee87c602020-02-19 16:57:15 -080084 // sdk_version.
85 Jni_uses_platform_apis *bool
86
Colin Cross7204cf02020-05-06 17:51:39 -070087 // if true, use JNI libraries that link against SDK APIs even if this module does not set
88 // sdk_version.
89 Jni_uses_sdk_apis *bool
90
Jaewoong Jungbc625cd2019-05-06 15:48:44 -070091 // STL library to use for JNI libraries.
92 Stl *string `android:"arch_variant"`
93
Colin Crosse4246ab2019-02-05 21:55:21 -080094 // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest
95 // 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 +090096 // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to true for
97 // android_app modules that are embedded to APEXes, defaults to false for other module types where the native
98 // libraries are generally preinstalled outside the APK.
Colin Crosse4246ab2019-02-05 21:55:21 -080099 Use_embedded_native_libs *bool
Colin Cross46abdad2019-02-07 13:07:08 -0800100
101 // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that
102 // they are used from inside the APK at runtime.
103 Use_embedded_dex *bool
Colin Cross47fa9d32019-03-26 10:51:39 -0700104
105 // Forces native libraries to always be packaged into the APK,
106 // Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed.
107 // True for android_test* modules.
108 AlwaysPackageNativeLibs bool `blueprint:"mutated"`
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700109
110 // If set, find and merge all NOTICE files that this module and its dependencies have and store
111 // it in the APK as an asset.
112 Embed_notices *bool
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700113
114 // cc.Coverage related properties
115 PreventInstall bool `blueprint:"mutated"`
116 HideFromMake bool `blueprint:"mutated"`
117 IsCoverageVariant bool `blueprint:"mutated"`
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100118
119 // Whether this app is considered mainline updatable or not. When set to true, this will enforce
Artur Satayevf40fc852020-04-16 13:43:02 +0100120 // additional rules to make sure an app can safely be updated. Default is false.
121 // Prefer using other specific properties if build behaviour must be changed; avoid using this
122 // flag for anything but neverallow rules (unless the behaviour change is invisible to owners).
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100123 Updatable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700124}
125
Jaewoong Jung525443a2019-02-28 15:35:54 -0800126// android_app properties that can be overridden by override_android_app
127type overridableAppProperties struct {
128 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
129 // or an android_app_certificate module name in the form ":module".
130 Certificate *string
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700131
Liz Kammere2b27f42020-05-07 13:24:05 -0700132 // Name of the signing certificate lineage file.
133 Lineage *string
134
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700135 // the package name of this app. The package name in the manifest file is used if one was not given.
136 Package_name *string
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800137
138 // the logging parent of this app.
139 Logging_parent *string
Jaewoong Jung525443a2019-02-28 15:35:54 -0800140}
141
Roshan Pius4df2bc72020-04-27 09:42:27 -0700142// runtime_resource_overlay properties that can be overridden by override_runtime_resource_overlay
143type OverridableRuntimeResourceOverlayProperties struct {
144 // the package name of this app. The package name in the manifest file is used if one was not given.
145 Package_name *string
146
147 // the target package name of this overlay app. The target package name in the manifest file is used if one was not given.
148 Target_package_name *string
149}
150
Colin Cross30e076a2015-04-13 13:58:27 -0700151type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -0700152 Library
153 aapt
Jaewoong Jung525443a2019-02-28 15:35:54 -0800154 android.OverridableModuleBase
Colin Crossa97c5d32018-03-28 14:58:31 -0700155
Colin Cross50ddcc42019-05-16 12:28:22 -0700156 usesLibrary usesLibrary
157
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900158 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -0700159
Colin Crossfabb6082018-02-20 17:22:23 -0800160 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -0700161
Jaewoong Jung525443a2019-02-28 15:35:54 -0800162 overridableAppProperties overridableAppProperties
163
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700164 installJniLibs []jniLib
165 jniCoverageOutputs android.Paths
Colin Crossf6237212018-10-29 23:14:58 -0700166
167 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800168
169 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
170 installApkName string
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800171
Colin Cross70dda7e2019-10-01 22:05:35 -0700172 installDir android.InstallPath
Jaewoong Jung0949f312019-09-11 10:25:18 -0700173
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700174 onDeviceDir string
175
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800176 additionalAaptFlags []string
Jaewoong Jung98772792019-07-01 17:15:13 -0700177
178 noticeOutputs android.NoticeOutputs
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900179
180 overriddenManifestPackageName string
Artur Satayev1111b842020-04-27 19:05:28 +0100181
182 android.ApexBundleDepsInfo
Colin Crosse1731a52017-12-14 11:22:55 -0800183}
184
Martin Stjernholm6d415272020-01-31 17:10:36 +0000185func (a *AndroidApp) IsInstallable() bool {
186 return Bool(a.properties.Installable)
187}
188
Colin Cross89c31582018-04-30 15:55:11 -0700189func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
190 return nil
191}
192
Colin Cross66f78822018-05-02 12:58:28 -0700193func (a *AndroidApp) ExportedStaticPackages() android.Paths {
194 return nil
195}
196
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900197func (a *AndroidApp) OutputFile() android.Path {
198 return a.outputFile
199}
200
Colin Cross503c1d02020-01-28 14:00:53 -0800201func (a *AndroidApp) Certificate() Certificate {
202 return a.certificate
203}
204
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700205func (a *AndroidApp) JniCoverageOutputs() android.Paths {
206 return a.jniCoverageOutputs
207}
208
Colin Crossa97c5d32018-03-28 14:58:31 -0700209var _ AndroidLibraryDependency = (*AndroidApp)(nil)
210
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900211type Certificate struct {
Colin Cross503c1d02020-01-28 14:00:53 -0800212 Pem, Key android.Path
213 presigned bool
214}
215
216var presignedCertificate = Certificate{presigned: true}
217
218func (c Certificate) AndroidMkString() string {
219 if c.presigned {
220 return "PRESIGNED"
221 } else {
222 return c.Pem.String()
223 }
Colin Cross30e076a2015-04-13 13:58:27 -0700224}
225
Colin Cross46c9b8b2017-06-22 16:51:17 -0700226func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
227 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700228
Jiyong Park6a927c42020-01-21 02:03:43 +0900229 if String(a.appProperties.Stl) == "c++_shared" && !a.sdkVersion().specified() {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700230 ctx.PropertyErrorf("stl", "sdk_version must be set in order to use c++_shared")
231 }
232
Paul Duffin250e6192019-06-07 10:44:37 +0100233 sdkDep := decodeSdkDep(ctx, sdkContext(a))
234 if sdkDep.hasFrameworkLibs() {
235 a.aapt.deps(ctx, sdkDep)
Colin Cross30e076a2015-04-13 13:58:27 -0700236 }
Colin Crossa4f08812018-10-02 22:03:40 -0700237
Colin Cross3c007702020-05-08 11:20:24 -0700238 usesSDK := a.sdkVersion().specified() && a.sdkVersion().kind != sdkCorePlatform
239
240 if usesSDK && Bool(a.appProperties.Jni_uses_sdk_apis) {
241 ctx.PropertyErrorf("jni_uses_sdk_apis",
242 "can only be set for modules that do not set sdk_version")
243 } else if !usesSDK && Bool(a.appProperties.Jni_uses_platform_apis) {
244 ctx.PropertyErrorf("jni_uses_platform_apis",
245 "can only be set for modules that set sdk_version")
246 }
247
Peter Collingbournead84f972019-12-17 16:46:18 -0800248 tag := &jniDependencyTag{}
Colin Crossa4f08812018-10-02 22:03:40 -0700249 for _, jniTarget := range ctx.MultiTargets() {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700250 variation := append(jniTarget.Variations(),
251 blueprint.Variation{Mutator: "link", Variation: "shared"})
Colin Crossc511bc52020-04-07 16:50:32 +0000252
253 // If the app builds against an Android SDK use the SDK variant of JNI dependencies
254 // unless jni_uses_platform_apis is set.
Colin Crossc2d24052020-05-13 11:05:02 -0700255 // Don't require the SDK variant for apps that are shipped on vendor, etc., as they already
256 // have stable APIs through the VNDK.
257 if (usesSDK && !a.RequiresStableAPIs(ctx) &&
258 !Bool(a.appProperties.Jni_uses_platform_apis)) ||
Colin Cross7204cf02020-05-06 17:51:39 -0700259 Bool(a.appProperties.Jni_uses_sdk_apis) {
Colin Crossc511bc52020-04-07 16:50:32 +0000260 variation = append(variation, blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
261 }
Colin Crossa4f08812018-10-02 22:03:40 -0700262 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
263 }
Colin Cross50ddcc42019-05-16 12:28:22 -0700264
Paul Duffin250e6192019-06-07 10:44:37 +0100265 a.usesLibrary.deps(ctx, sdkDep.hasFrameworkLibs())
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700266}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700267
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700268func (a *AndroidApp) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800269 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700270 if cert != "" {
271 ctx.AddDependency(ctx.Module(), certificateTag, cert)
272 }
273
274 for _, cert := range a.appProperties.Additional_certificates {
275 cert = android.SrcIsModule(cert)
276 if cert != "" {
277 ctx.AddDependency(ctx.Module(), certificateTag, cert)
278 } else {
279 ctx.PropertyErrorf("additional_certificates",
280 `must be names of android_app_certificate modules in the form ":module"`)
281 }
282 }
Colin Cross30e076a2015-04-13 13:58:27 -0700283}
284
Jeongik Cha538c0d02019-07-11 15:54:27 +0900285func (a *AndroidTestHelperApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
286 a.generateAndroidBuildActions(ctx)
287}
288
Colin Cross46c9b8b2017-06-22 16:51:17 -0700289func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100290 a.checkAppSdkVersions(ctx)
Colin Crossae5caf52018-05-22 11:11:52 -0700291 a.generateAndroidBuildActions(ctx)
292}
293
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100294func (a *AndroidApp) checkAppSdkVersions(ctx android.ModuleContext) {
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900295 if Bool(a.appProperties.Updatable) || a.ApexModuleBase.Updatable() {
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100296 if !a.sdkVersion().stable() {
297 ctx.PropertyErrorf("sdk_version", "Updatable apps must use stable SDKs, found %v", a.sdkVersion())
298 }
Artur Satayevf40fc852020-04-16 13:43:02 +0100299 if String(a.deviceProperties.Min_sdk_version) == "" {
300 ctx.PropertyErrorf("updatable", "updatable apps must set min_sdk_version.")
301 }
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900302 if minSdkVersion, err := a.minSdkVersion().effectiveVersion(ctx); err == nil {
303 a.checkJniLibsSdkVersion(ctx, minSdkVersion)
304 } else {
305 ctx.PropertyErrorf("min_sdk_version", "%s", err.Error())
306 }
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100307 }
308
309 a.checkPlatformAPI(ctx)
310 a.checkSdkVersions(ctx)
311}
312
Jooyung Hanbbc3fb72020-04-29 14:01:06 +0900313// If an updatable APK sets min_sdk_version, min_sdk_vesion of JNI libs should match with it.
314// This check is enforced for "updatable" APKs (including APK-in-APEX).
315// b/155209650: until min_sdk_version is properly supported, use sdk_version instead.
316// because, sdk_version is overridden by min_sdk_version (if set as smaller)
317// and linkType is checked with dependencies so we can be sure that the whole dependency tree
318// will meet the requirements.
319func (a *AndroidApp) checkJniLibsSdkVersion(ctx android.ModuleContext, minSdkVersion sdkVersion) {
320 // It's enough to check direct JNI deps' sdk_version because all transitive deps from JNI deps are checked in cc.checkLinkType()
321 ctx.VisitDirectDeps(func(m android.Module) {
322 if !IsJniDepTag(ctx.OtherModuleDependencyTag(m)) {
323 return
324 }
325 dep, _ := m.(*cc.Module)
326 jniSdkVersion, err := android.ApiStrToNum(ctx, dep.SdkVersion())
327 if err != nil || int(minSdkVersion) < jniSdkVersion {
328 ctx.OtherModuleErrorf(dep, "sdk_version(%v) is higher than min_sdk_version(%v) of the containing android_app(%v)",
329 dep.SdkVersion(), minSdkVersion, ctx.ModuleName())
330 return
331 }
332
333 })
334}
335
Sasha Smundak6ad77252019-05-01 13:16:22 -0700336// Returns true if the native libraries should be stored in the APK uncompressed and the
Colin Crosse4246ab2019-02-05 21:55:21 -0800337// extractNativeLibs application flag should be set to false in the manifest.
Sasha Smundak6ad77252019-05-01 13:16:22 -0700338func (a *AndroidApp) useEmbeddedNativeLibs(ctx android.ModuleContext) bool {
Jiyong Park6a927c42020-01-21 02:03:43 +0900339 minSdkVersion, err := a.minSdkVersion().effectiveVersion(ctx)
Colin Crosse4246ab2019-02-05 21:55:21 -0800340 if err != nil {
341 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
342 }
343
Jiyong Park52cd06f2019-11-11 10:14:32 +0900344 return (minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)) ||
345 !a.IsForPlatform()
Colin Crosse4246ab2019-02-05 21:55:21 -0800346}
347
Colin Cross43f08db2018-11-12 10:13:39 -0800348// Returns whether this module should have the dex file stored uncompressed in the APK.
349func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800350 if Bool(a.appProperties.Use_embedded_dex) {
351 return true
352 }
353
Colin Cross53a87f52019-06-25 13:35:30 -0700354 // Uncompress dex in APKs of privileged apps (even for unbundled builds, they may
355 // be preinstalled as prebuilts).
Jiyong Parkf7487312019-10-17 12:54:30 +0900356 if ctx.Config().UncompressPrivAppDex() && a.Privileged() {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000357 return true
358 }
359
Colin Cross53a87f52019-06-25 13:35:30 -0700360 if ctx.Config().UnbundledBuild() {
361 return false
362 }
363
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700364 return shouldUncompressDex(ctx, &a.dexpreopter)
Colin Cross5a0dcd52018-10-05 14:20:06 -0700365}
366
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700367func (a *AndroidApp) shouldEmbedJnis(ctx android.BaseModuleContext) bool {
368 return ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) ||
Jiyong Park52cd06f2019-11-11 10:14:32 +0900369 !a.IsForPlatform() || a.appProperties.AlwaysPackageNativeLibs
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700370}
371
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900372func (a *AndroidApp) OverriddenManifestPackageName() string {
373 return a.overriddenManifestPackageName
374}
375
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800376func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
David Brazdild25060a2019-02-18 18:24:16 +0000377 a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis)
378
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700379 // Ask manifest_fixer to add or update the application element indicating this app has no code.
380 a.aapt.hasNoCode = !a.hasCode(ctx)
381
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800382 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800383
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800384 // 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 -0800385 hasProduct := android.PrefixInList(a.aaptProperties.Aaptflags, "--product")
Colin Crosse78dcd32018-04-19 15:25:19 -0700386 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800387 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700388 }
389
Dan Willemsen72be5902018-10-24 20:24:57 -0700390 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
391 // Product AAPT config
392 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800393 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700394 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700395
Dan Willemsen72be5902018-10-24 20:24:57 -0700396 // Product AAPT preferred config
397 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800398 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700399 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700400 }
401
Jiyong Park7f67f482019-01-05 12:57:48 +0900402 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700403 if overridden || a.overridableAppProperties.Package_name != nil {
404 // The product override variable has a priority over the package_name property.
405 if !overridden {
406 manifestPackageName = *a.overridableAppProperties.Package_name
407 }
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800408 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900409 a.overriddenManifestPackageName = manifestPackageName
Jiyong Park7f67f482019-01-05 12:57:48 +0900410 }
411
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800412 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
413
Colin Crosse560c4a2019-03-19 16:03:11 -0700414 a.aapt.splitNames = a.appProperties.Package_splits
Colin Cross50ddcc42019-05-16 12:28:22 -0700415 a.aapt.sdkLibraries = a.exportedSdkLibs
Baligh Uddin5b16dfb2020-02-11 17:27:19 -0800416 a.aapt.LoggingParent = String(a.overridableAppProperties.Logging_parent)
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800417 a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700418
Colin Cross46c9b8b2017-06-22 16:51:17 -0700419 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700420 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800421}
Colin Cross30e076a2015-04-13 13:58:27 -0700422
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800423func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700424 var staticLibProguardFlagFiles android.Paths
425 ctx.VisitDirectDeps(func(m android.Module) {
426 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
427 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
428 }
429 })
430
431 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
432
433 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
434 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800435}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800436
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800437func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross43f08db2018-11-12 10:13:39 -0800438
439 var installDir string
440 if ctx.ModuleName() == "framework-res" {
441 // framework-res.apk is installed as system/framework/framework-res.apk
442 installDir = "framework"
Jiyong Parkf7487312019-10-17 12:54:30 +0900443 } else if a.Privileged() {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800444 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800445 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800446 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800447 }
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800448 a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000449 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -0700450
451 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
452 a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
453 a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
454 a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
455 a.dexpreopter.manifestFile = a.mergedManifestFile
456
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000457 a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex
Colin Cross5a0dcd52018-10-05 14:20:06 -0700458
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800459 if ctx.ModuleName() != "framework-res" {
460 a.Module.compile(ctx, a.aaptSrcJar)
461 }
Colin Cross30e076a2015-04-13 13:58:27 -0700462
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800463 return a.maybeStrippedDexJarFile
464}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800465
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800466func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700467 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700468 if len(jniLibs) > 0 {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700469 if a.shouldEmbedJnis(ctx) {
Colin Crossa4f08812018-10-02 22:03:40 -0700470 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Sasha Smundak6ad77252019-05-01 13:16:22 -0700471 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.useEmbeddedNativeLibs(ctx))
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700472 for _, jni := range jniLibs {
473 if jni.coverageFile.Valid() {
Jaewoong Jung46984ee2020-04-07 13:07:55 -0700474 // Only collect coverage for the first target arch if this is a multilib target.
475 // TODO(jungjw): Ideally, we want to collect both reports, but that would cause coverage
476 // data file path collisions since the current coverage file path format doesn't contain
477 // arch-related strings. This is fine for now though; the code coverage team doesn't use
478 // multi-arch targets such as test_suite_* for coverage collections yet.
479 //
480 // Work with the team to come up with a new format that handles multilib modules properly
481 // and change this.
482 if len(ctx.Config().Targets[android.Android]) == 1 ||
483 ctx.Config().Targets[android.Android][0].Arch.ArchType == jni.target.Arch.ArchType {
484 a.jniCoverageOutputs = append(a.jniCoverageOutputs, jni.coverageFile.Path())
485 }
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700486 }
487 }
Colin Crossa4f08812018-10-02 22:03:40 -0700488 } else {
489 a.installJniLibs = jniLibs
490 }
491 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800492 return jniJarFile
493}
Colin Crossa4f08812018-10-02 22:03:40 -0700494
Jaewoong Jung0949f312019-09-11 10:25:18 -0700495func (a *AndroidApp) noticeBuildActions(ctx android.ModuleContext) {
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700496 // Collect NOTICE files from all dependencies.
497 seenModules := make(map[android.Module]bool)
498 noticePathSet := make(map[android.Path]bool)
499
500 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
501 // Have we already seen this?
502 if _, ok := seenModules[child]; ok {
503 return false
504 }
505 seenModules[child] = true
506
507 // Skip host modules.
508 if child.Target().Os.Class == android.Host || child.Target().Os.Class == android.HostCross {
509 return false
510 }
511
Bob Badoura75b0572020-02-18 20:21:55 -0800512 paths := child.(android.Module).NoticeFiles()
513 if len(paths) > 0 {
514 for _, path := range paths {
515 noticePathSet[path] = true
516 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700517 }
518 return true
519 })
520
521 // If the app has one, add it too.
Bob Badoura75b0572020-02-18 20:21:55 -0800522 if len(a.NoticeFiles()) > 0 {
523 for _, path := range a.NoticeFiles() {
524 noticePathSet[path] = true
525 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700526 }
527
528 if len(noticePathSet) == 0 {
Jaewoong Jung98772792019-07-01 17:15:13 -0700529 return
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700530 }
531 var noticePaths []android.Path
532 for path := range noticePathSet {
533 noticePaths = append(noticePaths, path)
534 }
535 sort.Slice(noticePaths, func(i, j int) bool {
536 return noticePaths[i].String() < noticePaths[j].String()
537 })
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700538
Jaewoong Jung0949f312019-09-11 10:25:18 -0700539 a.noticeOutputs = android.BuildNoticeOutput(ctx, a.installDir, a.installApkName+".apk", noticePaths)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700540}
541
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700542// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
543// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
544func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate {
545 if android.SrcIsModule(certPropValue) == "" {
546 var mainCert Certificate
547 if certPropValue != "" {
548 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
549 mainCert = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -0800550 Pem: defaultDir.Join(ctx, certPropValue+".x509.pem"),
551 Key: defaultDir.Join(ctx, certPropValue+".pk8"),
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700552 }
553 } else {
554 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Colin Cross503c1d02020-01-28 14:00:53 -0800555 mainCert = Certificate{
556 Pem: pem,
557 Key: key,
558 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700559 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700560 certificates = append([]Certificate{mainCert}, certificates...)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700561 }
562
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700563 if !m.Platform() {
564 certPath := certificates[0].Pem.String()
Jeongik Chac9464142019-01-07 12:07:27 +0900565 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
566 if strings.HasPrefix(certPath, systemCertPath) {
567 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
568 whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
569
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700570 if enforceSystemCert && !inList(m.Name(), whitelist) {
Jeongik Chac9464142019-01-07 12:07:27 +0900571 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
572 }
573 }
574 }
575
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700576 return certificates
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800577}
578
Jooyung Han39ee1192020-03-23 20:21:11 +0900579func (a *AndroidApp) InstallApkName() string {
580 return a.installApkName
581}
582
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800583func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross50ddcc42019-05-16 12:28:22 -0700584 var apkDeps android.Paths
585
Jeongik Cha538c0d02019-07-11 15:54:27 +0900586 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
587 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
588
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800589 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800590 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800591
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700592 if ctx.ModuleName() == "framework-res" {
593 // framework-res.apk is installed as system/framework/framework-res.apk
Jaewoong Jung0949f312019-09-11 10:25:18 -0700594 a.installDir = android.PathForModuleInstall(ctx, "framework")
Jiyong Parkf7487312019-10-17 12:54:30 +0900595 } else if a.Privileged() {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700596 a.installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
597 } else if ctx.InstallInTestcases() {
Jaewoong Jung326a9412019-11-21 10:41:00 -0800598 a.installDir = android.PathForModuleInstall(ctx, a.installApkName, ctx.DeviceConfig().DeviceArch())
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700599 } else {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700600 a.installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700601 }
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700602 a.onDeviceDir = android.InstallPathToOnDevicePath(ctx, a.installDir)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700603
Jaewoong Jung0949f312019-09-11 10:25:18 -0700604 a.noticeBuildActions(ctx)
Jaewoong Jung98772792019-07-01 17:15:13 -0700605 if Bool(a.appProperties.Embed_notices) || ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") {
606 a.aapt.noticeFile = a.noticeOutputs.HtmlGzOutput
607 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700608
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800609 // Process all building blocks, from AAPT to certificates.
610 a.aaptBuildActions(ctx)
611
Colin Cross50ddcc42019-05-16 12:28:22 -0700612 if a.usesLibrary.enforceUsesLibraries() {
613 manifestCheckFile := a.usesLibrary.verifyUsesLibrariesManifest(ctx, a.mergedManifestFile)
614 apkDeps = append(apkDeps, manifestCheckFile)
615 }
616
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800617 a.proguardBuildActions(ctx)
618
619 dexJarFile := a.dexBuildActions(ctx)
620
Colin Crossc2d24052020-05-13 11:05:02 -0700621 jniLibs, certificateDeps := collectAppDeps(ctx, a, a.shouldEmbedJnis(ctx), !Bool(a.appProperties.Jni_uses_platform_apis))
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800622 jniJarFile := a.jniBuildActions(jniLibs, ctx)
623
624 if ctx.Failed() {
625 return
626 }
627
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700628 certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx)
629 a.certificate = certificates[0]
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800630
631 // Build a final signed app package.
Jaewoong Jung5a498812019-11-07 14:14:38 -0800632 packageFile := android.PathForModuleOut(ctx, a.installApkName+".apk")
Liz Kammere2b27f42020-05-07 13:24:05 -0700633 var lineageFile android.Path
634 if lineage := String(a.overridableAppProperties.Lineage); lineage != "" {
635 lineageFile = android.PathForModuleSrc(ctx, lineage)
636 }
637 CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates, apkDeps, lineageFile)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800638 a.outputFile = packageFile
639
Colin Crosse560c4a2019-03-19 16:03:11 -0700640 for _, split := range a.aapt.splits {
641 // Sign the split APKs
Jaewoong Jung5a498812019-11-07 14:14:38 -0800642 packageFile := android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk")
Liz Kammere2b27f42020-05-07 13:24:05 -0700643 CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates, apkDeps, lineageFile)
Colin Crosse560c4a2019-03-19 16:03:11 -0700644 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
645 }
646
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800647 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700648 bundleFile := android.PathForModuleOut(ctx, "base.zip")
649 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
650 a.bundleFile = bundleFile
651
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800652 // Install the app package.
Jiyong Park8ba50f92019-11-13 15:01:01 +0900653 if (Bool(a.Module.properties.Installable) || ctx.Host()) && a.IsForPlatform() {
654 ctx.InstallFile(a.installDir, a.outputFile.Base(), a.outputFile)
655 for _, extra := range a.extraOutputFiles {
656 ctx.InstallFile(a.installDir, extra.Base(), extra)
657 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800658 }
Artur Satayev1111b842020-04-27 19:05:28 +0100659
660 a.buildAppDependencyInfo(ctx)
Colin Cross30e076a2015-04-13 13:58:27 -0700661}
662
Colin Crossc2d24052020-05-13 11:05:02 -0700663type appDepsInterface interface {
664 sdkVersion() sdkSpec
665 minSdkVersion() sdkSpec
666 RequiresStableAPIs(ctx android.BaseModuleContext) bool
667}
668
669func collectAppDeps(ctx android.ModuleContext, app appDepsInterface,
670 shouldCollectRecursiveNativeDeps bool,
Colin Cross094cde42020-02-15 10:38:00 -0800671 checkNativeSdkVersion bool) ([]jniLib, []Certificate) {
Colin Crossc2d24052020-05-13 11:05:02 -0700672
Colin Crossa4f08812018-10-02 22:03:40 -0700673 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900674 var certificates []Certificate
Peter Collingbournead84f972019-12-17 16:46:18 -0800675 seenModulePaths := make(map[string]bool)
Colin Crossa4f08812018-10-02 22:03:40 -0700676
Colin Crossc2d24052020-05-13 11:05:02 -0700677 if checkNativeSdkVersion {
678 checkNativeSdkVersion = app.sdkVersion().specified() &&
679 app.sdkVersion().kind != sdkCorePlatform && !app.RequiresStableAPIs(ctx)
680 }
681
Peter Collingbournead84f972019-12-17 16:46:18 -0800682 ctx.WalkDeps(func(module android.Module, parent android.Module) bool {
Colin Crossa4f08812018-10-02 22:03:40 -0700683 otherName := ctx.OtherModuleName(module)
684 tag := ctx.OtherModuleDependencyTag(module)
685
Peter Collingbournead84f972019-12-17 16:46:18 -0800686 if IsJniDepTag(tag) || tag == cc.SharedDepTag {
Colin Crossa4f08812018-10-02 22:03:40 -0700687 if dep, ok := module.(*cc.Module); ok {
Peter Collingbournead84f972019-12-17 16:46:18 -0800688 if dep.IsNdk() || dep.IsStubs() {
689 return false
690 }
691
Colin Crossa4f08812018-10-02 22:03:40 -0700692 lib := dep.OutputFile()
Peter Collingbournead84f972019-12-17 16:46:18 -0800693 path := lib.Path()
694 if seenModulePaths[path.String()] {
695 return false
696 }
697 seenModulePaths[path.String()] = true
698
Colin Crossc2d24052020-05-13 11:05:02 -0700699 if checkNativeSdkVersion && dep.SdkVersion() == "" {
700 ctx.PropertyErrorf("jni_libs", "JNI dependency %q uses platform APIs, but this module does not",
701 otherName)
Colin Cross094cde42020-02-15 10:38:00 -0800702 }
703
Colin Crossa4f08812018-10-02 22:03:40 -0700704 if lib.Valid() {
705 jniLibs = append(jniLibs, jniLib{
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700706 name: ctx.OtherModuleName(module),
707 path: path,
708 target: module.Target(),
709 coverageFile: dep.CoverageOutputFile(),
Colin Crossa4f08812018-10-02 22:03:40 -0700710 })
711 } else {
712 ctx.ModuleErrorf("dependency %q missing output file", otherName)
713 }
714 } else {
715 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
Colin Crossa4f08812018-10-02 22:03:40 -0700716 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800717
718 return shouldCollectRecursiveNativeDeps
719 }
720
721 if tag == certificateTag {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700722 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900723 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700724 } else {
725 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
726 }
Colin Crossa4f08812018-10-02 22:03:40 -0700727 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800728
729 return false
Colin Crossa4f08812018-10-02 22:03:40 -0700730 })
731
Colin Crossbd01e2a2018-10-04 15:21:03 -0700732 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700733}
734
Artur Satayev1111b842020-04-27 19:05:28 +0100735func (a *AndroidApp) walkPayloadDeps(ctx android.ModuleContext,
736 do func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool)) {
737
738 ctx.WalkDeps(func(child, parent android.Module) bool {
739 isExternal := !a.DepIsInSameApex(ctx, child)
740 if am, ok := child.(android.ApexModule); ok {
741 do(ctx, parent, am, isExternal)
742 }
743 return !isExternal
744 })
745}
746
747func (a *AndroidApp) buildAppDependencyInfo(ctx android.ModuleContext) {
748 if ctx.Host() {
749 return
750 }
751
752 depsInfo := android.DepNameToDepInfoMap{}
753 a.walkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) {
754 depName := to.Name()
755 if info, exist := depsInfo[depName]; exist {
756 info.From = append(info.From, from.Name())
757 info.IsExternal = info.IsExternal && externalDep
758 depsInfo[depName] = info
759 } else {
760 toMinSdkVersion := "(no version)"
761 if m, ok := to.(interface{ MinSdkVersion() string }); ok {
762 if v := m.MinSdkVersion(); v != "" {
763 toMinSdkVersion = v
764 }
765 }
766 depsInfo[depName] = android.ApexModuleDepInfo{
767 To: depName,
768 From: []string{from.Name()},
769 IsExternal: externalDep,
770 MinSdkVersion: toMinSdkVersion,
771 }
772 }
773 })
774
775 a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, a.MinSdkVersion(), depsInfo)
776}
777
Colin Cross0ea8ba82019-06-06 14:33:29 -0700778func (a *AndroidApp) getCertString(ctx android.BaseModuleContext) string {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800779 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
780 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000781 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800782 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800783 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800784}
785
Jiyong Park0f80c182020-01-31 02:49:53 +0900786func (a *AndroidApp) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
787 if IsJniDepTag(ctx.OtherModuleDependencyTag(dep)) {
788 return true
789 }
790 return a.Library.DepIsInSameApex(ctx, dep)
791}
792
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900793// For OutputFileProducer interface
794func (a *AndroidApp) OutputFiles(tag string) (android.Paths, error) {
795 switch tag {
796 case ".aapt.srcjar":
797 return []android.Path{a.aaptSrcJar}, nil
798 }
799 return a.Library.OutputFiles(tag)
800}
801
Jiyong Parkf7487312019-10-17 12:54:30 +0900802func (a *AndroidApp) Privileged() bool {
803 return Bool(a.appProperties.Privileged)
804}
805
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700806func (a *AndroidApp) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
807 return ctx.Device() && (ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled())
808}
809
810func (a *AndroidApp) PreventInstall() {
811 a.appProperties.PreventInstall = true
812}
813
814func (a *AndroidApp) HideFromMake() {
815 a.appProperties.HideFromMake = true
816}
817
818func (a *AndroidApp) MarkAsCoverageVariant(coverage bool) {
819 a.appProperties.IsCoverageVariant = coverage
820}
821
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400822func (a *AndroidApp) EnableCoverageIfNeeded() {}
823
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700824var _ cc.Coverage = (*AndroidApp)(nil)
825
Colin Cross1b16b0e2019-02-12 14:41:32 -0800826// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700827func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700828 module := &AndroidApp{}
829
Sasha Smundak2057f822019-04-16 17:16:58 -0700830 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross66dbc0b2017-12-28 12:23:20 -0800831 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
832
Colin Crossae5caf52018-05-22 11:11:52 -0700833 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700834 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700835
Colin Cross36242852017-06-23 15:06:31 -0700836 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700837 &module.Module.properties,
838 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800839 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700840 &module.Module.protoProperties,
841 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800842 &module.appProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700843 &module.overridableAppProperties,
844 &module.usesLibrary.usesLibraryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700845
Colin Crossa9d8bee2018-10-02 13:59:46 -0700846 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
847 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
848 })
849
Colin Crossa4f08812018-10-02 22:03:40 -0700850 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
851 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800852 android.InitOverridableModule(module, &module.appProperties.Overrides)
Jiyong Park52cd06f2019-11-11 10:14:32 +0900853 android.InitApexModule(module)
Colin Crossa4f08812018-10-02 22:03:40 -0700854
Colin Cross36242852017-06-23 15:06:31 -0700855 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700856}
Colin Crossae5caf52018-05-22 11:11:52 -0700857
858type appTestProperties struct {
Liz Kammer6b0c5522020-04-28 16:10:55 -0700859 // The name of the android_app module that the tests will run against.
Colin Crossae5caf52018-05-22 11:11:52 -0700860 Instrumentation_for *string
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700861
862 // if specified, the instrumentation target package name in the manifest is overwritten by it.
863 Instrumentation_target_package *string
Colin Crossae5caf52018-05-22 11:11:52 -0700864}
865
866type AndroidTest struct {
867 AndroidApp
868
869 appTestProperties appTestProperties
870
871 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700872
873 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700874 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700875}
876
Jaewoong Jung0949f312019-09-11 10:25:18 -0700877func (a *AndroidTest) InstallInTestcases() bool {
878 return true
879}
880
Colin Crossae5caf52018-05-22 11:11:52 -0700881func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700882 if a.appTestProperties.Instrumentation_target_package != nil {
883 a.additionalAaptFlags = append(a.additionalAaptFlags,
884 "--rename-instrumentation-target-package "+*a.appTestProperties.Instrumentation_target_package)
885 } else if a.appTestProperties.Instrumentation_for != nil {
886 // Check if the instrumentation target package is overridden.
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800887 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
888 if overridden {
889 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
890 }
891 }
Colin Crossae5caf52018-05-22 11:11:52 -0700892 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700893
Jaewoong Jung39982342020-01-14 10:27:18 -0800894 testConfig := tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700895 a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites, a.testProperties.Auto_gen_config)
Jaewoong Jung39982342020-01-14 10:27:18 -0800896 a.testConfig = a.FixTestConfig(ctx, testConfig)
Colin Cross8a497952019-03-05 22:25:09 -0800897 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700898}
899
Jaewoong Jung39982342020-01-14 10:27:18 -0800900func (a *AndroidTest) FixTestConfig(ctx android.ModuleContext, testConfig android.Path) android.Path {
901 if testConfig == nil {
902 return nil
903 }
904
905 fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", "AndroidTest.xml")
906 rule := android.NewRuleBuilder()
907 command := rule.Command().BuiltTool(ctx, "test_config_fixer").Input(testConfig).Output(fixedConfig)
908 fixNeeded := false
909
910 if ctx.ModuleName() != a.installApkName {
911 fixNeeded = true
912 command.FlagWithArg("--test-file-name ", a.installApkName+".apk")
913 }
914
915 if a.overridableAppProperties.Package_name != nil {
916 fixNeeded = true
917 command.FlagWithInput("--manifest ", a.manifestPath).
918 FlagWithArg("--package-name ", *a.overridableAppProperties.Package_name)
919 }
920
921 if fixNeeded {
922 rule.Build(pctx, ctx, "fix_test_config", "fix test config")
923 return fixedConfig
924 }
925 return testConfig
926}
927
Colin Cross303e21f2018-08-07 16:49:25 -0700928func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -0700929 a.AndroidApp.DepsMutator(ctx)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700930}
931
932func (a *AndroidTest) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
933 a.AndroidApp.OverridablePropertiesDepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -0700934 if a.appTestProperties.Instrumentation_for != nil {
935 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
936 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
937 // use instrumentationForTag instead of libTag.
938 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
939 }
Colin Crossae5caf52018-05-22 11:11:52 -0700940}
941
Colin Cross1b16b0e2019-02-12 14:41:32 -0800942// android_test compiles test sources and Android resources into an Android application package `.apk` file and
943// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -0700944func AndroidTestFactory() android.Module {
945 module := &AndroidTest{}
946
Sasha Smundak2057f822019-04-16 17:16:58 -0700947 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross5067db92018-09-17 16:46:35 -0700948
949 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700950 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800951 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700952 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800953 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -0700954
955 module.AddProperties(
956 &module.Module.properties,
957 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800958 &module.Module.dexpreoptProperties,
Colin Crossae5caf52018-05-22 11:11:52 -0700959 &module.Module.protoProperties,
960 &module.aaptProperties,
961 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700962 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800963 &module.overridableAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700964 &module.usesLibrary.usesLibraryProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700965 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700966
Colin Crossa4f08812018-10-02 22:03:40 -0700967 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
968 android.InitDefaultableModule(module)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700969 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossae5caf52018-05-22 11:11:52 -0700970 return module
971}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700972
Colin Cross252fc6f2018-10-04 15:22:03 -0700973type appTestHelperAppProperties struct {
974 // list of compatibility suites (for example "cts", "vts") that the module should be
975 // installed into.
976 Test_suites []string `android:"arch_variant"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700977
978 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
979 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
980 // explicitly.
981 Auto_gen_config *bool
Colin Cross252fc6f2018-10-04 15:22:03 -0700982}
983
984type AndroidTestHelperApp struct {
985 AndroidApp
986
987 appTestHelperAppProperties appTestHelperAppProperties
988}
989
Jaewoong Jung326a9412019-11-21 10:41:00 -0800990func (a *AndroidTestHelperApp) InstallInTestcases() bool {
991 return true
992}
993
Colin Cross1b16b0e2019-02-12 14:41:32 -0800994// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
995// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
996// test.
Colin Cross252fc6f2018-10-04 15:22:03 -0700997func AndroidTestHelperAppFactory() android.Module {
998 module := &AndroidTestHelperApp{}
999
Sasha Smundak2057f822019-04-16 17:16:58 -07001000 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross252fc6f2018-10-04 15:22:03 -07001001
1002 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -08001003 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -07001004 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -08001005 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -07001006
1007 module.AddProperties(
1008 &module.Module.properties,
1009 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001010 &module.Module.dexpreoptProperties,
Colin Cross252fc6f2018-10-04 15:22:03 -07001011 &module.Module.protoProperties,
1012 &module.aaptProperties,
1013 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -08001014 &module.appTestHelperAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -07001015 &module.overridableAppProperties,
1016 &module.usesLibrary.usesLibraryProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -07001017
1018 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1019 android.InitDefaultableModule(module)
Anton Hansson3d2b6b42020-01-10 15:06:01 +00001020 android.InitApexModule(module)
Colin Cross252fc6f2018-10-04 15:22:03 -07001021 return module
1022}
1023
Colin Crossbd01e2a2018-10-04 15:21:03 -07001024type AndroidAppCertificate struct {
1025 android.ModuleBase
1026 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001027 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -07001028}
1029
1030type AndroidAppCertificateProperties struct {
1031 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
1032 Certificate *string
1033}
1034
Colin Cross1b16b0e2019-02-12 14:41:32 -08001035// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
1036// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -07001037func AndroidAppCertificateFactory() android.Module {
1038 module := &AndroidAppCertificate{}
1039 module.AddProperties(&module.properties)
1040 android.InitAndroidModule(module)
1041 return module
1042}
1043
Colin Crossbd01e2a2018-10-04 15:21:03 -07001044func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1045 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +09001046 c.Certificate = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -08001047 Pem: android.PathForModuleSrc(ctx, cert+".x509.pem"),
1048 Key: android.PathForModuleSrc(ctx, cert+".pk8"),
Colin Crossbd01e2a2018-10-04 15:21:03 -07001049 }
1050}
Jaewoong Jung525443a2019-02-28 15:35:54 -08001051
1052type OverrideAndroidApp struct {
1053 android.ModuleBase
1054 android.OverrideModuleBase
1055}
1056
1057func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1058 // All the overrides happen in the base module.
1059 // TODO(jungjw): Check the base module type.
1060}
1061
1062// override_android_app is used to create an android_app module based on another android_app by overriding
1063// some of its properties.
1064func OverrideAndroidAppModuleFactory() android.Module {
1065 m := &OverrideAndroidApp{}
1066 m.AddProperties(&overridableAppProperties{})
1067
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001068 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung525443a2019-02-28 15:35:54 -08001069 android.InitOverrideModule(m)
1070 return m
1071}
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001072
Jaewoong Jung26dedd32019-06-06 08:45:58 -07001073type OverrideAndroidTest struct {
1074 android.ModuleBase
1075 android.OverrideModuleBase
1076}
1077
1078func (i *OverrideAndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1079 // All the overrides happen in the base module.
1080 // TODO(jungjw): Check the base module type.
1081}
1082
1083// override_android_test is used to create an android_app module based on another android_test by overriding
1084// some of its properties.
1085func OverrideAndroidTestModuleFactory() android.Module {
1086 m := &OverrideAndroidTest{}
1087 m.AddProperties(&overridableAppProperties{})
1088 m.AddProperties(&appTestProperties{})
1089
1090 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
1091 android.InitOverrideModule(m)
1092 return m
1093}
1094
Roshan Pius4df2bc72020-04-27 09:42:27 -07001095type OverrideRuntimeResourceOverlay struct {
1096 android.ModuleBase
1097 android.OverrideModuleBase
1098}
1099
1100func (i *OverrideRuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1101 // All the overrides happen in the base module.
1102 // TODO(jungjw): Check the base module type.
1103}
1104
1105// override_runtime_resource_overlay is used to create a module based on another
1106// runtime_resource_overlay module by overriding some of its properties.
1107func OverrideRuntimeResourceOverlayModuleFactory() android.Module {
1108 m := &OverrideRuntimeResourceOverlay{}
1109 m.AddProperties(&OverridableRuntimeResourceOverlayProperties{})
1110
1111 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
1112 android.InitOverrideModule(m)
1113 return m
1114}
1115
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001116type AndroidAppImport struct {
1117 android.ModuleBase
1118 android.DefaultableModuleBase
Jiyong Park592a6a42020-04-21 22:34:28 +09001119 android.ApexModuleBase
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001120 prebuilt android.Prebuilt
1121
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001122 properties AndroidAppImportProperties
1123 dpiVariants interface{}
1124 archVariants interface{}
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001125
1126 outputFile android.Path
Colin Cross503c1d02020-01-28 14:00:53 -08001127 certificate Certificate
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001128
1129 dexpreopter
Colin Cross50ddcc42019-05-16 12:28:22 -07001130
1131 usesLibrary usesLibrary
Jaewoong Jung8aae22e2019-07-17 10:21:49 -07001132
Colin Cross70dda7e2019-10-01 22:05:35 -07001133 installPath android.InstallPath
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001134}
1135
1136type AndroidAppImportProperties struct {
1137 // A prebuilt apk to import
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001138 Apk *string
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001139
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001140 // The name of a certificate in the default certificate directory or an android_app_certificate
1141 // module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001142 Certificate *string
1143
1144 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
1145 // be set for presigned modules.
1146 Presigned *bool
1147
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001148 // Sign with the default system dev certificate. Must be used judiciously. Most imported apps
1149 // need to either specify a specific certificate or be presigned.
1150 Default_dev_cert *bool
1151
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001152 // Specifies that this app should be installed to the priv-app directory,
1153 // where the system will grant it additional privileges not available to
1154 // normal apps.
1155 Privileged *bool
1156
1157 // Names of modules to be overridden. Listed modules can only be other binaries
1158 // (in Make or Soong).
1159 // This does not completely prevent installation of the overridden binaries, but if both
1160 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
1161 // from PRODUCT_PACKAGES.
1162 Overrides []string
Jaewoong Jung8aae22e2019-07-17 10:21:49 -07001163
1164 // Optional name for the installed app. If unspecified, it is derived from the module name.
1165 Filename *string
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001166}
1167
Martin Stjernholm6d415272020-01-31 17:10:36 +00001168func (a *AndroidAppImport) IsInstallable() bool {
1169 return true
1170}
1171
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001172// Updates properties with variant-specific values.
1173func (a *AndroidAppImport) processVariants(ctx android.LoadHookContext) {
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001174 config := ctx.Config()
1175
1176 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants")
1177 // Try DPI variant matches in the reverse-priority order so that the highest priority match
1178 // overwrites everything else.
1179 // TODO(jungjw): Can we optimize this by making it priority order?
1180 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001181 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001182 }
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001183 if config.ProductAAPTPreferredConfig() != "" {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001184 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001185 }
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001186
1187 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName("Arch")
1188 archType := ctx.Config().Targets[android.Android][0].Arch.ArchType
1189 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001190}
1191
Colin Cross1184b642019-12-30 18:43:07 -08001192func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001193 dst interface{}, variantGroup reflect.Value, variant string) {
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001194 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
1195 if !src.IsValid() {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001196 return
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001197 }
1198
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001199 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
1200 if err != nil {
1201 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1202 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1203 } else {
1204 panic(err)
1205 }
1206 }
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001207}
1208
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001209func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
1210 cert := android.SrcIsModule(String(a.properties.Certificate))
1211 if cert != "" {
1212 ctx.AddDependency(ctx.Module(), certificateTag, cert)
1213 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001214
Paul Duffin250e6192019-06-07 10:44:37 +01001215 a.usesLibrary.deps(ctx, true)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001216}
1217
1218func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
1219 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001220 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
1221 // with them may invalidate pre-existing signature data.
1222 if ctx.InstallInTestcases() && Bool(a.properties.Presigned) {
1223 ctx.Build(pctx, android.BuildParams{
1224 Rule: android.Cp,
1225 Output: outputPath,
1226 Input: inputPath,
1227 })
1228 return
1229 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001230 rule := android.NewRuleBuilder()
1231 rule.Command().
1232 Textf(`if (zipinfo %s 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
Colin Crossee94d6a2019-07-08 17:08:34 -07001233 BuiltTool(ctx, "zip2zip").
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001234 FlagWithInput("-i ", inputPath).
1235 FlagWithOutput("-o ", outputPath).
1236 FlagWithArg("-0 ", "'lib/**/*.so'").
1237 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
1238 rule.Build(pctx, ctx, "uncompress-embedded-jni-libs", "Uncompress embedded JIN libs")
1239}
1240
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001241// Returns whether this module should have the dex file stored uncompressed in the APK.
1242func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
1243 if ctx.Config().UnbundledBuild() {
1244 return false
1245 }
1246
1247 // Uncompress dex in APKs of privileged apps
Jiyong Parkf7487312019-10-17 12:54:30 +09001248 if ctx.Config().UncompressPrivAppDex() && a.Privileged() {
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001249 return true
1250 }
1251
1252 return shouldUncompressDex(ctx, &a.dexpreopter)
1253}
1254
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001255func (a *AndroidAppImport) uncompressDex(
1256 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
1257 rule := android.NewRuleBuilder()
1258 rule.Command().
1259 Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
Colin Crossee94d6a2019-07-08 17:08:34 -07001260 BuiltTool(ctx, "zip2zip").
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001261 FlagWithInput("-i ", inputPath).
1262 FlagWithOutput("-o ", outputPath).
1263 FlagWithArg("-0 ", "'classes*.dex'").
1264 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
1265 rule.Build(pctx, ctx, "uncompress-dex", "Uncompress dex files")
1266}
1267
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001268func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001269 a.generateAndroidBuildActions(ctx)
1270}
1271
Jooyung Han39ee1192020-03-23 20:21:11 +09001272func (a *AndroidAppImport) InstallApkName() string {
1273 return a.BaseModuleName()
1274}
1275
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001276func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001277 numCertPropsSet := 0
1278 if String(a.properties.Certificate) != "" {
1279 numCertPropsSet++
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001280 }
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001281 if Bool(a.properties.Presigned) {
1282 numCertPropsSet++
1283 }
1284 if Bool(a.properties.Default_dev_cert) {
1285 numCertPropsSet++
1286 }
1287 if numCertPropsSet != 1 {
1288 ctx.ModuleErrorf("One and only one of certficate, presigned, and default_dev_cert properties must be set")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001289 }
1290
Colin Crossc2d24052020-05-13 11:05:02 -07001291 _, certificates := collectAppDeps(ctx, a, false, false)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001292
1293 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001294 // TODO: LOCAL_PACKAGE_SPLITS
1295
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001296 srcApk := a.prebuilt.SingleSourcePath(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -07001297
1298 if a.usesLibrary.enforceUsesLibraries() {
1299 srcApk = a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk)
1300 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001301
1302 // TODO: Install or embed JNI libraries
1303
1304 // Uncompress JNI libraries in the apk
1305 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
1306 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
1307
Kyeongkab.Namc4997142019-11-22 11:38:16 +09001308 var installDir android.InstallPath
1309 if Bool(a.properties.Privileged) {
1310 installDir = android.PathForModuleInstall(ctx, "priv-app", a.BaseModuleName())
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001311 } else if ctx.InstallInTestcases() {
1312 installDir = android.PathForModuleInstall(ctx, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch())
Kyeongkab.Namc4997142019-11-22 11:38:16 +09001313 } else {
1314 installDir = android.PathForModuleInstall(ctx, "app", a.BaseModuleName())
1315 }
1316
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001317 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001318 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001319 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -07001320
1321 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
1322 a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
1323 a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
1324 a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
1325
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001326 dexOutput := a.dexpreopter.dexpreopt(ctx, jnisUncompressed)
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001327 if a.dexpreopter.uncompressedDex {
1328 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
1329 a.uncompressDex(ctx, dexOutput, dexUncompressed.OutputPath)
1330 dexOutput = dexUncompressed
1331 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001332
Jooyung Han39ee1192020-03-23 20:21:11 +09001333 apkFilename := proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk")
1334
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001335 // Sign or align the package
1336 // TODO: Handle EXTERNAL
1337 if !Bool(a.properties.Presigned) {
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001338 // If the certificate property is empty at this point, default_dev_cert must be set to true.
1339 // Which makes processMainCert's behavior for the empty cert string WAI.
1340 certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001341 if len(certificates) != 1 {
1342 ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates)
1343 }
Colin Cross503c1d02020-01-28 14:00:53 -08001344 a.certificate = certificates[0]
Jooyung Han39ee1192020-03-23 20:21:11 +09001345 signed := android.PathForModuleOut(ctx, "signed", apkFilename)
Liz Kammere2b27f42020-05-07 13:24:05 -07001346 SignAppPackage(ctx, signed, dexOutput, certificates, nil)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001347 a.outputFile = signed
1348 } else {
Jooyung Han39ee1192020-03-23 20:21:11 +09001349 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001350 TransformZipAlign(ctx, alignedApk, dexOutput)
1351 a.outputFile = alignedApk
Colin Cross503c1d02020-01-28 14:00:53 -08001352 a.certificate = presignedCertificate
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001353 }
1354
1355 // TODO: Optionally compress the output apk.
1356
Jiyong Park592a6a42020-04-21 22:34:28 +09001357 if a.IsForPlatform() {
1358 a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
1359 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001360
1361 // TODO: androidmk converter jni libs
1362}
1363
1364func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
1365 return &a.prebuilt
1366}
1367
1368func (a *AndroidAppImport) Name() string {
1369 return a.prebuilt.Name(a.ModuleBase.Name())
1370}
1371
Dario Frenicde2a032019-10-27 00:29:22 +01001372func (a *AndroidAppImport) OutputFile() android.Path {
1373 return a.outputFile
1374}
1375
Jiyong Park618922e2020-01-08 13:35:43 +09001376func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
1377 return nil
1378}
1379
Colin Cross503c1d02020-01-28 14:00:53 -08001380func (a *AndroidAppImport) Certificate() Certificate {
1381 return a.certificate
1382}
1383
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001384var dpiVariantGroupType reflect.Type
1385var archVariantGroupType reflect.Type
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001386
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001387func initAndroidAppImportVariantGroupTypes() {
1388 dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants")
1389
1390 archNames := make([]string, len(android.ArchTypeList()))
1391 for i, archType := range android.ArchTypeList() {
1392 archNames[i] = archType.Name
1393 }
1394 archVariantGroupType = createVariantGroupType(archNames, "Arch")
1395}
1396
1397// Populates all variant struct properties at creation time.
1398func (a *AndroidAppImport) populateAllVariantStructs() {
1399 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
1400 a.AddProperties(a.dpiVariants)
1401
1402 a.archVariants = reflect.New(archVariantGroupType).Interface()
1403 a.AddProperties(a.archVariants)
1404}
1405
Jiyong Parkf7487312019-10-17 12:54:30 +09001406func (a *AndroidAppImport) Privileged() bool {
1407 return Bool(a.properties.Privileged)
1408}
1409
Jiyong Park592a6a42020-04-21 22:34:28 +09001410func (a *AndroidAppImport) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1411 // android_app_import might have extra dependencies via uses_libs property.
1412 // Don't track the dependency as we don't automatically add those libraries
1413 // to the classpath. It should be explicitly added to java_libs property of APEX
1414 return false
1415}
1416
Colin Crossc2d24052020-05-13 11:05:02 -07001417func (a *AndroidAppImport) sdkVersion() sdkSpec {
1418 return sdkSpecFrom("")
1419}
1420
1421func (a *AndroidAppImport) minSdkVersion() sdkSpec {
1422 return sdkSpecFrom("")
1423}
1424
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001425func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
1426 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
1427
1428 variantFields := make([]reflect.StructField, len(variants))
1429 for i, variant := range variants {
1430 variantFields[i] = reflect.StructField{
1431 Name: proptools.FieldNameForProperty(variant),
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001432 Type: props,
1433 }
1434 }
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001435
1436 variantGroupStruct := reflect.StructOf(variantFields)
1437 return reflect.StructOf([]reflect.StructField{
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001438 {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001439 Name: variantGroupName,
1440 Type: variantGroupStruct,
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001441 },
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001442 })
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001443}
1444
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001445// android_app_import imports a prebuilt apk with additional processing specified in the module.
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001446// DPI-specific apk source files can be specified using dpi_variants. Example:
1447//
1448// android_app_import {
1449// name: "example_import",
1450// apk: "prebuilts/example.apk",
1451// dpi_variants: {
1452// mdpi: {
1453// apk: "prebuilts/example_mdpi.apk",
1454// },
1455// xhdpi: {
1456// apk: "prebuilts/example_xhdpi.apk",
1457// },
1458// },
1459// certificate: "PRESIGNED",
1460// }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001461func AndroidAppImportFactory() android.Module {
1462 module := &AndroidAppImport{}
1463 module.AddProperties(&module.properties)
1464 module.AddProperties(&module.dexpreoptProperties)
Colin Cross50ddcc42019-05-16 12:28:22 -07001465 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001466 module.populateAllVariantStructs()
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001467 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001468 module.processVariants(ctx)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001469 })
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001470
Jiyong Park592a6a42020-04-21 22:34:28 +09001471 android.InitApexModule(module)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001472 InitJavaModule(module, android.DeviceSupported)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001473 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001474
1475 return module
1476}
Colin Cross50ddcc42019-05-16 12:28:22 -07001477
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001478type AndroidTestImport struct {
1479 AndroidAppImport
1480
1481 testProperties testProperties
1482
1483 data android.Paths
1484}
1485
1486func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1487 a.generateAndroidBuildActions(ctx)
1488
1489 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
1490}
1491
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001492func (a *AndroidTestImport) InstallInTestcases() bool {
1493 return true
1494}
1495
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001496// android_test_import imports a prebuilt test apk with additional processing specified in the
1497// module. DPI or arch variant configurations can be made as with android_app_import.
1498func AndroidTestImportFactory() android.Module {
1499 module := &AndroidTestImport{}
1500 module.AddProperties(&module.properties)
1501 module.AddProperties(&module.dexpreoptProperties)
1502 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
1503 module.AddProperties(&module.testProperties)
1504 module.populateAllVariantStructs()
1505 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
1506 module.processVariants(ctx)
1507 })
1508
Colin Crossc80828d2020-05-06 22:29:10 -07001509 module.dexpreopter.isTest = true
1510
Jiyong Park592a6a42020-04-21 22:34:28 +09001511 android.InitApexModule(module)
Jaewoong Jung243688e2020-05-01 15:50:08 -07001512 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1513 android.InitDefaultableModule(module)
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001514 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
1515
1516 return module
1517}
1518
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001519type RuntimeResourceOverlay struct {
1520 android.ModuleBase
1521 android.DefaultableModuleBase
Roshan Pius4df2bc72020-04-27 09:42:27 -07001522 android.OverridableModuleBase
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001523 aapt
1524
Roshan Pius4df2bc72020-04-27 09:42:27 -07001525 properties RuntimeResourceOverlayProperties
1526 overridableProperties OverridableRuntimeResourceOverlayProperties
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001527
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08001528 certificate Certificate
1529
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001530 outputFile android.Path
1531 installDir android.InstallPath
1532}
1533
1534type RuntimeResourceOverlayProperties struct {
1535 // the name of a certificate in the default certificate directory or an android_app_certificate
1536 // module name in the form ":module".
1537 Certificate *string
1538
1539 // optional theme name. If specified, the overlay package will be applied
1540 // only when the ro.boot.vendor.overlay.theme system property is set to the same value.
1541 Theme *string
1542
1543 // if not blank, set to the version of the sdk to compile against.
1544 // Defaults to compiling against the current platform.
1545 Sdk_version *string
1546
1547 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
1548 // Defaults to sdk_version if not set.
1549 Min_sdk_version *string
Jaewoong Jungfe3c7f62020-04-09 16:15:30 -07001550
1551 // list of android_library modules whose resources are extracted and linked against statically
1552 Static_libs []string
1553
1554 // list of android_app modules whose resources are extracted and linked against
1555 Resource_libs []string
Jaewoong Jungad0177b2020-04-24 15:22:40 -07001556
1557 // Names of modules to be overridden. Listed modules can only be other overlays
1558 // (in Make or Soong).
1559 // This does not completely prevent installation of the overridden overlays, but if both
1560 // overlays would be installed by default (in PRODUCT_PACKAGES) the other overlay will be removed
1561 // from PRODUCT_PACKAGES.
1562 Overrides []string
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001563}
1564
Jiyong Park69aeba92020-04-24 21:16:36 +09001565// RuntimeResourceOverlayModule interface is used by the apex package to gather information from
1566// a RuntimeResourceOverlay module.
1567type RuntimeResourceOverlayModule interface {
1568 android.Module
1569 OutputFile() android.Path
1570 Certificate() Certificate
1571 Theme() string
1572}
1573
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001574func (r *RuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
1575 sdkDep := decodeSdkDep(ctx, sdkContext(r))
1576 if sdkDep.hasFrameworkLibs() {
1577 r.aapt.deps(ctx, sdkDep)
1578 }
1579
1580 cert := android.SrcIsModule(String(r.properties.Certificate))
1581 if cert != "" {
1582 ctx.AddDependency(ctx.Module(), certificateTag, cert)
1583 }
Jaewoong Jungfe3c7f62020-04-09 16:15:30 -07001584
1585 ctx.AddVariationDependencies(nil, staticLibTag, r.properties.Static_libs...)
1586 ctx.AddVariationDependencies(nil, libTag, r.properties.Resource_libs...)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001587}
1588
1589func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1590 // Compile and link resources
1591 r.aapt.hasNoCode = true
Jaewoong Jungf0f747c2020-01-24 10:30:02 -08001592 // Do not remove resources without default values nor dedupe resource configurations with the same value
Roshan Pius4df2bc72020-04-27 09:42:27 -07001593 aaptLinkFlags := []string{"--no-resource-deduping", "--no-resource-removal"}
1594 // Allow the override of "package name" and "overlay target package name"
1595 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
1596 if overridden || r.overridableProperties.Package_name != nil {
1597 // The product override variable has a priority over the package_name property.
1598 if !overridden {
1599 manifestPackageName = *r.overridableProperties.Package_name
1600 }
1601 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
1602 }
1603 if r.overridableProperties.Target_package_name != nil {
1604 aaptLinkFlags = append(aaptLinkFlags,
1605 "--rename-overlay-target-package "+*r.overridableProperties.Target_package_name)
1606 }
1607 r.aapt.buildActions(ctx, r, aaptLinkFlags...)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001608
1609 // Sign the built package
Colin Crossc2d24052020-05-13 11:05:02 -07001610 _, certificates := collectAppDeps(ctx, r, false, false)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001611 certificates = processMainCert(r.ModuleBase, String(r.properties.Certificate), certificates, ctx)
1612 signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
Liz Kammere2b27f42020-05-07 13:24:05 -07001613 SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil)
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08001614 r.certificate = certificates[0]
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001615
1616 r.outputFile = signed
1617 r.installDir = android.PathForModuleInstall(ctx, "overlay", String(r.properties.Theme))
1618 ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile)
1619}
1620
Jiyong Park6a927c42020-01-21 02:03:43 +09001621func (r *RuntimeResourceOverlay) sdkVersion() sdkSpec {
1622 return sdkSpecFrom(String(r.properties.Sdk_version))
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001623}
1624
1625func (r *RuntimeResourceOverlay) systemModules() string {
1626 return ""
1627}
1628
Jiyong Park6a927c42020-01-21 02:03:43 +09001629func (r *RuntimeResourceOverlay) minSdkVersion() sdkSpec {
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001630 if r.properties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +09001631 return sdkSpecFrom(*r.properties.Min_sdk_version)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001632 }
1633 return r.sdkVersion()
1634}
1635
Jiyong Park6a927c42020-01-21 02:03:43 +09001636func (r *RuntimeResourceOverlay) targetSdkVersion() sdkSpec {
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001637 return r.sdkVersion()
1638}
1639
Jiyong Park69aeba92020-04-24 21:16:36 +09001640func (r *RuntimeResourceOverlay) Certificate() Certificate {
1641 return r.certificate
1642}
1643
1644func (r *RuntimeResourceOverlay) OutputFile() android.Path {
1645 return r.outputFile
1646}
1647
1648func (r *RuntimeResourceOverlay) Theme() string {
1649 return String(r.properties.Theme)
1650}
1651
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001652// runtime_resource_overlay generates a resource-only apk file that can overlay application and
1653// system resources at run time.
1654func RuntimeResourceOverlayFactory() android.Module {
1655 module := &RuntimeResourceOverlay{}
1656 module.AddProperties(
1657 &module.properties,
Roshan Pius4df2bc72020-04-27 09:42:27 -07001658 &module.aaptProperties,
1659 &module.overridableProperties)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001660
Roshan Pius4df2bc72020-04-27 09:42:27 -07001661 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
1662 android.InitDefaultableModule(module)
1663 android.InitOverridableModule(module, &module.properties.Overrides)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001664 return module
1665}
1666
Colin Cross50ddcc42019-05-16 12:28:22 -07001667type UsesLibraryProperties struct {
1668 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file.
1669 Uses_libs []string
1670
1671 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file with
1672 // required=false.
1673 Optional_uses_libs []string
1674
1675 // If true, the list of uses_libs and optional_uses_libs modules must match the AndroidManifest.xml file. Defaults
1676 // to true if either uses_libs or optional_uses_libs is set. Will unconditionally default to true in the future.
1677 Enforce_uses_libs *bool
1678}
1679
1680// usesLibrary provides properties and helper functions for AndroidApp and AndroidAppImport to verify that the
1681// <uses-library> tags that end up in the manifest of an APK match the ones known to the build system through the
1682// uses_libs and optional_uses_libs properties. The build system's values are used by dexpreopt to preopt apps
1683// with knowledge of their shared libraries.
1684type usesLibrary struct {
1685 usesLibraryProperties UsesLibraryProperties
1686}
1687
Paul Duffin250e6192019-06-07 10:44:37 +01001688func (u *usesLibrary) deps(ctx android.BottomUpMutatorContext, hasFrameworkLibs bool) {
Colin Cross3245b2c2019-06-07 13:18:09 -07001689 if !ctx.Config().UnbundledBuild() {
1690 ctx.AddVariationDependencies(nil, usesLibTag, u.usesLibraryProperties.Uses_libs...)
1691 ctx.AddVariationDependencies(nil, usesLibTag, u.presentOptionalUsesLibs(ctx)...)
Paul Duffin250e6192019-06-07 10:44:37 +01001692 // Only add these extra dependencies if the module depends on framework libs. This avoids
1693 // creating a cyclic dependency:
1694 // e.g. framework-res -> org.apache.http.legacy -> ... -> framework-res.
1695 if hasFrameworkLibs {
Colin Cross3245b2c2019-06-07 13:18:09 -07001696 // dexpreopt/dexpreopt.go needs the paths to the dex jars of these libraries in case construct_context.sh needs
1697 // to pass them to dex2oat. Add them as a dependency so we can determine the path to the dex jar of each
1698 // library to dexpreopt.
1699 ctx.AddVariationDependencies(nil, usesLibTag,
1700 "org.apache.http.legacy",
1701 "android.hidl.base-V1.0-java",
1702 "android.hidl.manager-V1.0-java")
1703 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001704 }
1705}
1706
1707// presentOptionalUsesLibs returns optional_uses_libs after filtering out MissingUsesLibraries, which don't exist in the
1708// build.
1709func (u *usesLibrary) presentOptionalUsesLibs(ctx android.BaseModuleContext) []string {
1710 optionalUsesLibs, _ := android.FilterList(u.usesLibraryProperties.Optional_uses_libs, ctx.Config().MissingUsesLibraries())
1711 return optionalUsesLibs
1712}
1713
1714// usesLibraryPaths returns a map of module names of shared library dependencies to the paths to their dex jars.
1715func (u *usesLibrary) usesLibraryPaths(ctx android.ModuleContext) map[string]android.Path {
1716 usesLibPaths := make(map[string]android.Path)
1717
1718 if !ctx.Config().UnbundledBuild() {
1719 ctx.VisitDirectDepsWithTag(usesLibTag, func(m android.Module) {
1720 if lib, ok := m.(Dependency); ok {
1721 if dexJar := lib.DexJar(); dexJar != nil {
1722 usesLibPaths[ctx.OtherModuleName(m)] = dexJar
1723 } else {
1724 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must produce a dex jar, does it have installable: true?",
1725 ctx.OtherModuleName(m))
1726 }
1727 } else if ctx.Config().AllowMissingDependencies() {
1728 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(m)})
1729 } else {
1730 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must be a java library",
1731 ctx.OtherModuleName(m))
1732 }
1733 })
1734 }
1735
1736 return usesLibPaths
1737}
1738
1739// enforceUsesLibraries returns true of <uses-library> tags should be checked against uses_libs and optional_uses_libs
1740// properties. Defaults to true if either of uses_libs or optional_uses_libs is specified. Will default to true
1741// unconditionally in the future.
1742func (u *usesLibrary) enforceUsesLibraries() bool {
1743 defaultEnforceUsesLibs := len(u.usesLibraryProperties.Uses_libs) > 0 ||
1744 len(u.usesLibraryProperties.Optional_uses_libs) > 0
1745 return BoolDefault(u.usesLibraryProperties.Enforce_uses_libs, defaultEnforceUsesLibs)
1746}
1747
1748// verifyUsesLibrariesManifest checks the <uses-library> tags in an AndroidManifest.xml against the ones specified
1749// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the manifest.
1750func (u *usesLibrary) verifyUsesLibrariesManifest(ctx android.ModuleContext, manifest android.Path) android.Path {
1751 outputFile := android.PathForModuleOut(ctx, "manifest_check", "AndroidManifest.xml")
1752
1753 rule := android.NewRuleBuilder()
Colin Crossee94d6a2019-07-08 17:08:34 -07001754 cmd := rule.Command().BuiltTool(ctx, "manifest_check").
Colin Cross50ddcc42019-05-16 12:28:22 -07001755 Flag("--enforce-uses-libraries").
1756 Input(manifest).
1757 FlagWithOutput("-o ", outputFile)
1758
1759 for _, lib := range u.usesLibraryProperties.Uses_libs {
1760 cmd.FlagWithArg("--uses-library ", lib)
1761 }
1762
1763 for _, lib := range u.usesLibraryProperties.Optional_uses_libs {
1764 cmd.FlagWithArg("--optional-uses-library ", lib)
1765 }
1766
1767 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1768
1769 return outputFile
1770}
1771
1772// verifyUsesLibrariesAPK checks the <uses-library> tags in the manifest of an APK against the ones specified
1773// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the APK.
1774func (u *usesLibrary) verifyUsesLibrariesAPK(ctx android.ModuleContext, apk android.Path) android.Path {
1775 outputFile := android.PathForModuleOut(ctx, "verify_uses_libraries", apk.Base())
1776
1777 rule := android.NewRuleBuilder()
1778 aapt := ctx.Config().HostToolPath(ctx, "aapt")
1779 rule.Command().
1780 Textf("aapt_binary=%s", aapt.String()).Implicit(aapt).
1781 Textf(`uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Uses_libs, " ")).
1782 Textf(`optional_uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Optional_uses_libs, " ")).
1783 Tool(android.PathForSource(ctx, "build/make/core/verify_uses_libraries.sh")).Input(apk)
1784 rule.Command().Text("cp -f").Input(apk).Output(outputFile)
1785
1786 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1787
1788 return outputFile
1789}