blob: 02f3e7fc05e5871972d114ef1d89d7fef1de259b [file] [log] [blame]
Colin Cross30e076a2015-04-13 13:58:27 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Android apps.
18
19import (
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070020 "path/filepath"
21 "reflect"
Jaewoong Jung5b425e22019-06-17 17:40:56 -070022 "sort"
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070023 "strings"
Colin Cross30e076a2015-04-13 13:58:27 -070024
Colin Cross50ddcc42019-05-16 12:28:22 -070025 "github.com/google/blueprint"
26 "github.com/google/blueprint/proptools"
27
Colin Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Colin Crossa4f08812018-10-02 22:03:40 -070029 "android/soong/cc"
Colin Cross303e21f2018-08-07 16:49:25 -070030 "android/soong/tradefed"
Colin Cross30e076a2015-04-13 13:58:27 -070031)
32
Jaewoong Jung3e18b192019-06-11 12:25:34 -070033var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070034
Colin Cross3bc7ffa2017-11-22 16:19:37 -080035func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000036 RegisterAppBuildComponents(android.InitRegistrationContext)
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -070037
38 initAndroidAppImportVariantGroupTypes()
Colin Cross3bc7ffa2017-11-22 16:19:37 -080039}
40
Paul Duffinf9b1da02019-12-18 19:51:55 +000041func RegisterAppBuildComponents(ctx android.RegistrationContext) {
42 ctx.RegisterModuleType("android_app", AndroidAppFactory)
43 ctx.RegisterModuleType("android_test", AndroidTestFactory)
44 ctx.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
45 ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
46 ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
47 ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory)
48 ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory)
49 ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -080050 ctx.RegisterModuleType("runtime_resource_overlay", RuntimeResourceOverlayFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000051}
52
Colin Cross30e076a2015-04-13 13:58:27 -070053// AndroidManifest.xml merging
54// package splits
55
Colin Crossfabb6082018-02-20 17:22:23 -080056type appProperties struct {
Colin Crossbd01e2a2018-10-04 15:21:03 -070057 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
Colin Cross7d5136f2015-05-11 13:39:40 -070058 Additional_certificates []string
59
60 // If set, create package-export.apk, which other packages can
61 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
Nan Zhangea568a42017-11-08 21:20:04 -080062 Export_package_resources *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070063
Colin Cross16056062017-12-13 22:46:28 -080064 // Specifies that this app should be installed to the priv-app directory,
65 // where the system will grant it additional privileges not available to
66 // normal apps.
67 Privileged *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070068
69 // list of resource labels to generate individual resource packages
70 Package_splits []string
Jason Monkd4122be2018-08-10 09:33:36 -040071
72 // Names of modules to be overridden. Listed modules can only be other binaries
73 // (in Make or Soong).
74 // This does not completely prevent installation of the overridden binaries, but if both
75 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
76 // from PRODUCT_PACKAGES.
77 Overrides []string
Colin Crossa4f08812018-10-02 22:03:40 -070078
79 // list of native libraries that will be provided in or alongside the resulting jar
80 Jni_libs []string `android:"arch_variant"`
81
Jaewoong Jungbc625cd2019-05-06 15:48:44 -070082 // STL library to use for JNI libraries.
83 Stl *string `android:"arch_variant"`
84
Colin Crosse4246ab2019-02-05 21:55:21 -080085 // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest
86 // 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 +090087 // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to true for
88 // android_app modules that are embedded to APEXes, defaults to false for other module types where the native
89 // libraries are generally preinstalled outside the APK.
Colin Crosse4246ab2019-02-05 21:55:21 -080090 Use_embedded_native_libs *bool
Colin Cross46abdad2019-02-07 13:07:08 -080091
92 // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that
93 // they are used from inside the APK at runtime.
94 Use_embedded_dex *bool
Colin Cross47fa9d32019-03-26 10:51:39 -070095
96 // Forces native libraries to always be packaged into the APK,
97 // Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed.
98 // True for android_test* modules.
99 AlwaysPackageNativeLibs bool `blueprint:"mutated"`
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700100
101 // If set, find and merge all NOTICE files that this module and its dependencies have and store
102 // it in the APK as an asset.
103 Embed_notices *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700104}
105
Jaewoong Jung525443a2019-02-28 15:35:54 -0800106// android_app properties that can be overridden by override_android_app
107type overridableAppProperties struct {
108 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
109 // or an android_app_certificate module name in the form ":module".
110 Certificate *string
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700111
112 // the package name of this app. The package name in the manifest file is used if one was not given.
113 Package_name *string
Jaewoong Jung525443a2019-02-28 15:35:54 -0800114}
115
Colin Cross30e076a2015-04-13 13:58:27 -0700116type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -0700117 Library
118 aapt
Jaewoong Jung525443a2019-02-28 15:35:54 -0800119 android.OverridableModuleBase
Colin Crossa97c5d32018-03-28 14:58:31 -0700120
Colin Cross50ddcc42019-05-16 12:28:22 -0700121 usesLibrary usesLibrary
122
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900123 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -0700124
Colin Crossfabb6082018-02-20 17:22:23 -0800125 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -0700126
Jaewoong Jung525443a2019-02-28 15:35:54 -0800127 overridableAppProperties overridableAppProperties
128
Colin Crossa4f08812018-10-02 22:03:40 -0700129 installJniLibs []jniLib
Colin Crossf6237212018-10-29 23:14:58 -0700130
131 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800132
133 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
134 installApkName string
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800135
Colin Cross70dda7e2019-10-01 22:05:35 -0700136 installDir android.InstallPath
Jaewoong Jung0949f312019-09-11 10:25:18 -0700137
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700138 onDeviceDir string
139
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800140 additionalAaptFlags []string
Jaewoong Jung98772792019-07-01 17:15:13 -0700141
142 noticeOutputs android.NoticeOutputs
Colin Crosse1731a52017-12-14 11:22:55 -0800143}
144
Martin Stjernholm6d415272020-01-31 17:10:36 +0000145func (a *AndroidApp) IsInstallable() bool {
146 return Bool(a.properties.Installable)
147}
148
Colin Cross89c31582018-04-30 15:55:11 -0700149func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
150 return nil
151}
152
Colin Cross66f78822018-05-02 12:58:28 -0700153func (a *AndroidApp) ExportedStaticPackages() android.Paths {
154 return nil
155}
156
Sundong Ahne1f05aa2019-08-27 13:55:42 +0900157func (a *AndroidApp) OutputFile() android.Path {
158 return a.outputFile
159}
160
Colin Cross503c1d02020-01-28 14:00:53 -0800161func (a *AndroidApp) Certificate() Certificate {
162 return a.certificate
163}
164
Colin Crossa97c5d32018-03-28 14:58:31 -0700165var _ AndroidLibraryDependency = (*AndroidApp)(nil)
166
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900167type Certificate struct {
Colin Cross503c1d02020-01-28 14:00:53 -0800168 Pem, Key android.Path
169 presigned bool
170}
171
172var presignedCertificate = Certificate{presigned: true}
173
174func (c Certificate) AndroidMkString() string {
175 if c.presigned {
176 return "PRESIGNED"
177 } else {
178 return c.Pem.String()
179 }
Colin Cross30e076a2015-04-13 13:58:27 -0700180}
181
Colin Cross46c9b8b2017-06-22 16:51:17 -0700182func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
183 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700184
Jiyong Park6a927c42020-01-21 02:03:43 +0900185 if String(a.appProperties.Stl) == "c++_shared" && !a.sdkVersion().specified() {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700186 ctx.PropertyErrorf("stl", "sdk_version must be set in order to use c++_shared")
187 }
188
Paul Duffin250e6192019-06-07 10:44:37 +0100189 sdkDep := decodeSdkDep(ctx, sdkContext(a))
190 if sdkDep.hasFrameworkLibs() {
191 a.aapt.deps(ctx, sdkDep)
Colin Cross30e076a2015-04-13 13:58:27 -0700192 }
Colin Crossa4f08812018-10-02 22:03:40 -0700193
Peter Collingbournead84f972019-12-17 16:46:18 -0800194 tag := &jniDependencyTag{}
Colin Crossa4f08812018-10-02 22:03:40 -0700195 for _, jniTarget := range ctx.MultiTargets() {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700196 variation := append(jniTarget.Variations(),
197 blueprint.Variation{Mutator: "link", Variation: "shared"})
Colin Crossa4f08812018-10-02 22:03:40 -0700198 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
199 }
Colin Cross50ddcc42019-05-16 12:28:22 -0700200
Paul Duffin250e6192019-06-07 10:44:37 +0100201 a.usesLibrary.deps(ctx, sdkDep.hasFrameworkLibs())
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700202}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700203
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700204func (a *AndroidApp) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800205 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700206 if cert != "" {
207 ctx.AddDependency(ctx.Module(), certificateTag, cert)
208 }
209
210 for _, cert := range a.appProperties.Additional_certificates {
211 cert = android.SrcIsModule(cert)
212 if cert != "" {
213 ctx.AddDependency(ctx.Module(), certificateTag, cert)
214 } else {
215 ctx.PropertyErrorf("additional_certificates",
216 `must be names of android_app_certificate modules in the form ":module"`)
217 }
218 }
Colin Cross30e076a2015-04-13 13:58:27 -0700219}
220
Jeongik Cha538c0d02019-07-11 15:54:27 +0900221func (a *AndroidTestHelperApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
222 a.generateAndroidBuildActions(ctx)
223}
224
Colin Cross46c9b8b2017-06-22 16:51:17 -0700225func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900226 a.checkPlatformAPI(ctx)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900227 a.checkSdkVersion(ctx)
Colin Crossae5caf52018-05-22 11:11:52 -0700228 a.generateAndroidBuildActions(ctx)
229}
230
Sasha Smundak6ad77252019-05-01 13:16:22 -0700231// Returns true if the native libraries should be stored in the APK uncompressed and the
Colin Crosse4246ab2019-02-05 21:55:21 -0800232// extractNativeLibs application flag should be set to false in the manifest.
Sasha Smundak6ad77252019-05-01 13:16:22 -0700233func (a *AndroidApp) useEmbeddedNativeLibs(ctx android.ModuleContext) bool {
Jiyong Park6a927c42020-01-21 02:03:43 +0900234 minSdkVersion, err := a.minSdkVersion().effectiveVersion(ctx)
Colin Crosse4246ab2019-02-05 21:55:21 -0800235 if err != nil {
236 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
237 }
238
Jiyong Park52cd06f2019-11-11 10:14:32 +0900239 return (minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)) ||
240 !a.IsForPlatform()
Colin Crosse4246ab2019-02-05 21:55:21 -0800241}
242
Colin Cross43f08db2018-11-12 10:13:39 -0800243// Returns whether this module should have the dex file stored uncompressed in the APK.
244func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800245 if Bool(a.appProperties.Use_embedded_dex) {
246 return true
247 }
248
Colin Cross53a87f52019-06-25 13:35:30 -0700249 // Uncompress dex in APKs of privileged apps (even for unbundled builds, they may
250 // be preinstalled as prebuilts).
Jiyong Parkf7487312019-10-17 12:54:30 +0900251 if ctx.Config().UncompressPrivAppDex() && a.Privileged() {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000252 return true
253 }
254
Colin Cross53a87f52019-06-25 13:35:30 -0700255 if ctx.Config().UnbundledBuild() {
256 return false
257 }
258
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700259 return shouldUncompressDex(ctx, &a.dexpreopter)
Colin Cross5a0dcd52018-10-05 14:20:06 -0700260}
261
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700262func (a *AndroidApp) shouldEmbedJnis(ctx android.BaseModuleContext) bool {
263 return ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) ||
Jiyong Park52cd06f2019-11-11 10:14:32 +0900264 !a.IsForPlatform() || a.appProperties.AlwaysPackageNativeLibs
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700265}
266
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800267func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
David Brazdild25060a2019-02-18 18:24:16 +0000268 a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis)
269
Jaewoong Jungc27ab662019-05-30 15:51:14 -0700270 // Ask manifest_fixer to add or update the application element indicating this app has no code.
271 a.aapt.hasNoCode = !a.hasCode(ctx)
272
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800273 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800274
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800275 // Add TARGET_AAPT_CHARACTERISTICS values to AAPT link flags if they exist and --product flags were not provided.
Colin Crosse78dcd32018-04-19 15:25:19 -0700276 hasProduct := false
277 for _, f := range a.aaptProperties.Aaptflags {
278 if strings.HasPrefix(f, "--product") {
279 hasProduct = true
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800280 break
Colin Crosse78dcd32018-04-19 15:25:19 -0700281 }
282 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700283 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800284 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700285 }
286
Dan Willemsen72be5902018-10-24 20:24:57 -0700287 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
288 // Product AAPT config
289 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800290 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700291 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700292
Dan Willemsen72be5902018-10-24 20:24:57 -0700293 // Product AAPT preferred config
294 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800295 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700296 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700297 }
298
Jiyong Park7f67f482019-01-05 12:57:48 +0900299 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700300 if overridden || a.overridableAppProperties.Package_name != nil {
301 // The product override variable has a priority over the package_name property.
302 if !overridden {
303 manifestPackageName = *a.overridableAppProperties.Package_name
304 }
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800305 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
Jiyong Park7f67f482019-01-05 12:57:48 +0900306 }
307
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800308 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
309
Colin Crosse560c4a2019-03-19 16:03:11 -0700310 a.aapt.splitNames = a.appProperties.Package_splits
Colin Cross50ddcc42019-05-16 12:28:22 -0700311 a.aapt.sdkLibraries = a.exportedSdkLibs
Colin Crosse560c4a2019-03-19 16:03:11 -0700312
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800313 a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700314
Colin Cross46c9b8b2017-06-22 16:51:17 -0700315 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700316 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800317}
Colin Cross30e076a2015-04-13 13:58:27 -0700318
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800319func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700320 var staticLibProguardFlagFiles android.Paths
321 ctx.VisitDirectDeps(func(m android.Module) {
322 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
323 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
324 }
325 })
326
327 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
328
329 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
330 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800331}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800332
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800333func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross43f08db2018-11-12 10:13:39 -0800334
335 var installDir string
336 if ctx.ModuleName() == "framework-res" {
337 // framework-res.apk is installed as system/framework/framework-res.apk
338 installDir = "framework"
Jiyong Parkf7487312019-10-17 12:54:30 +0900339 } else if a.Privileged() {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800340 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800341 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800342 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800343 }
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800344 a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000345 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -0700346
347 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
348 a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
349 a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
350 a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
351 a.dexpreopter.manifestFile = a.mergedManifestFile
352
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000353 a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex
Colin Cross5a0dcd52018-10-05 14:20:06 -0700354
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800355 if ctx.ModuleName() != "framework-res" {
356 a.Module.compile(ctx, a.aaptSrcJar)
357 }
Colin Cross30e076a2015-04-13 13:58:27 -0700358
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800359 return a.maybeStrippedDexJarFile
360}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800361
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800362func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700363 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700364 if len(jniLibs) > 0 {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700365 if a.shouldEmbedJnis(ctx) {
Colin Crossa4f08812018-10-02 22:03:40 -0700366 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Sasha Smundak6ad77252019-05-01 13:16:22 -0700367 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.useEmbeddedNativeLibs(ctx))
Colin Crossa4f08812018-10-02 22:03:40 -0700368 } else {
369 a.installJniLibs = jniLibs
370 }
371 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800372 return jniJarFile
373}
Colin Crossa4f08812018-10-02 22:03:40 -0700374
Jaewoong Jung0949f312019-09-11 10:25:18 -0700375func (a *AndroidApp) noticeBuildActions(ctx android.ModuleContext) {
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700376 // Collect NOTICE files from all dependencies.
377 seenModules := make(map[android.Module]bool)
378 noticePathSet := make(map[android.Path]bool)
379
380 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
381 // Have we already seen this?
382 if _, ok := seenModules[child]; ok {
383 return false
384 }
385 seenModules[child] = true
386
387 // Skip host modules.
388 if child.Target().Os.Class == android.Host || child.Target().Os.Class == android.HostCross {
389 return false
390 }
391
392 path := child.(android.Module).NoticeFile()
393 if path.Valid() {
394 noticePathSet[path.Path()] = true
395 }
396 return true
397 })
398
399 // If the app has one, add it too.
400 if a.NoticeFile().Valid() {
401 noticePathSet[a.NoticeFile().Path()] = true
402 }
403
404 if len(noticePathSet) == 0 {
Jaewoong Jung98772792019-07-01 17:15:13 -0700405 return
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700406 }
407 var noticePaths []android.Path
408 for path := range noticePathSet {
409 noticePaths = append(noticePaths, path)
410 }
411 sort.Slice(noticePaths, func(i, j int) bool {
412 return noticePaths[i].String() < noticePaths[j].String()
413 })
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700414
Jaewoong Jung0949f312019-09-11 10:25:18 -0700415 a.noticeOutputs = android.BuildNoticeOutput(ctx, a.installDir, a.installApkName+".apk", noticePaths)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700416}
417
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700418// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
419// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
420func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate {
421 if android.SrcIsModule(certPropValue) == "" {
422 var mainCert Certificate
423 if certPropValue != "" {
424 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
425 mainCert = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -0800426 Pem: defaultDir.Join(ctx, certPropValue+".x509.pem"),
427 Key: defaultDir.Join(ctx, certPropValue+".pk8"),
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700428 }
429 } else {
430 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Colin Cross503c1d02020-01-28 14:00:53 -0800431 mainCert = Certificate{
432 Pem: pem,
433 Key: key,
434 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700435 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700436 certificates = append([]Certificate{mainCert}, certificates...)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700437 }
438
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700439 if !m.Platform() {
440 certPath := certificates[0].Pem.String()
Jeongik Chac9464142019-01-07 12:07:27 +0900441 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
442 if strings.HasPrefix(certPath, systemCertPath) {
443 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
444 whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
445
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700446 if enforceSystemCert && !inList(m.Name(), whitelist) {
Jeongik Chac9464142019-01-07 12:07:27 +0900447 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
448 }
449 }
450 }
451
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700452 return certificates
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800453}
454
455func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross50ddcc42019-05-16 12:28:22 -0700456 var apkDeps android.Paths
457
Jeongik Cha538c0d02019-07-11 15:54:27 +0900458 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
459 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
460
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800461 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800462 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800463
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700464 if ctx.ModuleName() == "framework-res" {
465 // framework-res.apk is installed as system/framework/framework-res.apk
Jaewoong Jung0949f312019-09-11 10:25:18 -0700466 a.installDir = android.PathForModuleInstall(ctx, "framework")
Jiyong Parkf7487312019-10-17 12:54:30 +0900467 } else if a.Privileged() {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700468 a.installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
469 } else if ctx.InstallInTestcases() {
Jaewoong Jung326a9412019-11-21 10:41:00 -0800470 a.installDir = android.PathForModuleInstall(ctx, a.installApkName, ctx.DeviceConfig().DeviceArch())
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700471 } else {
Jaewoong Jung0949f312019-09-11 10:25:18 -0700472 a.installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700473 }
Jaewoong Jung7dd4ae22019-09-27 17:13:15 -0700474 a.onDeviceDir = android.InstallPathToOnDevicePath(ctx, a.installDir)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700475
Jaewoong Jung0949f312019-09-11 10:25:18 -0700476 a.noticeBuildActions(ctx)
Jaewoong Jung98772792019-07-01 17:15:13 -0700477 if Bool(a.appProperties.Embed_notices) || ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") {
478 a.aapt.noticeFile = a.noticeOutputs.HtmlGzOutput
479 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700480
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800481 // Process all building blocks, from AAPT to certificates.
482 a.aaptBuildActions(ctx)
483
Colin Cross50ddcc42019-05-16 12:28:22 -0700484 if a.usesLibrary.enforceUsesLibraries() {
485 manifestCheckFile := a.usesLibrary.verifyUsesLibrariesManifest(ctx, a.mergedManifestFile)
486 apkDeps = append(apkDeps, manifestCheckFile)
487 }
488
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800489 a.proguardBuildActions(ctx)
490
491 dexJarFile := a.dexBuildActions(ctx)
492
Peter Collingbournead84f972019-12-17 16:46:18 -0800493 jniLibs, certificateDeps := collectAppDeps(ctx, a.shouldEmbedJnis(ctx))
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800494 jniJarFile := a.jniBuildActions(jniLibs, ctx)
495
496 if ctx.Failed() {
497 return
498 }
499
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700500 certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx)
501 a.certificate = certificates[0]
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800502
503 // Build a final signed app package.
Jaewoong Jung5a498812019-11-07 14:14:38 -0800504 packageFile := android.PathForModuleOut(ctx, a.installApkName+".apk")
Colin Cross50ddcc42019-05-16 12:28:22 -0700505 CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates, apkDeps)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800506 a.outputFile = packageFile
507
Colin Crosse560c4a2019-03-19 16:03:11 -0700508 for _, split := range a.aapt.splits {
509 // Sign the split APKs
Jaewoong Jung5a498812019-11-07 14:14:38 -0800510 packageFile := android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk")
Colin Cross50ddcc42019-05-16 12:28:22 -0700511 CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates, apkDeps)
Colin Crosse560c4a2019-03-19 16:03:11 -0700512 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
513 }
514
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800515 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700516 bundleFile := android.PathForModuleOut(ctx, "base.zip")
517 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
518 a.bundleFile = bundleFile
519
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800520 // Install the app package.
Jiyong Park8ba50f92019-11-13 15:01:01 +0900521 if (Bool(a.Module.properties.Installable) || ctx.Host()) && a.IsForPlatform() {
522 ctx.InstallFile(a.installDir, a.outputFile.Base(), a.outputFile)
523 for _, extra := range a.extraOutputFiles {
524 ctx.InstallFile(a.installDir, extra.Base(), extra)
525 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800526 }
Colin Cross30e076a2015-04-13 13:58:27 -0700527}
528
Peter Collingbournead84f972019-12-17 16:46:18 -0800529func collectAppDeps(ctx android.ModuleContext, shouldCollectRecursiveNativeDeps bool) ([]jniLib, []Certificate) {
Colin Crossa4f08812018-10-02 22:03:40 -0700530 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900531 var certificates []Certificate
Peter Collingbournead84f972019-12-17 16:46:18 -0800532 seenModulePaths := make(map[string]bool)
Colin Crossa4f08812018-10-02 22:03:40 -0700533
Peter Collingbournead84f972019-12-17 16:46:18 -0800534 ctx.WalkDeps(func(module android.Module, parent android.Module) bool {
Colin Crossa4f08812018-10-02 22:03:40 -0700535 otherName := ctx.OtherModuleName(module)
536 tag := ctx.OtherModuleDependencyTag(module)
537
Peter Collingbournead84f972019-12-17 16:46:18 -0800538 if IsJniDepTag(tag) || tag == cc.SharedDepTag {
Colin Crossa4f08812018-10-02 22:03:40 -0700539 if dep, ok := module.(*cc.Module); ok {
Peter Collingbournead84f972019-12-17 16:46:18 -0800540 if dep.IsNdk() || dep.IsStubs() {
541 return false
542 }
543
Colin Crossa4f08812018-10-02 22:03:40 -0700544 lib := dep.OutputFile()
Peter Collingbournead84f972019-12-17 16:46:18 -0800545 path := lib.Path()
546 if seenModulePaths[path.String()] {
547 return false
548 }
549 seenModulePaths[path.String()] = true
550
Colin Crossa4f08812018-10-02 22:03:40 -0700551 if lib.Valid() {
552 jniLibs = append(jniLibs, jniLib{
553 name: ctx.OtherModuleName(module),
Peter Collingbournead84f972019-12-17 16:46:18 -0800554 path: path,
555 target: module.Target(),
Colin Crossa4f08812018-10-02 22:03:40 -0700556 })
557 } else {
558 ctx.ModuleErrorf("dependency %q missing output file", otherName)
559 }
560 } else {
561 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
Colin Crossa4f08812018-10-02 22:03:40 -0700562 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800563
564 return shouldCollectRecursiveNativeDeps
565 }
566
567 if tag == certificateTag {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700568 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900569 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700570 } else {
571 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
572 }
Colin Crossa4f08812018-10-02 22:03:40 -0700573 }
Peter Collingbournead84f972019-12-17 16:46:18 -0800574
575 return false
Colin Crossa4f08812018-10-02 22:03:40 -0700576 })
577
Colin Crossbd01e2a2018-10-04 15:21:03 -0700578 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700579}
580
Colin Cross0ea8ba82019-06-06 14:33:29 -0700581func (a *AndroidApp) getCertString(ctx android.BaseModuleContext) string {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800582 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
583 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000584 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800585 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800586 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800587}
588
Jiyong Park0f80c182020-01-31 02:49:53 +0900589func (a *AndroidApp) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
590 if IsJniDepTag(ctx.OtherModuleDependencyTag(dep)) {
591 return true
592 }
593 return a.Library.DepIsInSameApex(ctx, dep)
594}
595
Jiyong Parkb7c639e2019-08-19 14:56:02 +0900596// For OutputFileProducer interface
597func (a *AndroidApp) OutputFiles(tag string) (android.Paths, error) {
598 switch tag {
599 case ".aapt.srcjar":
600 return []android.Path{a.aaptSrcJar}, nil
601 }
602 return a.Library.OutputFiles(tag)
603}
604
Jiyong Parkf7487312019-10-17 12:54:30 +0900605func (a *AndroidApp) Privileged() bool {
606 return Bool(a.appProperties.Privileged)
607}
608
Colin Cross1b16b0e2019-02-12 14:41:32 -0800609// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700610func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700611 module := &AndroidApp{}
612
Sasha Smundak2057f822019-04-16 17:16:58 -0700613 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross66dbc0b2017-12-28 12:23:20 -0800614 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
615
Colin Crossae5caf52018-05-22 11:11:52 -0700616 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700617 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700618
Colin Cross36242852017-06-23 15:06:31 -0700619 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700620 &module.Module.properties,
621 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800622 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700623 &module.Module.protoProperties,
624 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800625 &module.appProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700626 &module.overridableAppProperties,
627 &module.usesLibrary.usesLibraryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700628
Colin Crossa9d8bee2018-10-02 13:59:46 -0700629 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
630 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
631 })
632
Colin Crossa4f08812018-10-02 22:03:40 -0700633 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
634 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800635 android.InitOverridableModule(module, &module.appProperties.Overrides)
Jiyong Park52cd06f2019-11-11 10:14:32 +0900636 android.InitApexModule(module)
Colin Crossa4f08812018-10-02 22:03:40 -0700637
Colin Cross36242852017-06-23 15:06:31 -0700638 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700639}
Colin Crossae5caf52018-05-22 11:11:52 -0700640
641type appTestProperties struct {
642 Instrumentation_for *string
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700643
644 // if specified, the instrumentation target package name in the manifest is overwritten by it.
645 Instrumentation_target_package *string
Colin Crossae5caf52018-05-22 11:11:52 -0700646}
647
648type AndroidTest struct {
649 AndroidApp
650
651 appTestProperties appTestProperties
652
653 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700654
655 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700656 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700657}
658
Jaewoong Jung0949f312019-09-11 10:25:18 -0700659func (a *AndroidTest) InstallInTestcases() bool {
660 return true
661}
662
Colin Crossae5caf52018-05-22 11:11:52 -0700663func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700664 if a.appTestProperties.Instrumentation_target_package != nil {
665 a.additionalAaptFlags = append(a.additionalAaptFlags,
666 "--rename-instrumentation-target-package "+*a.appTestProperties.Instrumentation_target_package)
667 } else if a.appTestProperties.Instrumentation_for != nil {
668 // Check if the instrumentation target package is overridden.
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800669 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
670 if overridden {
671 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
672 }
673 }
Colin Crossae5caf52018-05-22 11:11:52 -0700674 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700675
Jaewoong Jung39982342020-01-14 10:27:18 -0800676 testConfig := tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700677 a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites, a.testProperties.Auto_gen_config)
Jaewoong Jung39982342020-01-14 10:27:18 -0800678 a.testConfig = a.FixTestConfig(ctx, testConfig)
Colin Cross8a497952019-03-05 22:25:09 -0800679 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700680}
681
Jaewoong Jung39982342020-01-14 10:27:18 -0800682func (a *AndroidTest) FixTestConfig(ctx android.ModuleContext, testConfig android.Path) android.Path {
683 if testConfig == nil {
684 return nil
685 }
686
687 fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", "AndroidTest.xml")
688 rule := android.NewRuleBuilder()
689 command := rule.Command().BuiltTool(ctx, "test_config_fixer").Input(testConfig).Output(fixedConfig)
690 fixNeeded := false
691
692 if ctx.ModuleName() != a.installApkName {
693 fixNeeded = true
694 command.FlagWithArg("--test-file-name ", a.installApkName+".apk")
695 }
696
697 if a.overridableAppProperties.Package_name != nil {
698 fixNeeded = true
699 command.FlagWithInput("--manifest ", a.manifestPath).
700 FlagWithArg("--package-name ", *a.overridableAppProperties.Package_name)
701 }
702
703 if fixNeeded {
704 rule.Build(pctx, ctx, "fix_test_config", "fix test config")
705 return fixedConfig
706 }
707 return testConfig
708}
709
Colin Cross303e21f2018-08-07 16:49:25 -0700710func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -0700711 a.AndroidApp.DepsMutator(ctx)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700712}
713
714func (a *AndroidTest) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
715 a.AndroidApp.OverridablePropertiesDepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -0700716 if a.appTestProperties.Instrumentation_for != nil {
717 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
718 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
719 // use instrumentationForTag instead of libTag.
720 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
721 }
Colin Crossae5caf52018-05-22 11:11:52 -0700722}
723
Colin Cross1b16b0e2019-02-12 14:41:32 -0800724// android_test compiles test sources and Android resources into an Android application package `.apk` file and
725// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -0700726func AndroidTestFactory() android.Module {
727 module := &AndroidTest{}
728
Sasha Smundak2057f822019-04-16 17:16:58 -0700729 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross5067db92018-09-17 16:46:35 -0700730
731 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700732 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800733 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700734 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800735 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -0700736
737 module.AddProperties(
738 &module.Module.properties,
739 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800740 &module.Module.dexpreoptProperties,
Colin Crossae5caf52018-05-22 11:11:52 -0700741 &module.Module.protoProperties,
742 &module.aaptProperties,
743 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700744 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800745 &module.overridableAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700746 &module.usesLibrary.usesLibraryProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700747 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700748
Colin Crossa4f08812018-10-02 22:03:40 -0700749 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
750 android.InitDefaultableModule(module)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700751 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossae5caf52018-05-22 11:11:52 -0700752 return module
753}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700754
Colin Cross252fc6f2018-10-04 15:22:03 -0700755type appTestHelperAppProperties struct {
756 // list of compatibility suites (for example "cts", "vts") that the module should be
757 // installed into.
758 Test_suites []string `android:"arch_variant"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700759
760 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
761 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
762 // explicitly.
763 Auto_gen_config *bool
Colin Cross252fc6f2018-10-04 15:22:03 -0700764}
765
766type AndroidTestHelperApp struct {
767 AndroidApp
768
769 appTestHelperAppProperties appTestHelperAppProperties
770}
771
Jaewoong Jung326a9412019-11-21 10:41:00 -0800772func (a *AndroidTestHelperApp) InstallInTestcases() bool {
773 return true
774}
775
Colin Cross1b16b0e2019-02-12 14:41:32 -0800776// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
777// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
778// test.
Colin Cross252fc6f2018-10-04 15:22:03 -0700779func AndroidTestHelperAppFactory() android.Module {
780 module := &AndroidTestHelperApp{}
781
Sasha Smundak2057f822019-04-16 17:16:58 -0700782 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700783
784 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800785 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700786 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800787 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700788
789 module.AddProperties(
790 &module.Module.properties,
791 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800792 &module.Module.dexpreoptProperties,
Colin Cross252fc6f2018-10-04 15:22:03 -0700793 &module.Module.protoProperties,
794 &module.aaptProperties,
795 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800796 &module.appTestHelperAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700797 &module.overridableAppProperties,
798 &module.usesLibrary.usesLibraryProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -0700799
800 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
801 android.InitDefaultableModule(module)
Anton Hansson3d2b6b42020-01-10 15:06:01 +0000802 android.InitApexModule(module)
Colin Cross252fc6f2018-10-04 15:22:03 -0700803 return module
804}
805
Colin Crossbd01e2a2018-10-04 15:21:03 -0700806type AndroidAppCertificate struct {
807 android.ModuleBase
808 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900809 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -0700810}
811
812type AndroidAppCertificateProperties struct {
813 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
814 Certificate *string
815}
816
Colin Cross1b16b0e2019-02-12 14:41:32 -0800817// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
818// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -0700819func AndroidAppCertificateFactory() android.Module {
820 module := &AndroidAppCertificate{}
821 module.AddProperties(&module.properties)
822 android.InitAndroidModule(module)
823 return module
824}
825
Colin Crossbd01e2a2018-10-04 15:21:03 -0700826func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
827 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900828 c.Certificate = Certificate{
Colin Cross503c1d02020-01-28 14:00:53 -0800829 Pem: android.PathForModuleSrc(ctx, cert+".x509.pem"),
830 Key: android.PathForModuleSrc(ctx, cert+".pk8"),
Colin Crossbd01e2a2018-10-04 15:21:03 -0700831 }
832}
Jaewoong Jung525443a2019-02-28 15:35:54 -0800833
834type OverrideAndroidApp struct {
835 android.ModuleBase
836 android.OverrideModuleBase
837}
838
839func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
840 // All the overrides happen in the base module.
841 // TODO(jungjw): Check the base module type.
842}
843
844// override_android_app is used to create an android_app module based on another android_app by overriding
845// some of its properties.
846func OverrideAndroidAppModuleFactory() android.Module {
847 m := &OverrideAndroidApp{}
848 m.AddProperties(&overridableAppProperties{})
849
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700850 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800851 android.InitOverrideModule(m)
852 return m
853}
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700854
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700855type OverrideAndroidTest struct {
856 android.ModuleBase
857 android.OverrideModuleBase
858}
859
860func (i *OverrideAndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
861 // All the overrides happen in the base module.
862 // TODO(jungjw): Check the base module type.
863}
864
865// override_android_test is used to create an android_app module based on another android_test by overriding
866// some of its properties.
867func OverrideAndroidTestModuleFactory() android.Module {
868 m := &OverrideAndroidTest{}
869 m.AddProperties(&overridableAppProperties{})
870 m.AddProperties(&appTestProperties{})
871
872 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
873 android.InitOverrideModule(m)
874 return m
875}
876
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700877type AndroidAppImport struct {
878 android.ModuleBase
879 android.DefaultableModuleBase
880 prebuilt android.Prebuilt
881
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700882 properties AndroidAppImportProperties
883 dpiVariants interface{}
884 archVariants interface{}
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700885
886 outputFile android.Path
Colin Cross503c1d02020-01-28 14:00:53 -0800887 certificate Certificate
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700888
889 dexpreopter
Colin Cross50ddcc42019-05-16 12:28:22 -0700890
891 usesLibrary usesLibrary
Jaewoong Jung8aae22e2019-07-17 10:21:49 -0700892
Colin Cross70dda7e2019-10-01 22:05:35 -0700893 installPath android.InstallPath
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700894}
895
896type AndroidAppImportProperties struct {
897 // A prebuilt apk to import
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700898 Apk *string
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700899
Jaewoong Jung961d4fd2019-08-22 14:25:58 -0700900 // The name of a certificate in the default certificate directory or an android_app_certificate
901 // module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700902 Certificate *string
903
904 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
905 // be set for presigned modules.
906 Presigned *bool
907
Jaewoong Jung961d4fd2019-08-22 14:25:58 -0700908 // Sign with the default system dev certificate. Must be used judiciously. Most imported apps
909 // need to either specify a specific certificate or be presigned.
910 Default_dev_cert *bool
911
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700912 // Specifies that this app should be installed to the priv-app directory,
913 // where the system will grant it additional privileges not available to
914 // normal apps.
915 Privileged *bool
916
917 // Names of modules to be overridden. Listed modules can only be other binaries
918 // (in Make or Soong).
919 // This does not completely prevent installation of the overridden binaries, but if both
920 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
921 // from PRODUCT_PACKAGES.
922 Overrides []string
Jaewoong Jung8aae22e2019-07-17 10:21:49 -0700923
924 // Optional name for the installed app. If unspecified, it is derived from the module name.
925 Filename *string
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700926}
927
Martin Stjernholm6d415272020-01-31 17:10:36 +0000928func (a *AndroidAppImport) IsInstallable() bool {
929 return true
930}
931
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700932// Updates properties with variant-specific values.
933func (a *AndroidAppImport) processVariants(ctx android.LoadHookContext) {
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700934 config := ctx.Config()
935
936 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants")
937 // Try DPI variant matches in the reverse-priority order so that the highest priority match
938 // overwrites everything else.
939 // TODO(jungjw): Can we optimize this by making it priority order?
940 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700941 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700942 }
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700943 if config.ProductAAPTPreferredConfig() != "" {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700944 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700945 }
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700946
947 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName("Arch")
948 archType := ctx.Config().Targets[android.Android][0].Arch.ArchType
949 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700950}
951
Colin Cross1184b642019-12-30 18:43:07 -0800952func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700953 dst interface{}, variantGroup reflect.Value, variant string) {
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700954 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
955 if !src.IsValid() {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700956 return
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700957 }
958
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700959 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
960 if err != nil {
961 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
962 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
963 } else {
964 panic(err)
965 }
966 }
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700967}
968
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700969func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
970 cert := android.SrcIsModule(String(a.properties.Certificate))
971 if cert != "" {
972 ctx.AddDependency(ctx.Module(), certificateTag, cert)
973 }
Colin Cross50ddcc42019-05-16 12:28:22 -0700974
Paul Duffin250e6192019-06-07 10:44:37 +0100975 a.usesLibrary.deps(ctx, true)
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700976}
977
978func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
979 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
Jaewoong Jung7c5bd832020-01-13 09:55:39 -0800980 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
981 // with them may invalidate pre-existing signature data.
982 if ctx.InstallInTestcases() && Bool(a.properties.Presigned) {
983 ctx.Build(pctx, android.BuildParams{
984 Rule: android.Cp,
985 Output: outputPath,
986 Input: inputPath,
987 })
988 return
989 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700990 rule := android.NewRuleBuilder()
991 rule.Command().
992 Textf(`if (zipinfo %s 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
Colin Crossee94d6a2019-07-08 17:08:34 -0700993 BuiltTool(ctx, "zip2zip").
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700994 FlagWithInput("-i ", inputPath).
995 FlagWithOutput("-o ", outputPath).
996 FlagWithArg("-0 ", "'lib/**/*.so'").
997 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
998 rule.Build(pctx, ctx, "uncompress-embedded-jni-libs", "Uncompress embedded JIN libs")
999}
1000
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001001// Returns whether this module should have the dex file stored uncompressed in the APK.
1002func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
1003 if ctx.Config().UnbundledBuild() {
1004 return false
1005 }
1006
1007 // Uncompress dex in APKs of privileged apps
Jiyong Parkf7487312019-10-17 12:54:30 +09001008 if ctx.Config().UncompressPrivAppDex() && a.Privileged() {
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001009 return true
1010 }
1011
1012 return shouldUncompressDex(ctx, &a.dexpreopter)
1013}
1014
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001015func (a *AndroidAppImport) uncompressDex(
1016 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
1017 rule := android.NewRuleBuilder()
1018 rule.Command().
1019 Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
Colin Crossee94d6a2019-07-08 17:08:34 -07001020 BuiltTool(ctx, "zip2zip").
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001021 FlagWithInput("-i ", inputPath).
1022 FlagWithOutput("-o ", outputPath).
1023 FlagWithArg("-0 ", "'classes*.dex'").
1024 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
1025 rule.Build(pctx, ctx, "uncompress-dex", "Uncompress dex files")
1026}
1027
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001028func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001029 a.generateAndroidBuildActions(ctx)
1030}
1031
1032func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001033 numCertPropsSet := 0
1034 if String(a.properties.Certificate) != "" {
1035 numCertPropsSet++
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001036 }
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001037 if Bool(a.properties.Presigned) {
1038 numCertPropsSet++
1039 }
1040 if Bool(a.properties.Default_dev_cert) {
1041 numCertPropsSet++
1042 }
1043 if numCertPropsSet != 1 {
1044 ctx.ModuleErrorf("One and only one of certficate, presigned, and default_dev_cert properties must be set")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001045 }
1046
Peter Collingbournead84f972019-12-17 16:46:18 -08001047 _, certificates := collectAppDeps(ctx, false)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001048
1049 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001050 // TODO: LOCAL_PACKAGE_SPLITS
1051
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001052 srcApk := a.prebuilt.SingleSourcePath(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -07001053
1054 if a.usesLibrary.enforceUsesLibraries() {
1055 srcApk = a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk)
1056 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001057
1058 // TODO: Install or embed JNI libraries
1059
1060 // Uncompress JNI libraries in the apk
1061 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
1062 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
1063
Kyeongkab.Namc4997142019-11-22 11:38:16 +09001064 var installDir android.InstallPath
1065 if Bool(a.properties.Privileged) {
1066 installDir = android.PathForModuleInstall(ctx, "priv-app", a.BaseModuleName())
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001067 } else if ctx.InstallInTestcases() {
1068 installDir = android.PathForModuleInstall(ctx, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch())
Kyeongkab.Namc4997142019-11-22 11:38:16 +09001069 } else {
1070 installDir = android.PathForModuleInstall(ctx, "app", a.BaseModuleName())
1071 }
1072
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001073 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001074 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
Jaewoong Jungacf18d72019-05-02 14:55:29 -07001075 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -07001076
1077 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
1078 a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
1079 a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
1080 a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
1081
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001082 dexOutput := a.dexpreopter.dexpreopt(ctx, jnisUncompressed)
Jaewoong Jungea1bdb02019-05-09 14:36:34 -07001083 if a.dexpreopter.uncompressedDex {
1084 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
1085 a.uncompressDex(ctx, dexOutput, dexUncompressed.OutputPath)
1086 dexOutput = dexUncompressed
1087 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001088
1089 // Sign or align the package
1090 // TODO: Handle EXTERNAL
1091 if !Bool(a.properties.Presigned) {
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001092 // If the certificate property is empty at this point, default_dev_cert must be set to true.
1093 // Which makes processMainCert's behavior for the empty cert string WAI.
1094 certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001095 if len(certificates) != 1 {
1096 ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates)
1097 }
Colin Cross503c1d02020-01-28 14:00:53 -08001098 a.certificate = certificates[0]
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001099 signed := android.PathForModuleOut(ctx, "signed", ctx.ModuleName()+".apk")
1100 SignAppPackage(ctx, signed, dexOutput, certificates)
1101 a.outputFile = signed
1102 } else {
1103 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", ctx.ModuleName()+".apk")
1104 TransformZipAlign(ctx, alignedApk, dexOutput)
1105 a.outputFile = alignedApk
Colin Cross503c1d02020-01-28 14:00:53 -08001106 a.certificate = presignedCertificate
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001107 }
1108
1109 // TODO: Optionally compress the output apk.
1110
Jaewoong Jung8aae22e2019-07-17 10:21:49 -07001111 a.installPath = ctx.InstallFile(installDir,
1112 proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk"), a.outputFile)
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001113
1114 // TODO: androidmk converter jni libs
1115}
1116
1117func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
1118 return &a.prebuilt
1119}
1120
1121func (a *AndroidAppImport) Name() string {
1122 return a.prebuilt.Name(a.ModuleBase.Name())
1123}
1124
Dario Frenicde2a032019-10-27 00:29:22 +01001125func (a *AndroidAppImport) OutputFile() android.Path {
1126 return a.outputFile
1127}
1128
Jiyong Park618922e2020-01-08 13:35:43 +09001129func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
1130 return nil
1131}
1132
Colin Cross503c1d02020-01-28 14:00:53 -08001133func (a *AndroidAppImport) Certificate() Certificate {
1134 return a.certificate
1135}
1136
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001137var dpiVariantGroupType reflect.Type
1138var archVariantGroupType reflect.Type
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001139
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001140func initAndroidAppImportVariantGroupTypes() {
1141 dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants")
1142
1143 archNames := make([]string, len(android.ArchTypeList()))
1144 for i, archType := range android.ArchTypeList() {
1145 archNames[i] = archType.Name
1146 }
1147 archVariantGroupType = createVariantGroupType(archNames, "Arch")
1148}
1149
1150// Populates all variant struct properties at creation time.
1151func (a *AndroidAppImport) populateAllVariantStructs() {
1152 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
1153 a.AddProperties(a.dpiVariants)
1154
1155 a.archVariants = reflect.New(archVariantGroupType).Interface()
1156 a.AddProperties(a.archVariants)
1157}
1158
Jiyong Parkf7487312019-10-17 12:54:30 +09001159func (a *AndroidAppImport) Privileged() bool {
1160 return Bool(a.properties.Privileged)
1161}
1162
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001163func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
1164 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
1165
1166 variantFields := make([]reflect.StructField, len(variants))
1167 for i, variant := range variants {
1168 variantFields[i] = reflect.StructField{
1169 Name: proptools.FieldNameForProperty(variant),
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001170 Type: props,
1171 }
1172 }
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001173
1174 variantGroupStruct := reflect.StructOf(variantFields)
1175 return reflect.StructOf([]reflect.StructField{
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001176 {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001177 Name: variantGroupName,
1178 Type: variantGroupStruct,
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001179 },
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001180 })
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001181}
1182
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001183// android_app_import imports a prebuilt apk with additional processing specified in the module.
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001184// DPI-specific apk source files can be specified using dpi_variants. Example:
1185//
1186// android_app_import {
1187// name: "example_import",
1188// apk: "prebuilts/example.apk",
1189// dpi_variants: {
1190// mdpi: {
1191// apk: "prebuilts/example_mdpi.apk",
1192// },
1193// xhdpi: {
1194// apk: "prebuilts/example_xhdpi.apk",
1195// },
1196// },
1197// certificate: "PRESIGNED",
1198// }
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001199func AndroidAppImportFactory() android.Module {
1200 module := &AndroidAppImport{}
1201 module.AddProperties(&module.properties)
1202 module.AddProperties(&module.dexpreoptProperties)
Colin Cross50ddcc42019-05-16 12:28:22 -07001203 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001204 module.populateAllVariantStructs()
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001205 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001206 module.processVariants(ctx)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001207 })
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001208
1209 InitJavaModule(module, android.DeviceSupported)
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001210 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001211
1212 return module
1213}
Colin Cross50ddcc42019-05-16 12:28:22 -07001214
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001215type AndroidTestImport struct {
1216 AndroidAppImport
1217
1218 testProperties testProperties
1219
1220 data android.Paths
1221}
1222
1223func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1224 a.generateAndroidBuildActions(ctx)
1225
1226 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
1227}
1228
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001229func (a *AndroidTestImport) InstallInTestcases() bool {
1230 return true
1231}
1232
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001233// android_test_import imports a prebuilt test apk with additional processing specified in the
1234// module. DPI or arch variant configurations can be made as with android_app_import.
1235func AndroidTestImportFactory() android.Module {
1236 module := &AndroidTestImport{}
1237 module.AddProperties(&module.properties)
1238 module.AddProperties(&module.dexpreoptProperties)
1239 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
1240 module.AddProperties(&module.testProperties)
1241 module.populateAllVariantStructs()
1242 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
1243 module.processVariants(ctx)
1244 })
1245
1246 InitJavaModule(module, android.DeviceSupported)
1247 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
1248
1249 return module
1250}
1251
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001252type RuntimeResourceOverlay struct {
1253 android.ModuleBase
1254 android.DefaultableModuleBase
1255 aapt
1256
1257 properties RuntimeResourceOverlayProperties
1258
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08001259 certificate Certificate
1260
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001261 outputFile android.Path
1262 installDir android.InstallPath
1263}
1264
1265type RuntimeResourceOverlayProperties struct {
1266 // the name of a certificate in the default certificate directory or an android_app_certificate
1267 // module name in the form ":module".
1268 Certificate *string
1269
1270 // optional theme name. If specified, the overlay package will be applied
1271 // only when the ro.boot.vendor.overlay.theme system property is set to the same value.
1272 Theme *string
1273
1274 // if not blank, set to the version of the sdk to compile against.
1275 // Defaults to compiling against the current platform.
1276 Sdk_version *string
1277
1278 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
1279 // Defaults to sdk_version if not set.
1280 Min_sdk_version *string
1281}
1282
1283func (r *RuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
1284 sdkDep := decodeSdkDep(ctx, sdkContext(r))
1285 if sdkDep.hasFrameworkLibs() {
1286 r.aapt.deps(ctx, sdkDep)
1287 }
1288
1289 cert := android.SrcIsModule(String(r.properties.Certificate))
1290 if cert != "" {
1291 ctx.AddDependency(ctx.Module(), certificateTag, cert)
1292 }
1293}
1294
1295func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1296 // Compile and link resources
1297 r.aapt.hasNoCode = true
Jaewoong Jungf0f747c2020-01-24 10:30:02 -08001298 // Do not remove resources without default values nor dedupe resource configurations with the same value
1299 r.aapt.buildActions(ctx, r, "--no-resource-deduping", "--no-resource-removal")
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001300
1301 // Sign the built package
1302 _, certificates := collectAppDeps(ctx, false)
1303 certificates = processMainCert(r.ModuleBase, String(r.properties.Certificate), certificates, ctx)
1304 signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
1305 SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates)
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08001306 r.certificate = certificates[0]
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001307
1308 r.outputFile = signed
1309 r.installDir = android.PathForModuleInstall(ctx, "overlay", String(r.properties.Theme))
1310 ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile)
1311}
1312
Jiyong Park6a927c42020-01-21 02:03:43 +09001313func (r *RuntimeResourceOverlay) sdkVersion() sdkSpec {
1314 return sdkSpecFrom(String(r.properties.Sdk_version))
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001315}
1316
1317func (r *RuntimeResourceOverlay) systemModules() string {
1318 return ""
1319}
1320
Jiyong Park6a927c42020-01-21 02:03:43 +09001321func (r *RuntimeResourceOverlay) minSdkVersion() sdkSpec {
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001322 if r.properties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +09001323 return sdkSpecFrom(*r.properties.Min_sdk_version)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001324 }
1325 return r.sdkVersion()
1326}
1327
Jiyong Park6a927c42020-01-21 02:03:43 +09001328func (r *RuntimeResourceOverlay) targetSdkVersion() sdkSpec {
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08001329 return r.sdkVersion()
1330}
1331
1332// runtime_resource_overlay generates a resource-only apk file that can overlay application and
1333// system resources at run time.
1334func RuntimeResourceOverlayFactory() android.Module {
1335 module := &RuntimeResourceOverlay{}
1336 module.AddProperties(
1337 &module.properties,
1338 &module.aaptProperties)
1339
1340 InitJavaModule(module, android.DeviceSupported)
1341
1342 return module
1343}
1344
Colin Cross50ddcc42019-05-16 12:28:22 -07001345type UsesLibraryProperties struct {
1346 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file.
1347 Uses_libs []string
1348
1349 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file with
1350 // required=false.
1351 Optional_uses_libs []string
1352
1353 // If true, the list of uses_libs and optional_uses_libs modules must match the AndroidManifest.xml file. Defaults
1354 // to true if either uses_libs or optional_uses_libs is set. Will unconditionally default to true in the future.
1355 Enforce_uses_libs *bool
1356}
1357
1358// usesLibrary provides properties and helper functions for AndroidApp and AndroidAppImport to verify that the
1359// <uses-library> tags that end up in the manifest of an APK match the ones known to the build system through the
1360// uses_libs and optional_uses_libs properties. The build system's values are used by dexpreopt to preopt apps
1361// with knowledge of their shared libraries.
1362type usesLibrary struct {
1363 usesLibraryProperties UsesLibraryProperties
1364}
1365
Paul Duffin250e6192019-06-07 10:44:37 +01001366func (u *usesLibrary) deps(ctx android.BottomUpMutatorContext, hasFrameworkLibs bool) {
Colin Cross3245b2c2019-06-07 13:18:09 -07001367 if !ctx.Config().UnbundledBuild() {
1368 ctx.AddVariationDependencies(nil, usesLibTag, u.usesLibraryProperties.Uses_libs...)
1369 ctx.AddVariationDependencies(nil, usesLibTag, u.presentOptionalUsesLibs(ctx)...)
Paul Duffin250e6192019-06-07 10:44:37 +01001370 // Only add these extra dependencies if the module depends on framework libs. This avoids
1371 // creating a cyclic dependency:
1372 // e.g. framework-res -> org.apache.http.legacy -> ... -> framework-res.
1373 if hasFrameworkLibs {
Colin Cross3245b2c2019-06-07 13:18:09 -07001374 // dexpreopt/dexpreopt.go needs the paths to the dex jars of these libraries in case construct_context.sh needs
1375 // to pass them to dex2oat. Add them as a dependency so we can determine the path to the dex jar of each
1376 // library to dexpreopt.
1377 ctx.AddVariationDependencies(nil, usesLibTag,
1378 "org.apache.http.legacy",
1379 "android.hidl.base-V1.0-java",
1380 "android.hidl.manager-V1.0-java")
1381 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001382 }
1383}
1384
1385// presentOptionalUsesLibs returns optional_uses_libs after filtering out MissingUsesLibraries, which don't exist in the
1386// build.
1387func (u *usesLibrary) presentOptionalUsesLibs(ctx android.BaseModuleContext) []string {
1388 optionalUsesLibs, _ := android.FilterList(u.usesLibraryProperties.Optional_uses_libs, ctx.Config().MissingUsesLibraries())
1389 return optionalUsesLibs
1390}
1391
1392// usesLibraryPaths returns a map of module names of shared library dependencies to the paths to their dex jars.
1393func (u *usesLibrary) usesLibraryPaths(ctx android.ModuleContext) map[string]android.Path {
1394 usesLibPaths := make(map[string]android.Path)
1395
1396 if !ctx.Config().UnbundledBuild() {
1397 ctx.VisitDirectDepsWithTag(usesLibTag, func(m android.Module) {
1398 if lib, ok := m.(Dependency); ok {
1399 if dexJar := lib.DexJar(); dexJar != nil {
1400 usesLibPaths[ctx.OtherModuleName(m)] = dexJar
1401 } else {
1402 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must produce a dex jar, does it have installable: true?",
1403 ctx.OtherModuleName(m))
1404 }
1405 } else if ctx.Config().AllowMissingDependencies() {
1406 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(m)})
1407 } else {
1408 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must be a java library",
1409 ctx.OtherModuleName(m))
1410 }
1411 })
1412 }
1413
1414 return usesLibPaths
1415}
1416
1417// enforceUsesLibraries returns true of <uses-library> tags should be checked against uses_libs and optional_uses_libs
1418// properties. Defaults to true if either of uses_libs or optional_uses_libs is specified. Will default to true
1419// unconditionally in the future.
1420func (u *usesLibrary) enforceUsesLibraries() bool {
1421 defaultEnforceUsesLibs := len(u.usesLibraryProperties.Uses_libs) > 0 ||
1422 len(u.usesLibraryProperties.Optional_uses_libs) > 0
1423 return BoolDefault(u.usesLibraryProperties.Enforce_uses_libs, defaultEnforceUsesLibs)
1424}
1425
1426// verifyUsesLibrariesManifest checks the <uses-library> tags in an AndroidManifest.xml against the ones specified
1427// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the manifest.
1428func (u *usesLibrary) verifyUsesLibrariesManifest(ctx android.ModuleContext, manifest android.Path) android.Path {
1429 outputFile := android.PathForModuleOut(ctx, "manifest_check", "AndroidManifest.xml")
1430
1431 rule := android.NewRuleBuilder()
Colin Crossee94d6a2019-07-08 17:08:34 -07001432 cmd := rule.Command().BuiltTool(ctx, "manifest_check").
Colin Cross50ddcc42019-05-16 12:28:22 -07001433 Flag("--enforce-uses-libraries").
1434 Input(manifest).
1435 FlagWithOutput("-o ", outputFile)
1436
1437 for _, lib := range u.usesLibraryProperties.Uses_libs {
1438 cmd.FlagWithArg("--uses-library ", lib)
1439 }
1440
1441 for _, lib := range u.usesLibraryProperties.Optional_uses_libs {
1442 cmd.FlagWithArg("--optional-uses-library ", lib)
1443 }
1444
1445 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1446
1447 return outputFile
1448}
1449
1450// verifyUsesLibrariesAPK checks the <uses-library> tags in the manifest of an APK against the ones specified
1451// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the APK.
1452func (u *usesLibrary) verifyUsesLibrariesAPK(ctx android.ModuleContext, apk android.Path) android.Path {
1453 outputFile := android.PathForModuleOut(ctx, "verify_uses_libraries", apk.Base())
1454
1455 rule := android.NewRuleBuilder()
1456 aapt := ctx.Config().HostToolPath(ctx, "aapt")
1457 rule.Command().
1458 Textf("aapt_binary=%s", aapt.String()).Implicit(aapt).
1459 Textf(`uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Uses_libs, " ")).
1460 Textf(`optional_uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Optional_uses_libs, " ")).
1461 Tool(android.PathForSource(ctx, "build/make/core/verify_uses_libraries.sh")).Input(apk)
1462 rule.Command().Text("cp -f").Input(apk).Output(outputFile)
1463
1464 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1465
1466 return outputFile
1467}