blob: f044c68480d07f965c968097908363258bae7012 [file] [log] [blame]
Jaewoong Jungf9b44652020-12-21 12:29:12 -08001// Copyright 2020 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 implementations for android_app_import and android_test_import.
18
19import (
20 "reflect"
Cole Faust9c5c09f2023-09-06 16:11:44 -070021 "strings"
Jaewoong Jungf9b44652020-12-21 12:29:12 -080022
Cole Faustd5806132023-04-13 15:43:53 -070023 "github.com/google/blueprint"
24
Jaewoong Jungf9b44652020-12-21 12:29:12 -080025 "github.com/google/blueprint/proptools"
26
27 "android/soong/android"
Wei Li340ee8e2022-03-18 17:33:24 -070028 "android/soong/provenance"
Jaewoong Jungf9b44652020-12-21 12:29:12 -080029)
30
31func init() {
32 RegisterAppImportBuildComponents(android.InitRegistrationContext)
33
34 initAndroidAppImportVariantGroupTypes()
35}
36
Cole Faust4ec178c2023-01-13 12:03:38 -080037var (
38 uncompressEmbeddedJniLibsRule = pctx.AndroidStaticRule("uncompress-embedded-jni-libs", blueprint.RuleParams{
39 Command: `if (zipinfo $in 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then ` +
40 `${config.Zip2ZipCmd} -i $in -o $out -0 'lib/**/*.so'` +
41 `; else cp -f $in $out; fi`,
42 CommandDeps: []string{"${config.Zip2ZipCmd}"},
43 Description: "Uncompress embedded JNI libs",
44 })
45
46 uncompressDexRule = pctx.AndroidStaticRule("uncompress-dex", blueprint.RuleParams{
47 Command: `if (zipinfo $in '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then ` +
48 `${config.Zip2ZipCmd} -i $in -o $out -0 'classes*.dex'` +
49 `; else cp -f $in $out; fi`,
50 CommandDeps: []string{"${config.Zip2ZipCmd}"},
51 Description: "Uncompress dex files",
52 })
Cole Faust2f1da162023-04-17 15:06:56 -070053
Cole Faust9c5c09f2023-09-06 16:11:44 -070054 checkPresignedApkRule = pctx.AndroidStaticRule("check-presigned-apk", blueprint.RuleParams{
55 Command: "build/soong/scripts/check_prebuilt_presigned_apk.py --aapt2 ${config.Aapt2Cmd} --zipalign ${config.ZipAlign} $extraArgs $in $out",
56 CommandDeps: []string{"build/soong/scripts/check_prebuilt_presigned_apk.py", "${config.Aapt2Cmd}", "${config.ZipAlign}"},
57 Description: "Check presigned apk",
58 }, "extraArgs")
Cole Faust4ec178c2023-01-13 12:03:38 -080059)
60
Jaewoong Jungf9b44652020-12-21 12:29:12 -080061func RegisterAppImportBuildComponents(ctx android.RegistrationContext) {
62 ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory)
63 ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory)
Cole Faustc71b1752024-10-14 12:14:54 -070064 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
65 ctx.BottomUp("disable_prebuilts_without_apk", disablePrebuiltsWithoutApkMutator)
66 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -080067}
68
69type AndroidAppImport struct {
70 android.ModuleBase
71 android.DefaultableModuleBase
72 android.ApexModuleBase
73 prebuilt android.Prebuilt
74
Herbert Xue04354ae2024-01-29 13:57:51 +080075 properties AndroidAppImportProperties
76 dpiVariants interface{}
77 archVariants interface{}
78 arch_dpiVariants interface{}
Jaewoong Jungf9b44652020-12-21 12:29:12 -080079
80 outputFile android.Path
81 certificate Certificate
82
83 dexpreopter
84
85 usesLibrary usesLibrary
86
Jaewoong Jungf9b44652020-12-21 12:29:12 -080087 installPath android.InstallPath
88
89 hideApexVariantFromMake bool
Wei Li340ee8e2022-03-18 17:33:24 -070090
Cole Faust4e9f5922024-11-13 16:09:23 -080091 provenanceMetaDataFile android.Path
Jaewoong Jungf9b44652020-12-21 12:29:12 -080092}
93
94type AndroidAppImportProperties struct {
95 // A prebuilt apk to import
Cole Faustc71b1752024-10-14 12:14:54 -070096 Apk proptools.Configurable[string] `android:"path,replace_instead_of_append"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -080097
98 // The name of a certificate in the default certificate directory or an android_app_certificate
99 // module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
100 Certificate *string
101
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800102 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
103 Additional_certificates []string
104
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800105 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
106 // be set for presigned modules.
107 Presigned *bool
108
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800109 // Name of the signing certificate lineage file or filegroup module.
110 Lineage *string `android:"path"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800111
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400112 // For overriding the --rotation-min-sdk-version property of apksig
113 RotationMinSdkVersion *string
114
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800115 // Sign with the default system dev certificate. Must be used judiciously. Most imported apps
116 // need to either specify a specific certificate or be presigned.
117 Default_dev_cert *bool
118
119 // Specifies that this app should be installed to the priv-app directory,
120 // where the system will grant it additional privileges not available to
121 // normal apps.
122 Privileged *bool
123
124 // Names of modules to be overridden. Listed modules can only be other binaries
125 // (in Make or Soong).
126 // This does not completely prevent installation of the overridden binaries, but if both
127 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
128 // from PRODUCT_PACKAGES.
129 Overrides []string
130
131 // Optional name for the installed app. If unspecified, it is derived from the module name.
132 Filename *string
Bill Peckhama036da92021-01-08 16:09:09 -0800133
134 // If set, create package-export.apk, which other packages can
135 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
136 Export_package_resources *bool
Spandan Dasd1fac642021-05-18 17:01:41 +0000137
138 // Optional. Install to a subdirectory of the default install path for the module
139 Relative_install_path *string
Cole Faust2f1da162023-04-17 15:06:56 -0700140
141 // Whether the prebuilt apk can be installed without additional processing. Default is false.
142 Preprocessed *bool
143
144 // Whether or not to skip checking the preprocessed apk for proper alignment and uncompressed
145 // JNI libs and dex files. Default is false
146 Skip_preprocessed_apk_checks *bool
Spandan Dasefa14652024-02-27 18:19:16 +0000147
148 // Name of the source soong module that gets shadowed by this prebuilt
149 // If unspecified, follows the naming convention that the source module of
150 // the prebuilt is Name() without "prebuilt_" prefix
151 Source_module_name *string
Spandan Das3490dfd2024-03-11 21:37:25 +0000152
153 // Path to the .prebuilt_info file of the prebuilt app.
154 // In case of mainline modules, the .prebuilt_info file contains the build_id that was used
155 // to generate the prebuilt.
156 Prebuilt_info *string `android:"path"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800157}
158
159func (a *AndroidAppImport) IsInstallable() bool {
160 return true
161}
162
163// Updates properties with variant-specific values.
Cole Faust97494b12024-01-12 14:02:47 -0800164// This happens as a DefaultableHook instead of a LoadHook because we want to run it after
165// soong config variables are applied.
166func (a *AndroidAppImport) processVariants(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800167 config := ctx.Config()
Herbert Xue04354ae2024-01-29 13:57:51 +0800168 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName(DpiGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800169
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800170 // Try DPI variant matches in the reverse-priority order so that the highest priority match
171 // overwrites everything else.
172 // TODO(jungjw): Can we optimize this by making it priority order?
173 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
174 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
175 }
176 if config.ProductAAPTPreferredConfig() != "" {
177 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
178 }
Herbert Xue04354ae2024-01-29 13:57:51 +0800179 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName(ArchGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800180 archType := ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType
181 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
182
Herbert Xue04354ae2024-01-29 13:57:51 +0800183 // Process "arch" includes "dpi_variants"
184 archStructPtr := reflect.ValueOf(a.arch_dpiVariants).Elem().FieldByName(ArchGroupName)
185 if archStruct := archStructPtr.Elem(); archStruct.IsValid() {
186 archPartPropsPtr := archStruct.FieldByName(proptools.FieldNameForProperty(archType.Name))
187 if archPartProps := archPartPropsPtr.Elem(); archPartProps.IsValid() {
188 archDpiPropsPtr := archPartProps.FieldByName(DpiGroupName)
189 if archDpiProps := archDpiPropsPtr.Elem(); archDpiProps.IsValid() {
190 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
191 MergePropertiesFromVariant(ctx, &a.properties, archDpiProps, config.ProductAAPTPrebuiltDPI()[i])
192 }
193 if config.ProductAAPTPreferredConfig() != "" {
194 MergePropertiesFromVariant(ctx, &a.properties, archDpiProps, config.ProductAAPTPreferredConfig())
195 }
196 }
197 }
198 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800199}
200
201func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
202 dst interface{}, variantGroup reflect.Value, variant string) {
203 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
204 if !src.IsValid() {
205 return
206 }
207
208 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
209 if err != nil {
210 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
211 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
212 } else {
213 panic(err)
214 }
215 }
216}
217
Cole Faustc71b1752024-10-14 12:14:54 -0700218// disablePrebuiltsWithoutApkMutator is a pre-arch mutator that disables AndroidAppImport or
219// AndroidTestImport modules that don't have an apk set. We need this separate mutator instead
220// of doing it in processVariants because processVariants is a defaultable hook, and configurable
221// properties can only be evaluated after the defaults (and eventually, base configurabtion)
222// mutators.
223func disablePrebuiltsWithoutApkMutator(ctx android.BottomUpMutatorContext) {
224 switch a := ctx.Module().(type) {
225 case *AndroidAppImport:
226 if a.properties.Apk.GetOrDefault(ctx, "") == "" {
227 // Disable this module since the apk property is still empty after processing all
228 // matching variants. This likely means there is no matching variant, and the default
229 // variant doesn't have an apk property value either.
230 a.Disable()
231 }
232 case *AndroidTestImport:
233 if a.properties.Apk.GetOrDefault(ctx, "") == "" {
234 // Disable this module since the apk property is still empty after processing all
235 // matching variants. This likely means there is no matching variant, and the default
236 // variant doesn't have an apk property value either.
237 a.Disable()
238 }
239 }
240}
241
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800242func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
243 cert := android.SrcIsModule(String(a.properties.Certificate))
244 if cert != "" {
245 ctx.AddDependency(ctx.Module(), certificateTag, cert)
246 }
247
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800248 for _, cert := range a.properties.Additional_certificates {
249 cert = android.SrcIsModule(cert)
250 if cert != "" {
251 ctx.AddDependency(ctx.Module(), certificateTag, cert)
252 } else {
253 ctx.PropertyErrorf("additional_certificates",
254 `must be names of android_app_certificate modules in the form ":module"`)
255 }
256 }
257
Cole Faustd5806132023-04-13 15:43:53 -0700258 a.usesLibrary.deps(ctx, true)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800259}
260
261func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
Cole Faust4e9f5922024-11-13 16:09:23 -0800262 ctx android.ModuleContext, inputPath android.Path, outputPath android.WritablePath) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800263 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
264 // with them may invalidate pre-existing signature data.
Cole Faust2f1da162023-04-17 15:06:56 -0700265 if ctx.InstallInTestcases() && (Bool(a.properties.Presigned) || Bool(a.properties.Preprocessed)) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800266 ctx.Build(pctx, android.BuildParams{
267 Rule: android.Cp,
268 Output: outputPath,
269 Input: inputPath,
270 })
271 return
272 }
Cole Faust4ec178c2023-01-13 12:03:38 -0800273
274 ctx.Build(pctx, android.BuildParams{
275 Rule: uncompressEmbeddedJniLibsRule,
276 Input: inputPath,
277 Output: outputPath,
278 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800279}
280
281// Returns whether this module should have the dex file stored uncompressed in the APK.
282func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
Cole Faust2f1da162023-04-17 15:06:56 -0700283 if ctx.Config().UnbundledBuild() || proptools.Bool(a.properties.Preprocessed) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800284 return false
285 }
286
Ulya Trafimovich0061c0d2021-09-01 15:40:38 +0100287 // Uncompress dex in APKs of priv-apps if and only if DONT_UNCOMPRESS_PRIV_APPS_DEXS is false.
288 if a.Privileged() {
289 return ctx.Config().UncompressPrivAppDex()
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800290 }
291
Spandan Dase21a8d42024-01-23 23:56:29 +0000292 return shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &a.dexpreopter)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800293}
294
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800295func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
296 a.generateAndroidBuildActions(ctx)
297}
298
299func (a *AndroidAppImport) InstallApkName() string {
300 return a.BaseModuleName()
301}
302
Spandan Dasefa14652024-02-27 18:19:16 +0000303func (a *AndroidAppImport) BaseModuleName() string {
304 return proptools.StringDefault(a.properties.Source_module_name, a.ModuleBase.Name())
305}
306
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800307func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faustd5806132023-04-13 15:43:53 -0700308 if a.Name() == "prebuilt_framework-res" {
309 ctx.ModuleErrorf("prebuilt_framework-res found. This used to have special handling in soong, but was removed due to prebuilt_framework-res no longer existing. This check is to ensure it doesn't come back without readding the special handling.")
310 }
311
Colin Crossff694a82023-12-13 15:54:49 -0800312 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800313 if !apexInfo.IsForPlatform() {
314 a.hideApexVariantFromMake = true
315 }
316
Cole Faust61585282023-07-14 16:23:39 -0700317 if Bool(a.properties.Preprocessed) {
318 if a.properties.Presigned != nil && !*a.properties.Presigned {
319 ctx.ModuleErrorf("Setting preprocessed: true implies presigned: true, so you cannot set presigned to false")
320 }
321 t := true
322 a.properties.Presigned = &t
323 }
324
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800325 numCertPropsSet := 0
326 if String(a.properties.Certificate) != "" {
327 numCertPropsSet++
328 }
329 if Bool(a.properties.Presigned) {
330 numCertPropsSet++
331 }
332 if Bool(a.properties.Default_dev_cert) {
333 numCertPropsSet++
334 }
335 if numCertPropsSet != 1 {
Cole Faust61585282023-07-14 16:23:39 -0700336 ctx.ModuleErrorf("One and only one of certficate, presigned (implied by preprocessed), and default_dev_cert properties must be set")
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800337 }
338
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800339 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
340 // TODO: LOCAL_PACKAGE_SPLITS
341
342 srcApk := a.prebuilt.SingleSourcePath(ctx)
343
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800344 // TODO: Install or embed JNI libraries
345
346 // Uncompress JNI libraries in the apk
347 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
Cole Faust4e9f5922024-11-13 16:09:23 -0800348 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800349
Spandan Dasd1fac642021-05-18 17:01:41 +0000350 var pathFragments []string
351 relInstallPath := String(a.properties.Relative_install_path)
Bill Peckhama036da92021-01-08 16:09:09 -0800352
Cole Faustd5806132023-04-13 15:43:53 -0700353 if Bool(a.properties.Privileged) {
Spandan Dasd1fac642021-05-18 17:01:41 +0000354 pathFragments = []string{"priv-app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800355 } else if ctx.InstallInTestcases() {
Spandan Dasd1fac642021-05-18 17:01:41 +0000356 pathFragments = []string{relInstallPath, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800357 } else {
Spandan Dasd1fac642021-05-18 17:01:41 +0000358 pathFragments = []string{"app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800359 }
360
Spandan Dasd1fac642021-05-18 17:01:41 +0000361 installDir := android.PathForModuleInstall(ctx, pathFragments...)
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000362 a.dexpreopter.isApp = true
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800363 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
364 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
365 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
366
Cole Faust64f2d842024-10-17 13:28:34 -0700367 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries(ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800368 a.dexpreopter.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Spandan Das0727ba72024-02-13 16:37:43 +0000369 if a.usesLibrary.shouldDisableDexpreopt {
370 a.dexpreopter.disableDexpreopt()
371 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800372
Cole Faust64f2d842024-10-17 13:28:34 -0700373 if a.usesLibrary.enforceUsesLibraries(ctx) {
Jiakai Zhangf98da192024-04-15 11:15:41 +0000374 a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk, &a.dexpreopter.classLoaderContexts)
Ulya Trafimovichfe927a22021-02-26 14:36:48 +0000375 }
376
Spandan Dase21a8d42024-01-23 23:56:29 +0000377 a.dexpreopter.dexpreopt(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), jnisUncompressed)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800378 if a.dexpreopter.uncompressedDex {
379 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
Cole Faust4ec178c2023-01-13 12:03:38 -0800380 ctx.Build(pctx, android.BuildParams{
381 Rule: uncompressDexRule,
382 Input: jnisUncompressed,
383 Output: dexUncompressed,
384 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800385 jnisUncompressed = dexUncompressed
386 }
387
388 apkFilename := proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk")
389
390 // TODO: Handle EXTERNAL
391
392 // Sign or align the package if package has not been preprocessed
Bill Peckhama036da92021-01-08 16:09:09 -0800393
Cole Faust2f1da162023-04-17 15:06:56 -0700394 if proptools.Bool(a.properties.Preprocessed) {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700395 validationStamp := a.validatePresignedApk(ctx, srcApk)
396 output := android.PathForModuleOut(ctx, apkFilename)
397 ctx.Build(pctx, android.BuildParams{
398 Rule: android.Cp,
399 Input: srcApk,
400 Output: output,
401 Validation: validationStamp,
402 })
Cole Faust2f1da162023-04-17 15:06:56 -0700403 a.outputFile = output
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800404 a.certificate = PresignedCertificate
405 } else if !Bool(a.properties.Presigned) {
406 // If the certificate property is empty at this point, default_dev_cert must be set to true.
407 // Which makes processMainCert's behavior for the empty cert string WAI.
Cole Faust61585282023-07-14 16:23:39 -0700408 _, _, certificates := collectAppDeps(ctx, a, false, false)
Colin Crossbc2c8a72022-09-14 12:45:42 -0700409 a.certificate, certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800410 signed := android.PathForModuleOut(ctx, "signed", apkFilename)
411 var lineageFile android.Path
412 if lineage := String(a.properties.Lineage); lineage != "" {
413 lineageFile = android.PathForModuleSrc(ctx, lineage)
414 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400415
416 rotationMinSdkVersion := String(a.properties.RotationMinSdkVersion)
417
418 SignAppPackage(ctx, signed, jnisUncompressed, certificates, nil, lineageFile, rotationMinSdkVersion)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800419 a.outputFile = signed
420 } else {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700421 validationStamp := a.validatePresignedApk(ctx, srcApk)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800422 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename)
Cole Faust9c5c09f2023-09-06 16:11:44 -0700423 TransformZipAlign(ctx, alignedApk, jnisUncompressed, []android.Path{validationStamp})
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800424 a.outputFile = alignedApk
425 a.certificate = PresignedCertificate
426 }
427
428 // TODO: Optionally compress the output apk.
429
430 if apexInfo.IsForPlatform() {
431 a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
Cole Faustc71b1752024-10-14 12:14:54 -0700432 artifactPath := android.PathForModuleSrc(ctx, a.properties.Apk.GetOrDefault(ctx, ""))
Wei Li340ee8e2022-03-18 17:33:24 -0700433 a.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, artifactPath, a.installPath)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800434 }
435
Spandan Das3490dfd2024-03-11 21:37:25 +0000436 providePrebuiltInfo(ctx,
437 prebuiltInfoProps{
438 baseModuleName: a.BaseModuleName(),
439 isPrebuilt: true,
440 prebuiltInfo: a.properties.Prebuilt_info,
441 },
442 )
443
mrziwang68786d82024-07-09 10:41:55 -0700444 ctx.SetOutputFiles([]android.Path{a.outputFile}, "")
445
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800446 // TODO: androidmk converter jni libs
447}
448
Cole Faust9c5c09f2023-09-06 16:11:44 -0700449func (a *AndroidAppImport) validatePresignedApk(ctx android.ModuleContext, srcApk android.Path) android.Path {
450 stamp := android.PathForModuleOut(ctx, "validated-prebuilt", "check.stamp")
451 var extraArgs []string
Cole Faust93b89b42023-07-20 17:31:16 -0700452 if a.Privileged() {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700453 extraArgs = append(extraArgs, "--privileged")
Rashid Zaman3bd28702024-07-23 17:23:05 -0700454 if ctx.Config().UncompressPrivAppDex() {
455 extraArgs = append(extraArgs, "--uncompress-priv-app-dex")
456 }
Cole Faust9c5c09f2023-09-06 16:11:44 -0700457 }
458 if proptools.Bool(a.properties.Skip_preprocessed_apk_checks) {
459 extraArgs = append(extraArgs, "--skip-preprocessed-apk-checks")
460 }
461 if proptools.Bool(a.properties.Preprocessed) {
462 extraArgs = append(extraArgs, "--preprocessed")
Cole Faust93b89b42023-07-20 17:31:16 -0700463 }
464
Cole Faust2f1da162023-04-17 15:06:56 -0700465 ctx.Build(pctx, android.BuildParams{
Cole Faust9c5c09f2023-09-06 16:11:44 -0700466 Rule: checkPresignedApkRule,
Cole Faust61585282023-07-14 16:23:39 -0700467 Input: srcApk,
Cole Faust9c5c09f2023-09-06 16:11:44 -0700468 Output: stamp,
469 Args: map[string]string{
470 "extraArgs": strings.Join(extraArgs, " "),
471 },
Cole Faust61585282023-07-14 16:23:39 -0700472 })
Cole Faust9c5c09f2023-09-06 16:11:44 -0700473 return stamp
Cole Faust61585282023-07-14 16:23:39 -0700474}
475
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800476func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
477 return &a.prebuilt
478}
479
480func (a *AndroidAppImport) Name() string {
481 return a.prebuilt.Name(a.ModuleBase.Name())
482}
483
484func (a *AndroidAppImport) OutputFile() android.Path {
485 return a.outputFile
486}
487
488func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
489 return nil
490}
491
492func (a *AndroidAppImport) Certificate() Certificate {
493 return a.certificate
494}
495
Cole Faust4e9f5922024-11-13 16:09:23 -0800496func (a *AndroidAppImport) ProvenanceMetaDataFile() android.Path {
Wei Li340ee8e2022-03-18 17:33:24 -0700497 return a.provenanceMetaDataFile
498}
499
Andrei Onea580636b2022-08-17 16:53:46 +0000500func (a *AndroidAppImport) PrivAppAllowlist() android.OptionalPath {
501 return android.OptionalPath{}
502}
503
Herbert Xue04354ae2024-01-29 13:57:51 +0800504const (
505 ArchGroupName = "Arch"
506 DpiGroupName = "Dpi_variants"
507)
508
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800509var dpiVariantGroupType reflect.Type
510var archVariantGroupType reflect.Type
Herbert Xue04354ae2024-01-29 13:57:51 +0800511var archdpiVariantGroupType reflect.Type
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800512var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
513
514func initAndroidAppImportVariantGroupTypes() {
Herbert Xue04354ae2024-01-29 13:57:51 +0800515 dpiVariantGroupType = createVariantGroupType(supportedDpis, DpiGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800516
517 archNames := make([]string, len(android.ArchTypeList()))
518 for i, archType := range android.ArchTypeList() {
519 archNames[i] = archType.Name
520 }
Herbert Xue04354ae2024-01-29 13:57:51 +0800521 archVariantGroupType = createVariantGroupType(archNames, ArchGroupName)
522 archdpiVariantGroupType = createArchDpiVariantGroupType(archNames, supportedDpis)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800523}
524
525// Populates all variant struct properties at creation time.
526func (a *AndroidAppImport) populateAllVariantStructs() {
527 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
528 a.AddProperties(a.dpiVariants)
529
530 a.archVariants = reflect.New(archVariantGroupType).Interface()
531 a.AddProperties(a.archVariants)
Herbert Xue04354ae2024-01-29 13:57:51 +0800532
533 a.arch_dpiVariants = reflect.New(archdpiVariantGroupType).Interface()
534 a.AddProperties(a.arch_dpiVariants)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800535}
536
537func (a *AndroidAppImport) Privileged() bool {
538 return Bool(a.properties.Privileged)
539}
540
541func (a *AndroidAppImport) DepIsInSameApex(_ android.BaseModuleContext, _ android.Module) bool {
542 // android_app_import might have extra dependencies via uses_libs property.
543 // Don't track the dependency as we don't automatically add those libraries
544 // to the classpath. It should be explicitly added to java_libs property of APEX
545 return false
546}
547
Jiyong Park92315372021-04-02 08:45:46 +0900548func (a *AndroidAppImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
549 return android.SdkSpecPrivate
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800550}
551
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000552func (a *AndroidAppImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
553 return android.SdkSpecPrivate.ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800554}
555
556var _ android.ApexModule = (*AndroidAppImport)(nil)
557
558// Implements android.ApexModule
559func (j *AndroidAppImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
560 sdkVersion android.ApiLevel) error {
561 // Do not check for prebuilts against the min_sdk_version of enclosing APEX
562 return nil
563}
564
565func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
566 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
567
568 variantFields := make([]reflect.StructField, len(variants))
569 for i, variant := range variants {
570 variantFields[i] = reflect.StructField{
571 Name: proptools.FieldNameForProperty(variant),
572 Type: props,
573 }
574 }
575
576 variantGroupStruct := reflect.StructOf(variantFields)
577 return reflect.StructOf([]reflect.StructField{
578 {
579 Name: variantGroupName,
580 Type: variantGroupStruct,
581 },
582 })
583}
584
Herbert Xue04354ae2024-01-29 13:57:51 +0800585func createArchDpiVariantGroupType(archNames []string, dpiNames []string) reflect.Type {
586 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
587
588 dpiVariantFields := make([]reflect.StructField, len(dpiNames))
589 for i, variant_dpi := range dpiNames {
590 dpiVariantFields[i] = reflect.StructField{
591 Name: proptools.FieldNameForProperty(variant_dpi),
592 Type: props,
593 }
594 }
595 dpiVariantGroupStruct := reflect.StructOf(dpiVariantFields)
596 dpi_struct := reflect.StructOf([]reflect.StructField{
597 {
598 Name: DpiGroupName,
599 Type: reflect.PointerTo(dpiVariantGroupStruct),
600 },
601 })
602
603 archVariantFields := make([]reflect.StructField, len(archNames))
604 for i, variant_arch := range archNames {
605 archVariantFields[i] = reflect.StructField{
606 Name: proptools.FieldNameForProperty(variant_arch),
607 Type: reflect.PointerTo(dpi_struct),
608 }
609 }
610 archVariantGroupStruct := reflect.StructOf(archVariantFields)
611
612 return_struct := reflect.StructOf([]reflect.StructField{
613 {
614 Name: ArchGroupName,
615 Type: reflect.PointerTo(archVariantGroupStruct),
616 },
617 })
618 return return_struct
619}
620
Jiakai Zhangf98da192024-04-15 11:15:41 +0000621func (a *AndroidAppImport) UsesLibrary() *usesLibrary {
622 return &a.usesLibrary
623}
624
625var _ ModuleWithUsesLibrary = (*AndroidAppImport)(nil)
626
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800627// android_app_import imports a prebuilt apk with additional processing specified in the module.
628// DPI-specific apk source files can be specified using dpi_variants. Example:
629//
Colin Crossd079e0b2022-08-16 10:27:33 -0700630// android_app_import {
631// name: "example_import",
632// apk: "prebuilts/example.apk",
633// dpi_variants: {
634// mdpi: {
635// apk: "prebuilts/example_mdpi.apk",
636// },
637// xhdpi: {
638// apk: "prebuilts/example_xhdpi.apk",
639// },
640// },
641// presigned: true,
642// }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800643func AndroidAppImportFactory() android.Module {
644 module := &AndroidAppImport{}
645 module.AddProperties(&module.properties)
646 module.AddProperties(&module.dexpreoptProperties)
647 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
648 module.populateAllVariantStructs()
Cole Faust97494b12024-01-12 14:02:47 -0800649 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800650 module.processVariants(ctx)
651 })
652
653 android.InitApexModule(module)
654 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
655 android.InitDefaultableModule(module)
Cole Faustc71b1752024-10-14 12:14:54 -0700656 android.InitConfigurablePrebuiltModuleString(module, &module.properties.Apk, "Apk")
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800657
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000658 module.usesLibrary.enforce = true
659
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800660 return module
661}
662
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800663type AndroidTestImport struct {
664 AndroidAppImport
665
Jiyong Park2f83b312022-10-20 20:18:35 +0900666 testProperties struct {
667 // list of compatibility suites (for example "cts", "vts") that the module should be
668 // installed into.
669 Test_suites []string `android:"arch_variant"`
670
671 // list of files or filegroup modules that provide data that should be installed alongside
672 // the test
673 Data []string `android:"path"`
674
675 // Install the test into a folder named for the module in all test suites.
676 Per_testcase_directory *bool
677 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800678
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800679 data android.Paths
680}
681
682func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800683 a.generateAndroidBuildActions(ctx)
684
685 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
686}
687
688func (a *AndroidTestImport) InstallInTestcases() bool {
689 return true
690}
691
692// android_test_import imports a prebuilt test apk with additional processing specified in the
693// module. DPI or arch variant configurations can be made as with android_app_import.
694func AndroidTestImportFactory() android.Module {
695 module := &AndroidTestImport{}
696 module.AddProperties(&module.properties)
697 module.AddProperties(&module.dexpreoptProperties)
698 module.AddProperties(&module.testProperties)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800699 module.populateAllVariantStructs()
Cole Faust97494b12024-01-12 14:02:47 -0800700 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800701 module.processVariants(ctx)
702 })
703
704 module.dexpreopter.isTest = true
705
706 android.InitApexModule(module)
707 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
708 android.InitDefaultableModule(module)
Cole Faustc71b1752024-10-14 12:14:54 -0700709 android.InitConfigurablePrebuiltModuleString(module, &module.properties.Apk, "Apk")
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800710
711 return module
712}