Merge "Move -Wno-null-dereference to external only"
diff --git a/Android.bp b/Android.bp
index 37be36c..d5f052e 100644
--- a/Android.bp
+++ b/Android.bp
@@ -249,14 +249,17 @@
         "java/java_resources.go",
         "java/prebuilt_apis.go",
         "java/proto.go",
+        "java/sdk.go",
         "java/sdk_library.go",
         "java/support_libraries.go",
         "java/system_modules.go",
     ],
     testSrcs: [
         "java/app_test.go",
+        "java/dexpreopt_test.go",
         "java/java_test.go",
         "java/jdeps_test.go",
+        "java/sdk_test.go",
     ],
     pluginFor: ["soong_build"],
 }
diff --git a/OWNERS b/OWNERS
index 30546f6..d56644b 100644
--- a/OWNERS
+++ b/OWNERS
@@ -1,4 +1,6 @@
 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 f046318..38f6ec8 100644
--- a/android/config.go
+++ b/android/config.go
@@ -561,6 +561,19 @@
 	}
 }
 
+func (c *config) ApexKeyDir(ctx ModuleContext) SourcePath {
+	// TODO(b/121224311): define another variable such as TARGET_APEX_KEY_OVERRIDE
+	defaultCert := String(c.productVariables.DefaultAppCertificate)
+	if defaultCert == "" || filepath.Dir(defaultCert) == "build/target/product/security" {
+		// When defaultCert is unset or is set to the testkeys path, use the APEX keys
+		// that is under the module dir
+		return PathForModuleSrc(ctx).SourcePath
+	} else {
+		// If not, APEX keys are under the specified directory
+		return PathForSource(ctx, filepath.Dir(defaultCert))
+	}
+}
+
 func (c *config) AllowMissingDependencies() bool {
 	return Bool(c.productVariables.Allow_missing_dependencies)
 }
@@ -862,6 +875,24 @@
 	return c.config.productVariables.BoardPlatPrivateSepolicyDirs
 }
 
+func (c *deviceConfig) OverrideManifestPackageNameFor(name string) (manifestName string, overridden bool) {
+	overrides := c.config.productVariables.ManifestPackageNameOverrides
+	if overrides == nil || len(overrides) == 0 {
+		return "", false
+	}
+	for _, o := range overrides {
+		split := strings.Split(o, ":")
+		if len(split) != 2 {
+			// This shouldn't happen as this is first checked in make, but just in case.
+			panic(fmt.Errorf("invalid override rule %q in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES should be <module_name>:<manifest_name>", o))
+		}
+		if matchPattern(split[0], name) {
+			return substPattern(split[0], split[1], name), true
+		}
+	}
+	return "", false
+}
+
 func (c *config) SecondArchIsTranslated() bool {
 	deviceTargets := c.Targets[Android]
 	if len(deviceTargets) < 2 {
@@ -932,6 +963,14 @@
 	return Bool(c.productVariables.FlattenApex)
 }
 
+func (c *config) EnforceSystemCertificate() bool {
+	return Bool(c.productVariables.EnforceSystemCertificate)
+}
+
+func (c *config) EnforceSystemCertificateWhitelist() []string {
+	return c.productVariables.EnforceSystemCertificateWhitelist
+}
+
 func stringSlice(s *[]string) []string {
 	if s != nil {
 		return *s
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/testing.go b/android/testing.go
index ca7e7ce..d318839 100644
--- a/android/testing.go
+++ b/android/testing.go
@@ -121,33 +121,59 @@
 	ctx.Context.MockFileSystem(files)
 }
 
+// TestingModule is wrapper around an android.Module that provides methods to find information about individual
+// ctx.Build parameters for verification in tests.
 type TestingModule struct {
 	module Module
 }
 
+// Module returns the Module wrapped by the TestingModule.
 func (m TestingModule) Module() Module {
 	return m.module
 }
 
-func (m TestingModule) Rule(rule string) BuildParams {
+// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name.  Returns an empty
+// BuildParams if no rule is found.
+func (m TestingModule) MaybeRule(rule string) BuildParams {
 	for _, p := range m.module.BuildParamsForTests() {
 		if strings.Contains(p.Rule.String(), rule) {
 			return p
 		}
 	}
-	panic(fmt.Errorf("couldn't find rule %q", rule))
+	return BuildParams{}
 }
 
-func (m TestingModule) Description(desc string) BuildParams {
+// Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name.  Panics if no rule is found.
+func (m TestingModule) Rule(rule string) BuildParams {
+	p := m.MaybeRule(rule)
+	if p.Rule == nil {
+		panic(fmt.Errorf("couldn't find rule %q", rule))
+	}
+	return p
+}
+
+// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string.  Returns an empty
+// BuildParams if no rule is found.
+func (m TestingModule) MaybeDescription(desc string) BuildParams {
 	for _, p := range m.module.BuildParamsForTests() {
 		if p.Description == desc {
 			return p
 		}
 	}
-	panic(fmt.Errorf("couldn't find description %q", desc))
+	return BuildParams{}
 }
 
-func (m TestingModule) Output(file string) BuildParams {
+// Description finds a call to ctx.Build with BuildParams.Description set to a the given string.  Panics if no rule is
+// found.
+func (m TestingModule) Description(desc string) BuildParams {
+	p := m.MaybeDescription(desc)
+	if p.Rule == nil {
+		panic(fmt.Errorf("couldn't find description %q", desc))
+	}
+	return p
+}
+
+func (m TestingModule) maybeOutput(file string) (BuildParams, []string) {
 	var searchedOutputs []string
 	for _, p := range m.module.BuildParamsForTests() {
 		outputs := append(WritablePaths(nil), p.Outputs...)
@@ -156,13 +182,30 @@
 		}
 		for _, f := range outputs {
 			if f.String() == file || f.Rel() == file {
-				return p
+				return p, nil
 			}
 			searchedOutputs = append(searchedOutputs, f.Rel())
 		}
 	}
-	panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v",
-		file, searchedOutputs))
+	return BuildParams{}, searchedOutputs
+}
+
+// MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputspath whose String() or Rel()
+// value matches the provided string.  Returns an empty BuildParams if no rule is found.
+func (m TestingModule) MaybeOutput(file string) BuildParams {
+	p, _ := m.maybeOutput(file)
+	return p
+}
+
+// Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputspath whose String() or Rel()
+// value matches the provided string.  Panics if no rule is found.
+func (m TestingModule) Output(file string) BuildParams {
+	p, searchedOutputs := m.maybeOutput(file)
+	if p.Rule == nil {
+		panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v",
+			file, searchedOutputs))
+	}
+	return p
 }
 
 func FailIfErrored(t *testing.T, errs []error) {
diff --git a/android/util.go b/android/util.go
index e9b9764..92ab845 100644
--- a/android/util.go
+++ b/android/util.go
@@ -202,3 +202,44 @@
 	}
 	return v
 }
+
+// copied from build/kati/strutil.go
+func substPattern(pat, repl, str string) string {
+	ps := strings.SplitN(pat, "%", 2)
+	if len(ps) != 2 {
+		if str == pat {
+			return repl
+		}
+		return str
+	}
+	in := str
+	trimed := str
+	if ps[0] != "" {
+		trimed = strings.TrimPrefix(in, ps[0])
+		if trimed == in {
+			return str
+		}
+	}
+	in = trimed
+	if ps[1] != "" {
+		trimed = strings.TrimSuffix(in, ps[1])
+		if trimed == in {
+			return str
+		}
+	}
+
+	rs := strings.SplitN(repl, "%", 2)
+	if len(rs) != 2 {
+		return repl
+	}
+	return rs[0] + trimed + rs[1]
+}
+
+// copied from build/kati/strutil.go
+func matchPattern(pat, str string) bool {
+	i := strings.IndexByte(pat, '%')
+	if i < 0 {
+		return pat == str
+	}
+	return strings.HasPrefix(str, pat[:i]) && strings.HasSuffix(str, pat[i+1:])
+}
diff --git a/android/variable.go b/android/variable.go
index a1cdea1..7e976cd 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -261,6 +261,11 @@
 	FlattenApex *bool `json:",omitempty"`
 
 	DexpreoptGlobalConfig *string `json:",omitempty"`
+
+	ManifestPackageNameOverrides []string `json:",omitempty"`
+
+	EnforceSystemCertificate          *bool    `json:",omitempty"`
+	EnforceSystemCertificateWhitelist []string `json:",omitempty"`
 }
 
 func boolPtr(v bool) *bool {
diff --git a/androidmk/cmd/androidmk/android.go b/androidmk/cmd/androidmk/android.go
index 99b3523..a28cf49 100644
--- a/androidmk/cmd/androidmk/android.go
+++ b/androidmk/cmd/androidmk/android.go
@@ -42,6 +42,7 @@
 
 var rewriteProperties = map[string](func(variableAssignmentContext) error){
 	// custom functions
+	"LOCAL_32_BIT_ONLY":           local32BitOnly,
 	"LOCAL_AIDL_INCLUDES":         localAidlIncludes,
 	"LOCAL_C_INCLUDES":            localIncludeDirs,
 	"LOCAL_EXPORT_C_INCLUDE_DIRS": exportIncludeDirs,
@@ -144,6 +145,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",
@@ -156,6 +158,10 @@
 			"LOCAL_SHARED_ANDROID_LIBRARIES": "android_libs",
 			"LOCAL_STATIC_ANDROID_LIBRARIES": "android_static_libs",
 			"LOCAL_ADDITIONAL_CERTIFICATES":  "additional_certificates",
+
+			// Jacoco filters:
+			"LOCAL_JACK_COVERAGE_INCLUDE_FILTER": "jacoco.include_filter",
+			"LOCAL_JACK_COVERAGE_EXCLUDE_FILTER": "jacoco.exclude_filter",
 		})
 
 	addStandardProperties(bpparser.BoolType,
@@ -359,6 +365,20 @@
 	return splitAndAssign(ctx, classifyLocalOrGlobalPath, map[string]string{"global": "export_include_dirs", "local": "export_include_dirs"})
 }
 
