Merge "Switch to toybox chmod(1)."
diff --git a/Android.bp b/Android.bp
index 76f6798..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",
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 d1fb6f4..8d99d56 100644
--- a/android/apex.go
+++ b/android/apex.go
@@ -122,15 +122,13 @@
 
 func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []blueprint.Module {
 	if len(m.apexVariations) > 0 {
-		// The original module is mutated into "platform" variation.
-		variations := []string{"platform"}
-		for _, a := range m.apexVariations {
-			variations = append(variations, a)
-		}
+		variations := []string{""} // Original variation for platform
+		variations = append(variations, m.apexVariations...)
+
 		modules := mctx.CreateVariations(variations...)
 		for i, m := range modules {
 			if i == 0 {
-				continue // platform
+				continue
 			}
 			m.(ApexModule).setApexName(variations[i])
 		}
diff --git a/android/config.go b/android/config.go
index 54c9da8..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)
 }
@@ -732,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] {
@@ -854,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
diff --git a/android/module.go b/android/module.go
index dc0c856..d2f84ce 100644
--- a/android/module.go
+++ b/android/module.go
@@ -190,6 +190,7 @@
 	GetProperties() []interface{}
 
 	BuildParamsForTests() []BuildParams
+	VariablesForTests() map[string]string
 }
 
 type nameProperties struct {
@@ -473,6 +474,7 @@
 
 	// For tests
 	buildParams []BuildParams
+	variables   map[string]string
 
 	prefer32 func(ctx BaseModuleContext, base *ModuleBase, class OsClass) bool
 }
@@ -489,6 +491,10 @@
 	return a.buildParams
 }
 
+func (a *ModuleBase) VariablesForTests() map[string]string {
+	return a.variables
+}
+
 func (a *ModuleBase) Prefer32(prefer32 func(ctx BaseModuleContext, base *ModuleBase, class OsClass) bool) {
 	a.prefer32 = prefer32
 }
@@ -781,6 +787,7 @@
 		installDeps:            a.computeInstallDeps(blueprintCtx),
 		installFiles:           a.installFiles,
 		missingDeps:            blueprintCtx.GetMissingDependencies(),
+		variables:              make(map[string]string),
 	}
 
 	desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
@@ -842,6 +849,7 @@
 	}
 
 	a.buildParams = ctx.buildParams
+	a.variables = ctx.variables
 }
 
 type androidBaseContextImpl struct {
@@ -864,6 +872,7 @@
 
 	// For tests
 	buildParams []BuildParams
+	variables   map[string]string
 }
 
 func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
@@ -928,6 +937,10 @@
 }
 
 func (a *androidModuleContext) Variable(pctx PackageContext, name, value string) {
+	if a.config.captureBuild {
+		a.variables[name] = value
+	}
+
 	a.ModuleContext.Variable(pctx.PackageContext, name, value)
 }
 
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 f496008..a1cdea1 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -177,28 +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"`
-	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"`
 
@@ -257,6 +259,8 @@
 	Exclude_draft_ndk_apis *bool `json:",omitempty"`
 
 	FlattenApex *bool `json:",omitempty"`
+
+	DexpreoptGlobalConfig *string `json:",omitempty"`
 }
 
 func boolPtr(v bool) *bool {
diff --git a/androidmk/cmd/androidmk/android.go b/androidmk/cmd/androidmk/android.go
index 99b3523..d16ac93 100644
--- a/androidmk/cmd/androidmk/android.go
+++ b/androidmk/cmd/androidmk/android.go
@@ -144,6 +144,7 @@
 			"LOCAL_AAPT_FLAGS":            "aaptflags",
 			"LOCAL_PACKAGE_SPLITS":        "package_splits",
 			"LOCAL_COMPATIBILITY_SUITE":   "test_suites",
+			"LOCAL_OVERRIDES_PACKAGES":    "overrides",
 
 			"LOCAL_ANNOTATION_PROCESSORS":        "annotation_processors",
 			"LOCAL_ANNOTATION_PROCESSOR_CLASSES": "annotation_processor_classes",
@@ -738,27 +739,31 @@
 		true: "product_variables.pdk"},
 }
 
