blob: e25bcd1b7cd60f1582ce9e20f54cd71afe4ce71c [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"
22
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
54 checkJniAndDexLibsAreUncompressedRule = pctx.AndroidStaticRule("check-jni-and-dex-libs-are-uncompressed", blueprint.RuleParams{
55 // grep -v ' stor ' will search for lines that don't have ' stor '. stor means the file is stored uncompressed
56 Command: "if (zipinfo $in 'lib/*.so' '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then " +
57 "echo $in: Contains compressed JNI libraries and/or dex files >&2;" +
58 "exit 1; " +
59 "else " +
60 "touch $out; " +
61 "fi",
62 Description: "Check for compressed JNI libs or dex files",
63 })
Cole Faust4ec178c2023-01-13 12:03:38 -080064)
65
Jaewoong Jungf9b44652020-12-21 12:29:12 -080066func RegisterAppImportBuildComponents(ctx android.RegistrationContext) {
67 ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory)
68 ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory)
69}
70
71type AndroidAppImport struct {
72 android.ModuleBase
73 android.DefaultableModuleBase
74 android.ApexModuleBase
75 prebuilt android.Prebuilt
76
77 properties AndroidAppImportProperties
78 dpiVariants interface{}
79 archVariants interface{}
80
81 outputFile android.Path
82 certificate Certificate
83
84 dexpreopter
85
86 usesLibrary usesLibrary
87
Jaewoong Jungf9b44652020-12-21 12:29:12 -080088 installPath android.InstallPath
89
90 hideApexVariantFromMake bool
Wei Li340ee8e2022-03-18 17:33:24 -070091
92 provenanceMetaDataFile android.OutputPath
Jaewoong Jungf9b44652020-12-21 12:29:12 -080093}
94
95type AndroidAppImportProperties struct {
96 // A prebuilt apk to import
Jooyung Hanf05ca9c2021-06-28 21:48:51 +090097 Apk *string `android:"path"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -080098
99 // The name of a certificate in the default certificate directory or an android_app_certificate
100 // module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
101 Certificate *string
102
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800103 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
104 Additional_certificates []string
105
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800106 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
107 // be set for presigned modules.
108 Presigned *bool
109
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800110 // Name of the signing certificate lineage file or filegroup module.
111 Lineage *string `android:"path"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800112
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400113 // For overriding the --rotation-min-sdk-version property of apksig
114 RotationMinSdkVersion *string
115
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800116 // Sign with the default system dev certificate. Must be used judiciously. Most imported apps
117 // need to either specify a specific certificate or be presigned.
118 Default_dev_cert *bool
119
120 // Specifies that this app should be installed to the priv-app directory,
121 // where the system will grant it additional privileges not available to
122 // normal apps.
123 Privileged *bool
124
125 // Names of modules to be overridden. Listed modules can only be other binaries
126 // (in Make or Soong).
127 // This does not completely prevent installation of the overridden binaries, but if both
128 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
129 // from PRODUCT_PACKAGES.
130 Overrides []string
131
132 // Optional name for the installed app. If unspecified, it is derived from the module name.
133 Filename *string
Bill Peckhama036da92021-01-08 16:09:09 -0800134
135 // If set, create package-export.apk, which other packages can
136 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
137 Export_package_resources *bool
Spandan Dasd1fac642021-05-18 17:01:41 +0000138
139 // Optional. Install to a subdirectory of the default install path for the module
140 Relative_install_path *string
Cole Faust2f1da162023-04-17 15:06:56 -0700141
142 // Whether the prebuilt apk can be installed without additional processing. Default is false.
143 Preprocessed *bool
144
145 // Whether or not to skip checking the preprocessed apk for proper alignment and uncompressed
146 // JNI libs and dex files. Default is false
147 Skip_preprocessed_apk_checks *bool
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800148}
149
150func (a *AndroidAppImport) IsInstallable() bool {
151 return true
152}
153
154// Updates properties with variant-specific values.
155func (a *AndroidAppImport) processVariants(ctx android.LoadHookContext) {
156 config := ctx.Config()
157
158 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants")
159 // Try DPI variant matches in the reverse-priority order so that the highest priority match
160 // overwrites everything else.
161 // TODO(jungjw): Can we optimize this by making it priority order?
162 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
163 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
164 }
165 if config.ProductAAPTPreferredConfig() != "" {
166 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
167 }
168
169 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName("Arch")
170 archType := ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType
171 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
172
173 if String(a.properties.Apk) == "" {
174 // Disable this module since the apk property is still empty after processing all matching
175 // variants. This likely means there is no matching variant, and the default variant doesn't
176 // have an apk property value either.
177 a.Disable()
178 }
179}
180
181func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
182 dst interface{}, variantGroup reflect.Value, variant string) {
183 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
184 if !src.IsValid() {
185 return
186 }
187
188 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
189 if err != nil {
190 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
191 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
192 } else {
193 panic(err)
194 }
195 }
196}
197
198func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
199 cert := android.SrcIsModule(String(a.properties.Certificate))
200 if cert != "" {
201 ctx.AddDependency(ctx.Module(), certificateTag, cert)
202 }
203
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800204 for _, cert := range a.properties.Additional_certificates {
205 cert = android.SrcIsModule(cert)
206 if cert != "" {
207 ctx.AddDependency(ctx.Module(), certificateTag, cert)
208 } else {
209 ctx.PropertyErrorf("additional_certificates",
210 `must be names of android_app_certificate modules in the form ":module"`)
211 }
212 }
213
Cole Faustd5806132023-04-13 15:43:53 -0700214 a.usesLibrary.deps(ctx, true)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800215}
216
217func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
218 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
219 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
220 // with them may invalidate pre-existing signature data.
Cole Faust2f1da162023-04-17 15:06:56 -0700221 if ctx.InstallInTestcases() && (Bool(a.properties.Presigned) || Bool(a.properties.Preprocessed)) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800222 ctx.Build(pctx, android.BuildParams{
223 Rule: android.Cp,
224 Output: outputPath,
225 Input: inputPath,
226 })
227 return
228 }
Cole Faust4ec178c2023-01-13 12:03:38 -0800229
230 ctx.Build(pctx, android.BuildParams{
231 Rule: uncompressEmbeddedJniLibsRule,
232 Input: inputPath,
233 Output: outputPath,
234 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800235}
236
237// Returns whether this module should have the dex file stored uncompressed in the APK.
238func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
Cole Faust2f1da162023-04-17 15:06:56 -0700239 if ctx.Config().UnbundledBuild() || proptools.Bool(a.properties.Preprocessed) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800240 return false
241 }
242
Ulya Trafimovich0061c0d2021-09-01 15:40:38 +0100243 // Uncompress dex in APKs of priv-apps if and only if DONT_UNCOMPRESS_PRIV_APPS_DEXS is false.
244 if a.Privileged() {
245 return ctx.Config().UncompressPrivAppDex()
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800246 }
247
248 return shouldUncompressDex(ctx, &a.dexpreopter)
249}
250
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800251func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
252 a.generateAndroidBuildActions(ctx)
253}
254
255func (a *AndroidAppImport) InstallApkName() string {
256 return a.BaseModuleName()
257}
258
259func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faustd5806132023-04-13 15:43:53 -0700260 if a.Name() == "prebuilt_framework-res" {
261 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.")
262 }
263
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800264 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
265 if !apexInfo.IsForPlatform() {
266 a.hideApexVariantFromMake = true
267 }
268
269 numCertPropsSet := 0
270 if String(a.properties.Certificate) != "" {
271 numCertPropsSet++
272 }
273 if Bool(a.properties.Presigned) {
274 numCertPropsSet++
275 }
276 if Bool(a.properties.Default_dev_cert) {
277 numCertPropsSet++
278 }
279 if numCertPropsSet != 1 {
280 ctx.ModuleErrorf("One and only one of certficate, presigned, and default_dev_cert properties must be set")
281 }
282
Sam Delmerico82602492022-06-10 17:05:42 +0000283 _, _, certificates := collectAppDeps(ctx, a, false, false)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800284
285 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
286 // TODO: LOCAL_PACKAGE_SPLITS
287
288 srcApk := a.prebuilt.SingleSourcePath(ctx)
289
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800290 // TODO: Install or embed JNI libraries
291
292 // Uncompress JNI libraries in the apk
293 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
294 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
295
Spandan Dasd1fac642021-05-18 17:01:41 +0000296 var pathFragments []string
297 relInstallPath := String(a.properties.Relative_install_path)
Bill Peckhama036da92021-01-08 16:09:09 -0800298
Cole Faustd5806132023-04-13 15:43:53 -0700299 if Bool(a.properties.Privileged) {
Spandan Dasd1fac642021-05-18 17:01:41 +0000300 pathFragments = []string{"priv-app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800301 } else if ctx.InstallInTestcases() {
Spandan Dasd1fac642021-05-18 17:01:41 +0000302 pathFragments = []string{relInstallPath, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800303 } else {
Spandan Dasd1fac642021-05-18 17:01:41 +0000304 pathFragments = []string{"app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800305 }
306
Spandan Dasd1fac642021-05-18 17:01:41 +0000307 installDir := android.PathForModuleInstall(ctx, pathFragments...)
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000308 a.dexpreopter.isApp = true
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800309 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
310 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
311 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
312
313 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
314 a.dexpreopter.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
315
Ulya Trafimovichfe927a22021-02-26 14:36:48 +0000316 if a.usesLibrary.enforceUsesLibraries() {
Cole Faust2f1da162023-04-17 15:06:56 -0700317 a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk)
Ulya Trafimovichfe927a22021-02-26 14:36:48 +0000318 }
319
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800320 a.dexpreopter.dexpreopt(ctx, jnisUncompressed)
321 if a.dexpreopter.uncompressedDex {
322 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
Cole Faust4ec178c2023-01-13 12:03:38 -0800323 ctx.Build(pctx, android.BuildParams{
324 Rule: uncompressDexRule,
325 Input: jnisUncompressed,
326 Output: dexUncompressed,
327 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800328 jnisUncompressed = dexUncompressed
329 }
330
331 apkFilename := proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk")
332
333 // TODO: Handle EXTERNAL
334
335 // Sign or align the package if package has not been preprocessed
Bill Peckhama036da92021-01-08 16:09:09 -0800336
Cole Faust2f1da162023-04-17 15:06:56 -0700337 if proptools.Bool(a.properties.Preprocessed) {
Colin Cross5780d572023-07-10 15:15:27 -0700338 var output android.WritablePath
Cole Faustccb20f42023-05-04 12:38:24 -0700339 if !proptools.Bool(a.properties.Skip_preprocessed_apk_checks) {
Colin Cross5780d572023-07-10 15:15:27 -0700340 output = android.PathForModuleOut(ctx, "validated-prebuilt", apkFilename)
341 a.validatePreprocessedApk(ctx, srcApk, output)
342 } else {
343 // If using the input APK unmodified, still make a copy of it so that the output filename has the
344 // right basename.
345 output = android.PathForModuleOut(ctx, apkFilename)
346 ctx.Build(pctx, android.BuildParams{
347 Rule: android.Cp,
348 Input: srcApk,
349 Output: output,
350 })
Cole Faustccb20f42023-05-04 12:38:24 -0700351 }
Cole Faust2f1da162023-04-17 15:06:56 -0700352 a.outputFile = output
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800353 a.certificate = PresignedCertificate
354 } else if !Bool(a.properties.Presigned) {
355 // If the certificate property is empty at this point, default_dev_cert must be set to true.
356 // Which makes processMainCert's behavior for the empty cert string WAI.
Colin Crossbc2c8a72022-09-14 12:45:42 -0700357 a.certificate, certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800358 signed := android.PathForModuleOut(ctx, "signed", apkFilename)
359 var lineageFile android.Path
360 if lineage := String(a.properties.Lineage); lineage != "" {
361 lineageFile = android.PathForModuleSrc(ctx, lineage)
362 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400363
364 rotationMinSdkVersion := String(a.properties.RotationMinSdkVersion)
365
366 SignAppPackage(ctx, signed, jnisUncompressed, certificates, nil, lineageFile, rotationMinSdkVersion)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800367 a.outputFile = signed
368 } else {
369 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename)
370 TransformZipAlign(ctx, alignedApk, jnisUncompressed)
371 a.outputFile = alignedApk
372 a.certificate = PresignedCertificate
373 }
374
375 // TODO: Optionally compress the output apk.
376
377 if apexInfo.IsForPlatform() {
378 a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
Wei Li340ee8e2022-03-18 17:33:24 -0700379 artifactPath := android.PathForModuleSrc(ctx, *a.properties.Apk)
380 a.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, artifactPath, a.installPath)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800381 }
382
383 // TODO: androidmk converter jni libs
384}
385
Cole Faust2f1da162023-04-17 15:06:56 -0700386func (a *AndroidAppImport) validatePreprocessedApk(ctx android.ModuleContext, srcApk android.Path, dstApk android.WritablePath) {
387 alignmentStamp := android.PathForModuleOut(ctx, "validated-prebuilt", "alignment.stamp")
388 ctx.Build(pctx, android.BuildParams{
389 Rule: checkZipAlignment,
390 Input: srcApk,
391 Output: alignmentStamp,
392 })
393 compressionStamp := android.PathForModuleOut(ctx, "validated-prebuilt", "compression.stamp")
394 ctx.Build(pctx, android.BuildParams{
395 Rule: checkJniAndDexLibsAreUncompressedRule,
396 Input: srcApk,
397 Output: compressionStamp,
398 })
399 ctx.Build(pctx, android.BuildParams{
400 Rule: android.Cp,
401 Input: srcApk,
402 Output: dstApk,
403 Validations: []android.Path{
404 alignmentStamp,
405 compressionStamp,
406 },
407 })
408}
409
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800410func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
411 return &a.prebuilt
412}
413
414func (a *AndroidAppImport) Name() string {
415 return a.prebuilt.Name(a.ModuleBase.Name())
416}
417
418func (a *AndroidAppImport) OutputFile() android.Path {
419 return a.outputFile
420}
421
Colin Cross5368d0b2023-07-07 11:32:32 -0700422func (a *AndroidAppImport) OutputFiles(tag string) (android.Paths, error) {
423 switch tag {
424 case "":
425 return []android.Path{a.outputFile}, nil
426 default:
427 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
428 }
429}
430
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800431func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
432 return nil
433}
434
435func (a *AndroidAppImport) Certificate() Certificate {
436 return a.certificate
437}
438
Wei Li340ee8e2022-03-18 17:33:24 -0700439func (a *AndroidAppImport) ProvenanceMetaDataFile() android.OutputPath {
440 return a.provenanceMetaDataFile
441}
442
Andrei Onea580636b2022-08-17 16:53:46 +0000443func (a *AndroidAppImport) PrivAppAllowlist() android.OptionalPath {
444 return android.OptionalPath{}
445}
446
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800447var dpiVariantGroupType reflect.Type
448var archVariantGroupType reflect.Type
449var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
450
451func initAndroidAppImportVariantGroupTypes() {
452 dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants")
453
454 archNames := make([]string, len(android.ArchTypeList()))
455 for i, archType := range android.ArchTypeList() {
456 archNames[i] = archType.Name
457 }
458 archVariantGroupType = createVariantGroupType(archNames, "Arch")
459}
460
461// Populates all variant struct properties at creation time.
462func (a *AndroidAppImport) populateAllVariantStructs() {
463 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
464 a.AddProperties(a.dpiVariants)
465
466 a.archVariants = reflect.New(archVariantGroupType).Interface()
467 a.AddProperties(a.archVariants)
468}
469
470func (a *AndroidAppImport) Privileged() bool {
471 return Bool(a.properties.Privileged)
472}
473
474func (a *AndroidAppImport) DepIsInSameApex(_ android.BaseModuleContext, _ android.Module) bool {
475 // android_app_import might have extra dependencies via uses_libs property.
476 // Don't track the dependency as we don't automatically add those libraries
477 // to the classpath. It should be explicitly added to java_libs property of APEX
478 return false
479}
480
Jiyong Park92315372021-04-02 08:45:46 +0900481func (a *AndroidAppImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
482 return android.SdkSpecPrivate
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800483}
484
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000485func (a *AndroidAppImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
486 return android.SdkSpecPrivate.ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800487}
488
Colin Cross8355c152021-08-10 19:24:07 -0700489func (a *AndroidAppImport) LintDepSets() LintDepSets {
490 return LintDepSets{}
491}
492
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800493var _ android.ApexModule = (*AndroidAppImport)(nil)
494
495// Implements android.ApexModule
496func (j *AndroidAppImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
497 sdkVersion android.ApiLevel) error {
498 // Do not check for prebuilts against the min_sdk_version of enclosing APEX
499 return nil
500}
501
502func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
503 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
504
505 variantFields := make([]reflect.StructField, len(variants))
506 for i, variant := range variants {
507 variantFields[i] = reflect.StructField{
508 Name: proptools.FieldNameForProperty(variant),
509 Type: props,
510 }
511 }
512
513 variantGroupStruct := reflect.StructOf(variantFields)
514 return reflect.StructOf([]reflect.StructField{
515 {
516 Name: variantGroupName,
517 Type: variantGroupStruct,
518 },
519 })
520}
521
522// android_app_import imports a prebuilt apk with additional processing specified in the module.
523// DPI-specific apk source files can be specified using dpi_variants. Example:
524//
Colin Crossd079e0b2022-08-16 10:27:33 -0700525// android_app_import {
526// name: "example_import",
527// apk: "prebuilts/example.apk",
528// dpi_variants: {
529// mdpi: {
530// apk: "prebuilts/example_mdpi.apk",
531// },
532// xhdpi: {
533// apk: "prebuilts/example_xhdpi.apk",
534// },
535// },
536// presigned: true,
537// }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800538func AndroidAppImportFactory() android.Module {
539 module := &AndroidAppImport{}
540 module.AddProperties(&module.properties)
541 module.AddProperties(&module.dexpreoptProperties)
542 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
543 module.populateAllVariantStructs()
544 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
545 module.processVariants(ctx)
546 })
547
548 android.InitApexModule(module)
549 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
550 android.InitDefaultableModule(module)
551 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
552
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000553 module.usesLibrary.enforce = true
554
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800555 return module
556}
557
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800558type AndroidTestImport struct {
559 AndroidAppImport
560
Jiyong Park2f83b312022-10-20 20:18:35 +0900561 testProperties struct {
562 // list of compatibility suites (for example "cts", "vts") that the module should be
563 // installed into.
564 Test_suites []string `android:"arch_variant"`
565
566 // list of files or filegroup modules that provide data that should be installed alongside
567 // the test
568 Data []string `android:"path"`
569
570 // Install the test into a folder named for the module in all test suites.
571 Per_testcase_directory *bool
572 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800573
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800574 data android.Paths
575}
576
577func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800578 a.generateAndroidBuildActions(ctx)
579
580 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
581}
582
583func (a *AndroidTestImport) InstallInTestcases() bool {
584 return true
585}
586
587// android_test_import imports a prebuilt test apk with additional processing specified in the
588// module. DPI or arch variant configurations can be made as with android_app_import.
589func AndroidTestImportFactory() android.Module {
590 module := &AndroidTestImport{}
591 module.AddProperties(&module.properties)
592 module.AddProperties(&module.dexpreoptProperties)
593 module.AddProperties(&module.testProperties)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800594 module.populateAllVariantStructs()
595 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
596 module.processVariants(ctx)
597 })
598
599 module.dexpreopter.isTest = true
600
601 android.InitApexModule(module)
602 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
603 android.InitDefaultableModule(module)
604 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
605
606 return module
607}