blob: ec021fc000a8883e3ba330fe9a615de3aaa99e98 [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)
Jaewoong Jungccbb3932019-04-15 09:48:31 -070037 android.RegisterModuleType("android_app_import", AndroidAppImportFactory)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080038}
39
Colin Cross30e076a2015-04-13 13:58:27 -070040// AndroidManifest.xml merging
41// package splits
42
Colin Crossfabb6082018-02-20 17:22:23 -080043type appProperties struct {
Colin Crossbd01e2a2018-10-04 15:21:03 -070044 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
Colin Cross7d5136f2015-05-11 13:39:40 -070045 Additional_certificates []string
46
47 // If set, create package-export.apk, which other packages can
48 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
Nan Zhangea568a42017-11-08 21:20:04 -080049 Export_package_resources *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070050
Colin Cross16056062017-12-13 22:46:28 -080051 // Specifies that this app should be installed to the priv-app directory,
52 // where the system will grant it additional privileges not available to
53 // normal apps.
54 Privileged *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070055
56 // list of resource labels to generate individual resource packages
57 Package_splits []string
Jason Monkd4122be2018-08-10 09:33:36 -040058
59 // Names of modules to be overridden. Listed modules can only be other binaries
60 // (in Make or Soong).
61 // This does not completely prevent installation of the overridden binaries, but if both
62 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
63 // from PRODUCT_PACKAGES.
64 Overrides []string
Colin Crossa4f08812018-10-02 22:03:40 -070065
66 // list of native libraries that will be provided in or alongside the resulting jar
67 Jni_libs []string `android:"arch_variant"`
68
Colin Crosse4246ab2019-02-05 21:55:21 -080069 // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest
70 // flag so that they are used from inside the APK at runtime. Defaults to true for android_test modules unless
71 // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to false for other
72 // module types where the native libraries are generally preinstalled outside the APK.
73 Use_embedded_native_libs *bool
Colin Cross46abdad2019-02-07 13:07:08 -080074
75 // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that
76 // they are used from inside the APK at runtime.
77 Use_embedded_dex *bool
Colin Cross47fa9d32019-03-26 10:51:39 -070078
79 // Forces native libraries to always be packaged into the APK,
80 // Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed.
81 // True for android_test* modules.
82 AlwaysPackageNativeLibs bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -070083}
84
Jaewoong Jung525443a2019-02-28 15:35:54 -080085// android_app properties that can be overridden by override_android_app
86type overridableAppProperties struct {
87 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
88 // or an android_app_certificate module name in the form ":module".
89 Certificate *string
Jaewoong Jung6f373f62019-03-13 10:13:24 -070090
91 // the package name of this app. The package name in the manifest file is used if one was not given.
92 Package_name *string
Jaewoong Jung525443a2019-02-28 15:35:54 -080093}
94
Colin Cross30e076a2015-04-13 13:58:27 -070095type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -070096 Library
97 aapt
Jaewoong Jung525443a2019-02-28 15:35:54 -080098 android.OverridableModuleBase
Colin Crossa97c5d32018-03-28 14:58:31 -070099
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900100 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -0700101
Colin Crossfabb6082018-02-20 17:22:23 -0800102 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -0700103
Jaewoong Jung525443a2019-02-28 15:35:54 -0800104 overridableAppProperties overridableAppProperties
105
Colin Crossa4f08812018-10-02 22:03:40 -0700106 installJniLibs []jniLib
Colin Crossf6237212018-10-29 23:14:58 -0700107
108 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800109
110 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
111 installApkName string
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800112
113 additionalAaptFlags []string
Colin Crosse1731a52017-12-14 11:22:55 -0800114}
115
Colin Cross89c31582018-04-30 15:55:11 -0700116func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
117 return nil
118}
119
Colin Cross66f78822018-05-02 12:58:28 -0700120func (a *AndroidApp) ExportedStaticPackages() android.Paths {
121 return nil
122}
123
Colin Crossa97c5d32018-03-28 14:58:31 -0700124var _ AndroidLibraryDependency = (*AndroidApp)(nil)
125
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900126type Certificate struct {
127 Pem, Key android.Path
Colin Cross30e076a2015-04-13 13:58:27 -0700128}
129
Colin Cross46c9b8b2017-06-22 16:51:17 -0700130func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
131 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700132
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800133 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700134 a.aapt.deps(ctx, sdkContext(a))
Colin Cross30e076a2015-04-13 13:58:27 -0700135 }
Colin Crossa4f08812018-10-02 22:03:40 -0700136
137 for _, jniTarget := range ctx.MultiTargets() {
138 variation := []blueprint.Variation{
139 {Mutator: "arch", Variation: jniTarget.String()},
140 {Mutator: "link", Variation: "shared"},
141 }
142 tag := &jniDependencyTag{
143 target: jniTarget,
144 }
145 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
146 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700147
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800148 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700149 if cert != "" {
150 ctx.AddDependency(ctx.Module(), certificateTag, cert)
151 }
152
153 for _, cert := range a.appProperties.Additional_certificates {
154 cert = android.SrcIsModule(cert)
155 if cert != "" {
156 ctx.AddDependency(ctx.Module(), certificateTag, cert)
157 } else {
158 ctx.PropertyErrorf("additional_certificates",
159 `must be names of android_app_certificate modules in the form ":module"`)
160 }
161 }
Colin Cross30e076a2015-04-13 13:58:27 -0700162}
163
Colin Cross46c9b8b2017-06-22 16:51:17 -0700164func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Sasha Smundak6ad77252019-05-01 13:16:22 -0700165 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
Colin Cross46abdad2019-02-07 13:07:08 -0800166 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
Colin Crossae5caf52018-05-22 11:11:52 -0700167 a.generateAndroidBuildActions(ctx)
168}
169
Sasha Smundak6ad77252019-05-01 13:16:22 -0700170// Returns true if the native libraries should be stored in the APK uncompressed and the
Colin Crosse4246ab2019-02-05 21:55:21 -0800171// extractNativeLibs application flag should be set to false in the manifest.
Sasha Smundak6ad77252019-05-01 13:16:22 -0700172func (a *AndroidApp) useEmbeddedNativeLibs(ctx android.ModuleContext) bool {
Colin Crosse4246ab2019-02-05 21:55:21 -0800173 minSdkVersion, err := sdkVersionToNumber(ctx, a.minSdkVersion())
174 if err != nil {
175 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
176 }
177
178 return minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)
179}
180
Colin Cross43f08db2018-11-12 10:13:39 -0800181// Returns whether this module should have the dex file stored uncompressed in the APK.
182func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800183 if Bool(a.appProperties.Use_embedded_dex) {
184 return true
185 }
186
Colin Cross5a0dcd52018-10-05 14:20:06 -0700187 if ctx.Config().UnbundledBuild() {
Colin Cross43f08db2018-11-12 10:13:39 -0800188 return false
Colin Cross5a0dcd52018-10-05 14:20:06 -0700189 }
190
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700191 // Uncompress dex in APKs of privileged apps
192 if ctx.Config().UncompressPrivAppDex() && Bool(a.appProperties.Privileged) {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000193 return true
194 }
195
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700196 return shouldUncompressDex(ctx, &a.dexpreopter)
Colin Cross5a0dcd52018-10-05 14:20:06 -0700197}
198
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800199func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
David Brazdild25060a2019-02-18 18:24:16 +0000200 a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis)
201
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800202 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800203
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800204 // 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 -0700205 hasProduct := false
206 for _, f := range a.aaptProperties.Aaptflags {
207 if strings.HasPrefix(f, "--product") {
208 hasProduct = true
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800209 break
Colin Crosse78dcd32018-04-19 15:25:19 -0700210 }
211 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700212 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800213 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700214 }
215
Dan Willemsen72be5902018-10-24 20:24:57 -0700216 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
217 // Product AAPT config
218 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800219 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700220 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700221
Dan Willemsen72be5902018-10-24 20:24:57 -0700222 // Product AAPT preferred config
223 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800224 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700225 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700226 }
227
Jiyong Park7f67f482019-01-05 12:57:48 +0900228 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700229 if overridden || a.overridableAppProperties.Package_name != nil {
230 // The product override variable has a priority over the package_name property.
231 if !overridden {
232 manifestPackageName = *a.overridableAppProperties.Package_name
233 }
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800234 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
Jiyong Park7f67f482019-01-05 12:57:48 +0900235 }
236
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800237 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
238
Colin Crosse560c4a2019-03-19 16:03:11 -0700239 a.aapt.splitNames = a.appProperties.Package_splits
240
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800241 a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700242
Colin Cross46c9b8b2017-06-22 16:51:17 -0700243 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700244 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800245}
Colin Cross30e076a2015-04-13 13:58:27 -0700246
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800247func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700248 var staticLibProguardFlagFiles android.Paths
249 ctx.VisitDirectDeps(func(m android.Module) {
250 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
251 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
252 }
253 })
254
255 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
256
257 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
258 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800259}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800260
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800261func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross43f08db2018-11-12 10:13:39 -0800262
263 var installDir string
264 if ctx.ModuleName() == "framework-res" {
265 // framework-res.apk is installed as system/framework/framework-res.apk
266 installDir = "framework"
267 } else if Bool(a.appProperties.Privileged) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800268 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800269 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800270 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800271 }
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800272 a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000273 a.dexpreopter.isInstallable = Bool(a.properties.Installable)
274 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
275 a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex
Colin Cross5a0dcd52018-10-05 14:20:06 -0700276
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800277 if ctx.ModuleName() != "framework-res" {
278 a.Module.compile(ctx, a.aaptSrcJar)
279 }
Colin Cross30e076a2015-04-13 13:58:27 -0700280
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800281 return a.maybeStrippedDexJarFile
282}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800283
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800284func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700285 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700286 if len(jniLibs) > 0 {
Colin Cross47fa9d32019-03-26 10:51:39 -0700287 embedJni := ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) ||
288 a.appProperties.AlwaysPackageNativeLibs
Colin Crossa4f08812018-10-02 22:03:40 -0700289 if embedJni {
290 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Sasha Smundak6ad77252019-05-01 13:16:22 -0700291 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.useEmbeddedNativeLibs(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 Jungccbb3932019-04-15 09:48:31 -0700299// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
300// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
301func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate {
302 if android.SrcIsModule(certPropValue) == "" {
303 var mainCert Certificate
304 if certPropValue != "" {
305 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
306 mainCert = Certificate{
307 defaultDir.Join(ctx, certPropValue+".x509.pem"),
308 defaultDir.Join(ctx, certPropValue+".pk8"),
309 }
310 } else {
311 pem, key := ctx.Config().DefaultAppCertificate(ctx)
312 mainCert = Certificate{pem, key}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700313 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700314 certificates = append([]Certificate{mainCert}, certificates...)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700315 }
316
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700317 if !m.Platform() {
318 certPath := certificates[0].Pem.String()
Jeongik Chac9464142019-01-07 12:07:27 +0900319 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
320 if strings.HasPrefix(certPath, systemCertPath) {
321 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
322 whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
323
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700324 if enforceSystemCert && !inList(m.Name(), whitelist) {
Jeongik Chac9464142019-01-07 12:07:27 +0900325 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
326 }
327 }
328 }
329
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700330 return certificates
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800331}
332
333func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800334 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800335 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800336
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800337 // Process all building blocks, from AAPT to certificates.
338 a.aaptBuildActions(ctx)
339
340 a.proguardBuildActions(ctx)
341
342 dexJarFile := a.dexBuildActions(ctx)
343
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700344 jniLibs, certificateDeps := collectAppDeps(ctx)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800345 jniJarFile := a.jniBuildActions(jniLibs, ctx)
346
347 if ctx.Failed() {
348 return
349 }
350
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700351 certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx)
352 a.certificate = certificates[0]
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800353
354 // Build a final signed app package.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800355 // TODO(jungjw): Consider changing this to installApkName.
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800356 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700357 CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800358 a.outputFile = packageFile
359
Colin Crosse560c4a2019-03-19 16:03:11 -0700360 for _, split := range a.aapt.splits {
361 // Sign the split APKs
362 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"_"+split.suffix+".apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700363 CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates)
Colin Crosse560c4a2019-03-19 16:03:11 -0700364 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
365 }
366
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800367 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700368 bundleFile := android.PathForModuleOut(ctx, "base.zip")
369 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
370 a.bundleFile = bundleFile
371
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800372 // Install the app package.
Colin Crosse560c4a2019-03-19 16:03:11 -0700373 var installDir android.OutputPath
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800374 if ctx.ModuleName() == "framework-res" {
375 // framework-res.apk is installed as system/framework/framework-res.apk
Colin Crosse560c4a2019-03-19 16:03:11 -0700376 installDir = android.PathForModuleInstall(ctx, "framework")
Colin Cross16056062017-12-13 22:46:28 -0800377 } else if Bool(a.appProperties.Privileged) {
Colin Crosse560c4a2019-03-19 16:03:11 -0700378 installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800379 } else {
Colin Crosse560c4a2019-03-19 16:03:11 -0700380 installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
381 }
382
383 ctx.InstallFile(installDir, a.installApkName+".apk", a.outputFile)
384 for _, split := range a.aapt.splits {
385 ctx.InstallFile(installDir, a.installApkName+"_"+split.suffix+".apk", split.path)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800386 }
Colin Cross30e076a2015-04-13 13:58:27 -0700387}
388
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700389func collectAppDeps(ctx android.ModuleContext) ([]jniLib, []Certificate) {
Colin Crossa4f08812018-10-02 22:03:40 -0700390 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900391 var certificates []Certificate
Colin Crossa4f08812018-10-02 22:03:40 -0700392
393 ctx.VisitDirectDeps(func(module android.Module) {
394 otherName := ctx.OtherModuleName(module)
395 tag := ctx.OtherModuleDependencyTag(module)
396
397 if jniTag, ok := tag.(*jniDependencyTag); ok {
398 if dep, ok := module.(*cc.Module); ok {
399 lib := dep.OutputFile()
400 if lib.Valid() {
401 jniLibs = append(jniLibs, jniLib{
402 name: ctx.OtherModuleName(module),
403 path: lib.Path(),
404 target: jniTag.target,
405 })
406 } else {
407 ctx.ModuleErrorf("dependency %q missing output file", otherName)
408 }
409 } else {
410 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
Colin Crossa4f08812018-10-02 22:03:40 -0700411 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700412 } else if tag == certificateTag {
413 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900414 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700415 } else {
416 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
417 }
Colin Crossa4f08812018-10-02 22:03:40 -0700418 }
419 })
420
Colin Crossbd01e2a2018-10-04 15:21:03 -0700421 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700422}
423
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800424func (a *AndroidApp) getCertString(ctx android.BaseContext) string {
425 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
426 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000427 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800428 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800429 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800430}
431
Colin Cross1b16b0e2019-02-12 14:41:32 -0800432// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700433func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700434 module := &AndroidApp{}
435
Sasha Smundak2057f822019-04-16 17:16:58 -0700436 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross66dbc0b2017-12-28 12:23:20 -0800437 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
438
Colin Crossae5caf52018-05-22 11:11:52 -0700439 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700440 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700441
Colin Cross36242852017-06-23 15:06:31 -0700442 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700443 &module.Module.properties,
444 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800445 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700446 &module.Module.protoProperties,
447 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800448 &module.appProperties,
449 &module.overridableAppProperties)
Colin Cross36242852017-06-23 15:06:31 -0700450
Colin Crossa9d8bee2018-10-02 13:59:46 -0700451 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
452 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
453 })
454
Colin Crossa4f08812018-10-02 22:03:40 -0700455 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
456 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800457 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossa4f08812018-10-02 22:03:40 -0700458
Colin Cross36242852017-06-23 15:06:31 -0700459 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700460}
Colin Crossae5caf52018-05-22 11:11:52 -0700461
462type appTestProperties struct {
463 Instrumentation_for *string
464}
465
466type AndroidTest struct {
467 AndroidApp
468
469 appTestProperties appTestProperties
470
471 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700472
473 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700474 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700475}
476
477func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800478 // Check if the instrumentation target package is overridden before generating build actions.
479 if a.appTestProperties.Instrumentation_for != nil {
480 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
481 if overridden {
482 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
483 }
484 }
Sasha Smundak6ad77252019-05-01 13:16:22 -0700485 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
486 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
Colin Crossae5caf52018-05-22 11:11:52 -0700487 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700488
yangbill4f41bc22019-02-13 21:45:47 +0800489 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 -0800490 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700491}
492
493func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -0700494 a.AndroidApp.DepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -0700495 if a.appTestProperties.Instrumentation_for != nil {
496 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
497 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
498 // use instrumentationForTag instead of libTag.
499 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
500 }
Colin Crossae5caf52018-05-22 11:11:52 -0700501}
502
Colin Cross1b16b0e2019-02-12 14:41:32 -0800503// android_test compiles test sources and Android resources into an Android application package `.apk` file and
504// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -0700505func AndroidTestFactory() android.Module {
506 module := &AndroidTest{}
507
Sasha Smundak2057f822019-04-16 17:16:58 -0700508 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross5067db92018-09-17 16:46:35 -0700509
510 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700511 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800512 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700513 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800514 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -0700515
516 module.AddProperties(
517 &module.Module.properties,
518 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800519 &module.Module.dexpreoptProperties,
Colin Crossae5caf52018-05-22 11:11:52 -0700520 &module.Module.protoProperties,
521 &module.aaptProperties,
522 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700523 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800524 &module.overridableAppProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700525 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700526
Colin Crossa4f08812018-10-02 22:03:40 -0700527 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
528 android.InitDefaultableModule(module)
Colin Crossae5caf52018-05-22 11:11:52 -0700529 return module
530}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700531
Colin Cross252fc6f2018-10-04 15:22:03 -0700532type appTestHelperAppProperties struct {
533 // list of compatibility suites (for example "cts", "vts") that the module should be
534 // installed into.
535 Test_suites []string `android:"arch_variant"`
536}
537
538type AndroidTestHelperApp struct {
539 AndroidApp
540
541 appTestHelperAppProperties appTestHelperAppProperties
542}
543
Colin Cross1b16b0e2019-02-12 14:41:32 -0800544// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
545// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
546// test.
Colin Cross252fc6f2018-10-04 15:22:03 -0700547func AndroidTestHelperAppFactory() android.Module {
548 module := &AndroidTestHelperApp{}
549
Sasha Smundak2057f822019-04-16 17:16:58 -0700550 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700551
552 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800553 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700554 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800555 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700556
557 module.AddProperties(
558 &module.Module.properties,
559 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800560 &module.Module.dexpreoptProperties,
Colin Cross252fc6f2018-10-04 15:22:03 -0700561 &module.Module.protoProperties,
562 &module.aaptProperties,
563 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800564 &module.appTestHelperAppProperties,
565 &module.overridableAppProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -0700566
567 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
568 android.InitDefaultableModule(module)
569 return module
570}
571
Colin Crossbd01e2a2018-10-04 15:21:03 -0700572type AndroidAppCertificate struct {
573 android.ModuleBase
574 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900575 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -0700576}
577
578type AndroidAppCertificateProperties struct {
579 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
580 Certificate *string
581}
582
Colin Cross1b16b0e2019-02-12 14:41:32 -0800583// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
584// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -0700585func AndroidAppCertificateFactory() android.Module {
586 module := &AndroidAppCertificate{}
587 module.AddProperties(&module.properties)
588 android.InitAndroidModule(module)
589 return module
590}
591
Colin Crossbd01e2a2018-10-04 15:21:03 -0700592func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
593 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900594 c.Certificate = Certificate{
Colin Crossbd01e2a2018-10-04 15:21:03 -0700595 android.PathForModuleSrc(ctx, cert+".x509.pem"),
596 android.PathForModuleSrc(ctx, cert+".pk8"),
597 }
598}
Jaewoong Jung525443a2019-02-28 15:35:54 -0800599
600type OverrideAndroidApp struct {
601 android.ModuleBase
602 android.OverrideModuleBase
603}
604
605func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
606 // All the overrides happen in the base module.
607 // TODO(jungjw): Check the base module type.
608}
609
610// override_android_app is used to create an android_app module based on another android_app by overriding
611// some of its properties.
612func OverrideAndroidAppModuleFactory() android.Module {
613 m := &OverrideAndroidApp{}
614 m.AddProperties(&overridableAppProperties{})
615
616 android.InitAndroidModule(m)
617 android.InitOverrideModule(m)
618 return m
619}
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700620
621type AndroidAppImport struct {
622 android.ModuleBase
623 android.DefaultableModuleBase
624 prebuilt android.Prebuilt
625
626 properties AndroidAppImportProperties
627
628 outputFile android.Path
629 certificate *Certificate
630
631 dexpreopter
632}
633
634type AndroidAppImportProperties struct {
635 // A prebuilt apk to import
636 Apk string
637
638 // The name of a certificate in the default certificate directory, blank to use the default
639 // product certificate, or an android_app_certificate module name in the form ":module".
640 Certificate *string
641
642 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
643 // be set for presigned modules.
644 Presigned *bool
645
646 // Specifies that this app should be installed to the priv-app directory,
647 // where the system will grant it additional privileges not available to
648 // normal apps.
649 Privileged *bool
650
651 // Names of modules to be overridden. Listed modules can only be other binaries
652 // (in Make or Soong).
653 // This does not completely prevent installation of the overridden binaries, but if both
654 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
655 // from PRODUCT_PACKAGES.
656 Overrides []string
657}
658
659func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
660 cert := android.SrcIsModule(String(a.properties.Certificate))
661 if cert != "" {
662 ctx.AddDependency(ctx.Module(), certificateTag, cert)
663 }
664}
665
666func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
667 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
668 rule := android.NewRuleBuilder()
669 rule.Command().
670 Textf(`if (zipinfo %s 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
671 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
672 FlagWithInput("-i ", inputPath).
673 FlagWithOutput("-o ", outputPath).
674 FlagWithArg("-0 ", "'lib/**/*.so'").
675 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
676 rule.Build(pctx, ctx, "uncompress-embedded-jni-libs", "Uncompress embedded JIN libs")
677}
678
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700679// Returns whether this module should have the dex file stored uncompressed in the APK.
680func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
681 if ctx.Config().UnbundledBuild() {
682 return false
683 }
684
685 // Uncompress dex in APKs of privileged apps
686 if ctx.Config().UncompressPrivAppDex() && Bool(a.properties.Privileged) {
687 return true
688 }
689
690 return shouldUncompressDex(ctx, &a.dexpreopter)
691}
692
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700693func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
694 if String(a.properties.Certificate) == "" && !Bool(a.properties.Presigned) {
695 ctx.PropertyErrorf("certificate", "No certificate specified for prebuilt")
696 }
697 if String(a.properties.Certificate) != "" && Bool(a.properties.Presigned) {
698 ctx.PropertyErrorf("certificate", "Certificate can't be specified for presigned modules")
699 }
700
701 _, certificates := collectAppDeps(ctx)
702
703 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
704 // TODO: LOCAL_DPI_VARIANTS
705 // TODO: LOCAL_PACKAGE_SPLITS
706
707 srcApk := a.prebuilt.SingleSourcePath(ctx)
708
709 // TODO: Install or embed JNI libraries
710
711 // Uncompress JNI libraries in the apk
712 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
713 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
714
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700715 installDir := android.PathForModuleInstall(ctx, "app", a.BaseModuleName())
716 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
717 a.dexpreopter.isInstallable = true
718 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700719 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700720 dexOutput := a.dexpreopter.dexpreopt(ctx, jnisUncompressed)
721
722 // Sign or align the package
723 // TODO: Handle EXTERNAL
724 if !Bool(a.properties.Presigned) {
725 certificates = processMainCert(a.ModuleBase, *a.properties.Certificate, certificates, ctx)
726 if len(certificates) != 1 {
727 ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates)
728 }
729 a.certificate = &certificates[0]
730 signed := android.PathForModuleOut(ctx, "signed", ctx.ModuleName()+".apk")
731 SignAppPackage(ctx, signed, dexOutput, certificates)
732 a.outputFile = signed
733 } else {
734 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", ctx.ModuleName()+".apk")
735 TransformZipAlign(ctx, alignedApk, dexOutput)
736 a.outputFile = alignedApk
737 }
738
739 // TODO: Optionally compress the output apk.
740
741 ctx.InstallFile(installDir, a.BaseModuleName()+".apk", a.outputFile)
742
743 // TODO: androidmk converter jni libs
744}
745
746func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
747 return &a.prebuilt
748}
749
750func (a *AndroidAppImport) Name() string {
751 return a.prebuilt.Name(a.ModuleBase.Name())
752}
753
754// android_app_import imports a prebuilt apk with additional processing specified in the module.
755func AndroidAppImportFactory() android.Module {
756 module := &AndroidAppImport{}
757 module.AddProperties(&module.properties)
758 module.AddProperties(&module.dexpreoptProperties)
759
760 InitJavaModule(module, android.DeviceSupported)
761 android.InitSingleSourcePrebuiltModule(module, &module.properties.Apk)
762
763 return module
764}