blob: ff0f5fe2f4bd09d9f6099bb9ab83a3a57951b2f9 [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
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
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800144}
145
146func (a *AndroidAppImport) IsInstallable() bool {
147 return true
148}
149
150// Updates properties with variant-specific values.
151func (a *AndroidAppImport) processVariants(ctx android.LoadHookContext) {
152 config := ctx.Config()
153
154 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants")
155 // Try DPI variant matches in the reverse-priority order so that the highest priority match
156 // overwrites everything else.
157 // TODO(jungjw): Can we optimize this by making it priority order?
158 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
159 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
160 }
161 if config.ProductAAPTPreferredConfig() != "" {
162 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
163 }
164
165 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName("Arch")
166 archType := ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType
167 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
168
169 if String(a.properties.Apk) == "" {
170 // Disable this module since the apk property is still empty after processing all matching
171 // variants. This likely means there is no matching variant, and the default variant doesn't
172 // have an apk property value either.
173 a.Disable()
174 }
175}
176
177func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
178 dst interface{}, variantGroup reflect.Value, variant string) {
179 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
180 if !src.IsValid() {
181 return
182 }
183
184 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
185 if err != nil {
186 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
187 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
188 } else {
189 panic(err)
190 }
191 }
192}
193
194func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
195 cert := android.SrcIsModule(String(a.properties.Certificate))
196 if cert != "" {
197 ctx.AddDependency(ctx.Module(), certificateTag, cert)
198 }
199
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800200 for _, cert := range a.properties.Additional_certificates {
201 cert = android.SrcIsModule(cert)
202 if cert != "" {
203 ctx.AddDependency(ctx.Module(), certificateTag, cert)
204 } else {
205 ctx.PropertyErrorf("additional_certificates",
206 `must be names of android_app_certificate modules in the form ":module"`)
207 }
208 }
209
Cole Faustd5806132023-04-13 15:43:53 -0700210 a.usesLibrary.deps(ctx, true)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800211}
212
213func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
214 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
215 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
216 // with them may invalidate pre-existing signature data.
Cole Faust2f1da162023-04-17 15:06:56 -0700217 if ctx.InstallInTestcases() && (Bool(a.properties.Presigned) || Bool(a.properties.Preprocessed)) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800218 ctx.Build(pctx, android.BuildParams{
219 Rule: android.Cp,
220 Output: outputPath,
221 Input: inputPath,
222 })
223 return
224 }
Cole Faust4ec178c2023-01-13 12:03:38 -0800225
226 ctx.Build(pctx, android.BuildParams{
227 Rule: uncompressEmbeddedJniLibsRule,
228 Input: inputPath,
229 Output: outputPath,
230 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800231}
232
233// Returns whether this module should have the dex file stored uncompressed in the APK.
234func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
Cole Faust2f1da162023-04-17 15:06:56 -0700235 if ctx.Config().UnbundledBuild() || proptools.Bool(a.properties.Preprocessed) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800236 return false
237 }
238
Ulya Trafimovich0061c0d2021-09-01 15:40:38 +0100239 // Uncompress dex in APKs of priv-apps if and only if DONT_UNCOMPRESS_PRIV_APPS_DEXS is false.
240 if a.Privileged() {
241 return ctx.Config().UncompressPrivAppDex()
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800242 }
243
244 return shouldUncompressDex(ctx, &a.dexpreopter)
245}
246
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800247func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
248 a.generateAndroidBuildActions(ctx)
249}
250
251func (a *AndroidAppImport) InstallApkName() string {
252 return a.BaseModuleName()
253}
254
255func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faustd5806132023-04-13 15:43:53 -0700256 if a.Name() == "prebuilt_framework-res" {
257 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.")
258 }
259
Colin Crossff694a82023-12-13 15:54:49 -0800260 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800261 if !apexInfo.IsForPlatform() {
262 a.hideApexVariantFromMake = true
263 }
264
Cole Faust61585282023-07-14 16:23:39 -0700265 if Bool(a.properties.Preprocessed) {
266 if a.properties.Presigned != nil && !*a.properties.Presigned {
267 ctx.ModuleErrorf("Setting preprocessed: true implies presigned: true, so you cannot set presigned to false")
268 }
269 t := true
270 a.properties.Presigned = &t
271 }
272
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800273 numCertPropsSet := 0
274 if String(a.properties.Certificate) != "" {
275 numCertPropsSet++
276 }
277 if Bool(a.properties.Presigned) {
278 numCertPropsSet++
279 }
280 if Bool(a.properties.Default_dev_cert) {
281 numCertPropsSet++
282 }
283 if numCertPropsSet != 1 {
Cole Faust61585282023-07-14 16:23:39 -0700284 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 -0800285 }
286
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800287 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
288 // TODO: LOCAL_PACKAGE_SPLITS
289
290 srcApk := a.prebuilt.SingleSourcePath(ctx)
291
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800292 // TODO: Install or embed JNI libraries
293
294 // Uncompress JNI libraries in the apk
295 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
296 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
297
Spandan Dasd1fac642021-05-18 17:01:41 +0000298 var pathFragments []string
299 relInstallPath := String(a.properties.Relative_install_path)
Bill Peckhama036da92021-01-08 16:09:09 -0800300
Cole Faustd5806132023-04-13 15:43:53 -0700301 if Bool(a.properties.Privileged) {
Spandan Dasd1fac642021-05-18 17:01:41 +0000302 pathFragments = []string{"priv-app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800303 } else if ctx.InstallInTestcases() {
Spandan Dasd1fac642021-05-18 17:01:41 +0000304 pathFragments = []string{relInstallPath, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800305 } else {
Spandan Dasd1fac642021-05-18 17:01:41 +0000306 pathFragments = []string{"app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800307 }
308
Spandan Dasd1fac642021-05-18 17:01:41 +0000309 installDir := android.PathForModuleInstall(ctx, pathFragments...)
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000310 a.dexpreopter.isApp = true
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800311 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
312 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
313 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
314
315 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
316 a.dexpreopter.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
317
Ulya Trafimovichfe927a22021-02-26 14:36:48 +0000318 if a.usesLibrary.enforceUsesLibraries() {
Cole Faust2f1da162023-04-17 15:06:56 -0700319 a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk)
Ulya Trafimovichfe927a22021-02-26 14:36:48 +0000320 }
321
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800322 a.dexpreopter.dexpreopt(ctx, jnisUncompressed)
323 if a.dexpreopter.uncompressedDex {
324 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
Cole Faust4ec178c2023-01-13 12:03:38 -0800325 ctx.Build(pctx, android.BuildParams{
326 Rule: uncompressDexRule,
327 Input: jnisUncompressed,
328 Output: dexUncompressed,
329 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800330 jnisUncompressed = dexUncompressed
331 }
332
333 apkFilename := proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk")
334
335 // TODO: Handle EXTERNAL
336
337 // Sign or align the package if package has not been preprocessed
Bill Peckhama036da92021-01-08 16:09:09 -0800338
Cole Faust2f1da162023-04-17 15:06:56 -0700339 if proptools.Bool(a.properties.Preprocessed) {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700340 validationStamp := a.validatePresignedApk(ctx, srcApk)
341 output := android.PathForModuleOut(ctx, apkFilename)
342 ctx.Build(pctx, android.BuildParams{
343 Rule: android.Cp,
344 Input: srcApk,
345 Output: output,
346 Validation: validationStamp,
347 })
Cole Faust2f1da162023-04-17 15:06:56 -0700348 a.outputFile = output
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800349 a.certificate = PresignedCertificate
350 } else if !Bool(a.properties.Presigned) {
351 // If the certificate property is empty at this point, default_dev_cert must be set to true.
352 // Which makes processMainCert's behavior for the empty cert string WAI.
Cole Faust61585282023-07-14 16:23:39 -0700353 _, _, certificates := collectAppDeps(ctx, a, false, false)
Colin Crossbc2c8a72022-09-14 12:45:42 -0700354 a.certificate, certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800355 signed := android.PathForModuleOut(ctx, "signed", apkFilename)
356 var lineageFile android.Path
357 if lineage := String(a.properties.Lineage); lineage != "" {
358 lineageFile = android.PathForModuleSrc(ctx, lineage)
359 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400360
361 rotationMinSdkVersion := String(a.properties.RotationMinSdkVersion)
362
363 SignAppPackage(ctx, signed, jnisUncompressed, certificates, nil, lineageFile, rotationMinSdkVersion)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800364 a.outputFile = signed
365 } else {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700366 validationStamp := a.validatePresignedApk(ctx, srcApk)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800367 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename)
Cole Faust9c5c09f2023-09-06 16:11:44 -0700368 TransformZipAlign(ctx, alignedApk, jnisUncompressed, []android.Path{validationStamp})
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800369 a.outputFile = alignedApk
370 a.certificate = PresignedCertificate
371 }
372
373 // TODO: Optionally compress the output apk.
374
375 if apexInfo.IsForPlatform() {
376 a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
Wei Li340ee8e2022-03-18 17:33:24 -0700377 artifactPath := android.PathForModuleSrc(ctx, *a.properties.Apk)
378 a.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, artifactPath, a.installPath)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800379 }
380
381 // TODO: androidmk converter jni libs
382}
383
Cole Faust9c5c09f2023-09-06 16:11:44 -0700384func (a *AndroidAppImport) validatePresignedApk(ctx android.ModuleContext, srcApk android.Path) android.Path {
385 stamp := android.PathForModuleOut(ctx, "validated-prebuilt", "check.stamp")
386 var extraArgs []string
Cole Faust93b89b42023-07-20 17:31:16 -0700387 if a.Privileged() {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700388 extraArgs = append(extraArgs, "--privileged")
389 }
390 if proptools.Bool(a.properties.Skip_preprocessed_apk_checks) {
391 extraArgs = append(extraArgs, "--skip-preprocessed-apk-checks")
392 }
393 if proptools.Bool(a.properties.Preprocessed) {
394 extraArgs = append(extraArgs, "--preprocessed")
Cole Faust93b89b42023-07-20 17:31:16 -0700395 }
396
Cole Faust2f1da162023-04-17 15:06:56 -0700397 ctx.Build(pctx, android.BuildParams{
Cole Faust9c5c09f2023-09-06 16:11:44 -0700398 Rule: checkPresignedApkRule,
Cole Faust61585282023-07-14 16:23:39 -0700399 Input: srcApk,
Cole Faust9c5c09f2023-09-06 16:11:44 -0700400 Output: stamp,
401 Args: map[string]string{
402 "extraArgs": strings.Join(extraArgs, " "),
403 },
Cole Faust61585282023-07-14 16:23:39 -0700404 })
Cole Faust9c5c09f2023-09-06 16:11:44 -0700405 return stamp
Cole Faust61585282023-07-14 16:23:39 -0700406}
407
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800408func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
409 return &a.prebuilt
410}
411
412func (a *AndroidAppImport) Name() string {
413 return a.prebuilt.Name(a.ModuleBase.Name())
414}
415
416func (a *AndroidAppImport) OutputFile() android.Path {
417 return a.outputFile
418}
419
Colin Cross5368d0b2023-07-07 11:32:32 -0700420func (a *AndroidAppImport) OutputFiles(tag string) (android.Paths, error) {
421 switch tag {
422 case "":
423 return []android.Path{a.outputFile}, nil
424 default:
425 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
426 }
427}
428
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800429func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
430 return nil
431}
432
433func (a *AndroidAppImport) Certificate() Certificate {
434 return a.certificate
435}
436
Wei Li340ee8e2022-03-18 17:33:24 -0700437func (a *AndroidAppImport) ProvenanceMetaDataFile() android.OutputPath {
438 return a.provenanceMetaDataFile
439}
440
Andrei Onea580636b2022-08-17 16:53:46 +0000441func (a *AndroidAppImport) PrivAppAllowlist() android.OptionalPath {
442 return android.OptionalPath{}
443}
444
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800445var dpiVariantGroupType reflect.Type
446var archVariantGroupType reflect.Type
447var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
448
449func initAndroidAppImportVariantGroupTypes() {
450 dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants")
451
452 archNames := make([]string, len(android.ArchTypeList()))
453 for i, archType := range android.ArchTypeList() {
454 archNames[i] = archType.Name
455 }
456 archVariantGroupType = createVariantGroupType(archNames, "Arch")
457}
458
459// Populates all variant struct properties at creation time.
460func (a *AndroidAppImport) populateAllVariantStructs() {
461 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
462 a.AddProperties(a.dpiVariants)
463
464 a.archVariants = reflect.New(archVariantGroupType).Interface()
465 a.AddProperties(a.archVariants)
466}
467
468func (a *AndroidAppImport) Privileged() bool {
469 return Bool(a.properties.Privileged)
470}
471
472func (a *AndroidAppImport) DepIsInSameApex(_ android.BaseModuleContext, _ android.Module) bool {
473 // android_app_import might have extra dependencies via uses_libs property.
474 // Don't track the dependency as we don't automatically add those libraries
475 // to the classpath. It should be explicitly added to java_libs property of APEX
476 return false
477}
478
Jiyong Park92315372021-04-02 08:45:46 +0900479func (a *AndroidAppImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
480 return android.SdkSpecPrivate
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800481}
482
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000483func (a *AndroidAppImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
484 return android.SdkSpecPrivate.ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800485}
486
Colin Cross8355c152021-08-10 19:24:07 -0700487func (a *AndroidAppImport) LintDepSets() LintDepSets {
488 return LintDepSets{}
489}
490
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800491var _ android.ApexModule = (*AndroidAppImport)(nil)
492
493// Implements android.ApexModule
494func (j *AndroidAppImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
495 sdkVersion android.ApiLevel) error {
496 // Do not check for prebuilts against the min_sdk_version of enclosing APEX
497 return nil
498}
499
500func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
501 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
502
503 variantFields := make([]reflect.StructField, len(variants))
504 for i, variant := range variants {
505 variantFields[i] = reflect.StructField{
506 Name: proptools.FieldNameForProperty(variant),
507 Type: props,
508 }
509 }
510
511 variantGroupStruct := reflect.StructOf(variantFields)
512 return reflect.StructOf([]reflect.StructField{
513 {
514 Name: variantGroupName,
515 Type: variantGroupStruct,
516 },
517 })
518}
519
520// android_app_import imports a prebuilt apk with additional processing specified in the module.
521// DPI-specific apk source files can be specified using dpi_variants. Example:
522//
Colin Crossd079e0b2022-08-16 10:27:33 -0700523// android_app_import {
524// name: "example_import",
525// apk: "prebuilts/example.apk",
526// dpi_variants: {
527// mdpi: {
528// apk: "prebuilts/example_mdpi.apk",
529// },
530// xhdpi: {
531// apk: "prebuilts/example_xhdpi.apk",
532// },
533// },
534// presigned: true,
535// }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800536func AndroidAppImportFactory() android.Module {
537 module := &AndroidAppImport{}
538 module.AddProperties(&module.properties)
539 module.AddProperties(&module.dexpreoptProperties)
540 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
541 module.populateAllVariantStructs()
542 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
543 module.processVariants(ctx)
544 })
545
546 android.InitApexModule(module)
547 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
548 android.InitDefaultableModule(module)
549 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
550
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000551 module.usesLibrary.enforce = true
552
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800553 return module
554}
555
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800556type AndroidTestImport struct {
557 AndroidAppImport
558
Jiyong Park2f83b312022-10-20 20:18:35 +0900559 testProperties struct {
560 // list of compatibility suites (for example "cts", "vts") that the module should be
561 // installed into.
562 Test_suites []string `android:"arch_variant"`
563
564 // list of files or filegroup modules that provide data that should be installed alongside
565 // the test
566 Data []string `android:"path"`
567
568 // Install the test into a folder named for the module in all test suites.
569 Per_testcase_directory *bool
570 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800571
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800572 data android.Paths
573}
574
575func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800576 a.generateAndroidBuildActions(ctx)
577
578 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
579}
580
581func (a *AndroidTestImport) InstallInTestcases() bool {
582 return true
583}
584
585// android_test_import imports a prebuilt test apk with additional processing specified in the
586// module. DPI or arch variant configurations can be made as with android_app_import.
587func AndroidTestImportFactory() android.Module {
588 module := &AndroidTestImport{}
589 module.AddProperties(&module.properties)
590 module.AddProperties(&module.dexpreoptProperties)
591 module.AddProperties(&module.testProperties)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800592 module.populateAllVariantStructs()
593 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
594 module.processVariants(ctx)
595 })
596
597 module.dexpreopter.isTest = true
598
599 android.InitApexModule(module)
600 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
601 android.InitDefaultableModule(module)
602 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
603
604 return module
605}