blob: 674e5ece0f1b9647aa9a7ef81c642ec7fab101fb [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 (
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070020 "path/filepath"
21 "reflect"
Jaewoong Jung5b425e22019-06-17 17:40:56 -070022 "sort"
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070023 "strings"
Colin Cross30e076a2015-04-13 13:58:27 -070024
Colin Cross50ddcc42019-05-16 12:28:22 -070025 "github.com/google/blueprint"
26 "github.com/google/blueprint/proptools"
27
Colin Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Colin Crossa4f08812018-10-02 22:03:40 -070029 "android/soong/cc"
Colin Cross303e21f2018-08-07 16:49:25 -070030 "android/soong/tradefed"
Colin Cross30e076a2015-04-13 13:58:27 -070031)
32
Jaewoong Jung3e18b192019-06-11 12:25:34 -070033var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070034
Colin Cross3bc7ffa2017-11-22 16:19:37 -080035func init() {
Colin Cross5ab4e6d2017-11-22 16:20:45 -080036 android.RegisterModuleType("android_app", AndroidAppFactory)
Colin Crossae5caf52018-05-22 11:11:52 -070037 android.RegisterModuleType("android_test", AndroidTestFactory)
Colin Cross252fc6f2018-10-04 15:22:03 -070038 android.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
Colin Crossbd01e2a2018-10-04 15:21:03 -070039 android.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
Jaewoong Jung525443a2019-02-28 15:35:54 -080040 android.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
Jaewoong Jungccbb3932019-04-15 09:48:31 -070041 android.RegisterModuleType("android_app_import", AndroidAppImportFactory)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080042}
43
Colin Cross30e076a2015-04-13 13:58:27 -070044// AndroidManifest.xml merging
45// package splits
46
Colin Crossfabb6082018-02-20 17:22:23 -080047type appProperties struct {
Colin Crossbd01e2a2018-10-04 15:21:03 -070048 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
Colin Cross7d5136f2015-05-11 13:39:40 -070049 Additional_certificates []string
50
51 // If set, create package-export.apk, which other packages can
52 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
Nan Zhangea568a42017-11-08 21:20:04 -080053 Export_package_resources *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070054
Colin Cross16056062017-12-13 22:46:28 -080055 // Specifies that this app should be installed to the priv-app directory,
56 // where the system will grant it additional privileges not available to
57 // normal apps.
58 Privileged *bool
Colin Crossa97c5d32018-03-28 14:58:31 -070059
60 // list of resource labels to generate individual resource packages
61 Package_splits []string
Jason Monkd4122be2018-08-10 09:33:36 -040062
63 // Names of modules to be overridden. Listed modules can only be other binaries
64 // (in Make or Soong).
65 // This does not completely prevent installation of the overridden binaries, but if both
66 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
67 // from PRODUCT_PACKAGES.
68 Overrides []string
Colin Crossa4f08812018-10-02 22:03:40 -070069
70 // list of native libraries that will be provided in or alongside the resulting jar
71 Jni_libs []string `android:"arch_variant"`
72
Jaewoong Jungbc625cd2019-05-06 15:48:44 -070073 // STL library to use for JNI libraries.
74 Stl *string `android:"arch_variant"`
75
Colin Crosse4246ab2019-02-05 21:55:21 -080076 // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest
77 // flag so that they are used from inside the APK at runtime. Defaults to true for android_test modules unless
78 // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to false for other
79 // module types where the native libraries are generally preinstalled outside the APK.
80 Use_embedded_native_libs *bool
Colin Cross46abdad2019-02-07 13:07:08 -080081
82 // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that
83 // they are used from inside the APK at runtime.
84 Use_embedded_dex *bool
Colin Cross47fa9d32019-03-26 10:51:39 -070085
86 // Forces native libraries to always be packaged into the APK,
87 // Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed.
88 // True for android_test* modules.
89 AlwaysPackageNativeLibs bool `blueprint:"mutated"`
Jaewoong Jung5b425e22019-06-17 17:40:56 -070090
91 // If set, find and merge all NOTICE files that this module and its dependencies have and store
92 // it in the APK as an asset.
93 Embed_notices *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070094}
95
Jaewoong Jung525443a2019-02-28 15:35:54 -080096// android_app properties that can be overridden by override_android_app
97type overridableAppProperties struct {
98 // The name of a certificate in the default certificate directory, blank to use the default product certificate,
99 // or an android_app_certificate module name in the form ":module".
100 Certificate *string
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700101
102 // the package name of this app. The package name in the manifest file is used if one was not given.
103 Package_name *string
Jaewoong Jung525443a2019-02-28 15:35:54 -0800104}
105
Colin Cross30e076a2015-04-13 13:58:27 -0700106type AndroidApp struct {
Colin Crossa97c5d32018-03-28 14:58:31 -0700107 Library
108 aapt
Jaewoong Jung525443a2019-02-28 15:35:54 -0800109 android.OverridableModuleBase
Colin Crossa97c5d32018-03-28 14:58:31 -0700110
Colin Cross50ddcc42019-05-16 12:28:22 -0700111 usesLibrary usesLibrary
112
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900113 certificate Certificate
Colin Cross30e076a2015-04-13 13:58:27 -0700114
Colin Crossfabb6082018-02-20 17:22:23 -0800115 appProperties appProperties
Colin Crossae5caf52018-05-22 11:11:52 -0700116
Jaewoong Jung525443a2019-02-28 15:35:54 -0800117 overridableAppProperties overridableAppProperties
118
Colin Crossa4f08812018-10-02 22:03:40 -0700119 installJniLibs []jniLib
Colin Crossf6237212018-10-29 23:14:58 -0700120
121 bundleFile android.Path
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800122
123 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
124 installApkName string
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800125
126 additionalAaptFlags []string
Jaewoong Jung98772792019-07-01 17:15:13 -0700127
128 noticeOutputs android.NoticeOutputs
Colin Crosse1731a52017-12-14 11:22:55 -0800129}
130
Colin Cross89c31582018-04-30 15:55:11 -0700131func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
132 return nil
133}
134
Colin Cross66f78822018-05-02 12:58:28 -0700135func (a *AndroidApp) ExportedStaticPackages() android.Paths {
136 return nil
137}
138
Colin Crossa97c5d32018-03-28 14:58:31 -0700139var _ AndroidLibraryDependency = (*AndroidApp)(nil)
140
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900141type Certificate struct {
142 Pem, Key android.Path
Colin Cross30e076a2015-04-13 13:58:27 -0700143}
144
Colin Cross46c9b8b2017-06-22 16:51:17 -0700145func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
146 a.Module.deps(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700147
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700148 if String(a.appProperties.Stl) == "c++_shared" && a.sdkVersion() == "" {
149 ctx.PropertyErrorf("stl", "sdk_version must be set in order to use c++_shared")
150 }
151
Paul Duffin250e6192019-06-07 10:44:37 +0100152 sdkDep := decodeSdkDep(ctx, sdkContext(a))
153 if sdkDep.hasFrameworkLibs() {
154 a.aapt.deps(ctx, sdkDep)
Colin Cross30e076a2015-04-13 13:58:27 -0700155 }
Colin Crossa4f08812018-10-02 22:03:40 -0700156
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700157 embedJni := a.shouldEmbedJnis(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700158 for _, jniTarget := range ctx.MultiTargets() {
159 variation := []blueprint.Variation{
160 {Mutator: "arch", Variation: jniTarget.String()},
161 {Mutator: "link", Variation: "shared"},
162 }
163 tag := &jniDependencyTag{
164 target: jniTarget,
165 }
166 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...)
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700167 if String(a.appProperties.Stl) == "c++_shared" {
168 if embedJni {
Jaewoong Jung710756a2019-06-04 11:53:47 -0700169 ctx.AddFarVariationDependencies(variation, tag, "ndk_libc++_shared")
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700170 }
171 }
Colin Crossa4f08812018-10-02 22:03:40 -0700172 }
Colin Cross50ddcc42019-05-16 12:28:22 -0700173
Paul Duffin250e6192019-06-07 10:44:37 +0100174 a.usesLibrary.deps(ctx, sdkDep.hasFrameworkLibs())
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700175}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700176
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700177func (a *AndroidApp) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800178 cert := android.SrcIsModule(a.getCertString(ctx))
Colin Crossbd01e2a2018-10-04 15:21:03 -0700179 if cert != "" {
180 ctx.AddDependency(ctx.Module(), certificateTag, cert)
181 }
182
183 for _, cert := range a.appProperties.Additional_certificates {
184 cert = android.SrcIsModule(cert)
185 if cert != "" {
186 ctx.AddDependency(ctx.Module(), certificateTag, cert)
187 } else {
188 ctx.PropertyErrorf("additional_certificates",
189 `must be names of android_app_certificate modules in the form ":module"`)
190 }
191 }
Colin Cross30e076a2015-04-13 13:58:27 -0700192}
193
Jeongik Cha538c0d02019-07-11 15:54:27 +0900194func (a *AndroidTestHelperApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
195 a.generateAndroidBuildActions(ctx)
196}
197
Colin Cross46c9b8b2017-06-22 16:51:17 -0700198func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900199 a.checkPlatformAPI(ctx)
Colin Crossae5caf52018-05-22 11:11:52 -0700200 a.generateAndroidBuildActions(ctx)
201}
202
Sasha Smundak6ad77252019-05-01 13:16:22 -0700203// Returns true if the native libraries should be stored in the APK uncompressed and the
Colin Crosse4246ab2019-02-05 21:55:21 -0800204// extractNativeLibs application flag should be set to false in the manifest.
Sasha Smundak6ad77252019-05-01 13:16:22 -0700205func (a *AndroidApp) useEmbeddedNativeLibs(ctx android.ModuleContext) bool {
Colin Crosse4246ab2019-02-05 21:55:21 -0800206 minSdkVersion, err := sdkVersionToNumber(ctx, a.minSdkVersion())
207 if err != nil {
208 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
209 }
210
211 return minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)
212}
213
Colin Cross43f08db2018-11-12 10:13:39 -0800214// Returns whether this module should have the dex file stored uncompressed in the APK.
215func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
Colin Cross46abdad2019-02-07 13:07:08 -0800216 if Bool(a.appProperties.Use_embedded_dex) {
217 return true
218 }
219
Colin Cross53a87f52019-06-25 13:35:30 -0700220 // Uncompress dex in APKs of privileged apps (even for unbundled builds, they may
221 // be preinstalled as prebuilts).
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700222 if ctx.Config().UncompressPrivAppDex() && Bool(a.appProperties.Privileged) {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000223 return true
224 }
225
Colin Cross53a87f52019-06-25 13:35:30 -0700226 if ctx.Config().UnbundledBuild() {
227 return false
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 Jungc27ab662019-05-30 15:51:14 -0700241 // Ask manifest_fixer to add or update the application element indicating this app has no code.
242 a.aapt.hasNoCode = !a.hasCode(ctx)
243
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800244 aaptLinkFlags := []string{}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800245
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800246 // 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 -0700247 hasProduct := false
248 for _, f := range a.aaptProperties.Aaptflags {
249 if strings.HasPrefix(f, "--product") {
250 hasProduct = true
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800251 break
Colin Crosse78dcd32018-04-19 15:25:19 -0700252 }
253 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700254 if !hasProduct && len(ctx.Config().ProductAAPTCharacteristics()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800255 aaptLinkFlags = append(aaptLinkFlags, "--product", ctx.Config().ProductAAPTCharacteristics())
Colin Crosse78dcd32018-04-19 15:25:19 -0700256 }
257
Dan Willemsen72be5902018-10-24 20:24:57 -0700258 if !Bool(a.aaptProperties.Aapt_include_all_resources) {
259 // Product AAPT config
260 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800261 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig)
Dan Willemsen72be5902018-10-24 20:24:57 -0700262 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700263
Dan Willemsen72be5902018-10-24 20:24:57 -0700264 // Product AAPT preferred config
265 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 {
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800266 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig())
Dan Willemsen72be5902018-10-24 20:24:57 -0700267 }
Colin Crosse78dcd32018-04-19 15:25:19 -0700268 }
269
Jiyong Park7f67f482019-01-05 12:57:48 +0900270 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700271 if overridden || a.overridableAppProperties.Package_name != nil {
272 // The product override variable has a priority over the package_name property.
273 if !overridden {
274 manifestPackageName = *a.overridableAppProperties.Package_name
275 }
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800276 aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
Jiyong Park7f67f482019-01-05 12:57:48 +0900277 }
278
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800279 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
280
Colin Crosse560c4a2019-03-19 16:03:11 -0700281 a.aapt.splitNames = a.appProperties.Package_splits
Colin Cross50ddcc42019-05-16 12:28:22 -0700282 a.aapt.sdkLibraries = a.exportedSdkLibs
Colin Crosse560c4a2019-03-19 16:03:11 -0700283
Jaewoong Jungde4c02f2019-01-22 11:19:56 -0800284 a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
Colin Cross30e076a2015-04-13 13:58:27 -0700285
Colin Cross46c9b8b2017-06-22 16:51:17 -0700286 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700287 a.properties.Manifest = nil
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800288}
Colin Cross30e076a2015-04-13 13:58:27 -0700289
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800290func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) {
Colin Cross89c31582018-04-30 15:55:11 -0700291 var staticLibProguardFlagFiles android.Paths
292 ctx.VisitDirectDeps(func(m android.Module) {
293 if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
294 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
295 }
296 })
297
298 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles)
299
300 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
301 a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800302}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800303
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800304func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
Colin Cross43f08db2018-11-12 10:13:39 -0800305
306 var installDir string
307 if ctx.ModuleName() == "framework-res" {
308 // framework-res.apk is installed as system/framework/framework-res.apk
309 installDir = "framework"
310 } else if Bool(a.appProperties.Privileged) {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800311 installDir = filepath.Join("priv-app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800312 } else {
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800313 installDir = filepath.Join("app", a.installApkName)
Colin Cross43f08db2018-11-12 10:13:39 -0800314 }
Colin Cross50ddcc42019-05-16 12:28:22 -0700315
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800316 a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000317 a.dexpreopter.isInstallable = Bool(a.properties.Installable)
318 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -0700319
320 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
321 a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
322 a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
323 a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
324 a.dexpreopter.manifestFile = a.mergedManifestFile
325
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000326 a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex
Colin Cross5a0dcd52018-10-05 14:20:06 -0700327
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800328 if ctx.ModuleName() != "framework-res" {
329 a.Module.compile(ctx, a.aaptSrcJar)
330 }
Colin Cross30e076a2015-04-13 13:58:27 -0700331
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800332 return a.maybeStrippedDexJarFile
333}
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800334
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800335func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext) android.WritablePath {
Colin Crossa4f08812018-10-02 22:03:40 -0700336 var jniJarFile android.WritablePath
Colin Crossa4f08812018-10-02 22:03:40 -0700337 if len(jniLibs) > 0 {
Jaewoong Jungbc625cd2019-05-06 15:48:44 -0700338 if a.shouldEmbedJnis(ctx) {
Colin Crossa4f08812018-10-02 22:03:40 -0700339 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip")
Sasha Smundak6ad77252019-05-01 13:16:22 -0700340 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, a.useEmbeddedNativeLibs(ctx))
Colin Crossa4f08812018-10-02 22:03:40 -0700341 } else {
342 a.installJniLibs = jniLibs
343 }
344 }
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800345 return jniJarFile
346}
Colin Crossa4f08812018-10-02 22:03:40 -0700347
Jaewoong Jung98772792019-07-01 17:15:13 -0700348func (a *AndroidApp) noticeBuildActions(ctx android.ModuleContext, installDir android.OutputPath) {
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700349 // Collect NOTICE files from all dependencies.
350 seenModules := make(map[android.Module]bool)
351 noticePathSet := make(map[android.Path]bool)
352
353 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
354 // Have we already seen this?
355 if _, ok := seenModules[child]; ok {
356 return false
357 }
358 seenModules[child] = true
359
360 // Skip host modules.
361 if child.Target().Os.Class == android.Host || child.Target().Os.Class == android.HostCross {
362 return false
363 }
364
365 path := child.(android.Module).NoticeFile()
366 if path.Valid() {
367 noticePathSet[path.Path()] = true
368 }
369 return true
370 })
371
372 // If the app has one, add it too.
373 if a.NoticeFile().Valid() {
374 noticePathSet[a.NoticeFile().Path()] = true
375 }
376
377 if len(noticePathSet) == 0 {
Jaewoong Jung98772792019-07-01 17:15:13 -0700378 return
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700379 }
380 var noticePaths []android.Path
381 for path := range noticePathSet {
382 noticePaths = append(noticePaths, path)
383 }
384 sort.Slice(noticePaths, func(i, j int) bool {
385 return noticePaths[i].String() < noticePaths[j].String()
386 })
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700387
Jaewoong Jung98772792019-07-01 17:15:13 -0700388 a.noticeOutputs = android.BuildNoticeOutput(ctx, installDir, a.installApkName+".apk", noticePaths)
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700389}
390
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700391// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
392// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
393func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate {
394 if android.SrcIsModule(certPropValue) == "" {
395 var mainCert Certificate
396 if certPropValue != "" {
397 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
398 mainCert = Certificate{
399 defaultDir.Join(ctx, certPropValue+".x509.pem"),
400 defaultDir.Join(ctx, certPropValue+".pk8"),
401 }
402 } else {
403 pem, key := ctx.Config().DefaultAppCertificate(ctx)
404 mainCert = Certificate{pem, key}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700405 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700406 certificates = append([]Certificate{mainCert}, certificates...)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700407 }
408
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700409 if !m.Platform() {
410 certPath := certificates[0].Pem.String()
Jeongik Chac9464142019-01-07 12:07:27 +0900411 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
412 if strings.HasPrefix(certPath, systemCertPath) {
413 enforceSystemCert := ctx.Config().EnforceSystemCertificate()
414 whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
415
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700416 if enforceSystemCert && !inList(m.Name(), whitelist) {
Jeongik Chac9464142019-01-07 12:07:27 +0900417 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
418 }
419 }
420 }
421
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700422 return certificates
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800423}
424
425func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross50ddcc42019-05-16 12:28:22 -0700426 var apkDeps android.Paths
427
Jeongik Cha538c0d02019-07-11 15:54:27 +0900428 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx)
429 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex)
430
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800431 // Check if the install APK name needs to be overridden.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800432 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800433
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700434 var installDir android.OutputPath
435 if ctx.ModuleName() == "framework-res" {
436 // framework-res.apk is installed as system/framework/framework-res.apk
437 installDir = android.PathForModuleInstall(ctx, "framework")
438 } else if Bool(a.appProperties.Privileged) {
439 installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
440 } else {
441 installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
442 }
443
Jaewoong Jung98772792019-07-01 17:15:13 -0700444 a.noticeBuildActions(ctx, installDir)
445 if Bool(a.appProperties.Embed_notices) || ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") {
446 a.aapt.noticeFile = a.noticeOutputs.HtmlGzOutput
447 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700448
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800449 // Process all building blocks, from AAPT to certificates.
450 a.aaptBuildActions(ctx)
451
Colin Cross50ddcc42019-05-16 12:28:22 -0700452 if a.usesLibrary.enforceUsesLibraries() {
453 manifestCheckFile := a.usesLibrary.verifyUsesLibrariesManifest(ctx, a.mergedManifestFile)
454 apkDeps = append(apkDeps, manifestCheckFile)
455 }
456
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800457 a.proguardBuildActions(ctx)
458
459 dexJarFile := a.dexBuildActions(ctx)
460
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700461 jniLibs, certificateDeps := collectAppDeps(ctx)
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800462 jniJarFile := a.jniBuildActions(jniLibs, ctx)
463
464 if ctx.Failed() {
465 return
466 }
467
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700468 certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx)
469 a.certificate = certificates[0]
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800470
471 // Build a final signed app package.
Jaewoong Jung525443a2019-02-28 15:35:54 -0800472 // TODO(jungjw): Consider changing this to installApkName.
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800473 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".apk")
Colin Cross50ddcc42019-05-16 12:28:22 -0700474 CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates, apkDeps)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800475 a.outputFile = packageFile
476
Colin Crosse560c4a2019-03-19 16:03:11 -0700477 for _, split := range a.aapt.splits {
478 // Sign the split APKs
479 packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"_"+split.suffix+".apk")
Colin Cross50ddcc42019-05-16 12:28:22 -0700480 CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates, apkDeps)
Colin Crosse560c4a2019-03-19 16:03:11 -0700481 a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
482 }
483
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800484 // Build an app bundle.
Colin Crossf6237212018-10-29 23:14:58 -0700485 bundleFile := android.PathForModuleOut(ctx, "base.zip")
486 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile)
487 a.bundleFile = bundleFile
488
Jaewoong Jung590b1ae2019-01-22 16:40:58 -0800489 // Install the app package.
Colin Crosse560c4a2019-03-19 16:03:11 -0700490 ctx.InstallFile(installDir, a.installApkName+".apk", a.outputFile)
491 for _, split := range a.aapt.splits {
492 ctx.InstallFile(installDir, a.installApkName+"_"+split.suffix+".apk", split.path)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800493 }
Colin Cross30e076a2015-04-13 13:58:27 -0700494}
495
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700496func collectAppDeps(ctx android.ModuleContext) ([]jniLib, []Certificate) {
Colin Crossa4f08812018-10-02 22:03:40 -0700497 var jniLibs []jniLib
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900498 var certificates []Certificate
Colin Crossa4f08812018-10-02 22:03:40 -0700499
500 ctx.VisitDirectDeps(func(module android.Module) {
501 otherName := ctx.OtherModuleName(module)
502 tag := ctx.OtherModuleDependencyTag(module)
503
504 if jniTag, ok := tag.(*jniDependencyTag); ok {
505 if dep, ok := module.(*cc.Module); ok {
506 lib := dep.OutputFile()
507 if lib.Valid() {
508 jniLibs = append(jniLibs, jniLib{
509 name: ctx.OtherModuleName(module),
510 path: lib.Path(),
511 target: jniTag.target,
512 })
513 } else {
514 ctx.ModuleErrorf("dependency %q missing output file", otherName)
515 }
516 } else {
517 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
Colin Crossa4f08812018-10-02 22:03:40 -0700518 }
Colin Crossbd01e2a2018-10-04 15:21:03 -0700519 } else if tag == certificateTag {
520 if dep, ok := module.(*AndroidAppCertificate); ok {
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900521 certificates = append(certificates, dep.Certificate)
Colin Crossbd01e2a2018-10-04 15:21:03 -0700522 } else {
523 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName)
524 }
Colin Crossa4f08812018-10-02 22:03:40 -0700525 }
526 })
527
Colin Crossbd01e2a2018-10-04 15:21:03 -0700528 return jniLibs, certificates
Colin Crossa4f08812018-10-02 22:03:40 -0700529}
530
Colin Cross0ea8ba82019-06-06 14:33:29 -0700531func (a *AndroidApp) getCertString(ctx android.BaseModuleContext) string {
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800532 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
533 if overridden {
Jaewoong Jungacb6db32019-02-28 16:22:30 +0000534 return ":" + certificate
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800535 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800536 return String(a.overridableAppProperties.Certificate)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800537}
538
Colin Cross1b16b0e2019-02-12 14:41:32 -0800539// android_app compiles sources and Android resources into an Android application package `.apk` file.
Colin Cross36242852017-06-23 15:06:31 -0700540func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700541 module := &AndroidApp{}
542
Sasha Smundak2057f822019-04-16 17:16:58 -0700543 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross66dbc0b2017-12-28 12:23:20 -0800544 module.Module.deviceProperties.Optimize.Shrink = proptools.BoolPtr(true)
545
Colin Crossae5caf52018-05-22 11:11:52 -0700546 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700547 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crossae5caf52018-05-22 11:11:52 -0700548
Colin Cross36242852017-06-23 15:06:31 -0700549 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700550 &module.Module.properties,
551 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800552 &module.Module.dexpreoptProperties,
Colin Crossa97c5d32018-03-28 14:58:31 -0700553 &module.Module.protoProperties,
554 &module.aaptProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800555 &module.appProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700556 &module.overridableAppProperties,
557 &module.usesLibrary.usesLibraryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700558
Colin Crossa9d8bee2018-10-02 13:59:46 -0700559 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
560 return class == android.Device && ctx.Config().DevicePrefer32BitApps()
561 })
562
Colin Crossa4f08812018-10-02 22:03:40 -0700563 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
564 android.InitDefaultableModule(module)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800565 android.InitOverridableModule(module, &module.appProperties.Overrides)
Colin Crossa4f08812018-10-02 22:03:40 -0700566
Colin Cross36242852017-06-23 15:06:31 -0700567 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700568}
Colin Crossae5caf52018-05-22 11:11:52 -0700569
570type appTestProperties struct {
571 Instrumentation_for *string
572}
573
574type AndroidTest struct {
575 AndroidApp
576
577 appTestProperties appTestProperties
578
579 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700580
581 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -0700582 data android.Paths
Colin Crossae5caf52018-05-22 11:11:52 -0700583}
584
585func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800586 // Check if the instrumentation target package is overridden before generating build actions.
587 if a.appTestProperties.Instrumentation_for != nil {
588 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
589 if overridden {
590 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
591 }
592 }
Colin Crossae5caf52018-05-22 11:11:52 -0700593 a.generateAndroidBuildActions(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -0700594
yangbill4f41bc22019-02-13 21:45:47 +0800595 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 -0800596 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700597}
598
599func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross303e21f2018-08-07 16:49:25 -0700600 a.AndroidApp.DepsMutator(ctx)
Colin Cross4b964c02018-10-15 16:18:06 -0700601 if a.appTestProperties.Instrumentation_for != nil {
602 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac,
603 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so
604 // use instrumentationForTag instead of libTag.
605 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for))
606 }
Colin Crossae5caf52018-05-22 11:11:52 -0700607}
608
Colin Cross1b16b0e2019-02-12 14:41:32 -0800609// android_test compiles test sources and Android resources into an Android application package `.apk` file and
610// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
Colin Crossae5caf52018-05-22 11:11:52 -0700611func AndroidTestFactory() android.Module {
612 module := &AndroidTest{}
613
Sasha Smundak2057f822019-04-16 17:16:58 -0700614 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross5067db92018-09-17 16:46:35 -0700615
616 module.Module.properties.Instrument = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700617 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800618 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700619 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800620 module.Module.dexpreopter.isTest = true
Colin Crossae5caf52018-05-22 11:11:52 -0700621
622 module.AddProperties(
623 &module.Module.properties,
624 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800625 &module.Module.dexpreoptProperties,
Colin Crossae5caf52018-05-22 11:11:52 -0700626 &module.Module.protoProperties,
627 &module.aaptProperties,
628 &module.appProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700629 &module.appTestProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800630 &module.overridableAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700631 &module.usesLibrary.usesLibraryProperties,
Dan Willemsenf5531d22018-07-16 17:21:19 -0700632 &module.testProperties)
Colin Crossae5caf52018-05-22 11:11:52 -0700633
Colin Crossa4f08812018-10-02 22:03:40 -0700634 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
635 android.InitDefaultableModule(module)
Colin Crossae5caf52018-05-22 11:11:52 -0700636 return module
637}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700638
Colin Cross252fc6f2018-10-04 15:22:03 -0700639type appTestHelperAppProperties struct {
640 // list of compatibility suites (for example "cts", "vts") that the module should be
641 // installed into.
642 Test_suites []string `android:"arch_variant"`
643}
644
645type AndroidTestHelperApp struct {
646 AndroidApp
647
648 appTestHelperAppProperties appTestHelperAppProperties
649}
650
Colin Cross1b16b0e2019-02-12 14:41:32 -0800651// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that
652// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a
653// test.
Colin Cross252fc6f2018-10-04 15:22:03 -0700654func AndroidTestHelperAppFactory() android.Module {
655 module := &AndroidTestHelperApp{}
656
Sasha Smundak2057f822019-04-16 17:16:58 -0700657 module.Module.deviceProperties.Optimize.EnabledByDefault = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700658
659 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse4246ab2019-02-05 21:55:21 -0800660 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true)
Colin Cross47fa9d32019-03-26 10:51:39 -0700661 module.appProperties.AlwaysPackageNativeLibs = true
Colin Cross43f08db2018-11-12 10:13:39 -0800662 module.Module.dexpreopter.isTest = true
Colin Cross252fc6f2018-10-04 15:22:03 -0700663
664 module.AddProperties(
665 &module.Module.properties,
666 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -0800667 &module.Module.dexpreoptProperties,
Colin Cross252fc6f2018-10-04 15:22:03 -0700668 &module.Module.protoProperties,
669 &module.aaptProperties,
670 &module.appProperties,
Jaewoong Jung525443a2019-02-28 15:35:54 -0800671 &module.appTestHelperAppProperties,
Colin Cross50ddcc42019-05-16 12:28:22 -0700672 &module.overridableAppProperties,
673 &module.usesLibrary.usesLibraryProperties)
Colin Cross252fc6f2018-10-04 15:22:03 -0700674
675 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
676 android.InitDefaultableModule(module)
677 return module
678}
679
Colin Crossbd01e2a2018-10-04 15:21:03 -0700680type AndroidAppCertificate struct {
681 android.ModuleBase
682 properties AndroidAppCertificateProperties
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900683 Certificate Certificate
Colin Crossbd01e2a2018-10-04 15:21:03 -0700684}
685
686type AndroidAppCertificateProperties struct {
687 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name.
688 Certificate *string
689}
690
Colin Cross1b16b0e2019-02-12 14:41:32 -0800691// android_app_certificate modules can be referenced by the certificates property of android_app modules to select
692// the signing key.
Colin Crossbd01e2a2018-10-04 15:21:03 -0700693func AndroidAppCertificateFactory() android.Module {
694 module := &AndroidAppCertificate{}
695 module.AddProperties(&module.properties)
696 android.InitAndroidModule(module)
697 return module
698}
699
Colin Crossbd01e2a2018-10-04 15:21:03 -0700700func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {
701 cert := String(c.properties.Certificate)
Jiyong Parkc00cbd92018-10-30 21:20:05 +0900702 c.Certificate = Certificate{
Colin Crossbd01e2a2018-10-04 15:21:03 -0700703 android.PathForModuleSrc(ctx, cert+".x509.pem"),
704 android.PathForModuleSrc(ctx, cert+".pk8"),
705 }
706}
Jaewoong Jung525443a2019-02-28 15:35:54 -0800707
708type OverrideAndroidApp struct {
709 android.ModuleBase
710 android.OverrideModuleBase
711}
712
713func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
714 // All the overrides happen in the base module.
715 // TODO(jungjw): Check the base module type.
716}
717
718// override_android_app is used to create an android_app module based on another android_app by overriding
719// some of its properties.
720func OverrideAndroidAppModuleFactory() android.Module {
721 m := &OverrideAndroidApp{}
722 m.AddProperties(&overridableAppProperties{})
723
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700724 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800725 android.InitOverrideModule(m)
726 return m
727}
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700728
729type AndroidAppImport struct {
730 android.ModuleBase
731 android.DefaultableModuleBase
732 prebuilt android.Prebuilt
733
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700734 properties AndroidAppImportProperties
735 dpiVariants interface{}
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700736
737 outputFile android.Path
738 certificate *Certificate
739
740 dexpreopter
Colin Cross50ddcc42019-05-16 12:28:22 -0700741
742 usesLibrary usesLibrary
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700743}
744
745type AndroidAppImportProperties struct {
746 // A prebuilt apk to import
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700747 Apk *string
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700748
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700749 // The name of a certificate in the default certificate directory, blank to use the default
750 // product certificate, or an android_app_certificate module name in the form ":module".
751 Certificate *string
752
753 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
754 // be set for presigned modules.
755 Presigned *bool
756
757 // Specifies that this app should be installed to the priv-app directory,
758 // where the system will grant it additional privileges not available to
759 // normal apps.
760 Privileged *bool
761
762 // Names of modules to be overridden. Listed modules can only be other binaries
763 // (in Make or Soong).
764 // This does not completely prevent installation of the overridden binaries, but if both
765 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
766 // from PRODUCT_PACKAGES.
767 Overrides []string
768}
769
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700770// Chooses a source APK path to use based on the module and product specs.
771func (a *AndroidAppImport) updateSrcApkPath(ctx android.LoadHookContext) {
772 config := ctx.Config()
773
774 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants")
775 // Try DPI variant matches in the reverse-priority order so that the highest priority match
776 // overwrites everything else.
777 // TODO(jungjw): Can we optimize this by making it priority order?
778 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
779 dpi := config.ProductAAPTPrebuiltDPI()[i]
780 if inList(dpi, supportedDpis) {
781 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, dpi, "dpi_variants")
782 }
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700783 }
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700784 if config.ProductAAPTPreferredConfig() != "" {
785 dpi := config.ProductAAPTPreferredConfig()
786 if inList(dpi, supportedDpis) {
787 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, dpi, "dpi_variants")
788 }
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700789 }
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700790}
791
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700792func MergePropertiesFromVariant(ctx android.BaseModuleContext,
793 dst interface{}, variantGroup reflect.Value, variant, variantGroupPath string) {
794 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
795 if !src.IsValid() {
796 ctx.ModuleErrorf("field %q does not exist", variantGroupPath+"."+variant)
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700797 }
798
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700799 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
800 if err != nil {
801 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
802 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
803 } else {
804 panic(err)
805 }
806 }
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700807}
808
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700809func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
810 cert := android.SrcIsModule(String(a.properties.Certificate))
811 if cert != "" {
812 ctx.AddDependency(ctx.Module(), certificateTag, cert)
813 }
Colin Cross50ddcc42019-05-16 12:28:22 -0700814
Paul Duffin250e6192019-06-07 10:44:37 +0100815 a.usesLibrary.deps(ctx, true)
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700816}
817
818func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
819 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
820 rule := android.NewRuleBuilder()
821 rule.Command().
822 Textf(`if (zipinfo %s 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
Colin Crossee94d6a2019-07-08 17:08:34 -0700823 BuiltTool(ctx, "zip2zip").
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700824 FlagWithInput("-i ", inputPath).
825 FlagWithOutput("-o ", outputPath).
826 FlagWithArg("-0 ", "'lib/**/*.so'").
827 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
828 rule.Build(pctx, ctx, "uncompress-embedded-jni-libs", "Uncompress embedded JIN libs")
829}
830
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700831// Returns whether this module should have the dex file stored uncompressed in the APK.
832func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
833 if ctx.Config().UnbundledBuild() {
834 return false
835 }
836
837 // Uncompress dex in APKs of privileged apps
838 if ctx.Config().UncompressPrivAppDex() && Bool(a.properties.Privileged) {
839 return true
840 }
841
842 return shouldUncompressDex(ctx, &a.dexpreopter)
843}
844
Jaewoong Jungea1bdb02019-05-09 14:36:34 -0700845func (a *AndroidAppImport) uncompressDex(
846 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
847 rule := android.NewRuleBuilder()
848 rule.Command().
849 Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
Colin Crossee94d6a2019-07-08 17:08:34 -0700850 BuiltTool(ctx, "zip2zip").
Jaewoong Jungea1bdb02019-05-09 14:36:34 -0700851 FlagWithInput("-i ", inputPath).
852 FlagWithOutput("-o ", outputPath).
853 FlagWithArg("-0 ", "'classes*.dex'").
854 Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
855 rule.Build(pctx, ctx, "uncompress-dex", "Uncompress dex files")
856}
857
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700858func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
859 if String(a.properties.Certificate) == "" && !Bool(a.properties.Presigned) {
860 ctx.PropertyErrorf("certificate", "No certificate specified for prebuilt")
861 }
862 if String(a.properties.Certificate) != "" && Bool(a.properties.Presigned) {
863 ctx.PropertyErrorf("certificate", "Certificate can't be specified for presigned modules")
864 }
865
866 _, certificates := collectAppDeps(ctx)
867
868 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700869 // TODO: LOCAL_PACKAGE_SPLITS
870
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700871 srcApk := a.prebuilt.SingleSourcePath(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -0700872
873 if a.usesLibrary.enforceUsesLibraries() {
874 srcApk = a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk)
875 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700876
877 // TODO: Install or embed JNI libraries
878
879 // Uncompress JNI libraries in the apk
880 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
881 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
882
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700883 installDir := android.PathForModuleInstall(ctx, "app", a.BaseModuleName())
884 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
885 a.dexpreopter.isInstallable = true
886 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
Jaewoong Jungacf18d72019-05-02 14:55:29 -0700887 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
Colin Cross50ddcc42019-05-16 12:28:22 -0700888
889 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
890 a.dexpreopter.usesLibs = a.usesLibrary.usesLibraryProperties.Uses_libs
891 a.dexpreopter.optionalUsesLibs = a.usesLibrary.presentOptionalUsesLibs(ctx)
892 a.dexpreopter.libraryPaths = a.usesLibrary.usesLibraryPaths(ctx)
893
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700894 dexOutput := a.dexpreopter.dexpreopt(ctx, jnisUncompressed)
Jaewoong Jungea1bdb02019-05-09 14:36:34 -0700895 if a.dexpreopter.uncompressedDex {
896 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
897 a.uncompressDex(ctx, dexOutput, dexUncompressed.OutputPath)
898 dexOutput = dexUncompressed
899 }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700900
901 // Sign or align the package
902 // TODO: Handle EXTERNAL
903 if !Bool(a.properties.Presigned) {
904 certificates = processMainCert(a.ModuleBase, *a.properties.Certificate, certificates, ctx)
905 if len(certificates) != 1 {
906 ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates)
907 }
908 a.certificate = &certificates[0]
909 signed := android.PathForModuleOut(ctx, "signed", ctx.ModuleName()+".apk")
910 SignAppPackage(ctx, signed, dexOutput, certificates)
911 a.outputFile = signed
912 } else {
913 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", ctx.ModuleName()+".apk")
914 TransformZipAlign(ctx, alignedApk, dexOutput)
915 a.outputFile = alignedApk
916 }
917
918 // TODO: Optionally compress the output apk.
919
920 ctx.InstallFile(installDir, a.BaseModuleName()+".apk", a.outputFile)
921
922 // TODO: androidmk converter jni libs
923}
924
925func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
926 return &a.prebuilt
927}
928
929func (a *AndroidAppImport) Name() string {
930 return a.prebuilt.Name(a.ModuleBase.Name())
931}
932
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700933// Populates dpi_variants property and its fields at creation time.
934func (a *AndroidAppImport) addDpiVariants() {
935 // TODO(jungjw): Do we want to do some filtering here?
936 props := reflect.ValueOf(&a.properties).Type()
937
938 dpiFields := make([]reflect.StructField, len(supportedDpis))
939 for i, dpi := range supportedDpis {
940 dpiFields[i] = reflect.StructField{
941 Name: proptools.FieldNameForProperty(dpi),
942 Type: props,
943 }
944 }
945 dpiStruct := reflect.StructOf(dpiFields)
946 a.dpiVariants = reflect.New(reflect.StructOf([]reflect.StructField{
947 {
948 Name: "Dpi_variants",
949 Type: dpiStruct,
950 },
951 })).Interface()
952 a.AddProperties(a.dpiVariants)
953}
954
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700955// android_app_import imports a prebuilt apk with additional processing specified in the module.
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700956// DPI-specific apk source files can be specified using dpi_variants. Example:
957//
958// android_app_import {
959// name: "example_import",
960// apk: "prebuilts/example.apk",
961// dpi_variants: {
962// mdpi: {
963// apk: "prebuilts/example_mdpi.apk",
964// },
965// xhdpi: {
966// apk: "prebuilts/example_xhdpi.apk",
967// },
968// },
969// certificate: "PRESIGNED",
970// }
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700971func AndroidAppImportFactory() android.Module {
972 module := &AndroidAppImport{}
973 module.AddProperties(&module.properties)
974 module.AddProperties(&module.dexpreoptProperties)
Colin Cross50ddcc42019-05-16 12:28:22 -0700975 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700976 module.addDpiVariants()
977 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
978 module.updateSrcApkPath(ctx)
979 })
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700980
981 InitJavaModule(module, android.DeviceSupported)
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700982 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700983
984 return module
985}
Colin Cross50ddcc42019-05-16 12:28:22 -0700986
987type UsesLibraryProperties struct {
988 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file.
989 Uses_libs []string
990
991 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file with
992 // required=false.
993 Optional_uses_libs []string
994
995 // If true, the list of uses_libs and optional_uses_libs modules must match the AndroidManifest.xml file. Defaults
996 // to true if either uses_libs or optional_uses_libs is set. Will unconditionally default to true in the future.
997 Enforce_uses_libs *bool
998}
999
1000// usesLibrary provides properties and helper functions for AndroidApp and AndroidAppImport to verify that the
1001// <uses-library> tags that end up in the manifest of an APK match the ones known to the build system through the
1002// uses_libs and optional_uses_libs properties. The build system's values are used by dexpreopt to preopt apps
1003// with knowledge of their shared libraries.
1004type usesLibrary struct {
1005 usesLibraryProperties UsesLibraryProperties
1006}
1007
Paul Duffin250e6192019-06-07 10:44:37 +01001008func (u *usesLibrary) deps(ctx android.BottomUpMutatorContext, hasFrameworkLibs bool) {
Colin Cross3245b2c2019-06-07 13:18:09 -07001009 if !ctx.Config().UnbundledBuild() {
1010 ctx.AddVariationDependencies(nil, usesLibTag, u.usesLibraryProperties.Uses_libs...)
1011 ctx.AddVariationDependencies(nil, usesLibTag, u.presentOptionalUsesLibs(ctx)...)
Paul Duffin250e6192019-06-07 10:44:37 +01001012 // Only add these extra dependencies if the module depends on framework libs. This avoids
1013 // creating a cyclic dependency:
1014 // e.g. framework-res -> org.apache.http.legacy -> ... -> framework-res.
1015 if hasFrameworkLibs {
Colin Cross3245b2c2019-06-07 13:18:09 -07001016 // dexpreopt/dexpreopt.go needs the paths to the dex jars of these libraries in case construct_context.sh needs
1017 // to pass them to dex2oat. Add them as a dependency so we can determine the path to the dex jar of each
1018 // library to dexpreopt.
1019 ctx.AddVariationDependencies(nil, usesLibTag,
1020 "org.apache.http.legacy",
1021 "android.hidl.base-V1.0-java",
1022 "android.hidl.manager-V1.0-java")
1023 }
Colin Cross50ddcc42019-05-16 12:28:22 -07001024 }
1025}
1026
1027// presentOptionalUsesLibs returns optional_uses_libs after filtering out MissingUsesLibraries, which don't exist in the
1028// build.
1029func (u *usesLibrary) presentOptionalUsesLibs(ctx android.BaseModuleContext) []string {
1030 optionalUsesLibs, _ := android.FilterList(u.usesLibraryProperties.Optional_uses_libs, ctx.Config().MissingUsesLibraries())
1031 return optionalUsesLibs
1032}
1033
1034// usesLibraryPaths returns a map of module names of shared library dependencies to the paths to their dex jars.
1035func (u *usesLibrary) usesLibraryPaths(ctx android.ModuleContext) map[string]android.Path {
1036 usesLibPaths := make(map[string]android.Path)
1037
1038 if !ctx.Config().UnbundledBuild() {
1039 ctx.VisitDirectDepsWithTag(usesLibTag, func(m android.Module) {
1040 if lib, ok := m.(Dependency); ok {
1041 if dexJar := lib.DexJar(); dexJar != nil {
1042 usesLibPaths[ctx.OtherModuleName(m)] = dexJar
1043 } else {
1044 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must produce a dex jar, does it have installable: true?",
1045 ctx.OtherModuleName(m))
1046 }
1047 } else if ctx.Config().AllowMissingDependencies() {
1048 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(m)})
1049 } else {
1050 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must be a java library",
1051 ctx.OtherModuleName(m))
1052 }
1053 })
1054 }
1055
1056 return usesLibPaths
1057}
1058
1059// enforceUsesLibraries returns true of <uses-library> tags should be checked against uses_libs and optional_uses_libs
1060// properties. Defaults to true if either of uses_libs or optional_uses_libs is specified. Will default to true
1061// unconditionally in the future.
1062func (u *usesLibrary) enforceUsesLibraries() bool {
1063 defaultEnforceUsesLibs := len(u.usesLibraryProperties.Uses_libs) > 0 ||
1064 len(u.usesLibraryProperties.Optional_uses_libs) > 0
1065 return BoolDefault(u.usesLibraryProperties.Enforce_uses_libs, defaultEnforceUsesLibs)
1066}
1067
1068// verifyUsesLibrariesManifest checks the <uses-library> tags in an AndroidManifest.xml against the ones specified
1069// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the manifest.
1070func (u *usesLibrary) verifyUsesLibrariesManifest(ctx android.ModuleContext, manifest android.Path) android.Path {
1071 outputFile := android.PathForModuleOut(ctx, "manifest_check", "AndroidManifest.xml")
1072
1073 rule := android.NewRuleBuilder()
Colin Crossee94d6a2019-07-08 17:08:34 -07001074 cmd := rule.Command().BuiltTool(ctx, "manifest_check").
Colin Cross50ddcc42019-05-16 12:28:22 -07001075 Flag("--enforce-uses-libraries").
1076 Input(manifest).
1077 FlagWithOutput("-o ", outputFile)
1078
1079 for _, lib := range u.usesLibraryProperties.Uses_libs {
1080 cmd.FlagWithArg("--uses-library ", lib)
1081 }
1082
1083 for _, lib := range u.usesLibraryProperties.Optional_uses_libs {
1084 cmd.FlagWithArg("--optional-uses-library ", lib)
1085 }
1086
1087 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1088
1089 return outputFile
1090}
1091
1092// verifyUsesLibrariesAPK checks the <uses-library> tags in the manifest of an APK against the ones specified
1093// in the uses_libs and optional_uses_libs properties. It returns the path to a copy of the APK.
1094func (u *usesLibrary) verifyUsesLibrariesAPK(ctx android.ModuleContext, apk android.Path) android.Path {
1095 outputFile := android.PathForModuleOut(ctx, "verify_uses_libraries", apk.Base())
1096
1097 rule := android.NewRuleBuilder()
1098 aapt := ctx.Config().HostToolPath(ctx, "aapt")
1099 rule.Command().
1100 Textf("aapt_binary=%s", aapt.String()).Implicit(aapt).
1101 Textf(`uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Uses_libs, " ")).
1102 Textf(`optional_uses_library_names="%s"`, strings.Join(u.usesLibraryProperties.Optional_uses_libs, " ")).
1103 Tool(android.PathForSource(ctx, "build/make/core/verify_uses_libraries.sh")).Input(apk)
1104 rule.Command().Text("cp -f").Input(apk).Output(outputFile)
1105
1106 rule.Build(pctx, ctx, "verify_uses_libraries", "verify <uses-library>")
1107
1108 return outputFile
1109}