blob: 96594df36624e0933f5ed1ca73d4ba62287efc0d [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 Cross7d5136f2015-05-11 13:39:40 -070077}
78
Jaewoong Jung525443a2019-02-28 15:35:54 -080079// android_app properties that can be overridden by override_android_app
80type overridableAppProperties struct {
81 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
82 // or an android_app_certificate module name in the form ":module".
83 Certificate *string
Jaewoong Jung6f373f62019-03-13 10:13:24 -070084
85 // the package name of this app. The package name in the manifest file is used if one was not given.
86 Package_name *string
Jaewoong Jung525443a2019-02-28 15:35:54 -080087}
88
Colin Cross30e076a2015-04-13 13:58:27 -070089type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -070090 Library
91 aapt
Jaewoong Jung525443a2019-02-28 15:35:54 -080092 android.OverridableModuleBase
Colin Crossa97c5d32018-03-28 14:58:31 -070093
Jiyong Parkc00cbd92018-10-30 21:20:05 +090094 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -070095
Colin Crossfabb6082018-02-20 17:22:23 -080096 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -070097
Jaewoong Jung525443a2019-02-28 15:35:54 -080098 overridableAppProperties overridableAppProperties
99
Colin Crossa4f08812018-10-02 22:03:40 -0700100 installJniLibs []jniLib
Colin Crossf6237212018-10-29 23:14:58 -0700101
102 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800103
104 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
105 installApkName string
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800106
107 additionalAaptFlags []string
Colin Crosse1731a52017-12-14 11:22:55 -0800108}
109
Colin Cross89c31582018-04-30 15:55:11 -0700110func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
111 return nil
112}
113
Colin Cross66f78822018-05-02 12:58:28 -0700114func (a *AndroidApp) ExportedStaticPackages() android.Paths {
115 return nil
116}
117
Colin Crossa97c5d32018-03-28 14:58:31 -0700118var _ AndroidLibraryDependency = (*AndroidApp)(nil)
119
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900120type Certificate struct {
121 Pem, Key android.Path
Colin Cross30e076a2015-04-13 13:58:27 -0700122}
123
Colin Cross46c9b8b2017-06-22 16:51:17 -0700124func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
125 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700126
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800127 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700128 a.aapt.deps(ctx, sdkContext(a))
Colin Cross30e076a2015-04-13 13:58:27 -0700129 }
Colin Crossa4f08812018-10-02 22:03:40 -0700130
131 for _, jniTarget := range ctx.MultiTargets() {
132 variation := []blueprint.Variation{
133 {Mutator: "arch", Variation: jniTarget.String()},
134 {Mutator: "link", Variation: "shared"},
135 }
136 tag := &jniDependencyTag{
137 target: jniTarget,
138 }
139 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
140 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700141
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800142 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700143 if cert != "" {
144 ctx.AddDependency(ctx.Module(), certificateTag, cert)
145 }
146
147 for _, cert := range a.appProperties.Additional_certificates {
148 cert = android.SrcIsModule(cert)
149 if cert != "" {
150 ctx.AddDependency(ctx.Module(), certificateTag, cert)
151 } else {
152 ctx.PropertyErrorf("additional_certificates",
153 `must be names of android_app_certificate modules in the form ":module"`)
154 }
155 }
Colin Cross30e076a2015-04-13 13:58:27 -0700156}
157
Colin Cross46c9b8b2017-06-22 16:51:17 -0700158func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800159 a.aapt.uncompressedJNI = a.shouldUncompressJNI(ctx)
Colin Cross46abdad2019-02-07 13:07:08 -0800160 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
Colin Crossae5caf52018-05-22 11:11:52 -0700161 a.generateAndroidBuildActions(ctx)
162}
163
Colin Crosse4246ab2019-02-05 21:55:21 -0800164// shouldUncompressJNI returns true if the native libraries should be stored in the APK uncompressed and the
165// extractNativeLibs application flag should be set to false in the manifest.
166func (a *AndroidApp) shouldUncompressJNI(ctx android.ModuleContext) bool {
167 minSdkVersion, err := sdkVersionToNumber(ctx, a.minSdkVersion())
168 if err != nil {
169 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
170 }
171
172 return minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)
173}
174
Colin Cross43f08db2018-11-12 10:13:39 -0800175// Returns whether this module should have the dex file stored uncompressed in the APK.
176func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800177 if Bool(a.appProperties.Use_embedded_dex) {
178 return true
179 }
180
Colin Cross5a0dcd52018-10-05 14:20:06 -0700181 if ctx.Config().UnbundledBuild() {
Colin Cross43f08db2018-11-12 10:13:39 -0800182 return false
Colin Cross5a0dcd52018-10-05 14:20:06 -0700183 }
184
Colin Cross5a0dcd52018-10-05 14:20:06 -0700185 // Uncompress dex in APKs of privileged apps, and modules used by privileged apps.
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000186 if ctx.Config().UncompressPrivAppDex() &&
Colin Cross5a0dcd52018-10-05 14:20:06 -0700187 (Bool(a.appProperties.Privileged) ||
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000188 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules())) {
189 return true
190 }
191
192 // Uncompress if the dex files is preopted on /system.
193 if !a.dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, a.dexpreopter.installPath)) {
194 return true
195 }
196
197 return false
Colin Cross5a0dcd52018-10-05 14:20:06 -0700198}
199
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800200func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
David Brazdild25060a2019-02-18 18:24:16 +0000201 a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis)
202
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800203 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800204
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800205 // 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 -0700206 hasProduct := false
207 for _, f := range a.aaptProperties.Aaptflags {
208 if strings.HasPrefix(f, "--product") {
209 hasProduct = true
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800210 break
Colin Crosse78dcd32018-04-19 15:25:19 -0700211 }
212 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700213 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800214 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700215 }
216
Dan Willemsen72be5902018-10-24 20:24:57 -0700217 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
218 // Product AAPT config
219 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800220 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700221 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700222
Dan Willemsen72be5902018-10-24 20:24:57 -0700223 // Product AAPT preferred config
224 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800225 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700226 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700227 }
228
Jiyong Park7f67f482019-01-05 12:57:48 +0900229 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700230 if overridden || a.overridableAppProperties.Package_name != nil {
231 // The product override variable has a priority over the package_name property.
232 if !overridden {
233 manifestPackageName = *a.overridableAppProperties.Package_name
234 }
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800235 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
Jiyong Park7f67f482019-01-05 12:57:48 +0900236 }
237
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800238 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
239
Colin Crosse560c4a2019-03-19 16:03:11 -0700240 a.aapt.splitNames = a.appProperties.Package_splits
241
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800242 a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700243
Colin Cross46c9b8b2017-06-22 16:51:17 -0700244 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700245 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800246}
Colin Cross30e076a2015-04-13 13:58:27 -0700247
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800248func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700249 var staticLibProguardFlagFiles android.Paths
250 ctx.VisitDirectDeps(func(m android.Module) {
251 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
252 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
253 }
254 })
255
256 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
257
258 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
259 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800260}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800261
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800262func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross43f08db2018-11-12 10:13:39 -0800263
264 var installDir string
265 if ctx.ModuleName() == "framework-res" {
266 // framework-res.apk is installed as system/framework/framework-res.apk
267 installDir = "framework"
268 } else if Bool(a.appProperties.Privileged) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800269 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800270 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800271 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800272 }
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800273 a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000274 a.dexpreopter.isInstallable = Bool(a.properties.Installable)
275 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
276 a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex
Colin Cross5a0dcd52018-10-05 14:20:06 -0700277
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800278 if ctx.ModuleName() != "framework-res" {
279 a.Module.compile(ctx, a.aaptSrcJar)
280 }
Colin Cross30e076a2015-04-13 13:58:27 -0700281
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800282 return a.maybeStrippedDexJarFile
283}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800284
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800285func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700286 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700287 if len(jniLibs) > 0 {
Colin Crosse4246ab2019-02-05 21:55:21 -0800288 embedJni := ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs)
Colin Crossa4f08812018-10-02 22:03:40 -0700289 if embedJni {
290 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Colin Crosse4246ab2019-02-05 21:55:21 -0800291 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.shouldUncompressJNI(ctx))
Colin Crossa4f08812018-10-02 22:03:40 -0700292 } else {
293 a.installJniLibs = jniLibs
294 }
295 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800296 return jniJarFile
297}
Colin Crossa4f08812018-10-02 22:03:40 -0700298
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800299func (a *AndroidApp) certificateBuildActions(certificateDeps []Certificate, ctx android.ModuleContext) []Certificate {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800300 cert := a.getCertString(ctx)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700301 certModule := android.SrcIsModule(cert)
302 if certModule != "" {
303 a.certificate = certificateDeps[0]
304 certificateDeps = certificateDeps[1:]
305 } else if cert != "" {
306 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900307 a.certificate = Certificate{
Colin Crossbd01e2a2018-10-04 15:21:03 -0700308 defaultDir.Join(ctx, cert+".x509.pem"),
309 defaultDir.Join(ctx, cert+".pk8"),
310 }
311 } else {
312 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900313 a.certificate = Certificate{pem, key}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700314 }
315
Jeongik Chac9464142019-01-07 12:07:27 +0900316 if !a.Module.Platform() {
317 certPath := a.certificate.Pem.String()
318 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
319 if strings.HasPrefix(certPath, systemCertPath) {
320 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
321 whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
322
323 if enforceSystemCert && !inList(a.Module.Name(), whitelist) {
324 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
325 }
326 }
327 }
328
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800329 return append([]Certificate{a.certificate}, certificateDeps...)
330}
331
332func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800333 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800334 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800335
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800336 // Process all building blocks, from AAPT to certificates.
337 a.aaptBuildActions(ctx)
338
339 a.proguardBuildActions(ctx)
340
341 dexJarFile := a.dexBuildActions(ctx)
342
343 jniLibs, certificateDeps := a.collectAppDeps(ctx)
344 jniJarFile := a.jniBuildActions(jniLibs, ctx)
345
346 if ctx.Failed() {
347 return
348 }
349
350 certificates := a.certificateBuildActions(certificateDeps, ctx)
351
352 // Build a final signed app package.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800353 // TODO(jungjw): Consider changing this to installApkName.
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800354 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".apk")
355 CreateAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800356 a.outputFile = packageFile
357
Colin Crosse560c4a2019-03-19 16:03:11 -0700358 for _, split := range a.aapt.splits {
359 // Sign the split APKs
360 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"_"+split.suffix+".apk")
361 CreateAppPackage(ctx, packageFile, split.path, nil, nil, certificates)
362 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
363 }
364
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800365 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700366 bundleFile := android.PathForModuleOut(ctx, "base.zip")
367 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
368 a.bundleFile = bundleFile
369
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800370 // Install the app package.
Colin Crosse560c4a2019-03-19 16:03:11 -0700371 var installDir android.OutputPath
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800372 if ctx.ModuleName() == "framework-res" {
373 // framework-res.apk is installed as system/framework/framework-res.apk
Colin Crosse560c4a2019-03-19 16:03:11 -0700374 installDir = android.PathForModuleInstall(ctx, "framework")
Colin Cross16056062017-12-13 22:46:28 -0800375 } else if Bool(a.appProperties.Privileged) {
Colin Crosse560c4a2019-03-19 16:03:11 -0700376 installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800377 } else {
Colin Crosse560c4a2019-03-19 16:03:11 -0700378 installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
379 }
380
381 ctx.InstallFile(installDir, a.installApkName+".apk", a.outputFile)
382 for _, split := range a.aapt.splits {
383 ctx.InstallFile(installDir, a.installApkName+"_"+split.suffix+".apk", split.path)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800384 }
Colin Cross30e076a2015-04-13 13:58:27 -0700385}
386
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900387func (a *AndroidApp) collectAppDeps(ctx android.ModuleContext) ([]jniLib, []Certificate) {
Colin Crossa4f08812018-10-02 22:03:40 -0700388 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900389 var certificates []Certificate
Colin Crossa4f08812018-10-02 22:03:40 -0700390
391 ctx.VisitDirectDeps(func(module android.Module) {
392 otherName := ctx.OtherModuleName(module)
393 tag := ctx.OtherModuleDependencyTag(module)
394
395 if jniTag, ok := tag.(*jniDependencyTag); ok {
396 if dep, ok := module.(*cc.Module); ok {
397 lib := dep.OutputFile()
398 if lib.Valid() {
399 jniLibs = append(jniLibs, jniLib{
400 name: ctx.OtherModuleName(module),
401 path: lib.Path(),
402 target: jniTag.target,
403 })
404 } else {
405 ctx.ModuleErrorf("dependency %q missing output file", otherName)
406 }
407 } else {
408 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
409
410 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700411 } else if tag == certificateTag {
412 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900413 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700414 } else {
415 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
416 }
Colin Crossa4f08812018-10-02 22:03:40 -0700417 }
418 })
419
Colin Crossbd01e2a2018-10-04 15:21:03 -0700420 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700421}
422
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800423func (a *AndroidApp) getCertString(ctx android.BaseContext) string {
424 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
425 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000426 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800427 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800428 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800429}
430
Colin Cross1b16b0e2019-02-12 14:41:32 -0800431// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700432func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700433 module := &AndroidApp{}
434
Colin Cross66dbc0b2017-12-28 12:23:20 -0800435 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
436 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
437
Colin Crossae5caf52018-05-22 11:11:52 -0700438 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700439 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700440
Colin Cross36242852017-06-23 15:06:31 -0700441 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700442 &module.Module.properties,
443 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800444 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700445 &module.Module.protoProperties,
446 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800447 &module.appProperties,
448 &module.overridableAppProperties)
Colin Cross36242852017-06-23 15:06:31 -0700449
Colin Crossa9d8bee2018-10-02 13:59:46 -0700450 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
451 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
452 })
453
Colin Crossa4f08812018-10-02 22:03:40 -0700454 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
455 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800456 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossa4f08812018-10-02 22:03:40 -0700457
Colin Cross36242852017-06-23 15:06:31 -0700458 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700459}
Colin Crossae5caf52018-05-22 11:11:52 -0700460
461type appTestProperties struct {
462 Instrumentation_for *string
463}
464
465type AndroidTest struct {
466 AndroidApp
467
468 appTestProperties appTestProperties
469
470 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700471
472 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700473 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700474}
475
476func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800477 // Check if the instrumentation target package is overridden before generating build actions.
478 if a.appTestProperties.Instrumentation_for != nil {
479 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
480 if overridden {
481 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
482 }
483 }
Colin Crossae5caf52018-05-22 11:11:52 -0700484 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700485
yangbill4f41bc22019-02-13 21:45:47 +0800486 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 -0800487 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700488}
489
490func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -0700491 a.AndroidApp.DepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -0700492 if a.appTestProperties.Instrumentation_for != nil {
493 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
494 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
495 // use instrumentationForTag instead of libTag.
496 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
497 }
Colin Crossae5caf52018-05-22 11:11:52 -0700498}
499
Colin Cross1b16b0e2019-02-12 14:41:32 -0800500// android_test compiles test sources and Android resources into an Android application package `.apk` file and
501// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -0700502func AndroidTestFactory() android.Module {
503 module := &AndroidTest{}
504
505 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
Colin Cross5067db92018-09-17 16:46:35 -0700506
507 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700508 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800509 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross43f08db2018-11-12 10:13:39 -0800510 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -0700511
512 module.AddProperties(
513 &module.Module.properties,
514 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800515 &module.Module.dexpreoptProperties,
Colin Crossae5caf52018-05-22 11:11:52 -0700516 &module.Module.protoProperties,
517 &module.aaptProperties,
518 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700519 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800520 &module.overridableAppProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700521 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700522
Colin Crossa4f08812018-10-02 22:03:40 -0700523 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
524 android.InitDefaultableModule(module)
Colin Crossae5caf52018-05-22 11:11:52 -0700525 return module
526}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700527
Colin Cross252fc6f2018-10-04 15:22:03 -0700528type appTestHelperAppProperties struct {
529 // list of compatibility suites (for example "cts", "vts") that the module should be
530 // installed into.
531 Test_suites []string `android:"arch_variant"`
532}
533
534type AndroidTestHelperApp struct {
535 AndroidApp
536
537 appTestHelperAppProperties appTestHelperAppProperties
538}
539
Colin Cross1b16b0e2019-02-12 14:41:32 -0800540// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
541// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
542// test.
Colin Cross252fc6f2018-10-04 15:22:03 -0700543func AndroidTestHelperAppFactory() android.Module {
544 module := &AndroidTestHelperApp{}
545
546 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
547
548 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800549 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross43f08db2018-11-12 10:13:39 -0800550 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700551
552 module.AddProperties(
553 &module.Module.properties,
554 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800555 &module.Module.dexpreoptProperties,
Colin Cross252fc6f2018-10-04 15:22:03 -0700556 &module.Module.protoProperties,
557 &module.aaptProperties,
558 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800559 &module.appTestHelperAppProperties,
560 &module.overridableAppProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -0700561
562 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
563 android.InitDefaultableModule(module)
564 return module
565}
566
Colin Crossbd01e2a2018-10-04 15:21:03 -0700567type AndroidAppCertificate struct {
568 android.ModuleBase
569 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900570 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -0700571}
572
573type AndroidAppCertificateProperties struct {
574 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
575 Certificate *string
576}
577
Colin Cross1b16b0e2019-02-12 14:41:32 -0800578// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
579// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -0700580func AndroidAppCertificateFactory() android.Module {
581 module := &AndroidAppCertificate{}
582 module.AddProperties(&module.properties)
583 android.InitAndroidModule(module)
584 return module
585}
586
Colin Crossbd01e2a2018-10-04 15:21:03 -0700587func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
588 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900589 c.Certificate = Certificate{
Colin Crossbd01e2a2018-10-04 15:21:03 -0700590 android.PathForModuleSrc(ctx, cert+".x509.pem"),
591 android.PathForModuleSrc(ctx, cert+".pk8"),
592 }
593}
Jaewoong Jung525443a2019-02-28 15:35:54 -0800594
595type OverrideAndroidApp struct {
596 android.ModuleBase
597 android.OverrideModuleBase
598}
599
600func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
601 // All the overrides happen in the base module.
602 // TODO(jungjw): Check the base module type.
603}
604
605// override_android_app is used to create an android_app module based on another android_app by overriding
606// some of its properties.
607func OverrideAndroidAppModuleFactory() android.Module {
608 m := &OverrideAndroidApp{}
609 m.AddProperties(&overridableAppProperties{})
610
611 android.InitAndroidModule(m)
612 android.InitOverrideModule(m)
613 return m
614}