Merge "Make bpf implement SourceFileProducer."
diff --git a/Android.bp b/Android.bp
index 9711c11..37be36c 100644
--- a/Android.bp
+++ b/Android.bp
@@ -224,6 +224,7 @@
         "soong",
         "soong-android",
         "soong-cc",
+        "soong-dexpreopt",
         "soong-genrule",
         "soong-java-config",
         "soong-tradefed",
@@ -238,6 +239,7 @@
         "java/app.go",
         "java/builder.go",
         "java/dex.go",
+        "java/dexpreopt.go",
         "java/droiddoc.go",
         "java/gen.go",
         "java/genrule.go",
@@ -353,6 +355,9 @@
         "apex/apex.go",
         "apex/key.go",
     ],
+    testSrcs: [
+        "apex/apex_test.go",
+    ],
     pluginFor: ["soong_build"],
 }
 
diff --git a/OWNERS b/OWNERS
index 780a35b..d56644b 100644
--- a/OWNERS
+++ b/OWNERS
@@ -1,4 +1,6 @@
-per-file * = ccross@android.com,dwillemsen@google.com,nanzhang@google.com
+per-file * = ccross@android.com,dwillemsen@google.com,nanzhang@google.com,jungjw@google.com
 per-file ndk_*.go, *gen_stub_libs.py = danalbert@google.com
-per-file clang.go,global.go,tidy.go = srhines@google.com, chh@google.com
+per-file clang.go,global.go = srhines@google.com, chh@google.com, pirama@google.com, yikong@google.com
+per-file tidy.go = srhines@google.com, chh@google.com
+per-file lto.go,pgo.go = srhines@google.com, pirama@google.com, yikong@google.com
 per-file apex.go = jiyong@google.com
diff --git a/android/apex.go b/android/apex.go
index dae88ce..8d99d56 100644
--- a/android/apex.go
+++ b/android/apex.go
@@ -14,6 +14,12 @@
 
 package android
 
+import (
+	"sync"
+
+	"github.com/google/blueprint"
+)
+
 // ApexModule is the interface that a module type is expected to implement if
 // the module has to be built differently depending on whether the module
 // is destined for an apex or not (installed to one of the regular partitions).
@@ -23,7 +29,7 @@
 // or C APIs from other APEXs.
 //
 // A module implementing this interface will be mutated into multiple
-// variations by the apex mutator if it is directly or indirectly included
+// variations by apex.apexMutator if it is directly or indirectly included
 // in one or more APEXs. Specifically, if a module is included in apex.foo and
 // apex.bar then three apex variants are created: platform, apex.foo and
 // apex.bar. The platform variant is for the regular partitions
@@ -33,31 +39,44 @@
 	Module
 	apexModuleBase() *ApexModuleBase
 
-	// Marks that this module should be built for the APEX of the specified name
+	// Marks that this module should be built for the APEX of the specified name.
+	// Call this before apex.apexMutator is run.
 	BuildForApex(apexName string)
 
-	// Tests whether this module will be built for the platform or not (= APEXs)
-	IsForPlatform() bool
-
 	// Returns the name of APEX that this module will be built for. Empty string
 	// is returned when 'IsForPlatform() == true'. Note that a module can be
-	// included to multiple APEXs, in which case, the module is mutated into
+	// included in multiple APEXes, in which case, the module is mutated into
 	// multiple modules each of which for an APEX. This method returns the
 	// name of the APEX that a variant module is for.
+	// Call this after apex.apexMutator is run.
 	ApexName() string
 
-	// Tests if this module can have APEX variants. APEX variants are
+	// Tests whether this module will be built for the platform or not.
+	// This is a shortcut for ApexName() == ""
+	IsForPlatform() bool
+
+	// Tests if this module could have APEX variants. APEX variants are
 	// created only for the modules that returns true here. This is useful
-	// for not creating APEX variants for shared libraries such as NDK stubs.
+	// for not creating APEX variants for certain types of shared libraries
+	// such as NDK stubs.
 	CanHaveApexVariants() bool
 
 	// Tests if this module can be installed to APEX as a file. For example,
 	// this would return true for shared libs while return false for static
 	// libs.
 	IsInstallableToApex() bool
+
+	// Mutate this module into one or more variants each of which is built
+	// for an APEX marked via BuildForApex().
+	CreateApexVariations(mctx BottomUpMutatorContext) []blueprint.Module
+
+	// Sets the name of the apex variant of this module. Called inside
+	// CreateApexVariations.
+	setApexName(apexName string)
 }
 
 type ApexProperties struct {
+	// Name of the apex variant that this module is mutated into
 	ApexName string `blueprint:"mutated"`
 }
 
@@ -67,6 +86,7 @@
 	ApexProperties ApexProperties
 
 	canHaveApexVariants bool
+	apexVariations      []string
 }
 
 func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
@@ -74,15 +94,21 @@
 }
 
 func (m *ApexModuleBase) BuildForApex(apexName string) {
-	m.ApexProperties.ApexName = apexName
+	if !InList(apexName, m.apexVariations) {
+		m.apexVariations = append(m.apexVariations, apexName)
+	}
+}
+
+func (m *ApexModuleBase) ApexName() string {
+	return m.ApexProperties.ApexName
 }
 
 func (m *ApexModuleBase) IsForPlatform() bool {
 	return m.ApexProperties.ApexName == ""
 }
 
-func (m *ApexModuleBase) ApexName() string {
-	return m.ApexProperties.ApexName
+func (m *ApexModuleBase) setApexName(apexName string) {
+	m.ApexProperties.ApexName = apexName
 }
 
 func (m *ApexModuleBase) CanHaveApexVariants() bool {
@@ -94,6 +120,99 @@
 	return false
 }
 
+func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []blueprint.Module {
+	if len(m.apexVariations) > 0 {
+		variations := []string{""} // Original variation for platform
+		variations = append(variations, m.apexVariations...)
+
+		modules := mctx.CreateVariations(variations...)
+		for i, m := range modules {
+			if i == 0 {
+				continue
+			}
+			m.(ApexModule).setApexName(variations[i])
+		}
+		return modules
+	}
+	return nil
+}
+
+var apexData OncePer
+var apexNamesMapMutex sync.Mutex
+
+// This structure maintains the global mapping in between modules and APEXes.
+// Examples:
+//
+// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
+// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
+// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
+func apexNamesMap() map[string]map[string]bool {
+	return apexData.Once("apexNames", func() interface{} {
+		return make(map[string]map[string]bool)
+	}).(map[string]map[string]bool)
+}
+
+// Update the map to mark that a module named moduleName is directly or indirectly
+// depended on by an APEX named apexName. Directly depending means that a module
+// is explicitly listed in the build definition of the APEX via properties like
+// native_shared_libs, java_libs, etc.
+func UpdateApexDependency(apexName string, moduleName string, directDep bool) {
+	apexNamesMapMutex.Lock()
+	defer apexNamesMapMutex.Unlock()
+	apexNames, ok := apexNamesMap()[moduleName]
+	if !ok {
+		apexNames = make(map[string]bool)
+		apexNamesMap()[moduleName] = apexNames
+	}
+	apexNames[apexName] = apexNames[apexName] || directDep
+}
+
+// Tests whether a module named moduleName is directly depended on by an APEX
+// named apexName.
+func DirectlyInApex(apexName string, moduleName string) bool {
+	apexNamesMapMutex.Lock()
+	defer apexNamesMapMutex.Unlock()
+	if apexNames, ok := apexNamesMap()[moduleName]; ok {
+		return apexNames[apexName]
+	}
+	return false
+}
+
+// Tests whether a module named moduleName is directly depended on by any APEX.
+func DirectlyInAnyApex(moduleName string) bool {
+	apexNamesMapMutex.Lock()
+	defer apexNamesMapMutex.Unlock()
+	if apexNames, ok := apexNamesMap()[moduleName]; ok {
+		for an := range apexNames {
+			if apexNames[an] {
+				return true
+			}
+		}
+	}
+	return false
+}
+
+// Tests whether a module named module is depended on (including both
+// direct and indirect dependencies) by any APEX.
+func InAnyApex(moduleName string) bool {
+	apexNamesMapMutex.Lock()
+	defer apexNamesMapMutex.Unlock()
+	apexNames, ok := apexNamesMap()[moduleName]
+	return ok && len(apexNames) > 0
+}
+
+func GetApexesForModule(moduleName string) []string {
+	ret := []string{}
+	apexNamesMapMutex.Lock()
+	defer apexNamesMapMutex.Unlock()
+	if apexNames, ok := apexNamesMap()[moduleName]; ok {
+		for an := range apexNames {
+			ret = append(ret, an)
+		}
+	}
+	return ret
+}
+
 func InitApexModule(m ApexModule) {
 	base := m.apexModuleBase()
 	base.canHaveApexVariants = true
diff --git a/android/arch.go b/android/arch.go
index 7fe1b18..bee09b0 100644
--- a/android/arch.go
+++ b/android/arch.go
@@ -1080,20 +1080,20 @@
 		{"arm", "armv7-a-neon", "cortex-a73", []string{"armeabi-v7a"}},
 		{"arm", "armv7-a-neon", "cortex-a75", []string{"armeabi-v7a"}},
 		{"arm", "armv7-a-neon", "cortex-a76", []string{"armeabi-v7a"}},
-		{"arm", "armv7-a-neon", "denver", []string{"armeabi-v7a"}},
 		{"arm", "armv7-a-neon", "krait", []string{"armeabi-v7a"}},
 		{"arm", "armv7-a-neon", "kryo", []string{"armeabi-v7a"}},
+		{"arm", "armv7-a-neon", "kryo385", []string{"armeabi-v7a"}},
 		{"arm", "armv7-a-neon", "exynos-m1", []string{"armeabi-v7a"}},
 		{"arm", "armv7-a-neon", "exynos-m2", []string{"armeabi-v7a"}},
 		{"arm64", "armv8-a", "cortex-a53", []string{"arm64-v8a"}},
 		{"arm64", "armv8-a", "cortex-a72", []string{"arm64-v8a"}},
 		{"arm64", "armv8-a", "cortex-a73", []string{"arm64-v8a"}},
-		{"arm64", "armv8-a", "denver64", []string{"arm64-v8a"}},
 		{"arm64", "armv8-a", "kryo", []string{"arm64-v8a"}},
 		{"arm64", "armv8-a", "exynos-m1", []string{"arm64-v8a"}},
 		{"arm64", "armv8-a", "exynos-m2", []string{"arm64-v8a"}},
 		{"arm64", "armv8-2a", "cortex-a75", []string{"arm64-v8a"}},
 		{"arm64", "armv8-2a", "cortex-a76", []string{"arm64-v8a"}},
+		{"arm64", "armv8-2a", "kryo385", []string{"arm64-v8a"}},
 		{"mips", "mips32-fp", "", []string{"mips"}},
 		{"mips", "mips32r2-fp", "", []string{"mips"}},
 		{"mips", "mips32r2-fp-xburst", "", []string{"mips"}},
diff --git a/android/config.go b/android/config.go
index c737b70..f046318 100644
--- a/android/config.go
+++ b/android/config.go
@@ -569,6 +569,10 @@
 	return Bool(c.productVariables.Unbundled_build)
 }
 
+func (c *config) UnbundledBuildPrebuiltSdks() bool {
+	return Bool(c.productVariables.Unbundled_build) && !Bool(c.productVariables.Unbundled_build_sdks_from_source)
+}
+
 func (c *config) IsPdkBuild() bool {
 	return Bool(c.productVariables.Pdk)
 }
@@ -581,6 +585,10 @@
 	return Bool(c.productVariables.Debuggable)
 }
 
+func (c *config) Eng() bool {
+	return Bool(c.productVariables.Eng)
+}
+
 func (c *config) DevicePrefer32BitApps() bool {
 	return Bool(c.productVariables.DevicePrefer32BitApps)
 }
@@ -728,14 +736,18 @@
 	return c.productVariables.ModulesLoadedByPrivilegedModules
 }
 
-func (c *config) DefaultStripDex() bool {
-	return Bool(c.productVariables.DefaultStripDex)
-}
-
 func (c *config) DisableDexPreopt(name string) bool {
 	return Bool(c.productVariables.DisableDexPreopt) || InList(name, c.productVariables.DisableDexPreoptModules)
 }
 
+func (c *config) DexpreoptGlobalConfig() string {
+	return String(c.productVariables.DexpreoptGlobalConfig)
+}
+
+func (c *config) DexPreoptProfileDir() string {
+	return String(c.productVariables.DexPreoptProfileDir)
+}
+
 func (c *deviceConfig) Arches() []Arch {
 	var arches []Arch
 	for _, target := range c.config.Targets[Android] {
@@ -850,6 +862,18 @@
 	return c.config.productVariables.BoardPlatPrivateSepolicyDirs
 }
 
+func (c *config) SecondArchIsTranslated() bool {
+	deviceTargets := c.Targets[Android]
+	if len(deviceTargets) < 2 {
+		return false
+	}
+
+	arch := deviceTargets[0].Arch
+
+	return (arch.ArchType == X86 || arch.ArchType == X86_64) &&
+		(hasArmAbi(arch) || hasArmAndroidArch(deviceTargets))
+}
+
 func (c *config) IntegerOverflowDisabledForPath(path string) bool {
 	if c.productVariables.IntegerOverflowExcludePaths == nil {
 		return false
@@ -900,6 +924,10 @@
 	return Bool(c.productVariables.Ndk_abis)
 }
 
+func (c *config) ExcludeDraftNdkApis() bool {
+	return Bool(c.productVariables.Exclude_draft_ndk_apis)
+}
+
 func (c *config) FlattenApex() bool {
 	return Bool(c.productVariables.FlattenApex)
 }
diff --git a/android/paths.go b/android/paths.go
index b22e3c7..13b31c7 100644
--- a/android/paths.go
+++ b/android/paths.go
@@ -659,11 +659,7 @@
 	if len(paths) == 0 {
 		return OptionalPath{}
 	}
-	relPath, err := filepath.Rel(p.config.srcDir, paths[0])
-	if err != nil {
-		reportPathError(ctx, err)
-		return OptionalPath{}
-	}
+	relPath := Rel(ctx, p.config.srcDir, paths[0])
 	return OptionalPathForPath(PathForSource(ctx, relPath))
 }
 
@@ -788,13 +784,7 @@
 
 func (p ModuleSrcPath) WithSubDir(ctx ModuleContext, subdir string) ModuleSrcPath {
 	subdir = PathForModuleSrc(ctx, subdir).String()
-	var err error
-	rel, err := filepath.Rel(subdir, p.path)
-	if err != nil {
-		ctx.ModuleErrorf("source file %q is not under path %q", p.path, subdir)
-		return p
-	}
-	p.rel = rel
+	p.rel = Rel(ctx, subdir, p.path)
 	return p
 }
 
@@ -932,27 +922,7 @@
 func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) OutputPath {
 	var outPaths []string
 	if ctx.Device() {
-		var partition string
-		if ctx.InstallInData() {
-			partition = "data"
-		} else if ctx.InstallInRecovery() {
-			// the layout of recovery partion is the same as that of system partition
-			partition = "recovery/root/system"
-		} else if ctx.SocSpecific() {
-			partition = ctx.DeviceConfig().VendorPath()
-		} else if ctx.DeviceSpecific() {
-			partition = ctx.DeviceConfig().OdmPath()
-		} else if ctx.ProductSpecific() {
-			partition = ctx.DeviceConfig().ProductPath()
-		} else if ctx.ProductServicesSpecific() {
-			partition = ctx.DeviceConfig().ProductServicesPath()
-		} else {
-			partition = "system"
-		}
-
-		if ctx.InstallInSanitizerDir() {
-			partition = "data/asan/" + partition
-		}
+		partition := modulePartition(ctx)
 		outPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
 	} else {
 		switch ctx.Os() {
@@ -972,6 +942,36 @@
 	return PathForOutput(ctx, outPaths...)
 }
 
+func InstallPathToOnDevicePath(ctx PathContext, path OutputPath) string {
+	rel := Rel(ctx, PathForOutput(ctx, "target", "product", ctx.Config().DeviceName()).String(), path.String())
+
+	return "/" + rel
+}
+
+func modulePartition(ctx ModuleInstallPathContext) string {
+	var partition string
+	if ctx.InstallInData() {
+		partition = "data"
+	} else if ctx.InstallInRecovery() {
+		// the layout of recovery partion is the same as that of system partition
+		partition = "recovery/root/system"
+	} else if ctx.SocSpecific() {
+		partition = ctx.DeviceConfig().VendorPath()
+	} else if ctx.DeviceSpecific() {
+		partition = ctx.DeviceConfig().OdmPath()
+	} else if ctx.ProductSpecific() {
+		partition = ctx.DeviceConfig().ProductPath()
+	} else if ctx.ProductServicesSpecific() {
+		partition = ctx.DeviceConfig().ProductServicesPath()
+	} else {
+		partition = "system"
+	}
+	if ctx.InstallInSanitizerDir() {
+		partition = "data/asan/" + partition
+	}
+	return partition
+}
+
 // validateSafePath validates a path that we trust (may contain ninja variables).
 // Ensures that each path component does not attempt to leave its component.
 func validateSafePath(pathComponents ...string) (string, error) {
@@ -1039,3 +1039,31 @@
 
 	return p
 }
+
+// Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if
+// targetPath is not inside basePath.
+func Rel(ctx PathContext, basePath string, targetPath string) string {
+	rel, isRel := MaybeRel(ctx, basePath, targetPath)
+	if !isRel {
+		reportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath)
+		return ""
+	}
+	return rel
+}
+
+// MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if
+// targetPath is not inside basePath.
+func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) {
+	// filepath.Rel returns an error if one path is absolute and the other is not, handle that case first.
+	if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) {
+		return "", false
+	}
+	rel, err := filepath.Rel(basePath, targetPath)
+	if err != nil {
+		reportPathError(ctx, err)
+		return "", false
+	} else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") {
+		return "", false
+	}
+	return rel, true
+}
diff --git a/android/paths_test.go b/android/paths_test.go
index fbeccb1..c4332d2 100644
--- a/android/paths_test.go
+++ b/android/paths_test.go
@@ -573,3 +573,60 @@
 		t.Errorf("FilesInDirectory(b):\n %#v\n != \n %#v", inA.Strings(), expectedA)
 	}
 }
+
+func TestMaybeRel(t *testing.T) {
+	testCases := []struct {
+		name   string
+		base   string
+		target string
+		out    string
+		isRel  bool
+	}{
+		{
+			name:   "normal",
+			base:   "a/b/c",
+			target: "a/b/c/d",
+			out:    "d",
+			isRel:  true,
+		},
+		{
+			name:   "parent",
+			base:   "a/b/c/d",
+			target: "a/b/c",
+			isRel:  false,
+		},
+		{
+			name:   "not relative",
+			base:   "a/b",
+			target: "c/d",
+			isRel:  false,
+		},
+		{
+			name:   "abs1",
+			base:   "/a",
+			target: "a",
+			isRel:  false,
+		},
+		{
+			name:   "abs2",
+			base:   "a",
+			target: "/a",
+			isRel:  false,
+		},
+	}
+
+	for _, testCase := range testCases {
+		t.Run(testCase.name, func(t *testing.T) {
+			ctx := &configErrorWrapper{}
+			out, isRel := MaybeRel(ctx, testCase.base, testCase.target)
+			if len(ctx.errors) > 0 {
+				t.Errorf("MaybeRel(..., %s, %s) reported unexpected errors %v",
+					testCase.base, testCase.target, ctx.errors)
+			}
+			if isRel != testCase.isRel || out != testCase.out {
+				t.Errorf("MaybeRel(..., %s, %s) want %v, %v got %v, %v",
+					testCase.base, testCase.target, testCase.out, testCase.isRel, out, isRel)
+			}
+		})
+	}
+}
diff --git a/android/variable.go b/android/variable.go
index 0b344f9..a1cdea1 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -54,10 +54,6 @@
 			Cflags []string
 		}
 
-		Device_uses_hwc2 struct {
-			Cflags []string
-		}
-
 		Override_rs_driver struct {
 			Cflags []string
 		}
@@ -181,29 +177,30 @@
 
 	AppsDefaultVersionName *string `json:",omitempty"`
 
-	Allow_missing_dependencies *bool `json:",omitempty"`
-	Unbundled_build            *bool `json:",omitempty"`
-	Malloc_not_svelte          *bool `json:",omitempty"`
-	Safestack                  *bool `json:",omitempty"`
-	HostStaticBinaries         *bool `json:",omitempty"`
-	Binder32bit                *bool `json:",omitempty"`
-	UseGoma                    *bool `json:",omitempty"`
-	Debuggable                 *bool `json:",omitempty"`
-	Eng                        *bool `json:",omitempty"`
-	Device_uses_hwc2           *bool `json:",omitempty"`
-	Treble_linker_namespaces   *bool `json:",omitempty"`
-	Enforce_vintf_manifest     *bool `json:",omitempty"`
-	Pdk                        *bool `json:",omitempty"`
-	Uml                        *bool `json:",omitempty"`
-	Use_lmkd_stats_log         *bool `json:",omitempty"`
-	Arc                        *bool `json:",omitempty"`
-	MinimizeJavaDebugInfo      *bool `json:",omitempty"`
+	Allow_missing_dependencies       *bool `json:",omitempty"`
+	Unbundled_build                  *bool `json:",omitempty"`
+	Unbundled_build_sdks_from_source *bool `json:",omitempty"`
+	Malloc_not_svelte                *bool `json:",omitempty"`
+	Safestack                        *bool `json:",omitempty"`
+	HostStaticBinaries               *bool `json:",omitempty"`
+	Binder32bit                      *bool `json:",omitempty"`
+	UseGoma                          *bool `json:",omitempty"`
+	Debuggable                       *bool `json:",omitempty"`
+	Eng                              *bool `json:",omitempty"`
+	Treble_linker_namespaces         *bool `json:",omitempty"`
+	Enforce_vintf_manifest           *bool `json:",omitempty"`
+	Pdk                              *bool `json:",omitempty"`
+	Uml                              *bool `json:",omitempty"`
+	Use_lmkd_stats_log               *bool `json:",omitempty"`
+	Arc                              *bool `json:",omitempty"`
+	MinimizeJavaDebugInfo            *bool `json:",omitempty"`
 
 	UncompressPrivAppDex             *bool    `json:",omitempty"`
 	ModulesLoadedByPrivilegedModules []string `json:",omitempty"`
-	DefaultStripDex                  *bool    `json:",omitempty"`
-	DisableDexPreopt                 *bool    `json:",omitempty"`
-	DisableDexPreoptModules          []string `json:",omitempty"`
+
+	DisableDexPreopt        *bool    `json:",omitempty"`
+	DisableDexPreoptModules []string `json:",omitempty"`
+	DexPreoptProfileDir     *string  `json:",omitempty"`
 
 	IntegerOverflowExcludePaths *[]string `json:",omitempty"`
 
@@ -258,9 +255,12 @@
 
 	VendorVars map[string]map[string]string `json:",omitempty"`
 
-	Ndk_abis *bool `json:",omitempty"`
+	Ndk_abis               *bool `json:",omitempty"`
+	Exclude_draft_ndk_apis *bool `json:",omitempty"`
 
 	FlattenApex *bool `json:",omitempty"`
+
+	DexpreoptGlobalConfig *string `json:",omitempty"`
 }
 
 func boolPtr(v bool) *bool {
diff --git a/apex/apex.go b/apex/apex.go
index 8a652db..79b79e8 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -55,12 +55,24 @@
 			`${apexer} --force --manifest ${manifest} ` +
 			`--file_contexts ${file_contexts} ` +
 			`--canned_fs_config ${canned_fs_config} ` +
-			`--key ${key} ${image_dir} ${out} `,
+			`--payload_type image ` +
+			`--key ${key} ${opt_flags} ${image_dir} ${out} `,
 		CommandDeps: []string{"${apexer}", "${avbtool}", "${e2fsdroid}", "${merge_zips}",
 			"${mke2fs}", "${resize2fs}", "${sefcontext_compile}",
 			"${soong_zip}", "${zipalign}", "${aapt2}"},
 		Description: "APEX ${image_dir} => ${out}",
-	}, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key")
+	}, "tool_path", "image_dir", "copy_commands", "manifest", "file_contexts", "canned_fs_config", "key", "opt_flags")
+
+	zipApexRule = pctx.StaticRule("zipApexRule", blueprint.RuleParams{
+		Command: `rm -rf ${image_dir} && mkdir -p ${image_dir} && ` +
+			`(${copy_commands}) && ` +
+			`APEXER_TOOL_PATH=${tool_path} ` +
+			`${apexer} --force --manifest ${manifest} ` +
+			`--payload_type zip ` +
+			`${image_dir} ${out} `,
+		CommandDeps: []string{"${apexer}", "${merge_zips}", "${soong_zip}", "${zipalign}", "${aapt2}"},
+		Description: "ZipAPEX ${image_dir} => ${out}",
+	}, "tool_path", "image_dir", "copy_commands", "manifest")
 
 	apexProtoConvertRule = pctx.AndroidStaticRule("apexProtoConvertRule",
 		blueprint.RuleParams{
@@ -78,7 +90,11 @@
 	}, "abi")
 )
 
-var apexSuffix = ".apex"
+var imageApexSuffix = ".apex"
+var zipApexSuffix = ".zipapex"
+
+var imageApexType = "image"
+var zipApexType = "zip"
 
 type dependencyTag struct {
 	blueprint.BaseDependencyTag
@@ -120,7 +136,7 @@
 	pctx.HostBinToolVariable("zip2zip", "zip2zip")
 	pctx.HostBinToolVariable("zipalign", "zipalign")
 
-	android.RegisterModuleType("apex", apexBundleFactory)
+	android.RegisterModuleType("apex", ApexBundleFactory)
 
 	android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
 		ctx.TopDown("apex_deps", apexDepsMutator)
@@ -128,27 +144,19 @@
 	})
 }
 