+func local32BitOnly(ctx variableAssignmentContext) error {
+	val, err := makeVariableToBlueprint(ctx.file, ctx.mkvalue, bpparser.BoolType)
+	if err != nil {
+		return err
+	}
+	if val.(*bpparser.Bool).Value {
+		thirtyTwo := &bpparser.String{
+			Value: "32",
+		}
+		setVariable(ctx.file, false, ctx.prefix, "compile_multilib", thirtyTwo, true)
+	}
+	return nil
+}
+
 func localAidlIncludes(ctx variableAssignmentContext) error {
 	return splitAndAssign(ctx, classifyLocalOrGlobalPath, map[string]string{"global": "aidl.include_dirs", "local": "aidl.local_include_dirs"})
 }
@@ -738,27 +758,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..c750f22 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",
+				],
+			}
 		`,
 	},
 	{
@@ -632,12 +643,14 @@
 			include $(CLEAR_VARS)
 			LOCAL_SRC_FILES := test.java
 			LOCAL_RESOURCE_DIR := res
+			LOCAL_JACK_COVERAGE_INCLUDE_FILTER := foo.*
 			include $(BUILD_STATIC_JAVA_LIBRARY)
 
 			include $(CLEAR_VARS)
 			LOCAL_SRC_FILES := test.java
 			LOCAL_STATIC_LIBRARIES := foo
 			LOCAL_STATIC_ANDROID_LIBRARIES := bar
+			LOCAL_JACK_COVERAGE_EXCLUDE_FILTER := bar.*
 			include $(BUILD_STATIC_JAVA_LIBRARY)
 
 			include $(CLEAR_VARS)
@@ -655,6 +668,9 @@
 			android_library {
 				srcs: ["test.java"],
 				resource_dirs: ["res"],
+				jacoco: {
+					include_filter: ["foo.*"],
+				},
 			}
 
 			android_library {
@@ -663,6 +679,9 @@
 					"foo",
 					"bar",
 				],
+				jacoco: {
+					exclude_filter: ["bar.*"],
+				},
 			}
 
 			android_library {
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 6230236..e711c8b 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -40,7 +40,7 @@
 		Command: `echo '/ 1000 1000 0755' > ${out} && ` +
 			`echo '/apex_manifest.json 1000 1000 0644' >> ${out} && ` +
 			`echo ${ro_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0644"}' >> ${out} && ` +
-			`echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 1000 1000 0755"}' >> ${out}`,
+			`echo ${exec_paths} | tr ' ' '\n' | awk '{print "/"$$1 " 0 2000 0755"}' >> ${out}`,
 		Description: "fs_config ${out}",
 	}, "ro_paths", "exec_paths")
 
@@ -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} && ` +
@@ -374,6 +374,7 @@
 
 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" {
@@ -385,7 +386,7 @@
 		// multilib.both.
 		ctx.AddFarVariationDependencies([]blueprint.Variation{
 			{Mutator: "arch", Variation: target.String()},
-			{Mutator: "image", Variation: a.getImageVariation()},
+			{Mutator: "image", Variation: a.getImageVariation(config)},
 			{Mutator: "link", Variation: "shared"},
 		}, sharedLibTag, a.properties.Native_shared_libs...)
 
@@ -393,21 +394,21 @@
 		addDependenciesForNativeModules(ctx,
 			a.properties.Multilib.Both.Native_shared_libs,
 			a.properties.Multilib.Both.Binaries, target.String(),
-			a.getImageVariation())
+			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: a.getImageVariation()},
+				{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.getImageVariation())
+				a.getImageVariation(config))
 		}
 
 		switch target.Arch.ArchType.Multilib {
@@ -416,24 +417,24 @@
 			addDependenciesForNativeModules(ctx,
 				a.properties.Multilib.Lib32.Native_shared_libs,
 				a.properties.Multilib.Lib32.Binaries, target.String(),
-				a.getImageVariation())
+				a.getImageVariation(config))
 
 			addDependenciesForNativeModules(ctx,
 				a.properties.Multilib.Prefer32.Native_shared_libs,
 				a.properties.Multilib.Prefer32.Binaries, target.String(),
-				a.getImageVariation())
+				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.getImageVariation())
+				a.getImageVariation(config))
 
 			if !has32BitTarget {
 				addDependenciesForNativeModules(ctx,
 					a.properties.Multilib.Prefer32.Native_shared_libs,
 					a.properties.Multilib.Prefer32.Binaries, target.String(),
-					a.getImageVariation())
+					a.getImageVariation(config))
 			}
 		}
 
@@ -447,15 +448,17 @@
 		{Mutator: "arch", Variation: "android_common"},
 	}, prebuiltTag, a.properties.Prebuilts...)
 
-	if String(a.properties.Key) == "" {
-		ctx.ModuleErrorf("key is missing")
-		return
-	}
-	ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
+	if !ctx.Config().FlattenApex() || ctx.Config().UnbundledBuild() {
+		if String(a.properties.Key) == "" {
+			ctx.ModuleErrorf("key is missing")
+			return
+		}
+		ctx.AddDependency(ctx.Module(), keyTag, String(a.properties.Key))
 
-	cert := android.SrcIsModule(String(a.properties.Certificate))
-	if cert != "" {
-		ctx.AddDependency(ctx.Module(), certificateTag, cert)
+		cert := android.SrcIsModule(String(a.properties.Certificate))
+		if cert != "" {
+			ctx.AddDependency(ctx.Module(), certificateTag, cert)
+		}
 	}
 }
 
@@ -471,8 +474,8 @@
 	return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
 }
 
-func (a *apexBundle) getImageVariation() string {
-	if proptools.Bool(a.properties.Use_vendor) {
+func (a *apexBundle) getImageVariation(config android.DeviceConfig) string {
+	if config.VndkVersion() != "" && proptools.Bool(a.properties.Use_vendor) {
 		return "vendor"
 	} else {
 		return "core"
@@ -491,6 +494,19 @@
 	if !cc.Arch().Native {
 		dirInApex = filepath.Join(dirInApex, cc.Arch().ArchType.String())
 	}
+	switch cc.Name() {
+	case "libc", "libm", "libdl":
+		// Special case for bionic libs. This is to prevent the bionic libs
+		// from being included in the search path /apex/com.android.apex/lib.
+		// This exclusion is required because bionic libs in the runtime APEX
+		// are available via the legacy paths /system/lib/libc.so, etc. By the
+		// init process, the bionic libs in the APEX are bind-mounted to the
+		// legacy paths and thus will be loaded into the default linker namespace.
+		// If the bionic libs are directly in /apex/com.android.apex/lib then
+		// the same libs will be again loaded to the runtime linker namespace,
+		// which will result double loading of bionic libs that isn't supported.
+		dirInApex = filepath.Join(dirInApex, "bionic")
+	}
 
 	fileToCopy = cc.OutputFile().Path()
 	return
@@ -518,6 +534,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" {
@@ -576,6 +593,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)
@@ -605,7 +628,8 @@
 		return false
 	})
 
-	if keyFile == nil {
+	a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
+	if !a.flattened && keyFile == nil {
 		ctx.PropertyErrorf("key", "private_key for %q could not be found", String(a.properties.Key))
 		return
 	}
@@ -635,23 +659,23 @@
 		filesInfo[i].moduleName = ctx.ModuleName() + "." + filesInfo[i].moduleName
 	}
 
-	a.flattened = ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild()
 	a.installDir = android.PathForModuleInstall(ctx, "apex")
 	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)
@@ -739,8 +763,19 @@
 		}
 		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())
+		}
+
+		manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
+		if overridden {
+			optFlags = append(optFlags, "--override_apk_package_name "+manifestPackageName)
+		}
 
 		ctx.Build(pctx, android.BuildParams{
 			Rule:        apexRule,
@@ -755,6 +790,7 @@
 				"file_contexts":    fileContexts.String(),
 				"canned_fs_config": cannedFsConfig.String(),
 				"key":              keyFile.String(),
+				"opt_flags":        strings.Join(optFlags, " "),
 			},
 		})
 
@@ -816,7 +852,15 @@
 		// For flattened APEX, do nothing but make sure that apex_manifest.json file is also copied along
 		// with other ordinary files.
 		manifest := android.PathForModuleSrc(ctx, proptools.StringDefault(a.properties.Manifest, "apex_manifest.json"))
-		a.filesInfo = append(a.filesInfo, apexFile{manifest, ctx.ModuleName() + ".apex_manifest.json", android.Common, ".", etc, nil})
+
+		// rename to apex_manifest.json
+		copiedManifest := android.PathForModuleOut(ctx, "apex_manifest.json")
+		ctx.Build(pctx, android.BuildParams{
+			Rule:   android.Cp,
+			Input:  manifest,
+			Output: copiedManifest,
+		})
+		a.filesInfo = append(a.filesInfo, apexFile{copiedManifest, ctx.ModuleName() + ".apex_manifest.json", android.Common, ".", etc, nil})
 
 		for _, fi := range a.filesInfo {
 			dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
@@ -863,7 +907,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())
@@ -897,7 +941,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 dd5bf70..dbc85d9 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -124,13 +124,15 @@
 
 	ctx.MockFileSystem(map[string][]byte{
 		"Android.bp":                                []byte(bp),
-		"testkey.avbpubkey":                         nil,
-		"testkey.pem":                               nil,
 		"build/target/product/security":             nil,
 		"apex_manifest.json":                        nil,
 		"system/sepolicy/apex/myapex-file_contexts": nil,
 		"mylib.cpp":                                 nil,
 		"myprebuilt":                                nil,
+		"vendor/foo/devkeys/test.x509.pem":          nil,
+		"vendor/foo/devkeys/test.pk8":               nil,
+		"vendor/foo/devkeys/testkey.avbpubkey":      nil,
+		"vendor/foo/devkeys/testkey.pem":            nil,
 	})
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	android.FailIfErrored(t, errs)
@@ -148,6 +150,7 @@
 
 	config = android.TestArchConfig(buildDir, nil)
 	config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
+	config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
 	return
 }
 
@@ -227,6 +230,10 @@
 	// 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) {
@@ -488,6 +495,13 @@
 				versions: ["27", "28", "29"],
 			},
 		}
+
+		cc_library {
+			name: "libBootstrap",
+			srcs: ["mylib.cpp"],
+			stl: "none",
+			bootstrap: true,
+		}
 	`)
 
 	apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
