Merge changes from topic "mkboot"

* changes:
  Add bootimg module type
  android_filesystem supports compressed cpio format
  add prebuilt_kernel_modules module
  arch.<arch>.deps now works in android_filesystem
diff --git a/android/apex.go b/android/apex.go
index 31c62e9..b87ff09 100644
--- a/android/apex.go
+++ b/android/apex.go
@@ -111,6 +111,19 @@
 	return false
 }
 
+// InApexByBaseName tells whether this apex variant of the module is part of the given APEX or not,
+// where the APEX is specified by its canonical base name, i.e. typically beginning with
+// "com.android.". In particular this function doesn't differentiate between source and prebuilt
+// APEXes, where the latter may have "prebuilt_" prefixes.
+func (i ApexInfo) InApexByBaseName(apex string) bool {
+	for _, a := range i.InApexes {
+		if RemoveOptionalPrebuiltPrefix(a) == apex {
+			return true
+		}
+	}
+	return false
+}
+
 // ApexTestForInfo stores the contents of APEXes for which this module is a test - although this
 // module is not part of the APEX - and thus has access to APEX internals.
 type ApexTestForInfo struct {
@@ -830,9 +843,8 @@
 		return
 	}
 
-	// do not enforce deps.min_sdk_version if APEX/APK doesn't set min_sdk_version or
-	// min_sdk_version is not finalized (e.g. current or codenames)
-	if minSdkVersion.IsCurrent() {
+	// do not enforce deps.min_sdk_version if APEX/APK doesn't set min_sdk_version
+	if minSdkVersion.IsNone() {
 		return
 	}
 
diff --git a/android/api_levels.go b/android/api_levels.go
index 08328e1..1b53f3f 100644
--- a/android/api_levels.go
+++ b/android/api_levels.go
@@ -81,6 +81,10 @@
 	return this.value == "current"
 }
 
+func (this ApiLevel) IsNone() bool {
+	return this.number == -1
+}
+
 // Returns -1 if the current API level is less than the argument, 0 if they
 // are equal, and 1 if it is greater than the argument.
 func (this ApiLevel) CompareTo(other ApiLevel) int {
diff --git a/android/filegroup.go b/android/filegroup.go
index fd4a2fe..3d1bbc5 100644
--- a/android/filegroup.go
+++ b/android/filegroup.go
@@ -23,7 +23,7 @@
 
 func init() {
 	RegisterModuleType("filegroup", FileGroupFactory)
-	RegisterBp2BuildMutator("filegroup", bp2buildMutator)
+	RegisterBp2BuildMutator("filegroup", FilegroupBp2Build)
 }
 
 // https://docs.bazel.build/versions/master/be/general.html#filegroup
@@ -51,7 +51,7 @@
 func (bfg *bazelFilegroup) GenerateAndroidBuildActions(ctx ModuleContext) {}
 
 // TODO: Create helper functions to avoid this boilerplate.
-func bp2buildMutator(ctx TopDownMutatorContext) {
+func FilegroupBp2Build(ctx TopDownMutatorContext) {
 	if m, ok := ctx.Module().(*fileGroup); ok {
 		name := "__bp2build__" + m.base().BaseModuleName()
 		ctx.CreateModule(BazelFileGroupFactory, &bazelFilegroupAttributes{
diff --git a/android/mutator.go b/android/mutator.go
index 2a2be6c..6b19dc5 100644
--- a/android/mutator.go
+++ b/android/mutator.go
@@ -48,6 +48,14 @@
 func RegisterMutatorsForBazelConversion(ctx *blueprint.Context, bp2buildMutators []RegisterMutatorFunc) {
 	mctx := &registerMutatorsContext{}
 
+	sharedMutators := []RegisterMutatorFunc{
+		RegisterDefaultsPreArchMutators,
+	}
+
+	for _, f := range sharedMutators {
+		f(mctx)
+	}
+
 	// Register bp2build mutators
 	for _, f := range bp2buildMutators {
 		f(mctx)
diff --git a/android/testing.go b/android/testing.go
index 76172d1..5640c29 100644
--- a/android/testing.go
+++ b/android/testing.go
@@ -89,7 +89,7 @@
 	f := func(ctx RegisterMutatorsContext) {
 		ctx.TopDown(mutatorName, m)
 	}
-	bp2buildMutators = append(bp2buildMutators, f)
+	ctx.bp2buildMutators = append(ctx.bp2buildMutators, f)
 }
 
 func (ctx *TestContext) Register() {
@@ -100,7 +100,7 @@
 
 // RegisterForBazelConversion prepares a test context for bp2build conversion.
 func (ctx *TestContext) RegisterForBazelConversion() {
-	RegisterMutatorsForBazelConversion(ctx.Context.Context, bp2buildMutators)
+	RegisterMutatorsForBazelConversion(ctx.Context.Context, ctx.bp2buildMutators)
 }
 
 func (ctx *TestContext) ParseFileList(rootDir string, filePaths []string) (deps []string, errs []error) {
diff --git a/apex/Android.bp b/apex/Android.bp
index 77dde72..b6fdcf4 100644
--- a/apex/Android.bp
+++ b/apex/Android.bp
@@ -25,6 +25,7 @@
     ],
     testSrcs: [
         "apex_test.go",
+        "boot_image_test.go",
         "vndk_test.go",
     ],
     pluginFor: ["soong_build"],
diff --git a/apex/apex.go b/apex/apex.go
index 384d6c7..c897042 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -89,6 +89,9 @@
 
 	Multilib apexMultilibProperties
 
+	// List of boot images that are embedded inside this APEX bundle.
+	Boot_images []string
+
 	// List of java libraries that are embedded inside this APEX bundle.
 	Java_libs []string
 
@@ -544,6 +547,7 @@
 	certificateTag = dependencyTag{name: "certificate"}
 	executableTag  = dependencyTag{name: "executable", payload: true}
 	fsTag          = dependencyTag{name: "filesystem", payload: true}
+	bootImageTag   = dependencyTag{name: "bootImage", payload: true}
 	javaLibTag     = dependencyTag{name: "javaLib", payload: true}
 	jniLibTag      = dependencyTag{name: "jniLib", payload: true}
 	keyTag         = dependencyTag{name: "key"}
@@ -721,6 +725,7 @@
 
 	// Common-arch dependencies come next
 	commonVariation := ctx.Config().AndroidCommonTarget.Variations()
+	ctx.AddFarVariationDependencies(commonVariation, bootImageTag, a.properties.Boot_images...)
 	ctx.AddFarVariationDependencies(commonVariation, javaLibTag, a.properties.Java_libs...)
 	ctx.AddFarVariationDependencies(commonVariation, bpfTag, a.properties.Bpfs...)
 	ctx.AddFarVariationDependencies(commonVariation, fsTag, a.properties.Filesystems...)
@@ -730,10 +735,6 @@
 		if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
 			ctx.AddFarVariationDependencies(commonVariation, javaLibTag, "jacocoagent")
 		}
-		// The ART boot image depends on dex2oat to compile it.
-		if !java.SkipDexpreoptBootJars(ctx) {
-			dexpreopt.RegisterToolDeps(ctx)
-		}
 	}
 
 	// Dependencies for signing
@@ -854,11 +855,17 @@
 		Contents: apexContents,
 	})
 
+	minSdkVersion := a.minSdkVersion(mctx)
+	// When min_sdk_version is not set, the apex is built against FutureApiLevel.
+	if minSdkVersion.IsNone() {
+		minSdkVersion = android.FutureApiLevel
+	}
+
 	// This is the main part of this mutator. Mark the collected dependencies that they need to
 	// be built for this apexBundle.
 	apexInfo := android.ApexInfo{
 		ApexVariationName: mctx.ModuleName(),
-		MinSdkVersionStr:  a.minSdkVersion(mctx).String(),
+		MinSdkVersionStr:  minSdkVersion.String(),
 		RequiredSdks:      a.RequiredSdks(),
 		Updatable:         a.Updatable(),
 		InApexes:          []string{mctx.ModuleName()},
@@ -1642,6 +1649,23 @@
 				} else {
 					ctx.PropertyErrorf("binaries", "%q is neither cc_binary, rust_binary, (embedded) py_binary, (host) blueprint_go_binary, (host) bootstrap_go_binary, nor sh_binary", depName)
 				}
+			case bootImageTag:
+				{
+					if _, ok := child.(*java.BootImageModule); !ok {
+						ctx.PropertyErrorf("boot_images", "%q is not a boot_image module", depName)
+						return false
+					}
+					bootImageInfo := ctx.OtherModuleProvider(child, java.BootImageInfoProvider).(java.BootImageInfo)
+					for arch, files := range bootImageInfo.AndroidBootImageFilesByArchType() {
+						dirInApex := filepath.Join("javalib", arch.String())
+						for _, f := range files {
+							androidMkModuleName := "javalib_" + arch.String() + "_" + filepath.Base(f.String())
+							// TODO(b/177892522) - consider passing in the boot image module here instead of nil
+							af := newApexFile(ctx, f, androidMkModuleName, dirInApex, etc, nil)
+							filesInfo = append(filesInfo, af)
+						}
+					}
+				}
 			case javaLibTag:
 				switch child.(type) {
 				case *java.Library, *java.SdkLibrary, *java.DexImport, *java.SdkLibraryImport, *java.Import:
@@ -1856,25 +1880,6 @@
 		return
 	}
 
-	if a.artApex {
-		// Specific to the ART apex: dexpreopt artifacts for libcore Java libraries. Build rules are
-		// generated by the dexpreopt singleton, and here we access build artifacts via the global
-		// boot image config.
-		for arch, files := range java.DexpreoptedArtApexJars(ctx) {
-			dirInApex := filepath.Join("javalib", arch.String())
-			for _, f := range files {
-				localModule := "javalib_" + arch.String() + "_" + filepath.Base(f.String())
-				af := newApexFile(ctx, f, localModule, dirInApex, etc, nil)
-				filesInfo = append(filesInfo, af)
-			}
-		}
-		// Call GetGlobalSoongConfig to initialize it, which may be necessary if dexpreopt is
-		// disabled for libraries/apps, but boot images are still needed.
-		if !java.SkipDexpreoptBootJars(ctx) {
-			dexpreopt.GetGlobalSoongConfig(ctx)
-		}
-	}
-
 	// Remove duplicates in filesInfo
 	removeDup := func(filesInfo []apexFile) []apexFile {
 		encountered := make(map[string]apexFile)
@@ -2116,17 +2121,13 @@
 func (a *apexBundle) minSdkVersion(ctx android.BaseModuleContext) android.ApiLevel {
 	ver := proptools.String(a.properties.Min_sdk_version)
 	if ver == "" {
-		return android.FutureApiLevel
+		return android.NoneApiLevel
 	}
 	apiLevel, err := android.ApiLevelFromUser(ctx, ver)
 	if err != nil {
 		ctx.PropertyErrorf("min_sdk_version", "%s", err.Error())
 		return android.NoneApiLevel
 	}
-	if apiLevel.IsPreview() {
-		// All codenames should build against "current".
-		return android.FutureApiLevel
-	}
 	return apiLevel
 }
 
diff --git a/apex/apex_test.go b/apex/apex_test.go
index efe3e04..7f5be7e 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -2135,6 +2135,74 @@
 	expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
 }
 
+func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
+	withSAsActiveCodeNames := func(fs map[string][]byte, config android.Config) {
+		config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("S")
+		config.TestProductVariables.Platform_version_active_codenames = []string{"S"}
+	}
+	testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
+		apex {
+			name: "myapex",
+			key: "myapex.key",
+			native_shared_libs: ["libfoo"],
+			min_sdk_version: "S",
+		}
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+		cc_library {
+			name: "libfoo",
+			shared_libs: ["libbar"],
+			apex_available: ["myapex"],
+			min_sdk_version: "29",
+		}
+		cc_library {
+			name: "libbar",
+			apex_available: ["myapex"],
+		}
+	`, withSAsActiveCodeNames)
+}
+
+func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
+	withSAsActiveCodeNames := func(fs map[string][]byte, config android.Config) {
+		config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("S")
+		config.TestProductVariables.Platform_version_active_codenames = []string{"S", "T"}
+	}
+	ctx, _ := testApex(t, `
+		apex {
+			name: "myapex",
+			key: "myapex.key",
+			native_shared_libs: ["libfoo"],
+			min_sdk_version: "S",
+		}
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+		cc_library {
+			name: "libfoo",
+			shared_libs: ["libbar"],
+			apex_available: ["myapex"],
+			min_sdk_version: "S",
+		}
+		cc_library {
+			name: "libbar",
+			stubs: {
+				symbol_file: "libbar.map.txt",
+				versions: ["30", "S", "T"],
+			},
+		}
+	`, withSAsActiveCodeNames)
+
+	// ensure libfoo is linked with "S" version of libbar stub
+	libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
+	libFlags := libfoo.Rule("ld").Args["libFlags"]
+	ensureContains(t, libFlags, "android_arm64_armv8-a_shared_S/libbar.so")
+}
+
 func TestFilesInSubDir(t *testing.T) {
 	ctx, _ := testApex(t, `
 		apex {
@@ -4330,6 +4398,215 @@
 	})
 }
 
+func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
+	transform := func(config *dexpreopt.GlobalConfig) {
+		config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
+	}
+
+	checkBootDexJarPath := func(ctx *android.TestContext, bootDexJarPath string) {
+		s := ctx.SingletonForTests("dex_bootjars")
+		foundLibfooJar := false
+		for _, output := range s.AllOutputs() {
+			if strings.HasSuffix(output, "/libfoo.jar") {
+				foundLibfooJar = true
+				buildRule := s.Output(output)
+				actual := android.NormalizePathForTesting(buildRule.Input)
+				if actual != bootDexJarPath {
+					t.Errorf("Incorrect boot dex jar path '%s', expected '%s'", actual, bootDexJarPath)
+				}
+			}
+		}
+		if !foundLibfooJar {
+			t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs")
+		}
+	}
+
+	t.Run("prebuilt only", func(t *testing.T) {
+		bp := `
+		prebuilt_apex {
+			name: "myapex",
+			arch: {
+				arm64: {
+					src: "myapex-arm64.apex",
+				},
+				arm: {
+					src: "myapex-arm.apex",
+				},
+			},
+			exported_java_libs: ["libfoo"],
+		}
+
+		java_import {
+			name: "libfoo",
+			jars: ["libfoo.jar"],
+			apex_available: ["myapex"],
+		}
+	`
+
+		ctx := testDexpreoptWithApexes(t, bp, "", transform)
+		checkBootDexJarPath(ctx, ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
+	})
+
+	t.Run("prebuilt with source library preferred", func(t *testing.T) {
+		bp := `
+		prebuilt_apex {
+			name: "myapex",
+			arch: {
+				arm64: {
+					src: "myapex-arm64.apex",
+				},
+				arm: {
+					src: "myapex-arm.apex",
+				},
+			},
+			exported_java_libs: ["libfoo"],
+		}
+
+		java_import {
+			name: "libfoo",
+			jars: ["libfoo.jar"],
+			apex_available: ["myapex"],
+		}
+
+		java_library {
+			name: "libfoo",
+			srcs: ["foo/bar/MyClass.java"],
+			apex_available: ["myapex"],
+		}
+	`
+
+		// In this test the source (java_library) libfoo is active since the
+		// prebuilt (java_import) defaults to prefer:false. However the
+		// prebuilt_apex module always depends on the prebuilt, and so it doesn't
+		// find the dex boot jar in it. We either need to disable the source libfoo
+		// or make the prebuilt libfoo preferred.
+		testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", transform)
+	})
+
+	t.Run("prebuilt library preferred with source", func(t *testing.T) {
+		bp := `
+		prebuilt_apex {
+			name: "myapex",
+			arch: {
+				arm64: {
+					src: "myapex-arm64.apex",
+				},
+				arm: {
+					src: "myapex-arm.apex",
+				},
+			},
+			exported_java_libs: ["libfoo"],
+		}
+
+		java_import {
+			name: "libfoo",
+			prefer: true,
+			jars: ["libfoo.jar"],
+			apex_available: ["myapex"],
+		}
+
+		java_library {
+			name: "libfoo",
+			srcs: ["foo/bar/MyClass.java"],
+			apex_available: ["myapex"],
+		}
+	`
+
+		ctx := testDexpreoptWithApexes(t, bp, "", transform)
+		checkBootDexJarPath(ctx, ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
+	})
+
+	t.Run("prebuilt with source apex preferred", func(t *testing.T) {
+		bp := `
+		apex {
+			name: "myapex",
+			key: "myapex.key",
+			java_libs: ["libfoo"],
+		}
+
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+
+		prebuilt_apex {
+			name: "myapex",
+			arch: {
+				arm64: {
+					src: "myapex-arm64.apex",
+				},
+				arm: {
+					src: "myapex-arm.apex",
+				},
+			},
+			exported_java_libs: ["libfoo"],
+		}
+
+		java_import {
+			name: "libfoo",
+			jars: ["libfoo.jar"],
+			apex_available: ["myapex"],
+		}
+
+		java_library {
+			name: "libfoo",
+			srcs: ["foo/bar/MyClass.java"],
+			apex_available: ["myapex"],
+		}
+	`
+
+		ctx := testDexpreoptWithApexes(t, bp, "", transform)
+		checkBootDexJarPath(ctx, ".intermediates/libfoo/android_common_apex10000/aligned/libfoo.jar")
+	})
+
+	t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
+		bp := `
+		apex {
+			name: "myapex",
+			enabled: false,
+			key: "myapex.key",
+			java_libs: ["libfoo"],
+		}
+
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+
+		prebuilt_apex {
+			name: "myapex",
+			arch: {
+				arm64: {
+					src: "myapex-arm64.apex",
+				},
+				arm: {
+					src: "myapex-arm.apex",
+				},
+			},
+			exported_java_libs: ["libfoo"],
+		}
+
+		java_import {
+			name: "libfoo",
+			prefer: true,
+			jars: ["libfoo.jar"],
+			apex_available: ["myapex"],
+		}
+
+		java_library {
+			name: "libfoo",
+			srcs: ["foo/bar/MyClass.java"],
+			apex_available: ["myapex"],
+		}
+	`
+
+		ctx := testDexpreoptWithApexes(t, bp, "", transform)
+		checkBootDexJarPath(ctx, ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
+	})
+}
+
 func TestApexWithTests(t *testing.T) {
 	ctx, config := testApex(t, `
 		apex_test {
@@ -5865,7 +6142,7 @@
 			srcs: ["a.java"],
 			sdk_version: "current",
 			apex_available: [
-				"com.android.art.something",
+				"com.android.art.debug",
 			],
 			hostdex: true,
 		}
@@ -5893,15 +6170,15 @@
 		}
 
 		apex {
-			name: "com.android.art.something",
-			key: "com.android.art.something.key",
+			name: "com.android.art.debug",
+			key: "com.android.art.debug.key",
 			java_libs: ["some-art-lib"],
 			updatable: true,
 			min_sdk_version: "current",
 		}
 
 		apex_key {
-			name: "com.android.art.something.key",
+			name: "com.android.art.debug.key",
 		}
 
 		filegroup {
@@ -5934,10 +6211,11 @@
 		"build/make/target/product/security": nil,
 		"apex_manifest.json":                 nil,
 		"AndroidManifest.xml":                nil,
-		"system/sepolicy/apex/some-updatable-apex-file_contexts":       nil,
-		"system/sepolicy/apex/some-non-updatable-apex-file_contexts":   nil,
-		"system/sepolicy/apex/com.android.art.something-file_contexts": nil,
-		"framework/aidl/a.aidl": nil,
+		"system/sepolicy/apex/myapex-file_contexts":                  nil,
+		"system/sepolicy/apex/some-updatable-apex-file_contexts":     nil,
+		"system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
+		"system/sepolicy/apex/com.android.art.debug-file_contexts":   nil,
+		"framework/aidl/a.aidl":                                      nil,
 	}
 	cc.GatherRequiredFilesForTest(fs)
 
@@ -6000,15 +6278,15 @@
 
 	t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
 		transform = func(config *dexpreopt.GlobalConfig) {
-			config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.something:some-art-lib"})
+			config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
 		}
 		testNoUpdatableJarsInBootImage(t, "", transform)
 	})
 
 	t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
-		err = `module "some-art-lib" from updatable apexes \["com.android.art.something"\] is not allowed in the framework boot image`
+		err = `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
 		transform = func(config *dexpreopt.GlobalConfig) {
-			config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.something:some-art-lib"})
+			config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
 		}
 		testNoUpdatableJarsInBootImage(t, err, transform)
 	})
diff --git a/apex/boot_image_test.go b/apex/boot_image_test.go
new file mode 100644
index 0000000..27a1562
--- /dev/null
+++ b/apex/boot_image_test.go
@@ -0,0 +1,238 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// 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 apex
+
+import (
+	"reflect"
+	"strings"
+	"testing"
+
+	"android/soong/android"
+	"android/soong/dexpreopt"
+	"android/soong/java"
+)
+
+// Contains tests for boot_image logic from java/boot_image.go as the ART boot image requires
+// modules from the ART apex.
+
+func TestBootImages(t *testing.T) {
+	ctx, _ := testApex(t, `
+		java_sdk_library {
+			name: "foo",
+			srcs: ["b.java"],
+			unsafe_ignore_missing_latest_api: true,
+		}
+
+		java_library {
+			name: "bar",
+			srcs: ["b.java"],
+			installable: true,
+		}
+
+		apex {
+			name: "com.android.art",
+			key: "com.android.art.key",
+ 			java_libs: [
+				"baz",
+				"quuz",
+			],
+		}
+
+		apex_key {
+			name: "com.android.art.key",
+			public_key: "com.android.art.avbpubkey",
+			private_key: "com.android.art.pem",
+		}
+
+		java_library {
+			name: "baz",
+			apex_available: [
+				"com.android.art",
+			],
+			srcs: ["b.java"],
+		}
+
+		java_library {
+			name: "quuz",
+			apex_available: [
+				"com.android.art",
+			],
+			srcs: ["b.java"],
+		}
+
+		boot_image {
+			name: "art-boot-image",
+			image_name: "art",
+		}
+
+		boot_image {
+			name: "framework-boot-image",
+			image_name: "boot",
+		}
+`,
+		// Configure some libraries in the art and framework boot images.
+		withArtBootImageJars("com.android.art:baz", "com.android.art:quuz"),
+		withFrameworkBootImageJars("platform:foo", "platform:bar"),
+		withFiles(filesForSdkLibrary),
+		// Some additional files needed for the art apex.
+		withFiles(map[string][]byte{
+			"com.android.art.avbpubkey":                          nil,
+			"com.android.art.pem":                                nil,
+			"system/sepolicy/apex/com.android.art-file_contexts": nil,
+		}),
+	)
+
+	// Make sure that the framework-boot-image is using the correct configuration.
+	checkBootImage(t, ctx, "framework-boot-image", "platform:foo,platform:bar", `
+test_device/dex_bootjars/android/system/framework/arm/boot-foo.art
+test_device/dex_bootjars/android/system/framework/arm/boot-foo.oat
+test_device/dex_bootjars/android/system/framework/arm/boot-foo.vdex
+test_device/dex_bootjars/android/system/framework/arm/boot-bar.art
+test_device/dex_bootjars/android/system/framework/arm/boot-bar.oat
+test_device/dex_bootjars/android/system/framework/arm/boot-bar.vdex
+test_device/dex_bootjars/android/system/framework/arm64/boot-foo.art
+test_device/dex_bootjars/android/system/framework/arm64/boot-foo.oat
+test_device/dex_bootjars/android/system/framework/arm64/boot-foo.vdex
+test_device/dex_bootjars/android/system/framework/arm64/boot-bar.art
+test_device/dex_bootjars/android/system/framework/arm64/boot-bar.oat
+test_device/dex_bootjars/android/system/framework/arm64/boot-bar.vdex
+`)
+
+	// Make sure that the art-boot-image is using the correct configuration.
+	checkBootImage(t, ctx, "art-boot-image", "com.android.art:baz,com.android.art:quuz", `
+test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.art
+test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.oat
+test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot.vdex
+test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.art
+test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.oat
+test_device/dex_artjars/android/apex/art_boot_images/javalib/arm/boot-quuz.vdex
+test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.art
+test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.oat
+test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.vdex
+test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.art
+test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.oat
+test_device/dex_artjars/android/apex/art_boot_images/javalib/arm64/boot-quuz.vdex
+`)
+}
+
+func checkBootImage(t *testing.T, ctx *android.TestContext, moduleName string, expectedConfiguredModules string, expectedBootImageFiles string) {
+	t.Helper()
+
+	bootImage := ctx.ModuleForTests(moduleName, "android_common").Module().(*java.BootImageModule)
+
+	bootImageInfo := ctx.ModuleProvider(bootImage, java.BootImageInfoProvider).(java.BootImageInfo)
+	modules := bootImageInfo.Modules()
+	if actual := modules.String(); actual != expectedConfiguredModules {
+		t.Errorf("invalid modules for %s: expected %q, actual %q", moduleName, expectedConfiguredModules, actual)
+	}
+
+	// Get a list of all the paths in the boot image sorted by arch type.
+	allPaths := []string{}
+	bootImageFilesByArchType := bootImageInfo.AndroidBootImageFilesByArchType()
+	for _, archType := range android.ArchTypeList() {
+		if paths, ok := bootImageFilesByArchType[archType]; ok {
+			for _, path := range paths {
+				allPaths = append(allPaths, android.NormalizePathForTesting(path))
+			}
+		}
+	}
+	if expected, actual := strings.TrimSpace(expectedBootImageFiles), strings.TrimSpace(strings.Join(allPaths, "\n")); !reflect.DeepEqual(expected, actual) {
+		t.Errorf("invalid paths for %s: expected \n%s, actual \n%s", moduleName, expected, actual)
+	}
+}
+
+func modifyDexpreoptConfig(configModifier func(dexpreoptConfig *dexpreopt.GlobalConfig)) func(fs map[string][]byte, config android.Config) {
+	return func(fs map[string][]byte, config android.Config) {
+		// Initialize the dexpreopt GlobalConfig to an empty structure. This has no effect if it has
+		// already been set.
+		pathCtx := android.PathContextForTesting(config)
+		dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
+		dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
+
+		// Retrieve the existing configuration and modify it.
+		dexpreoptConfig = dexpreopt.GetGlobalConfig(pathCtx)
+		configModifier(dexpreoptConfig)
+	}
+}
+
+func withArtBootImageJars(bootJars ...string) func(fs map[string][]byte, config android.Config) {
+	return modifyDexpreoptConfig(func(dexpreoptConfig *dexpreopt.GlobalConfig) {
+		dexpreoptConfig.ArtApexJars = android.CreateTestConfiguredJarList(bootJars)
+	})
+}
+
+func withFrameworkBootImageJars(bootJars ...string) func(fs map[string][]byte, config android.Config) {
+	return modifyDexpreoptConfig(func(dexpreoptConfig *dexpreopt.GlobalConfig) {
+		dexpreoptConfig.BootJars = android.CreateTestConfiguredJarList(bootJars)
+	})
+}
+
+func TestBootImageInApex(t *testing.T) {
+	ctx, _ := testApex(t, `
+		apex {
+			name: "myapex",
+			key: "myapex.key",
+			boot_images: [
+				"mybootimage",
+			],
+		}
+
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+
+		java_library {
+			name: "foo",
+			srcs: ["b.java"],
+			installable: true,
+		}
+
+		java_library {
+			name: "bar",
+			srcs: ["b.java"],
+			installable: true,
+		}
+
+		boot_image {
+			name: "mybootimage",
+			image_name: "boot",
+			apex_available: [
+				"myapex",
+			],
+		}
+`,
+		// Configure some libraries in the framework boot image.
+		withFrameworkBootImageJars("platform:foo", "platform:bar"),
+	)
+
+	ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
+		"javalib/arm/boot-bar.art",
+		"javalib/arm/boot-bar.oat",
+		"javalib/arm/boot-bar.vdex",
+		"javalib/arm/boot-foo.art",
+		"javalib/arm/boot-foo.oat",
+		"javalib/arm/boot-foo.vdex",
+		"javalib/arm64/boot-bar.art",
+		"javalib/arm64/boot-bar.oat",
+		"javalib/arm64/boot-bar.vdex",
+		"javalib/arm64/boot-foo.art",
+		"javalib/arm64/boot-foo.oat",
+		"javalib/arm64/boot-foo.vdex",
+	})
+}
+
+// TODO(b/177892522) - add test for host apex.
diff --git a/apex/builder.go b/apex/builder.go
index 67314d8..16ca74c 100644
--- a/apex/builder.go
+++ b/apex/builder.go
@@ -598,7 +598,7 @@
 
 		// bundletool doesn't understand what "current" is. We need to transform it to
 		// codename
