blob: 919266f2871af7184c7f271d7378bb0c741807f5 [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
Jim Tang6c4ecd02024-12-17 22:06:12 +080046 stripEmbeddedJniLibsUnusedArchRule = pctx.AndroidStaticRule("strip-embedded-jni-libs-from-unused-arch", blueprint.RuleParams{
47 Command: `${config.Zip2ZipCmd} -i $in -o $out -x 'lib/**/*.so' $extraArgs`,
48 CommandDeps: []string{"${config.Zip2ZipCmd}"},
49 Description: "Remove all JNI libs from unused architectures",
50 }, "extraArgs")
51
Cole Faust4ec178c2023-01-13 12:03:38 -080052 uncompressDexRule = pctx.AndroidStaticRule("uncompress-dex", blueprint.RuleParams{
53 Command: `if (zipinfo $in '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then ` +
54 `${config.Zip2ZipCmd} -i $in -o $out -0 'classes*.dex'` +
55 `; else cp -f $in $out; fi`,
56 CommandDeps: []string{"${config.Zip2ZipCmd}"},
57 Description: "Uncompress dex files",
58 })
Cole Faust2f1da162023-04-17 15:06:56 -070059
Cole Faust9c5c09f2023-09-06 16:11:44 -070060 checkPresignedApkRule = pctx.AndroidStaticRule("check-presigned-apk", blueprint.RuleParams{
61 Command: "build/soong/scripts/check_prebuilt_presigned_apk.py --aapt2 ${config.Aapt2Cmd} --zipalign ${config.ZipAlign} $extraArgs $in $out",
62 CommandDeps: []string{"build/soong/scripts/check_prebuilt_presigned_apk.py", "${config.Aapt2Cmd}", "${config.ZipAlign}"},
63 Description: "Check presigned apk",
64 }, "extraArgs")
Herbert Xue3b5672c2024-12-16 16:05:36 +080065
66 extractApkRule = pctx.AndroidStaticRule("extract-apk", blueprint.RuleParams{
67 Command: "unzip -p $in $extract_apk > $out",
68 Description: "Extract specific sub apk",
69 }, "extract_apk")
Nelson Lib5b45a02025-01-10 08:19:58 +000070
71 gzipRule = pctx.AndroidStaticRule("gzip",
72 blueprint.RuleParams{
73 Command: "prebuilts/build-tools/path/linux-x86/gzip -9 -c $in > $out",
74 CommandDeps: []string{"prebuilts/build-tools/path/linux-x86/gzip"},
75 Description: "gzip $out",
76 })
Cole Faust4ec178c2023-01-13 12:03:38 -080077)
78
Jaewoong Jungf9b44652020-12-21 12:29:12 -080079func RegisterAppImportBuildComponents(ctx android.RegistrationContext) {
80 ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory)
81 ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory)
Cole Faustc71b1752024-10-14 12:14:54 -070082 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
83 ctx.BottomUp("disable_prebuilts_without_apk", disablePrebuiltsWithoutApkMutator)
84 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -080085}
86
87type AndroidAppImport struct {
88 android.ModuleBase
89 android.DefaultableModuleBase
90 android.ApexModuleBase
91 prebuilt android.Prebuilt
92
Herbert Xue04354ae2024-01-29 13:57:51 +080093 properties AndroidAppImportProperties
94 dpiVariants interface{}
95 archVariants interface{}
96 arch_dpiVariants interface{}
Jaewoong Jungf9b44652020-12-21 12:29:12 -080097
98 outputFile android.Path
99 certificate Certificate
100
101 dexpreopter
102
103 usesLibrary usesLibrary
104
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800105 installPath android.InstallPath
106
107 hideApexVariantFromMake bool
Wei Li340ee8e2022-03-18 17:33:24 -0700108
Cole Faust4e9f5922024-11-13 16:09:23 -0800109 provenanceMetaDataFile android.Path
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800110}
111
112type AndroidAppImportProperties struct {
113 // A prebuilt apk to import
Cole Faustc71b1752024-10-14 12:14:54 -0700114 Apk proptools.Configurable[string] `android:"path,replace_instead_of_append"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800115
116 // The name of a certificate in the default certificate directory or an android_app_certificate
117 // module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
Cole Fausteb9c1482024-11-18 16:49:19 -0800118 Certificate proptools.Configurable[string] `android:"replace_instead_of_append"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800119
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800120 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
121 Additional_certificates []string
122
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800123 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
124 // be set for presigned modules.
125 Presigned *bool
126
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800127 // Name of the signing certificate lineage file or filegroup module.
128 Lineage *string `android:"path"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800129
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400130 // For overriding the --rotation-min-sdk-version property of apksig
131 RotationMinSdkVersion *string
132
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800133 // Sign with the default system dev certificate. Must be used judiciously. Most imported apps
134 // need to either specify a specific certificate or be presigned.
135 Default_dev_cert *bool
136
137 // Specifies that this app should be installed to the priv-app directory,
138 // where the system will grant it additional privileges not available to
139 // normal apps.
140 Privileged *bool
141
142 // Names of modules to be overridden. Listed modules can only be other binaries
143 // (in Make or Soong).
144 // This does not completely prevent installation of the overridden binaries, but if both
145 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
146 // from PRODUCT_PACKAGES.
147 Overrides []string
148
149 // Optional name for the installed app. If unspecified, it is derived from the module name.
150 Filename *string
Bill Peckhama036da92021-01-08 16:09:09 -0800151
152 // If set, create package-export.apk, which other packages can
153 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
154 Export_package_resources *bool
Spandan Dasd1fac642021-05-18 17:01:41 +0000155
156 // Optional. Install to a subdirectory of the default install path for the module
157 Relative_install_path *string
Cole Faust2f1da162023-04-17 15:06:56 -0700158
159 // Whether the prebuilt apk can be installed without additional processing. Default is false.
160 Preprocessed *bool
161
162 // Whether or not to skip checking the preprocessed apk for proper alignment and uncompressed
163 // JNI libs and dex files. Default is false
164 Skip_preprocessed_apk_checks *bool
Spandan Dasefa14652024-02-27 18:19:16 +0000165
166 // Name of the source soong module that gets shadowed by this prebuilt
167 // If unspecified, follows the naming convention that the source module of
168 // the prebuilt is Name() without "prebuilt_" prefix
169 Source_module_name *string
Spandan Das3490dfd2024-03-11 21:37:25 +0000170
Jim Tang6c4ecd02024-12-17 22:06:12 +0800171 // Whether stripping all libraries from unused architectures.
172 Strip_unused_jni_arch *bool
173
Spandan Das3490dfd2024-03-11 21:37:25 +0000174 // Path to the .prebuilt_info file of the prebuilt app.
175 // In case of mainline modules, the .prebuilt_info file contains the build_id that was used
176 // to generate the prebuilt.
177 Prebuilt_info *string `android:"path"`
Herbert Xue3b5672c2024-12-16 16:05:36 +0800178
179 // Path of extracted apk which is extracted from prebuilt apk. Use this extracted to import.
180 Extract_apk *string
Nelson Lib5b45a02025-01-10 08:19:58 +0000181
182 // Compress the output APK using gzip. Defaults to false.
183 Compress_apk proptools.Configurable[bool] `android:"arch_variant,replace_instead_of_append"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800184}
185
186func (a *AndroidAppImport) IsInstallable() bool {
187 return true
188}
189
190// Updates properties with variant-specific values.
Cole Faust97494b12024-01-12 14:02:47 -0800191// This happens as a DefaultableHook instead of a LoadHook because we want to run it after
192// soong config variables are applied.
193func (a *AndroidAppImport) processVariants(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800194 config := ctx.Config()
Herbert Xue04354ae2024-01-29 13:57:51 +0800195 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName(DpiGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800196
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800197 // Try DPI variant matches in the reverse-priority order so that the highest priority match
198 // overwrites everything else.
199 // TODO(jungjw): Can we optimize this by making it priority order?
200 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
201 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
202 }
203 if config.ProductAAPTPreferredConfig() != "" {
204 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
205 }
Herbert Xue04354ae2024-01-29 13:57:51 +0800206 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName(ArchGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800207 archType := ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType
208 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
209
Herbert Xue04354ae2024-01-29 13:57:51 +0800210 // Process "arch" includes "dpi_variants"
211 archStructPtr := reflect.ValueOf(a.arch_dpiVariants).Elem().FieldByName(ArchGroupName)
212 if archStruct := archStructPtr.Elem(); archStruct.IsValid() {
213 archPartPropsPtr := archStruct.FieldByName(proptools.FieldNameForProperty(archType.Name))
214 if archPartProps := archPartPropsPtr.Elem(); archPartProps.IsValid() {
215 archDpiPropsPtr := archPartProps.FieldByName(DpiGroupName)
216 if archDpiProps := archDpiPropsPtr.Elem(); archDpiProps.IsValid() {
217 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
218 MergePropertiesFromVariant(ctx, &a.properties, archDpiProps, config.ProductAAPTPrebuiltDPI()[i])
219 }
220 if config.ProductAAPTPreferredConfig() != "" {
221 MergePropertiesFromVariant(ctx, &a.properties, archDpiProps, config.ProductAAPTPreferredConfig())
222 }
223 }
224 }
225 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800226}
227
228func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
229 dst interface{}, variantGroup reflect.Value, variant string) {
230 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
231 if !src.IsValid() {
232 return
233 }
234
235 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
236 if err != nil {
237 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
238 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
239 } else {
240 panic(err)
241 }
242 }
243}
244
Cole Faustc71b1752024-10-14 12:14:54 -0700245// disablePrebuiltsWithoutApkMutator is a pre-arch mutator that disables AndroidAppImport or
246// AndroidTestImport modules that don't have an apk set. We need this separate mutator instead
247// of doing it in processVariants because processVariants is a defaultable hook, and configurable
248// properties can only be evaluated after the defaults (and eventually, base configurabtion)
249// mutators.
250func disablePrebuiltsWithoutApkMutator(ctx android.BottomUpMutatorContext) {
251 switch a := ctx.Module().(type) {
252 case *AndroidAppImport:
253 if a.properties.Apk.GetOrDefault(ctx, "") == "" {
254 // Disable this module since the apk property is still empty after processing all
255 // matching variants. This likely means there is no matching variant, and the default
256 // variant doesn't have an apk property value either.
257 a.Disable()
258 }
259 case *AndroidTestImport:
260 if a.properties.Apk.GetOrDefault(ctx, "") == "" {
261 // Disable this module since the apk property is still empty after processing all
262 // matching variants. This likely means there is no matching variant, and the default
263 // variant doesn't have an apk property value either.
264 a.Disable()
265 }
266 }
267}
268
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800269func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Cole Fausteb9c1482024-11-18 16:49:19 -0800270 cert := android.SrcIsModule(a.properties.Certificate.GetOrDefault(ctx, ""))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800271 if cert != "" {
272 ctx.AddDependency(ctx.Module(), certificateTag, cert)
273 }
274
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800275 for _, cert := range a.properties.Additional_certificates {
276 cert = android.SrcIsModule(cert)
277 if cert != "" {
278 ctx.AddDependency(ctx.Module(), certificateTag, cert)
279 } else {
280 ctx.PropertyErrorf("additional_certificates",
281 `must be names of android_app_certificate modules in the form ":module"`)
282 }
283 }
284
Cole Faustd5806132023-04-13 15:43:53 -0700285 a.usesLibrary.deps(ctx, true)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800286}
287
288func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
Cole Faust4e9f5922024-11-13 16:09:23 -0800289 ctx android.ModuleContext, inputPath android.Path, outputPath android.WritablePath) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800290 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
291 // with them may invalidate pre-existing signature data.
Cole Faust2f1da162023-04-17 15:06:56 -0700292 if ctx.InstallInTestcases() && (Bool(a.properties.Presigned) || Bool(a.properties.Preprocessed)) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800293 ctx.Build(pctx, android.BuildParams{
294 Rule: android.Cp,
295 Output: outputPath,
296 Input: inputPath,
297 })
298 return
299 }
Cole Faust4ec178c2023-01-13 12:03:38 -0800300
301 ctx.Build(pctx, android.BuildParams{
302 Rule: uncompressEmbeddedJniLibsRule,
303 Input: inputPath,
304 Output: outputPath,
305 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800306}
307
Herbert Xue3b5672c2024-12-16 16:05:36 +0800308func (a *AndroidAppImport) extractSubApk(
309 ctx android.ModuleContext, inputPath android.Path, outputPath android.WritablePath) {
310 extractApkPath := *a.properties.Extract_apk
311 ctx.Build(pctx, android.BuildParams{
312 Rule: extractApkRule,
313 Input: inputPath,
314 Output: outputPath,
315 Args: map[string]string{
316 "extract_apk": extractApkPath,
317 },
318 })
319}
320
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800321// Returns whether this module should have the dex file stored uncompressed in the APK.
322func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
Cole Faust2f1da162023-04-17 15:06:56 -0700323 if ctx.Config().UnbundledBuild() || proptools.Bool(a.properties.Preprocessed) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800324 return false
325 }
326
Ulya Trafimovich0061c0d2021-09-01 15:40:38 +0100327 // Uncompress dex in APKs of priv-apps if and only if DONT_UNCOMPRESS_PRIV_APPS_DEXS is false.
328 if a.Privileged() {
329 return ctx.Config().UncompressPrivAppDex()
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800330 }
331
Spandan Dase21a8d42024-01-23 23:56:29 +0000332 return shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &a.dexpreopter)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800333}
334
Jim Tang6c4ecd02024-12-17 22:06:12 +0800335func (a *AndroidAppImport) stripEmbeddedJniLibsUnusedArch(
336 ctx android.ModuleContext, inputPath android.Path, outputPath android.WritablePath) {
337 var wantedJniLibSlice []string
338 for _, target := range ctx.MultiTargets() {
339 supported_abis := target.Arch.Abi
340 for _, arch := range supported_abis {
341 wantedJniLibSlice = append(wantedJniLibSlice, " -X lib/"+arch+"/*.so")
342 }
343 }
344 wantedJniLibString := strings.Join(wantedJniLibSlice, " ")
345 ctx.Build(pctx, android.BuildParams{
346 Rule: stripEmbeddedJniLibsUnusedArchRule,
347 Input: inputPath,
348 Output: outputPath,
349 Args: map[string]string{
350 "extraArgs": wantedJniLibString,
351 },
352 })
353}
354
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800355func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
356 a.generateAndroidBuildActions(ctx)
357}
358
359func (a *AndroidAppImport) InstallApkName() string {
360 return a.BaseModuleName()
361}
362
Spandan Dasefa14652024-02-27 18:19:16 +0000363func (a *AndroidAppImport) BaseModuleName() string {
364 return proptools.StringDefault(a.properties.Source_module_name, a.ModuleBase.Name())
365}
366
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800367func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faustd5806132023-04-13 15:43:53 -0700368 if a.Name() == "prebuilt_framework-res" {
369 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.")
370 }
371
Colin Crossff694a82023-12-13 15:54:49 -0800372 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800373 if !apexInfo.IsForPlatform() {
374 a.hideApexVariantFromMake = true
375 }
376
Cole Faust61585282023-07-14 16:23:39 -0700377 if Bool(a.properties.Preprocessed) {
378 if a.properties.Presigned != nil && !*a.properties.Presigned {
379 ctx.ModuleErrorf("Setting preprocessed: true implies presigned: true, so you cannot set presigned to false")
380 }
381 t := true
382 a.properties.Presigned = &t
383 }
384
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800385 numCertPropsSet := 0
Cole Fausteb9c1482024-11-18 16:49:19 -0800386 if a.properties.Certificate.GetOrDefault(ctx, "") != "" {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800387 numCertPropsSet++
388 }
389 if Bool(a.properties.Presigned) {
390 numCertPropsSet++
391 }
392 if Bool(a.properties.Default_dev_cert) {
393 numCertPropsSet++
394 }
395 if numCertPropsSet != 1 {
Cole Faust61585282023-07-14 16:23:39 -0700396 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 -0800397 }
398
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800399 // TODO: LOCAL_PACKAGE_SPLITS
400
401 srcApk := a.prebuilt.SingleSourcePath(ctx)
Herbert Xue3b5672c2024-12-16 16:05:36 +0800402 if a.properties.Extract_apk != nil {
403 extract_apk := android.PathForModuleOut(ctx, "extract-apk", ctx.ModuleName()+".apk")
404 a.extractSubApk(ctx, srcApk, extract_apk)
405 srcApk = extract_apk
406 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800407
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800408 // TODO: Install or embed JNI libraries
409
410 // Uncompress JNI libraries in the apk
411 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
Cole Faust4e9f5922024-11-13 16:09:23 -0800412 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800413
Jim Tang6c4ecd02024-12-17 22:06:12 +0800414 // Strip all embedded JNI libs and include only required ones accordingly to the module's compile_multilib
415 if Bool(a.properties.Strip_unused_jni_arch) {
416 jnisStripped := android.PathForModuleOut(ctx, "jnis-stripped", ctx.ModuleName()+".apk")
417 a.stripEmbeddedJniLibsUnusedArch(ctx, jnisUncompressed, jnisStripped)
418 jnisUncompressed = jnisStripped
419 }
420
Spandan Dasd1fac642021-05-18 17:01:41 +0000421 var pathFragments []string
422 relInstallPath := String(a.properties.Relative_install_path)
Bill Peckhama036da92021-01-08 16:09:09 -0800423
Cole Faustd5806132023-04-13 15:43:53 -0700424 if Bool(a.properties.Privileged) {
Spandan Dasd1fac642021-05-18 17:01:41 +0000425 pathFragments = []string{"priv-app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800426 } else if ctx.InstallInTestcases() {
Spandan Dasd1fac642021-05-18 17:01:41 +0000427 pathFragments = []string{relInstallPath, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800428 } else {
Spandan Dasd1fac642021-05-18 17:01:41 +0000429 pathFragments = []string{"app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800430 }
431
Spandan Dasd1fac642021-05-18 17:01:41 +0000432 installDir := android.PathForModuleInstall(ctx, pathFragments...)
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000433 a.dexpreopter.isApp = true
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800434 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
435 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
436 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
437
Cole Faust64f2d842024-10-17 13:28:34 -0700438 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries(ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800439 a.dexpreopter.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Nelson Lib5b45a02025-01-10 08:19:58 +0000440
441 // Disable Dexpreopt if Compress_apk is true. It follows the build/make/core/app_prebuilt_internal.mk
442 if a.usesLibrary.shouldDisableDexpreopt || a.properties.Compress_apk.GetOrDefault(ctx, false) {
Spandan Das0727ba72024-02-13 16:37:43 +0000443 a.dexpreopter.disableDexpreopt()
444 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800445
Cole Faust64f2d842024-10-17 13:28:34 -0700446 if a.usesLibrary.enforceUsesLibraries(ctx) {
Jiakai Zhangf98da192024-04-15 11:15:41 +0000447 a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk, &a.dexpreopter.classLoaderContexts)
Ulya Trafimovichfe927a22021-02-26 14:36:48 +0000448 }
449
Spandan Dase21a8d42024-01-23 23:56:29 +0000450 a.dexpreopter.dexpreopt(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), jnisUncompressed)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800451 if a.dexpreopter.uncompressedDex {
452 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
Cole Faust4ec178c2023-01-13 12:03:38 -0800453 ctx.Build(pctx, android.BuildParams{
454 Rule: uncompressDexRule,
455 Input: jnisUncompressed,
456 Output: dexUncompressed,
457 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800458 jnisUncompressed = dexUncompressed
459 }
460
Nelson Lib5b45a02025-01-10 08:19:58 +0000461 defaultApkFilename := a.BaseModuleName()
462 if a.properties.Compress_apk.GetOrDefault(ctx, false) {
463 defaultApkFilename += ".apk.gz"
464 } else {
465 defaultApkFilename += ".apk"
466 }
467 apkFilename := proptools.StringDefault(a.properties.Filename, defaultApkFilename)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800468
469 // TODO: Handle EXTERNAL
470
471 // Sign or align the package if package has not been preprocessed
Bill Peckhama036da92021-01-08 16:09:09 -0800472
Cole Faust2f1da162023-04-17 15:06:56 -0700473 if proptools.Bool(a.properties.Preprocessed) {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700474 validationStamp := a.validatePresignedApk(ctx, srcApk)
475 output := android.PathForModuleOut(ctx, apkFilename)
476 ctx.Build(pctx, android.BuildParams{
477 Rule: android.Cp,
478 Input: srcApk,
479 Output: output,
480 Validation: validationStamp,
481 })
Cole Faust2f1da162023-04-17 15:06:56 -0700482 a.outputFile = output
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800483 a.certificate = PresignedCertificate
484 } else if !Bool(a.properties.Presigned) {
485 // If the certificate property is empty at this point, default_dev_cert must be set to true.
486 // Which makes processMainCert's behavior for the empty cert string WAI.
Cole Faust61585282023-07-14 16:23:39 -0700487 _, _, certificates := collectAppDeps(ctx, a, false, false)
Cole Fausteb9c1482024-11-18 16:49:19 -0800488 a.certificate, certificates = processMainCert(a.ModuleBase, a.properties.Certificate.GetOrDefault(ctx, ""), certificates, ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800489 signed := android.PathForModuleOut(ctx, "signed", apkFilename)
490 var lineageFile android.Path
491 if lineage := String(a.properties.Lineage); lineage != "" {
492 lineageFile = android.PathForModuleSrc(ctx, lineage)
493 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400494
495 rotationMinSdkVersion := String(a.properties.RotationMinSdkVersion)
496
497 SignAppPackage(ctx, signed, jnisUncompressed, certificates, nil, lineageFile, rotationMinSdkVersion)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800498 a.outputFile = signed
499 } else {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700500 validationStamp := a.validatePresignedApk(ctx, srcApk)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800501 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename)
Cole Faust9c5c09f2023-09-06 16:11:44 -0700502 TransformZipAlign(ctx, alignedApk, jnisUncompressed, []android.Path{validationStamp})
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800503 a.outputFile = alignedApk
504 a.certificate = PresignedCertificate
505 }
506
Nelson Lib5b45a02025-01-10 08:19:58 +0000507 if a.properties.Compress_apk.GetOrDefault(ctx, false) {
508 outputFile := android.PathForModuleOut(ctx, "compressed_apk", apkFilename)
509 ctx.Build(pctx, android.BuildParams{
510 Rule: gzipRule,
511 Input: a.outputFile,
512 Output: outputFile,
513 Description: "Compressing " + a.outputFile.Base(),
514 })
515 a.outputFile = outputFile
516 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800517
518 if apexInfo.IsForPlatform() {
519 a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
Cole Faustc71b1752024-10-14 12:14:54 -0700520 artifactPath := android.PathForModuleSrc(ctx, a.properties.Apk.GetOrDefault(ctx, ""))
Wei Li340ee8e2022-03-18 17:33:24 -0700521 a.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, artifactPath, a.installPath)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800522 }
523
Spandan Das3490dfd2024-03-11 21:37:25 +0000524 providePrebuiltInfo(ctx,
525 prebuiltInfoProps{
526 baseModuleName: a.BaseModuleName(),
527 isPrebuilt: true,
528 prebuiltInfo: a.properties.Prebuilt_info,
529 },
530 )
531
mrziwang68786d82024-07-09 10:41:55 -0700532 ctx.SetOutputFiles([]android.Path{a.outputFile}, "")
533
Wei Li986fe742025-01-30 15:14:42 -0800534 buildComplianceMetadata(ctx)
535
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800536 // TODO: androidmk converter jni libs
537}
538
Cole Faust9c5c09f2023-09-06 16:11:44 -0700539func (a *AndroidAppImport) validatePresignedApk(ctx android.ModuleContext, srcApk android.Path) android.Path {
540 stamp := android.PathForModuleOut(ctx, "validated-prebuilt", "check.stamp")
541 var extraArgs []string
Cole Faust93b89b42023-07-20 17:31:16 -0700542 if a.Privileged() {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700543 extraArgs = append(extraArgs, "--privileged")
Rashid Zaman3bd28702024-07-23 17:23:05 -0700544 if ctx.Config().UncompressPrivAppDex() {
545 extraArgs = append(extraArgs, "--uncompress-priv-app-dex")
546 }
Cole Faust9c5c09f2023-09-06 16:11:44 -0700547 }
548 if proptools.Bool(a.properties.Skip_preprocessed_apk_checks) {
549 extraArgs = append(extraArgs, "--skip-preprocessed-apk-checks")
550 }
551 if proptools.Bool(a.properties.Preprocessed) {
552 extraArgs = append(extraArgs, "--preprocessed")
Cole Faust93b89b42023-07-20 17:31:16 -0700553 }
554
Cole Faust2f1da162023-04-17 15:06:56 -0700555 ctx.Build(pctx, android.BuildParams{
Cole Faust9c5c09f2023-09-06 16:11:44 -0700556 Rule: checkPresignedApkRule,
Cole Faust61585282023-07-14 16:23:39 -0700557 Input: srcApk,
Cole Faust9c5c09f2023-09-06 16:11:44 -0700558 Output: stamp,
559 Args: map[string]string{
560 "extraArgs": strings.Join(extraArgs, " "),
561 },
Cole Faust61585282023-07-14 16:23:39 -0700562 })
Cole Faust9c5c09f2023-09-06 16:11:44 -0700563 return stamp
Cole Faust61585282023-07-14 16:23:39 -0700564}
565
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800566func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
567 return &a.prebuilt
568}
569
570func (a *AndroidAppImport) Name() string {
571 return a.prebuilt.Name(a.ModuleBase.Name())
572}
573
574func (a *AndroidAppImport) OutputFile() android.Path {
575 return a.outputFile
576}
577
578func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
579 return nil
580}
581
582func (a *AndroidAppImport) Certificate() Certificate {
583 return a.certificate
584}
585
Cole Faust4e9f5922024-11-13 16:09:23 -0800586func (a *AndroidAppImport) ProvenanceMetaDataFile() android.Path {
Wei Li340ee8e2022-03-18 17:33:24 -0700587 return a.provenanceMetaDataFile
588}
589
Andrei Onea580636b2022-08-17 16:53:46 +0000590func (a *AndroidAppImport) PrivAppAllowlist() android.OptionalPath {
591 return android.OptionalPath{}
592}
593
Herbert Xue04354ae2024-01-29 13:57:51 +0800594const (
595 ArchGroupName = "Arch"
596 DpiGroupName = "Dpi_variants"
597)
598
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800599var dpiVariantGroupType reflect.Type
600var archVariantGroupType reflect.Type
Herbert Xue04354ae2024-01-29 13:57:51 +0800601var archdpiVariantGroupType reflect.Type
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800602var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
603
604func initAndroidAppImportVariantGroupTypes() {
Herbert Xue04354ae2024-01-29 13:57:51 +0800605 dpiVariantGroupType = createVariantGroupType(supportedDpis, DpiGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800606
607 archNames := make([]string, len(android.ArchTypeList()))
608 for i, archType := range android.ArchTypeList() {
609 archNames[i] = archType.Name
610 }
Herbert Xue04354ae2024-01-29 13:57:51 +0800611 archVariantGroupType = createVariantGroupType(archNames, ArchGroupName)
612 archdpiVariantGroupType = createArchDpiVariantGroupType(archNames, supportedDpis)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800613}
614
615// Populates all variant struct properties at creation time.
616func (a *AndroidAppImport) populateAllVariantStructs() {
617 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
618 a.AddProperties(a.dpiVariants)
619
620 a.archVariants = reflect.New(archVariantGroupType).Interface()
621 a.AddProperties(a.archVariants)
Herbert Xue04354ae2024-01-29 13:57:51 +0800622
623 a.arch_dpiVariants = reflect.New(archdpiVariantGroupType).Interface()
624 a.AddProperties(a.arch_dpiVariants)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800625}
626
627func (a *AndroidAppImport) Privileged() bool {
628 return Bool(a.properties.Privileged)
629}
630
Yu Liuf1806032025-02-07 00:23:34 +0000631func (m *AndroidAppImport) GetDepInSameApexChecker() android.DepInSameApexChecker {
632 return AppImportDepInSameApexChecker{}
633}
634
635type AppImportDepInSameApexChecker struct {
636 android.BaseDepInSameApexChecker
637}
638
639func (m AppImportDepInSameApexChecker) OutgoingDepIsInSameApex(tag blueprint.DependencyTag) bool {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800640 // android_app_import might have extra dependencies via uses_libs property.
641 // Don't track the dependency as we don't automatically add those libraries
642 // to the classpath. It should be explicitly added to java_libs property of APEX
643 return false
644}
645
Jiyong Park92315372021-04-02 08:45:46 +0900646func (a *AndroidAppImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
647 return android.SdkSpecPrivate
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800648}
649
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000650func (a *AndroidAppImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
651 return android.SdkSpecPrivate.ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800652}
653
654var _ android.ApexModule = (*AndroidAppImport)(nil)
655
656// Implements android.ApexModule
Yu Liudf0b8392025-02-12 18:27:03 +0000657func (m *AndroidAppImport) MinSdkVersionSupported(ctx android.BaseModuleContext) android.ApiLevel {
658 return android.MinApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800659}
660
661func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
662 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
663
664 variantFields := make([]reflect.StructField, len(variants))
665 for i, variant := range variants {
666 variantFields[i] = reflect.StructField{
667 Name: proptools.FieldNameForProperty(variant),
668 Type: props,
669 }
670 }
671
672 variantGroupStruct := reflect.StructOf(variantFields)
673 return reflect.StructOf([]reflect.StructField{
674 {
675 Name: variantGroupName,
676 Type: variantGroupStruct,
677 },
678 })
679}
680
Herbert Xue04354ae2024-01-29 13:57:51 +0800681func createArchDpiVariantGroupType(archNames []string, dpiNames []string) reflect.Type {
682 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
683
684 dpiVariantFields := make([]reflect.StructField, len(dpiNames))
685 for i, variant_dpi := range dpiNames {
686 dpiVariantFields[i] = reflect.StructField{
687 Name: proptools.FieldNameForProperty(variant_dpi),
688 Type: props,
689 }
690 }
691 dpiVariantGroupStruct := reflect.StructOf(dpiVariantFields)
692 dpi_struct := reflect.StructOf([]reflect.StructField{
693 {
694 Name: DpiGroupName,
695 Type: reflect.PointerTo(dpiVariantGroupStruct),
696 },
697 })
698
699 archVariantFields := make([]reflect.StructField, len(archNames))
700 for i, variant_arch := range archNames {
701 archVariantFields[i] = reflect.StructField{
702 Name: proptools.FieldNameForProperty(variant_arch),
703 Type: reflect.PointerTo(dpi_struct),
704 }
705 }
706 archVariantGroupStruct := reflect.StructOf(archVariantFields)
707
708 return_struct := reflect.StructOf([]reflect.StructField{
709 {
710 Name: ArchGroupName,
711 Type: reflect.PointerTo(archVariantGroupStruct),
712 },
713 })
714 return return_struct
715}
716
Jiakai Zhangf98da192024-04-15 11:15:41 +0000717func (a *AndroidAppImport) UsesLibrary() *usesLibrary {
718 return &a.usesLibrary
719}
720
721var _ ModuleWithUsesLibrary = (*AndroidAppImport)(nil)
722
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800723// android_app_import imports a prebuilt apk with additional processing specified in the module.
724// DPI-specific apk source files can be specified using dpi_variants. Example:
725//
Colin Crossd079e0b2022-08-16 10:27:33 -0700726// android_app_import {
727// name: "example_import",
728// apk: "prebuilts/example.apk",
729// dpi_variants: {
730// mdpi: {
731// apk: "prebuilts/example_mdpi.apk",
732// },
733// xhdpi: {
734// apk: "prebuilts/example_xhdpi.apk",
735// },
736// },
737// presigned: true,
738// }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800739func AndroidAppImportFactory() android.Module {
740 module := &AndroidAppImport{}
741 module.AddProperties(&module.properties)
742 module.AddProperties(&module.dexpreoptProperties)
743 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
744 module.populateAllVariantStructs()
Cole Faust97494b12024-01-12 14:02:47 -0800745 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800746 module.processVariants(ctx)
747 })
748
749 android.InitApexModule(module)
750 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
751 android.InitDefaultableModule(module)
Cole Faustc71b1752024-10-14 12:14:54 -0700752 android.InitConfigurablePrebuiltModuleString(module, &module.properties.Apk, "Apk")
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800753
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000754 module.usesLibrary.enforce = true
755
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800756 return module
757}
758
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800759type AndroidTestImport struct {
760 AndroidAppImport
761
Jiyong Park2f83b312022-10-20 20:18:35 +0900762 testProperties struct {
763 // list of compatibility suites (for example "cts", "vts") that the module should be
764 // installed into.
765 Test_suites []string `android:"arch_variant"`
766
767 // list of files or filegroup modules that provide data that should be installed alongside
768 // the test
769 Data []string `android:"path"`
770
771 // Install the test into a folder named for the module in all test suites.
772 Per_testcase_directory *bool
773 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800774
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800775 data android.Paths
776}
777
778func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800779 a.generateAndroidBuildActions(ctx)
780
mrziwanga25adf32025-02-05 23:50:55 +0000781 a.updateModuleInfoJSON(ctx)
782
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800783 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
784}
785
mrziwanga25adf32025-02-05 23:50:55 +0000786func (a *AndroidTestImport) updateModuleInfoJSON(ctx android.ModuleContext) {
787 moduleInfoJSON := ctx.ModuleInfoJSON()
788 moduleInfoJSON.Class = []string{"APPS"}
789 moduleInfoJSON.CompatibilitySuites = []string{"null-suite"}
790 if len(a.testProperties.Test_suites) > 0 {
791 moduleInfoJSON.CompatibilitySuites = a.testProperties.Test_suites
792 }
793 moduleInfoJSON.SystemSharedLibs = []string{"none"}
794 moduleInfoJSON.Tags = []string{"tests"}
795 moduleInfoJSON.RegisterNameOverride = a.BaseModuleName()
796 testConfig := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "AndroidTest.xml")
797 if testConfig.Valid() {
798 moduleInfoJSON.TestConfig = []string{testConfig.String()}
799 }
800}
801
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800802func (a *AndroidTestImport) InstallInTestcases() bool {
803 return true
804}
805
806// android_test_import imports a prebuilt test apk with additional processing specified in the
807// module. DPI or arch variant configurations can be made as with android_app_import.
808func AndroidTestImportFactory() android.Module {
809 module := &AndroidTestImport{}
810 module.AddProperties(&module.properties)
811 module.AddProperties(&module.dexpreoptProperties)
812 module.AddProperties(&module.testProperties)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800813 module.populateAllVariantStructs()
Cole Faust97494b12024-01-12 14:02:47 -0800814 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800815 module.processVariants(ctx)
816 })
817
818 module.dexpreopter.isTest = true
819
820 android.InitApexModule(module)
821 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
822 android.InitDefaultableModule(module)
Cole Faustc71b1752024-10-14 12:14:54 -0700823 android.InitConfigurablePrebuiltModuleString(module, &module.properties.Apk, "Apk")
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800824
825 return module
826}