@@ -495,11 +509,11 @@
 
 	// Ensure that mylib, libm, libdl are included.
 	ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
-	ensureContains(t, copyCmds, "image.apex/lib64/libm.so")
-	ensureContains(t, copyCmds, "image.apex/lib64/libdl.so")
+	ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
+	ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
 
 	// Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
-	ensureNotContains(t, copyCmds, "image.apex/lib64/libc.so")
+	ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
 
 	mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
 	mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
@@ -534,6 +548,12 @@
 	// ... Cflags from stub is correctly exported to mylib
 	ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
 	ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
+
+	// Ensure that libBootstrap is depending on the platform variant of bionic libs
+	libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
+	ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
+	ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
+	ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
 }
 
 func TestFilesInSubDir(t *testing.T) {
@@ -666,3 +686,46 @@
 	// 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")
 }
+
+func TestKeys(t *testing.T) {
+	ctx := testApex(t, `
+		apex {
+			name: "myapex",
+			key: "myapex.key",
+			native_shared_libs: ["mylib"],
+		}
+
+		cc_library {
+			name: "mylib",
+			srcs: ["mylib.cpp"],
+			system_shared_libs: [],
+			stl: "none",
+		}
+
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+
+	`)
+
+	// check the APEX keys
+	keys := ctx.ModuleForTests("myapex.key", "").Module().(*apexKey)
+
+	if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
+		t.Errorf("public key %q is not %q", keys.public_key_file.String(),
+			"vendor/foo/devkeys/testkey.avbpubkey")
+	}
+	if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
+		t.Errorf("private key %q is not %q", keys.private_key_file.String(),
+			"vendor/foo/devkeys/testkey.pem")
+	}
+
+	// check the APK certs
+	certs := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk").Args["certificates"]
+	if certs != "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8" {
+		t.Errorf("cert and private key %q are not %q", certs,
+			"vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8")
+	}
+}
diff --git a/apex/key.go b/apex/key.go
index ff348a8..7e98d2b 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,12 +57,29 @@
 	return module
 }
 
+func (m *apexKey) installable() bool {
+	return m.properties.Installable == nil || proptools.Bool(m.properties.Installable)
+}
+
 func (m *apexKey) DepsMutator(ctx android.BottomUpMutatorContext) {
 }
 
 func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
-	m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
-	m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
+	if ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuild() {
+		// Flattened APEXes are not signed
+		return
+	}
+
+	m.public_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
+	m.private_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
+
+	// If not found, fall back to the local key pairs
+	if !android.ExistentPathForSource(ctx, m.public_key_file.String()).Valid() {
+		m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
+	}
+	if !android.ExistentPathForSource(ctx, m.private_key_file.String()).Valid() {
+		m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
+	}
 
 	pubKeyName := m.public_key_file.Base()[0 : len(m.public_key_file.Base())-len(m.public_key_file.Ext())]
 	privKeyName := m.private_key_file.Base()[0 : len(m.private_key_file.Base())-len(m.private_key_file.Ext())]
@@ -71,7 +91,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 +104,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..7844756 100644
--- a/cc/androidmk.go
+++ b/cc/androidmk.go
@@ -29,9 +29,12 @@
 )
 
 type AndroidMkContext interface {
+	Name() string
 	Target() android.Target
 	subAndroidMk(*android.AndroidMkData, interface{})
 	useVndk() bool
+	static() bool
+	inRecovery() bool
 }
 
 type subAndroidMkProvider interface {
@@ -59,8 +62,12 @@
 
 	ret := android.AndroidMkData{
 		OutputFile: c.outputFile,
-		Required:   c.Properties.AndroidMkRuntimeLibs,
-		Include:    "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
+		// TODO(jiyong): add the APEXes providing shared libs to the required modules
+		// Currently, adding c.Properties.ApexesProvidingSharedLibs is causing multiple
+		// runtime APEXes (com.android.runtime.debug|release) to be installed. And this
+		// is breaking some older devices (like marlin) where system.img is small.
+		Required: c.Properties.AndroidMkRuntimeLibs,
+		Include:  "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
 
 		Extra: []android.AndroidMkExtraFunc{
 			func(w io.Writer, outputFile android.Path) {
@@ -76,9 +83,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")
@@ -175,13 +179,23 @@
 		}
 	})
 
-	if library.shared() {
+	if library.shared() && !library.buildStubs() {
 		ctx.subAndroidMk(ret, library.baseInstaller)
 	} else {
 		ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
 			fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
+			if library.buildStubs() {
+				fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true")
+			}
 		})
 	}
+
+	if len(library.Properties.Stubs.Versions) > 0 && android.DirectlyInAnyApex(ctx.Name()) &&
+		!ctx.inRecovery() && !ctx.useVndk() && !ctx.static() {
+		if !library.buildStubs() {
+			ret.SubName = ".bootstrap"
+		}
+	}
 }
 
 func (object *objectLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
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 c3e554c..85e3a67 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -201,6 +201,10 @@
 	Recovery_available *bool
 
 	InRecovery bool `blueprint:"mutated"`
+
+	// Allows this module to use non-APEX version of libraries. Useful
+	// for building binaries that are started before APEXes are activated.
+	Bootstrap *bool
 }
 
 type VendorProperties struct {
@@ -250,6 +254,8 @@
 	isPgoCompile() bool
 	useClangLld(actx ModuleContext) bool
 	isApex() bool
+	hasStubsVariants() bool
+	isStubs() bool
 }
 
 type ModuleContext interface {
@@ -634,6 +640,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
 	}
@@ -672,6 +682,14 @@
 	return ctx.mod.ApexName() != ""
 }
 
+func (ctx *moduleContextImpl) hasStubsVariants() bool {
+	return ctx.mod.HasStubsVariants()
+}
+
+func (ctx *moduleContextImpl) isStubs() bool {
+	return ctx.mod.IsStubs()
+}
+
 func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
 	return &Module{
 		hod:      hod,
@@ -851,6 +869,18 @@
 			return
 		}
 		c.outputFile = android.OptionalPathForPath(outputFile)
+
+		// If a lib is directly included in any of the APEXes, unhide the stubs
+		// variant having the latest version gets visible to make. In addition,
+		// the non-stubs variant is renamed to <libname>.bootstrap. This is to
+		// force anything in the make world to link against the stubs library.
+		// (unless it is explicitly referenced via .bootstrap suffix or the
+		// module is marked with 'bootstrap: true').
+		if c.HasStubsVariants() && android.DirectlyInAnyApex(ctx.baseModuleName()) &&
+			!c.inRecovery() && !c.useVndk() && !c.static() && c.IsStubs() {
+			c.Properties.HideFromMake = false // unhide
+			// Note: this is still non-installable
+		}
 	}
 
 	if c.installer != nil && !c.Properties.PreventInstall && c.IsForPlatform() && c.outputFile.Valid() {
@@ -1442,8 +1472,8 @@
 					// If not building for APEX, use stubs only when it is from
 					// an APEX (and not from platform)
 					useThisDep = (depInPlatform != depIsStubs)
-					if c.inRecovery() {
-						// However, for recovery modules, since there is no APEX there,
+					if c.inRecovery() || Bool(c.Properties.Bootstrap) {
+						// However, for recovery or bootstrap modules,
 						// always link to non-stub variant
 						useThisDep = !depIsStubs
 					}
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/library.go b/cc/library.go
index 40c1b4f..4adb081 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -646,6 +646,11 @@
 			linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
 		}
 	}
+	if library.buildStubs() {
+		linkerScriptFlags := "-Wl,--version-script," + library.versionScriptPath.String()
+		flags.LdFlags = append(flags.LdFlags, linkerScriptFlags)
+		linkerDeps = append(linkerDeps, library.versionScriptPath)
+	}
 
 	fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
 	outputFile := android.PathForModuleOut(ctx, fileName)
@@ -1059,7 +1064,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/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/config.go b/dexpreopt/config.go
index 6a4fd4a..6b5c40d 100644
--- a/dexpreopt/config.go
+++ b/dexpreopt/config.go
@@ -32,7 +32,10 @@
 
 	DisableGenerateProfile bool // don't generate profiles
 
-	BootJars         []string // jars that form the boot image
+	PreoptBootClassPathDexFiles     []string // file paths of boot class path files
+	PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
+
+	BootJars         []string // modules for jars that form the boot class path
 	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
@@ -83,14 +86,14 @@
 }
 
 type ModuleConfig struct {
-	Name            string
-	DexLocation     string // dex location on device
-	BuildPath       string
-	DexPath         string
-	PreferIntegrity bool
-	UncompressedDex bool
-	HasApkLibraries bool
-	PreoptFlags     []string
+	Name                string
+	DexLocation         string // dex location on device
+	BuildPath           string
+	DexPath             string
+	PreferCodeIntegrity bool
+	UncompressedDex     bool
+	HasApkLibraries     bool
+	PreoptFlags         []string
 
 	ProfileClassListing  string
 	ProfileIsTextListing bool
@@ -110,6 +113,7 @@
 
 	PresignedPrebuilt bool
 
+	NoStripping     bool
 	StripInputPath  string
 	StripOutputPath string
 }
diff --git a/dexpreopt/dexpreopt.go b/dexpreopt/dexpreopt.go
index 0ea32b8..f316be4 100644
--- a/dexpreopt/dexpreopt.go
+++ b/dexpreopt/dexpreopt.go
@@ -95,11 +95,38 @@
 
 	rule = &Rule{}
 
-	dexpreoptDisabled := contains(global.DisablePreoptModules, module.Name)
+	generateProfile := module.ProfileClassListing != "" && !global.DisableGenerateProfile
 