-		if moduleMinSdkVersion.IsCurrent() {
+		if moduleMinSdkVersion.IsCurrent() || moduleMinSdkVersion.IsNone() {
 			minSdkVersion = ctx.Config().DefaultAppTargetSdk(ctx).String()
 		}
 		// apex module doesn't have a concept of target_sdk_version, hence for the time
@@ -780,7 +780,7 @@
 		ctx.PropertyErrorf("test_only_force_compression", "not available")
 		return
 	}
-	compressionEnabled := ctx.Config().CompressedApex() && proptools.BoolDefault(a.properties.Compressible, true)
+	compressionEnabled := ctx.Config().CompressedApex() && proptools.BoolDefault(a.properties.Compressible, false)
 	if apexType == imageApex && (compressionEnabled || a.testOnlyShouldForceCompression()) {
 		a.isCompressed = true
 		unsignedCompressedOutputFile := android.PathForModuleOut(ctx, a.Name()+".capex.unsigned")
diff --git a/bazel/properties.go b/bazel/properties.go
index ac0047b..79956e1 100644
--- a/bazel/properties.go
+++ b/bazel/properties.go
@@ -31,4 +31,7 @@
 type BazelTargetModuleProperties struct {
 	// The Bazel rule class for this target.
 	Rule_class string
+
+	// The target label for the bzl file containing the definition of the rule class.
+	Bzl_load_location string
 }
diff --git a/bp2build/build_conversion.go b/bp2build/build_conversion.go
index a7c3adb..1af1d60 100644
--- a/bp2build/build_conversion.go
+++ b/bp2build/build_conversion.go
@@ -18,6 +18,7 @@
 	"android/soong/android"
 	"fmt"
 	"reflect"
+	"strconv"
 	"strings"
 
 	"github.com/google/blueprint"
@@ -29,8 +30,62 @@
 }
 
 type BazelTarget struct {
-	name    string
-	content string
+	name            string
+	content         string
+	ruleClass       string
+	bzlLoadLocation string
+}
+
+// IsLoadedFromStarlark determines if the BazelTarget's rule class is loaded from a .bzl file,
+// as opposed to a native rule built into Bazel.
+func (t BazelTarget) IsLoadedFromStarlark() bool {
+	return t.bzlLoadLocation != ""
+}
+
+// BazelTargets is a typedef for a slice of BazelTarget objects.
+type BazelTargets []BazelTarget
+
+// String returns the string representation of BazelTargets, without load
+// statements (use LoadStatements for that), since the targets are usually not
+// adjacent to the load statements at the top of the BUILD file.
+func (targets BazelTargets) String() string {
+	var res string
+	for i, target := range targets {
+		res += target.content
+		if i != len(targets)-1 {
+			res += "\n\n"
+		}
+	}
+	return res
+}
+
+// LoadStatements return the string representation of the sorted and deduplicated
+// Starlark rule load statements needed by a group of BazelTargets.
+func (targets BazelTargets) LoadStatements() string {
+	bzlToLoadedSymbols := map[string][]string{}
+	for _, target := range targets {
+		if target.IsLoadedFromStarlark() {
+			bzlToLoadedSymbols[target.bzlLoadLocation] =
+				append(bzlToLoadedSymbols[target.bzlLoadLocation], target.ruleClass)
+		}
+	}
+
+	var loadStatements []string
+	for bzl, ruleClasses := range bzlToLoadedSymbols {
+		loadStatement := "load(\""
+		loadStatement += bzl
+		loadStatement += "\", "
+		ruleClasses = android.SortedUniqueStrings(ruleClasses)
+		for i, ruleClass := range ruleClasses {
+			loadStatement += "\"" + ruleClass + "\""
+			if i != len(ruleClasses)-1 {
+				loadStatement += ", "
+			}
+		}
+		loadStatement += ")"
+		loadStatements = append(loadStatements, loadStatement)
+	}
+	return strings.Join(android.SortedUniqueStrings(loadStatements), "\n")
 }
 
 type bpToBuildContext interface {
@@ -104,8 +159,8 @@
 	return attributes
 }
 
