// Copyright 2018 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package java

import (
	"crypto/sha256"
	"fmt"
	"path/filepath"
	"slices"
	"strings"

	"android/soong/android"
	"android/soong/dexpreopt"

	"github.com/google/blueprint"
	"github.com/google/blueprint/depset"
	"github.com/google/blueprint/proptools"
)

type AndroidLibraryDependency interface {
	ExportPackage() android.Path
	ResourcesNodeDepSet() depset.DepSet[*resourcesNode]
	RRODirsDepSet() depset.DepSet[rroDir]
	ManifestsDepSet() depset.DepSet[android.Path]
	SetRROEnforcedForDependent(enforce bool)
	IsRROEnforced(ctx android.BaseModuleContext) bool
}

func init() {
	RegisterAARBuildComponents(android.InitRegistrationContext)
}

func RegisterAARBuildComponents(ctx android.RegistrationContext) {
	ctx.RegisterModuleType("android_library_import", AARImportFactory)
	ctx.RegisterModuleType("android_library", AndroidLibraryFactory)
	ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
		ctx.Transition("propagate_rro_enforcement", &propagateRROEnforcementTransitionMutator{})
	})
}

//
// AAR (android library)
//

type androidLibraryProperties struct {
	BuildAAR bool `blueprint:"mutated"`
}

type aaptProperties struct {
	// flags passed to aapt when creating the apk
	Aaptflags []string

	// include all resource configurations, not just the product-configured
	// ones.
	Aapt_include_all_resources *bool

	// list of files to use as assets.
	Assets []string `android:"path"`

	// list of directories relative to the Blueprints file containing assets.
	// Defaults to ["assets"] if a directory called assets exists.  Set to []
	// to disable the default.
	Asset_dirs []string

	// list of directories relative to the Blueprints file containing
	// Android resources.  Defaults to ["res"] if a directory called res exists.
	// Set to [] to disable the default.
	Resource_dirs proptools.Configurable[[]string] `android:"path"`

	// list of zip files containing Android resources.
	Resource_zips []string `android:"path"`

	// path to AndroidManifest.xml.  If unset, defaults to "AndroidManifest.xml".
	Manifest *string `android:"path"`

	// paths to additional manifest files to merge with main manifest.
	Additional_manifests []string `android:"path"`

	// do not include AndroidManifest from dependent libraries
	Dont_merge_manifests *bool

	// If use_resource_processor is set, use Bazel's resource processor instead of aapt2 to generate R.class files.
	// The resource processor produces more optimal R.class files that only list resources in the package of the
	// library that provided them, as opposed to aapt2 which produces R.java files for every package containing
	// every resource.  Using the resource processor can provide significant build time speedups, but requires
	// fixing the module to use the correct package to reference each resource, and to avoid having any other
	// libraries in the tree that use the same package name.  Defaults to false, but will default to true in the
	// future.
	Use_resource_processor *bool

	// true if RRO is enforced for any of the dependent modules
	RROEnforcedForDependent bool `blueprint:"mutated"`

	// Filter only specified product and ignore other products
	Filter_product *string `blueprint:"mutated"`

	// Names of aconfig_declarations modules that specify aconfig flags that the module depends on.
	Flags_packages []string
}

type aapt struct {
	aaptSrcJar                         android.Path
	transitiveAaptRJars                android.Paths
	transitiveAaptResourcePackagesFile android.Path
	exportPackage                      android.Path
	manifestPath                       android.Path
	proguardOptionsFile                android.Path
	rTxt                               android.Path
	rJar                               android.Path
	extraAaptPackagesFile              android.Path
	mergedManifestFile                 android.Path
	noticeFile                         android.OptionalPath
	assetPackage                       android.OptionalPath
	isLibrary                          bool
	defaultManifestVersion             string
	useEmbeddedNativeLibs              bool
	useEmbeddedDex                     bool
	usesNonSdkApis                     bool
	hasNoCode                          bool
	LoggingParent                      string
	resourceFiles                      android.Paths

	splitNames []string
	splits     []split

	aaptProperties aaptProperties

	resourcesNodesDepSet depset.DepSet[*resourcesNode]
	rroDirsDepSet        depset.DepSet[rroDir]
	manifestsDepSet      depset.DepSet[android.Path]

	manifestValues struct {
		applicationId string
	}
}

type split struct {
	name   string
	suffix string
	path   android.Path
}

// Propagate RRO enforcement flag to static lib dependencies transitively.  If EnforceRROGlobally is set then
// all modules will use the "" variant.  If specific modules have RRO enforced, then modules (usually apps) with
// RRO enabled will use the "" variation for themselves, but use the "rro" variant of direct and transitive static
// android_library dependencies.
type propagateRROEnforcementTransitionMutator struct{}

func (p propagateRROEnforcementTransitionMutator) Split(ctx android.BaseModuleContext) []string {
	// Never split modules, apps with or without RRO enabled use the "" variant, static android_library dependencies
	// will use create the "rro" variant from incoming tranisitons.
	return []string{""}
}

func (p propagateRROEnforcementTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
	// Non-static dependencies are not involved in RRO and always use the empty variant.
	if ctx.DepTag() != staticLibTag {
		return ""
	}

	m := ctx.Module()
	if _, ok := m.(AndroidLibraryDependency); ok {
		// If RRO is enforced globally don't bother using "rro" variants, the empty variant will have RRO enabled.
		if ctx.Config().EnforceRROGlobally() {
			return ""
		}

		// If RRO is enabled for this module use the "rro" variants of static dependencies.  IncomingTransition will
		// rewrite this back to "" if the dependency is not an android_library.
		if ctx.Config().EnforceRROForModule(ctx.Module().Name()) {
			return "rro"
		}
	}

	return sourceVariation
}

func (p propagateRROEnforcementTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
	// Propagate the "rro" variant to android_library modules, but use the empty variant for everything else.
	if incomingVariation == "rro" {
		m := ctx.Module()
		if _, ok := m.(AndroidLibraryDependency); ok {
			return "rro"
		}
		return ""
	}

	return ""
}

func (p propagateRROEnforcementTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
	m := ctx.Module()
	if d, ok := m.(AndroidLibraryDependency); ok {
		if variation == "rro" {
			// This is the "rro" variant of a module that has both variants, mark this one as RRO enabled and
			// hide it from make to avoid collisions with the non-RRO empty variant.
			d.SetRROEnforcedForDependent(true)
			m.HideFromMake()
		} else if ctx.Config().EnforceRROGlobally() {
			// RRO is enabled globally, mark it enabled for this module, but there is only one variant so no
			// need to hide it from make.
			d.SetRROEnforcedForDependent(true)
		}
	}
}

func (a *aapt) useResourceProcessorBusyBox(ctx android.BaseModuleContext) bool {
	return BoolDefault(a.aaptProperties.Use_resource_processor, ctx.Config().UseResourceProcessorByDefault()) &&
		// TODO(b/331641946): remove this when ResourceProcessorBusyBox supports generating shared libraries.
		!slices.Contains(a.aaptProperties.Aaptflags, "--shared-lib") &&
		// Use the legacy resource processor in kythe builds.
		// The legacy resource processor creates an R.srcjar, which kythe can use for generating crossrefs.
		// TODO(b/354854007): Re-enable BusyBox in kythe builds
		!ctx.Config().EmitXrefRules()
}

func (a *aapt) filterProduct() string {
	return String(a.aaptProperties.Filter_product)
}

func (a *aapt) ExportPackage() android.Path {
	return a.exportPackage
}
func (a *aapt) ResourcesNodeDepSet() depset.DepSet[*resourcesNode] {
	return a.resourcesNodesDepSet
}

func (a *aapt) RRODirsDepSet() depset.DepSet[rroDir] {
	return a.rroDirsDepSet
}

func (a *aapt) ManifestsDepSet() depset.DepSet[android.Path] {
	return a.manifestsDepSet
}

func (a *aapt) SetRROEnforcedForDependent(enforce bool) {
	a.aaptProperties.RROEnforcedForDependent = enforce
}

