blob: 12ead0aa23f4d4aa880b357a9d7ec26f5f357b6a [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 (
Colin Cross5368d0b2023-07-07 11:32:32 -070020 "fmt"
Jaewoong Jungf9b44652020-12-21 12:29:12 -080021 "reflect"
Cole Faust9c5c09f2023-09-06 16:11:44 -070022 "strings"
Jaewoong Jungf9b44652020-12-21 12:29:12 -080023
Cole Faustd5806132023-04-13 15:43:53 -070024 "github.com/google/blueprint"
25
Jaewoong Jungf9b44652020-12-21 12:29:12 -080026 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
Wei Li340ee8e2022-03-18 17:33:24 -070029 "android/soong/provenance"
Jaewoong Jungf9b44652020-12-21 12:29:12 -080030)
31
32func init() {
33 RegisterAppImportBuildComponents(android.InitRegistrationContext)
34
35 initAndroidAppImportVariantGroupTypes()
36}
37
Cole Faust4ec178c2023-01-13 12:03:38 -080038var (
39 uncompressEmbeddedJniLibsRule = pctx.AndroidStaticRule("uncompress-embedded-jni-libs", blueprint.RuleParams{
40 Command: `if (zipinfo $in 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then ` +
41 `${config.Zip2ZipCmd} -i $in -o $out -0 'lib/**/*.so'` +
42 `; else cp -f $in $out; fi`,
43 CommandDeps: []string{"${config.Zip2ZipCmd}"},
44 Description: "Uncompress embedded JNI libs",
45 })
46
47 uncompressDexRule = pctx.AndroidStaticRule("uncompress-dex", blueprint.RuleParams{
48 Command: `if (zipinfo $in '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then ` +
49 `${config.Zip2ZipCmd} -i $in -o $out -0 'classes*.dex'` +
50 `; else cp -f $in $out; fi`,
51 CommandDeps: []string{"${config.Zip2ZipCmd}"},
52 Description: "Uncompress dex files",
53 })
Cole Faust2f1da162023-04-17 15:06:56 -070054
Cole Faust9c5c09f2023-09-06 16:11:44 -070055 checkPresignedApkRule = pctx.AndroidStaticRule("check-presigned-apk", blueprint.RuleParams{
56 Command: "build/soong/scripts/check_prebuilt_presigned_apk.py --aapt2 ${config.Aapt2Cmd} --zipalign ${config.ZipAlign} $extraArgs $in $out",
57 CommandDeps: []string{"build/soong/scripts/check_prebuilt_presigned_apk.py", "${config.Aapt2Cmd}", "${config.ZipAlign}"},
58 Description: "Check presigned apk",
59 }, "extraArgs")
Cole Faust4ec178c2023-01-13 12:03:38 -080060)
61
Jaewoong Jungf9b44652020-12-21 12:29:12 -080062func RegisterAppImportBuildComponents(ctx android.RegistrationContext) {
63 ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory)
64 ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory)
65}
66
67type AndroidAppImport struct {
68 android.ModuleBase
69 android.DefaultableModuleBase
70 android.ApexModuleBase
71 prebuilt android.Prebuilt
72
73 properties AndroidAppImportProperties
74 dpiVariants interface{}
75 archVariants interface{}
76
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
LaMont Jonesafe7baf2024-01-09 22:47:39 +000089
90 // Single aconfig "cache file" merged from this module and all dependencies.
91 mergedAconfigFiles map[string]android.Paths
Jaewoong Jungf9b44652020-12-21 12:29:12 -080092}
93
94type AndroidAppImportProperties struct {
95 // A prebuilt apk to import
Jooyung Hanf05ca9c2021-06-28 21:48:51 +090096 Apk *string `android:"path"`
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
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800147}
148
149func (a *AndroidAppImport) IsInstallable() bool {
150 return true
151}
152
153// Updates properties with variant-specific values.
Cole Faust97494b12024-01-12 14:02:47 -0800154// This happens as a DefaultableHook instead of a LoadHook because we want to run it after
155// soong config variables are applied.
156func (a *AndroidAppImport) processVariants(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800157 config := ctx.Config()
158
159 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants")
160 // Try DPI variant matches in the reverse-priority order so that the highest priority match
161 // overwrites everything else.
162 // TODO(jungjw): Can we optimize this by making it priority order?
163 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
164 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
165 }
166 if config.ProductAAPTPreferredConfig() != "" {
167 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
168 }
169
170 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName("Arch")
171 archType := ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType
172 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
173
174 if String(a.properties.Apk) == "" {
175 // Disable this module since the apk property is still empty after processing all matching
176 // variants. This likely means there is no matching variant, and the default variant doesn't
177 // have an apk property value either.
178 a.Disable()
179 }
180}
181
182func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
183 dst interface{}, variantGroup reflect.Value, variant string) {
184 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
185 if !src.IsValid() {
186 return
187 }
188
189 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
190 if err != nil {
191 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
192 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
193 } else {
194 panic(err)
195 }
196 }
197}
198
199func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
200 cert := android.SrcIsModule(String(a.properties.Certificate))
201 if cert != "" {
202 ctx.AddDependency(ctx.Module(), certificateTag, cert)
203 }
204
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800205 for _, cert := range a.properties.Additional_certificates {
206 cert = android.SrcIsModule(cert)
207 if cert != "" {
208 ctx.AddDependency(ctx.Module(), certificateTag, cert)
209 } else {
210 ctx.PropertyErrorf("additional_certificates",
211 `must be names of android_app_certificate modules in the form ":module"`)
212 }
213 }
214
Cole Faustd5806132023-04-13 15:43:53 -0700215 a.usesLibrary.deps(ctx, true)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800216}
217
218func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
219 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
220 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
221 // with them may invalidate pre-existing signature data.
Cole Faust2f1da162023-04-17 15:06:56 -0700222 if ctx.InstallInTestcases() && (Bool(a.properties.Presigned) || Bool(a.properties.Preprocessed)) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800223 ctx.Build(pctx, android.BuildParams{
224 Rule: android.Cp,
225 Output: outputPath,
226 Input: inputPath,
227 })
228 return
229 }
Cole Faust4ec178c2023-01-13 12:03:38 -0800230
231 ctx.Build(pctx, android.BuildParams{
232 Rule: uncompressEmbeddedJniLibsRule,
233 Input: inputPath,
234 Output: outputPath,
235 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800236}
237
238// Returns whether this module should have the dex file stored uncompressed in the APK.
239func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
Cole Faust2f1da162023-04-17 15:06:56 -0700240 if ctx.Config().UnbundledBuild() || proptools.Bool(a.properties.Preprocessed) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800241 return false
242 }
243
Ulya Trafimovich0061c0d2021-09-01 15:40:38 +0100244 // Uncompress dex in APKs of priv-apps if and only if DONT_UNCOMPRESS_PRIV_APPS_DEXS is false.
245 if a.Privileged() {
246 return ctx.Config().UncompressPrivAppDex()
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800247 }
248
Spandan Dase21a8d42024-01-23 23:56:29 +0000249 return shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &a.dexpreopter)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800250}
251
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800252func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
253 a.generateAndroidBuildActions(ctx)
254}
255
256func (a *AndroidAppImport) InstallApkName() string {
257 return a.BaseModuleName()
258}
259
260func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faustd5806132023-04-13 15:43:53 -0700261 if a.Name() == "prebuilt_framework-res" {
262 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.")
263 }
264
Colin Crossff694a82023-12-13 15:54:49 -0800265 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800266 if !apexInfo.IsForPlatform() {
267 a.hideApexVariantFromMake = true
268 }
269
Cole Faust61585282023-07-14 16:23:39 -0700270 if Bool(a.properties.Preprocessed) {
271 if a.properties.Presigned != nil && !*a.properties.Presigned {
272 ctx.ModuleErrorf("Setting preprocessed: true implies presigned: true, so you cannot set presigned to false")
273 }
274 t := true
275 a.properties.Presigned = &t
276 }
277
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800278 numCertPropsSet := 0
279 if String(a.properties.Certificate) != "" {
280 numCertPropsSet++
281 }
282 if Bool(a.properties.Presigned) {
283 numCertPropsSet++
284 }
285 if Bool(a.properties.Default_dev_cert) {
286 numCertPropsSet++
287 }
288 if numCertPropsSet != 1 {
Cole Faust61585282023-07-14 16:23:39 -0700289 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 -0800290 }
291
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800292 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
293 // TODO: LOCAL_PACKAGE_SPLITS
294
295 srcApk := a.prebuilt.SingleSourcePath(ctx)
296
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800297 // TODO: Install or embed JNI libraries
298
299 // Uncompress JNI libraries in the apk
300 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
301 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
302
Spandan Dasd1fac642021-05-18 17:01:41 +0000303 var pathFragments []string
304 relInstallPath := String(a.properties.Relative_install_path)
Bill Peckhama036da92021-01-08 16:09:09 -0800305
Cole Faustd5806132023-04-13 15:43:53 -0700306 if Bool(a.properties.Privileged) {
Spandan Dasd1fac642021-05-18 17:01:41 +0000307 pathFragments = []string{"priv-app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800308 } else if ctx.InstallInTestcases() {
Spandan Dasd1fac642021-05-18 17:01:41 +0000309 pathFragments = []string{relInstallPath, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800310 } else {
Spandan Dasd1fac642021-05-18 17:01:41 +0000311 pathFragments = []string{"app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800312 }
313
Spandan Dasd1fac642021-05-18 17:01:41 +0000314 installDir := android.PathForModuleInstall(ctx, pathFragments...)
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000315 a.dexpreopter.isApp = true
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800316 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
317 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
318 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
319
320 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
321 a.dexpreopter.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
322
Ulya Trafimovichfe927a22021-02-26 14:36:48 +0000323 if a.usesLibrary.enforceUsesLibraries() {
Cole Faust2f1da162023-04-17 15:06:56 -0700324 a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk)
Ulya Trafimovichfe927a22021-02-26 14:36:48 +0000325 }
326
Spandan Dase21a8d42024-01-23 23:56:29 +0000327 a.dexpreopter.dexpreopt(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), jnisUncompressed)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800328 if a.dexpreopter.uncompressedDex {
329 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
Cole Faust4ec178c2023-01-13 12:03:38 -0800330 ctx.Build(pctx, android.BuildParams{
331 Rule: uncompressDexRule,
332 Input: jnisUncompressed,
333 Output: dexUncompressed,
334 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800335 jnisUncompressed = dexUncompressed
336 }
337
338 apkFilename := proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk")
339
340 // TODO: Handle EXTERNAL
341
342 // Sign or align the package if package has not been preprocessed
Bill Peckhama036da92021-01-08 16:09:09 -0800343
Cole Faust2f1da162023-04-17 15:06:56 -0700344 if proptools.Bool(a.properties.Preprocessed) {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700345 validationStamp := a.validatePresignedApk(ctx, srcApk)
346 output := android.PathForModuleOut(ctx, apkFilename)
347 ctx.Build(pctx, android.BuildParams{
348 Rule: android.Cp,
349 Input: srcApk,
350 Output: output,
351 Validation: validationStamp,
352 })
Cole Faust2f1da162023-04-17 15:06:56 -0700353 a.outputFile = output
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800354 a.certificate = PresignedCertificate
355 } else if !Bool(a.properties.Presigned) {
356 // If the certificate property is empty at this point, default_dev_cert must be set to true.
357 // Which makes processMainCert's behavior for the empty cert string WAI.
Cole Faust61585282023-07-14 16:23:39 -0700358 _, _, certificates := collectAppDeps(ctx, a, false, false)
Colin Crossbc2c8a72022-09-14 12:45:42 -0700359 a.certificate, certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800360 signed := android.PathForModuleOut(ctx, "signed", apkFilename)
361 var lineageFile android.Path
362 if lineage := String(a.properties.Lineage); lineage != "" {
363 lineageFile = android.PathForModuleSrc(ctx, lineage)
364 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400365
366 rotationMinSdkVersion := String(a.properties.RotationMinSdkVersion)
367
368 SignAppPackage(ctx, signed, jnisUncompressed, certificates, nil, lineageFile, rotationMinSdkVersion)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800369 a.outputFile = signed
370 } else {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700371 validationStamp := a.validatePresignedApk(ctx, srcApk)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800372 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename)
Cole Faust9c5c09f2023-09-06 16:11:44 -0700373 TransformZipAlign(ctx, alignedApk, jnisUncompressed, []android.Path{validationStamp})
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800374 a.outputFile = alignedApk
375 a.certificate = PresignedCertificate
376 }
377
378 // TODO: Optionally compress the output apk.
379
380 if apexInfo.IsForPlatform() {
381 a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
Wei Li340ee8e2022-03-18 17:33:24 -0700382 artifactPath := android.PathForModuleSrc(ctx, *a.properties.Apk)
383 a.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, artifactPath, a.installPath)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800384 }
LaMont Jonesafe7baf2024-01-09 22:47:39 +0000385 android.CollectDependencyAconfigFiles(ctx, &a.mergedAconfigFiles)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800386
387 // TODO: androidmk converter jni libs
388}
389
Cole Faust9c5c09f2023-09-06 16:11:44 -0700390func (a *AndroidAppImport) validatePresignedApk(ctx android.ModuleContext, srcApk android.Path) android.Path {
391 stamp := android.PathForModuleOut(ctx, "validated-prebuilt", "check.stamp")
392 var extraArgs []string
Cole Faust93b89b42023-07-20 17:31:16 -0700393 if a.Privileged() {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700394 extraArgs = append(extraArgs, "--privileged")
395 }
396 if proptools.Bool(a.properties.Skip_preprocessed_apk_checks) {
397 extraArgs = append(extraArgs, "--skip-preprocessed-apk-checks")
398 }
399 if proptools.Bool(a.properties.Preprocessed) {
400 extraArgs = append(extraArgs, "--preprocessed")
Cole Faust93b89b42023-07-20 17:31:16 -0700401 }
402
Cole Faust2f1da162023-04-17 15:06:56 -0700403 ctx.Build(pctx, android.BuildParams{
Cole Faust9c5c09f2023-09-06 16:11:44 -0700404 Rule: checkPresignedApkRule,
Cole Faust61585282023-07-14 16:23:39 -0700405 Input: srcApk,
Cole Faust9c5c09f2023-09-06 16:11:44 -0700406 Output: stamp,
407 Args: map[string]string{
408 "extraArgs": strings.Join(extraArgs, " "),
409 },
Cole Faust61585282023-07-14 16:23:39 -0700410 })
Cole Faust9c5c09f2023-09-06 16:11:44 -0700411 return stamp
Cole Faust61585282023-07-14 16:23:39 -0700412}
413
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800414func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
415 return &a.prebuilt
416}
417
418func (a *AndroidAppImport) Name() string {
419 return a.prebuilt.Name(a.ModuleBase.Name())
420}
421
422func (a *AndroidAppImport) OutputFile() android.Path {
423 return a.outputFile
424}
425
Colin Cross5368d0b2023-07-07 11:32:32 -0700426func (a *AndroidAppImport) OutputFiles(tag string) (android.Paths, error) {
427 switch tag {
428 case "":
429 return []android.Path{a.outputFile}, nil
430 default:
431 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
432 }
433}
434
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800435func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
436 return nil
437}
438
439func (a *AndroidAppImport) Certificate() Certificate {
440 return a.certificate
441}
442
Wei Li340ee8e2022-03-18 17:33:24 -0700443func (a *AndroidAppImport) ProvenanceMetaDataFile() android.OutputPath {
444 return a.provenanceMetaDataFile
445}
446
Andrei Onea580636b2022-08-17 16:53:46 +0000447func (a *AndroidAppImport) PrivAppAllowlist() android.OptionalPath {
448 return android.OptionalPath{}
449}
450
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800451var dpiVariantGroupType reflect.Type
452var archVariantGroupType reflect.Type
453var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
454
455func initAndroidAppImportVariantGroupTypes() {
456 dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants")
457
458 archNames := make([]string, len(android.ArchTypeList()))
459 for i, archType := range android.ArchTypeList() {
460 archNames[i] = archType.Name
461 }
462 archVariantGroupType = createVariantGroupType(archNames, "Arch")
463}
464
465// Populates all variant struct properties at creation time.
466func (a *AndroidAppImport) populateAllVariantStructs() {
467 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
468 a.AddProperties(a.dpiVariants)
469
470 a.archVariants = reflect.New(archVariantGroupType).Interface()
471 a.AddProperties(a.archVariants)
472}
473
474func (a *AndroidAppImport) Privileged() bool {
475 return Bool(a.properties.Privileged)
476}
477
478func (a *AndroidAppImport) DepIsInSameApex(_ android.BaseModuleContext, _ android.Module) bool {
479 // android_app_import might have extra dependencies via uses_libs property.
480 // Don't track the dependency as we don't automatically add those libraries
481 // to the classpath. It should be explicitly added to java_libs property of APEX
482 return false
483}
484
Jiyong Park92315372021-04-02 08:45:46 +0900485func (a *AndroidAppImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
486 return android.SdkSpecPrivate
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800487}
488
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000489func (a *AndroidAppImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
490 return android.SdkSpecPrivate.ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800491}
492
Colin Cross8355c152021-08-10 19:24:07 -0700493func (a *AndroidAppImport) LintDepSets() LintDepSets {
494 return LintDepSets{}
495}
496
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800497var _ android.ApexModule = (*AndroidAppImport)(nil)
498
499// Implements android.ApexModule
500func (j *AndroidAppImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
501 sdkVersion android.ApiLevel) error {
502 // Do not check for prebuilts against the min_sdk_version of enclosing APEX
503 return nil
504}
505
506func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
507 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
508
509 variantFields := make([]reflect.StructField, len(variants))
510 for i, variant := range variants {
511 variantFields[i] = reflect.StructField{
512 Name: proptools.FieldNameForProperty(variant),
513 Type: props,
514 }
515 }
516
517 variantGroupStruct := reflect.StructOf(variantFields)
518 return reflect.StructOf([]reflect.StructField{
519 {
520 Name: variantGroupName,
521 Type: variantGroupStruct,
522 },
523 })
524}
525
526// android_app_import imports a prebuilt apk with additional processing specified in the module.
527// DPI-specific apk source files can be specified using dpi_variants. Example:
528//
Colin Crossd079e0b2022-08-16 10:27:33 -0700529// android_app_import {
530// name: "example_import",
531// apk: "prebuilts/example.apk",
532// dpi_variants: {
533// mdpi: {
534// apk: "prebuilts/example_mdpi.apk",
535// },
536// xhdpi: {
537// apk: "prebuilts/example_xhdpi.apk",
538// },
539// },
540// presigned: true,
541// }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800542func AndroidAppImportFactory() android.Module {
543 module := &AndroidAppImport{}
544 module.AddProperties(&module.properties)
545 module.AddProperties(&module.dexpreoptProperties)
546 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
547 module.populateAllVariantStructs()
Cole Faust97494b12024-01-12 14:02:47 -0800548 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800549 module.processVariants(ctx)
550 })
551
552 android.InitApexModule(module)
553 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
554 android.InitDefaultableModule(module)
555 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
556
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000557 module.usesLibrary.enforce = true
558
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800559 return module
560}
561
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800562type AndroidTestImport struct {
563 AndroidAppImport
564
Jiyong Park2f83b312022-10-20 20:18:35 +0900565 testProperties struct {
566 // list of compatibility suites (for example "cts", "vts") that the module should be
567 // installed into.
568 Test_suites []string `android:"arch_variant"`
569
570 // list of files or filegroup modules that provide data that should be installed alongside
571 // the test
572 Data []string `android:"path"`
573
574 // Install the test into a folder named for the module in all test suites.
575 Per_testcase_directory *bool
576 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800577
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800578 data android.Paths
579}
580
581func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800582 a.generateAndroidBuildActions(ctx)
583
584 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
585}
586
587func (a *AndroidTestImport) InstallInTestcases() bool {
588 return true
589}
590
591// android_test_import imports a prebuilt test apk with additional processing specified in the
592// module. DPI or arch variant configurations can be made as with android_app_import.
593func AndroidTestImportFactory() android.Module {
594 module := &AndroidTestImport{}
595 module.AddProperties(&module.properties)
596 module.AddProperties(&module.dexpreoptProperties)
597 module.AddProperties(&module.testProperties)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800598 module.populateAllVariantStructs()
Cole Faust97494b12024-01-12 14:02:47 -0800599 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800600 module.processVariants(ctx)
601 })
602
603 module.dexpreopter.isTest = true
604
605 android.InitApexModule(module)
606 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
607 android.InitDefaultableModule(module)
608 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
609
610 return module
611}