-func mydir(args []string) string {
-	return "."
+func mydir(args []string) []string {
+	return []string{"."}
 }
 
-func allFilesUnder(wildcard string) func(args []string) string {
-	return func(args []string) string {
-		dir := ""
+func allFilesUnder(wildcard string) func(args []string) []string {
+	return func(args []string) []string {
+		dirs := []string{""}
 		if len(args) > 0 {
-			dir = strings.TrimSpace(args[0])
+			dirs = strings.Fields(args[0])
 		}
 
-		return fmt.Sprintf("%s/**/"+wildcard, dir)
+		paths := make([]string, len(dirs))
+		for i := range paths {
+			paths[i] = fmt.Sprintf("%s/**/"+wildcard, dirs[i])
+		}
+		return paths
 	}
 }
 
-func allSubdirJavaFiles(args []string) string {
-	return "**/*.java"
+func allSubdirJavaFiles(args []string) []string {
+	return []string{"**/*.java"}
 }
 
-func includeIgnored(args []string) string {
-	return include_ignored
+func includeIgnored(args []string) []string {
+	return []string{include_ignored}
 }
 
 var moduleTypes = map[string]string{
diff --git a/androidmk/cmd/androidmk/androidmk_test.go b/androidmk/cmd/androidmk/androidmk_test.go
index 0f6c5ac..40fc9f3 100644
--- a/androidmk/cmd/androidmk/androidmk_test.go
+++ b/androidmk/cmd/androidmk/androidmk_test.go
@@ -554,6 +554,10 @@
 			LOCAL_SRC_FILES := d.java
 			LOCAL_UNINSTALLABLE_MODULE := false
 			include $(BUILD_JAVA_LIBRARY)
+
+			include $(CLEAR_VARS)
+			LOCAL_SRC_FILES := $(call all-java-files-under, src gen)
+			include $(BUILD_STATIC_JAVA_LIBRARY)
 		`,
 		expected: `
 			java_library {
@@ -574,6 +578,13 @@
 				installable: true,
 				srcs: ["d.java"],
 			}
+
+			java_library {
+				srcs: [
+					"src/**/*.java",
+					"gen/**/*.java",
+				],
+			}
 		`,
 	},
 	{
diff --git a/androidmk/cmd/androidmk/values.go b/androidmk/cmd/androidmk/values.go
index d240a01..90f2e74 100644
--- a/androidmk/cmd/androidmk/values.go
+++ b/androidmk/cmd/androidmk/values.go
@@ -29,6 +29,14 @@
 	}
 }
 
+func stringListToStringValueList(list []string) []bpparser.Expression {
+	valList := make([]bpparser.Expression, len(list))
+	for i, l := range list {
+		valList[i] = stringToStringValue(l)
+	}
+	return valList
+}
+
 func addValues(val1, val2 bpparser.Expression) (bpparser.Expression, error) {
 	if val1 == nil {
 		return val2, nil
@@ -63,7 +71,10 @@
 
 	for i, s := range ms.Strings[1:] {
 		if ret, ok := ms.Variables[i].EvalFunction(scope); ok {
-			val, err = addValues(val, stringToStringValue(ret))
+			if len(ret) > 1 {
+				return nil, fmt.Errorf("Unexpected list value %s", ms.Dump())
+			}
+			val, err = addValues(val, stringToStringValue(ret[0]))
 		} else {
 			name := ms.Variables[i].Name
 			if !name.Const() {
@@ -125,9 +136,7 @@
 	for _, f := range fields {
 		if len(f.Variables) == 1 && f.Strings[0] == "" && f.Strings[1] == "" {
 			if ret, ok := f.Variables[0].EvalFunction(scope); ok {
-				listValue.Values = append(listValue.Values, &bpparser.String{
-					Value: ret,
-				})
+				listValue.Values = append(listValue.Values, stringListToStringValueList(ret)...)
 			} else {
 				// Variable by itself, variable is probably a list
 				if !f.Variables[0].Name.Const() {
diff --git a/androidmk/parser/scope.go b/androidmk/parser/scope.go
index 167e470..8111c89 100644
--- a/androidmk/parser/scope.go
+++ b/androidmk/parser/scope.go
@@ -21,13 +21,13 @@
 type Scope interface {
 	Get(name string) string
 	Set(name, value string)
-	Call(name string, args []string) string
-	SetFunc(name string, f func([]string) string)
+	Call(name string, args []string) []string
+	SetFunc(name string, f func([]string) []string)
 }
 
 type scope struct {
 	variables map[string]string
-	functions map[string]func([]string) string
+	functions map[string]func([]string) []string
 	parent    Scope
 }
 
@@ -47,22 +47,22 @@
 	s.variables[name] = value
 }
 
-func (s *scope) Call(name string, args []string) string {
+func (s *scope) Call(name string, args []string) []string {
 	if f, ok := s.functions[name]; ok {
 		return f(args)
 	}
 
-	return "<func:'" + name + "' unset>"
+	return []string{"<func:'" + name + "' unset>"}
 }
 
-func (s *scope) SetFunc(name string, f func([]string) string) {
+func (s *scope) SetFunc(name string, f func([]string) []string) {
 	s.functions[name] = f
 }
 
 func NewScope(parent Scope) Scope {
 	return &scope{
 		variables: make(map[string]string),
-		functions: make(map[string]func([]string) string),
+		functions: make(map[string]func([]string) []string),
 		parent:    parent,
 	}
 }
@@ -74,7 +74,7 @@
 	builtinScope[builtinDollar] = "$"
 }
 
-func (v Variable) EvalFunction(scope Scope) (string, bool) {
+func (v Variable) EvalFunction(scope Scope) ([]string, bool) {
 	f := v.Name.SplitN(" \t", 2)
 	if len(f) > 1 && f[0].Const() {
 		fname := f[0].Value(nil)
@@ -88,17 +88,20 @@
 			if fname == "call" {
 				return scope.Call(argVals[0], argVals[1:]), true
 			} else {
-				return "__builtin_func:" + fname + " " + strings.Join(argVals, " "), true
+				return []string{"__builtin_func:" + fname + " " + strings.Join(argVals, " ")}, true
 			}
 		}
 	}
 
-	return "", false
+	return []string{""}, false
 }
 
 func (v Variable) Value(scope Scope) string {
 	if ret, ok := v.EvalFunction(scope); ok {
-		return ret
+		if len(ret) > 1 {
+			panic("Expected a single value, but instead got a list")
+		}
+		return ret[0]
 	}
 	if scope == nil {
 		panic("Cannot take the value of a variable in a nil scope")
diff --git a/apex/apex.go b/apex/apex.go
index c14cd9e..d91bb3c 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -56,12 +56,12 @@
 			`--file_contexts ${file_contexts} ` +
 			`--canned_fs_config ${canned_fs_config} ` +
 			`--payload_type image ` +
-			`--key ${key} ${image_dir} ${out} `,
+			`--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} && ` +
@@ -214,6 +214,10 @@
 	// 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"
@@ -328,6 +332,7 @@
 	archType   android.ArchType
 	installDir string
 	class      apexFileClass
+	module     android.Module
 }
 
 type apexBundle struct {
@@ -349,26 +354,27 @@
 }
 
 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...)
 }
 
 func (a *apexBundle) DepsMutator(ctx android.BottomUpMutatorContext) {
 	targets := ctx.MultiTargets()
+	config := ctx.DeviceConfig()
 	has32BitTarget := false
 	for _, target := range targets {
 		if target.Arch.ArchType.Multilib == "lib32" {
@@ -380,27 +386,29 @@
 		// multilib.both.
 		ctx.AddFarVariationDependencies([]blueprint.Variation{
 			{Mutator: "arch", Variation: target.String()},
-			{Mutator: "image", Variation: "core"},
+			{Mutator: "image", Variation: a.getImageVariation(config)},
 			{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(config))
 
 		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(config)},
 			}, 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(config))
 		}
 
 		switch target.Arch.ArchType.Multilib {
@@ -408,21 +416,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(config))
 
 			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(config))
 		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(config))
 
 			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(config))
 			}
 		}
 
@@ -449,13 +461,25 @@
 }
 
 func (a *apexBundle) Srcs() android.Paths {
-	return android.Paths{a.outputFiles[imageApex]}
+	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(config android.DeviceConfig) string {
+	if config.VndkVersion() != "" && 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.
@@ -495,6 +519,7 @@
 	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" {
@@ -517,7 +542,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)
@@ -525,7 +550,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)
@@ -536,7 +561,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 {
@@ -545,7 +570,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)
@@ -553,6 +578,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)
@@ -574,7 +605,7 @@
 					}
 					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
 				}
 			}
@@ -617,18 +648,19 @@
 	a.filesInfo = filesInfo
 
 	if a.apexTypes.zip() {
-		a.buildUnflattenedApex(ctx, keyFile, certificate, zipApex)
+		a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, zipApex)
 	}
 	if a.apexTypes.image() {
 		if ctx.Config().FlattenApex() {
 			a.buildFlattenedApex(ctx)
 		} else {
-			a.buildUnflattenedApex(ctx, keyFile, certificate, imageApex)
+			a.buildUnflattenedApex(ctx, keyFile, pubKeyFile, certificate, imageApex)
 		}
 	}
 }
 
-func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext, keyFile android.Path, certificate java.Certificate, apexType apexPackaging) {
+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)
@@ -716,8 +748,14 @@
 		}
 		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,
@@ -732,6 +770,7 @@
 				"file_contexts":    fileContexts.String(),
 				"canned_fs_config": cannedFsConfig.String(),
 				"key":              keyFile.String(),
+				"opt_flags":        strings.Join(optFlags, " "),
 			},
 		})
 
@@ -793,7 +832,7 @@
 		// 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})
+		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)
@@ -840,7 +879,7 @@
 					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())
@@ -849,6 +888,9 @@
 						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")
@@ -871,7 +913,7 @@
 				fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
 				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+apexType.suffix())
+				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)")
diff --git a/apex/apex_test.go b/apex/apex_test.go
index 88c57bc..f8f9b33 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -15,12 +15,15 @@
 package apex
 
 import (
-	"android/soong/android"
-	"android/soong/cc"
 	"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 {
@@ -38,11 +41,15 @@
 
 	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()
 	})
@@ -53,38 +60,66 @@
 		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{
@@ -112,7 +147,7 @@
 	}
 
 	config = android.TestArchConfig(buildDir, nil)
-
+	config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
 	return
 }
 
@@ -184,14 +219,18 @@
 	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_shared_myapex")
+	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_shared_myapex")
+	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) {
@@ -232,10 +271,10 @@
 	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_shared_myapex")
+	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_shared_myapex")
+	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")
@@ -306,21 +345,24 @@
 	// Ensure that direct stubs dep is included
 	ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
 
-	mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
+	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_shared_3_myapex/mylib2.so")
+	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_shared_myapex/mylib2.so")
+	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_shared_myapex/mylib3.so")
+	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_shared_12_myapex/mylib3.so")
+	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_static_myapex").Rule("cc").Args["cFlags"]
+	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) {
@@ -377,14 +419,14 @@
 	// Ensure that dependency of stubs is not included
 	ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
 
-	mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
+	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_shared_10_myapex/libfoo.so")
+	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_shared_myapex/libfoo.so")
+	ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
 
-	libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10_myapex").Rule("ld").Args["libFlags"]
+	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")
@@ -463,36 +505,36 @@
 	// 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_shared_myapex").Rule("ld").Args["libFlags"]
-	mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
-	mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
+	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_shared_29_myapex/libc.so")
+	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_shared_myapex/libc.so")
+	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_shared_myapex/libm.so")
+	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_shared_29_myapex/libm.so")
+	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_shared_27_myapex/libdl.so")
+	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_shared_28_myapex/libdl.so")
-	ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29_myapex/libdl.so")
+	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_shared_myapex/libdl.so")
+	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")
@@ -527,3 +569,104 @@
 	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/bpf/bpf.go b/bpf/bpf.go
index fa1f3ff..3ef35b7 100644
--- a/bpf/bpf.go
+++ b/bpf/bpf.go
@@ -125,6 +125,14 @@
 	}
 }
 
+// Implements SourceFileProducer interface so that the obj output can be used in the data property
+// of other modules.
+func (bpf *bpf) Srcs() android.Paths {
+	return bpf.objs
+}
+
+var _ android.SourceFileProducer = (*bpf)(nil)
+
 func bpfFactory() android.Module {
 	module := &bpf{}
 
diff --git a/bpf/bpf_test.go b/bpf/bpf_test.go
new file mode 100644
index 0000000..1d53e41
--- /dev/null
+++ b/bpf/bpf_test.go
@@ -0,0 +1,192 @@
+// 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 bpf
+
+import (
+	"io/ioutil"
+	"os"
+	"testing"
+
+	"android/soong/android"
+	cc2 "android/soong/cc"
+)
+
+var buildDir string
+
+func setUp() {
+	var err error
+	buildDir, err = ioutil.TempDir("", "genrule_test")
+	if err != nil {
+		panic(err)
+	}
+}
+
+func tearDown() {
+	os.RemoveAll(buildDir)
+}
+
+func TestMain(m *testing.M) {
+	run := func() int {
+		setUp()
+		defer tearDown()
+
+		return m.Run()
+	}
+
+	os.Exit(run())
+}
+
+func testContext(bp string) *android.TestContext {
+	ctx := android.NewTestArchContext()
+	ctx.RegisterModuleType("bpf", android.ModuleFactoryAdaptor(bpfFactory))
+	ctx.RegisterModuleType("cc_test", android.ModuleFactoryAdaptor(cc2.TestFactory))
+	ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc2.LibraryFactory))
+	ctx.RegisterModuleType("cc_library_static", android.ModuleFactoryAdaptor(cc2.LibraryStaticFactory))
+	ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc2.ObjectFactory))
+	ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc2.ToolchainLibraryFactory))
+	ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
+		ctx.BottomUp("link", cc2.LinkageMutator).Parallel()
+	})
+	ctx.Register()
+
+	// Add some modules that are required by the compiler and/or linker
+	bp = bp + `
+		toolchain_library {
+			name: "libatomic",
+			vendor_available: true,
+			recovery_available: true,
+			src: "",
+		}
+
+		toolchain_library {
+			name: "libclang_rt.builtins-arm-android",
+			vendor_available: true,
+			recovery_available: true,
+			src: "",
+		}
+
+		toolchain_library {
+			name: "libclang_rt.builtins-aarch64-android",
+			vendor_available: true,
+			recovery_available: true,
+			src: "",
+		}
+
+		toolchain_library {
+			name: "libgcc",
+			vendor_available: true,
+			recovery_available: true,
+			src: "",
+		}
+
+		cc_library {
+			name: "libc",
+			no_libgcc: true,
+			nocrt: true,
+			system_shared_libs: [],
+			recovery_available: true,
+		}
+
+		cc_library {
+			name: "libm",
+			no_libgcc: true,
+			nocrt: true,
+			system_shared_libs: [],
+			recovery_available: true,
+		}
+
+		cc_library {
+			name: "libdl",
+			no_libgcc: true,
+			nocrt: true,
+			system_shared_libs: [],
+			recovery_available: true,
+		}
+
+		cc_library {
+			name: "libgtest",
+			host_supported: true,
+			vendor_available: true,
+		}
+
+		cc_library {
+			name: "libgtest_main",
+			host_supported: true,
+			vendor_available: true,
+		}
+
+		cc_object {
+			name: "crtbegin_dynamic",
+			recovery_available: true,
+			vendor_available: true,
+		}
+
+		cc_object {
+			name: "crtend_android",
+			recovery_available: true,
+			vendor_available: true,
+		}
+
+		cc_object {
+			name: "crtbegin_so",
+			recovery_available: true,
+			vendor_available: true,
+		}
+
+		cc_object {
+			name: "crtend_so",
+			recovery_available: true,
+			vendor_available: true,
+		}
+	`
+	mockFS := map[string][]byte{
+		"Android.bp":  []byte(bp),
+		"bpf.c":       nil,
+		"BpfTest.cpp": nil,
+	}
+
+	ctx.MockFileSystem(mockFS)
+
+	return ctx
+}
+
+func TestBpfDataDependency(t *testing.T) {
+	config := android.TestArchConfig(buildDir, nil)
+	bp := `
+		bpf {
+			name: "bpf.o",
+			srcs: ["bpf.c"],
+		}
+
+		cc_test {
+			name: "vts_test_binary_bpf_module",
+			srcs: ["BpfTest.cpp"],
+			data: [":bpf.o"],
+		}
+	`
+
+	ctx := testContext(bp)
+	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
+	if errs == nil {
+		_, errs = ctx.PrepareBuildActions(config)
+	}
+	if errs != nil {
+		t.Fatal(errs)
+	}
+
+	// We only verify the above BP configuration is processed successfully since the data property
+	// value is not available for testing from this package.
+	// TODO(jungjw): Add a check for data or move this test to the cc package.
+}
diff --git a/bpfix/bpfix/bpfix.go b/bpfix/bpfix/bpfix.go
index 24a38c9..a4723fb 100644
--- a/bpfix/bpfix/bpfix.go
+++ b/bpfix/bpfix/bpfix.go
@@ -205,6 +205,11 @@
 		if mod.Type != "java_import" {
 			continue
 		}
+		host, _ := getLiteralBoolPropertyValue(mod, "host")
+		if host {
+			mod.Type = "java_import_host"
+			removeProperty(mod, "host")
+		}
 		srcs, ok := getLiteralListProperty(mod, "srcs")
 		if !ok {
 			continue
@@ -705,6 +710,24 @@
 	return stringValue.Value, true
 }
 
+func getLiteralBoolProperty(mod *parser.Module, name string) (b *parser.Bool, found bool) {
+	prop, ok := mod.GetProperty(name)
+	if !ok {
+		return nil, false
+	}
+	b, ok = prop.Value.(*parser.Bool)
+	return b, ok
+}
+
+func getLiteralBoolPropertyValue(mod *parser.Module, name string) (s bool, found bool) {
+	boolValue, ok := getLiteralBoolProperty(mod, name)
+	if !ok {
+		return false, false
+	}
+
+	return boolValue.Value, true
+}
+
 func propertyIndex(props []*parser.Property, propertyName string) int {
 	for i, prop := range props {
 		if prop.Name == propertyName {
diff --git a/bpfix/bpfix/bpfix_test.go b/bpfix/bpfix/bpfix_test.go
index 16dfce0..0469faf 100644
--- a/bpfix/bpfix/bpfix_test.go
+++ b/bpfix/bpfix/bpfix_test.go
@@ -555,3 +555,69 @@
 		})
 	}
 }
+
+func TestRewritePrebuilts(t *testing.T) {
+	tests := []struct {
+		name string
+		in   string
+		out  string
+	}{
+		{
+			name: "jar srcs",
+			in: `
+				java_import {
+					name: "foo",
+					srcs: ["foo.jar"],
+				}
+			`,
+			out: `
+				java_import {
+					name: "foo",
+					jars: ["foo.jar"],
+				}
+			`,
+		},
+		{
+			name: "aar srcs",
+			in: `
+				java_import {
+					name: "foo",
+					srcs: ["foo.aar"],
+					installable: true,
+				}
+			`,
+			out: `
+				android_library_import {
+					name: "foo",
+					aars: ["foo.aar"],
+
+				}
+			`,
+		},
+		{
+			name: "host prebuilt",
+			in: `
+				java_import {
+					name: "foo",
+					srcs: ["foo.jar"],
+					host: true,
+				}
+			`,
+			out: `
+				java_import_host {
+					name: "foo",
+					jars: ["foo.jar"],
+
+				}
+			`,
+		},
+	}
+
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			runPass(t, test.in, test.out, func(fixer *Fixer) error {
+				return rewriteIncorrectAndroidmkPrebuilts(fixer)
+			})
+		})
+	}
+}
diff --git a/cc/androidmk.go b/cc/androidmk.go
index 1f5a8ad..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{
@@ -76,9 +76,6 @@
 				if len(c.Properties.AndroidMkWholeStaticLibs) > 0 {
 					fmt.Fprintln(w, "LOCAL_WHOLE_STATIC_LIBRARIES := "+strings.Join(c.Properties.AndroidMkWholeStaticLibs, " "))
 				}
-				if len(c.Properties.ApexesProvidingSharedLibs) > 0 {
-					fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES := "+strings.Join(c.Properties.ApexesProvidingSharedLibs, " "))
-				}
 				fmt.Fprintln(w, "LOCAL_SOONG_LINK_TYPE :=", c.getMakeLinkType())
 				if c.useVndk() {
 					fmt.Fprintln(w, "LOCAL_USE_VNDK := true")
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 02fc239..ce28a3b 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -34,9 +34,9 @@
 	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()
@@ -634,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
 	}
@@ -1419,7 +1423,12 @@
 		}
 
 		if t, ok := depTag.(dependencyTag); ok && t.library {
-			if dependentLibrary, ok := ccDep.linker.(*libraryDecorator); ok {
+			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)
@@ -1792,7 +1801,7 @@
 	}
 }
 
-func imageMutator(mctx android.BottomUpMutatorContext) {
+func ImageMutator(mctx android.BottomUpMutatorContext) {
 	if mctx.Os() != android.Android {
 		return
 	}
diff --git a/cc/cc_test.go b/cc/cc_test.go
index e368fb3..96233a1 100644
--- a/cc/cc_test.go
+++ b/cc/cc_test.go
@@ -53,19 +53,20 @@
 
 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("vndk", VndkMutator).Parallel()
 		ctx.BottomUp("version", VersionMutator).Parallel()
 		ctx.BottomUp("begin", BeginMutator).Parallel()
 	})
@@ -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",
 		}
@@ -1845,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/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 524b886..da223dc 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -395,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
 	}
@@ -807,6 +807,7 @@
 			"-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
 		}
 		library.reexportFlags(flags)
+		library.reexportDeps(library.baseCompiler.pathDeps)
 		library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
 	}
 
@@ -1058,7 +1059,7 @@
 			stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = copiedVersions
 
 			// "" is for the non-stubs variant
-			versions = append(versions, "")
+			versions = append([]string{""}, versions...)
 
 			modules := mctx.CreateVariations(versions...)
 			for i, m := range modules {
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_library.go b/cc/ndk_library.go
index 53fe314..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"
 
@@ -271,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")
@@ -288,7 +288,7 @@
 			"arch":     arch,
 			"apiLevel": apiLevel,
 			"apiMap":   apiLevelsJson.String(),
-			"vndk":     vndk,
+			"flags":    genstubFlags,
 		},
 	})
 
diff --git a/cc/sanitize.go b/cc/sanitize.go
index 5db6bdf..8d33de5 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -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
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/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/cmd/soong_ui/main.go b/cmd/soong_ui/main.go
index b603571..0380368 100644
--- a/cmd/soong_ui/main.go
+++ b/cmd/soong_ui/main.go
@@ -26,6 +26,7 @@
 
 	"android/soong/ui/build"
 	"android/soong/ui/logger"
+	"android/soong/ui/metrics"
 	"android/soong/ui/status"
 	"android/soong/ui/terminal"
 	"android/soong/ui/tracer"
@@ -73,6 +74,8 @@
 	trace := tracer.New(log)
 	defer trace.Close()
 
+	met := metrics.New()
+
 	stat := &status.Status{}
 	defer stat.Finish()
 	stat.AddOutput(terminal.NewStatusOutput(writer, os.Getenv("NINJA_STATUS")))
@@ -87,6 +90,7 @@
 	buildCtx := build.Context{ContextImpl: &build.ContextImpl{
 		Context: ctx,
 		Logger:  log,
+		Metrics: met,
 		Tracer:  trace,
 		Writer:  writer,
 		Status:  stat,
@@ -111,12 +115,14 @@
 	stat.AddOutput(status.NewVerboseLog(log, filepath.Join(logsDir, "verbose.log")))
 	stat.AddOutput(status.NewErrorLog(log, filepath.Join(logsDir, "error.log")))
 
+	defer met.Dump(filepath.Join(logsDir, "build_metrics"))
+
 	if start, ok := os.LookupEnv("TRACE_BEGIN_SOONG"); ok {
 		if !strings.HasSuffix(start, "N") {
 			if start_time, err := strconv.ParseUint(start, 10, 64); err == nil {
 				log.Verbosef("Took %dms to start up.",
 					time.Since(time.Unix(0, int64(start_time))).Nanoseconds()/time.Millisecond.Nanoseconds())
-				buildCtx.CompleteTrace("startup", start_time, uint64(time.Now().UnixNano()))
+				buildCtx.CompleteTrace(metrics.RunSetupTool, "startup", start_time, uint64(time.Now().UnixNano()))
 			}
 		}
 
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..3b21106
--- /dev/null
+++ b/dexpreopt/dexpreopt.go
@@ -0,0 +1,562 @@
+// 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")
+
+	invocationPath := pathtools.ReplaceExtension(odexPath, "invocation")
+
+	// 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").
+		FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
+		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 b70c075..77bc196 100644
--- a/genrule/genrule.go
+++ b/genrule/genrule.go
@@ -28,6 +28,8 @@
 )
 
 func init() {
+	android.RegisterModuleType("genrule_defaults", defaultsFactory)
+
 	android.RegisterModuleType("gensrcs", GenSrcsFactory)
 	android.RegisterModuleType("genrule", GenRuleFactory)
 }
@@ -95,6 +97,7 @@
 
 type Module struct {
 	android.ModuleBase
+	android.DefaultableModuleBase
 
 	// For other packages to make their own genrules with extra
 	// properties
@@ -502,6 +505,7 @@
 func GenRuleFactory() android.Module {
 	m := NewGenRule()
 	android.InitAndroidModule(m)
+	android.InitDefaultableModule(m)
 	return m
 }
 
@@ -512,3 +516,35 @@
 
 var Bool = proptools.Bool
 var String = proptools.String
+
+//
+// Defaults
+//
+type Defaults struct {
+	android.ModuleBase
+	android.DefaultsModuleBase
+}
+
+func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+}
+
+func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
+}
+
+func defaultsFactory() android.Module {
+	return DefaultsFactory()
+}
+
+func DefaultsFactory(props ...interface{}) android.Module {
+	module := &Defaults{}
+
+	module.AddProperties(props...)
+	module.AddProperties(
+		&generatorProperties{},
+		&genRuleProperties{},
+	)
+
+	android.InitDefaultsModule(module)
+
+	return module
+}
diff --git a/genrule/genrule_test.go b/genrule/genrule_test.go
index a99fa18..70b9090 100644
--- a/genrule/genrule_test.go
+++ b/genrule/genrule_test.go
@@ -21,6 +21,7 @@
 	"testing"
 
 	"android/soong/android"
+	"reflect"
 )
 
 var buildDir string
@@ -54,7 +55,9 @@
 	ctx := android.NewTestArchContext()
 	ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
 	ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(GenRuleFactory))
+	ctx.RegisterModuleType("genrule_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
 	ctx.RegisterModuleType("tool", android.ModuleFactoryAdaptor(toolFactory))
+	ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
 	ctx.Register()
 
 	bp += `
@@ -465,6 +468,46 @@
 
 }
 
+func TestGenruleDefaults(t *testing.T) {
+	config := android.TestArchConfig(buildDir, nil)
+	bp := `
+				genrule_defaults {
+					name: "gen_defaults1",
+					cmd: "cp $(in) $(out)",
+				}
+
+				genrule_defaults {
+					name: "gen_defaults2",
+					srcs: ["in1"],
+				}
+
+				genrule {
+					name: "gen",
+					out: ["out"],
+					defaults: ["gen_defaults1", "gen_defaults2"],
+				}
+			`
+	ctx := testContext(config, bp, nil)
+	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
+	if errs == nil {
+		_, errs = ctx.PrepareBuildActions(config)
+	}
+	if errs != nil {
+		t.Fatal(errs)
+	}
+	gen := ctx.ModuleForTests("gen", "").Module().(*Module)
+
+	expectedCmd := "'cp ${in} __SBOX_OUT_FILES__'"
+	if gen.rawCommand != expectedCmd {
+		t.Errorf("Expected cmd: %q, actual: %q", expectedCmd, gen.rawCommand)
+	}
+
+	expectedSrcs := []string{"in1"}
+	if !reflect.DeepEqual(expectedSrcs, gen.properties.Srcs) {
+		t.Errorf("Expected srcs: %q, actual: %q", expectedSrcs, gen.properties.Srcs)
+	}
+}
+
 type testTool struct {
 	android.ModuleBase
 	outputFile android.Path
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/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 8e0a62a..bd3b3ab 100644
--- a/java/droiddoc.go
+++ b/java/droiddoc.go
@@ -400,6 +400,7 @@
 
 	metalavaStubsFlags                string
 	metalavaAnnotationsFlags          string
+	metalavaMergeAnnoDirFlags         string
 	metalavaInclusionAnnotationsFlags string
 	metalavaApiLevelsAnnotationsFlags string
 
@@ -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,
@@ -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..c02ccd3 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)
 	}
 
@@ -1024,8 +1011,15 @@
 	}
 
 	if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
-		patchClasspath := ".:" + flags.classpath.FormJavaClassPath("")
-		javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchClasspath)
+		// Manually specify build directory in case it is not under the repo root.
+		// (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
+		// just adding a symlink under the root doesn't help.)
+		patchPaths := ".:" + ctx.Config().BuildDir()
+		classPath := flags.classpath.FormJavaClassPath("")
+		if classPath != "" {
+			patchPaths += ":" + classPath
+		}
+		javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
 	}
 
 	// systemModules