func (a *aapt) IsRROEnforced(ctx android.BaseModuleContext) bool {
	// True if RRO is enforced for this module or...
	return ctx.Config().EnforceRROForModule(ctx.ModuleName()) ||
		// if RRO is enforced for any of its dependents.
		a.aaptProperties.RROEnforcedForDependent
}

func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext android.SdkContext,
	manifestPath android.Path, doNotIncludeAssetDirImplicitly bool) (compileFlags, linkFlags []string, linkDeps android.Paths,
	resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) {

	hasVersionCode := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-code")
	hasVersionName := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-name")

	// Flags specified in Android.bp
	linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...)

	linkFlags = append(linkFlags, "--enable-compact-entries")

	// Find implicit or explicit asset and resource dirs
	assets := android.PathsRelativeToModuleSourceDir(android.SourceInput{
		Context:     ctx,
		Paths:       a.aaptProperties.Assets,
		IncludeDirs: false,
	})
	var assetDirs android.Paths
	if doNotIncludeAssetDirImplicitly {
		assetDirs = android.PathsForModuleSrc(ctx, a.aaptProperties.Asset_dirs)
	} else {
		assetDirs = android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets")
	}
	resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs.GetOrDefault(ctx, nil), "res")
	resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips)

	// Glob directories into lists of paths
	for _, dir := range resourceDirs {
		resDirs = append(resDirs, globbedResourceDir{
			dir:   dir,
			files: androidResourceGlob(ctx, dir),
		})
		resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, a, dir)
		overlayDirs = append(overlayDirs, resOverlayDirs...)
		rroDirs = append(rroDirs, resRRODirs...)
	}

	assetDirsHasher := sha256.New()
	var assetDeps android.Paths
	for _, dir := range assetDirs {
		// Add a dependency on every file in the asset directory.  This ensures the aapt2
		// rule will be rerun if one of the files in the asset directory is modified.
		dirContents := androidResourceGlob(ctx, dir)
		assetDeps = append(assetDeps, dirContents...)

		// Add a hash of all the files in the asset directory to the command line.
		// This ensures the aapt2 rule will be run if a file is removed from the asset directory,
		// or a file is added whose timestamp is older than the output of aapt2.
		for _, path := range dirContents.Strings() {
			assetDirsHasher.Write([]byte(path))
		}
	}

	assetDirStrings := assetDirs.Strings()
	if a.noticeFile.Valid() {
		assetDirStrings = append(assetDirStrings, filepath.Dir(a.noticeFile.Path().String()))
		assetDeps = append(assetDeps, a.noticeFile.Path())
	}
	if len(assets) > 0 {
		// aapt2 doesn't support adding individual asset files. Create a temp directory to hold asset
		// files and pass it to aapt2.
		tmpAssetDir := android.PathForModuleOut(ctx, "tmp_asset_dir")

		rule := android.NewRuleBuilder(pctx, ctx)
		rule.Command().
			Text("rm -rf").Text(tmpAssetDir.String()).
			Text("&&").
			Text("mkdir -p").Text(tmpAssetDir.String())

		for _, asset := range assets {
			output := tmpAssetDir.Join(ctx, asset.Rel())
			assetDeps = append(assetDeps, output)
			rule.Command().Text("mkdir -p").Text(filepath.Dir(output.String()))
			rule.Command().Text("cp").Input(asset).Output(output)
		}

		rule.Build("tmp_asset_dir", "tmp_asset_dir")

		assetDirStrings = append(assetDirStrings, tmpAssetDir.String())
	}

	linkFlags = append(linkFlags, "--manifest "+manifestPath.String())
	linkDeps = append(linkDeps, manifestPath)

	linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirStrings, "-A "))
	linkFlags = append(linkFlags, fmt.Sprintf("$$(: %x)", assetDirsHasher.Sum(nil)))
	linkDeps = append(linkDeps, assetDeps...)

	// Returns the effective version for {min|target}_sdk_version
	effectiveVersionString := func(sdkVersion android.SdkSpec, minSdkVersion android.ApiLevel) string {
		// If {min|target}_sdk_version is current, use sdk_version to determine the effective level
		// This is necessary for vendor modules.
		// The effective version does not _only_ depend on {min|target}_sdk_version(level),
		// but also on the sdk_version (kind+level)
		if minSdkVersion.IsCurrent() {
			ret, err := sdkVersion.EffectiveVersionString(ctx)
			if err != nil {
				ctx.ModuleErrorf("invalid sdk_version: %s", err)
			}
			return ret
		}
		ret, err := minSdkVersion.EffectiveVersionString(ctx)
		if err != nil {
			ctx.ModuleErrorf("invalid min_sdk_version: %s", err)
		}
		return ret
	}
	// SDK version flags
	sdkVersion := sdkContext.SdkVersion(ctx)
	minSdkVersion := effectiveVersionString(sdkVersion, sdkContext.MinSdkVersion(ctx))

	linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion)
	// Use minSdkVersion for target-sdk-version, even if `target_sdk_version` is set
	// This behavior has been copied from Make.
	linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion)

	// Version code
	if !hasVersionCode {
		linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion().String())
	}

	if !hasVersionName {
		var versionName string
		if ctx.ModuleName() == "framework-res" || ctx.ModuleName() == "org.lineageos.platform-res" {
			// Some builds set AppsDefaultVersionName() to include the build number ("O-123456").  aapt2 copies the
			// version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things
			// if it contains the build number.  Use the PlatformVersionName instead.
			versionName = ctx.Config().PlatformVersionName()
		} else {
			versionName = ctx.Config().AppsDefaultVersionName()
		}
		versionName = proptools.NinjaEscape(versionName)
		linkFlags = append(linkFlags, "--version-name ", versionName)
	}

	linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"})

	// Always set --pseudo-localize, it will be stripped out later for release
	// builds that don't want it.
	compileFlags = append(compileFlags, "--pseudo-localize")

	return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips
}

func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) {
	if sdkDep.frameworkResModule != "" {
		ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
	}
	if sdkDep.lineageResModule != "" {
		ctx.AddVariationDependencies(nil, lineageResTag, sdkDep.lineageResModule)
	}
}

var extractAssetsRule = pctx.AndroidStaticRule("extractAssets",
	blueprint.RuleParams{
		Command:     `${config.Zip2ZipCmd} -i ${in} -o ${out} "assets/**/*"`,
		CommandDeps: []string{"${config.Zip2ZipCmd}"},
	})

type aaptBuildActionOptions struct {
	sdkContext                     android.SdkContext
	classLoaderContexts            dexpreopt.ClassLoaderContextMap
	excludedLibs                   []string
	enforceDefaultTargetSdkVersion bool
	forceNonFinalResourceIDs       bool
	extraLinkFlags                 []string
	aconfigTextFiles               android.Paths
	usesLibrary                    *usesLibrary
	// If rroDirs is provided, it will be used to generate package-res.apk
	rroDirs *android.Paths
	// If manifestForAapt is not nil, it will be used for aapt instead of the default source manifest.
	manifestForAapt android.Path
}

func filterRRO(rroDirsDepSet depset.DepSet[rroDir], filter overlayType) android.Paths {
	var paths android.Paths
	seen := make(map[android.Path]bool)
	for _, d := range rroDirsDepSet.ToList() {
		if d.overlayType == filter {
			if seen[d.path] {
				continue
			}
			seen[d.path] = true
			paths = append(paths, d.path)
		}
	}
	return paths
}