-	if contains(global.BootJars, module.Name) {
-		// Don't preopt individual boot jars, they will be preopted together
-		dexpreoptDisabled = true
+	var profile string
+	if generateProfile {
+		profile = profileCommand(global, module, rule)
+	}
+
+	if !dexpreoptDisabled(global, module) {
+		// Don't preopt individual boot jars, they will be preopted together.
+		// This check is outside dexpreoptDisabled because they still need to be stripped.
+		if !contains(global.BootJars, module.Name) {
+			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 dexpreoptDisabled(global GlobalConfig, module ModuleConfig) bool {
+	if contains(global.DisablePreoptModules, module.Name) {
+		return true
 	}
 
 	// If OnlyPreoptBootImageAndSystemServer=true and module is not in boot class path skip
@@ -108,32 +135,10 @@
 	// 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
+		return 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
+	return false
 }
 
 func profileCommand(global GlobalConfig, module ModuleConfig, rule *Rule) string {
@@ -192,6 +197,9 @@
 			pathtools.ReplaceExtension(filepath.Base(path), "odex"))
 	}
 
+	bcp := strings.Join(global.PreoptBootClassPathDexFiles, ":")
+	bcp_locations := strings.Join(global.PreoptBootClassPathDexLocations, ":")
+
 	odexPath := toOdexPath(filepath.Join(filepath.Dir(module.BuildPath), base))
 	odexInstallPath := toOdexPath(module.DexLocation)
 	if odexOnSystemOther(module, global) {
@@ -201,6 +209,8 @@
 	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
@@ -265,11 +275,11 @@
 		const hidlManager = "android.hidl.manager-V1.0-java"
 
 		conditionalClassLoaderContextHost29 = append(conditionalClassLoaderContextHost29,
-      pathForLibrary(module, hidlManager))
+			pathForLibrary(module, hidlManager))
 		conditionalClassLoaderContextTarget29 = append(conditionalClassLoaderContextTarget29,
 			filepath.Join("/system/framework", hidlManager+".jar"))
 		conditionalClassLoaderContextHost29 = append(conditionalClassLoaderContextHost29,
-      pathForLibrary(module, hidlBase))
+			pathForLibrary(module, hidlBase))
 		conditionalClassLoaderContextTarget29 = append(conditionalClassLoaderContextTarget29,
 			filepath.Join("/system/framework", hidlBase+".jar"))
 	} else {
@@ -291,7 +301,7 @@
 		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_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, " "))
@@ -305,8 +315,12 @@
 		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("--runtime-arg").FlagWithArg("-Xbootclasspath:", bcp).
+		Implicits(global.PreoptBootClassPathDexFiles).
+		Flag("--runtime-arg").FlagWithArg("-Xbootclasspath-locations:", bcp_locations).
 		Flag("${class_loader_context_arg}").
 		Flag("${stored_class_loader_context_arg}").
 		FlagWithArg("--boot-image=", bootImageLocation).Implicit(bootImagePath).
@@ -436,6 +450,14 @@
 func shouldStripDex(module ModuleConfig, global GlobalConfig) bool {
 	strip := !global.DefaultNoStripping
 
+	if dexpreoptDisabled(global, module) {
+		strip = false
+	}
+
+	if module.NoStripping {
+		strip = false
+	}
+
 	// 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) {
diff --git a/dexpreopt/dexpreopt_test.go b/dexpreopt/dexpreopt_test.go
index 5265248..f218d01 100644
--- a/dexpreopt/dexpreopt_test.go
+++ b/dexpreopt/dexpreopt_test.go
@@ -66,7 +66,7 @@
 	DexLocation:            "",
 	BuildPath:              "",
 	DexPath:                "",
-	PreferIntegrity:        false,
+	PreferCodeIntegrity:    false,
 	UncompressedDex:        false,
 	HasApkLibraries:        false,
 	PreoptFlags:            nil,
@@ -82,6 +82,7 @@
 	NoCreateAppImage:       false,
 	ForceCreateAppImage:    false,
 	PresignedPrebuilt:      false,
+	NoStripping:            false,
 	StripInputPath:         "",
 	StripOutputPath:        "",
 }
@@ -162,47 +163,60 @@
 }
 
 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)
+	tests := []struct {
+		name  string
+		setup func(global *GlobalConfig, module *ModuleConfig)
+		strip bool
+	}{
+		{
+			name:  "default strip",
+			setup: func(global *GlobalConfig, module *ModuleConfig) {},
+			strip: true,
+		},
+		{
+			name:  "global no stripping",
+			setup: func(global *GlobalConfig, module *ModuleConfig) { global.DefaultNoStripping = true },
+			strip: false,
+		},
+		{
+			name:  "module no stripping",
+			setup: func(global *GlobalConfig, module *ModuleConfig) { module.NoStripping = true },
+			strip: false,
+		},
 	}
 
-	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])
-	}
-}
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
 
-func TestNoStripDex(t *testing.T) {
-	global, module := testGlobalConfig, testModuleConfig
+			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"
 
-	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"
+			test.setup(&global, &module)
 
-	rule, err := GenerateStripRule(global, module)
-	if err != nil {
-		t.Error(err)
-	}
+			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())
+			if test.strip {
+				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])
+				}
+			} else {
+				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
index fd4cf82..9d4329c 100644
--- a/dexpreopt/script.go
+++ b/dexpreopt/script.go
@@ -147,6 +147,11 @@
 	return c
 }
 
+func (c *Command) Implicits(paths []string) *Command {
+	c.inputs = append(c.inputs, paths...)
+	return c
+}
+
 func (c *Command) Output(path string) *Command {
 	c.outputs = append(c.outputs, path)
 	return c.Text(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/app.go b/java/app.go
index a037ef3..4bae78a 100644
--- a/java/app.go
+++ b/java/app.go
@@ -184,6 +184,11 @@
 	// TODO: LOCAL_PACKAGE_OVERRIDES
 	//    $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
 
+	manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
+	if overridden {
+		linkFlags = append(linkFlags, "--rename-manifest-package "+manifestPackageName)
+	}
+
 	a.aapt.buildActions(ctx, sdkContext(a), linkFlags...)
 
 	// apps manifests are handled by aapt, don't let Module see them
@@ -213,7 +218,6 @@
 		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)
@@ -259,6 +263,20 @@
 
 	packageFile := android.PathForModuleOut(ctx, "package.apk")
 	CreateAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates)
+
+	if !a.Module.Platform() {
+		certPath := a.certificate.Pem.String()
+		systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
+		if strings.HasPrefix(certPath, systemCertPath) {
+			enforceSystemCert := ctx.Config().EnforceSystemCertificate()
+			whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
+
+			if enforceSystemCert && !inList(a.Module.Name(), whitelist) {
+				ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
+			}
+		}
+	}
+
 	a.outputFile = packageFile
 
 	bundleFile := android.PathForModuleOut(ctx, "base.zip")
diff --git a/java/dexpreopt.go b/java/dexpreopt.go
index ae8d369..2c8de55 100644
--- a/java/dexpreopt.go
+++ b/java/dexpreopt.go
@@ -28,10 +28,11 @@
 type dexpreopter struct {
 	dexpreoptProperties DexpreoptProperties
 
-	installPath  android.OutputPath
-	isPrivApp    bool
-	isSDKLibrary bool
-	isTest       bool
+	installPath     android.OutputPath
+	uncompressedDex bool
+	isSDKLibrary    bool
+	isTest          bool
+	isInstallable   bool
 
 	builtInstalled []string
 }
@@ -42,6 +43,9 @@
 		// true.
 		Enabled *bool
 
+		// If true, never strip the dex files from the final jar when dexpreopting.  Defaults to false.
+		No_stripping *bool
+
 		// If true, generate an app image (.art file) for this module.
 		App_image *bool
 
@@ -74,6 +78,10 @@
 		return true
 	}
 
+	if !d.isInstallable {
+		return true
+	}
+
 	// TODO: contains no java code
 
 	return false
@@ -140,21 +148,15 @@
 		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(),
-		PreferIntegrity: false,
-		UncompressedDex: uncompressedDex,
-		HasApkLibraries: false,
-		PreoptFlags:     nil,
+		Name:                ctx.ModuleName(),
+		DexLocation:         dexLocation,
+		BuildPath:           android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").String(),
+		DexPath:             dexJarFile.String(),
+		PreferCodeIntegrity: false,
+		UncompressedDex:     d.uncompressedDex,
+		HasApkLibraries:     false,
+		PreoptFlags:         nil,
 
 		ProfileClassListing:  profileClassListing.String(),
 		ProfileIsTextListing: profileIsTextListing,
@@ -172,6 +174,7 @@
 		NoCreateAppImage:    !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
 		ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
 
+		NoStripping:     Bool(d.dexpreoptProperties.Dex_preopt.No_stripping),
 		StripInputPath:  dexJarFile.String(),
 		StripOutputPath: strippedDexJarFile.String(),
 	}
diff --git a/java/dexpreopt_test.go b/java/dexpreopt_test.go
new file mode 100644
index 0000000..6838bd2
--- /dev/null
+++ b/java/dexpreopt_test.go
@@ -0,0 +1,145 @@
+// 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 (
+	"testing"
+)
+
+func TestDexpreoptEnabled(t *testing.T) {
+	tests := []struct {
+		name    string
+		bp      string
+		enabled bool
+	}{
+		{
+			name: "app",
+			bp: `
+				android_app {
+					name: "foo",
+					srcs: ["a.java"],
+				}`,
+			enabled: true,
+		},
+		{
+			name: "installable java library",
+			bp: `
+				java_library {
+					name: "foo",
+					installable: true,
+					srcs: ["a.java"],
+				}`,
+			enabled: true,
+		},
+		{
+			name: "java binary",
+			bp: `
+				java_binary {
+					name: "foo",
+					srcs: ["a.java"],
+				}`,
+			enabled: true,
+		},
+
+		{
+			name: "app without sources",
+			bp: `
+				android_app {
+					name: "foo",
+				}`,
+			// TODO(ccross): this should probably be false
+			enabled: true,
+		},
+		{
+			name: "installable java library without sources",
+			bp: `
+				java_library {
+					name: "foo",
+					installable: true,
+				}`,
+			// TODO(ccross): this should probably be false
+			enabled: true,
+		},
+
+		{
+			name: "static java library",
+			bp: `
+				java_library {
+					name: "foo",
+					srcs: ["a.java"],
+				}`,
+			enabled: false,
+		},
+		{
+			name: "java test",
+			bp: `
+				java_test {
+					name: "foo",
+					srcs: ["a.java"],
+				}`,
+			enabled: false,
+		},
+		{
+			name: "android test",
+			bp: `
+				android_test {
+					name: "foo",
+					srcs: ["a.java"],
+				}`,
+			enabled: false,
+		},
+		{
+			name: "android test helper app",
+			bp: `
+				android_test_helper_app {
+					name: "foo",
+					srcs: ["a.java"],
+				}`,
+			enabled: false,
+		},
+		{
+			name: "compile_dex",
+			bp: `
+				java_library {
+					name: "foo",
+					srcs: ["a.java"],
+					compile_dex: true,
+				}`,
+			enabled: false,
+		},
+	}
+
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			ctx := testJava(t, test.bp)
+
+			dexpreopt := ctx.ModuleForTests("foo", "android_common").MaybeDescription("dexpreopt")
+			enabled := dexpreopt.Rule != nil
+
+			if enabled != test.enabled {
+				t.Fatalf("want dexpreopt %s, got %s", enabledString(test.enabled), enabledString(enabled))
+			}
+		})
+
+	}
+}
+
+func enabledString(enabled bool) string {
+	if enabled {
+		return "enabled"
+	} else {
+		return "disabled"
+	}
+}
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 6eae05c..fa4aee4 100644
--- a/java/java.go
+++ b/java/java.go
@@ -430,154 +430,6 @@
 	return j.sdkVersion()
 }
 