@@ -1332,7 +1326,15 @@
 
 		j.dexJarFile = dexOutputFile
 
+		dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
+
+		j.maybeStrippedDexJarFile = dexOutputFile
+
 		outputFile = dexOutputFile
+
+		if ctx.Failed() {
+			return
+		}
 	} else {
 		outputFile = implementationAndResourcesJar
 	}
@@ -1425,13 +1427,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 +1451,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 +1499,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 +1525,7 @@
 	module.AddProperties(
 		&module.Module.properties,
 		&module.Module.deviceProperties,
+		&module.Module.dexpreoptProperties,
 		&module.Module.protoProperties)
 
 	InitJavaModule(module, android.HostAndDeviceSupported)
@@ -1574,6 +1596,7 @@
 	module.AddProperties(
 		&module.Module.properties,
 		&module.Module.deviceProperties,
+		&module.Module.dexpreoptProperties,
 		&module.Module.protoProperties,
 		&module.testProperties)
 
@@ -1670,6 +1693,7 @@
 	module.AddProperties(
 		&module.Module.properties,
 		&module.Module.deviceProperties,
+		&module.Module.dexpreoptProperties,
 		&module.Module.protoProperties,
 		&module.binaryProperties)
 
@@ -1799,10 +1823,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 +1841,9 @@
 }
 
 func (j *Import) ImplementationAndResourcesJars() android.Paths {
+	if j.combinedClasspathFile == nil {
+		return nil
+	}
 	return android.Paths{j.combinedClasspathFile}
 }
 
@@ -1822,6 +1855,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 +1926,7 @@
 	module.AddProperties(
 		&CompilerProperties{},
 		&CompilerDeviceProperties{},
+		&DexpreoptProperties{},
 		&android.ProtoProperties{},
 		&aaptProperties{},
 		&androidLibraryProperties{},
diff --git a/java/java_test.go b/java/java_test.go
index 4d4b836..2a54f69 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -1185,3 +1185,65 @@
 		}
 	}
 }
