blob: 9f37836a33e311371d7e7dee0d8cb9fb5522f4a0 [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 Junga5e5abc2019-04-26 14:31:50 -070022 "path/filepath"
23 "reflect"
24 "strings"
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
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070031var supportedDpis = [...]string{"Ldpi", "Mdpi", "Hdpi", "Xhdpi", "Xxhdpi", "Xxxhdpi"}
32var dpiVariantsStruct reflect.Type
33
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 Jungccbb3932019-04-15 09:48:31 -070040 android.RegisterModuleType("android_app_import", AndroidAppImportFactory)
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070041
42 // Dynamically construct a struct for the dpi_variants property in android_app_import.
43 perDpiStruct := reflect.StructOf([]reflect.StructField{
44 {
45 Name: "Apk",
46 Type: reflect.TypeOf((*string)(nil)),
47 },
48 })
49 dpiVariantsFields := make([]reflect.StructField, len(supportedDpis))
50 for i, dpi := range supportedDpis {
51 dpiVariantsFields[i] = reflect.StructField{
52 Name: string(dpi),
53 Type: perDpiStruct,
54 }
55 }
56 dpiVariantsStruct = reflect.StructOf(dpiVariantsFields)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080057}
58
Colin Cross30e076a2015-04-13 13:58:27 -070059// AndroidManifest.xml merging
60// package splits
61
Colin Crossfabb6082018-02-20 17:22:23 -080062type appProperties struct {
Colin Crossbd01e2a2018-10-04 15:21:03 -070063 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
Colin Cross7d5136f2015-05-11 13:39:40 -070064 Additional_certificates []string
65
66 // If set, create package-export.apk, which other packages can
67 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
Nan Zhangea568a42017-11-08 21:20:04 -080068 Export_package_resources *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070069
Colin Cross16056062017-12-13 22:46:28 -080070 // Specifies that this app should be installed to the priv-app directory,
71 // where the system will grant it additional privileges not available to
72 // normal apps.
73 Privileged *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070074
75 // list of resource labels to generate individual resource packages
76 Package_splits []string
Jason Monkd4122be2018-08-10 09:33:36 -040077
78 // Names of modules to be overridden. Listed modules can only be other binaries
79 // (in Make or Soong).
80 // This does not completely prevent installation of the overridden binaries, but if both
81 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
82 // from PRODUCT_PACKAGES.
83 Overrides []string
Colin Crossa4f08812018-10-02 22:03:40 -070084
85 // list of native libraries that will be provided in or alongside the resulting jar
86 Jni_libs []string `android:"arch_variant"`
87
Colin Crosse4246ab2019-02-05 21:55:21 -080088 // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest
89 // flag so that they are used from inside the APK at runtime. Defaults to true for android_test modules unless
90 // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to false for other
91 // module types where the native libraries are generally preinstalled outside the APK.
92 Use_embedded_native_libs *bool
Colin Cross46abdad2019-02-07 13:07:08 -080093
94 // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that
95 // they are used from inside the APK at runtime.
96 Use_embedded_dex *bool
Colin Cross47fa9d32019-03-26 10:51:39 -070097
98 // Forces native libraries to always be packaged into the APK,
99 // Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed.
100 // True for android_test* modules.
101 AlwaysPackageNativeLibs bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700102}
103
Jaewoong Jung525443a2019-02-28 15:35:54 -0800104// android_app properties that can be overridden by override_android_app
105type overridableAppProperties struct {
106 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
107 // or an android_app_certificate module name in the form ":module".
108 Certificate *string
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700109
110 // the package name of this app. The package name in the manifest file is used if one was not given.
111 Package_name *string
Jaewoong Jung525443a2019-02-28 15:35:54 -0800112}
113
Colin Cross30e076a2015-04-13 13:58:27 -0700114type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -0700115 Library
116 aapt
Jaewoong Jung525443a2019-02-28 15:35:54 -0800117 android.OverridableModuleBase
Colin Crossa97c5d32018-03-28 14:58:31 -0700118
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900119 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -0700120
Colin Crossfabb6082018-02-20 17:22:23 -0800121 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -0700122
Jaewoong Jung525443a2019-02-28 15:35:54 -0800123 overridableAppProperties overridableAppProperties
124
Colin Crossa4f08812018-10-02 22:03:40 -0700125 installJniLibs []jniLib
Colin Crossf6237212018-10-29 23:14:58 -0700126
127 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800128
129 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
130 installApkName string
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800131
132 additionalAaptFlags []string
Colin Crosse1731a52017-12-14 11:22:55 -0800133}
134
Colin Cross89c31582018-04-30 15:55:11 -0700135func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
136 return nil
137}
138
Colin Cross66f78822018-05-02 12:58:28 -0700139func (a *AndroidApp) ExportedStaticPackages() android.Paths {
140 return nil
141}
142
Colin Crossa97c5d32018-03-28 14:58:31 -0700143var _ AndroidLibraryDependency = (*AndroidApp)(nil)
144
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900145type Certificate struct {
146 Pem, Key android.Path
Colin Cross30e076a2015-04-13 13:58:27 -0700147}
148
Colin Cross46c9b8b2017-06-22 16:51:17 -0700149func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
150 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700151
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800152 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700153 a.aapt.deps(ctx, sdkContext(a))
Colin Cross30e076a2015-04-13 13:58:27 -0700154 }
Colin Crossa4f08812018-10-02 22:03:40 -0700155
156 for _, jniTarget := range ctx.MultiTargets() {
157 variation := []blueprint.Variation{
158 {Mutator: "arch", Variation: jniTarget.String()},
159 {Mutator: "link", Variation: "shared"},
160 }
161 tag := &jniDependencyTag{
162 target: jniTarget,
163 }
164 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
165 }
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700166}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700167
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700168func (a *AndroidApp) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800169 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700170 if cert != "" {
171 ctx.AddDependency(ctx.Module(), certificateTag, cert)
172 }
173
174 for _, cert := range a.appProperties.Additional_certificates {
175 cert = android.SrcIsModule(cert)
176 if cert != "" {
177 ctx.AddDependency(ctx.Module(), certificateTag, cert)
178 } else {
179 ctx.PropertyErrorf("additional_certificates",
180 `must be names of android_app_certificate modules in the form ":module"`)
181 }
182 }
Colin Cross30e076a2015-04-13 13:58:27 -0700183}
184
Colin Cross46c9b8b2017-06-22 16:51:17 -0700185func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Sasha Smundak6ad77252019-05-01 13:16:22 -0700186 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
Colin Cross46abdad2019-02-07 13:07:08 -0800187 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
Colin Crossae5caf52018-05-22 11:11:52 -0700188 a.generateAndroidBuildActions(ctx)
189}
190
Sasha Smundak6ad77252019-05-01 13:16:22 -0700191// Returns true if the native libraries should be stored in the APK uncompressed and the
Colin Crosse4246ab2019-02-05 21:55:21 -0800192// extractNativeLibs application flag should be set to false in the manifest.
Sasha Smundak6ad77252019-05-01 13:16:22 -0700193func (a *AndroidApp) useEmbeddedNativeLibs(ctx android.ModuleContext) bool {
Colin Crosse4246ab2019-02-05 21:55:21 -0800194 minSdkVersion, err := sdkVersionToNumber(ctx, a.minSdkVersion())
195 if err != nil {
196 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
197 }
198
199 return minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)
200}
201
Colin Cross43f08db2018-11-12 10:13:39 -0800202// Returns whether this module should have the dex file stored uncompressed in the APK.
203func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800204 if Bool(a.appProperties.Use_embedded_dex) {
205 return true
206 }
207
Colin Cross5a0dcd52018-10-05 14:20:06 -0700208 if ctx.Config().UnbundledBuild() {
Colin Cross43f08db2018-11-12 10:13:39 -0800209 return false
Colin Cross5a0dcd52018-10-05 14:20:06 -0700210 }
211
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700212 // Uncompress dex in APKs of privileged apps
213 if ctx.Config().UncompressPrivAppDex() && Bool(a.appProperties.Privileged) {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000214 return true
215 }
216
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700217 return shouldUncompressDex(ctx, &a.dexpreopter)
Colin Cross5a0dcd52018-10-05 14:20:06 -0700218}
219
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800220func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
David Brazdild25060a2019-02-18 18:24:16 +0000221 a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis)
222
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800223 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800224
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800225 // 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 -0700226 hasProduct := false
227 for _, f := range a.aaptProperties.Aaptflags {
228 if strings.HasPrefix(f, "--product") {
229 hasProduct = true
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800230 break
Colin Crosse78dcd32018-04-19 15:25:19 -0700231 }
232 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700233 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800234 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700235 }
236
Dan Willemsen72be5902018-10-24 20:24:57 -0700237 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
238 // Product AAPT config
239 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800240 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700241 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700242
Dan Willemsen72be5902018-10-24 20:24:57 -0700243 // Product AAPT preferred config
244 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800245 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700246 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700247 }
248
Jiyong Park7f67f482019-01-05 12:57:48 +0900249 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700250 if overridden || a.overridableAppProperties.Package_name != nil {
251 // The product override variable has a priority over the package_name property.
252 if !overridden {
253 manifestPackageName = *a.overridableAppProperties.Package_name
254 }
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800255 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
Jiyong Park7f67f482019-01-05 12:57:48 +0900256 }
257
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800258 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
259
Colin Crosse560c4a2019-03-19 16:03:11 -0700260 a.aapt.splitNames = a.appProperties.Package_splits
261
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800262 a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700263
Colin Cross46c9b8b2017-06-22 16:51:17 -0700264 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700265 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800266}
Colin Cross30e076a2015-04-13 13:58:27 -0700267
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800268func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700269 var staticLibProguardFlagFiles android.Paths
270 ctx.VisitDirectDeps(func(m android.Module) {
271 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
272 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
273 }
274 })
275
276 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
277
278 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
279 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800280}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800281
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800282func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross43f08db2018-11-12 10:13:39 -0800283
284 var installDir string
285 if ctx.ModuleName() == "framework-res" {
286 // framework-res.apk is installed as system/framework/framework-res.apk
287 installDir = "framework"
288 } else if Bool(a.appProperties.Privileged) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800289 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800290 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800291 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800292 }
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800293 a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000294 a.dexpreopter.isInstallable = Bool(a.properties.Installable)
295 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
296 a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex
Colin Cross5a0dcd52018-10-05 14:20:06 -0700297
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800298 if ctx.ModuleName() != "framework-res" {
299 a.Module.compile(ctx, a.aaptSrcJar)
300 }
Colin Cross30e076a2015-04-13 13:58:27 -0700301
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800302 return a.maybeStrippedDexJarFile
303}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800304
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800305func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700306 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700307 if len(jniLibs) > 0 {
Colin Cross47fa9d32019-03-26 10:51:39 -0700308 embedJni := ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) ||
309 a.appProperties.AlwaysPackageNativeLibs
Colin Crossa4f08812018-10-02 22:03:40 -0700310 if embedJni {
311 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Sasha Smundak6ad77252019-05-01 13:16:22 -0700312 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.useEmbeddedNativeLibs(ctx))
Colin Crossa4f08812018-10-02 22:03:40 -0700313 } else {
314 a.installJniLibs = jniLibs
315 }
316 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800317 return jniJarFile
318}
Colin Crossa4f08812018-10-02 22:03:40 -0700319
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700320// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
321// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
322func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate {
323 if android.SrcIsModule(certPropValue) == "" {
324 var mainCert Certificate
325 if certPropValue != "" {
326 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
327 mainCert = Certificate{
328 defaultDir.Join(ctx, certPropValue+".x509.pem"),
329 defaultDir.Join(ctx, certPropValue+".pk8"),
330 }
331 } else {
332 pem, key := ctx.Config().DefaultAppCertificate(ctx)
333 mainCert = Certificate{pem, key}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700334 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700335 certificates = append([]Certificate{mainCert}, certificates...)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700336 }
337
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700338 if !m.Platform() {
339 certPath := certificates[0].Pem.String()
Jeongik Chac9464142019-01-07 12:07:27 +0900340 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
341 if strings.HasPrefix(certPath, systemCertPath) {
342 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
343 whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
344
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700345 if enforceSystemCert && !inList(m.Name(), whitelist) {
Jeongik Chac9464142019-01-07 12:07:27 +0900346 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
347 }
348 }
349 }
350
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700351 return certificates
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800352}
353
354func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800355 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800356 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800357
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800358 // Process all building blocks, from AAPT to certificates.
359 a.aaptBuildActions(ctx)
360
361 a.proguardBuildActions(ctx)
362
363 dexJarFile := a.dexBuildActions(ctx)
364
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700365 jniLibs, certificateDeps := collectAppDeps(ctx)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800366 jniJarFile := a.jniBuildActions(jniLibs, ctx)
367
368 if ctx.Failed() {
369 return
370 }
371
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700372 certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx)
373 a.certificate = certificates[0]
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800374
375 // Build a final signed app package.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800376 // TODO(jungjw): Consider changing this to installApkName.
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800377 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700378 CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800379 a.outputFile = packageFile
380
Colin Crosse560c4a2019-03-19 16:03:11 -0700381 for _, split := range a.aapt.splits {
382 // Sign the split APKs
383 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"_"+split.suffix+".apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700384 CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates)
Colin Crosse560c4a2019-03-19 16:03:11 -0700385 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
386 }
387
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800388 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700389 bundleFile := android.PathForModuleOut(ctx, "base.zip")
390 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
391 a.bundleFile = bundleFile
392
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800393 // Install the app package.
Colin Crosse560c4a2019-03-19 16:03:11 -0700394 var installDir android.OutputPath
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800395 if ctx.ModuleName() == "framework-res" {
396 // framework-res.apk is installed as system/framework/framework-res.apk
Colin Crosse560c4a2019-03-19 16:03:11 -0700397 installDir = android.PathForModuleInstall(ctx, "framework")
Colin Cross16056062017-12-13 22:46:28 -0800398 } else if Bool(a.appProperties.Privileged) {
Colin Crosse560c4a2019-03-19 16:03:11 -0700399 installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800400 } else {
Colin Crosse560c4a2019-03-19 16:03:11 -0700401 installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
402 }
403
404 ctx.InstallFile(installDir, a.installApkName+".apk", a.outputFile)
405 for _, split := range a.aapt.splits {
406 ctx.InstallFile(installDir, a.installApkName+"_"+split.suffix+".apk", split.path)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800407 }
Colin Cross30e076a2015-04-13 13:58:27 -0700408}
409
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700410func collectAppDeps(ctx android.ModuleContext) ([]jniLib, []Certificate) {
Colin Crossa4f08812018-10-02 22:03:40 -0700411 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900412 var certificates []Certificate
Colin Crossa4f08812018-10-02 22:03:40 -0700413
414 ctx.VisitDirectDeps(func(module android.Module) {
415 otherName := ctx.OtherModuleName(module)
416 tag := ctx.OtherModuleDependencyTag(module)
417
418 if jniTag, ok := tag.(*jniDependencyTag); ok {
419 if dep, ok := module.(*cc.Module); ok {
420 lib := dep.OutputFile()
421 if lib.Valid() {
422 jniLibs = append(jniLibs, jniLib{
423 name: ctx.OtherModuleName(module),
424 path: lib.Path(),
425 target: jniTag.target,
426 })
427 } else {
428 ctx.ModuleErrorf("dependency %q missing output file", otherName)
429 }
430 } else {
431 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
Colin Crossa4f08812018-10-02 22:03:40 -0700432 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700433 } else if tag == certificateTag {
434 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900435 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700436 } else {
437 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
438 }
Colin Crossa4f08812018-10-02 22:03:40 -0700439 }
440 })
441
Colin Crossbd01e2a2018-10-04 15:21:03 -0700442 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700443}
444
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800445func (a *AndroidApp) getCertString(ctx android.BaseContext) string {
446 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
447 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000448 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800449 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800450 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800451}
452
Colin Cross1b16b0e2019-02-12 14:41:32 -0800453// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700454func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700455 module := &AndroidApp{}
456
Sasha Smundak2057f822019-04-16 17:16:58 -0700457 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross66dbc0b2017-12-28 12:23:20 -0800458 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
459
Colin Crossae5caf52018-05-22 11:11:52 -0700460 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700461 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700462
Colin Cross36242852017-06-23 15:06:31 -0700463 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700464 &module.Module.properties,
465 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800466 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700467 &module.Module.protoProperties,
468 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800469 &module.appProperties,
470 &module.overridableAppProperties)
Colin Cross36242852017-06-23 15:06:31 -0700471
Colin Crossa9d8bee2018-10-02 13:59:46 -0700472 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
473 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
474 })
475
Colin Crossa4f08812018-10-02 22:03:40 -0700476 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
477 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800478 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossa4f08812018-10-02 22:03:40 -0700479
Colin Cross36242852017-06-23 15:06:31 -0700480 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700481}
Colin Crossae5caf52018-05-22 11:11:52 -0700482
483type appTestProperties struct {
484 Instrumentation_for *string
485}
486
487type AndroidTest struct {
488 AndroidApp
489
490 appTestProperties appTestProperties
491
492 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700493
494 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700495 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700496}
497
498func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800499 // Check if the instrumentation target package is overridden before generating build actions.
500 if a.appTestProperties.Instrumentation_for != nil {
501 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
502 if overridden {
503 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
504 }
505 }
Sasha Smundak6ad77252019-05-01 13:16:22 -0700506 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
507 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
Colin Crossae5caf52018-05-22 11:11:52 -0700508 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700509
yangbill4f41bc22019-02-13 21:45:47 +0800510 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 -0800511 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700512}
513
514func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -0700515 a.AndroidApp.DepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -0700516 if a.appTestProperties.Instrumentation_for != nil {
517 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
518 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
519 // use instrumentationForTag instead of libTag.
520 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
521 }
Colin Crossae5caf52018-05-22 11:11:52 -0700522}
523
Colin Cross1b16b0e2019-02-12 14:41:32 -0800524// android_test compiles test sources and Android resources into an Android application package `.apk` file and
525// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -0700526func AndroidTestFactory() android.Module {
527 module := &AndroidTest{}
528
Sasha Smundak2057f822019-04-16 17:16:58 -0700529 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross5067db92018-09-17 16:46:35 -0700530
531 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700532 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800533 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700534 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800535 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -0700536
537 module.AddProperties(
538 &module.Module.properties,
539 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800540 &module.Module.dexpreoptProperties,
Colin Crossae5caf52018-05-22 11:11:52 -0700541 &module.Module.protoProperties,
542 &module.aaptProperties,
543 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700544 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800545 &module.overridableAppProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700546 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700547
Colin Crossa4f08812018-10-02 22:03:40 -0700548 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
549 android.InitDefaultableModule(module)
Colin Crossae5caf52018-05-22 11:11:52 -0700550 return module
551}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700552
Colin Cross252fc6f2018-10-04 15:22:03 -0700553type appTestHelperAppProperties struct {
554 // list of compatibility suites (for example "cts", "vts") that the module should be
555 // installed into.
556 Test_suites []string `android:"arch_variant"`
557}
558
559type AndroidTestHelperApp struct {
560 AndroidApp
561
562 appTestHelperAppProperties appTestHelperAppProperties
563}
564
Colin Cross1b16b0e2019-02-12 14:41:32 -0800565// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
566// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
567// test.
Colin Cross252fc6f2018-10-04 15:22:03 -0700568func AndroidTestHelperAppFactory() android.Module {
569 module := &AndroidTestHelperApp{}
570
Sasha Smundak2057f822019-04-16 17:16:58 -0700571 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700572
573 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800574 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700575 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800576 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700577
578 module.AddProperties(
579 &module.Module.properties,
580 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800581 &module.Module.dexpreoptProperties,
Colin Cross252fc6f2018-10-04 15:22:03 -0700582 &module.Module.protoProperties,
583 &module.aaptProperties,
584 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800585 &module.appTestHelperAppProperties,
586 &module.overridableAppProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -0700587
588 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
589 android.InitDefaultableModule(module)
590 return module
591}
592
Colin Crossbd01e2a2018-10-04 15:21:03 -0700593type AndroidAppCertificate struct {
594 android.ModuleBase
595 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900596 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -0700597}
598
599type AndroidAppCertificateProperties struct {
600 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
601 Certificate *string
602}
603
Colin Cross1b16b0e2019-02-12 14:41:32 -0800604// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
605// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -0700606func AndroidAppCertificateFactory() android.Module {
607 module := &AndroidAppCertificate{}
608 module.AddProperties(&module.properties)
609 android.InitAndroidModule(module)
610 return module
611}
612
Colin Crossbd01e2a2018-10-04 15:21:03 -0700613func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
614 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900615 c.Certificate = Certificate{
Colin Crossbd01e2a2018-10-04 15:21:03 -0700616 android.PathForModuleSrc(ctx, cert+".x509.pem"),
617 android.PathForModuleSrc(ctx, cert+".pk8"),
618 }
619}
Jaewoong Jung525443a2019-02-28 15:35:54 -0800620
621type OverrideAndroidApp struct {
622 android.ModuleBase
623 android.OverrideModuleBase
624}
625
626func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
627 // All the overrides happen in the base module.
628 // TODO(jungjw): Check the base module type.
629}
630
631// override_android_app is used to create an android_app module based on another android_app by overriding
632// some of its properties.
633func OverrideAndroidAppModuleFactory() android.Module {
634 m := &OverrideAndroidApp{}
635 m.AddProperties(&overridableAppProperties{})
636
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700637 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800638 android.InitOverrideModule(m)
639 return m
640}
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700641
642type AndroidAppImport struct {
643 android.ModuleBase
644 android.DefaultableModuleBase
645 prebuilt android.Prebuilt
646
647 properties AndroidAppImportProperties
648
649 outputFile android.Path
650 certificate *Certificate
651
652 dexpreopter
653}
654
655type AndroidAppImportProperties struct {
656 // A prebuilt apk to import
657 Apk string
658
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700659 // Per-DPI settings. This property makes it possible to specify a different source apk path for
660 // each DPI.
661 //
662 // Example:
663 //
664 // android_app_import {
665 // name: "example_import",
666 // apk: "prebuilts/example.apk",
667 // dpi_variants: {
668 // mdpi: {
669 // apk: "prebuilts/example_mdpi.apk",
670 // },
671 // xhdpi: {
672 // apk: "prebuilts/example_xhdpi.apk",
673 // },
674 // },
675 // certificate: "PRESIGNED",
676 // }
677 Dpi_variants interface{}
678
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700679 // The name of a certificate in the default certificate directory, blank to use the default
680 // product certificate, or an android_app_certificate module name in the form ":module".
681 Certificate *string
682
683 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
684 // be set for presigned modules.
685 Presigned *bool
686
687 // Specifies that this app should be installed to the priv-app directory,
688 // where the system will grant it additional privileges not available to
689 // normal apps.
690 Privileged *bool
691
692 // Names of modules to be overridden. Listed modules can only be other binaries
693 // (in Make or Soong).
694 // This does not completely prevent installation of the overridden binaries, but if both
695 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
696 // from PRODUCT_PACKAGES.
697 Overrides []string
698}
699
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700700func getApkPathForDpi(dpiVariantsValue reflect.Value, dpi string) string {
701 dpiField := dpiVariantsValue.FieldByName(proptools.FieldNameForProperty(dpi))
702 if !dpiField.IsValid() {
703 return ""
704 }
705 apkValue := dpiField.FieldByName("Apk").Elem()
706 if apkValue.IsValid() {
707 return apkValue.String()
708 }
709 return ""
710}
711
712// Chooses a source APK path to use based on the module's per-DPI settings and the product config.
713func (a *AndroidAppImport) getSrcApkPath(ctx android.ModuleContext) string {
714 config := ctx.Config()
715 dpiVariantsValue := reflect.ValueOf(a.properties.Dpi_variants).Elem()
716 if !dpiVariantsValue.IsValid() {
717 return a.properties.Apk
718 }
719 // Match PRODUCT_AAPT_PREF_CONFIG first and then PRODUCT_AAPT_PREBUILT_DPI.
720 if config.ProductAAPTPreferredConfig() != "" {
721 if apk := getApkPathForDpi(dpiVariantsValue, config.ProductAAPTPreferredConfig()); apk != "" {
722 return apk
723 }
724 }
725 for _, dpi := range config.ProductAAPTPrebuiltDPI() {
726 if apk := getApkPathForDpi(dpiVariantsValue, dpi); apk != "" {
727 return apk
728 }
729 }
730
731 // No match. Use the generic one.
732 return a.properties.Apk
733}
734
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700735func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
736 cert := android.SrcIsModule(String(a.properties.Certificate))
737 if cert != "" {
738 ctx.AddDependency(ctx.Module(), certificateTag, cert)
739 }
740}
741
742func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
743 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
744 rule := android.NewRuleBuilder()
745 rule.Command().
746 Textf(`if (zipinfo %s 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
747 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
748 FlagWithInput("-i ", inputPath).
749 FlagWithOutput("-o ", outputPath).
750 FlagWithArg("-0 ", "'lib/**/*.so'").
751 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
752 rule.Build(pctx, ctx, "uncompress-embedded-jni-libs", "Uncompress embedded JIN libs")
753}
754
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700755// Returns whether this module should have the dex file stored uncompressed in the APK.
756func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
757 if ctx.Config().UnbundledBuild() {
758 return false
759 }
760
761 // Uncompress dex in APKs of privileged apps
762 if ctx.Config().UncompressPrivAppDex() && Bool(a.properties.Privileged) {
763 return true
764 }
765
766 return shouldUncompressDex(ctx, &a.dexpreopter)
767}
768
Jaewoong Jungea1bdb02019-05-09 14:36:34 -0700769func (a *AndroidAppImport) uncompressDex(
770 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
771 rule := android.NewRuleBuilder()
772 rule.Command().
773 Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
774 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
775 FlagWithInput("-i ", inputPath).
776 FlagWithOutput("-o ", outputPath).
777 FlagWithArg("-0 ", "'classes*.dex'").
778 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
779 rule.Build(pctx, ctx, "uncompress-dex", "Uncompress dex files")
780}
781
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700782func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
783 if String(a.properties.Certificate) == "" && !Bool(a.properties.Presigned) {
784 ctx.PropertyErrorf("certificate", "No certificate specified for prebuilt")
785 }
786 if String(a.properties.Certificate) != "" && Bool(a.properties.Presigned) {
787 ctx.PropertyErrorf("certificate", "Certificate can't be specified for presigned modules")
788 }
789
790 _, certificates := collectAppDeps(ctx)
791
792 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700793 // TODO: LOCAL_PACKAGE_SPLITS
794
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700795 srcApk := android.PathForModuleSrc(ctx, a.getSrcApkPath(ctx))
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700796
797 // TODO: Install or embed JNI libraries
798
799 // Uncompress JNI libraries in the apk
800 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
801 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
802
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700803 installDir := android.PathForModuleInstall(ctx, "app", a.BaseModuleName())
804 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
805 a.dexpreopter.isInstallable = true
806 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700807 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700808 dexOutput := a.dexpreopter.dexpreopt(ctx, jnisUncompressed)
Jaewoong Jungea1bdb02019-05-09 14:36:34 -0700809 if a.dexpreopter.uncompressedDex {
810 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
811 a.uncompressDex(ctx, dexOutput, dexUncompressed.OutputPath)
812 dexOutput = dexUncompressed
813 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700814
815 // Sign or align the package
816 // TODO: Handle EXTERNAL
817 if !Bool(a.properties.Presigned) {
818 certificates = processMainCert(a.ModuleBase, *a.properties.Certificate, certificates, ctx)
819 if len(certificates) != 1 {
820 ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates)
821 }
822 a.certificate = &certificates[0]
823 signed := android.PathForModuleOut(ctx, "signed", ctx.ModuleName()+".apk")
824 SignAppPackage(ctx, signed, dexOutput, certificates)
825 a.outputFile = signed
826 } else {
827 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", ctx.ModuleName()+".apk")
828 TransformZipAlign(ctx, alignedApk, dexOutput)
829 a.outputFile = alignedApk
830 }
831
832 // TODO: Optionally compress the output apk.
833
834 ctx.InstallFile(installDir, a.BaseModuleName()+".apk", a.outputFile)
835
836 // TODO: androidmk converter jni libs
837}
838
839func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
840 return &a.prebuilt
841}
842
843func (a *AndroidAppImport) Name() string {
844 return a.prebuilt.Name(a.ModuleBase.Name())
845}
846
847// android_app_import imports a prebuilt apk with additional processing specified in the module.
848func AndroidAppImportFactory() android.Module {
849 module := &AndroidAppImport{}
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700850 module.properties.Dpi_variants = reflect.New(dpiVariantsStruct).Interface()
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700851 module.AddProperties(&module.properties)
852 module.AddProperties(&module.dexpreoptProperties)
853
854 InitJavaModule(module, android.DeviceSupported)
855 android.InitSingleSourcePrebuiltModule(module, &module.properties.Apk)
856
857 return module
858}