blob: e897dca42969614ba68be6787c4324a4e24b4bbf [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 (
Colin Cross43f08db2018-11-12 10:13:39 -080020 "path/filepath"
Colin Cross30e076a2015-04-13 13:58:27 -070021 "strings"
22
Colin Crossa4f08812018-10-02 22:03:40 -070023 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070024 "github.com/google/blueprint/proptools"
Colin Cross30e076a2015-04-13 13:58:27 -070025
Colin Cross635c3b02016-05-18 15:37:25 -070026 "android/soong/android"
Colin Crossa4f08812018-10-02 22:03:40 -070027 "android/soong/cc"
Colin Cross303e21f2018-08-07 16:49:25 -070028 "android/soong/tradefed"
Colin Cross30e076a2015-04-13 13:58:27 -070029)
30
Colin Cross3bc7ffa2017-11-22 16:19:37 -080031func init() {
Colin Cross5ab4e6d2017-11-22 16:20:45 -080032 android.RegisterModuleType("android_app", AndroidAppFactory)
Colin Crossae5caf52018-05-22 11:11:52 -070033 android.RegisterModuleType("android_test", AndroidTestFactory)
Colin Cross252fc6f2018-10-04 15:22:03 -070034 android.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
Colin Crossbd01e2a2018-10-04 15:21:03 -070035 android.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
Jaewoong Jung525443a2019-02-28 15:35:54 -080036 android.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080037}
38
Colin Cross30e076a2015-04-13 13:58:27 -070039// AndroidManifest.xml merging
40// package splits
41
Colin Crossfabb6082018-02-20 17:22:23 -080042type appProperties struct {
Colin Crossbd01e2a2018-10-04 15:21:03 -070043 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
Colin Cross7d5136f2015-05-11 13:39:40 -070044 Additional_certificates []string
45
46 // If set, create package-export.apk, which other packages can
47 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
Nan Zhangea568a42017-11-08 21:20:04 -080048 Export_package_resources *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070049
Colin Cross16056062017-12-13 22:46:28 -080050 // Specifies that this app should be installed to the priv-app directory,
51 // where the system will grant it additional privileges not available to
52 // normal apps.
53 Privileged *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070054
55 // list of resource labels to generate individual resource packages
56 Package_splits []string
Jason Monkd4122be2018-08-10 09:33:36 -040057
58 // Names of modules to be overridden. Listed modules can only be other binaries
59 // (in Make or Soong).
60 // This does not completely prevent installation of the overridden binaries, but if both
61 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
62 // from PRODUCT_PACKAGES.
63 Overrides []string
Colin Crossa4f08812018-10-02 22:03:40 -070064
65 // list of native libraries that will be provided in or alongside the resulting jar
66 Jni_libs []string `android:"arch_variant"`
67
Colin Crosse4246ab2019-02-05 21:55:21 -080068 // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest
69 // flag so that they are used from inside the APK at runtime. Defaults to true for android_test modules unless
70 // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to false for other
71 // module types where the native libraries are generally preinstalled outside the APK.
72 Use_embedded_native_libs *bool
Colin Cross46abdad2019-02-07 13:07:08 -080073
74 // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that
75 // they are used from inside the APK at runtime.
76 Use_embedded_dex *bool
Colin Cross47fa9d32019-03-26 10:51:39 -070077
78 // Forces native libraries to always be packaged into the APK,
79 // Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed.
80 // True for android_test* modules.
81 AlwaysPackageNativeLibs bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -070082}
83
Jaewoong Jung525443a2019-02-28 15:35:54 -080084// android_app properties that can be overridden by override_android_app
85type overridableAppProperties struct {
86 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
87 // or an android_app_certificate module name in the form ":module".
88 Certificate *string
Jaewoong Jung6f373f62019-03-13 10:13:24 -070089
90 // the package name of this app. The package name in the manifest file is used if one was not given.
91 Package_name *string
Jaewoong Jung525443a2019-02-28 15:35:54 -080092}
93
Colin Cross30e076a2015-04-13 13:58:27 -070094type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -070095 Library
96 aapt
Jaewoong Jung525443a2019-02-28 15:35:54 -080097 android.OverridableModuleBase
Colin Crossa97c5d32018-03-28 14:58:31 -070098
Jiyong Parkc00cbd92018-10-30 21:20:05 +090099 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -0700100
Colin Crossfabb6082018-02-20 17:22:23 -0800101 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -0700102
Jaewoong Jung525443a2019-02-28 15:35:54 -0800103 overridableAppProperties overridableAppProperties
104
Colin Crossa4f08812018-10-02 22:03:40 -0700105 installJniLibs []jniLib
Colin Crossf6237212018-10-29 23:14:58 -0700106
107 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800108
109 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
110 installApkName string
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800111
112 additionalAaptFlags []string
Colin Crosse1731a52017-12-14 11:22:55 -0800113}
114
Colin Cross89c31582018-04-30 15:55:11 -0700115func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
116 return nil
117}
118
Colin Cross66f78822018-05-02 12:58:28 -0700119func (a *AndroidApp) ExportedStaticPackages() android.Paths {
120 return nil
121}
122
Colin Crossa97c5d32018-03-28 14:58:31 -0700123var _ AndroidLibraryDependency = (*AndroidApp)(nil)
124
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900125type Certificate struct {
126 Pem, Key android.Path
Colin Cross30e076a2015-04-13 13:58:27 -0700127}
128
Colin Cross46c9b8b2017-06-22 16:51:17 -0700129func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
130 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700131
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800132 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700133 a.aapt.deps(ctx, sdkContext(a))
Colin Cross30e076a2015-04-13 13:58:27 -0700134 }
Colin Crossa4f08812018-10-02 22:03:40 -0700135
136 for _, jniTarget := range ctx.MultiTargets() {
137 variation := []blueprint.Variation{
138 {Mutator: "arch", Variation: jniTarget.String()},
139 {Mutator: "link", Variation: "shared"},
140 }
141 tag := &jniDependencyTag{
142 target: jniTarget,
143 }
144 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
145 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700146
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800147 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700148 if cert != "" {
149 ctx.AddDependency(ctx.Module(), certificateTag, cert)
150 }
151
152 for _, cert := range a.appProperties.Additional_certificates {
153 cert = android.SrcIsModule(cert)
154 if cert != "" {
155 ctx.AddDependency(ctx.Module(), certificateTag, cert)
156 } else {
157 ctx.PropertyErrorf("additional_certificates",
158 `must be names of android_app_certificate modules in the form ":module"`)
159 }
160 }
Colin Cross30e076a2015-04-13 13:58:27 -0700161}
162
Colin Cross46c9b8b2017-06-22 16:51:17 -0700163func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Sasha Smundak6ad77252019-05-01 13:16:22 -0700164 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
Colin Cross46abdad2019-02-07 13:07:08 -0800165 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
Colin Crossae5caf52018-05-22 11:11:52 -0700166 a.generateAndroidBuildActions(ctx)
167}
168
Sasha Smundak6ad77252019-05-01 13:16:22 -0700169// Returns true if the native libraries should be stored in the APK uncompressed and the
Colin Crosse4246ab2019-02-05 21:55:21 -0800170// extractNativeLibs application flag should be set to false in the manifest.
Sasha Smundak6ad77252019-05-01 13:16:22 -0700171func (a *AndroidApp) useEmbeddedNativeLibs(ctx android.ModuleContext) bool {
Colin Crosse4246ab2019-02-05 21:55:21 -0800172 minSdkVersion, err := sdkVersionToNumber(ctx, a.minSdkVersion())
173 if err != nil {
174 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
175 }
176
177 return minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)
178}
179
Colin Cross43f08db2018-11-12 10:13:39 -0800180// Returns whether this module should have the dex file stored uncompressed in the APK.
181func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800182 if Bool(a.appProperties.Use_embedded_dex) {
183 return true
184 }
185
Colin Cross5a0dcd52018-10-05 14:20:06 -0700186 if ctx.Config().UnbundledBuild() {
Colin Cross43f08db2018-11-12 10:13:39 -0800187 return false
Colin Cross5a0dcd52018-10-05 14:20:06 -0700188 }
189
Colin Cross5a0dcd52018-10-05 14:20:06 -0700190 // Uncompress dex in APKs of privileged apps, and modules used by privileged apps.
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000191 if ctx.Config().UncompressPrivAppDex() &&
Colin Cross5a0dcd52018-10-05 14:20:06 -0700192 (Bool(a.appProperties.Privileged) ||
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000193 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules())) {
194 return true
195 }
196
197 // Uncompress if the dex files is preopted on /system.
198 if !a.dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, a.dexpreopter.installPath)) {
199 return true
200 }
201
202 return false
Colin Cross5a0dcd52018-10-05 14:20:06 -0700203}
204
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800205func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
David Brazdild25060a2019-02-18 18:24:16 +0000206 a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis)
207
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800208 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800209
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800210 // 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 -0700211 hasProduct := false
212 for _, f := range a.aaptProperties.Aaptflags {
213 if strings.HasPrefix(f, "--product") {
214 hasProduct = true
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800215 break
Colin Crosse78dcd32018-04-19 15:25:19 -0700216 }
217 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700218 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800219 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700220 }
221
Dan Willemsen72be5902018-10-24 20:24:57 -0700222 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
223 // Product AAPT config
224 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800225 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700226 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700227
Dan Willemsen72be5902018-10-24 20:24:57 -0700228 // Product AAPT preferred config
229 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800230 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700231 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700232 }
233
Jiyong Park7f67f482019-01-05 12:57:48 +0900234 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700235 if overridden || a.overridableAppProperties.Package_name != nil {
236 // The product override variable has a priority over the package_name property.
237 if !overridden {
238 manifestPackageName = *a.overridableAppProperties.Package_name
239 }
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800240 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
Jiyong Park7f67f482019-01-05 12:57:48 +0900241 }
242
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800243 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
244
Colin Crosse560c4a2019-03-19 16:03:11 -0700245 a.aapt.splitNames = a.appProperties.Package_splits
246
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800247 a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700248
Colin Cross46c9b8b2017-06-22 16:51:17 -0700249 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700250 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800251}
Colin Cross30e076a2015-04-13 13:58:27 -0700252
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800253func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700254 var staticLibProguardFlagFiles android.Paths
255 ctx.VisitDirectDeps(func(m android.Module) {
256 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
257 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
258 }
259 })
260
261 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
262
263 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
264 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800265}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800266
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800267func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross43f08db2018-11-12 10:13:39 -0800268
269 var installDir string
270 if ctx.ModuleName() == "framework-res" {
271 // framework-res.apk is installed as system/framework/framework-res.apk
272 installDir = "framework"
273 } else if Bool(a.appProperties.Privileged) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800274 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800275 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800276 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800277 }
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800278 a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000279 a.dexpreopter.isInstallable = Bool(a.properties.Installable)
280 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
281 a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex
Colin Cross5a0dcd52018-10-05 14:20:06 -0700282
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800283 if ctx.ModuleName() != "framework-res" {
284 a.Module.compile(ctx, a.aaptSrcJar)
285 }
Colin Cross30e076a2015-04-13 13:58:27 -0700286
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800287 return a.maybeStrippedDexJarFile
288}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800289
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800290func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700291 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700292 if len(jniLibs) > 0 {
Colin Cross47fa9d32019-03-26 10:51:39 -0700293 embedJni := ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) ||
294 a.appProperties.AlwaysPackageNativeLibs
Colin Crossa4f08812018-10-02 22:03:40 -0700295 if embedJni {
296 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Sasha Smundak6ad77252019-05-01 13:16:22 -0700297 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.useEmbeddedNativeLibs(ctx))
Colin Crossa4f08812018-10-02 22:03:40 -0700298 } else {
299 a.installJniLibs = jniLibs
300 }
301 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800302 return jniJarFile
303}
Colin Crossa4f08812018-10-02 22:03:40 -0700304
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800305func (a *AndroidApp) certificateBuildActions(certificateDeps []Certificate, ctx android.ModuleContext) []Certificate {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800306 cert := a.getCertString(ctx)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700307 certModule := android.SrcIsModule(cert)
308 if certModule != "" {
309 a.certificate = certificateDeps[0]
310 certificateDeps = certificateDeps[1:]
311 } else if cert != "" {
312 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900313 a.certificate = Certificate{
Colin Crossbd01e2a2018-10-04 15:21:03 -0700314 defaultDir.Join(ctx, cert+".x509.pem"),
315 defaultDir.Join(ctx, cert+".pk8"),
316 }
317 } else {
318 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900319 a.certificate = Certificate{pem, key}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700320 }
321
Jeongik Chac9464142019-01-07 12:07:27 +0900322 if !a.Module.Platform() {
323 certPath := a.certificate.Pem.String()
324 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
325 if strings.HasPrefix(certPath, systemCertPath) {
326 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
327 whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
328
329 if enforceSystemCert && !inList(a.Module.Name(), whitelist) {
330 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
331 }
332 }
333 }
334
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800335 return append([]Certificate{a.certificate}, certificateDeps...)
336}
337
338func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800339 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800340 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800341
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800342 // Process all building blocks, from AAPT to certificates.
343 a.aaptBuildActions(ctx)
344
345 a.proguardBuildActions(ctx)
346
347 dexJarFile := a.dexBuildActions(ctx)
348
349 jniLibs, certificateDeps := a.collectAppDeps(ctx)
350 jniJarFile := a.jniBuildActions(jniLibs, ctx)
351
352 if ctx.Failed() {
353 return
354 }
355
356 certificates := a.certificateBuildActions(certificateDeps, ctx)
357
358 // Build a final signed app package.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800359 // TODO(jungjw): Consider changing this to installApkName.
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800360 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".apk")
361 CreateAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800362 a.outputFile = packageFile
363
Colin Crosse560c4a2019-03-19 16:03:11 -0700364 for _, split := range a.aapt.splits {
365 // Sign the split APKs
366 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"_"+split.suffix+".apk")
367 CreateAppPackage(ctx, packageFile, split.path, nil, nil, certificates)
368 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
369 }
370
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800371 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700372 bundleFile := android.PathForModuleOut(ctx, "base.zip")
373 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
374 a.bundleFile = bundleFile
375
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800376 // Install the app package.
Colin Crosse560c4a2019-03-19 16:03:11 -0700377 var installDir android.OutputPath
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800378 if ctx.ModuleName() == "framework-res" {
379 // framework-res.apk is installed as system/framework/framework-res.apk
Colin Crosse560c4a2019-03-19 16:03:11 -0700380 installDir = android.PathForModuleInstall(ctx, "framework")
Colin Cross16056062017-12-13 22:46:28 -0800381 } else if Bool(a.appProperties.Privileged) {
Colin Crosse560c4a2019-03-19 16:03:11 -0700382 installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800383 } else {
Colin Crosse560c4a2019-03-19 16:03:11 -0700384 installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
385 }
386
387 ctx.InstallFile(installDir, a.installApkName+".apk", a.outputFile)
388 for _, split := range a.aapt.splits {
389 ctx.InstallFile(installDir, a.installApkName+"_"+split.suffix+".apk", split.path)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800390 }
Colin Cross30e076a2015-04-13 13:58:27 -0700391}
392
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900393func (a *AndroidApp) collectAppDeps(ctx android.ModuleContext) ([]jniLib, []Certificate) {
Colin Crossa4f08812018-10-02 22:03:40 -0700394 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900395 var certificates []Certificate
Colin Crossa4f08812018-10-02 22:03:40 -0700396
397 ctx.VisitDirectDeps(func(module android.Module) {
398 otherName := ctx.OtherModuleName(module)
399 tag := ctx.OtherModuleDependencyTag(module)
400
401 if jniTag, ok := tag.(*jniDependencyTag); ok {
402 if dep, ok := module.(*cc.Module); ok {
403 lib := dep.OutputFile()
404 if lib.Valid() {
405 jniLibs = append(jniLibs, jniLib{
406 name: ctx.OtherModuleName(module),
407 path: lib.Path(),
408 target: jniTag.target,
409 })
410 } else {
411 ctx.ModuleErrorf("dependency %q missing output file", otherName)
412 }
413 } else {
414 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
415
416 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700417 } else if tag == certificateTag {
418 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900419 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700420 } else {
421 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
422 }
Colin Crossa4f08812018-10-02 22:03:40 -0700423 }
424 })
425
Colin Crossbd01e2a2018-10-04 15:21:03 -0700426 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700427}
428
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800429func (a *AndroidApp) getCertString(ctx android.BaseContext) string {
430 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
431 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000432 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800433 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800434 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800435}
436
Colin Cross1b16b0e2019-02-12 14:41:32 -0800437// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700438func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700439 module := &AndroidApp{}
440
Sasha Smundak2057f822019-04-16 17:16:58 -0700441 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross66dbc0b2017-12-28 12:23:20 -0800442 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
443
Colin Crossae5caf52018-05-22 11:11:52 -0700444 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700445 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700446
Colin Cross36242852017-06-23 15:06:31 -0700447 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700448 &module.Module.properties,
449 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800450 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700451 &module.Module.protoProperties,
452 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800453 &module.appProperties,
454 &module.overridableAppProperties)
Colin Cross36242852017-06-23 15:06:31 -0700455
Colin Crossa9d8bee2018-10-02 13:59:46 -0700456 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
457 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
458 })
459
Colin Crossa4f08812018-10-02 22:03:40 -0700460 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
461 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800462 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossa4f08812018-10-02 22:03:40 -0700463
Colin Cross36242852017-06-23 15:06:31 -0700464 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700465}
Colin Crossae5caf52018-05-22 11:11:52 -0700466
467type appTestProperties struct {
468 Instrumentation_for *string
469}
470
471type AndroidTest struct {
472 AndroidApp
473
474 appTestProperties appTestProperties
475
476 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700477
478 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700479 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700480}
481
482func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800483 // Check if the instrumentation target package is overridden before generating build actions.
484 if a.appTestProperties.Instrumentation_for != nil {
485 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
486 if overridden {
487 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
488 }
489 }
Sasha Smundak6ad77252019-05-01 13:16:22 -0700490 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
491 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
Colin Crossae5caf52018-05-22 11:11:52 -0700492 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700493
yangbill4f41bc22019-02-13 21:45:47 +0800494 a.testConfig = tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config, a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites)
Colin Cross8a497952019-03-05 22:25:09 -0800495 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700496}
497
498func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -0700499 a.AndroidApp.DepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -0700500 if a.appTestProperties.Instrumentation_for != nil {
501 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
502 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
503 // use instrumentationForTag instead of libTag.
504 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
505 }
Colin Crossae5caf52018-05-22 11:11:52 -0700506}
507
Colin Cross1b16b0e2019-02-12 14:41:32 -0800508// android_test compiles test sources and Android resources into an Android application package `.apk` file and
509// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -0700510func AndroidTestFactory() android.Module {
511 module := &AndroidTest{}
512
Sasha Smundak2057f822019-04-16 17:16:58 -0700513 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross5067db92018-09-17 16:46:35 -0700514
515 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700516 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800517 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700518 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800519 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -0700520
521 module.AddProperties(
522 &module.Module.properties,
523 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800524 &module.Module.dexpreoptProperties,
Colin Crossae5caf52018-05-22 11:11:52 -0700525 &module.Module.protoProperties,
526 &module.aaptProperties,
527 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700528 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800529 &module.overridableAppProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700530 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700531
Colin Crossa4f08812018-10-02 22:03:40 -0700532 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
533 android.InitDefaultableModule(module)
Colin Crossae5caf52018-05-22 11:11:52 -0700534 return module
535}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700536
Colin Cross252fc6f2018-10-04 15:22:03 -0700537type appTestHelperAppProperties struct {
538 // list of compatibility suites (for example "cts", "vts") that the module should be
539 // installed into.
540 Test_suites []string `android:"arch_variant"`
541}
542
543type AndroidTestHelperApp struct {
544 AndroidApp
545
546 appTestHelperAppProperties appTestHelperAppProperties
547}
548
Colin Cross1b16b0e2019-02-12 14:41:32 -0800549// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
550// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
551// test.
Colin Cross252fc6f2018-10-04 15:22:03 -0700552func AndroidTestHelperAppFactory() android.Module {
553 module := &AndroidTestHelperApp{}
554
Sasha Smundak2057f822019-04-16 17:16:58 -0700555 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700556
557 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800558 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700559 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800560 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700561
562 module.AddProperties(
563 &module.Module.properties,
564 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800565 &module.Module.dexpreoptProperties,
Colin Cross252fc6f2018-10-04 15:22:03 -0700566 &module.Module.protoProperties,
567 &module.aaptProperties,
568 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800569 &module.appTestHelperAppProperties,
570 &module.overridableAppProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -0700571
572 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
573 android.InitDefaultableModule(module)
574 return module
575}
576
Colin Crossbd01e2a2018-10-04 15:21:03 -0700577type AndroidAppCertificate struct {
578 android.ModuleBase
579 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900580 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -0700581}
582
583type AndroidAppCertificateProperties struct {
584 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
585 Certificate *string
586}
587
Colin Cross1b16b0e2019-02-12 14:41:32 -0800588// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
589// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -0700590func AndroidAppCertificateFactory() android.Module {
591 module := &AndroidAppCertificate{}
592 module.AddProperties(&module.properties)
593 android.InitAndroidModule(module)
594 return module
595}
596
Colin Crossbd01e2a2018-10-04 15:21:03 -0700597func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
598 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900599 c.Certificate = Certificate{
Colin Crossbd01e2a2018-10-04 15:21:03 -0700600 android.PathForModuleSrc(ctx, cert+".x509.pem"),
601 android.PathForModuleSrc(ctx, cert+".pk8"),
602 }
603}
Jaewoong Jung525443a2019-02-28 15:35:54 -0800604
605type OverrideAndroidApp struct {
606 android.ModuleBase
607 android.OverrideModuleBase
608}
609
610func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
611 // All the overrides happen in the base module.
612 // TODO(jungjw): Check the base module type.
613}
614
615// override_android_app is used to create an android_app module based on another android_app by overriding
616// some of its properties.
617func OverrideAndroidAppModuleFactory() android.Module {
618 m := &OverrideAndroidApp{}
619 m.AddProperties(&overridableAppProperties{})
620
621 android.InitAndroidModule(m)
622 android.InitOverrideModule(m)
623 return m
624}