blob: 8c90e4c7a41b766813e892731f11845e45d5a3c4 [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
Herbert Xue04354ae2024-01-29 13:57:51 +080073 properties AndroidAppImportProperties
74 dpiVariants interface{}
75 archVariants interface{}
76 arch_dpiVariants interface{}
Jaewoong Jungf9b44652020-12-21 12:29:12 -080077
78 outputFile android.Path
79 certificate Certificate
80
81 dexpreopter
82
83 usesLibrary usesLibrary
84
Jaewoong Jungf9b44652020-12-21 12:29:12 -080085 installPath android.InstallPath
86
87 hideApexVariantFromMake bool
Wei Li340ee8e2022-03-18 17:33:24 -070088
89 provenanceMetaDataFile android.OutputPath
LaMont Jonesafe7baf2024-01-09 22:47:39 +000090
91 // Single aconfig "cache file" merged from this module and all dependencies.
92 mergedAconfigFiles map[string]android.Paths
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.
Cole Faust97494b12024-01-12 14:02:47 -0800155// This happens as a DefaultableHook instead of a LoadHook because we want to run it after
156// soong config variables are applied.
157func (a *AndroidAppImport) processVariants(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800158 config := ctx.Config()
Herbert Xue04354ae2024-01-29 13:57:51 +0800159 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName(DpiGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800160
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800161 // Try DPI variant matches in the reverse-priority order so that the highest priority match
162 // overwrites everything else.
163 // TODO(jungjw): Can we optimize this by making it priority order?
164 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
165 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
166 }
167 if config.ProductAAPTPreferredConfig() != "" {
168 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
169 }
Herbert Xue04354ae2024-01-29 13:57:51 +0800170 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName(ArchGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800171 archType := ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType
172 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
173
Herbert Xue04354ae2024-01-29 13:57:51 +0800174 // Process "arch" includes "dpi_variants"
175 archStructPtr := reflect.ValueOf(a.arch_dpiVariants).Elem().FieldByName(ArchGroupName)
176 if archStruct := archStructPtr.Elem(); archStruct.IsValid() {
177 archPartPropsPtr := archStruct.FieldByName(proptools.FieldNameForProperty(archType.Name))
178 if archPartProps := archPartPropsPtr.Elem(); archPartProps.IsValid() {
179 archDpiPropsPtr := archPartProps.FieldByName(DpiGroupName)
180 if archDpiProps := archDpiPropsPtr.Elem(); archDpiProps.IsValid() {
181 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
182 MergePropertiesFromVariant(ctx, &a.properties, archDpiProps, config.ProductAAPTPrebuiltDPI()[i])
183 }
184 if config.ProductAAPTPreferredConfig() != "" {
185 MergePropertiesFromVariant(ctx, &a.properties, archDpiProps, config.ProductAAPTPreferredConfig())
186 }
187 }
188 }
189 }
190
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800191 if String(a.properties.Apk) == "" {
192 // Disable this module since the apk property is still empty after processing all matching
193 // variants. This likely means there is no matching variant, and the default variant doesn't
194 // have an apk property value either.
195 a.Disable()
196 }
197}
198
199func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
200 dst interface{}, variantGroup reflect.Value, variant string) {
201 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
202 if !src.IsValid() {
203 return
204 }
205
206 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
207 if err != nil {
208 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
209 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
210 } else {
211 panic(err)
212 }
213 }
214}
215
216func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
217 cert := android.SrcIsModule(String(a.properties.Certificate))
218 if cert != "" {
219 ctx.AddDependency(ctx.Module(), certificateTag, cert)
220 }
221
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800222 for _, cert := range a.properties.Additional_certificates {
223 cert = android.SrcIsModule(cert)
224 if cert != "" {
225 ctx.AddDependency(ctx.Module(), certificateTag, cert)
226 } else {
227 ctx.PropertyErrorf("additional_certificates",
228 `must be names of android_app_certificate modules in the form ":module"`)
229 }
230 }
231
Cole Faustd5806132023-04-13 15:43:53 -0700232 a.usesLibrary.deps(ctx, true)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800233}
234
235func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
236 ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
237 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
238 // with them may invalidate pre-existing signature data.
Cole Faust2f1da162023-04-17 15:06:56 -0700239 if ctx.InstallInTestcases() && (Bool(a.properties.Presigned) || Bool(a.properties.Preprocessed)) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800240 ctx.Build(pctx, android.BuildParams{
241 Rule: android.Cp,
242 Output: outputPath,
243 Input: inputPath,
244 })
245 return
246 }
Cole Faust4ec178c2023-01-13 12:03:38 -0800247
248 ctx.Build(pctx, android.BuildParams{
249 Rule: uncompressEmbeddedJniLibsRule,
250 Input: inputPath,
251 Output: outputPath,
252 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800253}
254
255// Returns whether this module should have the dex file stored uncompressed in the APK.
256func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
Cole Faust2f1da162023-04-17 15:06:56 -0700257 if ctx.Config().UnbundledBuild() || proptools.Bool(a.properties.Preprocessed) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800258 return false
259 }
260
Ulya Trafimovich0061c0d2021-09-01 15:40:38 +0100261 // Uncompress dex in APKs of priv-apps if and only if DONT_UNCOMPRESS_PRIV_APPS_DEXS is false.
262 if a.Privileged() {
263 return ctx.Config().UncompressPrivAppDex()
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800264 }
265
Spandan Dase21a8d42024-01-23 23:56:29 +0000266 return shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &a.dexpreopter)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800267}
268
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800269func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
270 a.generateAndroidBuildActions(ctx)
271}
272
273func (a *AndroidAppImport) InstallApkName() string {
274 return a.BaseModuleName()
275}
276
277func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faustd5806132023-04-13 15:43:53 -0700278 if a.Name() == "prebuilt_framework-res" {
279 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.")
280 }
281
Colin Crossff694a82023-12-13 15:54:49 -0800282 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800283 if !apexInfo.IsForPlatform() {
284 a.hideApexVariantFromMake = true
285 }
286
Cole Faust61585282023-07-14 16:23:39 -0700287 if Bool(a.properties.Preprocessed) {
288 if a.properties.Presigned != nil && !*a.properties.Presigned {
289 ctx.ModuleErrorf("Setting preprocessed: true implies presigned: true, so you cannot set presigned to false")
290 }
291 t := true
292 a.properties.Presigned = &t
293 }
294
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800295 numCertPropsSet := 0
296 if String(a.properties.Certificate) != "" {
297 numCertPropsSet++
298 }
299 if Bool(a.properties.Presigned) {
300 numCertPropsSet++
301 }
302 if Bool(a.properties.Default_dev_cert) {
303 numCertPropsSet++
304 }
305 if numCertPropsSet != 1 {
Cole Faust61585282023-07-14 16:23:39 -0700306 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 -0800307 }
308
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800309 // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
310 // TODO: LOCAL_PACKAGE_SPLITS
311
312 srcApk := a.prebuilt.SingleSourcePath(ctx)
313
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800314 // TODO: Install or embed JNI libraries
315
316 // Uncompress JNI libraries in the apk
317 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
318 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
319
Spandan Dasd1fac642021-05-18 17:01:41 +0000320 var pathFragments []string
321 relInstallPath := String(a.properties.Relative_install_path)
Bill Peckhama036da92021-01-08 16:09:09 -0800322
Cole Faustd5806132023-04-13 15:43:53 -0700323 if Bool(a.properties.Privileged) {
Spandan Dasd1fac642021-05-18 17:01:41 +0000324 pathFragments = []string{"priv-app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800325 } else if ctx.InstallInTestcases() {
Spandan Dasd1fac642021-05-18 17:01:41 +0000326 pathFragments = []string{relInstallPath, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800327 } else {
Spandan Dasd1fac642021-05-18 17:01:41 +0000328 pathFragments = []string{"app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800329 }
330
Spandan Dasd1fac642021-05-18 17:01:41 +0000331 installDir := android.PathForModuleInstall(ctx, pathFragments...)
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000332 a.dexpreopter.isApp = true
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800333 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
334 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
335 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
336
337 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries()
338 a.dexpreopter.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Spandan Das0727ba72024-02-13 16:37:43 +0000339 if a.usesLibrary.shouldDisableDexpreopt {
340 a.dexpreopter.disableDexpreopt()
341 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800342
Ulya Trafimovichfe927a22021-02-26 14:36:48 +0000343 if a.usesLibrary.enforceUsesLibraries() {
Cole Faust2f1da162023-04-17 15:06:56 -0700344 a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk)
Ulya Trafimovichfe927a22021-02-26 14:36:48 +0000345 }
346
Spandan Dase21a8d42024-01-23 23:56:29 +0000347 a.dexpreopter.dexpreopt(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), jnisUncompressed)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800348 if a.dexpreopter.uncompressedDex {
349 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
Cole Faust4ec178c2023-01-13 12:03:38 -0800350 ctx.Build(pctx, android.BuildParams{
351 Rule: uncompressDexRule,
352 Input: jnisUncompressed,
353 Output: dexUncompressed,
354 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800355 jnisUncompressed = dexUncompressed
356 }
357
358 apkFilename := proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk")
359
360 // TODO: Handle EXTERNAL
361
362 // Sign or align the package if package has not been preprocessed
Bill Peckhama036da92021-01-08 16:09:09 -0800363
Cole Faust2f1da162023-04-17 15:06:56 -0700364 if proptools.Bool(a.properties.Preprocessed) {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700365 validationStamp := a.validatePresignedApk(ctx, srcApk)
366 output := android.PathForModuleOut(ctx, apkFilename)
367 ctx.Build(pctx, android.BuildParams{
368 Rule: android.Cp,
369 Input: srcApk,
370 Output: output,
371 Validation: validationStamp,
372 })
Cole Faust2f1da162023-04-17 15:06:56 -0700373 a.outputFile = output
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800374 a.certificate = PresignedCertificate
375 } else if !Bool(a.properties.Presigned) {
376 // If the certificate property is empty at this point, default_dev_cert must be set to true.
377 // Which makes processMainCert's behavior for the empty cert string WAI.
Cole Faust61585282023-07-14 16:23:39 -0700378 _, _, certificates := collectAppDeps(ctx, a, false, false)
Colin Crossbc2c8a72022-09-14 12:45:42 -0700379 a.certificate, certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800380 signed := android.PathForModuleOut(ctx, "signed", apkFilename)
381 var lineageFile android.Path
382 if lineage := String(a.properties.Lineage); lineage != "" {
383 lineageFile = android.PathForModuleSrc(ctx, lineage)
384 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400385
386 rotationMinSdkVersion := String(a.properties.RotationMinSdkVersion)
387
388 SignAppPackage(ctx, signed, jnisUncompressed, certificates, nil, lineageFile, rotationMinSdkVersion)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800389 a.outputFile = signed
390 } else {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700391 validationStamp := a.validatePresignedApk(ctx, srcApk)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800392 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename)
Cole Faust9c5c09f2023-09-06 16:11:44 -0700393 TransformZipAlign(ctx, alignedApk, jnisUncompressed, []android.Path{validationStamp})
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800394 a.outputFile = alignedApk
395 a.certificate = PresignedCertificate
396 }
397
398 // TODO: Optionally compress the output apk.
399
400 if apexInfo.IsForPlatform() {
401 a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
Wei Li340ee8e2022-03-18 17:33:24 -0700402 artifactPath := android.PathForModuleSrc(ctx, *a.properties.Apk)
403 a.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, artifactPath, a.installPath)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800404 }
LaMont Jonesafe7baf2024-01-09 22:47:39 +0000405 android.CollectDependencyAconfigFiles(ctx, &a.mergedAconfigFiles)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800406
407 // TODO: androidmk converter jni libs
408}
409
Cole Faust9c5c09f2023-09-06 16:11:44 -0700410func (a *AndroidAppImport) validatePresignedApk(ctx android.ModuleContext, srcApk android.Path) android.Path {
411 stamp := android.PathForModuleOut(ctx, "validated-prebuilt", "check.stamp")
412 var extraArgs []string
Cole Faust93b89b42023-07-20 17:31:16 -0700413 if a.Privileged() {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700414 extraArgs = append(extraArgs, "--privileged")
415 }
416 if proptools.Bool(a.properties.Skip_preprocessed_apk_checks) {
417 extraArgs = append(extraArgs, "--skip-preprocessed-apk-checks")
418 }
419 if proptools.Bool(a.properties.Preprocessed) {
420 extraArgs = append(extraArgs, "--preprocessed")
Cole Faust93b89b42023-07-20 17:31:16 -0700421 }
422
Cole Faust2f1da162023-04-17 15:06:56 -0700423 ctx.Build(pctx, android.BuildParams{
Cole Faust9c5c09f2023-09-06 16:11:44 -0700424 Rule: checkPresignedApkRule,
Cole Faust61585282023-07-14 16:23:39 -0700425 Input: srcApk,
Cole Faust9c5c09f2023-09-06 16:11:44 -0700426 Output: stamp,
427 Args: map[string]string{
428 "extraArgs": strings.Join(extraArgs, " "),
429 },
Cole Faust61585282023-07-14 16:23:39 -0700430 })
Cole Faust9c5c09f2023-09-06 16:11:44 -0700431 return stamp
Cole Faust61585282023-07-14 16:23:39 -0700432}
433
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800434func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
435 return &a.prebuilt
436}
437
438func (a *AndroidAppImport) Name() string {
439 return a.prebuilt.Name(a.ModuleBase.Name())
440}
441
442func (a *AndroidAppImport) OutputFile() android.Path {
443 return a.outputFile
444}
445
Colin Cross5368d0b2023-07-07 11:32:32 -0700446func (a *AndroidAppImport) OutputFiles(tag string) (android.Paths, error) {
447 switch tag {
448 case "":
449 return []android.Path{a.outputFile}, nil
450 default:
451 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
452 }
453}
454
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800455func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
456 return nil
457}
458
459func (a *AndroidAppImport) Certificate() Certificate {
460 return a.certificate
461}
462
Wei Li340ee8e2022-03-18 17:33:24 -0700463func (a *AndroidAppImport) ProvenanceMetaDataFile() android.OutputPath {
464 return a.provenanceMetaDataFile
465}
466
Andrei Onea580636b2022-08-17 16:53:46 +0000467func (a *AndroidAppImport) PrivAppAllowlist() android.OptionalPath {
468 return android.OptionalPath{}
469}
470
Herbert Xue04354ae2024-01-29 13:57:51 +0800471const (
472 ArchGroupName = "Arch"
473 DpiGroupName = "Dpi_variants"
474)
475
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800476var dpiVariantGroupType reflect.Type
477var archVariantGroupType reflect.Type
Herbert Xue04354ae2024-01-29 13:57:51 +0800478var archdpiVariantGroupType reflect.Type
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800479var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
480
481func initAndroidAppImportVariantGroupTypes() {
Herbert Xue04354ae2024-01-29 13:57:51 +0800482 dpiVariantGroupType = createVariantGroupType(supportedDpis, DpiGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800483
484 archNames := make([]string, len(android.ArchTypeList()))
485 for i, archType := range android.ArchTypeList() {
486 archNames[i] = archType.Name
487 }
Herbert Xue04354ae2024-01-29 13:57:51 +0800488 archVariantGroupType = createVariantGroupType(archNames, ArchGroupName)
489 archdpiVariantGroupType = createArchDpiVariantGroupType(archNames, supportedDpis)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800490}
491
492// Populates all variant struct properties at creation time.
493func (a *AndroidAppImport) populateAllVariantStructs() {
494 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
495 a.AddProperties(a.dpiVariants)
496
497 a.archVariants = reflect.New(archVariantGroupType).Interface()
498 a.AddProperties(a.archVariants)
Herbert Xue04354ae2024-01-29 13:57:51 +0800499
500 a.arch_dpiVariants = reflect.New(archdpiVariantGroupType).Interface()
501 a.AddProperties(a.arch_dpiVariants)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800502}
503
504func (a *AndroidAppImport) Privileged() bool {
505 return Bool(a.properties.Privileged)
506}
507
508func (a *AndroidAppImport) DepIsInSameApex(_ android.BaseModuleContext, _ android.Module) bool {
509 // android_app_import might have extra dependencies via uses_libs property.
510 // Don't track the dependency as we don't automatically add those libraries
511 // to the classpath. It should be explicitly added to java_libs property of APEX
512 return false
513}
514
Jiyong Park92315372021-04-02 08:45:46 +0900515func (a *AndroidAppImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
516 return android.SdkSpecPrivate
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800517}
518
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000519func (a *AndroidAppImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
520 return android.SdkSpecPrivate.ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800521}
522
Colin Cross8355c152021-08-10 19:24:07 -0700523func (a *AndroidAppImport) LintDepSets() LintDepSets {
524 return LintDepSets{}
525}
526
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800527var _ android.ApexModule = (*AndroidAppImport)(nil)
528
529// Implements android.ApexModule
530func (j *AndroidAppImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
531 sdkVersion android.ApiLevel) error {
532 // Do not check for prebuilts against the min_sdk_version of enclosing APEX
533 return nil
534}
535
536func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
537 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
538
539 variantFields := make([]reflect.StructField, len(variants))
540 for i, variant := range variants {
541 variantFields[i] = reflect.StructField{
542 Name: proptools.FieldNameForProperty(variant),
543 Type: props,
544 }
545 }
546
547 variantGroupStruct := reflect.StructOf(variantFields)
548 return reflect.StructOf([]reflect.StructField{
549 {
550 Name: variantGroupName,
551 Type: variantGroupStruct,
552 },
553 })
554}
555
Herbert Xue04354ae2024-01-29 13:57:51 +0800556func createArchDpiVariantGroupType(archNames []string, dpiNames []string) reflect.Type {
557 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
558
559 dpiVariantFields := make([]reflect.StructField, len(dpiNames))
560 for i, variant_dpi := range dpiNames {
561 dpiVariantFields[i] = reflect.StructField{
562 Name: proptools.FieldNameForProperty(variant_dpi),
563 Type: props,
564 }
565 }
566 dpiVariantGroupStruct := reflect.StructOf(dpiVariantFields)
567 dpi_struct := reflect.StructOf([]reflect.StructField{
568 {
569 Name: DpiGroupName,
570 Type: reflect.PointerTo(dpiVariantGroupStruct),
571 },
572 })
573
574 archVariantFields := make([]reflect.StructField, len(archNames))
575 for i, variant_arch := range archNames {
576 archVariantFields[i] = reflect.StructField{
577 Name: proptools.FieldNameForProperty(variant_arch),
578 Type: reflect.PointerTo(dpi_struct),
579 }
580 }
581 archVariantGroupStruct := reflect.StructOf(archVariantFields)
582
583 return_struct := reflect.StructOf([]reflect.StructField{
584 {
585 Name: ArchGroupName,
586 Type: reflect.PointerTo(archVariantGroupStruct),
587 },
588 })
589 return return_struct
590}
591
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800592// android_app_import imports a prebuilt apk with additional processing specified in the module.
593// DPI-specific apk source files can be specified using dpi_variants. Example:
594//
Colin Crossd079e0b2022-08-16 10:27:33 -0700595// android_app_import {
596// name: "example_import",
597// apk: "prebuilts/example.apk",
598// dpi_variants: {
599// mdpi: {
600// apk: "prebuilts/example_mdpi.apk",
601// },
602// xhdpi: {
603// apk: "prebuilts/example_xhdpi.apk",
604// },
605// },
606// presigned: true,
607// }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800608func AndroidAppImportFactory() android.Module {
609 module := &AndroidAppImport{}
610 module.AddProperties(&module.properties)
611 module.AddProperties(&module.dexpreoptProperties)
612 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
613 module.populateAllVariantStructs()
Cole Faust97494b12024-01-12 14:02:47 -0800614 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800615 module.processVariants(ctx)
616 })
617
618 android.InitApexModule(module)
619 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
620 android.InitDefaultableModule(module)
621 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
622
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000623 module.usesLibrary.enforce = true
624
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800625 return module
626}
627
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800628type AndroidTestImport struct {
629 AndroidAppImport
630
Jiyong Park2f83b312022-10-20 20:18:35 +0900631 testProperties struct {
632 // list of compatibility suites (for example "cts", "vts") that the module should be
633 // installed into.
634 Test_suites []string `android:"arch_variant"`
635
636 // list of files or filegroup modules that provide data that should be installed alongside
637 // the test
638 Data []string `android:"path"`
639
640 // Install the test into a folder named for the module in all test suites.
641 Per_testcase_directory *bool
642 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800643
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800644 data android.Paths
645}
646
647func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800648 a.generateAndroidBuildActions(ctx)
649
650 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
651}
652
653func (a *AndroidTestImport) InstallInTestcases() bool {
654 return true
655}
656
657// android_test_import imports a prebuilt test apk with additional processing specified in the
658// module. DPI or arch variant configurations can be made as with android_app_import.
659func AndroidTestImportFactory() android.Module {
660 module := &AndroidTestImport{}
661 module.AddProperties(&module.properties)
662 module.AddProperties(&module.dexpreoptProperties)
663 module.AddProperties(&module.testProperties)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800664 module.populateAllVariantStructs()
Cole Faust97494b12024-01-12 14:02:47 -0800665 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800666 module.processVariants(ctx)
667 })
668
669 module.dexpreopter.isTest = true
670
671 android.InitApexModule(module)
672 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
673 android.InitDefaultableModule(module)
674 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
675
676 return module
677}