-func GenerateSoongModuleTargets(ctx bpToBuildContext, codegenMode CodegenMode) map[string][]BazelTarget {
-	buildFileToTargets := make(map[string][]BazelTarget)
+func GenerateSoongModuleTargets(ctx bpToBuildContext, codegenMode CodegenMode) map[string]BazelTargets {
+	buildFileToTargets := make(map[string]BazelTargets)
 	ctx.VisitAllModules(func(m blueprint.Module) {
 		dir := ctx.ModuleDir(m)
 		var t BazelTarget
@@ -127,22 +182,44 @@
 	return buildFileToTargets
 }
 
+// Helper method to trim quotes around strings.
+func trimQuotes(s string) string {
+	if s == "" {
+		// strconv.Unquote would error out on empty strings, but this method
+		// allows them, so return the empty string directly.
+		return ""
+	}
+	ret, err := strconv.Unquote(s)
+	if err != nil {
+		// Panic the error immediately.
+		panic(fmt.Errorf("Trying to unquote '%s', but got error: %s", s, err))
+	}
+	return ret
+}
+
 func generateBazelTarget(ctx bpToBuildContext, m blueprint.Module) BazelTarget {
 	// extract the bazel attributes from the module.
 	props := getBuildProperties(ctx, m)
 
 	// extract the rule class name from the attributes. Since the string value
 	// will be string-quoted, remove the quotes here.
-	ruleClass := strings.Replace(props.Attrs["rule_class"], "\"", "", 2)
+	ruleClass := trimQuotes(props.Attrs["rule_class"])
 	// Delete it from being generated in the BUILD file.
 	delete(props.Attrs, "rule_class")
 
+	// extract the bzl_load_location, and also remove the quotes around it here.
+	bzlLoadLocation := trimQuotes(props.Attrs["bzl_load_location"])
+	// Delete it from being generated in the BUILD file.
+	delete(props.Attrs, "bzl_load_location")
+
 	// Return the Bazel target with rule class and attributes, ready to be
 	// code-generated.
 	attributes := propsToAttributes(props.Attrs)
 	targetName := targetNameForBp2Build(ctx, m)
 	return BazelTarget{
-		name: targetName,
+		name:            targetName,
+		ruleClass:       ruleClass,
+		bzlLoadLocation: bzlLoadLocation,
 		content: fmt.Sprintf(
 			bazelTarget,
 			ruleClass,
diff --git a/bp2build/build_conversion_test.go b/bp2build/build_conversion_test.go
index 367e13f..a01a7cd 100644
--- a/bp2build/build_conversion_test.go
+++ b/bp2build/build_conversion_test.go
@@ -268,18 +268,185 @@
 	}
 }
 
-func TestModuleTypeBp2Build(t *testing.T) {
+func TestLoadStatements(t *testing.T) {
 	testCases := []struct {
-		moduleTypeUnderTest        string
-		moduleTypeUnderTestFactory android.ModuleFactory
-		bp                         string
-		expectedBazelTarget        string
-		description                string
+		bazelTargets           BazelTargets
+		expectedLoadStatements string
 	}{
 		{
-			description:                "filegroup with no srcs",
-			moduleTypeUnderTest:        "filegroup",
-			moduleTypeUnderTestFactory: android.FileGroupFactory,
+			bazelTargets: BazelTargets{
+				BazelTarget{
+					name:            "foo",
+					ruleClass:       "cc_library",
+					bzlLoadLocation: "//build/bazel/rules:cc.bzl",
+				},
+			},
+			expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
+		},
+		{
+			bazelTargets: BazelTargets{
+				BazelTarget{
+					name:            "foo",
+					ruleClass:       "cc_library",
+					bzlLoadLocation: "//build/bazel/rules:cc.bzl",
+				},
+				BazelTarget{
+					name:            "bar",
+					ruleClass:       "cc_library",
+					bzlLoadLocation: "//build/bazel/rules:cc.bzl",
+				},
+			},
+			expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
+		},
+		{
+			bazelTargets: BazelTargets{
+				BazelTarget{
+					name:            "foo",
+					ruleClass:       "cc_library",
+					bzlLoadLocation: "//build/bazel/rules:cc.bzl",
+				},
+				BazelTarget{
+					name:            "bar",
+					ruleClass:       "cc_binary",
+					bzlLoadLocation: "//build/bazel/rules:cc.bzl",
+				},
+			},
+			expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
+		},
+		{
+			bazelTargets: BazelTargets{
+				BazelTarget{
+					name:            "foo",
+					ruleClass:       "cc_library",
+					bzlLoadLocation: "//build/bazel/rules:cc.bzl",
+				},
+				BazelTarget{
+					name:            "bar",
+					ruleClass:       "cc_binary",
+					bzlLoadLocation: "//build/bazel/rules:cc.bzl",
+				},
+				BazelTarget{
+					name:            "baz",
+					ruleClass:       "java_binary",
+					bzlLoadLocation: "//build/bazel/rules:java.bzl",
+				},
+			},
+			expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
+load("//build/bazel/rules:java.bzl", "java_binary")`,
+		},
+		{
+			bazelTargets: BazelTargets{
+				BazelTarget{
+					name:            "foo",
+					ruleClass:       "cc_binary",
+					bzlLoadLocation: "//build/bazel/rules:cc.bzl",
+				},
+				BazelTarget{
+					name:            "bar",
+					ruleClass:       "java_binary",
+					bzlLoadLocation: "//build/bazel/rules:java.bzl",
+				},
+				BazelTarget{
+					name:      "baz",
+					ruleClass: "genrule",
+					// Note: no bzlLoadLocation for native rules
+				},
+			},
+			expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
+load("//build/bazel/rules:java.bzl", "java_binary")`,
+		},
+	}
+
+	for _, testCase := range testCases {
+		actual := testCase.bazelTargets.LoadStatements()
+		expected := testCase.expectedLoadStatements
+		if actual != expected {
+			t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
+		}
+	}
+
+}
+
+func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
+	testCases := []struct {
+		bp                       string
+		expectedBazelTarget      string
+		expectedBazelTargetCount int
+		expectedLoadStatements   string
+	}{
+		{
+			bp: `custom {
+    name: "bar",
+}`,
+			expectedBazelTarget: `my_library(
+    name = "bar",
+)
+
+my_proto_library(
+    name = "bar_my_proto_library_deps",
+)
+
+proto_library(
+    name = "bar_proto_library_deps",
+)`,
+			expectedBazelTargetCount: 3,
+			expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
+load("//build/bazel/rules:rules.bzl", "my_library")`,
+		},
+	}
+
+	dir := "."
+	for _, testCase := range testCases {
+		config := android.TestConfig(buildDir, nil, testCase.bp, nil)
+		ctx := android.NewTestContext(config)
+		ctx.RegisterModuleType("custom", customModuleFactory)
+		ctx.RegisterBp2BuildMutator("custom_starlark", customBp2BuildMutatorFromStarlark)
+		ctx.RegisterForBazelConversion()
+
+		_, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
+		android.FailIfErrored(t, errs)
+		_, errs = ctx.ResolveDependencies(config)
+		android.FailIfErrored(t, errs)
+
+		bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, Bp2Build)[dir]
+		if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
+			t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
+		}
+
+		actualBazelTargets := bazelTargets.String()
+		if actualBazelTargets != testCase.expectedBazelTarget {
+			t.Errorf(
+				"Expected generated Bazel target to be '%s', got '%s'",
+				testCase.expectedBazelTarget,
+				actualBazelTargets,
+			)
+		}
+
+		actualLoadStatements := bazelTargets.LoadStatements()
+		if actualLoadStatements != testCase.expectedLoadStatements {
+			t.Errorf(
+				"Expected generated load statements to be '%s', got '%s'",
+				testCase.expectedLoadStatements,
+				actualLoadStatements,
+			)
+		}
+	}
+}
+
+func TestModuleTypeBp2Build(t *testing.T) {
+	testCases := []struct {
+		moduleTypeUnderTest                string
+		moduleTypeUnderTestFactory         android.ModuleFactory
+		moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
+		bp                                 string
+		expectedBazelTarget                string
+		description                        string
+	}{
+		{
+			description:                        "filegroup with no srcs",
+			moduleTypeUnderTest:                "filegroup",
+			moduleTypeUnderTestFactory:         android.FileGroupFactory,
+			moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
 			bp: `filegroup {
 	name: "foo",
 	srcs: [],
@@ -291,9 +458,10 @@
 )`,
 		},
 		{
-			description:                "filegroup with srcs",
-			moduleTypeUnderTest:        "filegroup",
-			moduleTypeUnderTestFactory: android.FileGroupFactory,
+			description:                        "filegroup with srcs",
+			moduleTypeUnderTest:                "filegroup",
+			moduleTypeUnderTestFactory:         android.FileGroupFactory,
+			moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
 			bp: `filegroup {
 	name: "foo",
 	srcs: ["a", "b"],
@@ -307,9 +475,10 @@
 )`,
 		},
 		{
-			description:                "genrule with command line variable replacements",
-			moduleTypeUnderTest:        "genrule",
-			moduleTypeUnderTestFactory: genrule.GenRuleFactory,
+			description:                        "genrule with command line variable replacements",
+			moduleTypeUnderTest:                "genrule",
+			moduleTypeUnderTestFactory:         genrule.GenRuleFactory,
+			moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
 			bp: `genrule {
     name: "foo",
     out: ["foo.out"],
@@ -332,9 +501,10 @@
 )`,
 		},
 		{
-			description:                "genrule using $(locations :label)",
-			moduleTypeUnderTest:        "genrule",
-			moduleTypeUnderTestFactory: genrule.GenRuleFactory,
+			description:                        "genrule using $(locations :label)",
+			moduleTypeUnderTest:                "genrule",
+			moduleTypeUnderTestFactory:         genrule.GenRuleFactory,
+			moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
 			bp: `genrule {
     name: "foo",
     out: ["foo.out"],
@@ -357,9 +527,10 @@
 )`,
 		},
 		{
-			description:                "genrule using $(location) label should substitute first tool label automatically",
-			moduleTypeUnderTest:        "genrule",
-			moduleTypeUnderTestFactory: genrule.GenRuleFactory,
+			description:                        "genrule using $(location) label should substitute first tool label automatically",
+			moduleTypeUnderTest:                "genrule",
+			moduleTypeUnderTestFactory:         genrule.GenRuleFactory,
+			moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
 			bp: `genrule {
     name: "foo",
     out: ["foo.out"],
@@ -383,9 +554,10 @@
 )`,
 		},
 		{
-			description:                "genrule using $(locations) label should substitute first tool label automatically",
-			moduleTypeUnderTest:        "genrule",
-			moduleTypeUnderTestFactory: genrule.GenRuleFactory,
+			description:                        "genrule using $(locations) label should substitute first tool label automatically",
+			moduleTypeUnderTest:                "genrule",
+			moduleTypeUnderTestFactory:         genrule.GenRuleFactory,
+			moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
 			bp: `genrule {
     name: "foo",
     out: ["foo.out"],
@@ -409,9 +581,10 @@
 )`,
 		},
 		{
-			description:                "genrule without tools or tool_files can convert successfully",
-			moduleTypeUnderTest:        "genrule",
-			moduleTypeUnderTestFactory: genrule.GenRuleFactory,
+			description:                        "genrule without tools or tool_files can convert successfully",
+			moduleTypeUnderTest:                "genrule",
+			moduleTypeUnderTestFactory:         genrule.GenRuleFactory,
+			moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
 			bp: `genrule {
     name: "foo",
     out: ["foo.out"],
@@ -436,6 +609,7 @@
 		config := android.TestConfig(buildDir, nil, testCase.bp, nil)
 		ctx := android.NewTestContext(config)
 		ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
+		ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
 		ctx.RegisterForBazelConversion()
 
 		_, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
@@ -459,3 +633,201 @@
 		}
 	}
 }
+
+type bp2buildMutator = func(android.TopDownMutatorContext)
+
+func TestBp2BuildInlinesDefaults(t *testing.T) {
+	testCases := []struct {
+		moduleTypesUnderTest      map[string]android.ModuleFactory
+		bp2buildMutatorsUnderTest map[string]bp2buildMutator
+		bp                        string
+		expectedBazelTarget       string
+		description               string
+	}{
+		{
+			moduleTypesUnderTest: map[string]android.ModuleFactory{
+				"genrule":          genrule.GenRuleFactory,
+				"genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
+			},
+			bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
+				"genrule": genrule.GenruleBp2Build,
+			},
+			bp: `genrule_defaults {
+    name: "gen_defaults",
+    cmd: "do-something $(in) $(out)",
+}
+genrule {
+    name: "gen",
+    out: ["out"],
+    srcs: ["in1"],
+    defaults: ["gen_defaults"],
+}
+`,
+			expectedBazelTarget: `genrule(
+    name = "gen",
+    cmd = "do-something $(SRCS) $(OUTS)",
+    outs = [
+        "out",
+    ],
+    srcs = [
+        "in1",
+    ],
+)`,
+			description: "genrule applies properties from a genrule_defaults dependency if not specified",
+		},
+		{
+			moduleTypesUnderTest: map[string]android.ModuleFactory{
+				"genrule":          genrule.GenRuleFactory,
+				"genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
+			},
+			bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
+				"genrule": genrule.GenruleBp2Build,
+			},
+			bp: `genrule_defaults {
+    name: "gen_defaults",
+    out: ["out-from-defaults"],
+    srcs: ["in-from-defaults"],
+    cmd: "cmd-from-defaults",
+}
+genrule {
+    name: "gen",
+    out: ["out"],
+    srcs: ["in1"],
+    defaults: ["gen_defaults"],
+    cmd: "do-something $(in) $(out)",
+}
+`,
+			expectedBazelTarget: `genrule(
+    name = "gen",
+    cmd = "do-something $(SRCS) $(OUTS)",
+    outs = [
+        "out-from-defaults",
+        "out",
+    ],
+    srcs = [
+        "in-from-defaults",
+        "in1",
+    ],
+)`,
+			description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
+		},
+		{
+			moduleTypesUnderTest: map[string]android.ModuleFactory{
+				"genrule":          genrule.GenRuleFactory,
+				"genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
+			},
+			bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
+				"genrule": genrule.GenruleBp2Build,
+			},
+			bp: `genrule_defaults {
+    name: "gen_defaults1",
+    cmd: "cp $(in) $(out)",
+}
+
+genrule_defaults {
+    name: "gen_defaults2",
+    srcs: ["in1"],
+}
+
+genrule {
+    name: "gen",
+    out: ["out"],
+    defaults: ["gen_defaults1", "gen_defaults2"],
+}
+`,
+			expectedBazelTarget: `genrule(
+    name = "gen",
+    cmd = "cp $(SRCS) $(OUTS)",
+    outs = [
+        "out",
+    ],
+    srcs = [
+        "in1",
+    ],
+)`,
+			description: "genrule applies properties from list of genrule_defaults",
+		},
+		{
+			moduleTypesUnderTest: map[string]android.ModuleFactory{
+				"genrule":          genrule.GenRuleFactory,
+				"genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
+			},
+			bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
+				"genrule": genrule.GenruleBp2Build,
+			},
+			bp: `genrule_defaults {
+    name: "gen_defaults1",
+    defaults: ["gen_defaults2"],
+    cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
+}
+
+genrule_defaults {
+    name: "gen_defaults2",
+    defaults: ["gen_defaults3"],
+    cmd: "cmd2 $(in) $(out)",
+    out: ["out-from-2"],
+    srcs: ["in1"],
+}
+
+genrule_defaults {
+    name: "gen_defaults3",
+    out: ["out-from-3"],
+    srcs: ["srcs-from-3"],
+}
+
+genrule {
+    name: "gen",
+    out: ["out"],
+    defaults: ["gen_defaults1"],
+}
+`,
+			expectedBazelTarget: `genrule(
+    name = "gen",
+    cmd = "cmd1 $(SRCS) $(OUTS)",
+    outs = [
+        "out-from-3",
+        "out-from-2",
+        "out",
+    ],
+    srcs = [
+        "srcs-from-3",
+        "in1",
+    ],
+)`,
+			description: "genrule applies properties from genrule_defaults transitively",
+		},
+	}
+
+	dir := "."
+	for _, testCase := range testCases {
+		config := android.TestConfig(buildDir, nil, testCase.bp, nil)
+		ctx := android.NewTestContext(config)
+		for m, factory := range testCase.moduleTypesUnderTest {
+			ctx.RegisterModuleType(m, factory)
+		}
+		for mutator, f := range testCase.bp2buildMutatorsUnderTest {
+			ctx.RegisterBp2BuildMutator(mutator, f)
+		}
+		ctx.RegisterForBazelConversion()
+
+		_, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
+		android.FailIfErrored(t, errs)
+		_, errs = ctx.ResolveDependencies(config)
+		android.FailIfErrored(t, errs)
+
+		bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, Bp2Build)[dir]
+		if actualCount := len(bazelTargets); actualCount != 1 {
+			t.Fatalf("%s: Expected 1 bazel target, got %d", testCase.description, actualCount)
+		}
+
+		actualBazelTarget := bazelTargets[0]
+		if actualBazelTarget.content != testCase.expectedBazelTarget {
+			t.Errorf(
+				"%s: Expected generated Bazel target to be '%s', got '%s'",
+				testCase.description,
+				testCase.expectedBazelTarget,
+				actualBazelTarget.content,
+			)
+		}
+	}
+}
diff --git a/bp2build/bzl_conversion_test.go b/bp2build/bzl_conversion_test.go
index 8cdee65..f2a4058 100644
--- a/bp2build/bzl_conversion_test.go
+++ b/bp2build/bzl_conversion_test.go
@@ -172,7 +172,7 @@
 			content: "irrelevant",
 		},
 	}
-	files := CreateBazelFiles(ruleShims, make(map[string][]BazelTarget), QueryView)
+	files := CreateBazelFiles(ruleShims, make(map[string]BazelTargets), QueryView)
 
 	var actualSoongModuleBzl BazelFile
 	for _, f := range files {
diff --git a/bp2build/conversion.go b/bp2build/conversion.go
index 2025ef7..62cd8d4 100644
--- a/bp2build/conversion.go
+++ b/bp2build/conversion.go
@@ -17,7 +17,7 @@
 
 func CreateBazelFiles(
 	ruleShims map[string]RuleShim,
-	buildToTargets map[string][]BazelTarget,
+	buildToTargets map[string]BazelTargets,
 	mode CodegenMode) []BazelFile {
 	files := make([]BazelFile, 0, len(ruleShims)+len(buildToTargets)+numAdditionalFiles)
 
@@ -43,20 +43,20 @@
 	return files
 }
 
-func createBuildFiles(buildToTargets map[string][]BazelTarget, mode CodegenMode) []BazelFile {
+func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
 	files := make([]BazelFile, 0, len(buildToTargets))
 	for _, dir := range android.SortedStringKeys(buildToTargets) {
-		content := soongModuleLoad
-		if mode == Bp2Build {
-			// No need to load soong_module for bp2build BUILD files.
-			content = ""
-		}
 		targets := buildToTargets[dir]
 		sort.Slice(targets, func(i, j int) bool { return targets[i].name < targets[j].name })
-		for _, t := range targets {
-			content += "\n\n"
-			content += t.content
+		content := soongModuleLoad
+		if mode == Bp2Build {
+			content = targets.LoadStatements()
 		}
+		if content != "" {
+			// If there are load statements, add a couple of newlines.
+			content += "\n\n"
+		}
+		content += targets.String()
 		files = append(files, newFile(dir, "BUILD.bazel", content))
 	}
 	return files
diff --git a/bp2build/conversion_test.go b/bp2build/conversion_test.go
index 7f75b14..ec5f27e 100644
--- a/bp2build/conversion_test.go
+++ b/bp2build/conversion_test.go
@@ -55,7 +55,7 @@
 }
 
 func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) {
-	files := CreateBazelFiles(map[string]RuleShim{}, map[string][]BazelTarget{}, QueryView)
+	files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, QueryView)
 	expectedFilePaths := []filepath{
 		{
 			dir:      "",
@@ -85,7 +85,7 @@
 }
 
 func TestCreateBazelFiles_Bp2Build_AddsTopLevelFiles(t *testing.T) {
-	files := CreateBazelFiles(map[string]RuleShim{}, map[string][]BazelTarget{}, Bp2Build)
+	files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, Bp2Build)
 	expectedFilePaths := []filepath{
 		{
 			dir:      "",
diff --git a/bp2build/testing.go b/bp2build/testing.go
index 4c31d2d..5e6481b 100644
--- a/bp2build/testing.go
+++ b/bp2build/testing.go
@@ -137,3 +137,29 @@
 		})
 	}
 }
+
+// A bp2build mutator that uses load statements and creates a 1:M mapping from
+// module to target.
+func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) {
+	if m, ok := ctx.Module().(*customModule); ok {
+		baseName := "__bp2build__" + m.Name()
+		ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
+			Name: proptools.StringPtr(baseName),
+		}, &bazel.BazelTargetModuleProperties{
+			Rule_class:        "my_library",
+			Bzl_load_location: "//build/bazel/rules:rules.bzl",
+		})
+		ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
+			Name: proptools.StringPtr(baseName + "_proto_library_deps"),
+		}, &bazel.BazelTargetModuleProperties{
+			Rule_class:        "proto_library",
+			Bzl_load_location: "//build/bazel/rules:proto.bzl",
+		})
+		ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
+			Name: proptools.StringPtr(baseName + "_my_proto_library_deps"),
+		}, &bazel.BazelTargetModuleProperties{
+			Rule_class:        "my_proto_library",
+			Bzl_load_location: "//build/bazel/rules:proto.bzl",
+		})
+	}
+}
diff --git a/cc/androidmk.go b/cc/androidmk.go
index 040aa0b..ddb81d9 100644
--- a/cc/androidmk.go
+++ b/cc/androidmk.go
@@ -168,7 +168,7 @@
 	}
 }
 
-func androidMkWriteTestData(data []android.DataPath, ctx AndroidMkContext, entries *android.AndroidMkEntries) {
+func AndroidMkWriteTestData(data []android.DataPath, entries *android.AndroidMkEntries) {
 	testFiles := android.AndroidMkDataPaths(data)
 	if len(testFiles) > 0 {
 		entries.ExtraEntries = append(entries.ExtraEntries, func(entries *android.AndroidMkEntries) {
@@ -353,7 +353,7 @@
 	for _, srcPath := range benchmark.data {
 		dataPaths = append(dataPaths, android.DataPath{SrcPath: srcPath})
 	}
-	androidMkWriteTestData(dataPaths, ctx, entries)
+	AndroidMkWriteTestData(dataPaths, entries)
 }
 
 func (test *testBinary) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
@@ -378,7 +378,7 @@
 		}
 	})
 
-	androidMkWriteTestData(test.data, ctx, entries)
+	AndroidMkWriteTestData(test.data, entries)
 	androidMkWriteExtraTestConfigs(test.extraTestConfigs, entries)
 }
 
@@ -519,7 +519,7 @@
 		entries.SubName += ".cfi"
 	}
 
