blob: b31f2323b9544f654ddb353e0f60f2df9679432c [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
84}
85
Colin Cross30e076a2015-04-13 13:58:27 -070086type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -070087 Library
88 aapt
Jaewoong Jung525443a2019-02-28 15:35:54 -080089 android.OverridableModuleBase
Colin Crossa97c5d32018-03-28 14:58:31 -070090
Jiyong Parkc00cbd92018-10-30 21:20:05 +090091 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -070092
Colin Crossfabb6082018-02-20 17:22:23 -080093 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -070094
Jaewoong Jung525443a2019-02-28 15:35:54 -080095 overridableAppProperties overridableAppProperties
96
Colin Crossa4f08812018-10-02 22:03:40 -070097 installJniLibs []jniLib
Colin Crossf6237212018-10-29 23:14:58 -070098
99 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800100
101 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
102 installApkName string
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800103
104 additionalAaptFlags []string
Colin Crosse1731a52017-12-14 11:22:55 -0800105}
106
Colin Cross89c31582018-04-30 15:55:11 -0700107func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
108 return nil
109}
110
Colin Cross66f78822018-05-02 12:58:28 -0700111func (a *AndroidApp) ExportedStaticPackages() android.Paths {
112 return nil
113}
114
Colin Crossa97c5d32018-03-28 14:58:31 -0700115var _ AndroidLibraryDependency = (*AndroidApp)(nil)
116
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900117type Certificate struct {
118 Pem, Key android.Path
Colin Cross30e076a2015-04-13 13:58:27 -0700119}
120
Colin Cross46c9b8b2017-06-22 16:51:17 -0700121func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
122 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700123
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800124 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700125 a.aapt.deps(ctx, sdkContext(a))
Colin Cross30e076a2015-04-13 13:58:27 -0700126 }
Colin Crossa4f08812018-10-02 22:03:40 -0700127
128 for _, jniTarget := range ctx.MultiTargets() {
129 variation := []blueprint.Variation{
130 {Mutator: "arch", Variation: jniTarget.String()},
131 {Mutator: "link", Variation: "shared"},
132 }
133 tag := &jniDependencyTag{
134 target: jniTarget,
135 }
136 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
137 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700138
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800139 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700140 if cert != "" {
141 ctx.AddDependency(ctx.Module(), certificateTag, cert)
142 }
143
144 for _, cert := range a.appProperties.Additional_certificates {
145 cert = android.SrcIsModule(cert)
146 if cert != "" {
147 ctx.AddDependency(ctx.Module(), certificateTag, cert)
148 } else {
149 ctx.PropertyErrorf("additional_certificates",
150 `must be names of android_app_certificate modules in the form ":module"`)
151 }
152 }
Colin Cross30e076a2015-04-13 13:58:27 -0700153}
154
Colin Cross46c9b8b2017-06-22 16:51:17 -0700155func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800156 a.aapt.uncompressedJNI = a.shouldUncompressJNI(ctx)
Colin Cross46abdad2019-02-07 13:07:08 -0800157 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
Colin Crossae5caf52018-05-22 11:11:52 -0700158 a.generateAndroidBuildActions(ctx)
159}
160
Colin Crosse4246ab2019-02-05 21:55:21 -0800161// shouldUncompressJNI returns true if the native libraries should be stored in the APK uncompressed and the
162// extractNativeLibs application flag should be set to false in the manifest.
163func (a *AndroidApp) shouldUncompressJNI(ctx android.ModuleContext) bool {
164 minSdkVersion, err := sdkVersionToNumber(ctx, a.minSdkVersion())
165 if err != nil {
166 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
167 }
168
169 return minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)
170}
171
Colin Cross43f08db2018-11-12 10:13:39 -0800172// Returns whether this module should have the dex file stored uncompressed in the APK.
173func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800174 if Bool(a.appProperties.Use_embedded_dex) {
175 return true
176 }
177
Colin Cross5a0dcd52018-10-05 14:20:06 -0700178 if ctx.Config().UnbundledBuild() {
Colin Cross43f08db2018-11-12 10:13:39 -0800179 return false
Colin Cross5a0dcd52018-10-05 14:20:06 -0700180 }
181
Colin Cross5a0dcd52018-10-05 14:20:06 -0700182 // Uncompress dex in APKs of privileged apps, and modules used by privileged apps.
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000183 if ctx.Config().UncompressPrivAppDex() &&
Colin Cross5a0dcd52018-10-05 14:20:06 -0700184 (Bool(a.appProperties.Privileged) ||
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000185 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules())) {
186 return true
187 }
188
189 // Uncompress if the dex files is preopted on /system.
190 if !a.dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, a.dexpreopter.installPath)) {
191 return true
192 }
193
194 return false
Colin Cross5a0dcd52018-10-05 14:20:06 -0700195}
196
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800197func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
David Brazdild25060a2019-02-18 18:24:16 +0000198 a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis)
199
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800200 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800201
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800202 // 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 -0700203 hasProduct := false
204 for _, f := range a.aaptProperties.Aaptflags {
205 if strings.HasPrefix(f, "--product") {
206 hasProduct = true
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800207 break
Colin Crosse78dcd32018-04-19 15:25:19 -0700208 }
209 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700210 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800211 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700212 }
213
Dan Willemsen72be5902018-10-24 20:24:57 -0700214 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
215 // Product AAPT config
216 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800217 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700218 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700219
Dan Willemsen72be5902018-10-24 20:24:57 -0700220 // Product AAPT preferred config
221 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800222 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700223 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700224 }
225
Colin Crossa97c5d32018-03-28 14:58:31 -0700226 // TODO: LOCAL_PACKAGE_OVERRIDES
227 // $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800228
Jiyong Park7f67f482019-01-05 12:57:48 +0900229 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
230 if overridden {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800231 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
Jiyong Park7f67f482019-01-05 12:57:48 +0900232 }
233
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800234 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
235
Colin Crosse560c4a2019-03-19 16:03:11 -0700236 a.aapt.splitNames = a.appProperties.Package_splits
237
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800238 a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700239
Colin Cross46c9b8b2017-06-22 16:51:17 -0700240 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700241 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800242}
Colin Cross30e076a2015-04-13 13:58:27 -0700243
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800244func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700245 var staticLibProguardFlagFiles android.Paths
246 ctx.VisitDirectDeps(func(m android.Module) {
247 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
248 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
249 }
250 })
251
252 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
253
254 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
255 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800256}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800257
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800258func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross43f08db2018-11-12 10:13:39 -0800259
260 var installDir string
261 if ctx.ModuleName() == "framework-res" {
262 // framework-res.apk is installed as system/framework/framework-res.apk
263 installDir = "framework"
264 } else if Bool(a.appProperties.Privileged) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800265 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800266 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800267 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800268 }
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800269 a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000270 a.dexpreopter.isInstallable = Bool(a.properties.Installable)
271 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
272 a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex
Colin Cross5a0dcd52018-10-05 14:20:06 -0700273
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800274 if ctx.ModuleName() != "framework-res" {
275 a.Module.compile(ctx, a.aaptSrcJar)
276 }
Colin Cross30e076a2015-04-13 13:58:27 -0700277
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800278 return a.maybeStrippedDexJarFile
279}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800280
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800281func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700282 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700283 if len(jniLibs) > 0 {
Colin Crosse4246ab2019-02-05 21:55:21 -0800284 embedJni := ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs)
Colin Crossa4f08812018-10-02 22:03:40 -0700285 if embedJni {
286 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Colin Crosse4246ab2019-02-05 21:55:21 -0800287 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.shouldUncompressJNI(ctx))
Colin Crossa4f08812018-10-02 22:03:40 -0700288 } else {
289 a.installJniLibs = jniLibs
290 }
291 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800292 return jniJarFile
293}
Colin Crossa4f08812018-10-02 22:03:40 -0700294
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800295func (a *AndroidApp) certificateBuildActions(certificateDeps []Certificate, ctx android.ModuleContext) []Certificate {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800296 cert := a.getCertString(ctx)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700297 certModule := android.SrcIsModule(cert)
298 if certModule != "" {
299 a.certificate = certificateDeps[0]
300 certificateDeps = certificateDeps[1:]
301 } else if cert != "" {
302 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900303 a.certificate = Certificate{
Colin Crossbd01e2a2018-10-04 15:21:03 -0700304 defaultDir.Join(ctx, cert+".x509.pem"),
305 defaultDir.Join(ctx, cert+".pk8"),
306 }
307 } else {
308 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900309 a.certificate = Certificate{pem, key}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700310 }
311
Jeongik Chac9464142019-01-07 12:07:27 +0900312 if !a.Module.Platform() {
313 certPath := a.certificate.Pem.String()
314 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
315 if strings.HasPrefix(certPath, systemCertPath) {
316 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
317 whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
318
319 if enforceSystemCert && !inList(a.Module.Name(), whitelist) {
320 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
321 }
322 }
323 }
324
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800325 return append([]Certificate{a.certificate}, certificateDeps...)
326}
327
328func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800329 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800330 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800331
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800332 // Process all building blocks, from AAPT to certificates.
333 a.aaptBuildActions(ctx)
334
335 a.proguardBuildActions(ctx)
336
337 dexJarFile := a.dexBuildActions(ctx)
338
339 jniLibs, certificateDeps := a.collectAppDeps(ctx)
340 jniJarFile := a.jniBuildActions(jniLibs, ctx)
341
342 if ctx.Failed() {
343 return
344 }
345
346 certificates := a.certificateBuildActions(certificateDeps, ctx)
347
348 // Build a final signed app package.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800349 // TODO(jungjw): Consider changing this to installApkName.
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800350 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".apk")
351 CreateAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800352 a.outputFile = packageFile
353
Colin Crosse560c4a2019-03-19 16:03:11 -0700354 for _, split := range a.aapt.splits {
355 // Sign the split APKs
356 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"_"+split.suffix+".apk")
357 CreateAppPackage(ctx, packageFile, split.path, nil, nil, certificates)
358 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
359 }
360
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800361 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700362 bundleFile := android.PathForModuleOut(ctx, "base.zip")
363 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
364 a.bundleFile = bundleFile
365
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800366 // Install the app package.
Colin Crosse560c4a2019-03-19 16:03:11 -0700367 var installDir android.OutputPath
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800368 if ctx.ModuleName() == "framework-res" {
369 // framework-res.apk is installed as system/framework/framework-res.apk
Colin Crosse560c4a2019-03-19 16:03:11 -0700370 installDir = android.PathForModuleInstall(ctx, "framework")
Colin Cross16056062017-12-13 22:46:28 -0800371 } else if Bool(a.appProperties.Privileged) {
Colin Crosse560c4a2019-03-19 16:03:11 -0700372 installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800373 } else {
Colin Crosse560c4a2019-03-19 16:03:11 -0700374 installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
375 }
376
377 ctx.InstallFile(installDir, a.installApkName+".apk", a.outputFile)
378 for _, split := range a.aapt.splits {
379 ctx.InstallFile(installDir, a.installApkName+"_"+split.suffix+".apk", split.path)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800380 }
Colin Cross30e076a2015-04-13 13:58:27 -0700381}
382
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900383func (a *AndroidApp) collectAppDeps(ctx android.ModuleContext) ([]jniLib, []Certificate) {
Colin Crossa4f08812018-10-02 22:03:40 -0700384 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900385 var certificates []Certificate
Colin Crossa4f08812018-10-02 22:03:40 -0700386
387 ctx.VisitDirectDeps(func(module android.Module) {
388 otherName := ctx.OtherModuleName(module)
389 tag := ctx.OtherModuleDependencyTag(module)
390
391 if jniTag, ok := tag.(*jniDependencyTag); ok {
392 if dep, ok := module.(*cc.Module); ok {
393 lib := dep.OutputFile()
394 if lib.Valid() {
395 jniLibs = append(jniLibs, jniLib{
396 name: ctx.OtherModuleName(module),
397 path: lib.Path(),
398 target: jniTag.target,
399 })
400 } else {
401 ctx.ModuleErrorf("dependency %q missing output file", otherName)
402 }
403 } else {
404 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
405
406 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700407 } else if tag == certificateTag {
408 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900409 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700410 } else {
411 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
412 }
Colin Crossa4f08812018-10-02 22:03:40 -0700413 }
414 })
415
Colin Crossbd01e2a2018-10-04 15:21:03 -0700416 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700417}
418
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800419func (a *AndroidApp) getCertString(ctx android.BaseContext) string {
420 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
421 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000422 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800423 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800424 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800425}
426
Colin Cross1b16b0e2019-02-12 14:41:32 -0800427// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700428func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700429 module := &AndroidApp{}
430
Colin Cross66dbc0b2017-12-28 12:23:20 -0800431 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
432 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
433
Colin Crossae5caf52018-05-22 11:11:52 -0700434 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700435 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700436
Colin Cross36242852017-06-23 15:06:31 -0700437 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700438 &module.Module.properties,
439 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800440 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700441 &module.Module.protoProperties,
442 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800443 &module.appProperties,
444 &module.overridableAppProperties)
Colin Cross36242852017-06-23 15:06:31 -0700445
Colin Crossa9d8bee2018-10-02 13:59:46 -0700446 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
447 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
448 })
449
Colin Crossa4f08812018-10-02 22:03:40 -0700450 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
451 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800452 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossa4f08812018-10-02 22:03:40 -0700453
Colin Cross36242852017-06-23 15:06:31 -0700454 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700455}
Colin Crossae5caf52018-05-22 11:11:52 -0700456
457type appTestProperties struct {
458 Instrumentation_for *string
459}
460
461type AndroidTest struct {
462 AndroidApp
463
464 appTestProperties appTestProperties
465
466 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700467
468 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700469 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700470}
471
472func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800473 // Check if the instrumentation target package is overridden before generating build actions.
474 if a.appTestProperties.Instrumentation_for != nil {
475 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
476 if overridden {
477 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
478 }
479 }
Colin Crossae5caf52018-05-22 11:11:52 -0700480 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700481
yangbill4f41bc22019-02-13 21:45:47 +0800482 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 -0800483 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700484}
485
486func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -0700487 a.AndroidApp.DepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -0700488 if a.appTestProperties.Instrumentation_for != nil {
489 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
490 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
491 // use instrumentationForTag instead of libTag.
492 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
493 }
Colin Crossae5caf52018-05-22 11:11:52 -0700494}
495
Colin Cross1b16b0e2019-02-12 14:41:32 -0800496// android_test compiles test sources and Android resources into an Android application package `.apk` file and
497// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -0700498func AndroidTestFactory() android.Module {
499 module := &AndroidTest{}
500
501 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
Colin Cross5067db92018-09-17 16:46:35 -0700502
503 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700504 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800505 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross43f08db2018-11-12 10:13:39 -0800506 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -0700507
508 module.AddProperties(
509 &module.Module.properties,
510 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800511 &module.Module.dexpreoptProperties,
Colin Crossae5caf52018-05-22 11:11:52 -0700512 &module.Module.protoProperties,
513 &module.aaptProperties,
514 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700515 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800516 &module.overridableAppProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700517 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700518
Colin Crossa4f08812018-10-02 22:03:40 -0700519 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
520 android.InitDefaultableModule(module)
Colin Crossae5caf52018-05-22 11:11:52 -0700521 return module
522}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700523
Colin Cross252fc6f2018-10-04 15:22:03 -0700524type appTestHelperAppProperties struct {
525 // list of compatibility suites (for example "cts", "vts") that the module should be
526 // installed into.
527 Test_suites []string `android:"arch_variant"`
528}
529
530type AndroidTestHelperApp struct {
531 AndroidApp
532
533 appTestHelperAppProperties appTestHelperAppProperties
534}
535
Colin Cross1b16b0e2019-02-12 14:41:32 -0800536// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
537// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
538// test.
Colin Cross252fc6f2018-10-04 15:22:03 -0700539func AndroidTestHelperAppFactory() android.Module {
540 module := &AndroidTestHelperApp{}
541
542 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
543
544 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800545 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross43f08db2018-11-12 10:13:39 -0800546 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700547
548 module.AddProperties(
549 &module.Module.properties,
550 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800551 &module.Module.dexpreoptProperties,
Colin Cross252fc6f2018-10-04 15:22:03 -0700552 &module.Module.protoProperties,
553 &module.aaptProperties,
554 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800555 &module.appTestHelperAppProperties,
556 &module.overridableAppProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -0700557
558 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
559 android.InitDefaultableModule(module)
560 return module
561}
562
Colin Crossbd01e2a2018-10-04 15:21:03 -0700563type AndroidAppCertificate struct {
564 android.ModuleBase
565 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900566 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -0700567}
568
569type AndroidAppCertificateProperties struct {
570 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
571 Certificate *string
572}
573
Colin Cross1b16b0e2019-02-12 14:41:32 -0800574// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
575// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -0700576func AndroidAppCertificateFactory() android.Module {
577 module := &AndroidAppCertificate{}
578 module.AddProperties(&module.properties)
579 android.InitAndroidModule(module)
580 return module
581}
582
Colin Crossbd01e2a2018-10-04 15:21:03 -0700583func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
584 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900585 c.Certificate = Certificate{
Colin Crossbd01e2a2018-10-04 15:21:03 -0700586 android.PathForModuleSrc(ctx, cert+".x509.pem"),
587 android.PathForModuleSrc(ctx, cert+".pk8"),
588 }
589}
Jaewoong Jung525443a2019-02-28 15:35:54 -0800590
591type OverrideAndroidApp struct {
592 android.ModuleBase
593 android.OverrideModuleBase
594}
595
596func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
597 // All the overrides happen in the base module.
598 // TODO(jungjw): Check the base module type.
599}
600
601// override_android_app is used to create an android_app module based on another android_app by overriding
602// some of its properties.
603func OverrideAndroidAppModuleFactory() android.Module {
604 m := &OverrideAndroidApp{}
605 m.AddProperties(&overridableAppProperties{})
606
607 android.InitAndroidModule(m)
608 android.InitOverrideModule(m)
609 return m
610}