+
+// TODO(jungjw): Consider making this more robust by ignoring path order.
+func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
+	variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
+	flags := strings.Split(variables["javacFlags"], " ")
+	got := ""
+	for _, flag := range flags {
+		keyEnd := strings.Index(flag, "=")
+		if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
+			got = flag[keyEnd+1:]
+			break
+		}
+	}
+	if expected != got {
+		t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
+	}
+}
+
+func TestPatchModule(t *testing.T) {
+	bp := `
+		java_library {
+			name: "foo",
+			srcs: ["a.java"],
+		}
+
+		java_library {
+			name: "bar",
+			srcs: ["b.java"],
+			no_standard_libs: true,
+			system_modules: "none",
+			patch_module: "java.base",
+		}
+
+		java_library {
+			name: "baz",
+			srcs: ["c.java"],
+			patch_module: "java.base",
+		}
+	`
+
+	t.Run("1.8", func(t *testing.T) {
+		// Test default javac 1.8
+		ctx := testJava(t, bp)
+
+		checkPatchModuleFlag(t, ctx, "foo", "")
+		checkPatchModuleFlag(t, ctx, "bar", "")
+		checkPatchModuleFlag(t, ctx, "baz", "")
+	})
+
+	t.Run("1.9", func(t *testing.T) {
+		// Test again with javac 1.9
+		config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
+		ctx := testContext(config, bp, nil)
+		run(t, ctx, config)
+
+		checkPatchModuleFlag(t, ctx, "foo", "")
+		expected := "java.base=.:" + buildDir
+		checkPatchModuleFlag(t, ctx, "bar", expected)
+		expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
+		checkPatchModuleFlag(t, ctx, "baz", expected)
+	})
+}
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 fdbf19d..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
@@ -564,6 +565,7 @@
 		Errorprone       struct {
 			Javacflags []string
 		}
+		IsSDKLibrary bool
 	}{}
 
 	props.Name = proptools.StringPtr(module.implName())
@@ -574,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)
@@ -583,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
@@ -716,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/scripts/stub_template_host.txt b/python/scripts/stub_template_host.txt
index 213401d..a48a86f 100644
--- a/python/scripts/stub_template_host.txt
+++ b/python/scripts/stub_template_host.txt
@@ -12,6 +12,9 @@
 MAIN_FILE = '%main%'
 PYTHON_PATH = 'PYTHONPATH'
 
+# Don't imply 'import site' on initialization
+PYTHON_ARG = '-S'
+
 def SearchPathEnv(name):
   search_path = os.getenv('PATH', os.defpath).split(os.pathsep)
   for directory in search_path:
@@ -73,7 +76,7 @@
     python_program = FindPythonBinary()
     if python_program is None:
       raise AssertionError('Could not find python binary: ' + PYTHON_BINARY)
-    args = [python_program, main_filepath] + args
+    args = [python_program, PYTHON_ARG, main_filepath] + args
 
     os.environ.update(new_env)
 
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/soong_ui.bash b/soong_ui.bash
index a39aa9c..c1c236b 100755
--- a/soong_ui.bash
+++ b/soong_ui.bash
@@ -23,7 +23,7 @@
 function gettop
 {
     local TOPFILE=build/soong/root.bp
-    if [ -z "${TOP-}" -a -f "${TOP-}/${TOPFILE}" ] ; then
+    if [ -n "${TOP-}" -a -f "${TOP-}/${TOPFILE}" ] ; then
         # The following circumlocution ensures we remove symlinks from TOP.
         (cd $TOP; PWD= /bin/pwd)
     else
diff --git a/ui/build/Android.bp b/ui/build/Android.bp
index a48a314..0eba5ce 100644
--- a/ui/build/Android.bp
+++ b/ui/build/Android.bp
@@ -30,6 +30,7 @@
     deps: [
         "soong-ui-build-paths",
         "soong-ui-logger",
+        "soong-ui-metrics",
         "soong-ui-status",
         "soong-ui-terminal",
         "soong-ui-tracer",
diff --git a/ui/build/cleanbuild.go b/ui/build/cleanbuild.go
index 4dee638..c47f614 100644
--- a/ui/build/cleanbuild.go
+++ b/ui/build/cleanbuild.go
@@ -20,6 +20,8 @@
 	"os"
 	"path/filepath"
 	"strings"
+
+	"android/soong/ui/metrics"
 )
 
 func removeGlobs(ctx Context, globs ...string) {
@@ -158,7 +160,7 @@
 		return
 	}
 
-	ctx.BeginTrace("installclean")
+	ctx.BeginTrace(metrics.PrimaryNinja, "installclean")
 	defer ctx.EndTrace()
 
 	prevConfig := strings.TrimPrefix(strings.TrimSuffix(string(prev), suffix), prefix)
diff --git a/ui/build/config.go b/ui/build/config.go
index 5a6d5db..97b009a 100644
--- a/ui/build/config.go
+++ b/ui/build/config.go
@@ -217,6 +217,9 @@
 	} else {
 		content = strconv.FormatInt(time.Now().Unix(), 10)
 	}
+	if ctx.Metrics != nil {
+		ctx.Metrics.SetBuildDateTime(content)
+	}
 	err := ioutil.WriteFile(buildDateTimeFile, []byte(content), 0777)
 	if err != nil {
 		ctx.Fatalln("Failed to write BUILD_DATETIME to file:", err)
diff --git a/ui/build/context.go b/ui/build/context.go
index c8b00c3..249e898 100644
--- a/ui/build/context.go
+++ b/ui/build/context.go
@@ -18,6 +18,8 @@
 	"context"
 
 	"android/soong/ui/logger"
+	"android/soong/ui/metrics"
+	"android/soong/ui/metrics/metrics_proto"
 	"android/soong/ui/status"
 	"android/soong/ui/terminal"
 	"android/soong/ui/tracer"
@@ -31,6 +33,8 @@
 	context.Context
 	logger.Logger
 
+	Metrics *metrics.Metrics
+
 	Writer terminal.Writer
 	Status *status.Status
 
@@ -39,9 +43,12 @@
 }
 
 // BeginTrace starts a new Duration Event.
-func (c ContextImpl) BeginTrace(name string) {
+func (c ContextImpl) BeginTrace(name, desc string) {
 	if c.Tracer != nil {
-		c.Tracer.Begin(name, c.Thread)
+		c.Tracer.Begin(desc, c.Thread)
+	}
+	if c.Metrics != nil {
+		c.Metrics.TimeTracer.Begin(name, desc, c.Thread)
 	}
 }
 
@@ -50,11 +57,23 @@
 	if c.Tracer != nil {
 		c.Tracer.End(c.Thread)
 	}
+	if c.Metrics != nil {
+		c.Metrics.SetTimeMetrics(c.Metrics.TimeTracer.End(c.Thread))
+	}
 }
 
 // CompleteTrace writes a trace with a beginning and end times.
-func (c ContextImpl) CompleteTrace(name string, begin, end uint64) {
+func (c ContextImpl) CompleteTrace(name, desc string, begin, end uint64) {
 	if c.Tracer != nil {
-		c.Tracer.Complete(name, c.Thread, begin, end)
+		c.Tracer.Complete(desc, c.Thread, begin, end)
+	}
+	if c.Metrics != nil {
+		realTime := end - begin
+		c.Metrics.SetTimeMetrics(
+			metrics_proto.PerfInfo{
+				Desc:      &desc,
+				Name:      &name,
+				StartTime: &begin,
+				RealTime:  &realTime})
 	}
 }
diff --git a/ui/build/dumpvars.go b/ui/build/dumpvars.go
index ad57d02..1ab855d 100644
--- a/ui/build/dumpvars.go
+++ b/ui/build/dumpvars.go
@@ -19,6 +19,7 @@
 	"fmt"
 	"strings"
 
+	"android/soong/ui/metrics"
 	"android/soong/ui/status"
 )
 
@@ -69,7 +70,7 @@
 }
 
 func dumpMakeVars(ctx Context, config Config, goals, vars []string, write_soong_vars bool) (map[string]string, error) {
-	ctx.BeginTrace("dumpvars")
+	ctx.BeginTrace(metrics.RunKati, "dumpvars")
 	defer ctx.EndTrace()
 
 	cmd := Command(ctx, config, "dumpvars",
@@ -113,6 +114,9 @@
 			return nil, fmt.Errorf("Failed to parse make line: %q", line)
 		}
 	}
+	if ctx.Metrics != nil {
+		ctx.Metrics.SetMetadataMetrics(ret)
+	}
 
 	return ret, nil
 }
diff --git a/ui/build/finder.go b/ui/build/finder.go
index 3130f74..0f34b5e 100644
--- a/ui/build/finder.go
+++ b/ui/build/finder.go
@@ -23,6 +23,8 @@
 	"os"
 	"path/filepath"
 	"strings"
+
+	"android/soong/ui/metrics"
 )
 
 // This file provides an interface to the Finder for use in Soong UI
@@ -31,7 +33,7 @@
 // NewSourceFinder returns a new Finder configured to search for source files.
 // Callers of NewSourceFinder should call <f.Shutdown()> when done
 func NewSourceFinder(ctx Context, config Config) (f *finder.Finder) {
-	ctx.BeginTrace("find modules")
+	ctx.BeginTrace(metrics.RunSetupTool, "find modules")
 	defer ctx.EndTrace()
 
 	dir, err := os.Getwd()
diff --git a/ui/build/kati.go b/ui/build/kati.go
index d2e9fe9..205d5ae 100644
--- a/ui/build/kati.go
+++ b/ui/build/kati.go
@@ -21,6 +21,7 @@
 	"path/filepath"
 	"strings"
 
+	"android/soong/ui/metrics"
 	"android/soong/ui/status"
 )
 
