| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 1 | // 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 |  | 
|  | 15 | package java | 
|  | 16 |  | 
|  | 17 | // This file contains the module implementations for android_app_import and android_test_import. | 
|  | 18 |  | 
|  | 19 | import ( | 
| Colin Cross | 5368d0b | 2023-07-07 11:32:32 -0700 | [diff] [blame] | 20 | "fmt" | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 21 | "reflect" | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 22 | "strings" | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 23 |  | 
| Cole Faust | d580613 | 2023-04-13 15:43:53 -0700 | [diff] [blame] | 24 | "github.com/google/blueprint" | 
|  | 25 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 26 | "github.com/google/blueprint/proptools" | 
|  | 27 |  | 
|  | 28 | "android/soong/android" | 
| Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 29 | "android/soong/provenance" | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 30 | ) | 
|  | 31 |  | 
|  | 32 | func init() { | 
|  | 33 | RegisterAppImportBuildComponents(android.InitRegistrationContext) | 
|  | 34 |  | 
|  | 35 | initAndroidAppImportVariantGroupTypes() | 
|  | 36 | } | 
|  | 37 |  | 
| Cole Faust | 4ec178c | 2023-01-13 12:03:38 -0800 | [diff] [blame] | 38 | var ( | 
|  | 39 | uncompressEmbeddedJniLibsRule = pctx.AndroidStaticRule("uncompress-embedded-jni-libs", blueprint.RuleParams{ | 
|  | 40 | Command: `if (zipinfo $in 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then ` + | 
|  | 41 | `${config.Zip2ZipCmd} -i $in -o $out -0 'lib/**/*.so'` + | 
|  | 42 | `; else cp -f $in $out; fi`, | 
|  | 43 | CommandDeps: []string{"${config.Zip2ZipCmd}"}, | 
|  | 44 | Description: "Uncompress embedded JNI libs", | 
|  | 45 | }) | 
|  | 46 |  | 
|  | 47 | uncompressDexRule = pctx.AndroidStaticRule("uncompress-dex", blueprint.RuleParams{ | 
|  | 48 | Command: `if (zipinfo $in '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then ` + | 
|  | 49 | `${config.Zip2ZipCmd} -i $in -o $out -0 'classes*.dex'` + | 
|  | 50 | `; else cp -f $in $out; fi`, | 
|  | 51 | CommandDeps: []string{"${config.Zip2ZipCmd}"}, | 
|  | 52 | Description: "Uncompress dex files", | 
|  | 53 | }) | 
| Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 54 |  | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 55 | checkPresignedApkRule = pctx.AndroidStaticRule("check-presigned-apk", blueprint.RuleParams{ | 
|  | 56 | Command:     "build/soong/scripts/check_prebuilt_presigned_apk.py --aapt2 ${config.Aapt2Cmd} --zipalign ${config.ZipAlign} $extraArgs $in $out", | 
|  | 57 | CommandDeps: []string{"build/soong/scripts/check_prebuilt_presigned_apk.py", "${config.Aapt2Cmd}", "${config.ZipAlign}"}, | 
|  | 58 | Description: "Check presigned apk", | 
|  | 59 | }, "extraArgs") | 
| Cole Faust | 4ec178c | 2023-01-13 12:03:38 -0800 | [diff] [blame] | 60 | ) | 
|  | 61 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 62 | func RegisterAppImportBuildComponents(ctx android.RegistrationContext) { | 
|  | 63 | ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory) | 
|  | 64 | ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory) | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | type AndroidAppImport struct { | 
|  | 68 | android.ModuleBase | 
|  | 69 | android.DefaultableModuleBase | 
|  | 70 | android.ApexModuleBase | 
|  | 71 | prebuilt android.Prebuilt | 
|  | 72 |  | 
|  | 73 | properties   AndroidAppImportProperties | 
|  | 74 | dpiVariants  interface{} | 
|  | 75 | archVariants interface{} | 
|  | 76 |  | 
|  | 77 | outputFile  android.Path | 
|  | 78 | certificate Certificate | 
|  | 79 |  | 
|  | 80 | dexpreopter | 
|  | 81 |  | 
|  | 82 | usesLibrary usesLibrary | 
|  | 83 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 84 | installPath android.InstallPath | 
|  | 85 |  | 
|  | 86 | hideApexVariantFromMake bool | 
| Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 87 |  | 
|  | 88 | provenanceMetaDataFile android.OutputPath | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 89 | } | 
|  | 90 |  | 
|  | 91 | type AndroidAppImportProperties struct { | 
|  | 92 | // A prebuilt apk to import | 
| Jooyung Han | f05ca9c | 2021-06-28 21:48:51 +0900 | [diff] [blame] | 93 | Apk *string `android:"path"` | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 94 |  | 
|  | 95 | // The name of a certificate in the default certificate directory or an android_app_certificate | 
|  | 96 | // module name in the form ":module". Should be empty if presigned or default_dev_cert is set. | 
|  | 97 | Certificate *string | 
|  | 98 |  | 
| Jaewoong Jung | 25ae8de | 2021-03-08 17:37:46 -0800 | [diff] [blame] | 99 | // Names of extra android_app_certificate modules to sign the apk with in the form ":module". | 
|  | 100 | Additional_certificates []string | 
|  | 101 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 102 | // Set this flag to true if the prebuilt apk is already signed. The certificate property must not | 
|  | 103 | // be set for presigned modules. | 
|  | 104 | Presigned *bool | 
|  | 105 |  | 
| Jaewoong Jung | 1c1b6e6 | 2021-03-09 15:02:31 -0800 | [diff] [blame] | 106 | // Name of the signing certificate lineage file or filegroup module. | 
|  | 107 | Lineage *string `android:"path"` | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 108 |  | 
| Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 109 | // For overriding the --rotation-min-sdk-version property of apksig | 
|  | 110 | RotationMinSdkVersion *string | 
|  | 111 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 112 | // Sign with the default system dev certificate. Must be used judiciously. Most imported apps | 
|  | 113 | // need to either specify a specific certificate or be presigned. | 
|  | 114 | Default_dev_cert *bool | 
|  | 115 |  | 
|  | 116 | // Specifies that this app should be installed to the priv-app directory, | 
|  | 117 | // where the system will grant it additional privileges not available to | 
|  | 118 | // normal apps. | 
|  | 119 | Privileged *bool | 
|  | 120 |  | 
|  | 121 | // Names of modules to be overridden. Listed modules can only be other binaries | 
|  | 122 | // (in Make or Soong). | 
|  | 123 | // This does not completely prevent installation of the overridden binaries, but if both | 
|  | 124 | // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed | 
|  | 125 | // from PRODUCT_PACKAGES. | 
|  | 126 | Overrides []string | 
|  | 127 |  | 
|  | 128 | // Optional name for the installed app. If unspecified, it is derived from the module name. | 
|  | 129 | Filename *string | 
| Bill Peckham | a036da9 | 2021-01-08 16:09:09 -0800 | [diff] [blame] | 130 |  | 
|  | 131 | // If set, create package-export.apk, which other packages can | 
|  | 132 | // use to get PRODUCT-agnostic resource data like IDs and type definitions. | 
|  | 133 | Export_package_resources *bool | 
| Spandan Das | d1fac64 | 2021-05-18 17:01:41 +0000 | [diff] [blame] | 134 |  | 
|  | 135 | // Optional. Install to a subdirectory of the default install path for the module | 
|  | 136 | Relative_install_path *string | 
| Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 137 |  | 
|  | 138 | // Whether the prebuilt apk can be installed without additional processing. Default is false. | 
|  | 139 | Preprocessed *bool | 
|  | 140 |  | 
|  | 141 | // Whether or not to skip checking the preprocessed apk for proper alignment and uncompressed | 
|  | 142 | // JNI libs and dex files. Default is false | 
|  | 143 | Skip_preprocessed_apk_checks *bool | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
|  | 146 | func (a *AndroidAppImport) IsInstallable() bool { | 
|  | 147 | return true | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | // Updates properties with variant-specific values. | 
|  | 151 | func (a *AndroidAppImport) processVariants(ctx android.LoadHookContext) { | 
|  | 152 | config := ctx.Config() | 
|  | 153 |  | 
|  | 154 | dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants") | 
|  | 155 | // Try DPI variant matches in the reverse-priority order so that the highest priority match | 
|  | 156 | // overwrites everything else. | 
|  | 157 | // TODO(jungjw): Can we optimize this by making it priority order? | 
|  | 158 | for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- { | 
|  | 159 | MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i]) | 
|  | 160 | } | 
|  | 161 | if config.ProductAAPTPreferredConfig() != "" { | 
|  | 162 | MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig()) | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName("Arch") | 
|  | 166 | archType := ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType | 
|  | 167 | MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name) | 
|  | 168 |  | 
|  | 169 | if String(a.properties.Apk) == "" { | 
|  | 170 | // Disable this module since the apk property is still empty after processing all matching | 
|  | 171 | // variants. This likely means there is no matching variant, and the default variant doesn't | 
|  | 172 | // have an apk property value either. | 
|  | 173 | a.Disable() | 
|  | 174 | } | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | func MergePropertiesFromVariant(ctx android.EarlyModuleContext, | 
|  | 178 | dst interface{}, variantGroup reflect.Value, variant string) { | 
|  | 179 | src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant)) | 
|  | 180 | if !src.IsValid() { | 
|  | 181 | return | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend) | 
|  | 185 | if err != nil { | 
|  | 186 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { | 
|  | 187 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) | 
|  | 188 | } else { | 
|  | 189 | panic(err) | 
|  | 190 | } | 
|  | 191 | } | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) { | 
|  | 195 | cert := android.SrcIsModule(String(a.properties.Certificate)) | 
|  | 196 | if cert != "" { | 
|  | 197 | ctx.AddDependency(ctx.Module(), certificateTag, cert) | 
|  | 198 | } | 
|  | 199 |  | 
| Jaewoong Jung | 25ae8de | 2021-03-08 17:37:46 -0800 | [diff] [blame] | 200 | for _, cert := range a.properties.Additional_certificates { | 
|  | 201 | cert = android.SrcIsModule(cert) | 
|  | 202 | if cert != "" { | 
|  | 203 | ctx.AddDependency(ctx.Module(), certificateTag, cert) | 
|  | 204 | } else { | 
|  | 205 | ctx.PropertyErrorf("additional_certificates", | 
|  | 206 | `must be names of android_app_certificate modules in the form ":module"`) | 
|  | 207 | } | 
|  | 208 | } | 
|  | 209 |  | 
| Cole Faust | d580613 | 2023-04-13 15:43:53 -0700 | [diff] [blame] | 210 | a.usesLibrary.deps(ctx, true) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 211 | } | 
|  | 212 |  | 
|  | 213 | func (a *AndroidAppImport) uncompressEmbeddedJniLibs( | 
|  | 214 | ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) { | 
|  | 215 | // Test apps don't need their JNI libraries stored uncompressed. As a matter of fact, messing | 
|  | 216 | // with them may invalidate pre-existing signature data. | 
| Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 217 | if ctx.InstallInTestcases() && (Bool(a.properties.Presigned) || Bool(a.properties.Preprocessed)) { | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 218 | ctx.Build(pctx, android.BuildParams{ | 
|  | 219 | Rule:   android.Cp, | 
|  | 220 | Output: outputPath, | 
|  | 221 | Input:  inputPath, | 
|  | 222 | }) | 
|  | 223 | return | 
|  | 224 | } | 
| Cole Faust | 4ec178c | 2023-01-13 12:03:38 -0800 | [diff] [blame] | 225 |  | 
|  | 226 | ctx.Build(pctx, android.BuildParams{ | 
|  | 227 | Rule:   uncompressEmbeddedJniLibsRule, | 
|  | 228 | Input:  inputPath, | 
|  | 229 | Output: outputPath, | 
|  | 230 | }) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 231 | } | 
|  | 232 |  | 
|  | 233 | // Returns whether this module should have the dex file stored uncompressed in the APK. | 
|  | 234 | func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool { | 
| Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 235 | if ctx.Config().UnbundledBuild() || proptools.Bool(a.properties.Preprocessed) { | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 236 | return false | 
|  | 237 | } | 
|  | 238 |  | 
| Ulya Trafimovich | 0061c0d | 2021-09-01 15:40:38 +0100 | [diff] [blame] | 239 | // Uncompress dex in APKs of priv-apps if and only if DONT_UNCOMPRESS_PRIV_APPS_DEXS is false. | 
|  | 240 | if a.Privileged() { | 
|  | 241 | return ctx.Config().UncompressPrivAppDex() | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 242 | } | 
|  | 243 |  | 
|  | 244 | return shouldUncompressDex(ctx, &a.dexpreopter) | 
|  | 245 | } | 
|  | 246 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 247 | func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 248 | a.generateAndroidBuildActions(ctx) | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | func (a *AndroidAppImport) InstallApkName() string { | 
|  | 252 | return a.BaseModuleName() | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext) { | 
| Cole Faust | d580613 | 2023-04-13 15:43:53 -0700 | [diff] [blame] | 256 | if a.Name() == "prebuilt_framework-res" { | 
|  | 257 | 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.") | 
|  | 258 | } | 
|  | 259 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 260 | apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo) | 
|  | 261 | if !apexInfo.IsForPlatform() { | 
|  | 262 | a.hideApexVariantFromMake = true | 
|  | 263 | } | 
|  | 264 |  | 
| Cole Faust | 6158528 | 2023-07-14 16:23:39 -0700 | [diff] [blame] | 265 | if Bool(a.properties.Preprocessed) { | 
|  | 266 | if a.properties.Presigned != nil && !*a.properties.Presigned { | 
|  | 267 | ctx.ModuleErrorf("Setting preprocessed: true implies presigned: true, so you cannot set presigned to false") | 
|  | 268 | } | 
|  | 269 | t := true | 
|  | 270 | a.properties.Presigned = &t | 
|  | 271 | } | 
|  | 272 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 273 | numCertPropsSet := 0 | 
|  | 274 | if String(a.properties.Certificate) != "" { | 
|  | 275 | numCertPropsSet++ | 
|  | 276 | } | 
|  | 277 | if Bool(a.properties.Presigned) { | 
|  | 278 | numCertPropsSet++ | 
|  | 279 | } | 
|  | 280 | if Bool(a.properties.Default_dev_cert) { | 
|  | 281 | numCertPropsSet++ | 
|  | 282 | } | 
|  | 283 | if numCertPropsSet != 1 { | 
| Cole Faust | 6158528 | 2023-07-14 16:23:39 -0700 | [diff] [blame] | 284 | ctx.ModuleErrorf("One and only one of certficate, presigned (implied by preprocessed), and default_dev_cert properties must be set") | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 285 | } | 
|  | 286 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 287 | // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK | 
|  | 288 | // TODO: LOCAL_PACKAGE_SPLITS | 
|  | 289 |  | 
|  | 290 | srcApk := a.prebuilt.SingleSourcePath(ctx) | 
|  | 291 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 292 | // TODO: Install or embed JNI libraries | 
|  | 293 |  | 
|  | 294 | // Uncompress JNI libraries in the apk | 
|  | 295 | jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk") | 
|  | 296 | a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath) | 
|  | 297 |  | 
| Spandan Das | d1fac64 | 2021-05-18 17:01:41 +0000 | [diff] [blame] | 298 | var pathFragments []string | 
|  | 299 | relInstallPath := String(a.properties.Relative_install_path) | 
| Bill Peckham | a036da9 | 2021-01-08 16:09:09 -0800 | [diff] [blame] | 300 |  | 
| Cole Faust | d580613 | 2023-04-13 15:43:53 -0700 | [diff] [blame] | 301 | if Bool(a.properties.Privileged) { | 
| Spandan Das | d1fac64 | 2021-05-18 17:01:41 +0000 | [diff] [blame] | 302 | pathFragments = []string{"priv-app", relInstallPath, a.BaseModuleName()} | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 303 | } else if ctx.InstallInTestcases() { | 
| Spandan Das | d1fac64 | 2021-05-18 17:01:41 +0000 | [diff] [blame] | 304 | pathFragments = []string{relInstallPath, a.BaseModuleName(), ctx.DeviceConfig().DeviceArch()} | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 305 | } else { | 
| Spandan Das | d1fac64 | 2021-05-18 17:01:41 +0000 | [diff] [blame] | 306 | pathFragments = []string{"app", relInstallPath, a.BaseModuleName()} | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 307 | } | 
|  | 308 |  | 
| Spandan Das | d1fac64 | 2021-05-18 17:01:41 +0000 | [diff] [blame] | 309 | installDir := android.PathForModuleInstall(ctx, pathFragments...) | 
| Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 310 | a.dexpreopter.isApp = true | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 311 | a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk") | 
|  | 312 | a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned) | 
|  | 313 | a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx) | 
|  | 314 |  | 
|  | 315 | a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries() | 
|  | 316 | a.dexpreopter.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx) | 
|  | 317 |  | 
| Ulya Trafimovich | fe927a2 | 2021-02-26 14:36:48 +0000 | [diff] [blame] | 318 | if a.usesLibrary.enforceUsesLibraries() { | 
| Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 319 | a.usesLibrary.verifyUsesLibrariesAPK(ctx, srcApk) | 
| Ulya Trafimovich | fe927a2 | 2021-02-26 14:36:48 +0000 | [diff] [blame] | 320 | } | 
|  | 321 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 322 | a.dexpreopter.dexpreopt(ctx, jnisUncompressed) | 
|  | 323 | if a.dexpreopter.uncompressedDex { | 
|  | 324 | dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk") | 
| Cole Faust | 4ec178c | 2023-01-13 12:03:38 -0800 | [diff] [blame] | 325 | ctx.Build(pctx, android.BuildParams{ | 
|  | 326 | Rule:   uncompressDexRule, | 
|  | 327 | Input:  jnisUncompressed, | 
|  | 328 | Output: dexUncompressed, | 
|  | 329 | }) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 330 | jnisUncompressed = dexUncompressed | 
|  | 331 | } | 
|  | 332 |  | 
|  | 333 | apkFilename := proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk") | 
|  | 334 |  | 
|  | 335 | // TODO: Handle EXTERNAL | 
|  | 336 |  | 
|  | 337 | // Sign or align the package if package has not been preprocessed | 
| Bill Peckham | a036da9 | 2021-01-08 16:09:09 -0800 | [diff] [blame] | 338 |  | 
| Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 339 | if proptools.Bool(a.properties.Preprocessed) { | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 340 | validationStamp := a.validatePresignedApk(ctx, srcApk) | 
|  | 341 | output := android.PathForModuleOut(ctx, apkFilename) | 
|  | 342 | ctx.Build(pctx, android.BuildParams{ | 
|  | 343 | Rule:       android.Cp, | 
|  | 344 | Input:      srcApk, | 
|  | 345 | Output:     output, | 
|  | 346 | Validation: validationStamp, | 
|  | 347 | }) | 
| Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 348 | a.outputFile = output | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 349 | a.certificate = PresignedCertificate | 
|  | 350 | } else if !Bool(a.properties.Presigned) { | 
|  | 351 | // If the certificate property is empty at this point, default_dev_cert must be set to true. | 
|  | 352 | // Which makes processMainCert's behavior for the empty cert string WAI. | 
| Cole Faust | 6158528 | 2023-07-14 16:23:39 -0700 | [diff] [blame] | 353 | _, _, certificates := collectAppDeps(ctx, a, false, false) | 
| Colin Cross | bc2c8a7 | 2022-09-14 12:45:42 -0700 | [diff] [blame] | 354 | a.certificate, certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 355 | signed := android.PathForModuleOut(ctx, "signed", apkFilename) | 
|  | 356 | var lineageFile android.Path | 
|  | 357 | if lineage := String(a.properties.Lineage); lineage != "" { | 
|  | 358 | lineageFile = android.PathForModuleSrc(ctx, lineage) | 
|  | 359 | } | 
| Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 360 |  | 
|  | 361 | rotationMinSdkVersion := String(a.properties.RotationMinSdkVersion) | 
|  | 362 |  | 
|  | 363 | SignAppPackage(ctx, signed, jnisUncompressed, certificates, nil, lineageFile, rotationMinSdkVersion) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 364 | a.outputFile = signed | 
|  | 365 | } else { | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 366 | validationStamp := a.validatePresignedApk(ctx, srcApk) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 367 | alignedApk := android.PathForModuleOut(ctx, "zip-aligned", apkFilename) | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 368 | TransformZipAlign(ctx, alignedApk, jnisUncompressed, []android.Path{validationStamp}) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 369 | a.outputFile = alignedApk | 
|  | 370 | a.certificate = PresignedCertificate | 
|  | 371 | } | 
|  | 372 |  | 
|  | 373 | // TODO: Optionally compress the output apk. | 
|  | 374 |  | 
|  | 375 | if apexInfo.IsForPlatform() { | 
|  | 376 | a.installPath = ctx.InstallFile(installDir, apkFilename, a.outputFile) | 
| Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 377 | artifactPath := android.PathForModuleSrc(ctx, *a.properties.Apk) | 
|  | 378 | a.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, artifactPath, a.installPath) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 379 | } | 
|  | 380 |  | 
|  | 381 | // TODO: androidmk converter jni libs | 
|  | 382 | } | 
|  | 383 |  | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 384 | func (a *AndroidAppImport) validatePresignedApk(ctx android.ModuleContext, srcApk android.Path) android.Path { | 
|  | 385 | stamp := android.PathForModuleOut(ctx, "validated-prebuilt", "check.stamp") | 
|  | 386 | var extraArgs []string | 
| Cole Faust | 93b89b4 | 2023-07-20 17:31:16 -0700 | [diff] [blame] | 387 | if a.Privileged() { | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 388 | extraArgs = append(extraArgs, "--privileged") | 
|  | 389 | } | 
|  | 390 | if proptools.Bool(a.properties.Skip_preprocessed_apk_checks) { | 
|  | 391 | extraArgs = append(extraArgs, "--skip-preprocessed-apk-checks") | 
|  | 392 | } | 
|  | 393 | if proptools.Bool(a.properties.Preprocessed) { | 
|  | 394 | extraArgs = append(extraArgs, "--preprocessed") | 
| Cole Faust | 93b89b4 | 2023-07-20 17:31:16 -0700 | [diff] [blame] | 395 | } | 
|  | 396 |  | 
| Cole Faust | 2f1da16 | 2023-04-17 15:06:56 -0700 | [diff] [blame] | 397 | ctx.Build(pctx, android.BuildParams{ | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 398 | Rule:   checkPresignedApkRule, | 
| Cole Faust | 6158528 | 2023-07-14 16:23:39 -0700 | [diff] [blame] | 399 | Input:  srcApk, | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 400 | Output: stamp, | 
|  | 401 | Args: map[string]string{ | 
|  | 402 | "extraArgs": strings.Join(extraArgs, " "), | 
|  | 403 | }, | 
| Cole Faust | 6158528 | 2023-07-14 16:23:39 -0700 | [diff] [blame] | 404 | }) | 
| Cole Faust | 9c5c09f | 2023-09-06 16:11:44 -0700 | [diff] [blame] | 405 | return stamp | 
| Cole Faust | 6158528 | 2023-07-14 16:23:39 -0700 | [diff] [blame] | 406 | } | 
|  | 407 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 408 | func (a *AndroidAppImport) Prebuilt() *android.Prebuilt { | 
|  | 409 | return &a.prebuilt | 
|  | 410 | } | 
|  | 411 |  | 
|  | 412 | func (a *AndroidAppImport) Name() string { | 
|  | 413 | return a.prebuilt.Name(a.ModuleBase.Name()) | 
|  | 414 | } | 
|  | 415 |  | 
|  | 416 | func (a *AndroidAppImport) OutputFile() android.Path { | 
|  | 417 | return a.outputFile | 
|  | 418 | } | 
|  | 419 |  | 
| Colin Cross | 5368d0b | 2023-07-07 11:32:32 -0700 | [diff] [blame] | 420 | func (a *AndroidAppImport) OutputFiles(tag string) (android.Paths, error) { | 
|  | 421 | switch tag { | 
|  | 422 | case "": | 
|  | 423 | return []android.Path{a.outputFile}, nil | 
|  | 424 | default: | 
|  | 425 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) | 
|  | 426 | } | 
|  | 427 | } | 
|  | 428 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 429 | func (a *AndroidAppImport) JacocoReportClassesFile() android.Path { | 
|  | 430 | return nil | 
|  | 431 | } | 
|  | 432 |  | 
|  | 433 | func (a *AndroidAppImport) Certificate() Certificate { | 
|  | 434 | return a.certificate | 
|  | 435 | } | 
|  | 436 |  | 
| Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 437 | func (a *AndroidAppImport) ProvenanceMetaDataFile() android.OutputPath { | 
|  | 438 | return a.provenanceMetaDataFile | 
|  | 439 | } | 
|  | 440 |  | 
| Andrei Onea | 580636b | 2022-08-17 16:53:46 +0000 | [diff] [blame] | 441 | func (a *AndroidAppImport) PrivAppAllowlist() android.OptionalPath { | 
|  | 442 | return android.OptionalPath{} | 
|  | 443 | } | 
|  | 444 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 445 | var dpiVariantGroupType reflect.Type | 
|  | 446 | var archVariantGroupType reflect.Type | 
|  | 447 | var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"} | 
|  | 448 |  | 
|  | 449 | func initAndroidAppImportVariantGroupTypes() { | 
|  | 450 | dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants") | 
|  | 451 |  | 
|  | 452 | archNames := make([]string, len(android.ArchTypeList())) | 
|  | 453 | for i, archType := range android.ArchTypeList() { | 
|  | 454 | archNames[i] = archType.Name | 
|  | 455 | } | 
|  | 456 | archVariantGroupType = createVariantGroupType(archNames, "Arch") | 
|  | 457 | } | 
|  | 458 |  | 
|  | 459 | // Populates all variant struct properties at creation time. | 
|  | 460 | func (a *AndroidAppImport) populateAllVariantStructs() { | 
|  | 461 | a.dpiVariants = reflect.New(dpiVariantGroupType).Interface() | 
|  | 462 | a.AddProperties(a.dpiVariants) | 
|  | 463 |  | 
|  | 464 | a.archVariants = reflect.New(archVariantGroupType).Interface() | 
|  | 465 | a.AddProperties(a.archVariants) | 
|  | 466 | } | 
|  | 467 |  | 
|  | 468 | func (a *AndroidAppImport) Privileged() bool { | 
|  | 469 | return Bool(a.properties.Privileged) | 
|  | 470 | } | 
|  | 471 |  | 
|  | 472 | func (a *AndroidAppImport) DepIsInSameApex(_ android.BaseModuleContext, _ android.Module) bool { | 
|  | 473 | // android_app_import might have extra dependencies via uses_libs property. | 
|  | 474 | // Don't track the dependency as we don't automatically add those libraries | 
|  | 475 | // to the classpath. It should be explicitly added to java_libs property of APEX | 
|  | 476 | return false | 
|  | 477 | } | 
|  | 478 |  | 
| Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 479 | func (a *AndroidAppImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { | 
|  | 480 | return android.SdkSpecPrivate | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 481 | } | 
|  | 482 |  | 
| Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 483 | func (a *AndroidAppImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { | 
|  | 484 | return android.SdkSpecPrivate.ApiLevel | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 485 | } | 
|  | 486 |  | 
| Colin Cross | 8355c15 | 2021-08-10 19:24:07 -0700 | [diff] [blame] | 487 | func (a *AndroidAppImport) LintDepSets() LintDepSets { | 
|  | 488 | return LintDepSets{} | 
|  | 489 | } | 
|  | 490 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 491 | var _ android.ApexModule = (*AndroidAppImport)(nil) | 
|  | 492 |  | 
|  | 493 | // Implements android.ApexModule | 
|  | 494 | func (j *AndroidAppImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext, | 
|  | 495 | sdkVersion android.ApiLevel) error { | 
|  | 496 | // Do not check for prebuilts against the min_sdk_version of enclosing APEX | 
|  | 497 | return nil | 
|  | 498 | } | 
|  | 499 |  | 
|  | 500 | func createVariantGroupType(variants []string, variantGroupName string) reflect.Type { | 
|  | 501 | props := reflect.TypeOf((*AndroidAppImportProperties)(nil)) | 
|  | 502 |  | 
|  | 503 | variantFields := make([]reflect.StructField, len(variants)) | 
|  | 504 | for i, variant := range variants { | 
|  | 505 | variantFields[i] = reflect.StructField{ | 
|  | 506 | Name: proptools.FieldNameForProperty(variant), | 
|  | 507 | Type: props, | 
|  | 508 | } | 
|  | 509 | } | 
|  | 510 |  | 
|  | 511 | variantGroupStruct := reflect.StructOf(variantFields) | 
|  | 512 | return reflect.StructOf([]reflect.StructField{ | 
|  | 513 | { | 
|  | 514 | Name: variantGroupName, | 
|  | 515 | Type: variantGroupStruct, | 
|  | 516 | }, | 
|  | 517 | }) | 
|  | 518 | } | 
|  | 519 |  | 
|  | 520 | // android_app_import imports a prebuilt apk with additional processing specified in the module. | 
|  | 521 | // DPI-specific apk source files can be specified using dpi_variants. Example: | 
|  | 522 | // | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 523 | //	android_app_import { | 
|  | 524 | //	    name: "example_import", | 
|  | 525 | //	    apk: "prebuilts/example.apk", | 
|  | 526 | //	    dpi_variants: { | 
|  | 527 | //	        mdpi: { | 
|  | 528 | //	            apk: "prebuilts/example_mdpi.apk", | 
|  | 529 | //	        }, | 
|  | 530 | //	        xhdpi: { | 
|  | 531 | //	            apk: "prebuilts/example_xhdpi.apk", | 
|  | 532 | //	        }, | 
|  | 533 | //	    }, | 
|  | 534 | //	    presigned: true, | 
|  | 535 | //	} | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 536 | func AndroidAppImportFactory() android.Module { | 
|  | 537 | module := &AndroidAppImport{} | 
|  | 538 | module.AddProperties(&module.properties) | 
|  | 539 | module.AddProperties(&module.dexpreoptProperties) | 
|  | 540 | module.AddProperties(&module.usesLibrary.usesLibraryProperties) | 
|  | 541 | module.populateAllVariantStructs() | 
|  | 542 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { | 
|  | 543 | module.processVariants(ctx) | 
|  | 544 | }) | 
|  | 545 |  | 
|  | 546 | android.InitApexModule(module) | 
|  | 547 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) | 
|  | 548 | android.InitDefaultableModule(module) | 
|  | 549 | android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk") | 
|  | 550 |  | 
| Ulya Trafimovich | 22890c4 | 2021-01-05 12:04:17 +0000 | [diff] [blame] | 551 | module.usesLibrary.enforce = true | 
|  | 552 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 553 | return module | 
|  | 554 | } | 
|  | 555 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 556 | type AndroidTestImport struct { | 
|  | 557 | AndroidAppImport | 
|  | 558 |  | 
| Jiyong Park | 2f83b31 | 2022-10-20 20:18:35 +0900 | [diff] [blame] | 559 | testProperties struct { | 
|  | 560 | // list of compatibility suites (for example "cts", "vts") that the module should be | 
|  | 561 | // installed into. | 
|  | 562 | Test_suites []string `android:"arch_variant"` | 
|  | 563 |  | 
|  | 564 | // list of files or filegroup modules that provide data that should be installed alongside | 
|  | 565 | // the test | 
|  | 566 | Data []string `android:"path"` | 
|  | 567 |  | 
|  | 568 | // Install the test into a folder named for the module in all test suites. | 
|  | 569 | Per_testcase_directory *bool | 
|  | 570 | } | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 571 |  | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 572 | data android.Paths | 
|  | 573 | } | 
|  | 574 |  | 
|  | 575 | func (a *AndroidTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 576 | a.generateAndroidBuildActions(ctx) | 
|  | 577 |  | 
|  | 578 | a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data) | 
|  | 579 | } | 
|  | 580 |  | 
|  | 581 | func (a *AndroidTestImport) InstallInTestcases() bool { | 
|  | 582 | return true | 
|  | 583 | } | 
|  | 584 |  | 
|  | 585 | // android_test_import imports a prebuilt test apk with additional processing specified in the | 
|  | 586 | // module. DPI or arch variant configurations can be made as with android_app_import. | 
|  | 587 | func AndroidTestImportFactory() android.Module { | 
|  | 588 | module := &AndroidTestImport{} | 
|  | 589 | module.AddProperties(&module.properties) | 
|  | 590 | module.AddProperties(&module.dexpreoptProperties) | 
|  | 591 | module.AddProperties(&module.testProperties) | 
| Jaewoong Jung | f9b4465 | 2020-12-21 12:29:12 -0800 | [diff] [blame] | 592 | module.populateAllVariantStructs() | 
|  | 593 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { | 
|  | 594 | module.processVariants(ctx) | 
|  | 595 | }) | 
|  | 596 |  | 
|  | 597 | module.dexpreopter.isTest = true | 
|  | 598 |  | 
|  | 599 | android.InitApexModule(module) | 
|  | 600 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) | 
|  | 601 | android.InitDefaultableModule(module) | 
|  | 602 | android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk") | 
|  | 603 |  | 
|  | 604 | return module | 
|  | 605 | } |