-	entries.SubName += c.androidMkSuffix
+	entries.SubName += c.baseProperties.Androidmk_suffix
 
 	entries.ExtraEntries = append(entries.ExtraEntries, func(entries *android.AndroidMkEntries) {
 		c.libraryDecorator.androidMkWriteExportedFlags(entries)
@@ -546,7 +546,7 @@
 
 func (c *snapshotBinaryDecorator) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
 	entries.Class = "EXECUTABLES"
-	entries.SubName = c.androidMkSuffix
+	entries.SubName = c.baseProperties.Androidmk_suffix
 
 	entries.ExtraEntries = append(entries.ExtraEntries, func(entries *android.AndroidMkEntries) {
 		entries.AddStrings("LOCAL_MODULE_SYMLINKS", c.Properties.Symlinks...)
@@ -555,7 +555,7 @@
 
 func (c *snapshotObjectLinker) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
 	entries.Class = "STATIC_LIBRARIES"
-	entries.SubName = c.androidMkSuffix
+	entries.SubName = c.baseProperties.Androidmk_suffix
 
 	entries.ExtraFooters = append(entries.ExtraFooters,
 		func(w io.Writer, name, prefix, moduleDir string) {
diff --git a/cc/cc.go b/cc/cc.go
index afa6bf9..d282b6e 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -50,10 +50,6 @@
 		ctx.BottomUp("version", versionMutator).Parallel()
 		ctx.BottomUp("begin", BeginMutator).Parallel()
 		ctx.BottomUp("sysprop_cc", SyspropMutator).Parallel()
-		ctx.BottomUp("vendor_snapshot", VendorSnapshotMutator).Parallel()
-		ctx.BottomUp("vendor_snapshot_source", VendorSnapshotSourceMutator).Parallel()
-		ctx.BottomUp("recovery_snapshot", RecoverySnapshotMutator).Parallel()
-		ctx.BottomUp("recovery_snapshot_source", RecoverySnapshotSourceMutator).Parallel()
 	})
 
 	ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
@@ -1237,7 +1233,7 @@
 }
 
 func (c *Module) isSnapshotPrebuilt() bool {
-	if p, ok := c.linker.(interface{ isSnapshotPrebuilt() bool }); ok {
+	if p, ok := c.linker.(snapshotInterface); ok {
 		return p.isSnapshotPrebuilt()
 	}
 	return false
@@ -1937,6 +1933,40 @@
 
 	c.Properties.AndroidMkSystemSharedLibs = deps.SystemSharedLibs
 
+	var snapshotInfo *SnapshotInfo
+	getSnapshot := func() SnapshotInfo {
+		// Only modules with BOARD_VNDK_VERSION uses snapshot.  Others use the zero value of
+		// SnapshotInfo, which provides no mappings.
+		if snapshotInfo == nil {
+			// Only retrieve the snapshot on demand in order to avoid circular dependencies
+			// between the modules in the snapshot and the snapshot itself.
+			var snapshotModule []blueprint.Module
+			if c.InVendor() && c.VndkVersion() == actx.DeviceConfig().VndkVersion() {
+				snapshotModule = ctx.AddVariationDependencies(nil, nil, "vendor_snapshot")
+			} else if recoverySnapshotVersion := actx.DeviceConfig().RecoverySnapshotVersion(); recoverySnapshotVersion != "current" && recoverySnapshotVersion != "" && c.InRecovery() {
+				snapshotModule = ctx.AddVariationDependencies(nil, nil, "recovery_snapshot")
+			}
+			if len(snapshotModule) > 0 {
+				snapshot := ctx.OtherModuleProvider(snapshotModule[0], SnapshotInfoProvider).(SnapshotInfo)
+				snapshotInfo = &snapshot
+				// republish the snapshot for use in later mutators on this module
+				ctx.SetProvider(SnapshotInfoProvider, snapshot)
+			} else {
+				snapshotInfo = &SnapshotInfo{}
+			}
+		}
+
+		return *snapshotInfo
+	}
+
+	rewriteSnapshotLib := func(lib string, snapshotMap map[string]string) string {
+		if snapshot, ok := snapshotMap[lib]; ok {
+			return snapshot
+		}
+
+		return lib
+	}
+
 	variantNdkLibs := []string{}
 	variantLateNdkLibs := []string{}
 	if ctx.Os() == android.Android {
@@ -1956,21 +1986,6 @@
 		// nonvariantLibs
 
 		vendorPublicLibraries := vendorPublicLibraries(actx.Config())
-		vendorSnapshotSharedLibs := vendorSnapshotSharedLibs(actx.Config())
-		recoverySnapshotSharedLibs := recoverySnapshotSharedLibs(actx.Config())
-
-		rewriteVendorLibs := func(lib string) string {
-			// only modules with BOARD_VNDK_VERSION uses snapshot.
-			if c.VndkVersion() != actx.DeviceConfig().VndkVersion() {
-				return lib
-			}
-
-			if snapshot, ok := vendorSnapshotSharedLibs.get(lib, actx.Arch().ArchType); ok {
-				return snapshot
-			}
-
-			return lib
-		}
 
 		rewriteLibs := func(list []string) (nonvariantLibs []string, variantLibs []string) {
 			variantLibs = []string{}
@@ -1979,21 +1994,11 @@
 				// strip #version suffix out
 				name, _ := StubsLibNameAndVersion(entry)
 				if c.InRecovery() {
-					recoverySnapshotVersion :=
-						actx.DeviceConfig().RecoverySnapshotVersion()
-					if recoverySnapshotVersion == "current" ||
-						recoverySnapshotVersion == "" {
-						nonvariantLibs = append(nonvariantLibs, name)
-					} else if snapshot, ok := recoverySnapshotSharedLibs.get(
-						name, actx.Arch().ArchType); ok {
-						nonvariantLibs = append(nonvariantLibs, snapshot)
-					} else {
-						nonvariantLibs = append(nonvariantLibs, name)
-					}
+					nonvariantLibs = append(nonvariantLibs, rewriteSnapshotLib(entry, getSnapshot().SharedLibs))
 				} else if ctx.useSdk() && inList(name, *getNDKKnownLibs(ctx.Config())) {
 					variantLibs = append(variantLibs, name+ndkLibrarySuffix)
 				} else if ctx.useVndk() {
-					nonvariantLibs = append(nonvariantLibs, rewriteVendorLibs(entry))
+					nonvariantLibs = append(nonvariantLibs, rewriteSnapshotLib(entry, getSnapshot().SharedLibs))
 				} else if (ctx.Platform() || ctx.ProductSpecific()) && inList(name, *vendorPublicLibraries) {
 					vendorPublicLib := name + vendorPublicLibrarySuffix
 					if actx.OtherModuleExists(vendorPublicLib) {
@@ -2015,56 +2020,19 @@
 		deps.SharedLibs, variantNdkLibs = rewriteLibs(deps.SharedLibs)
 		deps.LateSharedLibs, variantLateNdkLibs = rewriteLibs(deps.LateSharedLibs)
 		deps.ReexportSharedLibHeaders, _ = rewriteLibs(deps.ReexportSharedLibHeaders)
-		if ctx.useVndk() {
-			for idx, lib := range deps.RuntimeLibs {
-				deps.RuntimeLibs[idx] = rewriteVendorLibs(lib)
-			}
+
+		for idx, lib := range deps.RuntimeLibs {
+			deps.RuntimeLibs[idx] = rewriteSnapshotLib(lib, getSnapshot().SharedLibs)
 		}
 	}
 
-	rewriteSnapshotLibs := func(lib string, snapshotMap *snapshotMap) string {
-		// only modules with BOARD_VNDK_VERSION uses snapshot.
-		if c.VndkVersion() != actx.DeviceConfig().VndkVersion() {
-			return lib
-		}
-
-		if snapshot, ok := snapshotMap.get(lib, actx.Arch().ArchType); ok {
-			return snapshot
-		}
-
-		return lib
-	}
-
-	snapshotHeaderLibs := vendorSnapshotHeaderLibs(actx.Config())
-	snapshotStaticLibs := vendorSnapshotStaticLibs(actx.Config())
-	snapshotObjects := vendorSnapshotObjects(actx.Config())
-
-	if c.InRecovery() {
-		rewriteSnapshotLibs = func(lib string, snapshotMap *snapshotMap) string {
-			recoverySnapshotVersion :=
-				actx.DeviceConfig().RecoverySnapshotVersion()
-			if recoverySnapshotVersion == "current" ||
-				recoverySnapshotVersion == "" {
-				return lib
-			} else if snapshot, ok := snapshotMap.get(lib, actx.Arch().ArchType); ok {
-				return snapshot
-			}
-
-			return lib
-		}
-
-		snapshotHeaderLibs = recoverySnapshotHeaderLibs(actx.Config())
-		snapshotStaticLibs = recoverySnapshotStaticLibs(actx.Config())
-		snapshotObjects = recoverySnapshotObjects(actx.Config())
-	}
-
 	for _, lib := range deps.HeaderLibs {
 		depTag := libraryDependencyTag{Kind: headerLibraryDependency}
 		if inList(lib, deps.ReexportHeaderLibHeaders) {
 			depTag.reexportFlags = true
 		}
 
-		lib = rewriteSnapshotLibs(lib, snapshotHeaderLibs)
+		lib = rewriteSnapshotLib(lib, getSnapshot().HeaderLibs)
 
 		if c.IsStubs() {
 			actx.AddFarVariationDependencies(append(ctx.Target().Variations(), c.ImageVariation()),
@@ -2087,7 +2055,7 @@
 			lib = impl
 		}
 
-		lib = rewriteSnapshotLibs(lib, snapshotStaticLibs)
+		lib = rewriteSnapshotLib(lib, getSnapshot().StaticLibs)
 
 		actx.AddVariationDependencies([]blueprint.Variation{
 			{Mutator: "link", Variation: "static"},
@@ -2107,7 +2075,7 @@
 			lib = impl
 		}
 
-		lib = rewriteSnapshotLibs(lib, snapshotStaticLibs)
+		lib = rewriteSnapshotLib(lib, getSnapshot().StaticLibs)
 
 		actx.AddVariationDependencies([]blueprint.Variation{
 			{Mutator: "link", Variation: "static"},
@@ -2121,14 +2089,14 @@
 		depTag := libraryDependencyTag{Kind: staticLibraryDependency, staticUnwinder: true}
 		actx.AddVariationDependencies([]blueprint.Variation{
 			{Mutator: "link", Variation: "static"},
-		}, depTag, rewriteSnapshotLibs(staticUnwinder(actx), snapshotStaticLibs))
+		}, depTag, rewriteSnapshotLib(staticUnwinder(actx), getSnapshot().StaticLibs))
 	}
 
 	for _, lib := range deps.LateStaticLibs {
 		depTag := libraryDependencyTag{Kind: staticLibraryDependency, Order: lateLibraryDependency}
 		actx.AddVariationDependencies([]blueprint.Variation{
 			{Mutator: "link", Variation: "static"},
-		}, depTag, rewriteSnapshotLibs(lib, snapshotStaticLibs))
+		}, depTag, rewriteSnapshotLib(lib, getSnapshot().StaticLibs))
 	}
 
 	// shared lib names without the #version suffix
@@ -2192,11 +2160,11 @@
 	actx.AddVariationDependencies(crtVariations, objDepTag, deps.ObjFiles...)
 	if deps.CrtBegin != "" {
 		actx.AddVariationDependencies(crtVariations, CrtBeginDepTag,
-			rewriteSnapshotLibs(deps.CrtBegin, snapshotObjects))
+			rewriteSnapshotLib(deps.CrtBegin, getSnapshot().Objects))
 	}
 	if deps.CrtEnd != "" {
 		actx.AddVariationDependencies(crtVariations, CrtEndDepTag,
-			rewriteSnapshotLibs(deps.CrtEnd, snapshotObjects))
+			rewriteSnapshotLib(deps.CrtEnd, getSnapshot().Objects))
 	}
 	if deps.LinkerFlagsFile != "" {
 		actx.AddDependency(c, linkerFlagsDepTag, deps.LinkerFlagsFile)
@@ -2917,8 +2885,6 @@
 }
 
 func (c *Module) makeLibName(ctx android.ModuleContext, ccDep LinkableInterface, depName string) string {
-	vendorSuffixModules := vendorSuffixModules(ctx.Config())
-	recoverySuffixModules := recoverySuffixModules(ctx.Config())
 	vendorPublicLibraries := vendorPublicLibraries(ctx.Config())
 
 	libName := baseLibName(depName)
@@ -2929,20 +2895,10 @@
 
 	if c, ok := ccDep.(*Module); ok {
 		// Use base module name for snapshots when exporting to Makefile.
-		if c.isSnapshotPrebuilt() {
+		if snapshotPrebuilt, ok := c.linker.(snapshotInterface); ok {
 			baseName := c.BaseModuleName()
 
-			if c.IsVndk() {
-				return baseName + ".vendor"
-			}
-
-			if c.InVendor() && vendorSuffixModules[baseName] {
-				return baseName + ".vendor"
-			} else if c.InRecovery() && recoverySuffixModules[baseName] {
-				return baseName + ".recovery"
-			} else {
-				return baseName
-			}
+			return baseName + snapshotPrebuilt.snapshotAndroidMkSuffix()
 		}
 	}
 
diff --git a/cc/image.go b/cc/image.go
index 231da7e..afe6a0e 100644
--- a/cc/image.go
+++ b/cc/image.go
@@ -318,9 +318,7 @@
 	} else if m.isSnapshotPrebuilt() {
 		// Make vendor variants only for the versions in BOARD_VNDK_VERSION and
 		// PRODUCT_EXTRA_VNDK_VERSIONS.
-		if snapshot, ok := m.linker.(interface {
-			version() string
-		}); ok {
+		if snapshot, ok := m.linker.(snapshotInterface); ok {
 			if m.InstallInRecovery() {
 				recoveryVariantNeeded = true
 			} else {
diff --git a/cc/sanitize.go b/cc/sanitize.go
index 8eeb355..8218d97 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -1149,13 +1149,11 @@
 			// added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
 			if c.staticBinary() {
 				deps := append(extraStaticDeps, runtimeLibrary)
-				// If we're using snapshots and in vendor, redirect to snapshot whenever possible
-				if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() {
-					snapshots := vendorSnapshotStaticLibs(mctx.Config())
-					for idx, dep := range deps {
-						if lib, ok := snapshots.get(dep, mctx.Arch().ArchType); ok {
-							deps[idx] = lib
-						}
+				// If we're using snapshots, redirect to snapshot whenever possible
+				snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
+				for idx, dep := range deps {
+					if lib, ok := snapshot.StaticLibs[dep]; ok {
+						deps[idx] = lib
 					}
 				}
 
@@ -1168,13 +1166,12 @@
 				}
 				mctx.AddFarVariationDependencies(variations, depTag, deps...)
 			} else if !c.static() && !c.Header() {
-				// If we're using snapshots and in vendor, redirect to snapshot whenever possible
-				if c.VndkVersion() == mctx.DeviceConfig().VndkVersion() {
-					snapshots := vendorSnapshotSharedLibs(mctx.Config())
-					if lib, ok := snapshots.get(runtimeLibrary, mctx.Arch().ArchType); ok {
-						runtimeLibrary = lib
-					}
+				// If we're using snapshots, redirect to snapshot whenever possible
+				snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
+				if lib, ok := snapshot.SharedLibs[runtimeLibrary]; ok {
+					runtimeLibrary = lib
 				}
+
 				// Skip apex dependency check for sharedLibraryDependency
 				// when sanitizer diags are enabled. Skipping the check will allow
 				// building with diag libraries without having to list the
diff --git a/cc/sdk.go b/cc/sdk.go
index 2c3fec3..aec950b 100644
--- a/cc/sdk.go
+++ b/cc/sdk.go
@@ -75,5 +75,7 @@
 			}
 			ctx.AliasVariation("")
 		}
+	case *snapshot:
+		ctx.CreateVariations("")
 	}
 }
diff --git a/cc/snapshot_prebuilt.go b/cc/snapshot_prebuilt.go
index 2003e03..ffaed8e 100644
--- a/cc/snapshot_prebuilt.go
+++ b/cc/snapshot_prebuilt.go
@@ -19,19 +19,15 @@
 
 import (
 	"strings"
-	"sync"
 
 	"android/soong/android"
 
-	"github.com/google/blueprint/proptools"
+	"github.com/google/blueprint"
 )
 
 // Defines the specifics of different images to which the snapshot process is applicable, e.g.,
 // vendor, recovery, ramdisk.
 type snapshotImage interface {
-	// Used to register callbacks with the build system.
-	init()
-
 	// Returns true if a snapshot should be generated for this image.
 	shouldGenerateSnapshot(ctx android.SingletonContext) bool
 
@@ -61,46 +57,41 @@
 	// exclude_from_recovery_snapshot properties.
 	excludeFromSnapshot(m *Module) bool
 
-	// Returns the snapshotMap to be used for a given module and config, or nil if the
-	// module is not included in this image.
-	getSnapshotMap(m *Module, cfg android.Config) *snapshotMap
-
-	// Returns mutex used for mutual exclusion when updating the snapshot maps.
-	getMutex() *sync.Mutex
-
-	// For a given arch, a maps of which modules are included in this image.
-	suffixModules(config android.Config) map[string]bool
-
-	// Whether to add a given module to the suffix map.
-	shouldBeAddedToSuffixModules(m *Module) bool
-
 	// Returns true if the build is using a snapshot for this image.
 	isUsingSnapshot(cfg android.DeviceConfig) bool
 
-	// Whether to skip the module mutator for a module in a given context.
-	skipModuleMutator(ctx android.BottomUpMutatorContext) bool
-
-	// Whether to skip the source mutator for a given module.
-	skipSourceMutator(ctx android.BottomUpMutatorContext) bool
+	// Returns a version of which the snapshot should be used in this target.
+	// This will only be meaningful when isUsingSnapshot is true.
+	targetSnapshotVersion(cfg android.DeviceConfig) string
 
 	// Whether to exclude a given module from the directed snapshot or not.
 	// If the makefile variable DIRECTED_{IMAGE}_SNAPSHOT is true, directed snapshot is turned on,
 	// and only modules listed in {IMAGE}_SNAPSHOT_MODULES will be captured.
 	excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool
+
+	// The image variant name for this snapshot image.
+	// For example, recovery snapshot image will return "recovery", and vendor snapshot image will
+	// return "vendor." + version.
+	imageVariantName(cfg android.DeviceConfig) string
+
+	// The variant suffix for snapshot modules. For example, vendor snapshot modules will have
+	// ".vendor" as their suffix.
+	moduleNameSuffix() string
 }
 
 type vendorSnapshotImage struct{}
 type recoverySnapshotImage struct{}
 
-func (vendorSnapshotImage) init() {
-	android.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton)
-	android.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory)
-	android.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory)
-	android.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory)
-	android.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory)
-	android.RegisterModuleType("vendor_snapshot_object", VendorSnapshotObjectFactory)
+func (vendorSnapshotImage) init(ctx android.RegistrationContext) {
+	ctx.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton)
+	ctx.RegisterModuleType("vendor_snapshot", vendorSnapshotFactory)
+	ctx.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory)
+	ctx.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory)
+	ctx.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory)
+	ctx.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory)
+	ctx.RegisterModuleType("vendor_snapshot_object", VendorSnapshotObjectFactory)
 
-	android.RegisterSingletonType("vendor-fake-snapshot", VendorFakeSnapshotSingleton)
+	ctx.RegisterSingletonType("vendor-fake-snapshot", VendorFakeSnapshotSingleton)
 }
 
 func (vendorSnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
@@ -129,73 +120,13 @@
 	return m.ExcludeFromVendorSnapshot()
 }
 
-func (vendorSnapshotImage) getSnapshotMap(m *Module, cfg android.Config) *snapshotMap {
-	if lib, ok := m.linker.(libraryInterface); ok {
-		if lib.static() {
-			return vendorSnapshotStaticLibs(cfg)
-		} else if lib.shared() {
-			return vendorSnapshotSharedLibs(cfg)
-		} else {
-			// header
-			return vendorSnapshotHeaderLibs(cfg)
-		}
-	} else if m.binary() {
-		return vendorSnapshotBinaries(cfg)
-	} else if m.object() {
-		return vendorSnapshotObjects(cfg)
-	} else {
-		return nil
-	}
-}
-
-func (vendorSnapshotImage) getMutex() *sync.Mutex {
-	return &vendorSnapshotsLock
-}
-
-func (vendorSnapshotImage) suffixModules(config android.Config) map[string]bool {
-	return vendorSuffixModules(config)
-}
-
-func (vendorSnapshotImage) shouldBeAddedToSuffixModules(module *Module) bool {
-	// vendor suffix should be added to snapshots if the source module isn't vendor: true.
-	if module.SocSpecific() {
-		return false
-	}
-
-	// But we can't just check SocSpecific() since we already passed the image mutator.
-	// Check ramdisk and recovery to see if we are real "vendor: true" module.
-	ramdiskAvailable := module.InRamdisk() && !module.OnlyInRamdisk()
-	vendorRamdiskAvailable := module.InVendorRamdisk() && !module.OnlyInVendorRamdisk()
-	recoveryAvailable := module.InRecovery() && !module.OnlyInRecovery()
-
-	return !ramdiskAvailable && !recoveryAvailable && !vendorRamdiskAvailable
-}
-
 func (vendorSnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
 	vndkVersion := cfg.VndkVersion()
 	return vndkVersion != "current" && vndkVersion != ""
 }
 
-func (vendorSnapshotImage) skipModuleMutator(ctx android.BottomUpMutatorContext) bool {
-	vndkVersion := ctx.DeviceConfig().VndkVersion()
-	module, ok := ctx.Module().(*Module)
-	return !ok || module.VndkVersion() != vndkVersion
-}
-
-func (vendorSnapshotImage) skipSourceMutator(ctx android.BottomUpMutatorContext) bool {
-	vndkVersion := ctx.DeviceConfig().VndkVersion()
-	module, ok := ctx.Module().(*Module)
-	if !ok {
-		return true
-	}
-	if module.VndkVersion() != vndkVersion {
-		return true
-	}
-	// .. and also filter out llndk library
-	if module.IsLlndk() {
-		return true
-	}
-	return false
+func (vendorSnapshotImage) targetSnapshotVersion(cfg android.DeviceConfig) string {
+	return cfg.VndkVersion()
 }
 
 // returns true iff a given module SHOULD BE EXCLUDED, false if included
@@ -208,13 +139,22 @@
 	return !cfg.VendorSnapshotModules()[name]
 }
 
-func (recoverySnapshotImage) init() {
-	android.RegisterSingletonType("recovery-snapshot", RecoverySnapshotSingleton)
-	android.RegisterModuleType("recovery_snapshot_shared", RecoverySnapshotSharedFactory)
-	android.RegisterModuleType("recovery_snapshot_static", RecoverySnapshotStaticFactory)
-	android.RegisterModuleType("recovery_snapshot_header", RecoverySnapshotHeaderFactory)
-	android.RegisterModuleType("recovery_snapshot_binary", RecoverySnapshotBinaryFactory)
-	android.RegisterModuleType("recovery_snapshot_object", RecoverySnapshotObjectFactory)
+func (vendorSnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
+	return VendorVariationPrefix + cfg.VndkVersion()
+}
+
+func (vendorSnapshotImage) moduleNameSuffix() string {
+	return vendorSuffix
+}
+
+func (recoverySnapshotImage) init(ctx android.RegistrationContext) {
+	ctx.RegisterSingletonType("recovery-snapshot", RecoverySnapshotSingleton)
+	ctx.RegisterModuleType("recovery_snapshot", recoverySnapshotFactory)
+	ctx.RegisterModuleType("recovery_snapshot_shared", RecoverySnapshotSharedFactory)
+	ctx.RegisterModuleType("recovery_snapshot_static", RecoverySnapshotStaticFactory)
+	ctx.RegisterModuleType("recovery_snapshot_header", RecoverySnapshotHeaderFactory)
+	ctx.RegisterModuleType("recovery_snapshot_binary", RecoverySnapshotBinaryFactory)
+	ctx.RegisterModuleType("recovery_snapshot_object", RecoverySnapshotObjectFactory)
 }
 
 func (recoverySnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
@@ -245,50 +185,13 @@
 	return m.ExcludeFromRecoverySnapshot()
 }
 
-func (recoverySnapshotImage) getSnapshotMap(m *Module, cfg android.Config) *snapshotMap {
-	if lib, ok := m.linker.(libraryInterface); ok {
-		if lib.static() {
-			return recoverySnapshotStaticLibs(cfg)
-		} else if lib.shared() {
-			return recoverySnapshotSharedLibs(cfg)
-		} else {
-			// header
-			return recoverySnapshotHeaderLibs(cfg)
-		}
-	} else if m.binary() {
-		return recoverySnapshotBinaries(cfg)
-	} else if m.object() {
-		return recoverySnapshotObjects(cfg)
-	} else {
-		return nil
-	}
-}
-
-func (recoverySnapshotImage) getMutex() *sync.Mutex {
-	return &recoverySnapshotsLock
-}
-
-func (recoverySnapshotImage) suffixModules(config android.Config) map[string]bool {
-	return recoverySuffixModules(config)
-}
-
-func (recoverySnapshotImage) shouldBeAddedToSuffixModules(module *Module) bool {
-	return proptools.BoolDefault(module.Properties.Recovery_available, false)
-}
-
 func (recoverySnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
 	recoverySnapshotVersion := cfg.RecoverySnapshotVersion()
 	return recoverySnapshotVersion != "current" && recoverySnapshotVersion != ""
 }
 
-func (recoverySnapshotImage) skipModuleMutator(ctx android.BottomUpMutatorContext) bool {
-	module, ok := ctx.Module().(*Module)
-	return !ok || !module.InRecovery()
-}
-
-func (recoverySnapshotImage) skipSourceMutator(ctx android.BottomUpMutatorContext) bool {
-	module, ok := ctx.Module().(*Module)
-	return !ok || !module.InRecovery()
+func (recoverySnapshotImage) targetSnapshotVersion(cfg android.DeviceConfig) string {
+	return cfg.RecoverySnapshotVersion()
 }
 
 func (recoverySnapshotImage) excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool {
@@ -296,130 +199,160 @@
 	return false
 }
 
+func (recoverySnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
+	return android.RecoveryVariation
+}
+
+func (recoverySnapshotImage) moduleNameSuffix() string {
+	return recoverySuffix
+}
+
 var vendorSnapshotImageSingleton vendorSnapshotImage
 var recoverySnapshotImageSingleton recoverySnapshotImage
 
 func init() {
-	vendorSnapshotImageSingleton.init()
-	recoverySnapshotImageSingleton.init()
+	vendorSnapshotImageSingleton.init(android.InitRegistrationContext)
+	recoverySnapshotImageSingleton.init(android.InitRegistrationContext)
 }
 
 const (
-	vendorSnapshotHeaderSuffix = ".vendor_header."
-	vendorSnapshotSharedSuffix = ".vendor_shared."
-	vendorSnapshotStaticSuffix = ".vendor_static."
-	vendorSnapshotBinarySuffix = ".vendor_binary."
-	vendorSnapshotObjectSuffix = ".vendor_object."
+	snapshotHeaderSuffix = "_header."
+	snapshotSharedSuffix = "_shared."
+	snapshotStaticSuffix = "_static."
+	snapshotBinarySuffix = "_binary."
+	snapshotObjectSuffix = "_object."
 )
 
-const (
-	recoverySnapshotHeaderSuffix = ".recovery_header."
-	recoverySnapshotSharedSuffix = ".recovery_shared."
-	recoverySnapshotStaticSuffix = ".recovery_static."
-	recoverySnapshotBinarySuffix = ".recovery_binary."
-	recoverySnapshotObjectSuffix = ".recovery_object."
-)
-
-var (
-	vendorSnapshotsLock         sync.Mutex
-	vendorSuffixModulesKey      = android.NewOnceKey("vendorSuffixModules")
-	vendorSnapshotHeaderLibsKey = android.NewOnceKey("vendorSnapshotHeaderLibs")
-	vendorSnapshotStaticLibsKey = android.NewOnceKey("vendorSnapshotStaticLibs")
-	vendorSnapshotSharedLibsKey = android.NewOnceKey("vendorSnapshotSharedLibs")
-	vendorSnapshotBinariesKey   = android.NewOnceKey("vendorSnapshotBinaries")
-	vendorSnapshotObjectsKey    = android.NewOnceKey("vendorSnapshotObjects")
-)
-
-var (
-	recoverySnapshotsLock         sync.Mutex
-	recoverySuffixModulesKey      = android.NewOnceKey("recoverySuffixModules")
-	recoverySnapshotHeaderLibsKey = android.NewOnceKey("recoverySnapshotHeaderLibs")
-	recoverySnapshotStaticLibsKey = android.NewOnceKey("recoverySnapshotStaticLibs")
-	recoverySnapshotSharedLibsKey = android.NewOnceKey("recoverySnapshotSharedLibs")
-	recoverySnapshotBinariesKey   = android.NewOnceKey("recoverySnapshotBinaries")
-	recoverySnapshotObjectsKey    = android.NewOnceKey("recoverySnapshotObjects")
-)
-
-// vendorSuffixModules holds names of modules whose vendor variants should have the vendor suffix.
-// This is determined by source modules, and then this will be used when exporting snapshot modules
-// to Makefile.
-//
-// For example, if libbase has "vendor_available: true", the name of core variant will be "libbase"
-// while the name of vendor variant will be "libbase.vendor". In such cases, the vendor snapshot of
-// "libbase" should be exported with the name "libbase.vendor".
-//
-// Refer to VendorSnapshotSourceMutator and makeLibName which use this.
-func vendorSuffixModules(config android.Config) map[string]bool {
-	return config.Once(vendorSuffixModulesKey, func() interface{} {
-		return make(map[string]bool)
-	}).(map[string]bool)
+type SnapshotProperties struct {
+	Header_libs []string `android:"arch_variant"`
+	Static_libs []string `android:"arch_variant"`
+	Shared_libs []string `android:"arch_variant"`
+	Vndk_libs   []string `android:"arch_variant"`
+	Binaries    []string `android:"arch_variant"`
+	Objects     []string `android:"arch_variant"`
 }
 
-// these are vendor snapshot maps holding names of vendor snapshot modules
-func vendorSnapshotHeaderLibs(config android.Config) *snapshotMap {
-	return config.Once(vendorSnapshotHeaderLibsKey, func() interface{} {
-		return newSnapshotMap()
-	}).(*snapshotMap)
+type snapshot struct {
+	android.ModuleBase
+
+	properties SnapshotProperties
+
+	baseSnapshot baseSnapshotDecorator
+
+	image snapshotImage
 }
 
-func vendorSnapshotSharedLibs(config android.Config) *snapshotMap {
-	return config.Once(vendorSnapshotSharedLibsKey, func() interface{} {
-		return newSnapshotMap()
-	}).(*snapshotMap)
+func (s *snapshot) ImageMutatorBegin(ctx android.BaseModuleContext) {
+	cfg := ctx.DeviceConfig()
+	if !s.image.isUsingSnapshot(cfg) || s.image.targetSnapshotVersion(cfg) != s.baseSnapshot.version() {
+		s.Disable()
+	}
 }
 
-func vendorSnapshotStaticLibs(config android.Config) *snapshotMap {
-	return config.Once(vendorSnapshotStaticLibsKey, func() interface{} {
-		return newSnapshotMap()
-	}).(*snapshotMap)
+func (s *snapshot) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
+	return false
 }
 
-func vendorSnapshotBinaries(config android.Config) *snapshotMap {
-	return config.Once(vendorSnapshotBinariesKey, func() interface{} {
-		return newSnapshotMap()
-	}).(*snapshotMap)
+func (s *snapshot) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
+	return false
 }
 
-func vendorSnapshotObjects(config android.Config) *snapshotMap {
-	return config.Once(vendorSnapshotObjectsKey, func() interface{} {
-		return newSnapshotMap()
-	}).(*snapshotMap)
+func (s *snapshot) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
+	return false
 }
 
-func recoverySuffixModules(config android.Config) map[string]bool {
-	return config.Once(recoverySuffixModulesKey, func() interface{} {
-		return make(map[string]bool)
-	}).(map[string]bool)
+func (s *snapshot) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
+	return false
 }
 
-func recoverySnapshotHeaderLibs(config android.Config) *snapshotMap {
-	return config.Once(recoverySnapshotHeaderLibsKey, func() interface{} {
-		return newSnapshotMap()
-	}).(*snapshotMap)
+func (s *snapshot) ExtraImageVariations(ctx android.BaseModuleContext) []string {
+	return []string{s.image.imageVariantName(ctx.DeviceConfig())}
 }
 
-func recoverySnapshotSharedLibs(config android.Config) *snapshotMap {
-	return config.Once(recoverySnapshotSharedLibsKey, func() interface{} {
-		return newSnapshotMap()
-	}).(*snapshotMap)
+func (s *snapshot) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
 }
 
-func recoverySnapshotStaticLibs(config android.Config) *snapshotMap {
-	return config.Once(recoverySnapshotStaticLibsKey, func() interface{} {
-		return newSnapshotMap()
-	}).(*snapshotMap)
+func (s *snapshot) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+	// Nothing, the snapshot module is only used to forward dependency information in DepsMutator.
 }
 
-func recoverySnapshotBinaries(config android.Config) *snapshotMap {
-	return config.Once(recoverySnapshotBinariesKey, func() interface{} {
-		return newSnapshotMap()
-	}).(*snapshotMap)
+func (s *snapshot) DepsMutator(ctx android.BottomUpMutatorContext) {
+	collectSnapshotMap := func(variations []blueprint.Variation, depTag blueprint.DependencyTag,
+		names []string, snapshotSuffix, moduleSuffix string) map[string]string {
+
+		decoratedNames := make([]string, 0, len(names))
+		for _, name := range names {
+			decoratedNames = append(decoratedNames, name+
+				snapshotSuffix+moduleSuffix+
+				s.baseSnapshot.version()+
+				"."+ctx.Arch().ArchType.Name)
+		}
+
+		deps := ctx.AddVariationDependencies(variations, depTag, decoratedNames...)
+		snapshotMap := make(map[string]string)
+		for _, dep := range deps {
+			if dep == nil {
+				continue
+			}
+
+			snapshotMap[dep.(*Module).BaseModuleName()] = ctx.OtherModuleName(dep)
+		}
+		return snapshotMap
+	}
+
+	snapshotSuffix := s.image.moduleNameSuffix()
+	headers := collectSnapshotMap(nil, HeaderDepTag(), s.properties.Header_libs, snapshotSuffix, snapshotHeaderSuffix)
+	binaries := collectSnapshotMap(nil, nil, s.properties.Binaries, snapshotSuffix, snapshotBinarySuffix)
+	objects := collectSnapshotMap(nil, nil, s.properties.Objects, snapshotSuffix, snapshotObjectSuffix)
+
+	staticLibs := collectSnapshotMap([]blueprint.Variation{
+		{Mutator: "link", Variation: "static"},
+	}, StaticDepTag(), s.properties.Static_libs, snapshotSuffix, snapshotStaticSuffix)
+
+	sharedLibs := collectSnapshotMap([]blueprint.Variation{
+		{Mutator: "link", Variation: "shared"},
+	}, SharedDepTag(), s.properties.Shared_libs, snapshotSuffix, snapshotSharedSuffix)
+
+	vndkLibs := collectSnapshotMap([]blueprint.Variation{
+		{Mutator: "link", Variation: "shared"},
+	}, SharedDepTag(), s.properties.Vndk_libs, "", vndkSuffix)
+
+	for k, v := range vndkLibs {
+		sharedLibs[k] = v
+	}
+	ctx.SetProvider(SnapshotInfoProvider, SnapshotInfo{
+		HeaderLibs: headers,
+		Binaries:   binaries,
+		Objects:    objects,
+		StaticLibs: staticLibs,
+		SharedLibs: sharedLibs,
+	})
 }
 
-func recoverySnapshotObjects(config android.Config) *snapshotMap {
-	return config.Once(recoverySnapshotObjectsKey, func() interface{} {
-		return newSnapshotMap()
-	}).(*snapshotMap)
+type SnapshotInfo struct {
+	HeaderLibs, Binaries, Objects, StaticLibs, SharedLibs map[string]string
+}
+
+var SnapshotInfoProvider = blueprint.NewMutatorProvider(SnapshotInfo{}, "deps")
+
+var _ android.ImageInterface = (*snapshot)(nil)
+
+func vendorSnapshotFactory() android.Module {
+	return snapshotFactory(vendorSnapshotImageSingleton)
+}
+
+func recoverySnapshotFactory() android.Module {
+	return snapshotFactory(recoverySnapshotImageSingleton)
+}
+
+func snapshotFactory(image snapshotImage) android.Module {
+	snapshot := &snapshot{}
+	snapshot.image = image
+	snapshot.AddProperties(
+		&snapshot.properties,
+		&snapshot.baseSnapshot.baseProperties)
+	android.InitAndroidArchModule(snapshot, android.DeviceSupported, android.MultilibBoth)
+	return snapshot
 }
 
 type baseSnapshotDecoratorProperties struct {
@@ -429,9 +362,12 @@
 	// Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
 	Target_arch string
 
+	// Suffix to be added to the module name when exporting to Android.mk, e.g. ".vendor".
+	Androidmk_suffix string
+
 	// Suffix to be added to the module name, e.g., vendor_shared,
 	// recovery_shared, etc.
-	Module_suffix string
+	ModuleSuffix string `blueprint:"mutated"`
 }
 
 // baseSnapshotDecorator provides common basic functions for all snapshot modules, such as snapshot
@@ -460,7 +396,7 @@
 		versionSuffix += "." + p.arch()
 	}
 
-	return p.baseProperties.Module_suffix + versionSuffix
+	return p.baseProperties.ModuleSuffix + versionSuffix
 }
 
 func (p *baseSnapshotDecorator) version() string {
@@ -471,18 +407,22 @@
 	return p.baseProperties.Target_arch
 }
 
-func (p *baseSnapshotDecorator) module_suffix() string {
-	return p.baseProperties.Module_suffix
+func (p *baseSnapshotDecorator) moduleSuffix() string {
+	return p.baseProperties.ModuleSuffix
 }
 
 func (p *baseSnapshotDecorator) isSnapshotPrebuilt() bool {
 	return true
 }
 
+func (p *baseSnapshotDecorator) snapshotAndroidMkSuffix() string {
+	return p.baseProperties.Androidmk_suffix
+}
+
 // Call this with a module suffix after creating a snapshot module, such as
 // vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc.
-func (p *baseSnapshotDecorator) init(m *Module, suffix string) {
-	p.baseProperties.Module_suffix = suffix
+func (p *baseSnapshotDecorator) init(m *Module, snapshotSuffix, moduleSuffix string) {
+	p.baseProperties.ModuleSuffix = snapshotSuffix + moduleSuffix
 	m.AddProperties(&p.baseProperties)
 	android.AddLoadHook(m, func(ctx android.LoadHookContext) {
 		vendorSnapshotLoadHook(ctx, p)
@@ -542,7 +482,6 @@
 		// Library flags for cfi variant.
 		Cfi snapshotLibraryProperties `android:"arch_variant"`
 	}
-	androidMkSuffix string
 }
 
 func (p *snapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
@@ -565,14 +504,6 @@
 // As snapshots are prebuilts, this just returns the prebuilt binary after doing things which are
 // done by normal library decorator, e.g. exporting flags.
 func (p *snapshotLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
-	m := ctx.Module().(*Module)
-
-	if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] {
-		p.androidMkSuffix = vendorSuffix
-	} else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] {
-		p.androidMkSuffix = recoverySuffix
-	}
-
 	if p.header() {
 		return p.libraryDecorator.link(ctx, flags, deps, objs)
 	}
@@ -655,7 +586,7 @@
 	}
 }
 
-func snapshotLibraryFactory(suffix string) (*Module, *snapshotLibraryDecorator) {
+func snapshotLibraryFactory(snapshotSuffix, moduleSuffix string) (*Module, *snapshotLibraryDecorator) {
 	module, library := NewLibrary(android.DeviceSupported)
 
 	module.stl = nil
@@ -678,7 +609,7 @@
 	module.linker = prebuilt
 	module.installer = prebuilt
 
-	prebuilt.init(module, suffix)
+	prebuilt.init(module, snapshotSuffix, moduleSuffix)
 	module.AddProperties(
 		&prebuilt.properties,
 		&prebuilt.sanitizerProperties,
@@ -692,7 +623,7 @@
 // overrides the vendor variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
 // is set.
 func VendorSnapshotSharedFactory() android.Module {
-	module, prebuilt := snapshotLibraryFactory(vendorSnapshotSharedSuffix)
+	module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotSharedSuffix)
 	prebuilt.libraryDecorator.BuildOnlyShared()
 	return module.Init()
 }
@@ -702,7 +633,7 @@
 // overrides the recovery variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
 // is set.
 func RecoverySnapshotSharedFactory() android.Module {
-	module, prebuilt := snapshotLibraryFactory(recoverySnapshotSharedSuffix)
+	module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotSharedSuffix)
 	prebuilt.libraryDecorator.BuildOnlyShared()
 	return module.Init()
 }
@@ -712,7 +643,7 @@
 // overrides the vendor variant of the cc static library with the same name, if BOARD_VNDK_VERSION
 // is set.
 func VendorSnapshotStaticFactory() android.Module {
-	module, prebuilt := snapshotLibraryFactory(vendorSnapshotStaticSuffix)
+	module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotStaticSuffix)
 	prebuilt.libraryDecorator.BuildOnlyStatic()
 	return module.Init()
 }
@@ -722,7 +653,7 @@
 // overrides the recovery variant of the cc static library with the same name, if BOARD_VNDK_VERSION
 // is set.
 func RecoverySnapshotStaticFactory() android.Module {
-	module, prebuilt := snapshotLibraryFactory(recoverySnapshotStaticSuffix)
+	module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotStaticSuffix)
 	prebuilt.libraryDecorator.BuildOnlyStatic()
 	return module.Init()
 }
@@ -732,7 +663,7 @@
 // overrides the vendor variant of the cc header library with the same name, if BOARD_VNDK_VERSION
 // is set.
 func VendorSnapshotHeaderFactory() android.Module {
-	module, prebuilt := snapshotLibraryFactory(vendorSnapshotHeaderSuffix)
+	module, prebuilt := snapshotLibraryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotHeaderSuffix)
 	prebuilt.libraryDecorator.HeaderOnly()
 	return module.Init()
 }
@@ -742,7 +673,7 @@
 // overrides the recovery variant of the cc header library with the same name, if BOARD_VNDK_VERSION
 // is set.
 func RecoverySnapshotHeaderFactory() android.Module {
-	module, prebuilt := snapshotLibraryFactory(recoverySnapshotHeaderSuffix)
+	module, prebuilt := snapshotLibraryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotHeaderSuffix)
 	prebuilt.libraryDecorator.HeaderOnly()
 	return module.Init()
 }
@@ -764,8 +695,7 @@
 type snapshotBinaryDecorator struct {
 	baseSnapshotDecorator
 	*binaryDecorator
-	properties      snapshotBinaryProperties
-	androidMkSuffix string
+	properties snapshotBinaryProperties
 }
 
 func (p *snapshotBinaryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
@@ -789,14 +719,6 @@
 	p.unstrippedOutputFile = in
 	binName := in.Base()
 
-	m := ctx.Module().(*Module)
-	if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] {
-		p.androidMkSuffix = vendorSuffix
-	} else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] {
-		p.androidMkSuffix = recoverySuffix
-
-	}
-
 	// use cpExecutable to make it executable
 	outputFile := android.PathForModuleOut(ctx, binName)
 	ctx.Build(pctx, android.BuildParams{
@@ -817,17 +739,17 @@
 // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_binary
 // overrides the vendor variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
 func VendorSnapshotBinaryFactory() android.Module {
-	return snapshotBinaryFactory(vendorSnapshotBinarySuffix)
+	return snapshotBinaryFactory(vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotBinarySuffix)
 }
 
 // recovery_snapshot_binary is a special prebuilt executable binary which is auto-generated by
 // development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_binary
 // overrides the recovery variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
 func RecoverySnapshotBinaryFactory() android.Module {
-	return snapshotBinaryFactory(recoverySnapshotBinarySuffix)
+	return snapshotBinaryFactory(recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotBinarySuffix)
 }
 
-func snapshotBinaryFactory(suffix string) android.Module {
+func snapshotBinaryFactory(snapshotSuffix, moduleSuffix string) android.Module {
 	module, binary := NewBinary(android.DeviceSupported)
 	binary.baseLinker.Properties.No_libcrt = BoolPtr(true)
 	binary.baseLinker.Properties.Nocrt = BoolPtr(true)
@@ -846,7 +768,7 @@
 	module.stl = nil
 	module.linker = prebuilt
 
-	prebuilt.init(module, suffix)
+	prebuilt.init(module, snapshotSuffix, moduleSuffix)
 	module.AddProperties(&prebuilt.properties)
 	return module.Init()
 }
@@ -866,8 +788,7 @@
 type snapshotObjectLinker struct {
 	baseSnapshotDecorator
 	objectLinker
-	properties      vendorSnapshotObjectProperties
-	androidMkSuffix string
+	properties vendorSnapshotObjectProperties
 }
 
 func (p *snapshotObjectLinker) matchesWithDevice(config android.DeviceConfig) bool {
@@ -887,14 +808,6 @@
 		return nil
 	}
 
-	m := ctx.Module().(*Module)
-
-	if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] {
-		p.androidMkSuffix = vendorSuffix
-	} else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] {
-		p.androidMkSuffix = recoverySuffix
-	}
-
 	return android.PathForModuleSrc(ctx, *p.properties.Src)
 }
 
@@ -915,7 +828,7 @@
 	}
 	module.linker = prebuilt
 
-	prebuilt.init(module, vendorSnapshotObjectSuffix)
+	prebuilt.init(module, vendorSnapshotImageSingleton.moduleNameSuffix(), snapshotObjectSuffix)
 	module.AddProperties(&prebuilt.properties)
 	return module.Init()
 }
@@ -933,130 +846,19 @@
 	}
 	module.linker = prebuilt
 
-	prebuilt.init(module, recoverySnapshotObjectSuffix)
+	prebuilt.init(module, recoverySnapshotImageSingleton.moduleNameSuffix(), snapshotObjectSuffix)
 	module.AddProperties(&prebuilt.properties)
 	return module.Init()
 }
 
 type snapshotInterface interface {
 	matchesWithDevice(config android.DeviceConfig) bool
+	isSnapshotPrebuilt() bool
+	version() string
+	snapshotAndroidMkSuffix() string
 }
 
 var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
 var _ snapshotInterface = (*snapshotLibraryDecorator)(nil)
 var _ snapshotInterface = (*snapshotBinaryDecorator)(nil)
 var _ snapshotInterface = (*snapshotObjectLinker)(nil)
-
-//
-// Mutators that helps vendor snapshot modules override source modules.
-//
-
-// VendorSnapshotMutator gathers all snapshots for vendor, and disable all snapshots which don't
-// match with device, e.g.
-//   - snapshot version is different with BOARD_VNDK_VERSION
-//   - snapshot arch is different with device's arch (e.g. arm vs x86)
-//
-// This also handles vndk_prebuilt_shared, except for they won't be disabled in any cases, given
-// that any versions of VNDK might be packed into vndk APEX.
-//
-// TODO(b/145966707): remove mutator and utilize android.Prebuilt to override source modules
-func VendorSnapshotMutator(ctx android.BottomUpMutatorContext) {
-	snapshotMutator(ctx, vendorSnapshotImageSingleton)
-}
-
-func RecoverySnapshotMutator(ctx android.BottomUpMutatorContext) {
-	snapshotMutator(ctx, recoverySnapshotImageSingleton)
-}
-
-func snapshotMutator(ctx android.BottomUpMutatorContext, image snapshotImage) {
-	if !image.isUsingSnapshot(ctx.DeviceConfig()) {
-		return
-	}
-	module, ok := ctx.Module().(*Module)
-	if !ok || !module.Enabled() {
-		return
-	}
-	if image.skipModuleMutator(ctx) {
-		return
-	}
-	if !module.isSnapshotPrebuilt() {
-		return
-	}
-
-	// isSnapshotPrebuilt ensures snapshotInterface
-	if !module.linker.(snapshotInterface).matchesWithDevice(ctx.DeviceConfig()) {
-		// Disable unnecessary snapshot module, but do not disable
-		// vndk_prebuilt_shared because they might be packed into vndk APEX
-		if !module.IsVndk() {
-			module.Disable()
-		}
-		return
-	}
-
-	var snapshotMap *snapshotMap = image.getSnapshotMap(module, ctx.Config())
-	if snapshotMap == nil {
-		return
-	}
-
-	mutex := image.getMutex()
-	mutex.Lock()
-	defer mutex.Unlock()
-	snapshotMap.add(module.BaseModuleName(), ctx.Arch().ArchType, ctx.ModuleName())
-}
-
-// VendorSnapshotSourceMutator disables source modules which have corresponding snapshots.
-func VendorSnapshotSourceMutator(ctx android.BottomUpMutatorContext) {
-	snapshotSourceMutator(ctx, vendorSnapshotImageSingleton)
-}
-
-func RecoverySnapshotSourceMutator(ctx android.BottomUpMutatorContext) {
-	snapshotSourceMutator(ctx, recoverySnapshotImageSingleton)
-}
-
-func snapshotSourceMutator(ctx android.BottomUpMutatorContext, image snapshotImage) {
-	if !ctx.Device() {
-		return
-	}
-	if !image.isUsingSnapshot(ctx.DeviceConfig()) {
-		return
-	}
-
-	module, ok := ctx.Module().(*Module)
-	if !ok {
-		return
-	}
-
-	if image.shouldBeAddedToSuffixModules(module) {
-		mutex := image.getMutex()
-		mutex.Lock()
-		defer mutex.Unlock()
-
-		image.suffixModules(ctx.Config())[ctx.ModuleName()] = true
-	}
-
-	if module.isSnapshotPrebuilt() {
-		return
-	}
-	if image.skipSourceMutator(ctx) {
-		return
-	}
-
-	var snapshotMap *snapshotMap = image.getSnapshotMap(module, ctx.Config())
-	if snapshotMap == nil {
-		return
-	}
-
-	if _, ok := snapshotMap.get(ctx.ModuleName(), ctx.Arch().ArchType); !ok {
-		// Corresponding snapshot doesn't exist
-		return
-	}
-
-	// Disables source modules if corresponding snapshot exists.
-	if lib, ok := module.linker.(libraryInterface); ok && lib.buildStatic() && lib.buildShared() {
-		// But do not disable because the shared variant depends on the static variant.
-		module.HideFromMake()
-		module.Properties.HideFromMake = true
-	} else {
-		module.Disable()
-	}
-}
diff --git a/cc/testing.go b/cc/testing.go
index dc9a59d..3a5bd17 100644
--- a/cc/testing.go
+++ b/cc/testing.go
@@ -586,17 +586,13 @@
 	ctx.RegisterModuleType("vendor_public_library", vendorPublicLibraryFactory)
 	ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
 	ctx.RegisterModuleType("vndk_prebuilt_shared", VndkPrebuiltSharedFactory)
-	ctx.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory)
-	ctx.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory)
-	ctx.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory)
+	vendorSnapshotImageSingleton.init(ctx)
+	recoverySnapshotImageSingleton.init(ctx)
 	RegisterVndkLibraryTxtTypes(ctx)
 	ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
 	android.RegisterPrebuiltMutators(ctx)
 	RegisterRequiredBuildComponentsForTest(ctx)
 	ctx.RegisterSingletonType("vndk-snapshot", VndkSnapshotSingleton)