@@ -101,7 +102,7 @@
 }
 
 func runKatiBuild(ctx Context, config Config) {
-	ctx.BeginTrace("kati build")
+	ctx.BeginTrace(metrics.RunKati, "kati build")
 	defer ctx.EndTrace()
 
 	args := []string{
@@ -137,7 +138,7 @@
 }
 
 func runKatiPackage(ctx Context, config Config) {
-	ctx.BeginTrace("kati package")
+	ctx.BeginTrace(metrics.RunKati, "kati package")
 	defer ctx.EndTrace()
 
 	args := []string{
@@ -178,7 +179,7 @@
 }
 
 func runKatiCleanSpec(ctx Context, config Config) {
-	ctx.BeginTrace("kati cleanspec")
+	ctx.BeginTrace(metrics.RunKati, "kati cleanspec")
 	defer ctx.EndTrace()
 
 	runKati(ctx, config, katiCleanspecSuffix, []string{
diff --git a/ui/build/ninja.go b/ui/build/ninja.go
index c8f19d1..e5d6da1 100644
--- a/ui/build/ninja.go
+++ b/ui/build/ninja.go
@@ -22,11 +22,12 @@
 	"strings"
 	"time"
 
+	"android/soong/ui/metrics"
 	"android/soong/ui/status"
 )
 
 func runNinja(ctx Context, config Config) {
-	ctx.BeginTrace("ninja")
+	ctx.BeginTrace(metrics.PrimaryNinja, "ninja")
 	defer ctx.EndTrace()
 
 	fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
@@ -97,6 +98,7 @@
 		}
 	}()
 
+	ctx.Status.Status("Starting ninja...")
 	cmd.RunAndPrintOrFatal()
 }
 
diff --git a/ui/build/path.go b/ui/build/path.go
index 8260ff9..ee72cfd 100644
--- a/ui/build/path.go
+++ b/ui/build/path.go
@@ -25,6 +25,7 @@
 	"github.com/google/blueprint/microfactory"
 
 	"android/soong/ui/build/paths"
+	"android/soong/ui/metrics"
 )
 
 func parsePathDir(dir string) []string {
@@ -57,7 +58,7 @@
 		return
 	}
 
-	ctx.BeginTrace("path")
+	ctx.BeginTrace(metrics.RunSetupTool, "path")
 	defer ctx.EndTrace()
 
 	origPath, _ := config.Environment().Get("PATH")
diff --git a/ui/build/soong.go b/ui/build/soong.go
index 478c02c..c89f0d5 100644
--- a/ui/build/soong.go
+++ b/ui/build/soong.go
@@ -22,15 +22,16 @@
 
 	"github.com/google/blueprint/microfactory"
 
+	"android/soong/ui/metrics"
 	"android/soong/ui/status"
 )
 
 func runSoong(ctx Context, config Config) {
-	ctx.BeginTrace("soong")
+	ctx.BeginTrace(metrics.RunSoong, "soong")
 	defer ctx.EndTrace()
 
 	func() {
-		ctx.BeginTrace("blueprint bootstrap")
+		ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap")
 		defer ctx.EndTrace()
 
 		cmd := Command(ctx, config, "blueprint bootstrap", "build/blueprint/bootstrap.bash", "-t")
@@ -48,7 +49,7 @@
 	}()
 
 	func() {
-		ctx.BeginTrace("environment check")
+		ctx.BeginTrace(metrics.RunSoong, "environment check")
 		defer ctx.EndTrace()
 
 		envFile := filepath.Join(config.SoongOutDir(), ".soong.environment")
@@ -84,7 +85,7 @@
 	cfg.TrimPath = absPath(ctx, ".")
 
 	func() {
-		ctx.BeginTrace("minibp")
+		ctx.BeginTrace(metrics.RunSoong, "minibp")
 		defer ctx.EndTrace()
 
 		minibp := filepath.Join(config.SoongOutDir(), ".minibootstrap/minibp")
@@ -94,7 +95,7 @@
 	}()
 
 	func() {
-		ctx.BeginTrace("bpglob")
+		ctx.BeginTrace(metrics.RunSoong, "bpglob")
 		defer ctx.EndTrace()
 
 		bpglob := filepath.Join(config.SoongOutDir(), ".minibootstrap/bpglob")
@@ -104,7 +105,7 @@
 	}()
 
 	ninja := func(name, file string) {
-		ctx.BeginTrace(name)
+		ctx.BeginTrace(metrics.RunSoong, name)
 		defer ctx.EndTrace()
 
 		fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
diff --git a/ui/build/test_build.go b/ui/build/test_build.go
index 348d41f..5109465 100644
--- a/ui/build/test_build.go
+++ b/ui/build/test_build.go
@@ -22,6 +22,7 @@
 	"sort"
 	"strings"
 
+	"android/soong/ui/metrics"
 	"android/soong/ui/status"
 )
 
@@ -37,7 +38,7 @@
 		return
 	}
 
-	ctx.BeginTrace("test for dangling rules")
+	ctx.BeginTrace(metrics.TestRun, "test for dangling rules")
 	defer ctx.EndTrace()
 
 	ts := ctx.Status.StartTool()
diff --git a/ui/metrics/Android.bp b/ui/metrics/Android.bp
new file mode 100644
index 0000000..529639d
--- /dev/null
+++ b/ui/metrics/Android.bp
@@ -0,0 +1,37 @@
+// 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.
+
+bootstrap_go_package {
+    name: "soong-ui-metrics",
+    pkgPath: "android/soong/ui/metrics",
+    deps: [
+        "golang-protobuf-proto",
+        "soong-ui-metrics_proto",
+        "soong-ui-tracer",
+    ],
+    srcs: [
+        "metrics.go",
+        "time.go",
+    ],
+}
+
+bootstrap_go_package {
+    name: "soong-ui-metrics_proto",
+    pkgPath: "android/soong/ui/metrics/metrics_proto",
+    deps: ["golang-protobuf-proto"],
+    srcs: [
+        "metrics_proto/metrics.pb.go",
+    ],
+}
+
diff --git a/ui/metrics/metrics.go b/ui/metrics/metrics.go
new file mode 100644
index 0000000..790b67a
--- /dev/null
+++ b/ui/metrics/metrics.go
@@ -0,0 +1,161 @@
+// 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 metrics
+
+import (
+	"io/ioutil"
+	"os"
+	"strconv"
+
+	"android/soong/ui/metrics/metrics_proto"
+
+	"github.com/golang/protobuf/proto"
+)
+
+const (
+	RunSetupTool = "setup"
+	RunKati      = "kati"
+	RunSoong     = "soong"
+	PrimaryNinja = "ninja"
+	TestRun      = "test"
+)
+
+type Metrics struct {
+	metrics    metrics_proto.MetricsBase
+	TimeTracer TimeTracer
+}
+
+func New() (metrics *Metrics) {
+	m := &Metrics{
+		metrics:    metrics_proto.MetricsBase{},
+		TimeTracer: &timeTracerImpl{},
+	}
+	return m
+}
+
+func (m *Metrics) SetTimeMetrics(perf metrics_proto.PerfInfo) {
+	switch perf.GetName() {
+	case RunKati:
+		m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf)
+		break
+	case RunSoong:
+		m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf)
+		break
+	case PrimaryNinja:
+		m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf)
+		break
+	default:
+		// ignored
+	}
+}
+
+func (m *Metrics) SetMetadataMetrics(metadata map[string]string) {
+	for k, v := range metadata {
+		switch k {
+		case "BUILD_ID":
+			m.metrics.BuildId = proto.String(v)
+			break
+		case "PLATFORM_VERSION_CODENAME":
+			m.metrics.PlatformVersionCodename = proto.String(v)
+			break
+		case "TARGET_PRODUCT":
+			m.metrics.TargetProduct = proto.String(v)
+			break
+		case "TARGET_BUILD_VARIANT":
+			switch v {
+			case "user":
+				m.metrics.TargetBuildVariant = metrics_proto.MetricsBase_USER.Enum()
+			case "userdebug":
+				m.metrics.TargetBuildVariant = metrics_proto.MetricsBase_USERDEBUG.Enum()
+			case "eng":
+				m.metrics.TargetBuildVariant = metrics_proto.MetricsBase_ENG.Enum()
+			default:
+				// ignored
+			}
+		case "TARGET_ARCH":
+			m.metrics.TargetArch = m.getArch(v)
+		case "TARGET_ARCH_VARIANT":
+			m.metrics.TargetArchVariant = proto.String(v)
+		case "TARGET_CPU_VARIANT":
+			m.metrics.TargetCpuVariant = proto.String(v)
+		case "HOST_ARCH":
+			m.metrics.HostArch = m.getArch(v)
+		case "HOST_2ND_ARCH":
+			m.metrics.Host_2NdArch = m.getArch(v)
+		case "HOST_OS":
+			m.metrics.HostOs = proto.String(v)
+		case "HOST_OS_EXTRA":
+			m.metrics.HostOsExtra = proto.String(v)
+		case "HOST_CROSS_OS":
+			m.metrics.HostCrossOs = proto.String(v)
+		case "HOST_CROSS_ARCH":
+			m.metrics.HostCrossArch = proto.String(v)
+		case "HOST_CROSS_2ND_ARCH":
+			m.metrics.HostCross_2NdArch = proto.String(v)
+		case "OUT_DIR":
+			m.metrics.OutDir = proto.String(v)
+		default:
+			// ignored
+		}
+	}
+}
+
+func (m *Metrics) getArch(arch string) *metrics_proto.MetricsBase_ARCH {
+	switch arch {
+	case "arm":
+		return metrics_proto.MetricsBase_ARM.Enum()
+	case "arm64":
+		return metrics_proto.MetricsBase_ARM64.Enum()
+	case "x86":
+		return metrics_proto.MetricsBase_X86.Enum()
+	case "x86_64":
+		return metrics_proto.MetricsBase_X86_64.Enum()
+	default:
+		return metrics_proto.MetricsBase_UNKNOWN.Enum()
+	}
+}
+
+func (m *Metrics) SetBuildDateTime(date_time string) {
+	if date_time != "" {
+		date_time_timestamp, err := strconv.ParseInt(date_time, 10, 64)
+		if err != nil {
+			panic(err)
+		}
+		m.metrics.BuildDateTimestamp = &date_time_timestamp
+	}
+}
+
+func (m *Metrics) Serialize() (data []byte, err error) {
+	return proto.Marshal(&m.metrics)
+}
+
+// exports the output to the file at outputPath
+func (m *Metrics) Dump(outputPath string) (err error) {
+	data, err := m.Serialize()
+	if err != nil {
+		return err
+	}
+	tempPath := outputPath + ".tmp"
+	err = ioutil.WriteFile(tempPath, []byte(data), 0777)
+	if err != nil {
+		return err
+	}
+	err = os.Rename(tempPath, outputPath)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
diff --git a/ui/metrics/metrics_proto/metrics.pb.go b/ui/metrics/metrics_proto/metrics.pb.go
new file mode 100644
index 0000000..feefc89
--- /dev/null
+++ b/ui/metrics/metrics_proto/metrics.pb.go
@@ -0,0 +1,557 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: metrics.proto
+
+package metrics_proto
+
+import proto "github.com/golang/protobuf/proto"
+import fmt "fmt"
+import math "math"
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ = proto.Marshal
+var _ = fmt.Errorf
+var _ = math.Inf
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the proto package it is being compiled against.
+// A compilation error at this line likely means your copy of the
+// proto package needs to be updated.
+const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
+
+type MetricsBase_BUILDVARIANT int32
+
+const (
+	MetricsBase_USER      MetricsBase_BUILDVARIANT = 0
+	MetricsBase_USERDEBUG MetricsBase_BUILDVARIANT = 1
+	MetricsBase_ENG       MetricsBase_BUILDVARIANT = 2
+)
+
+var MetricsBase_BUILDVARIANT_name = map[int32]string{
+	0: "USER",
+	1: "USERDEBUG",
+	2: "ENG",
+}
+var MetricsBase_BUILDVARIANT_value = map[string]int32{
+	"USER":      0,
+	"USERDEBUG": 1,
+	"ENG":       2,
+}
+
+func (x MetricsBase_BUILDVARIANT) Enum() *MetricsBase_BUILDVARIANT {
+	p := new(MetricsBase_BUILDVARIANT)
+	*p = x
+	return p
+}
+func (x MetricsBase_BUILDVARIANT) String() string {
+	return proto.EnumName(MetricsBase_BUILDVARIANT_name, int32(x))
+}
+func (x *MetricsBase_BUILDVARIANT) UnmarshalJSON(data []byte) error {
+	value, err := proto.UnmarshalJSONEnum(MetricsBase_BUILDVARIANT_value, data, "MetricsBase_BUILDVARIANT")
+	if err != nil {
+		return err
+	}
+	*x = MetricsBase_BUILDVARIANT(value)
+	return nil
+}
+func (MetricsBase_BUILDVARIANT) EnumDescriptor() ([]byte, []int) {
+	return fileDescriptor_metrics_9e7b895801991242, []int{0, 0}
+}
+
+type MetricsBase_ARCH int32
+
+const (
+	MetricsBase_UNKNOWN MetricsBase_ARCH = 0
+	MetricsBase_ARM     MetricsBase_ARCH = 1
+	MetricsBase_ARM64   MetricsBase_ARCH = 2
+	MetricsBase_X86     MetricsBase_ARCH = 3
+	MetricsBase_X86_64  MetricsBase_ARCH = 4
+)
+
+var MetricsBase_ARCH_name = map[int32]string{
+	0: "UNKNOWN",
+	1: "ARM",
+	2: "ARM64",
+	3: "X86",
+	4: "X86_64",
+}
+var MetricsBase_ARCH_value = map[string]int32{
+	"UNKNOWN": 0,
+	"ARM":     1,
+	"ARM64":   2,
+	"X86":     3,
+	"X86_64":  4,
+}
+
+func (x MetricsBase_ARCH) Enum() *MetricsBase_ARCH {
+	p := new(MetricsBase_ARCH)
+	*p = x
+	return p
+}
+func (x MetricsBase_ARCH) String() string {
+	return proto.EnumName(MetricsBase_ARCH_name, int32(x))
+}
+func (x *MetricsBase_ARCH) UnmarshalJSON(data []byte) error {
+	value, err := proto.UnmarshalJSONEnum(MetricsBase_ARCH_value, data, "MetricsBase_ARCH")
+	if err != nil {
+		return err
+	}
+	*x = MetricsBase_ARCH(value)
+	return nil
+}
+func (MetricsBase_ARCH) EnumDescriptor() ([]byte, []int) {
+	return fileDescriptor_metrics_9e7b895801991242, []int{0, 1}
+}
+
+type ModuleTypeInfo_BUILDSYSTEM int32
+
+const (
+	ModuleTypeInfo_UNKNOWN ModuleTypeInfo_BUILDSYSTEM = 0
+	ModuleTypeInfo_SOONG   ModuleTypeInfo_BUILDSYSTEM = 1
+	ModuleTypeInfo_MAKE    ModuleTypeInfo_BUILDSYSTEM = 2
+)
+
+var ModuleTypeInfo_BUILDSYSTEM_name = map[int32]string{
+	0: "UNKNOWN",
+	1: "SOONG",
+	2: "MAKE",
+}
+var ModuleTypeInfo_BUILDSYSTEM_value = map[string]int32{
+	"UNKNOWN": 0,
+	"SOONG":   1,
+	"MAKE":    2,
+}
+
+func (x ModuleTypeInfo_BUILDSYSTEM) Enum() *ModuleTypeInfo_BUILDSYSTEM {
+	p := new(ModuleTypeInfo_BUILDSYSTEM)
+	*p = x
+	return p
+}
+func (x ModuleTypeInfo_BUILDSYSTEM) String() string {
+	return proto.EnumName(ModuleTypeInfo_BUILDSYSTEM_name, int32(x))
+}
+func (x *ModuleTypeInfo_BUILDSYSTEM) UnmarshalJSON(data []byte) error {
+	value, err := proto.UnmarshalJSONEnum(ModuleTypeInfo_BUILDSYSTEM_value, data, "ModuleTypeInfo_BUILDSYSTEM")
+	if err != nil {
+		return err
+	}
+	*x = ModuleTypeInfo_BUILDSYSTEM(value)
+	return nil
+}
+func (ModuleTypeInfo_BUILDSYSTEM) EnumDescriptor() ([]byte, []int) {
+	return fileDescriptor_metrics_9e7b895801991242, []int{2, 0}
+}
+
+type MetricsBase struct {
+	// Timestamp generated when the build starts.
+	BuildDateTimestamp *int64 `protobuf:"varint,1,opt,name=build_date_timestamp,json=buildDateTimestamp" json:"build_date_timestamp,omitempty"`
+	// It is usually used to specify the branch name [and release candidate].
+	BuildId *string `protobuf:"bytes,2,opt,name=build_id,json=buildId" json:"build_id,omitempty"`
+	// The platform version codename, eg. P, Q, REL.
+	PlatformVersionCodename *string `protobuf:"bytes,3,opt,name=platform_version_codename,json=platformVersionCodename" json:"platform_version_codename,omitempty"`
+	// The target product information, eg. aosp_arm.
+	TargetProduct *string `protobuf:"bytes,4,opt,name=target_product,json=targetProduct" json:"target_product,omitempty"`
+	// The target build variant information, eg. eng.
+	TargetBuildVariant *MetricsBase_BUILDVARIANT `protobuf:"varint,5,opt,name=target_build_variant,json=targetBuildVariant,enum=build_metrics.MetricsBase_BUILDVARIANT,def=2" json:"target_build_variant,omitempty"`
+	// The target arch information, eg. arm.
+	TargetArch *MetricsBase_ARCH `protobuf:"varint,6,opt,name=target_arch,json=targetArch,enum=build_metrics.MetricsBase_ARCH,def=0" json:"target_arch,omitempty"`
+	// The target arch variant information, eg. armv7-a-neon.
+	TargetArchVariant *string `protobuf:"bytes,7,opt,name=target_arch_variant,json=targetArchVariant" json:"target_arch_variant,omitempty"`
+	// The target cpu variant information, eg. generic.
+	TargetCpuVariant *string `protobuf:"bytes,8,opt,name=target_cpu_variant,json=targetCpuVariant" json:"target_cpu_variant,omitempty"`
+	// The host arch information, eg. x86_64.
+	HostArch *MetricsBase_ARCH `protobuf:"varint,9,opt,name=host_arch,json=hostArch,enum=build_metrics.MetricsBase_ARCH,def=0" json:"host_arch,omitempty"`
+	// The host 2nd arch information, eg. x86.
+	Host_2NdArch *MetricsBase_ARCH `protobuf:"varint,10,opt,name=host_2nd_arch,json=host2ndArch,enum=build_metrics.MetricsBase_ARCH,def=0" json:"host_2nd_arch,omitempty"`
+	// The host os information, eg. linux.
+	HostOs *string `protobuf:"bytes,11,opt,name=host_os,json=hostOs" json:"host_os,omitempty"`
+	// The host os extra information, eg. Linux-4.17.0-3rodete2-amd64-x86_64-Debian-GNU.
+	HostOsExtra *string `protobuf:"bytes,12,opt,name=host_os_extra,json=hostOsExtra" json:"host_os_extra,omitempty"`
+	// The host cross os information, eg. windows.
+	HostCrossOs *string `protobuf:"bytes,13,opt,name=host_cross_os,json=hostCrossOs" json:"host_cross_os,omitempty"`
+	// The host cross arch information, eg. x86.
+	HostCrossArch *string `protobuf:"bytes,14,opt,name=host_cross_arch,json=hostCrossArch" json:"host_cross_arch,omitempty"`
+	// The host cross 2nd arch information, eg. x86_64.
+	HostCross_2NdArch *string `protobuf:"bytes,15,opt,name=host_cross_2nd_arch,json=hostCross2ndArch" json:"host_cross_2nd_arch,omitempty"`
+	// The directory for generated built artifacts installation, eg. out.
+	OutDir *string `protobuf:"bytes,16,opt,name=out_dir,json=outDir" json:"out_dir,omitempty"`
+	// The metrics for calling various tools (microfactory) before Soong_UI starts.
+	SetupTools []*PerfInfo `protobuf:"bytes,17,rep,name=setup_tools,json=setupTools" json:"setup_tools,omitempty"`
+	// The metrics for calling Kati by multiple times.
+	KatiRuns []*PerfInfo `protobuf:"bytes,18,rep,name=kati_runs,json=katiRuns" json:"kati_runs,omitempty"`
+	// The metrics for calling Soong.
+	SoongRuns []*PerfInfo `protobuf:"bytes,19,rep,name=soong_runs,json=soongRuns" json:"soong_runs,omitempty"`
+	// The metrics for calling Ninja.
+	NinjaRuns            []*PerfInfo `protobuf:"bytes,20,rep,name=ninja_runs,json=ninjaRuns" json:"ninja_runs,omitempty"`
+	XXX_NoUnkeyedLiteral struct{}    `json:"-"`
+	XXX_unrecognized     []byte      `json:"-"`
+	XXX_sizecache        int32       `json:"-"`
+}
+
+func (m *MetricsBase) Reset()         { *m = MetricsBase{} }
+func (m *MetricsBase) String() string { return proto.CompactTextString(m) }
+func (*MetricsBase) ProtoMessage()    {}
+func (*MetricsBase) Descriptor() ([]byte, []int) {
+	return fileDescriptor_metrics_9e7b895801991242, []int{0}
+}
+func (m *MetricsBase) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_MetricsBase.Unmarshal(m, b)
+}
+func (m *MetricsBase) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_MetricsBase.Marshal(b, m, deterministic)
+}
+func (dst *MetricsBase) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_MetricsBase.Merge(dst, src)
+}
+func (m *MetricsBase) XXX_Size() int {
+	return xxx_messageInfo_MetricsBase.Size(m)
+}
+func (m *MetricsBase) XXX_DiscardUnknown() {
+	xxx_messageInfo_MetricsBase.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_MetricsBase proto.InternalMessageInfo
+
+const Default_MetricsBase_TargetBuildVariant MetricsBase_BUILDVARIANT = MetricsBase_ENG
+const Default_MetricsBase_TargetArch MetricsBase_ARCH = MetricsBase_UNKNOWN
+const Default_MetricsBase_HostArch MetricsBase_ARCH = MetricsBase_UNKNOWN
+const Default_MetricsBase_Host_2NdArch MetricsBase_ARCH = MetricsBase_UNKNOWN
+
+func (m *MetricsBase) GetBuildDateTimestamp() int64 {
+	if m != nil && m.BuildDateTimestamp != nil {
+		return *m.BuildDateTimestamp
+	}
+	return 0
+}
+
+func (m *MetricsBase) GetBuildId() string {
+	if m != nil && m.BuildId != nil {
+		return *m.BuildId
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetPlatformVersionCodename() string {
+	if m != nil && m.PlatformVersionCodename != nil {
+		return *m.PlatformVersionCodename
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetTargetProduct() string {
+	if m != nil && m.TargetProduct != nil {
+		return *m.TargetProduct
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetTargetBuildVariant() MetricsBase_BUILDVARIANT {
+	if m != nil && m.TargetBuildVariant != nil {
+		return *m.TargetBuildVariant
+	}
+	return Default_MetricsBase_TargetBuildVariant
+}
+
+func (m *MetricsBase) GetTargetArch() MetricsBase_ARCH {
+	if m != nil && m.TargetArch != nil {
+		return *m.TargetArch
+	}
+	return Default_MetricsBase_TargetArch
+}
+
+func (m *MetricsBase) GetTargetArchVariant() string {
+	if m != nil && m.TargetArchVariant != nil {
+		return *m.TargetArchVariant
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetTargetCpuVariant() string {
+	if m != nil && m.TargetCpuVariant != nil {
+		return *m.TargetCpuVariant
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetHostArch() MetricsBase_ARCH {
+	if m != nil && m.HostArch != nil {
+		return *m.HostArch
+	}
+	return Default_MetricsBase_HostArch
+}
+
+func (m *MetricsBase) GetHost_2NdArch() MetricsBase_ARCH {
+	if m != nil && m.Host_2NdArch != nil {
+		return *m.Host_2NdArch
+	}
+	return Default_MetricsBase_Host_2NdArch
+}
+
+func (m *MetricsBase) GetHostOs() string {
+	if m != nil && m.HostOs != nil {
+		return *m.HostOs
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetHostOsExtra() string {
+	if m != nil && m.HostOsExtra != nil {
+		return *m.HostOsExtra
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetHostCrossOs() string {
+	if m != nil && m.HostCrossOs != nil {
+		return *m.HostCrossOs
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetHostCrossArch() string {
+	if m != nil && m.HostCrossArch != nil {
+		return *m.HostCrossArch
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetHostCross_2NdArch() string {
+	if m != nil && m.HostCross_2NdArch != nil {
+		return *m.HostCross_2NdArch
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetOutDir() string {
+	if m != nil && m.OutDir != nil {
+		return *m.OutDir
+	}
+	return ""
+}
+
+func (m *MetricsBase) GetSetupTools() []*PerfInfo {
+	if m != nil {
+		return m.SetupTools
+	}
+	return nil
+}
+
+func (m *MetricsBase) GetKatiRuns() []*PerfInfo {
+	if m != nil {
+		return m.KatiRuns
+	}
+	return nil
+}
+
+func (m *MetricsBase) GetSoongRuns() []*PerfInfo {
+	if m != nil {
+		return m.SoongRuns
+	}
+	return nil
+}
+
+func (m *MetricsBase) GetNinjaRuns() []*PerfInfo {
+	if m != nil {
+		return m.NinjaRuns
+	}
+	return nil
+}
+
+type PerfInfo struct {
+	// The description for the phase/action/part while the tool running.
+	Desc *string `protobuf:"bytes,1,opt,name=desc" json:"desc,omitempty"`
+	// The name for the running phase/action/part.
+	Name *string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"`
+	// The absolute start time.
+	// The number of nanoseconds elapsed since January 1, 1970 UTC.
+	StartTime *uint64 `protobuf:"varint,3,opt,name=start_time,json=startTime" json:"start_time,omitempty"`
+	// The real running time.
+	// The number of nanoseconds elapsed since start_time.
+	RealTime *uint64 `protobuf:"varint,4,opt,name=real_time,json=realTime" json:"real_time,omitempty"`
+	// The number of MB for memory use.
+	MemoryUse            *uint64  `protobuf:"varint,5,opt,name=memory_use,json=memoryUse" json:"memory_use,omitempty"`
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
+}
+
+func (m *PerfInfo) Reset()         { *m = PerfInfo{} }
+func (m *PerfInfo) String() string { return proto.CompactTextString(m) }
+func (*PerfInfo) ProtoMessage()    {}
+func (*PerfInfo) Descriptor() ([]byte, []int) {
+	return fileDescriptor_metrics_9e7b895801991242, []int{1}
+}
+func (m *PerfInfo) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_PerfInfo.Unmarshal(m, b)
+}
+func (m *PerfInfo) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_PerfInfo.Marshal(b, m, deterministic)
+}
+func (dst *PerfInfo) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_PerfInfo.Merge(dst, src)
+}
+func (m *PerfInfo) XXX_Size() int {
+	return xxx_messageInfo_PerfInfo.Size(m)
+}
+func (m *PerfInfo) XXX_DiscardUnknown() {
+	xxx_messageInfo_PerfInfo.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_PerfInfo proto.InternalMessageInfo
+
+func (m *PerfInfo) GetDesc() string {
+	if m != nil && m.Desc != nil {
+		return *m.Desc
+	}
+	return ""
+}
+
+func (m *PerfInfo) GetName() string {
+	if m != nil && m.Name != nil {
+		return *m.Name
+	}
+	return ""
+}
+
+func (m *PerfInfo) GetStartTime() uint64 {
+	if m != nil && m.StartTime != nil {
+		return *m.StartTime
+	}
+	return 0
+}
+
+func (m *PerfInfo) GetRealTime() uint64 {
+	if m != nil && m.RealTime != nil {
+		return *m.RealTime
+	}
+	return 0
+}
+
+func (m *PerfInfo) GetMemoryUse() uint64 {
+	if m != nil && m.MemoryUse != nil {
+		return *m.MemoryUse
+	}
+	return 0
+}
+
+type ModuleTypeInfo struct {
+	// The build system, eg. Soong or Make.
+	BuildSystem *ModuleTypeInfo_BUILDSYSTEM `protobuf:"varint,1,opt,name=build_system,json=buildSystem,enum=build_metrics.ModuleTypeInfo_BUILDSYSTEM,def=0" json:"build_system,omitempty"`
+	// The module type, eg. java_library, cc_binary, and etc.
+	ModuleType *string `protobuf:"bytes,2,opt,name=module_type,json=moduleType" json:"module_type,omitempty"`
+	// The number of logical modules.
+	NumOfModules         *uint32  `protobuf:"varint,3,opt,name=num_of_modules,json=numOfModules" json:"num_of_modules,omitempty"`
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
+}
+
+func (m *ModuleTypeInfo) Reset()         { *m = ModuleTypeInfo{} }
+func (m *ModuleTypeInfo) String() string { return proto.CompactTextString(m) }
+func (*ModuleTypeInfo) ProtoMessage()    {}
+func (*ModuleTypeInfo) Descriptor() ([]byte, []int) {
+	return fileDescriptor_metrics_9e7b895801991242, []int{2}
+}
+func (m *ModuleTypeInfo) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_ModuleTypeInfo.Unmarshal(m, b)
+}
+func (m *ModuleTypeInfo) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_ModuleTypeInfo.Marshal(b, m, deterministic)
+}
+func (dst *ModuleTypeInfo) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_ModuleTypeInfo.Merge(dst, src)
+}
+func (m *ModuleTypeInfo) XXX_Size() int {
+	return xxx_messageInfo_ModuleTypeInfo.Size(m)
+}
+func (m *ModuleTypeInfo) XXX_DiscardUnknown() {
+	xxx_messageInfo_ModuleTypeInfo.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_ModuleTypeInfo proto.InternalMessageInfo
+
+const Default_ModuleTypeInfo_BuildSystem ModuleTypeInfo_BUILDSYSTEM = ModuleTypeInfo_UNKNOWN
+
+func (m *ModuleTypeInfo) GetBuildSystem() ModuleTypeInfo_BUILDSYSTEM {
+	if m != nil && m.BuildSystem != nil {
+		return *m.BuildSystem
+	}
+	return Default_ModuleTypeInfo_BuildSystem
+}
+
+func (m *ModuleTypeInfo) GetModuleType() string {
+	if m != nil && m.ModuleType != nil {
+		return *m.ModuleType
+	}
+	return ""
+}
+
+func (m *ModuleTypeInfo) GetNumOfModules() uint32 {
+	if m != nil && m.NumOfModules != nil {
+		return *m.NumOfModules
+	}
+	return 0
+}
+
+func init() {
+	proto.RegisterType((*MetricsBase)(nil), "build_metrics.MetricsBase")
+	proto.RegisterType((*PerfInfo)(nil), "build_metrics.PerfInfo")
+	proto.RegisterType((*ModuleTypeInfo)(nil), "build_metrics.ModuleTypeInfo")
+	proto.RegisterEnum("build_metrics.MetricsBase_BUILDVARIANT", MetricsBase_BUILDVARIANT_name, MetricsBase_BUILDVARIANT_value)
+	proto.RegisterEnum("build_metrics.MetricsBase_ARCH", MetricsBase_ARCH_name, MetricsBase_ARCH_value)
+	proto.RegisterEnum("build_metrics.ModuleTypeInfo_BUILDSYSTEM", ModuleTypeInfo_BUILDSYSTEM_name, ModuleTypeInfo_BUILDSYSTEM_value)
+}
+
+func init() { proto.RegisterFile("metrics.proto", fileDescriptor_metrics_9e7b895801991242) }
+
+var fileDescriptor_metrics_9e7b895801991242 = []byte{
+	// 783 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x55, 0xdd, 0x6e, 0xdb, 0x36,
+	0x14, 0xae, 0x62, 0x25, 0x96, 0x8e, 0x62, 0x57, 0x61, 0x02, 0x44, 0xc5, 0x50, 0x34, 0x30, 0xf6,
+	0x93, 0x01, 0x9b, 0x57, 0x18, 0x81, 0x11, 0x04, 0xbb, 0xb1, 0x13, 0xa3, 0x35, 0x5a, 0xdb, 0x85,
+	0x6c, 0x67, 0xdd, 0x2e, 0x46, 0x68, 0x12, 0xdd, 0x68, 0xb3, 0x44, 0x81, 0xa4, 0x8a, 0xf9, 0x21,
+	0xf6, 0x8c, 0x7b, 0x91, 0x5d, 0x0c, 0x3c, 0xb4, 0x5c, 0xa5, 0x17, 0x29, 0x72, 0x47, 0x9d, 0xef,
+	0x87, 0xdf, 0x91, 0xc8, 0x23, 0x68, 0x65, 0x4c, 0x89, 0x34, 0x96, 0xdd, 0x42, 0x70, 0xc5, 0x49,
+	0xeb, 0x8f, 0x32, 0x5d, 0x27, 0x74, 0x5b, 0xec, 0xfc, 0xe7, 0x80, 0x37, 0x31, 0xeb, 0x61, 0x24,
+	0x19, 0x79, 0x09, 0x27, 0x86, 0x90, 0x44, 0x8a, 0x51, 0x95, 0x66, 0x4c, 0xaa, 0x28, 0x2b, 0x02,
+	0xeb, 0xcc, 0x3a, 0x6f, 0x84, 0x04, 0xb1, 0x9b, 0x48, 0xb1, 0x45, 0x85, 0x90, 0x67, 0xe0, 0x18,
+	0x45, 0x9a, 0x04, 0x7b, 0x67, 0xd6, 0xb9, 0x1b, 0x36, 0xf1, 0x79, 0x9c, 0x90, 0x2b, 0x78, 0x56,
+	0xac, 0x23, 0xb5, 0xe2, 0x22, 0xa3, 0x1f, 0x99, 0x90, 0x29, 0xcf, 0x69, 0xcc, 0x13, 0x96, 0x47,
+	0x19, 0x0b, 0x1a, 0xc8, 0x3d, 0xad, 0x08, 0xb7, 0x06, 0xbf, 0xde, 0xc2, 0xe4, 0x1b, 0x68, 0xab,
+	0x48, 0x7c, 0x60, 0x8a, 0x16, 0x82, 0x27, 0x65, 0xac, 0x02, 0x1b, 0x05, 0x2d, 0x53, 0x7d, 0x67,
+	0x8a, 0xe4, 0x77, 0x38, 0xd9, 0xd2, 0x4c, 0x88, 0x8f, 0x91, 0x48, 0xa3, 0x5c, 0x05, 0xfb, 0x67,
+	0xd6, 0x79, 0xbb, 0xf7, 0x5d, 0xf7, 0x5e, 0xb7, 0xdd, 0x5a, 0xa7, 0xdd, 0xe1, 0x72, 0xfc, 0xf6,
+	0xe6, 0x76, 0x10, 0x8e, 0x07, 0xd3, 0xc5, 0x55, 0x63, 0x34, 0x7d, 0x15, 0x12, 0xe3, 0x34, 0xd4,
+	0x92, 0x5b, 0xe3, 0x43, 0xc6, 0xe0, 0x6d, 0xfd, 0x23, 0x11, 0xdf, 0x05, 0x07, 0x68, 0xfb, 0xe2,
+	0x01, 0xdb, 0x41, 0x78, 0xfd, 0xfa, 0xaa, 0xb9, 0x9c, 0xbe, 0x99, 0xce, 0x7e, 0x99, 0x86, 0x60,
+	0xc4, 0x03, 0x11, 0xdf, 0x91, 0x2e, 0x1c, 0xd7, 0xac, 0x76, 0x49, 0x9b, 0xd8, 0xd6, 0xd1, 0x27,
+	0x62, 0xb5, 0xf5, 0x0f, 0xb0, 0x0d, 0x44, 0xe3, 0xa2, 0xdc, 0xd1, 0x1d, 0xa4, 0xfb, 0x06, 0xb9,
+	0x2e, 0xca, 0x8a, 0x3d, 0x02, 0xf7, 0x8e, 0xcb, 0x6d, 0x4c, 0xf7, 0x91, 0x31, 0x1d, 0x2d, 0xc5,
+	0x90, 0x6f, 0xa1, 0x85, 0x36, 0xbd, 0x3c, 0x31, 0x56, 0xf0, 0x48, 0x2b, 0x4f, 0xcb, 0x7b, 0x79,
+	0x82, 0x6e, 0xa7, 0xd0, 0x44, 0x37, 0x2e, 0x03, 0x0f, 0x73, 0x1f, 0xe8, 0xc7, 0x99, 0x24, 0x9d,
+	0xed, 0x36, 0x5c, 0x52, 0xf6, 0xb7, 0x12, 0x51, 0x70, 0x88, 0xb0, 0x67, 0xe0, 0x91, 0x2e, 0xed,
+	0x38, 0xb1, 0xe0, 0x52, 0x6a, 0x8b, 0xd6, 0x27, 0xce, 0xb5, 0xae, 0xcd, 0x24, 0xf9, 0x16, 0x9e,
+	0xd6, 0x38, 0x18, 0xb8, 0x6d, 0x8e, 0xc9, 0x8e, 0x85, 0x41, 0x7e, 0x84, 0xe3, 0x1a, 0x6f, 0xd7,
+	0xdc, 0x53, 0xf3, 0x32, 0x77, 0xdc, 0x5a, 0x6e, 0x5e, 0x2a, 0x9a, 0xa4, 0x22, 0xf0, 0x4d, 0x6e,
+	0x5e, 0xaa, 0x9b, 0x54, 0x90, 0x4b, 0xf0, 0x24, 0x53, 0x65, 0x41, 0x15, 0xe7, 0x6b, 0x19, 0x1c,
+	0x9d, 0x35, 0xce, 0xbd, 0xde, 0xe9, 0x67, 0x2f, 0xe7, 0x1d, 0x13, 0xab, 0x71, 0xbe, 0xe2, 0x21,
+	0x20, 0x77, 0xa1, 0xa9, 0xe4, 0x02, 0xdc, 0xbf, 0x22, 0x95, 0x52, 0x51, 0xe6, 0x32, 0x20, 0x0f,
+	0xeb, 0x1c, 0xcd, 0x0c, 0xcb, 0x5c, 0x92, 0x3e, 0x80, 0xe4, 0x3c, 0xff, 0x60, 0x64, 0xc7, 0x0f,
+	0xcb, 0x5c, 0xa4, 0x56, 0xba, 0x3c, 0xcd, 0xff, 0x8c, 0x8c, 0xee, 0xe4, 0x0b, 0x3a, 0xa4, 0x6a,
+	0x5d, 0xe7, 0x25, 0x1c, 0xd6, 0xef, 0x05, 0x71, 0xc0, 0x5e, 0xce, 0x47, 0xa1, 0xff, 0x84, 0xb4,
+	0xc0, 0xd5, 0xab, 0x9b, 0xd1, 0x70, 0xf9, 0xca, 0xb7, 0x48, 0x13, 0xf4, 0x95, 0xf1, 0xf7, 0x3a,
+	0x3f, 0x83, 0xad, 0x0f, 0x00, 0xf1, 0xa0, 0x3a, 0x02, 0xfe, 0x13, 0x8d, 0x0e, 0xc2, 0x89, 0x6f,
+	0x11, 0x17, 0xf6, 0x07, 0xe1, 0xa4, 0x7f, 0xe1, 0xef, 0xe9, 0xda, 0xfb, 0xcb, 0xbe, 0xdf, 0x20,
+	0x00, 0x07, 0xef, 0x2f, 0xfb, 0xb4, 0x7f, 0xe1, 0xdb, 0x9d, 0x7f, 0x2c, 0x70, 0xaa, 0x1c, 0x84,
+	0x80, 0x9d, 0x30, 0x19, 0xe3, 0xac, 0x71, 0x43, 0x5c, 0xeb, 0x1a, 0x4e, 0x0b, 0x33, 0x59, 0x70,
+	0x4d, 0x9e, 0x03, 0x48, 0x15, 0x09, 0x85, 0xe3, 0x09, 0xe7, 0x88, 0x1d, 0xba, 0x58, 0xd1, 0x53,
+	0x89, 0x7c, 0x05, 0xae, 0x60, 0xd1, 0xda, 0xa0, 0x36, 0xa2, 0x8e, 0x2e, 0x20, 0xf8, 0x1c, 0x20,
+	0x63, 0x19, 0x17, 0x1b, 0x5a, 0x4a, 0x86, 0x53, 0xc2, 0x0e, 0x5d, 0x53, 0x59, 0x4a, 0xd6, 0xf9,
+	0xd7, 0x82, 0xf6, 0x84, 0x27, 0xe5, 0x9a, 0x2d, 0x36, 0x05, 0xc3, 0x54, 0x4b, 0x38, 0x34, 0xef,
+	0x4d, 0x6e, 0xa4, 0x62, 0x19, 0xa6, 0x6b, 0xf7, 0xbe, 0xff, 0xfc, 0x42, 0xdc, 0x13, 0x99, 0xe1,
+	0x32, 0xff, 0x75, 0xbe, 0x18, 0x4d, 0x6a, 0x57, 0x03, 0x25, 0x73, 0xb4, 0x21, 0x2f, 0xc0, 0xcb,
+	0x50, 0x43, 0xd5, 0xa6, 0xa8, 0xfa, 0x83, 0x6c, 0x67, 0x43, 0xbe, 0x86, 0x76, 0x5e, 0x66, 0x94,
+	0xaf, 0xa8, 0x29, 0x4a, 0xec, 0xb4, 0x15, 0x1e, 0xe6, 0x65, 0x36, 0x5b, 0x99, 0xfd, 0x64, 0xe7,
+	0x27, 0xf0, 0x6a, 0x7b, 0xdd, 0xff, 0x0a, 0x2e, 0xec, 0xcf, 0x67, 0xb3, 0xa9, 0xfe, 0x5c, 0x0e,
+	0xd8, 0x93, 0xc1, 0x9b, 0x91, 0xbf, 0x37, 0x3c, 0x7a, 0xdd, 0xf8, 0xad, 0xfa, 0x25, 0x50, 0xfc,
+	0x25, 0xfc, 0x1f, 0x00, 0x00, 0xff, 0xff, 0xd4, 0x8d, 0x19, 0x89, 0x22, 0x06, 0x00, 0x00,
+}
diff --git a/ui/metrics/metrics_proto/metrics.proto b/ui/metrics/metrics_proto/metrics.proto
new file mode 100644
index 0000000..b3de2f4
--- /dev/null
+++ b/ui/metrics/metrics_proto/metrics.proto
@@ -0,0 +1,129 @@
+// 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.
+
+syntax = "proto2";
+
+option optimize_for = LITE_RUNTIME;
+
+package build_metrics;
+option go_package = "metrics_proto";
+
+message MetricsBase {
+  // Timestamp generated when the build starts.
+  optional int64 build_date_timestamp = 1;
+
+  // It is usually used to specify the branch name [and release candidate].
+  optional string build_id  = 2;
+
+  // The platform version codename, eg. P, Q, REL.
+  optional string platform_version_codename = 3;
+
+  // The target product information, eg. aosp_arm.
+  optional string target_product = 4;
+
+  enum BUILDVARIANT {
+    USER = 0;
+    USERDEBUG = 1;
+    ENG = 2;
+  }
+  // The target build variant information, eg. eng.
+  optional BUILDVARIANT target_build_variant = 5 [default = ENG];
+
+  enum ARCH {
+    UNKNOWN = 0;
+    ARM = 1;
+    ARM64 = 2;
+    X86 = 3;
+    X86_64 = 4;
+  }
+  // The target arch information, eg. arm.
+  optional ARCH target_arch = 6 [default = UNKNOWN];
+
+  // The target arch variant information, eg. armv7-a-neon.
+  optional string target_arch_variant = 7;
+
+  // The target cpu variant information, eg. generic.
+  optional string target_cpu_variant = 8;
+
+  // The host arch information, eg. x86_64.
+  optional ARCH host_arch = 9 [default = UNKNOWN];
+
+  // The host 2nd arch information, eg. x86.
+  optional ARCH host_2nd_arch = 10 [default = UNKNOWN];
+
+  // The host os information, eg. linux.
+  optional string host_os = 11;
+
+  // The host os extra information, eg. Linux-4.17.0-3rodete2-amd64-x86_64-Debian-GNU.
+  optional string host_os_extra = 12;
+
+  // The host cross os information, eg. windows.
+  optional string host_cross_os = 13;
+
+  // The host cross arch information, eg. x86.
+  optional string host_cross_arch = 14;
+
+  // The host cross 2nd arch information, eg. x86_64.
+  optional string host_cross_2nd_arch = 15;
+
+  // The directory for generated built artifacts installation, eg. out.
+  optional string out_dir = 16;
+
+  // The metrics for calling various tools (microfactory) before Soong_UI starts.
+  repeated PerfInfo setup_tools = 17;
+
+  // The metrics for calling Kati by multiple times.
+  repeated PerfInfo kati_runs = 18;
+
+  // The metrics for calling Soong.
+  repeated PerfInfo soong_runs = 19;
+
+  // The metrics for calling Ninja.
+  repeated PerfInfo ninja_runs = 20;
+}
+
+message PerfInfo {
+  // The description for the phase/action/part while the tool running.
+  optional string desc = 1;
+
+  // The name for the running phase/action/part.
+  optional string name = 2;
+
+  // The absolute start time.
+  // The number of nanoseconds elapsed since January 1, 1970 UTC.
+  optional uint64 start_time = 3;
+
+  // The real running time.
+  // The number of nanoseconds elapsed since start_time.
+  optional uint64 real_time = 4;
+
+  // The number of MB for memory use.
+  optional uint64 memory_use = 5;
+}
+
+message ModuleTypeInfo {
+  enum BUILDSYSTEM {
+    UNKNOWN = 0;
+    SOONG = 1;
+    MAKE = 2;
+  }
+  // The build system, eg. Soong or Make.
+  optional BUILDSYSTEM build_system = 1 [default = UNKNOWN];
+
+  // The module type, eg. java_library, cc_binary, and etc.
+  optional string module_type = 2;
+
+  // The number of logical modules.
+  optional uint32 num_of_modules = 3;
+}
diff --git a/ui/metrics/metrics_proto/regen.sh b/ui/metrics/metrics_proto/regen.sh
new file mode 100755
index 0000000..343c638
--- /dev/null
+++ b/ui/metrics/metrics_proto/regen.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+aprotoc --go_out=paths=source_relative:. metrics.proto
diff --git a/ui/metrics/time.go b/ui/metrics/time.go
new file mode 100644
index 0000000..7e8801a
--- /dev/null
+++ b/ui/metrics/time.go
@@ -0,0 +1,71 @@
+// 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 metrics
+
+import (
+	"time"
+
+	"android/soong/ui/metrics/metrics_proto"
+	"android/soong/ui/tracer"
+)
+
+type timeEvent struct {
+	desc string
+	name string
+
+	atNanos uint64 // timestamp measured in nanoseconds since the reference date
+}
+
+type TimeTracer interface {
+	Begin(name, desc string, thread tracer.Thread)
+	End(thread tracer.Thread) metrics_proto.PerfInfo
+}
+
+type timeTracerImpl struct {
+	activeEvents []timeEvent
+}
+
+var _ TimeTracer = &timeTracerImpl{}
+
+func (t *timeTracerImpl) now() uint64 {
+	return uint64(time.Now().UnixNano())
+}
+
+func (t *timeTracerImpl) Begin(name, desc string, thread tracer.Thread) {
+	t.beginAt(name, desc, t.now())
+}
+
+func (t *timeTracerImpl) beginAt(name, desc string, atNanos uint64) {
+	t.activeEvents = append(t.activeEvents, timeEvent{name: name, desc: desc, atNanos: atNanos})
+}
+
+func (t *timeTracerImpl) End(thread tracer.Thread) metrics_proto.PerfInfo {
+	return t.endAt(t.now())
+}
+
+func (t *timeTracerImpl) endAt(atNanos uint64) metrics_proto.PerfInfo {
+	if len(t.activeEvents) < 1 {
+		panic("Internal error: No pending events for endAt to end!")
+	}
+	lastEvent := t.activeEvents[len(t.activeEvents)-1]
+	t.activeEvents = t.activeEvents[:len(t.activeEvents)-1]
+	realTime := atNanos - lastEvent.atNanos
+
+	return metrics_proto.PerfInfo{
+		Desc:      &lastEvent.desc,
+		Name:      &lastEvent.name,
+		StartTime: &lastEvent.atNanos,
+		RealTime:  &realTime}
+}
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
 				}