func (a *aapt) buildActions(ctx android.ModuleContext, opts aaptBuildActionOptions) {

	staticResourcesNodesDepSet, sharedResourcesNodesDepSet, staticRRODirsDepSet, staticManifestsDepSet, sharedExportPackages, libFlags :=
		aaptLibs(ctx, opts.sdkContext, opts.classLoaderContexts, opts.usesLibrary)

	// Exclude any libraries from the supplied list.
	opts.classLoaderContexts = opts.classLoaderContexts.ExcludeLibs(opts.excludedLibs)

	// App manifest file
	var manifestFilePath android.Path
	if opts.manifestForAapt != nil {
		manifestFilePath = opts.manifestForAapt
	} else {
		manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml")
		manifestFilePath = android.PathForModuleSrc(ctx, manifestFile)
	}

	manifestPath := ManifestFixer(ctx, manifestFilePath, ManifestFixerParams{
		SdkContext:                     opts.sdkContext,
		ClassLoaderContexts:            opts.classLoaderContexts,
		IsLibrary:                      a.isLibrary,
		DefaultManifestVersion:         a.defaultManifestVersion,
		UseEmbeddedNativeLibs:          a.useEmbeddedNativeLibs,
		UsesNonSdkApis:                 a.usesNonSdkApis,
		UseEmbeddedDex:                 a.useEmbeddedDex,
		HasNoCode:                      a.hasNoCode,
		LoggingParent:                  a.LoggingParent,
		EnforceDefaultTargetSdkVersion: opts.enforceDefaultTargetSdkVersion,
	})

	staticDeps := transitiveAarDeps(staticResourcesNodesDepSet.ToList())
	sharedDeps := transitiveAarDeps(sharedResourcesNodesDepSet.ToList())

	// Add additional manifest files to transitive manifests.
	additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests)
	transitiveManifestPaths := append(android.Paths{manifestPath}, additionalManifests...)
	transitiveManifestPaths = append(transitiveManifestPaths, staticManifestsDepSet.ToList()...)

	if len(transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) {
		manifestMergerParams := ManifestMergerParams{
			staticLibManifests: transitiveManifestPaths[1:],
			isLibrary:          a.isLibrary,
			packageName:        a.manifestValues.applicationId,
		}
		a.mergedManifestFile = manifestMerger(ctx, transitiveManifestPaths[0], manifestMergerParams)
		ctx.CheckbuildFile(a.mergedManifestFile)
		if !a.isLibrary {
			// Only use the merged manifest for applications.  For libraries, the transitive closure of manifests
			// will be propagated to the final application and merged there.  The merged manifest for libraries is
			// only passed to Make, which can't handle transitive dependencies.
			manifestPath = a.mergedManifestFile
		}
	} else {
		a.mergedManifestFile = manifestPath
	}

	// do not include assets in autogenerated RRO.
	compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, opts.sdkContext, manifestPath, opts.rroDirs != nil)

	a.rroDirsDepSet = depset.NewBuilder[rroDir](depset.TOPOLOGICAL).
		Direct(rroDirs...).
		Transitive(staticRRODirsDepSet).Build()

	linkFlags = append(linkFlags, libFlags...)
	linkDeps = append(linkDeps, sharedExportPackages...)
	linkDeps = append(linkDeps, staticDeps.resPackages()...)
	linkFlags = append(linkFlags, opts.extraLinkFlags...)
	if a.isLibrary {
		linkFlags = append(linkFlags, "--static-lib")
	}
	if opts.forceNonFinalResourceIDs {
		linkFlags = append(linkFlags, "--non-final-ids")
	}

	linkFlags = append(linkFlags, "--no-static-lib-packages")
	if a.isLibrary {
		// Pass --merge-only to skip resource references validation until the final
		// app link step when when all static libraries are present.
		linkFlags = append(linkFlags, "--merge-only")
	}

	packageRes := android.PathForModuleOut(ctx, "package-res.apk")
	proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
	rTxt := android.PathForModuleOut(ctx, "R.txt")
	// This file isn't used by Soong, but is generated for exporting
	extraPackages := android.PathForModuleOut(ctx, "extra_packages")
	var transitiveRJars android.Paths
	var srcJar android.WritablePath

	var compiledResDirs []android.Paths
	for _, dir := range resDirs {
		a.resourceFiles = append(a.resourceFiles, dir.files...)
		compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files,
			compileFlags, a.filterProduct(), opts.aconfigTextFiles).Paths())
	}

	for i, zip := range resZips {
		flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
		aapt2CompileZip(ctx, flata, zip, "", compileFlags)
		compiledResDirs = append(compiledResDirs, android.Paths{flata})
	}

	var compiledRes, compiledOverlay android.Paths

	// AAPT2 overlays are in lowest to highest priority order, reverse the topological order
	// of transitiveStaticLibs.
	transitiveStaticLibs := android.ReversePaths(staticDeps.resPackages())

	if a.isLibrary && a.useResourceProcessorBusyBox(ctx) {
		// When building an android_library with ResourceProcessorBusyBox enabled treat static library dependencies
		// as imports.  The resources from dependencies will not be merged into this module's package-res.apk, and
		// instead modules depending on this module will reference package-res.apk from all transitive static
		// dependencies.
		for _, sharedDep := range sharedDeps {
			if sharedDep.usedResourceProcessor {
				transitiveRJars = append(transitiveRJars, sharedDep.rJar)
			}
		}
		for _, staticDep := range staticDeps {
			linkDeps = append(linkDeps, staticDep.resPackage)
			linkFlags = append(linkFlags, "-I "+staticDep.resPackage.String())
			if staticDep.usedResourceProcessor {
				transitiveRJars = append(transitiveRJars, staticDep.rJar)
			}
		}
	} else {
		// When building an app or building a library without ResourceProcessorBusyBox enabled all static
		// dependencies are compiled into this module's package-res.apk as overlays.
		compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
	}

	if len(transitiveStaticLibs) > 0 {
		// If we are using static android libraries, every source file becomes an overlay.
		// This is to emulate old AAPT behavior which simulated library support.
		for _, compiledResDir := range compiledResDirs {
			compiledOverlay = append(compiledOverlay, compiledResDir...)
		}
	} else if a.isLibrary {
		// Otherwise, for a static library we treat all the resources equally with no overlay.
		for _, compiledResDir := range compiledResDirs {
			compiledRes = append(compiledRes, compiledResDir...)
		}
	} else if len(compiledResDirs) > 0 {
		// Without static libraries, the first directory is our directory, which can then be
		// overlaid by the rest.
		compiledRes = append(compiledRes, compiledResDirs[0]...)
		for _, compiledResDir := range compiledResDirs[1:] {
			compiledOverlay = append(compiledOverlay, compiledResDir...)
		}
	}

	var compiledRro, compiledRroOverlay android.Paths
	if opts.rroDirs != nil {
		compiledRro, compiledRroOverlay = a.compileResInDir(ctx, *opts.rroDirs, compileFlags, opts.aconfigTextFiles)
	} else {
		// RRO enforcement is done based on module name. Compile the overlayDirs only if rroDirs is nil.
		// This ensures that the autogenerated RROs do not compile the overlay dirs twice.
		for _, dir := range overlayDirs {
			compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files,
				compileFlags, a.filterProduct(), opts.aconfigTextFiles).Paths()...)
		}
	}

	var splitPackages android.WritablePaths
	var splits []split

	for _, s := range a.splitNames {
		suffix := strings.Replace(s, ",", "_", -1)
		path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
		linkFlags = append(linkFlags, "--split", path.String()+":"+s)
		splitPackages = append(splitPackages, path)
		splits = append(splits, split{
			name:   s,
			suffix: suffix,
			path:   path,
		})
	}

	if !a.useResourceProcessorBusyBox(ctx) {
		// the subdir "android" is required to be filtered by package names
		srcJar = android.PathForModuleGen(ctx, "android", "R.srcjar")
	}

	// No need to specify assets from dependencies to aapt2Link for libraries, all transitive assets will be
	// provided to the final app aapt2Link step.
	var transitiveAssets android.Paths
	if !a.isLibrary {
		transitiveAssets = android.ReverseSliceInPlace(staticDeps.assets())
	}
	if opts.rroDirs == nil { // link resources and overlay
		aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt,
			linkFlags, linkDeps, compiledRes, compiledOverlay, transitiveAssets, splitPackages,
			opts.aconfigTextFiles)
		ctx.CheckbuildFile(packageRes)
	} else { // link autogenerated rro
		if len(compiledRro) == 0 {
			return
		}
		aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt,
			linkFlags, linkDeps, compiledRro, compiledRroOverlay, nil, nil,
			opts.aconfigTextFiles)
		ctx.CheckbuildFile(packageRes)
	}

	// Extract assets from the resource package output so that they can be used later in aapt2link
	// for modules that depend on this one.
	if android.PrefixInList(linkFlags, "-A ") {
		assets := android.PathForModuleOut(ctx, "assets.zip")
		ctx.Build(pctx, android.BuildParams{
			Rule:        extractAssetsRule,
			Input:       packageRes,
			Output:      assets,
			Description: "extract assets from built resource file",
		})
		a.assetPackage = android.OptionalPathForPath(assets)
	}

	if a.useResourceProcessorBusyBox(ctx) {
		rJar := android.PathForModuleOut(ctx, "busybox/R.jar")
		resourceProcessorBusyBoxGenerateBinaryR(ctx, rTxt, a.mergedManifestFile, rJar, staticDeps, a.isLibrary, a.aaptProperties.Aaptflags,
			opts.forceNonFinalResourceIDs)
		aapt2ExtractExtraPackages(ctx, extraPackages, rJar)
		transitiveRJars = append(transitiveRJars, rJar)
		a.rJar = rJar
	} else {
		aapt2ExtractExtraPackages(ctx, extraPackages, srcJar)
	}

	transitiveAaptResourcePackages := staticDeps.resPackages().Strings()
	transitiveAaptResourcePackages = slices.DeleteFunc(transitiveAaptResourcePackages, func(p string) bool {
		return p == packageRes.String()
	})
	transitiveAaptResourcePackagesFile := android.PathForModuleOut(ctx, "transitive-res-packages")
	android.WriteFileRule(ctx, transitiveAaptResourcePackagesFile, strings.Join(transitiveAaptResourcePackages, "\n"))

	// Reverse the list of R.jar files so that the current module comes first, and direct dependencies come before
	// transitive dependencies.
	transitiveRJars = android.ReversePaths(transitiveRJars)

	a.aaptSrcJar = srcJar
	a.transitiveAaptRJars = transitiveRJars
	a.transitiveAaptResourcePackagesFile = transitiveAaptResourcePackagesFile
	a.exportPackage = packageRes
	a.manifestPath = manifestPath
	a.proguardOptionsFile = proguardOptionsFile
	a.extraAaptPackagesFile = extraPackages
	a.rTxt = rTxt
	a.splits = splits
	a.resourcesNodesDepSet = depset.NewBuilder[*resourcesNode](depset.TOPOLOGICAL).
		Direct(&resourcesNode{
			resPackage:          a.exportPackage,
			manifest:            a.manifestPath,
			additionalManifests: additionalManifests,
			rTxt:                a.rTxt,
			rJar:                a.rJar,
			assets:              a.assetPackage,

			usedResourceProcessor: a.useResourceProcessorBusyBox(ctx),
		}).
		Transitive(staticResourcesNodesDepSet).Build()
	a.manifestsDepSet = depset.NewBuilder[android.Path](depset.TOPOLOGICAL).
		Direct(a.manifestPath).
		DirectSlice(additionalManifests).
		Transitive(staticManifestsDepSet).Build()
}

