blob: ad1765e9d7f37115e3712bfd1181d3bbe163642a [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
Cole Faust93b89b42023-07-20 17:31:16 -070054 checkDexLibsAreUncompressedRule = pctx.AndroidStaticRule("check-dex-libs-are-uncompressed", blueprint.RuleParams{
Cole Faust2f1da162023-04-17 15:06:56 -070055 // grep -v ' stor ' will search for lines that don't have ' stor '. stor means the file is stored uncompressed
Cole Faust93b89b42023-07-20 17:31:16 -070056 Command: "if (zipinfo $in '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then " +
Cole Faust2f1da162023-04-17 15:06:56 -070057 "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 Faust93b89b42023-07-20 17:31:16 -070064
65 checkJniLibsAreUncompressedRule = pctx.AndroidStaticRule("check-jni-libs-are-uncompressed", blueprint.RuleParams{
66 // grep -v ' stor ' will search for lines that don't have ' stor '. stor means the file is stored uncompressed
67 Command: "if (zipinfo $in 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then " +
68 "echo $in: Contains compressed JNI libraries >&2;" +
69 "exit 1; " +
70 "else " +
71 "touch $out; " +
72 "fi",
73 Description: "Check for compressed JNI libs or dex files",
74 })
Cole Faust4ec178c2023-01-13 12:03:38 -080075)
76
Jaewoong Jungf9b44652020-12-21 12:29:12 -080077func RegisterAppImportBuildComponents(ctx android.RegistrationContext) {
78 ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory)
79 ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory)
80}
81
82type AndroidAppImport struct {
83 android.ModuleBase
84 android.DefaultableModuleBase
85 android.ApexModuleBase
86 prebuilt android.Prebuilt
87
88 properties AndroidAppImportProperties
89 dpiVariants interface{}
90 archVariants interface{}
91
92 outputFile android.Path
93 certificate Certificate
94
95 dexpreopter
96
97 usesLibrary usesLibrary
98
Jaewoong Jungf9b44652020-12-21 12:29:12 -080099 installPath android.InstallPath
100
101 hideApexVariantFromMake bool
Wei Li340ee8e2022-03-18 17:33:24 -0700102
103 provenanceMetaDataFile android.OutputPath
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800104}
105
106type AndroidAppImportProperties struct {
107 // A prebuilt apk to import
Jooyung Hanf05ca9c2021-06-28 21:48:51 +0900108 Apk *string `android:"path"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800109
110 // The name of a certificate in the default certificate directory or an android_app_certificate
111 // module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
112 Certificate *string
113
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800114 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
115 Additional_certificates []string
116
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800117 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
118 // be set for presigned modules.
119 Presigned *bool
120
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800121 // Name of the signing certificate lineage file or filegroup module.
122 Lineage *string `android:"path"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800123
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400124 // For overriding the --rotation-min-sdk-version property of apksig
125 RotationMinSdkVersion *string
126
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800127 // Sign with the default system dev certificate. Must be used judiciously. Most imported apps
128 // need to either specify a specific certificate or be presigned.
129 Default_dev_cert *bool
130
131 // Specifies that this app should be installed to the priv-app directory,
132 // where the system will grant it additional privileges not available to
133 // normal apps.
134 Privileged *bool
135
136 // Names of modules to be overridden. Listed modules can only be other binaries
137 // (in Make or Soong).
138 // This does not completely prevent installation of the overridden binaries, but if both
139 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
140 // from PRODUCT_PACKAGES.
141 Overrides []string
142
143 // Optional name for the installed app. If unspecified, it is derived from the module name.
144 Filename *string
Bill Peckhama036da92021-01-08 16:09:09 -0800145
146 // If set, create package-export.apk, which other packages can
147 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
148 Export_package_resources *bool
Spandan Dasd1fac642021-05-18 17:01:41 +0000149
150 // Optional. Install to a subdirectory of the default install path for the module
151 Relative_install_path *string
Cole Faust2f1da162023-04-17 15:06:56 -0700152
153 // Whether the prebuilt apk can be installed without additional processing. Default is false.
154 Preprocessed *bool
155
156 // Whether or not to skip checking the preprocessed apk for proper alignment and uncompressed
157 // JNI libs and dex files. Default is false
158 Skip_preprocessed_apk_checks *bool
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800159}
160
161func (a *AndroidAppImport) IsInstallable() bool {
162 return true
163}
164
165// Updates properties with variant-specific values.
166func (a *AndroidAppImport) processVariants(ctx android.LoadHookContext) {
167 config := ctx.Config()
168
169 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants")
170 // Try DPI variant matches in the reverse-priority order so that the highest priority match
171 // overwrites everything else.
172 // TODO(jungjw): Can we optimize this by making it priority order?
173 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
174 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
175 }
176 if config.ProductAAPTPreferredConfig() != "" {
177 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
178 }
179
180 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName("Arch")
181 archType := ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType
182 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
183
184 if String(a.properties.Apk) == "" {
185 // Disable this module since the apk property is still empty after processing all matching
186 // variants. This likely means there is no matching variant, and the default variant doesn't
187 // have an apk property value either.
188 a.Disable()
189 }
190}
191
192func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
193 dst interface{}, variantGroup reflect.Value, variant string) {
194 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
195 if !src.IsValid() {
196 return
197 }
198
199 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
200 if err != nil {
201 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
202 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
203 } else {
204 panic(err)
205 }
206 }
207}
208
209func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
210 cert := android.SrcIsModule(String(a.properties.Certificate))
211 if cert != "" {
212 ctx.AddDependency(ctx.Module(), certificateTag, cert)
213 }
214
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800215 for _, cert := range a.properties.Additional_certificates {
216 cert = android.SrcIsModule(cert)
217 if cert != "" {
218 ctx.AddDependency(ctx.Module(), certificateTag, cert)
219 } else {
220 ctx.PropertyErrorf("additional_certificates",
221 `must be names of android_app_certificate modules in the form ":module"`)
222 }
223 }
224
Cole Faustd5806132023-04-13 15:43:53 -0700225 a.usesLibrary.deps(ctx, true)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800226}
227
228func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
229 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
230 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
231 // with them may invalidate pre-existing signature data.
Cole Faust2f1da162023-04-17 15:06:56 -0700232 if ctx.InstallInTestcases() && (Bool(a.properties.Presigned) || Bool(a.properties.Preprocessed)) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800233 ctx.Build(pctx, android.BuildParams{
234 Rule: android.Cp,
235 Output: outputPath,
236 Input: inputPath,
237 })
238 return
239 }
Cole Faust4ec178c2023-01-13 12:03:38 -0800240
241 ctx.Build(pctx, android.BuildParams{
242 Rule: uncompressEmbeddedJniLibsRule,
243 Input: inputPath,
244 Output: outputPath,
245 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800246}
247
248// Returns whether this module should have the dex file stored uncompressed in the APK.
249func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
Cole Faust2f1da162023-04-17 15:06:56 -0700250 if ctx.Config().UnbundledBuild() || proptools.Bool(a.properties.Preprocessed) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800251 return false
252 }
253
Ulya Trafimovich0061c0d2021-09-01 15:40:38 +0100254 // Uncompress dex in APKs of priv-apps if and only if DONT_UNCOMPRESS_PRIV_APPS_DEXS is false.
255 if a.Privileged() {
256 return ctx.Config().UncompressPrivAppDex()
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800257 }
258
259 return shouldUncompressDex(ctx, &a.dexpreopter)
260}
261
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800262func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
263 a.generateAndroidBuildActions(ctx)
264}
265
266func (a *AndroidAppImport) InstallApkName() string {
267 return a.BaseModuleName()
268}
269
270func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faustd5806132023-04-13 15:43:53 -0700271 if a.Name() == "prebuilt_framework-res" {
272 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.")
273 }
274
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800275 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
276 if !apexInfo.IsForPlatform() {
277 a.hideApexVariantFromMake = true
278 }
279
280 numCertPropsSet := 0
281 if String(a.properties.Certificate) != "" {
282 numCertPropsSet++
283 }
284 if Bool(a.properties.Presigned) {
285 numCertPropsSet++
286 }
287 if Bool(a.properties.Default_dev_cert) {
288 numCertPropsSet++
289 }
290 if numCertPropsSet != 1 {
291 ctx.ModuleErrorf("One and only one of certficate, presigned, and default_dev_cert properties must be set")
292 }
293
Sam Delmerico82602492022-06-10 17:05:42 +0000294 _, _, certificates := collectAppDeps(ctx, a, false, false)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800295
296 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
297 // TODO: LOCAL_PACKAGE_SPLITS
298
299 srcApk := a.prebuilt.SingleSourcePath(ctx)
300
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800301 // TODO: Install or embed JNI libraries
302
303 // Uncompress JNI libraries in the apk
304 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
305 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
306
Spandan Dasd1fac642021-05-18 17:01:41 +0000307 var pathFragments []string
308 relInstallPath := String(a.properties.Relative_install_path)
Bill Peckhama036da92021-01-08 16:09:09 -0800309
Cole Faustd5806132023-04-13 15:43:53 -0700310 if Bool(a.properties.Privileged) {
Spandan Dasd1fac642021-05-18 17:01:41 +0000311 pathFragments = []string{"priv-app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800312 } else if ctx.InstallInTestcases() {
Spandan Dasd1fac642021-05-18 17:01:41 +0000313 pathFragments = []string{relInstallPath, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800314 } else {
Spandan Dasd1fac642021-05-18 17:01:41 +0000315 pathFragments = []string{"app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800316 }
317
Spandan Dasd1fac642021-05-18 17:01:41 +0000318 installDir := android.PathForModuleInstall(ctx, pathFragments...)
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000319 a.dexpreopter.isApp = true
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800320 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
321 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
322 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
323
324 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
325 a.dexpreopter.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
326
Ulya Trafimovichfe927a22021-02-26 14:36:48 +0000327 if a.usesLibrary.enforceUsesLibraries() {
Cole Faust2f1da162023-04-17 15:06:56 -0700328 a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk)
Ulya Trafimovichfe927a22021-02-26 14:36:48 +0000329 }
330
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800331 a.dexpreopter.dexpreopt(ctx, jnisUncompressed)
332 if a.dexpreopter.uncompressedDex {
333 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
Cole Faust4ec178c2023-01-13 12:03:38 -0800334 ctx.Build(pctx, android.BuildParams{
335 Rule: uncompressDexRule,
336 Input: jnisUncompressed,
337 Output: dexUncompressed,
338 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800339 jnisUncompressed = dexUncompressed
340 }
341
342 apkFilename := proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk")
343
344 // TODO: Handle EXTERNAL
345
346 // Sign or align the package if package has not been preprocessed
Bill Peckhama036da92021-01-08 16:09:09 -0800347
Cole Faust2f1da162023-04-17 15:06:56 -0700348 if proptools.Bool(a.properties.Preprocessed) {
Colin Cross5780d572023-07-10 15:15:27 -0700349 var output android.WritablePath
Cole Faustccb20f42023-05-04 12:38:24 -0700350 if !proptools.Bool(a.properties.Skip_preprocessed_apk_checks) {
Colin Cross5780d572023-07-10 15:15:27 -0700351 output = android.PathForModuleOut(ctx, "validated-prebuilt", apkFilename)
352 a.validatePreprocessedApk(ctx, srcApk, output)
353 } else {
354 // If using the input APK unmodified, still make a copy of it so that the output filename has the
355 // right basename.
356 output = android.PathForModuleOut(ctx, apkFilename)
357 ctx.Build(pctx, android.BuildParams{
358 Rule: android.Cp,
359 Input: srcApk,
360 Output: output,
361 })
Cole Faustccb20f42023-05-04 12:38:24 -0700362 }
Cole Faust2f1da162023-04-17 15:06:56 -0700363 a.outputFile = output
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800364 a.certificate = PresignedCertificate
365 } else if !Bool(a.properties.Presigned) {
366 // If the certificate property is empty at this point, default_dev_cert must be set to true.
367 // Which makes processMainCert's behavior for the empty cert string WAI.
Colin Crossbc2c8a72022-09-14 12:45:42 -0700368 a.certificate, certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800369 signed := android.PathForModuleOut(ctx, "signed", apkFilename)
370 var lineageFile android.Path
371 if lineage := String(a.properties.Lineage); lineage != "" {
372 lineageFile = android.PathForModuleSrc(ctx, lineage)
373 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400374
375 rotationMinSdkVersion := String(a.properties.RotationMinSdkVersion)
376
377 SignAppPackage(ctx, signed, jnisUncompressed, certificates, nil, lineageFile, rotationMinSdkVersion)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800378 a.outputFile = signed
379 } else {
380 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename)
381 TransformZipAlign(ctx, alignedApk, jnisUncompressed)
382 a.outputFile = alignedApk
383 a.certificate = PresignedCertificate
384 }
385
386 // TODO: Optionally compress the output apk.
387
388 if apexInfo.IsForPlatform() {
389 a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
Wei Li340ee8e2022-03-18 17:33:24 -0700390 artifactPath := android.PathForModuleSrc(ctx, *a.properties.Apk)
391 a.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, artifactPath, a.installPath)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800392 }
393
394 // TODO: androidmk converter jni libs
395}
396
Cole Faust2f1da162023-04-17 15:06:56 -0700397func (a *AndroidAppImport) validatePreprocessedApk(ctx android.ModuleContext, srcApk android.Path, dstApk android.WritablePath) {
Cole Faust93b89b42023-07-20 17:31:16 -0700398 var validations android.Paths
399
Cole Faust2f1da162023-04-17 15:06:56 -0700400 alignmentStamp := android.PathForModuleOut(ctx, "validated-prebuilt", "alignment.stamp")
401 ctx.Build(pctx, android.BuildParams{
402 Rule: checkZipAlignment,
403 Input: srcApk,
404 Output: alignmentStamp,
405 })
Cole Faust93b89b42023-07-20 17:31:16 -0700406
407 validations = append(validations, alignmentStamp)
408 jniCompressionStamp := android.PathForModuleOut(ctx, "validated-prebuilt", "jni_compression.stamp")
Cole Faust2f1da162023-04-17 15:06:56 -0700409 ctx.Build(pctx, android.BuildParams{
Cole Faust93b89b42023-07-20 17:31:16 -0700410 Rule: checkJniLibsAreUncompressedRule,
Cole Faust2f1da162023-04-17 15:06:56 -0700411 Input: srcApk,
Cole Faust93b89b42023-07-20 17:31:16 -0700412 Output: jniCompressionStamp,
Cole Faust2f1da162023-04-17 15:06:56 -0700413 })
Cole Faust93b89b42023-07-20 17:31:16 -0700414 validations = append(validations, jniCompressionStamp)
415
416 if a.Privileged() {
417 // It's ok for non-privileged apps to have compressed dex files, see go/gms-uncompressed-jni-slides
418 dexCompressionStamp := android.PathForModuleOut(ctx, "validated-prebuilt", "dex_compression.stamp")
419 ctx.Build(pctx, android.BuildParams{
420 Rule: checkDexLibsAreUncompressedRule,
421 Input: srcApk,
422 Output: dexCompressionStamp,
423 })
424 validations = append(validations, dexCompressionStamp)
425 }
426
Cole Faust2f1da162023-04-17 15:06:56 -0700427 ctx.Build(pctx, android.BuildParams{
Cole Faust93b89b42023-07-20 17:31:16 -0700428 Rule: android.Cp,
429 Input: srcApk,
430 Output: dstApk,
431 Validations: validations,
Cole Faust2f1da162023-04-17 15:06:56 -0700432 })
433}
434
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800435func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
436 return &a.prebuilt
437}
438
439func (a *AndroidAppImport) Name() string {
440 return a.prebuilt.Name(a.ModuleBase.Name())
441}
442
443func (a *AndroidAppImport) OutputFile() android.Path {
444 return a.outputFile
445}
446
Colin Cross5368d0b2023-07-07 11:32:32 -0700447func (a *AndroidAppImport) OutputFiles(tag string) (android.Paths, error) {
448 switch tag {
449 case "":
450 return []android.Path{a.outputFile}, nil
451 default:
452 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
453 }
454}
455
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800456func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
457 return nil
458}
459
460func (a *AndroidAppImport) Certificate() Certificate {
461 return a.certificate
462}
463
Wei Li340ee8e2022-03-18 17:33:24 -0700464func (a *AndroidAppImport) ProvenanceMetaDataFile() android.OutputPath {
465 return a.provenanceMetaDataFile
466}
467
Andrei Onea580636b2022-08-17 16:53:46 +0000468func (a *AndroidAppImport) PrivAppAllowlist() android.OptionalPath {
469 return android.OptionalPath{}
470}
471
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800472var dpiVariantGroupType reflect.Type
473var archVariantGroupType reflect.Type
474var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
475
476func initAndroidAppImportVariantGroupTypes() {
477 dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants")
478
479 archNames := make([]string, len(android.ArchTypeList()))
480 for i, archType := range android.ArchTypeList() {
481 archNames[i] = archType.Name
482 }
483 archVariantGroupType = createVariantGroupType(archNames, "Arch")
484}
485
486// Populates all variant struct properties at creation time.
487func (a *AndroidAppImport) populateAllVariantStructs() {
488 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
489 a.AddProperties(a.dpiVariants)
490
491 a.archVariants = reflect.New(archVariantGroupType).Interface()
492 a.AddProperties(a.archVariants)
493}
494
495func (a *AndroidAppImport) Privileged() bool {
496 return Bool(a.properties.Privileged)
497}
498
499func (a *AndroidAppImport) DepIsInSameApex(_ android.BaseModuleContext, _ android.Module) bool {
500 // android_app_import might have extra dependencies via uses_libs property.
501 // Don't track the dependency as we don't automatically add those libraries
502 // to the classpath. It should be explicitly added to java_libs property of APEX
503 return false
504}
505
Jiyong Park92315372021-04-02 08:45:46 +0900506func (a *AndroidAppImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
507 return android.SdkSpecPrivate
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800508}
509
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000510func (a *AndroidAppImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
511 return android.SdkSpecPrivate.ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800512}
513
Colin Cross8355c152021-08-10 19:24:07 -0700514func (a *AndroidAppImport) LintDepSets() LintDepSets {
515 return LintDepSets{}
516}
517
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800518var _ android.ApexModule = (*AndroidAppImport)(nil)
519
520// Implements android.ApexModule
521func (j *AndroidAppImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
522 sdkVersion android.ApiLevel) error {
523 // Do not check for prebuilts against the min_sdk_version of enclosing APEX
524 return nil
525}
526
527func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
528 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
529
530 variantFields := make([]reflect.StructField, len(variants))
531 for i, variant := range variants {
532 variantFields[i] = reflect.StructField{
533 Name: proptools.FieldNameForProperty(variant),
534 Type: props,
535 }
536 }
537
538 variantGroupStruct := reflect.StructOf(variantFields)
539 return reflect.StructOf([]reflect.StructField{
540 {
541 Name: variantGroupName,
542 Type: variantGroupStruct,
543 },
544 })
545}
546
547// android_app_import imports a prebuilt apk with additional processing specified in the module.
548// DPI-specific apk source files can be specified using dpi_variants. Example:
549//
Colin Crossd079e0b2022-08-16 10:27:33 -0700550// android_app_import {
551// name: "example_import",
552// apk: "prebuilts/example.apk",
553// dpi_variants: {
554// mdpi: {
555// apk: "prebuilts/example_mdpi.apk",
556// },
557// xhdpi: {
558// apk: "prebuilts/example_xhdpi.apk",
559// },
560// },
561// presigned: true,
562// }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800563func AndroidAppImportFactory() android.Module {
564 module := &AndroidAppImport{}
565 module.AddProperties(&module.properties)
566 module.AddProperties(&module.dexpreoptProperties)
567 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
568 module.populateAllVariantStructs()
569 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
570 module.processVariants(ctx)
571 })
572
573 android.InitApexModule(module)
574 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
575 android.InitDefaultableModule(module)
576 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
577
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000578 module.usesLibrary.enforce = true
579
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800580 return module
581}
582
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800583type AndroidTestImport struct {
584 AndroidAppImport
585
Jiyong Park2f83b312022-10-20 20:18:35 +0900586 testProperties struct {
587 // list of compatibility suites (for example "cts", "vts") that the module should be
588 // installed into.
589 Test_suites []string `android:"arch_variant"`
590
591 // list of files or filegroup modules that provide data that should be installed alongside
592 // the test
593 Data []string `android:"path"`
594
595 // Install the test into a folder named for the module in all test suites.
596 Per_testcase_directory *bool
597 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800598
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800599 data android.Paths
600}
601
602func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800603 a.generateAndroidBuildActions(ctx)
604
605 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
606}
607
608func (a *AndroidTestImport) InstallInTestcases() bool {
609 return true
610}
611
612// android_test_import imports a prebuilt test apk with additional processing specified in the
613// module. DPI or arch variant configurations can be made as with android_app_import.
614func AndroidTestImportFactory() android.Module {
615 module := &AndroidTestImport{}
616 module.AddProperties(&module.properties)
617 module.AddProperties(&module.dexpreoptProperties)
618 module.AddProperties(&module.testProperties)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800619 module.populateAllVariantStructs()
620 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
621 module.processVariants(ctx)
622 })
623
624 module.dexpreopter.isTest = true
625
626 android.InitApexModule(module)
627 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
628 android.InitDefaultableModule(module)
629 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
630
631 return module
632}