blob: 7e38f8770fb35f717625b0bd11eed6414ee9e626 [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 Crossa4f08812018-10-02 22:03:40 -070020 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070021 "github.com/google/blueprint/proptools"
Jaewoong Jung9c6f3142019-04-26 14:31:50 -070022 "path/filepath"
23 "reflect"
24 "sort"
25 "strings"
Colin Cross30e076a2015-04-13 13:58:27 -070026
Colin Cross635c3b02016-05-18 15:37:25 -070027 "android/soong/android"
Colin Crossa4f08812018-10-02 22:03:40 -070028 "android/soong/cc"
Colin Cross303e21f2018-08-07 16:49:25 -070029 "android/soong/tradefed"
Colin Cross30e076a2015-04-13 13:58:27 -070030)
31
Jaewoong Jung05d5b542019-06-11 12:25:34 -070032var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
Jaewoong Jung9c6f3142019-04-26 14:31:50 -070033
Colin Cross3bc7ffa2017-11-22 16:19:37 -080034func init() {
Colin Cross5ab4e6d2017-11-22 16:20:45 -080035 android.RegisterModuleType("android_app", AndroidAppFactory)
Colin Crossae5caf52018-05-22 11:11:52 -070036 android.RegisterModuleType("android_test", AndroidTestFactory)
Colin Cross252fc6f2018-10-04 15:22:03 -070037 android.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
Colin Crossbd01e2a2018-10-04 15:21:03 -070038 android.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
Jaewoong Jung525443a2019-02-28 15:35:54 -080039 android.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
Jaewoong Jung33695b92019-04-15 09:48:31 -070040 android.RegisterModuleType("android_app_import", AndroidAppImportFactory)
Jaewoong Jung9e0fbd52019-08-13 14:11:33 -070041
42 initAndroidAppImportVariantGroupTypes()
Colin Cross3bc7ffa2017-11-22 16:19:37 -080043}
44
Colin Cross30e076a2015-04-13 13:58:27 -070045// AndroidManifest.xml merging
46// package splits
47
Colin Crossfabb6082018-02-20 17:22:23 -080048type appProperties struct {
Colin Crossbd01e2a2018-10-04 15:21:03 -070049 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
Colin Cross7d5136f2015-05-11 13:39:40 -070050 Additional_certificates []string
51
52 // If set, create package-export.apk, which other packages can
53 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
Nan Zhangea568a42017-11-08 21:20:04 -080054 Export_package_resources *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070055
Colin Cross16056062017-12-13 22:46:28 -080056 // Specifies that this app should be installed to the priv-app directory,
57 // where the system will grant it additional privileges not available to
58 // normal apps.
59 Privileged *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070060
61 // list of resource labels to generate individual resource packages
62 Package_splits []string
Jason Monkd4122be2018-08-10 09:33:36 -040063
64 // Names of modules to be overridden. Listed modules can only be other binaries
65 // (in Make or Soong).
66 // This does not completely prevent installation of the overridden binaries, but if both
67 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
68 // from PRODUCT_PACKAGES.
69 Overrides []string
Colin Crossa4f08812018-10-02 22:03:40 -070070
71 // list of native libraries that will be provided in or alongside the resulting jar
72 Jni_libs []string `android:"arch_variant"`
73
Colin Crosse4246ab2019-02-05 21:55:21 -080074 // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest
75 // flag so that they are used from inside the APK at runtime. Defaults to true for android_test modules unless
76 // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to false for other
77 // module types where the native libraries are generally preinstalled outside the APK.
78 Use_embedded_native_libs *bool
Colin Cross46abdad2019-02-07 13:07:08 -080079
80 // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that
81 // they are used from inside the APK at runtime.
82 Use_embedded_dex *bool
Colin Cross47fa9d32019-03-26 10:51:39 -070083
84 // Forces native libraries to always be packaged into the APK,
85 // Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed.
86 // True for android_test* modules.
87 AlwaysPackageNativeLibs bool `blueprint:"mutated"`
Jaewoong Jung5c6572e2019-06-17 17:40:56 -070088
89 // If set, find and merge all NOTICE files that this module and its dependencies have and store
90 // it in the APK as an asset.
91 Embed_notices *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070092}
93
Jaewoong Jung525443a2019-02-28 15:35:54 -080094// android_app properties that can be overridden by override_android_app
95type overridableAppProperties struct {
96 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
97 // or an android_app_certificate module name in the form ":module".
98 Certificate *string
Jaewoong Jung6f373f62019-03-13 10:13:24 -070099
100 // the package name of this app. The package name in the manifest file is used if one was not given.
101 Package_name *string
Jaewoong Jung525443a2019-02-28 15:35:54 -0800102}
103
Colin Cross30e076a2015-04-13 13:58:27 -0700104type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -0700105 Library
106 aapt
Jaewoong Jung525443a2019-02-28 15:35:54 -0800107 android.OverridableModuleBase
Colin Crossa97c5d32018-03-28 14:58:31 -0700108
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900109 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -0700110
Colin Crossfabb6082018-02-20 17:22:23 -0800111 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -0700112
Jaewoong Jung525443a2019-02-28 15:35:54 -0800113 overridableAppProperties overridableAppProperties
114
Colin Crossa4f08812018-10-02 22:03:40 -0700115 installJniLibs []jniLib
Colin Crossf6237212018-10-29 23:14:58 -0700116
117 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800118
119 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
120 installApkName string
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800121
Jaewoong Jung1ffd7932019-09-11 10:25:18 -0700122 installDir android.OutputPath
123
Jaewoong Jung0b09bdb2019-09-27 17:13:15 -0700124 onDeviceDir string
125
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800126 additionalAaptFlags []string
Colin Crosse1731a52017-12-14 11:22:55 -0800127}
128
Colin Cross89c31582018-04-30 15:55:11 -0700129func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
130 return nil
131}
132
Colin Cross66f78822018-05-02 12:58:28 -0700133func (a *AndroidApp) ExportedStaticPackages() android.Paths {
134 return nil
135}
136
Colin Crossa97c5d32018-03-28 14:58:31 -0700137var _ AndroidLibraryDependency = (*AndroidApp)(nil)
138
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900139type Certificate struct {
140 Pem, Key android.Path
Colin Cross30e076a2015-04-13 13:58:27 -0700141}
142
Colin Cross46c9b8b2017-06-22 16:51:17 -0700143func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
144 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700145
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800146 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700147 a.aapt.deps(ctx, sdkContext(a))
Colin Cross30e076a2015-04-13 13:58:27 -0700148 }
Colin Crossa4f08812018-10-02 22:03:40 -0700149
150 for _, jniTarget := range ctx.MultiTargets() {
151 variation := []blueprint.Variation{
152 {Mutator: "arch", Variation: jniTarget.String()},
153 {Mutator: "link", Variation: "shared"},
154 }
155 tag := &jniDependencyTag{
156 target: jniTarget,
157 }
158 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
159 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700160
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800161 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700162 if cert != "" {
163 ctx.AddDependency(ctx.Module(), certificateTag, cert)
164 }
165
166 for _, cert := range a.appProperties.Additional_certificates {
167 cert = android.SrcIsModule(cert)
168 if cert != "" {
169 ctx.AddDependency(ctx.Module(), certificateTag, cert)
170 } else {
171 ctx.PropertyErrorf("additional_certificates",
172 `must be names of android_app_certificate modules in the form ":module"`)
173 }
174 }
Colin Cross30e076a2015-04-13 13:58:27 -0700175}
176
Colin Cross46c9b8b2017-06-22 16:51:17 -0700177func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosse4246ab2019-02-05 21:55:21 -0800178 a.aapt.uncompressedJNI = a.shouldUncompressJNI(ctx)
Colin Cross46abdad2019-02-07 13:07:08 -0800179 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
Colin Crossae5caf52018-05-22 11:11:52 -0700180 a.generateAndroidBuildActions(ctx)
181}
182
Colin Crosse4246ab2019-02-05 21:55:21 -0800183// shouldUncompressJNI returns true if the native libraries should be stored in the APK uncompressed and the
184// extractNativeLibs application flag should be set to false in the manifest.
185func (a *AndroidApp) shouldUncompressJNI(ctx android.ModuleContext) bool {
186 minSdkVersion, err := sdkVersionToNumber(ctx, a.minSdkVersion())
187 if err != nil {
188 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
189 }
190
191 return minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)
192}
193
Colin Cross43f08db2018-11-12 10:13:39 -0800194// Returns whether this module should have the dex file stored uncompressed in the APK.
195func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800196 if Bool(a.appProperties.Use_embedded_dex) {
197 return true
198 }
199
Colin Cross43377ee2019-06-25 13:35:30 -0700200 if ctx.Config().UnbundledBuild() {
201 return false
202 }
203
Jaewoong Jung160456b2019-05-02 14:55:29 -0700204 // Uncompress dex in APKs of privileged apps
205 if ctx.Config().UncompressPrivAppDex() && Bool(a.appProperties.Privileged) {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000206 return true
207 }
208
Jaewoong Jung160456b2019-05-02 14:55:29 -0700209 return shouldUncompressDex(ctx, &a.dexpreopter)
Colin Cross5a0dcd52018-10-05 14:20:06 -0700210}
211
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800212func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
David Brazdild25060a2019-02-18 18:24:16 +0000213 a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis)
214
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800215 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800216
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800217 // 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 -0700218 hasProduct := false
219 for _, f := range a.aaptProperties.Aaptflags {
220 if strings.HasPrefix(f, "--product") {
221 hasProduct = true
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800222 break
Colin Crosse78dcd32018-04-19 15:25:19 -0700223 }
224 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700225 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800226 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700227 }
228
Dan Willemsen72be5902018-10-24 20:24:57 -0700229 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
230 // Product AAPT config
231 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800232 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700233 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700234
Dan Willemsen72be5902018-10-24 20:24:57 -0700235 // Product AAPT preferred config
236 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800237 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700238 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700239 }
240
Jiyong Park7f67f482019-01-05 12:57:48 +0900241 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700242 if overridden || a.overridableAppProperties.Package_name != nil {
243 // The product override variable has a priority over the package_name property.
244 if !overridden {
245 manifestPackageName = *a.overridableAppProperties.Package_name
246 }
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800247 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
Jiyong Park7f67f482019-01-05 12:57:48 +0900248 }
249
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800250 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
251
Colin Crosse560c4a2019-03-19 16:03:11 -0700252 a.aapt.splitNames = a.appProperties.Package_splits
253
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800254 a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700255
Colin Cross46c9b8b2017-06-22 16:51:17 -0700256 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700257 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800258}
Colin Cross30e076a2015-04-13 13:58:27 -0700259
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800260func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700261 var staticLibProguardFlagFiles android.Paths
262 ctx.VisitDirectDeps(func(m android.Module) {
263 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
264 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
265 }
266 })
267
268 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
269
270 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
271 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800272}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800273
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800274func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross43f08db2018-11-12 10:13:39 -0800275
276 var installDir string
277 if ctx.ModuleName() == "framework-res" {
278 // framework-res.apk is installed as system/framework/framework-res.apk
279 installDir = "framework"
280 } else if Bool(a.appProperties.Privileged) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800281 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800282 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800283 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800284 }
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800285 a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000286 a.dexpreopter.isInstallable = Bool(a.properties.Installable)
287 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
288 a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex
Colin Cross5a0dcd52018-10-05 14:20:06 -0700289
Rashed Abdel-Tawab47d35aa2018-08-09 14:08:53 -0700290 if ctx.ModuleName() != "framework-res" && ctx.ModuleName() != "org.lineageos.platform-res" {
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800291 a.Module.compile(ctx, a.aaptSrcJar)
292 }
Colin Cross30e076a2015-04-13 13:58:27 -0700293
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800294 return a.maybeStrippedDexJarFile
295}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800296
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800297func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700298 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700299 if len(jniLibs) > 0 {
Colin Cross47fa9d32019-03-26 10:51:39 -0700300 embedJni := ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) ||
301 a.appProperties.AlwaysPackageNativeLibs
Colin Crossa4f08812018-10-02 22:03:40 -0700302 if embedJni {
303 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Colin Crosse4246ab2019-02-05 21:55:21 -0800304 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.shouldUncompressJNI(ctx))
Colin Crossa4f08812018-10-02 22:03:40 -0700305 } else {
306 a.installJniLibs = jniLibs
307 }
308 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800309 return jniJarFile
310}
Colin Crossa4f08812018-10-02 22:03:40 -0700311
Jaewoong Jung33695b92019-04-15 09:48:31 -0700312// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
313// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
314func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate {
315 if android.SrcIsModule(certPropValue) == "" {
316 var mainCert Certificate
317 if certPropValue != "" {
318 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
319 mainCert = Certificate{
320 defaultDir.Join(ctx, certPropValue+".x509.pem"),
321 defaultDir.Join(ctx, certPropValue+".pk8"),
322 }
323 } else {
324 pem, key := ctx.Config().DefaultAppCertificate(ctx)
325 mainCert = Certificate{pem, key}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700326 }
Jaewoong Jung33695b92019-04-15 09:48:31 -0700327 certificates = append([]Certificate{mainCert}, certificates...)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700328 }
329
Jaewoong Jung33695b92019-04-15 09:48:31 -0700330 if !m.Platform() {
331 certPath := certificates[0].Pem.String()
Jeongik Chac9464142019-01-07 12:07:27 +0900332 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
333 if strings.HasPrefix(certPath, systemCertPath) {
334 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
335 whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
336
Jaewoong Jung33695b92019-04-15 09:48:31 -0700337 if enforceSystemCert && !inList(m.Name(), whitelist) {
Jeongik Chac9464142019-01-07 12:07:27 +0900338 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
339 }
340 }
341 }
342
Jaewoong Jung33695b92019-04-15 09:48:31 -0700343 return certificates
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800344}
345
Jaewoong Jung1ffd7932019-09-11 10:25:18 -0700346func (a *AndroidApp) noticeBuildActions(ctx android.ModuleContext) android.OptionalPath {
Jaewoong Jung5c6572e2019-06-17 17:40:56 -0700347 if !Bool(a.appProperties.Embed_notices) && !ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") {
348 return android.OptionalPath{}
349 }
350
351 // Collect NOTICE files from all dependencies.
352 seenModules := make(map[android.Module]bool)
353 noticePathSet := make(map[android.Path]bool)
354
355 ctx.WalkDepsBlueprint(func(child blueprint.Module, parent blueprint.Module) bool {
356 if _, ok := child.(android.Module); !ok {
357 return false
358 }
359 module := child.(android.Module)
360 // Have we already seen this?
361 if _, ok := seenModules[module]; ok {
362 return false
363 }
364 seenModules[module] = true
365
366 // Skip host modules.
367 if module.Target().Os.Class == android.Host || module.Target().Os.Class == android.HostCross {
368 return false
369 }
370
371 path := module.NoticeFile()
372 if path.Valid() {
373 noticePathSet[path.Path()] = true
374 }
375 return true
376 })
377
378 // If the app has one, add it too.
379 if a.NoticeFile().Valid() {
380 noticePathSet[a.NoticeFile().Path()] = true
381 }
382
383 if len(noticePathSet) == 0 {
384 return android.OptionalPath{}
385 }
386 var noticePaths []android.Path
387 for path := range noticePathSet {
388 noticePaths = append(noticePaths, path)
389 }
390 sort.Slice(noticePaths, func(i, j int) bool {
391 return noticePaths[i].String() < noticePaths[j].String()
392 })
Jaewoong Jung1ffd7932019-09-11 10:25:18 -0700393 noticeFile := android.BuildNoticeOutput(ctx, a.installDir, a.installApkName+".apk", noticePaths)
Jaewoong Jung5c6572e2019-06-17 17:40:56 -0700394
395 return android.OptionalPathForPath(noticeFile)
396}
397
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800398func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800399 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800400 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800401
Jaewoong Jung5c6572e2019-06-17 17:40:56 -0700402 if ctx.ModuleName() == "framework-res" {
403 // framework-res.apk is installed as system/framework/framework-res.apk
Jaewoong Jung1ffd7932019-09-11 10:25:18 -0700404 a.installDir = android.PathForModuleInstall(ctx, "framework")
Rashed Abdel-Tawab47d35aa2018-08-09 14:08:53 -0700405 } else if ctx.ModuleName() == "org.lineageos.platform-res" {
406 // org.lineageos.platform-res.apk needs to be in system/framework
Jaewoong Jung1ffd7932019-09-11 10:25:18 -0700407 a.installDir = android.PathForModuleInstall(ctx, "framework")
Jaewoong Jung5c6572e2019-06-17 17:40:56 -0700408 } else if Bool(a.appProperties.Privileged) {
Jaewoong Jung1ffd7932019-09-11 10:25:18 -0700409 a.installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
410 } else if ctx.InstallInTestcases() {
411 a.installDir = android.PathForModuleInstall(ctx, a.installApkName)
Jaewoong Jung5c6572e2019-06-17 17:40:56 -0700412 } else {
Jaewoong Jung1ffd7932019-09-11 10:25:18 -0700413 a.installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
Jaewoong Jung5c6572e2019-06-17 17:40:56 -0700414 }
Jaewoong Jung0b09bdb2019-09-27 17:13:15 -0700415 a.onDeviceDir = android.InstallPathToOnDevicePath(ctx, a.installDir)
Jaewoong Jung5c6572e2019-06-17 17:40:56 -0700416
Jaewoong Jung1ffd7932019-09-11 10:25:18 -0700417 a.aapt.noticeFile = a.noticeBuildActions(ctx)
Jaewoong Jung5c6572e2019-06-17 17:40:56 -0700418
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800419 // Process all building blocks, from AAPT to certificates.
420 a.aaptBuildActions(ctx)
421
422 a.proguardBuildActions(ctx)
423
424 dexJarFile := a.dexBuildActions(ctx)
425
Jaewoong Jung33695b92019-04-15 09:48:31 -0700426 jniLibs, certificateDeps := collectAppDeps(ctx)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800427 jniJarFile := a.jniBuildActions(jniLibs, ctx)
428
429 if ctx.Failed() {
430 return
431 }
432
Jaewoong Jung33695b92019-04-15 09:48:31 -0700433 certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx)
434 a.certificate = certificates[0]
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800435
436 // Build a final signed app package.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800437 // TODO(jungjw): Consider changing this to installApkName.
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800438 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".apk")
Jaewoong Jung33695b92019-04-15 09:48:31 -0700439 CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800440 a.outputFile = packageFile
441
Colin Crosse560c4a2019-03-19 16:03:11 -0700442 for _, split := range a.aapt.splits {
443 // Sign the split APKs
444 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"_"+split.suffix+".apk")
Jaewoong Jung33695b92019-04-15 09:48:31 -0700445 CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates)
Colin Crosse560c4a2019-03-19 16:03:11 -0700446 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
447 }
448
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800449 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700450 bundleFile := android.PathForModuleOut(ctx, "base.zip")
451 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
452 a.bundleFile = bundleFile
453
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800454 // Install the app package.
Jaewoong Jung1ffd7932019-09-11 10:25:18 -0700455 ctx.InstallFile(a.installDir, a.installApkName+".apk", a.outputFile)
Colin Crosse560c4a2019-03-19 16:03:11 -0700456 for _, split := range a.aapt.splits {
Jaewoong Jung1ffd7932019-09-11 10:25:18 -0700457 ctx.InstallFile(a.installDir, a.installApkName+"_"+split.suffix+".apk", split.path)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800458 }
Colin Cross30e076a2015-04-13 13:58:27 -0700459}
460
Jaewoong Jung33695b92019-04-15 09:48:31 -0700461func collectAppDeps(ctx android.ModuleContext) ([]jniLib, []Certificate) {
Colin Crossa4f08812018-10-02 22:03:40 -0700462 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900463 var certificates []Certificate
Colin Crossa4f08812018-10-02 22:03:40 -0700464
465 ctx.VisitDirectDeps(func(module android.Module) {
466 otherName := ctx.OtherModuleName(module)
467 tag := ctx.OtherModuleDependencyTag(module)
468
469 if jniTag, ok := tag.(*jniDependencyTag); ok {
470 if dep, ok := module.(*cc.Module); ok {
471 lib := dep.OutputFile()
472 if lib.Valid() {
473 jniLibs = append(jniLibs, jniLib{
474 name: ctx.OtherModuleName(module),
475 path: lib.Path(),
476 target: jniTag.target,
477 })
478 } else {
479 ctx.ModuleErrorf("dependency %q missing output file", otherName)
480 }
481 } else {
482 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
Colin Crossa4f08812018-10-02 22:03:40 -0700483 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700484 } else if tag == certificateTag {
485 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900486 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700487 } else {
488 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
489 }
Colin Crossa4f08812018-10-02 22:03:40 -0700490 }
491 })
492
Colin Crossbd01e2a2018-10-04 15:21:03 -0700493 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700494}
495
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800496func (a *AndroidApp) getCertString(ctx android.BaseContext) string {
497 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
498 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000499 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800500 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800501 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800502}
503
Colin Cross1b16b0e2019-02-12 14:41:32 -0800504// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700505func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700506 module := &AndroidApp{}
507
Sasha Smundak4eaeab42019-04-16 17:16:58 -0700508 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross66dbc0b2017-12-28 12:23:20 -0800509 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
510
Colin Crossae5caf52018-05-22 11:11:52 -0700511 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700512 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700513
Colin Cross36242852017-06-23 15:06:31 -0700514 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700515 &module.Module.properties,
516 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800517 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700518 &module.Module.protoProperties,
519 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800520 &module.appProperties,
521 &module.overridableAppProperties)
Colin Cross36242852017-06-23 15:06:31 -0700522
Colin Crossa9d8bee2018-10-02 13:59:46 -0700523 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
524 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
525 })
526
Colin Crossa4f08812018-10-02 22:03:40 -0700527 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
528 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800529 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossa4f08812018-10-02 22:03:40 -0700530
Colin Cross36242852017-06-23 15:06:31 -0700531 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700532}
Colin Crossae5caf52018-05-22 11:11:52 -0700533
534type appTestProperties struct {
535 Instrumentation_for *string
536}
537
538type AndroidTest struct {
539 AndroidApp
540
541 appTestProperties appTestProperties
542
543 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700544
545 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700546 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700547}
548
Jaewoong Jung1ffd7932019-09-11 10:25:18 -0700549func (a *AndroidTest) InstallInTestcases() bool {
550 return true
551}
552
Colin Crossae5caf52018-05-22 11:11:52 -0700553func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800554 // Check if the instrumentation target package is overridden before generating build actions.
555 if a.appTestProperties.Instrumentation_for != nil {
556 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
557 if overridden {
558 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
559 }
560 }
Colin Crossae5caf52018-05-22 11:11:52 -0700561 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700562
yangbill4f41bc22019-02-13 21:45:47 +0800563 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 -0800564 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700565}
566
567func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -0700568 a.AndroidApp.DepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -0700569 if a.appTestProperties.Instrumentation_for != nil {
570 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
571 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
572 // use instrumentationForTag instead of libTag.
573 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
574 }
Colin Crossae5caf52018-05-22 11:11:52 -0700575}
576
Colin Cross1b16b0e2019-02-12 14:41:32 -0800577// android_test compiles test sources and Android resources into an Android application package `.apk` file and
578// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -0700579func AndroidTestFactory() android.Module {
580 module := &AndroidTest{}
581
Sasha Smundak4eaeab42019-04-16 17:16:58 -0700582 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross5067db92018-09-17 16:46:35 -0700583
584 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700585 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800586 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700587 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800588 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -0700589
590 module.AddProperties(
591 &module.Module.properties,
592 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800593 &module.Module.dexpreoptProperties,
Colin Crossae5caf52018-05-22 11:11:52 -0700594 &module.Module.protoProperties,
595 &module.aaptProperties,
596 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700597 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800598 &module.overridableAppProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700599 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700600
Colin Crossa4f08812018-10-02 22:03:40 -0700601 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
602 android.InitDefaultableModule(module)
Colin Crossae5caf52018-05-22 11:11:52 -0700603 return module
604}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700605
Colin Cross252fc6f2018-10-04 15:22:03 -0700606type appTestHelperAppProperties struct {
607 // list of compatibility suites (for example "cts", "vts") that the module should be
608 // installed into.
609 Test_suites []string `android:"arch_variant"`
610}
611
612type AndroidTestHelperApp struct {
613 AndroidApp
614
615 appTestHelperAppProperties appTestHelperAppProperties
616}
617
Colin Cross1b16b0e2019-02-12 14:41:32 -0800618// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
619// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
620// test.
Colin Cross252fc6f2018-10-04 15:22:03 -0700621func AndroidTestHelperAppFactory() android.Module {
622 module := &AndroidTestHelperApp{}
623
Sasha Smundak4eaeab42019-04-16 17:16:58 -0700624 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700625
626 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800627 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700628 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800629 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700630
631 module.AddProperties(
632 &module.Module.properties,
633 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800634 &module.Module.dexpreoptProperties,
Colin Cross252fc6f2018-10-04 15:22:03 -0700635 &module.Module.protoProperties,
636 &module.aaptProperties,
637 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800638 &module.appTestHelperAppProperties,
639 &module.overridableAppProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -0700640
641 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
642 android.InitDefaultableModule(module)
643 return module
644}
645
Colin Crossbd01e2a2018-10-04 15:21:03 -0700646type AndroidAppCertificate struct {
647 android.ModuleBase
648 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900649 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -0700650}
651
652type AndroidAppCertificateProperties struct {
653 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
654 Certificate *string
655}
656
Colin Cross1b16b0e2019-02-12 14:41:32 -0800657// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
658// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -0700659func AndroidAppCertificateFactory() android.Module {
660 module := &AndroidAppCertificate{}
661 module.AddProperties(&module.properties)
662 android.InitAndroidModule(module)
663 return module
664}
665
Colin Crossbd01e2a2018-10-04 15:21:03 -0700666func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
667 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900668 c.Certificate = Certificate{
Colin Crossbd01e2a2018-10-04 15:21:03 -0700669 android.PathForModuleSrc(ctx, cert+".x509.pem"),
670 android.PathForModuleSrc(ctx, cert+".pk8"),
671 }
672}
Jaewoong Jung525443a2019-02-28 15:35:54 -0800673
674type OverrideAndroidApp struct {
675 android.ModuleBase
676 android.OverrideModuleBase
677}
678
679func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
680 // All the overrides happen in the base module.
681 // TODO(jungjw): Check the base module type.
682}
683
684// override_android_app is used to create an android_app module based on another android_app by overriding
685// some of its properties.
686func OverrideAndroidAppModuleFactory() android.Module {
687 m := &OverrideAndroidApp{}
688 m.AddProperties(&overridableAppProperties{})
689
690 android.InitAndroidModule(m)
691 android.InitOverrideModule(m)
692 return m
693}
Jaewoong Jung33695b92019-04-15 09:48:31 -0700694
695type AndroidAppImport struct {
696 android.ModuleBase
697 android.DefaultableModuleBase
698 prebuilt android.Prebuilt
699
Jaewoong Jung9e0fbd52019-08-13 14:11:33 -0700700 properties AndroidAppImportProperties
701 dpiVariants interface{}
702 archVariants interface{}
Jaewoong Jung33695b92019-04-15 09:48:31 -0700703
704 outputFile android.Path
705 certificate *Certificate
706
707 dexpreopter
Jaewoong Jung9b008e42019-07-17 10:21:49 -0700708
709 installPath android.OutputPath
Jaewoong Jung33695b92019-04-15 09:48:31 -0700710}
711
712type AndroidAppImportProperties struct {
713 // A prebuilt apk to import
Jaewoong Jung05d5b542019-06-11 12:25:34 -0700714 Apk *string
Jaewoong Jung9c6f3142019-04-26 14:31:50 -0700715
Jaewoong Junga7d90022019-08-22 14:25:58 -0700716 // The name of a certificate in the default certificate directory or an android_app_certificate
717 // module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
Jaewoong Jung33695b92019-04-15 09:48:31 -0700718 Certificate *string
719
720 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
721 // be set for presigned modules.
722 Presigned *bool
723
Jaewoong Junga7d90022019-08-22 14:25:58 -0700724 // Sign with the default system dev certificate. Must be used judiciously. Most imported apps
725 // need to either specify a specific certificate or be presigned.
726 Default_dev_cert *bool
727
Jaewoong Jung33695b92019-04-15 09:48:31 -0700728 // Specifies that this app should be installed to the priv-app directory,
729 // where the system will grant it additional privileges not available to
730 // normal apps.
731 Privileged *bool
732
733 // Names of modules to be overridden. Listed modules can only be other binaries
734 // (in Make or Soong).
735 // This does not completely prevent installation of the overridden binaries, but if both
736 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
737 // from PRODUCT_PACKAGES.
738 Overrides []string
Jaewoong Jung9b008e42019-07-17 10:21:49 -0700739
740 // Optional name for the installed app. If unspecified, it is derived from the module name.
741 Filename *string
Jaewoong Jung33695b92019-04-15 09:48:31 -0700742}
743
Jaewoong Jung9e0fbd52019-08-13 14:11:33 -0700744// Updates properties with variant-specific values.
745func (a *AndroidAppImport) processVariants(ctx android.LoadHookContext) {
Jaewoong Jung05d5b542019-06-11 12:25:34 -0700746 config := ctx.Config()
747
748 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants")
749 // Try DPI variant matches in the reverse-priority order so that the highest priority match
750 // overwrites everything else.
751 // TODO(jungjw): Can we optimize this by making it priority order?
752 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
Jaewoong Jung9e0fbd52019-08-13 14:11:33 -0700753 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
Jaewoong Jung9c6f3142019-04-26 14:31:50 -0700754 }
Jaewoong Jung05d5b542019-06-11 12:25:34 -0700755 if config.ProductAAPTPreferredConfig() != "" {
Jaewoong Jung9e0fbd52019-08-13 14:11:33 -0700756 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
Jaewoong Jung9c6f3142019-04-26 14:31:50 -0700757 }
Jaewoong Jung9e0fbd52019-08-13 14:11:33 -0700758
759 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName("Arch")
760 archType := ctx.Config().Targets[android.Android][0].Arch.ArchType
761 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
Jaewoong Jung9c6f3142019-04-26 14:31:50 -0700762}
763
Jaewoong Jung05d5b542019-06-11 12:25:34 -0700764func MergePropertiesFromVariant(ctx android.BaseModuleContext,
Jaewoong Jung9e0fbd52019-08-13 14:11:33 -0700765 dst interface{}, variantGroup reflect.Value, variant string) {
Jaewoong Jung05d5b542019-06-11 12:25:34 -0700766 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
767 if !src.IsValid() {
Jaewoong Jung9e0fbd52019-08-13 14:11:33 -0700768 return
Jaewoong Jung9c6f3142019-04-26 14:31:50 -0700769 }
770
Jaewoong Jung05d5b542019-06-11 12:25:34 -0700771 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
772 if err != nil {
773 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
774 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
775 } else {
776 panic(err)
777 }
778 }
Jaewoong Jung9c6f3142019-04-26 14:31:50 -0700779}
780
Jaewoong Jung33695b92019-04-15 09:48:31 -0700781func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
782 cert := android.SrcIsModule(String(a.properties.Certificate))
783 if cert != "" {
784 ctx.AddDependency(ctx.Module(), certificateTag, cert)
785 }
786}
787
788func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
789 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
790 rule := android.NewRuleBuilder()
791 rule.Command().
792 Textf(`if (zipinfo %s 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
793 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
794 FlagWithInput("-i ", inputPath).
795 FlagWithOutput("-o ", outputPath).
796 FlagWithArg("-0 ", "'lib/**/*.so'").
797 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
798 rule.Build(pctx, ctx, "uncompress-embedded-jni-libs", "Uncompress embedded JIN libs")
799}
800
Jaewoong Jung160456b2019-05-02 14:55:29 -0700801// Returns whether this module should have the dex file stored uncompressed in the APK.
802func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
803 if ctx.Config().UnbundledBuild() {
804 return false
805 }
806
807 // Uncompress dex in APKs of privileged apps
808 if ctx.Config().UncompressPrivAppDex() && Bool(a.properties.Privileged) {
809 return true
810 }
811
812 return shouldUncompressDex(ctx, &a.dexpreopter)
813}
814
Jaewoong Jung7fffdc42019-05-09 14:36:34 -0700815func (a *AndroidAppImport) uncompressDex(
816 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
817 rule := android.NewRuleBuilder()
818 rule.Command().
819 Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
820 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
821 FlagWithInput("-i ", inputPath).
822 FlagWithOutput("-o ", outputPath).
823 FlagWithArg("-0 ", "'classes*.dex'").
824 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
825 rule.Build(pctx, ctx, "uncompress-dex", "Uncompress dex files")
826}
827
Jaewoong Jung33695b92019-04-15 09:48:31 -0700828func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Junga7d90022019-08-22 14:25:58 -0700829 numCertPropsSet := 0
830 if String(a.properties.Certificate) != "" {
831 numCertPropsSet++
Jaewoong Jung33695b92019-04-15 09:48:31 -0700832 }
Jaewoong Junga7d90022019-08-22 14:25:58 -0700833 if Bool(a.properties.Presigned) {
834 numCertPropsSet++
835 }
836 if Bool(a.properties.Default_dev_cert) {
837 numCertPropsSet++
838 }
839 if numCertPropsSet != 1 {
840 ctx.ModuleErrorf("One and only one of certficate, presigned, and default_dev_cert properties must be set")
Jaewoong Jung33695b92019-04-15 09:48:31 -0700841 }
842
843 _, certificates := collectAppDeps(ctx)
844
845 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
Jaewoong Jung33695b92019-04-15 09:48:31 -0700846 // TODO: LOCAL_PACKAGE_SPLITS
847
Jaewoong Jung05d5b542019-06-11 12:25:34 -0700848 srcApk := a.prebuilt.SingleSourcePath(ctx)
Jaewoong Jung33695b92019-04-15 09:48:31 -0700849
850 // TODO: Install or embed JNI libraries
851
852 // Uncompress JNI libraries in the apk
853 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
854 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
855
Jaewoong Jung33695b92019-04-15 09:48:31 -0700856 installDir := android.PathForModuleInstall(ctx, "app", a.BaseModuleName())
857 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
858 a.dexpreopter.isInstallable = true
859 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
Jaewoong Jung160456b2019-05-02 14:55:29 -0700860 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Jaewoong Jung33695b92019-04-15 09:48:31 -0700861 dexOutput := a.dexpreopter.dexpreopt(ctx, jnisUncompressed)
Jaewoong Jung7fffdc42019-05-09 14:36:34 -0700862 if a.dexpreopter.uncompressedDex {
863 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
864 a.uncompressDex(ctx, dexOutput, dexUncompressed.OutputPath)
865 dexOutput = dexUncompressed
866 }
Jaewoong Jung33695b92019-04-15 09:48:31 -0700867
868 // Sign or align the package
869 // TODO: Handle EXTERNAL
870 if !Bool(a.properties.Presigned) {
Jaewoong Junga7d90022019-08-22 14:25:58 -0700871 // If the certificate property is empty at this point, default_dev_cert must be set to true.
872 // Which makes processMainCert's behavior for the empty cert string WAI.
873 certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
Jaewoong Jung33695b92019-04-15 09:48:31 -0700874 if len(certificates) != 1 {
875 ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates)
876 }
877 a.certificate = &certificates[0]
878 signed := android.PathForModuleOut(ctx, "signed", ctx.ModuleName()+".apk")
879 SignAppPackage(ctx, signed, dexOutput, certificates)
880 a.outputFile = signed
881 } else {
882 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", ctx.ModuleName()+".apk")
883 TransformZipAlign(ctx, alignedApk, dexOutput)
884 a.outputFile = alignedApk
885 }
886
887 // TODO: Optionally compress the output apk.
888
Jaewoong Jung9b008e42019-07-17 10:21:49 -0700889 a.installPath = ctx.InstallFile(installDir,
890 proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk"), a.outputFile)
Jaewoong Jung33695b92019-04-15 09:48:31 -0700891
892 // TODO: androidmk converter jni libs
893}
894
895func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
896 return &a.prebuilt
897}
898
899func (a *AndroidAppImport) Name() string {
900 return a.prebuilt.Name(a.ModuleBase.Name())
901}
902
Jaewoong Jung9e0fbd52019-08-13 14:11:33 -0700903var dpiVariantGroupType reflect.Type
904var archVariantGroupType reflect.Type
Jaewoong Jung05d5b542019-06-11 12:25:34 -0700905
Jaewoong Jung9e0fbd52019-08-13 14:11:33 -0700906func initAndroidAppImportVariantGroupTypes() {
907 dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants")
908
909 archNames := make([]string, len(android.ArchTypeList()))
910 for i, archType := range android.ArchTypeList() {
911 archNames[i] = archType.Name
912 }
913 archVariantGroupType = createVariantGroupType(archNames, "Arch")
914}
915
916// Populates all variant struct properties at creation time.
917func (a *AndroidAppImport) populateAllVariantStructs() {
918 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
919 a.AddProperties(a.dpiVariants)
920
921 a.archVariants = reflect.New(archVariantGroupType).Interface()
922 a.AddProperties(a.archVariants)
923}
924
925func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
926 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
927
928 variantFields := make([]reflect.StructField, len(variants))
929 for i, variant := range variants {
930 variantFields[i] = reflect.StructField{
931 Name: proptools.FieldNameForProperty(variant),
Jaewoong Jung05d5b542019-06-11 12:25:34 -0700932 Type: props,
933 }
934 }
Jaewoong Jung9e0fbd52019-08-13 14:11:33 -0700935
936 variantGroupStruct := reflect.StructOf(variantFields)
937 return reflect.StructOf([]reflect.StructField{
Jaewoong Jung05d5b542019-06-11 12:25:34 -0700938 {
Jaewoong Jung9e0fbd52019-08-13 14:11:33 -0700939 Name: variantGroupName,
940 Type: variantGroupStruct,
Jaewoong Jung05d5b542019-06-11 12:25:34 -0700941 },
Jaewoong Jung9e0fbd52019-08-13 14:11:33 -0700942 })
Jaewoong Jung05d5b542019-06-11 12:25:34 -0700943}
944
Jaewoong Jung33695b92019-04-15 09:48:31 -0700945// android_app_import imports a prebuilt apk with additional processing specified in the module.
Jaewoong Jung05d5b542019-06-11 12:25:34 -0700946// DPI-specific apk source files can be specified using dpi_variants. Example:
947//
948// android_app_import {
949// name: "example_import",
950// apk: "prebuilts/example.apk",
951// dpi_variants: {
952// mdpi: {
953// apk: "prebuilts/example_mdpi.apk",
954// },
955// xhdpi: {
956// apk: "prebuilts/example_xhdpi.apk",
957// },
958// },
959// certificate: "PRESIGNED",
960// }
Jaewoong Jung33695b92019-04-15 09:48:31 -0700961func AndroidAppImportFactory() android.Module {
962 module := &AndroidAppImport{}
963 module.AddProperties(&module.properties)
964 module.AddProperties(&module.dexpreoptProperties)
Jaewoong Jung9e0fbd52019-08-13 14:11:33 -0700965 module.populateAllVariantStructs()
Jaewoong Jung05d5b542019-06-11 12:25:34 -0700966 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jaewoong Jung9e0fbd52019-08-13 14:11:33 -0700967 module.processVariants(ctx)
Jaewoong Jung05d5b542019-06-11 12:25:34 -0700968 })
Jaewoong Jung33695b92019-04-15 09:48:31 -0700969
970 InitJavaModule(module, android.DeviceSupported)
Jaewoong Jung05d5b542019-06-11 12:25:34 -0700971 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
Jaewoong Jung33695b92019-04-15 09:48:31 -0700972
973 return module
974}