-	ctx.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton)
-	ctx.RegisterSingletonType("vendor-fake-snapshot", VendorFakeSnapshotSingleton)
-	ctx.RegisterSingletonType("recovery-snapshot", RecoverySnapshotSingleton)
 
 	return ctx
 }
diff --git a/cc/vendor_snapshot.go b/cc/vendor_snapshot.go
index 7346aac..35fc1c1 100644
--- a/cc/vendor_snapshot.go
+++ b/cc/vendor_snapshot.go
@@ -291,6 +291,7 @@
 type snapshotJsonFlags struct {
 	ModuleName          string `json:",omitempty"`
 	RelativeInstallPath string `json:",omitempty"`
+	AndroidMkSuffix     string `json:",omitempty"`
 
 	// library flags
 	ExportedDirs       []string `json:",omitempty"`
@@ -403,6 +404,7 @@
 		} else {
 			prop.RelativeInstallPath = m.RelativeInstallPath()
 		}
+		prop.AndroidMkSuffix = m.Properties.SubName
 		prop.RuntimeLibs = m.Properties.SnapshotRuntimeLibs
 		prop.Required = m.RequiredModuleNames()
 		for _, path := range m.InitRc() {
diff --git a/cc/vendor_snapshot_test.go b/cc/vendor_snapshot_test.go
index cce28b0..499d7ae 100644
--- a/cc/vendor_snapshot_test.go
+++ b/cc/vendor_snapshot_test.go
@@ -329,6 +329,24 @@
 			},
 		},
 	}
