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