-type sdkContext interface {
-	// sdkVersion eturns the sdk_version property of the current module, or an empty string if it is not set.
-	sdkVersion() string
-	// minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
-	minSdkVersion() string
-	// targetSdkVersion returns the target_sdk_version property of the current module, or sdkVersion() if it is not set.
-	targetSdkVersion() string
-}
-
-func sdkVersionOrDefault(ctx android.BaseContext, v string) string {
-	switch v {
-	case "", "current", "system_current", "test_current", "core_current":
-		return ctx.Config().DefaultAppTargetSdk()
-	default:
-		return v
-	}
-}
-
-// Returns a sdk version as a number.  For modules targeting an unreleased SDK (meaning it does not yet have a number)
-// it returns android.FutureApiLevel (10000).
-func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) {
-	switch v {
-	case "", "current", "test_current", "system_current", "core_current":
-		return ctx.Config().DefaultAppTargetSdkInt(), nil
-	default:
-		n := android.GetNumericSdkVersion(v)
-		if i, err := strconv.Atoi(n); err != nil {
-			return -1, fmt.Errorf("invalid sdk version %q", n)
-		} else {
-			return i, nil
-		}
-	}
-}
-
-func sdkVersionToNumberAsString(ctx android.BaseContext, v string) (string, error) {
-	n, err := sdkVersionToNumber(ctx, v)
-	if err != nil {
-		return "", err
-	}
-	return strconv.Itoa(n), nil
-}
-
-func decodeSdkDep(ctx android.BaseContext, sdkContext sdkContext) sdkDep {
-	v := sdkContext.sdkVersion()
-	i, err := sdkVersionToNumber(ctx, v)
-	if err != nil {
-		ctx.PropertyErrorf("sdk_version", "%s", err)
-		return sdkDep{}
-	}
-
-	// Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
-	// or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
-	if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
-		allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
-		if ctx.DeviceSpecific() || ctx.SocSpecific() {
-			if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
-				allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
-			}
-		}
-		version := strings.TrimPrefix(v, "system_")
-		if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
-			ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
-				v, allowed_versions)
-		}
-	}
-
-	toPrebuilt := func(sdk string) sdkDep {
-		var api, v string
-		if strings.Contains(sdk, "_") {
-			t := strings.Split(sdk, "_")
-			api = t[0]
-			v = t[1]
-		} else {
-			api = "public"
-			v = sdk
-		}
-		dir := filepath.Join("prebuilts", "sdk", v, api)
-		jar := filepath.Join(dir, "android.jar")
-		// There's no aidl for other SDKs yet.
-		// TODO(77525052): Add aidl files for other SDKs too.
-		public_dir := filepath.Join("prebuilts", "sdk", v, "public")
-		aidl := filepath.Join(public_dir, "framework.aidl")
-		jarPath := android.ExistentPathForSource(ctx, jar)
-		aidlPath := android.ExistentPathForSource(ctx, aidl)
-		lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
-
-		if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
-			return sdkDep{
-				invalidVersion: true,
-				modules:        []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
-			}
-		}
-
-		if !jarPath.Valid() {
-			ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
-			return sdkDep{}
-		}
-
-		if !aidlPath.Valid() {
-			ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
-			return sdkDep{}
-		}
-
-		return sdkDep{
-			useFiles: true,
-			jars:     android.Paths{jarPath.Path(), lambdaStubsPath},
-			aidl:     aidlPath.Path(),
-		}
-	}
-
-	toModule := func(m, r string) sdkDep {
-		ret := sdkDep{
-			useModule:          true,
-			modules:            []string{m, config.DefaultLambdaStubsLibrary},
-			systemModules:      m + "_system_modules",
-			frameworkResModule: r,
-		}
-		if m == "core.current.stubs" {
-			ret.systemModules = "core-system-modules"
-		} else if m == "core.platform.api.stubs" {
-			ret.systemModules = "core-platform-api-stubs-system-modules"
-		}
-		return ret
-	}
-
-	if ctx.Config().UnbundledBuildPrebuiltSdks() && v != "" {
-		return toPrebuilt(v)
-	}
-
-	switch v {
-	case "":
-		return sdkDep{
-			useDefaultLibs:     true,
-			frameworkResModule: "framework-res",
-		}
-	case "current":
-		return toModule("android_stubs_current", "framework-res")
-	case "system_current":
-		return toModule("android_system_stubs_current", "framework-res")
-	case "test_current":
-		return toModule("android_test_stubs_current", "framework-res")
-	case "core_current":
-		return toModule("core.current.stubs", "")
-	default:
-		return toPrebuilt(v)
-	}
-}
-
 func (j *Module) deps(ctx android.BottomUpMutatorContext) {
 	if ctx.Device() {
 		if !Bool(j.properties.No_standard_libs) {
@@ -925,7 +777,18 @@
 
 func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
 	var ret string
-	sdk, err := sdkVersionToNumber(ctx, sdkContext.sdkVersion())
+	v := sdkContext.sdkVersion()
+	// For PDK builds, use the latest SDK version instead of "current"
+	if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
+		sdkVersions := ctx.Config().Get(sdkSingletonKey).([]int)
+		latestSdkVersion := 0
+		if len(sdkVersions) > 0 {
+			latestSdkVersion = sdkVersions[len(sdkVersions)-1]
+		}
+		v = strconv.Itoa(latestSdkVersion)
+	}
+
+	sdk, err := sdkVersionToNumber(ctx, v)
 	if err != nil {
 		ctx.PropertyErrorf("sdk_version", "%s", err)
 	}
@@ -1011,8 +874,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
@@ -1319,6 +1189,8 @@
 
 		j.dexJarFile = dexOutputFile
 
+		j.dexpreopter.isInstallable = Bool(j.properties.Installable)
+		j.dexpreopter.uncompressedDex = j.deviceProperties.UncompressDex
 		dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
 
 		j.maybeStrippedDexJarFile = dexOutputFile
@@ -1420,13 +1292,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}
 }
 
@@ -1438,14 +1316,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
 }
 
@@ -1480,9 +1363,14 @@
 	Module
 }
 
+func (j *Library) shouldUncompressDex(ctx android.ModuleContext) bool {
+	return false
+}
+
 func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
 	j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
 	j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
+	j.deviceProperties.UncompressDex = j.shouldUncompressDex(ctx)
 	j.compile(ctx)
 
 	if Bool(j.properties.Installable) || ctx.Host() {
@@ -1583,6 +1471,7 @@
 		&module.testProperties)
 
 	module.Module.properties.Installable = proptools.BoolPtr(true)
+	module.Module.dexpreopter.isTest = true
 
 	InitJavaModule(module, android.HostAndDeviceSupported)
 	return module
@@ -1805,10 +1694,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}
 }
 
@@ -1817,6 +1712,9 @@
 }
 
 func (j *Import) ImplementationAndResourcesJars() android.Paths {
+	if j.combinedClasspathFile == nil {
+		return nil
+	}
 	return android.Paths{j.combinedClasspathFile}
 }
 
@@ -1828,6 +1726,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_"
diff --git a/java/java_test.go b/java/java_test.go
index 4d4b836..570efb7 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -19,13 +19,10 @@
 	"io/ioutil"
 	"os"
 	"path/filepath"
-	"reflect"
 	"strconv"
 	"strings"
 	"testing"
 
-	"github.com/google/blueprint/proptools"
-
 	"android/soong/android"
 	"android/soong/cc"
 	"android/soong/genrule"
@@ -76,9 +73,12 @@
 	ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
 	ctx.RegisterModuleType("android_library", android.ModuleFactoryAdaptor(AndroidLibraryFactory))
 	ctx.RegisterModuleType("android_test", android.ModuleFactoryAdaptor(AndroidTestFactory))
+	ctx.RegisterModuleType("android_test_helper_app", android.ModuleFactoryAdaptor(AndroidTestHelperAppFactory))
+	ctx.RegisterModuleType("java_binary", android.ModuleFactoryAdaptor(BinaryFactory))
 	ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory))
 	ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory))
 	ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
+	ctx.RegisterModuleType("java_test", android.ModuleFactoryAdaptor(TestFactory))
 	ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
 	ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
 	ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
@@ -98,6 +98,7 @@
 		ctx.TopDown("java_sdk_library", sdkLibraryMutator).Parallel()
 	})
 	ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
+	ctx.RegisterPreSingletonType("sdk", android.SingletonFactoryAdaptor(sdkSingletonFactory))
 
 	// Register module types and mutators from cc needed for JNI testing
 	ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
@@ -181,6 +182,9 @@
 		"prebuilts/sdk/14/public/android.jar":         nil,
 		"prebuilts/sdk/14/public/framework.aidl":      nil,
 		"prebuilts/sdk/14/system/android.jar":         nil,