+
+	// old snapshot module which has to be ignored
+	vndk_prebuilt_shared {
+		name: "libvndk",
+		version: "OLD",
+		target_arch: "arm64",
+		vendor_available: true,
+		product_available: true,
+		vndk: {
+			enabled: true,
+		},
+		arch: {
+			arm64: {
+				srcs: ["libvndk.so"],
+				export_include_dirs: ["include/libvndk"],
+			},
+		},
+	}
 `
 
 	vendorProprietaryBp := `
@@ -367,6 +385,27 @@
 		srcs: ["bin.cpp"],
 	}
 
+	vendor_snapshot {
+		name: "vendor_snapshot",
+		compile_multilib: "first",
+		version: "BOARD",
+		vndk_libs: [
+			"libvndk",
+		],
+		static_libs: [
+			"libvendor",
+			"libvendor_available",
+			"libvndk",
+		],
+		shared_libs: [
+			"libvendor",
+			"libvendor_available",
+		],
+		binaries: [
+			"bin",
+		],
+	}
+
 	vendor_snapshot_static {
 		name: "libvndk",
 		version: "BOARD",
@@ -408,6 +447,7 @@
 
 	vendor_snapshot_shared {
 		name: "libvendor_available",
+		androidmk_suffix: ".vendor",
 		version: "BOARD",
 		target_arch: "arm64",
 		vendor: true,
@@ -421,6 +461,7 @@
 
 	vendor_snapshot_static {
 		name: "libvendor_available",
+		androidmk_suffix: ".vendor",
 		version: "BOARD",
 		target_arch: "arm64",
 		vendor: true,
@@ -443,6 +484,19 @@
 			},
 		},
 	}