// comileResInDir finds the resource files in dirs by globbing and then compiles them using aapt2
// returns the file paths of compiled resources
// dirs[0] is used as compileRes
// dirs[1:] is used as compileOverlay
func (a *aapt) compileResInDir(ctx android.ModuleContext, dirs android.Paths, compileFlags []string, aconfig android.Paths) (android.Paths, android.Paths) {
	filesInDir := func(dir android.Path) android.Paths {
		files, err := ctx.GlobWithDeps(filepath.Join(dir.String(), "**/*"), androidResourceIgnoreFilenames)
		if err != nil {
			ctx.ModuleErrorf("failed to glob overlay resource dir %q: %s", dir, err.Error())
			return nil
		}
		var filePaths android.Paths
		for _, file := range files {
			if strings.HasSuffix(file, "/") {
				continue // ignore directories
			}
			filePaths = append(filePaths, android.PathForSource(ctx, file))
		}
		return filePaths
	}

	var compiledRes, compiledOverlay android.Paths
	if len(dirs) == 0 {
		return nil, nil
	}
	compiledRes = append(compiledRes, aapt2Compile(ctx, dirs[0], filesInDir(dirs[0]), compileFlags, a.filterProduct(), aconfig).Paths()...)
	if len(dirs) > 0 {
		for _, dir := range dirs[1:] {
			compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir, filesInDir(dir), compileFlags, a.filterProduct(), aconfig).Paths()...)
		}
	}
	return compiledRes, compiledOverlay
}

var resourceProcessorBusyBox = pctx.AndroidStaticRule("resourceProcessorBusyBox",
	blueprint.RuleParams{
		Command: "${config.JavaCmd} -cp ${config.ResourceProcessorBusyBox} " +
			"com.google.devtools.build.android.ResourceProcessorBusyBox --tool=GENERATE_BINARY_R -- @${out}.args && " +
			"if cmp -s ${out}.tmp ${out} ; then rm ${out}.tmp ; else mv ${out}.tmp ${out}; fi",
		CommandDeps:    []string{"${config.ResourceProcessorBusyBox}"},
		Rspfile:        "${out}.args",
		RspfileContent: "--primaryRTxt ${rTxt} --primaryManifest ${manifest} --classJarOutput ${out}.tmp ${args}",
		Restat:         true,
	}, "rTxt", "manifest", "args")

// resourceProcessorBusyBoxGenerateBinaryR converts the R.txt file produced by aapt2 into R.class files
// using Bazel's ResourceProcessorBusyBox tool, which is faster than compiling the R.java files and
// supports producing classes for static dependencies that only include resources from that dependency.
func resourceProcessorBusyBoxGenerateBinaryR(ctx android.ModuleContext, rTxt, manifest android.Path,
	rJar android.WritablePath, transitiveDeps transitiveAarDeps, isLibrary bool, aaptFlags []string,
	forceNonFinalIds bool) {

	var args []string
	var deps android.Paths

	if !isLibrary {
		// When compiling an app, pass all R.txt and AndroidManifest.xml from transitive static library dependencies
		// to ResourceProcessorBusyBox so that it can regenerate R.class files with the final resource IDs for each
		// package.
		args, deps = transitiveDeps.resourceProcessorDeps()
		if forceNonFinalIds {
			args = append(args, "--finalFields=false")
		}
	} else {
		// When compiling a library don't pass any dependencies as it only needs to generate an R.class file for this
		// library.  Pass --finalFields=false so that the R.class file contains non-final fields so they don't get
		// inlined into the library before the final IDs are assigned during app compilation.
		args = append(args, "--finalFields=false")
	}

	for i, arg := range aaptFlags {
		const AAPT_CUSTOM_PACKAGE = "--custom-package"
		if strings.HasPrefix(arg, AAPT_CUSTOM_PACKAGE) {
			pkg := strings.TrimSpace(strings.TrimPrefix(arg, AAPT_CUSTOM_PACKAGE))
			if pkg == "" && i+1 < len(aaptFlags) {
				pkg = aaptFlags[i+1]
			}
			args = append(args, "--packageForR "+pkg)
		}
	}

	deps = append(deps, rTxt, manifest)

	ctx.Build(pctx, android.BuildParams{
		Rule:        resourceProcessorBusyBox,
		Output:      rJar,
		Implicits:   deps,
		Description: "ResourceProcessorBusyBox",
		Args: map[string]string{
			"rTxt":     rTxt.String(),
			"manifest": manifest.String(),
			"args":     strings.Join(args, " "),
		},
	})
}

type resourcesNode struct {
	resPackage          android.Path
	manifest            android.Path
	additionalManifests android.Paths
	rTxt                android.Path
	rJar                android.Path
	assets              android.OptionalPath

	usedResourceProcessor bool
}

type transitiveAarDeps []*resourcesNode

func (t transitiveAarDeps) resPackages() android.Paths {
	paths := make(android.Paths, 0, len(t))
	for _, dep := range t {
		paths = append(paths, dep.resPackage)
	}
	return paths
}

func (t transitiveAarDeps) manifests() android.Paths {
	paths := make(android.Paths, 0, len(t))
	for _, dep := range t {
		paths = append(paths, dep.manifest)
		paths = append(paths, dep.additionalManifests...)
	}
	return paths
}

func (t transitiveAarDeps) resourceProcessorDeps() (args []string, deps android.Paths) {
	for _, dep := range t {
		args = append(args, "--library="+dep.rTxt.String()+","+dep.manifest.String())
		deps = append(deps, dep.rTxt, dep.manifest)
	}
	return args, deps
}

