1// Copyright 2015 Google Inc. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package java 16 17// This file contains the module implementations for android_app, android_test, and some more 18// related module types, including their override variants. 19 20import ( 21 "fmt" 22 "path/filepath" 23 "strings" 24 25 "android/soong/testing" 26 27 "github.com/google/blueprint" 28 "github.com/google/blueprint/proptools" 29 30 "android/soong/android" 31 "android/soong/cc" 32 "android/soong/dexpreopt" 33 "android/soong/genrule" 34 "android/soong/tradefed" 35) 36 37func init() { 38 RegisterAppBuildComponents(android.InitRegistrationContext) 39 pctx.HostBinToolVariable("ModifyAllowlistCmd", "modify_permissions_allowlist") 40} 41 42var ( 43 modifyAllowlist = pctx.AndroidStaticRule("modifyAllowlist", 44 blueprint.RuleParams{ 45 Command: "${ModifyAllowlistCmd} $in $packageName $out", 46 CommandDeps: []string{"${ModifyAllowlistCmd}"}, 47 }, "packageName") 48) 49 50func RegisterAppBuildComponents(ctx android.RegistrationContext) { 51 ctx.RegisterModuleType("android_app", AndroidAppFactory) 52 ctx.RegisterModuleType("android_test", AndroidTestFactory) 53 ctx.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory) 54 ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory) 55 ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory) 56 ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory) 57} 58 59// AndroidManifest.xml merging 60// package splits 61 62type appProperties struct { 63 // Names of extra android_app_certificate modules to sign the apk with in the form ":module". 64 Additional_certificates []string 65 66 // If set, create package-export.apk, which other packages can 67 // use to get PRODUCT-agnostic resource data like IDs and type definitions. 68 Export_package_resources *bool 69 70 // Specifies that this app should be installed to the priv-app directory, 71 // where the system will grant it additional privileges not available to 72 // normal apps. 73 Privileged *bool 74 75 // list of resource labels to generate individual resource packages 76 Package_splits []string 77 78 // list of native libraries that will be provided in or alongside the resulting jar 79 Jni_libs []string `android:"arch_variant"` 80 81 // if true, use JNI libraries that link against platform APIs even if this module sets 82 // sdk_version. 83 Jni_uses_platform_apis *bool 84 85 // if true, use JNI libraries that link against SDK APIs even if this module does not set 86 // sdk_version. 87 Jni_uses_sdk_apis *bool 88 89 // STL library to use for JNI libraries. 90 Stl *string `android:"arch_variant"` 91 92 // Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest 93 // flag so that they are used from inside the APK at runtime. Defaults to true for android_test modules unless 94 // sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to true for 95 // android_app modules that are embedded to APEXes, defaults to false for other module types where the native 96 // libraries are generally preinstalled outside the APK. 97 Use_embedded_native_libs *bool 98 99 // Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that 100 // they are used from inside the APK at runtime. 101 Use_embedded_dex *bool 102 103 // Forces native libraries to always be packaged into the APK, 104 // Use_embedded_native_libs still selects whether they are stored uncompressed and aligned or compressed. 105 // True for android_test* modules. 106 AlwaysPackageNativeLibs bool `blueprint:"mutated"` 107 108 // If set, find and merge all NOTICE files that this module and its dependencies have and store 109 // it in the APK as an asset. 110 Embed_notices *bool 111 112 // cc.Coverage related properties 113 PreventInstall bool `blueprint:"mutated"` 114 IsCoverageVariant bool `blueprint:"mutated"` 115 116 // It can be set to test the behaviour of default target sdk version. 117 // Only required when updatable: false. It is an error if updatable: true and this is false. 118 Enforce_default_target_sdk_version *bool 119 120 // If set, the targetSdkVersion for the target is set to the latest default API level. 121 // This would be by default false, unless updatable: true or 122 // enforce_default_target_sdk_version: true in which case this defaults to true. 123 EnforceDefaultTargetSdkVersion bool `blueprint:"mutated"` 124 125 // Whether this app is considered mainline updatable or not. When set to true, this will enforce 126 // additional rules to make sure an app can safely be updated. Default is false. 127 // Prefer using other specific properties if build behaviour must be changed; avoid using this 128 // flag for anything but neverallow rules (unless the behaviour change is invisible to owners). 129 Updatable *bool 130 131 // Specifies the file that contains the allowlist for this app. 132 Privapp_allowlist *string `android:"path"` 133 134 // If set, create an RRO package which contains only resources having PRODUCT_CHARACTERISTICS 135 // and install the RRO package to /product partition, instead of passing --product argument 136 // to aapt2. Default is false. 137 // Setting this will make this APK identical to all targets, regardless of 138 // PRODUCT_CHARACTERISTICS. 139 Generate_product_characteristics_rro *bool 140 141 ProductCharacteristicsRROPackageName *string `blueprint:"mutated"` 142 ProductCharacteristicsRROManifestModuleName *string `blueprint:"mutated"` 143} 144 145// android_app properties that can be overridden by override_android_app 146type overridableAppProperties struct { 147 // The name of a certificate in the default certificate directory, blank to use the default product certificate, 148 // or an android_app_certificate module name in the form ":module". 149 Certificate *string 150 151 // Name of the signing certificate lineage file or filegroup module. 152 Lineage *string `android:"path"` 153 154 // For overriding the --rotation-min-sdk-version property of apksig 155 RotationMinSdkVersion *string 156 157 // the package name of this app. The package name in the manifest file is used if one was not given. 158 Package_name *string 159 160 // the logging parent of this app. 161 Logging_parent *string 162 163 // Whether to rename the package in resources to the override name rather than the base name. Defaults to true. 164 Rename_resources_package *bool 165 166 // Names of modules to be overridden. Listed modules can only be other binaries 167 // (in Make or Soong). 168 // This does not completely prevent installation of the overridden binaries, but if both 169 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed 170 // from PRODUCT_PACKAGES. 171 Overrides []string 172} 173 174type AndroidApp struct { 175 Library 176 aapt 177 android.OverridableModuleBase 178 179 certificate Certificate 180 181 appProperties appProperties 182 183 overridableAppProperties overridableAppProperties 184 185 jniLibs []jniLib 186 installPathForJNISymbols android.Path 187 embeddedJniLibs bool 188 jniCoverageOutputs android.Paths 189 190 bundleFile android.Path 191 192 // the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES. 193 installApkName string 194 195 installDir android.InstallPath 196 197 onDeviceDir string 198 199 additionalAaptFlags []string 200 201 overriddenManifestPackageName string 202 203 android.ApexBundleDepsInfo 204 205 javaApiUsedByOutputFile android.ModuleOutPath 206 207 privAppAllowlist android.OptionalPath 208} 209 210func (a *AndroidApp) IsInstallable() bool { 211 return Bool(a.properties.Installable) 212} 213 214func (a *AndroidApp) ResourcesNodeDepSet() *android.DepSet[*resourcesNode] { 215 return a.aapt.resourcesNodesDepSet 216} 217 218func (a *AndroidApp) OutputFile() android.Path { 219 return a.outputFile 220} 221 222func (a *AndroidApp) Certificate() Certificate { 223 return a.certificate 224} 225 226func (a *AndroidApp) JniCoverageOutputs() android.Paths { 227 return a.jniCoverageOutputs 228} 229 230func (a *AndroidApp) PrivAppAllowlist() android.OptionalPath { 231 return a.privAppAllowlist 232} 233 234var _ AndroidLibraryDependency = (*AndroidApp)(nil) 235 236type Certificate struct { 237 Pem, Key android.Path 238 presigned bool 239} 240 241var PresignedCertificate = Certificate{presigned: true} 242 243func (c Certificate) AndroidMkString() string { 244 if c.presigned { 245 return "PRESIGNED" 246 } else { 247 return c.Pem.String() 248 } 249} 250 251func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) { 252 if String(a.appProperties.Stl) == "c++_shared" && !a.SdkVersion(ctx).Specified() { 253 ctx.PropertyErrorf("stl", "sdk_version must be set in order to use c++_shared") 254 } 255 256 sdkDep := decodeSdkDep(ctx, android.SdkContext(a)) 257 a.usesLibrary.deps(ctx, sdkDep.hasFrameworkLibs()) 258 a.Module.deps(ctx) 259 if sdkDep.hasFrameworkLibs() { 260 a.aapt.deps(ctx, sdkDep) 261 } 262 263 usesSDK := a.SdkVersion(ctx).Specified() && a.SdkVersion(ctx).Kind != android.SdkCorePlatform 264 265 if usesSDK && Bool(a.appProperties.Jni_uses_sdk_apis) { 266 ctx.PropertyErrorf("jni_uses_sdk_apis", 267 "can only be set for modules that do not set sdk_version") 268 } else if !usesSDK && Bool(a.appProperties.Jni_uses_platform_apis) { 269 ctx.PropertyErrorf("jni_uses_platform_apis", 270 "can only be set for modules that set sdk_version") 271 } 272 273 for _, jniTarget := range ctx.MultiTargets() { 274 variation := append(jniTarget.Variations(), 275 blueprint.Variation{Mutator: "link", Variation: "shared"}) 276 277 // Test whether to use the SDK variant or the non-SDK variant of JNI dependencies. 278 // Many factors are considered here. 279 // 1. Basically, the selection follows whether the app has sdk_version set or not. 280 jniUsesSdkVariant := usesSDK 281 // 2. However, jni_uses_platform_apis and jni_uses_sdk_apis can override it 282 if Bool(a.appProperties.Jni_uses_sdk_apis) { 283 jniUsesSdkVariant = true 284 } 285 if Bool(a.appProperties.Jni_uses_platform_apis) { 286 jniUsesSdkVariant = false 287 } 288 // 3. Then the use of SDK variant is again prohibited for the following cases: 289 // 3.1. the app is shipped on unbundled partitions like vendor. Since the entire 290 // partition (not only the app) is considered unbudled, there's no need to use the 291 // SDK variant. 292 // 3.2. the app doesn't support embedding the JNI libs 293 if a.RequiresStableAPIs(ctx) || !a.shouldEmbedJnis(ctx) { 294 jniUsesSdkVariant = false 295 } 296 if jniUsesSdkVariant { 297 variation = append(variation, blueprint.Variation{Mutator: "sdk", Variation: "sdk"}) 298 } 299 300 // Use the installable dep tag when the JNIs are not embedded 301 var tag dependencyTag 302 if a.shouldEmbedJnis(ctx) { 303 tag = jniLibTag 304 } else { 305 tag = jniInstallTag 306 } 307 ctx.AddFarVariationDependencies(variation, tag, a.appProperties.Jni_libs...) 308 } 309 for _, aconfig_declaration := range a.aaptProperties.Flags_packages { 310 ctx.AddDependency(ctx.Module(), aconfigDeclarationTag, aconfig_declaration) 311 } 312} 313 314func (a *AndroidApp) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) { 315 cert := android.SrcIsModule(a.getCertString(ctx)) 316 if cert != "" { 317 ctx.AddDependency(ctx.Module(), certificateTag, cert) 318 } 319 320 if a.appProperties.Privapp_allowlist != nil && !Bool(a.appProperties.Privileged) { 321 // There are a few uids that are explicitly considered privileged regardless of their 322 // app's location. Bluetooth is one such app. It should arguably be moved to priv-app, 323 // but for now, allow it not to be in priv-app. 324 privilegedBecauseOfUid := ctx.ModuleName() == "Bluetooth" 325 if !privilegedBecauseOfUid { 326 ctx.PropertyErrorf("privapp_allowlist", "privileged must be set in order to use privapp_allowlist (with a few exceptions)") 327 } 328 } 329 330 for _, cert := range a.appProperties.Additional_certificates { 331 cert = android.SrcIsModule(cert) 332 if cert != "" { 333 ctx.AddDependency(ctx.Module(), certificateTag, cert) 334 } else { 335 ctx.PropertyErrorf("additional_certificates", 336 `must be names of android_app_certificate modules in the form ":module"`) 337 } 338 } 339} 340 341func (a *AndroidTestHelperApp) GenerateAndroidBuildActions(ctx android.ModuleContext) { 342 applicationId := a.appTestHelperAppProperties.Manifest_values.ApplicationId 343 if applicationId != nil { 344 if a.overridableAppProperties.Package_name != nil { 345 ctx.PropertyErrorf("manifest_values.applicationId", "property is not supported when property package_name is set.") 346 } 347 a.aapt.manifestValues.applicationId = *applicationId 348 } 349 a.generateAndroidBuildActions(ctx) 350 android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{ 351 TestOnly: true, 352 }) 353 354} 355 356func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) { 357 a.checkAppSdkVersions(ctx) 358 a.checkEmbedJnis(ctx) 359 a.generateAndroidBuildActions(ctx) 360 a.generateJavaUsedByApex(ctx) 361} 362 363func (a *AndroidApp) checkAppSdkVersions(ctx android.ModuleContext) { 364 if a.Updatable() { 365 if !a.SdkVersion(ctx).Stable() { 366 ctx.PropertyErrorf("sdk_version", "Updatable apps must use stable SDKs, found %v", a.SdkVersion(ctx)) 367 } 368 if String(a.overridableProperties.Min_sdk_version) == "" { 369 ctx.PropertyErrorf("updatable", "updatable apps must set min_sdk_version.") 370 } 371 372 if minSdkVersion, err := a.MinSdkVersion(ctx).EffectiveVersion(ctx); err == nil { 373 a.checkJniLibsSdkVersion(ctx, minSdkVersion) 374 android.CheckMinSdkVersion(ctx, minSdkVersion, a.WalkPayloadDeps) 375 } else { 376 ctx.PropertyErrorf("min_sdk_version", "%s", err.Error()) 377 } 378 379 if !BoolDefault(a.appProperties.Enforce_default_target_sdk_version, true) { 380 ctx.PropertyErrorf("enforce_default_target_sdk_version", "Updatable apps must enforce default target sdk version") 381 } 382 // TODO(b/227460469) after all the modules removes the target sdk version, throw an error if the target sdk version is explicitly set. 383 if a.deviceProperties.Target_sdk_version == nil { 384 a.SetEnforceDefaultTargetSdkVersion(true) 385 } 386 } 387 388 a.checkPlatformAPI(ctx) 389 a.checkSdkVersions(ctx) 390} 391 392// Ensures that use_embedded_native_libs are set for apk-in-apex 393func (a *AndroidApp) checkEmbedJnis(ctx android.BaseModuleContext) { 394 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider) 395 apkInApex := !apexInfo.IsForPlatform() 396 hasJnis := len(a.appProperties.Jni_libs) > 0 397 398 if apkInApex && hasJnis && !Bool(a.appProperties.Use_embedded_native_libs) { 399 ctx.ModuleErrorf("APK in APEX should have use_embedded_native_libs: true") 400 } 401} 402 403// If an updatable APK sets min_sdk_version, min_sdk_vesion of JNI libs should match with it. 404// This check is enforced for "updatable" APKs (including APK-in-APEX). 405func (a *AndroidApp) checkJniLibsSdkVersion(ctx android.ModuleContext, minSdkVersion android.ApiLevel) { 406 // It's enough to check direct JNI deps' sdk_version because all transitive deps from JNI deps are checked in cc.checkLinkType() 407 ctx.VisitDirectDeps(func(m android.Module) { 408 if !IsJniDepTag(ctx.OtherModuleDependencyTag(m)) { 409 return 410 } 411 dep, _ := m.(*cc.Module) 412 // The domain of cc.sdk_version is "current" and <number> 413 // We can rely on android.SdkSpec to convert it to <number> so that "current" is 414 // handled properly regardless of sdk finalization. 415 jniSdkVersion, err := android.SdkSpecFrom(ctx, dep.MinSdkVersion()).EffectiveVersion(ctx) 416 if err != nil || minSdkVersion.LessThan(jniSdkVersion) { 417 ctx.OtherModuleErrorf(dep, "min_sdk_version(%v) is higher than min_sdk_version(%v) of the containing android_app(%v)", 418 dep.MinSdkVersion(), minSdkVersion, ctx.ModuleName()) 419 return 420 } 421 422 }) 423} 424 425// Returns true if the native libraries should be stored in the APK uncompressed and the 426// extractNativeLibs application flag should be set to false in the manifest. 427func (a *AndroidApp) useEmbeddedNativeLibs(ctx android.ModuleContext) bool { 428 minSdkVersion, err := a.MinSdkVersion(ctx).EffectiveVersion(ctx) 429 if err != nil { 430 ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.MinSdkVersion(ctx), err) 431 } 432 433 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider) 434 return (minSdkVersion.FinalOrFutureInt() >= 23 && Bool(a.appProperties.Use_embedded_native_libs)) || 435 !apexInfo.IsForPlatform() 436} 437 438// Returns whether this module should have the dex file stored uncompressed in the APK. 439func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool { 440 if Bool(a.appProperties.Use_embedded_dex) { 441 return true 442 } 443 444 // Uncompress dex in APKs of privileged apps (even for unbundled builds, they may 445 // be preinstalled as prebuilts). 446 if ctx.Config().UncompressPrivAppDex() && a.Privileged() { 447 return true 448 } 449 450 if ctx.Config().UnbundledBuild() { 451 return false 452 } 453 454 return shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &a.dexpreopter) 455} 456 457func (a *AndroidApp) shouldEmbedJnis(ctx android.BaseModuleContext) bool { 458 return ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) || 459 Bool(a.appProperties.Updatable) || 460 a.appProperties.AlwaysPackageNativeLibs 461} 462 463func generateAaptRenamePackageFlags(packageName string, renameResourcesPackage bool) []string { 464 aaptFlags := []string{"--rename-manifest-package " + packageName} 465 if renameResourcesPackage { 466 // Required to rename the package name in the resources table. 467 aaptFlags = append(aaptFlags, "--rename-resources-package "+packageName) 468 } 469 return aaptFlags 470} 471 472func (a *AndroidApp) OverriddenManifestPackageName() string { 473 return a.overriddenManifestPackageName 474} 475 476func (a *AndroidApp) renameResourcesPackage() bool { 477 return proptools.BoolDefault(a.overridableAppProperties.Rename_resources_package, true) 478} 479 480func getAconfigFilePaths(ctx android.ModuleContext) (aconfigTextFilePaths android.Paths) { 481 ctx.VisitDirectDepsWithTag(aconfigDeclarationTag, func(dep android.Module) { 482 if provider, ok := android.OtherModuleProvider(ctx, dep, android.AconfigDeclarationsProviderKey); ok { 483 aconfigTextFilePaths = append(aconfigTextFilePaths, provider.IntermediateDumpOutputPath) 484 } else { 485 ctx.ModuleErrorf("Only aconfig_declarations module type is allowed for "+ 486 "flags_packages property, but %s is not aconfig_declarations module type", 487 dep.Name(), 488 ) 489 } 490 }) 491 492 return aconfigTextFilePaths 493} 494 495func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) { 496 usePlatformAPI := proptools.Bool(a.Module.deviceProperties.Platform_apis) 497 if ctx.Module().(android.SdkContext).SdkVersion(ctx).Kind == android.SdkModule { 498 usePlatformAPI = true 499 } 500 a.aapt.usesNonSdkApis = usePlatformAPI 501 502 // Ask manifest_fixer to add or update the application element indicating this app has no code. 503 a.aapt.hasNoCode = !a.hasCode(ctx) 504 505 aaptLinkFlags := []string{} 506 507 // Add TARGET_AAPT_CHARACTERISTICS values to AAPT link flags if they exist and --product flags were not provided. 508 autogenerateRRO := proptools.Bool(a.appProperties.Generate_product_characteristics_rro) 509 hasProduct := android.PrefixInList(a.aaptProperties.Aaptflags, "--product") 510 characteristics := ctx.Config().ProductAAPTCharacteristics() 511 if !autogenerateRRO && !hasProduct && len(characteristics) > 0 && characteristics != "default" { 512 aaptLinkFlags = append(aaptLinkFlags, "--product", characteristics) 513 } 514 515 if !Bool(a.aaptProperties.Aapt_include_all_resources) { 516 // Product AAPT config 517 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() { 518 aaptLinkFlags = append(aaptLinkFlags, "-c", aaptConfig) 519 } 520 521 // Product AAPT preferred config 522 if len(ctx.Config().ProductAAPTPreferredConfig()) > 0 { 523 aaptLinkFlags = append(aaptLinkFlags, "--preferred-density", ctx.Config().ProductAAPTPreferredConfig()) 524 } 525 } 526 527 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName()) 528 if overridden || a.overridableAppProperties.Package_name != nil { 529 // The product override variable has a priority over the package_name property. 530 if !overridden { 531 manifestPackageName = *a.overridableAppProperties.Package_name 532 } 533 aaptLinkFlags = append(aaptLinkFlags, generateAaptRenamePackageFlags(manifestPackageName, a.renameResourcesPackage())...) 534 a.overriddenManifestPackageName = manifestPackageName 535 } 536 537 aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...) 538 539 a.aapt.splitNames = a.appProperties.Package_splits 540 a.aapt.LoggingParent = String(a.overridableAppProperties.Logging_parent) 541 if a.Updatable() { 542 if override := ctx.Config().Getenv("OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION"); override != "" { 543 a.aapt.defaultManifestVersion = override 544 } else { 545 a.aapt.defaultManifestVersion = android.DefaultUpdatableModuleVersion 546 } 547 } 548 549 // Use non final ids if we are doing optimized shrinking and are using R8. 550 nonFinalIds := a.dexProperties.optimizedResourceShrinkingEnabled(ctx) && a.dexer.effectiveOptimizeEnabled() 551 a.aapt.buildActions(ctx, 552 aaptBuildActionOptions{ 553 sdkContext: android.SdkContext(a), 554 classLoaderContexts: a.classLoaderContexts, 555 excludedLibs: a.usesLibraryProperties.Exclude_uses_libs, 556 enforceDefaultTargetSdkVersion: a.enforceDefaultTargetSdkVersion(), 557 forceNonFinalResourceIDs: nonFinalIds, 558 extraLinkFlags: aaptLinkFlags, 559 aconfigTextFiles: getAconfigFilePaths(ctx), 560 usesLibrary: &a.usesLibrary, 561 }, 562 ) 563 564 // apps manifests are handled by aapt, don't let Module see them 565 a.properties.Manifest = nil 566} 567 568func (a *AndroidApp) proguardBuildActions(ctx android.ModuleContext) { 569 var staticLibProguardFlagFiles android.Paths 570 ctx.VisitDirectDeps(func(m android.Module) { 571 depProguardInfo, _ := android.OtherModuleProvider(ctx, m, ProguardSpecInfoProvider) 572 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, depProguardInfo.UnconditionallyExportedProguardFlags.ToList()...) 573 if ctx.OtherModuleDependencyTag(m) == staticLibTag { 574 staticLibProguardFlagFiles = append(staticLibProguardFlagFiles, depProguardInfo.ProguardFlagsFiles.ToList()...) 575 } 576 }) 577 578 staticLibProguardFlagFiles = android.FirstUniquePaths(staticLibProguardFlagFiles) 579 580 a.Module.extraProguardFlagsFiles = append(a.Module.extraProguardFlagsFiles, staticLibProguardFlagFiles...) 581 if !(a.dexProperties.optimizedResourceShrinkingEnabled(ctx)) { 582 // When using the optimized shrinking the R8 enqueuer will traverse the xml files that become 583 // live for code references and (transitively) mark these as live. 584 // In this case we explicitly don't wan't the aapt2 generated keep files (which would keep the now 585 // dead code alive) 586 a.Module.extraProguardFlagsFiles = append(a.Module.extraProguardFlagsFiles, a.proguardOptionsFile) 587 } 588} 589 590func (a *AndroidApp) installPath(ctx android.ModuleContext) android.InstallPath { 591 var installDir string 592 if ctx.ModuleName() == "framework-res" { 593 // framework-res.apk is installed as system/framework/framework-res.apk 594 installDir = "framework" 595 } else if a.Privileged() { 596 installDir = filepath.Join("priv-app", a.installApkName) 597 } else { 598 installDir = filepath.Join("app", a.installApkName) 599 } 600 601 return android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk") 602} 603 604func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) (android.Path, android.Path) { 605 a.dexpreopter.installPath = a.installPath(ctx) 606 a.dexpreopter.isApp = true 607 if a.dexProperties.Uncompress_dex == nil { 608 // If the value was not force-set by the user, use reasonable default based on the module. 609 a.dexProperties.Uncompress_dex = proptools.BoolPtr(a.shouldUncompressDex(ctx)) 610 } 611 a.dexpreopter.uncompressedDex = *a.dexProperties.Uncompress_dex 612 a.dexpreopter.enforceUsesLibs = a.usesLibrary.enforceUsesLibraries() 613 a.dexpreopter.classLoaderContexts = a.classLoaderContexts 614 a.dexpreopter.manifestFile = a.mergedManifestFile 615 a.dexpreopter.preventInstall = a.appProperties.PreventInstall 616 617 var packageResources = a.exportPackage 618 619 if ctx.ModuleName() != "framework-res" { 620 if a.dexProperties.resourceShrinkingEnabled(ctx) { 621 protoFile := android.PathForModuleOut(ctx, packageResources.Base()+".proto.apk") 622 aapt2Convert(ctx, protoFile, packageResources, "proto") 623 a.dexer.resourcesInput = android.OptionalPathForPath(protoFile) 624 } 625 626 var extraSrcJars android.Paths 627 var extraClasspathJars android.Paths 628 var extraCombinedJars android.Paths 629 if a.useResourceProcessorBusyBox(ctx) { 630 // When building an app with ResourceProcessorBusyBox enabled ResourceProcessorBusyBox has already 631 // created R.class files that provide IDs for resources in busybox/R.jar. Pass that file in the 632 // classpath when compiling everything else, and add it to the final classes jar. 633 extraClasspathJars = android.Paths{a.aapt.rJar} 634 extraCombinedJars = android.Paths{a.aapt.rJar} 635 } else { 636 // When building an app without ResourceProcessorBusyBox the aapt2 rule creates R.srcjar containing 637 // R.java files for the app's package and the packages from all transitive static android_library 638 // dependencies. Compile the srcjar alongside the rest of the sources. 639 extraSrcJars = android.Paths{a.aapt.aaptSrcJar} 640 } 641 642 a.Module.compile(ctx, extraSrcJars, extraClasspathJars, extraCombinedJars) 643 if a.dexProperties.resourceShrinkingEnabled(ctx) { 644 binaryResources := android.PathForModuleOut(ctx, packageResources.Base()+".binary.out.apk") 645 aapt2Convert(ctx, binaryResources, a.dexer.resourcesOutput.Path(), "binary") 646 packageResources = binaryResources 647 } 648 } 649 650 return a.dexJarFile.PathOrNil(), packageResources 651} 652 653func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, prebuiltJniPackages android.Paths, ctx android.ModuleContext) android.WritablePath { 654 var jniJarFile android.WritablePath 655 if len(jniLibs) > 0 || len(prebuiltJniPackages) > 0 { 656 a.jniLibs = jniLibs 657 if a.shouldEmbedJnis(ctx) { 658 jniJarFile = android.PathForModuleOut(ctx, "jnilibs.zip") 659 a.installPathForJNISymbols = a.installPath(ctx) 660 TransformJniLibsToJar(ctx, jniJarFile, jniLibs, prebuiltJniPackages, a.useEmbeddedNativeLibs(ctx)) 661 for _, jni := range jniLibs { 662 if jni.coverageFile.Valid() { 663 // Only collect coverage for the first target arch if this is a multilib target. 664 // TODO(jungjw): Ideally, we want to collect both reports, but that would cause coverage 665 // data file path collisions since the current coverage file path format doesn't contain 666 // arch-related strings. This is fine for now though; the code coverage team doesn't use 667 // multi-arch targets such as test_suite_* for coverage collections yet. 668 // 669 // Work with the team to come up with a new format that handles multilib modules properly 670 // and change this. 671 if len(ctx.Config().Targets[android.Android]) == 1 || 672 ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType == jni.target.Arch.ArchType { 673 a.jniCoverageOutputs = append(a.jniCoverageOutputs, jni.coverageFile.Path()) 674 } 675 } 676 } 677 a.embeddedJniLibs = true 678 } 679 } 680 return jniJarFile 681} 682 683func (a *AndroidApp) JNISymbolsInstalls(installPath string) android.RuleBuilderInstalls { 684 var jniSymbols android.RuleBuilderInstalls 685 for _, jniLib := range a.jniLibs { 686 if jniLib.unstrippedFile != nil { 687 jniSymbols = append(jniSymbols, android.RuleBuilderInstall{ 688 From: jniLib.unstrippedFile, 689 To: filepath.Join(installPath, targetToJniDir(jniLib.target), jniLib.unstrippedFile.Base()), 690 }) 691 } 692 } 693 return jniSymbols 694} 695 696// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it 697// isn't a cert module reference. Also checks and enforces system cert restriction if applicable. 698func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, 699 ctx android.ModuleContext) (mainCertificate Certificate, allCertificates []Certificate) { 700 if android.SrcIsModule(certPropValue) == "" { 701 var mainCert Certificate 702 if certPropValue != "" { 703 defaultDir := ctx.Config().DefaultAppCertificateDir(ctx) 704 mainCert = Certificate{ 705 Pem: defaultDir.Join(ctx, certPropValue+".x509.pem"), 706 Key: defaultDir.Join(ctx, certPropValue+".pk8"), 707 } 708 } else { 709 pem, key := ctx.Config().DefaultAppCertificate(ctx) 710 mainCert = Certificate{ 711 Pem: pem, 712 Key: key, 713 } 714 } 715 certificates = append([]Certificate{mainCert}, certificates...) 716 } 717 718 if len(certificates) > 0 { 719 mainCertificate = certificates[0] 720 } else { 721 // This can be reached with an empty certificate list if AllowMissingDependencies is set 722 // and the certificate property for this module is a module reference to a missing module. 723 if !ctx.Config().AllowMissingDependencies() && len(ctx.GetMissingDependencies()) > 0 { 724 panic("Should only get here if AllowMissingDependencies set and there are missing dependencies") 725 } 726 // Set a certificate to avoid panics later when accessing it. 727 mainCertificate = Certificate{ 728 Key: android.PathForModuleOut(ctx, "missing.pk8"), 729 Pem: android.PathForModuleOut(ctx, "missing.x509.pem"), 730 } 731 } 732 733 if !m.Platform() { 734 certPath := mainCertificate.Pem.String() 735 systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String() 736 if strings.HasPrefix(certPath, systemCertPath) { 737 enforceSystemCert := ctx.Config().EnforceSystemCertificate() 738 allowed := ctx.Config().EnforceSystemCertificateAllowList() 739 740 if enforceSystemCert && !inList(m.Name(), allowed) { 741 ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.") 742 } 743 } 744 } 745 746 return mainCertificate, certificates 747} 748 749func (a *AndroidApp) InstallApkName() string { 750 return a.installApkName 751} 752 753func (a *AndroidApp) createPrivappAllowlist(ctx android.ModuleContext) android.Path { 754 if a.appProperties.Privapp_allowlist == nil { 755 return nil 756 } 757 758 isOverrideApp := a.GetOverriddenBy() != "" 759 if !isOverrideApp { 760 // if this is not an override, we don't need to rewrite the existing privapp allowlist 761 return android.PathForModuleSrc(ctx, *a.appProperties.Privapp_allowlist) 762 } 763 764 if a.overridableAppProperties.Package_name == nil { 765 ctx.PropertyErrorf("privapp_allowlist", "package_name must be set to use privapp_allowlist") 766 } 767 768 packageName := *a.overridableAppProperties.Package_name 769 fileName := "privapp_allowlist_" + packageName + ".xml" 770 outPath := android.PathForModuleOut(ctx, fileName).OutputPath 771 ctx.Build(pctx, android.BuildParams{ 772 Rule: modifyAllowlist, 773 Input: android.PathForModuleSrc(ctx, *a.appProperties.Privapp_allowlist), 774 Output: outPath, 775 Args: map[string]string{ 776 "packageName": packageName, 777 }, 778 }) 779 return &outPath 780} 781 782func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) { 783 var apkDeps android.Paths 784 785 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider) 786 if !apexInfo.IsForPlatform() { 787 a.hideApexVariantFromMake = true 788 } 789 790 a.aapt.useEmbeddedNativeLibs = a.useEmbeddedNativeLibs(ctx) 791 a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex) 792 793 // Unlike installApkName, a.stem should respect base module name for override_android_app. 794 // Therefore, use ctx.ModuleName() instead of a.Name(). 795 a.stem = proptools.StringDefault(a.overridableProperties.Stem, ctx.ModuleName()) 796 797 // Check if the install APK name needs to be overridden. 798 // Both android_app and override_android_app module are expected to possess 799 // its module bound apk path. However, override_android_app inherits ctx.ModuleName() 800 // from the base module. Therefore, use a.Name() which represents 801 // the module name for both android_app and override_android_app. 802 a.installApkName = ctx.DeviceConfig().OverridePackageNameFor( 803 proptools.StringDefault(a.overridableProperties.Stem, a.Name())) 804 805 if ctx.ModuleName() == "framework-res" { 806 // framework-res.apk is installed as system/framework/framework-res.apk 807 a.installDir = android.PathForModuleInstall(ctx, "framework") 808 } else if a.Privileged() { 809 a.installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName) 810 } else if ctx.InstallInTestcases() { 811 a.installDir = android.PathForModuleInstall(ctx, a.installApkName, ctx.DeviceConfig().DeviceArch()) 812 } else { 813 a.installDir = android.PathForModuleInstall(ctx, "app", a.installApkName) 814 } 815 a.onDeviceDir = android.InstallPathToOnDevicePath(ctx, a.installDir) 816 817 a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx) 818 if a.usesLibrary.shouldDisableDexpreopt { 819 a.dexpreopter.disableDexpreopt() 820 } 821 822 var noticeAssetPath android.WritablePath 823 if Bool(a.appProperties.Embed_notices) || ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") { 824 // The rule to create the notice file can't be generated yet, as the final output path 825 // for the apk isn't known yet. Add the path where the notice file will be generated to the 826 // aapt rules now before calling aaptBuildActions, the rule to create the notice file will 827 // be generated later. 828 noticeAssetPath = android.PathForModuleOut(ctx, "NOTICE", "NOTICE.html.gz") 829 a.aapt.noticeFile = android.OptionalPathForPath(noticeAssetPath) 830 } 831 832 // For apps targeting latest target_sdk_version 833 if Bool(a.appProperties.Enforce_default_target_sdk_version) { 834 a.SetEnforceDefaultTargetSdkVersion(true) 835 } 836 837 // Process all building blocks, from AAPT to certificates. 838 a.aaptBuildActions(ctx) 839 // The decision to enforce <uses-library> checks is made before adding implicit SDK libraries. 840 a.usesLibrary.freezeEnforceUsesLibraries() 841 842 // Check that the <uses-library> list is coherent with the manifest. 843 if a.usesLibrary.enforceUsesLibraries() { 844 manifestCheckFile := a.usesLibrary.verifyUsesLibrariesManifest( 845 ctx, a.mergedManifestFile, &a.classLoaderContexts) 846 apkDeps = append(apkDeps, manifestCheckFile) 847 } 848 849 a.proguardBuildActions(ctx) 850 851 a.linter.mergedManifest = a.aapt.mergedManifestFile 852 a.linter.manifest = a.aapt.manifestPath 853 a.linter.resources = a.aapt.resourceFiles 854 a.linter.buildModuleReportZip = ctx.Config().UnbundledBuildApps() 855 856 dexJarFile, packageResources := a.dexBuildActions(ctx) 857 858 // No need to check the SDK version of the JNI deps unless we embed them 859 checkNativeSdkVersion := a.shouldEmbedJnis(ctx) && !Bool(a.appProperties.Jni_uses_platform_apis) 860 jniLibs, prebuiltJniPackages, certificates := collectAppDeps(ctx, a, a.shouldEmbedJnis(ctx), checkNativeSdkVersion) 861 jniJarFile := a.jniBuildActions(jniLibs, prebuiltJniPackages, ctx) 862 863 if ctx.Failed() { 864 return 865 } 866 867 a.certificate, certificates = processMainCert(a.ModuleBase, a.getCertString(ctx), certificates, ctx) 868 869 // Build a final signed app package. 870 packageFile := android.PathForModuleOut(ctx, a.installApkName+".apk") 871 v4SigningRequested := Bool(a.Module.deviceProperties.V4_signature) 872 var v4SignatureFile android.WritablePath = nil 873 if v4SigningRequested { 874 v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+".apk.idsig") 875 } 876 var lineageFile android.Path 877 if lineage := String(a.overridableAppProperties.Lineage); lineage != "" { 878 lineageFile = android.PathForModuleSrc(ctx, lineage) 879 } 880 rotationMinSdkVersion := String(a.overridableAppProperties.RotationMinSdkVersion) 881 882 CreateAndSignAppPackage(ctx, packageFile, packageResources, jniJarFile, dexJarFile, certificates, apkDeps, v4SignatureFile, lineageFile, rotationMinSdkVersion) 883 a.outputFile = packageFile 884 if v4SigningRequested { 885 a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile) 886 } 887 888 if a.aapt.noticeFile.Valid() { 889 // Generating the notice file rule has to be here after a.outputFile is known. 890 noticeFile := android.PathForModuleOut(ctx, "NOTICE.html.gz") 891 android.BuildNoticeHtmlOutputFromLicenseMetadata( 892 ctx, noticeFile, "", "", 893 []string{ 894 a.installDir.String() + "/", 895 android.PathForModuleInstall(ctx).String() + "/", 896 a.outputFile.String(), 897 }) 898 builder := android.NewRuleBuilder(pctx, ctx) 899 builder.Command().Text("cp"). 900 Input(noticeFile). 901 Output(noticeAssetPath) 902 builder.Build("notice_dir", "Building notice dir") 903 } 904 905 for _, split := range a.aapt.splits { 906 // Sign the split APKs 907 packageFile := android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk") 908 if v4SigningRequested { 909 v4SignatureFile = android.PathForModuleOut(ctx, a.installApkName+"_"+split.suffix+".apk.idsig") 910 } 911 CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates, apkDeps, v4SignatureFile, lineageFile, rotationMinSdkVersion) 912 a.extraOutputFiles = append(a.extraOutputFiles, packageFile) 913 if v4SigningRequested { 914 a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile) 915 } 916 } 917 918 // Build an app bundle. 919 bundleFile := android.PathForModuleOut(ctx, "base.zip") 920 BuildBundleModule(ctx, bundleFile, a.exportPackage, jniJarFile, dexJarFile) 921 a.bundleFile = bundleFile 922 923 allowlist := a.createPrivappAllowlist(ctx) 924 if allowlist != nil { 925 a.privAppAllowlist = android.OptionalPathForPath(allowlist) 926 } 927 928 // Install the app package. 929 shouldInstallAppPackage := (Bool(a.Module.properties.Installable) || ctx.Host()) && apexInfo.IsForPlatform() && !a.appProperties.PreventInstall 930 if shouldInstallAppPackage { 931 if a.privAppAllowlist.Valid() { 932 allowlistInstallPath := android.PathForModuleInstall(ctx, "etc", "permissions") 933 allowlistInstallFilename := a.installApkName + ".xml" 934 ctx.InstallFile(allowlistInstallPath, allowlistInstallFilename, a.privAppAllowlist.Path()) 935 } 936 937 var extraInstalledPaths android.InstallPaths 938 for _, extra := range a.extraOutputFiles { 939 installed := ctx.InstallFile(a.installDir, extra.Base(), extra) 940 extraInstalledPaths = append(extraInstalledPaths, installed) 941 } 942 // If we don't embed jni libs, make sure that those are installed along with the 943 // app, and also place symlinks to the installed paths under the lib/<arch> 944 // directory of the app installation directory. ex: 945 // /system/app/MyApp/lib/arm64/libfoo.so -> /system/lib64/libfoo.so 946 if !a.embeddedJniLibs { 947 for _, jniLib := range jniLibs { 948 archStr := jniLib.target.Arch.ArchType.String() 949 symlinkDir := a.installDir.Join(ctx, "lib", archStr) 950 for _, installedLib := range jniLib.installPaths { 951 // install the symlink itself 952 symlinkName := installedLib.Base() 953 symlinkTarget := android.InstallPathToOnDevicePath(ctx, installedLib) 954 ctx.InstallAbsoluteSymlink(symlinkDir, symlinkName, symlinkTarget) 955 } 956 } 957 } 958 ctx.InstallFile(a.installDir, a.outputFile.Base(), a.outputFile, extraInstalledPaths...) 959 } 960 961 a.buildAppDependencyInfo(ctx) 962 963 providePrebuiltInfo(ctx, 964 prebuiltInfoProps{ 965 baseModuleName: a.BaseModuleName(), 966 isPrebuilt: false, 967 }, 968 ) 969} 970 971type appDepsInterface interface { 972 SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec 973 MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel 974 RequiresStableAPIs(ctx android.BaseModuleContext) bool 975} 976 977func collectAppDeps(ctx android.ModuleContext, app appDepsInterface, 978 shouldCollectRecursiveNativeDeps bool, 979 checkNativeSdkVersion bool) ([]jniLib, android.Paths, []Certificate) { 980 981 if checkNativeSdkVersion { 982 checkNativeSdkVersion = app.SdkVersion(ctx).Specified() && 983 app.SdkVersion(ctx).Kind != android.SdkCorePlatform && !app.RequiresStableAPIs(ctx) 984 } 985 jniLib, prebuiltJniPackages := collectJniDeps(ctx, shouldCollectRecursiveNativeDeps, 986 checkNativeSdkVersion, func(dep cc.LinkableInterface) bool { 987 return !dep.IsNdk(ctx.Config()) && !dep.IsStubs() 988 }) 989 990 var certificates []Certificate 991 992 ctx.VisitDirectDeps(func(module android.Module) { 993 otherName := ctx.OtherModuleName(module) 994 tag := ctx.OtherModuleDependencyTag(module) 995 996 if tag == certificateTag { 997 if dep, ok := module.(*AndroidAppCertificate); ok { 998 certificates = append(certificates, dep.Certificate) 999 } else { 1000 ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName) 1001 } 1002 } 1003 }) 1004 return jniLib, prebuiltJniPackages, certificates 1005} 1006 1007func collectJniDeps(ctx android.ModuleContext, 1008 shouldCollectRecursiveNativeDeps bool, 1009 checkNativeSdkVersion bool, 1010 filter func(cc.LinkableInterface) bool) ([]jniLib, android.Paths) { 1011 var jniLibs []jniLib 1012 var prebuiltJniPackages android.Paths 1013 seenModulePaths := make(map[string]bool) 1014 1015 ctx.WalkDeps(func(module android.Module, parent android.Module) bool { 1016 otherName := ctx.OtherModuleName(module) 1017 tag := ctx.OtherModuleDependencyTag(module) 1018 1019 if IsJniDepTag(tag) || cc.IsSharedDepTag(tag) { 1020 if dep, ok := module.(cc.LinkableInterface); ok { 1021 if filter != nil && !filter(dep) { 1022 return false 1023 } 1024 1025 lib := dep.OutputFile() 1026 if lib.Valid() { 1027 path := lib.Path() 1028 if seenModulePaths[path.String()] { 1029 return false 1030 } 1031 seenModulePaths[path.String()] = true 1032 1033 if checkNativeSdkVersion && dep.SdkVersion() == "" { 1034 ctx.PropertyErrorf("jni_libs", "JNI dependency %q uses platform APIs, but this module does not", 1035 otherName) 1036 } 1037 1038 jniLibs = append(jniLibs, jniLib{ 1039 name: ctx.OtherModuleName(module), 1040 path: path, 1041 target: module.Target(), 1042 coverageFile: dep.CoverageOutputFile(), 1043 unstrippedFile: dep.UnstrippedOutputFile(), 1044 partition: dep.Partition(), 1045 installPaths: dep.FilesToInstall(), 1046 }) 1047 } else if ctx.Config().AllowMissingDependencies() { 1048 ctx.AddMissingDependencies([]string{otherName}) 1049 } else { 1050 ctx.ModuleErrorf("dependency %q missing output file", otherName) 1051 } 1052 } else { 1053 ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName) 1054 } 1055 1056 return shouldCollectRecursiveNativeDeps 1057 } 1058 1059 if info, ok := android.OtherModuleProvider(ctx, module, JniPackageProvider); ok { 1060 prebuiltJniPackages = append(prebuiltJniPackages, info.JniPackages...) 1061 } 1062 1063 return false 1064 }) 1065 1066 return jniLibs, prebuiltJniPackages 1067} 1068 1069func (a *AndroidApp) WalkPayloadDeps(ctx android.ModuleContext, do android.PayloadDepsCallback) { 1070 ctx.WalkDeps(func(child, parent android.Module) bool { 1071 isExternal := !a.DepIsInSameApex(ctx, child) 1072 if am, ok := child.(android.ApexModule); ok { 1073 if !do(ctx, parent, am, isExternal) { 1074 return false 1075 } 1076 } 1077 return !isExternal 1078 }) 1079} 1080 1081func (a *AndroidApp) buildAppDependencyInfo(ctx android.ModuleContext) { 1082 if ctx.Host() { 1083 return 1084 } 1085 1086 depsInfo := android.DepNameToDepInfoMap{} 1087 a.WalkPayloadDeps(ctx, func(ctx android.ModuleContext, from blueprint.Module, to android.ApexModule, externalDep bool) bool { 1088 depName := to.Name() 1089 1090 // Skip dependencies that are only available to APEXes; they are developed with updatability 1091 // in mind and don't need manual approval. 1092 if to.(android.ApexModule).NotAvailableForPlatform() { 1093 return true 1094 } 1095 1096 if info, exist := depsInfo[depName]; exist { 1097 info.From = append(info.From, from.Name()) 1098 info.IsExternal = info.IsExternal && externalDep 1099 depsInfo[depName] = info 1100 } else { 1101 toMinSdkVersion := "(no version)" 1102 if m, ok := to.(interface { 1103 MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel 1104 }); ok { 1105 if v := m.MinSdkVersion(ctx); !v.IsNone() { 1106 toMinSdkVersion = v.String() 1107 } 1108 } else if m, ok := to.(interface{ MinSdkVersion() string }); ok { 1109 // TODO(b/175678607) eliminate the use of MinSdkVersion returning 1110 // string 1111 if v := m.MinSdkVersion(); v != "" { 1112 toMinSdkVersion = v 1113 } 1114 } 1115 depsInfo[depName] = android.ApexModuleDepInfo{ 1116 To: depName, 1117 From: []string{from.Name()}, 1118 IsExternal: externalDep, 1119 MinSdkVersion: toMinSdkVersion, 1120 } 1121 } 1122 return true 1123 }) 1124 1125 a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, a.MinSdkVersion(ctx).String(), depsInfo) 1126} 1127 1128func (a *AndroidApp) enforceDefaultTargetSdkVersion() bool { 1129 return a.appProperties.EnforceDefaultTargetSdkVersion 1130} 1131 1132func (a *AndroidApp) SetEnforceDefaultTargetSdkVersion(val bool) { 1133 a.appProperties.EnforceDefaultTargetSdkVersion = val 1134} 1135 1136func (a *AndroidApp) Updatable() bool { 1137 return Bool(a.appProperties.Updatable) 1138} 1139 1140func (a *AndroidApp) SetUpdatable(val bool) { 1141 a.appProperties.Updatable = &val 1142} 1143 1144func (a *AndroidApp) getCertString(ctx android.BaseModuleContext) string { 1145 certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName()) 1146 if overridden { 1147 return ":" + certificate 1148 } 1149 return String(a.overridableAppProperties.Certificate) 1150} 1151 1152func (a *AndroidApp) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool { 1153 if IsJniDepTag(ctx.OtherModuleDependencyTag(dep)) { 1154 return true 1155 } 1156 return a.Library.DepIsInSameApex(ctx, dep) 1157} 1158 1159// For OutputFileProducer interface 1160func (a *AndroidApp) OutputFiles(tag string) (android.Paths, error) { 1161 switch tag { 1162 // In some instances, it can be useful to reference the aapt-generated flags from another 1163 // target, e.g., system server implements services declared in the framework-res manifest. 1164 case ".aapt.proguardOptionsFile": 1165 return []android.Path{a.proguardOptionsFile}, nil 1166 case ".aapt.srcjar": 1167 if a.aaptSrcJar != nil { 1168 return []android.Path{a.aaptSrcJar}, nil 1169 } 1170 case ".aapt.jar": 1171 if a.rJar != nil { 1172 return []android.Path{a.rJar}, nil 1173 } 1174 case ".apk": 1175 return []android.Path{a.outputFile}, nil 1176 case ".export-package.apk": 1177 return []android.Path{a.exportPackage}, nil 1178 case ".manifest.xml": 1179 return []android.Path{a.aapt.manifestPath}, nil 1180 } 1181 return a.Library.OutputFiles(tag) 1182} 1183 1184func (a *AndroidApp) Privileged() bool { 1185 return Bool(a.appProperties.Privileged) 1186} 1187 1188func (a *AndroidApp) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool { 1189 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled() 1190} 1191 1192func (a *AndroidApp) SetPreventInstall() { 1193 a.appProperties.PreventInstall = true 1194} 1195 1196func (a *AndroidApp) MarkAsCoverageVariant(coverage bool) { 1197 a.appProperties.IsCoverageVariant = coverage 1198} 1199 1200func (a *AndroidApp) EnableCoverageIfNeeded() {} 1201 1202var _ cc.Coverage = (*AndroidApp)(nil) 1203 1204func (a *AndroidApp) IDEInfo(dpInfo *android.IdeInfo) { 1205 a.Library.IDEInfo(dpInfo) 1206 a.aapt.IDEInfo(dpInfo) 1207} 1208 1209func (a *AndroidApp) productCharacteristicsRROPackageName() string { 1210 return proptools.String(a.appProperties.ProductCharacteristicsRROPackageName) 1211} 1212 1213func (a *AndroidApp) productCharacteristicsRROManifestModuleName() string { 1214 return proptools.String(a.appProperties.ProductCharacteristicsRROManifestModuleName) 1215} 1216 1217// android_app compiles sources and Android resources into an Android application package `.apk` file. 1218func AndroidAppFactory() android.Module { 1219 module := &AndroidApp{} 1220 1221 module.Module.dexProperties.Optimize.EnabledByDefault = true 1222 module.Module.dexProperties.Optimize.Shrink = proptools.BoolPtr(true) 1223 module.Module.dexProperties.Optimize.Proguard_compatibility = proptools.BoolPtr(false) 1224 1225 module.Module.properties.Instrument = true 1226 module.Module.properties.Supports_static_instrumentation = true 1227 module.Module.properties.Installable = proptools.BoolPtr(true) 1228 1229 module.addHostAndDeviceProperties() 1230 module.AddProperties( 1231 &module.aaptProperties, 1232 &module.appProperties, 1233 &module.overridableAppProperties, 1234 &module.Library.sourceProperties) 1235 1236 module.usesLibrary.enforce = true 1237 1238 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) 1239 android.InitDefaultableModule(module) 1240 android.InitOverridableModule(module, &module.overridableAppProperties.Overrides) 1241 android.InitApexModule(module) 1242 1243 android.AddLoadHook(module, func(ctx android.LoadHookContext) { 1244 a := ctx.Module().(*AndroidApp) 1245 1246 characteristics := ctx.Config().ProductAAPTCharacteristics() 1247 if characteristics == "default" || characteristics == "" { 1248 module.appProperties.Generate_product_characteristics_rro = nil 1249 // no need to create RRO 1250 return 1251 } 1252 1253 if !proptools.Bool(module.appProperties.Generate_product_characteristics_rro) { 1254 return 1255 } 1256 1257 rroPackageName := a.Name() + "__" + strings.ReplaceAll(characteristics, ",", "_") + "__auto_generated_characteristics_rro" 1258 rroManifestName := rroPackageName + "_manifest" 1259 1260 a.appProperties.ProductCharacteristicsRROPackageName = proptools.StringPtr(rroPackageName) 1261 a.appProperties.ProductCharacteristicsRROManifestModuleName = proptools.StringPtr(rroManifestName) 1262 1263 rroManifestProperties := struct { 1264 Name *string 1265 Tools []string 1266 Out []string 1267 Srcs []string 1268 Cmd *string 1269 }{ 1270 Name: proptools.StringPtr(rroManifestName), 1271 Tools: []string{"characteristics_rro_generator", "aapt2"}, 1272 Out: []string{"AndroidManifest.xml"}, 1273 Srcs: []string{":" + a.Name() + "{.apk}"}, 1274 Cmd: proptools.StringPtr("$(location characteristics_rro_generator) $$($(location aapt2) dump packagename $(in)) $(out)"), 1275 } 1276 ctx.CreateModule(genrule.GenRuleFactory, &rroManifestProperties) 1277 1278 rroProperties := struct { 1279 Name *string 1280 Filter_product *string 1281 Aaptflags []string 1282 Manifest *string 1283 Resource_dirs []string 1284 }{ 1285 Name: proptools.StringPtr(rroPackageName), 1286 Filter_product: proptools.StringPtr(characteristics), 1287 Aaptflags: []string{"--auto-add-overlay"}, 1288 Manifest: proptools.StringPtr(":" + rroManifestName), 1289 Resource_dirs: a.aaptProperties.Resource_dirs, 1290 } 1291 if !Bool(a.aaptProperties.Aapt_include_all_resources) { 1292 for _, aaptConfig := range ctx.Config().ProductAAPTConfig() { 1293 rroProperties.Aaptflags = append(rroProperties.Aaptflags, "-c", aaptConfig) 1294 } 1295 } 1296 ctx.CreateModule(RuntimeResourceOverlayFactory, &rroProperties) 1297 }) 1298 1299 return module 1300} 1301 1302// A dictionary of values to be overridden in the manifest. 1303type Manifest_values struct { 1304 // Overrides the value of package_name in the manifest 1305 ApplicationId *string 1306} 1307 1308type appTestProperties struct { 1309 // The name of the android_app module that the tests will run against. 1310 Instrumentation_for *string 1311 1312 // If specified, the instrumentation target package name in the manifest is overwritten by it. 1313 Instrumentation_target_package *string 1314 1315 // If specified, the mainline module package name in the test config is overwritten by it. 1316 Mainline_package_name *string 1317 1318 Manifest_values Manifest_values 1319} 1320 1321type AndroidTest struct { 1322 AndroidApp 1323 1324 appTestProperties appTestProperties 1325 1326 testProperties testProperties 1327 1328 testConfig android.Path 1329 extraTestConfigs android.Paths 1330 data android.Paths 1331} 1332 1333func (a *AndroidTest) InstallInTestcases() bool { 1334 return true 1335} 1336 1337type androidTestApp interface { 1338 includedInTestSuite(searchPrefix string) bool 1339} 1340 1341func (a *AndroidTest) includedInTestSuite(searchPrefix string) bool { 1342 return android.PrefixInList(a.testProperties.Test_suites, searchPrefix) 1343} 1344 1345func (a *AndroidTestHelperApp) includedInTestSuite(searchPrefix string) bool { 1346 return android.PrefixInList(a.appTestHelperAppProperties.Test_suites, searchPrefix) 1347} 1348 1349func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) { 1350 var configs []tradefed.Config 1351 if a.appTestProperties.Instrumentation_target_package != nil { 1352 a.additionalAaptFlags = append(a.additionalAaptFlags, 1353 "--rename-instrumentation-target-package "+*a.appTestProperties.Instrumentation_target_package) 1354 } else if a.appTestProperties.Instrumentation_for != nil { 1355 // Check if the instrumentation target package is overridden. 1356 manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for) 1357 if overridden { 1358 a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName) 1359 } 1360 } 1361 applicationId := a.appTestProperties.Manifest_values.ApplicationId 1362 if applicationId != nil { 1363 if a.overridableAppProperties.Package_name != nil { 1364 ctx.PropertyErrorf("manifest_values.applicationId", "property is not supported when property package_name is set.") 1365 } 1366 a.aapt.manifestValues.applicationId = *applicationId 1367 } 1368 a.generateAndroidBuildActions(ctx) 1369 1370 for _, module := range a.testProperties.Test_mainline_modules { 1371 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module}) 1372 } 1373 1374 testConfig := tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config, 1375 a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites, a.testProperties.Auto_gen_config, configs) 1376 a.testConfig = a.FixTestConfig(ctx, testConfig) 1377 a.extraTestConfigs = android.PathsForModuleSrc(ctx, a.testProperties.Test_options.Extra_test_configs) 1378 a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data) 1379 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{}) 1380 android.SetProvider(ctx, tradefed.BaseTestProviderKey, tradefed.BaseTestProviderData{ 1381 InstalledFiles: a.data, 1382 OutputFile: a.OutputFile(), 1383 TestConfig: a.testConfig, 1384 HostRequiredModuleNames: a.HostRequiredModuleNames(), 1385 TestSuites: a.testProperties.Test_suites, 1386 IsHost: false, 1387 LocalCertificate: a.certificate.AndroidMkString(), 1388 IsUnitTest: Bool(a.testProperties.Test_options.Unit_test), 1389 }) 1390 android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{ 1391 TestOnly: true, 1392 TopLevelTarget: true, 1393 }) 1394 1395} 1396 1397func (a *AndroidTest) FixTestConfig(ctx android.ModuleContext, testConfig android.Path) android.Path { 1398 if testConfig == nil { 1399 return nil 1400 } 1401 1402 fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", "AndroidTest.xml") 1403 rule := android.NewRuleBuilder(pctx, ctx) 1404 command := rule.Command().BuiltTool("test_config_fixer").Input(testConfig).Output(fixedConfig) 1405 fixNeeded := false 1406 1407 // Auto-generated test config uses `ModuleName` as the APK name. So fix it if it is not the case. 1408 if ctx.ModuleName() != a.installApkName { 1409 fixNeeded = true 1410 command.FlagWithArg("--test-file-name ", a.installApkName+".apk") 1411 } 1412 1413 if a.overridableAppProperties.Package_name != nil { 1414 fixNeeded = true 1415 command.FlagWithInput("--manifest ", a.manifestPath). 1416 FlagWithArg("--package-name ", *a.overridableAppProperties.Package_name) 1417 } 1418 1419 if a.appTestProperties.Mainline_package_name != nil { 1420 fixNeeded = true 1421 command.FlagWithArg("--mainline-package-name ", *a.appTestProperties.Mainline_package_name) 1422 } 1423 1424 if fixNeeded { 1425 rule.Build("fix_test_config", "fix test config") 1426 return fixedConfig 1427 } 1428 return testConfig 1429} 1430 1431func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) { 1432 a.AndroidApp.DepsMutator(ctx) 1433} 1434 1435func (a *AndroidTest) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) { 1436 a.AndroidApp.OverridablePropertiesDepsMutator(ctx) 1437 if a.appTestProperties.Instrumentation_for != nil { 1438 // The android_app dependency listed in instrumentation_for needs to be added to the classpath for javac, 1439 // but not added to the aapt2 link includes like a normal android_app or android_library dependency, so 1440 // use instrumentationForTag instead of libTag. 1441 ctx.AddVariationDependencies(nil, instrumentationForTag, String(a.appTestProperties.Instrumentation_for)) 1442 } 1443} 1444 1445// android_test compiles test sources and Android resources into an Android application package `.apk` file and 1446// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file. 1447func AndroidTestFactory() android.Module { 1448 module := &AndroidTest{} 1449 1450 module.Module.dexProperties.Optimize.EnabledByDefault = false 1451 1452 module.Module.properties.Instrument = true 1453 module.Module.properties.Supports_static_instrumentation = true 1454 module.Module.properties.Installable = proptools.BoolPtr(true) 1455 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true) 1456 module.appProperties.AlwaysPackageNativeLibs = true 1457 module.Module.dexpreopter.isTest = true 1458 module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true) 1459 1460 module.addHostAndDeviceProperties() 1461 module.AddProperties( 1462 &module.aaptProperties, 1463 &module.appProperties, 1464 &module.appTestProperties, 1465 &module.overridableAppProperties, 1466 &module.testProperties) 1467 1468 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) 1469 android.InitDefaultableModule(module) 1470 android.InitOverridableModule(module, &module.overridableAppProperties.Overrides) 1471 1472 return module 1473} 1474 1475type appTestHelperAppProperties struct { 1476 // list of compatibility suites (for example "cts", "vts") that the module should be 1477 // installed into. 1478 Test_suites []string `android:"arch_variant"` 1479 1480 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml 1481 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true 1482 // explicitly. 1483 Auto_gen_config *bool 1484 1485 // Install the test into a folder named for the module in all test suites. 1486 Per_testcase_directory *bool 1487 1488 Manifest_values Manifest_values 1489} 1490 1491type AndroidTestHelperApp struct { 1492 AndroidApp 1493 1494 appTestHelperAppProperties appTestHelperAppProperties 1495} 1496 1497func (a *AndroidTestHelperApp) InstallInTestcases() bool { 1498 return true 1499} 1500 1501// android_test_helper_app compiles sources and Android resources into an Android application package `.apk` file that 1502// will be used by tests, but does not produce an `AndroidTest.xml` file so the module will not be run directly as a 1503// test. 1504func AndroidTestHelperAppFactory() android.Module { 1505 module := &AndroidTestHelperApp{} 1506 1507 // TODO(b/192032291): Disable by default after auditing downstream usage. 1508 module.Module.dexProperties.Optimize.EnabledByDefault = true 1509 1510 module.Module.properties.Installable = proptools.BoolPtr(true) 1511 module.appProperties.Use_embedded_native_libs = proptools.BoolPtr(true) 1512 module.appProperties.AlwaysPackageNativeLibs = true 1513 module.Module.dexpreopter.isTest = true 1514 module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true) 1515 1516 module.addHostAndDeviceProperties() 1517 module.AddProperties( 1518 &module.aaptProperties, 1519 &module.appProperties, 1520 &module.appTestHelperAppProperties, 1521 &module.overridableAppProperties) 1522 1523 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) 1524 android.InitDefaultableModule(module) 1525 android.InitApexModule(module) 1526 return module 1527} 1528 1529type AndroidAppCertificate struct { 1530 android.ModuleBase 1531 1532 properties AndroidAppCertificateProperties 1533 Certificate Certificate 1534} 1535 1536type AndroidAppCertificateProperties struct { 1537 // Name of the certificate files. Extensions .x509.pem and .pk8 will be added to the name. 1538 Certificate *string 1539} 1540 1541// android_app_certificate modules can be referenced by the certificates property of android_app modules to select 1542// the signing key. 1543func AndroidAppCertificateFactory() android.Module { 1544 module := &AndroidAppCertificate{} 1545 module.AddProperties(&module.properties) 1546 android.InitAndroidModule(module) 1547 return module 1548} 1549 1550func (c *AndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) { 1551 cert := String(c.properties.Certificate) 1552 c.Certificate = Certificate{ 1553 Pem: android.PathForModuleSrc(ctx, cert+".x509.pem"), 1554 Key: android.PathForModuleSrc(ctx, cert+".pk8"), 1555 } 1556} 1557 1558type OverrideAndroidApp struct { 1559 android.ModuleBase 1560 android.OverrideModuleBase 1561} 1562 1563func (i *OverrideAndroidApp) GenerateAndroidBuildActions(_ android.ModuleContext) { 1564 // All the overrides happen in the base module. 1565 // TODO(jungjw): Check the base module type. 1566} 1567 1568// override_android_app is used to create an android_app module based on another android_app by overriding 1569// some of its properties. 1570func OverrideAndroidAppModuleFactory() android.Module { 1571 m := &OverrideAndroidApp{} 1572 m.AddProperties( 1573 &OverridableProperties{}, 1574 &overridableAppProperties{}, 1575 ) 1576 1577 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon) 1578 android.InitOverrideModule(m) 1579 return m 1580} 1581 1582type OverrideAndroidTest struct { 1583 android.ModuleBase 1584 android.OverrideModuleBase 1585} 1586 1587func (i *OverrideAndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) { 1588 // All the overrides happen in the base module. 1589 // TODO(jungjw): Check the base module type. 1590 android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{ 1591 TestOnly: true, 1592 TopLevelTarget: true, 1593 }) 1594} 1595 1596// override_android_test is used to create an android_app module based on another android_test by overriding 1597// some of its properties. 1598func OverrideAndroidTestModuleFactory() android.Module { 1599 m := &OverrideAndroidTest{} 1600 m.AddProperties(&overridableAppProperties{}) 1601 m.AddProperties(&appTestProperties{}) 1602 1603 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon) 1604 android.InitOverrideModule(m) 1605 return m 1606} 1607 1608type UsesLibraryProperties struct { 1609 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file. 1610 Uses_libs []string 1611 1612 // A list of shared library modules that will be listed in uses-library tags in the AndroidManifest.xml file with 1613 // required=false. 1614 Optional_uses_libs []string 1615 1616 // If true, the list of uses_libs and optional_uses_libs modules must match the AndroidManifest.xml file. Defaults 1617 // to true if either uses_libs or optional_uses_libs is set. Will unconditionally default to true in the future. 1618 Enforce_uses_libs *bool 1619 1620 // Optional name of the <uses-library> provided by this module. This is needed for non-SDK 1621 // libraries, because SDK ones are automatically picked up by Soong. The <uses-library> name 1622 // normally is the same as the module name, but there are exceptions. 1623 Provides_uses_lib *string 1624 1625 // A list of shared library names to exclude from the classpath of the APK. Adding a library here 1626 // will prevent it from being used when precompiling the APK and prevent it from being implicitly 1627 // added to the APK's manifest's <uses-library> elements. 1628 // 1629 // Care must be taken when using this as it could result in runtime errors if the APK actually 1630 // uses classes provided by the library and which are not provided in any other way. 1631 // 1632 // This is primarily intended for use by various CTS tests that check the runtime handling of the 1633 // android.test.base shared library (and related libraries) but which depend on some common 1634 // libraries that depend on the android.test.base library. Without this those tests will end up 1635 // with a <uses-library android:name="android.test.base"/> in their manifest which would either 1636 // render the tests worthless (as they would be testing the wrong behavior), or would break the 1637 // test altogether by providing access to classes that the tests were not expecting. Those tests 1638 // provide the android.test.base statically and use jarjar to rename them so they do not collide 1639 // with the classes provided by the android.test.base library. 1640 Exclude_uses_libs []string 1641 1642 // The module names of optional uses-library libraries that are missing from the source tree. 1643 Missing_optional_uses_libs []string `blueprint:"mutated"` 1644} 1645 1646// usesLibrary provides properties and helper functions for AndroidApp and AndroidAppImport to verify that the 1647// <uses-library> tags that end up in the manifest of an APK match the ones known to the build system through the 1648// uses_libs and optional_uses_libs properties. The build system's values are used by dexpreopt to preopt apps 1649// with knowledge of their shared libraries. 1650type usesLibrary struct { 1651 usesLibraryProperties UsesLibraryProperties 1652 1653 // Whether to enforce verify_uses_library check. 1654 enforce bool 1655 1656 // Whether dexpreopt should be disabled 1657 shouldDisableDexpreopt bool 1658} 1659 1660func (u *usesLibrary) deps(ctx android.BottomUpMutatorContext, addCompatDeps bool) { 1661 if !ctx.Config().UnbundledBuild() || ctx.Config().UnbundledBuildImage() { 1662 ctx.AddVariationDependencies(nil, usesLibReqTag, u.usesLibraryProperties.Uses_libs...) 1663 presentOptionalUsesLibs := u.presentOptionalUsesLibs(ctx) 1664 ctx.AddVariationDependencies(nil, usesLibOptTag, presentOptionalUsesLibs...) 1665 // Only add these extra dependencies if the module is an app that depends on framework 1666 // libs. This avoids creating a cyclic dependency: 1667 // e.g. framework-res -> org.apache.http.legacy -> ... -> framework-res. 1668 if addCompatDeps { 1669 // Dexpreopt needs paths to the dex jars of these libraries in order to construct 1670 // class loader context for dex2oat. Add them as a dependency with a special tag. 1671 ctx.AddVariationDependencies(nil, usesLibCompat29ReqTag, dexpreopt.CompatUsesLibs29...) 1672 ctx.AddVariationDependencies(nil, usesLibCompat28OptTag, dexpreopt.OptionalCompatUsesLibs28...) 1673 ctx.AddVariationDependencies(nil, usesLibCompat30OptTag, dexpreopt.OptionalCompatUsesLibs30...) 1674 } 1675 _, diff, _ := android.ListSetDifference(u.usesLibraryProperties.Optional_uses_libs, presentOptionalUsesLibs) 1676 u.usesLibraryProperties.Missing_optional_uses_libs = diff 1677 } else { 1678 ctx.AddVariationDependencies(nil, r8LibraryJarTag, u.usesLibraryProperties.Uses_libs...) 1679 ctx.AddVariationDependencies(nil, r8LibraryJarTag, u.presentOptionalUsesLibs(ctx)...) 1680 } 1681} 1682 1683// presentOptionalUsesLibs returns optional_uses_libs after filtering out libraries that don't exist in the source tree. 1684func (u *usesLibrary) presentOptionalUsesLibs(ctx android.BaseModuleContext) []string { 1685 optionalUsesLibs := android.FilterListPred(u.usesLibraryProperties.Optional_uses_libs, func(s string) bool { 1686 exists := ctx.OtherModuleExists(s) 1687 if !exists && !android.InList(ctx.ModuleName(), ctx.Config().BuildWarningBadOptionalUsesLibsAllowlist()) { 1688 fmt.Printf("Warning: Module '%s' depends on non-existing optional_uses_libs '%s'\n", ctx.ModuleName(), s) 1689 } 1690 return exists 1691 }) 1692 return optionalUsesLibs 1693} 1694 1695// Returns a map of module names of shared library dependencies to the paths to their dex jars on 1696// host and on device. 1697func (u *usesLibrary) classLoaderContextForUsesLibDeps(ctx android.ModuleContext) dexpreopt.ClassLoaderContextMap { 1698 clcMap := make(dexpreopt.ClassLoaderContextMap) 1699 1700 // Skip when UnbundledBuild() is true, but UnbundledBuildImage() is false. With 1701 // UnbundledBuildImage() it is necessary to generate dexpreopt.config for post-dexpreopting. 1702 if ctx.Config().UnbundledBuild() && !ctx.Config().UnbundledBuildImage() { 1703 return clcMap 1704 } 1705 1706 ctx.VisitDirectDeps(func(m android.Module) { 1707 tag, isUsesLibTag := ctx.OtherModuleDependencyTag(m).(usesLibraryDependencyTag) 1708 if !isUsesLibTag { 1709 return 1710 } 1711 1712 dep := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(m)) 1713 1714 // Skip stub libraries. A dependency on the implementation library has been added earlier, 1715 // so it will be added to CLC, but the stub shouldn't be. Stub libraries can be distingushed 1716 // from implementation libraries by their name, which is different as it has a suffix. 1717 if comp, ok := m.(SdkLibraryComponentDependency); ok { 1718 if impl := comp.OptionalSdkLibraryImplementation(); impl != nil && *impl != dep { 1719 return 1720 } 1721 } 1722 1723 // Skip java_sdk_library dependencies that provide stubs, but not an implementation. 1724 // This will be restricted to optional_uses_libs 1725 if sdklib, ok := m.(SdkLibraryDependency); ok { 1726 if tag == usesLibOptTag && sdklib.DexJarBuildPath(ctx).PathOrNil() == nil { 1727 u.shouldDisableDexpreopt = true 1728 return 1729 } 1730 } 1731 1732 if lib, ok := m.(UsesLibraryDependency); ok { 1733 libName := dep 1734 if ulib, ok := m.(ProvidesUsesLib); ok && ulib.ProvidesUsesLib() != nil { 1735 libName = *ulib.ProvidesUsesLib() 1736 } 1737 clcMap.AddContext(ctx, tag.sdkVersion, libName, tag.optional, 1738 lib.DexJarBuildPath(ctx).PathOrNil(), lib.DexJarInstallPath(), 1739 lib.ClassLoaderContexts()) 1740 } else if ctx.Config().AllowMissingDependencies() { 1741 ctx.AddMissingDependencies([]string{dep}) 1742 } else { 1743 ctx.ModuleErrorf("module %q in uses_libs or optional_uses_libs must be a java library", dep) 1744 } 1745 }) 1746 return clcMap 1747} 1748 1749// enforceUsesLibraries returns true of <uses-library> tags should be checked against uses_libs and optional_uses_libs 1750// properties. Defaults to true if either of uses_libs or optional_uses_libs is specified. Will default to true 1751// unconditionally in the future. 1752func (u *usesLibrary) enforceUsesLibraries() bool { 1753 defaultEnforceUsesLibs := len(u.usesLibraryProperties.Uses_libs) > 0 || 1754 len(u.usesLibraryProperties.Optional_uses_libs) > 0 1755 return BoolDefault(u.usesLibraryProperties.Enforce_uses_libs, u.enforce || defaultEnforceUsesLibs) 1756} 1757 1758// Freeze the value of `enforce_uses_libs` based on the current values of `uses_libs` and `optional_uses_libs`. 1759func (u *usesLibrary) freezeEnforceUsesLibraries() { 1760 enforce := u.enforceUsesLibraries() 1761 u.usesLibraryProperties.Enforce_uses_libs = &enforce 1762} 1763 1764// verifyUsesLibraries checks the <uses-library> tags in the manifest against the ones specified 1765// in the `uses_libs`/`optional_uses_libs` properties. The input can be either an XML manifest, or 1766// an APK with the manifest embedded in it (manifest_check will know which one it is by the file 1767// extension: APKs are supposed to end with '.apk'). 1768func (u *usesLibrary) verifyUsesLibraries(ctx android.ModuleContext, inputFile android.Path, 1769 outputFile android.WritablePath, classLoaderContexts *dexpreopt.ClassLoaderContextMap) android.Path { 1770 1771 statusFile := dexpreopt.UsesLibrariesStatusFile(ctx) 1772 1773 // Disable verify_uses_libraries check if dexpreopt is globally disabled. Without dexpreopt the 1774 // check is not necessary, and although it is good to have, it is difficult to maintain on 1775 // non-linux build platforms where dexpreopt is generally disabled (the check may fail due to 1776 // various unrelated reasons, such as a failure to get manifest from an APK). 1777 global := dexpreopt.GetGlobalConfig(ctx) 1778 if global.DisablePreopt || global.OnlyPreoptArtBootImage { 1779 return inputFile 1780 } 1781 1782 rule := android.NewRuleBuilder(pctx, ctx) 1783 cmd := rule.Command().BuiltTool("manifest_check"). 1784 Flag("--enforce-uses-libraries"). 1785 Input(inputFile). 1786 FlagWithOutput("--enforce-uses-libraries-status ", statusFile). 1787 FlagWithInput("--aapt ", ctx.Config().HostToolPath(ctx, "aapt2")) 1788 1789 if outputFile != nil { 1790 cmd.FlagWithOutput("-o ", outputFile) 1791 } 1792 1793 if dexpreopt.GetGlobalConfig(ctx).RelaxUsesLibraryCheck { 1794 cmd.Flag("--enforce-uses-libraries-relax") 1795 } 1796 1797 requiredUsesLibs, optionalUsesLibs := classLoaderContexts.UsesLibs() 1798 for _, lib := range requiredUsesLibs { 1799 cmd.FlagWithArg("--uses-library ", lib) 1800 } 1801 for _, lib := range optionalUsesLibs { 1802 cmd.FlagWithArg("--optional-uses-library ", lib) 1803 } 1804 1805 // Also add missing optional uses libs, as the manifest check expects them. 1806 // Note that what we add here are the module names of those missing libs, not library names, while 1807 // the manifest check actually expects library names. However, the case where a library is missing 1808 // and the module name != the library name is too rare for us to handle. 1809 for _, lib := range u.usesLibraryProperties.Missing_optional_uses_libs { 1810 cmd.FlagWithArg("--missing-optional-uses-library ", lib) 1811 } 1812 1813 rule.Build("verify_uses_libraries", "verify <uses-library>") 1814 return outputFile 1815} 1816 1817// verifyUsesLibrariesManifest checks the <uses-library> tags in an AndroidManifest.xml against 1818// the build system and returns the path to a copy of the manifest. 1819func (u *usesLibrary) verifyUsesLibrariesManifest(ctx android.ModuleContext, manifest android.Path, 1820 classLoaderContexts *dexpreopt.ClassLoaderContextMap) android.Path { 1821 outputFile := android.PathForModuleOut(ctx, "manifest_check", "AndroidManifest.xml") 1822 return u.verifyUsesLibraries(ctx, manifest, outputFile, classLoaderContexts) 1823} 1824 1825// verifyUsesLibrariesAPK checks the <uses-library> tags in the manifest of an APK against the build 1826// system and returns the path to a copy of the APK. 1827func (u *usesLibrary) verifyUsesLibrariesAPK(ctx android.ModuleContext, apk android.Path, 1828 classLoaderContexts *dexpreopt.ClassLoaderContextMap) { 1829 u.verifyUsesLibraries(ctx, apk, nil, classLoaderContexts) // for APKs manifest_check does not write output file 1830} 1831