-// maps a module name to set of apex bundle names that the module should be built for
-func apexBundleNamesFor(config android.Config) map[string]map[string]bool {
-	return config.Once("apexBundleNames", func() interface{} {
-		return make(map[string]map[string]bool)
-	}).(map[string]map[string]bool)
-}
-
 // Mark the direct and transitive dependencies of apex bundles so that they
 // can be built for the apex bundles.
 func apexDepsMutator(mctx android.TopDownMutatorContext) {
 	if _, ok := mctx.Module().(*apexBundle); ok {
 		apexBundleName := mctx.ModuleName()
 		mctx.WalkDeps(func(child, parent android.Module) bool {
+			depName := mctx.OtherModuleName(child)
+			// If the parent is apexBundle, this child is directly depended.
+			_, directDep := parent.(*apexBundle)
+			android.UpdateApexDependency(apexBundleName, depName, directDep)
+
 			if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() {
-				moduleName := mctx.OtherModuleName(am) + "-" + am.Target().String()
-				bundleNames, ok := apexBundleNamesFor(mctx.Config())[moduleName]
-				if !ok {
-					bundleNames = make(map[string]bool)
-					apexBundleNamesFor(mctx.Config())[moduleName] = bundleNames
-				}
-				bundleNames[apexBundleName] = true
+				am.BuildForApex(apexBundleName)
 				return true
 			} else {
 				return false
@@ -160,20 +168,7 @@
 // Create apex variations if a module is included in APEX(s).
 func apexMutator(mctx android.BottomUpMutatorContext) {
 	if am, ok := mctx.Module().(android.ApexModule); ok && am.CanHaveApexVariants() {
-		moduleName := mctx.ModuleName() + "-" + am.Target().String()
-		if bundleNames, ok := apexBundleNamesFor(mctx.Config())[moduleName]; ok {
-			variations := []string{"platform"}
-			for bn := range bundleNames {
-				variations = append(variations, bn)
-			}
-			modules := mctx.CreateVariations(variations...)
-			for i, m := range modules {
-				if i == 0 {
-					continue // platform
-				}
-				m.(android.ApexModule).BuildForApex(variations[i])
-			}
-		}
+		am.CreateApexVariations(mctx)
 	} else if _, ok := mctx.Module().(*apexBundle); ok {
 		// apex bundle itself is mutated so that it and its modules have same
 		// apex variant.
@@ -208,10 +203,21 @@
 	// Name of the apex_key module that provides the private key to sign APEX
 	Key *string
 
+	// The type of APEX to build. Controls what the APEX payload is. Either
+	// 'image', 'zip' or 'both'. Default: 'image'.
+	Payload_type *string
+
 	// The name of a certificate in the default certificate directory, blank to use the default product certificate,
 	// or an android_app_certificate module name in the form ":module".
 	Certificate *string
 
+	// Whether this APEX is installable to one of the partitions. Default: true.
+	Installable *bool
+
+	// For native libraries and binaries, use the vendor variant instead of the core (platform) variant.
+	// Default is false.
+	Use_vendor *bool
+
 	Multilib struct {
 		First struct {
 			// List of native libraries whose compile_multilib is "first"
@@ -255,6 +261,56 @@
 	javaSharedLib
 )
 
+type apexPackaging int
+
+const (
+	imageApex apexPackaging = iota
+	zipApex
+	both
+)
+
+func (a apexPackaging) image() bool {
+	switch a {
+	case imageApex, both:
+		return true
+	}
+	return false
+}
+
+func (a apexPackaging) zip() bool {
+	switch a {
+	case zipApex, both:
+		return true
+	}
+	return false
+}
+
+func (a apexPackaging) suffix() string {
+	switch a {
+	case imageApex:
+		return imageApexSuffix
+	case zipApex:
+		return zipApexSuffix
+	case both:
+		panic(fmt.Errorf("must be either zip or image"))
+	default:
+		panic(fmt.Errorf("unkonwn APEX type %d", a))
+	}
+}
+
+func (a apexPackaging) name() string {
+	switch a {
+	case imageApex:
+		return imageApexType
+	case zipApex:
+		return zipApexType
+	case both:
+		panic(fmt.Errorf("must be either zip or image"))
+	default:
+		panic(fmt.Errorf("unkonwn APEX type %d", a))
+	}
+}
+
 func (class apexFileClass) NameInMake() string {
 	switch class {
 	case etc:
@@ -276,6 +332,7 @@
 	archType   android.ArchType
 	installDir string
 	class      apexFileClass
+	module     android.Module
 }
 
 type apexBundle struct {
@@ -284,8 +341,10 @@
 
 	properties apexBundleProperties
 
+	apexTypes apexPackaging
+
 	bundleModuleFile android.WritablePath
-	outputFile       android.WritablePath
+	outputFiles      map[apexPackaging]android.WritablePath
 	installDir       android.OutputPath
 
 	// list of files to be included in this apex
@@ -295,20 +354,21 @@
 }
 
 func addDependenciesForNativeModules(ctx android.BottomUpMutatorContext,
-	native_shared_libs []string, binaries []string, arch string) {
+	native_shared_libs []string, binaries []string, arch string, imageVariation string) {
 	// Use *FarVariation* to be able to depend on modules having
 	// conflicting variations with this module. This is required since
 	// arch variant of an APEX bundle is 'common' but it is 'arm' or 'arm64'
 	// for native shared libs.
 	ctx.AddFarVariationDependencies([]blueprint.Variation{
 		{Mutator: "arch", Variation: arch},
-		{Mutator: "image", Variation: "core"},
+		{Mutator: "image", Variation: imageVariation},
 		{Mutator: "link", Variation: "shared"},
+		{Mutator: "version", Variation: ""}, // "" is the non-stub variant
 	}, sharedLibTag, native_shared_libs...)
 
 	ctx.AddFarVariationDependencies([]blueprint.Variation{
 		{Mutator: "arch", Variation: arch},
-		{Mutator: "image", Variation: "core"},
+		{Mutator: "image", Variation: imageVariation},
 	}, executableTag, binaries...)
 }
 
@@ -325,27 +385,29 @@
 		// multilib.both.
 		ctx.AddFarVariationDependencies([]blueprint.Variation{
 			{Mutator: "arch", Variation: target.String()},
-			{Mutator: "image", Variation: "core"},
+			{Mutator: "image", Variation: a.getImageVariation()},
 			{Mutator: "link", Variation: "shared"},
 		}, sharedLibTag, a.properties.Native_shared_libs...)
 
 		// Add native modules targetting both ABIs
 		addDependenciesForNativeModules(ctx,
 			a.properties.Multilib.Both.Native_shared_libs,
-			a.properties.Multilib.Both.Binaries, target.String())
+			a.properties.Multilib.Both.Binaries, target.String(),
+			a.getImageVariation())
 
 		if i == 0 {
 			// When multilib.* is omitted for binaries, it implies
 			// multilib.first.
 			ctx.AddFarVariationDependencies([]blueprint.Variation{
 				{Mutator: "arch", Variation: target.String()},
-				{Mutator: "image", Variation: "core"},
+				{Mutator: "image", Variation: a.getImageVariation()},
 			}, executableTag, a.properties.Binaries...)
 
 			// Add native modules targetting the first ABI
 			addDependenciesForNativeModules(ctx,
 				a.properties.Multilib.First.Native_shared_libs,
-				a.properties.Multilib.First.Binaries, target.String())
+				a.properties.Multilib.First.Binaries, target.String(),
+				a.getImageVariation())
 		}
 
 		switch target.Arch.ArchType.Multilib {
@@ -353,21 +415,25 @@
 			// Add native modules targetting 32-bit ABI
 			addDependenciesForNativeModules(ctx,
 				a.properties.Multilib.Lib32.Native_shared_libs,
-				a.properties.Multilib.Lib32.Binaries, target.String())
+				a.properties.Multilib.Lib32.Binaries, target.String(),
+				a.getImageVariation())
 
 			addDependenciesForNativeModules(ctx,
 				a.properties.Multilib.Prefer32.Native_shared_libs,
-				a.properties.Multilib.Prefer32.Binaries, target.String())
+				a.properties.Multilib.Prefer32.Binaries, target.String(),
+				a.getImageVariation())
 		case "lib64":
 			// Add native modules targetting 64-bit ABI
 			addDependenciesForNativeModules(ctx,
 				a.properties.Multilib.Lib64.Native_shared_libs,
-				a.properties.Multilib.Lib64.Binaries, target.String())
+				a.properties.Multilib.Lib64.Binaries, target.String(),
+				a.getImageVariation())
 
 			if !has32BitTarget {
 				addDependenciesForNativeModules(ctx,
 					a.properties.Multilib.Prefer32.Native_shared_libs,
-					a.properties.Multilib.Prefer32.Binaries, target.String())
+					a.properties.Multilib.Prefer32.Binaries, target.String(),
+					a.getImageVariation())
 			}
 		}
 
@@ -393,6 +459,26 @@
 	}
 }
 
+func (a *apexBundle) Srcs() android.Paths {
+	if file, ok := a.outputFiles[imageApex]; ok {
+		return android.Paths{file}
+	} else {
+		return nil
+	}
+}
+
+func (a *apexBundle) installable() bool {
+	return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
+}
+
+func (a *apexBundle) getImageVariation() string {
+	if proptools.Bool(a.properties.Use_vendor) {
+		return "vendor"
+	} else {
+		return "core"
+	}
+}
+
 func getCopyManifestForNativeLibrary(cc *cc.Module) (fileToCopy android.Path, dirInApex string) {
 	// Decide the APEX-local directory by the multilib of the library
 	// In the future, we may query this to the module.
@@ -432,8 +518,20 @@
 	filesInfo := []apexFile{}
 
 	var keyFile android.Path
+	var pubKeyFile android.Path
 	var certificate java.Certificate
 
+	if a.properties.Payload_type == nil || *a.properties.Payload_type == "image" {
+		a.apexTypes = imageApex
+	} else if *a.properties.Payload_type == "zip" {
+		a.apexTypes = zipApex
+	} else if *a.properties.Payload_type == "both" {
+		a.apexTypes = both
+	} else {
+		ctx.PropertyErrorf("type", "%q is not one of \"image\", \"zip\", or \"both\".", *a.properties.Payload_type)
+		return
+	}
+
 	ctx.WalkDeps(func(child, parent android.Module) bool {
 		if _, ok := parent.(*apexBundle); ok {
 			// direct dependencies
@@ -443,7 +541,7 @@
 			case sharedLibTag:
 				if cc, ok := child.(*cc.Module); ok {
 					fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc)
-					filesInfo = append(filesInfo, apexFile{fileToCopy, depName, cc.Arch().ArchType, dirInApex, nativeSharedLib})
+					filesInfo = append(filesInfo, apexFile{fileToCopy, depName, cc.Arch().ArchType, dirInApex, nativeSharedLib, cc})
 					return true
 				} else {
 					ctx.PropertyErrorf("native_shared_libs", "%q is not a cc_library or cc_library_shared module", depName)
@@ -451,7 +549,7 @@
 			case executableTag:
 				if cc, ok := child.(*cc.Module); ok {
 					fileToCopy, dirInApex := getCopyManifestForExecutable(cc)
-					filesInfo = append(filesInfo, apexFile{fileToCopy, depName, cc.Arch().ArchType, dirInApex, nativeExecutable})
+					filesInfo = append(filesInfo, apexFile{fileToCopy, depName, cc.Arch().ArchType, dirInApex, nativeExecutable, cc})
 					return true
 				} else {
 					ctx.PropertyErrorf("binaries", "%q is not a cc_binary module", depName)
@@ -462,7 +560,7 @@
 					if fileToCopy == nil {
 						ctx.PropertyErrorf("java_libs", "%q is not configured to be compiled into dex", depName)
 					} else {
-						filesInfo = append(filesInfo, apexFile{fileToCopy, depName, java.Arch().ArchType, dirInApex, javaSharedLib})
+						filesInfo = append(filesInfo, apexFile{fileToCopy, depName, java.Arch().ArchType, dirInApex, javaSharedLib, java})
 					}
 					return true
 				} else {
@@ -471,7 +569,7 @@
 			case prebuiltTag:
 				if prebuilt, ok := child.(*android.PrebuiltEtc); ok {
 					fileToCopy, dirInApex := getCopyManifestForPrebuiltEtc(prebuilt)
-					filesInfo = append(filesInfo, apexFile{fileToCopy, depName, prebuilt.Arch().ArchType, dirInApex, etc})
+					filesInfo = append(filesInfo, apexFile{fileToCopy, depName, prebuilt.Arch().ArchType, dirInApex, etc, prebuilt})
 					return true
 				} else {
 					ctx.PropertyErrorf("prebuilts", "%q is not a prebuilt_etc module", depName)
@@ -479,6 +577,12 @@
 			case keyTag:
 				if key, ok := child.(*apexKey); ok {
 					keyFile = key.private_key_file
+					if !key.installable() && ctx.Config().Debuggable() {
+						// If the key is not installed, bundled it with the APEX.
+						// Note: this bundled key is valid only for non-production builds
+						// (eng/userdebug).
+						pubKeyFile = key.public_key_file
+					}
 					return false
 				} else {
 					ctx.PropertyErrorf("key", "%q is not an apex_key module", depName)
@@ -495,9 +599,12 @@
 			// indirect dependencies
 			if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
 				if cc, ok := child.(*cc.Module); ok {
+					if cc.IsStubs() || cc.HasStubsVariants() {
+						return false
+					}
 					depName := ctx.OtherModuleName(child)
 					fileToCopy, dirInApex := getCopyManifestForNativeLibrary(cc)
-					filesInfo = append(filesInfo, apexFile{fileToCopy, depName, cc.Arch().ArchType, dirInApex, nativeSharedLib})
+					filesInfo = append(filesInfo, apexFile{fileToCopy, depName, cc.Arch().ArchType, dirInApex, nativeSharedLib, cc})
 					return true
 				}
 			}
@@ -538,14 +645,21 @@
 	a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
 	a.installDir = android.PathForModuleInstall(ctx, "apex")
 	a.filesInfo = filesInfo
-	if ctx.Config().FlattenApex() {
-		a.buildFlattenedApex(ctx)
-	} else {
-		a.buildUnflattenedApex(ctx, keyFile, certificate)
+
+	if a.apexTypes.zip() {
+		a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, zipApex)
+	}
+	if a.apexTypes.image() {
+		if ctx.Config().FlattenApex() {
+			a.buildFlattenedApex(ctx)
+		} else {
+			a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, imageApex)
+		}
 	}
 }
 
-func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, keyFile android.Path, certificate java.Certificate) {
+func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, keyFile android.Path,
+	pubKeyFile android.Path, certificate java.Certificate, apexType apexPackaging) {
 	cert := String(a.properties.Certificate)
 	if cert != "" && android.SrcIsModule(cert) == "" {
 		defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
@@ -558,45 +672,19 @@
 		certificate = java.Certificate{pem, key}
 	}
 
-	// files and dirs that will be created in apex
-	var readOnlyPaths []string
-	var executablePaths []string // this also includes dirs
-	for _, f := range a.filesInfo {
-		pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
-		if f.installDir == "bin" {
-			executablePaths = append(executablePaths, pathInApex)
-		} else {
-			readOnlyPaths = append(readOnlyPaths, pathInApex)
-		}
-		if !android.InList(f.installDir, executablePaths) {
-			executablePaths = append(executablePaths, f.installDir)
-		}
-	}
-	sort.Strings(readOnlyPaths)
-	sort.Strings(executablePaths)
-	cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
-	ctx.Build(pctx, android.BuildParams{
-		Rule:        generateFsConfig,
-		Output:      cannedFsConfig,
-		Description: "generate fs config",
-		Args: map[string]string{
-			"ro_paths":   strings.Join(readOnlyPaths, " "),
-			"exec_paths": strings.Join(executablePaths, " "),
-		},
-	})
-
 	manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
 
-	fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
-	fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
-	fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
-	if !fileContextsOptionalPath.Valid() {
-		ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
-		return
+	var abis []string
+	for _, target := range ctx.MultiTargets() {
+		if len(target.Arch.Abi) > 0 {
+			abis = append(abis, target.Arch.Abi[0])
+		}
 	}
-	fileContexts := fileContextsOptionalPath.Path()
 
-	unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+apexSuffix+".unsigned")
+	abis = android.FirstUniqueStrings(abis)
+
+	suffix := apexType.suffix()
+	unsignedOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+".unsigned")
 
 	filesToCopy := []android.Path{}
 	for _, f := range a.filesInfo {
@@ -606,84 +694,171 @@
 	copyCommands := []string{}
 	for i, src := range filesToCopy {
 		dest := filepath.Join(a.filesInfo[i].installDir, src.Base())
-		dest_path := filepath.Join(android.PathForModuleOut(ctx, "image").String(), dest)
+		dest_path := filepath.Join(android.PathForModuleOut(ctx, "image"+suffix).String(), dest)
 		copyCommands = append(copyCommands, "mkdir -p "+filepath.Dir(dest_path))
 		copyCommands = append(copyCommands, "cp "+src.String()+" "+dest_path)
 	}
 	implicitInputs := append(android.Paths(nil), filesToCopy...)
-	implicitInputs = append(implicitInputs, cannedFsConfig, manifest, fileContexts, keyFile)
+	implicitInputs = append(implicitInputs, manifest)
+
 	outHostBinDir := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin").String()
 	prebuiltSdkToolsBinDir := filepath.Join("prebuilts", "sdk", "tools", runtime.GOOS, "bin")
-	ctx.Build(pctx, android.BuildParams{
-		Rule:        apexRule,
-		Implicits:   implicitInputs,
-		Output:      unsignedOutputFile,
-		Description: "apex",
-		Args: map[string]string{
-			"tool_path":        outHostBinDir + ":" + prebuiltSdkToolsBinDir,
-			"image_dir":        android.PathForModuleOut(ctx, "image").String(),
-			"copy_commands":    strings.Join(copyCommands, " && "),
-			"manifest":         manifest.String(),
-			"file_contexts":    fileContexts.String(),
-			"canned_fs_config": cannedFsConfig.String(),
-			"key":              keyFile.String(),
-		},
-	})
 
-	var abis []string
-	for _, target := range ctx.MultiTargets() {
-		abis = append(abis, target.Arch.Abi[0])
+	if apexType.image() {
+		// files and dirs that will be created in APEX
+		var readOnlyPaths []string
+		var executablePaths []string // this also includes dirs
+		for _, f := range a.filesInfo {
+			pathInApex := filepath.Join(f.installDir, f.builtFile.Base())
+			if f.installDir == "bin" {
+				executablePaths = append(executablePaths, pathInApex)
+			} else {
+				readOnlyPaths = append(readOnlyPaths, pathInApex)
+			}
+			dir := f.installDir
+			for !android.InList(dir, executablePaths) && dir != "" {
+				executablePaths = append(executablePaths, dir)
+				dir, _ = filepath.Split(dir) // move up to the parent
+				if len(dir) > 0 {
+					// remove trailing slash
+					dir = dir[:len(dir)-1]
+				}
+			}
+		}
+		sort.Strings(readOnlyPaths)
+		sort.Strings(executablePaths)
+		cannedFsConfig := android.PathForModuleOut(ctx, "canned_fs_config")
+		ctx.Build(pctx, android.BuildParams{
+			Rule:        generateFsConfig,
+			Output:      cannedFsConfig,
+			Description: "generate fs config",
+			Args: map[string]string{
+				"ro_paths":   strings.Join(readOnlyPaths, " "),
+				"exec_paths": strings.Join(executablePaths, " "),
+			},
+		})
+
+		fcName := proptools.StringDefault(a.properties.File_contexts, ctx.ModuleName())
+		fileContextsPath := "system/sepolicy/apex/" + fcName + "-file_contexts"
+		fileContextsOptionalPath := android.ExistentPathForSource(ctx, fileContextsPath)
+		if !fileContextsOptionalPath.Valid() {
+			ctx.ModuleErrorf("Cannot find file_contexts file: %q", fileContextsPath)
+			return
+		}
+		fileContexts := fileContextsOptionalPath.Path()
+
+		optFlags := []string{}
+
+		// Additional implicit inputs.
+		implicitInputs = append(implicitInputs, cannedFsConfig, fileContexts, keyFile)
+		if pubKeyFile != nil {
+			implicitInputs = append(implicitInputs, pubKeyFile)
+			optFlags = append(optFlags, "--pubkey "+pubKeyFile.String())
+		}
+
+		ctx.Build(pctx, android.BuildParams{
+			Rule:        apexRule,
+			Implicits:   implicitInputs,
+			Output:      unsignedOutputFile,
+			Description: "apex (" + apexType.name() + ")",
+			Args: map[string]string{
+				"tool_path":        outHostBinDir + ":" + prebuiltSdkToolsBinDir,
+				"image_dir":        android.PathForModuleOut(ctx, "image"+suffix).String(),
+				"copy_commands":    strings.Join(copyCommands, " && "),
+				"manifest":         manifest.String(),
+				"file_contexts":    fileContexts.String(),
+				"canned_fs_config": cannedFsConfig.String(),
+				"key":              keyFile.String(),
+				"opt_flags":        strings.Join(optFlags, " "),
+			},
+		})
+
+		apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+suffix)
+		bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+suffix+"-base.zip")
+		a.bundleModuleFile = bundleModuleFile
+
+		ctx.Build(pctx, android.BuildParams{
+			Rule:        apexProtoConvertRule,
+			Input:       unsignedOutputFile,
+			Output:      apexProtoFile,
+			Description: "apex proto convert",
+		})
+
+		ctx.Build(pctx, android.BuildParams{
+			Rule:        apexBundleRule,
+			Input:       apexProtoFile,
+			Output:      a.bundleModuleFile,
+			Description: "apex bundle module",
+			Args: map[string]string{
+				"abi": strings.Join(abis, "."),
+			},
+		})
+	} else {
+		ctx.Build(pctx, android.BuildParams{
+			Rule:        zipApexRule,
+			Implicits:   implicitInputs,
+			Output:      unsignedOutputFile,
+			Description: "apex (" + apexType.name() + ")",
+			Args: map[string]string{
+				"tool_path":     outHostBinDir + ":" + prebuiltSdkToolsBinDir,
+				"image_dir":     android.PathForModuleOut(ctx, "image"+suffix).String(),
+				"copy_commands": strings.Join(copyCommands, " && "),
+				"manifest":      manifest.String(),
+			},
+		})
 	}
-	abis = android.FirstUniqueStrings(abis)
 
-	apexProtoFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".pb"+apexSuffix)
-	bundleModuleFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-base.zip")
-	a.bundleModuleFile = bundleModuleFile
-
-	ctx.Build(pctx, android.BuildParams{
-		Rule:        apexProtoConvertRule,
-		Input:       unsignedOutputFile,
-		Output:      apexProtoFile,
-		Description: "apex proto convert",
-	})
-
-	ctx.Build(pctx, android.BuildParams{
-		Rule:        apexBundleRule,
-		Input:       apexProtoFile,
-		Output:      bundleModuleFile,
-		Description: "apex bundle module",
-		Args: map[string]string{
-			"abi": strings.Join(abis, "."),
-		},
-	})
-
-	a.outputFile = android.PathForModuleOut(ctx, ctx.ModuleName()+apexSuffix)
+	a.outputFiles[apexType] = android.PathForModuleOut(ctx, ctx.ModuleName()+suffix)
 	ctx.Build(pctx, android.BuildParams{
 		Rule:        java.Signapk,
 		Description: "signapk",
-		Output:      a.outputFile,
+		Output:      a.outputFiles[apexType],
 		Input:       unsignedOutputFile,
 		Args: map[string]string{
 			"certificates": strings.Join([]string{certificate.Pem.String(), certificate.Key.String()}, " "),
 			"flags":        "-a 4096", //alignment
 		},
 	})
+
+	// Install to $OUT/soong/{target,host}/.../apex
+	if a.installable() {
+		ctx.InstallFile(android.PathForModuleInstall(ctx, "apex"), ctx.ModuleName()+suffix, a.outputFiles[apexType])
+	}
 }
 
 func (a *apexBundle) buildFlattenedApex(ctx android.ModuleContext) {
-	// For flattened APEX, do nothing but make sure that apex_manifest.json file is also copied along
-	// with other ordinary files.
-	manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
-	a.filesInfo = append(a.filesInfo, apexFile{manifest, ctx.ModuleName() + ".apex_manifest.json", android.Common, ".", etc})
+	if a.installable() {
+		// For flattened APEX, do nothing but make sure that apex_manifest.json file is also copied along
+		// with other ordinary files.
+		manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
+		a.filesInfo = append(a.filesInfo, apexFile{manifest, ctx.ModuleName() + ".apex_manifest.json", android.Common, ".", etc, nil})
 
-	for _, fi := range a.filesInfo {
-		dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
-		ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
+		for _, fi := range a.filesInfo {
+			dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
+			ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
+		}
 	}
 }
 
 func (a *apexBundle) AndroidMk() android.AndroidMkData {
-	if a.flattened {
+	writers := []android.AndroidMkData{}
+	if a.apexTypes.image() {
+		writers = append(writers, a.androidMkForType(imageApex))
+	}
+	if a.apexTypes.zip() {
+		writers = append(writers, a.androidMkForType(zipApex))
+	}
+	return android.AndroidMkData{
+		Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
+			for _, data := range writers {
+				data.Custom(w, name, prefix, moduleDir, data)
+			}
+		}}
+}
+
+func (a *apexBundle) androidMkForType(apexType apexPackaging) android.AndroidMkData {
+	// Only image APEXes can be flattened.
+	if a.flattened && apexType.image() {
 		return android.AndroidMkData{
 			Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
 				moduleNames := []string{}
@@ -703,14 +878,18 @@
 					fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
 					fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
 					fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString(), name, fi.installDir))
-					fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", fi.builtFile.Base())
+					fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
 					fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
 					fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
+					fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
 					archStr := fi.archType.String()
 					if archStr != "common" {
 						fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
 					}
 					if fi.class == javaSharedLib {
+						javaModule := fi.module.(*java.Library)
+						fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
+						fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
 						fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
 						fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
 						fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
@@ -722,29 +901,38 @@
 	} else {
 		return android.AndroidMkData{
 			Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
+				// zip-apex is the less common type so have the name refer to the image-apex
+				// only and use {name}.zip if you want the zip-apex
+				if apexType == zipApex && a.apexTypes == both {
+					name = name + ".zip"
+				}
 				fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
 				fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
 				fmt.Fprintln(w, "LOCAL_MODULE :=", name)
 				fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
-				fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
+				fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFiles[apexType].String())
 				fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)", a.installDir.RelPathString()))
-				fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", name+apexSuffix)
+				fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
+				fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
 				fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", String(a.properties.Key))
 				fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
 
-				fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
+				if apexType == imageApex {
+					fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
+				}
 			}}
 	}
 }
 
-func apexBundleFactory() android.Module {
-	module := &apexBundle{}
+func ApexBundleFactory() android.Module {
+	module := &apexBundle{
+		outputFiles: map[apexPackaging]android.WritablePath{},
+	}
 	module.AddProperties(&module.properties)
-	module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase,
-		class android.OsClass) bool {
+	module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
 		return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
 	})
-	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
+	android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
 	android.InitDefaultableModule(module)
 	return module
 }
diff --git a/apex/apex_test.go b/apex/apex_test.go
new file mode 100644
index 0000000..f8f9b33
--- /dev/null
+++ b/apex/apex_test.go
@@ -0,0 +1,672 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package apex
+
+import (
+	"io/ioutil"
+	"os"
+	"strings"
+	"testing"
+
+	"github.com/google/blueprint/proptools"
+
+	"android/soong/android"
+	"android/soong/cc"
+)
+
+func testApex(t *testing.T, bp string) *android.TestContext {
+	config, buildDir := setup(t)
+	defer teardown(buildDir)
+
+	ctx := android.NewTestArchContext()
+	ctx.RegisterModuleType("apex", android.ModuleFactoryAdaptor(ApexBundleFactory))
+	ctx.RegisterModuleType("apex_key", android.ModuleFactoryAdaptor(apexKeyFactory))
+
+	ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
+		ctx.TopDown("apex_deps", apexDepsMutator)
+		ctx.BottomUp("apex", apexMutator)
+	})
+
+	ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
+	ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(cc.LibrarySharedFactory))
+	ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(cc.BinaryFactory))
+	ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
+	ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
+	ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
+	ctx.RegisterModuleType("prebuilt_etc", android.ModuleFactoryAdaptor(android.PrebuiltEtcFactory))
+	ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
+		ctx.BottomUp("image", cc.ImageMutator).Parallel()
+		ctx.BottomUp("link", cc.LinkageMutator).Parallel()
+		ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
+		ctx.BottomUp("version", cc.VersionMutator).Parallel()
+		ctx.BottomUp("begin", cc.BeginMutator).Parallel()
+	})
+
+	ctx.Register()
+
+	bp = bp + `
+		toolchain_library {
+			name: "libcompiler_rt-extras",
+			src: "",
+			vendor_available: true,
+			recovery_available: true,
+		}
+
+		toolchain_library {
+			name: "libatomic",
+			src: "",
+			vendor_available: true,
+			recovery_available: true,
+		}
+
+		toolchain_library {
+			name: "libgcc",
+			src: "",
+			vendor_available: true,
+			recovery_available: true,
+		}
+
+		toolchain_library {
+			name: "libclang_rt.builtins-aarch64-android",
+			src: "",
+			vendor_available: true,
+			recovery_available: true,
+		}
+
+		toolchain_library {
+			name: "libclang_rt.builtins-arm-android",
+			src: "",
+			vendor_available: true,
+			recovery_available: true,
+		}
+
+		cc_object {
+			name: "crtbegin_so",
+			stl: "none",
+			vendor_available: true,
+			recovery_available: true,
+		}
+
+		cc_object {
+			name: "crtend_so",
+			stl: "none",
+			vendor_available: true,
+			recovery_available: true,
+		}
+
+		llndk_library {
+			name: "libc",
+			symbol_file: "",
+		}
+
+		llndk_library {
+			name: "libm",
+			symbol_file: "",
+		}
+
+		llndk_library {
+			name: "libdl",
+			symbol_file: "",
+		}
+	`
+
+	ctx.MockFileSystem(map[string][]byte{
+		"Android.bp":                                []byte(bp),
+		"testkey.avbpubkey":                         nil,
+		"testkey.pem":                               nil,
+		"build/target/product/security":             nil,
+		"apex_manifest.json":                        nil,
+		"system/sepolicy/apex/myapex-file_contexts": nil,
+		"mylib.cpp":                                 nil,
+		"myprebuilt":                                nil,
+	})
+	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
+	android.FailIfErrored(t, errs)
+	_, errs = ctx.PrepareBuildActions(config)
+	android.FailIfErrored(t, errs)
+
+	return ctx
+}
+
+func setup(t *testing.T) (config android.Config, buildDir string) {
+	buildDir, err := ioutil.TempDir("", "soong_apex_test")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	config = android.TestArchConfig(buildDir, nil)
+	config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
+	return
+}
+
+func teardown(buildDir string) {
+	os.RemoveAll(buildDir)
+}
+
+// ensure that 'result' contains 'expected'
+func ensureContains(t *testing.T, result string, expected string) {
+	if !strings.Contains(result, expected) {
+		t.Errorf("%q is not found in %q", expected, result)
+	}
+}
+
+// ensures that 'result' does not contain 'notExpected'
+func ensureNotContains(t *testing.T, result string, notExpected string) {
+	if strings.Contains(result, notExpected) {
+		t.Errorf("%q is found in %q", notExpected, result)
+	}
+}
+
+func ensureListContains(t *testing.T, result []string, expected string) {
+	if !android.InList(expected, result) {
+		t.Errorf("%q is not found in %v", expected, result)
+	}
+}
+
+func ensureListNotContains(t *testing.T, result []string, notExpected string) {
+	if android.InList(notExpected, result) {
+		t.Errorf("%q is found in %v", notExpected, result)
+	}
+}
+
+// Minimal test
+func TestBasicApex(t *testing.T) {
+	ctx := testApex(t, `
+		apex {
+			name: "myapex",
+			key: "myapex.key",
+			native_shared_libs: ["mylib"],
+		}
+
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+
+		cc_library {
+			name: "mylib",
+			srcs: ["mylib.cpp"],
+			shared_libs: ["mylib2"],
+			system_shared_libs: [],
+			stl: "none",
+		}
+
+		cc_library {
+			name: "mylib2",
+			srcs: ["mylib.cpp"],
+			system_shared_libs: [],
+			stl: "none",
+		}
+	`)
+
+	apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
+	copyCmds := apexRule.Args["copy_commands"]
+
+	// Ensure that main rule creates an output
+	ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
+
+	// Ensure that apex variant is created for the direct dep
+	ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
+
+	// Ensure that apex variant is created for the indirect dep
+	ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
+
+	// Ensure that both direct and indirect deps are copied into apex
+	ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
+	ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
+
+	// Ensure that the platform variant ends with _core_shared
+	ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
+	ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
+}
+
+func TestBasicZipApex(t *testing.T) {
+	ctx := testApex(t, `
+		apex {
+			name: "myapex",
+			key: "myapex.key",
+			payload_type: "zip",
+			native_shared_libs: ["mylib"],
+		}
+
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+
+		cc_library {
+			name: "mylib",
+			srcs: ["mylib.cpp"],
+			shared_libs: ["mylib2"],
+			system_shared_libs: [],
+			stl: "none",
+		}
+
+		cc_library {
+			name: "mylib2",
+			srcs: ["mylib.cpp"],
+			system_shared_libs: [],
+			stl: "none",
+		}
+	`)
+
+	zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
+	copyCmds := zipApexRule.Args["copy_commands"]
+
+	// Ensure that main rule creates an output
+	ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
+
+	// Ensure that APEX variant is created for the direct dep
+	ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
+
+	// Ensure that APEX variant is created for the indirect dep
+	ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
+
+	// Ensure that both direct and indirect deps are copied into apex
+	ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
+	ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
+}
+
+func TestApexWithStubs(t *testing.T) {
+	ctx := testApex(t, `
+		apex {
+			name: "myapex",
+			key: "myapex.key",
+			native_shared_libs: ["mylib", "mylib3"],
+		}
+
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+
+		cc_library {
+			name: "mylib",
+			srcs: ["mylib.cpp"],
+			shared_libs: ["mylib2", "mylib3"],
+			system_shared_libs: [],
+			stl: "none",
+		}
+
+		cc_library {
+			name: "mylib2",
+			srcs: ["mylib.cpp"],
+			cflags: ["-include mylib.h"],
+			system_shared_libs: [],
+			stl: "none",
+			stubs: {
+				versions: ["1", "2", "3"],
+			},
+		}
+
+		cc_library {
+			name: "mylib3",
+			srcs: ["mylib.cpp"],
+			shared_libs: ["mylib4"],
+			system_shared_libs: [],
+			stl: "none",
+			stubs: {
+				versions: ["10", "11", "12"],
+			},
+		}
+
+		cc_library {
+			name: "mylib4",
+			srcs: ["mylib.cpp"],
+			system_shared_libs: [],
+			stl: "none",
+		}
+	`)
+
+	apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
+	copyCmds := apexRule.Args["copy_commands"]
+
+	// Ensure that direct non-stubs dep is always included
+	ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
+
+	// Ensure that indirect stubs dep is not included
+	ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
+
+	// Ensure that direct stubs dep is included
+	ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
+
+	mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
+
+	// Ensure that mylib is linking with the latest version of stubs for mylib2
+	ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
+	// ... and not linking to the non-stub (impl) variant of mylib2
+	ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
+
+	// Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
+	ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
+	// .. and not linking to the stubs variant of mylib3
+	ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
+
+	// Ensure that stubs libs are built without -include flags
+	mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
+	ensureNotContains(t, mylib2Cflags, "-include ")
+
+	// Ensure that genstub is invoked with --apex
+	ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_3_myapex").Rule("genStubSrc").Args["flags"])
+}
+
+func TestApexWithExplicitStubsDependency(t *testing.T) {
+	ctx := testApex(t, `
+		apex {
+			name: "myapex",
+			key: "myapex.key",
+			native_shared_libs: ["mylib"],
+		}
+
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+
+		cc_library {
+			name: "mylib",
+			srcs: ["mylib.cpp"],
+			shared_libs: ["libfoo#10"],
+			system_shared_libs: [],
+			stl: "none",
+		}
+
+		cc_library {
+			name: "libfoo",
+			srcs: ["mylib.cpp"],
+			shared_libs: ["libbar"],
+			system_shared_libs: [],
+			stl: "none",
+			stubs: {
+				versions: ["10", "20", "30"],
+			},
+		}
+
+		cc_library {
+			name: "libbar",
+			srcs: ["mylib.cpp"],
+			system_shared_libs: [],
+			stl: "none",
+		}
+
+	`)
+
+	apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
+	copyCmds := apexRule.Args["copy_commands"]
+
+	// Ensure that direct non-stubs dep is always included
+	ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
+
+	// Ensure that indirect stubs dep is not included
+	ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
+
+	// Ensure that dependency of stubs is not included
+	ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
+
+	mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
+
+	// Ensure that mylib is linking with version 10 of libfoo
+	ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
+	// ... and not linking to the non-stub (impl) variant of libfoo
+	ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
+
+	libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
+
+	// Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
+	ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
+}
+
+func TestApexWithSystemLibsStubs(t *testing.T) {
+	ctx := testApex(t, `
+		apex {
+			name: "myapex",
+			key: "myapex.key",
+			native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
+		}
+
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+
+		cc_library {
+			name: "mylib",
+			srcs: ["mylib.cpp"],
+			shared_libs: ["libdl#27"],
+			stl: "none",
+		}
+
+		cc_library_shared {
+			name: "mylib_shared",
+			srcs: ["mylib.cpp"],
+			shared_libs: ["libdl#27"],
+			stl: "none",
+		}
+
+		cc_library {
+			name: "libc",
+			no_libgcc: true,
+			nocrt: true,
+			system_shared_libs: [],
+			stl: "none",
+			stubs: {
+				versions: ["27", "28", "29"],
+			},
+		}
+
+		cc_library {
+			name: "libm",
+			no_libgcc: true,
+			nocrt: true,
+			system_shared_libs: [],
+			stl: "none",
+			stubs: {
+				versions: ["27", "28", "29"],
+			},
+		}
+
+		cc_library {
+			name: "libdl",
+			no_libgcc: true,
+			nocrt: true,
+			system_shared_libs: [],
+			stl: "none",
+			stubs: {
+				versions: ["27", "28", "29"],
+			},
+		}
+	`)
+
+	apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
+	copyCmds := apexRule.Args["copy_commands"]
+
+	// Ensure that mylib, libm, libdl are included.
+	ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
+	ensureContains(t, copyCmds, "image.apex/lib64/libm.so")
+	ensureContains(t, copyCmds, "image.apex/lib64/libdl.so")
+
+	// Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
+	ensureNotContains(t, copyCmds, "image.apex/lib64/libc.so")
+
+	mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
+	mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
+	mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
+
+	// For dependency to libc
+	// Ensure that mylib is linking with the latest version of stubs
+	ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
+	// ... and not linking to the non-stub (impl) variant
+	ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
+	// ... Cflags from stub is correctly exported to mylib
+	ensureContains(t, mylibCFlags, "__LIBC_API__=29")
+	ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
+
+	// For dependency to libm
+	// Ensure that mylib is linking with the non-stub (impl) variant
+	ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
+	// ... and not linking to the stub variant
+	ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
+	// ... and is not compiling with the stub
+	ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
+	ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
+
+	// For dependency to libdl
+	// Ensure that mylib is linking with the specified version of stubs
+	ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
+	// ... and not linking to the other versions of stubs
+	ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
+	ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
+	// ... and not linking to the non-stub (impl) variant
+	ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
+	// ... Cflags from stub is correctly exported to mylib
+	ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
+	ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
+}
+
+func TestFilesInSubDir(t *testing.T) {
+	ctx := testApex(t, `
+		apex {
+			name: "myapex",
+			key: "myapex.key",
+			prebuilts: ["myetc"],
+		}
+
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+
+		prebuilt_etc {
+			name: "myetc",
+			src: "myprebuilt",
+			sub_dir: "foo/bar",
+		}
+	`)
+
+	generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
+	dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
+
+	// Ensure that etc, etc/foo, and etc/foo/bar are all listed
+	ensureListContains(t, dirs, "etc")
+	ensureListContains(t, dirs, "etc/foo")
+	ensureListContains(t, dirs, "etc/foo/bar")
+}
+
+func TestUseVendor(t *testing.T) {
+	ctx := testApex(t, `
+		apex {
+			name: "myapex",
+			key: "myapex.key",
+			native_shared_libs: ["mylib"],
+			use_vendor: true,
+		}
+
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+
+		cc_library {
+			name: "mylib",
+			srcs: ["mylib.cpp"],
+			shared_libs: ["mylib2"],
+			system_shared_libs: [],
+			vendor_available: true,
+			stl: "none",
+		}
+
+		cc_library {
+			name: "mylib2",
+			srcs: ["mylib.cpp"],
+			system_shared_libs: [],
+			vendor_available: true,
+			stl: "none",
+		}
+	`)
+
+	inputsList := []string{}
+	for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
+		for _, implicit := range i.Implicits {
+			inputsList = append(inputsList, implicit.String())
+		}
+	}
+	inputsString := strings.Join(inputsList, " ")
+
+	// ensure that the apex includes vendor variants of the direct and indirect deps
+	ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
+	ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
+
+	// ensure that the apex does not include core variants
+	ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
+	ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
+}
+
+func TestStaticLinking(t *testing.T) {
+	ctx := testApex(t, `
+		apex {
+			name: "myapex",
+			key: "myapex.key",
+			native_shared_libs: ["mylib"],
+		}
+
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+
+		cc_library {
+			name: "mylib",
+			srcs: ["mylib.cpp"],
+			system_shared_libs: [],
+			stl: "none",
+			stubs: {
+				versions: ["1", "2", "3"],
+			},
+		}
+
+		cc_binary {
+			name: "not_in_apex",
+			srcs: ["mylib.cpp"],
+			static_libs: ["mylib"],
+			static_executable: true,
+			system_shared_libs: [],
+			stl: "none",
+		}
+
+		cc_object {
+			name: "crtbegin_static",
+			stl: "none",
+		}
+
+		cc_object {
+			name: "crtend_android",
+			stl: "none",
+		}
+
+	`)
+
+	ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
+
+	// Ensure that not_in_apex is linking with the static variant of mylib
+	ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
+}
diff --git a/apex/key.go b/apex/key.go
index ff348a8..6fbc9b0 100644
--- a/apex/key.go
+++ b/apex/key.go
@@ -45,6 +45,9 @@
 	Public_key *string
 	// Path to the private key file in pem format. Used to sign APEXs.
 	Private_key *string
