blob: c08aefd1f914b8b1ca1b05aba020c990626974e0 [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)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080036}
37
Colin Cross30e076a2015-04-13 13:58:27 -070038// AndroidManifest.xml merging
39// package splits
40
Colin Crossfabb6082018-02-20 17:22:23 -080041type appProperties struct {
Colin Crossbd01e2a2018-10-04 15:21:03 -070042 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
43 // or an android_app_certificate module name in the form ":module".
Nan Zhangea568a42017-11-08 21:20:04 -080044 Certificate *string
Colin Cross7d5136f2015-05-11 13:39:40 -070045
Colin Crossbd01e2a2018-10-04 15:21:03 -070046 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
Colin Cross7d5136f2015-05-11 13:39:40 -070047 Additional_certificates []string
48
49 // If set, create package-export.apk, which other packages can
50 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
Nan Zhangea568a42017-11-08 21:20:04 -080051 Export_package_resources *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070052
Colin Cross16056062017-12-13 22:46:28 -080053 // Specifies that this app should be installed to the priv-app directory,
54 // where the system will grant it additional privileges not available to
55 // normal apps.
56 Privileged *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070057
58 // list of resource labels to generate individual resource packages
59 Package_splits []string
Jason Monkd4122be2018-08-10 09:33:36 -040060
61 // Names of modules to be overridden. Listed modules can only be other binaries
62 // (in Make or Soong).
63 // This does not completely prevent installation of the overridden binaries, but if both
64 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
65 // from PRODUCT_PACKAGES.
66 Overrides []string
Colin Crossa4f08812018-10-02 22:03:40 -070067
68 // list of native libraries that will be provided in or alongside the resulting jar
69 Jni_libs []string `android:"arch_variant"`
70
Colin Crosse4246ab2019-02-05 21:55:21 -080071 // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest
72 // flag so that they are used from inside the APK at runtime. Defaults to true for android_test modules unless
73 // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to false for other
74 // module types where the native libraries are generally preinstalled outside the APK.
75 Use_embedded_native_libs *bool
Colin Cross46abdad2019-02-07 13:07:08 -080076
77 // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that
78 // they are used from inside the APK at runtime.
79 Use_embedded_dex *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070080}
81
Colin Cross30e076a2015-04-13 13:58:27 -070082type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -070083 Library
84 aapt
85
Jiyong Parkc00cbd92018-10-30 21:20:05 +090086 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -070087
Colin Crossfabb6082018-02-20 17:22:23 -080088 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -070089
Colin Crossa4f08812018-10-02 22:03:40 -070090 installJniLibs []jniLib
Colin Crossf6237212018-10-29 23:14:58 -070091
92 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -080093
94 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
95 installApkName string
Colin Crosse1731a52017-12-14 11:22:55 -080096}
97
Colin Cross89c31582018-04-30 15:55:11 -070098func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
99 return nil
100}
101
Colin Cross66f78822018-05-02 12:58:28 -0700102func (a *AndroidApp) ExportedStaticPackages() android.Paths {
103 return nil
104}
105
Colin Crossa97c5d32018-03-28 14:58:31 -0700106var _ AndroidLibraryDependency = (*AndroidApp)(nil)
107
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900108type Certificate struct {
109 Pem, Key android.Path
Colin Cross30e076a2015-04-13 13:58:27 -0700110}
111
Colin Cross46c9b8b2017-06-22 16:51:17 -0700112func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
113 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700114
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800115 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700116 a.aapt.deps(ctx, sdkContext(a))
Colin Cross30e076a2015-04-13 13:58:27 -0700117 }
Colin Crossa4f08812018-10-02 22:03:40 -0700118
119 for _, jniTarget := range ctx.MultiTargets() {
120 variation := []blueprint.Variation{
121 {Mutator: "arch", Variation: jniTarget.String()},
122 {Mutator: "link", Variation: "shared"},
123 }
124 tag := &jniDependencyTag{
125 target: jniTarget,
126 }
127 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
128 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700129
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800130 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700131 if cert != "" {
132 ctx.AddDependency(ctx.Module(), certificateTag, cert)
133 }
134
135 for _, cert := range a.appProperties.Additional_certificates {
136 cert = android.SrcIsModule(cert)
137 if cert != "" {
138 ctx.AddDependency(ctx.Module(), certificateTag, cert)
139 } else {
140 ctx.PropertyErrorf("additional_certificates",
141 `must be names of android_app_certificate modules in the form ":module"`)
142 }
143 }
Colin Cross30e076a2015-04-13 13:58:27 -0700144}
145
Colin Cross46c9b8b2017-06-22 16:51:17 -0700146func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800147 a.aapt.uncompressedJNI = a.shouldUncompressJNI(ctx)
Colin Cross46abdad2019-02-07 13:07:08 -0800148 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
Colin Crossae5caf52018-05-22 11:11:52 -0700149 a.generateAndroidBuildActions(ctx)
150}
151
Colin Crosse4246ab2019-02-05 21:55:21 -0800152// shouldUncompressJNI returns true if the native libraries should be stored in the APK uncompressed and the
153// extractNativeLibs application flag should be set to false in the manifest.
154func (a *AndroidApp) shouldUncompressJNI(ctx android.ModuleContext) bool {
155 minSdkVersion, err := sdkVersionToNumber(ctx, a.minSdkVersion())
156 if err != nil {
157 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
158 }
159
160 return minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)
161}
162
Colin Cross43f08db2018-11-12 10:13:39 -0800163// Returns whether this module should have the dex file stored uncompressed in the APK.
164func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800165 if Bool(a.appProperties.Use_embedded_dex) {
166 return true
167 }
168
Colin Cross5a0dcd52018-10-05 14:20:06 -0700169 if ctx.Config().UnbundledBuild() {
Colin Cross43f08db2018-11-12 10:13:39 -0800170 return false
Colin Cross5a0dcd52018-10-05 14:20:06 -0700171 }
172
Colin Cross5a0dcd52018-10-05 14:20:06 -0700173 // Uncompress dex in APKs of privileged apps, and modules used by privileged apps.
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000174 if ctx.Config().UncompressPrivAppDex() &&
Colin Cross5a0dcd52018-10-05 14:20:06 -0700175 (Bool(a.appProperties.Privileged) ||
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000176 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules())) {
177 return true
178 }
179
180 // Uncompress if the dex files is preopted on /system.
181 if !a.dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, a.dexpreopter.installPath)) {
182 return true
183 }
184
185 return false
Colin Cross5a0dcd52018-10-05 14:20:06 -0700186}
187
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800188func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
David Brazdild25060a2019-02-18 18:24:16 +0000189 a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis)
190
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800191 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800192
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800193 // 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 -0700194 hasProduct := false
195 for _, f := range a.aaptProperties.Aaptflags {
196 if strings.HasPrefix(f, "--product") {
197 hasProduct = true
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800198 break
Colin Crosse78dcd32018-04-19 15:25:19 -0700199 }
200 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700201 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800202 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700203 }
204
Dan Willemsen72be5902018-10-24 20:24:57 -0700205 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
206 // Product AAPT config
207 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800208 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700209 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700210
Dan Willemsen72be5902018-10-24 20:24:57 -0700211 // Product AAPT preferred config
212 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800213 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700214 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700215 }
216
Colin Crossa97c5d32018-03-28 14:58:31 -0700217 // TODO: LOCAL_PACKAGE_OVERRIDES
218 // $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800219
Jiyong Park7f67f482019-01-05 12:57:48 +0900220 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
221 if overridden {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800222 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
Jiyong Park7f67f482019-01-05 12:57:48 +0900223 }
224
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800225 a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700226
Colin Cross46c9b8b2017-06-22 16:51:17 -0700227 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700228 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800229}
Colin Cross30e076a2015-04-13 13:58:27 -0700230
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800231func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700232 var staticLibProguardFlagFiles android.Paths
233 ctx.VisitDirectDeps(func(m android.Module) {
234 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
235 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
236 }
237 })
238
239 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
240
241 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
242 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800243}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800244
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800245func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross43f08db2018-11-12 10:13:39 -0800246
247 var installDir string
248 if ctx.ModuleName() == "framework-res" {
249 // framework-res.apk is installed as system/framework/framework-res.apk
250 installDir = "framework"
251 } else if Bool(a.appProperties.Privileged) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800252 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800253 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800254 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800255 }
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800256 a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000257 a.dexpreopter.isInstallable = Bool(a.properties.Installable)
258 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
259 a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex
Colin Cross5a0dcd52018-10-05 14:20:06 -0700260
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800261 if ctx.ModuleName() != "framework-res" {
262 a.Module.compile(ctx, a.aaptSrcJar)
263 }
Colin Cross30e076a2015-04-13 13:58:27 -0700264
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800265 return a.maybeStrippedDexJarFile
266}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800267
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800268func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700269 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700270 if len(jniLibs) > 0 {
Colin Crosse4246ab2019-02-05 21:55:21 -0800271 embedJni := ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs)
Colin Crossa4f08812018-10-02 22:03:40 -0700272 if embedJni {
273 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Colin Crosse4246ab2019-02-05 21:55:21 -0800274 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.shouldUncompressJNI(ctx))
Colin Crossa4f08812018-10-02 22:03:40 -0700275 } else {
276 a.installJniLibs = jniLibs
277 }
278 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800279 return jniJarFile
280}
Colin Crossa4f08812018-10-02 22:03:40 -0700281
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800282func (a *AndroidApp) certificateBuildActions(certificateDeps []Certificate, ctx android.ModuleContext) []Certificate {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800283 cert := a.getCertString(ctx)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700284 certModule := android.SrcIsModule(cert)
285 if certModule != "" {
286 a.certificate = certificateDeps[0]
287 certificateDeps = certificateDeps[1:]
288 } else if cert != "" {
289 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900290 a.certificate = Certificate{
Colin Crossbd01e2a2018-10-04 15:21:03 -0700291 defaultDir.Join(ctx, cert+".x509.pem"),
292 defaultDir.Join(ctx, cert+".pk8"),
293 }
294 } else {
295 pem, key := ctx.Config().DefaultAppCertificate(ctx)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900296 a.certificate = Certificate{pem, key}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700297 }
298
Jeongik Chac9464142019-01-07 12:07:27 +0900299 if !a.Module.Platform() {
300 certPath := a.certificate.Pem.String()
301 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
302 if strings.HasPrefix(certPath, systemCertPath) {
303 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
304 whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
305
306 if enforceSystemCert && !inList(a.Module.Name(), whitelist) {
307 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
308 }
309 }
310 }
311
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800312 return append([]Certificate{a.certificate}, certificateDeps...)
313}
314
315func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800316 // Check if the install APK name needs to be overridden.
317 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(ctx.ModuleName())
318
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800319 // Process all building blocks, from AAPT to certificates.
320 a.aaptBuildActions(ctx)
321
322 a.proguardBuildActions(ctx)
323
324 dexJarFile := a.dexBuildActions(ctx)
325
326 jniLibs, certificateDeps := a.collectAppDeps(ctx)
327 jniJarFile := a.jniBuildActions(jniLibs, ctx)
328
329 if ctx.Failed() {
330 return
331 }
332
333 certificates := a.certificateBuildActions(certificateDeps, ctx)
334
335 // Build a final signed app package.
336 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".apk")
337 CreateAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800338 a.outputFile = packageFile
339
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800340 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700341 bundleFile := android.PathForModuleOut(ctx, "base.zip")
342 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
343 a.bundleFile = bundleFile
344
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800345 // Install the app package.
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800346 if ctx.ModuleName() == "framework-res" {
347 // framework-res.apk is installed as system/framework/framework-res.apk
348 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".apk", a.outputFile)
Colin Cross16056062017-12-13 22:46:28 -0800349 } else if Bool(a.appProperties.Privileged) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800350 ctx.InstallFile(android.PathForModuleInstall(ctx, "priv-app", a.installApkName), a.installApkName+".apk", a.outputFile)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800351 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800352 ctx.InstallFile(android.PathForModuleInstall(ctx, "app", a.installApkName), a.installApkName+".apk", a.outputFile)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800353 }
Colin Cross30e076a2015-04-13 13:58:27 -0700354}
355
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900356func (a *AndroidApp) collectAppDeps(ctx android.ModuleContext) ([]jniLib, []Certificate) {
Colin Crossa4f08812018-10-02 22:03:40 -0700357 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900358 var certificates []Certificate
Colin Crossa4f08812018-10-02 22:03:40 -0700359
360 ctx.VisitDirectDeps(func(module android.Module) {
361 otherName := ctx.OtherModuleName(module)
362 tag := ctx.OtherModuleDependencyTag(module)
363
364 if jniTag, ok := tag.(*jniDependencyTag); ok {
365 if dep, ok := module.(*cc.Module); ok {
366 lib := dep.OutputFile()
367 if lib.Valid() {
368 jniLibs = append(jniLibs, jniLib{
369 name: ctx.OtherModuleName(module),
370 path: lib.Path(),
371 target: jniTag.target,
372 })
373 } else {
374 ctx.ModuleErrorf("dependency %q missing output file", otherName)
375 }
376 } else {
377 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
378
379 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700380 } else if tag == certificateTag {
381 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900382 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700383 } else {
384 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
385 }
Colin Crossa4f08812018-10-02 22:03:40 -0700386 }
387 })
388
Colin Crossbd01e2a2018-10-04 15:21:03 -0700389 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700390}
391
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800392func (a *AndroidApp) getCertString(ctx android.BaseContext) string {
393 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
394 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000395 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800396 }
397 return String(a.appProperties.Certificate)
398}
399
Colin Cross1b16b0e2019-02-12 14:41:32 -0800400// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700401func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700402 module := &AndroidApp{}
403
Colin Cross66dbc0b2017-12-28 12:23:20 -0800404 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
405 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
406
Colin Crossae5caf52018-05-22 11:11:52 -0700407 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700408 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700409
Colin Cross36242852017-06-23 15:06:31 -0700410 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700411 &module.Module.properties,
412 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800413 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700414 &module.Module.protoProperties,
415 &module.aaptProperties,
Colin Cross540eff82017-06-22 17:01:52 -0700416 &module.appProperties)
Colin Cross36242852017-06-23 15:06:31 -0700417
Colin Crossa9d8bee2018-10-02 13:59:46 -0700418 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
419 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
420 })
421
Colin Crossa4f08812018-10-02 22:03:40 -0700422 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
423 android.InitDefaultableModule(module)
424
Colin Cross36242852017-06-23 15:06:31 -0700425 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700426}
Colin Crossae5caf52018-05-22 11:11:52 -0700427
428type appTestProperties struct {
429 Instrumentation_for *string
430}
431
432type AndroidTest struct {
433 AndroidApp
434
435 appTestProperties appTestProperties
436
437 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700438
439 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700440 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700441}
442
443func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossae5caf52018-05-22 11:11:52 -0700444 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700445
yangbill4f41bc22019-02-13 21:45:47 +0800446 a.testConfig = tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config, a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites)
Colin Crossd96ca352018-08-10 16:06:24 -0700447 a.data = ctx.ExpandSources(a.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -0700448}
449
450func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
451 android.ExtractSourceDeps(ctx, a.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -0700452 android.ExtractSourceDeps(ctx, a.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -0700453 android.ExtractSourcesDeps(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700454 a.AndroidApp.DepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -0700455 if a.appTestProperties.Instrumentation_for != nil {
456 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
457 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
458 // use instrumentationForTag instead of libTag.
459 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
460 }
Colin Crossae5caf52018-05-22 11:11:52 -0700461}
462
Colin Cross1b16b0e2019-02-12 14:41:32 -0800463// android_test compiles test sources and Android resources into an Android application package `.apk` file and
464// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -0700465func AndroidTestFactory() android.Module {
466 module := &AndroidTest{}
467
468 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
Colin Cross5067db92018-09-17 16:46:35 -0700469
470 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700471 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800472 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross43f08db2018-11-12 10:13:39 -0800473 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -0700474
475 module.AddProperties(
476 &module.Module.properties,
477 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800478 &module.Module.dexpreoptProperties,
Colin Crossae5caf52018-05-22 11:11:52 -0700479 &module.Module.protoProperties,
480 &module.aaptProperties,
481 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700482 &module.appTestProperties,
483 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700484
Colin Crossa4f08812018-10-02 22:03:40 -0700485 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
486 android.InitDefaultableModule(module)
Colin Crossae5caf52018-05-22 11:11:52 -0700487 return module
488}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700489
Colin Cross252fc6f2018-10-04 15:22:03 -0700490type appTestHelperAppProperties struct {
491 // list of compatibility suites (for example "cts", "vts") that the module should be
492 // installed into.
493 Test_suites []string `android:"arch_variant"`
494}
495
496type AndroidTestHelperApp struct {
497 AndroidApp
498
499 appTestHelperAppProperties appTestHelperAppProperties
500}
501
Colin Cross1b16b0e2019-02-12 14:41:32 -0800502// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
503// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
504// test.
Colin Cross252fc6f2018-10-04 15:22:03 -0700505func AndroidTestHelperAppFactory() android.Module {
506 module := &AndroidTestHelperApp{}
507
508 module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
509
510 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800511 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross43f08db2018-11-12 10:13:39 -0800512 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700513
514 module.AddProperties(
515 &module.Module.properties,
516 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800517 &module.Module.dexpreoptProperties,
Colin Cross252fc6f2018-10-04 15:22:03 -0700518 &module.Module.protoProperties,
519 &module.aaptProperties,
520 &module.appProperties,
521 &module.appTestHelperAppProperties)
522
523 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
524 android.InitDefaultableModule(module)
525 return module
526}
527
Colin Crossbd01e2a2018-10-04 15:21:03 -0700528type AndroidAppCertificate struct {
529 android.ModuleBase
530 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900531 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -0700532}
533
534type AndroidAppCertificateProperties struct {
535 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
536 Certificate *string
537}
538
Colin Cross1b16b0e2019-02-12 14:41:32 -0800539// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
540// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -0700541func AndroidAppCertificateFactory() android.Module {
542 module := &AndroidAppCertificate{}
543 module.AddProperties(&module.properties)
544 android.InitAndroidModule(module)
545 return module
546}
547
Colin Crossbd01e2a2018-10-04 15:21:03 -0700548func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
549 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900550 c.Certificate = Certificate{
Colin Crossbd01e2a2018-10-04 15:21:03 -0700551 android.PathForModuleSrc(ctx, cert+".x509.pem"),
552 android.PathForModuleSrc(ctx, cert+".pk8"),
553 }
554}