func (t transitiveAarDeps) assets() android.Paths {
	paths := make(android.Paths, 0, len(t))
	for _, dep := range t {
		if dep.assets.Valid() {
			paths = append(paths, dep.assets.Path())
		}
	}
	return paths
}

// aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
func aaptLibs(ctx android.ModuleContext, sdkContext android.SdkContext,
	classLoaderContexts dexpreopt.ClassLoaderContextMap, usesLibrary *usesLibrary) (
	staticResourcesNodes, sharedResourcesNodes depset.DepSet[*resourcesNode], staticRRODirs depset.DepSet[rroDir],
	staticManifests depset.DepSet[android.Path], sharedLibs android.Paths, flags []string) {

	if classLoaderContexts == nil {
		// Not all callers need to compute class loader context, those who don't just pass nil.
		// Create a temporary class loader context here (it will be computed, but not used).
		classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
	}

	sdkDep := decodeSdkDep(ctx, sdkContext)
	if sdkDep.useFiles {
		sharedLibs = append(sharedLibs, sdkDep.jars...)
	}

	var staticResourcesNodeDepSets []depset.DepSet[*resourcesNode]
	var sharedResourcesNodeDepSets []depset.DepSet[*resourcesNode]
	rroDirsDepSetBuilder := depset.NewBuilder[rroDir](depset.TOPOLOGICAL)
	manifestsDepSetBuilder := depset.NewBuilder[android.Path](depset.TOPOLOGICAL)

	ctx.VisitDirectDeps(func(module android.Module) {
		depTag := ctx.OtherModuleDependencyTag(module)

		var exportPackage android.Path
		aarDep, _ := module.(AndroidLibraryDependency)
		if aarDep != nil {
			exportPackage = aarDep.ExportPackage()
		}

		switch depTag {
		case instrumentationForTag:
			// Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2.
		case sdkLibTag, libTag, rroDepTag:
			if exportPackage != nil {
				sharedResourcesNodeDepSets = append(sharedResourcesNodeDepSets, aarDep.ResourcesNodeDepSet())
				sharedLibs = append(sharedLibs, exportPackage)
			}
		case frameworkResTag, lineageResTag:
			if exportPackage != nil {
				sharedLibs = append(sharedLibs, exportPackage)
			}
		case staticLibTag:
			if exportPackage != nil {
				staticResourcesNodeDepSets = append(staticResourcesNodeDepSets, aarDep.ResourcesNodeDepSet())
				rroDirsDepSetBuilder.Transitive(aarDep.RRODirsDepSet())
				manifestsDepSetBuilder.Transitive(aarDep.ManifestsDepSet())
			}
		}

		addCLCFromDep(ctx, module, classLoaderContexts)
		if usesLibrary != nil {
			addMissingOptionalUsesLibsFromDep(ctx, module, usesLibrary)
		}
	})

	// AAPT2 overlays are in lowest to highest priority order, the topological order will be reversed later.
	// Reverse the dependency order now going into the depset so that it comes out in order after the second
	// reverse later.
	// NOTE: this is legacy and probably incorrect behavior, for most other cases (e.g. conflicting classes in
	// dependencies) the highest priority dependency is listed first, but for resources the highest priority
	// dependency has to be listed last.  This is also inconsistent with the way manifests from the same
	// transitive dependencies are merged.
	staticResourcesNodes = depset.New(depset.TOPOLOGICAL, nil,
		android.ReverseSliceInPlace(staticResourcesNodeDepSets))
	sharedResourcesNodes = depset.New(depset.TOPOLOGICAL, nil,
		android.ReverseSliceInPlace(sharedResourcesNodeDepSets))

	staticRRODirs = rroDirsDepSetBuilder.Build()
	staticManifests = manifestsDepSetBuilder.Build()

	if len(staticResourcesNodes.ToList()) > 0 {
		flags = append(flags, "--auto-add-overlay")
	}

	for _, sharedLib := range sharedLibs {
		flags = append(flags, "-I "+sharedLib.String())
	}

	return staticResourcesNodes, sharedResourcesNodes, staticRRODirs, staticManifests, sharedLibs, flags
}

type AndroidLibrary struct {
	Library
	aapt

	androidLibraryProperties androidLibraryProperties

	aarFile android.WritablePath
}

var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)

func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
	a.usesLibrary.deps(ctx, false)
	a.Module.deps(ctx)
	sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
	if sdkDep.hasFrameworkLibs() {
		a.aapt.deps(ctx, sdkDep)
	}

	for _, aconfig_declaration := range a.aaptProperties.Flags_packages {
		ctx.AddDependency(ctx.Module(), aconfigDeclarationTag, aconfig_declaration)
	}
}

func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
	a.aapt.isLibrary = true
	a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
	if a.usesLibrary.shouldDisableDexpreopt {
		a.dexpreopter.disableDexpreopt()
	}
	aconfigTextFilePaths := getAconfigFilePaths(ctx)
	a.aapt.buildActions(ctx,
		aaptBuildActionOptions{
			sdkContext:                     android.SdkContext(a),
			classLoaderContexts:            a.classLoaderContexts,
			enforceDefaultTargetSdkVersion: false,
			aconfigTextFiles:               aconfigTextFilePaths,
			usesLibrary:                    &a.usesLibrary,
		},
	)

	apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
	a.hideApexVariantFromMake = !apexInfo.IsForPlatform()

	a.stem = proptools.StringDefault(a.overridableProperties.Stem, ctx.ModuleName())

	ctx.CheckbuildFile(a.aapt.proguardOptionsFile)
	ctx.CheckbuildFile(a.aapt.exportPackage)
	if a.useResourceProcessorBusyBox(ctx) {
		ctx.CheckbuildFile(a.aapt.rJar)
	} else {
		ctx.CheckbuildFile(a.aapt.aaptSrcJar)
	}

	// apps manifests are handled by aapt, don't let Module see them
	a.properties.Manifest = nil

	a.linter.mergedManifest = a.aapt.mergedManifestFile
	a.linter.manifest = a.aapt.manifestPath
	a.linter.resources = a.aapt.resourceFiles

	proguardSpecInfo := a.collectProguardSpecInfo(ctx)
	android.SetProvider(ctx, ProguardSpecInfoProvider, proguardSpecInfo)
	exportedProguardFlagsFiles := proguardSpecInfo.ProguardFlagsFiles.ToList()
	a.extraProguardFlagsFiles = append(a.extraProguardFlagsFiles, exportedProguardFlagsFiles...)
	a.extraProguardFlagsFiles = append(a.extraProguardFlagsFiles, a.proguardOptionsFile)

	combinedExportedProguardFlagFile := android.PathForModuleOut(ctx, "export_proguard_flags")
	writeCombinedProguardFlagsFile(ctx, combinedExportedProguardFlagFile, exportedProguardFlagsFiles)
	a.combinedExportedProguardFlagsFile = combinedExportedProguardFlagFile

	var extraSrcJars android.Paths
	var extraCombinedJars android.Paths
	var extraClasspathJars android.Paths
	if a.useResourceProcessorBusyBox(ctx) {
		// When building a library with ResourceProcessorBusyBox enabled ResourceProcessorBusyBox for this
		// library and each of the transitive static android_library dependencies has already created an
		// R.class file for the appropriate package.  Add all of those R.class files to the classpath.
		extraClasspathJars = a.transitiveAaptRJars
	} else {
		// When building a library without ResourceProcessorBusyBox the aapt2 rule creates R.srcjar containing
		// R.java files for the library's package and the packages from all transitive static android_library
		// dependencies.  Compile the srcjar alongside the rest of the sources.
		extraSrcJars = android.Paths{a.aapt.aaptSrcJar}
	}

	a.Module.compile(ctx, extraSrcJars, extraClasspathJars, extraCombinedJars, nil)

	a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar")
	var res android.Paths
	if a.androidLibraryProperties.BuildAAR {
		BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res)
	}

	prebuiltJniPackages := android.Paths{}
	ctx.VisitDirectDeps(func(module android.Module) {
		if info, ok := android.OtherModuleProvider(ctx, module, JniPackageProvider); ok {
			prebuiltJniPackages = append(prebuiltJniPackages, info.JniPackages...)
		}
	})
	if len(prebuiltJniPackages) > 0 {
		android.SetProvider(ctx, JniPackageProvider, JniPackageInfo{
			JniPackages: prebuiltJniPackages,
		})
	}

	android.SetProvider(ctx, FlagsPackagesProvider, FlagsPackages{
		AconfigTextFiles: aconfigTextFilePaths,
	})

	a.setOutputFiles(ctx)
}