+
+	// old snapshot module which has to be ignored
+	vendor_snapshot_binary {
+		name: "bin",
+		version: "OLD",
+		target_arch: "arm64",
+		vendor: true,
+		arch: {
+			arm64: {
+				src: "bin",
+			},
+		},
+	}
 `
 	depsBp := GatherRequiredDepsForTest(android.Android)
 
diff --git a/cc/vndk_prebuilt.go b/cc/vndk_prebuilt.go
index 04162cd..71e6427 100644
--- a/cc/vndk_prebuilt.go
+++ b/cc/vndk_prebuilt.go
@@ -107,6 +107,10 @@
 	return "64"
 }
 
+func (p *vndkPrebuiltLibraryDecorator) snapshotAndroidMkSuffix() string {
+	return ".vendor"
+}
+
 func (p *vndkPrebuiltLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
 	p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
 	return p.libraryDecorator.linkerFlags(ctx, flags)
diff --git a/cmd/soong_build/main.go b/cmd/soong_build/main.go
index 8cd4f97..9dcb5f3 100644
--- a/cmd/soong_build/main.go
+++ b/cmd/soong_build/main.go
@@ -19,6 +19,7 @@
 	"fmt"
 	"os"
 	"path/filepath"
+	"strings"
 
 	"github.com/google/blueprint/bootstrap"
 
@@ -166,12 +167,43 @@
 	// conversion for Bazel conversion.
 	bp2buildCtx := android.NewContext(configuration)
 	bp2buildCtx.RegisterForBazelConversion()
+
+	// No need to generate Ninja build rules/statements from Modules and Singletons.
 	configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions)
 	bp2buildCtx.SetNameInterface(newNameResolver(configuration))
+
+	// Run the loading and analysis pipeline.
 	bootstrap.Main(bp2buildCtx.Context, configuration, extraNinjaDeps...)
 
+	// Run the code-generation phase to convert BazelTargetModules to BUILD files.
 	codegenContext := bp2build.NewCodegenContext(configuration, *bp2buildCtx, bp2build.Bp2Build)
 	bp2build.Codegen(codegenContext)
+
+	// Workarounds to support running bp2build in a clean AOSP checkout with no
+	// prior builds, and exiting early as soon as the BUILD files get generated,
+	// therefore not creating build.ninja files that soong_ui and callers of
+	// soong_build expects.
+	//
+	// These files are: build.ninja and build.ninja.d. Since Kati hasn't been
+	// ran as well, and `nothing` is defined in a .mk file, there isn't a ninja
+	// target called `nothing`, so we manually create it here.
+	//
+	// Even though outFile (build.ninja) and depFile (build.ninja.d) are values
+	// passed into bootstrap.Main, they are package-private fields in bootstrap.
+	// Short of modifying Blueprint to add an exported getter, inlining them
+	// here is the next-best practical option.
+	ninjaFileName := "build.ninja"
+	ninjaFile := android.PathForOutput(codegenContext, ninjaFileName)
+	ninjaFileD := android.PathForOutput(codegenContext, ninjaFileName+".d")
+	extraNinjaDepsString := strings.Join(extraNinjaDeps, " \\\n ")
+	// A workaround to create the 'nothing' ninja target so `m nothing` works,
+	// since bp2build runs without Kati, and the 'nothing' target is declared in
+	// a Makefile.
+	android.WriteFileToOutputDir(ninjaFile, []byte("build nothing: phony\n  phony_output = true\n"), 0666)
+	android.WriteFileToOutputDir(
+		ninjaFileD,
+		[]byte(fmt.Sprintf("%s: \\\n %s\n", ninjaFileName, extraNinjaDepsString)),
+		0666)
 }
 
 // shouldPrepareBuildActions reads configuration and flags if build actions
diff --git a/dexpreopt/class_loader_context.go b/dexpreopt/class_loader_context.go
index bf610ef..ec62eb3 100644
--- a/dexpreopt/class_loader_context.go
+++ b/dexpreopt/class_loader_context.go
@@ -534,3 +534,26 @@
 	}
 	return clcs
 }
+
+// Convert Soong CLC map to JSON representation for Make.
+func toJsonClassLoaderContext(clcMap ClassLoaderContextMap) jsonClassLoaderContextMap {
+	jClcMap := make(jsonClassLoaderContextMap)
+	for sdkVer, clcs := range clcMap {
+		sdkVerStr := fmt.Sprintf("%d", sdkVer)
+		jClcMap[sdkVerStr] = toJsonClassLoaderContextRec(clcs)
+	}
+	return jClcMap
+}
+
+// Recursive helper for toJsonClassLoaderContext.
+func toJsonClassLoaderContextRec(clcs []*ClassLoaderContext) map[string]*jsonClassLoaderContext {
+	jClcs := make(map[string]*jsonClassLoaderContext, len(clcs))
+	for _, clc := range clcs {
+		jClcs[clc.Name] = &jsonClassLoaderContext{
+			Host:        clc.Host.String(),
+			Device:      clc.Device,
+			Subcontexts: toJsonClassLoaderContextRec(clc.Subcontexts),
+		}
+	}
+	return jClcs
+}
diff --git a/dexpreopt/config.go b/dexpreopt/config.go
index 867ece6..d55204b 100644
--- a/dexpreopt/config.go
+++ b/dexpreopt/config.go
@@ -114,6 +114,7 @@
 	ProfileBootListing   android.OptionalPath
 
 	EnforceUsesLibraries bool
+	ProvidesUsesLibrary  string // the name of the <uses-library> (usually the same as its module)
 	ClassLoaderContexts  ClassLoaderContextMap
 
 	Archs                   []android.ArchType
@@ -290,6 +291,42 @@
 	return config.ModuleConfig, nil
 }
 
+// WriteSlimModuleConfigForMake serializes a subset of ModuleConfig into a per-module
+// dexpreopt.config JSON file. It is a way to pass dexpreopt information about Soong modules to
+// Make, which is needed when a Make module has a <uses-library> dependency on a Soong module.
+func WriteSlimModuleConfigForMake(ctx android.ModuleContext, config *ModuleConfig, path android.WritablePath) {
+	if path == nil {
+		return
+	}
+
+	// JSON representation of the slim module dexpreopt.config.
+	type slimModuleJSONConfig struct {
+		Name                 string
+		DexLocation          string
+		BuildPath            string
+		EnforceUsesLibraries bool
+		ProvidesUsesLibrary  string
+		ClassLoaderContexts  jsonClassLoaderContextMap
+	}
+
+	jsonConfig := &slimModuleJSONConfig{
+		Name:                 config.Name,
+		DexLocation:          config.DexLocation,
+		BuildPath:            config.BuildPath.String(),
+		EnforceUsesLibraries: config.EnforceUsesLibraries,
+		ProvidesUsesLibrary:  config.ProvidesUsesLibrary,
+		ClassLoaderContexts:  toJsonClassLoaderContext(config.ClassLoaderContexts),
+	}
+
+	data, err := json.MarshalIndent(jsonConfig, "", "    ")
+	if err != nil {
+		ctx.ModuleErrorf("failed to JSON marshal module dexpreopt.config: %v", err)
+		return
+	}
+
+	android.WriteFileRule(ctx, path, string(data))
+}
+
 // dex2oatModuleName returns the name of the module to use for the dex2oat host
 // tool. It should be a binary module with public visibility that is compiled
 // and installed for host.
diff --git a/genrule/genrule.go b/genrule/genrule.go
index 66e5652..ddfb459 100644
--- a/genrule/genrule.go
+++ b/genrule/genrule.go
@@ -47,7 +47,7 @@
 		ctx.BottomUp("genrule_tool_deps", toolDepsMutator).Parallel()
 	})
 
-	android.RegisterBp2BuildMutator("genrule", bp2buildMutator)
+	android.RegisterBp2BuildMutator("genrule", GenruleBp2Build)
 }
 
 var (
@@ -794,7 +794,7 @@
 	return module
 }
 
-func bp2buildMutator(ctx android.TopDownMutatorContext) {
+func GenruleBp2Build(ctx android.TopDownMutatorContext) {
 	if m, ok := ctx.Module().(*Module); ok {
 		name := "__bp2build__" + m.Name()
 		// Bazel only has the "tools" attribute.
diff --git a/java/Android.bp b/java/Android.bp
index 9c28968..364566a 100644
--- a/java/Android.bp
+++ b/java/Android.bp
@@ -24,6 +24,7 @@
         "app.go",
         "app_import.go",
         "app_set.go",
+        "boot_image.go",
         "boot_jars.go",
         "builder.go",
         "device_host_converter.go",
@@ -63,6 +64,7 @@
         "app_import_test.go",
         "app_set_test.go",
         "app_test.go",
+        "boot_image_test.go",
         "device_host_converter_test.go",
         "dexpreopt_test.go",
         "dexpreopt_bootjars_test.go",
diff --git a/java/androidmk.go b/java/androidmk.go
index cc454b0..21f3012 100644
--- a/java/androidmk.go
+++ b/java/androidmk.go
@@ -125,6 +125,10 @@
 					entries.SetString("LOCAL_MODULE_STEM", library.Stem())
 
 					entries.SetOptionalPaths("LOCAL_SOONG_LINT_REPORTS", library.linter.reports)
+
+					if library.dexpreopter.configPath != nil {
+						entries.SetPath("LOCAL_SOONG_DEXPREOPT_CONFIG", library.dexpreopter.configPath)
+					}
 				},
 			},
 		})
diff --git a/java/app.go b/java/app.go
index e6c9a2d..ce89e9b 100755
--- a/java/app.go
+++ b/java/app.go
@@ -455,6 +455,7 @@
 
 func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
 	a.dexpreopter.installPath = a.installPath(ctx)
+	a.dexpreopter.isApp = true
 	if a.dexProperties.Uncompress_dex == nil {
 		// If the value was not force-set by the user, use reasonable default based on the module.
 		a.dexProperties.Uncompress_dex = proptools.BoolPtr(a.shouldUncompressDex(ctx))
diff --git a/java/app_import.go b/java/app_import.go
index df940f1..6f21bfb 100644
--- a/java/app_import.go
+++ b/java/app_import.go
@@ -255,6 +255,7 @@
 		installDir = android.PathForModuleInstall(ctx, "app", a.BaseModuleName())
 	}
 
+	a.dexpreopter.isApp = true
 	a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
 	a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
 	a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
diff --git a/java/boot_image.go b/java/boot_image.go
new file mode 100644
index 0000000..0a525b7
--- /dev/null
+++ b/java/boot_image.go
@@ -0,0 +1,136 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// 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 (
+	"fmt"
+	"strings"
+
+	"android/soong/android"
+	"android/soong/dexpreopt"
+	"github.com/google/blueprint"
+)
+
+func init() {
+	RegisterBootImageBuildComponents(android.InitRegistrationContext)
+}
+
+func RegisterBootImageBuildComponents(ctx android.RegistrationContext) {
+	ctx.RegisterModuleType("boot_image", bootImageFactory)
+}
+
+type bootImageProperties struct {
+	// The name of the image this represents.
+	//
+	// Must be one of "art" or "boot".
+	Image_name string
+}
+
+type BootImageModule struct {
+	android.ModuleBase
+	android.ApexModuleBase
+
+	properties bootImageProperties
+}
+
+func bootImageFactory() android.Module {
+	m := &BootImageModule{}
+	m.AddProperties(&m.properties)
+	android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon)
+	android.InitApexModule(m)
+	return m
+}
+
+var BootImageInfoProvider = blueprint.NewProvider(BootImageInfo{})
+
+type BootImageInfo struct {
+	// The image config, internal to this module (and the dex_bootjars singleton).
+	//
+	// Will be nil if the BootImageInfo has not been provided for a specific module. That can occur
+	// when SkipDexpreoptBootJars(ctx) returns true.
+	imageConfig *bootImageConfig
+}
+
+func (i BootImageInfo) Modules() android.ConfiguredJarList {
+	return i.imageConfig.modules
+}
+
+// Get a map from ArchType to the associated boot image's contents for Android.
+//
+// Extension boot images only return their own files, not the files of the boot images they extend.
+func (i BootImageInfo) AndroidBootImageFilesByArchType() map[android.ArchType]android.OutputPaths {
+	files := map[android.ArchType]android.OutputPaths{}
+	if i.imageConfig != nil {
+		for _, variant := range i.imageConfig.variants {
+			// We also generate boot images for host (for testing), but we don't need those in the apex.
+			// TODO(b/177892522) - consider changing this to check Os.OsClass = android.Device
+			if variant.target.Os == android.Android {
+				files[variant.target.Arch.ArchType] = variant.imagesDeps
+			}
+		}
+	}
+	return files
+}
+
+func (b *BootImageModule) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
+	tag := ctx.OtherModuleDependencyTag(dep)
+	if tag == dexpreopt.Dex2oatDepTag {
+		// The dex2oat tool is only needed for building and is not required in the apex.
+		return false
+	}
+	panic(fmt.Errorf("boot_image module %q should not have a dependency on %q via tag %s", b, dep, android.PrettyPrintTag(tag)))
+}
+
+func (b *BootImageModule) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
+	return nil
+}
+
+func (b *BootImageModule) DepsMutator(ctx android.BottomUpMutatorContext) {
+	if SkipDexpreoptBootJars(ctx) {
+		return
+	}
+
+	// Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
+	// path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
+	dexpreopt.RegisterToolDeps(ctx)
+}
+
+func (b *BootImageModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+	// Nothing to do if skipping the dexpreopt of boot image jars.
+	if SkipDexpreoptBootJars(ctx) {
+		return
+	}
+
+	// Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars
+	// GenerateSingletonBuildActions method as it cannot create it for itself.
+	dexpreopt.GetGlobalSoongConfig(ctx)
+
+	// Get a map of the image configs that are supported.
+	imageConfigs := genBootImageConfigs(ctx)
+
+	// Retrieve the config for this image.
+	imageName := b.properties.Image_name
+	imageConfig := imageConfigs[imageName]
+	if imageConfig == nil {
+		ctx.PropertyErrorf("image_name", "Unknown image name %q, expected one of %s", imageName, strings.Join(android.SortedStringKeys(imageConfigs), ", "))
+		return
+	}
+
+	// Construct the boot image info from the config.
+	info := BootImageInfo{imageConfig: imageConfig}
+
+	// Make it available for other modules.
+	ctx.SetProvider(BootImageInfoProvider, info)
+}
diff --git a/java/boot_image_test.go b/java/boot_image_test.go
new file mode 100644
index 0000000..a295782
--- /dev/null
+++ b/java/boot_image_test.go
@@ -0,0 +1,31 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// 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 (
+	"testing"
+)
+
+// Contains some simple tests for boot_image logic, additional tests can be found in
+// apex/boot_image_test.go as the ART boot image requires modules from the ART apex.
+
+func TestUnknownBootImage(t *testing.T) {
+	testJavaError(t, "image_name: Unknown image name \\\"unknown\\\", expected one of art, boot", `
+		boot_image {
+			name: "unknown-boot-image",
+			image_name: "unknown",
+		}
+`)
+}
diff --git a/java/boot_jars.go b/java/boot_jars.go
index 823275b..ac8107b 100644
--- a/java/boot_jars.go
+++ b/java/boot_jars.go
@@ -49,14 +49,36 @@
 	return true
 }
 
+// isActiveModule returns true if the given module should be considered for boot
+// jars, i.e. if it's enabled and the preferred one in case of source and
+// prebuilt alternatives.
+func isActiveModule(module android.Module) bool {
+	if !module.Enabled() {
+		return false
+	}
+	if module.IsReplacedByPrebuilt() {
+		// A source module that has been replaced by a prebuilt counterpart.
+		return false
+	}
+	if prebuilt, ok := module.(android.PrebuiltInterface); ok {
+		if p := prebuilt.Prebuilt(); p != nil {
+			return p.UsePrebuilt()
+		}
+	}
+	return true
+}
+
 func (b *bootJarsSingleton) GenerateBuildActions(ctx android.SingletonContext) {
 	config := ctx.Config()
 	if config.SkipBootJarsCheck() {
 		return
 	}
 
-	// Populate a map from module name to APEX from the boot jars. If there is a problem
-	// such as duplicate modules then fail and return immediately.
+	// Populate a map from module name to APEX from the boot jars. If there is a
+	// problem such as duplicate modules then fail and return immediately. Note
+	// that both module and APEX names are tracked by base names here, so we need
+	// to be careful to remove "prebuilt_" prefixes when comparing them with
+	// actual modules and APEX bundles.
 	moduleToApex := make(map[string]string)
 	if !populateMapFromConfiguredJarList(ctx, moduleToApex, config.NonUpdatableBootJars(), "BootJars") ||
 		!populateMapFromConfiguredJarList(ctx, moduleToApex, config.UpdatableBootJars(), "UpdatableBootJars") {
@@ -69,10 +91,14 @@
 	// Scan all the modules looking for the module/apex variants corresponding to the
 	// boot jars.
 	ctx.VisitAllModules(func(module android.Module) {
-		name := ctx.ModuleName(module)
+		if !isActiveModule(module) {
+			return
+		}
+
+		name := android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName(module))
 		if apex, ok := moduleToApex[name]; ok {
 			apexInfo := ctx.ModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo)
-			if (apex == "platform" && apexInfo.IsForPlatform()) || apexInfo.InApex(apex) {
+			if (apex == "platform" && apexInfo.IsForPlatform()) || apexInfo.InApexByBaseName(apex) {
 				// The module name/apex variant should be unique in the system but double check
 				// just in case something has gone wrong.
 				if existing, ok := nameToApexVariant[name]; ok {
diff --git a/java/dexpreopt.go b/java/dexpreopt.go
index b5830c7..ac00592 100644
--- a/java/dexpreopt.go
+++ b/java/dexpreopt.go
@@ -30,6 +30,7 @@
 	installPath         android.InstallPath
 	uncompressedDex     bool
 	isSDKLibrary        bool
+	isApp               bool
 	isTest              bool
 	isPresignedPrebuilt bool
 
@@ -38,6 +39,11 @@
 	classLoaderContexts dexpreopt.ClassLoaderContextMap
 
 	builtInstalled string
+
+	// A path to a dexpreopt.config file generated by Soong for libraries that may be used as a
+	// <uses-library> by Make modules. The path is passed to Make via LOCAL_SOONG_DEXPREOPT_CONFIG
+	// variable. If the path is nil, no config is generated (which is the case for apps and tests).
+	configPath android.WritablePath
 }
 
 type DexpreoptProperties struct {
@@ -117,7 +123,40 @@
 	// the dexpreopter struct hasn't been fully initialized before we're called,
 	// e.g. in aar.go. This keeps the behaviour that dexpreopting is effectively
 	// disabled, even if installable is true.
-	if d.dexpreoptDisabled(ctx) || d.installPath.Base() == "." {
+	if d.installPath.Base() == "." {
+		return
+	}
+
+	dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
+
+	buildPath := android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath
+
+	providesUsesLib := ctx.ModuleName()
+	if ulib, ok := ctx.Module().(ProvidesUsesLib); ok {
+		name := ulib.ProvidesUsesLib()
+		if name != nil {
+			providesUsesLib = *name
+		}
+	}
+
+	if !d.isApp && !d.isTest {
+		// Slim dexpreopt config is serialized to dexpreopt.config files and used by
+		// dex_preopt_config_merger.py to get information about <uses-library> dependencies.
+		// Note that it might be needed even if dexpreopt is disabled for this module.
+		slimDexpreoptConfig := &dexpreopt.ModuleConfig{
+			Name:                 ctx.ModuleName(),
+			DexLocation:          dexLocation,
+			BuildPath:            buildPath,
+			EnforceUsesLibraries: d.enforceUsesLibs,
+			ProvidesUsesLibrary:  providesUsesLib,
+			ClassLoaderContexts:  d.classLoaderContexts,
+			// The rest of the fields are not needed.
+		}
+		d.configPath = android.PathForModuleOut(ctx, "dexpreopt", "dexpreopt.config")
+		dexpreopt.WriteSlimModuleConfigForMake(ctx, slimDexpreoptConfig, d.configPath)
+	}
+
+	if d.dexpreoptDisabled(ctx) {
 		return
 	}
 
@@ -157,8 +196,6 @@
 	// The image locations for all Android variants are identical.
 	imageLocations := bootImage.getAnyAndroidVariant().imageLocations()
 
-	dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
-
 	var profileClassListing android.OptionalPath
 	var profileBootListing android.OptionalPath
 	profileIsTextListing := false
@@ -177,10 +214,11 @@
 		}
 	}
 
+	// Full dexpreopt config, used to create dexpreopt build rules.
 	dexpreoptConfig := &dexpreopt.ModuleConfig{
 		Name:            ctx.ModuleName(),
 		DexLocation:     dexLocation,
-		BuildPath:       android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
+		BuildPath:       buildPath,
 		DexPath:         dexJarFile,
 		ManifestPath:    d.manifestFile,
 		UncompressedDex: d.uncompressedDex,
@@ -192,6 +230,7 @@
 		ProfileBootListing:   profileBootListing,
 
 		EnforceUsesLibraries: d.enforceUsesLibs,
+		ProvidesUsesLibrary:  providesUsesLib,
 		ClassLoaderContexts:  d.classLoaderContexts,
 
 		Archs:                   archs,
diff --git a/java/dexpreopt_bootjars.go b/java/dexpreopt_bootjars.go
index 004cbbb..2a7eb42 100644
--- a/java/dexpreopt_bootjars.go
+++ b/java/dexpreopt_bootjars.go
@@ -35,6 +35,13 @@
 //
 // Changes:
 // 1) dex_bootjars is now a singleton module and not a plain singleton.
+// 2) Boot images are now represented by the boot_image module type.
+// 3) The art boot image is called "art-boot-image", the framework boot image is called
+//    "framework-boot-image".
+// 4) They are defined in art/build/boot/Android.bp and frameworks/base/boot/Android.bp
+//    respectively.
+// 5) Each boot_image retrieves the appropriate boot image configuration from the map returned by
+//    genBootImageConfigs() using the image_name specified in the boot_image module.
 // =================================================================================================
 
 // This comment describes:
@@ -385,22 +392,6 @@
 	dexpreoptConfigForMake android.WritablePath
 }
 
-// Accessor function for the apex package. Returns nil if dexpreopt is disabled.
-func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths {
-	if SkipDexpreoptBootJars(ctx) {
-		return nil
-	}
-	// Include dexpreopt files for the primary boot image.
-	files := map[android.ArchType]android.OutputPaths{}
-	for _, variant := range artBootImageConfig(ctx).variants {
-		// We also generate boot images for host (for testing), but we don't need those in the apex.
-		if variant.target.Os == android.Android {
-			files[variant.target.Arch.ArchType] = variant.imagesDeps
-		}
-	}
-	return files
-}
-
 // Provide paths to boot images for use by modules that depend upon them.
 //
 // The build rules are created in GenerateSingletonBuildActions().
@@ -479,7 +470,7 @@
 			// A platform variant is required but this is for an apex so ignore it.
 			return -1, nil
 		}
-	} else if !android.InList(requiredApex, apexInfo.InApexes) {
+	} else if !apexInfo.InApexByBaseName(requiredApex) {
 		// An apex variant for a specific apex is required but this is the wrong apex.
 		return -1, nil
 	}
@@ -489,7 +480,7 @@
 
 	switch image.name {
 	case artBootImageName:
-		if len(apexInfo.InApexes) > 0 && allHavePrefix(apexInfo.InApexes, "com.android.art") {
+		if apexInfo.InApexByBaseName("com.android.art") || apexInfo.InApexByBaseName("com.android.art.debug") || apexInfo.InApexByBaseName("com.android.art,testing") {
 			// ok: found the jar in the ART apex
 		} else if name == "jacocoagent" && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
 			// exception (skip and continue): Jacoco platform variant for a coverage build
@@ -516,21 +507,17 @@
 	return index, jar.DexJarBuildPath()
 }
 
-func allHavePrefix(list []string, prefix string) bool {
-	for _, s := range list {
-		if s != prefix && !strings.HasPrefix(s, prefix+".") {
-			return false
-		}
-	}
-	return true
-}
-
 // buildBootImage takes a bootImageConfig, creates rules to build it, and returns the image.
 func buildBootImage(ctx android.SingletonContext, image *bootImageConfig) *bootImageConfig {
 	// Collect dex jar paths for the boot image modules.
 	// This logic is tested in the apex package to avoid import cycle apex <-> java.
 	bootDexJars := make(android.Paths, image.modules.Len())
+
 	ctx.VisitAllModules(func(module android.Module) {
+		if !isActiveModule(module) {
+			return
+		}
+
 		if i, j := getBootImageJar(ctx, image, module); i != -1 {
 			if existing := bootDexJars[i]; existing != nil {
 				ctx.Errorf("Multiple dex jars found for %s:%s - %s and %s",
@@ -860,6 +847,9 @@
 		// Collect `permitted_packages` for updatable boot jars.
 		var updatablePackages []string
 		ctx.VisitAllModules(func(module android.Module) {
+			if !isActiveModule(module) {
+				return
+			}
 			if j, ok := module.(PermittedPackagesForUpdatableBootJars); ok {
 				name := ctx.ModuleName(module)
 				if i := android.IndexList(name, updatableModules); i != -1 {
diff --git a/java/dexpreopt_test.go b/java/dexpreopt_test.go
index 5550a4c..a9e0773 100644
--- a/java/dexpreopt_test.go
+++ b/java/dexpreopt_test.go
@@ -148,7 +148,7 @@
 		t.Run(test.name, func(t *testing.T) {
 			ctx, _ := testJava(t, test.bp)
 
-			dexpreopt := ctx.ModuleForTests("foo", "android_common").MaybeDescription("dexpreopt")
+			dexpreopt := ctx.ModuleForTests("foo", "android_common").MaybeRule("dexpreopt")
 			enabled := dexpreopt.Rule != nil
 
 			if enabled != test.enabled {
diff --git a/java/hiddenapi.go b/java/hiddenapi.go
index 71f1e57..2cd025e 100644
--- a/java/hiddenapi.go
+++ b/java/hiddenapi.go
@@ -28,10 +28,40 @@
 }, "outFlag", "stubAPIFlags")
 
 type hiddenAPI struct {
-	bootDexJarPath  android.Path
-	flagsCSVPath    android.Path
-	indexCSVPath    android.Path
+	// The path to the dex jar that is in the boot class path. If this is nil then the associated
+	// module is not a boot jar, but could be one of the <x>-hiddenapi modules that provide additional
+	// annotations for the <x> boot dex jar but which do not actually provide a boot dex jar
+	// themselves.
+	bootDexJarPath android.Path
+
+	// The path to the CSV file that contains mappings from Java signature to various flags derived
+	// from annotations in the source, e.g. whether it is public or the sdk version above which it
+	// can no longer be used.
+	//
+	// It is created by the Class2NonSdkList tool which processes the .class files in the class
+	// implementation jar looking for UnsupportedAppUsage and CovariantReturnType annotations. The
+	// tool also consumes the hiddenAPISingletonPathsStruct.stubFlags file in order to perform
+	// consistency checks on the information in the annotations and to filter out bridge methods
+	// that are already part of the public API.
+	flagsCSVPath android.Path
+
+	// The path to the CSV file that contains mappings from Java signature to the value of properties
+	// specified on UnsupportedAppUsage annotations in the source.
+	//
+	// Like the flagsCSVPath file this is also created by the Class2NonSdkList in the same way.
+	// Although the two files could potentially be created in a single invocation of the
+	// Class2NonSdkList at the moment they are created using their own invocation, with the behavior
+	// being determined by the property that is used.
 	metadataCSVPath android.Path
+
+	// The path to the CSV file that contains mappings from Java signature to source location
+	// information.
+	//
+	// It is created by the merge_csv tool which processes the class implementation jar, extracting
+	// all the files ending in .uau (which are CSV files) and merges them together. The .uau files are
+	// created by the unsupported app usage annotation processor during compilation of the class
+	// implementation jar.
+	indexCSVPath android.Path
 }
 
 func (h *hiddenAPI) flagsCSV() android.Path {
@@ -78,11 +108,9 @@
 		// not on the list then that will cause failures in the CtsHiddenApiBlacklist...
 		// tests.
 		if inList(bootJarName, ctx.Config().BootJars()) {
-			// Derive the greylist from classes jar.
-			flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
-			metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
-			indexCSV := android.PathForModuleOut(ctx, "hiddenapi", "index.csv")
-			h.hiddenAPIGenerateCSV(ctx, flagsCSV, metadataCSV, indexCSV, implementationJar)
+			// Create ninja rules to generate various CSV files needed by hiddenapi and store the paths
+			// in the hiddenAPI structure.
+			h.hiddenAPIGenerateCSV(ctx, implementationJar)
 
 			// If this module is actually on the boot jars list and not providing
 			// hiddenapi information for a module on the boot jars list then encode
@@ -106,9 +134,10 @@
 	return dexJar
 }
 
-func (h *hiddenAPI) hiddenAPIGenerateCSV(ctx android.ModuleContext, flagsCSV, metadataCSV, indexCSV android.WritablePath, classesJar android.Path) {
+func (h *hiddenAPI) hiddenAPIGenerateCSV(ctx android.ModuleContext, classesJar android.Path) {
 	stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
 
+	flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
 	ctx.Build(pctx, android.BuildParams{
 		Rule:        hiddenAPIGenerateCSVRule,
 		Description: "hiddenapi flags",
@@ -122,6 +151,7 @@
 	})
 	h.flagsCSVPath = flagsCSV
 
+	metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
 	ctx.Build(pctx, android.BuildParams{
 		Rule:        hiddenAPIGenerateCSVRule,
 		Description: "hiddenapi metadata",
@@ -135,6 +165,7 @@
 	})
 	h.metadataCSVPath = metadataCSV
 
+	indexCSV := android.PathForModuleOut(ctx, "hiddenapi", "index.csv")
 	rule := android.NewRuleBuilder(pctx, ctx)
 	rule.Command().
 		BuiltTool("merge_csv").
diff --git a/java/hiddenapi_singleton.go b/java/hiddenapi_singleton.go
index 4bd255c..ccb8745 100644
--- a/java/hiddenapi_singleton.go
+++ b/java/hiddenapi_singleton.go
@@ -28,9 +28,64 @@
 }
 
 type hiddenAPISingletonPathsStruct struct {
-	flags     android.OutputPath
-	index     android.OutputPath
-	metadata  android.OutputPath
+	// The path to the CSV file that contains the flags that will be encoded into the dex boot jars.
+	//
+	// It is created by the generate_hiddenapi_lists.py tool that is passed the stubFlags along with
+	// a number of additional files that are used to augment the information in the stubFlags with
+	// manually curated data.
+	flags android.OutputPath
+
+	// The path to the CSV index file that contains mappings from Java signature to source location
+	// information for all Java elements annotated with the UnsupportedAppUsage annotation in the
+	// source of all the boot jars.
+	//
+	// It is created by the merge_csv tool which merges all the hiddenAPI.indexCSVPath files that have
+	// been created by the rest of the build. That includes the index files generated for
+	// <x>-hiddenapi modules.
+	index android.OutputPath
+
+	// The path to the CSV metadata file that contains mappings from Java signature to the value of
+	// properties specified on UnsupportedAppUsage annotations in the source of all the boot jars.
+	//
+	// It is created by the merge_csv tool which merges all the hiddenAPI.metadataCSVPath files that
+	// have been created by the rest of the build. That includes the metadata files generated for
+	// <x>-hiddenapi modules.
+	metadata android.OutputPath
+
+	// The path to the CSV metadata file that contains mappings from Java signature to flags obtained
+	// from the public, system and test API stubs.
+	//
+	// This is created by the hiddenapi tool which is given dex files for the public, system and test
+	// API stubs (including product specific stubs) along with dex boot jars, so does not include
+	// <x>-hiddenapi modules. For each API surface (i.e. public, system, test) it records which
+	// members in the dex boot jars match a member in the dex stub jars for that API surface and then
+	// outputs a file containing the signatures of all members in the dex boot jars along with the
+	// flags that indicate which API surface it belongs, if any.
+	//
+	// e.g. a dex member that matches a member in the public dex stubs would have flags
+	// "public-api,system-api,test-api" set (as system and test are both supersets of public). A dex
+	// member that didn't match a member in any of the dex stubs is still output it just has an empty
+	// set of flags.
+	//
+	// The notion of matching is quite complex, it is not restricted to just exact matching but also
+	// follows the Java inheritance rules. e.g. if a method is public then all overriding/implementing
+	// methods are also public. If an interface method is public and a class inherits an
+	// implementation of that method from a super class then that super class method is also public.
+	// That ensures that any method that can be called directly by an App through a public method is
+	// visible to that App.
+	//
+	// Propagating the visibility of members across the inheritance hierarchy at build time will cause
+	// problems when modularizing and unbundling as it that propagation can cross module boundaries.
+	// e.g. Say that a private framework class implements a public interface and inherits an
+	// implementation of one of its methods from a core platform ART class. In that case the ART
+	// implementation method needs to be marked as public which requires the build to have access to
+	// the framework implementation classes at build time. The work to rectify this is being tracked
+	// at http://b/178693149.
+	//
+	// This file (or at least those items marked as being in the public-api) is used by hiddenapi when
+	// creating the metadata and flags for the individual modules in order to perform consistency
+	// checks and filter out bridge methods that are part of the public API. The latter relies on the
+	// propagation of visibility across the inheritance hierarchy.
 	stubFlags android.OutputPath
 }
 
diff --git a/java/prebuilt_apis.go b/java/prebuilt_apis.go
index 0ffbaaa..1e90149 100644
--- a/java/prebuilt_apis.go
+++ b/java/prebuilt_apis.go
@@ -15,12 +15,14 @@
 package java
 
 import (
+	"fmt"
 	"strconv"
 	"strings"
 
 	"github.com/google/blueprint/proptools"
 
 	"android/soong/android"
+	"android/soong/genrule"
 )
 
 func init() {
@@ -35,6 +37,12 @@
 	// list of api version directories
 	Api_dirs []string
 
+	// The next API directory can optionally point to a directory where
+	// files incompatibility-tracking files are stored for the current
+	// "in progress" API. Each module present in one of the api_dirs will have
+	// a <module>-incompatibilities.api.<scope>.latest module created.
+	Next_api_dir *string
+
 	// The sdk_version of java_import modules generated based on jar files.
 	// Defaults to "current"
 	Imports_sdk_version *string
@@ -98,28 +106,46 @@
 	mctx.CreateModule(ImportFactory, &props)
 }
 
-func createFilegroup(mctx android.LoadHookContext, module string, scope string, apiver string, path string) {
-	fgName := module + ".api." + scope + "." + apiver
+func createFilegroup(mctx android.LoadHookContext, name string, path string) {
 	filegroupProps := struct {
 		Name *string
 		Srcs []string
 	}{}
-	filegroupProps.Name = proptools.StringPtr(fgName)
+	filegroupProps.Name = proptools.StringPtr(name)
 	filegroupProps.Srcs = []string{path}
 	mctx.CreateModule(android.FileGroupFactory, &filegroupProps)
 }
 
+func createEmptyFile(mctx android.LoadHookContext, name string) {
+	props := struct {
+		Name *string
+		Cmd  *string
+		Out  []string
+	}{}
+	props.Name = proptools.StringPtr(name)
+	props.Out = []string{name}
+	props.Cmd = proptools.StringPtr("touch $(genDir)/" + name)
+	mctx.CreateModule(genrule.GenRuleFactory, &props)
+}
+
 func getPrebuiltFiles(mctx android.LoadHookContext, p *prebuiltApis, name string) []string {
-	mydir := mctx.ModuleDir() + "/"
 	var files []string
 	for _, apiver := range p.properties.Api_dirs {
-		for _, scope := range []string{"public", "system", "test", "core", "module-lib", "system-server"} {
-			vfiles, err := mctx.GlobWithDeps(mydir+apiver+"/"+scope+"/"+name, nil)
-			if err != nil {
-				mctx.ModuleErrorf("failed to glob %s files under %q: %s", name, mydir+apiver+"/"+scope, err)
-			}
-			files = append(files, vfiles...)
+		files = append(files, getPrebuiltFilesInSubdir(mctx, apiver, name)...)
+	}
+	return files
+}
+
+func getPrebuiltFilesInSubdir(mctx android.LoadHookContext, subdir string, name string) []string {
+	var files []string
+	dir := mctx.ModuleDir() + "/" + subdir
+	for _, scope := range []string{"public", "system", "test", "core", "module-lib", "system-server"} {
+		glob := fmt.Sprintf("%s/%s/%s", dir, scope, name)
+		vfiles, err := mctx.GlobWithDeps(glob, nil)
+		if err != nil {
+			mctx.ModuleErrorf("failed to glob %s files under %q: %s", name, dir+"/"+scope, err)
 		}
+		files = append(files, vfiles...)
 	}
 	return files
 }
@@ -181,11 +207,14 @@
 
 	// Create filegroups for all (<module>, <scope, <version>) triplets,
 	// and a "latest" filegroup variant for each (<module>, <scope>) pair
+	moduleName := func(module, scope, version string) string {
+		return module + ".api." + scope + "." + version
+	}
 	m := make(map[string]latestApiInfo)
 	for _, f := range files {
 		localPath := strings.TrimPrefix(f, mydir)
 		module, apiver, scope := parseApiFilePath(mctx, localPath)
-		createFilegroup(mctx, module, scope, apiver, localPath)
+		createFilegroup(mctx, moduleName(module, scope, apiver), localPath)
 
 		version, err := strconv.Atoi(apiver)
 		if err != nil {
@@ -193,20 +222,52 @@
 			return
 		}
 
-		key := module + "." + scope
-		info, ok := m[key]
-		if !ok {
-			m[key] = latestApiInfo{module, scope, version, localPath}
-		} else if version > info.version {
-			info.version = version
-			info.path = localPath
-			m[key] = info
+		// Track latest version of each module/scope, except for incompatibilities
+		if !strings.HasSuffix(module, "incompatibilities") {
+			key := module + "." + scope
+			info, ok := m[key]
+			if !ok {
+				m[key] = latestApiInfo{module, scope, version, localPath}
+			} else if version > info.version {
+				info.version = version
+				info.path = localPath
+				m[key] = info
+			}
 		}
 	}
+
 	// Sort the keys in order to make build.ninja stable
 	for _, k := range android.SortedStringKeys(m) {
 		info := m[k]
-		createFilegroup(mctx, info.module, info.scope, "latest", info.path)
+		name := moduleName(info.module, info.scope, "latest")
+		createFilegroup(mctx, name, info.path)
+	}
+
+	// Create incompatibilities tracking files for all modules, if we have a "next" api.
+	if nextApiDir := String(p.properties.Next_api_dir); nextApiDir != "" {
+		files := getPrebuiltFilesInSubdir(mctx, nextApiDir, "api/*incompatibilities.txt")
+		incompatibilities := make(map[string]bool)
+		for _, f := range files {
+			localPath := strings.TrimPrefix(f, mydir)
+			module, _, scope := parseApiFilePath(mctx, localPath)
+
+			// Figure out which module is referenced by this file. Special case for "android".
+			referencedModule := strings.TrimSuffix(module, "incompatibilities")
+			referencedModule = strings.TrimSuffix(referencedModule, "-")
+			if referencedModule == "" {
+				referencedModule = "android"
+			}
+
+			createFilegroup(mctx, moduleName(referencedModule+"-incompatibilities", scope, "latest"), localPath)
+
+			incompatibilities[referencedModule+"."+scope] = true
+		}
+		// Create empty incompatibilities files for remaining modules
+		for _, k := range android.SortedStringKeys(m) {
+			if _, ok := incompatibilities[k]; !ok {
+				createEmptyFile(mctx, moduleName(m[k].module+"-incompatibilities", m[k].scope, "latest"))
+			}
+		}
 	}
 }
 
diff --git a/java/rro.go b/java/rro.go
index 98cd379..aafa88e 100644
--- a/java/rro.go
+++ b/java/rro.go
@@ -55,7 +55,11 @@
 	// only when the ro.boot.vendor.overlay.theme system property is set to the same value.
 	Theme *string
 
-	// if not blank, set to the version of the sdk to compile against.
+	// If not blank, set to the version of the sdk to compile against. This
+	// can be either an API version (e.g. "29" for API level 29 AKA Android 10)
+	// or special subsets of the current platform, for example "none", "current",
+	// "core", "system", "test". See build/soong/java/sdk.go for the full and
+	// up-to-date list of possible values.
 	// Defaults to compiling against the current platform.
 	Sdk_version *string
 
diff --git a/java/testing.go b/java/testing.go
index 0b1f2d1..5fcf84c 100644
--- a/java/testing.go
+++ b/java/testing.go
@@ -107,6 +107,7 @@
 	RegisterAppBuildComponents(ctx)
 	RegisterAppImportBuildComponents(ctx)
 	RegisterAppSetBuildComponents(ctx)
+	RegisterBootImageBuildComponents(ctx)
 	RegisterDexpreoptBootJarsComponents(ctx)
 	RegisterDocsBuildComponents(ctx)
 	RegisterGenRuleBuildComponents(ctx)
diff --git a/licenses/Android.bp b/licenses/Android.bp
index d7fac90..f4a76d7 100644
--- a/licenses/Android.bp
+++ b/licenses/Android.bp
@@ -125,6 +125,22 @@
 }
 
 license_kind {
+    name: "SPDX-license-identifier-APSL-1.1",
+    conditions: [
+        "reciprocal",
+    ],
+    url: "https://spdx.org/licenses/APSL-1.1.html",
+}
+
+license_kind {
+    name: "SPDX-license-identifier-APSL-2.0",
+    conditions: [
+        "reciprocal",
+    ],
+    url: "https://spdx.org/licenses/APSL-2.0.html",
+}
+
+license_kind {
     name: "SPDX-license-identifier-Apache",
     conditions: ["notice"],
 }
diff --git a/rust/androidmk.go b/rust/androidmk.go
index 0307727..1a286f7 100644
--- a/rust/androidmk.go
+++ b/rust/androidmk.go
@@ -18,6 +18,7 @@
 	"path/filepath"
 
 	"android/soong/android"
+	"android/soong/cc"
 )
 
 type AndroidMkContext interface {
@@ -85,7 +86,8 @@
 }
 
 func (test *testDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
-	test.binaryDecorator.AndroidMk(ctx, ret)
+	ctx.SubAndroidMk(ret, test.binaryDecorator)
+
 	ret.Class = "NATIVE_TESTS"
 	ret.ExtraEntries = append(ret.ExtraEntries, func(entries *android.AndroidMkEntries) {
 		entries.AddCompatibilityTestSuites(test.Properties.Test_suites...)
@@ -95,7 +97,8 @@
 		entries.SetBoolIfTrue("LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG", !BoolDefault(test.Properties.Auto_gen_config, true))
 		entries.SetBoolIfTrue("LOCAL_IS_UNIT_TEST", Bool(test.Properties.Test_options.Unit_test))
 	})
-	// TODO(chh): add test data with androidMkWriteTestData(test.data, ctx, ret)
+
+	cc.AndroidMkWriteTestData(test.data, ret)
 }
 
 func (library *libraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
diff --git a/rust/config/allowed_list.go b/rust/config/allowed_list.go
index 21df024..fc11d29 100644
--- a/rust/config/allowed_list.go
+++ b/rust/config/allowed_list.go
@@ -19,6 +19,7 @@
 		"prebuilts/rust",
 		"system/bt",
 		"system/extras/profcollectd",
+		"system/extras/simpleperf",
 		"system/hardware/interfaces/keystore2",
 		"system/security",
 		"system/tools/aidl",
diff --git a/rust/config/global.go b/rust/config/global.go
index fb62278..182dc6a 100644
--- a/rust/config/global.go
+++ b/rust/config/global.go
@@ -47,6 +47,8 @@
 		"-C debuginfo=2",
 		"-C opt-level=3",
 		"-C relocation-model=pic",
+		// Use v0 mangling to distinguish from C++ symbols
+		"-Z symbol-mangling-version=v0",
 	}
 
 	deviceGlobalRustFlags = []string{
diff --git a/rust/rust_test.go b/rust/rust_test.go
index fc7f47e..abc9af9 100644
--- a/rust/rust_test.go
+++ b/rust/rust_test.go
@@ -116,6 +116,7 @@
 		"foo.proto":       nil,
 		"liby.so":         nil,
 		"libz.so":         nil,
+		"data.txt":        nil,
 	}
 }
 
diff --git a/rust/test.go b/rust/test.go
index 35e04ff..92b4860 100644
--- a/rust/test.go
+++ b/rust/test.go
@@ -43,6 +43,10 @@
 	// installed into.
 	Test_suites []string `android:"arch_variant"`
 
+	// list of files or filegroup modules that provide data that should be installed alongside
+	// the test
+	Data []string `android:"path,arch_variant"`
+
 	// Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
 	// doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
 	// explicitly.
@@ -62,6 +66,12 @@
 	*binaryDecorator
 	Properties TestProperties
 	testConfig android.Path
+
+	data []android.DataPath
+}
+
+func (test *testDecorator) dataPaths() []android.DataPath {
+	return test.data
 }
 
 func (test *testDecorator) nativeCoverage() bool {
@@ -89,7 +99,6 @@
 	}
 
 	module.compiler = test
-	module.AddProperties(&test.Properties)
 	return module, test
 }
 
@@ -105,6 +114,12 @@
 		nil,
 		test.Properties.Auto_gen_config)
 
+	dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
+
+	for _, dataSrcPath := range dataSrcPaths {
+		test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
+	}
+
 	// default relative install path is module name
 	if !Bool(test.Properties.No_named_install_directory) {
 		test.baseCompiler.relative = ctx.ModuleName()
diff --git a/rust/test_test.go b/rust/test_test.go
index fea2ad0..892761a 100644
--- a/rust/test_test.go
+++ b/rust/test_test.go
@@ -26,6 +26,7 @@
 		rust_test_host {
 			name: "my_test",
 			srcs: ["foo.rs"],
+			data: ["data.txt"],
 		}`)
 
 	testingModule := ctx.ModuleForTests("my_test", "linux_glibc_x86_64")