+
+	// Whether this key is installable to one of the partitions. Defualt: true.
+	Installable *bool
 }
 
 func apexKeyFactory() android.Module {
@@ -54,6 +57,10 @@
 	return module
 }
 
+func (m *apexKey) installable() bool {
+	return m.properties.Installable == nil || proptools.Bool(m.properties.Installable)
+}
+
 func (m *apexKey) DepsMutator(ctx android.BottomUpMutatorContext) {
 }
 
@@ -71,7 +78,9 @@
 	}
 	m.keyName = pubKeyName
 
-	ctx.InstallFile(android.PathForModuleInstall(ctx, "etc/security/apex"), m.keyName, m.public_key_file)
+	if m.installable() {
+		ctx.InstallFile(android.PathForModuleInstall(ctx, "etc/security/apex"), m.keyName, m.public_key_file)
+	}
 }
 
 func (m *apexKey) AndroidMk() android.AndroidMkData {
@@ -82,6 +91,7 @@
 			func(w io.Writer, outputFile android.Path) {
 				fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(TARGET_OUT)/etc/security/apex")
 				fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", m.keyName)
+				fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !m.installable())
 			},
 		},
 	}
diff --git a/cc/androidmk.go b/cc/androidmk.go
index f5e04bb..82feec8 100644
--- a/cc/androidmk.go
+++ b/cc/androidmk.go
@@ -59,7 +59,7 @@
 
 	ret := android.AndroidMkData{
 		OutputFile: c.outputFile,
-		Required:   c.Properties.AndroidMkRuntimeLibs,
+		Required:   append(c.Properties.AndroidMkRuntimeLibs, c.Properties.ApexesProvidingSharedLibs...),
 		Include:    "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
 
 		Extra: []android.AndroidMkExtraFunc{
diff --git a/cc/binary.go b/cc/binary.go
index 6923f2b..c9e6cab 100644
--- a/cc/binary.go
+++ b/cc/binary.go
@@ -51,12 +51,12 @@
 }
 
 func init() {
-	android.RegisterModuleType("cc_binary", binaryFactory)
+	android.RegisterModuleType("cc_binary", BinaryFactory)
 	android.RegisterModuleType("cc_binary_host", binaryHostFactory)
 }
 
 // Module factory for binaries
-func binaryFactory() android.Module {
+func BinaryFactory() android.Module {
 	module, _ := NewBinary(android.HostAndDeviceSupported)
 	return module.Init()
 }
@@ -299,9 +299,6 @@
 
 	var linkerDeps android.Paths
 
-	sharedLibs := deps.SharedLibs
-	sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
-
 	if deps.LinkerFlagsFile.Valid() {
 		flags.LdFlags = append(flags.LdFlags, "$$(cat "+deps.LinkerFlagsFile.String()+")")
 		linkerDeps = append(linkerDeps, deps.LinkerFlagsFile.Path())
@@ -363,8 +360,15 @@
 		binary.injectHostBionicLinkerSymbols(ctx, outputFile, deps.DynamicLinker.Path(), injectedOutputFile)
 	}
 
-	linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
-	linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
+	var sharedLibs android.Paths
+	// Ignore shared libs for static executables.
+	if !binary.static() {
+		sharedLibs = deps.SharedLibs
+		sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
+		linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
+		linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
+	}
+
 	linkerDeps = append(linkerDeps, objs.tidyFiles...)
 	linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
 
diff --git a/cc/builder.go b/cc/builder.go
index bf35f84..5dbd23e 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -55,6 +55,13 @@
 		},
 		"ccCmd", "cFlags")
 
+	ccNoDeps = pctx.AndroidGomaStaticRule("ccNoDeps",
+		blueprint.RuleParams{
+			Command:     "$relPwd ${config.CcWrapper}$ccCmd -c $cFlags -o $out $in",
+			CommandDeps: []string{"$ccCmd"},
+		},
+		"ccCmd", "cFlags")
+
 	ld = pctx.AndroidStaticRule("ld",
 		blueprint.RuleParams{
 			Command: "$ldCmd ${crtBegin} @${out}.rsp " +
@@ -383,9 +390,13 @@
 		tidy := flags.tidy
 		coverage := flags.coverage
 		dump := flags.sAbiDump
+		rule := cc
 
 		switch srcFile.Ext() {
-		case ".S", ".s":
+		case ".s":
+			rule = ccNoDeps
+			fallthrough
+		case ".S":
 			ccCmd = "clang"
 			moduleCflags = asflags
 			tidy = false
@@ -416,7 +427,7 @@
 		}
 
 		ctx.Build(pctx, android.BuildParams{
-			Rule:            cc,
+			Rule:            rule,
 			Description:     ccDesc + " " + srcFile.Rel(),
 			Output:          objFile,
 			ImplicitOutputs: implicitOutputs,
diff --git a/cc/cc.go b/cc/cc.go
index 9fa7c3a..ce28a3b 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -34,12 +34,12 @@
 	android.RegisterModuleType("cc_defaults", defaultsFactory)
 
 	android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
-		ctx.BottomUp("image", imageMutator).Parallel()
+		ctx.BottomUp("image", ImageMutator).Parallel()
 		ctx.BottomUp("link", LinkageMutator).Parallel()
-		ctx.BottomUp("vndk", vndkMutator).Parallel()
+		ctx.BottomUp("vndk", VndkMutator).Parallel()
 		ctx.BottomUp("ndk_api", ndkApiMutator).Parallel()
 		ctx.BottomUp("test_per_src", testPerSrcMutator).Parallel()
-		ctx.BottomUp("version", versionMutator).Parallel()
+		ctx.BottomUp("version", VersionMutator).Parallel()
 		ctx.BottomUp("begin", BeginMutator).Parallel()
 	})
 
@@ -183,12 +183,13 @@
 	// Minimum sdk version supported when compiling against the ndk
 	Sdk_version *string
 
-	AndroidMkSharedLibs      []string `blueprint:"mutated"`
-	AndroidMkStaticLibs      []string `blueprint:"mutated"`
-	AndroidMkRuntimeLibs     []string `blueprint:"mutated"`
-	AndroidMkWholeStaticLibs []string `blueprint:"mutated"`
-	HideFromMake             bool     `blueprint:"mutated"`
-	PreventInstall           bool     `blueprint:"mutated"`
+	AndroidMkSharedLibs       []string `blueprint:"mutated"`
+	AndroidMkStaticLibs       []string `blueprint:"mutated"`
+	AndroidMkRuntimeLibs      []string `blueprint:"mutated"`
+	AndroidMkWholeStaticLibs  []string `blueprint:"mutated"`
+	HideFromMake              bool     `blueprint:"mutated"`
+	PreventInstall            bool     `blueprint:"mutated"`
+	ApexesProvidingSharedLibs []string `blueprint:"mutated"`
 
 	UseVndk bool `blueprint:"mutated"`
 
@@ -247,6 +248,8 @@
 	baseModuleName() string
 	getVndkExtendsModuleName() string
 	isPgoCompile() bool
+	useClangLld(actx ModuleContext) bool
+	isApex() bool
 }
 
 type ModuleContext interface {
@@ -287,6 +290,7 @@
 	linkerDeps(ctx DepsContext, deps Deps) Deps
 	linkerFlags(ctx ModuleContext, flags Flags) Flags
 	linkerProps() []interface{}
+	useClangLld(actx ModuleContext) bool
 
 	link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
 	appendLdflags([]string)
@@ -306,6 +310,8 @@
 	library bool
 
 	reexportFlags bool
+
+	explicitlyVersioned bool
 }
 
 var (
@@ -509,6 +515,20 @@
 	return c.ModuleBase.InstallInRecovery()
 }
 
+func (c *Module) IsStubs() bool {
+	if library, ok := c.linker.(*libraryDecorator); ok {
+		return library.buildStubs()
+	}
+	return false
+}
+
+func (c *Module) HasStubsVariants() bool {
+	if library, ok := c.linker.(*libraryDecorator); ok {
+		return len(library.Properties.Stubs.Versions) > 0
+	}
+	return false
+}
+
 type baseModuleContext struct {
 	android.BaseContext
 	moduleContextImpl
@@ -614,6 +634,10 @@
 		// Host modules do not need ABI dumps.
 		return false
 	}
+	if !ctx.mod.IsForPlatform() {
+		// APEX variants do not need ABI dumps.
+		return false
+	}
 	if inList(ctx.baseModuleName(), llndkLibraries) {
 		return true
 	}
@@ -635,6 +659,10 @@
 	return ""
 }
 
+func (ctx *moduleContextImpl) useClangLld(actx ModuleContext) bool {
+	return ctx.mod.linker.useClangLld(actx)
+}
+
 func (ctx *moduleContextImpl) baseModuleName() string {
 	return ctx.mod.ModuleBase.BaseModuleName()
 }
@@ -643,6 +671,11 @@
 	return ctx.mod.getVndkExtendsModuleName()
 }
 
+// Tests if this module is built for APEX
+func (ctx *moduleContextImpl) isApex() bool {
+	return ctx.mod.ApexName() != ""
+}
+
 func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
 	return &Module{
 		hod:      hod,
@@ -1075,6 +1108,30 @@
 		{Mutator: "link", Variation: "static"},
 	}, lateStaticDepTag, deps.LateStaticLibs...)
 
+	addSharedLibDependencies := func(depTag dependencyTag, name string, version string) {
+		var variations []blueprint.Variation
+		variations = append(variations, blueprint.Variation{Mutator: "link", Variation: "shared"})
+		versionVariantAvail := !ctx.useVndk() && !c.inRecovery()
+		if version != "" && versionVariantAvail {
+			// Version is explicitly specified. i.e. libFoo#30
+			variations = append(variations, blueprint.Variation{Mutator: "version", Variation: version})
+			depTag.explicitlyVersioned = true
+		}
+		actx.AddVariationDependencies(variations, depTag, name)
+
+		// If the version is not specified, add dependency to the latest stubs library.
+		// The stubs library will be used when the depending module is built for APEX and
+		// the dependent module is not in the same APEX.
+		latestVersion := latestStubsVersionFor(actx.Config(), name)
+		if version == "" && latestVersion != "" && versionVariantAvail {
+			actx.AddVariationDependencies([]blueprint.Variation{
+				{Mutator: "link", Variation: "shared"},
+				{Mutator: "version", Variation: latestVersion},
+			}, depTag, name)
+			// Note that depTag.explicitlyVersioned is false in this case.
+		}
+	}
+
 	// shared lib names without the #version suffix
 	var sharedLibNames []string
 
@@ -1085,29 +1142,17 @@
 		if inList(lib, deps.ReexportSharedLibHeaders) {
 			depTag = sharedExportDepTag
 		}
-		var variations []blueprint.Variation
-		variations = append(variations, blueprint.Variation{Mutator: "link", Variation: "shared"})
-		if version != "" && ctx.Os() == android.Android && !ctx.useVndk() && !c.inRecovery() {
-			variations = append(variations, blueprint.Variation{Mutator: "version", Variation: version})
-		}
-		actx.AddVariationDependencies(variations, depTag, name)
+		addSharedLibDependencies(depTag, name, version)
 	}
 
 	for _, lib := range deps.LateSharedLibs {
-		name, version := stubsLibNameAndVersion(lib)
-		if inList(name, sharedLibNames) {
+		if inList(lib, sharedLibNames) {
 			// This is to handle the case that some of the late shared libs (libc, libdl, libm, ...)
 			// are added also to SharedLibs with version (e.g., libc#10). If not skipped, we will be
 			// linking against both the stubs lib and the non-stubs lib at the same time.
 			continue
 		}
-		depTag := lateSharedDepTag
-		var variations []blueprint.Variation
-		variations = append(variations, blueprint.Variation{Mutator: "link", Variation: "shared"})
-		if version != "" && ctx.Os() == android.Android && !ctx.useVndk() && !c.inRecovery() {
-			variations = append(variations, blueprint.Variation{Mutator: "version", Variation: version})
-		}
-		actx.AddVariationDependencies(variations, depTag, name)
+		addSharedLibDependencies(lateSharedDepTag, lib, "")
 	}
 
 	actx.AddVariationDependencies([]blueprint.Variation{
@@ -1366,7 +1411,57 @@
 				return
 			}
 		}
+
+		// Extract explicitlyVersioned field from the depTag and reset it inside the struct.
+		// Otherwise, sharedDepTag and lateSharedDepTag with explicitlyVersioned set to true
+		// won't be matched to sharedDepTag and lateSharedDepTag.
+		explicitlyVersioned := false
+		if t, ok := depTag.(dependencyTag); ok {
+			explicitlyVersioned = t.explicitlyVersioned
+			t.explicitlyVersioned = false
+			depTag = t
+		}
+
 		if t, ok := depTag.(dependencyTag); ok && t.library {
+			depIsStatic := false
+			switch depTag {
+			case staticDepTag, staticExportDepTag, lateStaticDepTag, wholeStaticDepTag:
+				depIsStatic = true
+			}
+			if dependentLibrary, ok := ccDep.linker.(*libraryDecorator); ok && !depIsStatic {
+				depIsStubs := dependentLibrary.buildStubs()
+				depHasStubs := ccDep.HasStubsVariants()
+				depInSameApex := android.DirectlyInApex(c.ApexName(), depName)
+				depInPlatform := !android.DirectlyInAnyApex(depName)
+
+				var useThisDep bool
+				if depIsStubs && explicitlyVersioned {
+					// Always respect dependency to the versioned stubs (i.e. libX#10)
+					useThisDep = true
+				} else if !depHasStubs {
+					// Use non-stub variant if that is the only choice
+					// (i.e. depending on a lib without stubs.version property)
+					useThisDep = true
+				} else if c.IsForPlatform() {
+					// If not building for APEX, use stubs only when it is from
+					// an APEX (and not from platform)
+					useThisDep = (depInPlatform != depIsStubs)
+					if c.inRecovery() {
+						// However, for recovery modules, since there is no APEX there,
+						// always link to non-stub variant
+						useThisDep = !depIsStubs
+					}
+				} else {
+					// If building for APEX, use stubs only when it is not from
+					// the same APEX
+					useThisDep = (depInSameApex != depIsStubs)
+				}
+
+				if !useThisDep {
+					return // stop processing this dep
+				}
+			}
+
 			if i, ok := ccDep.linker.(exportedFlagsProducer); ok {
 				flags := i.exportedFlags()
 				deps := i.exportedFlagsDeps()
@@ -1493,6 +1588,20 @@
 		// Export the shared libs to Make.
 		switch depTag {
 		case sharedDepTag, sharedExportDepTag, lateSharedDepTag:
+			// Dependency to the stubs lib which is already included in an APEX
+			// is not added to the androidmk dependency
+			if dependentLibrary, ok := ccDep.linker.(*libraryDecorator); ok {
+				if dependentLibrary.buildStubs() && android.InAnyApex(depName) {
+					// Also add the dependency to the APEX(es) providing the library so that
+					// m <module> can trigger building the APEXes as well.
+					for _, an := range android.GetApexesForModule(depName) {
+						c.Properties.ApexesProvidingSharedLibs = append(
+							c.Properties.ApexesProvidingSharedLibs, an)
+					}
+					break
+				}
+			}
+
 			// Note: the order of libs in this list is not important because
 			// they merely serve as Make dependencies and do not affect this lib itself.
 			c.Properties.AndroidMkSharedLibs = append(
@@ -1692,7 +1801,7 @@
 	}
 }
 
-func imageMutator(mctx android.BottomUpMutatorContext) {
+func ImageMutator(mctx android.BottomUpMutatorContext) {
 	if mctx.Os() != android.Android {
 		return
 	}
@@ -1751,6 +1860,7 @@
 
 	// Sanity check
 	vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
+	productSpecific := mctx.ProductSpecific()
 
 	if m.VendorProperties.Vendor_available != nil && vendorSpecific {
 		mctx.PropertyErrorf("vendor_available",
@@ -1760,6 +1870,11 @@
 
 	if vndkdep := m.vndkdep; vndkdep != nil {
 		if vndkdep.isVndk() {
+			if productSpecific {
+				mctx.PropertyErrorf("product_specific",
+					"product_specific must not be true when `vndk: {enabled: true}`")
+				return
+			}
 			if vendorSpecific {
 				if !vndkdep.isVndkExt() {
 					mctx.PropertyErrorf("vndk",
diff --git a/cc/cc_test.go b/cc/cc_test.go
index 3e78ec7..96233a1 100644
--- a/cc/cc_test.go
+++ b/cc/cc_test.go
@@ -53,20 +53,21 @@
 
 func createTestContext(t *testing.T, config android.Config, bp string) *android.TestContext {
 	ctx := android.NewTestArchContext()
+	ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(BinaryFactory))
 	ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(LibraryFactory))
 	ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(LibrarySharedFactory))
 	ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(LibraryHeaderFactory))
 	ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(ToolchainLibraryFactory))
-	ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(llndkLibraryFactory))
+	ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(LlndkLibraryFactory))
 	ctx.RegisterModuleType("llndk_headers", android.ModuleFactoryAdaptor(llndkHeadersFactory))
 	ctx.RegisterModuleType("vendor_public_library", android.ModuleFactoryAdaptor(vendorPublicLibraryFactory))
 	ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(ObjectFactory))
 	ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
 	ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
-		ctx.BottomUp("image", imageMutator).Parallel()
+		ctx.BottomUp("image", ImageMutator).Parallel()
 		ctx.BottomUp("link", LinkageMutator).Parallel()
-		ctx.BottomUp("vndk", vndkMutator).Parallel()
-		ctx.BottomUp("version", versionMutator).Parallel()
+		ctx.BottomUp("vndk", VndkMutator).Parallel()
+		ctx.BottomUp("version", VersionMutator).Parallel()
 		ctx.BottomUp("begin", BeginMutator).Parallel()
 	})
 	ctx.Register()
@@ -194,11 +195,23 @@
 		}
 
 		cc_object {
+			name: "crtbegin_static",
+			recovery_available: true,
+			vendor_available: true,
+		}
+
+		cc_object {
 			name: "crtend_so",
 			recovery_available: true,
 			vendor_available: true,
 		}
 
+		cc_object {
+			name: "crtend_android",
+			recovery_available: true,
+			vendor_available: true,
+		}
+
 		cc_library {
 			name: "libprotobuf-cpp-lite",
 		}
@@ -492,6 +505,21 @@
 	`)
 }
 
+func TestVndkMustNotBeProductSpecific(t *testing.T) {
+	// Check whether an error is emitted when a vndk lib has 'product_specific: true'.
+	testCcError(t, "product_specific must not be true when `vndk: {enabled: true}`", `
+		cc_library {
+			name: "libvndk",
+			product_specific: true,  // Cause error
+			vendor_available: true,
+			vndk: {
+				enabled: true,
+			},
+			nocrt: true,
+		}
+	`)
+}
+
 func TestVndkExt(t *testing.T) {
 	// This test checks the VNDK-Ext properties.
 	ctx := testCc(t, `
@@ -1830,3 +1858,28 @@
 		t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
 	}
 }
+
+func TestStaticExecutable(t *testing.T) {
+	ctx := testCc(t, `
+		cc_binary {
+			name: "static_test",
+			srcs: ["foo.c"],
+			static_executable: true,
+		}`)
+
+	variant := "android_arm64_armv8-a_core"
+	binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
+	libFlags := binModuleRule.Args["libFlags"]
+	systemStaticLibs := []string{"libc.a", "libm.a", "libdl.a"}
+	for _, lib := range systemStaticLibs {
+		if !strings.Contains(libFlags, lib) {
+			t.Errorf("Static lib %q was not found in %q", lib, libFlags)
+		}
+	}
+	systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
+	for _, lib := range systemSharedLibs {
+		if strings.Contains(libFlags, lib) {
+			t.Errorf("Shared lib %q was found in %q", lib, libFlags)
+		}
+	}
+}
diff --git a/cc/compiler.go b/cc/compiler.go
index ad1fc6d..63d2ade 100644
--- a/cc/compiler.go
+++ b/cc/compiler.go
@@ -75,6 +75,10 @@
 	// be added to the include path using -I
 	Local_include_dirs []string `android:"arch_variant,variant_prepend"`
 
+	// Add the directory containing the Android.bp file to the list of include
+	// directories. Defaults to true.
+	Include_build_directory *bool
+
 	// list of generated sources to compile. These are the names of gensrcs or
 	// genrule modules.
 	Generated_sources []string `android:"arch_variant"`
@@ -288,8 +292,11 @@
 		flags.YasmFlags = append(flags.YasmFlags, f)
 	}
 
-	flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.PathForModuleSrc(ctx).String())
-	flags.YasmFlags = append(flags.YasmFlags, "-I"+android.PathForModuleSrc(ctx).String())
+	if compiler.Properties.Include_build_directory == nil ||
+		*compiler.Properties.Include_build_directory {
+		flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.PathForModuleSrc(ctx).String())
+		flags.YasmFlags = append(flags.YasmFlags, "-I"+android.PathForModuleSrc(ctx).String())
+	}
 
 	if !(ctx.useSdk() || ctx.useVndk()) || ctx.Host() {
 		flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
@@ -414,9 +421,6 @@
 		cppStd = config.CppStdVersion
 	case "experimental":
 		cppStd = config.ExperimentalCppStdVersion
-	case "c++17", "gnu++17":
-		// Map c++17 and gnu++17 to their 1z equivalents, until 17 is finalized.
-		cppStd = strings.Replace(String(compiler.Properties.Cpp_std), "17", "1z", 1)
 	}
 
 	if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
diff --git a/cc/config/arm64_device.go b/cc/config/arm64_device.go
index 6a63828..f98e1be 100644
--- a/cc/config/arm64_device.go
+++ b/cc/config/arm64_device.go
@@ -68,6 +68,10 @@
 		"kryo": []string{
 			"-mcpu=kryo",
 		},
+		"kryo385": []string{
+			// Use cortex-a53 because kryo385 is not supported in GCC/clang.
+			"-mcpu=cortex-a53",
+		},
 		"exynos-m1": []string{
 			"-mcpu=exynos-m1",
 		},
@@ -92,9 +96,9 @@
 		"cortex-a75",
 		"cortex-a76",
 		"kryo",
+		"kryo385",
 		"exynos-m1",
-		"exynos-m2",
-		"denver64")
+		"exynos-m2")
 
 	pctx.StaticVariable("arm64GccVersion", arm64GccVersion)
 
@@ -144,6 +148,7 @@
 		"cortex-a75": "${config.Arm64ClangCortexA55Cflags}",
 		"cortex-a76": "${config.Arm64ClangCortexA55Cflags}",
 		"kryo":       "${config.Arm64ClangKryoCflags}",
+		"kryo385":    "${config.Arm64ClangCortexA53Cflags}",
 		"exynos-m1":  "${config.Arm64ClangExynosM1Cflags}",
 		"exynos-m2":  "${config.Arm64ClangExynosM2Cflags}",
 	}
@@ -220,10 +225,7 @@
 
 	var extraLdflags string
 	switch arch.CpuVariant {
-	case "cortex-a53", "cortex-a72", "cortex-a73", "kryo", "exynos-m1", "exynos-m2",
-		// This variant might not need the workaround but leave it
-		// in the list since it has had the workaround on before.
-		"denver64":
+	case "cortex-a53", "cortex-a72", "cortex-a73", "kryo", "exynos-m1", "exynos-m2":
 		extraLdflags = "-Wl,--fix-cortex-a53-843419"
 	}
 
diff --git a/cc/config/arm_device.go b/cc/config/arm_device.go
index d759125..60bb91a 100644
--- a/cc/config/arm_device.go
+++ b/cc/config/arm_device.go
@@ -151,6 +151,15 @@
 			// better solution comes around. See Bug 27340895
 			"-D__ARM_FEATURE_LPAE=1",
 		},