func (a *AndroidLibrary) setOutputFiles(ctx android.ModuleContext) {
	ctx.SetOutputFiles([]android.Path{a.aarFile}, ".aar")
	setOutputFiles(ctx, a.Library.Module)
}

func (a *AndroidLibrary) IDEInfo(ctx android.BaseModuleContext, dpInfo *android.IdeInfo) {
	a.Library.IDEInfo(ctx, dpInfo)
	a.aapt.IDEInfo(ctx, dpInfo)
}

func (a *aapt) IDEInfo(ctx android.BaseModuleContext, dpInfo *android.IdeInfo) {
	if a.rJar != nil {
		dpInfo.Jars = append(dpInfo.Jars, a.rJar.String())
	}
}

// android_library builds and links sources into a `.jar` file for the device along with Android resources.
//
// An android_library has a single variant that produces a `.jar` file containing `.class` files that were
// compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled
// with aapt2.  This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
// an android_app module.
func AndroidLibraryFactory() android.Module {
	module := &AndroidLibrary{}

	module.Module.addHostAndDeviceProperties()
	module.AddProperties(
		&module.aaptProperties,
		&module.androidLibraryProperties,
		&module.sourceProperties)

	module.androidLibraryProperties.BuildAAR = true
	module.Module.linter.library = true

	android.InitApexModule(module)
	InitJavaModule(module, android.DeviceSupported)
	return module
}

//
// AAR (android library) prebuilts
//

// Properties for android_library_import
type AARImportProperties struct {
	// ARR (android library prebuilt) filepath. Exactly one ARR is required.
	Aars []string `android:"path"`
	// If not blank, set to the version of the sdk to compile against.
	// Defaults to private.
	// Values are of one of the following forms:
	// 1) numerical API level, "current", "none", or "core_platform"
	// 2) An SDK kind with an API level: "<sdk kind>_<API level>"
	// See build/soong/android/sdk_version.go for the complete and up to date list of SDK kinds.
	// If the SDK kind is empty, it will be set to public
	Sdk_version *string
	// If not blank, set the minimum version of the sdk that the compiled artifacts will run against.
	// Defaults to sdk_version if not set. See sdk_version for possible values.
	Min_sdk_version *string
	// List of java static libraries that the included ARR (android library prebuilts) has dependencies to.
	Static_libs proptools.Configurable[[]string]
	// List of java libraries that the included ARR (android library prebuilts) has dependencies to.
	Libs []string
	// If set to true, run Jetifier against .aar file. Defaults to false.
	Jetifier *bool
	// If true, extract JNI libs from AAR archive. These libs will be accessible to android_app modules and
	// will be passed transitively through android_libraries to an android_app.
	//TODO(b/241138093) evaluate whether we can have this flag default to true for Bazel conversion
	Extract_jni *bool

	// If set, overrides the manifest extracted from the AAR with the provided path.
	Manifest *string `android:"path"`
}

type AARImport struct {
	android.ModuleBase
	android.DefaultableModuleBase
	android.ApexModuleBase
	prebuilt android.Prebuilt

	// Functionality common to Module and Import.
	embeddableInModuleAndImport

	providesTransitiveHeaderJarsForR8

	properties AARImportProperties

	headerJarFile                      android.Path
	implementationJarFile              android.Path
	implementationAndResourcesJarFile  android.Path
	proguardFlags                      android.Path
	exportPackage                      android.Path
	transitiveAaptResourcePackagesFile android.Path
	extraAaptPackagesFile              android.Path
	manifest                           android.Path
	assetsPackage                      android.Path
	rTxt                               android.Path
	rJar                               android.Path

	resourcesNodesDepSet depset.DepSet[*resourcesNode]
	manifestsDepSet      depset.DepSet[android.Path]

	hideApexVariantFromMake bool

	aarPath     android.Path
	jniPackages android.Paths

	sdkVersion    android.SdkSpec
	minSdkVersion android.ApiLevel

	usesLibrary
	classLoaderContexts dexpreopt.ClassLoaderContextMap
}

func (a *AARImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
	return android.SdkSpecFrom(ctx, String(a.properties.Sdk_version))
}

func (a *AARImport) SystemModules() string {
	return ""
}

func (a *AARImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
	if a.properties.Min_sdk_version != nil {
		return android.ApiLevelFrom(ctx, *a.properties.Min_sdk_version)
	}
	return a.SdkVersion(ctx).ApiLevel
}

func (a *AARImport) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel {
	return android.SdkSpecFrom(ctx, "").ApiLevel
}

func (a *AARImport) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
	return a.SdkVersion(ctx).ApiLevel
}

func (a *AARImport) javaVersion() string {
	return ""
}

var _ AndroidLibraryDependency = (*AARImport)(nil)

func (a *AARImport) ExportPackage() android.Path {
	return a.exportPackage
}
func (a *AARImport) ResourcesNodeDepSet() depset.DepSet[*resourcesNode] {
	return a.resourcesNodesDepSet
}

func (a *AARImport) RRODirsDepSet() depset.DepSet[rroDir] {
	return depset.New[rroDir](depset.TOPOLOGICAL, nil, nil)
}

func (a *AARImport) ManifestsDepSet() depset.DepSet[android.Path] {
	return a.manifestsDepSet
}

// RRO enforcement is not available on aar_import since its RRO dirs are not
// exported.
func (a *AARImport) SetRROEnforcedForDependent(enforce bool) {
}

// RRO enforcement is not available on aar_import since its RRO dirs are not
// exported.
func (a *AARImport) IsRROEnforced(ctx android.BaseModuleContext) bool {
	return false
}

func (a *AARImport) Prebuilt() *android.Prebuilt {
	return &a.prebuilt
}

func (a *AARImport) Name() string {
	return a.prebuilt.Name(a.ModuleBase.Name())
}

func (a *AARImport) JacocoReportClassesFile() android.Path {
	return nil
}

func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
	if !ctx.Config().AlwaysUsePrebuiltSdks() {
		sdkDep := decodeSdkDep(ctx, android.SdkContext(a))
		if sdkDep.useModule && sdkDep.frameworkResModule != "" {
			ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
		}
		if sdkDep.useModule && sdkDep.lineageResModule != "" {
			ctx.AddVariationDependencies(nil, lineageResTag, sdkDep.lineageResModule)
		}
	}

	ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...)
	ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs.GetOrDefault(ctx, nil)...)

	a.usesLibrary.deps(ctx, false)
}

type JniPackageInfo struct {
	// List of zip files containing JNI libraries
	// Zip files should have directory structure jni/<arch>/*.so
	JniPackages android.Paths
}

var JniPackageProvider = blueprint.NewProvider[JniPackageInfo]()

// Unzip an AAR and extract the JNI libs for $archString.
var extractJNI = pctx.AndroidStaticRule("extractJNI",
	blueprint.RuleParams{
		Command: `rm -rf $out $outDir && touch $out && ` +
			`unzip -qoDD -d $outDir $in "jni/${archString}/*" && ` +
			`jni_files=$$(find $outDir/jni -type f) && ` +
			// print error message if there are no JNI libs for this arch
			`[ -n "$$jni_files" ] || (echo "ERROR: no JNI libs found for arch ${archString}" && exit 1) && ` +
			`${config.SoongZipCmd} -o $out -L 0 -P 'lib/${archString}' ` +
			`-C $outDir/jni/${archString} $$(echo $$jni_files | xargs -n1 printf " -f %s")`,
		CommandDeps: []string{"${config.SoongZipCmd}"},
	},
	"outDir", "archString")

