blob: b77e31a8a3a09612d899bb0a129a56353e55e410 [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")
Cole Faust4ec178c2023-01-13 12:03:38 -080070)
71
Jaewoong Jungf9b44652020-12-21 12:29:12 -080072func RegisterAppImportBuildComponents(ctx android.RegistrationContext) {
73 ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory)
74 ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory)
Cole Faustc71b1752024-10-14 12:14:54 -070075 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
76 ctx.BottomUp("disable_prebuilts_without_apk", disablePrebuiltsWithoutApkMutator)
77 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -080078}
79
80type AndroidAppImport struct {
81 android.ModuleBase
82 android.DefaultableModuleBase
83 android.ApexModuleBase
84 prebuilt android.Prebuilt
85
Herbert Xue04354ae2024-01-29 13:57:51 +080086 properties AndroidAppImportProperties
87 dpiVariants interface{}
88 archVariants interface{}
89 arch_dpiVariants interface{}
Jaewoong Jungf9b44652020-12-21 12:29:12 -080090
91 outputFile android.Path
92 certificate Certificate
93
94 dexpreopter
95
96 usesLibrary usesLibrary
97
Jaewoong Jungf9b44652020-12-21 12:29:12 -080098 installPath android.InstallPath
99
100 hideApexVariantFromMake bool
Wei Li340ee8e2022-03-18 17:33:24 -0700101
Cole Faust4e9f5922024-11-13 16:09:23 -0800102 provenanceMetaDataFile android.Path
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800103}
104
105type AndroidAppImportProperties struct {
106 // A prebuilt apk to import
Cole Faustc71b1752024-10-14 12:14:54 -0700107 Apk proptools.Configurable[string] `android:"path,replace_instead_of_append"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800108
109 // The name of a certificate in the default certificate directory or an android_app_certificate
110 // module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
Cole Fausteb9c1482024-11-18 16:49:19 -0800111 Certificate proptools.Configurable[string] `android:"replace_instead_of_append"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800112
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800113 // Names of extra android_app_certificate modules to sign the apk with in the form ":module".
114 Additional_certificates []string
115
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800116 // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
117 // be set for presigned modules.
118 Presigned *bool
119
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800120 // Name of the signing certificate lineage file or filegroup module.
121 Lineage *string `android:"path"`
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800122
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400123 // For overriding the --rotation-min-sdk-version property of apksig
124 RotationMinSdkVersion *string
125
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800126 // Sign with the default system dev certificate. Must be used judiciously. Most imported apps
127 // need to either specify a specific certificate or be presigned.
128 Default_dev_cert *bool
129
130 // Specifies that this app should be installed to the priv-app directory,
131 // where the system will grant it additional privileges not available to
132 // normal apps.
133 Privileged *bool
134
135 // Names of modules to be overridden. Listed modules can only be other binaries
136 // (in Make or Soong).
137 // This does not completely prevent installation of the overridden binaries, but if both
138 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
139 // from PRODUCT_PACKAGES.
140 Overrides []string
141
142 // Optional name for the installed app. If unspecified, it is derived from the module name.
143 Filename *string
Bill Peckhama036da92021-01-08 16:09:09 -0800144
145 // If set, create package-export.apk, which other packages can
146 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
147 Export_package_resources *bool
Spandan Dasd1fac642021-05-18 17:01:41 +0000148
149 // Optional. Install to a subdirectory of the default install path for the module
150 Relative_install_path *string
Cole Faust2f1da162023-04-17 15:06:56 -0700151
152 // Whether the prebuilt apk can be installed without additional processing. Default is false.
153 Preprocessed *bool
154
155 // Whether or not to skip checking the preprocessed apk for proper alignment and uncompressed
156 // JNI libs and dex files. Default is false
157 Skip_preprocessed_apk_checks *bool
Spandan Dasefa14652024-02-27 18:19:16 +0000158
159 // Name of the source soong module that gets shadowed by this prebuilt
160 // If unspecified, follows the naming convention that the source module of
161 // the prebuilt is Name() without "prebuilt_" prefix
162 Source_module_name *string
Spandan Das3490dfd2024-03-11 21:37:25 +0000163
Jim Tang6c4ecd02024-12-17 22:06:12 +0800164 // Whether stripping all libraries from unused architectures.
165 Strip_unused_jni_arch *bool
166
Spandan Das3490dfd2024-03-11 21:37:25 +0000167 // Path to the .prebuilt_info file of the prebuilt app.
168 // In case of mainline modules, the .prebuilt_info file contains the build_id that was used
169 // to generate the prebuilt.
170 Prebuilt_info *string `android:"path"`
Herbert Xue3b5672c2024-12-16 16:05:36 +0800171
172 // Path of extracted apk which is extracted from prebuilt apk. Use this extracted to import.
173 Extract_apk *string
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800174}
175
176func (a *AndroidAppImport) IsInstallable() bool {
177 return true
178}
179
180// Updates properties with variant-specific values.
Cole Faust97494b12024-01-12 14:02:47 -0800181// This happens as a DefaultableHook instead of a LoadHook because we want to run it after
182// soong config variables are applied.
183func (a *AndroidAppImport) processVariants(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800184 config := ctx.Config()
Herbert Xue04354ae2024-01-29 13:57:51 +0800185 dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName(DpiGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800186
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800187 // Try DPI variant matches in the reverse-priority order so that the highest priority match
188 // overwrites everything else.
189 // TODO(jungjw): Can we optimize this by making it priority order?
190 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
191 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
192 }
193 if config.ProductAAPTPreferredConfig() != "" {
194 MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
195 }
Herbert Xue04354ae2024-01-29 13:57:51 +0800196 archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName(ArchGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800197 archType := ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType
198 MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
199
Herbert Xue04354ae2024-01-29 13:57:51 +0800200 // Process "arch" includes "dpi_variants"
201 archStructPtr := reflect.ValueOf(a.arch_dpiVariants).Elem().FieldByName(ArchGroupName)
202 if archStruct := archStructPtr.Elem(); archStruct.IsValid() {
203 archPartPropsPtr := archStruct.FieldByName(proptools.FieldNameForProperty(archType.Name))
204 if archPartProps := archPartPropsPtr.Elem(); archPartProps.IsValid() {
205 archDpiPropsPtr := archPartProps.FieldByName(DpiGroupName)
206 if archDpiProps := archDpiPropsPtr.Elem(); archDpiProps.IsValid() {
207 for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
208 MergePropertiesFromVariant(ctx, &a.properties, archDpiProps, config.ProductAAPTPrebuiltDPI()[i])
209 }
210 if config.ProductAAPTPreferredConfig() != "" {
211 MergePropertiesFromVariant(ctx, &a.properties, archDpiProps, config.ProductAAPTPreferredConfig())
212 }
213 }
214 }
215 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800216}
217
218func MergePropertiesFromVariant(ctx android.EarlyModuleContext,
219 dst interface{}, variantGroup reflect.Value, variant string) {
220 src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
221 if !src.IsValid() {
222 return
223 }
224
225 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
226 if err != nil {
227 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
228 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
229 } else {
230 panic(err)
231 }
232 }
233}
234
Cole Faustc71b1752024-10-14 12:14:54 -0700235// disablePrebuiltsWithoutApkMutator is a pre-arch mutator that disables AndroidAppImport or
236// AndroidTestImport modules that don't have an apk set. We need this separate mutator instead
237// of doing it in processVariants because processVariants is a defaultable hook, and configurable
238// properties can only be evaluated after the defaults (and eventually, base configurabtion)
239// mutators.
240func disablePrebuiltsWithoutApkMutator(ctx android.BottomUpMutatorContext) {
241 switch a := ctx.Module().(type) {
242 case *AndroidAppImport:
243 if a.properties.Apk.GetOrDefault(ctx, "") == "" {
244 // Disable this module since the apk property is still empty after processing all
245 // matching variants. This likely means there is no matching variant, and the default
246 // variant doesn't have an apk property value either.
247 a.Disable()
248 }
249 case *AndroidTestImport:
250 if a.properties.Apk.GetOrDefault(ctx, "") == "" {
251 // Disable this module since the apk property is still empty after processing all
252 // matching variants. This likely means there is no matching variant, and the default
253 // variant doesn't have an apk property value either.
254 a.Disable()
255 }
256 }
257}
258
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800259func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Cole Fausteb9c1482024-11-18 16:49:19 -0800260 cert := android.SrcIsModule(a.properties.Certificate.GetOrDefault(ctx, ""))
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800261 if cert != "" {
262 ctx.AddDependency(ctx.Module(), certificateTag, cert)
263 }
264
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800265 for _, cert := range a.properties.Additional_certificates {
266 cert = android.SrcIsModule(cert)
267 if cert != "" {
268 ctx.AddDependency(ctx.Module(), certificateTag, cert)
269 } else {
270 ctx.PropertyErrorf("additional_certificates",
271 `must be names of android_app_certificate modules in the form ":module"`)
272 }
273 }
274
Cole Faustd5806132023-04-13 15:43:53 -0700275 a.usesLibrary.deps(ctx, true)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800276}
277
278func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
Cole Faust4e9f5922024-11-13 16:09:23 -0800279 ctx android.ModuleContext, inputPath android.Path, outputPath android.WritablePath) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800280 // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing
281 // with them may invalidate pre-existing signature data.
Cole Faust2f1da162023-04-17 15:06:56 -0700282 if ctx.InstallInTestcases() && (Bool(a.properties.Presigned) || Bool(a.properties.Preprocessed)) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800283 ctx.Build(pctx, android.BuildParams{
284 Rule: android.Cp,
285 Output: outputPath,
286 Input: inputPath,
287 })
288 return
289 }
Cole Faust4ec178c2023-01-13 12:03:38 -0800290
291 ctx.Build(pctx, android.BuildParams{
292 Rule: uncompressEmbeddedJniLibsRule,
293 Input: inputPath,
294 Output: outputPath,
295 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800296}
297
Herbert Xue3b5672c2024-12-16 16:05:36 +0800298func (a *AndroidAppImport) extractSubApk(
299 ctx android.ModuleContext, inputPath android.Path, outputPath android.WritablePath) {
300 extractApkPath := *a.properties.Extract_apk
301 ctx.Build(pctx, android.BuildParams{
302 Rule: extractApkRule,
303 Input: inputPath,
304 Output: outputPath,
305 Args: map[string]string{
306 "extract_apk": extractApkPath,
307 },
308 })
309}
310
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800311// Returns whether this module should have the dex file stored uncompressed in the APK.
312func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
Cole Faust2f1da162023-04-17 15:06:56 -0700313 if ctx.Config().UnbundledBuild() || proptools.Bool(a.properties.Preprocessed) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800314 return false
315 }
316
Ulya Trafimovich0061c0d2021-09-01 15:40:38 +0100317 // Uncompress dex in APKs of priv-apps if and only if DONT_UNCOMPRESS_PRIV_APPS_DEXS is false.
318 if a.Privileged() {
319 return ctx.Config().UncompressPrivAppDex()
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800320 }
321
Spandan Dase21a8d42024-01-23 23:56:29 +0000322 return shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &a.dexpreopter)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800323}
324
Jim Tang6c4ecd02024-12-17 22:06:12 +0800325func (a *AndroidAppImport) stripEmbeddedJniLibsUnusedArch(
326 ctx android.ModuleContext, inputPath android.Path, outputPath android.WritablePath) {
327 var wantedJniLibSlice []string
328 for _, target := range ctx.MultiTargets() {
329 supported_abis := target.Arch.Abi
330 for _, arch := range supported_abis {
331 wantedJniLibSlice = append(wantedJniLibSlice, " -X lib/"+arch+"/*.so")
332 }
333 }
334 wantedJniLibString := strings.Join(wantedJniLibSlice, " ")
335 ctx.Build(pctx, android.BuildParams{
336 Rule: stripEmbeddedJniLibsUnusedArchRule,
337 Input: inputPath,
338 Output: outputPath,
339 Args: map[string]string{
340 "extraArgs": wantedJniLibString,
341 },
342 })
343}
344
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800345func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
346 a.generateAndroidBuildActions(ctx)
347}
348
349func (a *AndroidAppImport) InstallApkName() string {
350 return a.BaseModuleName()
351}
352
Spandan Dasefa14652024-02-27 18:19:16 +0000353func (a *AndroidAppImport) BaseModuleName() string {
354 return proptools.StringDefault(a.properties.Source_module_name, a.ModuleBase.Name())
355}
356
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800357func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faustd5806132023-04-13 15:43:53 -0700358 if a.Name() == "prebuilt_framework-res" {
359 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.")
360 }
361
Colin Crossff694a82023-12-13 15:54:49 -0800362 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800363 if !apexInfo.IsForPlatform() {
364 a.hideApexVariantFromMake = true
365 }
366
Cole Faust61585282023-07-14 16:23:39 -0700367 if Bool(a.properties.Preprocessed) {
368 if a.properties.Presigned != nil && !*a.properties.Presigned {
369 ctx.ModuleErrorf("Setting preprocessed: true implies presigned: true, so you cannot set presigned to false")
370 }
371 t := true
372 a.properties.Presigned = &t
373 }
374
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800375 numCertPropsSet := 0
Cole Fausteb9c1482024-11-18 16:49:19 -0800376 if a.properties.Certificate.GetOrDefault(ctx, "") != "" {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800377 numCertPropsSet++
378 }
379 if Bool(a.properties.Presigned) {
380 numCertPropsSet++
381 }
382 if Bool(a.properties.Default_dev_cert) {
383 numCertPropsSet++
384 }
385 if numCertPropsSet != 1 {
Cole Faust61585282023-07-14 16:23:39 -0700386 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 -0800387 }
388
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800389 // TODO: LOCAL_PACKAGE_SPLITS
390
391 srcApk := a.prebuilt.SingleSourcePath(ctx)
Herbert Xue3b5672c2024-12-16 16:05:36 +0800392 if a.properties.Extract_apk != nil {
393 extract_apk := android.PathForModuleOut(ctx, "extract-apk", ctx.ModuleName()+".apk")
394 a.extractSubApk(ctx, srcApk, extract_apk)
395 srcApk = extract_apk
396 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800397
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800398 // TODO: Install or embed JNI libraries
399
400 // Uncompress JNI libraries in the apk
401 jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
Cole Faust4e9f5922024-11-13 16:09:23 -0800402 a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800403
Jim Tang6c4ecd02024-12-17 22:06:12 +0800404 // Strip all embedded JNI libs and include only required ones accordingly to the module's compile_multilib
405 if Bool(a.properties.Strip_unused_jni_arch) {
406 jnisStripped := android.PathForModuleOut(ctx, "jnis-stripped", ctx.ModuleName()+".apk")
407 a.stripEmbeddedJniLibsUnusedArch(ctx, jnisUncompressed, jnisStripped)
408 jnisUncompressed = jnisStripped
409 }
410
Spandan Dasd1fac642021-05-18 17:01:41 +0000411 var pathFragments []string
412 relInstallPath := String(a.properties.Relative_install_path)
Bill Peckhama036da92021-01-08 16:09:09 -0800413
Cole Faustd5806132023-04-13 15:43:53 -0700414 if Bool(a.properties.Privileged) {
Spandan Dasd1fac642021-05-18 17:01:41 +0000415 pathFragments = []string{"priv-app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800416 } else if ctx.InstallInTestcases() {
Spandan Dasd1fac642021-05-18 17:01:41 +0000417 pathFragments = []string{relInstallPath, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800418 } else {
Spandan Dasd1fac642021-05-18 17:01:41 +0000419 pathFragments = []string{"app", relInstallPath, a.BaseModuleName()}
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800420 }
421
Spandan Dasd1fac642021-05-18 17:01:41 +0000422 installDir := android.PathForModuleInstall(ctx, pathFragments...)
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000423 a.dexpreopter.isApp = true
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800424 a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
425 a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
426 a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
427
Cole Faust64f2d842024-10-17 13:28:34 -0700428 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries(ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800429 a.dexpreopter.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Spandan Das0727ba72024-02-13 16:37:43 +0000430 if a.usesLibrary.shouldDisableDexpreopt {
431 a.dexpreopter.disableDexpreopt()
432 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800433
Cole Faust64f2d842024-10-17 13:28:34 -0700434 if a.usesLibrary.enforceUsesLibraries(ctx) {
Jiakai Zhangf98da192024-04-15 11:15:41 +0000435 a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk, &a.dexpreopter.classLoaderContexts)
Ulya Trafimovichfe927a22021-02-26 14:36:48 +0000436 }
437
Spandan Dase21a8d42024-01-23 23:56:29 +0000438 a.dexpreopter.dexpreopt(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), jnisUncompressed)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800439 if a.dexpreopter.uncompressedDex {
440 dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
Cole Faust4ec178c2023-01-13 12:03:38 -0800441 ctx.Build(pctx, android.BuildParams{
442 Rule: uncompressDexRule,
443 Input: jnisUncompressed,
444 Output: dexUncompressed,
445 })
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800446 jnisUncompressed = dexUncompressed
447 }
448
449 apkFilename := proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk")
450
451 // TODO: Handle EXTERNAL
452
453 // Sign or align the package if package has not been preprocessed
Bill Peckhama036da92021-01-08 16:09:09 -0800454
Cole Faust2f1da162023-04-17 15:06:56 -0700455 if proptools.Bool(a.properties.Preprocessed) {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700456 validationStamp := a.validatePresignedApk(ctx, srcApk)
457 output := android.PathForModuleOut(ctx, apkFilename)
458 ctx.Build(pctx, android.BuildParams{
459 Rule: android.Cp,
460 Input: srcApk,
461 Output: output,
462 Validation: validationStamp,
463 })
Cole Faust2f1da162023-04-17 15:06:56 -0700464 a.outputFile = output
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800465 a.certificate = PresignedCertificate
466 } else if !Bool(a.properties.Presigned) {
467 // If the certificate property is empty at this point, default_dev_cert must be set to true.
468 // Which makes processMainCert's behavior for the empty cert string WAI.
Cole Faust61585282023-07-14 16:23:39 -0700469 _, _, certificates := collectAppDeps(ctx, a, false, false)
Cole Fausteb9c1482024-11-18 16:49:19 -0800470 a.certificate, certificates = processMainCert(a.ModuleBase, a.properties.Certificate.GetOrDefault(ctx, ""), certificates, ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800471 signed := android.PathForModuleOut(ctx, "signed", apkFilename)
472 var lineageFile android.Path
473 if lineage := String(a.properties.Lineage); lineage != "" {
474 lineageFile = android.PathForModuleSrc(ctx, lineage)
475 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400476
477 rotationMinSdkVersion := String(a.properties.RotationMinSdkVersion)
478
479 SignAppPackage(ctx, signed, jnisUncompressed, certificates, nil, lineageFile, rotationMinSdkVersion)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800480 a.outputFile = signed
481 } else {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700482 validationStamp := a.validatePresignedApk(ctx, srcApk)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800483 alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename)
Cole Faust9c5c09f2023-09-06 16:11:44 -0700484 TransformZipAlign(ctx, alignedApk, jnisUncompressed, []android.Path{validationStamp})
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800485 a.outputFile = alignedApk
486 a.certificate = PresignedCertificate
487 }
488
489 // TODO: Optionally compress the output apk.
490
491 if apexInfo.IsForPlatform() {
492 a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile)
Cole Faustc71b1752024-10-14 12:14:54 -0700493 artifactPath := android.PathForModuleSrc(ctx, a.properties.Apk.GetOrDefault(ctx, ""))
Wei Li340ee8e2022-03-18 17:33:24 -0700494 a.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, artifactPath, a.installPath)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800495 }
496
Spandan Das3490dfd2024-03-11 21:37:25 +0000497 providePrebuiltInfo(ctx,
498 prebuiltInfoProps{
499 baseModuleName: a.BaseModuleName(),
500 isPrebuilt: true,
501 prebuiltInfo: a.properties.Prebuilt_info,
502 },
503 )
504
mrziwang68786d82024-07-09 10:41:55 -0700505 ctx.SetOutputFiles([]android.Path{a.outputFile}, "")
506
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800507 // TODO: androidmk converter jni libs
508}
509
Cole Faust9c5c09f2023-09-06 16:11:44 -0700510func (a *AndroidAppImport) validatePresignedApk(ctx android.ModuleContext, srcApk android.Path) android.Path {
511 stamp := android.PathForModuleOut(ctx, "validated-prebuilt", "check.stamp")
512 var extraArgs []string
Cole Faust93b89b42023-07-20 17:31:16 -0700513 if a.Privileged() {
Cole Faust9c5c09f2023-09-06 16:11:44 -0700514 extraArgs = append(extraArgs, "--privileged")
Rashid Zaman3bd28702024-07-23 17:23:05 -0700515 if ctx.Config().UncompressPrivAppDex() {
516 extraArgs = append(extraArgs, "--uncompress-priv-app-dex")
517 }
Cole Faust9c5c09f2023-09-06 16:11:44 -0700518 }
519 if proptools.Bool(a.properties.Skip_preprocessed_apk_checks) {
520 extraArgs = append(extraArgs, "--skip-preprocessed-apk-checks")
521 }
522 if proptools.Bool(a.properties.Preprocessed) {
523 extraArgs = append(extraArgs, "--preprocessed")
Cole Faust93b89b42023-07-20 17:31:16 -0700524 }
525
Cole Faust2f1da162023-04-17 15:06:56 -0700526 ctx.Build(pctx, android.BuildParams{
Cole Faust9c5c09f2023-09-06 16:11:44 -0700527 Rule: checkPresignedApkRule,
Cole Faust61585282023-07-14 16:23:39 -0700528 Input: srcApk,
Cole Faust9c5c09f2023-09-06 16:11:44 -0700529 Output: stamp,
530 Args: map[string]string{
531 "extraArgs": strings.Join(extraArgs, " "),
532 },
Cole Faust61585282023-07-14 16:23:39 -0700533 })
Cole Faust9c5c09f2023-09-06 16:11:44 -0700534 return stamp
Cole Faust61585282023-07-14 16:23:39 -0700535}
536
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800537func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
538 return &a.prebuilt
539}
540
541func (a *AndroidAppImport) Name() string {
542 return a.prebuilt.Name(a.ModuleBase.Name())
543}
544
545func (a *AndroidAppImport) OutputFile() android.Path {
546 return a.outputFile
547}
548
549func (a *AndroidAppImport) JacocoReportClassesFile() android.Path {
550 return nil
551}
552
553func (a *AndroidAppImport) Certificate() Certificate {
554 return a.certificate
555}
556
Cole Faust4e9f5922024-11-13 16:09:23 -0800557func (a *AndroidAppImport) ProvenanceMetaDataFile() android.Path {
Wei Li340ee8e2022-03-18 17:33:24 -0700558 return a.provenanceMetaDataFile
559}
560
Andrei Onea580636b2022-08-17 16:53:46 +0000561func (a *AndroidAppImport) PrivAppAllowlist() android.OptionalPath {
562 return android.OptionalPath{}
563}
564
Herbert Xue04354ae2024-01-29 13:57:51 +0800565const (
566 ArchGroupName = "Arch"
567 DpiGroupName = "Dpi_variants"
568)
569
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800570var dpiVariantGroupType reflect.Type
571var archVariantGroupType reflect.Type
Herbert Xue04354ae2024-01-29 13:57:51 +0800572var archdpiVariantGroupType reflect.Type
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800573var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
574
575func initAndroidAppImportVariantGroupTypes() {
Herbert Xue04354ae2024-01-29 13:57:51 +0800576 dpiVariantGroupType = createVariantGroupType(supportedDpis, DpiGroupName)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800577
578 archNames := make([]string, len(android.ArchTypeList()))
579 for i, archType := range android.ArchTypeList() {
580 archNames[i] = archType.Name
581 }
Herbert Xue04354ae2024-01-29 13:57:51 +0800582 archVariantGroupType = createVariantGroupType(archNames, ArchGroupName)
583 archdpiVariantGroupType = createArchDpiVariantGroupType(archNames, supportedDpis)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800584}
585
586// Populates all variant struct properties at creation time.
587func (a *AndroidAppImport) populateAllVariantStructs() {
588 a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
589 a.AddProperties(a.dpiVariants)
590
591 a.archVariants = reflect.New(archVariantGroupType).Interface()
592 a.AddProperties(a.archVariants)
Herbert Xue04354ae2024-01-29 13:57:51 +0800593
594 a.arch_dpiVariants = reflect.New(archdpiVariantGroupType).Interface()
595 a.AddProperties(a.arch_dpiVariants)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800596}
597
598func (a *AndroidAppImport) Privileged() bool {
599 return Bool(a.properties.Privileged)
600}
601
Colin Crossf7bbd2f2024-12-05 13:57:10 -0800602func (a *AndroidAppImport) OutgoingDepIsInSameApex(tag blueprint.DependencyTag) bool {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800603 // android_app_import might have extra dependencies via uses_libs property.
604 // Don't track the dependency as we don't automatically add those libraries
605 // to the classpath. It should be explicitly added to java_libs property of APEX
606 return false
607}
608
Jiyong Park92315372021-04-02 08:45:46 +0900609func (a *AndroidAppImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
610 return android.SdkSpecPrivate
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800611}
612
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000613func (a *AndroidAppImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
614 return android.SdkSpecPrivate.ApiLevel
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800615}
616
617var _ android.ApexModule = (*AndroidAppImport)(nil)
618
619// Implements android.ApexModule
620func (j *AndroidAppImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
621 sdkVersion android.ApiLevel) error {
622 // Do not check for prebuilts against the min_sdk_version of enclosing APEX
623 return nil
624}
625
626func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
627 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
628
629 variantFields := make([]reflect.StructField, len(variants))
630 for i, variant := range variants {
631 variantFields[i] = reflect.StructField{
632 Name: proptools.FieldNameForProperty(variant),
633 Type: props,
634 }
635 }
636
637 variantGroupStruct := reflect.StructOf(variantFields)
638 return reflect.StructOf([]reflect.StructField{
639 {
640 Name: variantGroupName,
641 Type: variantGroupStruct,
642 },
643 })
644}
645
Herbert Xue04354ae2024-01-29 13:57:51 +0800646func createArchDpiVariantGroupType(archNames []string, dpiNames []string) reflect.Type {
647 props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
648
649 dpiVariantFields := make([]reflect.StructField, len(dpiNames))
650 for i, variant_dpi := range dpiNames {
651 dpiVariantFields[i] = reflect.StructField{
652 Name: proptools.FieldNameForProperty(variant_dpi),
653 Type: props,
654 }
655 }
656 dpiVariantGroupStruct := reflect.StructOf(dpiVariantFields)
657 dpi_struct := reflect.StructOf([]reflect.StructField{
658 {
659 Name: DpiGroupName,
660 Type: reflect.PointerTo(dpiVariantGroupStruct),
661 },
662 })
663
664 archVariantFields := make([]reflect.StructField, len(archNames))
665 for i, variant_arch := range archNames {
666 archVariantFields[i] = reflect.StructField{
667 Name: proptools.FieldNameForProperty(variant_arch),
668 Type: reflect.PointerTo(dpi_struct),
669 }
670 }
671 archVariantGroupStruct := reflect.StructOf(archVariantFields)
672
673 return_struct := reflect.StructOf([]reflect.StructField{
674 {
675 Name: ArchGroupName,
676 Type: reflect.PointerTo(archVariantGroupStruct),
677 },
678 })
679 return return_struct
680}
681
Jiakai Zhangf98da192024-04-15 11:15:41 +0000682func (a *AndroidAppImport) UsesLibrary() *usesLibrary {
683 return &a.usesLibrary
684}
685
686var _ ModuleWithUsesLibrary = (*AndroidAppImport)(nil)
687
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800688// android_app_import imports a prebuilt apk with additional processing specified in the module.
689// DPI-specific apk source files can be specified using dpi_variants. Example:
690//
Colin Crossd079e0b2022-08-16 10:27:33 -0700691// android_app_import {
692// name: "example_import",
693// apk: "prebuilts/example.apk",
694// dpi_variants: {
695// mdpi: {
696// apk: "prebuilts/example_mdpi.apk",
697// },
698// xhdpi: {
699// apk: "prebuilts/example_xhdpi.apk",
700// },
701// },
702// presigned: true,
703// }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800704func AndroidAppImportFactory() android.Module {
705 module := &AndroidAppImport{}
706 module.AddProperties(&module.properties)
707 module.AddProperties(&module.dexpreoptProperties)
708 module.AddProperties(&module.usesLibrary.usesLibraryProperties)
709 module.populateAllVariantStructs()
Cole Faust97494b12024-01-12 14:02:47 -0800710 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800711 module.processVariants(ctx)
712 })
713
714 android.InitApexModule(module)
715 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
716 android.InitDefaultableModule(module)
Cole Faustc71b1752024-10-14 12:14:54 -0700717 android.InitConfigurablePrebuiltModuleString(module, &module.properties.Apk, "Apk")
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800718
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000719 module.usesLibrary.enforce = true
720
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800721 return module
722}
723
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800724type AndroidTestImport struct {
725 AndroidAppImport
726
Jiyong Park2f83b312022-10-20 20:18:35 +0900727 testProperties struct {
728 // list of compatibility suites (for example "cts", "vts") that the module should be
729 // installed into.
730 Test_suites []string `android:"arch_variant"`
731
732 // list of files or filegroup modules that provide data that should be installed alongside
733 // the test
734 Data []string `android:"path"`
735
736 // Install the test into a folder named for the module in all test suites.
737 Per_testcase_directory *bool
738 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800739
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800740 data android.Paths
741}
742
743func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800744 a.generateAndroidBuildActions(ctx)
745
746 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
747}
748
749func (a *AndroidTestImport) InstallInTestcases() bool {
750 return true
751}
752
753// android_test_import imports a prebuilt test apk with additional processing specified in the
754// module. DPI or arch variant configurations can be made as with android_app_import.
755func AndroidTestImportFactory() android.Module {
756 module := &AndroidTestImport{}
757 module.AddProperties(&module.properties)
758 module.AddProperties(&module.dexpreoptProperties)
759 module.AddProperties(&module.testProperties)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800760 module.populateAllVariantStructs()
Cole Faust97494b12024-01-12 14:02:47 -0800761 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800762 module.processVariants(ctx)
763 })
764
765 module.dexpreopter.isTest = true
766
767 android.InitApexModule(module)
768 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
769 android.InitDefaultableModule(module)
Cole Faustc71b1752024-10-14 12:14:54 -0700770 android.InitConfigurablePrebuiltModuleString(module, &module.properties.Apk, "Apk")
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800771
772 return module
773}