+		"kryo385": []string{
+			// Use cortex-a53 because kryo385 is not supported in GCC/clang.
+			"-mcpu=cortex-a53",
+			// Fake an ARM compiler flag as these processors support LPAE which GCC/clang
+			// don't advertise.
+			// TODO This is a hack and we need to add it for each processor that supports LPAE until some
+			// better solution comes around. See Bug 27340895
+			"-D__ARM_FEATURE_LPAE=1",
+		},
 	}
 )
 
@@ -180,9 +189,9 @@
 		"cortex-a76",
 		"krait",
 		"kryo",
+		"kryo385",
 		"exynos-m1",
-		"exynos-m2",
-		"denver")
+		"exynos-m2")
 
 	android.RegisterArchVariantFeatures(android.Arm, "armv7-a-neon", "neon")
 	android.RegisterArchVariantFeatures(android.Arm, "armv8-a", "neon")
@@ -242,7 +251,7 @@
 		"armv7-a":      "${config.ArmClangArmv7ACflags}",
 		"armv7-a-neon": "${config.ArmClangArmv7ANeonCflags}",
 		"armv8-a":      "${config.ArmClangArmv8ACflags}",
-		"armv8-2a":      "${config.ArmClangArmv82ACflags}",
+		"armv8-2a":     "${config.ArmClangArmv82ACflags}",
 	}
 
 	armClangCpuVariantCflagsVar = map[string]string{
@@ -258,9 +267,9 @@
 		"cortex-a75":     "${config.ArmClangCortexA55Cflags}",
 		"krait":          "${config.ArmClangKraitCflags}",
 		"kryo":           "${config.ArmClangKryoCflags}",
+		"kryo385":        "${config.ArmClangCortexA53Cflags}",
 		"exynos-m1":      "${config.ArmClangCortexA53Cflags}",
 		"exynos-m2":      "${config.ArmClangCortexA53Cflags}",
-		"denver":         "${config.ArmClangCortexA15Cflags}",
 	}
 )
 
diff --git a/cc/config/clang.go b/cc/config/clang.go
index 0f22034..a0ebd10 100644
--- a/cc/config/clang.go
+++ b/cc/config/clang.go
@@ -91,7 +91,7 @@
 	"-Wl,-m,aarch64_elf64_le_vec",
 })
 
-var ClangLibToolingUnknownCflags []string = nil
+var ClangLibToolingUnknownCflags = sorted([]string{})
 
 func init() {
 	pctx.StaticVariable("ClangExtraCflags", strings.Join([]string{
@@ -120,14 +120,10 @@
 		// color codes if it is not running in a terminal.
 		"-fcolor-diagnostics",
 
-		// http://b/29823425 Disable -Wexpansion-to-defined for Clang update to r271374
-		"-Wno-expansion-to-defined",
-
 		// http://b/68236239 Allow 0/NULL instead of using nullptr everywhere.
 		"-Wno-zero-as-null-pointer-constant",
 
 		// Warnings from clang-7.0
-		"-Wno-deprecated-register",
 		"-Wno-sign-compare",
 
 		// Warnings from clang-8.0
@@ -171,10 +167,6 @@
 		"-Wno-tautological-unsigned-enum-zero-compare",
 		"-Wno-tautological-unsigned-zero-compare",
 
-		// http://b/72331524 Allow null pointer arithmetic until the instances detected by
-		// this new warning are fixed.
-		"-Wno-null-pointer-arithmetic",
-
 		// http://b/72330874 Disable -Wenum-compare until the instances detected by this new
 		// warning are fixed.
 		"-Wno-enum-compare",
@@ -184,22 +176,17 @@
 		// compatibility.
 		"-Wno-c++98-compat-extra-semi",
 
-		// Disable this warning until we can fix all instances where it fails.
-		"-Wno-self-assign-overloaded",
-
-		// Disable this warning until we can fix all instances where it fails.
-		"-Wno-constant-logical-operand",
-
 		// Disable this warning because we don't care about behavior with older compilers.
 		"-Wno-return-std-move-in-c++11",
-
-		// Disable this warning until we can fix all instances where it fails.
-		"-Wno-dangling-field",
 	}, " "))
 
 	// Extra cflags for projects under external/ directory
 	pctx.StaticVariable("ClangExtraExternalCflags", strings.Join([]string{
 		// TODO(yikong): Move -Wno flags here
+
+		// http://b/72331524 Allow null pointer arithmetic until the instances detected by
+		// this new warning are fixed.
+		"-Wno-null-pointer-arithmetic",
 	}, " "))
 }
 
diff --git a/cc/config/global.go b/cc/config/global.go
index e2377e3..13ad27c 100644
--- a/cc/config/global.go
+++ b/cc/config/global.go
@@ -87,8 +87,6 @@
 
 	deviceGlobalLldflags = append(ClangFilterUnknownLldflags(deviceGlobalLdflags),
 		[]string{
-			"-Wl,--pack-dyn-relocs=android+relr",
-			"-Wl,--use-android-relr-tags",
 			"-fuse-ld=lld",
 		}...)
 
@@ -114,16 +112,16 @@
 	}
 
 	CStdVersion               = "gnu99"
-	CppStdVersion             = "gnu++14"
+	CppStdVersion             = "gnu++17"
 	ExperimentalCStdVersion   = "gnu11"
-	ExperimentalCppStdVersion = "gnu++1z"
+	ExperimentalCppStdVersion = "gnu++2a"
 
 	NdkMaxPrebuiltVersionInt = 27
 
 	// prebuilts/clang default settings.
 	ClangDefaultBase         = "prebuilts/clang/host"
-	ClangDefaultVersion      = "clang-r344140b"
-	ClangDefaultShortVersion = "8.0.4"
+	ClangDefaultVersion      = "clang-r346389b"
+	ClangDefaultShortVersion = "8.0.6"
 
 	// Directories with warnings from Android.bp files.
 	WarningAllowedProjects = []string{
diff --git a/cc/config/x86_windows_host.go b/cc/config/x86_windows_host.go
index 6300a1b..dfdd40c 100644
--- a/cc/config/x86_windows_host.go
+++ b/cc/config/x86_windows_host.go
@@ -33,9 +33,9 @@
 
 		// Use C99-compliant printf functions (%zd).
 		"-D__USE_MINGW_ANSI_STDIO=1",
-		// Admit to using >= Vista. Both are needed because of <_mingw.h>.
-		"-D_WIN32_WINNT=0x0600",
-		"-DWINVER=0x0600",
+		// Admit to using >= Windows 7. Both are needed because of <_mingw.h>.
+		"-D_WIN32_WINNT=0x0601",
+		"-DWINVER=0x0601",
 		// Get 64-bit off_t and related functions.
 		"-D_FILE_OFFSET_BITS=64",
 
@@ -140,6 +140,9 @@
 	pctx.StaticVariable("WindowsX8664ClangCppflags", strings.Join(windowsX8664ClangCppflags, " "))
 
 	pctx.StaticVariable("WindowsIncludeFlags", strings.Join(windowsIncludeFlags, " "))
+	// Yasm flags
+	pctx.StaticVariable("WindowsX86YasmFlags", "-f win32 -m x86")
+	pctx.StaticVariable("WindowsX8664YasmFlags", "-f win64 -m amd64")
 }
 
 type toolchainWindows struct {
@@ -228,6 +231,14 @@
 	return "${config.WindowsClangLldflags} ${config.WindowsX8664ClangLldflags}"
 }
 
+func (t *toolchainWindowsX86) YasmFlags() string {
+	return "${config.WindowsX86YasmFlags}"
+}
+
+func (t *toolchainWindowsX8664) YasmFlags() string {
+	return "${config.WindowsX8664YasmFlags}"
+}
+
 func (t *toolchainWindows) ShlibSuffix() string {
 	return ".dll"
 }
diff --git a/cc/gen.go b/cc/gen.go
index 4852794..c3088f4 100644
--- a/cc/gen.go
+++ b/cc/gen.go
@@ -14,10 +14,6 @@
 
 package cc
 
-// This file generates the final rules for compiling all C/C++.  All properties related to
-// compiling should have been translated into builderFlags or another argument to the Transform*
-// functions.
-
 import (
 	"path/filepath"
 
diff --git a/cc/gen_stub_libs.py b/cc/gen_stub_libs.py
index c49d197..4906ea2 100755
--- a/cc/gen_stub_libs.py
+++ b/cc/gen_stub_libs.py
@@ -108,7 +108,7 @@
     return version.endswith('_PRIVATE') or version.endswith('_PLATFORM')
 
 
-def should_omit_version(version, arch, api, vndk):
+def should_omit_version(version, arch, api, vndk, apex):
     """Returns True if the version section should be ommitted.
 
     We want to omit any sections that do not have any symbols we'll have in the
@@ -121,6 +121,8 @@
         return True
     if 'vndk' in version.tags and not vndk:
         return True
+    if 'apex' in version.tags and not apex:
+        return True
     if not symbol_in_arch(version.tags, arch):
         return True
     if not symbol_in_api(version.tags, arch, api):
@@ -128,10 +130,12 @@
     return False
 
 
-def should_omit_symbol(symbol, arch, api, vndk):
+def should_omit_symbol(symbol, arch, api, vndk, apex):
     """Returns True if the symbol should be omitted."""
     if not vndk and 'vndk' in symbol.tags:
         return True
+    if not apex and 'apex' in symbol.tags:
+        return True
     if not symbol_in_arch(symbol.tags, arch):
         return True
     if not symbol_in_api(symbol.tags, arch, api):
@@ -239,15 +243,15 @@
     def __eq__(self, other):
         return self.name == other.name and set(self.tags) == set(other.tags)
 
-
 class SymbolFileParser(object):
     """Parses NDK symbol files."""
-    def __init__(self, input_file, api_map, arch, api, vndk):
+    def __init__(self, input_file, api_map, arch, api, vndk, apex):
         self.input_file = input_file
         self.api_map = api_map
         self.arch = arch
         self.api = api
         self.vndk = vndk
+        self.apex = apex
         self.current_line = None
 
     def parse(self):
@@ -275,11 +279,11 @@
         symbol_names = set()
         multiply_defined_symbols = set()
         for version in versions:
-            if should_omit_version(version, self.arch, self.api, self.vndk):
+            if should_omit_version(version, self.arch, self.api, self.vndk, self.apex):
                 continue
 
             for symbol in version.symbols:
-                if should_omit_symbol(symbol, self.arch, self.api, self.vndk):
+                if should_omit_symbol(symbol, self.arch, self.api, self.vndk, self.apex):
                     continue
 
                 if symbol.name in symbol_names:
@@ -363,12 +367,13 @@
 
 class Generator(object):
     """Output generator that writes stub source files and version scripts."""
-    def __init__(self, src_file, version_script, arch, api, vndk):
+    def __init__(self, src_file, version_script, arch, api, vndk, apex):
         self.src_file = src_file
         self.version_script = version_script
         self.arch = arch
         self.api = api
         self.vndk = vndk
+        self.apex = apex
 
     def write(self, versions):
         """Writes all symbol data to the output files."""
@@ -377,14 +382,14 @@
 
     def write_version(self, version):
         """Writes a single version block's data to the output files."""
-        if should_omit_version(version, self.arch, self.api, self.vndk):
+        if should_omit_version(version, self.arch, self.api, self.vndk, self.apex):
             return
 
         section_versioned = symbol_versioned_in_api(version.tags, self.api)
         version_empty = True
         pruned_symbols = []
         for symbol in version.symbols:
-            if should_omit_symbol(symbol, self.arch, self.api, self.vndk):
+            if should_omit_symbol(symbol, self.arch, self.api, self.vndk, self.apex):
                 continue
 
             if symbol_versioned_in_api(symbol.tags, self.api):
@@ -447,6 +452,8 @@
         help='Architecture being targeted.')
     parser.add_argument(
         '--vndk', action='store_true', help='Use the VNDK variant.')
+    parser.add_argument(
+        '--apex', action='store_true', help='Use the APEX variant.')
 
     parser.add_argument(
         '--api-map', type=os.path.realpath, required=True,
@@ -481,14 +488,14 @@
     with open(args.symbol_file) as symbol_file:
         try:
             versions = SymbolFileParser(symbol_file, api_map, args.arch, api,
-                                        args.vndk).parse()
+                                        args.vndk, args.apex).parse()
         except MultiplyDefinedSymbolError as ex:
             sys.exit('{}: error: {}'.format(args.symbol_file, ex))
 
     with open(args.stub_src, 'w') as src_file:
         with open(args.version_script, 'w') as version_file:
             generator = Generator(src_file, version_file, args.arch, api,
-                                  args.vndk)
+                                  args.vndk, args.apex)
             generator.write(versions)
 
 
diff --git a/cc/library.go b/cc/library.go
index a9d63f9..da223dc 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -16,6 +16,8 @@
 
 import (
 	"regexp"
+	"sort"
+	"strconv"
 	"strings"
 	"sync"
 
@@ -32,19 +34,21 @@
 		Srcs   []string `android:"arch_variant"`
 		Cflags []string `android:"arch_variant"`
 
-		Enabled           *bool    `android:"arch_variant"`
-		Whole_static_libs []string `android:"arch_variant"`
-		Static_libs       []string `android:"arch_variant"`
-		Shared_libs       []string `android:"arch_variant"`
+		Enabled            *bool    `android:"arch_variant"`
+		Whole_static_libs  []string `android:"arch_variant"`
+		Static_libs        []string `android:"arch_variant"`
+		Shared_libs        []string `android:"arch_variant"`
+		System_shared_libs []string `android:"arch_variant"`
 	} `android:"arch_variant"`
 	Shared struct {
 		Srcs   []string `android:"arch_variant"`
 		Cflags []string `android:"arch_variant"`
 
-		Enabled           *bool    `android:"arch_variant"`
-		Whole_static_libs []string `android:"arch_variant"`
-		Static_libs       []string `android:"arch_variant"`
-		Shared_libs       []string `android:"arch_variant"`
+		Enabled            *bool    `android:"arch_variant"`
+		Whole_static_libs  []string `android:"arch_variant"`
+		Static_libs        []string `android:"arch_variant"`
+		Shared_libs        []string `android:"arch_variant"`
+		System_shared_libs []string `android:"arch_variant"`
 	} `android:"arch_variant"`
 
 	// local file name to pass to the linker as -unexported_symbols_list
@@ -347,6 +351,24 @@
 
 	flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
 	if library.buildStubs() {
+		// Remove -include <file> when compiling stubs. Otherwise, the force included
+		// headers might cause conflicting types error with the symbols in the
+		// generated stubs source code. e.g.
+		// double acos(double); // in header
+		// void acos() {} // in the generated source code
+		removeInclude := func(flags []string) []string {
+			ret := flags[:0]
+			for _, f := range flags {
+				if strings.HasPrefix(f, "-include ") {
+					continue
+				}
+				ret = append(ret, f)
+			}
+			return ret
+		}
+		flags.GlobalFlags = removeInclude(flags.GlobalFlags)
+		flags.CFlags = removeInclude(flags.CFlags)
+
 		flags = addStubLibraryCompilerFlags(flags)
 	}
 	return flags
@@ -373,7 +395,7 @@
 
 func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
 	if library.buildStubs() {
-		objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "")
+		objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "--apex")
 		library.versionScriptPath = versionScript
 		return objs
 	}
@@ -411,11 +433,11 @@
 	buildFlags := flagsToBuilderFlags(flags)
 
 	if library.static() {
-		srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
+		srcs := ctx.ExpandSources(library.Properties.Static.Srcs, nil)
 		objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
 			srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
 	} else if library.shared() {
-		srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
+		srcs := ctx.ExpandSources(library.Properties.Shared.Srcs, nil)
 		objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
 			srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
 	}
@@ -487,7 +509,29 @@
 	}
 }
 
+func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
+	deps = library.baseCompiler.compilerDeps(ctx, deps)
+
+	if library.static() {
+		android.ExtractSourcesDeps(ctx, library.Properties.Static.Srcs)
+	} else if library.shared() {
+		android.ExtractSourcesDeps(ctx, library.Properties.Shared.Srcs)
+	}
+
+	return deps
+}
+
 func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
+	if library.static() {
+		if library.Properties.Static.System_shared_libs != nil {
+			library.baseLinker.Properties.System_shared_libs = library.Properties.Static.System_shared_libs
+		}
+	} else if library.shared() {
+		if library.Properties.Shared.System_shared_libs != nil {
+			library.baseLinker.Properties.System_shared_libs = library.Properties.Shared.System_shared_libs
+		}
+	}
+
 	deps = library.baseLinker.linkerDeps(ctx, deps)
 
 	if library.static() {
@@ -763,6 +807,7 @@
 			"-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
 		}
 		library.reexportFlags(flags)
+		library.reexportDeps(library.baseCompiler.pathDeps)
 		library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
 	}
 
@@ -921,8 +966,19 @@
 func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
 	if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
 		sharedCompiler := shared.compiler.(*libraryDecorator)
+
+		// Check libraries in addition to cflags, since libraries may be exporting different
+		// include directories.
 		if len(staticCompiler.Properties.Static.Cflags) == 0 &&
-			len(sharedCompiler.Properties.Shared.Cflags) == 0 {
+			len(sharedCompiler.Properties.Shared.Cflags) == 0 &&
+			len(staticCompiler.Properties.Static.Whole_static_libs) == 0 &&
+			len(sharedCompiler.Properties.Shared.Whole_static_libs) == 0 &&
+			len(staticCompiler.Properties.Static.Static_libs) == 0 &&
+			len(sharedCompiler.Properties.Shared.Static_libs) == 0 &&
+			len(staticCompiler.Properties.Static.Shared_libs) == 0 &&
+			len(sharedCompiler.Properties.Shared.Shared_libs) == 0 &&
+			staticCompiler.Properties.Static.System_shared_libs == nil &&
+			sharedCompiler.Properties.Shared.System_shared_libs == nil {
 
 			mctx.AddInterVariantDependency(reuseObjTag, shared, static)
 			sharedCompiler.baseCompiler.Properties.OriginalSrcs =
@@ -958,30 +1014,64 @@
 	}
 }
 
-// Version mutator splits a module into the mandatory non-stubs variant
-// (which is named "impl") and zero or more stubs variants.
-func versionMutator(mctx android.BottomUpMutatorContext) {
-	if mctx.Os() != android.Android {
-		return
-	}
+// maps a module name to the list of stubs versions available for the module
+func stubsVersionsFor(config android.Config) map[string][]string {
+	return config.Once("stubVersions", func() interface{} {
+		return make(map[string][]string)
+	}).(map[string][]string)
+}
 
+var stubsVersionsLock sync.Mutex
+
+func latestStubsVersionFor(config android.Config, name string) string {
+	versions, ok := stubsVersionsFor(config)[name]
+	if ok && len(versions) > 0 {
+		// the versions are alreay sorted in ascending order
+		return versions[len(versions)-1]
+	}
+	return ""
+}
+
+// Version mutator splits a module into the mandatory non-stubs variant
+// (which is unnamed) and zero or more stubs variants.
+func VersionMutator(mctx android.BottomUpMutatorContext) {
 	if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
-		if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() {
-			versions := []string{""}
+		if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() &&
+			len(library.Properties.Stubs.Versions) > 0 {
+			versions := []string{}
 			for _, v := range library.Properties.Stubs.Versions {
+				if _, err := strconv.Atoi(v); err != nil {
+					mctx.PropertyErrorf("versions", "%q is not a number", v)
+				}
 				versions = append(versions, v)
 			}
+			sort.Slice(versions, func(i, j int) bool {
+				left, _ := strconv.Atoi(versions[i])
+				right, _ := strconv.Atoi(versions[j])
+				return left < right
+			})
+
+			// save the list of versions for later use
+			copiedVersions := make([]string, len(versions))
+			copy(copiedVersions, versions)
+			stubsVersionsLock.Lock()
+			defer stubsVersionsLock.Unlock()
+			stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = copiedVersions
+
+			// "" is for the non-stubs variant
+			versions = append([]string{""}, versions...)
+
 			modules := mctx.CreateVariations(versions...)
 			for i, m := range modules {
 				l := m.(*Module).linker.(*libraryDecorator)
-				if i == 0 {
-					l.MutatedProperties.BuildStubs = false
-					continue
+				if versions[i] != "" {
+					l.MutatedProperties.BuildStubs = true
+					l.MutatedProperties.StubsVersion = versions[i]
+					m.(*Module).Properties.HideFromMake = true
+					m.(*Module).sanitize = nil
+					m.(*Module).stl = nil
+					m.(*Module).Properties.PreventInstall = true
 				}
-				// Mark that this variant is for stubs.
-				l.MutatedProperties.BuildStubs = true
-				l.MutatedProperties.StubsVersion = versions[i]
-				m.(*Module).Properties.HideFromMake = true
 			}
 		} else {
 			mctx.CreateVariations("")
diff --git a/cc/linker.go b/cc/linker.go
index 3053609..854dfc5 100644
--- a/cc/linker.go
+++ b/cc/linker.go
@@ -18,6 +18,7 @@
 	"android/soong/android"
 	"android/soong/cc/config"
 	"fmt"
+	"strconv"
 
 	"github.com/google/blueprint"
 	"github.com/google/blueprint/proptools"
@@ -48,7 +49,7 @@
 	// list of system libraries that will be dynamically linked to
 	// shared library and executable modules.  If unset, generally defaults to libc,
 	// libm, and libdl.  Set to [] to prevent linking against the defaults.
-	System_shared_libs []string
+	System_shared_libs []string `android:"arch_variant"`
 
 	// allow the module to contain undefined symbols.  By default,
 	// modules cannot contain undefined symbols that are not satisified by their immediate
@@ -236,35 +237,34 @@
 			deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
 		}
 
-		if !ctx.static() {
-			systemSharedLibs := linker.Properties.System_shared_libs
-			if systemSharedLibs == nil {
-				systemSharedLibs = []string{"libc", "libm", "libdl"}
-			}
-
-			if inList("libdl", deps.SharedLibs) {
-				// If system_shared_libs has libc but not libdl, make sure shared_libs does not
-				// have libdl to avoid loading libdl before libc.
-				if inList("libc", systemSharedLibs) {
-					if !inList("libdl", systemSharedLibs) {
-						ctx.PropertyErrorf("shared_libs",
-							"libdl must be in system_shared_libs, not shared_libs")
-					}
-					_, deps.SharedLibs = removeFromList("libdl", deps.SharedLibs)
-				}
-			}
-
-			// If libc and libdl are both in system_shared_libs make sure libd comes after libc
-			// to avoid loading libdl before libc.
-			if inList("libdl", systemSharedLibs) && inList("libc", systemSharedLibs) &&
-				indexList("libdl", systemSharedLibs) < indexList("libc", systemSharedLibs) {
-				ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc")
-			}
-
-			deps.LateSharedLibs = append(deps.LateSharedLibs, systemSharedLibs...)
-		} else if ctx.useSdk() || ctx.useVndk() {
-			deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm", "libdl")
+		var systemSharedLibs []string
+		if !ctx.useSdk() && !ctx.useVndk() {
+			systemSharedLibs = linker.Properties.System_shared_libs
 		}
+		if systemSharedLibs == nil {
+			systemSharedLibs = []string{"libc", "libm", "libdl"}
+		}
+
+		if inList("libdl", deps.SharedLibs) {
+			// If system_shared_libs has libc but not libdl, make sure shared_libs does not
+			// have libdl to avoid loading libdl before libc.
+			if inList("libc", systemSharedLibs) {
+				if !inList("libdl", systemSharedLibs) {
+					ctx.PropertyErrorf("shared_libs",
+						"libdl must be in system_shared_libs, not shared_libs")
+				}
+				_, deps.SharedLibs = removeFromList("libdl", deps.SharedLibs)
+			}
+		}
+
+		// If libc and libdl are both in system_shared_libs make sure libdl comes after libc
+		// to avoid loading libdl before libc.
+		if inList("libdl", systemSharedLibs) && inList("libc", systemSharedLibs) &&
+			indexList("libdl", systemSharedLibs) < indexList("libc", systemSharedLibs) {
+			ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc")
+		}
+
+		deps.LateSharedLibs = append(deps.LateSharedLibs, systemSharedLibs...)
 	}
 
 	if ctx.Windows() {
@@ -298,6 +298,23 @@
 	return true
 }
 
+// Check whether the SDK version is not older than the specific one
+func CheckSdkVersionAtLeast(ctx ModuleContext, SdkVersion int) bool {
+	if ctx.sdkVersion() == "current" {
+		return true
+	}
+	parsedSdkVersion, err := strconv.Atoi(ctx.sdkVersion())
+	if err != nil {
+		ctx.PropertyErrorf("sdk_version",
+			"Invalid sdk_version value (must be int or current): %q",
+			ctx.sdkVersion())
+	}
+	if parsedSdkVersion < SdkVersion {
+		return false
+	}
+	return true
+}
+
 // ModuleContext extends BaseModuleContext
 // BaseModuleContext should know if LLD is used?
 func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
@@ -312,6 +329,13 @@
 		flags.LdFlags = append(flags.LdFlags, fmt.Sprintf("${config.%sGlobalLldflags}", hod))
 		if !BoolDefault(linker.Properties.Pack_relocations, true) {
 			flags.LdFlags = append(flags.LdFlags, "-Wl,--pack-dyn-relocs=none")
+		} else if ctx.Device() {
+			// The SHT_RELR relocations is only supported by API level >= 28.
+			// Do not turn this on if older version NDK is used.
+			if !ctx.useSdk() || CheckSdkVersionAtLeast(ctx, 28) {
+				flags.LdFlags = append(flags.LdFlags, "-Wl,--pack-dyn-relocs=android+relr")
+				flags.LdFlags = append(flags.LdFlags, "-Wl,--use-android-relr-tags")
+			}
 		}
 	} else {
 		flags.LdFlags = append(flags.LdFlags, fmt.Sprintf("${config.%sGlobalLdflags}", hod))
diff --git a/cc/llndk_library.go b/cc/llndk_library.go
index 32da059..cdd2c48 100644
--- a/cc/llndk_library.go
+++ b/cc/llndk_library.go
@@ -185,7 +185,7 @@
 	return module
 }
 
-func llndkLibraryFactory() android.Module {
+func LlndkLibraryFactory() android.Module {
 	module := NewLLndkStubLibrary()
 	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
 	return module
@@ -219,6 +219,6 @@
 }
 
 func init() {
-	android.RegisterModuleType("llndk_library", llndkLibraryFactory)
+	android.RegisterModuleType("llndk_library", LlndkLibraryFactory)
 	android.RegisterModuleType("llndk_headers", llndkHeadersFactory)
 }
diff --git a/cc/ndk_headers.go b/cc/ndk_headers.go
index 8177ff1..504a6a0 100644
--- a/cc/ndk_headers.go
+++ b/cc/ndk_headers.go
@@ -77,6 +77,11 @@
 
 	// Path to the NOTICE file associated with the headers.
 	License *string
+
+	// True if this API is not yet ready to be shipped in the NDK. It will be
+	// available in the platform for testing, but will be excluded from the
+	// sysroot provided to the NDK proper.
+	Draft bool
 }
 
 type headerModule struct {
@@ -182,6 +187,11 @@
 
 	// Path to the NOTICE file associated with the headers.
 	License *string
+
+	// True if this API is not yet ready to be shipped in the NDK. It will be
+	// available in the platform for testing, but will be excluded from the
+	// sysroot provided to the NDK proper.
+	Draft bool
 }
 
 // Like ndk_headers, but preprocesses the headers with the bionic versioner:
@@ -309,6 +319,11 @@
 
 	// Path to the NOTICE file associated with the headers.
 	License *string
+
+	// True if this API is not yet ready to be shipped in the NDK. It will be
+	// available in the platform for testing, but will be excluded from the
+	// sysroot provided to the NDK proper.
+	Draft bool
 }
 
 type preprocessedHeadersModule struct {
diff --git a/cc/ndk_library.go b/cc/ndk_library.go
index 63d9f29..1b09f88 100644
--- a/cc/ndk_library.go
+++ b/cc/ndk_library.go
@@ -31,9 +31,9 @@
 	genStubSrc = pctx.AndroidStaticRule("genStubSrc",
 		blueprint.RuleParams{
 			Command: "$toolPath --arch $arch --api $apiLevel --api-map " +
-				"$apiMap $vndk $in $out",
+				"$apiMap $flags $in $out",
 			CommandDeps: []string{"$toolPath"},
-		}, "arch", "apiLevel", "apiMap", "vndk")
+		}, "arch", "apiLevel", "apiMap", "flags")
 
 	ndkLibrarySuffix = ".ndk"
 
@@ -91,6 +91,11 @@
 
 	// Private property for use by the mutator that splits per-API level.
 	ApiLevel string `blueprint:"mutated"`
+
+	// True if this API is not yet ready to be shipped in the NDK. It will be
+	// available in the platform for testing, but will be excluded from the
+	// sysroot provided to the NDK proper.
+	Draft bool
 }
 
 type stubDecorator struct {
@@ -266,7 +271,7 @@
 	return addStubLibraryCompilerFlags(flags)
 }
 
-func compileStubLibrary(ctx ModuleContext, flags Flags, symbolFile, apiLevel, vndk string) (Objects, android.ModuleGenPath) {
+func compileStubLibrary(ctx ModuleContext, flags Flags, symbolFile, apiLevel, genstubFlags string) (Objects, android.ModuleGenPath) {
 	arch := ctx.Arch().ArchType.String()
 
 	stubSrcPath := android.PathForModuleGen(ctx, "stub.c")
@@ -283,7 +288,7 @@
 			"arch":     arch,
 			"apiLevel": apiLevel,
 			"apiMap":   apiLevelsJson.String(),
-			"vndk":     vndk,
+			"flags":    genstubFlags,
 		},
 	})
 