// Unzip an AAR into its constituent files and directories.  Any files in Outputs that don't exist in the AAR will be
// touched to create an empty file. The res directory is not extracted, as it will be extracted in its own rule.
var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
	blueprint.RuleParams{
		Command: `rm -rf $outDir && mkdir -p $outDir && ` +
			`unzip -qoDD -d $outDir $in && rm -rf $outDir/res && touch $out && ` +
			`${config.Zip2ZipCmd} -i $in -o $assetsPackage 'assets/**/*' && ` +
			`${config.MergeZipsCmd} $combinedClassesJar $$(ls $outDir/classes.jar 2> /dev/null) $$(ls $outDir/libs/*.jar 2> /dev/null)`,
		CommandDeps: []string{"${config.MergeZipsCmd}", "${config.Zip2ZipCmd}"},
	},
	"outDir", "combinedClassesJar", "assetsPackage")

func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
	if len(a.properties.Aars) != 1 {
		ctx.PropertyErrorf("aars", "exactly one aar is required")
		return
	}

	a.sdkVersion = a.SdkVersion(ctx)
	a.minSdkVersion = a.MinSdkVersion(ctx)

	apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
	a.hideApexVariantFromMake = !apexInfo.IsForPlatform()

	aarName := ctx.ModuleName() + ".aar"
	a.aarPath = android.PathForModuleSrc(ctx, a.properties.Aars[0])

	if Bool(a.properties.Jetifier) {
		inputFile := a.aarPath
		jetifierPath := android.PathForModuleOut(ctx, "jetifier", aarName)
		TransformJetifier(ctx, jetifierPath, inputFile)
		a.aarPath = jetifierPath
	}

	jarName := ctx.ModuleName() + ".jar"
	extractedAARDir := android.PathForModuleOut(ctx, "aar")
	classpathFile := extractedAARDir.Join(ctx, jarName)

	extractedManifest := extractedAARDir.Join(ctx, "AndroidManifest.xml")
	providedManifest := android.OptionalPathForModuleSrc(ctx, a.properties.Manifest)
	if providedManifest.Valid() {
		a.manifest = providedManifest.Path()
	} else {
		a.manifest = extractedManifest
	}

	rTxt := extractedAARDir.Join(ctx, "R.txt")
	assetsPackage := android.PathForModuleOut(ctx, "assets.zip")
	proguardFlags := extractedAARDir.Join(ctx, "proguard.txt")
	transitiveProguardFlags, transitiveUnconditionalExportedFlags := collectDepProguardSpecInfo(ctx)
	android.SetProvider(ctx, ProguardSpecInfoProvider, ProguardSpecInfo{
		ProguardFlagsFiles: depset.New[android.Path](
			depset.POSTORDER,
			android.Paths{proguardFlags},
			transitiveProguardFlags,
		),
		UnconditionallyExportedProguardFlags: depset.New[android.Path](
			depset.POSTORDER,
			nil,
			transitiveUnconditionalExportedFlags,
		),
	})

	ctx.Build(pctx, android.BuildParams{
		Rule:        unzipAAR,
		Input:       a.aarPath,
		Outputs:     android.WritablePaths{classpathFile, proguardFlags, extractedManifest, assetsPackage, rTxt},
		Description: "unzip AAR",
		Args: map[string]string{
			"outDir":             extractedAARDir.String(),
			"combinedClassesJar": classpathFile.String(),
			"assetsPackage":      assetsPackage.String(),
		},
	})

	a.proguardFlags = proguardFlags
	a.assetsPackage = assetsPackage
	a.rTxt = rTxt

	// Always set --pseudo-localize, it will be stripped out later for release
	// builds that don't want it.
	compileFlags := []string{"--pseudo-localize"}
	compiledResDir := android.PathForModuleOut(ctx, "flat-res")
	flata := compiledResDir.Join(ctx, "gen_res.flata")
	aapt2CompileZip(ctx, flata, a.aarPath, "res", compileFlags)

	exportPackage := android.PathForModuleOut(ctx, "package-res.apk")
	proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
	aaptRTxt := android.PathForModuleOut(ctx, "R.txt")
	extraAaptPackagesFile := android.PathForModuleOut(ctx, "extra_packages")

	var linkDeps android.Paths

	linkFlags := []string{
		"--static-lib",
		"--merge-only",
		"--auto-add-overlay",
		"--no-static-lib-packages",
	}

	linkFlags = append(linkFlags, "--manifest "+a.manifest.String())
	linkDeps = append(linkDeps, a.manifest)

	staticResourcesNodesDepSet, sharedResourcesNodesDepSet, staticRRODirsDepSet, staticManifestsDepSet, sharedLibs, libFlags :=
		aaptLibs(ctx, android.SdkContext(a), nil, nil)

	_ = sharedResourcesNodesDepSet
	_ = staticRRODirsDepSet

	staticDeps := transitiveAarDeps(staticResourcesNodesDepSet.ToList())

	linkDeps = append(linkDeps, sharedLibs...)
	linkDeps = append(linkDeps, staticDeps.resPackages()...)
	linkFlags = append(linkFlags, libFlags...)

	overlayRes := android.Paths{flata}

	// Treat static library dependencies of static libraries as imports.
	transitiveStaticLibs := staticDeps.resPackages()
	linkDeps = append(linkDeps, transitiveStaticLibs...)
	for _, staticLib := range transitiveStaticLibs {
		linkFlags = append(linkFlags, "-I "+staticLib.String())
	}

	transitiveAssets := android.ReverseSliceInPlace(staticDeps.assets())
	aapt2Link(ctx, exportPackage, nil, proguardOptionsFile, aaptRTxt,
		linkFlags, linkDeps, nil, overlayRes, transitiveAssets, nil, nil)
	ctx.CheckbuildFile(exportPackage)
	a.exportPackage = exportPackage

	rJar := android.PathForModuleOut(ctx, "busybox/R.jar")
	resourceProcessorBusyBoxGenerateBinaryR(ctx, a.rTxt, a.manifest, rJar, nil, true, nil, false)
	ctx.CheckbuildFile(rJar)
	a.rJar = rJar

	aapt2ExtractExtraPackages(ctx, extraAaptPackagesFile, a.rJar)
	a.extraAaptPackagesFile = extraAaptPackagesFile

	resourcesNodesDepSetBuilder := depset.NewBuilder[*resourcesNode](depset.TOPOLOGICAL)
	resourcesNodesDepSetBuilder.Direct(&resourcesNode{
		resPackage: a.exportPackage,
		manifest:   a.manifest,
		rTxt:       a.rTxt,
		rJar:       a.rJar,
		assets:     android.OptionalPathForPath(a.assetsPackage),

		usedResourceProcessor: true,
	})
	resourcesNodesDepSetBuilder.Transitive(staticResourcesNodesDepSet)
	a.resourcesNodesDepSet = resourcesNodesDepSetBuilder.Build()

	manifestDepSetBuilder := depset.NewBuilder[android.Path](depset.TOPOLOGICAL).Direct(a.manifest)
	manifestDepSetBuilder.Transitive(staticManifestsDepSet)
	a.manifestsDepSet = manifestDepSetBuilder.Build()

	transitiveAaptResourcePackages := staticDeps.resPackages().Strings()
	transitiveAaptResourcePackages = slices.DeleteFunc(transitiveAaptResourcePackages, func(p string) bool {
		return p == a.exportPackage.String()
	})
	transitiveAaptResourcePackagesFile := android.PathForModuleOut(ctx, "transitive-res-packages")
	android.WriteFileRule(ctx, transitiveAaptResourcePackagesFile, strings.Join(transitiveAaptResourcePackages, "\n"))
	a.transitiveAaptResourcePackagesFile = transitiveAaptResourcePackagesFile

	a.collectTransitiveHeaderJarsForR8(ctx)

	a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)

	var staticJars android.Paths
	var staticHeaderJars android.Paths
	var staticResourceJars android.Paths
	var transitiveStaticLibsHeaderJars []depset.DepSet[android.Path]
	var transitiveStaticLibsImplementationJars []depset.DepSet[android.Path]
	var transitiveStaticLibsResourceJars []depset.DepSet[android.Path]

	ctx.VisitDirectDeps(func(module android.Module) {
		if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
			tag := ctx.OtherModuleDependencyTag(module)
			switch tag {
			case staticLibTag:
				staticJars = append(staticJars, dep.ImplementationJars...)
				staticHeaderJars = append(staticHeaderJars, dep.HeaderJars...)
				staticResourceJars = append(staticResourceJars, dep.ResourceJars...)
				transitiveStaticLibsHeaderJars = append(transitiveStaticLibsHeaderJars, dep.TransitiveStaticLibsHeaderJars)
				transitiveStaticLibsImplementationJars = append(transitiveStaticLibsImplementationJars, dep.TransitiveStaticLibsImplementationJars)
				transitiveStaticLibsResourceJars = append(transitiveStaticLibsResourceJars, dep.TransitiveStaticLibsResourceJars)
			}
		}
		addCLCFromDep(ctx, module, a.classLoaderContexts)
		addMissingOptionalUsesLibsFromDep(ctx, module, &a.usesLibrary)
	})

	completeStaticLibsHeaderJars := depset.New(depset.PREORDER, android.Paths{classpathFile}, transitiveStaticLibsHeaderJars)
	completeStaticLibsImplementationJars := depset.New(depset.PREORDER, android.Paths{classpathFile}, transitiveStaticLibsImplementationJars)
	completeStaticLibsResourceJars := depset.New(depset.PREORDER, nil, transitiveStaticLibsResourceJars)

	var implementationJarFile android.Path
	var combineJars android.Paths
	if ctx.Config().UseTransitiveJarsInClasspath() {
		combineJars = completeStaticLibsImplementationJars.ToList()
	} else {
		combineJars = append(android.Paths{classpathFile}, staticJars...)
	}

	if len(combineJars) > 1 {
		implementationJarOutputPath := android.PathForModuleOut(ctx, "combined", jarName)
		TransformJarsToJar(ctx, implementationJarOutputPath, "combine", combineJars, android.OptionalPath{}, false, nil, nil)
		implementationJarFile = implementationJarOutputPath
	} else {
		implementationJarFile = classpathFile
	}

	var resourceJarFile android.Path
	var resourceJars android.Paths
	if ctx.Config().UseTransitiveJarsInClasspath() {
		resourceJars = completeStaticLibsResourceJars.ToList()
	} else {
		resourceJars = staticResourceJars
	}
	if len(resourceJars) > 1 {
		combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
		TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
			false, nil, nil)
		resourceJarFile = combinedJar
	} else if len(resourceJars) == 1 {
		resourceJarFile = resourceJars[0]
	}

	// merge implementation jar with resources if necessary
	var implementationAndResourcesJars android.Paths
	if ctx.Config().UseTransitiveJarsInClasspath() {
		implementationAndResourcesJars = append(slices.Clone(resourceJars), combineJars...)
	} else {
		implementationAndResourcesJars = android.PathsIfNonNil(resourceJarFile, implementationJarFile)
	}
	var implementationAndResourcesJar android.Path
	if len(implementationAndResourcesJars) > 1 {
		combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
		TransformJarsToJar(ctx, combinedJar, "for resources", implementationAndResourcesJars, android.OptionalPath{},
			false, nil, nil)
		implementationAndResourcesJar = combinedJar
	} else {
		implementationAndResourcesJar = implementationAndResourcesJars[0]
	}

	a.implementationJarFile = implementationJarFile
	// Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
	a.implementationAndResourcesJarFile = implementationAndResourcesJar.WithoutRel()

	var headerJars android.Paths
	if ctx.Config().UseTransitiveJarsInClasspath() {
		headerJars = completeStaticLibsHeaderJars.ToList()
	} else {
		headerJars = append(android.Paths{classpathFile}, staticHeaderJars...)
	}
	if len(headerJars) > 1 {
		headerJarFile := android.PathForModuleOut(ctx, "turbine-combined", jarName)
		TransformJarsToJar(ctx, headerJarFile, "combine header jars", headerJars, android.OptionalPath{}, false, nil, nil)
		a.headerJarFile = headerJarFile
	} else {
		a.headerJarFile = headerJars[0]
	}

	if ctx.Config().UseTransitiveJarsInClasspath() {
		ctx.CheckbuildFile(classpathFile)
	} else {
		ctx.CheckbuildFile(a.headerJarFile)
		ctx.CheckbuildFile(a.implementationJarFile)
	}

	android.SetProvider(ctx, JavaInfoProvider, &JavaInfo{
		HeaderJars:                             android.PathsIfNonNil(a.headerJarFile),
		LocalHeaderJars:                        android.PathsIfNonNil(classpathFile),
		TransitiveStaticLibsHeaderJars:         completeStaticLibsHeaderJars,
		TransitiveStaticLibsImplementationJars: completeStaticLibsImplementationJars,
		TransitiveStaticLibsResourceJars:       completeStaticLibsResourceJars,
		ResourceJars:                           android.PathsIfNonNil(resourceJarFile),
		TransitiveLibsHeaderJarsForR8:          a.transitiveLibsHeaderJarsForR8,
		TransitiveStaticLibsHeaderJarsForR8:    a.transitiveStaticLibsHeaderJarsForR8,
		ImplementationAndResourcesJars:         android.PathsIfNonNil(a.implementationAndResourcesJarFile),
		ImplementationJars:                     android.PathsIfNonNil(a.implementationJarFile),
		StubsLinkType:                          Implementation,
		// TransitiveAconfigFiles: // TODO(b/289117800): LOCAL_ACONFIG_FILES for prebuilts
	})

	if proptools.Bool(a.properties.Extract_jni) {
		for _, t := range ctx.MultiTargets() {
			arch := t.Arch.Abi[0]
			path := android.PathForModuleOut(ctx, arch+"_jni.zip")
			a.jniPackages = append(a.jniPackages, path)

			outDir := android.PathForModuleOut(ctx, "aarForJni")
			aarPath := android.PathForModuleSrc(ctx, a.properties.Aars[0])
			ctx.Build(pctx, android.BuildParams{
				Rule:        extractJNI,
				Input:       aarPath,
				Outputs:     android.WritablePaths{path},
				Description: "extract JNI from AAR",
				Args: map[string]string{
					"outDir":     outDir.String(),
					"archString": arch,
				},
			})
		}
	}

	android.SetProvider(ctx, JniPackageProvider, JniPackageInfo{
		JniPackages: a.jniPackages,
	})

	ctx.SetOutputFiles([]android.Path{a.implementationAndResourcesJarFile}, "")
	ctx.SetOutputFiles([]android.Path{a.aarPath}, ".aar")
}