+		"prebuilts/sdk/17/public/android.jar":         nil,
+		"prebuilts/sdk/17/public/framework.aidl":      nil,
+		"prebuilts/sdk/17/system/android.jar":         nil,
 		"prebuilts/sdk/current/core/android.jar":      nil,
 		"prebuilts/sdk/current/public/android.jar":    nil,
 		"prebuilts/sdk/current/public/framework.aidl": nil,
@@ -351,241 +355,6 @@
 
 }
 
-var classpathTestcases = []struct {
-	name          string
-	unbundled     bool
-	moduleType    string
-	host          android.OsClass
-	properties    string
-	bootclasspath []string
-	system        string
-	classpath     []string
-}{
-	{
-		name:          "default",
-		bootclasspath: []string{"core.platform.api.stubs", "core-lambda-stubs"},
-		system:        "core-platform-api-stubs-system-modules",
-		classpath:     []string{"ext", "framework"},
-	},
-	{
-		name:          "blank sdk version",
-		properties:    `sdk_version: "",`,
-		bootclasspath: []string{"core.platform.api.stubs", "core-lambda-stubs"},
-		system:        "core-platform-api-stubs-system-modules",
-		classpath:     []string{"ext", "framework"},
-	},
-	{
-
-		name:          "sdk v14",
-		properties:    `sdk_version: "14",`,
-		bootclasspath: []string{`""`},
-		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
-		classpath:     []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
-	},
-	{
-
-		name:          "current",
-		properties:    `sdk_version: "current",`,
-		bootclasspath: []string{"android_stubs_current", "core-lambda-stubs"},
-		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
-	},
-	{
-
-		name:          "system_current",
-		properties:    `sdk_version: "system_current",`,
-		bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"},
-		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
-	},
-	{
-
-		name:          "system_14",
-		properties:    `sdk_version: "system_14",`,
-		bootclasspath: []string{`""`},
-		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
-		classpath:     []string{"prebuilts/sdk/14/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
-	},
-	{
-
-		name:          "test_current",
-		properties:    `sdk_version: "test_current",`,
-		bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"},
-		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
-	},
-	{
-
-		name:          "core_current",
-		properties:    `sdk_version: "core_current",`,
-		bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"},
-		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
-	},
-	{
-
-		name:          "nostdlib",
-		properties:    `no_standard_libs: true, system_modules: "none"`,
-		system:        "none",
-		bootclasspath: []string{`""`},
-		classpath:     []string{},
-	},
-	{
-
-		name:          "nostdlib system_modules",
-		properties:    `no_standard_libs: true, system_modules: "core-platform-api-stubs-system-modules"`,
-		system:        "core-platform-api-stubs-system-modules",
-		bootclasspath: []string{`""`},
-		classpath:     []string{},
-	},
-	{
-
-		name:          "host default",
-		moduleType:    "java_library_host",
-		properties:    ``,
-		host:          android.Host,
-		bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
-		classpath:     []string{},
-	},
-	{
-		name:       "host nostdlib",
-		moduleType: "java_library_host",
-		host:       android.Host,
-		properties: `no_standard_libs: true`,
-		classpath:  []string{},
-	},
-	{
-
-		name:          "host supported default",
-		host:          android.Host,
-		properties:    `host_supported: true,`,
-		classpath:     []string{},
-		bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
-	},
-	{
-		name:       "host supported nostdlib",
-		host:       android.Host,
-		properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
-		classpath:  []string{},
-	},
-	{
-
-		name:          "unbundled sdk v14",
-		unbundled:     true,
-		properties:    `sdk_version: "14",`,
-		bootclasspath: []string{`""`},
-		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
-		classpath:     []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
-	},
-	{
-
-		name:          "unbundled current",
-		unbundled:     true,
-		properties:    `sdk_version: "current",`,
-		bootclasspath: []string{`""`},
-		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
-		classpath:     []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
-	},
-}
-
-func TestClasspath(t *testing.T) {
-	for _, testcase := range classpathTestcases {
-		t.Run(testcase.name, func(t *testing.T) {
-			moduleType := "java_library"
-			if testcase.moduleType != "" {
-				moduleType = testcase.moduleType
-			}
-
-			bp := moduleType + ` {
-				name: "foo",
-				srcs: ["a.java"],
-				` + testcase.properties + `
-			}`
-
-			variant := "android_common"
-			if testcase.host == android.Host {
-				variant = android.BuildOs.String() + "_common"
-			}
-
-			convertModulesToPaths := func(cp []string) []string {
-				ret := make([]string, len(cp))
-				for i, e := range cp {
-					ret[i] = moduleToPath(e)
-				}
-				return ret
-			}
-
-			bootclasspath := convertModulesToPaths(testcase.bootclasspath)
-			classpath := convertModulesToPaths(testcase.classpath)
-
-			bc := strings.Join(bootclasspath, ":")
-			if bc != "" {
-				bc = "-bootclasspath " + bc
-			}
-
-			c := strings.Join(classpath, ":")
-			if c != "" {
-				c = "-classpath " + c
-			}
-			system := ""
-			if testcase.system == "none" {
-				system = "--system=none"
-			} else if testcase.system != "" {
-				system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
-			}
-
-			t.Run("1.8", func(t *testing.T) {
-				// Test default javac 1.8
-				config := testConfig(nil)
-				if testcase.unbundled {
-					config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
-				}
-				ctx := testContext(config, bp, nil)
-				run(t, ctx, config)
-
-				javac := ctx.ModuleForTests("foo", variant).Rule("javac")
-
-				got := javac.Args["bootClasspath"]
-				if got != bc {
-					t.Errorf("bootclasspath expected %q != got %q", bc, got)
-				}
-
-				got = javac.Args["classpath"]
-				if got != c {
-					t.Errorf("classpath expected %q != got %q", c, got)
-				}
-
-				var deps []string
-				if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
-					deps = append(deps, bootclasspath...)
-				}
-				deps = append(deps, classpath...)
-
-				if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
-					t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
-				}
-			})
-
-			// Test again with javac 1.9
-			t.Run("1.9", func(t *testing.T) {
-				config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
-				if testcase.unbundled {
-					config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
-				}
-				ctx := testContext(config, bp, nil)
-				run(t, ctx, config)
-
-				javac := ctx.ModuleForTests("foo", variant).Rule("javac")
-				got := javac.Args["bootClasspath"]
-				expected := system
-				if testcase.system == "bootclasspath" {
-					expected = bc
-				}
-				if got != expected {
-					t.Errorf("bootclasspath expected %q != got %q", expected, got)
-				}
-			})
-		})
-	}
-
-}
-
 func TestPrebuilts(t *testing.T) {
 	ctx := testJava(t, `
 		java_library {
@@ -1185,3 +954,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.go b/java/sdk.go
new file mode 100644
index 0000000..988610f
--- /dev/null
+++ b/java/sdk.go
@@ -0,0 +1,218 @@
+// Copyright 2019 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 (
+	"android/soong/android"
+	"android/soong/java/config"
+	"fmt"
+	"path/filepath"
+	"sort"
+	"strconv"
+	"strings"
+)
+
+func init() {
+	android.RegisterPreSingletonType("sdk", sdkSingletonFactory)
+}
+
+const sdkSingletonKey = "sdkSingletonKey"
+
+type sdkContext interface {
+	// sdkVersion eturns the sdk_version property of the current module, or an empty string if it is not set.
+	sdkVersion() string
+	// minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
+	minSdkVersion() string
+	// targetSdkVersion returns the target_sdk_version property of the current module, or sdkVersion() if it is not set.
+	targetSdkVersion() string
+}
+
+func sdkVersionOrDefault(ctx android.BaseContext, v string) string {
+	switch v {
+	case "", "current", "system_current", "test_current", "core_current":
+		return ctx.Config().DefaultAppTargetSdk()
+	default:
+		return v
+	}
+}
+
+// Returns a sdk version as a number.  For modules targeting an unreleased SDK (meaning it does not yet have a number)
+// it returns android.FutureApiLevel (10000).
+func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) {
+	switch v {
+	case "", "current", "test_current", "system_current", "core_current":
+		return ctx.Config().DefaultAppTargetSdkInt(), nil
+	default:
+		n := android.GetNumericSdkVersion(v)
+		if i, err := strconv.Atoi(n); err != nil {
+			return -1, fmt.Errorf("invalid sdk version %q", n)
+		} else {
+			return i, nil
+		}
+	}
+}
+
+func sdkVersionToNumberAsString(ctx android.BaseContext, v string) (string, error) {
+	n, err := sdkVersionToNumber(ctx, v)
+	if err != nil {
+		return "", err
+	}
+	return strconv.Itoa(n), nil
+}
+
+func decodeSdkDep(ctx android.BaseContext, sdkContext sdkContext) sdkDep {
+	v := sdkContext.sdkVersion()
+	// For PDK builds, use the latest SDK version instead of "current"
+	if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
+		sdkVersions := ctx.Config().Get(sdkSingletonKey).([]int)
+		latestSdkVersion := 0
+		if len(sdkVersions) > 0 {
+			latestSdkVersion = sdkVersions[len(sdkVersions)-1]
+		}
+		v = strconv.Itoa(latestSdkVersion)
+	}
+
+	i, err := sdkVersionToNumber(ctx, v)
+	if err != nil {
+		ctx.PropertyErrorf("sdk_version", "%s", err)
+		return sdkDep{}
+	}
+
+	toPrebuilt := func(sdk string) sdkDep {
+		var api, v string
+		if strings.Contains(sdk, "_") {
+			t := strings.Split(sdk, "_")
+			api = t[0]
+			v = t[1]
+		} else {
+			api = "public"
+			v = sdk
+		}
+		dir := filepath.Join("prebuilts", "sdk", v, api)
+		jar := filepath.Join(dir, "android.jar")
+		// There's no aidl for other SDKs yet.
+		// TODO(77525052): Add aidl files for other SDKs too.
+		public_dir := filepath.Join("prebuilts", "sdk", v, "public")
+		aidl := filepath.Join(public_dir, "framework.aidl")
+		jarPath := android.ExistentPathForSource(ctx, jar)
+		aidlPath := android.ExistentPathForSource(ctx, aidl)
+		lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
+
+		if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
+			return sdkDep{
+				invalidVersion: true,
+				modules:        []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
+			}
+		}
+
+		if !jarPath.Valid() {
+			ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
+			return sdkDep{}
+		}
+
+		if !aidlPath.Valid() {
+			ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
+			return sdkDep{}
+		}
+
+		return sdkDep{
+			useFiles: true,
+			jars:     android.Paths{jarPath.Path(), lambdaStubsPath},
+			aidl:     aidlPath.Path(),
+		}
+	}
+
+	toModule := func(m, r string) sdkDep {
+		ret := sdkDep{
+			useModule:          true,
+			modules:            []string{m, config.DefaultLambdaStubsLibrary},
+			systemModules:      m + "_system_modules",
+			frameworkResModule: r,
+		}
+		if m == "core.current.stubs" {
+			ret.systemModules = "core-system-modules"
+		} else if m == "core.platform.api.stubs" {
+			ret.systemModules = "core-platform-api-stubs-system-modules"
+		}
+		return ret
+	}
+
+	// Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
+	// or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
+	if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
+		allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
+		if ctx.DeviceSpecific() || ctx.SocSpecific() {
+			if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
+				allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
+			}
+		}
+		version := strings.TrimPrefix(v, "system_")
+		if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
+			ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
+				v, allowed_versions)
+		}
+	}
+
+	if ctx.Config().UnbundledBuildPrebuiltSdks() && v != "" {
+		return toPrebuilt(v)
+	}
+
+	switch v {
+	case "":
+		return sdkDep{
+			useDefaultLibs:     true,
+			frameworkResModule: "framework-res",
+		}
+	case "current":
+		return toModule("android_stubs_current", "framework-res")
+	case "system_current":
+		return toModule("android_system_stubs_current", "framework-res")
+	case "test_current":
+		return toModule("android_test_stubs_current", "framework-res")
+	case "core_current":
+		return toModule("core.current.stubs", "")
+	default:
+		return toPrebuilt(v)
+	}
+}
+
+func sdkSingletonFactory() android.Singleton {
+	return sdkSingleton{}
+}
+
+type sdkSingleton struct{}
+
+func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
+	sdkJars, err := ctx.GlobWithDeps("prebuilts/sdk/*/public/android.jar", nil)
+	if err != nil {
+		ctx.Errorf("failed to glob prebuilts/sdk/*/public/android.jar: %s", err.Error())
+	}
+
+	var sdkVersions []int
+	for _, sdkJar := range sdkJars {
+		dir := filepath.Base(filepath.Dir(filepath.Dir(sdkJar)))
+		v, err := strconv.Atoi(dir)
+		if scerr, ok := err.(*strconv.NumError); ok && scerr.Err == strconv.ErrSyntax {
+			continue
+		} else if err != nil {
+			ctx.Errorf("invalid sdk jar %q, %s, %v", sdkJar, err.Error())
+		}
+		sdkVersions = append(sdkVersions, v)
+	}
+
+	sort.Ints(sdkVersions)
+
+	ctx.Config().Once(sdkSingletonKey, func() interface{} { return sdkVersions })
+}
diff --git a/java/sdk_library.go b/java/sdk_library.go
index 877abe4..55a9590 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -124,6 +124,12 @@
 	// This applies to the stubs lib.
 	Compile_dex *bool
 
+	// the java library (in classpath) for documentation that provides java srcs and srcjars.
+	Srcs_lib *string
+
+	// the base dirs under srcs_lib will be scanned for java srcs.
+	Srcs_lib_whitelist_dirs []string
+
 	// the sub dirs under srcs_lib_whitelist_dirs will be scanned for java srcs.
 	// Defaults to "android.annotation".
 	Srcs_lib_whitelist_pkgs []string
@@ -541,6 +547,10 @@
 		} else {
 			props.Srcs_lib_whitelist_pkgs = []string{"android.annotation"}
 		}
+	} else {
+		props.Srcs_lib = module.properties.Srcs_lib
+		props.Srcs_lib_whitelist_dirs = module.properties.Srcs_lib_whitelist_dirs
+		props.Srcs_lib_whitelist_pkgs = module.properties.Srcs_lib_whitelist_pkgs
 	}
 
 	if Bool(module.properties.Metalava_enabled) == true {
diff --git a/java/sdk_test.go b/java/sdk_test.go
new file mode 100644
index 0000000..6924e26
--- /dev/null
+++ b/java/sdk_test.go
@@ -0,0 +1,291 @@
+// Copyright 2019 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 (
+	"android/soong/android"
+	"path/filepath"
+	"reflect"
+	"strings"
+	"testing"
+
+	"github.com/google/blueprint/proptools"
+)
+
+var classpathTestcases = []struct {
+	name          string
+	unbundled     bool
+	pdk           bool
+	moduleType    string
+	host          android.OsClass
+	properties    string
+	bootclasspath []string
+	system        string
+	classpath     []string
+}{
+	{
+		name:          "default",
+		bootclasspath: []string{"core.platform.api.stubs", "core-lambda-stubs"},
+		system:        "core-platform-api-stubs-system-modules",
+		classpath:     []string{"ext", "framework"},
+	},
+	{
+		name:          "blank sdk version",
+		properties:    `sdk_version: "",`,
+		bootclasspath: []string{"core.platform.api.stubs", "core-lambda-stubs"},
+		system:        "core-platform-api-stubs-system-modules",
+		classpath:     []string{"ext", "framework"},
+	},
+	{
+
+		name:          "sdk v14",
+		properties:    `sdk_version: "14",`,
+		bootclasspath: []string{`""`},
+		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
+		classpath:     []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
+	},
+	{
+
+		name:          "current",
+		properties:    `sdk_version: "current",`,
+		bootclasspath: []string{"android_stubs_current", "core-lambda-stubs"},
+		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
+	},
+	{
+
+		name:          "system_current",
+		properties:    `sdk_version: "system_current",`,
+		bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"},
+		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
+	},
+	{
+
+		name:          "system_14",
+		properties:    `sdk_version: "system_14",`,
+		bootclasspath: []string{`""`},
+		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
+		classpath:     []string{"prebuilts/sdk/14/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
+	},
+	{
+
+		name:          "test_current",
+		properties:    `sdk_version: "test_current",`,
+		bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"},
+		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
+	},
+	{
+
+		name:          "core_current",
+		properties:    `sdk_version: "core_current",`,
+		bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"},
+		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
+	},
+	{
+
+		name:          "nostdlib",
+		properties:    `no_standard_libs: true, system_modules: "none"`,
+		system:        "none",
+		bootclasspath: []string{`""`},
+		classpath:     []string{},
+	},
+	{
+
+		name:          "nostdlib system_modules",
+		properties:    `no_standard_libs: true, system_modules: "core-platform-api-stubs-system-modules"`,
+		system:        "core-platform-api-stubs-system-modules",
+		bootclasspath: []string{`""`},
+		classpath:     []string{},
+	},
+	{
+
+		name:          "host default",
+		moduleType:    "java_library_host",
+		properties:    ``,
+		host:          android.Host,
+		bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
+		classpath:     []string{},
+	},
+	{
+		name:       "host nostdlib",
+		moduleType: "java_library_host",
+		host:       android.Host,
+		properties: `no_standard_libs: true`,
+		classpath:  []string{},
+	},
+	{
+
+		name:          "host supported default",
+		host:          android.Host,
+		properties:    `host_supported: true,`,
+		classpath:     []string{},
+		bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
+	},
+	{
+		name:       "host supported nostdlib",
+		host:       android.Host,
+		properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
+		classpath:  []string{},
+	},
+	{
+
+		name:          "unbundled sdk v14",
+		unbundled:     true,
+		properties:    `sdk_version: "14",`,
+		bootclasspath: []string{`""`},
+		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
+		classpath:     []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
+	},
+	{
+
+		name:          "unbundled current",
+		unbundled:     true,
+		properties:    `sdk_version: "current",`,
+		bootclasspath: []string{`""`},
+		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
+		classpath:     []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
+	},
+
+	{
+		name:          "pdk default",
+		pdk:           true,
+		bootclasspath: []string{`""`},
+		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
+		classpath:     []string{"prebuilts/sdk/17/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
+	},
+	{
+		name:          "pdk current",
+		pdk:           true,
+		properties:    `sdk_version: "current",`,
+		bootclasspath: []string{`""`},
+		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
+		classpath:     []string{"prebuilts/sdk/17/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
+	},
+	{
+		name:          "pdk 14",
+		pdk:           true,
+		properties:    `sdk_version: "14",`,
+		bootclasspath: []string{`""`},
+		system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
+		classpath:     []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
+	},
+}
+
+func TestClasspath(t *testing.T) {
+	for _, testcase := range classpathTestcases {
+		t.Run(testcase.name, func(t *testing.T) {
+			moduleType := "java_library"
+			if testcase.moduleType != "" {
+				moduleType = testcase.moduleType
+			}
+
+			bp := moduleType + ` {
+				name: "foo",
+				srcs: ["a.java"],
+				` + testcase.properties + `
+			}`
+
+			variant := "android_common"
+			if testcase.host == android.Host {
+				variant = android.BuildOs.String() + "_common"
+			}
+
+			convertModulesToPaths := func(cp []string) []string {
+				ret := make([]string, len(cp))
+				for i, e := range cp {
+					ret[i] = moduleToPath(e)
+				}
+				return ret
+			}
+
+			bootclasspath := convertModulesToPaths(testcase.bootclasspath)
+			classpath := convertModulesToPaths(testcase.classpath)
+
+			bc := strings.Join(bootclasspath, ":")
+			if bc != "" {
+				bc = "-bootclasspath " + bc
+			}
+
+			c := strings.Join(classpath, ":")
+			if c != "" {
+				c = "-classpath " + c
+			}
+			system := ""
+			if testcase.system == "none" {
+				system = "--system=none"
+			} else if testcase.system != "" {
+				system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
+			}
+
+			t.Run("1.8", func(t *testing.T) {
+				// Test default javac 1.8
+				config := testConfig(nil)
+				if testcase.unbundled {
+					config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
+				}
+				if testcase.pdk {
+					config.TestProductVariables.Pdk = proptools.BoolPtr(true)
+				}
+				ctx := testContext(config, bp, nil)
+				run(t, ctx, config)
+
+				javac := ctx.ModuleForTests("foo", variant).Rule("javac")
+
+				got := javac.Args["bootClasspath"]
+				if got != bc {
+					t.Errorf("bootclasspath expected %q != got %q", bc, got)
+				}
+
+				got = javac.Args["classpath"]
+				if got != c {
+					t.Errorf("classpath expected %q != got %q", c, got)
+				}
+
+				var deps []string
+				if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
+					deps = append(deps, bootclasspath...)
+				}
+				deps = append(deps, classpath...)
+
+				if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
+					t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
+				}
+			})
+
+			// Test again with javac 1.9
+			t.Run("1.9", func(t *testing.T) {
+				config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
+				if testcase.unbundled {
+					config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
+				}
+				if testcase.pdk {
+					config.TestProductVariables.Pdk = proptools.BoolPtr(true)
+				}
+				ctx := testContext(config, bp, nil)
+				run(t, ctx, config)
+
+				javac := ctx.ModuleForTests("foo", variant).Rule("javac")
+				got := javac.Args["bootClasspath"]
+				expected := system
+				if testcase.system == "bootclasspath" {
+					expected = bc
+				}
+				if got != expected {
+					t.Errorf("bootclasspath expected %q != got %q", expected, got)
+				}
+			})
+		})
+	}
+
+}
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..1ddaf68 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",
@@ -46,6 +47,7 @@
         "environment.go",
         "exec.go",
         "finder.go",
+        "goma.go",
         "kati.go",
         "ninja.go",
         "path.go",
diff --git a/ui/build/build.go b/ui/build/build.go
index c902a0f..0ae06d6 100644
--- a/ui/build/build.go
+++ b/ui/build/build.go
@@ -156,6 +156,11 @@
 
 	SetupPath(ctx, config)
 
+	if config.StartGoma() {
+		// Ensure start Goma compiler_proxy
+		startGoma(ctx, config)
+	}
+
 	if what&BuildProductConfig != 0 {
 		// Run make for product config
 		runMakeProductConfig(ctx, config)
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..64270f8 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)
@@ -475,6 +478,20 @@
 	return false
 }
 
+func (c *configImpl) StartGoma() bool {
+	if !c.UseGoma() {
+		return false
+	}
+
+	if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
+		v = strings.TrimSpace(v)
+		if v != "" && v != "false" {
+			return false
+		}
+	}
+	return true
+}
+
 // RemoteParallel controls how many remote jobs (i.e., commands which contain
 // gomacc) are run in parallel.  Note the parallelism of all other jobs is
 // still limited by Parallel()
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/goma.go b/ui/build/goma.go
new file mode 100644
index 0000000..015a7c7
--- /dev/null
+++ b/ui/build/goma.go
@@ -0,0 +1,79 @@
+// 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 build
+
+import (
+	"fmt"
+	"math"
+	"path/filepath"
+	"strconv"
+	"strings"
+
+	"android/soong/ui/metrics"
+)
+
+const gomaCtlScript = "goma_ctl.py"
+const gomaLeastNProcs = 2500
+const gomaLeastNFiles = 16000
+
+// ulimit returns ulimit result for |opt|.
+// if the resource is unlimited, it returns math.MaxInt32 so that a caller do
+// not need special handling of the returned value.
+//
+// Note that since go syscall package do not have RLIMIT_NPROC constant,
+// we use bash ulimit instead.
+func ulimitOrFatal(ctx Context, config Config, opt string) int {
+	commandText := fmt.Sprintf("ulimit %s", opt)
+	cmd := Command(ctx, config, commandText, "bash", "-c", commandText)
+	output := strings.TrimRight(string(cmd.CombinedOutputOrFatal()), "\n")
+	ctx.Verbose(output + "\n")
+	ctx.Verbose("done\n")
+
+	if output == "unlimited" {
+		return math.MaxInt32
+	}
+	num, err := strconv.Atoi(output)
+	if err != nil {
+		ctx.Fatalf("ulimit returned unexpected value: %s: %v\n", opt, err)
+	}
+	return num
+}
+
+func startGoma(ctx Context, config Config) {
+	ctx.BeginTrace(metrics.RunSetupTool, "goma_ctl")
+	defer ctx.EndTrace()
+
+	if u := ulimitOrFatal(ctx, config, "-u"); u < gomaLeastNProcs {
+		ctx.Fatalf("max user processes is insufficient: %d; want >= %d.\n", u, gomaLeastNProcs)
+	}
+	if n := ulimitOrFatal(ctx, config, "-n"); n < gomaLeastNFiles {
+		ctx.Fatalf("max open files is insufficient: %d; want >= %d.\n", n, gomaLeastNFiles)
+	}
+
+	var gomaCtl string
+	if gomaDir, ok := config.Environment().Get("GOMA_DIR"); ok {
+		gomaCtl = filepath.Join(gomaDir, gomaCtlScript)
+	} else if home, ok := config.Environment().Get("HOME"); ok {
+		gomaCtl = filepath.Join(home, "goma", gomaCtlScript)
+	} else {
+		ctx.Fatalln("goma_ctl.py not found")
+	}
+
+	cmd := Command(ctx, config, "goma_ctl.py ensure_start", gomaCtl, "ensure_start")
+
+	if err := cmd.Run(); err != nil {
+		ctx.Fatalf("goma_ctl.py ensure_start failed with: %v\n", err)
+	}
+}
diff --git a/ui/build/kati.go b/ui/build/kati.go
index d2e9fe9..439c928 100644
--- a/ui/build/kati.go
+++ b/ui/build/kati.go
@@ -18,9 +18,12 @@
 	"crypto/md5"
 	"fmt"
 	"io/ioutil"
+	"os"
+	"os/user"
 	"path/filepath"
 	"strings"
 
+	"android/soong/ui/metrics"
 	"android/soong/ui/status"
 )
 
@@ -95,13 +98,29 @@
 
 	envFunc(cmd.Environment)
 
+	if _, ok := cmd.Environment.Get("BUILD_USERNAME"); !ok {
+		u, err := user.Current()
+		if err != nil {
+			ctx.Println("Failed to get current user")
+		}
+		cmd.Environment.Set("BUILD_USERNAME", u.Username)
+	}
+
+	if _, ok := cmd.Environment.Get("BUILD_HOSTNAME"); !ok {
+		hostname, err := os.Hostname()
+		if err != nil {
+			ctx.Println("Failed to read hostname")
+		}
+		cmd.Environment.Set("BUILD_HOSTNAME", hostname)
+	}
+
 	cmd.StartOrFatal()
 	status.KatiReader(ctx.Status.StartTool(), pipe)
 	cmd.WaitOrFatal()
 }
 
 func runKatiBuild(ctx Context, config Config) {
-	ctx.BeginTrace("kati build")
+	ctx.BeginTrace(metrics.RunKati, "kati build")
 	defer ctx.EndTrace()
 
 	args := []string{
@@ -137,7 +156,7 @@
 }
 
 func runKatiPackage(ctx Context, config Config) {
-	ctx.BeginTrace("kati package")
+	ctx.BeginTrace(metrics.RunKati, "kati package")
 	defer ctx.EndTrace()
 
 	args := []string{
@@ -178,7 +197,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..835f820 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")
@@ -53,7 +54,9 @@
 
 	args = append(args, "-f", config.CombinedNinjaFile())
 
-	args = append(args, "-w", "dupbuild=err")
+	args = append(args,
+		"-w", "dupbuild=err",
+		"-w", "missingdepfile=err")
 
 	cmd := Command(ctx, config, "ninja", executable, args...)
 	if config.HasKatiSuffix() {
@@ -97,6 +100,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/paths/config.go b/ui/build/paths/config.go
index e2e4368..fdd291e 100644
--- a/ui/build/paths/config.go
+++ b/ui/build/paths/config.go
@@ -78,18 +78,13 @@
 	"bash":      Allowed,
 	"bc":        Allowed,
 	"bzip2":     Allowed,
-	"chmod":     Allowed,
 	"cp":        Allowed,
 	"date":      Allowed,
 	"dd":        Allowed,
 	"diff":      Allowed,
-	"du":        Allowed,
-	"echo":      Allowed,
 	"egrep":     Allowed,
-	"expr":      Allowed,
 	"find":      Allowed,
 	"fuser":     Allowed,
-	"getconf":   Allowed,
 	"getopt":    Allowed,
 	"git":       Allowed,
 	"grep":      Allowed,
@@ -99,12 +94,9 @@
 	"jar":       Allowed,
 	"java":      Allowed,
 	"javap":     Allowed,
-	"ln":        Allowed,
-	"ls":        Allowed,
 	"lsof":      Allowed,
 	"m4":        Allowed,
 	"md5sum":    Allowed,
-	"mktemp":    Allowed,
 	"mv":        Allowed,
 	"openssl":   Allowed,
 	"patch":     Allowed,
@@ -125,7 +117,6 @@
 	"sha256sum": Allowed,
 	"sha512sum": Allowed,
 	"sort":      Allowed,
-	"stat":      Allowed,
 	"tar":       Allowed,
 	"timeout":   Allowed,
 	"tr":        Allowed,
@@ -154,20 +145,29 @@
 	// On linux we'll use the toybox version of these instead
 	"basename": Toybox,
 	"cat":      Toybox,
+	"chmod":    Toybox,
 	"cmp":      Toybox,
 	"comm":     Toybox,
 	"cut":      Toybox,
 	"dirname":  Toybox,
+	"du":       Toybox,
+	"echo":     Toybox,
 	"env":      Toybox,
+	"expr":     Toybox,
 	"head":     Toybox,
+	"getconf":  Toybox,
 	"id":       Toybox,
+	"ln":       Toybox,
+	"ls":       Toybox,
 	"mkdir":    Toybox,
+	"mktemp":   Toybox,
 	"od":       Toybox,
 	"paste":    Toybox,
 	"pwd":      Toybox,
 	"rmdir":    Toybox,
 	"setsid":   Toybox,
 	"sleep":    Toybox,
+	"stat":     Toybox,
 	"tail":     Toybox,
 	"tee":      Toybox,
 	"touch":    Toybox,
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
 				}