diff --git a/cc/ndk_sysroot.go b/cc/ndk_sysroot.go
index 80b5c6a..9265bff 100644
--- a/cc/ndk_sysroot.go
+++ b/cc/ndk_sysroot.go
@@ -104,22 +104,38 @@
 		}
 
 		if m, ok := module.(*headerModule); ok {
+			if ctx.Config().ExcludeDraftNdkApis() && m.properties.Draft {
+				return
+			}
+
 			installPaths = append(installPaths, m.installPaths...)
 			licensePaths = append(licensePaths, m.licensePath)
 		}
 
 		if m, ok := module.(*versionedHeaderModule); ok {
+			if ctx.Config().ExcludeDraftNdkApis() && m.properties.Draft {
+				return
+			}
+
 			installPaths = append(installPaths, m.installPaths...)
 			licensePaths = append(licensePaths, m.licensePath)
 		}
 
 		if m, ok := module.(*preprocessedHeadersModule); ok {
+			if ctx.Config().ExcludeDraftNdkApis() && m.properties.Draft {
+				return
+			}
+
 			installPaths = append(installPaths, m.installPaths...)
 			licensePaths = append(licensePaths, m.licensePath)
 		}
 
 		if m, ok := module.(*Module); ok {
 			if installer, ok := m.installer.(*stubDecorator); ok {
+				if ctx.Config().ExcludeDraftNdkApis() &&
+					installer.properties.Draft {
+					return
+				}
 				installPaths = append(installPaths, installer.installPath)
 			}
 
diff --git a/cc/sanitize.go b/cc/sanitize.go
index 96c149a..8d33de5 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -36,7 +36,7 @@
 	asanLdflags = []string{"-Wl,-u,__asan_preinit"}
 	asanLibs    = []string{"libasan"}
 
-	hwasanCflags = []string{"-mllvm", "-hwasan-with-ifunc=0", "-fno-omit-frame-pointer", "-Wno-frame-larger-than="}
+	hwasanCflags = []string{"-mllvm", "-hwasan-with-ifunc=0", "-fno-omit-frame-pointer", "-Wno-frame-larger-than=", "-mllvm", "-hwasan-create-frame-descriptions=0"}
 
 	cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
 		"-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
@@ -49,7 +49,7 @@
 	cfiStaticLibsMutex    sync.Mutex
 	hwasanStaticLibsMutex sync.Mutex
 
-	intOverflowCflags   = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
+	intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blacklist.txt"}
 
 	// Pass -Xclang before -fsanitize-minimal-runtime to work around a driver
 	// check which rejects -fsanitize-minimal-runtime together with
@@ -58,7 +58,7 @@
 	// TODO(pcc): Remove the -Xclang once LLVM r346526 is rolled into the compiler.
 	minimalRuntimeFlags = []string{"-Xclang", "-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
 		"-fno-sanitize-recover=integer,undefined"}
-	hwasanGlobalOptions = []string{"heap_history_size=4095"}
+	hwasanGlobalOptions = []string{"heap_history_size=1023,stack_history_size=512"}
 )
 
 type sanitizerType int
@@ -128,6 +128,7 @@
 			Cfi              *bool    `android:"arch_variant"`
 			Integer_overflow *bool    `android:"arch_variant"`
 			Misc_undefined   []string `android:"arch_variant"`
+			No_recover       []string
 		}
 
 		// value to pass to -fsanitize-recover=
@@ -376,6 +377,22 @@
 	return deps
 }
 
+func toDisableImplicitIntegerChange(flags []string) bool {
+	// Returns true if any flag is fsanitize*integer, and there is
+	// no explicit flag about sanitize=implicit-integer-sign-change.
+	for _, f := range flags {
+		if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
+			return false
+		}
+	}
+	for _, f := range flags {
+		if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
+			return true
+		}
+	}
+	return false
+}
+
 func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
 	minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
 	minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
@@ -533,6 +550,10 @@
 				flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
 			}
 		}
+		// http://b/119329758, Android core does not boot up with this sanitizer yet.
+		if toDisableImplicitIntegerChange(flags.CFlags) {
+			flags.CFlags = append(flags.CFlags, "-fno-sanitize=implicit-integer-sign-change")
+		}
 	}
 
 	if len(diagSanitizers) > 0 {
@@ -545,6 +566,11 @@
 			strings.Join(sanitize.Properties.Sanitize.Recover, ","))
 	}
 
+	if sanitize.Properties.Sanitize.Diag.No_recover != nil {
+		flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover="+
+			strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
+	}
+
 	// Link a runtime library if needed.
 	runtimeLibrary := ""
 	if Bool(sanitize.Properties.Sanitize.Address) {
@@ -576,9 +602,12 @@
 		sanitize.runtimeLibrary = runtimeLibrary
 
 		// When linking against VNDK, use the vendor variant of the runtime lib
-		sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary
 		if ctx.useVndk() {
 			sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + vendorSuffix
+		} else if ctx.inRecovery() {
+			sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary + recoverySuffix
+		} else {
+			sanitize.androidMkRuntimeLibrary = sanitize.runtimeLibrary
 		}
 	}
 
diff --git a/cc/stl.go b/cc/stl.go
index 9dc8107..4870870 100644
--- a/cc/stl.go
+++ b/cc/stl.go
@@ -251,7 +251,7 @@
 		android.Linux:  []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
 		android.Darwin: []string{"-lc", "-lSystem"},
 		android.Windows: []string{"-Wl,--start-group", "-lmingw32", "-lgcc", "-lgcc_eh",
-			"-lmoldname", "-lmingwex", "-lmsvcr110", "-lmsvcrt", "-lpthread",
+			"-lmoldname", "-lmingwex", "-lmsvcrt", "-lucrt", "-lpthread",
 			"-ladvapi32", "-lshell32", "-luser32", "-lkernel32", "-lpsapi",
 			"-Wl,--end-group"},
 	}
diff --git a/cc/test_gen_stub_libs.py b/cc/test_gen_stub_libs.py
index 3b5585a..594c1bc 100755
--- a/cc/test_gen_stub_libs.py
+++ b/cc/test_gen_stub_libs.py
@@ -165,92 +165,115 @@
     def test_omit_private(self):
         self.assertFalse(
             gsl.should_omit_version(
-                gsl.Version('foo', None, [], []), 'arm', 9, False))
+                gsl.Version('foo', None, [], []), 'arm', 9, False, False))
 
         self.assertTrue(
             gsl.should_omit_version(
-                gsl.Version('foo_PRIVATE', None, [], []), 'arm', 9, False))
+                gsl.Version('foo_PRIVATE', None, [], []), 'arm', 9, False, False))
         self.assertTrue(
             gsl.should_omit_version(
-                gsl.Version('foo_PLATFORM', None, [], []), 'arm', 9, False))
+                gsl.Version('foo_PLATFORM', None, [], []), 'arm', 9, False, False))
 
         self.assertTrue(
             gsl.should_omit_version(
                 gsl.Version('foo', None, ['platform-only'], []), 'arm', 9,
-                False))
+                False, False))
 
     def test_omit_vndk(self):
         self.assertTrue(
             gsl.should_omit_version(
-                gsl.Version('foo', None, ['vndk'], []), 'arm', 9, False))
+                gsl.Version('foo', None, ['vndk'], []), 'arm', 9, False, False))
 
         self.assertFalse(
             gsl.should_omit_version(
-                gsl.Version('foo', None, [], []), 'arm', 9, True))
+                gsl.Version('foo', None, [], []), 'arm', 9, True, False))
         self.assertFalse(
             gsl.should_omit_version(
-                gsl.Version('foo', None, ['vndk'], []), 'arm', 9, True))
+                gsl.Version('foo', None, ['vndk'], []), 'arm', 9, True, False))
+
+    def test_omit_apex(self):
+        self.assertTrue(
+            gsl.should_omit_version(
+                gsl.Version('foo', None, ['apex'], []), 'arm', 9, False, False))
+
+        self.assertFalse(
+            gsl.should_omit_version(
+                gsl.Version('foo', None, [], []), 'arm', 9, False, True))
+        self.assertFalse(
+            gsl.should_omit_version(
+                gsl.Version('foo', None, ['apex'], []), 'arm', 9, False, True))
 
     def test_omit_arch(self):
         self.assertFalse(
             gsl.should_omit_version(
-                gsl.Version('foo', None, [], []), 'arm', 9, False))
+                gsl.Version('foo', None, [], []), 'arm', 9, False, False))
         self.assertFalse(
             gsl.should_omit_version(
-                gsl.Version('foo', None, ['arm'], []), 'arm', 9, False))
+                gsl.Version('foo', None, ['arm'], []), 'arm', 9, False, False))
 
         self.assertTrue(
             gsl.should_omit_version(
-                gsl.Version('foo', None, ['x86'], []), 'arm', 9, False))
+                gsl.Version('foo', None, ['x86'], []), 'arm', 9, False, False))
 
     def test_omit_api(self):
         self.assertFalse(
             gsl.should_omit_version(
-                gsl.Version('foo', None, [], []), 'arm', 9, False))
+                gsl.Version('foo', None, [], []), 'arm', 9, False, False))
         self.assertFalse(
             gsl.should_omit_version(
                 gsl.Version('foo', None, ['introduced=9'], []), 'arm', 9,
-                False))
+                False, False))
 
         self.assertTrue(
             gsl.should_omit_version(
                 gsl.Version('foo', None, ['introduced=14'], []), 'arm', 9,
-                False))
+                False, False))
 
 
 class OmitSymbolTest(unittest.TestCase):
     def test_omit_vndk(self):
         self.assertTrue(
             gsl.should_omit_symbol(
-                gsl.Symbol('foo', ['vndk']), 'arm', 9, False))
+                gsl.Symbol('foo', ['vndk']), 'arm', 9, False, False))
 
         self.assertFalse(
-            gsl.should_omit_symbol(gsl.Symbol('foo', []), 'arm', 9, True))
+            gsl.should_omit_symbol(gsl.Symbol('foo', []), 'arm', 9, True, False))
         self.assertFalse(
             gsl.should_omit_symbol(
-                gsl.Symbol('foo', ['vndk']), 'arm', 9, True))
+                gsl.Symbol('foo', ['vndk']), 'arm', 9, True, False))
+
+    def test_omit_apex(self):
+        self.assertTrue(
+            gsl.should_omit_symbol(
+                gsl.Symbol('foo', ['apex']), 'arm', 9, False, False))
+
+        self.assertFalse(
+            gsl.should_omit_symbol(gsl.Symbol('foo', []), 'arm', 9, False, True))
+        self.assertFalse(
+            gsl.should_omit_symbol(
+                gsl.Symbol('foo', ['apex']), 'arm', 9, False, True))
 
     def test_omit_arch(self):
         self.assertFalse(
-            gsl.should_omit_symbol(gsl.Symbol('foo', []), 'arm', 9, False))
+            gsl.should_omit_symbol(gsl.Symbol('foo', []), 'arm', 9, False, False))
         self.assertFalse(
             gsl.should_omit_symbol(
-                gsl.Symbol('foo', ['arm']), 'arm', 9, False))
+                gsl.Symbol('foo', ['arm']), 'arm', 9, False, False))
 
         self.assertTrue(
             gsl.should_omit_symbol(
-                gsl.Symbol('foo', ['x86']), 'arm', 9, False))
+                gsl.Symbol('foo', ['x86']), 'arm', 9, False, False))
 
     def test_omit_api(self):
         self.assertFalse(
-            gsl.should_omit_symbol(gsl.Symbol('foo', []), 'arm', 9, False))
+            gsl.should_omit_symbol(gsl.Symbol('foo', []), 'arm', 9, False, False))
         self.assertFalse(
             gsl.should_omit_symbol(
-                gsl.Symbol('foo', ['introduced=9']), 'arm', 9, False))
+                gsl.Symbol('foo', ['introduced=9']), 'arm', 9, False, False))
 
         self.assertTrue(
             gsl.should_omit_symbol(
-                gsl.Symbol('foo', ['introduced=14']), 'arm', 9, False))
+                gsl.Symbol('foo', ['introduced=14']), 'arm', 9, False, False))
 
 
 class SymbolFileParseTest(unittest.TestCase):
@@ -262,7 +285,7 @@
             # baz
             qux
         """))
-        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False)
+        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False, False)
         self.assertIsNone(parser.current_line)
 
         self.assertEqual('foo', parser.next_line().strip())
@@ -287,7 +310,7 @@
             VERSION_2 {
             } VERSION_1; # asdf
         """))
-        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False)
+        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False, False)
 
         parser.next_line()
         version = parser.parse_version()
@@ -311,7 +334,7 @@
         input_file = io.StringIO(textwrap.dedent("""\
             VERSION_1 {
         """))
-        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False)
+        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False, False)
         parser.next_line()
         with self.assertRaises(gsl.ParseError):
             parser.parse_version()
@@ -322,7 +345,7 @@
                 foo:
             }
         """))
-        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False)
+        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False, False)
         parser.next_line()
         with self.assertRaises(gsl.ParseError):
             parser.parse_version()
@@ -332,7 +355,7 @@
             foo;
             bar; # baz qux
         """))
-        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False)
+        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False, False)
 
         parser.next_line()
         symbol = parser.parse_symbol()
@@ -350,7 +373,7 @@
                 *;
             };
         """))
-        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False)
+        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False, False)
         parser.next_line()
         with self.assertRaises(gsl.ParseError):
             parser.parse_version()
@@ -362,7 +385,7 @@
                     *;
             };
         """))
-        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False)
+        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False, False)
         parser.next_line()
         version = parser.parse_version()
         self.assertEqual([], version.symbols)
@@ -373,7 +396,7 @@
                 foo
             };
         """))
-        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False)
+        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False, False)
         parser.next_line()
         with self.assertRaises(gsl.ParseError):
             parser.parse_version()
@@ -381,7 +404,7 @@
     def test_parse_fails_invalid_input(self):
         with self.assertRaises(gsl.ParseError):
             input_file = io.StringIO('foo')
-            parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False)
+            parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False, False)
             parser.parse()
 
     def test_parse(self):
@@ -402,7 +425,7 @@
                     qwerty;
             } VERSION_1;
         """))
-        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False)
+        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False, False)
         versions = parser.parse()
 
         expected = [
@@ -418,6 +441,30 @@
 
         self.assertEqual(expected, versions)
 
+    def test_parse_vndk_apex_symbol(self):
+        input_file = io.StringIO(textwrap.dedent("""\
+            VERSION_1 {
+                foo;
+                bar; # vndk
+                baz; # vndk apex
+                qux; # apex
+            };
+        """))
+        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False, True)
+
+        parser.next_line()
+        version = parser.parse_version()
+        self.assertEqual('VERSION_1', version.name)
+        self.assertIsNone(version.base)
+
+        expected_symbols = [
+            gsl.Symbol('foo', []),
+            gsl.Symbol('bar', ['vndk']),
+            gsl.Symbol('baz', ['vndk', 'apex']),
+            gsl.Symbol('qux', ['apex']),
+        ]
+        self.assertEqual(expected_symbols, version.symbols)
+
 
 class GeneratorTest(unittest.TestCase):
     def test_omit_version(self):
@@ -425,7 +472,7 @@
         # OmitVersionTest, PrivateVersionTest, and SymbolPresenceTest.
         src_file = io.StringIO()
         version_file = io.StringIO()
