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