blob: fa87997cf6403627c209af30a51471455b09135b [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)
64}
65
66type AndroidAppImport struct {
67 android.ModuleBase
68 android.DefaultableModuleBase
69 android.ApexModuleBase
70 prebuilt android.Prebuilt
71
Herbert Xue04354ae2024-01-29 13:57:51 +080072 properties AndroidAppImportProperties
73 dpiVariants interface{}
74 archVariants interface{}
75 arch_dpiVariants interface{}
Jaewoong Jungf9b44652020-12-21 12:29:12 -080076
77 outputFile android.Path
78 certificate Certificate
79
80 dexpreopter
81
82 usesLibrary usesLibrary
83
Jaewoong Jungf9b44652020-12-21 12:29:12 -080084 installPath android.InstallPath
85
86 hideApexVariantFromMake bool
Wei Li340ee8e2022-03-18 17:33:24 -070087
88 provenanceMetaDataFile android.OutputPath
Jaewoong Jungf9b44652020-12-21 12:29:12 -080089}
90
91type AndroidAppImportProperties struct {
92 // A prebuilt apk to import
Jooyung Hanf05ca9c2021-06-28 21:48:51 +090093 Apk *string `android:"path"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -080094
95 // The name of a certificate in the default certificate directory or an android_app_certificate
96 // module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
97 Certificate *string
98
Jaewoong Jung25ae8de2021-03-08 17:37:46 -080099 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
100 Additional_certificates []string
101
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800102 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
103 // be set for presigned modules.
104 Presigned *bool
105
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800106 // Name of the signing certificate lineage file or filegroup module.
107 Lineage *string `android:"path"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800108
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400109 // For overriding the --rotation-min-sdk-version property of apksig
110 RotationMinSdkVersion *string
111
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800112 // Sign with the default system dev certificate. Must be used judiciously. Most imported apps
113 // need to either specify a specific certificate or be presigned.
114 Default_dev_cert *bool
115
116 // Specifies that this app should be installed to the priv-app directory,
117 // where the system will grant it additional privileges not available to
118 // normal apps.
119 Privileged *bool
120
121 // Names of modules to be overridden. Listed modules can only be other binaries
122 // (in Make or Soong).
123 // This does not completely prevent installation of the overridden binaries, but if both
124 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
125 // from PRODUCT_PACKAGES.
126 Overrides []string
127
128 // Optional name for the installed app. If unspecified, it is derived from the module name.
129 Filename *string
Bill Peckhama036da92021-01-08 16:09:09 -0800130
131 // If set, create package-export.apk, which other packages can
132 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
133 Export_package_resources *bool
Spandan Dasd1fac642021-05-18 17:01:41 +0000134
135 // Optional. Install to a subdirectory of the default install path for the module
136 Relative_install_path *string
Cole Faust2f1da162023-04-17 15:06:56 -0700137
138 // Whether the prebuilt apk can be installed without additional processing. Default is false.
139 Preprocessed *bool
140
141 // Whether or not to skip checking the preprocessed apk for proper alignment and uncompressed
142 // JNI libs and dex files. Default is false
143 Skip_preprocessed_apk_checks *bool
Spandan Dasefa14652024-02-27 18:19:16 +0000144
145 // Name of the source soong module that gets shadowed by this prebuilt
146 // If unspecified, follows the naming convention that the source module of
147 // the prebuilt is Name() without "prebuilt_" prefix
148 Source_module_name *string
Spandan Das3490dfd2024-03-11 21:37:25 +0000149
150 // Path to the .prebuilt_info file of the prebuilt app.
151 // In case of mainline modules, the .prebuilt_info file contains the build_id that was used
152 // to generate the prebuilt.
153 Prebuilt_info *string `android:"path"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800154}
155
156func (a *AndroidAppImport) IsInstallable() bool {
157 return true
158}
159
160// Updates properties with variant-specific values.
Cole Faust97494b12024-01-12 14:02:47 -0800161// This happens as a DefaultableHook instead of a LoadHook because we want to run it after
162// soong config variables are applied.
163func (a *AndroidAppImport) processVariants(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800164 config := ctx.Config()
Herbert Xue04354ae2024-01-29 13:57:51 +0800165 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName(DpiGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800166
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800167 // Try DPI variant matches in the reverse-priority order so that the highest priority match
168 // overwrites everything else.
169 // TODO(jungjw): Can we optimize this by making it priority order?
170 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
171 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
172 }
173 if config.ProductAAPTPreferredConfig() != "" {
174 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
175 }
Herbert Xue04354ae2024-01-29 13:57:51 +0800176 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName(ArchGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800177 archType := ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType
178 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
179
Herbert Xue04354ae2024-01-29 13:57:51 +0800180 // Process "arch" includes "dpi_variants"
181 archStructPtr := reflect.ValueOf(a.arch_dpiVariants).Elem().FieldByName(ArchGroupName)
182 if archStruct := archStructPtr.Elem(); archStruct.IsValid() {
183 archPartPropsPtr := archStruct.FieldByName(proptools.FieldNameForProperty(archType.Name))
184 if archPartProps := archPartPropsPtr.Elem(); archPartProps.IsValid() {
185 archDpiPropsPtr := archPartProps.FieldByName(DpiGroupName)
186 if archDpiProps := archDpiPropsPtr.Elem(); archDpiProps.IsValid() {
187 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
188 MergePropertiesFromVariant(ctx, &a.properties, archDpiProps, config.ProductAAPTPrebuiltDPI()[i])
189 }
190 if config.ProductAAPTPreferredConfig() != "" {
191 MergePropertiesFromVariant(ctx, &a.properties, archDpiProps, config.ProductAAPTPreferredConfig())
192 }
193 }
194 }
195 }
196
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800197 if String(a.properties.Apk) == "" {
198 // Disable this module since the apk property is still empty after processing all matching
199 // variants. This likely means there is no matching variant, and the default variant doesn't
200 // have an apk property value either.
201 a.Disable()
202 }
203}
204
205func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
206 dst interface{}, variantGroup reflect.Value, variant string) {
207 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
208 if !src.IsValid() {
209 return
210 }
211
212 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
213 if err != nil {
214 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
215 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
216 } else {
217 panic(err)
218 }
219 }
220}
221
222func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
223 cert := android.SrcIsModule(String(a.properties.Certificate))
224 if cert != "" {
225 ctx.AddDependency(ctx.Module(), certificateTag, cert)
226 }
227
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800228 for _, cert := range a.properties.Additional_certificates {
229 cert = android.SrcIsModule(cert)
230 if cert != "" {
231 ctx.AddDependency(ctx.Module(), certificateTag, cert)
232 } else {
233 ctx.PropertyErrorf("additional_certificates",
234 `must be names of android_app_certificate modules in the form ":module"`)
235 }
236 }
237
Cole Faustd5806132023-04-13 15:43:53 -0700238 a.usesLibrary.deps(ctx, true)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800239}
240
241func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
242 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
243 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
244 // with them may invalidate pre-existing signature data.
Cole Faust2f1da162023-04-17 15:06:56 -0700245 if ctx.InstallInTestcases() && (Bool(a.properties.Presigned) || Bool(a.properties.Preprocessed)) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800246 ctx.Build(pctx, android.BuildParams{
247 Rule: android.Cp,
248 Output: outputPath,
249 Input: inputPath,
250 })
251 return
252 }
Cole Faust4ec178c2023-01-13 12:03:38 -0800253
254 ctx.Build(pctx, android.BuildParams{
255 Rule: uncompressEmbeddedJniLibsRule,
256 Input: inputPath,
257 Output: outputPath,
258 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800259}
260
261// Returns whether this module should have the dex file stored uncompressed in the APK.
262func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
Cole Faust2f1da162023-04-17 15:06:56 -0700263 if ctx.Config().UnbundledBuild() || proptools.Bool(a.properties.Preprocessed) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800264 return false
265 }
266
Ulya Trafimovich0061c0d2021-09-01 15:40:38 +0100267 // Uncompress dex in APKs of priv-apps if and only if DONT_UNCOMPRESS_PRIV_APPS_DEXS is false.
268 if a.Privileged() {
269 return ctx.Config().UncompressPrivAppDex()
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800270 }
271
Spandan Dase21a8d42024-01-23 23:56:29 +0000272 return shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &a.dexpreopter)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800273}
274
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800275func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
276 a.generateAndroidBuildActions(ctx)
277}
278
279func (a *AndroidAppImport) InstallApkName() string {
280 return a.BaseModuleName()
281}
282
Spandan Dasefa14652024-02-27 18:19:16 +0000283func (a *AndroidAppImport) BaseModuleName() string {
284 return proptools.StringDefault(a.properties.Source_module_name, a.ModuleBase.Name())
285}
286
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800287func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faustd5806132023-04-13 15:43:53 -0700288 if a.Name() == "prebuilt_framework-res" {
289 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.")
290 }
291
Colin Crossff694a82023-12-13 15:54:49 -0800292 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800293 if !apexInfo.IsForPlatform() {
294 a.hideApexVariantFromMake = true
295 }
296
Cole Faust61585282023-07-14 16:23:39 -0700297 if Bool(a.properties.Preprocessed) {
298 if a.properties.Presigned != nil && !*a.properties.Presigned {
299 ctx.ModuleErrorf("Setting preprocessed: true implies presigned: true, so you cannot set presigned to false")
300 }
301 t := true
302 a.properties.Presigned = &t
303 }
304
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800305 numCertPropsSet := 0
306 if String(a.properties.Certificate) != "" {
307 numCertPropsSet++
308 }
309 if Bool(a.properties.Presigned) {
310 numCertPropsSet++
311 }
312 if Bool(a.properties.Default_dev_cert) {
313 numCertPropsSet++
314 }
315 if numCertPropsSet != 1 {
Cole Faust61585282023-07-14 16:23:39 -0700316 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 -0800317 }
318
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800319 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
320 // TODO: LOCAL_PACKAGE_SPLITS
321
322 srcApk := a.prebuilt.SingleSourcePath(ctx)
323
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800324 // TODO: Install or embed JNI libraries
325
326 // Uncompress JNI libraries in the apk
327 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
328 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
329
Spandan Dasd1fac642021-05-18 17:01:41 +0000330 var pathFragments []string
331 relInstallPath := String(a.properties.Relative_install_path)
Bill Peckhama036da92021-01-08 16:09:09 -0800332
Cole Faustd5806132023-04-13 15:43:53 -0700333 if Bool(a.properties.Privileged) {
Spandan Dasd1fac642021-05-18 17:01:41 +0000334 pathFragments = []string{"priv-app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800335 } else if ctx.InstallInTestcases() {
Spandan Dasd1fac642021-05-18 17:01:41 +0000336 pathFragments = []string{relInstallPath, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800337 } else {
Spandan Dasd1fac642021-05-18 17:01:41 +0000338 pathFragments = []string{"app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800339 }
340
Spandan Dasd1fac642021-05-18 17:01:41 +0000341 installDir := android.PathForModuleInstall(ctx, pathFragments...)
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000342 a.dexpreopter.isApp = true
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800343 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
344 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
345 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
346
347 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
348 a.dexpreopter.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Spandan Das0727ba72024-02-13 16:37:43 +0000349 if a.usesLibrary.shouldDisableDexpreopt {
350 a.dexpreopter.disableDexpreopt()
351 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800352
Ulya Trafimovichfe927a22021-02-26 14:36:48 +0000353 if a.usesLibrary.enforceUsesLibraries() {
Jiakai Zhangf98da192024-04-15 11:15:41 +0000354 a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk, &a.dexpreopter.classLoaderContexts)
Ulya Trafimovichfe927a22021-02-26 14:36:48 +0000355 }
356
Spandan Dase21a8d42024-01-23 23:56:29 +0000357 a.dexpreopter.dexpreopt(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), jnisUncompressed)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800358 if a.dexpreopter.uncompressedDex {
359 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
Cole Faust4ec178c2023-01-13 12:03:38 -0800360 ctx.Build(pctx, android.BuildParams{
361 Rule: uncompressDexRule,
362 Input: jnisUncompressed,
363 Output: dexUncompressed,
364 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800365 jnisUncompressed = dexUncompressed
366 }
367
368 apkFilename := proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk")
369
370 // TODO: Handle EXTERNAL
371
372 // Sign or align the package if package has not been preprocessed
Bill Peckhama036da92021-01-08 16:09:09 -0800373
Cole Faust2f1da162023-04-17 15:06:56 -0700374 if proptools.Bool(a.properties.Preprocessed) {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700375 validationStamp := a.validatePresignedApk(ctx, srcApk)
376 output := android.PathForModuleOut(ctx, apkFilename)
377 ctx.Build(pctx, android.BuildParams{
378 Rule: android.Cp,
379 Input: srcApk,
380 Output: output,
381 Validation: validationStamp,
382 })
Cole Faust2f1da162023-04-17 15:06:56 -0700383 a.outputFile = output
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800384 a.certificate = PresignedCertificate
385 } else if !Bool(a.properties.Presigned) {
386 // If the certificate property is empty at this point, default_dev_cert must be set to true.
387 // Which makes processMainCert's behavior for the empty cert string WAI.
Cole Faust61585282023-07-14 16:23:39 -0700388 _, _, certificates := collectAppDeps(ctx, a, false, false)
Colin Crossbc2c8a72022-09-14 12:45:42 -0700389 a.certificate, certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800390 signed := android.PathForModuleOut(ctx, "signed", apkFilename)
391 var lineageFile android.Path
392 if lineage := String(a.properties.Lineage); lineage != "" {
393 lineageFile = android.PathForModuleSrc(ctx, lineage)
394 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400395
396 rotationMinSdkVersion := String(a.properties.RotationMinSdkVersion)
397
398 SignAppPackage(ctx, signed, jnisUncompressed, certificates, nil, lineageFile, rotationMinSdkVersion)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800399 a.outputFile = signed
400 } else {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700401 validationStamp := a.validatePresignedApk(ctx, srcApk)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800402 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename)
Cole Faust9c5c09f2023-09-06 16:11:44 -0700403 TransformZipAlign(ctx, alignedApk, jnisUncompressed, []android.Path{validationStamp})
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800404 a.outputFile = alignedApk
405 a.certificate = PresignedCertificate
406 }
407
408 // TODO: Optionally compress the output apk.
409
410 if apexInfo.IsForPlatform() {
411 a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
Wei Li340ee8e2022-03-18 17:33:24 -0700412 artifactPath := android.PathForModuleSrc(ctx, *a.properties.Apk)
413 a.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, artifactPath, a.installPath)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800414 }
415
Spandan Das3490dfd2024-03-11 21:37:25 +0000416 providePrebuiltInfo(ctx,
417 prebuiltInfoProps{
418 baseModuleName: a.BaseModuleName(),
419 isPrebuilt: true,
420 prebuiltInfo: a.properties.Prebuilt_info,
421 },
422 )
423
mrziwang68786d82024-07-09 10:41:55 -0700424 ctx.SetOutputFiles([]android.Path{a.outputFile}, "")
425
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800426 // TODO: androidmk converter jni libs
427}
428
Cole Faust9c5c09f2023-09-06 16:11:44 -0700429func (a *AndroidAppImport) validatePresignedApk(ctx android.ModuleContext, srcApk android.Path) android.Path {
430 stamp := android.PathForModuleOut(ctx, "validated-prebuilt", "check.stamp")
431 var extraArgs []string
Cole Faust93b89b42023-07-20 17:31:16 -0700432 if a.Privileged() {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700433 extraArgs = append(extraArgs, "--privileged")
434 }
435 if proptools.Bool(a.properties.Skip_preprocessed_apk_checks) {
436 extraArgs = append(extraArgs, "--skip-preprocessed-apk-checks")
437 }
438 if proptools.Bool(a.properties.Preprocessed) {
439 extraArgs = append(extraArgs, "--preprocessed")
Cole Faust93b89b42023-07-20 17:31:16 -0700440 }
441
Cole Faust2f1da162023-04-17 15:06:56 -0700442 ctx.Build(pctx, android.BuildParams{
Cole Faust9c5c09f2023-09-06 16:11:44 -0700443 Rule: checkPresignedApkRule,
Cole Faust61585282023-07-14 16:23:39 -0700444 Input: srcApk,
Cole Faust9c5c09f2023-09-06 16:11:44 -0700445 Output: stamp,
446 Args: map[string]string{
447 "extraArgs": strings.Join(extraArgs, " "),
448 },
Cole Faust61585282023-07-14 16:23:39 -0700449 })
Cole Faust9c5c09f2023-09-06 16:11:44 -0700450 return stamp
Cole Faust61585282023-07-14 16:23:39 -0700451}
452
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800453func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
454 return &a.prebuilt
455}
456
457func (a *AndroidAppImport) Name() string {
458 return a.prebuilt.Name(a.ModuleBase.Name())
459}
460
461func (a *AndroidAppImport) OutputFile() android.Path {
462 return a.outputFile
463}
464
465func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
466 return nil
467}
468
469func (a *AndroidAppImport) Certificate() Certificate {
470 return a.certificate
471}
472
Wei Li340ee8e2022-03-18 17:33:24 -0700473func (a *AndroidAppImport) ProvenanceMetaDataFile() android.OutputPath {
474 return a.provenanceMetaDataFile
475}
476
Andrei Onea580636b2022-08-17 16:53:46 +0000477func (a *AndroidAppImport) PrivAppAllowlist() android.OptionalPath {
478 return android.OptionalPath{}
479}
480
Herbert Xue04354ae2024-01-29 13:57:51 +0800481const (
482 ArchGroupName = "Arch"
483 DpiGroupName = "Dpi_variants"
484)
485
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800486var dpiVariantGroupType reflect.Type
487var archVariantGroupType reflect.Type
Herbert Xue04354ae2024-01-29 13:57:51 +0800488var archdpiVariantGroupType reflect.Type
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800489var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
490
491func initAndroidAppImportVariantGroupTypes() {
Herbert Xue04354ae2024-01-29 13:57:51 +0800492 dpiVariantGroupType = createVariantGroupType(supportedDpis, DpiGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800493
494 archNames := make([]string, len(android.ArchTypeList()))
495 for i, archType := range android.ArchTypeList() {
496 archNames[i] = archType.Name
497 }
Herbert Xue04354ae2024-01-29 13:57:51 +0800498 archVariantGroupType = createVariantGroupType(archNames, ArchGroupName)
499 archdpiVariantGroupType = createArchDpiVariantGroupType(archNames, supportedDpis)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800500}
501
502// Populates all variant struct properties at creation time.
503func (a *AndroidAppImport) populateAllVariantStructs() {
504 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
505 a.AddProperties(a.dpiVariants)
506
507 a.archVariants = reflect.New(archVariantGroupType).Interface()
508 a.AddProperties(a.archVariants)
Herbert Xue04354ae2024-01-29 13:57:51 +0800509
510 a.arch_dpiVariants = reflect.New(archdpiVariantGroupType).Interface()
511 a.AddProperties(a.arch_dpiVariants)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800512}
513
514func (a *AndroidAppImport) Privileged() bool {
515 return Bool(a.properties.Privileged)
516}
517
518func (a *AndroidAppImport) DepIsInSameApex(_ android.BaseModuleContext, _ android.Module) bool {
519 // android_app_import might have extra dependencies via uses_libs property.
520 // Don't track the dependency as we don't automatically add those libraries
521 // to the classpath. It should be explicitly added to java_libs property of APEX
522 return false
523}
524
Jiyong Park92315372021-04-02 08:45:46 +0900525func (a *AndroidAppImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
526 return android.SdkSpecPrivate
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800527}
528
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000529func (a *AndroidAppImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
530 return android.SdkSpecPrivate.ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800531}
532
Colin Cross8355c152021-08-10 19:24:07 -0700533func (a *AndroidAppImport) LintDepSets() LintDepSets {
534 return LintDepSets{}
535}
536
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800537var _ android.ApexModule = (*AndroidAppImport)(nil)
538
539// Implements android.ApexModule
540func (j *AndroidAppImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
541 sdkVersion android.ApiLevel) error {
542 // Do not check for prebuilts against the min_sdk_version of enclosing APEX
543 return nil
544}
545
546func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
547 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
548
549 variantFields := make([]reflect.StructField, len(variants))
550 for i, variant := range variants {
551 variantFields[i] = reflect.StructField{
552 Name: proptools.FieldNameForProperty(variant),
553 Type: props,
554 }
555 }
556
557 variantGroupStruct := reflect.StructOf(variantFields)
558 return reflect.StructOf([]reflect.StructField{
559 {
560 Name: variantGroupName,
561 Type: variantGroupStruct,
562 },
563 })
564}
565
Herbert Xue04354ae2024-01-29 13:57:51 +0800566func createArchDpiVariantGroupType(archNames []string, dpiNames []string) reflect.Type {
567 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
568
569 dpiVariantFields := make([]reflect.StructField, len(dpiNames))
570 for i, variant_dpi := range dpiNames {
571 dpiVariantFields[i] = reflect.StructField{
572 Name: proptools.FieldNameForProperty(variant_dpi),
573 Type: props,
574 }
575 }
576 dpiVariantGroupStruct := reflect.StructOf(dpiVariantFields)
577 dpi_struct := reflect.StructOf([]reflect.StructField{
578 {
579 Name: DpiGroupName,
580 Type: reflect.PointerTo(dpiVariantGroupStruct),
581 },
582 })
583
584 archVariantFields := make([]reflect.StructField, len(archNames))
585 for i, variant_arch := range archNames {
586 archVariantFields[i] = reflect.StructField{
587 Name: proptools.FieldNameForProperty(variant_arch),
588 Type: reflect.PointerTo(dpi_struct),
589 }
590 }
591 archVariantGroupStruct := reflect.StructOf(archVariantFields)
592
593 return_struct := reflect.StructOf([]reflect.StructField{
594 {
595 Name: ArchGroupName,
596 Type: reflect.PointerTo(archVariantGroupStruct),
597 },
598 })
599 return return_struct
600}
601
Jiakai Zhangf98da192024-04-15 11:15:41 +0000602func (a *AndroidAppImport) UsesLibrary() *usesLibrary {
603 return &a.usesLibrary
604}
605
606var _ ModuleWithUsesLibrary = (*AndroidAppImport)(nil)
607
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800608// android_app_import imports a prebuilt apk with additional processing specified in the module.
609// DPI-specific apk source files can be specified using dpi_variants. Example:
610//
Colin Crossd079e0b2022-08-16 10:27:33 -0700611// android_app_import {
612// name: "example_import",
613// apk: "prebuilts/example.apk",
614// dpi_variants: {
615// mdpi: {
616// apk: "prebuilts/example_mdpi.apk",
617// },
618// xhdpi: {
619// apk: "prebuilts/example_xhdpi.apk",
620// },
621// },
622// presigned: true,
623// }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800624func AndroidAppImportFactory() android.Module {
625 module := &AndroidAppImport{}
626 module.AddProperties(&module.properties)
627 module.AddProperties(&module.dexpreoptProperties)
628 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
629 module.populateAllVariantStructs()
Cole Faust97494b12024-01-12 14:02:47 -0800630 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800631 module.processVariants(ctx)
632 })
633
634 android.InitApexModule(module)
635 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
636 android.InitDefaultableModule(module)
637 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
638
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000639 module.usesLibrary.enforce = true
640
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800641 return module
642}
643
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800644type AndroidTestImport struct {
645 AndroidAppImport
646
Jiyong Park2f83b312022-10-20 20:18:35 +0900647 testProperties struct {
648 // list of compatibility suites (for example "cts", "vts") that the module should be
649 // installed into.
650 Test_suites []string `android:"arch_variant"`
651
652 // list of files or filegroup modules that provide data that should be installed alongside
653 // the test
654 Data []string `android:"path"`
655
656 // Install the test into a folder named for the module in all test suites.
657 Per_testcase_directory *bool
658 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800659
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800660 data android.Paths
661}
662
663func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800664 a.generateAndroidBuildActions(ctx)
665
666 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
667}
668
669func (a *AndroidTestImport) InstallInTestcases() bool {
670 return true
671}
672
673// android_test_import imports a prebuilt test apk with additional processing specified in the
674// module. DPI or arch variant configurations can be made as with android_app_import.
675func AndroidTestImportFactory() android.Module {
676 module := &AndroidTestImport{}
677 module.AddProperties(&module.properties)
678 module.AddProperties(&module.dexpreoptProperties)
679 module.AddProperties(&module.testProperties)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800680 module.populateAllVariantStructs()
Cole Faust97494b12024-01-12 14:02:47 -0800681 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800682 module.processVariants(ctx)
683 })
684
685 module.dexpreopter.isTest = true
686
687 android.InitApexModule(module)
688 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
689 android.InitDefaultableModule(module)
690 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
691
692 return module
693}