-        generator = gsl.Generator(src_file, version_file, 'arm', 9, False)
+        generator = gsl.Generator(src_file, version_file, 'arm', 9, False, False)
 
         version = gsl.Version('VERSION_PRIVATE', None, [], [
             gsl.Symbol('foo', []),
@@ -453,7 +500,7 @@
         # SymbolPresenceTest.
         src_file = io.StringIO()
         version_file = io.StringIO()
-        generator = gsl.Generator(src_file, version_file, 'arm', 9, False)
+        generator = gsl.Generator(src_file, version_file, 'arm', 9, False, False)
 
         version = gsl.Version('VERSION_1', None, [], [
             gsl.Symbol('foo', ['x86']),
@@ -476,10 +523,17 @@
         self.assertEqual('', src_file.getvalue())
         self.assertEqual('', version_file.getvalue())
 
+        version = gsl.Version('VERSION_1', None, [], [
+            gsl.Symbol('foo', ['apex']),
+        ])
+        generator.write_version(version)
+        self.assertEqual('', src_file.getvalue())
+        self.assertEqual('', version_file.getvalue())
+
     def test_write(self):
         src_file = io.StringIO()
         version_file = io.StringIO()
-        generator = gsl.Generator(src_file, version_file, 'arm', 9, False)
+        generator = gsl.Generator(src_file, version_file, 'arm', 9, False, False)
 
         versions = [
             gsl.Version('VERSION_1', None, [], [
@@ -554,18 +608,19 @@
             VERSION_4 { # versioned=9
                 wibble;
                 wizzes; # vndk
+                waggle; # apex
             } VERSION_2;
 
             VERSION_5 { # versioned=14
                 wobble;
             } VERSION_4;
         """))
-        parser = gsl.SymbolFileParser(input_file, api_map, 'arm', 9, False)
+        parser = gsl.SymbolFileParser(input_file, api_map, 'arm', 9, False, False)
         versions = parser.parse()
 
         src_file = io.StringIO()
         version_file = io.StringIO()
-        generator = gsl.Generator(src_file, version_file, 'arm', 9, False)
+        generator = gsl.Generator(src_file, version_file, 'arm', 9, False, False)
         generator.write(versions)
 
         expected_src = textwrap.dedent("""\
@@ -610,12 +665,12 @@
                     *;
             };
         """))
-        parser = gsl.SymbolFileParser(input_file, api_map, 'arm', 9001, False)
+        parser = gsl.SymbolFileParser(input_file, api_map, 'arm', 9001, False, False)
         versions = parser.parse()
 
         src_file = io.StringIO()
         version_file = io.StringIO()
-        generator = gsl.Generator(src_file, version_file, 'arm', 9001, False)
+        generator = gsl.Generator(src_file, version_file, 'arm', 9001, False, False)
         generator.write(versions)
 
         expected_src = textwrap.dedent("""\
@@ -658,13 +713,84 @@
             } VERSION_2;
 
         """))
-        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False)
+        parser = gsl.SymbolFileParser(input_file, {}, 'arm', 16, False, False)
 
         with self.assertRaises(gsl.MultiplyDefinedSymbolError) as cm:
             parser.parse()
         self.assertEquals(['bar', 'foo'],
                           cm.exception.multiply_defined_symbols)
 
+    def test_integration_with_apex(self):
+        api_map = {
+            'O': 9000,
+            'P': 9001,
+        }
+
+        input_file = io.StringIO(textwrap.dedent("""\
+            VERSION_1 {
+                global:
+                    foo; # var
+                    bar; # x86
+                    fizz; # introduced=O
+                    buzz; # introduced=P
+                local:
+                    *;
+            };
+
+            VERSION_2 { # arm
+                baz; # introduced=9
+                qux; # versioned=14
+            } VERSION_1;
+
+            VERSION_3 { # introduced=14
+                woodly;
+                doodly; # var
+            } VERSION_2;
+
+            VERSION_4 { # versioned=9
+                wibble;
+                wizzes; # vndk
+                waggle; # apex
+            } VERSION_2;
+
+            VERSION_5 { # versioned=14
+                wobble;
+            } VERSION_4;
+        """))
+        parser = gsl.SymbolFileParser(input_file, api_map, 'arm', 9, False, True)
+        versions = parser.parse()
+
+        src_file = io.StringIO()
+        version_file = io.StringIO()
+        generator = gsl.Generator(src_file, version_file, 'arm', 9, False, True)
+        generator.write(versions)
+
+        expected_src = textwrap.dedent("""\
+            int foo = 0;
+            void baz() {}
+            void qux() {}
+            void wibble() {}
+            void waggle() {}
+            void wobble() {}
+        """)
+        self.assertEqual(expected_src, src_file.getvalue())
+
+        expected_version = textwrap.dedent("""\
+            VERSION_1 {
+                global:
+                    foo;
+            };
+            VERSION_2 {
+                global:
+                    baz;
+            } VERSION_1;
+            VERSION_4 {
+                global:
+                    wibble;
+                    waggle;
+            } VERSION_2;
+        """)
+        self.assertEqual(expected_version, version_file.getvalue())
 
 def main():
     suite = unittest.TestLoader().loadTestsFromName(__name__)
diff --git a/cc/tidy.go b/cc/tidy.go
index ddb445a..6bac846 100644
--- a/cc/tidy.go
+++ b/cc/tidy.go
@@ -108,6 +108,12 @@
 	if len(tidy.Properties.Tidy_checks) > 0 {
 		tidyChecks = tidyChecks + "," + strings.Join(esc(tidy.Properties.Tidy_checks), ",")
 	}
+	if ctx.Windows() {
+		// https://b.corp.google.com/issues/120614316
+		// mingw32 has cert-dcl16-c warning in NO_ERROR,
+		// which is used in many Android files.
+		tidyChecks = tidyChecks + ",-cert-dcl16-c"
+	}
 	flags.TidyFlags = append(flags.TidyFlags, tidyChecks)
 
 	return flags
diff --git a/cc/vndk.go b/cc/vndk.go
index 1a9b77a..623097d 100644
--- a/cc/vndk.go
+++ b/cc/vndk.go
@@ -199,7 +199,7 @@
 )
 
 // gather list of vndk-core, vndk-sp, and ll-ndk libs
-func vndkMutator(mctx android.BottomUpMutatorContext) {
+func VndkMutator(mctx android.BottomUpMutatorContext) {
 	if m, ok := mctx.Module().(*Module); ok && m.Enabled() {
 		if lib, ok := m.linker.(*llndkStubDecorator); ok {
 			vndkLibrariesLock.Lock()
diff --git a/cc/xom.go b/cc/xom.go
index f65fc24..182069f 100644
--- a/cc/xom.go
+++ b/cc/xom.go
@@ -66,7 +66,8 @@
 	// Enable execute-only if none of the dependencies disable it,
 	// also if it's explicitly set true (allows overriding dependencies disabling it).
 	if !disableXom || (xom.Properties.Xom != nil && *xom.Properties.Xom) {
-		if ctx.Arch().ArchType == android.Arm64 {
+		// XOM is only supported on AArch64 when using lld.
+		if ctx.Arch().ArchType == android.Arm64 && ctx.useClangLld(ctx) {
 			flags.LdFlags = append(flags.LdFlags, "-Wl,-execute-only")
 		}
 	}
diff --git a/cmd/merge_zips/merge_zips.go b/cmd/merge_zips/merge_zips.go
index 8e71a97..c21da44 100644
--- a/cmd/merge_zips/merge_zips.go
+++ b/cmd/merge_zips/merge_zips.go
@@ -19,6 +19,7 @@
 	"flag"
 	"fmt"
 	"hash/crc32"
+	"io"
 	"io/ioutil"
 	"log"
 	"os"
@@ -66,6 +67,7 @@
 	manifest         = flag.String("m", "", "manifest file to insert in jar")
 	pyMain           = flag.String("pm", "", "__main__.py file to insert in par")
 	entrypoint       = flag.String("e", "", "par entrypoint file to insert in par")
+	prefix           = flag.String("prefix", "", "A file to prefix to the zip file")
 	ignoreDuplicates = flag.Bool("ignore-duplicates", false, "take each entry from the first zip it exists in and don't warn")
 )
 
@@ -77,7 +79,7 @@
 
 func main() {
 	flag.Usage = func() {
-		fmt.Fprintln(os.Stderr, "usage: merge_zips [-jpsD] [-m manifest] [-e entrypoint] [-pm __main__.py] output [inputs...]")
+		fmt.Fprintln(os.Stderr, "usage: merge_zips [-jpsD] [-m manifest] [--prefix script] [-e entrypoint] [-pm __main__.py] output [inputs...]")
 		flag.PrintDefaults()
 	}
 
@@ -99,6 +101,19 @@
 		log.Fatal(err)
 	}
 	defer output.Close()
+
+	var offset int64
+	if *prefix != "" {
+		prefixFile, err := os.Open(*prefix)
+		if err != nil {
+			log.Fatal(err)
+		}
+		offset, err = io.Copy(output, prefixFile)
+		if err != nil {
+			log.Fatal(err)
+		}
+	}
+
 	writer := zip.NewWriter(output)
 	defer func() {
 		err := writer.Close()
@@ -106,6 +121,7 @@
 			log.Fatal(err)
 		}
 	}()
+	writer.SetOffset(offset)
 
 	// make readers
 	readers := []namedZipReader{}
diff --git a/dexpreopt/Android.bp b/dexpreopt/Android.bp
new file mode 100644
index 0000000..b832529
--- /dev/null
+++ b/dexpreopt/Android.bp
@@ -0,0 +1,15 @@
+bootstrap_go_package {
+    name: "soong-dexpreopt",
+    pkgPath: "android/soong/dexpreopt",
+    srcs: [
+        "config.go",
+        "dexpreopt.go",
+        "script.go",
+    ],
+    testSrcs: [
+        "dexpreopt_test.go",
+    ],
+    deps: [
+        "blueprint-pathtools",
+    ],
+}
\ No newline at end of file
diff --git a/dexpreopt/OWNERS b/dexpreopt/OWNERS
new file mode 100644
index 0000000..166472f
--- /dev/null
+++ b/dexpreopt/OWNERS
@@ -0,0 +1 @@
+per-file * = ngeoffray@google.com,calin@google.com,mathieuc@google.com
diff --git a/dexpreopt/config.go b/dexpreopt/config.go
new file mode 100644
index 0000000..c24caac
--- /dev/null
+++ b/dexpreopt/config.go
@@ -0,0 +1,141 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package dexpreopt
+
+import (
+	"encoding/json"
+	"io/ioutil"
+)
+
+// GlobalConfig stores the configuration for dex preopting set by the product
+type GlobalConfig struct {
+	DefaultNoStripping bool // don't strip dex files by default
+
+	DisablePreoptModules []string // modules with preopt disabled by product-specific config
+
+	OnlyPreoptBootImageAndSystemServer bool // only preopt jars in the boot image or system server
+
+	HasSystemOther        bool     // store odex files that match PatternsOnSystemOther on the system_other partition
+	PatternsOnSystemOther []string // patterns (using '%' to denote a prefix match) to put odex on the system_other partition
+
+	DisableGenerateProfile bool // don't generate profiles
+
+	BootJars         []string // jars that form the boot image
+	SystemServerJars []string // jars that form the system server
+	SystemServerApps []string // apps that are loaded into system server
+	SpeedApps        []string // apps that should be speed optimized
+
+	PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
+
+	DefaultCompilerFilter      string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
+	SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
+
+	GenerateDMFiles bool // generate Dex Metadata files
+
+	NoDebugInfo                 bool // don't generate debug info by default
+	AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
+	NeverSystemServerDebugInfo  bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
+	AlwaysOtherDebugInfo        bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
+	NeverOtherDebugInfo         bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
+
+	MissingUsesLibraries []string // libraries that may be listed in OptionalUsesLibraries but will not be installed by the product
+
+	IsEng        bool // build is a eng variant
+	SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
+
+	DefaultAppImages bool // build app images (TODO: .art files?) by default
+
+	Dex2oatXmx string // max heap size
+	Dex2oatXms string // initial heap size
+
+	EmptyDirectory string // path to an empty directory
+
+	DefaultDexPreoptImageLocation map[string]string // default boot image location for each architecture
+	CpuVariant                    map[string]string // cpu variant for each architecture
+	InstructionSetFeatures        map[string]string // instruction set for each architecture
+
+	Tools Tools // paths to tools possibly used by the generated commands
+}
+
+// Tools contains paths to tools possibly used by the generated commands.  If you add a new tool here you MUST add it
+// to the order-only dependency list in DEXPREOPT_GEN_DEPS.
+type Tools struct {
+	Profman  string
+	Dex2oat  string
+	Aapt     string
+	SoongZip string
+	Zip2zip  string
+
+	VerifyUsesLibraries string
+	ConstructContext    string
+}
+
+type ModuleConfig struct {
+	Name                string
+	DexLocation         string // dex location on device
+	BuildPath           string
+	DexPath             string
+	PreferCodeIntegrity bool
+	UncompressedDex     bool
+	HasApkLibraries     bool
+	PreoptFlags         []string
+
+	ProfileClassListing  string
+	ProfileIsTextListing bool
+
+	EnforceUsesLibraries  bool
+	OptionalUsesLibraries []string
+	UsesLibraries         []string
+	LibraryPaths          map[string]string
+
+	Archs                  []string
+	DexPreoptImageLocation string
+
+	PreoptExtractedApk bool // Overrides OnlyPreoptModules
+
+	NoCreateAppImage    bool
+	ForceCreateAppImage bool
+
+	PresignedPrebuilt bool
+
+	StripInputPath  string
+	StripOutputPath string
+}
+
+func LoadGlobalConfig(path string) (GlobalConfig, error) {
+	config := GlobalConfig{}
+	err := loadConfig(path, &config)
+	return config, err
+}
+
+func LoadModuleConfig(path string) (ModuleConfig, error) {
+	config := ModuleConfig{}
+	err := loadConfig(path, &config)
+	return config, err
+}
+
+func loadConfig(path string, config interface{}) error {
+	data, err := ioutil.ReadFile(path)
+	if err != nil {
+		return err
+	}
+
+	err = json.Unmarshal(data, config)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
diff --git a/dexpreopt/dexpreopt.go b/dexpreopt/dexpreopt.go
new file mode 100644
index 0000000..0ea32b8
--- /dev/null
+++ b/dexpreopt/dexpreopt.go
@@ -0,0 +1,559 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// The dexpreopt package converts a global dexpreopt config and a module dexpreopt config into rules to perform
+// dexpreopting and to strip the dex files from the APK or JAR.
+//
+// It is used in two places; in the dexpeopt_gen binary for modules defined in Make, and directly linked into Soong.
+//
+// For Make modules it is built into the dexpreopt_gen binary, which is executed as a Make rule using global config and
+// module config specified in JSON files.  The binary writes out two shell scripts, only updating them if they have
+// changed.  One script takes an APK or JAR as an input and produces a zip file containing any outputs of preopting,
+// in the location they should be on the device.  The Make build rules will unzip the zip file into $(PRODUCT_OUT) when
+// installing the APK, which will install the preopt outputs into $(PRODUCT_OUT)/system or $(PRODUCT_OUT)/system_other
+// as necessary.  The zip file may be empty if preopting was disabled for any reason.  The second script takes an APK or
+// JAR as an input and strips the dex files in it as necessary.
+//
+// The intermediate shell scripts allow changes to this package or to the global config to regenerate the shell scripts
+// but only require re-executing preopting if the script has changed.
+//
+// For Soong modules this package is linked directly into Soong and run from the java package.  It generates the same
+// commands as for make, using athe same global config JSON file used by make, but using a module config structure
+// provided by Soong.  The generated commands are then converted into Soong rule and written directly to the ninja file,
+// with no extra shell scripts involved.
+package dexpreopt
+
+import (
+	"fmt"
+	"path/filepath"
+	"strings"
+
+	"github.com/google/blueprint/pathtools"
+)
+
+const SystemPartition = "/system/"
+const SystemOtherPartition = "/system_other/"
+
+// GenerateStripRule generates a set of commands that will take an APK or JAR as an input and strip the dex files if
+// they are no longer necessary after preopting.
+func GenerateStripRule(global GlobalConfig, module ModuleConfig) (rule *Rule, err error) {
+	defer func() {
+		if r := recover(); r != nil {
+			if e, ok := r.(error); ok {
+				err = e
+				rule = nil
+			} else {
+				panic(r)
+			}
+		}
+	}()
+
+	tools := global.Tools
+
+	rule = &Rule{}
+
+	strip := shouldStripDex(module, global)
+
+	if strip {
+		// Only strips if the dex files are not already uncompressed
+		rule.Command().
+			Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, module.StripInputPath).
+			Tool(tools.Zip2zip).FlagWithInput("-i ", module.StripInputPath).FlagWithOutput("-o ", module.StripOutputPath).
+			FlagWithArg("-x ", `"classes*.dex"`).
+			Textf(`; else cp -f %s %s; fi`, module.StripInputPath, module.StripOutputPath)
+	} else {
+		rule.Command().Text("cp -f").Input(module.StripInputPath).Output(module.StripOutputPath)
+	}
+
+	return rule, nil
+}
+
+// GenerateDexpreoptRule generates a set of commands that will preopt a module based on a GlobalConfig and a
+// ModuleConfig.  The produced files and their install locations will be available through rule.Installs().
+func GenerateDexpreoptRule(global GlobalConfig, module ModuleConfig) (rule *Rule, err error) {
+	defer func() {
+		if r := recover(); r != nil {
+			if e, ok := r.(error); ok {
+				err = e
+				rule = nil
+			} else {
+				panic(r)
+			}
+		}
+	}()
+
+	rule = &Rule{}
+
+	dexpreoptDisabled := contains(global.DisablePreoptModules, module.Name)
+
+	if contains(global.BootJars, module.Name) {
+		// Don't preopt individual boot jars, they will be preopted together
+		dexpreoptDisabled = true
+	}
+
+	// If OnlyPreoptBootImageAndSystemServer=true and module is not in boot class path skip
+	// Also preopt system server jars since selinux prevents system server from loading anything from
+	// /data. If we don't do this they will need to be extracted which is not favorable for RAM usage
+	// or performance. If PreoptExtractedApk is true, we ignore the only preopt boot image options.
+	if global.OnlyPreoptBootImageAndSystemServer && !contains(global.BootJars, module.Name) &&
+		!contains(global.SystemServerJars, module.Name) && !module.PreoptExtractedApk {
+		dexpreoptDisabled = true
+	}
+
+	generateProfile := module.ProfileClassListing != "" && !global.DisableGenerateProfile
+
+	var profile string
+	if generateProfile {
+		profile = profileCommand(global, module, rule)
+	}
+
+	if !dexpreoptDisabled {
+		appImage := (generateProfile || module.ForceCreateAppImage || global.DefaultAppImages) &&
+			!module.NoCreateAppImage
+
+		generateDM := shouldGenerateDM(module, global)
+
+		for _, arch := range module.Archs {
+			imageLocation := module.DexPreoptImageLocation
+			if imageLocation == "" {
+				imageLocation = global.DefaultDexPreoptImageLocation[arch]
+			}
+			dexpreoptCommand(global, module, rule, profile, arch, imageLocation, appImage, generateDM)
+		}
+	}
+
+	return rule, nil
+}
+
+func profileCommand(global GlobalConfig, module ModuleConfig, rule *Rule) string {
+	profilePath := filepath.Join(filepath.Dir(module.BuildPath), "profile.prof")
+	profileInstalledPath := module.DexLocation + ".prof"
+
+	if !module.ProfileIsTextListing {
+		rule.Command().FlagWithOutput("touch ", profilePath)
+	}
+
+	cmd := rule.Command().
+		Text(`ANDROID_LOG_TAGS="*:e"`).
+		Tool(global.Tools.Profman)
+
+	if module.ProfileIsTextListing {
+		// The profile is a test listing of classes (used for framework jars).
+		// We need to generate the actual binary profile before being able to compile.
+		cmd.FlagWithInput("--create-profile-from=", module.ProfileClassListing)
+	} else {
+		// The profile is binary profile (used for apps). Run it through profman to
+		// ensure the profile keys match the apk.
+		cmd.
+			Flag("--copy-and-update-profile-key").
+			FlagWithInput("--profile-file=", module.ProfileClassListing)
+	}
+
+	cmd.
+		FlagWithInput("--apk=", module.DexPath).
+		Flag("--dex-location="+module.DexLocation).
+		FlagWithOutput("--reference-profile-file=", profilePath)
+
+	if !module.ProfileIsTextListing {
+		cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath))
+	}
+	rule.Install(profilePath, profileInstalledPath)
+
+	return profilePath
+}
+
+func dexpreoptCommand(global GlobalConfig, module ModuleConfig, rule *Rule, profile, arch, bootImageLocation string,
+	appImage, generateDM bool) {
+
+	// HACK: make soname in Soong-generated .odex files match Make.
+	base := filepath.Base(module.DexLocation)
+	if filepath.Ext(base) == ".jar" {
+		base = "javalib.jar"
+	} else if filepath.Ext(base) == ".apk" {
+		base = "package.apk"
+	}
+
+	toOdexPath := func(path string) string {
+		return filepath.Join(
+			filepath.Dir(path),
+			"oat",
+			arch,
+			pathtools.ReplaceExtension(filepath.Base(path), "odex"))
+	}
+
+	odexPath := toOdexPath(filepath.Join(filepath.Dir(module.BuildPath), base))
+	odexInstallPath := toOdexPath(module.DexLocation)
+	if odexOnSystemOther(module, global) {
+		odexInstallPath = strings.Replace(odexInstallPath, SystemPartition, SystemOtherPartition, 1)
+	}
+
+	vdexPath := pathtools.ReplaceExtension(odexPath, "vdex")
+	vdexInstallPath := pathtools.ReplaceExtension(odexInstallPath, "vdex")
+
+	// bootImageLocation is $OUT/dex_bootjars/system/framework/boot.art, but dex2oat actually reads
+	// $OUT/dex_bootjars/system/framework/arm64/boot.art
+	var bootImagePath string
+	if bootImageLocation != "" {
+		bootImagePath = filepath.Join(filepath.Dir(bootImageLocation), arch, filepath.Base(bootImageLocation))
+	}
+
+	// Lists of used and optional libraries from the build config to be verified against the manifest in the APK
+	var verifyUsesLibs []string
+	var verifyOptionalUsesLibs []string
+
+	// Lists of used and optional libraries from the build config, with optional libraries that are known to not
+	// be present in the current product removed.
+	var filteredUsesLibs []string
+	var filteredOptionalUsesLibs []string
+
+	// The class loader context using paths in the build
+	var classLoaderContextHost []string
+
+	// The class loader context using paths as they will be on the device
+	var classLoaderContextTarget []string
+
+	// Extra paths that will be appended to the class loader if the APK manifest has targetSdkVersion < 28
+	var conditionalClassLoaderContextHost28 []string
+	var conditionalClassLoaderContextTarget28 []string
+
+	// Extra paths that will be appended to the class loader if the APK manifest has targetSdkVersion < 29
+	var conditionalClassLoaderContextHost29 []string
+	var conditionalClassLoaderContextTarget29 []string
+
+	if module.EnforceUsesLibraries {
+		verifyUsesLibs = copyOf(module.UsesLibraries)
+		verifyOptionalUsesLibs = copyOf(module.OptionalUsesLibraries)
+
+		filteredOptionalUsesLibs = filterOut(global.MissingUsesLibraries, module.OptionalUsesLibraries)
+		filteredUsesLibs = append(copyOf(module.UsesLibraries), filteredOptionalUsesLibs...)
+
+		// Create class loader context for dex2oat from uses libraries and filtered optional libraries
+		for _, l := range filteredUsesLibs {
+
+			classLoaderContextHost = append(classLoaderContextHost,
+				pathForLibrary(module, l))
+			classLoaderContextTarget = append(classLoaderContextTarget,
+				filepath.Join("/system/framework", l+".jar"))
+		}
+
+		const httpLegacy = "org.apache.http.legacy"
+		const httpLegacyImpl = "org.apache.http.legacy.impl"
+
+		// Fix up org.apache.http.legacy.impl since it should be org.apache.http.legacy in the manifest.
+		replace(verifyUsesLibs, httpLegacyImpl, httpLegacy)
+		replace(verifyOptionalUsesLibs, httpLegacyImpl, httpLegacy)
+
+		if !contains(verifyUsesLibs, httpLegacy) && !contains(verifyOptionalUsesLibs, httpLegacy) {
+			conditionalClassLoaderContextHost28 = append(conditionalClassLoaderContextHost28,
+				pathForLibrary(module, httpLegacyImpl))
+			conditionalClassLoaderContextTarget28 = append(conditionalClassLoaderContextTarget28,
+				filepath.Join("/system/framework", httpLegacyImpl+".jar"))
+		}
+
+		const hidlBase = "android.hidl.base-V1.0-java"
+		const hidlManager = "android.hidl.manager-V1.0-java"
+
+		conditionalClassLoaderContextHost29 = append(conditionalClassLoaderContextHost29,
+      pathForLibrary(module, hidlManager))
+		conditionalClassLoaderContextTarget29 = append(conditionalClassLoaderContextTarget29,
+			filepath.Join("/system/framework", hidlManager+".jar"))
+		conditionalClassLoaderContextHost29 = append(conditionalClassLoaderContextHost29,
+      pathForLibrary(module, hidlBase))
+		conditionalClassLoaderContextTarget29 = append(conditionalClassLoaderContextTarget29,
+			filepath.Join("/system/framework", hidlBase+".jar"))
+	} else {
+		// Pass special class loader context to skip the classpath and collision check.
+		// This will get removed once LOCAL_USES_LIBRARIES is enforced.
+		// Right now LOCAL_USES_LIBRARIES is opt in, for the case where it's not specified we still default
+		// to the &.
+		classLoaderContextHost = []string{`\&`}
+	}
+
+	rule.Command().FlagWithArg("mkdir -p ", filepath.Dir(odexPath))
+	rule.Command().FlagWithOutput("rm -f ", odexPath)
+	// Set values in the environment of the rule.  These may be modified by construct_context.sh.
+	rule.Command().FlagWithArg("class_loader_context_arg=--class-loader-context=",
+		strings.Join(classLoaderContextHost, ":"))
+	rule.Command().Text(`stored_class_loader_context_arg=""`)
+
+	if module.EnforceUsesLibraries {
+		rule.Command().Textf(`uses_library_names="%s"`, strings.Join(verifyUsesLibs, " "))
+		rule.Command().Textf(`optional_uses_library_names="%s"`, strings.Join(verifyOptionalUsesLibs, " "))
+		rule.Command().Textf(`aapt_binary="%s"`, global.Tools.Aapt)
+		rule.Command().Textf(`dex_preopt_host_libraries="%s"`, strings.Join(classLoaderContextHost, " " ))
+		rule.Command().Textf(`dex_preopt_target_libraries="%s"`, strings.Join(classLoaderContextTarget, " "))
+		rule.Command().Textf(`conditional_host_libs_28="%s"`, strings.Join(conditionalClassLoaderContextHost28, " "))
+		rule.Command().Textf(`conditional_target_libs_28="%s"`, strings.Join(conditionalClassLoaderContextTarget28, " "))
+		rule.Command().Textf(`conditional_host_libs_29="%s"`, strings.Join(conditionalClassLoaderContextHost29, " "))
+		rule.Command().Textf(`conditional_target_libs_29="%s"`, strings.Join(conditionalClassLoaderContextTarget29, " "))
+		rule.Command().Text("source").Tool(global.Tools.VerifyUsesLibraries).Input(module.DexPath)
+		rule.Command().Text("source").Tool(global.Tools.ConstructContext)
+	}
+
+	cmd := rule.Command().
+		Text(`ANDROID_LOG_TAGS="*:e"`).
+		Tool(global.Tools.Dex2oat).
+		Flag("--avoid-storing-invocation").
+		Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatXms).
+		Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatXmx).
+		Flag("${class_loader_context_arg}").
+		Flag("${stored_class_loader_context_arg}").
+		FlagWithArg("--boot-image=", bootImageLocation).Implicit(bootImagePath).
+		FlagWithInput("--dex-file=", module.DexPath).
+		FlagWithArg("--dex-location=", module.DexLocation).
+		FlagWithOutput("--oat-file=", odexPath).ImplicitOutput(vdexPath).
+		// Pass an empty directory, dex2oat shouldn't be reading arbitrary files
+		FlagWithArg("--android-root=", global.EmptyDirectory).
+		FlagWithArg("--instruction-set=", arch).
+		FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
+		FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
+		Flag("--no-generate-debug-info").
+		Flag("--generate-build-id").
+		Flag("--abort-on-hard-verifier-error").
+		Flag("--force-determinism").
+		FlagWithArg("--no-inline-from=", "core-oj.jar")
+
+	var preoptFlags []string
+	if len(module.PreoptFlags) > 0 {
+		preoptFlags = module.PreoptFlags
+	} else if len(global.PreoptFlags) > 0 {
+		preoptFlags = global.PreoptFlags
+	}
+
+	if len(preoptFlags) > 0 {
+		cmd.Text(strings.Join(preoptFlags, " "))
+	}
+
+	if module.UncompressedDex {
+		cmd.FlagWithArg("--copy-dex-files=", "false")
+	}
+
+	if !anyHavePrefix(preoptFlags, "--compiler-filter=") {
+		var compilerFilter string
+		if contains(global.SystemServerJars, module.Name) {
+			// Jars of system server, use the product option if it is set, speed otherwise.
+			if global.SystemServerCompilerFilter != "" {
+				compilerFilter = global.SystemServerCompilerFilter
+			} else {
+				compilerFilter = "speed"
+			}
+		} else if contains(global.SpeedApps, module.Name) || contains(global.SystemServerApps, module.Name) {
+			// Apps loaded into system server, and apps the product default to being compiled with the
+			// 'speed' compiler filter.
+			compilerFilter = "speed"
+		} else if profile != "" {
+			// For non system server jars, use speed-profile when we have a profile.
+			compilerFilter = "speed-profile"
+		} else if global.DefaultCompilerFilter != "" {
+			compilerFilter = global.DefaultCompilerFilter
+		} else {
+			compilerFilter = "quicken"
+		}
+		cmd.FlagWithArg("--compiler-filter=", compilerFilter)
+	}
+
+	if generateDM {
+		cmd.FlagWithArg("--copy-dex-files=", "false")
+		dmPath := filepath.Join(filepath.Dir(module.BuildPath), "generated.dm")
+		dmInstalledPath := pathtools.ReplaceExtension(module.DexLocation, "dm")
+		tmpPath := filepath.Join(filepath.Dir(module.BuildPath), "primary.vdex")
+		rule.Command().Text("cp -f").Input(vdexPath).Output(tmpPath)
+		rule.Command().Tool(global.Tools.SoongZip).
+			FlagWithArg("-L", "9").
+			FlagWithOutput("-o", dmPath).
+			Flag("-j").
+			Input(tmpPath)
+		rule.Install(dmPath, dmInstalledPath)
+	}
+
+	// By default, emit debug info.
+	debugInfo := true
+	if global.NoDebugInfo {
+		// If the global setting suppresses mini-debug-info, disable it.
+		debugInfo = false
+	}
+
+	// PRODUCT_SYSTEM_SERVER_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO.
+	// PRODUCT_OTHER_JAVA_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO.
+	if contains(global.SystemServerJars, module.Name) {
+		if global.AlwaysSystemServerDebugInfo {
+			debugInfo = true
+		} else if global.NeverSystemServerDebugInfo {
+			debugInfo = false
+		}
+	} else {
+		if global.AlwaysOtherDebugInfo {
+			debugInfo = true
+		} else if global.NeverOtherDebugInfo {
+			debugInfo = false
+		}
+	}
+
+	// Never enable on eng.
+	if global.IsEng {
+		debugInfo = false
+	}
+
+	if debugInfo {
+		cmd.Flag("--generate-mini-debug-info")
+	} else {
+		cmd.Flag("--no-generate-mini-debug-info")
+	}
+
+	// Set the compiler reason to 'prebuilt' to identify the oat files produced
+	// during the build, as opposed to compiled on the device.
+	cmd.FlagWithArg("--compilation-reason=", "prebuilt")
+
+	if appImage {
+		appImagePath := pathtools.ReplaceExtension(odexPath, "art")
+		appImageInstallPath := pathtools.ReplaceExtension(odexInstallPath, "art")
+		cmd.FlagWithOutput("--app-image-file=", appImagePath).
+			FlagWithArg("--image-format=", "lz4")
+		rule.Install(appImagePath, appImageInstallPath)
+	}
+
+	if profile != "" {
+		cmd.FlagWithArg("--profile-file=", profile)
+	}
+
+	rule.Install(odexPath, odexInstallPath)
+	rule.Install(vdexPath, vdexInstallPath)
+}
+
+// Return if the dex file in the APK should be stripped.  If an APK is found to contain uncompressed dex files at
+// dex2oat time it will not be stripped even if strip=true.
+func shouldStripDex(module ModuleConfig, global GlobalConfig) bool {
+	strip := !global.DefaultNoStripping
+
+	// Don't strip modules that are not on the system partition in case the oat/vdex version in system ROM
+	// doesn't match the one in other partitions. It needs to be able to fall back to the APK for that case.
+	if !strings.HasPrefix(module.DexLocation, SystemPartition) {
+		strip = false
+	}
+
+	// system_other isn't there for an OTA, so don't strip if module is on system, and odex is on system_other.
+	if odexOnSystemOther(module, global) {
+		strip = false
+	}
+
+	if module.HasApkLibraries {
+		strip = false
+	}
+
+	// Don't strip with dex files we explicitly uncompress (dexopt will not store the dex code).
+	if module.UncompressedDex {
+		strip = false
+	}
+
+	if shouldGenerateDM(module, global) {
+		strip = false
+	}
+
+	if module.PresignedPrebuilt {
+		// Only strip out files if we can re-sign the package.
+		strip = false
+	}
+
+	return strip
+}
+
+func shouldGenerateDM(module ModuleConfig, global GlobalConfig) bool {
+	// Generating DM files only makes sense for verify, avoid doing for non verify compiler filter APKs.
+	// No reason to use a dm file if the dex is already uncompressed.
+	return global.GenerateDMFiles && !module.UncompressedDex &&
+		contains(module.PreoptFlags, "--compiler-filter=verify")
+}
+
+func odexOnSystemOther(module ModuleConfig, global GlobalConfig) bool {
+	if !global.HasSystemOther {
+		return false
+	}
+
+	if global.SanitizeLite {
+		return false
+	}
+
+	if contains(global.SpeedApps, module.Name) || contains(global.SystemServerApps, module.Name) {
+		return false
+	}
+
+	for _, f := range global.PatternsOnSystemOther {
+		if makefileMatch(filepath.Join(SystemPartition, f), module.DexLocation) {
+			return true
+		}
+	}
+
+	return false
+}
+
+func pathForLibrary(module ModuleConfig, lib string) string {
+	path := module.LibraryPaths[lib]
+	if path == "" {
+		panic(fmt.Errorf("unknown library path for %q", lib))
+	}
+	return path
+}
+
+func makefileMatch(pattern, s string) bool {
+	percent := strings.IndexByte(pattern, '%')
+	switch percent {
+	case -1:
+		return pattern == s
+	case len(pattern) - 1:
+		return strings.HasPrefix(s, pattern[:len(pattern)-1])
+	default:
+		panic(fmt.Errorf("unsupported makefile pattern %q", pattern))
+	}
+}
+
+func contains(l []string, s string) bool {
+	for _, e := range l {
+		if e == s {
+			return true
+		}
+	}
+	return false
+}
+
+// remove all elements in a from b, returning a new slice
+func filterOut(a []string, b []string) []string {
+	var ret []string
+	for _, x := range b {
+		if !contains(a, x) {
+			ret = append(ret, x)
+		}
+	}
+	return ret
+}
+
+func replace(l []string, from, to string) {
+	for i := range l {
+		if l[i] == from {
+			l[i] = to
+		}
+	}
+}
+
+func copyOf(l []string) []string {
+	return append([]string(nil), l...)
+}
+
+func anyHavePrefix(l []string, prefix string) bool {
+	for _, x := range l {
+		if strings.HasPrefix(x, prefix) {
+			return true
+		}
+	}
+	return false
+}
diff --git a/dexpreopt/dexpreopt_gen/Android.bp b/dexpreopt/dexpreopt_gen/Android.bp
new file mode 100644
index 0000000..0790391
--- /dev/null
+++ b/dexpreopt/dexpreopt_gen/Android.bp
@@ -0,0 +1,11 @@
+blueprint_go_binary {
+    name: "dexpreopt_gen",
+    srcs: [
+        "dexpreopt_gen.go",
+    ],
+    deps: [
+        "soong-dexpreopt",
+        "blueprint-pathtools",
+        "blueprint-proptools",
+    ],
+}
\ No newline at end of file
diff --git a/dexpreopt/dexpreopt_gen/dexpreopt_gen.go b/dexpreopt/dexpreopt_gen/dexpreopt_gen.go
new file mode 100644
index 0000000..46d8795
--- /dev/null
+++ b/dexpreopt/dexpreopt_gen/dexpreopt_gen.go
@@ -0,0 +1,191 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package main
+
+import (
+	"bytes"
+	"flag"
+	"fmt"
+	"os"
+	"path/filepath"
+	"runtime"
+
+	"android/soong/dexpreopt"
+
+	"github.com/google/blueprint/pathtools"
+)
+
+var (
+	dexpreoptScriptPath = flag.String("dexpreopt_script", "", "path to output dexpreopt script")
+	stripScriptPath     = flag.String("strip_script", "", "path to output strip script")
+	globalConfigPath    = flag.String("global", "", "path to global configuration file")
+	moduleConfigPath    = flag.String("module", "", "path to module configuration file")
+)
+
+func main() {
+	flag.Parse()
+
+	usage := func(err string) {
+		if err != "" {
+			fmt.Println(err)
+			flag.Usage()
+			os.Exit(1)
+		}
+	}
+
+	if flag.NArg() > 0 {
+		usage("unrecognized argument " + flag.Arg(0))
+	}
+
+	if *dexpreoptScriptPath == "" {
+		usage("path to output dexpreopt script is required")
+	}
+
+	if *stripScriptPath == "" {
+		usage("path to output strip script is required")
+	}
+
+	if *globalConfigPath == "" {
+		usage("path to global configuration file is required")
+	}
+
+	if *moduleConfigPath == "" {
+		usage("path to module configuration file is required")
+	}
+
+	globalConfig, err := dexpreopt.LoadGlobalConfig(*globalConfigPath)
+	if err != nil {
+		fmt.Fprintf(os.Stderr, "error loading global config %q: %s\n", *globalConfigPath, err)
+		os.Exit(2)
+	}
+
+	moduleConfig, err := dexpreopt.LoadModuleConfig(*moduleConfigPath)
+	if err != nil {
+		fmt.Fprintf(os.Stderr, "error loading module config %q: %s\n", *moduleConfigPath, err)
+		os.Exit(2)
+	}
+
+	defer func() {
+		if r := recover(); r != nil {
+			switch x := r.(type) {
+			case runtime.Error:
+				panic(x)
+			case error:
+				fmt.Fprintln(os.Stderr, "error:", r)
+				os.Exit(3)
+			default:
+				panic(x)
+			}
+		}
+	}()
+
+	writeScripts(globalConfig, moduleConfig, *dexpreoptScriptPath, *stripScriptPath)
+}
+
+func writeScripts(global dexpreopt.GlobalConfig, module dexpreopt.ModuleConfig,
+	dexpreoptScriptPath, stripScriptPath string) {
+	dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(global, module)
+	if err != nil {
+		panic(err)
+	}
+
+	installDir := filepath.Join(filepath.Dir(module.BuildPath), "dexpreopt_install")
+
+	dexpreoptRule.Command().FlagWithArg("rm -rf ", installDir)
+	dexpreoptRule.Command().FlagWithArg("mkdir -p ", installDir)
+
+	for _, install := range dexpreoptRule.Installs() {
+		installPath := filepath.Join(installDir, install.To)
+		dexpreoptRule.Command().Text("mkdir -p").Flag(filepath.Dir(installPath))
+		dexpreoptRule.Command().Text("cp -f").Input(install.From).Output(installPath)
+	}
+	dexpreoptRule.Command().Tool(global.Tools.SoongZip).
+		FlagWithOutput("-o ", "$2").
+		FlagWithArg("-C ", installDir).
+		FlagWithArg("-D ", installDir)
+
+	stripRule, err := dexpreopt.GenerateStripRule(global, module)
+	if err != nil {
+		panic(err)
+	}
+
+	write := func(rule *dexpreopt.Rule, file string) {
+		script := &bytes.Buffer{}
+		script.WriteString(scriptHeader)
+		for _, c := range rule.Commands() {
+			script.WriteString(c)
+			script.WriteString("\n\n")
+		}
+
+		depFile := &bytes.Buffer{}
+
+		fmt.Fprint(depFile, `: \`+"\n")
+		for _, tool := range dexpreoptRule.Tools() {
+			fmt.Fprintf(depFile, `    %s \`+"\n", tool)
+		}
+		for _, input := range dexpreoptRule.Inputs() {
+			// Assume the rule that ran the script already has a dependency on the input file passed on the
+			// command line.
+			if input != "$1" {
+				fmt.Fprintf(depFile, `    %s \`+"\n", input)
+			}
+		}
+		depFile.WriteString("\n")
+
+		fmt.Fprintln(script, "rm -f $2.d")
+		// Write the output path unescaped so the $2 gets expanded
+		fmt.Fprintln(script, `echo -n $2 > $2.d`)
+		// Write the rest of the depsfile using cat <<'EOF', which will not do any shell expansion on
+		// the contents to preserve backslashes and special characters in filenames.
+		fmt.Fprintf(script, "cat >> $2.d <<'EOF'\n%sEOF\n", depFile.String())
+
+		err := pathtools.WriteFileIfChanged(file, script.Bytes(), 0755)
+		if err != nil {
+			panic(err)
+		}
+	}
+
+	// The written scripts will assume the input is $1 and the output is $2
+	if module.DexPath != "$1" {
+		panic(fmt.Errorf("module.DexPath must be '$1', was %q", module.DexPath))
+	}
+	if module.StripInputPath != "$1" {
+		panic(fmt.Errorf("module.StripInputPath must be '$1', was %q", module.StripInputPath))
+	}
+	if module.StripOutputPath != "$2" {
+		panic(fmt.Errorf("module.StripOutputPath must be '$2', was %q", module.StripOutputPath))
+	}
+
+	write(dexpreoptRule, dexpreoptScriptPath)
+	write(stripRule, stripScriptPath)
+}
+
+const scriptHeader = `#!/bin/bash
+
+err() {
+  errno=$?
+  echo "error: $0:$1 exited with status $errno" >&2
+  echo "error in command:" >&2
+  sed -n -e "$1p" $0 >&2
+  if [ "$errno" -ne 0 ]; then
+    exit $errno
+  else
+    exit 1
+  fi
+}
+
+trap 'err $LINENO' ERR
+
+`
diff --git a/dexpreopt/dexpreopt_test.go b/dexpreopt/dexpreopt_test.go
new file mode 100644
index 0000000..fef85a7
--- /dev/null
+++ b/dexpreopt/dexpreopt_test.go
@@ -0,0 +1,208 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package dexpreopt
+
+import (
+	"reflect"
+	"strings"
+	"testing"
+)
+
+var testGlobalConfig = GlobalConfig{
+	DefaultNoStripping:                 false,
+	DisablePreoptModules:               nil,
+	OnlyPreoptBootImageAndSystemServer: false,
+	HasSystemOther:                     false,
+	PatternsOnSystemOther:              nil,
+	DisableGenerateProfile:             false,
+	BootJars:                           nil,
+	SystemServerJars:                   nil,
+	SystemServerApps:                   nil,
+	SpeedApps:                          nil,
+	PreoptFlags:                        nil,
+	DefaultCompilerFilter:              "",
+	SystemServerCompilerFilter:         "",
+	GenerateDMFiles:                    false,
+	NoDebugInfo:                        false,
+	AlwaysSystemServerDebugInfo:        false,
+	NeverSystemServerDebugInfo:         false,
+	AlwaysOtherDebugInfo:               false,
+	NeverOtherDebugInfo:                false,
+	MissingUsesLibraries:               nil,
+	IsEng:                              false,
+	SanitizeLite:                       false,
+	DefaultAppImages:                   false,
+	Dex2oatXmx:                         "",
+	Dex2oatXms:                         "",
+	EmptyDirectory:                     "",
+	DefaultDexPreoptImageLocation:      nil,
+	CpuVariant:                         nil,
+	InstructionSetFeatures:             nil,
+	Tools: Tools{
+		Profman:             "profman",
+		Dex2oat:             "dex2oat",
+		Aapt:                "aapt",
+		SoongZip:            "soong_zip",
+		Zip2zip:             "zip2zip",
+		VerifyUsesLibraries: "verify_uses_libraries.sh",
+		ConstructContext:    "construct_context.sh",
+	},
+}
+
+var testModuleConfig = ModuleConfig{
+	Name:                   "",
+	DexLocation:            "",
+	BuildPath:              "",
+	DexPath:                "",
+	PreferCodeIntegrity:    false,
+	UncompressedDex:        false,
+	HasApkLibraries:        false,
+	PreoptFlags:            nil,
+	ProfileClassListing:    "",
+	ProfileIsTextListing:   false,
+	EnforceUsesLibraries:   false,
+	OptionalUsesLibraries:  nil,
+	UsesLibraries:          nil,
+	LibraryPaths:           nil,
+	Archs:                  nil,
+	DexPreoptImageLocation: "",
+	PreoptExtractedApk:     false,
+	NoCreateAppImage:       false,
+	ForceCreateAppImage:    false,
+	PresignedPrebuilt:      false,
+	StripInputPath:         "",
+	StripOutputPath:        "",
+}
+
+func TestDexPreopt(t *testing.T) {
+	global, module := testGlobalConfig, testModuleConfig
+
+	module.Name = "test"
+	module.DexLocation = "/system/app/test/test.apk"
+	module.BuildPath = "out/test/test.apk"
+	module.Archs = []string{"arm"}
+
+	rule, err := GenerateDexpreoptRule(global, module)
+	if err != nil {
+		t.Error(err)
+	}
+
+	wantInstalls := []Install{
+		{"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},
+		{"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"},
+	}
+
+	if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
+		t.Errorf("\nwant installs:\n   %v\ngot:\n   %v", wantInstalls, rule.Installs())
+	}
+}
+
+func TestDexPreoptSystemOther(t *testing.T) {
+	global, module := testGlobalConfig, testModuleConfig
+
+	global.HasSystemOther = true
+	global.PatternsOnSystemOther = []string{"app/%"}
+
+	module.Name = "test"
+	module.DexLocation = "/system/app/test/test.apk"
+	module.BuildPath = "out/test/test.apk"
+	module.Archs = []string{"arm"}
+
+	rule, err := GenerateDexpreoptRule(global, module)
+	if err != nil {
+		t.Error(err)
+	}
+
+	wantInstalls := []Install{
+		{"out/test/oat/arm/package.odex", "/system_other/app/test/oat/arm/test.odex"},
+		{"out/test/oat/arm/package.vdex", "/system_other/app/test/oat/arm/test.vdex"},
+	}
+
+	if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
+		t.Errorf("\nwant installs:\n   %v\ngot:\n   %v", wantInstalls, rule.Installs())
+	}
+}
+
+func TestDexPreoptProfile(t *testing.T) {
+	global, module := testGlobalConfig, testModuleConfig
+
+	module.Name = "test"
+	module.DexLocation = "/system/app/test/test.apk"
+	module.BuildPath = "out/test/test.apk"
+	module.ProfileClassListing = "profile"
+	module.Archs = []string{"arm"}
+
+	rule, err := GenerateDexpreoptRule(global, module)
+	if err != nil {
+		t.Error(err)
+	}
+
+	wantInstalls := []Install{
+		{"out/test/profile.prof", "/system/app/test/test.apk.prof"},
+		{"out/test/oat/arm/package.art", "/system/app/test/oat/arm/test.art"},
+		{"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},
+		{"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"},
+	}
+
+	if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
+		t.Errorf("\nwant installs:\n   %v\ngot:\n   %v", wantInstalls, rule.Installs())
+	}
+}
+
+func TestStripDex(t *testing.T) {
+	global, module := testGlobalConfig, testModuleConfig
+
+	module.Name = "test"
+	module.DexLocation = "/system/app/test/test.apk"
+	module.BuildPath = "out/test/test.apk"
+	module.Archs = []string{"arm"}
+	module.StripInputPath = "$1"
+	module.StripOutputPath = "$2"
+
+	rule, err := GenerateStripRule(global, module)
+	if err != nil {
+		t.Error(err)
+	}
+
+	want := `zip2zip -i $1 -o $2 -x "classes*.dex"`
+	if len(rule.Commands()) < 1 || !strings.Contains(rule.Commands()[0], want) {
+		t.Errorf("\nwant commands[0] to have:\n   %v\ngot:\n   %v", want, rule.Commands()[0])
+	}
+}
+
+func TestNoStripDex(t *testing.T) {
+	global, module := testGlobalConfig, testModuleConfig
+
+	global.DefaultNoStripping = true
+
+	module.Name = "test"
+	module.DexLocation = "/system/app/test/test.apk"
+	module.BuildPath = "out/test/test.apk"
+	module.Archs = []string{"arm"}
+	module.StripInputPath = "$1"
+	module.StripOutputPath = "$2"
+
+	rule, err := GenerateStripRule(global, module)
+	if err != nil {
+		t.Error(err)
+	}
+
+	wantCommands := []string{
+		"cp -f $1 $2",
+	}
+	if !reflect.DeepEqual(rule.Commands(), wantCommands) {
+		t.Errorf("\nwant commands:\n   %v\ngot:\n   %v", wantCommands, rule.Commands())
+	}
+}
diff --git a/dexpreopt/script.go b/dexpreopt/script.go
new file mode 100644
index 0000000..fd4cf82
--- /dev/null
+++ b/dexpreopt/script.go
@@ -0,0 +1,173 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package dexpreopt
+
+import (
+	"fmt"
+	"sort"
+	"strings"
+)
+
+type Install struct {
+	From, To string
+}
+
+type Rule struct {
+	commands []*Command
+	installs []Install
+}
+
+func (r *Rule) Install(from, to string) {
+	r.installs = append(r.installs, Install{from, to})
+}
+
+func (r *Rule) Command() *Command {
+	command := &Command{}
+	r.commands = append(r.commands, command)
+	return command
+}
+
+func (r *Rule) Inputs() []string {
+	outputs := r.outputSet()
+
+	inputs := make(map[string]bool)
+	for _, c := range r.commands {
+		for _, input := range c.inputs {
+			if !outputs[input] {
+				inputs[input] = true
+			}
+		}
+	}
+
+	var inputList []string
+	for input := range inputs {
+		inputList = append(inputList, input)
+	}
+	sort.Strings(inputList)
+
+	return inputList
+}
+
+func (r *Rule) outputSet() map[string]bool {
+	outputs := make(map[string]bool)
+	for _, c := range r.commands {
+		for _, output := range c.outputs {
+			outputs[output] = true
+		}
+	}
+	return outputs
+}
+
+func (r *Rule) Outputs() []string {
+	outputs := r.outputSet()
+
+	var outputList []string
+	for output := range outputs {
+		outputList = append(outputList, output)
+	}
+	sort.Strings(outputList)
+	return outputList
+}
+
+func (r *Rule) Installs() []Install {
+	return append([]Install(nil), r.installs...)
+}
+
+func (r *Rule) Tools() []string {
+	var tools []string
+	for _, c := range r.commands {
+		tools = append(tools, c.tools...)
+	}
+	return tools
+}
+
+func (r *Rule) Commands() []string {
+	var commands []string
+	for _, c := range r.commands {
+		commands = append(commands, string(c.buf))
+	}
+	return commands
+}
+
+type Command struct {
+	buf     []byte
+	inputs  []string
+	outputs []string
+	tools   []string
+}
+
+func (c *Command) Text(text string) *Command {
+	if len(c.buf) > 0 {
+		c.buf = append(c.buf, ' ')
+	}
+	c.buf = append(c.buf, text...)
+	return c
+}
+
+func (c *Command) Textf(format string, a ...interface{}) *Command {
+	return c.Text(fmt.Sprintf(format, a...))
+}
+
+func (c *Command) Flag(flag string) *Command {
+	return c.Text(flag)
+}
+
+func (c *Command) FlagWithArg(flag, arg string) *Command {
+	return c.Text(flag + arg)
+}
+
+func (c *Command) FlagWithList(flag string, list []string, sep string) *Command {
+	return c.Text(flag + strings.Join(list, sep))
+}
+
+func (c *Command) Tool(path string) *Command {
+	c.tools = append(c.tools, path)
+	return c.Text(path)
+}
+
+func (c *Command) Input(path string) *Command {
+	c.inputs = append(c.inputs, path)
+	return c.Text(path)
+}
+
+func (c *Command) Implicit(path string) *Command {
+	c.inputs = append(c.inputs, path)
+	return c
+}
+
+func (c *Command) Output(path string) *Command {
+	c.outputs = append(c.outputs, path)
+	return c.Text(path)
+}
+
+func (c *Command) ImplicitOutput(path string) *Command {
+	c.outputs = append(c.outputs, path)
+	return c
+}
+
+func (c *Command) FlagWithInput(flag, path string) *Command {
+	c.inputs = append(c.inputs, path)
+	return c.Text(flag + path)
+}
+
+func (c *Command) FlagWithInputList(flag string, paths []string, sep string) *Command {
+	c.inputs = append(c.inputs, paths...)
+	return c.FlagWithList(flag, paths, sep)
+}
+
+func (c *Command) FlagWithOutput(flag, path string) *Command {
+	c.outputs = append(c.outputs, path)
+	return c.Text(flag + path)
+}
diff --git a/genrule/genrule.go b/genrule/genrule.go
index e6c6efd..b70c075 100644
--- a/genrule/genrule.go
+++ b/genrule/genrule.go
@@ -88,6 +88,9 @@
 
 	// list of input files
 	Srcs []string `android:"arch_variant"`
+
+	// input files to exclude
+	Exclude_srcs []string `android:"arch_variant"`
 }
 
 type Module struct {
@@ -228,7 +231,7 @@
 
 	var srcFiles android.Paths
 	for _, in := range g.properties.Srcs {
-		paths := ctx.ExpandSources([]string{in}, nil)
+		paths := ctx.ExpandSources([]string{in}, g.properties.Exclude_srcs)
 		srcFiles = append(srcFiles, paths...)
 		addLocationLabel(in, paths.Strings())
 	}
diff --git a/java/OWNERS b/java/OWNERS
new file mode 100644
index 0000000..d68a5b0
--- /dev/null
+++ b/java/OWNERS
@@ -0,0 +1 @@
+per-file dexpreopt.go = ngeoffray@google.com,calin@google.com,mathieuc@google.com
diff --git a/java/aar.go b/java/aar.go
index 99e9136..a108ba0 100644
--- a/java/aar.go
+++ b/java/aar.go
@@ -357,6 +357,7 @@
 	module.AddProperties(
 		&module.Module.properties,
 		&module.Module.deviceProperties,
+		&module.Module.dexpreoptProperties,
 		&module.Module.protoProperties,
 		&module.aaptProperties,
 		&module.androidLibraryProperties)
@@ -442,7 +443,7 @@
 }
 
 func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
-	if !ctx.Config().UnbundledBuild() {
+	if !ctx.Config().UnbundledBuildPrebuiltSdks() {
 		sdkDep := decodeSdkDep(ctx, sdkContext(a))
 		if sdkDep.useModule && sdkDep.frameworkResModule != "" {
 			ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule)
diff --git a/java/androidmk.go b/java/androidmk.go
index 0700b58..70d0f7f 100644
--- a/java/androidmk.go
+++ b/java/androidmk.go
@@ -25,7 +25,7 @@
 func (library *Library) AndroidMk() android.AndroidMkData {
 	return android.AndroidMkData{
 		Class:      "JAVA_LIBRARIES",
-		OutputFile: android.OptionalPathForPath(library.implementationAndResourcesJar),
+		OutputFile: android.OptionalPathForPath(library.outputFile),
 		Include:    "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
 		Extra: []android.AndroidMkExtraFunc{
 			func(w io.Writer, outputFile android.Path) {
@@ -42,21 +42,12 @@
 				}
 				if library.dexJarFile != nil {
 					fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", library.dexJarFile.String())
-					if library.deviceProperties.Dex_preopt.Enabled != nil {
-						fmt.Fprintln(w, "LOCAL_DEX_PREOPT :=", *library.deviceProperties.Dex_preopt.Enabled)
-					}
-					if library.deviceProperties.Dex_preopt.App_image != nil {
-						fmt.Fprintln(w, "LOCAL_DEX_PREOPT_APP_IMAGE :=", *library.deviceProperties.Dex_preopt.App_image)
-					}
-					if library.deviceProperties.Dex_preopt.Profile_guided != nil {
-						fmt.Fprintln(w, "LOCAL_DEX_PREOPT_GENERATE_PROFILE :=", *library.deviceProperties.Dex_preopt.Profile_guided)
-					}
-					if library.deviceProperties.Dex_preopt.Profile != nil {
-						fmt.Fprintln(w, "LOCAL_DEX_PREOPT_GENERATE_PROFILE := true")
-						fmt.Fprintln(w, "LOCAL_DEX_PREOPT_PROFILE_CLASS_LISTING := $(LOCAL_PATH)/"+*library.deviceProperties.Dex_preopt.Profile)
-					}
+				}
+				if len(library.dexpreopter.builtInstalled) > 0 {
+					fmt.Fprintln(w, "LOCAL_SOONG_BUILT_INSTALLED :=", strings.Join(library.dexpreopter.builtInstalled, " "))
 				}
 				fmt.Fprintln(w, "LOCAL_SDK_VERSION :=", library.sdkVersion())
+				fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", library.implementationAndResourcesJar.String())
 				fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", library.headerJarFile.String())
 
 				if library.jacocoReportClassesFile != nil {
@@ -84,7 +75,6 @@
 				fmt.Fprintln(w, "LOCAL_MODULE := "+name+"-hostdex")
 				fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
 				fmt.Fprintln(w, "LOCAL_MODULE_CLASS := JAVA_LIBRARIES")
-				fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", library.implementationAndResourcesJar.String())
 				if library.installFile == nil {
 					fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
 				}
@@ -92,6 +82,7 @@
 					fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", library.dexJarFile.String())
 				}
 				fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", library.headerJarFile.String())
+				fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", library.implementationAndResourcesJar.String())
 				fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES := "+strings.Join(data.Required, " "))
 				fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
 			}
@@ -128,6 +119,7 @@
 			func(w io.Writer, outputFile android.Path) {
 				fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := ", !Bool(prebuilt.properties.Installable))
 				fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", prebuilt.combinedClasspathFile.String())
+				fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", prebuilt.combinedClasspathFile.String())
 				fmt.Fprintln(w, "LOCAL_SDK_VERSION :=", prebuilt.sdkVersion())
 			},
 		},
@@ -142,8 +134,8 @@
 		Extra: []android.AndroidMkExtraFunc{
 			func(w io.Writer, outputFile android.Path) {
 				fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
-				fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
 				fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", prebuilt.classpathFile.String())
+				fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", prebuilt.classpathFile.String())
 				fmt.Fprintln(w, "LOCAL_SOONG_RESOURCE_EXPORT_PACKAGE :=", prebuilt.exportPackage.String())
 				fmt.Fprintln(w, "LOCAL_SOONG_EXPORT_PROGUARD_FLAGS :=", prebuilt.proguardFlags.String())
 				fmt.Fprintln(w, "LOCAL_SOONG_STATIC_LIBRARY_EXTRA_PACKAGES :=", prebuilt.extraAaptPackagesFile.String())
@@ -164,6 +156,7 @@
 			Extra: []android.AndroidMkExtraFunc{
 				func(w io.Writer, outputFile android.Path) {
 					fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", binary.headerJarFile.String())
+					fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", binary.implementationAndResourcesJar.String())
 				},
 			},
 			Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
@@ -250,6 +243,9 @@
 				for _, jniLib := range app.installJniLibs {
 					fmt.Fprintln(w, "LOCAL_SOONG_JNI_LIBS_"+jniLib.target.Arch.ArchType.String(), "+=", jniLib.name)
 				}
+				if len(app.dexpreopter.builtInstalled) > 0 {
+					fmt.Fprintln(w, "LOCAL_SOONG_BUILT_INSTALLED :=", strings.Join(app.dexpreopter.builtInstalled, " "))
+				}
 			},
 		},
 	}
@@ -313,7 +309,6 @@
 		fmt.Fprintln(w, "LOCAL_SOONG_EXPORT_PROGUARD_FLAGS :=",
 			strings.Join(a.exportedProguardFlagFiles.Strings(), " "))
 		fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
-		fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
 	})
 
 	return data
diff --git a/java/app.go b/java/app.go
index 392ad3f..a037ef3 100644
--- a/java/app.go
+++ b/java/app.go
@@ -17,6 +17,7 @@
 // This file contains the module types for compiling Android apps.
 
 import (
+	"path/filepath"
 	"strings"
 
 	"github.com/google/blueprint"
@@ -67,9 +68,7 @@
 	// list of native libraries that will be provided in or alongside the resulting jar
 	Jni_libs []string `android:"arch_variant"`
 
-	AllowDexPreopt bool `blueprint:"mutated"`
-	EmbedJNI       bool `blueprint:"mutated"`
-	StripDex       bool `blueprint:"mutated"`
+	EmbedJNI bool `blueprint:"mutated"`
 }
 
 type AndroidApp struct {
@@ -143,42 +142,16 @@
 	a.generateAndroidBuildActions(ctx)
 }
 
-// Returns whether this module should have the dex file stored uncompressed in the APK, or stripped completely.  If
-// stripped, the code will still be present on the device in the dexpreopted files.
-// This is only necessary for APKs, and not jars, because APKs are signed and the dex file should not be uncompressed
-// or removed after the signature has been generated.  For jars, which are not signed, the dex file is uncompressed
-// or removed at installation time in Make.
-func (a *AndroidApp) uncompressOrStripDex(ctx android.ModuleContext) (uncompress, strip bool) {
+// Returns whether this module should have the dex file stored uncompressed in the APK.
+func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
 	if ctx.Config().UnbundledBuild() {
-		return false, false
+		return false
 	}
 
-	strip = ctx.Config().DefaultStripDex()
-	// TODO(ccross): don't strip dex installed on partitions that may be updated separately (like vendor)
-	// TODO(ccross): don't strip dex on modules with LOCAL_APK_LIBRARIES equivalent
-
 	// Uncompress dex in APKs of privileged apps, and modules used by privileged apps.
-	if ctx.Config().UncompressPrivAppDex() &&
+	return ctx.Config().UncompressPrivAppDex() &&
 		(Bool(a.appProperties.Privileged) ||
-			inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules())) {
-
-		uncompress = true
-		// If the dex files is store uncompressed, don't strip it, we will reuse the uncompressed dex from the APK
-		// instead of copying it into the odex file.
-		strip = false
-	}
-
-	// If dexpreopt is disabled, don't strip the dex file
-	if !a.appProperties.AllowDexPreopt ||
-		!BoolDefault(a.deviceProperties.Dex_preopt.Enabled, true) ||
-		ctx.Config().DisableDexPreopt(ctx.ModuleName()) {
-		strip = false
-	}
-
-	// TODO(ccross): strip dexpropted modules that are not propted to system_other
-	strip = false
-
-	return uncompress, strip
+			inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()))
 }
 
 func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
@@ -228,16 +201,25 @@
 	a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, staticLibProguardFlagFiles...)
 	a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, a.proguardOptionsFile)
 
-	a.deviceProperties.UncompressDex, a.appProperties.StripDex = a.uncompressOrStripDex(ctx)
+	a.deviceProperties.UncompressDex = a.shouldUncompressDex(ctx)
+
+	var installDir string
+	if ctx.ModuleName() == "framework-res" {
+		// framework-res.apk is installed as system/framework/framework-res.apk
+		installDir = "framework"
+	} else if Bool(a.appProperties.Privileged) {
+		installDir = filepath.Join("priv-app", ctx.ModuleName())
+	} else {
+		installDir = filepath.Join("app", ctx.ModuleName())
+	}
+	a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, ctx.ModuleName()+".apk")
+	a.dexpreopter.isPrivApp = Bool(a.appProperties.Privileged)
 
 	if ctx.ModuleName() != "framework-res" {
 		a.Module.compile(ctx, a.aaptSrcJar)
 	}
-	dexJarFile := a.dexJarFile
 
-	if a.appProperties.StripDex {
-		dexJarFile = nil
-	}
+	dexJarFile := a.maybeStrippedDexJarFile
 
 	var certificates []Certificate
 
@@ -287,9 +269,9 @@
 		// framework-res.apk is installed as system/framework/framework-res.apk
 		ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".apk", a.outputFile)
 	} else if Bool(a.appProperties.Privileged) {
-		ctx.InstallFile(android.PathForModuleInstall(ctx, "priv-app"), ctx.ModuleName()+".apk", a.outputFile)
+		ctx.InstallFile(android.PathForModuleInstall(ctx, "priv-app", ctx.ModuleName()), ctx.ModuleName()+".apk", a.outputFile)
 	} else {
-		ctx.InstallFile(android.PathForModuleInstall(ctx, "app"), ctx.ModuleName()+".apk", a.outputFile)
+		ctx.InstallFile(android.PathForModuleInstall(ctx, "app", ctx.ModuleName()), ctx.ModuleName()+".apk", a.outputFile)
 	}
 }
 
@@ -337,11 +319,11 @@
 
 	module.Module.properties.Instrument = true
 	module.Module.properties.Installable = proptools.BoolPtr(true)
-	module.appProperties.AllowDexPreopt = true
 
 	module.AddProperties(
 		&module.Module.properties,
 		&module.Module.deviceProperties,
+		&module.Module.dexpreoptProperties,
 		&module.Module.protoProperties,
 		&module.aaptProperties,
 		&module.appProperties)
@@ -399,11 +381,12 @@
 	module.Module.properties.Instrument = true
 	module.Module.properties.Installable = proptools.BoolPtr(true)
 	module.appProperties.EmbedJNI = true
-	module.appProperties.AllowDexPreopt = false
+	module.Module.dexpreopter.isTest = true
 
 	module.AddProperties(
 		&module.Module.properties,
 		&module.Module.deviceProperties,
+		&module.Module.dexpreoptProperties,
 		&module.Module.protoProperties,
 		&module.aaptProperties,
 		&module.appProperties,
@@ -434,11 +417,12 @@
 
 	module.Module.properties.Installable = proptools.BoolPtr(true)
 	module.appProperties.EmbedJNI = true
-	module.appProperties.AllowDexPreopt = false
+	module.Module.dexpreopter.isTest = true
 
 	module.AddProperties(
 		&module.Module.properties,
 		&module.Module.deviceProperties,
+		&module.Module.dexpreoptProperties,
 		&module.Module.protoProperties,
 		&module.aaptProperties,
 		&module.appProperties,
diff --git a/java/builder.go b/java/builder.go
index cefb916..8615664 100644
--- a/java/builder.go
+++ b/java/builder.go
@@ -138,6 +138,17 @@
 			CommandDeps: []string{"${config.JavaCmd}", "${config.JetifierJar}"},
 		},
 	)
+
+	zipalign = pctx.AndroidStaticRule("zipalign",
+		blueprint.RuleParams{
+			Command: "if ! ${config.ZipAlign} -c 4 $in > /dev/null; then " +
+				"${config.ZipAlign} -f 4 $in $out; " +
+				"else " +
+				"cp -f $in $out; " +
+				"fi",
+			CommandDeps: []string{"${config.ZipAlign}"},
+		},
+	)
 )
 
 func init() {
@@ -410,6 +421,15 @@
 	})
 }
 
+func TransformZipAlign(ctx android.ModuleContext, outputFile android.WritablePath, inputFile android.Path) {
+	ctx.Build(pctx, android.BuildParams{
+		Rule:        zipalign,
+		Description: "align",
+		Input:       inputFile,
+		Output:      outputFile,
+	})
+}
+
 type classpath []android.Path
 
 func (x *classpath) FormJavaClassPath(optName string) string {
diff --git a/java/config/config.go b/java/config/config.go
index d2a8c46..da4eed7 100644
--- a/java/config/config.go
+++ b/java/config/config.go
@@ -126,6 +126,7 @@
 	pctx.HostJavaToolVariable("JetifierJar", "jetifier.jar")
 
 	pctx.HostBinToolVariable("SoongJavacWrapper", "soong_javac_wrapper")
+	pctx.HostBinToolVariable("DexpreoptGen", "dexpreopt_gen")
 
 	pctx.VariableFunc("JavacWrapper", func(ctx android.PackageVarContext) string {
 		if override := ctx.Config().Getenv("JAVAC_WRAPPER"); override != "" {
@@ -152,4 +153,6 @@
 
 	pctx.SourcePathsVariable("ManifestMergerJars", " ", ManifestMergerClasspath...)
 	pctx.SourcePathsVariable("ManifestMergerClasspath", ":", ManifestMergerClasspath...)
+
+	pctx.HostBinToolVariable("ZipAlign", "zipalign")
 }
diff --git a/java/config/makevars.go b/java/config/makevars.go
index 275f496..01adaa7 100644
--- a/java/config/makevars.go
+++ b/java/config/makevars.go
@@ -65,6 +65,7 @@
 	ctx.Strict("JMOD", "${JmodCmd}")
 
 	ctx.Strict("SOONG_JAVAC_WRAPPER", "${SoongJavacWrapper}")
+	ctx.Strict("DEXPREOPT_GEN", "${DexpreoptGen}")
 	ctx.Strict("ZIPSYNC", "${ZipSyncCmd}")
 
 	ctx.Strict("JACOCO_CLI_JAR", "${JacocoCLIJar}")
diff --git a/java/dex.go b/java/dex.go
index 5cec325..a6d486a 100644
--- a/java/dex.go
+++ b/java/dex.go
@@ -160,6 +160,11 @@
 	// TODO(ccross): if this is an instrumentation test of an obfuscated app, use the
 	// dictionary of the app and move the app from libraryjars to injars.
 
+	// Don't strip out debug information for eng builds.
+	if ctx.Config().Eng() {
+		r8Flags = append(r8Flags, "--debug")
+	}
+
 	return r8Flags, r8Deps
 }
 
diff --git a/java/dexpreopt.go b/java/dexpreopt.go
new file mode 100644
index 0000000..de9c5f3
--- /dev/null
+++ b/java/dexpreopt.go
@@ -0,0 +1,250 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package java
+
+import (
+	"path/filepath"
+	"strings"
+
+	"github.com/google/blueprint"
+	"github.com/google/blueprint/proptools"
+
+	"android/soong/android"
+	"android/soong/dexpreopt"
+)
+
+type dexpreopter struct {
+	dexpreoptProperties DexpreoptProperties
+
+	installPath  android.OutputPath
+	isPrivApp    bool
+	isSDKLibrary bool
+	isTest       bool
+
+	builtInstalled []string
+}
+
+type DexpreoptProperties struct {
+	Dex_preopt struct {
+		// If false, prevent dexpreopting and stripping the dex file from the final jar.  Defaults to
+		// true.
+		Enabled *bool
+
+		// If true, generate an app image (.art file) for this module.
+		App_image *bool
+
+		// If true, use a checked-in profile to guide optimization.  Defaults to false unless
+		// a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
+		// that matches the name of this module, in which case it is defaulted to true.
+		Profile_guided *bool
+
+		// If set, provides the path to profile relative to the Android.bp file.  If not set,
+		// defaults to searching for a file that matches the name of this module in the default
+		// profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
+		Profile *string
+	}
+}
+
+func (d *dexpreopter) dexpreoptDisabled(ctx android.ModuleContext) bool {
+	if ctx.Config().DisableDexPreopt(ctx.ModuleName()) {
+		return true
+	}
+
+	if ctx.Config().UnbundledBuild() {
+		return true
+	}
+
+	if d.isTest {
+		return true
+	}
+
+	if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
+		return true
+	}
+
+	// TODO: contains no java code
+
+	return false
+}
+
+func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath {
+	if d.dexpreoptDisabled(ctx) {
+		return dexJarFile
+	}
+
+	globalConfig := ctx.Config().Once("DexpreoptGlobalConfig", func() interface{} {
+		if f := ctx.Config().DexpreoptGlobalConfig(); f != "" {
+			ctx.AddNinjaFileDeps(f)
+			globalConfig, err := dexpreopt.LoadGlobalConfig(f)
+			if err != nil {
+				panic(err)
+			}
+			return globalConfig
+		}
+		return dexpreopt.GlobalConfig{}
+	}).(dexpreopt.GlobalConfig)
+
+	var archs []string
+	for _, a := range ctx.MultiTargets() {
+		archs = append(archs, a.Arch.ArchType.String())
+	}
+	if len(archs) == 0 {
+		// assume this is a java library, dexpreopt for all arches for now
+		for _, target := range ctx.Config().Targets[android.Android] {
+			archs = append(archs, target.Arch.ArchType.String())
+		}
+		if inList(ctx.ModuleName(), globalConfig.SystemServerJars) && !d.isSDKLibrary {
+			// If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
+			archs = archs[:1]
+		}
+	}
+	if ctx.Config().SecondArchIsTranslated() {
+		// Only preopt primary arch for translated arch since there is only an image there.
+		archs = archs[:1]
+	}
+
+	dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
+
+	strippedDexJarFile := android.PathForModuleOut(ctx, "dexpreopt", dexJarFile.Base())
+
+	deps := android.Paths{dexJarFile}
+
+	var profileClassListing android.OptionalPath
+	profileIsTextListing := false
+	if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
+		// If dex_preopt.profile_guided is not set, default it based on the existence of the
+		// dexprepot.profile option or the profile class listing.
+		if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
+			profileClassListing = android.OptionalPathForPath(
+				android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
+			profileIsTextListing = true
+		} else {
+			profileClassListing = android.ExistentPathForSource(ctx,
+				ctx.Config().DexPreoptProfileDir(), ctx.ModuleName()+".prof")
+		}
+	}
+
+	if profileClassListing.Valid() {
+		deps = append(deps, profileClassListing.Path())
+	}
+
+	uncompressedDex := false
+	if ctx.Config().UncompressPrivAppDex() &&
+		(d.isPrivApp || inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules())) {
+		uncompressedDex = true
+	}
+
+	dexpreoptConfig := dexpreopt.ModuleConfig{
+		Name:                ctx.ModuleName(),
+		DexLocation:         dexLocation,
+		BuildPath:           android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").String(),
+		DexPath:             dexJarFile.String(),
+		PreferCodeIntegrity: false,
+		UncompressedDex:     uncompressedDex,
+		HasApkLibraries:     false,
+		PreoptFlags:         nil,
+
+		ProfileClassListing:  profileClassListing.String(),
+		ProfileIsTextListing: profileIsTextListing,
+
+		EnforceUsesLibraries:  false,
+		OptionalUsesLibraries: nil,
+		UsesLibraries:         nil,
+		LibraryPaths:          nil,
+
+		Archs:                  archs,
+		DexPreoptImageLocation: "",
+
+		PreoptExtractedApk: false,
+
+		NoCreateAppImage:    !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
+		ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
+
+		StripInputPath:  dexJarFile.String(),
+		StripOutputPath: strippedDexJarFile.String(),
+	}
+
+	dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(globalConfig, dexpreoptConfig)
+	if err != nil {
+		ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
+		return dexJarFile
+	}
+
+	var inputs android.Paths
+	for _, input := range dexpreoptRule.Inputs() {
+		if input == "" {
+			// Tests sometimes have empty configuration values that lead to empty inputs
+			continue
+		}
+		rel, isRel := android.MaybeRel(ctx, android.PathForModuleOut(ctx).String(), input)
+		if isRel {
+			inputs = append(inputs, android.PathForModuleOut(ctx, rel))
+		} else {
+			// TODO: use PathForOutput once boot image is moved to where PathForOutput can find it.
+			inputs = append(inputs, &bootImagePath{input})
+		}
+	}
+
+	var outputs android.WritablePaths
+	for _, output := range dexpreoptRule.Outputs() {
+		rel := android.Rel(ctx, android.PathForModuleOut(ctx).String(), output)
+		outputs = append(outputs, android.PathForModuleOut(ctx, rel))
+	}
+
+	for _, install := range dexpreoptRule.Installs() {
+		d.builtInstalled = append(d.builtInstalled, install.From+":"+install.To)
+	}
+
+	if len(dexpreoptRule.Commands()) > 0 {
+		ctx.Build(pctx, android.BuildParams{
+			Rule: ctx.Rule(pctx, "dexpreopt", blueprint.RuleParams{
+				Command:     strings.Join(proptools.NinjaEscape(dexpreoptRule.Commands()), " && "),
+				CommandDeps: dexpreoptRule.Tools(),
+			}),
+			Implicits:   inputs,
+			Outputs:     outputs,
+			Description: "dexpreopt",
+		})
+	}
+
+	stripRule, err := dexpreopt.GenerateStripRule(globalConfig, dexpreoptConfig)
+	if err != nil {
+		ctx.ModuleErrorf("error generating dexpreopt strip rule: %s", err.Error())
+		return dexJarFile
+	}
+
+	ctx.Build(pctx, android.BuildParams{
+		Rule: ctx.Rule(pctx, "dexpreopt_strip", blueprint.RuleParams{
+			Command:     strings.Join(proptools.NinjaEscape(stripRule.Commands()), " && "),
+			CommandDeps: stripRule.Tools(),
+		}),
+		Input:       dexJarFile,
+		Output:      strippedDexJarFile,
+		Description: "dexpreopt strip",
+	})
+
+	return strippedDexJarFile
+}
+
+type bootImagePath struct {
+	path string
+}
+
+var _ android.Path = (*bootImagePath)(nil)
+
+func (p *bootImagePath) String() string { return p.path }
+func (p *bootImagePath) Ext() string    { return filepath.Ext(p.path) }
+func (p *bootImagePath) Base() string   { return filepath.Base(p.path) }
+func (p *bootImagePath) Rel() string    { return p.path }
diff --git a/java/droiddoc.go b/java/droiddoc.go
index 0c4877a..bd3b3ab 100644
--- a/java/droiddoc.go
+++ b/java/droiddoc.go
@@ -400,6 +400,7 @@
 
 	metalavaStubsFlags                string
 	metalavaAnnotationsFlags          string
+	metalavaMergeAnnoDirFlags         string
 	metalavaInclusionAnnotationsFlags string
 	metalavaApiLevelsAnnotationsFlags string
 
@@ -626,7 +627,7 @@
 		case libTag:
 			switch dep := module.(type) {
 			case Dependency:
-				deps.classpath = append(deps.classpath, dep.ImplementationJars()...)
+				deps.classpath = append(deps.classpath, dep.HeaderJars()...)
 			case SdkLibraryDependency:
 				sdkVersion := j.sdkVersion()
 				linkType := javaSdk
@@ -1129,9 +1130,9 @@
 		Inputs:      d.Javadoc.srcFiles,
 		Implicits:   implicits,
 		Args: map[string]string{
-			"outDir":        android.PathForModuleOut(ctx, "out").String(),
-			"srcJarDir":     android.PathForModuleOut(ctx, "srcjars").String(),
-			"stubsDir":      android.PathForModuleOut(ctx, "stubsDir").String(),
+			"outDir":        android.PathForModuleOut(ctx, "dokka-out").String(),
+			"srcJarDir":     android.PathForModuleOut(ctx, "dokka-srcjars").String(),
+			"stubsDir":      android.PathForModuleOut(ctx, "dokka-stubsDir").String(),
 			"srcJars":       strings.Join(d.Javadoc.srcJars.Strings(), " "),
 			"classpathArgs": classpathArgs,
 			"opts":          opts,
@@ -1413,8 +1414,8 @@
 }
 
 func (d *Droidstubs) collectAnnotationsFlags(ctx android.ModuleContext,
-	implicits *android.Paths, implicitOutputs *android.WritablePaths) string {
-	var flags string
+	implicits *android.Paths, implicitOutputs *android.WritablePaths) (string, string) {
+	var flags, mergeAnnoDirFlags string
 	if Bool(d.properties.Annotations_enabled) {
 		flags += " --include-annotations"
 		validatingNullability :=
@@ -1451,17 +1452,18 @@
 		ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
 			if t, ok := m.(*ExportedDroiddocDir); ok {
 				*implicits = append(*implicits, t.deps...)
-				flags += " --merge-qualifier-annotations " + t.dir.String()
+				mergeAnnoDirFlags += " --merge-qualifier-annotations " + t.dir.String()
 			} else {
 				ctx.PropertyErrorf("merge_annotations_dirs",
 					"module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
 			}
 		})
+		flags += mergeAnnoDirFlags
 		// TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
 		flags += " --hide HiddenTypedefConstant --hide SuperfluousPrefix --hide AnnotationExtraction"
 	}
 
-	return flags
+	return flags, mergeAnnoDirFlags
 }
 
 func (d *Droidstubs) collectInclusionAnnotationsFlags(ctx android.ModuleContext,
@@ -1585,7 +1587,7 @@
 		Implicits: append(android.Paths{apiFile, removedApiFile, d.apiFile, d.removedApiFile},
 			implicits...),
 		Args: map[string]string{
-			"srcJarDir":         android.PathForModuleOut(ctx, "srcjars").String(),
+			"srcJarDir":         android.PathForModuleOut(ctx, "apicheck-srcjars").String(),
 			"srcJars":           strings.Join(d.Javadoc.srcJars.Strings(), " "),
 			"javaVersion":       javaVersion,
 			"bootclasspathArgs": bootclasspathArgs,
@@ -1644,7 +1646,8 @@
 	}
 
 	flags.metalavaStubsFlags = d.collectStubsFlags(ctx, &implicitOutputs)
-	flags.metalavaAnnotationsFlags = d.collectAnnotationsFlags(ctx, &implicits, &implicitOutputs)
+	flags.metalavaAnnotationsFlags, flags.metalavaMergeAnnoDirFlags =
+		d.collectAnnotationsFlags(ctx, &implicits, &implicitOutputs)
 	flags.metalavaInclusionAnnotationsFlags = d.collectInclusionAnnotationsFlags(ctx, &implicits, &implicitOutputs)
 	flags.metalavaApiLevelsAnnotationsFlags = d.collectAPILevelsAnnotationsFlags(ctx, &implicits, &implicitOutputs)
 	flags.metalavaApiToXmlFlags = d.collectApiToXmlFlags(ctx, &implicits, &implicitOutputs)
@@ -1670,7 +1673,7 @@
 		d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
 		opts := " " + d.Javadoc.args + " --check-compatibility:api:current " + apiFile.String() +
 			" --check-compatibility:removed:current " + removedApiFile.String() +
-			flags.metalavaInclusionAnnotationsFlags
+			flags.metalavaInclusionAnnotationsFlags + flags.metalavaMergeAnnoDirFlags + " "
 
 		d.transformCheckApi(ctx, apiFile, removedApiFile, metalavaCheckApiImplicits,
 			javaVersion, flags.bootClasspathArgs, flags.classpathArgs, flags.sourcepathArgs, opts,
@@ -1701,7 +1704,7 @@
 		d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
 		opts := " " + d.Javadoc.args + " --check-compatibility:api:released " + apiFile.String() +
 			flags.metalavaInclusionAnnotationsFlags + " --check-compatibility:removed:released " +
-			removedApiFile.String() + " "
+			removedApiFile.String() + flags.metalavaMergeAnnoDirFlags + " "
 
 		d.transformCheckApi(ctx, apiFile, removedApiFile, metalavaCheckApiImplicits,
 			javaVersion, flags.bootClasspathArgs, flags.classpathArgs, flags.sourcepathArgs, opts,
diff --git a/java/gen.go b/java/gen.go
index 993e6d1..8362556 100644
--- a/java/gen.go
+++ b/java/gen.go
@@ -14,10 +14,6 @@
 
 package java
 
-// This file generates the final rules for compiling all C/C++.  All properties related to
-// compiling should have been translated into builderFlags or another argument to the Transform*
-// functions.
-
 import (
 	"github.com/google/blueprint"
 
diff --git a/java/java.go b/java/java.go
index 5ed99f7..71967a8 100644
--- a/java/java.go
+++ b/java/java.go
@@ -217,25 +217,6 @@
 	// If set to true, compile dex regardless of installable.  Defaults to false.
 	Compile_dex *bool
 
-	Dex_preopt struct {
-		// If false, prevent dexpreopting and stripping the dex file from the final jar.  Defaults to
-		// true.
-		Enabled *bool
-
-		// If true, generate an app image (.art file) for this module.
-		App_image *bool
-
-		// If true, use a checked-in profile to guide optimization.  Defaults to false unless
-		// a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
-		// that matches the name of this module, in which case it is defaulted to true.
-		Profile_guided *bool
-
-		// If set, provides the path to profile relative to the Android.bp file.  If not set,
-		// defaults to searching for a file that matches the name of this module in the default
-		// profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
-		Profile *string
-	}
-
 	Optimize struct {
 		// If false, disable all optimization.  Defaults to true for android_app and android_test
 		// modules, false for java_library and java_test modules.
@@ -266,6 +247,7 @@
 	System_modules *string
 
 	UncompressDex bool `blueprint:"mutated"`
+	IsSDKLibrary  bool `blueprint:"mutated"`
 }
 
 // Module contains the properties and members used by all java module types
@@ -295,6 +277,9 @@
 	// output file containing classes.dex and resources
 	dexJarFile android.Path
 
+	// output file that contains classes.dex if it should be in the output file
+	maybeStrippedDexJarFile android.Path
+
 	// output file containing uninstrumented classes that will be instrumented by jacoco
 	jacocoReportClassesFile android.Path
 
@@ -327,6 +312,8 @@
 	// list of source files, collected from compiledJavaSrcs and compiledSrcJars
 	// filter out Exclude_srcs, will be used by android.IDEInfo struct
 	expandIDEInfoCompiledSrcs []string
+
+	dexpreopter
 }
 
 func (j *Module) Srcs() android.Paths {
@@ -568,7 +555,7 @@
 		return ret
 	}
 
-	if ctx.Config().UnbundledBuild() && v != "" {
+	if ctx.Config().UnbundledBuildPrebuiltSdks() && v != "" {
 		return toPrebuilt(v)
 	}
 
@@ -1332,7 +1319,15 @@
 
 		j.dexJarFile = dexOutputFile
 
+		dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
+
+		j.maybeStrippedDexJarFile = dexOutputFile
+
 		outputFile = dexOutputFile
+
+		if ctx.Failed() {
+			return
+		}
 	} else {
 		outputFile = implementationAndResourcesJar
 	}
@@ -1425,13 +1420,19 @@
 	return instrumentedJar
 }
 
-var _ Dependency = (*Library)(nil)
+var _ Dependency = (*Module)(nil)
 
 func (j *Module) HeaderJars() android.Paths {
+	if j.headerJarFile == nil {
+		return nil
+	}
 	return android.Paths{j.headerJarFile}
 }
 
 func (j *Module) ImplementationJars() android.Paths {
+	if j.implementationJarFile == nil {
+		return nil
+	}
 	return android.Paths{j.implementationJarFile}
 }
 
@@ -1443,14 +1444,19 @@
 }
 
 func (j *Module) ImplementationAndResourcesJars() android.Paths {
+	if j.implementationAndResourcesJar == nil {
+		return nil
+	}
 	return android.Paths{j.implementationAndResourcesJar}
 }
 
 func (j *Module) AidlIncludeDirs() android.Paths {
+	// exportAidlIncludeDirs is type android.Paths already
 	return j.exportAidlIncludeDirs
 }
 
 func (j *Module) ExportedSdkLibs() []string {
+	// exportedSdkLibs is type []string
 	return j.exportedSdkLibs
 }
 
@@ -1486,9 +1492,17 @@
 }
 
 func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+	j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
+	j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
 	j.compile(ctx)
 
 	if Bool(j.properties.Installable) || ctx.Host() {
+		if j.deviceProperties.UncompressDex {
+			alignedOutputFile := android.PathForModuleOut(ctx, "aligned", ctx.ModuleName()+".jar")
+			TransformZipAlign(ctx, alignedOutputFile, j.outputFile)
+			j.outputFile = alignedOutputFile
+		}
+
 		j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
 			ctx.ModuleName()+".jar", j.outputFile)
 	}
@@ -1504,6 +1518,7 @@
 	module.AddProperties(
 		&module.Module.properties,
 		&module.Module.deviceProperties,
+		&module.Module.dexpreoptProperties,
 		&module.Module.protoProperties)
 
 	InitJavaModule(module, android.HostAndDeviceSupported)
@@ -1574,6 +1589,7 @@
 	module.AddProperties(
 		&module.Module.properties,
 		&module.Module.deviceProperties,
+		&module.Module.dexpreoptProperties,
 		&module.Module.protoProperties,
 		&module.testProperties)
 
@@ -1670,6 +1686,7 @@
 	module.AddProperties(
 		&module.Module.properties,
 		&module.Module.deviceProperties,
+		&module.Module.dexpreoptProperties,
 		&module.Module.protoProperties,
 		&module.binaryProperties)
 
@@ -1799,10 +1816,16 @@
 var _ Dependency = (*Import)(nil)
 
 func (j *Import) HeaderJars() android.Paths {
+	if j.combinedClasspathFile == nil {
+		return nil
+	}
 	return android.Paths{j.combinedClasspathFile}
 }
 
 func (j *Import) ImplementationJars() android.Paths {
+	if j.combinedClasspathFile == nil {
+		return nil
+	}
 	return android.Paths{j.combinedClasspathFile}
 }
 
@@ -1811,6 +1834,9 @@
 }
 
 func (j *Import) ImplementationAndResourcesJars() android.Paths {
+	if j.combinedClasspathFile == nil {
+		return nil
+	}
 	return android.Paths{j.combinedClasspathFile}
 }
 
@@ -1822,6 +1848,10 @@
 	return j.exportedSdkLibs
 }
 
+// Add compile time check for interface implementation
+var _ android.IDEInfo = (*Import)(nil)
+var _ android.IDECustomizedModuleName = (*Import)(nil)
+
 // Collect information for opening IDE project files in java/jdeps.go.
 const (
 	removedPrefix = "prebuilt_"
@@ -1889,6 +1919,7 @@
 	module.AddProperties(
 		&CompilerProperties{},
 		&CompilerDeviceProperties{},
+		&DexpreoptProperties{},
 		&android.ProtoProperties{},
 		&aaptProperties{},
 		&androidLibraryProperties{},
diff --git a/java/jdeps.go b/java/jdeps.go
index c7fa42a..2eaeab8 100644
--- a/java/jdeps.go
+++ b/java/jdeps.go
@@ -78,9 +78,9 @@
 		if data.Class != "" {
 			dpInfo.Classes = append(dpInfo.Classes, data.Class)
 		}
-		out := data.OutputFile.String()
-		if out != "" {
-			dpInfo.Installed_paths = append(dpInfo.Installed_paths, out)
+
+		if dep, ok := module.(Dependency); ok {
+			dpInfo.Installed_paths = append(dpInfo.Installed_paths, dep.ImplementationJars().Strings()...)
 		}
 		dpInfo.Classes = android.FirstUniqueStrings(dpInfo.Classes)
 		dpInfo.Installed_paths = android.FirstUniqueStrings(dpInfo.Installed_paths)
diff --git a/java/sdk_library.go b/java/sdk_library.go
index 573fc8e..877abe4 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -141,8 +141,9 @@
 	android.ModuleBase
 	android.DefaultableModuleBase
 
-	properties       sdkLibraryProperties
-	deviceProperties CompilerDeviceProperties
+	properties          sdkLibraryProperties
+	deviceProperties    CompilerDeviceProperties
+	dexpreoptProperties DexpreoptProperties
 
 	publicApiStubsPath android.Paths
 	systemApiStubsPath android.Paths
@@ -541,11 +542,6 @@
 			props.Srcs_lib_whitelist_pkgs = []string{"android.annotation"}
 		}
 	}
-	// These libs are required by doclava to parse the framework sources add via
-	// Src_lib and Src_lib_whitelist_* properties just above.
-	// If we don't add them to the classpath, errors messages are generated by doclava,
-	// though they don't break the build.
-	props.Libs = append(props.Libs, "framework")
 
 	if Bool(module.properties.Metalava_enabled) == true {
 		mctx.CreateModule(android.ModuleFactoryAdaptor(DroidstubsFactory), &props)
@@ -569,6 +565,7 @@
 		Errorprone       struct {
 			Javacflags []string
 		}
+		IsSDKLibrary bool
 	}{}
 
 	props.Name = proptools.StringPtr(module.implName())
@@ -579,6 +576,7 @@
 	// XML file is installed along with the impl lib
 	props.Required = []string{module.xmlFileName()}
 	props.Errorprone.Javacflags = module.properties.Errorprone.Javacflags
+	props.IsSDKLibrary = true
 
 	if module.SocSpecific() {
 		props.Soc_specific = proptools.BoolPtr(true)
@@ -588,7 +586,10 @@
 		props.Product_specific = proptools.BoolPtr(true)
 	}
 
-	mctx.CreateModule(android.ModuleFactoryAdaptor(LibraryFactory), &props, &module.deviceProperties)
+	mctx.CreateModule(android.ModuleFactoryAdaptor(LibraryFactory),
+		&props,
+		&module.deviceProperties,
+		&module.dexpreoptProperties)
 }
 
 // Creates the xml file that publicizes the runtime library
@@ -721,6 +722,7 @@
 	module := &sdkLibrary{}
 	module.AddProperties(&module.properties)
 	module.AddProperties(&module.deviceProperties)
+	module.AddProperties(&module.dexpreoptProperties)
 	InitJavaModule(module, android.DeviceSupported)
 	return module
 }
diff --git a/python/builder.go b/python/builder.go
index 11a792a..cbbe56e 100644
--- a/python/builder.go
+++ b/python/builder.go
@@ -45,20 +45,24 @@
 	hostPar = pctx.AndroidStaticRule("hostPar",
 		blueprint.RuleParams{
 			Command: `sed -e 's/%interpreter%/$interp/g' -e 's/%main%/$main/g' $template > $stub && ` +
-				`$mergeParCmd -p -pm $stub $mergedZip $srcsZips && echo '#!/usr/bin/env python' | cat - $mergedZip > $out && ` +
-				`chmod +x $out && (rm -f $stub; rm -f $mergedZip)`,
+				`echo "#!/usr/bin/env python" >${out}.prefix &&` +
+				`$mergeParCmd -p --prefix ${out}.prefix -pm $stub $out $srcsZips && ` +
+				`chmod +x $out && (rm -f $stub; rm -f ${out}.prefix)`,
 			CommandDeps: []string{"$mergeParCmd"},
 		},
-		"interp", "main", "template", "stub", "mergedZip", "srcsZips")
+		"interp", "main", "template", "stub", "srcsZips")
 
 	embeddedPar = pctx.AndroidStaticRule("embeddedPar",
 		blueprint.RuleParams{
-			Command: `echo '$main' > $entryPoint &&` +
-				`$mergeParCmd -p -e $entryPoint $mergedZip $srcsZips && cat $launcher | cat - $mergedZip > $out && ` +
-				`chmod +x $out && (rm -f $entryPoint; rm -f $mergedZip)`,
+			// `echo -n` to trim the newline, since the python code just wants the name.
+			// /bin/sh (used by ninja) on Mac turns off posix mode, and stops supporting -n.
+			// Explicitly use bash instead.
+			Command: `/bin/bash -c "echo -n '$main' > $entryPoint" &&` +
+				`$mergeParCmd -p --prefix $launcher -e $entryPoint $out $srcsZips && ` +
+				`chmod +x $out && (rm -f $entryPoint)`,
 			CommandDeps: []string{"$mergeParCmd"},
 		},
-		"main", "entryPoint", "mergedZip", "srcsZips", "launcher")
+		"main", "entryPoint", "srcsZips", "launcher")
 )
 
 func init() {
@@ -73,9 +77,6 @@
 	launcherPath android.OptionalPath, interpreter, main, binName string,
 	srcsZips android.Paths) android.Path {
 
-	// .intermediate output path for merged zip file.
-	mergedZip := android.PathForModuleOut(ctx, binName+".mergedzip")
-
 	// .intermediate output path for bin executable.
 	binFile := android.PathForModuleOut(ctx, binName)
 
@@ -96,12 +97,11 @@
 			Output:      binFile,
 			Implicits:   implicits,
 			Args: map[string]string{
-				"interp":    strings.Replace(interpreter, "/", `\/`, -1),
-				"main":      strings.Replace(main, "/", `\/`, -1),
-				"template":  template.String(),
-				"stub":      stub,
-				"mergedZip": mergedZip.String(),
-				"srcsZips":  strings.Join(srcsZips.Strings(), " "),
+				"interp":   strings.Replace(interpreter, "/", `\/`, -1),
+				"main":     strings.Replace(main, "/", `\/`, -1),
+				"template": template.String(),
+				"stub":     stub,
+				"srcsZips": strings.Join(srcsZips.Strings(), " "),
 			},
 		})
 	} else if launcherPath.Valid() {
@@ -117,9 +117,8 @@
 			Output:      binFile,
 			Implicits:   implicits,
 			Args: map[string]string{
-				"main":       main,
+				"main":       strings.Replace(strings.TrimSuffix(main, pyExt), "/", ".", -1),
 				"entryPoint": entryPoint,
-				"mergedZip":  mergedZip.String(),
 				"srcsZips":   strings.Join(srcsZips.Strings(), " "),
 				"launcher":   launcherPath.String(),
 			},
diff --git a/scripts/build-ndk-prebuilts.sh b/scripts/build-ndk-prebuilts.sh
index 81f8564..947458a 100755
--- a/scripts/build-ndk-prebuilts.sh
+++ b/scripts/build-ndk-prebuilts.sh
@@ -48,7 +48,8 @@
     "Malloc_not_svelte": false,
     "Safestack": false,
 
-    "Ndk_abis": true
+    "Ndk_abis": true,
+    "Exclude_draft_ndk_apis": true
 }
 EOF
 m --skip-make ${SOONG_OUT}/ndk.timestamp
diff --git a/scripts/manifest_fixer.py b/scripts/manifest_fixer.py
index 64f49cb..ebfc4d8 100755
--- a/scripts/manifest_fixer.py
+++ b/scripts/manifest_fixer.py
@@ -61,9 +61,9 @@
                       help='specify additional <uses-library> tag to add. android:requred is set to false')
   parser.add_argument('--uses-non-sdk-api', dest='uses_non_sdk_api', action='store_true',
                       help='manifest is for a package built against the platform')
-  parser.add_argument('--prefer-integrity', dest='prefer_integrity', action='store_true',
-                      help=('specify if the app prefers strict integrity. Should not be conflict if ' +
-                            'already declared in the manifest.'))
+  parser.add_argument('--prefer-code-integrity', dest='prefer_code_integrity', action='store_true',
+                      help=('specify if the app prefers strict code integrity. Should not be conflict '
+                            'if already declared in the manifest.'))
   parser.add_argument('input', help='input AndroidManifest.xml file')
   parser.add_argument('output', help='output AndroidManifest.xml file')
   return parser.parse_args()
@@ -272,7 +272,7 @@
     application.setAttributeNode(attr)
 
 
-def add_prefer_integrity(doc):
+def add_prefer_code_integrity(doc):
   manifest = parse_manifest(doc)
   elems = get_children_with_tag(manifest, 'application')
   application = elems[0] if len(elems) == 1 else None
@@ -285,13 +285,13 @@
     manifest.insertBefore(doc.createTextNode(indent), first)
     manifest.insertBefore(application, first)
 
-  attr = application.getAttributeNodeNS(android_ns, 'preferIntegrity')
+  attr = application.getAttributeNodeNS(android_ns, 'preferCodeIntegrity')
   if attr is None:
-    attr = doc.createAttributeNS(android_ns, 'android:preferIntegrity')
+    attr = doc.createAttributeNS(android_ns, 'android:preferCodeIntegrity')
     attr.value = 'true'
     application.setAttributeNode(attr)
   elif attr.value != 'true':
-    raise RuntimeError('existing attribute mismatches the option of --prefer-integrity')
+    raise RuntimeError('existing attribute mismatches the option of --prefer-code-integrity')
 
 
 def write_xml(f, doc):
@@ -321,8 +321,8 @@
     if args.uses_non_sdk_api:
       add_uses_non_sdk_api(doc)
 
-    if args.prefer_integrity:
-      add_prefer_integrity(doc)
+    if args.prefer_code_integrity:
+      add_prefer_code_integrity(doc)
 
     with open(args.output, 'wb') as f:
       write_xml(f, doc)
diff --git a/scripts/manifest_fixer_test.py b/scripts/manifest_fixer_test.py
index a621445..edc98cd 100755
--- a/scripts/manifest_fixer_test.py
+++ b/scripts/manifest_fixer_test.py
@@ -346,12 +346,12 @@
     self.assertEqual(output, expected)
 
 
-class PreferIntegrityTest(unittest.TestCase):
-  """Unit tests for add_prefer_integrity function."""
+class PreferCodeIntegrityTest(unittest.TestCase):
+  """Unit tests for add_prefer_code_integrity function."""
 
   def run_test(self, input_manifest):
     doc = minidom.parseString(input_manifest)
-    manifest_fixer.add_prefer_integrity(doc)
+    manifest_fixer.add_prefer_code_integrity(doc)
     output = StringIO.StringIO()
     manifest_fixer.write_xml(output, doc)
     return output.getvalue()
@@ -362,23 +362,23 @@
       '    <application%s/>\n'
       '</manifest>\n')
 
-  def prefer_integrity(self, value):
-    return ' android:preferIntegrity="%s"' % value
+  def prefer_code_integrity(self, value):
+    return ' android:preferCodeIntegrity="%s"' % value
 
   def test_manifest_with_undeclared_preference(self):
     manifest_input = self.manifest_tmpl % ''
-    expected = self.manifest_tmpl % self.prefer_integrity('true')
+    expected = self.manifest_tmpl % self.prefer_code_integrity('true')
     output = self.run_test(manifest_input)
     self.assertEqual(output, expected)
 
-  def test_manifest_with_prefer_integrity(self):
-    manifest_input = self.manifest_tmpl % self.prefer_integrity('true')
+  def test_manifest_with_prefer_code_integrity(self):
+    manifest_input = self.manifest_tmpl % self.prefer_code_integrity('true')
     expected = manifest_input
     output = self.run_test(manifest_input)
     self.assertEqual(output, expected)
 
-  def test_manifest_with_not_prefer_integrity(self):
-    manifest_input = self.manifest_tmpl % self.prefer_integrity('false')
+  def test_manifest_with_not_prefer_code_integrity(self):
+    manifest_input = self.manifest_tmpl % self.prefer_code_integrity('false')
     self.assertRaises(RuntimeError, self.run_test, manifest_input)
 
 if __name__ == '__main__':
diff --git a/ui/build/ninja.go b/ui/build/ninja.go
index c8f19d1..ab15e86 100644
--- a/ui/build/ninja.go
+++ b/ui/build/ninja.go
@@ -97,6 +97,7 @@
 		}
 	}()
 
+	ctx.Status.Status("Starting ninja...")
 	cmd.RunAndPrintOrFatal()
 }
 
diff --git a/ui/build/paths/config.go b/ui/build/paths/config.go
index d1d7c60..e2e4368 100644
--- a/ui/build/paths/config.go
+++ b/ui/build/paths/config.go
@@ -75,18 +75,14 @@
 
 var Configuration = map[string]PathConfig{
 	"awk":       Allowed,
-	"basename":  Allowed,
 	"bash":      Allowed,
 	"bc":        Allowed,
 	"bzip2":     Allowed,
 	"chmod":     Allowed,
-	"cmp":       Allowed,
 	"cp":        Allowed,
-	"cut":       Allowed,
 	"date":      Allowed,
 	"dd":        Allowed,
 	"diff":      Allowed,
-	"dirname":   Allowed,
 	"du":        Allowed,
 	"echo":      Allowed,
 	"egrep":     Allowed,
@@ -98,7 +94,6 @@
 	"git":       Allowed,
 	"grep":      Allowed,
 	"gzip":      Allowed,
-	"head":      Allowed,
 	"hexdump":   Allowed,
 	"hostname":  Allowed,
 	"jar":       Allowed,
@@ -109,27 +104,22 @@
 	"lsof":      Allowed,
 	"m4":        Allowed,
 	"md5sum":    Allowed,
-	"mkdir":     Allowed,
 	"mktemp":    Allowed,
 	"mv":        Allowed,
 	"openssl":   Allowed,
-	"paste":     Allowed,
 	"patch":     Allowed,
 	"pgrep":     Allowed,
 	"pkill":     Allowed,
 	"ps":        Allowed,
 	"pstree":    Allowed,
-	"pwd":       Allowed,
 	"python":    Allowed,
 	"python2.7": Allowed,
 	"python3":   Allowed,
 	"readlink":  Allowed,
 	"realpath":  Allowed,
 	"rm":        Allowed,
-	"rmdir":     Allowed,
 	"rsync":     Allowed,
 	"sed":       Allowed,
-	"setsid":    Allowed,
 	"sh":        Allowed,
 	"sha1sum":   Allowed,
 	"sha256sum": Allowed,
@@ -137,17 +127,12 @@
 	"sort":      Allowed,
 	"stat":      Allowed,
 	"tar":       Allowed,
-	"tee":       Allowed,
 	"timeout":   Allowed,
-	"todos":     Allowed,
-	"touch":     Allowed,
 	"tr":        Allowed,
-	"unix2dos":  Allowed,
 	"unzip":     Allowed,
 	"wc":        Allowed,
 	"which":     Allowed,
 	"xargs":     Allowed,
-	"xxd":       Allowed,
 	"xz":        Allowed,
 	"zip":       Allowed,
 	"zipinfo":   Allowed,
@@ -167,17 +152,31 @@
 	"pkg-config": Forbidden,
 
 	// On linux we'll use the toybox version of these instead
-	"cat":    Toybox,
-	"comm":   Toybox,
-	"env":    Toybox,
-	"id":     Toybox,
-	"od":     Toybox,
-	"sleep":  Toybox,
-	"tail":   Toybox,
-	"true":   Toybox,
-	"uname":  Toybox,
-	"uniq":   Toybox,
-	"whoami": Toybox,
+	"basename": Toybox,
+	"cat":      Toybox,
+	"cmp":      Toybox,
+	"comm":     Toybox,
+	"cut":      Toybox,
+	"dirname":  Toybox,
+	"env":      Toybox,
+	"head":     Toybox,
+	"id":       Toybox,
+	"mkdir":    Toybox,
+	"od":       Toybox,
+	"paste":    Toybox,
+	"pwd":      Toybox,
+	"rmdir":    Toybox,
+	"setsid":   Toybox,
+	"sleep":    Toybox,
+	"tail":     Toybox,
+	"tee":      Toybox,
+	"touch":    Toybox,
+	"true":     Toybox,
+	"uname":    Toybox,
+	"uniq":     Toybox,
+	"unix2dos": Toybox,
+	"whoami":   Toybox,
+	"xxd":      Toybox,
 }
 
 func init() {
diff --git a/ui/status/status.go b/ui/status/status.go
index c851d7f..46ec72e 100644
--- a/ui/status/status.go
+++ b/ui/status/status.go
@@ -260,6 +260,10 @@
 	}
 }
 
+func (s *Status) Status(msg string) {
+	s.message(StatusLvl, msg)
+}
+
 type toolStatus struct {
 	status *Status
 
diff --git a/zip/zip.go b/zip/zip.go
index 774966a..1f5fe43 100644
--- a/zip/zip.go
+++ b/zip/zip.go
@@ -317,7 +317,7 @@
 					Err:  os.ErrNotExist,
 				}
 				if args.IgnoreMissingFiles {
-					fmt.Fprintln(args.Stderr, "warning:", err)
+					fmt.Fprintln(z.stderr, "warning:", err)
 				} else {
 					return err
 				}
@@ -334,7 +334,7 @@
 					Err:  os.ErrNotExist,
 				}
 				if args.IgnoreMissingFiles {
-					fmt.Fprintln(args.Stderr, "warning:", err)
+					fmt.Fprintln(z.stderr, "warning:", err)
 				} else {
 					return err
 				}
@@ -345,7 +345,7 @@
 					Err:  syscall.ENOTDIR,
 				}
 				if args.IgnoreMissingFiles {
-					fmt.Fprintln(args.Stderr, "warning:", err)
+					fmt.Fprintln(z.stderr, "warning:", err)
 				} else {
 					return err
 				}