func (a *AARImport) HeaderJars() android.Paths {
	return android.Paths{a.headerJarFile}
}

func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
	return android.Paths{a.implementationAndResourcesJarFile}
}

func (a *AARImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
	return OptionalDexJarPath{}
}

func (a *AARImport) DexJarInstallPath() android.Path {
	return nil
}

func (a *AARImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
	return a.classLoaderContexts
}

var _ UsesLibraryDependency = (*AARImport)(nil)

var _ android.ApexModule = (*AARImport)(nil)

// Implements android.ApexModule
func (a *AARImport) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
	return a.depIsInSameApex(ctx, dep)
}

// Implements android.ApexModule
func (a *AARImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
	sdkVersion android.ApiLevel) error {
	return nil
}

var _ android.PrebuiltInterface = (*AARImport)(nil)

func (a *AARImport) UsesLibrary() *usesLibrary {
	return &a.usesLibrary
}

var _ ModuleWithUsesLibrary = (*AARImport)(nil)

// android_library_import imports an `.aar` file into the build graph as if it was built with android_library.
//
// This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of
// an android_app module.
func AARImportFactory() android.Module {
	module := &AARImport{}

	module.AddProperties(
		&module.properties,
		&module.usesLibrary.usesLibraryProperties,
	)

	android.InitPrebuiltModule(module, &module.properties.Aars)
	android.InitApexModule(module)
	InitJavaModuleMultiTargets(module, android.DeviceSupported)
	return module
}

func (a *AARImport) IDEInfo(ctx android.BaseModuleContext, dpInfo *android.IdeInfo) {
	dpInfo.Jars = append(dpInfo.Jars, a.headerJarFile.String(), a.rJar.String())
}
