blob: f7f08a88ddc40c2499a51e385850d4def881f98f [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
Jaewoong Jungbc625cd2019-05-06 15:48:44 -070088 // STL library to use for JNI libraries.
89 Stl *string `android:"arch_variant"`
90
Colin Crosse4246ab2019-02-05 21:55:21 -080091 // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest
92 // flag so that they are used from inside the APK at runtime. Defaults to true for android_test modules unless
93 // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to false for other
94 // module types where the native libraries are generally preinstalled outside the APK.
95 Use_embedded_native_libs *bool
Colin Cross46abdad2019-02-07 13:07:08 -080096
97 // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that
98 // they are used from inside the APK at runtime.
99 Use_embedded_dex *bool
Colin Cross47fa9d32019-03-26 10:51:39 -0700100
101 // Forces native libraries to always be packaged into the APK,
102 // Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed.
103 // True for android_test* modules.
104 AlwaysPackageNativeLibs bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700105}
106
Jaewoong Jung525443a2019-02-28 15:35:54 -0800107// android_app properties that can be overridden by override_android_app
108type overridableAppProperties struct {
109 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
110 // or an android_app_certificate module name in the form ":module".
111 Certificate *string
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700112
113 // the package name of this app. The package name in the manifest file is used if one was not given.
114 Package_name *string
Jaewoong Jung525443a2019-02-28 15:35:54 -0800115}
116
Colin Cross30e076a2015-04-13 13:58:27 -0700117type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -0700118 Library
119 aapt
Jaewoong Jung525443a2019-02-28 15:35:54 -0800120 android.OverridableModuleBase
Colin Crossa97c5d32018-03-28 14:58:31 -0700121
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900122 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -0700123
Colin Crossfabb6082018-02-20 17:22:23 -0800124 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -0700125
Jaewoong Jung525443a2019-02-28 15:35:54 -0800126 overridableAppProperties overridableAppProperties
127
Colin Crossa4f08812018-10-02 22:03:40 -0700128 installJniLibs []jniLib
Colin Crossf6237212018-10-29 23:14:58 -0700129
130 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800131
132 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
133 installApkName string
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800134
135 additionalAaptFlags []string
Colin Crosse1731a52017-12-14 11:22:55 -0800136}
137
Colin Cross89c31582018-04-30 15:55:11 -0700138func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
139 return nil
140}
141
Colin Cross66f78822018-05-02 12:58:28 -0700142func (a *AndroidApp) ExportedStaticPackages() android.Paths {
143 return nil
144}
145
Colin Crossa97c5d32018-03-28 14:58:31 -0700146var _ AndroidLibraryDependency = (*AndroidApp)(nil)
147
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900148type Certificate struct {
149 Pem, Key android.Path
Colin Cross30e076a2015-04-13 13:58:27 -0700150}
151
Colin Cross46c9b8b2017-06-22 16:51:17 -0700152func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
153 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700154
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700155 if String(a.appProperties.Stl) == "c++_shared" && a.sdkVersion() == "" {
156 ctx.PropertyErrorf("stl", "sdk_version must be set in order to use c++_shared")
157 }
158
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800159 if !Bool(a.properties.No_framework_libs) && !Bool(a.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700160 a.aapt.deps(ctx, sdkContext(a))
Colin Cross30e076a2015-04-13 13:58:27 -0700161 }
Colin Crossa4f08812018-10-02 22:03:40 -0700162
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700163 embedJni := a.shouldEmbedJnis(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700164 for _, jniTarget := range ctx.MultiTargets() {
165 variation := []blueprint.Variation{
166 {Mutator: "arch", Variation: jniTarget.String()},
167 {Mutator: "link", Variation: "shared"},
168 }
169 tag := &jniDependencyTag{
170 target: jniTarget,
171 }
172 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700173 if String(a.appProperties.Stl) == "c++_shared" {
174 if embedJni {
175 ctx.AddFarVariationDependencies(variation, tag, "libc++")
176 }
177 }
Colin Crossa4f08812018-10-02 22:03:40 -0700178 }
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700179}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700180
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700181func (a *AndroidApp) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800182 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700183 if cert != "" {
184 ctx.AddDependency(ctx.Module(), certificateTag, cert)
185 }
186
187 for _, cert := range a.appProperties.Additional_certificates {
188 cert = android.SrcIsModule(cert)
189 if cert != "" {
190 ctx.AddDependency(ctx.Module(), certificateTag, cert)
191 } else {
192 ctx.PropertyErrorf("additional_certificates",
193 `must be names of android_app_certificate modules in the form ":module"`)
194 }
195 }
Colin Cross30e076a2015-04-13 13:58:27 -0700196}
197
Colin Cross46c9b8b2017-06-22 16:51:17 -0700198func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Sasha Smundak6ad77252019-05-01 13:16:22 -0700199 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
Colin Cross46abdad2019-02-07 13:07:08 -0800200 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
Colin Crossae5caf52018-05-22 11:11:52 -0700201 a.generateAndroidBuildActions(ctx)
202}
203
Sasha Smundak6ad77252019-05-01 13:16:22 -0700204// Returns true if the native libraries should be stored in the APK uncompressed and the
Colin Crosse4246ab2019-02-05 21:55:21 -0800205// extractNativeLibs application flag should be set to false in the manifest.
Sasha Smundak6ad77252019-05-01 13:16:22 -0700206func (a *AndroidApp) useEmbeddedNativeLibs(ctx android.ModuleContext) bool {
Colin Crosse4246ab2019-02-05 21:55:21 -0800207 minSdkVersion, err := sdkVersionToNumber(ctx, a.minSdkVersion())
208 if err != nil {
209 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
210 }
211
212 return minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)
213}
214
Colin Cross43f08db2018-11-12 10:13:39 -0800215// Returns whether this module should have the dex file stored uncompressed in the APK.
216func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800217 if Bool(a.appProperties.Use_embedded_dex) {
218 return true
219 }
220
Colin Cross5a0dcd52018-10-05 14:20:06 -0700221 if ctx.Config().UnbundledBuild() {
Colin Cross43f08db2018-11-12 10:13:39 -0800222 return false
Colin Cross5a0dcd52018-10-05 14:20:06 -0700223 }
224
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700225 // Uncompress dex in APKs of privileged apps
226 if ctx.Config().UncompressPrivAppDex() && Bool(a.appProperties.Privileged) {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000227 return true
228 }
229
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700230 return shouldUncompressDex(ctx, &a.dexpreopter)
Colin Cross5a0dcd52018-10-05 14:20:06 -0700231}
232
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700233func (a *AndroidApp) shouldEmbedJnis(ctx android.BaseModuleContext) bool {
234 return ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) ||
235 a.appProperties.AlwaysPackageNativeLibs
236}
237
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800238func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
David Brazdild25060a2019-02-18 18:24:16 +0000239 a.aapt.usesNonSdkApis = Bool(a.Module.deviceProperties.Platform_apis)
240
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800241 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800242
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800243 // 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 -0700244 hasProduct := false
245 for _, f := range a.aaptProperties.Aaptflags {
246 if strings.HasPrefix(f, "--product") {
247 hasProduct = true
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800248 break
Colin Crosse78dcd32018-04-19 15:25:19 -0700249 }
250 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700251 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800252 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700253 }
254
Dan Willemsen72be5902018-10-24 20:24:57 -0700255 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
256 // Product AAPT config
257 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800258 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700259 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700260
Dan Willemsen72be5902018-10-24 20:24:57 -0700261 // Product AAPT preferred config
262 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800263 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700264 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700265 }
266
Jiyong Park7f67f482019-01-05 12:57:48 +0900267 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700268 if overridden || a.overridableAppProperties.Package_name != nil {
269 // The product override variable has a priority over the package_name property.
270 if !overridden {
271 manifestPackageName = *a.overridableAppProperties.Package_name
272 }
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800273 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
Jiyong Park7f67f482019-01-05 12:57:48 +0900274 }
275
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800276 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
277
Colin Crosse560c4a2019-03-19 16:03:11 -0700278 a.aapt.splitNames = a.appProperties.Package_splits
279
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800280 a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700281
Colin Cross46c9b8b2017-06-22 16:51:17 -0700282 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700283 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800284}
Colin Cross30e076a2015-04-13 13:58:27 -0700285
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800286func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700287 var staticLibProguardFlagFiles android.Paths
288 ctx.VisitDirectDeps(func(m android.Module) {
289 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
290 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
291 }
292 })
293
294 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
295
296 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
297 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800298}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800299
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800300func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross43f08db2018-11-12 10:13:39 -0800301
302 var installDir string
303 if ctx.ModuleName() == "framework-res" {
304 // framework-res.apk is installed as system/framework/framework-res.apk
305 installDir = "framework"
306 } else if Bool(a.appProperties.Privileged) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800307 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800308 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800309 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800310 }
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800311 a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000312 a.dexpreopter.isInstallable = Bool(a.properties.Installable)
313 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
314 a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex
Colin Cross5a0dcd52018-10-05 14:20:06 -0700315
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800316 if ctx.ModuleName() != "framework-res" {
317 a.Module.compile(ctx, a.aaptSrcJar)
318 }
Colin Cross30e076a2015-04-13 13:58:27 -0700319
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800320 return a.maybeStrippedDexJarFile
321}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800322
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800323func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700324 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700325 if len(jniLibs) > 0 {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700326 if a.shouldEmbedJnis(ctx) {
Colin Crossa4f08812018-10-02 22:03:40 -0700327 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Sasha Smundak6ad77252019-05-01 13:16:22 -0700328 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.useEmbeddedNativeLibs(ctx))
Colin Crossa4f08812018-10-02 22:03:40 -0700329 } else {
330 a.installJniLibs = jniLibs
331 }
332 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800333 return jniJarFile
334}
Colin Crossa4f08812018-10-02 22:03:40 -0700335
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700336// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
337// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
338func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate {
339 if android.SrcIsModule(certPropValue) == "" {
340 var mainCert Certificate
341 if certPropValue != "" {
342 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
343 mainCert = Certificate{
344 defaultDir.Join(ctx, certPropValue+".x509.pem"),
345 defaultDir.Join(ctx, certPropValue+".pk8"),
346 }
347 } else {
348 pem, key := ctx.Config().DefaultAppCertificate(ctx)
349 mainCert = Certificate{pem, key}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700350 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700351 certificates = append([]Certificate{mainCert}, certificates...)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700352 }
353
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700354 if !m.Platform() {
355 certPath := certificates[0].Pem.String()
Jeongik Chac9464142019-01-07 12:07:27 +0900356 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
357 if strings.HasPrefix(certPath, systemCertPath) {
358 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
359 whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
360
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700361 if enforceSystemCert && !inList(m.Name(), whitelist) {
Jeongik Chac9464142019-01-07 12:07:27 +0900362 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
363 }
364 }
365 }
366
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700367 return certificates
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800368}
369
370func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800371 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800372 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800373
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800374 // Process all building blocks, from AAPT to certificates.
375 a.aaptBuildActions(ctx)
376
377 a.proguardBuildActions(ctx)
378
379 dexJarFile := a.dexBuildActions(ctx)
380
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700381 jniLibs, certificateDeps := collectAppDeps(ctx)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800382 jniJarFile := a.jniBuildActions(jniLibs, ctx)
383
384 if ctx.Failed() {
385 return
386 }
387
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700388 certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx)
389 a.certificate = certificates[0]
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800390
391 // Build a final signed app package.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800392 // TODO(jungjw): Consider changing this to installApkName.
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800393 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700394 CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800395 a.outputFile = packageFile
396
Colin Crosse560c4a2019-03-19 16:03:11 -0700397 for _, split := range a.aapt.splits {
398 // Sign the split APKs
399 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"_"+split.suffix+".apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700400 CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates)
Colin Crosse560c4a2019-03-19 16:03:11 -0700401 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
402 }
403
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800404 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700405 bundleFile := android.PathForModuleOut(ctx, "base.zip")
406 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
407 a.bundleFile = bundleFile
408
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800409 // Install the app package.
Colin Crosse560c4a2019-03-19 16:03:11 -0700410 var installDir android.OutputPath
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800411 if ctx.ModuleName() == "framework-res" {
412 // framework-res.apk is installed as system/framework/framework-res.apk
Colin Crosse560c4a2019-03-19 16:03:11 -0700413 installDir = android.PathForModuleInstall(ctx, "framework")
Colin Cross16056062017-12-13 22:46:28 -0800414 } else if Bool(a.appProperties.Privileged) {
Colin Crosse560c4a2019-03-19 16:03:11 -0700415 installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800416 } else {
Colin Crosse560c4a2019-03-19 16:03:11 -0700417 installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
418 }
419
420 ctx.InstallFile(installDir, a.installApkName+".apk", a.outputFile)
421 for _, split := range a.aapt.splits {
422 ctx.InstallFile(installDir, a.installApkName+"_"+split.suffix+".apk", split.path)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800423 }
Colin Cross30e076a2015-04-13 13:58:27 -0700424}
425
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700426func collectAppDeps(ctx android.ModuleContext) ([]jniLib, []Certificate) {
Colin Crossa4f08812018-10-02 22:03:40 -0700427 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900428 var certificates []Certificate
Colin Crossa4f08812018-10-02 22:03:40 -0700429
430 ctx.VisitDirectDeps(func(module android.Module) {
431 otherName := ctx.OtherModuleName(module)
432 tag := ctx.OtherModuleDependencyTag(module)
433
434 if jniTag, ok := tag.(*jniDependencyTag); ok {
435 if dep, ok := module.(*cc.Module); ok {
436 lib := dep.OutputFile()
437 if lib.Valid() {
438 jniLibs = append(jniLibs, jniLib{
439 name: ctx.OtherModuleName(module),
440 path: lib.Path(),
441 target: jniTag.target,
442 })
443 } else {
444 ctx.ModuleErrorf("dependency %q missing output file", otherName)
445 }
446 } else {
447 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
Colin Crossa4f08812018-10-02 22:03:40 -0700448 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700449 } else if tag == certificateTag {
450 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900451 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700452 } else {
453 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
454 }
Colin Crossa4f08812018-10-02 22:03:40 -0700455 }
456 })
457
Colin Crossbd01e2a2018-10-04 15:21:03 -0700458 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700459}
460
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800461func (a *AndroidApp) getCertString(ctx android.BaseContext) string {
462 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
463 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000464 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800465 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800466 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800467}
468
Colin Cross1b16b0e2019-02-12 14:41:32 -0800469// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700470func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700471 module := &AndroidApp{}
472
Sasha Smundak2057f822019-04-16 17:16:58 -0700473 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross66dbc0b2017-12-28 12:23:20 -0800474 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
475
Colin Crossae5caf52018-05-22 11:11:52 -0700476 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700477 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700478
Colin Cross36242852017-06-23 15:06:31 -0700479 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700480 &module.Module.properties,
481 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800482 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700483 &module.Module.protoProperties,
484 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800485 &module.appProperties,
486 &module.overridableAppProperties)
Colin Cross36242852017-06-23 15:06:31 -0700487
Colin Crossa9d8bee2018-10-02 13:59:46 -0700488 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
489 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
490 })
491
Colin Crossa4f08812018-10-02 22:03:40 -0700492 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
493 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800494 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossa4f08812018-10-02 22:03:40 -0700495
Colin Cross36242852017-06-23 15:06:31 -0700496 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700497}
Colin Crossae5caf52018-05-22 11:11:52 -0700498
499type appTestProperties struct {
500 Instrumentation_for *string
501}
502
503type AndroidTest struct {
504 AndroidApp
505
506 appTestProperties appTestProperties
507
508 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700509
510 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700511 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700512}
513
514func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800515 // Check if the instrumentation target package is overridden before generating build actions.
516 if a.appTestProperties.Instrumentation_for != nil {
517 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
518 if overridden {
519 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
520 }
521 }
Sasha Smundak6ad77252019-05-01 13:16:22 -0700522 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
523 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
Colin Crossae5caf52018-05-22 11:11:52 -0700524 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700525
yangbill4f41bc22019-02-13 21:45:47 +0800526 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 -0800527 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700528}
529
530func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -0700531 a.AndroidApp.DepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -0700532 if a.appTestProperties.Instrumentation_for != nil {
533 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
534 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
535 // use instrumentationForTag instead of libTag.
536 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
537 }
Colin Crossae5caf52018-05-22 11:11:52 -0700538}
539
Colin Cross1b16b0e2019-02-12 14:41:32 -0800540// android_test compiles test sources and Android resources into an Android application package `.apk` file and
541// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -0700542func AndroidTestFactory() android.Module {
543 module := &AndroidTest{}
544
Sasha Smundak2057f822019-04-16 17:16:58 -0700545 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross5067db92018-09-17 16:46:35 -0700546
547 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700548 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800549 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700550 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800551 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -0700552
553 module.AddProperties(
554 &module.Module.properties,
555 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800556 &module.Module.dexpreoptProperties,
Colin Crossae5caf52018-05-22 11:11:52 -0700557 &module.Module.protoProperties,
558 &module.aaptProperties,
559 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700560 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800561 &module.overridableAppProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700562 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700563
Colin Crossa4f08812018-10-02 22:03:40 -0700564 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
565 android.InitDefaultableModule(module)
Colin Crossae5caf52018-05-22 11:11:52 -0700566 return module
567}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700568
Colin Cross252fc6f2018-10-04 15:22:03 -0700569type appTestHelperAppProperties struct {
570 // list of compatibility suites (for example "cts", "vts") that the module should be
571 // installed into.
572 Test_suites []string `android:"arch_variant"`
573}
574
575type AndroidTestHelperApp struct {
576 AndroidApp
577
578 appTestHelperAppProperties appTestHelperAppProperties
579}
580
Colin Cross1b16b0e2019-02-12 14:41:32 -0800581// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
582// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
583// test.
Colin Cross252fc6f2018-10-04 15:22:03 -0700584func AndroidTestHelperAppFactory() android.Module {
585 module := &AndroidTestHelperApp{}
586
Sasha Smundak2057f822019-04-16 17:16:58 -0700587 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700588
589 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800590 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700591 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800592 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700593
594 module.AddProperties(
595 &module.Module.properties,
596 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800597 &module.Module.dexpreoptProperties,
Colin Cross252fc6f2018-10-04 15:22:03 -0700598 &module.Module.protoProperties,
599 &module.aaptProperties,
600 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800601 &module.appTestHelperAppProperties,
602 &module.overridableAppProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -0700603
604 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
605 android.InitDefaultableModule(module)
606 return module
607}
608
Colin Crossbd01e2a2018-10-04 15:21:03 -0700609type AndroidAppCertificate struct {
610 android.ModuleBase
611 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900612 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -0700613}
614
615type AndroidAppCertificateProperties struct {
616 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
617 Certificate *string
618}
619
Colin Cross1b16b0e2019-02-12 14:41:32 -0800620// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
621// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -0700622func AndroidAppCertificateFactory() android.Module {
623 module := &AndroidAppCertificate{}
624 module.AddProperties(&module.properties)
625 android.InitAndroidModule(module)
626 return module
627}
628
Colin Crossbd01e2a2018-10-04 15:21:03 -0700629func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
630 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900631 c.Certificate = Certificate{
Colin Crossbd01e2a2018-10-04 15:21:03 -0700632 android.PathForModuleSrc(ctx, cert+".x509.pem"),
633 android.PathForModuleSrc(ctx, cert+".pk8"),
634 }
635}
Jaewoong Jung525443a2019-02-28 15:35:54 -0800636
637type OverrideAndroidApp struct {
638 android.ModuleBase
639 android.OverrideModuleBase
640}
641
642func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
643 // All the overrides happen in the base module.
644 // TODO(jungjw): Check the base module type.
645}
646
647// override_android_app is used to create an android_app module based on another android_app by overriding
648// some of its properties.
649func OverrideAndroidAppModuleFactory() android.Module {
650 m := &OverrideAndroidApp{}
651 m.AddProperties(&overridableAppProperties{})
652
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700653 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800654 android.InitOverrideModule(m)
655 return m
656}
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700657
658type AndroidAppImport struct {
659 android.ModuleBase
660 android.DefaultableModuleBase
661 prebuilt android.Prebuilt
662
663 properties AndroidAppImportProperties
664
665 outputFile android.Path
666 certificate *Certificate
667
668 dexpreopter
669}
670
671type AndroidAppImportProperties struct {
672 // A prebuilt apk to import
673 Apk string
674
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700675 // Per-DPI settings. This property makes it possible to specify a different source apk path for
676 // each DPI.
677 //
678 // Example:
679 //
680 // android_app_import {
681 // name: "example_import",
682 // apk: "prebuilts/example.apk",
683 // dpi_variants: {
684 // mdpi: {
685 // apk: "prebuilts/example_mdpi.apk",
686 // },
687 // xhdpi: {
688 // apk: "prebuilts/example_xhdpi.apk",
689 // },
690 // },
691 // certificate: "PRESIGNED",
692 // }
693 Dpi_variants interface{}
694
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700695 // The name of a certificate in the default certificate directory, blank to use the default
696 // product certificate, or an android_app_certificate module name in the form ":module".
697 Certificate *string
698
699 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
700 // be set for presigned modules.
701 Presigned *bool
702
703 // Specifies that this app should be installed to the priv-app directory,
704 // where the system will grant it additional privileges not available to
705 // normal apps.
706 Privileged *bool
707
708 // Names of modules to be overridden. Listed modules can only be other binaries
709 // (in Make or Soong).
710 // This does not completely prevent installation of the overridden binaries, but if both
711 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
712 // from PRODUCT_PACKAGES.
713 Overrides []string
714}
715
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700716func getApkPathForDpi(dpiVariantsValue reflect.Value, dpi string) string {
717 dpiField := dpiVariantsValue.FieldByName(proptools.FieldNameForProperty(dpi))
718 if !dpiField.IsValid() {
719 return ""
720 }
721 apkValue := dpiField.FieldByName("Apk").Elem()
722 if apkValue.IsValid() {
723 return apkValue.String()
724 }
725 return ""
726}
727
728// Chooses a source APK path to use based on the module's per-DPI settings and the product config.
729func (a *AndroidAppImport) getSrcApkPath(ctx android.ModuleContext) string {
730 config := ctx.Config()
731 dpiVariantsValue := reflect.ValueOf(a.properties.Dpi_variants).Elem()
732 if !dpiVariantsValue.IsValid() {
733 return a.properties.Apk
734 }
735 // Match PRODUCT_AAPT_PREF_CONFIG first and then PRODUCT_AAPT_PREBUILT_DPI.
736 if config.ProductAAPTPreferredConfig() != "" {
737 if apk := getApkPathForDpi(dpiVariantsValue, config.ProductAAPTPreferredConfig()); apk != "" {
738 return apk
739 }
740 }
741 for _, dpi := range config.ProductAAPTPrebuiltDPI() {
742 if apk := getApkPathForDpi(dpiVariantsValue, dpi); apk != "" {
743 return apk
744 }
745 }
746
747 // No match. Use the generic one.
748 return a.properties.Apk
749}
750
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700751func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
752 cert := android.SrcIsModule(String(a.properties.Certificate))
753 if cert != "" {
754 ctx.AddDependency(ctx.Module(), certificateTag, cert)
755 }
756}
757
758func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
759 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
760 rule := android.NewRuleBuilder()
761 rule.Command().
762 Textf(`if (zipinfo %s 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
763 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
764 FlagWithInput("-i ", inputPath).
765 FlagWithOutput("-o ", outputPath).
766 FlagWithArg("-0 ", "'lib/**/*.so'").
767 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
768 rule.Build(pctx, ctx, "uncompress-embedded-jni-libs", "Uncompress embedded JIN libs")
769}
770
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700771// Returns whether this module should have the dex file stored uncompressed in the APK.
772func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
773 if ctx.Config().UnbundledBuild() {
774 return false
775 }
776
777 // Uncompress dex in APKs of privileged apps
778 if ctx.Config().UncompressPrivAppDex() && Bool(a.properties.Privileged) {
779 return true
780 }
781
782 return shouldUncompressDex(ctx, &a.dexpreopter)
783}
784
Jaewoong Jungea1bdb02019-05-09 14:36:34 -0700785func (a *AndroidAppImport) uncompressDex(
786 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
787 rule := android.NewRuleBuilder()
788 rule.Command().
789 Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
790 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
791 FlagWithInput("-i ", inputPath).
792 FlagWithOutput("-o ", outputPath).
793 FlagWithArg("-0 ", "'classes*.dex'").
794 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
795 rule.Build(pctx, ctx, "uncompress-dex", "Uncompress dex files")
796}
797
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700798func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
799 if String(a.properties.Certificate) == "" && !Bool(a.properties.Presigned) {
800 ctx.PropertyErrorf("certificate", "No certificate specified for prebuilt")
801 }
802 if String(a.properties.Certificate) != "" && Bool(a.properties.Presigned) {
803 ctx.PropertyErrorf("certificate", "Certificate can't be specified for presigned modules")
804 }
805
806 _, certificates := collectAppDeps(ctx)
807
808 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700809 // TODO: LOCAL_PACKAGE_SPLITS
810
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700811 srcApk := android.PathForModuleSrc(ctx, a.getSrcApkPath(ctx))
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700812
813 // TODO: Install or embed JNI libraries
814
815 // Uncompress JNI libraries in the apk
816 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
817 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
818
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700819 installDir := android.PathForModuleInstall(ctx, "app", a.BaseModuleName())
820 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
821 a.dexpreopter.isInstallable = true
822 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700823 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700824 dexOutput := a.dexpreopter.dexpreopt(ctx, jnisUncompressed)
Jaewoong Jungea1bdb02019-05-09 14:36:34 -0700825 if a.dexpreopter.uncompressedDex {
826 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
827 a.uncompressDex(ctx, dexOutput, dexUncompressed.OutputPath)
828 dexOutput = dexUncompressed
829 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700830
831 // Sign or align the package
832 // TODO: Handle EXTERNAL
833 if !Bool(a.properties.Presigned) {
834 certificates = processMainCert(a.ModuleBase, *a.properties.Certificate, certificates, ctx)
835 if len(certificates) != 1 {
836 ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates)
837 }
838 a.certificate = &certificates[0]
839 signed := android.PathForModuleOut(ctx, "signed", ctx.ModuleName()+".apk")
840 SignAppPackage(ctx, signed, dexOutput, certificates)
841 a.outputFile = signed
842 } else {
843 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", ctx.ModuleName()+".apk")
844 TransformZipAlign(ctx, alignedApk, dexOutput)
845 a.outputFile = alignedApk
846 }
847
848 // TODO: Optionally compress the output apk.
849
850 ctx.InstallFile(installDir, a.BaseModuleName()+".apk", a.outputFile)
851
852 // TODO: androidmk converter jni libs
853}
854
855func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
856 return &a.prebuilt
857}
858
859func (a *AndroidAppImport) Name() string {
860 return a.prebuilt.Name(a.ModuleBase.Name())
861}
862
863// android_app_import imports a prebuilt apk with additional processing specified in the module.
864func AndroidAppImportFactory() android.Module {
865 module := &AndroidAppImport{}
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700866 module.properties.Dpi_variants = reflect.New(dpiVariantsStruct).Interface()
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700867 module.AddProperties(&module.properties)
868 module.AddProperties(&module.dexpreoptProperties)
869
870 InitJavaModule(module, android.DeviceSupported)
871 android.InitSingleSourcePrebuiltModule(module, &module.properties.Apk)
872
873 return module
874}