@@ -34,6 +35,12 @@
 	if !strings.Contains(outPath, expectedOut) {
 		t.Errorf("wrong output path: %v;  expected: %v", outPath, expectedOut)
 	}
+
+	dataPaths := testingModule.Module().(*Module).compiler.(*testDecorator).dataPaths()
+	if len(dataPaths) != 1 {
+		t.Errorf("expected exactly one test data file. test data files: [%s]", dataPaths)
+		return
+	}
 }
 
 func TestRustTestLinkage(t *testing.T) {
diff --git a/ui/build/build.go b/ui/build/build.go
index 926da31..215a6c8 100644
--- a/ui/build/build.go
+++ b/ui/build/build.go
@@ -252,6 +252,11 @@
 	if what&BuildSoong != 0 {
 		// Run Soong
 		runSoong(ctx, config)
+
+		if config.Environment().IsEnvTrue("GENERATE_BAZEL_FILES") {
+			// Return early, if we're using Soong as the bp2build converter.
+			return
+		}
 	}
 
 	if what&BuildKati != 0 {
diff --git a/ui/build/soong.go b/ui/build/soong.go
index 6a93672..899ab5d 100644
--- a/ui/build/soong.go
+++ b/ui/build/soong.go
@@ -169,8 +169,11 @@
 	// This build generates <builddir>/build.ninja, which is used later by build/soong/ui/build/build.go#Build().
 	ninja("bootstrap", ".bootstrap/build.ninja")
 
-	soongBuildMetrics := loadSoongBuildMetrics(ctx, config)
-	logSoongBuildMetrics(ctx, soongBuildMetrics)
+	var soongBuildMetrics *soong_metrics_proto.SoongBuildMetrics
+	if shouldCollectBuildSoongMetrics(config) {
+		soongBuildMetrics := loadSoongBuildMetrics(ctx, config)
+		logSoongBuildMetrics(ctx, soongBuildMetrics)
+	}
 
 	distGzipFile(ctx, config, config.SoongNinjaFile(), "soong")
 
@@ -179,11 +182,16 @@
 		distGzipFile(ctx, config, config.SoongMakeVarsMk(), "soong")
 	}
 
-	if ctx.Metrics != nil {
+	if shouldCollectBuildSoongMetrics(config) && ctx.Metrics != nil {
 		ctx.Metrics.SetSoongBuildMetrics(soongBuildMetrics)
 	}
 }
 
+func shouldCollectBuildSoongMetrics(config Config) bool {
+	// Do not collect metrics protobuf if the soong_build binary ran as the bp2build converter.
+	return config.Environment().IsFalse("GENERATE_BAZEL_FILES")
+}
+
 func loadSoongBuildMetrics(ctx Context, config Config) *soong_metrics_proto.SoongBuildMetrics {
 	soongBuildMetricsFile := filepath.Join(config.OutDir(), "soong", "soong_build_metrics.pb")
 	buf, err := ioutil.ReadFile(soongBuildMetricsFile)