blob: e20913513a3688f2c49d74362f2891e4648e9300 [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 (
20 "reflect"
Cole Faust9c5c09f2023-09-06 16:11:44 -070021 "strings"
Jaewoong Jungf9b44652020-12-21 12:29:12 -080022
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 Faust9c5c09f2023-09-06 16:11:44 -070054 checkPresignedApkRule = pctx.AndroidStaticRule("check-presigned-apk", blueprint.RuleParams{
55 Command: "build/soong/scripts/check_prebuilt_presigned_apk.py --aapt2 ${config.Aapt2Cmd} --zipalign ${config.ZipAlign} $extraArgs $in $out",
56 CommandDeps: []string{"build/soong/scripts/check_prebuilt_presigned_apk.py", "${config.Aapt2Cmd}", "${config.ZipAlign}"},
57 Description: "Check presigned apk",
58 }, "extraArgs")
Herbert Xue3b5672c2024-12-16 16:05:36 +080059
60 extractApkRule = pctx.AndroidStaticRule("extract-apk", blueprint.RuleParams{
61 Command: "unzip -p $in $extract_apk > $out",
62 Description: "Extract specific sub apk",
63 }, "extract_apk")
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)
Cole Faustc71b1752024-10-14 12:14:54 -070069 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
70 ctx.BottomUp("disable_prebuilts_without_apk", disablePrebuiltsWithoutApkMutator)
71 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -080072}
73
74type AndroidAppImport struct {
75 android.ModuleBase
76 android.DefaultableModuleBase
77 android.ApexModuleBase
78 prebuilt android.Prebuilt
79
Herbert Xue04354ae2024-01-29 13:57:51 +080080 properties AndroidAppImportProperties
81 dpiVariants interface{}
82 archVariants interface{}
83 arch_dpiVariants interface{}
Jaewoong Jungf9b44652020-12-21 12:29:12 -080084
85 outputFile android.Path
86 certificate Certificate
87
88 dexpreopter
89
90 usesLibrary usesLibrary
91
Jaewoong Jungf9b44652020-12-21 12:29:12 -080092 installPath android.InstallPath
93
94 hideApexVariantFromMake bool
Wei Li340ee8e2022-03-18 17:33:24 -070095
Cole Faust4e9f5922024-11-13 16:09:23 -080096 provenanceMetaDataFile android.Path
Jaewoong Jungf9b44652020-12-21 12:29:12 -080097}
98
99type AndroidAppImportProperties struct {
100 // A prebuilt apk to import
Cole Faustc71b1752024-10-14 12:14:54 -0700101 Apk proptools.Configurable[string] `android:"path,replace_instead_of_append"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800102
103 // The name of a certificate in the default certificate directory or an android_app_certificate
104 // module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
Cole Fausteb9c1482024-11-18 16:49:19 -0800105 Certificate proptools.Configurable[string] `android:"replace_instead_of_append"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800106
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800107 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
108 Additional_certificates []string
109
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800110 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
111 // be set for presigned modules.
112 Presigned *bool
113
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800114 // Name of the signing certificate lineage file or filegroup module.
115 Lineage *string `android:"path"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800116
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400117 // For overriding the --rotation-min-sdk-version property of apksig
118 RotationMinSdkVersion *string
119
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800120 // Sign with the default system dev certificate. Must be used judiciously. Most imported apps
121 // need to either specify a specific certificate or be presigned.
122 Default_dev_cert *bool
123
124 // Specifies that this app should be installed to the priv-app directory,
125 // where the system will grant it additional privileges not available to
126 // normal apps.
127 Privileged *bool
128
129 // Names of modules to be overridden. Listed modules can only be other binaries
130 // (in Make or Soong).
131 // This does not completely prevent installation of the overridden binaries, but if both
132 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
133 // from PRODUCT_PACKAGES.
134 Overrides []string
135
136 // Optional name for the installed app. If unspecified, it is derived from the module name.
137 Filename *string
Bill Peckhama036da92021-01-08 16:09:09 -0800138
139 // If set, create package-export.apk, which other packages can
140 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
141 Export_package_resources *bool
Spandan Dasd1fac642021-05-18 17:01:41 +0000142
143 // Optional. Install to a subdirectory of the default install path for the module
144 Relative_install_path *string
Cole Faust2f1da162023-04-17 15:06:56 -0700145
146 // Whether the prebuilt apk can be installed without additional processing. Default is false.
147 Preprocessed *bool
148
149 // Whether or not to skip checking the preprocessed apk for proper alignment and uncompressed
150 // JNI libs and dex files. Default is false
151 Skip_preprocessed_apk_checks *bool
Spandan Dasefa14652024-02-27 18:19:16 +0000152
153 // Name of the source soong module that gets shadowed by this prebuilt
154 // If unspecified, follows the naming convention that the source module of
155 // the prebuilt is Name() without "prebuilt_" prefix
156 Source_module_name *string
Spandan Das3490dfd2024-03-11 21:37:25 +0000157
158 // Path to the .prebuilt_info file of the prebuilt app.
159 // In case of mainline modules, the .prebuilt_info file contains the build_id that was used
160 // to generate the prebuilt.
161 Prebuilt_info *string `android:"path"`
Herbert Xue3b5672c2024-12-16 16:05:36 +0800162
163 // Path of extracted apk which is extracted from prebuilt apk. Use this extracted to import.
164 Extract_apk *string
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800165}
166
167func (a *AndroidAppImport) IsInstallable() bool {
168 return true
169}
170
171// Updates properties with variant-specific values.
Cole Faust97494b12024-01-12 14:02:47 -0800172// This happens as a DefaultableHook instead of a LoadHook because we want to run it after
173// soong config variables are applied.
174func (a *AndroidAppImport) processVariants(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800175 config := ctx.Config()
Herbert Xue04354ae2024-01-29 13:57:51 +0800176 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName(DpiGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800177
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800178 // Try DPI variant matches in the reverse-priority order so that the highest priority match
179 // overwrites everything else.
180 // TODO(jungjw): Can we optimize this by making it priority order?
181 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
182 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
183 }
184 if config.ProductAAPTPreferredConfig() != "" {
185 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
186 }
Herbert Xue04354ae2024-01-29 13:57:51 +0800187 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName(ArchGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800188 archType := ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType
189 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
190
Herbert Xue04354ae2024-01-29 13:57:51 +0800191 // Process "arch" includes "dpi_variants"
192 archStructPtr := reflect.ValueOf(a.arch_dpiVariants).Elem().FieldByName(ArchGroupName)
193 if archStruct := archStructPtr.Elem(); archStruct.IsValid() {
194 archPartPropsPtr := archStruct.FieldByName(proptools.FieldNameForProperty(archType.Name))
195 if archPartProps := archPartPropsPtr.Elem(); archPartProps.IsValid() {
196 archDpiPropsPtr := archPartProps.FieldByName(DpiGroupName)
197 if archDpiProps := archDpiPropsPtr.Elem(); archDpiProps.IsValid() {
198 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
199 MergePropertiesFromVariant(ctx, &a.properties, archDpiProps, config.ProductAAPTPrebuiltDPI()[i])
200 }
201 if config.ProductAAPTPreferredConfig() != "" {
202 MergePropertiesFromVariant(ctx, &a.properties, archDpiProps, config.ProductAAPTPreferredConfig())
203 }
204 }
205 }
206 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800207}
208
209func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
210 dst interface{}, variantGroup reflect.Value, variant string) {
211 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
212 if !src.IsValid() {
213 return
214 }
215
216 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
217 if err != nil {
218 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
219 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
220 } else {
221 panic(err)
222 }
223 }
224}
225
Cole Faustc71b1752024-10-14 12:14:54 -0700226// disablePrebuiltsWithoutApkMutator is a pre-arch mutator that disables AndroidAppImport or
227// AndroidTestImport modules that don't have an apk set. We need this separate mutator instead
228// of doing it in processVariants because processVariants is a defaultable hook, and configurable
229// properties can only be evaluated after the defaults (and eventually, base configurabtion)
230// mutators.
231func disablePrebuiltsWithoutApkMutator(ctx android.BottomUpMutatorContext) {
232 switch a := ctx.Module().(type) {
233 case *AndroidAppImport:
234 if a.properties.Apk.GetOrDefault(ctx, "") == "" {
235 // Disable this module since the apk property is still empty after processing all
236 // matching variants. This likely means there is no matching variant, and the default
237 // variant doesn't have an apk property value either.
238 a.Disable()
239 }
240 case *AndroidTestImport:
241 if a.properties.Apk.GetOrDefault(ctx, "") == "" {
242 // Disable this module since the apk property is still empty after processing all
243 // matching variants. This likely means there is no matching variant, and the default
244 // variant doesn't have an apk property value either.
245 a.Disable()
246 }
247 }
248}
249
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800250func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Cole Fausteb9c1482024-11-18 16:49:19 -0800251 cert := android.SrcIsModule(a.properties.Certificate.GetOrDefault(ctx, ""))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800252 if cert != "" {
253 ctx.AddDependency(ctx.Module(), certificateTag, cert)
254 }
255
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800256 for _, cert := range a.properties.Additional_certificates {
257 cert = android.SrcIsModule(cert)
258 if cert != "" {
259 ctx.AddDependency(ctx.Module(), certificateTag, cert)
260 } else {
261 ctx.PropertyErrorf("additional_certificates",
262 `must be names of android_app_certificate modules in the form ":module"`)
263 }
264 }
265
Cole Faustd5806132023-04-13 15:43:53 -0700266 a.usesLibrary.deps(ctx, true)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800267}
268
269func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
Cole Faust4e9f5922024-11-13 16:09:23 -0800270 ctx android.ModuleContext, inputPath android.Path, outputPath android.WritablePath) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800271 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
272 // with them may invalidate pre-existing signature data.
Cole Faust2f1da162023-04-17 15:06:56 -0700273 if ctx.InstallInTestcases() && (Bool(a.properties.Presigned) || Bool(a.properties.Preprocessed)) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800274 ctx.Build(pctx, android.BuildParams{
275 Rule: android.Cp,
276 Output: outputPath,
277 Input: inputPath,
278 })
279 return
280 }
Cole Faust4ec178c2023-01-13 12:03:38 -0800281
282 ctx.Build(pctx, android.BuildParams{
283 Rule: uncompressEmbeddedJniLibsRule,
284 Input: inputPath,
285 Output: outputPath,
286 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800287}
288
Herbert Xue3b5672c2024-12-16 16:05:36 +0800289func (a *AndroidAppImport) extractSubApk(
290 ctx android.ModuleContext, inputPath android.Path, outputPath android.WritablePath) {
291 extractApkPath := *a.properties.Extract_apk
292 ctx.Build(pctx, android.BuildParams{
293 Rule: extractApkRule,
294 Input: inputPath,
295 Output: outputPath,
296 Args: map[string]string{
297 "extract_apk": extractApkPath,
298 },
299 })
300}
301
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800302// Returns whether this module should have the dex file stored uncompressed in the APK.
303func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
Cole Faust2f1da162023-04-17 15:06:56 -0700304 if ctx.Config().UnbundledBuild() || proptools.Bool(a.properties.Preprocessed) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800305 return false
306 }
307
Ulya Trafimovich0061c0d2021-09-01 15:40:38 +0100308 // Uncompress dex in APKs of priv-apps if and only if DONT_UNCOMPRESS_PRIV_APPS_DEXS is false.
309 if a.Privileged() {
310 return ctx.Config().UncompressPrivAppDex()
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800311 }
312
Spandan Dase21a8d42024-01-23 23:56:29 +0000313 return shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &a.dexpreopter)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800314}
315
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800316func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
317 a.generateAndroidBuildActions(ctx)
318}
319
320func (a *AndroidAppImport) InstallApkName() string {
321 return a.BaseModuleName()
322}
323
Spandan Dasefa14652024-02-27 18:19:16 +0000324func (a *AndroidAppImport) BaseModuleName() string {
325 return proptools.StringDefault(a.properties.Source_module_name, a.ModuleBase.Name())
326}
327
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800328func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faustd5806132023-04-13 15:43:53 -0700329 if a.Name() == "prebuilt_framework-res" {
330 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.")
331 }
332
Colin Crossff694a82023-12-13 15:54:49 -0800333 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800334 if !apexInfo.IsForPlatform() {
335 a.hideApexVariantFromMake = true
336 }
337
Cole Faust61585282023-07-14 16:23:39 -0700338 if Bool(a.properties.Preprocessed) {
339 if a.properties.Presigned != nil && !*a.properties.Presigned {
340 ctx.ModuleErrorf("Setting preprocessed: true implies presigned: true, so you cannot set presigned to false")
341 }
342 t := true
343 a.properties.Presigned = &t
344 }
345
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800346 numCertPropsSet := 0
Cole Fausteb9c1482024-11-18 16:49:19 -0800347 if a.properties.Certificate.GetOrDefault(ctx, "") != "" {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800348 numCertPropsSet++
349 }
350 if Bool(a.properties.Presigned) {
351 numCertPropsSet++
352 }
353 if Bool(a.properties.Default_dev_cert) {
354 numCertPropsSet++
355 }
356 if numCertPropsSet != 1 {
Cole Faust61585282023-07-14 16:23:39 -0700357 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 -0800358 }
359
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800360 // TODO: LOCAL_PACKAGE_SPLITS
361
362 srcApk := a.prebuilt.SingleSourcePath(ctx)
Herbert Xue3b5672c2024-12-16 16:05:36 +0800363 if a.properties.Extract_apk != nil {
364 extract_apk := android.PathForModuleOut(ctx, "extract-apk", ctx.ModuleName()+".apk")
365 a.extractSubApk(ctx, srcApk, extract_apk)
366 srcApk = extract_apk
367 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800368
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800369 // TODO: Install or embed JNI libraries
370
371 // Uncompress JNI libraries in the apk
372 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
Cole Faust4e9f5922024-11-13 16:09:23 -0800373 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800374
Spandan Dasd1fac642021-05-18 17:01:41 +0000375 var pathFragments []string
376 relInstallPath := String(a.properties.Relative_install_path)
Bill Peckhama036da92021-01-08 16:09:09 -0800377
Cole Faustd5806132023-04-13 15:43:53 -0700378 if Bool(a.properties.Privileged) {
Spandan Dasd1fac642021-05-18 17:01:41 +0000379 pathFragments = []string{"priv-app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800380 } else if ctx.InstallInTestcases() {
Spandan Dasd1fac642021-05-18 17:01:41 +0000381 pathFragments = []string{relInstallPath, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800382 } else {
Spandan Dasd1fac642021-05-18 17:01:41 +0000383 pathFragments = []string{"app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800384 }
385
Spandan Dasd1fac642021-05-18 17:01:41 +0000386 installDir := android.PathForModuleInstall(ctx, pathFragments...)
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000387 a.dexpreopter.isApp = true
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800388 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
389 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
390 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
391
Cole Faust64f2d842024-10-17 13:28:34 -0700392 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries(ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800393 a.dexpreopter.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Spandan Das0727ba72024-02-13 16:37:43 +0000394 if a.usesLibrary.shouldDisableDexpreopt {
395 a.dexpreopter.disableDexpreopt()
396 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800397
Cole Faust64f2d842024-10-17 13:28:34 -0700398 if a.usesLibrary.enforceUsesLibraries(ctx) {
Jiakai Zhangf98da192024-04-15 11:15:41 +0000399 a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk, &a.dexpreopter.classLoaderContexts)
Ulya Trafimovichfe927a22021-02-26 14:36:48 +0000400 }
401
Spandan Dase21a8d42024-01-23 23:56:29 +0000402 a.dexpreopter.dexpreopt(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), jnisUncompressed)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800403 if a.dexpreopter.uncompressedDex {
404 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
Cole Faust4ec178c2023-01-13 12:03:38 -0800405 ctx.Build(pctx, android.BuildParams{
406 Rule: uncompressDexRule,
407 Input: jnisUncompressed,
408 Output: dexUncompressed,
409 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800410 jnisUncompressed = dexUncompressed
411 }
412
413 apkFilename := proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk")
414
415 // TODO: Handle EXTERNAL
416
417 // Sign or align the package if package has not been preprocessed
Bill Peckhama036da92021-01-08 16:09:09 -0800418
Cole Faust2f1da162023-04-17 15:06:56 -0700419 if proptools.Bool(a.properties.Preprocessed) {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700420 validationStamp := a.validatePresignedApk(ctx, srcApk)
421 output := android.PathForModuleOut(ctx, apkFilename)
422 ctx.Build(pctx, android.BuildParams{
423 Rule: android.Cp,
424 Input: srcApk,
425 Output: output,
426 Validation: validationStamp,
427 })
Cole Faust2f1da162023-04-17 15:06:56 -0700428 a.outputFile = output
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800429 a.certificate = PresignedCertificate
430 } else if !Bool(a.properties.Presigned) {
431 // If the certificate property is empty at this point, default_dev_cert must be set to true.
432 // Which makes processMainCert's behavior for the empty cert string WAI.
Cole Faust61585282023-07-14 16:23:39 -0700433 _, _, certificates := collectAppDeps(ctx, a, false, false)
Cole Fausteb9c1482024-11-18 16:49:19 -0800434 a.certificate, certificates = processMainCert(a.ModuleBase, a.properties.Certificate.GetOrDefault(ctx, ""), certificates, ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800435 signed := android.PathForModuleOut(ctx, "signed", apkFilename)
436 var lineageFile android.Path
437 if lineage := String(a.properties.Lineage); lineage != "" {
438 lineageFile = android.PathForModuleSrc(ctx, lineage)
439 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400440
441 rotationMinSdkVersion := String(a.properties.RotationMinSdkVersion)
442
443 SignAppPackage(ctx, signed, jnisUncompressed, certificates, nil, lineageFile, rotationMinSdkVersion)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800444 a.outputFile = signed
445 } else {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700446 validationStamp := a.validatePresignedApk(ctx, srcApk)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800447 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename)
Cole Faust9c5c09f2023-09-06 16:11:44 -0700448 TransformZipAlign(ctx, alignedApk, jnisUncompressed, []android.Path{validationStamp})
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800449 a.outputFile = alignedApk
450 a.certificate = PresignedCertificate
451 }
452
453 // TODO: Optionally compress the output apk.
454
455 if apexInfo.IsForPlatform() {
456 a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
Cole Faustc71b1752024-10-14 12:14:54 -0700457 artifactPath := android.PathForModuleSrc(ctx, a.properties.Apk.GetOrDefault(ctx, ""))
Wei Li340ee8e2022-03-18 17:33:24 -0700458 a.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, artifactPath, a.installPath)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800459 }
460
Spandan Das3490dfd2024-03-11 21:37:25 +0000461 providePrebuiltInfo(ctx,
462 prebuiltInfoProps{
463 baseModuleName: a.BaseModuleName(),
464 isPrebuilt: true,
465 prebuiltInfo: a.properties.Prebuilt_info,
466 },
467 )
468
mrziwang68786d82024-07-09 10:41:55 -0700469 ctx.SetOutputFiles([]android.Path{a.outputFile}, "")
470
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800471 // TODO: androidmk converter jni libs
472}
473
Cole Faust9c5c09f2023-09-06 16:11:44 -0700474func (a *AndroidAppImport) validatePresignedApk(ctx android.ModuleContext, srcApk android.Path) android.Path {
475 stamp := android.PathForModuleOut(ctx, "validated-prebuilt", "check.stamp")
476 var extraArgs []string
Cole Faust93b89b42023-07-20 17:31:16 -0700477 if a.Privileged() {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700478 extraArgs = append(extraArgs, "--privileged")
Rashid Zaman3bd28702024-07-23 17:23:05 -0700479 if ctx.Config().UncompressPrivAppDex() {
480 extraArgs = append(extraArgs, "--uncompress-priv-app-dex")
481 }
Cole Faust9c5c09f2023-09-06 16:11:44 -0700482 }
483 if proptools.Bool(a.properties.Skip_preprocessed_apk_checks) {
484 extraArgs = append(extraArgs, "--skip-preprocessed-apk-checks")
485 }
486 if proptools.Bool(a.properties.Preprocessed) {
487 extraArgs = append(extraArgs, "--preprocessed")
Cole Faust93b89b42023-07-20 17:31:16 -0700488 }
489
Cole Faust2f1da162023-04-17 15:06:56 -0700490 ctx.Build(pctx, android.BuildParams{
Cole Faust9c5c09f2023-09-06 16:11:44 -0700491 Rule: checkPresignedApkRule,
Cole Faust61585282023-07-14 16:23:39 -0700492 Input: srcApk,
Cole Faust9c5c09f2023-09-06 16:11:44 -0700493 Output: stamp,
494 Args: map[string]string{
495 "extraArgs": strings.Join(extraArgs, " "),
496 },
Cole Faust61585282023-07-14 16:23:39 -0700497 })
Cole Faust9c5c09f2023-09-06 16:11:44 -0700498 return stamp
Cole Faust61585282023-07-14 16:23:39 -0700499}
500
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800501func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
502 return &a.prebuilt
503}
504
505func (a *AndroidAppImport) Name() string {
506 return a.prebuilt.Name(a.ModuleBase.Name())
507}
508
509func (a *AndroidAppImport) OutputFile() android.Path {
510 return a.outputFile
511}
512
513func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
514 return nil
515}
516
517func (a *AndroidAppImport) Certificate() Certificate {
518 return a.certificate
519}
520
Cole Faust4e9f5922024-11-13 16:09:23 -0800521func (a *AndroidAppImport) ProvenanceMetaDataFile() android.Path {
Wei Li340ee8e2022-03-18 17:33:24 -0700522 return a.provenanceMetaDataFile
523}
524
Andrei Onea580636b2022-08-17 16:53:46 +0000525func (a *AndroidAppImport) PrivAppAllowlist() android.OptionalPath {
526 return android.OptionalPath{}
527}
528
Herbert Xue04354ae2024-01-29 13:57:51 +0800529const (
530 ArchGroupName = "Arch"
531 DpiGroupName = "Dpi_variants"
532)
533
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800534var dpiVariantGroupType reflect.Type
535var archVariantGroupType reflect.Type
Herbert Xue04354ae2024-01-29 13:57:51 +0800536var archdpiVariantGroupType reflect.Type
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800537var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
538
539func initAndroidAppImportVariantGroupTypes() {
Herbert Xue04354ae2024-01-29 13:57:51 +0800540 dpiVariantGroupType = createVariantGroupType(supportedDpis, DpiGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800541
542 archNames := make([]string, len(android.ArchTypeList()))
543 for i, archType := range android.ArchTypeList() {
544 archNames[i] = archType.Name
545 }
Herbert Xue04354ae2024-01-29 13:57:51 +0800546 archVariantGroupType = createVariantGroupType(archNames, ArchGroupName)
547 archdpiVariantGroupType = createArchDpiVariantGroupType(archNames, supportedDpis)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800548}
549
550// Populates all variant struct properties at creation time.
551func (a *AndroidAppImport) populateAllVariantStructs() {
552 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
553 a.AddProperties(a.dpiVariants)
554
555 a.archVariants = reflect.New(archVariantGroupType).Interface()
556 a.AddProperties(a.archVariants)
Herbert Xue04354ae2024-01-29 13:57:51 +0800557
558 a.arch_dpiVariants = reflect.New(archdpiVariantGroupType).Interface()
559 a.AddProperties(a.arch_dpiVariants)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800560}
561
562func (a *AndroidAppImport) Privileged() bool {
563 return Bool(a.properties.Privileged)
564}
565
Colin Crossf7bbd2f2024-12-05 13:57:10 -0800566func (a *AndroidAppImport) OutgoingDepIsInSameApex(tag blueprint.DependencyTag) bool {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800567 // android_app_import might have extra dependencies via uses_libs property.
568 // Don't track the dependency as we don't automatically add those libraries
569 // to the classpath. It should be explicitly added to java_libs property of APEX
570 return false
571}
572
Jiyong Park92315372021-04-02 08:45:46 +0900573func (a *AndroidAppImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
574 return android.SdkSpecPrivate
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800575}
576
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000577func (a *AndroidAppImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
578 return android.SdkSpecPrivate.ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800579}
580
581var _ android.ApexModule = (*AndroidAppImport)(nil)
582
583// Implements android.ApexModule
584func (j *AndroidAppImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
585 sdkVersion android.ApiLevel) error {
586 // Do not check for prebuilts against the min_sdk_version of enclosing APEX
587 return nil
588}
589
590func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
591 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
592
593 variantFields := make([]reflect.StructField, len(variants))
594 for i, variant := range variants {
595 variantFields[i] = reflect.StructField{
596 Name: proptools.FieldNameForProperty(variant),
597 Type: props,
598 }
599 }
600
601 variantGroupStruct := reflect.StructOf(variantFields)
602 return reflect.StructOf([]reflect.StructField{
603 {
604 Name: variantGroupName,
605 Type: variantGroupStruct,
606 },
607 })
608}
609
Herbert Xue04354ae2024-01-29 13:57:51 +0800610func createArchDpiVariantGroupType(archNames []string, dpiNames []string) reflect.Type {
611 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
612
613 dpiVariantFields := make([]reflect.StructField, len(dpiNames))
614 for i, variant_dpi := range dpiNames {
615 dpiVariantFields[i] = reflect.StructField{
616 Name: proptools.FieldNameForProperty(variant_dpi),
617 Type: props,
618 }
619 }
620 dpiVariantGroupStruct := reflect.StructOf(dpiVariantFields)
621 dpi_struct := reflect.StructOf([]reflect.StructField{
622 {
623 Name: DpiGroupName,
624 Type: reflect.PointerTo(dpiVariantGroupStruct),
625 },
626 })
627
628 archVariantFields := make([]reflect.StructField, len(archNames))
629 for i, variant_arch := range archNames {
630 archVariantFields[i] = reflect.StructField{
631 Name: proptools.FieldNameForProperty(variant_arch),
632 Type: reflect.PointerTo(dpi_struct),
633 }
634 }
635 archVariantGroupStruct := reflect.StructOf(archVariantFields)
636
637 return_struct := reflect.StructOf([]reflect.StructField{
638 {
639 Name: ArchGroupName,
640 Type: reflect.PointerTo(archVariantGroupStruct),
641 },
642 })
643 return return_struct
644}
645
Jiakai Zhangf98da192024-04-15 11:15:41 +0000646func (a *AndroidAppImport) UsesLibrary() *usesLibrary {
647 return &a.usesLibrary
648}
649
650var _ ModuleWithUsesLibrary = (*AndroidAppImport)(nil)
651
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800652// android_app_import imports a prebuilt apk with additional processing specified in the module.
653// DPI-specific apk source files can be specified using dpi_variants. Example:
654//
Colin Crossd079e0b2022-08-16 10:27:33 -0700655// android_app_import {
656// name: "example_import",
657// apk: "prebuilts/example.apk",
658// dpi_variants: {
659// mdpi: {
660// apk: "prebuilts/example_mdpi.apk",
661// },
662// xhdpi: {
663// apk: "prebuilts/example_xhdpi.apk",
664// },
665// },
666// presigned: true,
667// }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800668func AndroidAppImportFactory() android.Module {
669 module := &AndroidAppImport{}
670 module.AddProperties(&module.properties)
671 module.AddProperties(&module.dexpreoptProperties)
672 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
673 module.populateAllVariantStructs()
Cole Faust97494b12024-01-12 14:02:47 -0800674 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800675 module.processVariants(ctx)
676 })
677
678 android.InitApexModule(module)
679 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
680 android.InitDefaultableModule(module)
Cole Faustc71b1752024-10-14 12:14:54 -0700681 android.InitConfigurablePrebuiltModuleString(module, &module.properties.Apk, "Apk")
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800682
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000683 module.usesLibrary.enforce = true
684
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800685 return module
686}
687
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800688type AndroidTestImport struct {
689 AndroidAppImport
690
Jiyong Park2f83b312022-10-20 20:18:35 +0900691 testProperties struct {
692 // list of compatibility suites (for example "cts", "vts") that the module should be
693 // installed into.
694 Test_suites []string `android:"arch_variant"`
695
696 // list of files or filegroup modules that provide data that should be installed alongside
697 // the test
698 Data []string `android:"path"`
699
700 // Install the test into a folder named for the module in all test suites.
701 Per_testcase_directory *bool
702 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800703
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800704 data android.Paths
705}
706
707func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800708 a.generateAndroidBuildActions(ctx)
709
710 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
711}
712
713func (a *AndroidTestImport) InstallInTestcases() bool {
714 return true
715}
716
717// android_test_import imports a prebuilt test apk with additional processing specified in the
718// module. DPI or arch variant configurations can be made as with android_app_import.
719func AndroidTestImportFactory() android.Module {
720 module := &AndroidTestImport{}
721 module.AddProperties(&module.properties)
722 module.AddProperties(&module.dexpreoptProperties)
723 module.AddProperties(&module.testProperties)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800724 module.populateAllVariantStructs()
Cole Faust97494b12024-01-12 14:02:47 -0800725 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800726 module.processVariants(ctx)
727 })
728
729 module.dexpreopter.isTest = true
730
731 android.InitApexModule(module)
732 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
733 android.InitDefaultableModule(module)
Cole Faustc71b1752024-10-14 12:14:54 -0700734 android.InitConfigurablePrebuiltModuleString(module, &module.properties.Apk, "Apk")
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800735
736 return module
737}