Merge "Move libgcc to libcrt.builtins"
diff --git a/Android.bp b/Android.bp
index 2f7b051..ac50166 100644
--- a/Android.bp
+++ b/Android.bp
@@ -134,7 +134,6 @@
         "cc/pgo.go",
         "cc/prebuilt.go",
         "cc/proto.go",
-        "cc/relocation_packer.go",
         "cc/rs.go",
         "cc/sanitize.go",
         "cc/sabi.go",
@@ -236,6 +235,7 @@
         "java/genrule.go",
         "java/jacoco.go",
         "java/java.go",
+        "java/jdeps.go",
         "java/java_resources.go",
         "java/prebuilt_apis.go",
         "java/proto.go",
@@ -246,6 +246,7 @@
     testSrcs: [
         "java/app_test.go",
         "java/java_test.go",
+        "java/jdeps_test.go",
     ],
     pluginFor: ["soong_build"],
 }
diff --git a/README.md b/README.md
index 9f427c4..16d3cce 100644
--- a/README.md
+++ b/README.md
@@ -30,7 +30,7 @@
 all Android.bp files.
 
 For a list of valid module types and their properties see
-[$OUT_DIR/soong/.bootstrap/docs/soong_build.html](https://go/Android.bp).
+[$OUT_DIR/soong/.bootstrap/docs/soong_build.html](http://go/Android.bp).
 
 ### Globs
 
diff --git a/android/androidmk.go b/android/androidmk.go
index 1d11161..5df4a85 100644
--- a/android/androidmk.go
+++ b/android/androidmk.go
@@ -25,6 +25,7 @@
 	"strings"
 
 	"github.com/google/blueprint"
+	"github.com/google/blueprint/bootstrap"
 )
 
 func init() {
@@ -64,13 +65,13 @@
 		return
 	}
 
-	var androidMkModulesList []Module
+	var androidMkModulesList []blueprint.Module
 
-	ctx.VisitAllModules(func(module Module) {
+	ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
 		androidMkModulesList = append(androidMkModulesList, module)
 	})
 
-	sort.Sort(AndroidModulesByName{androidMkModulesList, ctx})
+	sort.Sort(ModulesByName{androidMkModulesList, ctx})
 
 	transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
 	if ctx.Failed() {
@@ -88,7 +89,7 @@
 	})
 }
 
-func translateAndroidMk(ctx SingletonContext, mkFile string, mods []Module) error {
+func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
 	buf := &bytes.Buffer{}
 
 	fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
@@ -101,8 +102,8 @@
 			return err
 		}
 
-		if ctx.PrimaryModule(mod) == mod {
-			type_stats[ctx.ModuleType(mod)] += 1
+		if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
+			type_stats[ctx.ModuleType(amod)] += 1
 		}
 	}
 
@@ -141,10 +142,36 @@
 }
 
 func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
-	provider, ok := mod.(AndroidMkDataProvider)
-	if !ok {
+	defer func() {
+		if r := recover(); r != nil {
+			panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
+				r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
+		}
+	}()
+
+	switch x := mod.(type) {
+	case AndroidMkDataProvider:
+		return translateAndroidModule(ctx, w, mod, x)
+	case bootstrap.GoBinaryTool:
+		return translateGoBinaryModule(ctx, w, mod, x)
+	default:
 		return nil
 	}
+}
+
+func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
+	goBinary bootstrap.GoBinaryTool) error {
+
+	name := ctx.ModuleName(mod)
+	fmt.Fprintln(w, ".PHONY:", name)
+	fmt.Fprintln(w, name+":", goBinary.InstallPath())
+	fmt.Fprintln(w, "")
+
+	return nil
+}
+
+func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
+	provider AndroidMkDataProvider) error {
 
 	name := provider.BaseModuleName()
 	amod := mod.(Module).base()
diff --git a/android/arch.go b/android/arch.go
index 6971bc8..57899cd 100644
--- a/android/arch.go
+++ b/android/arch.go
@@ -959,6 +959,7 @@
 		{"arm", "armv7-a-neon", "cortex-a15", []string{"armeabi-v7a"}},
 		{"arm", "armv7-a-neon", "cortex-a53", []string{"armeabi-v7a"}},
 		{"arm", "armv7-a-neon", "cortex-a53.a57", []string{"armeabi-v7a"}},
+		{"arm", "armv7-a-neon", "cortex-a72", []string{"armeabi-v7a"}},
 		{"arm", "armv7-a-neon", "cortex-a73", []string{"armeabi-v7a"}},
 		{"arm", "armv7-a-neon", "cortex-a75", []string{"armeabi-v7a"}},
 		{"arm", "armv7-a-neon", "denver", []string{"armeabi-v7a"}},
@@ -967,6 +968,7 @@
 		{"arm", "armv7-a-neon", "exynos-m1", []string{"armeabi-v7a"}},
 		{"arm", "armv7-a-neon", "exynos-m2", []string{"armeabi-v7a"}},
 		{"arm64", "armv8-a", "cortex-a53", []string{"arm64-v8a"}},
+		{"arm64", "armv8-a", "cortex-a72", []string{"arm64-v8a"}},
 		{"arm64", "armv8-a", "cortex-a73", []string{"arm64-v8a"}},
 		{"arm64", "armv8-a", "denver64", []string{"arm64-v8a"}},
 		{"arm64", "armv8-a", "kryo", []string{"arm64-v8a"}},
@@ -1100,11 +1102,11 @@
 	return ret
 }
 
-func preferTargets(targets []Target, filters ...string) []Target {
+func firstTarget(targets []Target, filters ...string) []Target {
 	for _, filter := range filters {
 		buildTargets := filterMultilibTargets(targets, filter)
 		if len(buildTargets) > 0 {
-			return buildTargets
+			return buildTargets[:1]
 		}
 	}
 	return nil
@@ -1120,9 +1122,9 @@
 	case "common_first":
 		buildTargets = getCommonTargets(targets)
 		if prefer32 {
-			buildTargets = append(buildTargets, preferTargets(targets, "lib32", "lib64")...)
+			buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
 		} else {
-			buildTargets = append(buildTargets, preferTargets(targets, "lib64", "lib32")...)
+			buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
 		}
 	case "both":
 		if prefer32 {
@@ -1138,12 +1140,15 @@
 		buildTargets = filterMultilibTargets(targets, "lib64")
 	case "first":
 		if prefer32 {
-			buildTargets = preferTargets(targets, "lib32", "lib64")
+			buildTargets = firstTarget(targets, "lib32", "lib64")
 		} else {
-			buildTargets = preferTargets(targets, "lib64", "lib32")
+			buildTargets = firstTarget(targets, "lib64", "lib32")
 		}
 	case "prefer32":
-		buildTargets = preferTargets(targets, "lib32", "lib64")
+		buildTargets = filterMultilibTargets(targets, "lib32")
+		if len(buildTargets) == 0 {
+			buildTargets = filterMultilibTargets(targets, "lib64")
+		}
 	default:
 		return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", or "prefer32" found %q`,
 			multilib)
diff --git a/android/config.go b/android/config.go
index 3a2a005..db5d004 100644
--- a/android/config.go
+++ b/android/config.go
@@ -572,6 +572,14 @@
 	return Bool(c.productVariables.MinimizeJavaDebugInfo) && !Bool(c.productVariables.Eng)
 }
 
+func (c *config) Debuggable() bool {
+	return Bool(c.productVariables.Debuggable)
+}
+
+func (c *config) DevicePrefer32BitApps() bool {
+	return Bool(c.productVariables.DevicePrefer32BitApps)
+}
+
 func (c *config) DevicePrefer32BitExecutables() bool {
 	return Bool(c.productVariables.DevicePrefer32BitExecutables)
 }
diff --git a/android/module.go b/android/module.go
index 77765f1..92b11ed 100644
--- a/android/module.go
+++ b/android/module.go
@@ -23,6 +23,7 @@
 
 	"github.com/google/blueprint"
 	"github.com/google/blueprint/pathtools"
+	"github.com/google/blueprint/proptools"
 )
 
 var (
@@ -601,6 +602,10 @@
 	return Bool(p.commonProperties.Recovery)
 }
 
+func (a *ModuleBase) Owner() string {
+	return String(a.commonProperties.Owner)
+}
+
 func (a *ModuleBase) generateModuleTarget(ctx ModuleContext) {
 	allInstalledFiles := Paths{}
 	allCheckbuildFiles := Paths{}
@@ -844,6 +849,13 @@
 		bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
 	}
 
+	bparams.Outputs = proptools.NinjaEscape(bparams.Outputs)
+	bparams.ImplicitOutputs = proptools.NinjaEscape(bparams.ImplicitOutputs)
+	bparams.Inputs = proptools.NinjaEscape(bparams.Inputs)
+	bparams.Implicits = proptools.NinjaEscape(bparams.Implicits)
+	bparams.OrderOnly = proptools.NinjaEscape(bparams.OrderOnly)
+	bparams.Depfile = proptools.NinjaEscape([]string{bparams.Depfile})[0]
+
 	return bparams
 }
 
@@ -1515,16 +1527,16 @@
 	}
 }
 
-type AndroidModulesByName struct {
-	slice []Module
+type ModulesByName struct {
+	slice []blueprint.Module
 	ctx   interface {
 		ModuleName(blueprint.Module) string
 		ModuleSubDir(blueprint.Module) string
 	}
 }
 
-func (s AndroidModulesByName) Len() int { return len(s.slice) }
-func (s AndroidModulesByName) Less(i, j int) bool {
+func (s ModulesByName) Len() int { return len(s.slice) }
+func (s ModulesByName) Less(i, j int) bool {
 	mi, mj := s.slice[i], s.slice[j]
 	ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
 
@@ -1534,4 +1546,28 @@
 		return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
 	}
 }
-func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }
+func (s ModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }
+
+// Collect information for opening IDE project files in java/jdeps.go.
+type IDEInfo interface {
+	IDEInfo(ideInfo *IdeInfo)
+	BaseModuleName() string
+}
+
+// Extract the base module name from the Import name.
+// Often the Import name has a prefix "prebuilt_".
+// Remove the prefix explicitly if needed
+// until we find a better solution to get the Import name.
+type IDECustomizedModuleName interface {
+	IDECustomizedModuleName() string
+}
+
+type IdeInfo struct {
+	Deps              []string `json:"dependencies,omitempty"`
+	Srcs              []string `json:"srcs,omitempty"`
+	Aidl_include_dirs []string `json:"aidl_include_dirs,omitempty"`
+	Jarjar_rules      []string `json:"jarjar_rules,omitempty"`
+	Jars              []string `json:"jars,omitempty"`
+	Classes           []string `json:"class,omitempty"`
+	Installed_paths   []string `json:"installed,omitempty"`
+}
diff --git a/android/neverallow.go b/android/neverallow.go
index ec440d5..a7f4f82 100644
--- a/android/neverallow.go
+++ b/android/neverallow.go
@@ -56,7 +56,7 @@
 		without("vendor", "true").
 		without("owner", "").
 		because("a VNDK module can never have an owner."),
-	neverallow().notIn("libcore", "development").with("no_standard_libs", "true"),
+	neverallow().notIn("libcore", "development", "external/conscrypt").with("no_standard_libs", "true"),
 
 	// TODO(b/67974785): always enforce the manifest
 	neverallow().
diff --git a/android/package_ctx.go b/android/package_ctx.go
index e228bba..00b99ff 100644
--- a/android/package_ctx.go
+++ b/android/package_ctx.go
@@ -124,7 +124,11 @@
 // package-scoped variable's initialization.
 func (p PackageContext) SourcePathVariable(name, path string) blueprint.Variable {
 	return p.VariableFunc(name, func(ctx PackageVarContext) string {
-		return safePathForSource(ctx, path).String()
+		p, err := safePathForSource(ctx, path)
+		if err != nil {
+			ctx.Errorf("%s", err.Error())
+		}
+		return p.String()
 	})
 }
 
@@ -136,7 +140,10 @@
 	return p.VariableFunc(name, func(ctx PackageVarContext) string {
 		var ret []string
 		for _, path := range paths {
-			p := safePathForSource(ctx, path)
+			p, err := safePathForSource(ctx, path)
+			if err != nil {
+				ctx.Errorf("%s", err.Error())
+			}
 			ret = append(ret, p.String())
 		}
 		return strings.Join(ret, separator)
@@ -150,7 +157,10 @@
 // as part of a package-scoped variable's initialization.
 func (p PackageContext) SourcePathVariableWithEnvOverride(name, path, env string) blueprint.Variable {
 	return p.VariableFunc(name, func(ctx PackageVarContext) string {
-		p := safePathForSource(ctx, path)
+		p, err := safePathForSource(ctx, path)
+		if err != nil {
+			ctx.Errorf("%s", err.Error())
+		}
 		return ctx.Config().GetenvWithDefault(env, p.String())
 	})
 }
diff --git a/android/paths.go b/android/paths.go
index 57ebae2..daaf857 100644
--- a/android/paths.go
+++ b/android/paths.go
@@ -230,6 +230,8 @@
 // pathsForModuleSrcFromFullPath returns Paths rooted from the module's local
 // source directory, but strip the local source directory from the beginning of
 // each string. If incDirs is false, strip paths with a trailing '/' from the list.
+// It intended for use in globs that only list files that exist, so it allows '$' in
+// filenames.
 func pathsForModuleSrcFromFullPath(ctx ModuleContext, paths []string, incDirs bool) Paths {
 	prefix := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir()) + "/"
 	if prefix == "./" {
@@ -246,7 +248,7 @@
 			continue
 		}
 
-		srcPath, err := pathForSource(ctx, ctx.ModuleDir(), path[len(prefix):])
+		srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):])
 		if err != nil {
 			reportPathError(ctx, err)
 			continue
@@ -494,29 +496,26 @@
 
 // safePathForSource is for paths that we expect are safe -- only for use by go
 // code that is embedding ninja variables in paths
-func safePathForSource(ctx PathContext, path string) SourcePath {
-	p, err := validateSafePath(path)
-	if err != nil {
-		reportPathError(ctx, err)
-	}
+func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
+	p, err := validateSafePath(pathComponents...)
 	ret := SourcePath{basePath{p, ctx.Config(), ""}}
+	if err != nil {
+		return ret, err
+	}
 
 	abs, err := filepath.Abs(ret.String())
 	if err != nil {
-		reportPathError(ctx, err)
-		return ret
+		return ret, err
 	}
 	buildroot, err := filepath.Abs(ctx.Config().buildDir)
 	if err != nil {
-		reportPathError(ctx, err)
-		return ret
+		return ret, err
 	}
 	if strings.HasPrefix(abs, buildroot) {
-		reportPathErrorf(ctx, "source path %s is in output", abs)
-		return ret
+		return ret, fmt.Errorf("source path %s is in output", abs)
 	}
 
-	return ret
+	return ret, err
 }
 
 // pathForSource creates a SourcePath from pathComponents, but does not check that it exists.
@@ -555,7 +554,7 @@
 		var deps []string
 		// We cannot add build statements in this context, so we fall back to
 		// AddNinjaFileDeps
-		files, deps, err = pathtools.Glob(path.String(), nil)
+		files, deps, err = pathtools.Glob(path.String(), nil, pathtools.FollowSymlinks)
 		ctx.AddNinjaFileDeps(deps...)
 	}
 
diff --git a/android/singleton.go b/android/singleton.go
index fa1efdc..f926435 100644
--- a/android/singleton.go
+++ b/android/singleton.go
@@ -48,6 +48,7 @@
 	// are expanded in the scope of the PackageContext.
 	Eval(pctx PackageContext, ninjaStr string) (string, error)
 
+	VisitAllModulesBlueprint(visit func(blueprint.Module))
 	VisitAllModules(visit func(Module))
 	VisitAllModulesIf(pred func(Module) bool, visit func(Module))
 	// Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
@@ -138,6 +139,10 @@
 	}
 }
 
+func (s singletonContextAdaptor) VisitAllModulesBlueprint(visit func(blueprint.Module)) {
+	s.SingletonContext.VisitAllModules(visit)
+}
+
 func (s singletonContextAdaptor) VisitAllModules(visit func(Module)) {
 	s.SingletonContext.VisitAllModules(visitAdaptor(visit))
 }
diff --git a/android/variable.go b/android/variable.go
index b4ed1b7..9d965d5 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -43,8 +43,8 @@
 		} `android:"arch_variant"`
 
 		Malloc_not_svelte struct {
-			Cflags []string
-		}
+			Cflags []string `android:"arch_variant"`
+		} `android:"arch_variant"`
 
 		Safestack struct {
 			Cflags []string `android:"arch_variant"`
@@ -211,6 +211,7 @@
 	CoveragePaths        *[]string `json:",omitempty"`
 	CoverageExcludePaths *[]string `json:",omitempty"`
 
+	DevicePrefer32BitApps        *bool `json:",omitempty"`
 	DevicePrefer32BitExecutables *bool `json:",omitempty"`
 	HostPrefer32BitExecutables   *bool `json:",omitempty"`
 
diff --git a/cc/androidmk.go b/cc/androidmk.go
index 7595db1..5daacb7 100644
--- a/cc/androidmk.go
+++ b/cc/androidmk.go
@@ -60,24 +60,17 @@
 	ret := android.AndroidMkData{
 		OutputFile: c.outputFile,
 		Required:   c.Properties.AndroidMkRuntimeLibs,
+		Include:    "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
 
 		Extra: []android.AndroidMkExtraFunc{
 			func(w io.Writer, outputFile android.Path) {
 				if len(c.Properties.Logtags) > 0 {
 					fmt.Fprintln(w, "LOCAL_LOGTAGS_FILES :=", strings.Join(c.Properties.Logtags, " "))
 				}
-				fmt.Fprintln(w, "LOCAL_SANITIZE := never")
 				if len(c.Properties.AndroidMkSharedLibs) > 0 {
 					fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES := "+strings.Join(c.Properties.AndroidMkSharedLibs, " "))
 				}
-				if c.Target().Os == android.Android &&
-					String(c.Properties.Sdk_version) != "" && !c.useVndk() && !c.inRecovery() {
-					fmt.Fprintln(w, "LOCAL_SDK_VERSION := "+String(c.Properties.Sdk_version))
-					fmt.Fprintln(w, "LOCAL_NDK_STL_VARIANT := none")
-				} else {
-					// These are already included in LOCAL_SHARED_LIBRARIES
-					fmt.Fprintln(w, "LOCAL_CXX_STL := none")
-				}
+				fmt.Fprintln(w, "LOCAL_SOONG_LINK_TYPE :=", c.getMakeLinkType())
 				if c.useVndk() {
 					fmt.Fprintln(w, "LOCAL_USE_VNDK := true")
 				}
@@ -140,45 +133,13 @@
 	if library.static() {
 		ret.Class = "STATIC_LIBRARIES"
 	} else if library.shared() {
-		ctx.subAndroidMk(ret, &library.stripper)
-		ctx.subAndroidMk(ret, &library.relocationPacker)
-
 		ret.Class = "SHARED_LIBRARIES"
+		ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
+			fmt.Fprintln(w, "LOCAL_SOONG_TOC :=", library.toc().String())
+			fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", library.unstrippedOutputFile.String())
+		})
 	} else if library.header() {
-		ret.Custom = func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
-			fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
-			fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
-			fmt.Fprintln(w, "LOCAL_MODULE :=", name+data.SubName)
-
-			archStr := ctx.Target().Arch.ArchType.String()
-			var host bool
-			switch ctx.Target().Os.Class {
-			case android.Host:
-				fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH := ", archStr)
-				host = true
-			case android.HostCross:
-				fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH := ", archStr)
-				host = true
-			case android.Device:
-				fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH := ", archStr)
-			}
-
-			if host {
-				makeOs := ctx.Target().Os.String()
-				if ctx.Target().Os == android.Linux || ctx.Target().Os == android.LinuxBionic {
-					makeOs = "linux"
-				}
-				fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
-				fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
-			} else if ctx.useVndk() {
-				fmt.Fprintln(w, "LOCAL_USE_VNDK := true")
-			}
-
-			library.androidMkWriteExportedFlags(w)
-			fmt.Fprintln(w, "include $(BUILD_HEADER_LIBRARY)")
-		}
-
-		return
+		ret.Class = "HEADER_LIBRARIES"
 	}
 
 	ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
@@ -196,8 +157,6 @@
 
 		fmt.Fprintln(w, "LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE)"+ext)
 
-		fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
-
 		if library.coverageOutputFile.Valid() {
 			fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", library.coverageOutputFile.String())
 		}
@@ -205,29 +164,29 @@
 
 	if library.shared() {
 		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")
+		})
 	}
 }
 
 func (object *objectLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
 	ret.Custom = func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
 		out := ret.OutputFile.Path()
+		varname := fmt.Sprintf("SOONG_%sOBJECT_%s%s", prefix, name, data.SubName)
 
-		fmt.Fprintln(w, "\n$("+prefix+"OUT_INTERMEDIATE_LIBRARIES)/"+name+data.SubName+objectExtension+":", out.String())
-		fmt.Fprintln(w, "\t$(copy-file-to-target)")
+		fmt.Fprintf(w, "\n%s := %s\n", varname, out.String())
+		fmt.Fprintln(w, ".KATI_READONLY: "+varname)
 	}
 }
 
 func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
 	ctx.subAndroidMk(ret, binary.baseInstaller)
-	ctx.subAndroidMk(ret, &binary.stripper)
 
 	ret.Class = "EXECUTABLES"
 	ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
-		fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
-		if Bool(binary.Properties.Static_executable) {
-			fmt.Fprintln(w, "LOCAL_FORCE_STATIC_EXECUTABLE := true")
-		}
-
+		fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", binary.unstrippedOutputFile.String())
 		if len(binary.symlinks) > 0 {
 			fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS := "+strings.Join(binary.symlinks, " "))
 		}
@@ -288,33 +247,6 @@
 	ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
 		_, suffix, _ := splitFileExt(outputFile.Base())
 		fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+suffix)
-		fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
-	})
-}
-
-func (stripper *stripper) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
-	// Make only supports stripping target modules
-	if ctx.Target().Os != android.Android {
-		return
-	}
-
-	ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
-		if Bool(stripper.StripProperties.Strip.None) {
-
-			fmt.Fprintln(w, "LOCAL_STRIP_MODULE := false")
-		} else if Bool(stripper.StripProperties.Strip.Keep_symbols) {
-			fmt.Fprintln(w, "LOCAL_STRIP_MODULE := keep_symbols")
-		} else {
-			fmt.Fprintln(w, "LOCAL_STRIP_MODULE := mini-debug-info")
-		}
-	})
-}
-
-func (packer *relocationPacker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
-	ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
-		if packer.Properties.PackingRelocations {
-			fmt.Fprintln(w, "LOCAL_PACK_MODULE_RELOCATIONS := true")
-		}
 	})
 }
 
@@ -342,16 +274,10 @@
 	ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
 		path, file := filepath.Split(c.installPath.String())
 		stem, suffix, _ := splitFileExt(file)
-		fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
 		fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+suffix)
 		fmt.Fprintln(w, "LOCAL_MODULE_PATH := "+path)
 		fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
 		fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true")
-
-		// Prevent make from installing the libraries to obj/lib (since we have
-		// dozens of libraries with the same name, they'll clobber each other
-		// and the real versions of the libraries from the platform).
-		fmt.Fprintln(w, "LOCAL_COPY_TO_INTERMEDIATE_LIBRARIES := false")
 	})
 }
 
@@ -364,11 +290,9 @@
 		_, _, ext := splitFileExt(outputFile.Base())
 
 		fmt.Fprintln(w, "LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE)"+ext)
-		fmt.Fprintln(w, "LOCAL_STRIP_MODULE := false")
-		fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
 		fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
 		fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true")
-		fmt.Fprintln(w, "LOCAL_USE_VNDK := true")
+		fmt.Fprintln(w, "LOCAL_SOONG_TOC :=", c.toc().String())
 	})
 }
 
@@ -383,9 +307,6 @@
 		path := c.path.RelPathString()
 		dir, file := filepath.Split(path)
 		stem, suffix, ext := splitFileExt(file)
-		fmt.Fprintln(w, "LOCAL_STRIP_MODULE := false")
-		fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
-		fmt.Fprintln(w, "LOCAL_USE_VNDK := true")
 		fmt.Fprintln(w, "LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE)"+ext)
 		fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+suffix)
 		fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(OUT_DIR)/"+filepath.Clean(dir))
@@ -395,13 +316,6 @@
 
 func (c *ndkPrebuiltStlLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
 	ret.Class = "SHARED_LIBRARIES"
-
-	ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
-		// Prevent make from installing the libraries to obj/lib (since we have
-		// dozens of libraries with the same name, they'll clobber each other
-		// and the real versions of the libraries from the platform).
-		fmt.Fprintln(w, "LOCAL_COPY_TO_INTERMEDIATE_LIBRARIES := false")
-	})
 }
 
 func (c *vendorPublicLibraryStubDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
@@ -413,8 +327,6 @@
 		_, _, ext := splitFileExt(outputFile.Base())
 
 		fmt.Fprintln(w, "LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE)"+ext)
-		fmt.Fprintln(w, "LOCAL_STRIP_MODULE := false")
-		fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
 		fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
 		fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true")
 	})
diff --git a/cc/binary.go b/cc/binary.go
index be79032..07d503b 100644
--- a/cc/binary.go
+++ b/cc/binary.go
@@ -78,6 +78,9 @@
 
 	toolPath android.OptionalPath
 
+	// Location of the linked, unstripped binary
+	unstrippedOutputFile android.Path
+
 	// Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
 	symlinks []string
 
@@ -205,7 +208,7 @@
 func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
 	flags = binary.baseLinker.linkerFlags(ctx, flags)
 
-	if ctx.Host() && !binary.static() {
+	if ctx.Host() && !ctx.Windows() && !binary.static() {
 		if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") {
 			flags.LdFlags = append(flags.LdFlags, "-pie")
 		}
@@ -306,6 +309,8 @@
 		binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
 	}
 
+	binary.unstrippedOutputFile = outputFile
+
 	if String(binary.Properties.Prefix_symbols) != "" {
 		afterPrefixSymbols := outputFile
 		outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
diff --git a/cc/builder.go b/cc/builder.go
index be63fd7..58196f4 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -144,11 +144,11 @@
 		blueprint.RuleParams{
 			Depfile:     "${out}.d",
 			Deps:        blueprint.DepsGCC,
-			Command:     "CROSS_COMPILE=$crossCompile $tocPath -i ${in} -o ${out} -d ${out}.d",
+			Command:     "CROSS_COMPILE=$crossCompile $tocPath $format -i ${in} -o ${out} -d ${out}.d",
 			CommandDeps: []string{"$tocPath"},
 			Restat:      true,
 		},
-		"crossCompile")
+		"crossCompile", "format")
 
 	clangTidy = pctx.AndroidStaticRule("clangTidy",
 		blueprint.RuleParams{
@@ -759,7 +759,18 @@
 func TransformSharedObjectToToc(ctx android.ModuleContext, inputFile android.Path,
 	outputFile android.WritablePath, flags builderFlags) {
 
-	crossCompile := gccCmd(flags.toolchain, "")
+	var format string
+	var crossCompile string
+	if ctx.Darwin() {
+		format = "--macho"
+		crossCompile = "${config.MacToolPath}"
+	} else if ctx.Windows() {
+		format = "--pe"
+		crossCompile = gccCmd(flags.toolchain, "")
+	} else {
+		format = "--elf"
+		crossCompile = gccCmd(flags.toolchain, "")
+	}
 
 	ctx.Build(pctx, android.BuildParams{
 		Rule:        toc,
@@ -768,6 +779,7 @@
 		Input:       inputFile,
 		Args: map[string]string{
 			"crossCompile": crossCompile,
+			"format":       format,
 		},
 	})
 }
diff --git a/cc/cc.go b/cc/cc.go
index 2d967ed..d31a38a 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -366,6 +366,10 @@
 	staticVariant *Module
 }
 
+func (c *Module) OutputFile() android.OptionalPath {
+	return c.outputFile
+}
+
 func (c *Module) Init() android.Module {
 	c.AddProperties(&c.Properties, &c.VendorProperties)
 	if c.compiler != nil {
@@ -1089,10 +1093,6 @@
 		ctx.PropertyErrorf("clang", "false (GCC) is no longer supported")
 	}
 
-	if !c.toolchain(ctx).ClangSupported() {
-		panic("GCC is no longer supported")
-	}
-
 	return !c.Properties.Gcc
 }
 
@@ -1181,7 +1181,7 @@
 		// We can be permissive with the system "STL" since it is only the C++
 		// ABI layer, but in the future we should make sure that everyone is
 		// using either libc++ or nothing.
-	} else if getNdkStlFamily(ctx, from) != getNdkStlFamily(ctx, to) {
+	} else if getNdkStlFamily(from) != getNdkStlFamily(to) {
 		ctx.ModuleErrorf("uses %q and depends on %q which uses incompatible %q",
 			from.stl.Properties.SelectedStl, ctx.OtherModuleName(to),
 			to.stl.Properties.SelectedStl)
@@ -1491,6 +1491,29 @@
 	return false
 }
 
+func (c *Module) getMakeLinkType() string {
+	if c.useVndk() {
+		if inList(c.Name(), vndkCoreLibraries) || inList(c.Name(), vndkSpLibraries) || inList(c.Name(), llndkLibraries) {
+			if inList(c.Name(), vndkPrivateLibraries) {
+				return "native:vndk_private"
+			} else {
+				return "native:vndk"
+			}
+		} else {
+			return "native:vendor"
+		}
+	} else if c.inRecovery() {
+		return "native:recovery"
+	} else if c.Target().Os == android.Android && String(c.Properties.Sdk_version) != "" {
+		return "native:ndk:none:none"
+		// TODO(b/114741097): use the correct ndk stl once build errors have been fixed
+		//family, link := getNdkStlFamilyAndLinkType(c)
+		//return fmt.Sprintf("native:ndk:%s:%s", family, link)
+	} else {
+		return "native:platform"
+	}
+}
+
 //
 // Defaults
 //
diff --git a/cc/cmakelists.go b/cc/cmakelists.go
index a2f46cd..699f5c0 100644
--- a/cc/cmakelists.go
+++ b/cc/cmakelists.go
@@ -204,10 +204,11 @@
 	writeAllIncludeDirectories(c.systemHeaderSearchPath, f, true)
 	writeAllIncludeDirectories(c.headerSearchPath, f, false)
 	if cflags {
+		writeAllRelativeFilePathFlags(c.relativeFilePathFlags, f, "CMAKE_C_FLAGS")
 		writeAllFlags(c.flags, f, "CMAKE_C_FLAGS")
 	}
-
 	if cppflags {
+		writeAllRelativeFilePathFlags(c.relativeFilePathFlags, f, "CMAKE_CXX_FLAGS")
 		writeAllFlags(c.flags, f, "CMAKE_CXX_FLAGS")
 	}
 	if c.sysroot != "" {
@@ -249,6 +250,17 @@
 	f.WriteString("list (APPEND SOURCE_FILES ${TMP_HEADERS})\n\n")
 }
 
+type relativeFilePathFlagType struct {
+	flag             string
+	relativeFilePath string
+}
+
+func writeAllRelativeFilePathFlags(relativeFilePathFlags []relativeFilePathFlagType, f *os.File, tag string) {
+	for _, flag := range relativeFilePathFlags {
+		f.WriteString(fmt.Sprintf("set(%s \"${%s} %s=%s\")\n", tag, tag, flag.flag, buildCMakePath(flag.relativeFilePath)))
+	}
+}
+
 func writeAllFlags(flags []string, f *os.File, tag string) {
 	for _, flag := range flags {
 		f.WriteString(fmt.Sprintf("set(%s \"${%s} %s\")\n", tag, tag, flag))
@@ -263,6 +275,7 @@
 	systemHeaderSearchPath
 	flag
 	systemRoot
+	relativeFilePathFlag
 )
 
 type compilerParameters struct {
@@ -270,6 +283,8 @@
 	systemHeaderSearchPath []string
 	flags                  []string
 	sysroot                string
+	// Must be in a=b/c/d format and can be split into "a" and "b/c/d"
+	relativeFilePathFlags []relativeFilePathFlagType
 }
 
 func makeCompilerParameters() compilerParameters {
@@ -294,6 +309,9 @@
 	if strings.HasPrefix(parameter, "--sysroot") {
 		return systemRoot
 	}
+	if strings.HasPrefix(parameter, "-fsanitize-blacklist") {
+		return relativeFilePathFlag
+	}
 	return flag
 }
 
@@ -347,6 +365,16 @@
 				f.WriteString("# Found a system root path marker with no path")
 			}
 			i = i + 1
+		case relativeFilePathFlag:
+			flagComponents := strings.Split(param, "=")
+			if len(flagComponents) == 2 {
+				flagStruct := relativeFilePathFlagType{flag: flagComponents[0], relativeFilePath: flagComponents[1]}
+				compilerParameters.relativeFilePathFlags = append(compilerParameters.relativeFilePathFlags, flagStruct)
+			} else {
+				if outputDebugInfo {
+					f.WriteString(fmt.Sprintf("# Relative File Path Flag [%s] is not formatted as a=b/c/d \n", param))
+				}
+			}
 		}
 	}
 	return compilerParameters
diff --git a/cc/compdb.go b/cc/compdb.go
index a9c5b5e..acfc924 100644
--- a/cc/compdb.go
+++ b/cc/compdb.go
@@ -181,7 +181,7 @@
 func evalAndSplitVariable(ctx android.SingletonContext, str string) ([]string, error) {
 	evaluated, err := ctx.Eval(pctx, str)
 	if err == nil {
-		return strings.Split(evaluated, " "), nil
+		return strings.Fields(evaluated), nil
 	}
 	return []string{""}, err
 }
diff --git a/cc/compiler.go b/cc/compiler.go
index f3cf040..5ef9e4e 100644
--- a/cc/compiler.go
+++ b/cc/compiler.go
@@ -444,16 +444,6 @@
 		cppStd = strings.Replace(String(compiler.Properties.Cpp_std), "17", "1z", 1)
 	}
 
-	if !flags.Clang {
-		// GCC uses an invalid C++14 ABI (emits calls to
-		// __cxa_throw_bad_array_length, which is not a valid C++ RT ABI).
-		// http://b/25022512
-		// The host GCC doesn't support C++14 (and is deprecated, so likely
-		// never will).
-		// Build these modules with C++11.
-		cppStd = config.GccCppStdVersion
-	}
-
 	if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
 		cStd = gnuToCReplacer.Replace(cStd)
 		cppStd = gnuToCReplacer.Replace(cppStd)
diff --git a/cc/config/arm64_device.go b/cc/config/arm64_device.go
index 6fdd524..12e9114 100644
--- a/cc/config/arm64_device.go
+++ b/cc/config/arm64_device.go
@@ -86,6 +86,7 @@
 		"armv8_2a",
 		"cortex-a53",
 		"cortex-a55",
+		"cortex-a72",
 		"cortex-a73",
 		"cortex-a75",
 		"kryo",
@@ -146,6 +147,7 @@
 		"":           "",
 		"cortex-a53": "${config.Arm64CortexA53Cflags}",
 		"cortex-a55": "${config.Arm64CortexA55Cflags}",
+		"cortex-a72": "${config.Arm64CortexA53Cflags}",
 		"cortex-a73": "${config.Arm64CortexA53Cflags}",
 		"cortex-a75": "${config.Arm64CortexA55Cflags}",
 		"kryo":       "${config.Arm64KryoCflags}",
@@ -162,6 +164,7 @@
 		"":           "",
 		"cortex-a53": "${config.Arm64ClangCortexA53Cflags}",
 		"cortex-a55": "${config.Arm64ClangCortexA55Cflags}",
+		"cortex-a72": "${config.Arm64ClangCortexA53Cflags}",
 		"cortex-a73": "${config.Arm64ClangCortexA53Cflags}",
 		"cortex-a75": "${config.Arm64ClangCortexA55Cflags}",
 		"kryo":       "${config.Arm64ClangKryoCflags}",
@@ -257,7 +260,7 @@
 
 	var extraLdflags string
 	switch arch.CpuVariant {
-	case "cortex-a53", "cortex-a73", "kryo", "exynos-m1", "exynos-m2",
+	case "cortex-a53", "cortex-a72", "cortex-a73", "kryo", "exynos-m1", "exynos-m2",
 		// This variant might not need the workaround but leave it
 		// in the list since it has had the workaround on before.
 		"denver64":
diff --git a/cc/config/arm_device.go b/cc/config/arm_device.go
index 4135179..c1c2c7b 100644
--- a/cc/config/arm_device.go
+++ b/cc/config/arm_device.go
@@ -162,6 +162,7 @@
 		"cortex-a53",
 		"cortex-a53-a57",
 		"cortex-a55",
+		"cortex-a72",
 		"cortex-a73",
 		"cortex-a75",
 		"krait",
@@ -269,6 +270,7 @@
 		"cortex-a53":     "${config.ArmCortexA53Cflags}",
 		"cortex-a53.a57": "${config.ArmCortexA53Cflags}",
 		"cortex-a55":     "${config.ArmCortexA55Cflags}",
+		"cortex-a72":     "${config.ArmCortexA53Cflags}",
 		"cortex-a73":     "${config.ArmCortexA53Cflags}",
 		"cortex-a75":     "${config.ArmCortexA55Cflags}",
 		"krait":          "${config.ArmKraitCflags}",
@@ -292,6 +294,7 @@
 		"cortex-a53":     "${config.ArmClangCortexA53Cflags}",
 		"cortex-a53.a57": "${config.ArmClangCortexA53Cflags}",
 		"cortex-a55":     "${config.ArmClangCortexA55Cflags}",
+		"cortex-a72":     "${config.ArmClangCortexA53Cflags}",
 		"cortex-a73":     "${config.ArmClangCortexA53Cflags}",
 		"cortex-a75":     "${config.ArmClangCortexA55Cflags}",
 		"krait":          "${config.ArmClangKraitCflags}",
diff --git a/cc/config/clang.go b/cc/config/clang.go
index 186d790..5e22d6a 100644
--- a/cc/config/clang.go
+++ b/cc/config/clang.go
@@ -178,6 +178,18 @@
 		// Disable c++98-specific warning since Android is not concerned with C++98
 		// compatibility.
 		"-Wno-c++98-compat-extra-semi",
+
+		// Disable this warning until we can fix all instances where it fails.
+		"-Wno-self-assign-overloaded",
+
+		// Disable this warning until we can fix all instances where it fails.
+		"-Wno-constant-logical-operand",
+
+		// Disable this warning because we don't care about behavior with older compilers.
+		"-Wno-return-std-move-in-c++11",
+
+		// Disable this warning until we can fix all instances where it fails.
+		"-Wno-dangling-field",
 	}, " "))
 
 	// Extra cflags for projects under external/ directory to disable warnings that are infeasible
diff --git a/cc/config/global.go b/cc/config/global.go
index 8b02f02..ad63c97 100644
--- a/cc/config/global.go
+++ b/cc/config/global.go
@@ -119,7 +119,6 @@
 
 	CStdVersion               = "gnu99"
 	CppStdVersion             = "gnu++14"
-	GccCppStdVersion          = "gnu++11"
 	ExperimentalCStdVersion   = "gnu11"
 	ExperimentalCppStdVersion = "gnu++1z"
 
@@ -127,8 +126,8 @@
 
 	// prebuilts/clang default settings.
 	ClangDefaultBase         = "prebuilts/clang/host"
-	ClangDefaultVersion      = "clang-r328903"
-	ClangDefaultShortVersion = "7.0.2"
+	ClangDefaultVersion      = "clang-r339409"
+	ClangDefaultShortVersion = "8.0.1"
 
 	// Directories with warnings from Android.bp files.
 	WarningAllowedProjects = []string{
diff --git a/cc/config/tidy.go b/cc/config/tidy.go
index 67f92a2..2e49c16 100644
--- a/cc/config/tidy.go
+++ b/cc/config/tidy.go
@@ -36,6 +36,7 @@
 			"performance*",
 			"-google-readability*",
 			"-google-runtime-references",
+			"-performance-noexcept-move-constructor",
 		}, ",")
 	})
 
@@ -61,20 +62,30 @@
 	// Give warnings to header files only in selected directories.
 	// Do not give warnings to external or vendor header files, which contain too
 	// many warnings.
-	pctx.StaticVariable("TidyDefaultHeaderDirs", strings.Join([]string{
-		"art/",
-		"bionic/",
-		"bootable/",
-		"build/",
-		"cts/",
-		"dalvik/",
-		"developers/",
-		"development/",
-		"frameworks/",
-		"libcore/",
-		"libnativehelper/",
-		"system/",
-	}, "|"))
+	pctx.VariableFunc("TidyDefaultHeaderDirs", func(ctx android.PackageVarContext) string {
+		if override := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS"); override != "" {
+			return override
+		}
+		return strings.Join([]string{
+			"art/",
+			"bionic/",
+			"bootable/",
+			"build/",
+			"cts/",
+			"dalvik/",
+			"developers/",
+			"development/",
+			"frameworks/",
+			"libcore/",
+			"libnativehelper/",
+			"system/",
+		}, "|")
+	})
+
+	// Use WTIH_TIDY_FLAGS to pass extra global default clang-tidy flags.
+	pctx.VariableFunc("TidyWithTidyFlags", func(ctx android.PackageVarContext) string {
+		return ctx.Config().Getenv("WITH_TIDY_FLAGS")
+	})
 }
 
 type PathBasedTidyCheck struct {
diff --git a/cc/config/toolchain.go b/cc/config/toolchain.go
index fefe7c2..930b18f 100644
--- a/cc/config/toolchain.go
+++ b/cc/config/toolchain.go
@@ -57,7 +57,6 @@
 	IncludeFlags() string
 	InstructionSetFlags(string) (string, error)
 
-	ClangSupported() bool
 	ClangTriple() string
 	ToolchainClangCflags() string
 	ToolchainClangLdflags() string
@@ -132,10 +131,6 @@
 	return ""
 }
 
-func (toolchainBase) ClangSupported() bool {
-	return true
-}
-
 func (toolchainBase) ShlibSuffix() string {
 	return ".so"
 }
diff --git a/cc/config/x86_windows_host.go b/cc/config/x86_windows_host.go
index 9003e85..9573174 100644
--- a/cc/config/x86_windows_host.go
+++ b/cc/config/x86_windows_host.go
@@ -106,9 +106,6 @@
 		"-L${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3",
 		"-B${WindowsGccRoot}/${WindowsGccTriple}/lib64",
 		"-pthread",
-		// Bug: http://b/109759970 - WAR until issue with ld.bfd's
-		// inability to handle Clang-generated section names is fixed.
-		"-Wl,--allow-multiple-definition",
 	}...)
 	windowsX8664ClangLldflags = ClangFilterUnknownLldflags(windowsX8664ClangLdflags)
 
@@ -233,10 +230,6 @@
 	return "-F pe-x86-64"
 }
 
-func (t *toolchainWindows) ClangSupported() bool {
-	return true
-}
-
 func (t *toolchainWindowsX86) ClangTriple() string {
 	return "i686-windows-gnu"
 }
diff --git a/cc/kernel_headers.go b/cc/kernel_headers.go
index 42dc770..82a779c 100644
--- a/cc/kernel_headers.go
+++ b/cc/kernel_headers.go
@@ -26,7 +26,7 @@
 	if ctx.Device() {
 		f := &stub.libraryDecorator.flagExporter
 		for _, dir := range ctx.DeviceConfig().DeviceKernelHeaderDirs() {
-			f.flags = append(f.flags, "-isystem"+dir)
+			f.flags = append(f.flags, "-isystem "+dir)
 		}
 	}
 	return stub.libraryDecorator.linkStatic(ctx, flags, deps, objs)
diff --git a/cc/library.go b/cc/library.go
index 7ff7885..0e45af9 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -210,7 +210,6 @@
 
 	flagExporter
 	stripper
-	relocationPacker
 
 	// If we're used as a whole_static_lib, our missing dependencies need
 	// to be given
@@ -238,6 +237,9 @@
 	// not included in the NDK.
 	ndkSysrootPath android.Path
 
+	// Location of the linked, unstripped library for shared libraries
+	unstrippedOutputFile android.Path
+
 	// Decorated interafaces
 	*baseCompiler
 	*baseLinker
@@ -251,8 +253,7 @@
 		&library.Properties,
 		&library.MutatedProperties,
 		&library.flagExporter.Properties,
-		&library.stripper.StripProperties,
-		&library.relocationPacker.Properties)
+		&library.stripper.StripProperties)
 }
 
 func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
@@ -428,8 +429,6 @@
 	library.baseInstaller.location = location
 
 	library.baseLinker.linkerInit(ctx)
-
-	library.relocationPacker.packingInit(ctx)
 }
 
 func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
@@ -548,21 +547,13 @@
 
 	builderFlags := flagsToBuilderFlags(flags)
 
-	if !ctx.Darwin() && !ctx.Windows() {
-		// Optimize out relinking against shared libraries whose interface hasn't changed by
-		// depending on a table of contents file instead of the library itself.
-		tocPath := outputFile.RelPathString()
-		tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
-		tocFile := android.PathForOutput(ctx, tocPath)
-		library.tocFile = android.OptionalPathForPath(tocFile)
-		TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
-	}
-
-	if library.relocationPacker.needsPacking(ctx) {
-		packedOutputFile := outputFile
-		outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
-		library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
-	}
+	// Optimize out relinking against shared libraries whose interface hasn't changed by
+	// depending on a table of contents file instead of the library itself.
+	tocPath := outputFile.RelPathString()
+	tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
+	tocFile := android.PathForOutput(ctx, tocPath)
+	library.tocFile = android.OptionalPathForPath(tocFile)
+	TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
 
 	if library.stripper.needsStrip(ctx) {
 		// b/80093681, GNU strip/objcopy bug.
@@ -574,6 +565,8 @@
 		library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
 	}
 
+	library.unstrippedOutputFile = outputFile
+
 	if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
 		versionedOutputFile := outputFile
 		outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
diff --git a/cc/llndk_library.go b/cc/llndk_library.go
index 6e64acf..32da059 100644
--- a/cc/llndk_library.go
+++ b/cc/llndk_library.go
@@ -144,17 +144,17 @@
 			timestampFiles = append(timestampFiles, stub.processHeaders(ctx, dir, genHeaderOutDir))
 		}
 
-		includePrefix := "-I "
+		includePrefix := "-I"
 		if Bool(stub.Properties.Export_headers_as_system) {
 			includePrefix = "-isystem "
 		}
 
-		stub.reexportFlags([]string{includePrefix + " " + genHeaderOutDir.String()})
+		stub.reexportFlags([]string{includePrefix + genHeaderOutDir.String()})
 		stub.reexportDeps(timestampFiles)
 	}
 
 	if Bool(stub.Properties.Export_headers_as_system) {
-		stub.exportIncludes(ctx, "-isystem")
+		stub.exportIncludes(ctx, "-isystem ")
 		stub.libraryDecorator.flagExporter.Properties.Export_include_dirs = []string{}
 	}
 
@@ -172,6 +172,7 @@
 		libraryDecorator: library,
 	}
 	stub.Properties.Vendor_available = BoolPtr(true)
+	module.Properties.UseVndk = true
 	module.compiler = stub
 	module.linker = stub
 	module.installer = nil
diff --git a/cc/makevars.go b/cc/makevars.go
index c4ef5f1..b7fb575 100644
--- a/cc/makevars.go
+++ b/cc/makevars.go
@@ -130,24 +130,28 @@
 	ctx.Strict("HWADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS", strings.Join(hwasanCflags, " "))
 
 	ctx.Strict("CFI_EXTRA_CFLAGS", strings.Join(cfiCflags, " "))
+	ctx.Strict("CFI_EXTRA_ASFLAGS", strings.Join(cfiAsflags, " "))
 	ctx.Strict("CFI_EXTRA_LDFLAGS", strings.Join(cfiLdflags, " "))
 
 	ctx.Strict("INTEGER_OVERFLOW_EXTRA_CFLAGS", strings.Join(intOverflowCflags, " "))
 
 	ctx.Strict("DEFAULT_C_STD_VERSION", config.CStdVersion)
 	ctx.Strict("DEFAULT_CPP_STD_VERSION", config.CppStdVersion)
-	ctx.Strict("DEFAULT_GCC_CPP_STD_VERSION", config.GccCppStdVersion)
 	ctx.Strict("EXPERIMENTAL_C_STD_VERSION", config.ExperimentalCStdVersion)
 	ctx.Strict("EXPERIMENTAL_CPP_STD_VERSION", config.ExperimentalCppStdVersion)
 
 	ctx.Strict("DEFAULT_GLOBAL_TIDY_CHECKS", "${config.TidyDefaultGlobalChecks}")
 	ctx.Strict("DEFAULT_LOCAL_TIDY_CHECKS", joinLocalTidyChecks(config.DefaultLocalTidyChecks))
 	ctx.Strict("DEFAULT_TIDY_HEADER_DIRS", "${config.TidyDefaultHeaderDirs}")
+	ctx.Strict("WITH_TIDY_FLAGS", "${config.TidyWithTidyFlags}")
 
 	ctx.Strict("AIDL_CPP", "${aidlCmd}")
 
 	ctx.Strict("RS_GLOBAL_INCLUDES", "${config.RsGlobalIncludes}")
 
+	ctx.Strict("SOONG_STRIP_PATH", "${stripPath}")
+	ctx.Strict("XZ", "${xzCmd}")
+
 	nativeHelperIncludeFlags, err := ctx.Eval("${config.CommonNativehelperInclude}")
 	if err != nil {
 		panic(err)
@@ -217,34 +221,6 @@
 		productExtraLdflags += "-static"
 	}
 
-	ctx.Strict(makePrefix+"GLOBAL_CFLAGS", strings.Join([]string{
-		toolchain.Cflags(),
-		"${config.CommonGlobalCflags}",
-		fmt.Sprintf("${config.%sGlobalCflags}", hod),
-		toolchain.ToolchainCflags(),
-		productExtraCflags,
-	}, " "))
-	ctx.Strict(makePrefix+"GLOBAL_CONLYFLAGS", strings.Join([]string{
-		"${config.CommonGlobalConlyflags}",
-	}, " "))
-	ctx.Strict(makePrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
-		"${config.CommonGlobalCppflags}",
-		fmt.Sprintf("${config.%sGlobalCppflags}", hod),
-		toolchain.Cppflags(),
-	}, " "))
-	ctx.Strict(makePrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
-		fmt.Sprintf("${config.%sGlobalLdflags}", hod),
-		toolchain.Ldflags(),
-		toolchain.ToolchainLdflags(),
-		productExtraLdflags,
-	}, " "))
-	ctx.Strict(makePrefix+"GLOBAL_LLDFLAGS", strings.Join([]string{
-		fmt.Sprintf("${config.%sGlobalLldflags}", hod),
-		toolchain.Ldflags(),
-		toolchain.ToolchainLdflags(),
-		productExtraLdflags,
-	}, " "))
-
 	includeFlags, err := ctx.Eval(toolchain.IncludeFlags())
 	if err != nil {
 		panic(err)
@@ -267,60 +243,51 @@
 		ctx.Strict(makePrefix+"thumb_CFLAGS", flags)
 	}
 
-	if toolchain.ClangSupported() {
-		clangPrefix := secondPrefix + "CLANG_" + typePrefix
-		clangExtras := "-target " + toolchain.ClangTriple()
-		clangExtras += " -B" + config.ToolPath(toolchain)
+	clangPrefix := secondPrefix + "CLANG_" + typePrefix
+	clangExtras := "-target " + toolchain.ClangTriple()
+	clangExtras += " -B" + config.ToolPath(toolchain)
 
-		ctx.Strict(clangPrefix+"GLOBAL_CFLAGS", strings.Join([]string{
-			toolchain.ClangCflags(),
-			"${config.CommonClangGlobalCflags}",
-			fmt.Sprintf("${config.%sClangGlobalCflags}", hod),
-			toolchain.ToolchainClangCflags(),
-			clangExtras,
-			productExtraCflags,
-		}, " "))
-		ctx.Strict(clangPrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
-			"${config.CommonClangGlobalCppflags}",
-			fmt.Sprintf("${config.%sGlobalCppflags}", hod),
-			toolchain.ClangCppflags(),
-		}, " "))
-		ctx.Strict(clangPrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
-			fmt.Sprintf("${config.%sGlobalLdflags}", hod),
-			toolchain.ClangLdflags(),
-			toolchain.ToolchainClangLdflags(),
-			productExtraLdflags,
-			clangExtras,
-		}, " "))
-		ctx.Strict(clangPrefix+"GLOBAL_LLDFLAGS", strings.Join([]string{
-			fmt.Sprintf("${config.%sGlobalLldflags}", hod),
-			toolchain.ClangLldflags(),
-			toolchain.ToolchainClangLdflags(),
-			productExtraLdflags,
-			clangExtras,
-		}, " "))
+	ctx.Strict(clangPrefix+"GLOBAL_CFLAGS", strings.Join([]string{
+		toolchain.ClangCflags(),
+		"${config.CommonClangGlobalCflags}",
+		fmt.Sprintf("${config.%sClangGlobalCflags}", hod),
+		toolchain.ToolchainClangCflags(),
+		clangExtras,
+		productExtraCflags,
+	}, " "))
+	ctx.Strict(clangPrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
+		"${config.CommonClangGlobalCppflags}",
+		fmt.Sprintf("${config.%sGlobalCppflags}", hod),
+		toolchain.ClangCppflags(),
+	}, " "))
+	ctx.Strict(clangPrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
+		fmt.Sprintf("${config.%sGlobalLdflags}", hod),
+		toolchain.ClangLdflags(),
+		toolchain.ToolchainClangLdflags(),
+		productExtraLdflags,
+		clangExtras,
+	}, " "))
+	ctx.Strict(clangPrefix+"GLOBAL_LLDFLAGS", strings.Join([]string{
+		fmt.Sprintf("${config.%sGlobalLldflags}", hod),
+		toolchain.ClangLldflags(),
+		toolchain.ToolchainClangLdflags(),
+		productExtraLdflags,
+		clangExtras,
+	}, " "))
 
-		if target.Os.Class == android.Device {
-			ctx.Strict(secondPrefix+"ADDRESS_SANITIZER_RUNTIME_LIBRARY", strings.TrimSuffix(config.AddressSanitizerRuntimeLibrary(toolchain), ".so"))
-			ctx.Strict(secondPrefix+"HWADDRESS_SANITIZER_RUNTIME_LIBRARY", strings.TrimSuffix(config.HWAddressSanitizerRuntimeLibrary(toolchain), ".so"))
-			ctx.Strict(secondPrefix+"HWADDRESS_SANITIZER_STATIC_LIBRARY", strings.TrimSuffix(config.HWAddressSanitizerStaticLibrary(toolchain), ".a"))
-			ctx.Strict(secondPrefix+"UBSAN_RUNTIME_LIBRARY", strings.TrimSuffix(config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain), ".so"))
-			ctx.Strict(secondPrefix+"UBSAN_MINIMAL_RUNTIME_LIBRARY", strings.TrimSuffix(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(toolchain), ".a"))
-			ctx.Strict(secondPrefix+"TSAN_RUNTIME_LIBRARY", strings.TrimSuffix(config.ThreadSanitizerRuntimeLibrary(toolchain), ".so"))
-			ctx.Strict(secondPrefix+"SCUDO_RUNTIME_LIBRARY", strings.TrimSuffix(config.ScudoRuntimeLibrary(toolchain), ".so"))
-		}
-
-		// This is used by external/gentoo/...
-		ctx.Strict("CLANG_CONFIG_"+target.Arch.ArchType.Name+"_"+typePrefix+"TRIPLE",
-			toolchain.ClangTriple())
-
-		ctx.Strict(makePrefix+"CLANG_SUPPORTED", "true")
-	} else {
-		ctx.Strict(makePrefix+"CLANG_SUPPORTED", "")
+	if target.Os.Class == android.Device {
+		ctx.Strict(secondPrefix+"ADDRESS_SANITIZER_RUNTIME_LIBRARY", strings.TrimSuffix(config.AddressSanitizerRuntimeLibrary(toolchain), ".so"))
+		ctx.Strict(secondPrefix+"HWADDRESS_SANITIZER_RUNTIME_LIBRARY", strings.TrimSuffix(config.HWAddressSanitizerRuntimeLibrary(toolchain), ".so"))
+		ctx.Strict(secondPrefix+"HWADDRESS_SANITIZER_STATIC_LIBRARY", strings.TrimSuffix(config.HWAddressSanitizerStaticLibrary(toolchain), ".a"))
+		ctx.Strict(secondPrefix+"UBSAN_RUNTIME_LIBRARY", strings.TrimSuffix(config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain), ".so"))
+		ctx.Strict(secondPrefix+"UBSAN_MINIMAL_RUNTIME_LIBRARY", strings.TrimSuffix(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(toolchain), ".a"))
+		ctx.Strict(secondPrefix+"TSAN_RUNTIME_LIBRARY", strings.TrimSuffix(config.ThreadSanitizerRuntimeLibrary(toolchain), ".so"))
+		ctx.Strict(secondPrefix+"SCUDO_RUNTIME_LIBRARY", strings.TrimSuffix(config.ScudoRuntimeLibrary(toolchain), ".so"))
 	}
 
-	ctx.Strict(makePrefix+"CC", gccCmd(toolchain, "gcc"))
-	ctx.Strict(makePrefix+"CXX", gccCmd(toolchain, "g++"))
+	// This is used by external/gentoo/...
+	ctx.Strict("CLANG_CONFIG_"+target.Arch.ArchType.Name+"_"+typePrefix+"TRIPLE",
+		toolchain.ClangTriple())
 
 	if target.Os == android.Darwin {
 		ctx.Strict(makePrefix+"AR", "${config.MacArPath}")
diff --git a/cc/ndk_library.go b/cc/ndk_library.go
index 3bffacd..2709a89 100644
--- a/cc/ndk_library.go
+++ b/cc/ndk_library.go
@@ -39,6 +39,7 @@
 
 	ndkPrebuiltSharedLibs = []string{
 		"android",
+		"binder_ndk",
 		"c",
 		"dl",
 		"EGL",
diff --git a/cc/ndk_prebuilt.go b/cc/ndk_prebuilt.go
index 258d6bd..2a7e657 100644
--- a/cc/ndk_prebuilt.go
+++ b/cc/ndk_prebuilt.go
@@ -137,7 +137,7 @@
 		ctx.ModuleErrorf("NDK prebuilt libraries must have an ndk_lib prefixed name")
 	}
 
-	ndk.exportIncludes(ctx, "-isystem")
+	ndk.exportIncludes(ctx, "-isystem ")
 
 	libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
 	libExt := flags.Toolchain.ShlibSuffix()
diff --git a/cc/object.go b/cc/object.go
index b5fd835..7c134ac 100644
--- a/cc/object.go
+++ b/cc/object.go
@@ -39,6 +39,7 @@
 		baseLinker: NewBaseLinker(nil),
 	}
 	module.compiler = NewBaseCompiler()
+	module.stl = &stl{}
 	return module.Init()
 }
 
diff --git a/cc/prebuilt.go b/cc/prebuilt.go
index 3dd4d11..47994a8 100644
--- a/cc/prebuilt.go
+++ b/cc/prebuilt.go
@@ -62,7 +62,7 @@
 }
 
 func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
-	return Flags{}
+	return flags
 }
 
 func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
@@ -76,8 +76,28 @@
 		p.libraryDecorator.exportIncludes(ctx, "-I")
 		p.libraryDecorator.reexportFlags(deps.ReexportedFlags)
 		p.libraryDecorator.reexportDeps(deps.ReexportedFlagsDeps)
-		// TODO(ccross): .toc optimization, stripping, packing
-		return p.Prebuilt.SingleSourcePath(ctx)
+
+		builderFlags := flagsToBuilderFlags(flags)
+
+		in := p.Prebuilt.SingleSourcePath(ctx)
+
+		if p.shared() {
+			p.unstrippedOutputFile = in
+			libName := ctx.baseModuleName() + flags.Toolchain.ShlibSuffix()
+			if p.needsStrip(ctx) {
+				stripped := android.PathForModuleOut(ctx, "stripped", libName)
+				p.strip(ctx, in, stripped, builderFlags)
+				in = stripped
+			}
+
+			// Optimize out relinking against shared libraries whose interface hasn't changed by
+			// depending on a table of contents file instead of the library itself.
+			tocFile := android.PathForModuleOut(ctx, libName+".toc")
+			p.tocFile = android.OptionalPathForPath(tocFile)
+			TransformSharedObjectToToc(ctx, in, tocFile, builderFlags)
+		}
+
+		return in
 	}
 
 	return nil
@@ -136,17 +156,26 @@
 	flags Flags, deps PathDeps, objs Objects) android.Path {
 	// TODO(ccross): verify shared library dependencies
 	if len(p.properties.Srcs) > 0 {
-		// TODO(ccross): .toc optimization, stripping, packing
+		builderFlags := flagsToBuilderFlags(flags)
+
+		fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
+		in := p.Prebuilt.SingleSourcePath(ctx)
+
+		p.unstrippedOutputFile = in
+
+		if p.needsStrip(ctx) {
+			stripped := android.PathForModuleOut(ctx, "stripped", fileName)
+			p.strip(ctx, in, stripped, builderFlags)
+			in = stripped
+		}
 
 		// Copy binaries to a name matching the final installed name
-		fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
 		outputFile := android.PathForModuleOut(ctx, fileName)
-
 		ctx.Build(pctx, android.BuildParams{
 			Rule:        android.CpExecutable,
 			Description: "prebuilt",
 			Output:      outputFile,
-			Input:       p.Prebuilt.SingleSourcePath(ctx),
+			Input:       in,
 		})
 
 		return outputFile
diff --git a/cc/relocation_packer.go b/cc/relocation_packer.go
deleted file mode 100644
index 8989b29..0000000
--- a/cc/relocation_packer.go
+++ /dev/null
@@ -1,100 +0,0 @@
-// Copyright 2016 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 cc
-
-import (
-	"runtime"
-
-	"github.com/google/blueprint"
-
-	"android/soong/android"
-)
-
-func init() {
-	pctx.SourcePathVariable("relocationPackerCmd", "prebuilts/misc/${config.HostPrebuiltTag}/relocation_packer/relocation_packer")
-}
-
-var relocationPackerRule = pctx.AndroidStaticRule("packRelocations",
-	blueprint.RuleParams{
-		Command:     "rm -f $out && cp $in $out && $relocationPackerCmd $out",
-		CommandDeps: []string{"$relocationPackerCmd"},
-	})
-
-type RelocationPackerProperties struct {
-	// Generate compact dynamic relocation table, default true.
-	Pack_relocations *bool `android:"arch_variant"`
-
-	// This will be true even if we're embedded in Make, in which case
-	// we'll defer to make to actually do the packing.
-	PackingRelocations bool `blueprint:"mutated"`
-
-	// Use clang lld instead of gnu ld.
-	Use_clang_lld *bool
-}
-
-type relocationPacker struct {
-	Properties RelocationPackerProperties
-}
-
-func (p *relocationPacker) useClangLld(ctx BaseModuleContext) bool {
-	if p.Properties.Use_clang_lld != nil {
-		return Bool(p.Properties.Use_clang_lld)
-	}
-	return ctx.Config().UseClangLld()
-}
-
-func (p *relocationPacker) packingInit(ctx BaseModuleContext) {
-	enabled := true
-	// Relocation packer isn't available on Darwin yet
-	if runtime.GOOS == "darwin" {
-		enabled = false
-	}
-	if ctx.Target().Os != android.Android {
-		enabled = false
-	}
-	if ctx.Config().Getenv("DISABLE_RELOCATION_PACKER") == "true" {
-		enabled = false
-	}
-	// Relocation packer does not work with lld output files.
-	// Packed files won't load.
-	if p.useClangLld(ctx) {
-		enabled = false
-	}
-	if ctx.useSdk() {
-		enabled = false
-	}
-	if p.Properties.Pack_relocations != nil &&
-		*p.Properties.Pack_relocations == false {
-		enabled = false
-	}
-
-	p.Properties.PackingRelocations = enabled
-}
-
-func (p *relocationPacker) needsPacking(ctx ModuleContext) bool {
-	if ctx.Config().EmbeddedInMake() {
-		return false
-	}
-	return p.Properties.PackingRelocations
-}
-
-func (p *relocationPacker) pack(ctx ModuleContext, in, out android.ModuleOutPath, flags builderFlags) {
-	ctx.Build(pctx, android.BuildParams{
-		Rule:        relocationPackerRule,
-		Description: "pack relocations",
-		Output:      out,
-		Input:       in,
-	})
-}
diff --git a/cc/sanitize.go b/cc/sanitize.go
index a522a60..b2fc63f 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -40,6 +40,9 @@
 
 	cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
 		"-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
+	// -flto and -fvisibility are required by clang when -fsanitize=cfi is
+	// used, but have no effect on assembly files
+	cfiAsflags = []string{"-flto", "-fvisibility=default"}
 	cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
 		"-Wl,-plugin-opt,O1"}
 	cfiExportsMapPath     = "build/soong/cc/config/cfi_exports.map"
@@ -460,6 +463,7 @@
 		sanitizers = append(sanitizers, "cfi")
 
 		flags.CFlags = append(flags.CFlags, cfiCflags...)
+		flags.AsFlags = append(flags.AsFlags, cfiAsflags...)
 		// Only append the default visibility flag if -fvisibility has not already been set
 		// to hidden.
 		if !inList("-fvisibility=hidden", flags.CFlags) {
@@ -495,6 +499,7 @@
 		sanitizeArg := "-fsanitize=" + strings.Join(sanitizers, ",")
 
 		flags.CFlags = append(flags.CFlags, sanitizeArg)
+		flags.AsFlags = append(flags.AsFlags, sanitizeArg)
 		if ctx.Host() {
 			flags.CFlags = append(flags.CFlags, "-fno-sanitize-recover=all")
 			flags.LdFlags = append(flags.LdFlags, sanitizeArg)
diff --git a/cc/stl.go b/cc/stl.go
index 6f63835..f44902e 100644
--- a/cc/stl.go
+++ b/cc/stl.go
@@ -19,18 +19,24 @@
 	"fmt"
 )
 
-func getNdkStlFamily(ctx android.ModuleContext, m *Module) string {
+func getNdkStlFamily(m *Module) string {
+	family, _ := getNdkStlFamilyAndLinkType(m)
+	return family
+}
+
+func getNdkStlFamilyAndLinkType(m *Module) (string, string) {
 	stl := m.stl.Properties.SelectedStl
 	switch stl {
-	case "ndk_libc++_shared", "ndk_libc++_static":
-		return "libc++"
+	case "ndk_libc++_shared":
+		return "libc++", "shared"
+	case "ndk_libc++_static":
+		return "libc++", "static"
 	case "ndk_system":
-		return "system"
+		return "system", "shared"
 	case "":
-		return "none"
+		return "none", "none"
 	default:
-		ctx.ModuleErrorf("stl: %q is not a valid STL", stl)
-		return ""
+		panic(fmt.Errorf("stl: %q is not a valid STL", stl))
 	}
 }
 
@@ -207,10 +213,9 @@
 	hostDynamicGccLibs = map[android.OsType][]string{
 		android.Linux:  []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
 		android.Darwin: []string{"-lc", "-lSystem"},
-		android.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
-			"-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
-			"-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
-			"-lmsvcrt"},
+		android.Windows: []string{"-lmingw32", "-lgcc", "-lmoldname", "-lmingwex", "-lmsvcr110",
+			"-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32", "-lkernel32", "-lmingw32",
+			"-lgcc", "-lmoldname", "-lmingwex", "-lmsvcrt"},
 	}
 	hostStaticGccLibs = map[android.OsType][]string{
 		android.Linux:   []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
diff --git a/cc/strip.go b/cc/strip.go
index a7c2d4e..02397f4 100644
--- a/cc/strip.go
+++ b/cc/strip.go
@@ -21,6 +21,7 @@
 type StripProperties struct {
 	Strip struct {
 		None         *bool
+		All          *bool
 		Keep_symbols *bool
 	}
 }
@@ -30,17 +31,23 @@
 }
 
 func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
-	return !ctx.Config().EmbeddedInMake() && !Bool(stripper.StripProperties.Strip.None)
+	// TODO(ccross): enable host stripping when embedded in make?  Make never had support for stripping host binaries.
+	return (!ctx.Config().EmbeddedInMake() || ctx.Device()) && !Bool(stripper.StripProperties.Strip.None)
 }
 
-func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
+func (stripper *stripper) strip(ctx ModuleContext, in android.Path, out android.ModuleOutPath,
 	flags builderFlags) {
 	if ctx.Darwin() {
 		TransformDarwinStrip(ctx, in, out)
 	} else {
-		flags.stripKeepSymbols = Bool(stripper.StripProperties.Strip.Keep_symbols)
-		// TODO(ccross): don't add gnu debuglink for user builds
-		flags.stripAddGnuDebuglink = true
+		if Bool(stripper.StripProperties.Strip.Keep_symbols) {
+			flags.stripKeepSymbols = true
+		} else if !Bool(stripper.StripProperties.Strip.All) {
+			flags.stripKeepMiniDebugInfo = true
+		}
+		if ctx.Config().Debuggable() && !flags.stripKeepMiniDebugInfo {
+			flags.stripAddGnuDebuglink = true
+		}
 		TransformStrip(ctx, in, out, flags)
 	}
 }
diff --git a/cc/test.go b/cc/test.go
index b67341e..96049db 100644
--- a/cc/test.go
+++ b/cc/test.go
@@ -26,6 +26,9 @@
 type TestProperties struct {
 	// if set, build against the gtest library. Defaults to true.
 	Gtest *bool
+
+	// if set, use the isolated gtest runner. Defaults to false.
+	Isolated *bool
 }
 
 type TestBinaryProperties struct {
@@ -49,6 +52,10 @@
 	// the name of the test configuration (for example "AndroidTest.xml") that should be
 	// installed with the module.
 	Test_config *string `android:"arch_variant"`
+
+	// the name of the test configuration template (for example "AndroidTestTemplate.xml") that
+	// should be installed with the module.
+	Test_config_template *string `android:"arch_variant"`
 }
 
 func init() {
@@ -164,6 +171,8 @@
 	if test.gtest() {
 		if ctx.useSdk() && ctx.Device() {
 			deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
+		} else if BoolDefault(test.Properties.Isolated, false) {
+			deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
 		} else {
 			deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
 		}
@@ -220,6 +229,7 @@
 func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
 	android.ExtractSourcesDeps(ctx, test.Properties.Data)
 	android.ExtractSourceDeps(ctx, test.Properties.Test_config)
+	android.ExtractSourceDeps(ctx, test.Properties.Test_config_template)
 
 	deps = test.testDecorator.linkerDeps(ctx, deps)
 	deps = test.binaryDecorator.linkerDeps(ctx, deps)
@@ -234,7 +244,8 @@
 
 func (test *testBinary) install(ctx ModuleContext, file android.Path) {
 	test.data = ctx.ExpandSources(test.Properties.Data, nil)
-	test.testConfig = tradefed.AutoGenNativeTestConfig(ctx, test.Properties.Test_config)
+	test.testConfig = tradefed.AutoGenNativeTestConfig(ctx, test.Properties.Test_config,
+		test.Properties.Test_config_template)
 
 	test.binaryDecorator.baseInstaller.dir = "nativetest"
 	test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
@@ -317,6 +328,10 @@
 	// the name of the test configuration (for example "AndroidTest.xml") that should be
 	// installed with the module.
 	Test_config *string `android:"arch_variant"`
+
+	// the name of the test configuration template (for example "AndroidTestTemplate.xml") that
+	// should be installed with the module.
+	Test_config_template *string `android:"arch_variant"`
 }
 
 type benchmarkDecorator struct {
@@ -344,6 +359,7 @@
 func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
 	android.ExtractSourcesDeps(ctx, benchmark.Properties.Data)
 	android.ExtractSourceDeps(ctx, benchmark.Properties.Test_config)
+	android.ExtractSourceDeps(ctx, benchmark.Properties.Test_config_template)
 
 	deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
 	deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
@@ -352,7 +368,8 @@
 
 func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
 	benchmark.data = ctx.ExpandSources(benchmark.Properties.Data, nil)
-	benchmark.testConfig = tradefed.AutoGenNativeBenchmarkTestConfig(ctx, benchmark.Properties.Test_config)
+	benchmark.testConfig = tradefed.AutoGenNativeBenchmarkTestConfig(ctx, benchmark.Properties.Test_config,
+		benchmark.Properties.Test_config_template)
 
 	benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
 	benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
diff --git a/cc/tidy.go b/cc/tidy.go
index 491cc22..0a6b413 100644
--- a/cc/tidy.go
+++ b/cc/tidy.go
@@ -69,9 +69,14 @@
 
 	flags.Tidy = true
 
+	// Add global WITH_TIDY_FLAGS and local tidy_flags.
+	withTidyFlags := ctx.Config().Getenv("WITH_TIDY_FLAGS")
+	if len(withTidyFlags) > 0 {
+		flags.TidyFlags = append(flags.TidyFlags, withTidyFlags)
+	}
 	esc := proptools.NinjaAndShellEscape
-
 	flags.TidyFlags = append(flags.TidyFlags, esc(tidy.Properties.Tidy_flags)...)
+	// If TidyFlags is empty, add default header filter.
 	if len(flags.TidyFlags) == 0 {
 		headerFilter := "-header-filter=\"(" + ctx.ModuleDir() + "|${config.TidyDefaultHeaderDirs})\""
 		flags.TidyFlags = append(flags.TidyFlags, headerFilter)
diff --git a/cc/util.go b/cc/util.go
index 93cf536..bab4d32 100644
--- a/cc/util.go
+++ b/cc/util.go
@@ -105,7 +105,7 @@
 	return list
 }
 
-var shlibVersionPattern = regexp.MustCompile("(?:\\.\\d+)+")
+var shlibVersionPattern = regexp.MustCompile("(?:\\.\\d+(?:svn)?)+")
 
 // splitFileExt splits a file name into root, suffix and ext. root stands for the file name without
 // the file extension and the version number (e.g. "libexample"). suffix stands for the
diff --git a/cc/util_test.go b/cc/util_test.go
index 3108294..7c718ea 100644
--- a/cc/util_test.go
+++ b/cc/util_test.go
@@ -35,6 +35,22 @@
 		}
 	})
 
+	t.Run("soname with svn version", func(t *testing.T) {
+		root, suffix, ext := splitFileExt("libtest.so.1svn")
+		expected := "libtest"
+		if root != expected {
+			t.Errorf("root should be %q but got %q", expected, root)
+		}
+		expected = ".so.1svn"
+		if suffix != expected {
+			t.Errorf("suffix should be %q but got %q", expected, suffix)
+		}
+		expected = ".so"
+		if ext != expected {
+			t.Errorf("ext should be %q but got %q", expected, ext)
+		}
+	})
+
 	t.Run("version numbers in the middle should be ignored", func(t *testing.T) {
 		root, suffix, ext := splitFileExt("libtest.1.0.30.so")
 		expected := "libtest.1.0.30"
diff --git a/cc/vndk_prebuilt.go b/cc/vndk_prebuilt.go
index 849bb3f..2d7274d 100644
--- a/cc/vndk_prebuilt.go
+++ b/cc/vndk_prebuilt.go
@@ -149,6 +149,7 @@
 	module.stl = nil
 	module.sanitize = nil
 	library.StripProperties.Strip.None = BoolPtr(true)
+	module.Properties.UseVndk = true
 
 	prebuilt := &vndkPrebuiltLibraryDecorator{
 		libraryDecorator: library,
diff --git a/cmd/javac_wrapper/javac_wrapper.go b/cmd/javac_wrapper/javac_wrapper.go
index 4df4938..7a448ba 100644
--- a/cmd/javac_wrapper/javac_wrapper.go
+++ b/cmd/javac_wrapper/javac_wrapper.go
@@ -176,4 +176,12 @@
 	regexp.MustCompile(`Note: (Some input files|.*\.java) uses? unchecked or unsafe operations.`),
 	regexp.MustCompile(`Note: Recompile with -Xlint:unchecked for details.`),
 	regexp.MustCompile(`bootstrap class path not set in conjunction with -source`),
+
+	regexp.MustCompile(`javadoc: warning - The old Doclet and Taglet APIs in the packages`),
+	regexp.MustCompile(`com.sun.javadoc, com.sun.tools.doclets and their implementations`),
+	regexp.MustCompile(`are planned to be removed in a future JDK release. These`),
+	regexp.MustCompile(`components have been superseded by the new APIs in jdk.javadoc.doclet.`),
+	regexp.MustCompile(`Users are strongly recommended to migrate to the new APIs.`),
+
+	regexp.MustCompile(`javadoc: option --boot-class-path not allowed with target 1.9`),
 }
diff --git a/cmd/javac_wrapper/javac_wrapper_test.go b/cmd/javac_wrapper/javac_wrapper_test.go
index d76793f..ad657e7 100644
--- a/cmd/javac_wrapper/javac_wrapper_test.go
+++ b/cmd/javac_wrapper/javac_wrapper_test.go
@@ -64,6 +64,17 @@
 		in:  "\n",
 		out: "\n",
 	},
+	{
+		in: `
+javadoc: warning - The old Doclet and Taglet APIs in the packages
+com.sun.javadoc, com.sun.tools.doclets and their implementations
+are planned to be removed in a future JDK release. These
+components have been superseded by the new APIs in jdk.javadoc.doclet.
+Users are strongly recommended to migrate to the new APIs.
+javadoc: option --boot-class-path not allowed with target 1.9
+`,
+		out: "\n",
+	},
 }
 
 func TestJavacColorize(t *testing.T) {
diff --git a/cmd/merge_zips/merge_zips.go b/cmd/merge_zips/merge_zips.go
index 95ff70b..f383de9 100644
--- a/cmd/merge_zips/merge_zips.go
+++ b/cmd/merge_zips/merge_zips.go
@@ -250,7 +250,12 @@
 			addMapping(jar.MetaDir, dirSource)
 		}
 
-		fh, buf, err := jar.ManifestFileContents(manifest)
+		contents, err := ioutil.ReadFile(manifest)
+		if err != nil {
+			return err
+		}
+
+		fh, buf, err := jar.ManifestFileContents(contents)
 		if err != nil {
 			return err
 		}
diff --git a/cmd/multiproduct_kati/main.go b/cmd/multiproduct_kati/main.go
index 813c060..9cb75fa 100644
--- a/cmd/multiproduct_kati/main.go
+++ b/cmd/multiproduct_kati/main.go
@@ -18,6 +18,7 @@
 	"context"
 	"flag"
 	"fmt"
+	"io"
 	"io/ioutil"
 	"os"
 	"path/filepath"
@@ -27,6 +28,7 @@
 	"syscall"
 	"time"
 
+	"android/soong/finder"
 	"android/soong/ui/build"
 	"android/soong/ui/logger"
 	"android/soong/ui/status"
@@ -49,6 +51,7 @@
 var numJobs = flag.Int("j", detectNumJobs(), "number of parallel kati jobs")
 
 var keepArtifacts = flag.Bool("keep", false, "keep archives of artifacts")
+var incremental = flag.Bool("incremental", false, "run in incremental mode (saving intermediates)")
 
 var outDir = flag.String("out", "", "path to store output directories (defaults to tmpdir under $OUT when empty)")
 var alternateResultDir = flag.Bool("dist", false, "write select results to $DIST_DIR (or <out>/dist when empty)")
@@ -64,13 +67,6 @@
 const errorLeadingLines = 20
 const errorTrailingLines = 20
 
-type Product struct {
-	ctx     build.Context
-	config  build.Config
-	logFile string
-	action  *status.Action
-}
-
 func errMsgFromLog(filename string) string {
 	if filename == "" {
 		return ""
@@ -131,6 +127,34 @@
 	return false
 }
 
+func copyFile(from, to string) error {
+	fromFile, err := os.Open(from)
+	if err != nil {
+		return err
+	}
+	defer fromFile.Close()
+
+	toFile, err := os.Create(to)
+	if err != nil {
+		return err
+	}
+	defer toFile.Close()
+
+	_, err = io.Copy(toFile, fromFile)
+	return err
+}
+
+type mpContext struct {
+	Context context.Context
+	Logger  logger.Logger
+	Status  status.ToolStatus
+	Tracer  tracer.Tracer
+	Finder  *finder.Finder
+	Config  build.Config
+
+	LogsDir string
+}
+
 func main() {
 	writer := terminal.NewWriter(terminal.StdioImpl{})
 	defer writer.Finish()
@@ -169,7 +193,10 @@
 
 	config := build.NewConfig(buildCtx)
 	if *outDir == "" {
-		name := "multiproduct-" + time.Now().Format("20060102150405")
+		name := "multiproduct"
+		if !*incremental {
+			name += "-" + time.Now().Format("20060102150405")
+		}
 
 		*outDir = filepath.Join(config.OutDir(), name)
 
@@ -231,7 +258,7 @@
 		productsList = allProducts
 	}
 
-	products := make([]string, 0, len(productsList))
+	finalProductsList := make([]string, 0, len(productsList))
 	skipList := strings.Split(*skipProducts, ",")
 	skipProduct := func(p string) bool {
 		for _, s := range skipList {
@@ -243,136 +270,54 @@
 	}
 	for _, product := range productsList {
 		if !skipProduct(product) {
-			products = append(products, product)
+			finalProductsList = append(finalProductsList, product)
 		} else {
 			log.Verbose("Skipping: ", product)
 		}
 	}
 
-	log.Verbose("Got product list: ", products)
+	log.Verbose("Got product list: ", finalProductsList)
 
 	s := buildCtx.Status.StartTool()
-	s.SetTotalActions(len(products))
+	s.SetTotalActions(len(finalProductsList))
 
-	var wg sync.WaitGroup
-	productConfigs := make(chan Product, len(products))
+	mpCtx := &mpContext{
+		Context: ctx,
+		Logger:  log,
+		Status:  s,
+		Tracer:  trace,
 
-	// Run the product config for every product in parallel
-	for _, product := range products {
-		wg.Add(1)
-		go func(product string) {
-			var stdLog string
+		Finder: finder,
+		Config: config,
 
-			defer wg.Done()
-
-			action := &status.Action{
-				Description: product,
-				Outputs:     []string{product},
-			}
-			s.StartAction(action)
-			defer logger.Recover(func(err error) {
-				s.FinishAction(status.ActionResult{
-					Action: action,
-					Error:  err,
-					Output: errMsgFromLog(stdLog),
-				})
-			})
-
-			productOutDir := filepath.Join(config.OutDir(), product)
-			productLogDir := filepath.Join(logsDir, product)
-
-			if err := os.MkdirAll(productOutDir, 0777); err != nil {
-				log.Fatalf("Error creating out directory: %v", err)
-			}
-			if err := os.MkdirAll(productLogDir, 0777); err != nil {
-				log.Fatalf("Error creating log directory: %v", err)
-			}
-
-			stdLog = filepath.Join(productLogDir, "std.log")
-			f, err := os.Create(stdLog)
-			if err != nil {
-				log.Fatalf("Error creating std.log: %v", err)
-			}
-
-			productLog := logger.New(f)
-			productLog.SetOutput(filepath.Join(productLogDir, "soong.log"))
-
-			productCtx := build.Context{ContextImpl: &build.ContextImpl{
-				Context: ctx,
-				Logger:  productLog,
-				Tracer:  trace,
-				Writer:  terminal.NewWriter(terminal.NewCustomStdio(nil, f, f)),
-				Thread:  trace.NewThread(product),
-				Status:  &status.Status{},
-			}}
-			productCtx.Status.AddOutput(terminal.NewStatusOutput(productCtx.Writer, ""))
-
-			productConfig := build.NewConfig(productCtx, flag.Args()...)
-			productConfig.Environment().Set("OUT_DIR", productOutDir)
-			build.FindSources(productCtx, productConfig, finder)
-			productConfig.Lunch(productCtx, product, *buildVariant)
-
-			build.Build(productCtx, productConfig, build.BuildProductConfig)
-			productConfigs <- Product{productCtx, productConfig, stdLog, action}
-		}(product)
+		LogsDir: logsDir,
 	}
+
+	products := make(chan string, len(productsList))
 	go func() {
-		defer close(productConfigs)
-		wg.Wait()
+		defer close(products)
+		for _, product := range finalProductsList {
+			products <- product
+		}
 	}()
 
-	var wg2 sync.WaitGroup
-	// Then run up to numJobs worth of Soong and Kati
+	var wg sync.WaitGroup
 	for i := 0; i < *numJobs; i++ {
-		wg2.Add(1)
+		wg.Add(1)
 		go func() {
-			defer wg2.Done()
-			for product := range productConfigs {
-				func() {
-					defer logger.Recover(func(err error) {
-						s.FinishAction(status.ActionResult{
-							Action: product.action,
-							Error:  err,
-							Output: errMsgFromLog(product.logFile),
-						})
-					})
-
-					defer func() {
-						if *keepArtifacts {
-							args := zip.ZipArgs{
-								FileArgs: []zip.FileArg{
-									{
-										GlobDir:             product.config.OutDir(),
-										SourcePrefixToStrip: product.config.OutDir(),
-									},
-								},
-								OutputFilePath:   filepath.Join(config.OutDir(), product.config.TargetProduct()+".zip"),
-								NumParallelJobs:  runtime.NumCPU(),
-								CompressionLevel: 5,
-							}
-							if err := zip.Run(args); err != nil {
-								log.Fatalf("Error zipping artifacts: %v", err)
-							}
-						}
-						os.RemoveAll(product.config.OutDir())
-					}()
-
-					buildWhat := 0
-					if !*onlyConfig {
-						buildWhat |= build.BuildSoong
-						if !*onlySoong {
-							buildWhat |= build.BuildKati
-						}
+			defer wg.Done()
+			for {
+				select {
+				case product := <-products:
+					if product == "" {
+						return
 					}
-					build.Build(product.ctx, product.config, buildWhat)
-					s.FinishAction(status.ActionResult{
-						Action: product.action,
-					})
-				}()
+					buildProduct(mpCtx, product)
+				}
 			}
 		}()
 	}
-	wg2.Wait()
+	wg.Wait()
 
 	if *alternateResultDir {
 		args := zip.ZipArgs{
@@ -383,7 +328,7 @@
 			NumParallelJobs:  runtime.NumCPU(),
 			CompressionLevel: 5,
 		}
-		if err := zip.Run(args); err != nil {
+		if err := zip.Zip(args); err != nil {
 			log.Fatalf("Error zipping logs: %v", err)
 		}
 	}
@@ -399,6 +344,111 @@
 	}
 }
 
+func buildProduct(mpctx *mpContext, product string) {
+	var stdLog string
+
+	outDir := filepath.Join(mpctx.Config.OutDir(), product)
+	logsDir := filepath.Join(mpctx.LogsDir, product)
+
+	if err := os.MkdirAll(outDir, 0777); err != nil {
+		mpctx.Logger.Fatalf("Error creating out directory: %v", err)
+	}
+	if err := os.MkdirAll(logsDir, 0777); err != nil {
+		mpctx.Logger.Fatalf("Error creating log directory: %v", err)
+	}
+
+	stdLog = filepath.Join(logsDir, "std.log")
+	f, err := os.Create(stdLog)
+	if err != nil {
+		mpctx.Logger.Fatalf("Error creating std.log: %v", err)
+	}
+	defer f.Close()
+
+	log := logger.New(f)
+	defer log.Cleanup()
+	log.SetOutput(filepath.Join(logsDir, "soong.log"))
+
+	action := &status.Action{
+		Description: product,
+		Outputs:     []string{product},
+	}
+	mpctx.Status.StartAction(action)
+	defer logger.Recover(func(err error) {
+		mpctx.Status.FinishAction(status.ActionResult{
+			Action: action,
+			Error:  err,
+			Output: errMsgFromLog(stdLog),
+		})
+	})
+
+	ctx := build.Context{ContextImpl: &build.ContextImpl{
+		Context: mpctx.Context,
+		Logger:  log,
+		Tracer:  mpctx.Tracer,
+		Writer:  terminal.NewWriter(terminal.NewCustomStdio(nil, f, f)),
+		Thread:  mpctx.Tracer.NewThread(product),
+		Status:  &status.Status{},
+	}}
+	ctx.Status.AddOutput(terminal.NewStatusOutput(ctx.Writer, ""))
+
+	config := build.NewConfig(ctx, flag.Args()...)
+	config.Environment().Set("OUT_DIR", outDir)
+	build.FindSources(ctx, config, mpctx.Finder)
+	config.Lunch(ctx, product, *buildVariant)
+
+	defer func() {
+		if *keepArtifacts {
+			args := zip.ZipArgs{
+				FileArgs: []zip.FileArg{
+					{
+						GlobDir:             outDir,
+						SourcePrefixToStrip: outDir,
+					},
+				},
+				OutputFilePath:   filepath.Join(mpctx.Config.OutDir(), product+".zip"),
+				NumParallelJobs:  runtime.NumCPU(),
+				CompressionLevel: 5,
+			}
+			if err := zip.Zip(args); err != nil {
+				log.Fatalf("Error zipping artifacts: %v", err)
+			}
+		}
+		if *incremental {
+			// Save space, Kati doesn't notice
+			if f := config.KatiBuildNinjaFile(); f != "" {
+				os.Truncate(f, 0)
+			}
+		} else {
+			os.RemoveAll(outDir)
+		}
+	}()
+
+	buildWhat := build.BuildProductConfig
+	if !*onlyConfig {
+		buildWhat |= build.BuildSoong
+		if !*onlySoong {
+			buildWhat |= build.BuildKati
+		}
+	}
+
+	before := time.Now()
+	build.Build(ctx, config, buildWhat)
+
+	// Save std_full.log if Kati re-read the makefiles
+	if buildWhat&build.BuildKati != 0 {
+		if after, err := os.Stat(config.KatiBuildNinjaFile()); err == nil && after.ModTime().After(before) {
+			err := copyFile(stdLog, filepath.Join(filepath.Dir(stdLog), "std_full.log"))
+			if err != nil {
+				log.Fatalf("Error copying log file: %s", err)
+			}
+		}
+	}
+
+	mpctx.Status.FinishAction(status.ActionResult{
+		Action: action,
+	})
+}
+
 type failureCount int
 
 func (f *failureCount) StartAction(action *status.Action, counts status.Counts) {}
diff --git a/cmd/pom2bp/pom2bp.go b/cmd/pom2bp/pom2bp.go
index a66a424..a79c84f 100644
--- a/cmd/pom2bp/pom2bp.go
+++ b/cmd/pom2bp/pom2bp.go
@@ -186,6 +186,34 @@
 	return !p.IsHostModule()
 }
 
+func (p Pom) ModuleType() string {
+	if p.IsAar() {
+		return "android_library"
+	} else if p.IsHostModule() {
+		return "java_library_host"
+	} else {
+		return "java_library_static"
+	}
+}
+
+func (p Pom) ImportModuleType() string {
+	if p.IsAar() {
+		return "android_library_import"
+	} else if p.IsHostModule() {
+		return "java_import_host"
+	} else {
+		return "java_import"
+	}
+}
+
+func (p Pom) ImportProperty() string {
+	if p.IsAar() {
+		return "aars"
+	} else {
+		return "jars"
+	}
+}
+
 func (p Pom) BpName() string {
 	if p.BpTarget == "" {
 		p.BpTarget = rewriteNames.MavenToBp(p.GroupId, p.ArtifactId)
@@ -293,27 +321,43 @@
 }
 
 var bpTemplate = template.Must(template.New("bp").Parse(`
-{{if .IsAar}}android_library_import{{else if .IsDeviceModule}}java_import{{else}}java_import_host{{end}} {
+{{.ImportModuleType}} {
     name: "{{.BpName}}-nodeps",
-    {{if .IsAar}}aars{{else}}jars{{end}}: ["{{.ArtifactFile}}"],
-    sdk_version: "{{.SdkVersion}}",{{if .IsAar}}
+    {{.ImportProperty}}: ["{{.ArtifactFile}}"],
+    sdk_version: "{{.SdkVersion}}",
+    {{- if .IsAar}}
     min_sdk_version: "{{.MinSdkVersion}}",
-    static_libs: [{{range .BpAarDeps}}
-        "{{.}}",{{end}}{{range .BpExtraDeps}}
-        "{{.}}",{{end}}
-    ],{{end}}
+    static_libs: [
+        {{- range .BpAarDeps}}
+        "{{.}}",
+        {{- end}}
+        {{- range .BpExtraDeps}}
+        "{{.}}",
+        {{- end}}
+    ],
+    {{- end}}
 }
 
-{{if .IsAar}}android_library{{else if .IsDeviceModule}}java_library_static{{else}}java_library_host{{end}} {
-    name: "{{.BpName}}",{{if .IsDeviceModule}}
-    sdk_version: "{{.SdkVersion}}",{{if .IsAar}}
+{{.ModuleType}} {
+    name: "{{.BpName}}",
+    {{- if .IsDeviceModule}}
+    sdk_version: "{{.SdkVersion}}",
+    {{- if .IsAar}}
     min_sdk_version: "{{.MinSdkVersion}}",
-    manifest: "manifests/{{.BpName}}/AndroidManifest.xml",{{end}}{{end}}
+    manifest: "manifests/{{.BpName}}/AndroidManifest.xml",
+    {{- end}}
+    {{- end}}
     static_libs: [
-        "{{.BpName}}-nodeps",{{range .BpJarDeps}}
-        "{{.}}",{{end}}{{range .BpAarDeps}}
-        "{{.}}",{{end}}{{range .BpExtraDeps}}
-        "{{.}}",{{end}}
+        "{{.BpName}}-nodeps",
+         {{- range .BpJarDeps}}
+        "{{.}}",
+        {{- end}}
+        {{- range .BpAarDeps}}
+        "{{.}}",
+        {{- end}}
+        {{- range .BpExtraDeps}}
+        "{{.}}",
+        {{- end}}
     ],
     java_version: "1.7",
 }
diff --git a/genrule/genrule.go b/genrule/genrule.go
index 8fedc60..e3823c5 100644
--- a/genrule/genrule.go
+++ b/genrule/genrule.go
@@ -344,6 +344,17 @@
 	g.outputDeps = append(g.outputDeps, task.out[0])
 }
 
+// Collect information for opening IDE project files in java/jdeps.go.
+func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
+	dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
+	for _, src := range g.properties.Srcs {
+		if strings.HasPrefix(src, ":") {
+			src = strings.Trim(src, ":")
+			dpInfo.Deps = append(dpInfo.Deps, src)
+		}
+	}
+}
+
 func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
 	module := &Module{
 		taskGenerator: taskGenerator,
diff --git a/jar/jar.go b/jar/jar.go
index 653e5ee..fa0e693 100644
--- a/jar/jar.go
+++ b/jar/jar.go
@@ -17,7 +17,6 @@
 import (
 	"bytes"
 	"fmt"
-	"io/ioutil"
 	"os"
 	"strings"
 	"time"
@@ -81,10 +80,9 @@
 	return dirHeader
 }
 
-// Convert manifest source path to zip header and contents.  If path is empty uses a default
-// manifest.
-func ManifestFileContents(src string) (*zip.FileHeader, []byte, error) {
-	b, err := manifestContents(src)
+// Create a manifest zip header and contents using the provided contents if any.
+func ManifestFileContents(contents []byte) (*zip.FileHeader, []byte, error) {
+	b, err := manifestContents(contents)
 	if err != nil {
 		return nil, nil, err
 	}
@@ -100,26 +98,16 @@
 	return fh, b, nil
 }
 
-// Convert manifest source path to contents.  If path is empty uses a default manifest.
-func manifestContents(src string) ([]byte, error) {
-	var givenBytes []byte
-	var err error
-
-	if src != "" {
-		givenBytes, err = ioutil.ReadFile(src)
-		if err != nil {
-			return nil, err
-		}
-	}
-
+// Create manifest contents, using the provided contents if any.
+func manifestContents(contents []byte) ([]byte, error) {
 	manifestMarker := []byte("Manifest-Version:")
 	header := append(manifestMarker, []byte(" 1.0\nCreated-By: soong_zip\n")...)
 
 	var finalBytes []byte
-	if !bytes.Contains(givenBytes, manifestMarker) {
-		finalBytes = append(append(header, givenBytes...), byte('\n'))
+	if !bytes.Contains(contents, manifestMarker) {
+		finalBytes = append(append(header, contents...), byte('\n'))
 	} else {
-		finalBytes = givenBytes
+		finalBytes = contents
 	}
 
 	return finalBytes, nil
diff --git a/java/android_manifest.go b/java/android_manifest.go
index 6577475..168a22d 100644
--- a/java/android_manifest.go
+++ b/java/android_manifest.go
@@ -25,10 +25,14 @@
 
 var manifestFixerRule = pctx.AndroidStaticRule("manifestFixer",
 	blueprint.RuleParams{
-		Command:     `${config.ManifestFixerCmd} --minSdkVersion ${minSdkVersion} $args $in $out`,
+		Command: `${config.ManifestFixerCmd} ` +
+			`--minSdkVersion ${minSdkVersion} ` +
+			`--targetSdkVersion ${targetSdkVersion} ` +
+			`--raise-min-sdk-version ` +
+			`$args $in $out`,
 		CommandDeps: []string{"${config.ManifestFixerCmd}"},
 	},
-	"minSdkVersion", "args")
+	"minSdkVersion", "targetSdkVersion", "args")
 
 var manifestMergerRule = pctx.AndroidStaticRule("manifestMerger",
 	blueprint.RuleParams{
@@ -53,8 +57,9 @@
 		Input:  manifest,
 		Output: fixedManifest,
 		Args: map[string]string{
-			"minSdkVersion": sdkVersionOrDefault(ctx, sdkContext.minSdkVersion()),
-			"args":          strings.Join(args, " "),
+			"minSdkVersion":    sdkVersionOrDefault(ctx, sdkContext.minSdkVersion()),
+			"targetSdkVersion": sdkVersionOrDefault(ctx, sdkContext.sdkVersion()),
+			"args":             strings.Join(args, " "),
 		},
 	})
 	manifest = fixedManifest
diff --git a/java/androidmk.go b/java/androidmk.go
index 877d840..313a144 100644
--- a/java/androidmk.go
+++ b/java/androidmk.go
@@ -70,7 +70,7 @@
 				// Temporary hack: export sources used to compile framework.jar to Make
 				// to be used for droiddoc
 				// TODO(ccross): remove this once droiddoc is in soong
-				if library.Name() == "framework" {
+				if (library.Name() == "framework") || (library.Name() == "framework-annotation-proc") {
 					fmt.Fprintln(w, "SOONG_FRAMEWORK_SRCS :=", strings.Join(library.compiledJavaSrcs.Strings(), " "))
 					fmt.Fprintln(w, "SOONG_FRAMEWORK_SRCJARS :=", strings.Join(library.compiledSrcJars.Strings(), " "))
 				}
@@ -319,15 +319,12 @@
 		Include:    "$(BUILD_SYSTEM)/soong_droiddoc_prebuilt.mk",
 		Extra: []android.AndroidMkExtraFunc{
 			func(w io.Writer, outputFile android.Path) {
-				if BoolDefault(ddoc.Javadoc.properties.Installable, true) {
+				if BoolDefault(ddoc.Javadoc.properties.Installable, true) && ddoc.Javadoc.docZip != nil {
 					fmt.Fprintln(w, "LOCAL_DROIDDOC_DOC_ZIP := ", ddoc.Javadoc.docZip.String())
 				}
 				if ddoc.Javadoc.stubsSrcJar != nil {
 					fmt.Fprintln(w, "LOCAL_DROIDDOC_STUBS_SRCJAR := ", ddoc.Javadoc.stubsSrcJar.String())
 				}
-				if ddoc.annotationsZip != nil {
-					fmt.Fprintln(w, "LOCAL_DROIDDOC_ANNOTATIONS_ZIP := ", ddoc.annotationsZip.String())
-				}
 				if ddoc.checkCurrentApiTimestamp != nil {
 					fmt.Fprintln(w, ".PHONY:", ddoc.Name()+"-check-current-api")
 					fmt.Fprintln(w, ddoc.Name()+"-check-current-api:",
@@ -387,6 +384,81 @@
 	}
 }
 
+func (dstubs *Droidstubs) AndroidMk() android.AndroidMkData {
+	return android.AndroidMkData{
+		Class:      "JAVA_LIBRARIES",
+		OutputFile: android.OptionalPathForPath(dstubs.stubsSrcJar),
+		Include:    "$(BUILD_SYSTEM)/soong_droiddoc_prebuilt.mk",
+		Extra: []android.AndroidMkExtraFunc{
+			func(w io.Writer, outputFile android.Path) {
+				if dstubs.Javadoc.stubsSrcJar != nil {
+					fmt.Fprintln(w, "LOCAL_DROIDDOC_STUBS_SRCJAR := ", dstubs.Javadoc.stubsSrcJar.String())
+				}
+				if dstubs.apiVersionsXml != nil {
+					fmt.Fprintln(w, "LOCAL_DROIDDOC_API_VERSIONS_XML := ", dstubs.apiVersionsXml.String())
+				}
+				if dstubs.annotationsZip != nil {
+					fmt.Fprintln(w, "LOCAL_DROIDDOC_ANNOTATIONS_ZIP := ", dstubs.annotationsZip.String())
+				}
+				if dstubs.jdiffDocZip != nil {
+					fmt.Fprintln(w, "LOCAL_DROIDDOC_JDIFF_DOC_ZIP := ", dstubs.jdiffDocZip.String())
+				}
+				if dstubs.checkCurrentApiTimestamp != nil {
+					fmt.Fprintln(w, ".PHONY:", dstubs.Name()+"-check-current-api")
+					fmt.Fprintln(w, dstubs.Name()+"-check-current-api:",
+						dstubs.checkCurrentApiTimestamp.String())
+
+					fmt.Fprintln(w, ".PHONY: checkapi")
+					fmt.Fprintln(w, "checkapi:",
+						dstubs.checkCurrentApiTimestamp.String())
+
+					fmt.Fprintln(w, ".PHONY: droidcore")
+					fmt.Fprintln(w, "droidcore: checkapi")
+				}
+				if dstubs.updateCurrentApiTimestamp != nil {
+					fmt.Fprintln(w, ".PHONY:", dstubs.Name()+"-update-current-api")
+					fmt.Fprintln(w, dstubs.Name()+"-update-current-api:",
+						dstubs.updateCurrentApiTimestamp.String())
+
+					fmt.Fprintln(w, ".PHONY: update-api")
+					fmt.Fprintln(w, "update-api:",
+						dstubs.updateCurrentApiTimestamp.String())
+				}
+				if dstubs.checkLastReleasedApiTimestamp != nil {
+					fmt.Fprintln(w, ".PHONY:", dstubs.Name()+"-check-last-released-api")
+					fmt.Fprintln(w, dstubs.Name()+"-check-last-released-api:",
+						dstubs.checkLastReleasedApiTimestamp.String())
+				}
+				apiFilePrefix := "INTERNAL_PLATFORM_"
+				if String(dstubs.properties.Api_tag_name) != "" {
+					apiFilePrefix += String(dstubs.properties.Api_tag_name) + "_"
+				}
+				if dstubs.apiFile != nil {
+					fmt.Fprintln(w, apiFilePrefix+"API_FILE := ", dstubs.apiFile.String())
+				}
+				if dstubs.dexApiFile != nil {
+					fmt.Fprintln(w, apiFilePrefix+"DEX_API_FILE := ", dstubs.dexApiFile.String())
+				}
+				if dstubs.privateApiFile != nil {
+					fmt.Fprintln(w, apiFilePrefix+"PRIVATE_API_FILE := ", dstubs.privateApiFile.String())
+				}
+				if dstubs.privateDexApiFile != nil {
+					fmt.Fprintln(w, apiFilePrefix+"PRIVATE_DEX_API_FILE := ", dstubs.privateDexApiFile.String())
+				}
+				if dstubs.removedApiFile != nil {
+					fmt.Fprintln(w, apiFilePrefix+"REMOVED_API_FILE := ", dstubs.removedApiFile.String())
+				}
+				if dstubs.removedDexApiFile != nil {
+					fmt.Fprintln(w, apiFilePrefix+"REMOVED_DEX_API_FILE := ", dstubs.removedDexApiFile.String())
+				}
+				if dstubs.exactApiFile != nil {
+					fmt.Fprintln(w, apiFilePrefix+"EXACT_API_FILE := ", dstubs.exactApiFile.String())
+				}
+			},
+		},
+	}
+}
+
 func androidMkWriteTestData(data android.Paths, ret *android.AndroidMkData) {
 	var testFiles []string
 	for _, d := range data {
diff --git a/java/app.go b/java/app.go
index 3f72ec9..f8bef1c 100644
--- a/java/app.go
+++ b/java/app.go
@@ -236,12 +236,13 @@
 
 	a.generateAndroidBuildActions(ctx)
 
-	a.testConfig = tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config, a.manifestPath)
+	a.testConfig = tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config, a.testProperties.Test_config_template, a.manifestPath)
 	a.data = ctx.ExpandSources(a.testProperties.Data, nil)
 }
 
 func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) {
 	android.ExtractSourceDeps(ctx, a.testProperties.Test_config)
+	android.ExtractSourceDeps(ctx, a.testProperties.Test_config_template)
 	android.ExtractSourcesDeps(ctx, a.testProperties.Data)
 	a.AndroidApp.DepsMutator(ctx)
 }
@@ -250,6 +251,8 @@
 	module := &AndroidTest{}
 
 	module.Module.deviceProperties.Optimize.Enabled = proptools.BoolPtr(true)
+
+	module.Module.properties.Instrument = true
 	module.Module.properties.Installable = proptools.BoolPtr(true)
 
 	module.AddProperties(
diff --git a/java/app_builder.go b/java/app_builder.go
index 954ca44..e27b1b7 100644
--- a/java/app_builder.go
+++ b/java/app_builder.go
@@ -103,10 +103,10 @@
 			`cp ${manifest} ${outDir}/AndroidManifest.xml && ` +
 			`cp ${classesJar} ${outDir}/classes.jar && ` +
 			`cp ${rTxt} ${outDir}/R.txt && ` +
-			`${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir} ${resArgs}`,
+			`${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`,
 		CommandDeps: []string{"${config.SoongZipCmd}"},
 	},
-	"manifest", "classesJar", "rTxt", "resArgs", "outDir")
+	"manifest", "classesJar", "rTxt", "outDir")
 
 func BuildAAR(ctx android.ModuleContext, outputFile android.WritablePath,
 	classesJar, manifest, rTxt android.Path, res android.Paths) {
diff --git a/java/builder.go b/java/builder.go
index c645ee2..07af8eb 100644
--- a/java/builder.go
+++ b/java/builder.go
@@ -24,6 +24,7 @@
 	"strings"
 
 	"github.com/google/blueprint"
+	"github.com/google/blueprint/proptools"
 
 	"android/soong/android"
 )
@@ -61,12 +62,12 @@
 
 	kotlinc = pctx.AndroidGomaStaticRule("kotlinc",
 		blueprint.RuleParams{
-			Command: `rm -rf "$outDir" "$srcJarDir" && mkdir -p "$outDir" "$srcJarDir" && ` +
+			Command: `rm -rf "$classesDir" "$srcJarDir" "$kotlinBuildFile" && mkdir -p "$classesDir" "$srcJarDir" && ` +
 				`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
-				`${config.GenKotlinBuildFileCmd} $classpath $outDir $out.rsp $srcJarDir/list > $outDir/kotlinc-build.xml &&` +
+				`${config.GenKotlinBuildFileCmd} $classpath $classesDir $out.rsp $srcJarDir/list > $kotlinBuildFile &&` +
 				`${config.KotlincCmd} $kotlincFlags ` +
-				`-jvm-target $kotlinJvmTarget -Xbuild-file=$outDir/kotlinc-build.xml && ` +
-				`${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`,
+				`-jvm-target $kotlinJvmTarget -Xbuild-file=$kotlinBuildFile && ` +
+				`${config.SoongZipCmd} -jar -o $out -C $classesDir -D $classesDir`,
 			CommandDeps: []string{
 				"${config.KotlincCmd}",
 				"${config.KotlinCompilerJar}",
@@ -77,7 +78,7 @@
 			Rspfile:        "$out.rsp",
 			RspfileContent: `$in`,
 		},
-		"kotlincFlags", "classpath", "srcJars", "srcJarDir", "outDir", "kotlinJvmTarget")
+		"kotlincFlags", "classpath", "srcJars", "srcJarDir", "classesDir", "kotlinJvmTarget", "kotlinBuildFile")
 
 	turbine = pctx.AndroidStaticRule("turbine",
 		blueprint.RuleParams{
@@ -173,11 +174,12 @@
 		Inputs:      inputs,
 		Implicits:   deps,
 		Args: map[string]string{
-			"classpath":    flags.kotlincClasspath.FormJavaClassPath("-classpath"),
-			"kotlincFlags": flags.kotlincFlags,
-			"srcJars":      strings.Join(srcJars.Strings(), " "),
-			"outDir":       android.PathForModuleOut(ctx, "kotlinc", "classes").String(),
-			"srcJarDir":    android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(),
+			"classpath":       flags.kotlincClasspath.FormJavaClassPath("-classpath"),
+			"kotlincFlags":    flags.kotlincFlags,
+			"srcJars":         strings.Join(srcJars.Strings(), " "),
+			"classesDir":      android.PathForModuleOut(ctx, "kotlinc", "classes").String(),
+			"srcJarDir":       android.PathForModuleOut(ctx, "kotlinc", "srcJars").String(),
+			"kotlinBuildFile": android.PathForModuleOut(ctx, "kotlinc-build.xml").String(),
 			// http://b/69160377 kotlinc only supports -jvm-target 1.6 and 1.8
 			"kotlinJvmTarget": "1.8",
 		},
@@ -319,7 +321,7 @@
 		Output:      outputFile,
 		Implicits:   deps,
 		Args: map[string]string{
-			"jarArgs": strings.Join(jarArgs, " "),
+			"jarArgs": strings.Join(proptools.NinjaAndShellEscape(jarArgs), " "),
 		},
 	})
 }
diff --git a/java/dex.go b/java/dex.go
index 054a176..ce0c18e 100644
--- a/java/dex.go
+++ b/java/dex.go
@@ -25,8 +25,8 @@
 var d8 = pctx.AndroidStaticRule("d8",
 	blueprint.RuleParams{
 		Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
-			`${config.D8Cmd} --output $outDir $dxFlags $in && ` +
-			`${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` +
+			`${config.D8Cmd} --output $outDir $d8Flags $in && ` +
+			`${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
 			`${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`,
 		CommandDeps: []string{
 			"${config.D8Cmd}",
@@ -34,7 +34,7 @@
 			"${config.MergeZipsCmd}",
 		},
 	},
-	"outDir", "dxFlags")
+	"outDir", "d8Flags")
 
 var r8 = pctx.AndroidStaticRule("r8",
 	blueprint.RuleParams{
@@ -42,10 +42,11 @@
 			`rm -f "$outDict" && ` +
 			`${config.R8Cmd} -injars $in --output $outDir ` +
 			`--force-proguard-compatibility ` +
+			`--no-data-resources ` +
 			`-printmapping $outDict ` +
-			`$dxFlags $r8Flags && ` +
+			`$r8Flags && ` +
 			`touch "$outDict" && ` +
-			`${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` +
+			`${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -f "$outDir/classes*.dex" && ` +
 			`${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`,
 		CommandDeps: []string{
 			"${config.R8Cmd}",
@@ -53,9 +54,9 @@
 			"${config.MergeZipsCmd}",
 		},
 	},
-	"outDir", "outDict", "dxFlags", "r8Flags")
+	"outDir", "outDict", "r8Flags")
 
-func (j *Module) dxFlags(ctx android.ModuleContext) []string {
+func (j *Module) dexCommonFlags(ctx android.ModuleContext) []string {
 	flags := j.deviceProperties.Dxflags
 	// Translate all the DX flags to D8 ones until all the build files have been migrated
 	// to D8 flags. See: b/69377755
@@ -81,6 +82,19 @@
 	return flags
 }
 
+func (j *Module) d8Flags(ctx android.ModuleContext, flags javaBuilderFlags) ([]string, android.Paths) {
+	d8Flags := j.dexCommonFlags(ctx)
+
+	d8Flags = append(d8Flags, flags.bootClasspath.FormTurbineClasspath("--lib")...)
+	d8Flags = append(d8Flags, flags.classpath.FormTurbineClasspath("--lib")...)
+
+	var d8Deps android.Paths
+	d8Deps = append(d8Deps, flags.bootClasspath...)
+	d8Deps = append(d8Deps, flags.classpath...)
+
+	return d8Flags, d8Deps
+}
+
 func (j *Module) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Flags []string, r8Deps android.Paths) {
 	opt := j.deviceProperties.Optimize
 
@@ -96,11 +110,17 @@
 		proguardRaiseDeps = append(proguardRaiseDeps, dep.(Dependency).HeaderJars()...)
 	})
 
+	r8Flags = append(r8Flags, j.dexCommonFlags(ctx)...)
+
 	r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars"))
 	r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars"))
 	r8Flags = append(r8Flags, flags.classpath.FormJavaClassPath("-libraryjars"))
 	r8Flags = append(r8Flags, "-forceprocessing")
 
+	r8Deps = append(r8Deps, proguardRaiseDeps...)
+	r8Deps = append(r8Deps, flags.bootClasspath...)
+	r8Deps = append(r8Deps, flags.classpath...)
+
 	flagFiles := android.Paths{
 		android.PathForSource(ctx, "build/make/core/proguard.flags"),
 	}
@@ -146,8 +166,6 @@
 
 	useR8 := Bool(j.deviceProperties.Optimize.Enabled)
 
-	dxFlags := j.dxFlags(ctx)
-
 	// Compile classes.jar into classes.dex and then javalib.jar
 	javalibJar := android.PathForModuleOut(ctx, "dex", jarName)
 	outDir := android.PathForModuleOut(ctx, "dex")
@@ -166,20 +184,21 @@
 			Input:          classesJar,
 			Implicits:      r8Deps,
 			Args: map[string]string{
-				"dxFlags": strings.Join(dxFlags, " "),
 				"r8Flags": strings.Join(r8Flags, " "),
 				"outDict": j.proguardDictionary.String(),
 				"outDir":  outDir.String(),
 			},
 		})
 	} else {
+		d8Flags, d8Deps := j.d8Flags(ctx, flags)
 		ctx.Build(pctx, android.BuildParams{
 			Rule:        d8,
 			Description: "d8",
 			Output:      javalibJar,
 			Input:       classesJar,
+			Implicits:   d8Deps,
 			Args: map[string]string{
-				"dxFlags": strings.Join(dxFlags, " "),
+				"d8Flags": strings.Join(d8Flags, " "),
 				"outDir":  outDir.String(),
 			},
 		})
diff --git a/java/droiddoc.go b/java/droiddoc.go
index 68d7861..cc2043d 100644
--- a/java/droiddoc.go
+++ b/java/droiddoc.go
@@ -30,8 +30,8 @@
 		blueprint.RuleParams{
 			Command: `rm -rf "$outDir" "$srcJarDir" "$stubsDir" && mkdir -p "$outDir" "$srcJarDir" "$stubsDir" && ` +
 				`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
-				`${config.JavadocCmd} -encoding UTF-8 @$out.rsp @$srcJarDir/list ` +
-				`$opts $bootclasspathArgs $classpathArgs -sourcepath $sourcepath ` +
+				`${config.SoongJavacWrapper} ${config.JavadocCmd} -encoding UTF-8 @$out.rsp @$srcJarDir/list ` +
+				`$opts $bootclasspathArgs $classpathArgs $sourcepathArgs ` +
 				`-d $outDir -quiet  && ` +
 				`${config.SoongZipCmd} -write_if_changed -d -o $docZip -C $outDir -D $outDir && ` +
 				`${config.SoongZipCmd} -write_if_changed -jar -o $out -C $stubsDir -D $stubsDir $postDoclavaCmds`,
@@ -40,12 +40,13 @@
 				"${config.JavadocCmd}",
 				"${config.SoongZipCmd}",
 			},
-			Rspfile:        "$out.rsp",
-			RspfileContent: "$in",
-			Restat:         true,
+			CommandOrderOnly: []string{"${config.SoongJavacWrapper}"},
+			Rspfile:          "$out.rsp",
+			RspfileContent:   "$in",
+			Restat:           true,
 		},
 		"outDir", "srcJarDir", "stubsDir", "srcJars", "opts",
-		"bootclasspathArgs", "classpathArgs", "sourcepath", "docZip", "postDoclavaCmds")
+		"bootclasspathArgs", "classpathArgs", "sourcepathArgs", "docZip", "postDoclavaCmds")
 
 	apiCheck = pctx.AndroidStaticRule("apiCheck",
 		blueprint.RuleParams{
@@ -60,41 +61,39 @@
 
 	updateApi = pctx.AndroidStaticRule("updateApi",
 		blueprint.RuleParams{
-			Command: `( ( cp -f $apiFileToCheck $apiFile && cp -f $removedApiFileToCheck $removedApiFile ) ` +
+			Command: `( ( cp -f $srcApiFile $destApiFile && cp -f $srcRemovedApiFile $destRemovedApiFile ) ` +
 				`&& touch $out ) || (echo failed to update public API ; exit 38)`,
 		},
-		"apiFile", "apiFileToCheck", "removedApiFile", "removedApiFileToCheck")
+		"srcApiFile", "destApiFile", "srcRemovedApiFile", "destRemovedApiFile")
 
 	metalava = pctx.AndroidStaticRule("metalava",
 		blueprint.RuleParams{
-			Command: `rm -rf "$outDir" "$srcJarDir" "$stubsDir" "$docStubsDir" && ` +
-				`mkdir -p "$outDir" "$srcJarDir" "$stubsDir" "$docStubsDir" && ` +
+			Command: `rm -rf "$outDir" "$srcJarDir" "$stubsDir" && ` +
+				`mkdir -p "$outDir" "$srcJarDir" "$stubsDir" && ` +
 				`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
 				`${config.JavaCmd} -jar ${config.MetalavaJar} -encoding UTF-8 -source $javaVersion @$out.rsp @$srcJarDir/list ` +
-				`$bootclasspathArgs $classpathArgs -sourcepath $sourcepath --no-banner --color --quiet ` +
-				`--stubs $stubsDir $opts && ` +
-				`${config.SoongZipCmd} -write_if_changed -d -o $docZip -C $outDir -D $outDir && ` +
+				`$bootclasspathArgs $classpathArgs $sourcepathArgs --no-banner --color --quiet ` +
+				`$opts && ` +
 				`${config.SoongZipCmd} -write_if_changed -jar -o $out -C $stubsDir -D $stubsDir`,
 			CommandDeps: []string{
 				"${config.ZipSyncCmd}",
 				"${config.JavaCmd}",
 				"${config.MetalavaJar}",
-				"${config.JavadocCmd}",
 				"${config.SoongZipCmd}",
 			},
 			Rspfile:        "$out.rsp",
 			RspfileContent: "$in",
 			Restat:         true,
 		},
-		"outDir", "srcJarDir", "stubsDir", "docStubsDir", "srcJars", "javaVersion", "bootclasspathArgs",
-		"classpathArgs", "sourcepath", "opts", "docZip")
+		"outDir", "srcJarDir", "stubsDir", "srcJars", "javaVersion", "bootclasspathArgs",
+		"classpathArgs", "sourcepathArgs", "opts")
 
 	metalavaApiCheck = pctx.AndroidStaticRule("metalavaApiCheck",
 		blueprint.RuleParams{
 			Command: `( rm -rf "$srcJarDir" && mkdir -p "$srcJarDir" && ` +
 				`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
 				`${config.JavaCmd} -jar ${config.MetalavaJar} -encoding UTF-8 -source $javaVersion @$out.rsp @$srcJarDir/list ` +
-				`$bootclasspathArgs $classpathArgs -sourcepath $sourcepath --no-banner --color --quiet ` +
+				`$bootclasspathArgs $classpathArgs $sourcepathArgs --no-banner --color --quiet ` +
 				`$opts && touch $out ) || ` +
 				`( echo -e "$msg" ; exit 38 )`,
 			CommandDeps: []string{
@@ -105,17 +104,40 @@
 			Rspfile:        "$out.rsp",
 			RspfileContent: "$in",
 		},
-		"srcJarDir", "srcJars", "javaVersion", "bootclasspathArgs", "classpathArgs", "sourcepath", "opts", "msg")
+		"srcJarDir", "srcJars", "javaVersion", "bootclasspathArgs", "classpathArgs", "sourcepathArgs", "opts", "msg")
+
+	dokka = pctx.AndroidStaticRule("dokka",
+		blueprint.RuleParams{
+			Command: `rm -rf "$outDir" "$srcJarDir" "$stubsDir" && ` +
+				`mkdir -p "$outDir" "$srcJarDir" "$stubsDir" && ` +
+				`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
+				`${config.JavaCmd} -jar ${config.DokkaJar} $srcJarDir ` +
+				`$classpathArgs -format dac -dacRoot /reference/kotlin -output $outDir $opts && ` +
+				`${config.SoongZipCmd} -write_if_changed -d -o $docZip -C $outDir -D $outDir && ` +
+				`${config.SoongZipCmd} -write_if_changed -jar -o $out -C $stubsDir -D $stubsDir`,
+			CommandDeps: []string{
+				"${config.ZipSyncCmd}",
+				"${config.DokkaJar}",
+				"${config.MetalavaJar}",
+				"${config.SoongZipCmd}",
+			},
+			Restat: true,
+		},
+		"outDir", "srcJarDir", "stubsDir", "srcJars", "classpathArgs", "opts", "docZip")
 )
 
 func init() {
 	android.RegisterModuleType("doc_defaults", DocDefaultsFactory)
+	android.RegisterModuleType("stubs_defaults", StubsDefaultsFactory)
 
 	android.RegisterModuleType("droiddoc", DroiddocFactory)
 	android.RegisterModuleType("droiddoc_host", DroiddocHostFactory)
 	android.RegisterModuleType("droiddoc_exported_dir", ExportedDroiddocDirFactory)
 	android.RegisterModuleType("javadoc", JavadocFactory)
 	android.RegisterModuleType("javadoc_host", JavadocHostFactory)
+
+	android.RegisterModuleType("droidstubs", DroidstubsFactory)
+	android.RegisterModuleType("droidstubs_host", DroidstubsHostFactory)
 }
 
 var (
@@ -139,6 +161,10 @@
 	// list of java libraries that will be in the classpath.
 	Libs []string `android:"arch_variant"`
 
+	// don't build against the default libraries (bootclasspath, legacy-test, core-junit,
+	// ext, and framework for device targets)
+	No_standard_libs *bool
+
 	// don't build against the framework libraries (legacy-test, core-junit,
 	// ext, and framework for device targets)
 	No_framework_libs *bool
@@ -168,6 +194,18 @@
 
 	// If not blank, set the java version passed to javadoc as -source
 	Java_version *string
+
+	// local files that are used within user customized droiddoc options.
+	Arg_files []string
+
+	// user customized droiddoc args.
+	// Available variables for substitution:
+	//
+	//  $(location <label>): the path to the arg_files with name <label>
+	Args *string
+
+	// names of the output files used in args that will be generated
+	Out []string
 }
 
 type ApiToCheck struct {
@@ -208,18 +246,6 @@
 	// resources output directory under out/soong/.intermediates.
 	Resourcesoutdir *string
 
-	// local files that are used within user customized droiddoc options.
-	Arg_files []string
-
-	// user customized droiddoc args.
-	// Available variables for substitution:
-	//
-	//  $(location <label>): the path to the arg_files with name <label>
-	Args *string
-
-	// names of the output files used in args that will be generated
-	Out []string
-
 	// if set to true, collect the values used by the Dev tools and
 	// write them in files packaged with the SDK. Defaults to false.
 	Write_sdk_values *bool
@@ -274,31 +300,85 @@
 		Current ApiToCheck
 	}
 
-	// if set to true, create stubs through Metalava instead of Doclava. Javadoc/Doclava is
-	// currently still used for documentation generation, and will be replaced by Dokka soon.
-	Metalava_enabled *bool
+	// if set to true, generate docs through Dokka instead of Doclava.
+	Dokka_enabled *bool
+}
+
+type DroidstubsProperties struct {
+	// the tag name used to distinguish if the API files belong to public/system/test.
+	Api_tag_name *string
+
+	// the generated public API filename by Metalava.
+	Api_filename *string
+
+	// the generated public Dex API filename by Metalava.
+	Dex_api_filename *string
+
+	// the generated private API filename by Metalava.
+	Private_api_filename *string
+
+	// the generated private Dex API filename by Metalava.
+	Private_dex_api_filename *string
+
+	// the generated removed API filename by Metalava.
+	Removed_api_filename *string
+
+	// the generated removed Dex API filename by Metalava.
+	Removed_dex_api_filename *string
+
+	// mapping of dex signatures to source file and line number. This is a temporary property and
+	// will be deleted; you probably shouldn't be using it.
+	Dex_mapping_filename *string
+
+	// the generated exact API filename by Metalava.
+	Exact_api_filename *string
+
+	// the generated proguard filename by Metalava.
+	Proguard_filename *string
+
+	Check_api struct {
+		Last_released ApiToCheck
+
+		Current ApiToCheck
+	}
 
 	// user can specify the version of previous released API file in order to do compatibility check.
-	Metalava_previous_api *string
+	Previous_api *string
 
 	// is set to true, Metalava will allow framework SDK to contain annotations.
-	Metalava_annotations_enabled *bool
+	Annotations_enabled *bool
 
-	// a list of top-level directories containing files to merge annotations from.
-	Metalava_merge_annotations_dirs []string
+	// a list of top-level directories containing files to merge qualifier annotations (i.e. those intended to be included in the stubs written) from.
+	Merge_annotations_dirs []string
 
-	// if set to true, generate docs through Dokka instead of Doclava. Valid only when
-	// metalava_enabled is set to true.
-	Dokka_enabled *bool
+	// a list of top-level directories containing Java stub files to merge show/hide annotations from.
+	Merge_inclusion_annotations_dirs []string
+
+	// if set to true, allow Metalava to generate doc_stubs source files. Defaults to false.
+	Create_doc_stubs *bool
+
+	// is set to true, Metalava will allow framework SDK to contain API levels annotations.
+	Api_levels_annotations_enabled *bool
+
+	// the dirs which Metalava extracts API levels annotations from.
+	Api_levels_annotations_dirs []string
+
+	// if set to true, collect the values used by the Dev tools and
+	// write them in files packaged with the SDK. Defaults to false.
+	Write_sdk_values *bool
+
+	// If set to true, .xml based public API file will be also generated, and
+	// JDiff tool will be invoked to genreate javadoc files. Defaults to false.
+	Jdiff_enabled *bool
 }
 
 //
 // Common flags passed down to build rule
 //
 type droiddocBuilderFlags struct {
-	args               string
 	bootClasspathArgs  string
 	classpathArgs      string
+	sourcepathArgs     string
 	dokkaClasspathArgs string
 	aidlFlags          string
 
@@ -306,11 +386,11 @@
 	doclavaDocsFlags  string
 	postDoclavaCmds   string
 
-	metalavaStubsFlags       string
-	metalavaAnnotationsFlags string
-	metalavaJavadocFlags     string
+	metalavaStubsFlags                string
+	metalavaAnnotationsFlags          string
+	metalavaApiLevelsAnnotationsFlags string
 
-	metalavaDokkaFlags string
+	metalavaApiToXmlFlags string
 }
 
 func InitDroiddocModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
@@ -318,6 +398,39 @@
 	android.InitDefaultableModule(module)
 }
 
+func apiCheckEnabled(apiToCheck ApiToCheck, apiVersionTag string) bool {
+	if String(apiToCheck.Api_file) != "" && String(apiToCheck.Removed_api_file) != "" {
+		return true
+	} else if String(apiToCheck.Api_file) != "" {
+		panic("for " + apiVersionTag + " removed_api_file has to be non-empty!")
+	} else if String(apiToCheck.Removed_api_file) != "" {
+		panic("for " + apiVersionTag + " api_file has to be non-empty!")
+	}
+
+	return false
+}
+
+type ApiFilePath interface {
+	ApiFilePath() android.Path
+}
+
+func transformUpdateApi(ctx android.ModuleContext, destApiFile, destRemovedApiFile,
+	srcApiFile, srcRemovedApiFile android.Path, output android.WritablePath) {
+	ctx.Build(pctx, android.BuildParams{
+		Rule:        updateApi,
+		Description: "Update API",
+		Output:      output,
+		Implicits: append(android.Paths{}, srcApiFile, srcRemovedApiFile,
+			destApiFile, destRemovedApiFile),
+		Args: map[string]string{
+			"destApiFile":        destApiFile.String(),
+			"srcApiFile":         srcApiFile.String(),
+			"destRemovedApiFile": destRemovedApiFile.String(),
+			"srcRemovedApiFile":  srcRemovedApiFile.String(),
+		},
+	})
+}
+
 //
 // Javadoc
 //
@@ -330,6 +443,9 @@
 	srcJars     android.Paths
 	srcFiles    android.Paths
 	sourcepaths android.Paths
+	argFiles    android.Paths
+
+	args string
 
 	docZip      android.WritablePath
 	stubsSrcJar android.WritablePath
@@ -369,20 +485,22 @@
 
 func (j *Javadoc) addDeps(ctx android.BottomUpMutatorContext) {
 	if ctx.Device() {
-		sdkDep := decodeSdkDep(ctx, sdkContext(j))
-		if sdkDep.useDefaultLibs {
-			ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
-			if ctx.Config().TargetOpenJDK9() {
-				ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
+		if !Bool(j.properties.No_standard_libs) {
+			sdkDep := decodeSdkDep(ctx, sdkContext(j))
+			if sdkDep.useDefaultLibs {
+				ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
+				if ctx.Config().TargetOpenJDK9() {
+					ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
+				}
+				if !Bool(j.properties.No_framework_libs) {
+					ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
+				}
+			} else if sdkDep.useModule {
+				if ctx.Config().TargetOpenJDK9() {
+					ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
+				}
+				ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
 			}
-			if !Bool(j.properties.No_framework_libs) {
-				ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
-			}
-		} else if sdkDep.useModule {
-			if ctx.Config().TargetOpenJDK9() {
-				ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
-			}
-			ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
 		}
 	}
 
@@ -395,6 +513,9 @@
 
 	// exclude_srcs may contain filegroup or genrule.
 	android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
+
+	// arg_files may contains filegroup or genrule.
+	android.ExtractSourcesDeps(ctx, j.properties.Arg_files)
 }
 
 func (j *Javadoc) genWhitelistPathPrefixes(whitelistPathPrefixes map[string]bool) {
@@ -514,7 +635,7 @@
 					if _, ok := src.(android.WritablePath); ok { // generated sources
 						deps.srcs = append(deps.srcs, src)
 					} else { // select source path for documentation based on whitelist path prefixs.
-						for k, _ := range whitelistPathPrefixes {
+						for k := range whitelistPathPrefixes {
 							if strings.HasPrefix(src.Rel(), k) {
 								deps.srcs = append(deps.srcs, src)
 								break
@@ -553,11 +674,42 @@
 	j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
 	j.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
 
-	if j.properties.Local_sourcepaths == nil {
+	if j.properties.Local_sourcepaths == nil && len(j.srcFiles) > 0 {
 		j.properties.Local_sourcepaths = append(j.properties.Local_sourcepaths, ".")
 	}
 	j.sourcepaths = android.PathsForModuleSrc(ctx, j.properties.Local_sourcepaths)
 
+	j.argFiles = ctx.ExpandSources(j.properties.Arg_files, nil)
+	argFilesMap := map[string]android.Path{}
+
+	for _, f := range j.argFiles {
+		if _, exists := argFilesMap[f.Rel()]; !exists {
+			argFilesMap[f.Rel()] = f
+		} else {
+			ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
+				f, argFilesMap[f.Rel()], f.Rel())
+		}
+	}
+
+	var err error
+	j.args, err = android.Expand(String(j.properties.Args), func(name string) (string, error) {
+		if strings.HasPrefix(name, "location ") {
+			label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
+			if f, ok := argFilesMap[label]; ok {
+				return f.String(), nil
+			} else {
+				return "", fmt.Errorf("unknown location label %q", label)
+			}
+		} else if name == "genDir" {
+			return android.PathForModuleGen(ctx).String(), nil
+		}
+		return "", fmt.Errorf("unknown variable '$(%s)'", name)
+	})
+
+	if err != nil {
+		ctx.PropertyErrorf("args", "%s", err.Error())
+	}
+
 	return deps
 }
 
@@ -572,7 +724,7 @@
 	implicits = append(implicits, deps.bootClasspath...)
 	implicits = append(implicits, deps.classpath...)
 
-	var bootClasspathArgs, classpathArgs string
+	var bootClasspathArgs, classpathArgs, sourcepathArgs string
 
 	javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
 	if len(deps.bootClasspath) > 0 {
@@ -588,9 +740,12 @@
 	}
 
 	implicits = append(implicits, j.srcJars...)
+	implicits = append(implicits, j.argFiles...)
 
 	opts := "-source " + javaVersion + " -J-Xmx1024m -XDignore.symbol.file -Xdoclint:none"
 
+	sourcepathArgs = "-sourcepath " + strings.Join(j.sourcepaths.Strings(), ":")
+
 	ctx.Build(pctx, android.BuildParams{
 		Rule:           javadoc,
 		Description:    "Javadoc",
@@ -606,7 +761,7 @@
 			"opts":              opts,
 			"bootclasspathArgs": bootClasspathArgs,
 			"classpathArgs":     classpathArgs,
-			"sourcepath":        strings.Join(j.sourcepaths.Strings(), ":"),
+			"sourcepathArgs":    sourcepathArgs,
 			"docZip":            j.docZip.String(),
 		},
 	})
@@ -633,15 +788,9 @@
 	updateCurrentApiTimestamp     android.WritablePath
 	checkLastReleasedApiTimestamp android.WritablePath
 
-	annotationsZip android.WritablePath
-
 	apiFilePath android.Path
 }
 
-type ApiFilePath interface {
-	ApiFilePath() android.Path
-}
-
 func DroiddocFactory() android.Module {
 	module := &Droiddoc{}
 
@@ -666,32 +815,6 @@
 	return d.apiFilePath
 }
 
-func (d *Droiddoc) checkCurrentApi() bool {
-	if String(d.properties.Check_api.Current.Api_file) != "" &&
-		String(d.properties.Check_api.Current.Removed_api_file) != "" {
-		return true
-	} else if String(d.properties.Check_api.Current.Api_file) != "" {
-		panic("check_api.current.removed_api_file: has to be non empty!")
-	} else if String(d.properties.Check_api.Current.Removed_api_file) != "" {
-		panic("check_api.current.api_file: has to be non empty!")
-	}
-
-	return false
-}
-
-func (d *Droiddoc) checkLastReleasedApi() bool {
-	if String(d.properties.Check_api.Last_released.Api_file) != "" &&
-		String(d.properties.Check_api.Last_released.Removed_api_file) != "" {
-		return true
-	} else if String(d.properties.Check_api.Last_released.Api_file) != "" {
-		panic("check_api.last_released.removed_api_file: has to be non empty!")
-	} else if String(d.properties.Check_api.Last_released.Removed_api_file) != "" {
-		panic("check_api.last_released.api_file: has to be non empty!")
-	}
-
-	return false
-}
-
 func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
 	d.Javadoc.addDeps(ctx)
 
@@ -699,9 +822,6 @@
 		ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
 	}
 
-	// arg_files may contains filegroup or genrule.
-	android.ExtractSourcesDeps(ctx, d.properties.Arg_files)
-
 	// knowntags may contain filegroup or genrule.
 	android.ExtractSourcesDeps(ctx, d.properties.Knowntags)
 
@@ -713,25 +833,15 @@
 		android.ExtractSourceDeps(ctx, d.properties.Static_doc_properties)
 	}
 
-	if d.checkCurrentApi() {
+	if apiCheckEnabled(d.properties.Check_api.Current, "current") {
 		android.ExtractSourceDeps(ctx, d.properties.Check_api.Current.Api_file)
 		android.ExtractSourceDeps(ctx, d.properties.Check_api.Current.Removed_api_file)
 	}
 
-	if d.checkLastReleasedApi() {
+	if apiCheckEnabled(d.properties.Check_api.Last_released, "last_released") {
 		android.ExtractSourceDeps(ctx, d.properties.Check_api.Last_released.Api_file)
 		android.ExtractSourceDeps(ctx, d.properties.Check_api.Last_released.Removed_api_file)
 	}
-
-	if String(d.properties.Metalava_previous_api) != "" {
-		android.ExtractSourceDeps(ctx, d.properties.Metalava_previous_api)
-	}
-
-	if len(d.properties.Metalava_merge_annotations_dirs) != 0 {
-		for _, mergeAnnotationsDir := range d.properties.Metalava_merge_annotations_dirs {
-			ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
-		}
-	}
 }
 
 func (d *Droiddoc) initBuilderFlags(ctx android.ModuleContext, implicits *android.Paths,
@@ -741,51 +851,28 @@
 	*implicits = append(*implicits, deps.bootClasspath...)
 	*implicits = append(*implicits, deps.classpath...)
 
-	// continue to use -bootclasspath even if Metalava under -source 1.9 is enabled
-	// since it doesn't support system modules yet.
 	if len(deps.bootClasspath.Strings()) > 0 {
 		// For OpenJDK 8 we can use -bootclasspath to define the core libraries code.
 		flags.bootClasspathArgs = deps.bootClasspath.FormJavaClassPath("-bootclasspath")
 	}
 	flags.classpathArgs = deps.classpath.FormJavaClassPath("-classpath")
-	// Dokka doesn't support boocClasspath, so combine these two classpath vars for Dokka.
+	// Dokka doesn't support bootClasspath, so combine these two classpath vars for Dokka.
 	dokkaClasspath := classpath{}
 	dokkaClasspath = append(dokkaClasspath, deps.bootClasspath...)
 	dokkaClasspath = append(dokkaClasspath, deps.classpath...)
 	flags.dokkaClasspathArgs = dokkaClasspath.FormJavaClassPath("-classpath")
 
-	argFiles := ctx.ExpandSources(d.properties.Arg_files, nil)
-	argFilesMap := map[string]android.Path{}
-
-	for _, f := range argFiles {
-		*implicits = append(*implicits, f)
-		if _, exists := argFilesMap[f.Rel()]; !exists {
-			argFilesMap[f.Rel()] = f
-		} else {
-			ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
-				f, argFilesMap[f.Rel()], f.Rel())
-		}
+	// TODO(nanzhang): Remove this if- statement once we finish migration for all Doclava
+	// based stubs generation.
+	// In the future, all the docs generation depends on Metalava stubs (droidstubs) srcjar
+	// dir. We need add the srcjar dir to -sourcepath arg, so that Javadoc can figure out
+	// the correct package name base path.
+	if len(d.Javadoc.properties.Local_sourcepaths) > 0 {
+		flags.sourcepathArgs = "-sourcepath " + strings.Join(d.Javadoc.sourcepaths.Strings(), ":")
+	} else {
+		flags.sourcepathArgs = "-sourcepath " + android.PathForModuleOut(ctx, "srcjars").String()
 	}
 
-	var err error
-	flags.args, err = android.Expand(String(d.properties.Args), func(name string) (string, error) {
-		if strings.HasPrefix(name, "location ") {
-			label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
-			if f, ok := argFilesMap[label]; ok {
-				return f.String(), nil
-			} else {
-				return "", fmt.Errorf("unknown location label %q", label)
-			}
-		} else if name == "genDir" {
-			return android.PathForModuleGen(ctx).String(), nil
-		}
-		return "", fmt.Errorf("unknown variable '$(%s)'", name)
-	})
-
-	if err != nil {
-		ctx.PropertyErrorf("args", "%s", err.Error())
-		return droiddocBuilderFlags{}, err
-	}
 	return flags, nil
 }
 
@@ -877,69 +964,65 @@
 	return args
 }
 
-func (d *Droiddoc) collectStubsFlags(ctx android.ModuleContext, implicitOutputs *android.WritablePaths) (string, string) {
-	var doclavaFlags, MetalavaFlags string
-	if d.checkCurrentApi() || d.checkLastReleasedApi() || String(d.properties.Api_filename) != "" {
+func (d *Droiddoc) collectStubsFlags(ctx android.ModuleContext,
+	implicitOutputs *android.WritablePaths) string {
+	var doclavaFlags string
+	if apiCheckEnabled(d.properties.Check_api.Current, "current") ||
+		apiCheckEnabled(d.properties.Check_api.Last_released, "last_released") ||
+		String(d.properties.Api_filename) != "" {
 		d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
 		doclavaFlags += " -api " + d.apiFile.String()
-		MetalavaFlags = MetalavaFlags + " --api " + d.apiFile.String()
 		*implicitOutputs = append(*implicitOutputs, d.apiFile)
 		d.apiFilePath = d.apiFile
 	}
 
-	if d.checkCurrentApi() || d.checkLastReleasedApi() || String(d.properties.Removed_api_filename) != "" {
+	if apiCheckEnabled(d.properties.Check_api.Current, "current") ||
+		apiCheckEnabled(d.properties.Check_api.Last_released, "last_released") ||
+		String(d.properties.Removed_api_filename) != "" {
 		d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
 		doclavaFlags += " -removedApi " + d.removedApiFile.String()
-		MetalavaFlags = MetalavaFlags + " --removed-api " + d.removedApiFile.String()
 		*implicitOutputs = append(*implicitOutputs, d.removedApiFile)
 	}
 
 	if String(d.properties.Private_api_filename) != "" {
 		d.privateApiFile = android.PathForModuleOut(ctx, String(d.properties.Private_api_filename))
 		doclavaFlags += " -privateApi " + d.privateApiFile.String()
-		MetalavaFlags = MetalavaFlags + " --private-api " + d.privateApiFile.String()
 		*implicitOutputs = append(*implicitOutputs, d.privateApiFile)
 	}
 
 	if String(d.properties.Dex_api_filename) != "" {
 		d.dexApiFile = android.PathForModuleOut(ctx, String(d.properties.Dex_api_filename))
 		doclavaFlags += " -dexApi " + d.dexApiFile.String()
-		MetalavaFlags += " --dex-api " + d.dexApiFile.String()
 		*implicitOutputs = append(*implicitOutputs, d.dexApiFile)
 	}
 
 	if String(d.properties.Private_dex_api_filename) != "" {
 		d.privateDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Private_dex_api_filename))
 		doclavaFlags += " -privateDexApi " + d.privateDexApiFile.String()
-		MetalavaFlags = MetalavaFlags + " --private-dex-api " + d.privateDexApiFile.String()
 		*implicitOutputs = append(*implicitOutputs, d.privateDexApiFile)
 	}
 
 	if String(d.properties.Removed_dex_api_filename) != "" {
 		d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
 		doclavaFlags += " -removedDexApi " + d.removedDexApiFile.String()
-		MetalavaFlags = MetalavaFlags + " --removed-dex-api " + d.removedDexApiFile.String()
 		*implicitOutputs = append(*implicitOutputs, d.removedDexApiFile)
 	}
 
 	if String(d.properties.Exact_api_filename) != "" {
 		d.exactApiFile = android.PathForModuleOut(ctx, String(d.properties.Exact_api_filename))
 		doclavaFlags += " -exactApi " + d.exactApiFile.String()
-		MetalavaFlags = MetalavaFlags + " --exact-api " + d.exactApiFile.String()
 		*implicitOutputs = append(*implicitOutputs, d.exactApiFile)
 	}
 
 	if String(d.properties.Dex_mapping_filename) != "" {
 		d.apiMappingFile = android.PathForModuleOut(ctx, String(d.properties.Dex_mapping_filename))
 		doclavaFlags += " -apiMapping " + d.apiMappingFile.String()
-		// Omitted: metalava support
 		*implicitOutputs = append(*implicitOutputs, d.apiMappingFile)
 	}
 
 	if String(d.properties.Proguard_filename) != "" {
 		d.proguardFile = android.PathForModuleOut(ctx, String(d.properties.Proguard_filename))
 		doclavaFlags += " -proguard " + d.proguardFile.String()
-		// Omitted: metalava support
 		*implicitOutputs = append(*implicitOutputs, d.proguardFile)
 	}
 
@@ -950,7 +1033,8 @@
 	if Bool(d.properties.Write_sdk_values) {
 		doclavaFlags += " -sdkvalues " + android.PathForModuleOut(ctx, "out").String()
 	}
-	return doclavaFlags, MetalavaFlags
+
+	return doclavaFlags
 }
 
 func (d *Droiddoc) getPostDoclavaCmds(ctx android.ModuleContext, implicits *android.Paths) string {
@@ -973,93 +1057,9 @@
 	return cmds
 }
 
-func (d *Droiddoc) collectMetalavaAnnotationsFlags(
-	ctx android.ModuleContext, implicits *android.Paths, implicitOutputs *android.WritablePaths) string {
-	var flags string
-	if Bool(d.properties.Metalava_annotations_enabled) {
-		if String(d.properties.Metalava_previous_api) == "" {
-			ctx.PropertyErrorf("metalava_previous_api",
-				"has to be non-empty if annotations was enabled!")
-		}
-		previousApi := ctx.ExpandSource(String(d.properties.Metalava_previous_api),
-			"metalava_previous_api")
-		*implicits = append(*implicits, previousApi)
-
-		flags += " --include-annotations --migrate-nullness " + previousApi.String()
-
-		d.annotationsZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"_annotations.zip")
-		*implicitOutputs = append(*implicitOutputs, d.annotationsZip)
-
-		flags += " --extract-annotations " + d.annotationsZip.String()
-
-		if len(d.properties.Metalava_merge_annotations_dirs) == 0 {
-			ctx.PropertyErrorf("metalava_merge_annotations_dirs",
-				"has to be non-empty if annotations was enabled!")
-		}
-		ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
-			if t, ok := m.(*ExportedDroiddocDir); ok {
-				*implicits = append(*implicits, t.deps...)
-				flags += " --merge-annotations " + t.dir.String()
-			} else {
-				ctx.PropertyErrorf("metalava_merge_annotations_dirs",
-					"module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
-			}
-		})
-		// TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
-		flags += " --hide HiddenTypedefConstant --hide SuperfluousPrefix --hide AnnotationExtraction "
-	}
-
-	return flags
-}
-
-func (d *Droiddoc) collectMetalavaJavadocFlags(ctx android.ModuleContext,
-	bootClasspathArgs, classpathArgs, outDir, docStubsDir string) string {
-	return " --doc-stubs " + docStubsDir +
-		" --write-doc-stubs-source-list " + android.PathForModuleOut(ctx, "doc_stubs.srclist").String() +
-		" --generate-documentation ${config.JavadocCmd} -encoding UTF-8 DOC_STUBS_SOURCE_LIST " +
-		bootClasspathArgs + " " + classpathArgs + " " + " -sourcepath " +
-		docStubsDir + " -quiet -d " + outDir
-}
-
-func (d *Droiddoc) collectMetalavaDokkaFlags(ctx android.ModuleContext, implicits *android.Paths,
-	classpathArgs, outDir, docStubsDir string) string {
-	dokka := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "dokka.jar")
-	*implicits = append(*implicits, dokka)
-
-	return " --doc-stubs " + docStubsDir + " --write-doc-stubs-source-list " +
-		android.PathForModuleOut(ctx, "doc_stubs.srclist").String() +
-		" --generate-documentation ${config.JavaCmd} -jar " + dokka.String() + " " +
-		docStubsDir + " " + classpathArgs + " -format dac -dacRoot /reference/kotlin -output " + outDir
-}
-
-func (d *Droiddoc) transformMetalava(ctx android.ModuleContext, implicits android.Paths,
-	implicitOutputs android.WritablePaths, outDir, docStubsDir, javaVersion,
-	bootclasspathArgs, classpathArgs, opts string) {
-	ctx.Build(pctx, android.BuildParams{
-		Rule:            metalava,
-		Description:     "Metalava",
-		Output:          d.Javadoc.stubsSrcJar,
-		Inputs:          d.Javadoc.srcFiles,
-		Implicits:       implicits,
-		ImplicitOutputs: implicitOutputs,
-		Args: map[string]string{
-			"outDir":            outDir,
-			"srcJarDir":         android.PathForModuleOut(ctx, "srcjars").String(),
-			"stubsDir":          android.PathForModuleOut(ctx, "stubsDir").String(),
-			"docStubsDir":       docStubsDir,
-			"srcJars":           strings.Join(d.Javadoc.srcJars.Strings(), " "),
-			"javaVersion":       javaVersion,
-			"bootclasspathArgs": bootclasspathArgs,
-			"classpathArgs":     classpathArgs,
-			"sourcepath":        strings.Join(d.Javadoc.sourcepaths.Strings(), ":"),
-			"docZip":            d.Javadoc.docZip.String(),
-			"opts":              opts,
-		},
-	})
-}
-
 func (d *Droiddoc) transformDoclava(ctx android.ModuleContext, implicits android.Paths,
-	implicitOutputs android.WritablePaths, bootclasspathArgs, classpathArgs, opts, postDoclavaCmds string) {
+	implicitOutputs android.WritablePaths,
+	bootclasspathArgs, classpathArgs, sourcepathArgs, opts, postDoclavaCmds string) {
 	ctx.Build(pctx, android.BuildParams{
 		Rule:            javadoc,
 		Description:     "Doclava",
@@ -1075,7 +1075,7 @@
 			"opts":              opts,
 			"bootclasspathArgs": bootclasspathArgs,
 			"classpathArgs":     classpathArgs,
-			"sourcepath":        strings.Join(d.Javadoc.sourcepaths.Strings(), ":"),
+			"sourcepathArgs":    sourcepathArgs,
 			"docZip":            d.Javadoc.docZip.String(),
 			"postDoclavaCmds":   postDoclavaCmds,
 		},
@@ -1092,19 +1092,445 @@
 		Implicits: append(android.Paths{apiFile, removedApiFile, d.apiFile, d.removedApiFile},
 			checkApiClasspath...),
 		Args: map[string]string{
+			"msg":                   msg,
 			"classpath":             checkApiClasspath.FormJavaClassPath(""),
 			"opts":                  opts,
 			"apiFile":               apiFile.String(),
 			"apiFileToCheck":        d.apiFile.String(),
 			"removedApiFile":        removedApiFile.String(),
 			"removedApiFileToCheck": d.removedApiFile.String(),
-			"msg":                   msg,
 		},
 	})
 }
 
-func (d *Droiddoc) transformMetalavaCheckApi(ctx android.ModuleContext, apiFile, removedApiFile android.Path,
-	implicits android.Paths, javaVersion, bootclasspathArgs, classpathArgs, opts, msg string,
+func (d *Droiddoc) transformDokka(ctx android.ModuleContext, implicits android.Paths,
+	classpathArgs, opts string) {
+	ctx.Build(pctx, android.BuildParams{
+		Rule:        dokka,
+		Description: "Dokka",
+		Output:      d.Javadoc.stubsSrcJar,
+		Inputs:      d.Javadoc.srcFiles,
+		Implicits:   implicits,
+		Args: map[string]string{
+			"outDir":        android.PathForModuleOut(ctx, "out").String(),
+			"srcJarDir":     android.PathForModuleOut(ctx, "srcjars").String(),
+			"stubsDir":      android.PathForModuleOut(ctx, "stubsDir").String(),
+			"srcJars":       strings.Join(d.Javadoc.srcJars.Strings(), " "),
+			"classpathArgs": classpathArgs,
+			"opts":          opts,
+			"docZip":        d.Javadoc.docZip.String(),
+		},
+	})
+}
+
+func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+	deps := d.Javadoc.collectDeps(ctx)
+
+	jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
+	doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
+	java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
+	checkApiClasspath := classpath{jsilver, doclava, android.PathForSource(ctx, java8Home, "lib/tools.jar")}
+
+	var implicits android.Paths
+	implicits = append(implicits, d.Javadoc.srcJars...)
+	implicits = append(implicits, d.Javadoc.argFiles...)
+
+	var implicitOutputs android.WritablePaths
+	implicitOutputs = append(implicitOutputs, d.Javadoc.docZip)
+	for _, o := range d.Javadoc.properties.Out {
+		implicitOutputs = append(implicitOutputs, android.PathForModuleGen(ctx, o))
+	}
+
+	flags, err := d.initBuilderFlags(ctx, &implicits, deps)
+	if err != nil {
+		return
+	}
+
+	flags.doclavaStubsFlags = d.collectStubsFlags(ctx, &implicitOutputs)
+	if Bool(d.properties.Dokka_enabled) {
+		d.transformDokka(ctx, implicits, flags.classpathArgs, d.Javadoc.args)
+	} else {
+		flags.doclavaDocsFlags = d.collectDoclavaDocsFlags(ctx, &implicits, jsilver, doclava)
+		flags.postDoclavaCmds = d.getPostDoclavaCmds(ctx, &implicits)
+		d.transformDoclava(ctx, implicits, implicitOutputs, flags.bootClasspathArgs, flags.classpathArgs,
+			flags.sourcepathArgs, flags.doclavaDocsFlags+flags.doclavaStubsFlags+" "+d.Javadoc.args,
+			flags.postDoclavaCmds)
+	}
+
+	if apiCheckEnabled(d.properties.Check_api.Current, "current") &&
+		!ctx.Config().IsPdkBuild() {
+		apiFile := ctx.ExpandSource(String(d.properties.Check_api.Current.Api_file),
+			"check_api.current.api_file")
+		removedApiFile := ctx.ExpandSource(String(d.properties.Check_api.Current.Removed_api_file),
+			"check_api.current_removed_api_file")
+
+		d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
+		d.transformCheckApi(ctx, apiFile, removedApiFile, checkApiClasspath,
+			fmt.Sprintf(`\n******************************\n`+
+				`You have tried to change the API from what has been previously approved.\n\n`+
+				`To make these errors go away, you have two choices:\n`+
+				`   1. You can add '@hide' javadoc comments to the methods, etc. listed in the\n`+
+				`      errors above.\n\n`+
+				`   2. You can update current.txt by executing the following command:\n`+
+				`         make %s-update-current-api\n\n`+
+				`      To submit the revised current.txt to the main Android repository,\n`+
+				`      you will need approval.\n`+
+				`******************************\n`, ctx.ModuleName()), String(d.properties.Check_api.Current.Args),
+			d.checkCurrentApiTimestamp)
+
+		d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
+		transformUpdateApi(ctx, apiFile, removedApiFile, d.apiFile, d.removedApiFile,
+			d.updateCurrentApiTimestamp)
+	}
+
+	if apiCheckEnabled(d.properties.Check_api.Last_released, "last_released") &&
+		!ctx.Config().IsPdkBuild() {
+		apiFile := ctx.ExpandSource(String(d.properties.Check_api.Last_released.Api_file),
+			"check_api.last_released.api_file")
+		removedApiFile := ctx.ExpandSource(String(d.properties.Check_api.Last_released.Removed_api_file),
+			"check_api.last_released.removed_api_file")
+
+		d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
+		d.transformCheckApi(ctx, apiFile, removedApiFile, checkApiClasspath,
+			`\n******************************\n`+
+				`You have tried to change the API from what has been previously released in\n`+
+				`an SDK.  Please fix the errors listed above.\n`+
+				`******************************\n`, String(d.properties.Check_api.Last_released.Args),
+			d.checkLastReleasedApiTimestamp)
+	}
+}
+
+//
+// Droidstubs
+//
+type Droidstubs struct {
+	Javadoc
+
+	properties             DroidstubsProperties
+	apiFile                android.WritablePath
+	apiXmlFile             android.WritablePath
+	lastReleasedApiXmlFile android.WritablePath
+	dexApiFile             android.WritablePath
+	privateApiFile         android.WritablePath
+	privateDexApiFile      android.WritablePath
+	removedApiFile         android.WritablePath
+	removedDexApiFile      android.WritablePath
+	apiMappingFile         android.WritablePath
+	exactApiFile           android.WritablePath
+	proguardFile           android.WritablePath
+
+	checkCurrentApiTimestamp      android.WritablePath
+	updateCurrentApiTimestamp     android.WritablePath
+	checkLastReleasedApiTimestamp android.WritablePath
+
+	annotationsZip android.WritablePath
+	apiVersionsXml android.WritablePath
+
+	apiFilePath android.Path
+
+	jdiffDocZip      android.WritablePath
+	jdiffStubsSrcJar android.WritablePath
+}
+
+func DroidstubsFactory() android.Module {
+	module := &Droidstubs{}
+
+	module.AddProperties(&module.properties,
+		&module.Javadoc.properties)
+
+	InitDroiddocModule(module, android.HostAndDeviceSupported)
+	return module
+}
+
+func DroidstubsHostFactory() android.Module {
+	module := &Droidstubs{}
+
+	module.AddProperties(&module.properties,
+		&module.Javadoc.properties)
+
+	InitDroiddocModule(module, android.HostSupported)
+	return module
+}
+
+func (d *Droidstubs) ApiFilePath() android.Path {
+	return d.apiFilePath
+}
+
+func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
+	d.Javadoc.addDeps(ctx)
+
+	if apiCheckEnabled(d.properties.Check_api.Current, "current") {
+		android.ExtractSourceDeps(ctx, d.properties.Check_api.Current.Api_file)
+		android.ExtractSourceDeps(ctx, d.properties.Check_api.Current.Removed_api_file)
+	}
+
+	if apiCheckEnabled(d.properties.Check_api.Last_released, "last_released") {
+		android.ExtractSourceDeps(ctx, d.properties.Check_api.Last_released.Api_file)
+		android.ExtractSourceDeps(ctx, d.properties.Check_api.Last_released.Removed_api_file)
+	}
+
+	if String(d.properties.Previous_api) != "" {
+		android.ExtractSourceDeps(ctx, d.properties.Previous_api)
+	}
+
+	if len(d.properties.Merge_annotations_dirs) != 0 {
+		for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
+			ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
+		}
+	}
+
+	if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
+		for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
+			ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
+		}
+	}
+
+	if len(d.properties.Api_levels_annotations_dirs) != 0 {
+		for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
+			ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
+		}
+	}
+}
+
+func (d *Droidstubs) initBuilderFlags(ctx android.ModuleContext, implicits *android.Paths,
+	deps deps) (droiddocBuilderFlags, error) {
+	var flags droiddocBuilderFlags
+
+	*implicits = append(*implicits, deps.bootClasspath...)
+	*implicits = append(*implicits, deps.classpath...)
+
+	// continue to use -bootclasspath even if Metalava under -source 1.9 is enabled
+	// since it doesn't support system modules yet.
+	if len(deps.bootClasspath.Strings()) > 0 {
+		// For OpenJDK 8 we can use -bootclasspath to define the core libraries code.
+		flags.bootClasspathArgs = deps.bootClasspath.FormJavaClassPath("-bootclasspath")
+	}
+	flags.classpathArgs = deps.classpath.FormJavaClassPath("-classpath")
+
+	flags.sourcepathArgs = "-sourcepath " + strings.Join(d.Javadoc.sourcepaths.Strings(), ":")
+
+	return flags, nil
+}
+
+func (d *Droidstubs) collectStubsFlags(ctx android.ModuleContext,
+	implicitOutputs *android.WritablePaths) string {
+	var metalavaFlags string
+	if apiCheckEnabled(d.properties.Check_api.Current, "current") ||
+		apiCheckEnabled(d.properties.Check_api.Last_released, "last_released") ||
+		String(d.properties.Api_filename) != "" {
+		d.apiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.txt")
+		metalavaFlags = metalavaFlags + " --api " + d.apiFile.String()
+		*implicitOutputs = append(*implicitOutputs, d.apiFile)
+		d.apiFilePath = d.apiFile
+	}
+
+	if apiCheckEnabled(d.properties.Check_api.Current, "current") ||
+		apiCheckEnabled(d.properties.Check_api.Last_released, "last_released") ||
+		String(d.properties.Removed_api_filename) != "" {
+		d.removedApiFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_removed.txt")
+		metalavaFlags = metalavaFlags + " --removed-api " + d.removedApiFile.String()
+		*implicitOutputs = append(*implicitOutputs, d.removedApiFile)
+	}
+
+	if String(d.properties.Private_api_filename) != "" {
+		d.privateApiFile = android.PathForModuleOut(ctx, String(d.properties.Private_api_filename))
+		metalavaFlags = metalavaFlags + " --private-api " + d.privateApiFile.String()
+		*implicitOutputs = append(*implicitOutputs, d.privateApiFile)
+	}
+
+	if String(d.properties.Dex_api_filename) != "" {
+		d.dexApiFile = android.PathForModuleOut(ctx, String(d.properties.Dex_api_filename))
+		metalavaFlags += " --dex-api " + d.dexApiFile.String()
+		*implicitOutputs = append(*implicitOutputs, d.dexApiFile)
+	}
+
+	if String(d.properties.Private_dex_api_filename) != "" {
+		d.privateDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Private_dex_api_filename))
+		metalavaFlags = metalavaFlags + " --private-dex-api " + d.privateDexApiFile.String()
+		*implicitOutputs = append(*implicitOutputs, d.privateDexApiFile)
+	}
+
+	if String(d.properties.Removed_dex_api_filename) != "" {
+		d.removedDexApiFile = android.PathForModuleOut(ctx, String(d.properties.Removed_dex_api_filename))
+		metalavaFlags = metalavaFlags + " --removed-dex-api " + d.removedDexApiFile.String()
+		*implicitOutputs = append(*implicitOutputs, d.removedDexApiFile)
+	}
+
+	if String(d.properties.Exact_api_filename) != "" {
+		d.exactApiFile = android.PathForModuleOut(ctx, String(d.properties.Exact_api_filename))
+		metalavaFlags = metalavaFlags + " --exact-api " + d.exactApiFile.String()
+		*implicitOutputs = append(*implicitOutputs, d.exactApiFile)
+	}
+
+	if String(d.properties.Dex_mapping_filename) != "" {
+		d.apiMappingFile = android.PathForModuleOut(ctx, String(d.properties.Dex_mapping_filename))
+		metalavaFlags = metalavaFlags + " --dex-api-mapping " + d.apiMappingFile.String()
+		*implicitOutputs = append(*implicitOutputs, d.apiMappingFile)
+	}
+
+	if String(d.properties.Proguard_filename) != "" {
+		d.proguardFile = android.PathForModuleOut(ctx, String(d.properties.Proguard_filename))
+		metalavaFlags += " --proguard " + d.proguardFile.String()
+		*implicitOutputs = append(*implicitOutputs, d.proguardFile)
+	}
+
+	if Bool(d.properties.Write_sdk_values) {
+		metalavaFlags = metalavaFlags + " --sdk-values " + android.PathForModuleOut(ctx, "out").String()
+	}
+
+	if Bool(d.properties.Create_doc_stubs) {
+		metalavaFlags += " --doc-stubs " + android.PathForModuleOut(ctx, "stubsDir").String()
+	} else {
+		metalavaFlags += " --stubs " + android.PathForModuleOut(ctx, "stubsDir").String()
+	}
+
+	return metalavaFlags
+}
+
+func (d *Droidstubs) collectAnnotationsFlags(ctx android.ModuleContext,
+	implicits *android.Paths, implicitOutputs *android.WritablePaths) string {
+	var flags string
+	if Bool(d.properties.Annotations_enabled) {
+		if String(d.properties.Previous_api) == "" {
+			ctx.PropertyErrorf("metalava_previous_api",
+				"has to be non-empty if annotations was enabled!")
+		}
+		previousApi := ctx.ExpandSource(String(d.properties.Previous_api),
+			"metalava_previous_api")
+		*implicits = append(*implicits, previousApi)
+
+		flags += " --include-annotations --migrate-nullness " + previousApi.String()
+
+		d.annotationsZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"_annotations.zip")
+		*implicitOutputs = append(*implicitOutputs, d.annotationsZip)
+
+		flags += " --extract-annotations " + d.annotationsZip.String()
+
+		if len(d.properties.Merge_annotations_dirs) == 0 {
+			ctx.PropertyErrorf("merge_annotations_dirs",
+				"has to be non-empty if annotations was enabled!")
+		}
+		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()
+			} else {
+				ctx.PropertyErrorf("merge_annotations_dirs",
+					"module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
+			}
+		})
+		// TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
+		flags += " --hide HiddenTypedefConstant --hide SuperfluousPrefix --hide AnnotationExtraction "
+	}
+	ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
+		if t, ok := m.(*ExportedDroiddocDir); ok {
+			*implicits = append(*implicits, t.deps...)
+			flags += " --merge-inclusion-annotations " + t.dir.String()
+		} else {
+			ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
+				"module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
+		}
+	})
+
+	return flags
+}
+
+func (d *Droidstubs) collectAPILevelsAnnotationsFlags(ctx android.ModuleContext,
+	implicits *android.Paths, implicitOutputs *android.WritablePaths) string {
+	var flags string
+	if Bool(d.properties.Api_levels_annotations_enabled) {
+		d.apiVersionsXml = android.PathForModuleOut(ctx, "api-versions.xml")
+		*implicitOutputs = append(*implicitOutputs, d.apiVersionsXml)
+
+		if len(d.properties.Api_levels_annotations_dirs) == 0 {
+			ctx.PropertyErrorf("api_levels_annotations_dirs",
+				"has to be non-empty if api levels annotations was enabled!")
+		}
+
+		flags = " --generate-api-levels " + d.apiVersionsXml.String() + " --apply-api-levels " +
+			d.apiVersionsXml.String() + " --current-version " + ctx.Config().PlatformSdkVersion() +
+			" --current-codename " + ctx.Config().PlatformSdkCodename() + " "
+
+		ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
+			if t, ok := m.(*ExportedDroiddocDir); ok {
+				var androidJars android.Paths
+				for _, dep := range t.deps {
+					if strings.HasSuffix(dep.String(), "android.jar") {
+						androidJars = append(androidJars, dep)
+					}
+				}
+				*implicits = append(*implicits, androidJars...)
+				flags += " --android-jar-pattern " + t.dir.String() + "/%/android.jar "
+			} else {
+				ctx.PropertyErrorf("api_levels_annotations_dirs",
+					"module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
+			}
+		})
+
+	}
+
+	return flags
+}
+
+func (d *Droidstubs) collectApiToXmlFlags(ctx android.ModuleContext, implicits *android.Paths,
+	implicitOutputs *android.WritablePaths) string {
+	var flags string
+	if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() {
+		if d.apiFile.String() == "" {
+			ctx.ModuleErrorf("API signature file has to be specified in Metalava when jdiff is enabled.")
+		}
+
+		d.apiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_api.xml")
+		*implicitOutputs = append(*implicitOutputs, d.apiXmlFile)
+
+		flags = " --api-xml " + d.apiXmlFile.String()
+
+		if String(d.properties.Check_api.Last_released.Api_file) == "" {
+			ctx.PropertyErrorf("check_api.last_released.api_file",
+				"has to be non-empty if jdiff was enabled!")
+		}
+		lastReleasedApi := ctx.ExpandSource(String(d.properties.Check_api.Last_released.Api_file),
+			"check_api.last_released.api_file")
+		*implicits = append(*implicits, lastReleasedApi)
+
+		d.lastReleasedApiXmlFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"_last_released_api.xml")
+		*implicitOutputs = append(*implicitOutputs, d.lastReleasedApiXmlFile)
+
+		flags += " --convert-to-jdiff " + lastReleasedApi.String() + " " +
+			d.lastReleasedApiXmlFile.String()
+	}
+
+	return flags
+}
+
+func (d *Droidstubs) transformMetalava(ctx android.ModuleContext, implicits android.Paths,
+	implicitOutputs android.WritablePaths, javaVersion,
+	bootclasspathArgs, classpathArgs, sourcepathArgs, opts string) {
+
+	ctx.Build(pctx, android.BuildParams{
+		Rule:            metalava,
+		Description:     "Metalava",
+		Output:          d.Javadoc.stubsSrcJar,
+		Inputs:          d.Javadoc.srcFiles,
+		Implicits:       implicits,
+		ImplicitOutputs: implicitOutputs,
+		Args: map[string]string{
+			"outDir":            android.PathForModuleOut(ctx, "out").String(),
+			"srcJarDir":         android.PathForModuleOut(ctx, "srcjars").String(),
+			"stubsDir":          android.PathForModuleOut(ctx, "stubsDir").String(),
+			"srcJars":           strings.Join(d.Javadoc.srcJars.Strings(), " "),
+			"javaVersion":       javaVersion,
+			"bootclasspathArgs": bootclasspathArgs,
+			"classpathArgs":     classpathArgs,
+			"sourcepathArgs":    sourcepathArgs,
+			"opts":              opts,
+		},
+	})
+}
+
+func (d *Droidstubs) transformCheckApi(ctx android.ModuleContext,
+	apiFile, removedApiFile android.Path, implicits android.Paths,
+	javaVersion, bootclasspathArgs, classpathArgs, sourcepathArgs, opts, msg string,
 	output android.WritablePath) {
 	ctx.Build(pctx, android.BuildParams{
 		Rule:        metalavaApiCheck,
@@ -1119,161 +1545,148 @@
 			"javaVersion":       javaVersion,
 			"bootclasspathArgs": bootclasspathArgs,
 			"classpathArgs":     classpathArgs,
-			"sourcepath":        strings.Join(d.Javadoc.sourcepaths.Strings(), ":"),
+			"sourcepathArgs":    sourcepathArgs,
 			"opts":              opts,
 			"msg":               msg,
 		},
 	})
 }
 
-func (d *Droiddoc) transformUpdateApi(ctx android.ModuleContext, apiFile, removedApiFile android.Path,
-	output android.WritablePath) {
+func (d *Droidstubs) transformJdiff(ctx android.ModuleContext, implicits android.Paths,
+	implicitOutputs android.WritablePaths,
+	bootclasspathArgs, classpathArgs, sourcepathArgs, opts string) {
 	ctx.Build(pctx, android.BuildParams{
-		Rule:        updateApi,
-		Description: "Update API",
-		Output:      output,
-		Implicits:   append(android.Paths{}, apiFile, removedApiFile, d.apiFile, d.removedApiFile),
+		Rule:            javadoc,
+		Description:     "Jdiff",
+		Output:          d.jdiffStubsSrcJar,
+		Inputs:          d.Javadoc.srcFiles,
+		Implicits:       implicits,
+		ImplicitOutputs: implicitOutputs,
 		Args: map[string]string{
-			"apiFile":               apiFile.String(),
-			"apiFileToCheck":        d.apiFile.String(),
-			"removedApiFile":        removedApiFile.String(),
-			"removedApiFileToCheck": d.removedApiFile.String(),
+			"outDir":            android.PathForModuleOut(ctx, "jdiff-out").String(),
+			"srcJarDir":         android.PathForModuleOut(ctx, "jdiff-srcjars").String(),
+			"stubsDir":          android.PathForModuleOut(ctx, "jdiff-stubsDir").String(),
+			"srcJars":           strings.Join(d.Javadoc.srcJars.Strings(), " "),
+			"opts":              opts,
+			"bootclasspathArgs": bootclasspathArgs,
+			"classpathArgs":     classpathArgs,
+			"sourcepathArgs":    sourcepathArgs,
+			"docZip":            d.jdiffDocZip.String(),
 		},
 	})
 }
 
-func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
 	deps := d.Javadoc.collectDeps(ctx)
 
 	javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), sdkContext(d))
-	jsilver := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jsilver.jar")
-	doclava := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "doclava.jar")
-	java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
-	checkApiClasspath := classpath{jsilver, doclava, android.PathForSource(ctx, java8Home, "lib/tools.jar")}
 
 	var implicits android.Paths
 	implicits = append(implicits, d.Javadoc.srcJars...)
+	implicits = append(implicits, d.Javadoc.argFiles...)
 
 	var implicitOutputs android.WritablePaths
-	implicitOutputs = append(implicitOutputs, d.Javadoc.docZip)
-	for _, o := range d.properties.Out {
+	for _, o := range d.Javadoc.properties.Out {
 		implicitOutputs = append(implicitOutputs, android.PathForModuleGen(ctx, o))
 	}
 
 	flags, err := d.initBuilderFlags(ctx, &implicits, deps)
 	metalavaCheckApiImplicits := implicits
+	jdiffImplicits := implicits
+
 	if err != nil {
 		return
 	}
 
-	flags.doclavaStubsFlags, flags.metalavaStubsFlags = d.collectStubsFlags(ctx, &implicitOutputs)
-	if Bool(d.properties.Metalava_enabled) {
-		flags.metalavaAnnotationsFlags = d.collectMetalavaAnnotationsFlags(ctx, &implicits, &implicitOutputs)
-		outDir := android.PathForModuleOut(ctx, "out").String()
-		docStubsDir := android.PathForModuleOut(ctx, "docStubsDir").String()
-		// TODO(nanzhang): Add a Soong property to handle documentation args.
-		if strings.Contains(flags.args, "--generate-documentation") { // enable docs generation
-			if Bool(d.properties.Dokka_enabled) {
-				flags.metalavaDokkaFlags = d.collectMetalavaDokkaFlags(ctx, &implicits,
-					flags.dokkaClasspathArgs, outDir, docStubsDir)
-				d.transformMetalava(ctx, implicits, implicitOutputs, outDir, docStubsDir, javaVersion,
-					flags.bootClasspathArgs, flags.classpathArgs, flags.metalavaStubsFlags+
-						flags.metalavaAnnotationsFlags+" "+
-						strings.Split(flags.args, "--generate-documentation")[0]+
-						flags.metalavaDokkaFlags+" "+strings.Split(flags.args, "--generate-documentation")[1])
-			} else {
-				flags.metalavaJavadocFlags = d.collectMetalavaJavadocFlags(
-					ctx, flags.bootClasspathArgs, flags.classpathArgs, outDir, docStubsDir)
-				flags.doclavaDocsFlags = d.collectDoclavaDocsFlags(ctx, &implicits, jsilver, doclava)
-				d.transformMetalava(ctx, implicits, implicitOutputs, outDir, docStubsDir, javaVersion,
-					flags.bootClasspathArgs, flags.classpathArgs, flags.metalavaStubsFlags+
-						flags.metalavaAnnotationsFlags+" "+
-						strings.Split(flags.args, "--generate-documentation")[0]+
-						flags.metalavaJavadocFlags+flags.doclavaDocsFlags+
-						" "+strings.Split(flags.args, "--generate-documentation")[1])
-			}
-		} else {
-			d.transformMetalava(ctx, implicits, implicitOutputs, outDir, docStubsDir, javaVersion,
-				flags.bootClasspathArgs, flags.classpathArgs,
-				flags.metalavaStubsFlags+flags.metalavaAnnotationsFlags+" "+flags.args)
-		}
-	} else {
-		flags.doclavaDocsFlags = d.collectDoclavaDocsFlags(ctx, &implicits, jsilver, doclava)
-		flags.postDoclavaCmds = d.getPostDoclavaCmds(ctx, &implicits)
-		d.transformDoclava(ctx, implicits, implicitOutputs, flags.bootClasspathArgs, flags.classpathArgs,
-			flags.doclavaDocsFlags+flags.doclavaStubsFlags+" "+flags.args,
-			flags.postDoclavaCmds)
-	}
+	flags.metalavaStubsFlags = d.collectStubsFlags(ctx, &implicitOutputs)
+	flags.metalavaAnnotationsFlags = d.collectAnnotationsFlags(ctx, &implicits, &implicitOutputs)
+	flags.metalavaApiLevelsAnnotationsFlags = d.collectAPILevelsAnnotationsFlags(ctx, &implicits, &implicitOutputs)
+	flags.metalavaApiToXmlFlags = d.collectApiToXmlFlags(ctx, &implicits, &implicitOutputs)
 
-	if d.checkCurrentApi() && !ctx.Config().IsPdkBuild() {
+	if strings.Contains(d.Javadoc.args, "--generate-documentation") {
+		// Currently Metalava have the ability to invoke Javadoc in a seperate process.
+		// Pass "-nodocs" to suppress the Javadoc invocation when Metalava receives
+		// "--generate-documentation" arg. This is not needed when Metalava removes this feature.
+		d.Javadoc.args = d.Javadoc.args + " -nodocs "
+	}
+	d.transformMetalava(ctx, implicits, implicitOutputs, javaVersion,
+		flags.bootClasspathArgs, flags.classpathArgs, flags.sourcepathArgs,
+		flags.metalavaStubsFlags+flags.metalavaAnnotationsFlags+
+			flags.metalavaApiLevelsAnnotationsFlags+flags.metalavaApiToXmlFlags+" "+d.Javadoc.args)
+
+	if apiCheckEnabled(d.properties.Check_api.Current, "current") &&
+		!ctx.Config().IsPdkBuild() {
 		apiFile := ctx.ExpandSource(String(d.properties.Check_api.Current.Api_file),
 			"check_api.current.api_file")
 		removedApiFile := ctx.ExpandSource(String(d.properties.Check_api.Current.Removed_api_file),
 			"check_api.current_removed_api_file")
 
 		d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "check_current_api.timestamp")
-		if !Bool(d.properties.Metalava_enabled) {
-			d.transformCheckApi(ctx, apiFile, removedApiFile, checkApiClasspath,
-				fmt.Sprintf(`\n******************************\n`+
-					`You have tried to change the API from what has been previously approved.\n\n`+
-					`To make these errors go away, you have two choices:\n`+
-					`   1. You can add '@hide' javadoc comments to the methods, etc. listed in the\n`+
-					`      errors above.\n\n`+
-					`   2. You can update current.txt by executing the following command:\n`+
-					`         make %s-update-current-api\n\n`+
-					`      To submit the revised current.txt to the main Android repository,\n`+
-					`      you will need approval.\n`+
-					`******************************\n`, ctx.ModuleName()), String(d.properties.Check_api.Current.Args),
-				d.checkCurrentApiTimestamp)
-		} else {
-			opts := flags.args + " --check-compatibility:api:current " + apiFile.String() +
-				" --check-compatibility:removed:current " + removedApiFile.String() + " "
+		opts := d.Javadoc.args + " --check-compatibility:api:current " + apiFile.String() +
+			" --check-compatibility:removed:current " + removedApiFile.String() + " "
 
-			d.transformMetalavaCheckApi(ctx, apiFile, removedApiFile, metalavaCheckApiImplicits,
-				javaVersion, flags.bootClasspathArgs, flags.classpathArgs, opts,
-				fmt.Sprintf(`\n******************************\n`+
-					`You have tried to change the API from what has been previously approved.\n\n`+
-					`To make these errors go away, you have two choices:\n`+
-					`   1. You can add '@hide' javadoc comments to the methods, etc. listed in the\n`+
-					`      errors above.\n\n`+
-					`   2. You can update current.txt by executing the following command:\n`+
-					`         make %s-update-current-api\n\n`+
-					`      To submit the revised current.txt to the main Android repository,\n`+
-					`      you will need approval.\n`+
-					`******************************\n`, ctx.ModuleName()),
-				d.checkCurrentApiTimestamp)
-		}
+		d.transformCheckApi(ctx, apiFile, removedApiFile, metalavaCheckApiImplicits,
+			javaVersion, flags.bootClasspathArgs, flags.classpathArgs, flags.sourcepathArgs, opts,
+			fmt.Sprintf(`\n******************************\n`+
+				`You have tried to change the API from what has been previously approved.\n\n`+
+				`To make these errors go away, you have two choices:\n`+
+				`   1. You can add '@hide' javadoc comments to the methods, etc. listed in the\n`+
+				`      errors above.\n\n`+
+				`   2. You can update current.txt by executing the following command:\n`+
+				`         make %s-update-current-api\n\n`+
+				`      To submit the revised current.txt to the main Android repository,\n`+
+				`      you will need approval.\n`+
+				`******************************\n`, ctx.ModuleName()),
+			d.checkCurrentApiTimestamp)
 
 		d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "update_current_api.timestamp")
-		d.transformUpdateApi(ctx, apiFile, removedApiFile, d.updateCurrentApiTimestamp)
+		transformUpdateApi(ctx, apiFile, removedApiFile, d.apiFile, d.removedApiFile,
+			d.updateCurrentApiTimestamp)
 	}
 
-	if d.checkLastReleasedApi() && !ctx.Config().IsPdkBuild() {
+	if apiCheckEnabled(d.properties.Check_api.Last_released, "last_released") &&
+		!ctx.Config().IsPdkBuild() {
 		apiFile := ctx.ExpandSource(String(d.properties.Check_api.Last_released.Api_file),
 			"check_api.last_released.api_file")
 		removedApiFile := ctx.ExpandSource(String(d.properties.Check_api.Last_released.Removed_api_file),
 			"check_api.last_released.removed_api_file")
 
 		d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "check_last_released_api.timestamp")
-		if !Bool(d.properties.Metalava_enabled) {
-			d.transformCheckApi(ctx, apiFile, removedApiFile, checkApiClasspath,
-				`\n******************************\n`+
-					`You have tried to change the API from what has been previously released in\n`+
-					`an SDK.  Please fix the errors listed above.\n`+
-					`******************************\n`, String(d.properties.Check_api.Last_released.Args),
-				d.checkLastReleasedApiTimestamp)
-		} else {
-			opts := flags.args + " --check-compatibility:api:released " + apiFile.String() +
-				" --check-compatibility:removed:released " + removedApiFile.String() + " "
+		opts := d.Javadoc.args + " --check-compatibility:api:released " + apiFile.String() +
+			" --check-compatibility:removed:released " + removedApiFile.String() + " "
 
-			d.transformMetalavaCheckApi(ctx, apiFile, removedApiFile, metalavaCheckApiImplicits,
-				javaVersion, flags.bootClasspathArgs, flags.classpathArgs, opts,
-				`\n******************************\n`+
-					`You have tried to change the API from what has been previously released in\n`+
-					`an SDK.  Please fix the errors listed above.\n`+
-					`******************************\n`,
-				d.checkLastReleasedApiTimestamp)
-		}
+		d.transformCheckApi(ctx, apiFile, removedApiFile, metalavaCheckApiImplicits,
+			javaVersion, flags.bootClasspathArgs, flags.classpathArgs, flags.sourcepathArgs, opts,
+			`\n******************************\n`+
+				`You have tried to change the API from what has been previously released in\n`+
+				`an SDK.  Please fix the errors listed above.\n`+
+				`******************************\n`,
+			d.checkLastReleasedApiTimestamp)
+	}
+
+	if Bool(d.properties.Jdiff_enabled) && !ctx.Config().IsPdkBuild() {
+
+		// Please sync with android-api-council@ before making any changes for the name of jdiffDocZip below
+		// since there's cron job downstream that fetch this .zip file periodically.
+		// See b/116221385 for reference.
+		d.jdiffDocZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-docs.zip")
+		d.jdiffStubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"jdiff-stubs.srcjar")
+
+		var jdiffImplicitOutputs android.WritablePaths
+		jdiffImplicitOutputs = append(jdiffImplicitOutputs, d.jdiffDocZip)
+
+		jdiff := android.PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", "jdiff.jar")
+		jdiffImplicits = append(jdiffImplicits, android.Paths{jdiff, d.apiXmlFile, d.lastReleasedApiXmlFile}...)
+
+		opts := " -encoding UTF-8 -source 1.8 -J-Xmx1600m -XDignore.symbol.file " +
+			"-doclet jdiff.JDiff -docletpath " + jdiff.String() + " -quiet " +
+			"-newapi " + strings.TrimSuffix(d.apiXmlFile.Base(), d.apiXmlFile.Ext()) +
+			" -newapidir " + filepath.Dir(d.apiXmlFile.String()) +
+			" -oldapi " + strings.TrimSuffix(d.lastReleasedApiXmlFile.Base(), d.lastReleasedApiXmlFile.Ext()) +
+			" -oldapidir " + filepath.Dir(d.lastReleasedApiXmlFile.String())
+
+		d.transformJdiff(ctx, jdiffImplicits, jdiffImplicitOutputs, flags.bootClasspathArgs, flags.classpathArgs,
+			flags.sourcepathArgs, opts)
 	}
 }
 
@@ -1282,6 +1695,8 @@
 //
 var droiddocTemplateTag = dependencyTag{name: "droiddoc-template"}
 var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
+var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
+var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
 
 type ExportedDroiddocDirProperties struct {
 	// path to the directory containing Droiddoc related files.
@@ -1338,3 +1753,16 @@
 
 	return module
 }
+
+func StubsDefaultsFactory() android.Module {
+	module := &DocDefaults{}
+
+	module.AddProperties(
+		&JavadocProperties{},
+		&DroidstubsProperties{},
+	)
+
+	android.InitDefaultsModule(module)
+
+	return module
+}
diff --git a/java/java.go b/java/java.go
index f6c9484..8b354d9 100644
--- a/java/java.go
+++ b/java/java.go
@@ -75,7 +75,7 @@
 	// list of files to use as Java resources
 	Java_resources []string `android:"arch_variant"`
 
-	// list of files that should be excluded from java_resources
+	// list of files that should be excluded from java_resources and java_resource_dirs
 	Exclude_java_resources []string `android:"arch_variant"`
 
 	// don't build against the default libraries (bootclasspath, legacy-test, core-junit,
@@ -95,6 +95,9 @@
 	// list of java libraries that will be compiled into the resulting jar
 	Static_libs []string `android:"arch_variant"`
 
+	// list of native libraries that will be provided in or alongside the resulting jar
+	Jni_libs []string `android:"arch_variant"`
+
 	// manifest file to be included in resulting jar
 	Manifest *string
 
@@ -138,7 +141,7 @@
 	// supported at compile time. It should only be needed to compile tests in
 	// packages that exist in libcore and which are inconvenient to move
 	// elsewhere.
-	Patch_module *string
+	Patch_module *string `android:"arch_variant"`
 
 	Jacoco struct {
 		// List of classes to include for instrumentation with jacoco to collect coverage
@@ -311,6 +314,10 @@
 
 	// list of SDK lib names that this java moudule is exporting
 	exportedSdkLibs []string
+
+	// list of source files, collected from compiledJavaSrcs and compiledSrcJars
+	// filter out Exclude_srcs, will be used by android.IDEInfo struct
+	expandIDEInfoCompiledSrcs []string
 }
 
 func (j *Module) Srcs() android.Paths {
@@ -412,7 +419,7 @@
 
 func sdkVersionOrDefault(ctx android.BaseContext, v string) string {
 	switch v {
-	case "", "current", "system_current", "test_current", "core_current":
+	case "", "current", "system_current", "test_current", "core_current", "core_platform_current":
 		return ctx.Config().DefaultAppTargetSdk()
 	default:
 		return v
@@ -423,7 +430,7 @@
 // it returns android.FutureApiLevel (10000).
 func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) {
 	switch v {
-	case "", "current", "test_current", "system_current", "core_current":
+	case "", "current", "test_current", "system_current", "core_current", "core_platform_current":
 		return ctx.Config().DefaultAppTargetSdkInt(), nil
 	default:
 		n := android.GetNumericSdkVersion(v)
@@ -520,6 +527,8 @@
 		}
 		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
 	}
@@ -542,6 +551,8 @@
 		return toModule("android_test_stubs_current", "framework-res")
 	case "core_current":
 		return toModule("core.current.stubs", "")
+	case "core_platform_current":
+		return toModule("core.platform.api.stubs", "")
 	default:
 		return toPrebuilt(v)
 	}
@@ -553,16 +564,12 @@
 			sdkDep := decodeSdkDep(ctx, sdkContext(j))
 			if sdkDep.useDefaultLibs {
 				ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
-				if ctx.Config().TargetOpenJDK9() {
-					ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
-				}
+				ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
 				if !Bool(j.properties.No_framework_libs) {
 					ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
 				}
 			} else if sdkDep.useModule {
-				if ctx.Config().TargetOpenJDK9() {
-					ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
-				}
+				ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
 				ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
 				if Bool(j.deviceProperties.Optimize.Enabled) {
 					ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
@@ -572,10 +579,10 @@
 		} else if j.deviceProperties.System_modules == nil {
 			ctx.PropertyErrorf("no_standard_libs",
 				"system_modules is required to be set when no_standard_libs is true, did you mean no_framework_libs?")
-		} else if *j.deviceProperties.System_modules != "none" && ctx.Config().TargetOpenJDK9() {
+		} else if *j.deviceProperties.System_modules != "none" {
 			ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
 		}
-		if ctx.ModuleName() == "framework" {
+		if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
 			ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
 		}
 		if ctx.ModuleName() == "android_stubs_current" ||
@@ -708,8 +715,9 @@
 	ver := m.sdkVersion()
 	noStdLibs := Bool(m.properties.No_standard_libs)
 	switch {
-	case name == "core.current.stubs" || ver == "core_current" || noStdLibs || name == "stub-annotations" ||
-		name == "private-stub-annotations-jar":
+	case name == "core.current.stubs" || ver == "core_current" ||
+		name == "core.platform.api.stubs" || ver == "core_platform_current" ||
+		noStdLibs || name == "stub-annotations" || name == "private-stub-annotations-jar":
 		return javaCore
 	case name == "android_system_stubs_current" || strings.HasPrefix(ver, "system_"):
 		return javaSystem
@@ -804,7 +812,7 @@
 			case annoTag:
 				deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
 			case frameworkResTag:
-				if ctx.ModuleName() == "framework" {
+				if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
 					// framework.jar has a one-off dependency on the R.java and Manifest.java files
 					// generated by framework-res.apk
 					deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
@@ -884,7 +892,7 @@
 		ret = javaVersion
 	} else if ctx.Device() && sdk <= 23 {
 		ret = "1.7"
-	} else if ctx.Device() && sdk <= 26 || !ctx.Config().TargetOpenJDK9() {
+	} else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
 		ret = "1.8"
 	} else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
 		// TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
@@ -900,9 +908,12 @@
 
 	var flags javaBuilderFlags
 
+	// javaVersion flag.
+	flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
+
 	// javac flags.
 	javacFlags := j.properties.Javacflags
-	if ctx.Config().TargetOpenJDK9() {
+	if flags.javaVersion == "1.9" {
 		javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
 	}
 	if ctx.Config().MinimizeJavaDebugInfo() {
@@ -927,15 +938,12 @@
 		flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
 	}
 
-	// javaVersion flag.
-	flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
-
 	// classpath
 	flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
 	flags.classpath = append(flags.classpath, deps.classpath...)
 	flags.processorPath = append(flags.processorPath, deps.processorPath...)
 
-	if len(flags.bootClasspath) == 0 && ctx.Host() && !ctx.Config().TargetOpenJDK9() &&
+	if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
 		!Bool(j.properties.No_standard_libs) &&
 		inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
 		// Give host-side tools a version of OpenJDK's standard libraries
@@ -961,7 +969,7 @@
 		}
 	}
 
-	if j.properties.Patch_module != nil && ctx.Config().TargetOpenJDK9() {
+	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)
 	}
@@ -995,7 +1003,7 @@
 	deps := j.collectDeps(ctx)
 	flags := j.collectBuilderFlags(ctx, deps)
 
-	if ctx.Config().TargetOpenJDK9() {
+	if flags.javaVersion == "1.9" {
 		j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
 	}
 	srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
@@ -1009,6 +1017,10 @@
 	srcJars = append(srcJars, deps.srcJars...)
 	srcJars = append(srcJars, extraSrcJars...)
 
+	// Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
+	// that IDEInfo struct will use
+	j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
+
 	jarName := ctx.ModuleName() + ".jar"
 
 	javaSrcFiles := srcFiles.FilterByExt(".java")
@@ -1118,7 +1130,8 @@
 		}
 	}
 
-	dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
+	dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
+		j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
 	fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
 
 	var resArgs []string
@@ -1360,6 +1373,23 @@
 	return j.logtagsSrcs
 }
 
+// Collect information for opening IDE project files in java/jdeps.go.
+func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
+	dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
+	dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
+	dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
+	if j.properties.Jarjar_rules != nil {
+		dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, *j.properties.Jarjar_rules)
+	}
+}
+
+func (j *Module) CompilerDeps() []string {
+	jdeps := []string{}
+	jdeps = append(jdeps, j.properties.Libs...)
+	jdeps = append(jdeps, j.properties.Static_libs...)
+	return jdeps
+}
+
 //
 // Java libraries (.jar file)
 //
@@ -1419,6 +1449,10 @@
 	// installed with the module.
 	Test_config *string `android:"arch_variant"`
 
+	// the name of the test configuration template (for example "AndroidTestTemplate.xml") that
+	// should be installed with the module.
+	Test_config_template *string `android:"arch_variant"`
+
 	// list of files or filegroup modules that provide data that should be installed alongside
 	// the test
 	Data []string
@@ -1434,7 +1468,7 @@
 }
 
 func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
-	j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config)
+	j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template)
 	j.data = ctx.ExpandSources(j.testProperties.Data, nil)
 
 	j.Library.GenerateAndroidBuildActions(ctx)
@@ -1443,6 +1477,7 @@
 func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
 	j.deps(ctx)
 	android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
+	android.ExtractSourceDeps(ctx, j.testProperties.Test_config_template)
 	android.ExtractSourcesDeps(ctx, j.testProperties.Data)
 }
 
@@ -1689,6 +1724,26 @@
 	return j.exportedSdkLibs
 }
 
+// Collect information for opening IDE project files in java/jdeps.go.
+const (
+	removedPrefix = "prebuilt_"
+)
+
+func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
+	dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
+}
+
+func (j *Import) IDECustomizedModuleName() string {
+	// TODO(b/113562217): Extract the base module name from the Import name, often the Import name
+	// has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
+	// solution to get the Import name.
+	name := j.Name()
+	if strings.HasPrefix(name, removedPrefix) {
+		name = strings.Trim(name, removedPrefix)
+	}
+	return name
+}
+
 var _ android.PrebuiltInterface = (*Import)(nil)
 
 func ImportFactory() android.Module {
diff --git a/java/java_resources.go b/java/java_resources.go
index e02709d..6c1fd39 100644
--- a/java/java_resources.go
+++ b/java/java_resources.go
@@ -19,6 +19,8 @@
 	"path/filepath"
 	"strings"
 
+	"github.com/google/blueprint/pathtools"
+
 	"android/soong/android"
 )
 
@@ -32,7 +34,7 @@
 }
 
 func ResourceDirsToJarArgs(ctx android.ModuleContext,
-	resourceDirs, excludeResourceDirs []string) (args []string, deps android.Paths) {
+	resourceDirs, excludeResourceDirs, excludeResourceFiles []string) (args []string, deps android.Paths) {
 	var excludeDirs []string
 	var excludeFiles []string
 
@@ -44,6 +46,8 @@
 		}
 	}
 
+	excludeFiles = append(excludeFiles, ctx.ExpandSources(excludeResourceFiles, nil).Strings()...)
+
 	excludeFiles = append(excludeFiles, resourceExcludes...)
 
 	for _, resourceDir := range resourceDirs {
@@ -62,7 +66,7 @@
 					if !strings.HasPrefix(path, dir.String()) {
 						panic(fmt.Errorf("path %q does not start with %q", path, dir))
 					}
-					args = append(args, "-f", path)
+					args = append(args, "-f", pathtools.MatchEscape(path))
 				}
 			}
 		}
@@ -105,7 +109,7 @@
 		if i == 0 || dir != lastDir {
 			args = append(args, "-C", dir)
 		}
-		args = append(args, "-f", path)
+		args = append(args, "-f", pathtools.MatchEscape(path))
 		lastDir = dir
 	}
 
diff --git a/java/java_test.go b/java/java_test.go
index 3ace528..82accd5 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -702,6 +702,30 @@
 			prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
 			args: "-C java-res/a -f java-res/a/a",
 		},
+		{
+			// Test wildcards in java_resources
+			name: "wildcard files",
+			prop: `java_resources: ["java-res/**/*"]`,
+			args: "-C . -f java-res/a/a -f java-res/b/b",
+		},
+		{
+			// Test exclude_java_resources with java_resources
+			name: "wildcard files with exclude",
+			prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
+			args: "-C . -f java-res/a/a",
+		},
+		{
+			// Test exclude_java_resources with java_resource_dirs
+			name: "resource dirs with exclude files",
+			prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
+			args: "-C java-res -f java-res/a/a",
+		},
+		{
+			// Test exclude_java_resource_dirs with java_resource_dirs
+			name: "resource dirs with exclude files",
+			prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
+			args: "-C java-res -f java-res/a/a -f java-res/b/b",
+		},
 	}
 
 	for _, test := range table {
@@ -734,42 +758,6 @@
 	}
 }
 
-func TestExcludeResources(t *testing.T) {
-	ctx := testJava(t, `
-		java_library {
-			name: "foo",
-			srcs: ["a.java"],
-			java_resource_dirs: ["java-res", "java-res2"],
-			exclude_java_resource_dirs: ["java-res2"],
-		}
-
-		java_library {
-			name: "bar",
-			srcs: ["a.java"],
-			java_resources: ["java-res/*/*"],
-			exclude_java_resources: ["java-res/b/*"],
-		}
-	`)
-
-	fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
-
-	expected := "-C java-res -f java-res/a/a -f java-res/b/b"
-	if fooRes.Args["jarArgs"] != expected {
-		t.Errorf("foo resource jar args %q is not %q",
-			fooRes.Args["jarArgs"], expected)
-
-	}
-
-	barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
-
-	expected = "-C . -f java-res/a/a"
-	if barRes.Args["jarArgs"] != expected {
-		t.Errorf("bar resource jar args %q is not %q",
-			barRes.Args["jarArgs"], expected)
-
-	}
-}
-
 func TestGeneratedSources(t *testing.T) {
 	ctx := testJava(t, `
 		java_library {
diff --git a/java/jdeps.go b/java/jdeps.go
new file mode 100644
index 0000000..c7fa42a
--- /dev/null
+++ b/java/jdeps.go
@@ -0,0 +1,109 @@
+// 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 (
+	"encoding/json"
+	"fmt"
+	"os"
+
+	"android/soong/android"
+)
+
+// This singleton generates android java dependency into to a json file. It does so for each
+// blueprint Android.bp resulting in a java.Module when either make, mm, mma, mmm or mmma is
+// called. Dependency info file is generated in $OUT/module_bp_java_depend.json.
+
+func init() {
+	android.RegisterSingletonType("jdeps_generator", jDepsGeneratorSingleton)
+}
+
+func jDepsGeneratorSingleton() android.Singleton {
+	return &jdepsGeneratorSingleton{}
+}
+
+type jdepsGeneratorSingleton struct {
+}
+
+const (
+	// Environment variables used to modify behavior of this singleton.
+	envVariableCollectJavaDeps = "SOONG_COLLECT_JAVA_DEPS"
+	jdepsJsonFileName          = "module_bp_java_deps.json"
+)
+
+func (j *jdepsGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) {
+	if !ctx.Config().IsEnvTrue(envVariableCollectJavaDeps) {
+		return
+	}
+
+	moduleInfos := make(map[string]android.IdeInfo)
+
+	ctx.VisitAllModules(func(module android.Module) {
+		ideInfoProvider, ok := module.(android.IDEInfo)
+		if !ok {
+			return
+		}
+		name := ideInfoProvider.BaseModuleName()
+		ideModuleNameProvider, ok := module.(android.IDECustomizedModuleName)
+		if ok {
+			name = ideModuleNameProvider.IDECustomizedModuleName()
+		}
+
+		dpInfo := moduleInfos[name]
+		ideInfoProvider.IDEInfo(&dpInfo)
+		dpInfo.Deps = android.FirstUniqueStrings(dpInfo.Deps)
+		dpInfo.Srcs = android.FirstUniqueStrings(dpInfo.Srcs)
+		dpInfo.Aidl_include_dirs = android.FirstUniqueStrings(dpInfo.Aidl_include_dirs)
+		dpInfo.Jarjar_rules = android.FirstUniqueStrings(dpInfo.Jarjar_rules)
+		dpInfo.Jars = android.FirstUniqueStrings(dpInfo.Jars)
+		moduleInfos[name] = dpInfo
+
+		mkProvider, ok := module.(android.AndroidMkDataProvider)
+		if !ok {
+			return
+		}
+		data := mkProvider.AndroidMk()
+		if data.Class != "" {
+			dpInfo.Classes = append(dpInfo.Classes, data.Class)
+		}
+		out := data.OutputFile.String()
+		if out != "" {
+			dpInfo.Installed_paths = append(dpInfo.Installed_paths, out)
+		}
+		dpInfo.Classes = android.FirstUniqueStrings(dpInfo.Classes)
+		dpInfo.Installed_paths = android.FirstUniqueStrings(dpInfo.Installed_paths)
+		moduleInfos[name] = dpInfo
+	})
+
+	jfpath := android.PathForOutput(ctx, jdepsJsonFileName).String()
+	err := createJsonFile(moduleInfos, jfpath)
+	if err != nil {
+		ctx.Errorf(err.Error())
+	}
+}
+
+func createJsonFile(moduleInfos map[string]android.IdeInfo, jfpath string) error {
+	file, err := os.Create(jfpath)
+	if err != nil {
+		return fmt.Errorf("Failed to create file: %s, relative: %v", jdepsJsonFileName, err)
+	}
+	defer file.Close()
+	buf, err := json.MarshalIndent(moduleInfos, "", "\t")
+	if err != nil {
+		return fmt.Errorf("Write file failed: %s, relative: %v", jdepsJsonFileName, err)
+	}
+	fmt.Fprintf(file, string(buf))
+	return nil
+}
diff --git a/java/jdeps_test.go b/java/jdeps_test.go
new file mode 100644
index 0000000..ca8a3cd
--- /dev/null
+++ b/java/jdeps_test.go
@@ -0,0 +1,87 @@
+// 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 (
+	"reflect"
+	"testing"
+
+	"android/soong/android"
+)
+
+func TestCollectJavaLibraryPropertiesAddLibsDeps(t *testing.T) {
+	expected := []string{"Foo", "Bar"}
+	module := LibraryFactory().(*Library)
+	module.properties.Libs = append(module.properties.Libs, expected...)
+	dpInfo := &android.IdeInfo{}
+
+	module.IDEInfo(dpInfo)
+
+	if !reflect.DeepEqual(dpInfo.Deps, expected) {
+		t.Errorf("Library.IDEInfo() Deps = %v, want %v", dpInfo.Deps, expected)
+	}
+}
+
+func TestCollectJavaLibraryPropertiesAddStaticLibsDeps(t *testing.T) {
+	expected := []string{"Foo", "Bar"}
+	module := LibraryFactory().(*Library)
+	module.properties.Static_libs = append(module.properties.Static_libs, expected...)
+	dpInfo := &android.IdeInfo{}
+
+	module.IDEInfo(dpInfo)
+
+	if !reflect.DeepEqual(dpInfo.Deps, expected) {
+		t.Errorf("Library.IDEInfo() Deps = %v, want %v", dpInfo.Deps, expected)
+	}
+}
+
+func TestCollectJavaLibraryPropertiesAddScrs(t *testing.T) {
+	expected := []string{"Foo", "Bar"}
+	module := LibraryFactory().(*Library)
+	module.expandIDEInfoCompiledSrcs = append(module.expandIDEInfoCompiledSrcs, expected...)
+	dpInfo := &android.IdeInfo{}
+
+	module.IDEInfo(dpInfo)
+
+	if !reflect.DeepEqual(dpInfo.Srcs, expected) {
+		t.Errorf("Library.IDEInfo() Srcs = %v, want %v", dpInfo.Srcs, expected)
+	}
+}
+
+func TestCollectJavaLibraryPropertiesAddAidlIncludeDirs(t *testing.T) {
+	expected := []string{"Foo", "Bar"}
+	module := LibraryFactory().(*Library)
+	module.deviceProperties.Aidl.Include_dirs = append(module.deviceProperties.Aidl.Include_dirs, expected...)
+	dpInfo := &android.IdeInfo{}
+
+	module.IDEInfo(dpInfo)
+
+	if !reflect.DeepEqual(dpInfo.Aidl_include_dirs, expected) {
+		t.Errorf("Library.IDEInfo() Aidl_include_dirs = %v, want %v", dpInfo.Aidl_include_dirs, expected)
+	}
+}
+
+func TestCollectJavaLibraryPropertiesAddJarjarRules(t *testing.T) {
+	expected := "Jarjar_rules.txt"
+	module := LibraryFactory().(*Library)
+	module.properties.Jarjar_rules = &expected
+	dpInfo := &android.IdeInfo{}
+
+	module.IDEInfo(dpInfo)
+
+	if dpInfo.Jarjar_rules[0] != expected {
+		t.Errorf("Library.IDEInfo() Jarjar_rules = %v, want %v", dpInfo.Jarjar_rules[0], expected)
+	}
+}
diff --git a/java/sdk_library.go b/java/sdk_library.go
index d588801..3e6908b 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -217,38 +217,45 @@
 			fmt.Fprintln(w, "LOCAL_MODULE :=", name)
 			fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES := "+module.implName())
 			fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
+			owner := module.ModuleBase.Owner()
+			if owner == "" {
+				owner = "android"
+			}
 			// Create dist rules to install the stubs libs to the dist dir
 			if len(module.publicApiStubsPath) == 1 {
 				fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
 					module.publicApiStubsPath.Strings()[0]+
-					":"+path.Join("apistubs", "public", module.BaseModuleName()+".jar")+")")
+					":"+path.Join("apistubs", owner, "public",
+					module.BaseModuleName()+".jar")+")")
 			}
 			if len(module.systemApiStubsPath) == 1 {
 				fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
 					module.systemApiStubsPath.Strings()[0]+
-					":"+path.Join("apistubs", "system", module.BaseModuleName()+".jar")+")")
+					":"+path.Join("apistubs", owner, "system",
+					module.BaseModuleName()+".jar")+")")
 			}
 			if len(module.testApiStubsPath) == 1 {
 				fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
 					module.testApiStubsPath.Strings()[0]+
-					":"+path.Join("apistubs", "test", module.BaseModuleName()+".jar")+")")
+					":"+path.Join("apistubs", owner, "test",
+					module.BaseModuleName()+".jar")+")")
 			}
 			if module.publicApiFilePath != nil {
 				fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
 					module.publicApiFilePath.String()+
-					":"+path.Join("apistubs", "public", "api",
+					":"+path.Join("apistubs", owner, "public", "api",
 					module.BaseModuleName()+".txt")+")")
 			}
 			if module.systemApiFilePath != nil {
 				fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
 					module.systemApiFilePath.String()+
-					":"+path.Join("apistubs", "system", "api",
+					":"+path.Join("apistubs", owner, "system", "api",
 					module.BaseModuleName()+".txt")+")")
 			}
 			if module.testApiFilePath != nil {
 				fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
 					module.testApiFilePath.String()+
-					":"+path.Join("apistubs", "test", "api",
+					":"+path.Join("apistubs", owner, "test", "api",
 					module.BaseModuleName()+".txt")+")")
 			}
 		},
diff --git a/python/binary.go b/python/binary.go
index 4135dfe..bf9acb4 100644
--- a/python/binary.go
+++ b/python/binary.go
@@ -84,15 +84,15 @@
 
 	main := binary.getPyMainFile(ctx, srcsPathMappings)
 
-	var launcherPath android.Path
+	var launcherPath android.OptionalPath
 	if embeddedLauncher {
 		ctx.VisitDirectDepsWithTag(launcherTag, func(m android.Module) {
 			if provider, ok := m.(IntermPathProvider); ok {
-				if launcherPath != nil {
+				if launcherPath.Valid() {
 					panic(fmt.Errorf("launcher path was found before: %q",
 						launcherPath))
 				}
-				launcherPath = provider.IntermPathForModuleOut().Path()
+				launcherPath = provider.IntermPathForModuleOut()
 			}
 		})
 	}
diff --git a/python/builder.go b/python/builder.go
index ec4cb4e..11a792a 100644
--- a/python/builder.go
+++ b/python/builder.go
@@ -70,7 +70,7 @@
 }
 
 func registerBuildActionForParFile(ctx android.ModuleContext, embeddedLauncher bool,
-	launcherPath android.Path, interpreter, main, binName string,
+	launcherPath android.OptionalPath, interpreter, main, binName string,
 	srcsZips android.Paths) android.Path {
 
 	// .intermediate output path for merged zip file.
@@ -104,9 +104,9 @@
 				"srcsZips":  strings.Join(srcsZips.Strings(), " "),
 			},
 		})
-	} else {
+	} else if launcherPath.Valid() {
 		// added launcherPath to the implicits Ninja dependencies.
-		implicits = append(implicits, launcherPath)
+		implicits = append(implicits, launcherPath.Path())
 
 		// .intermediate output path for entry_point.txt
 		entryPoint := android.PathForModuleOut(ctx, entryPointFile).String()
diff --git a/scripts/manifest_fixer.py b/scripts/manifest_fixer.py
index db35c8d..80a398b 100755
--- a/scripts/manifest_fixer.py
+++ b/scripts/manifest_fixer.py
@@ -49,6 +49,10 @@
   parser = argparse.ArgumentParser()
   parser.add_argument('--minSdkVersion', default='', dest='min_sdk_version',
                       help='specify minSdkVersion used by the build system')
+  parser.add_argument('--targetSdkVersion', default='', dest='target_sdk_version',
+                      help='specify targetSdkVersion used by the build system')
+  parser.add_argument('--raise-min-sdk-version', dest='raise_min_sdk_version', action='store_true',
+                      help='raise the minimum sdk version in the manifest if necessary')
   parser.add_argument('--library', dest='library', action='store_true',
                       help='manifest is for a static library')
   parser.add_argument('--uses-library', dest='uses_libraries', action='append',
@@ -130,12 +134,13 @@
   return indent
 
 
-def raise_min_sdk_version(doc, requested, library):
+def raise_min_sdk_version(doc, min_sdk_version, target_sdk_version, library):
   """Ensure the manifest contains a <uses-sdk> tag with a minSdkVersion.
 
   Args:
     doc: The XML document.  May be modified by this function.
-    requested: The requested minSdkVersion attribute.
+    min_sdk_version: The requested minSdkVersion attribute.
+    target_sdk_version: The requested targetSdkVersion attribute.
   Raises:
     RuntimeError: invalid manifest
   """
@@ -162,11 +167,11 @@
   min_attr = element.getAttributeNodeNS(android_ns, 'minSdkVersion')
   if min_attr is None:
     min_attr = doc.createAttributeNS(android_ns, 'android:minSdkVersion')
-    min_attr.value = requested
+    min_attr.value = min_sdk_version
     element.setAttributeNode(min_attr)
   else:
-    if compare_version_gt(requested, min_attr.value):
-      min_attr.value = requested
+    if compare_version_gt(min_sdk_version, min_attr.value):
+      min_attr.value = min_sdk_version
 
   # Insert the targetSdkVersion attribute if it is missing.  If it is already
   # present leave it as is.
@@ -174,9 +179,14 @@
   if target_attr is None:
     target_attr = doc.createAttributeNS(android_ns, 'android:targetSdkVersion')
     if library:
-      target_attr.value = '1'
+      # TODO(b/117122200): libraries shouldn't set targetSdkVersion at all, but
+      # ManifestMerger treats minSdkVersion="Q" as targetSdkVersion="Q" if it
+      # is empty.  Set it to something low so that it will be overriden by the
+      # main manifest, but high enough that it doesn't cause implicit
+      # permissions grants.
+      target_attr.value = '15'
     else:
-      target_attr.value = requested
+      target_attr.value = target_sdk_version
     element.setAttributeNode(target_attr)
 
 
@@ -271,8 +281,8 @@
 
     ensure_manifest_android_ns(doc)
 
-    if args.min_sdk_version:
-      raise_min_sdk_version(doc, args.min_sdk_version, args.library)
+    if args.raise_min_sdk_version:
+      raise_min_sdk_version(doc, args.min_sdk_version, args.target_sdk_version, args.library)
 
     if args.uses_libraries:
       add_uses_libraries(doc, args.uses_libraries)
diff --git a/scripts/manifest_fixer_test.py b/scripts/manifest_fixer_test.py
index ac72e6d..d1d401a 100755
--- a/scripts/manifest_fixer_test.py
+++ b/scripts/manifest_fixer_test.py
@@ -54,9 +54,11 @@
 class RaiseMinSdkVersionTest(unittest.TestCase):
   """Unit tests for raise_min_sdk_version function."""
 
-  def raise_min_sdk_version_test(self, input_manifest, min_sdk_version, library):
+  def raise_min_sdk_version_test(self, input_manifest, min_sdk_version,
+                                 target_sdk_version, library):
     doc = minidom.parseString(input_manifest)
-    manifest_fixer.raise_min_sdk_version(doc, min_sdk_version, library)
+    manifest_fixer.raise_min_sdk_version(doc, min_sdk_version,
+                                         target_sdk_version, library)
     output = StringIO.StringIO()
     manifest_fixer.write_xml(output, doc)
     return output.getvalue()
@@ -69,13 +71,13 @@
 
   # pylint: disable=redefined-builtin
   def uses_sdk(self, min=None, target=None, extra=''):
-    attrs = ""
+    attrs = ''
     if min:
       attrs += ' android:minSdkVersion="%s"' % (min)
     if target:
       attrs += ' android:targetSdkVersion="%s"' % (target)
     if extra:
-        attrs += ' ' + extra
+      attrs += ' ' + extra
     return '    <uses-sdk%s/>\n' % (attrs)
 
   def test_no_uses_sdk(self):
@@ -83,7 +85,7 @@
 
     manifest_input = self.manifest_tmpl % ''
     expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28')
-    output = self.raise_min_sdk_version_test(manifest_input, '28', False)
+    output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
     self.assertEqual(output, expected)
 
   def test_no_min(self):
@@ -92,47 +94,47 @@
     manifest_input = self.manifest_tmpl % '    <uses-sdk extra="foo"/>\n'
     expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28',
                                                   extra='extra="foo"')
-    output = self.raise_min_sdk_version_test(manifest_input, '28', False)
+    output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
     self.assertEqual(output, expected)
 
   def test_raise_min(self):
     """Tests inserting a minSdkVersion attribute into a uses-sdk element."""
 
     manifest_input = self.manifest_tmpl % self.uses_sdk(min='27')
-    expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
-    output = self.raise_min_sdk_version_test(manifest_input, '28', False)
+    expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28')
+    output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
     self.assertEqual(output, expected)
 
   def test_raise(self):
     """Tests raising a minSdkVersion attribute."""
 
     manifest_input = self.manifest_tmpl % self.uses_sdk(min='27')
-    expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
-    output = self.raise_min_sdk_version_test(manifest_input, '28', False)
+    expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28')
+    output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
     self.assertEqual(output, expected)
 
   def test_no_raise_min(self):
     """Tests a minSdkVersion that doesn't need raising."""
 
     manifest_input = self.manifest_tmpl % self.uses_sdk(min='28')
-    expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28')
-    output = self.raise_min_sdk_version_test(manifest_input, '27', False)
+    expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
+    output = self.raise_min_sdk_version_test(manifest_input, '27', '27', False)
     self.assertEqual(output, expected)
 
   def test_raise_codename(self):
     """Tests raising a minSdkVersion attribute to a codename."""
 
     manifest_input = self.manifest_tmpl % self.uses_sdk(min='28')
-    expected = self.manifest_tmpl % self.uses_sdk(min='P', target='28')
-    output = self.raise_min_sdk_version_test(manifest_input, 'P', False)
+    expected = self.manifest_tmpl % self.uses_sdk(min='P', target='P')
+    output = self.raise_min_sdk_version_test(manifest_input, 'P', 'P', False)
     self.assertEqual(output, expected)
 
   def test_no_raise_codename(self):
     """Tests a minSdkVersion codename that doesn't need raising."""
 
     manifest_input = self.manifest_tmpl % self.uses_sdk(min='P')
-    expected = self.manifest_tmpl % self.uses_sdk(min='P', target='P')
-    output = self.raise_min_sdk_version_test(manifest_input, '28', False)
+    expected = self.manifest_tmpl % self.uses_sdk(min='P', target='28')
+    output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
     self.assertEqual(output, expected)
 
   def test_target(self):
@@ -140,56 +142,56 @@
 
     manifest_input = self.manifest_tmpl % self.uses_sdk(min='26', target='27')
     expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
-    output = self.raise_min_sdk_version_test(manifest_input, '28', False)
+    output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
     self.assertEqual(output, expected)
 
   def test_no_target(self):
     """Tests inserting targetSdkVersion when minSdkVersion exists."""
 
     manifest_input = self.manifest_tmpl % self.uses_sdk(min='27')
-    expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
-    output = self.raise_min_sdk_version_test(manifest_input, '28', False)
+    expected = self.manifest_tmpl % self.uses_sdk(min='28', target='29')
+    output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
     self.assertEqual(output, expected)
 
   def test_target_no_min(self):
-      """Tests inserting targetSdkVersion when minSdkVersion exists."""
+    """"Tests inserting targetSdkVersion when minSdkVersion exists."""
 
-      manifest_input = self.manifest_tmpl % self.uses_sdk(target='27')
-      expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
-      output = self.raise_min_sdk_version_test(manifest_input, '28', False)
-      self.assertEqual(output, expected)
+    manifest_input = self.manifest_tmpl % self.uses_sdk(target='27')
+    expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
+    output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
+    self.assertEqual(output, expected)
 
   def test_no_target_no_min(self):
     """Tests inserting targetSdkVersion when minSdkVersion does not exist."""
 
     manifest_input = self.manifest_tmpl % ''
-    expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28')
-    output = self.raise_min_sdk_version_test(manifest_input, '28', False)
+    expected = self.manifest_tmpl % self.uses_sdk(min='28', target='29')
+    output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
     self.assertEqual(output, expected)
 
   def test_library_no_target(self):
-      """Tests inserting targetSdkVersion when minSdkVersion exists."""
+    """Tests inserting targetSdkVersion when minSdkVersion exists."""
 
-      manifest_input = self.manifest_tmpl % self.uses_sdk(min='27')
-      expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
-      output = self.raise_min_sdk_version_test(manifest_input, '28', True)
-      self.assertEqual(output, expected)
+    manifest_input = self.manifest_tmpl % self.uses_sdk(min='27')
+    expected = self.manifest_tmpl % self.uses_sdk(min='28', target='15')
+    output = self.raise_min_sdk_version_test(manifest_input, '28', '29', True)
+    self.assertEqual(output, expected)
 
   def test_library_target_no_min(self):
-      """Tests inserting targetSdkVersion when minSdkVersion exists."""
+    """Tests inserting targetSdkVersion when minSdkVersion exists."""
 
-      manifest_input = self.manifest_tmpl % self.uses_sdk(target='27')
-      expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
-      output = self.raise_min_sdk_version_test(manifest_input, '28', True)
-      self.assertEqual(output, expected)
+    manifest_input = self.manifest_tmpl % self.uses_sdk(target='27')
+    expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
+    output = self.raise_min_sdk_version_test(manifest_input, '28', '29', True)
+    self.assertEqual(output, expected)
 
   def test_library_no_target_no_min(self):
-      """Tests inserting targetSdkVersion when minSdkVersion does not exist."""
+    """Tests inserting targetSdkVersion when minSdkVersion does not exist."""
 
-      manifest_input = self.manifest_tmpl % ''
-      expected = self.manifest_tmpl % self.uses_sdk(min='28', target='1')
-      output = self.raise_min_sdk_version_test(manifest_input, '28', True)
-      self.assertEqual(output, expected)
+    manifest_input = self.manifest_tmpl % ''
+    expected = self.manifest_tmpl % self.uses_sdk(min='28', target='15')
+    output = self.raise_min_sdk_version_test(manifest_input, '28', '29', True)
+    self.assertEqual(output, expected)
 
   def test_extra(self):
     """Tests that extra attributes and elements are maintained."""
@@ -202,10 +204,10 @@
     # pylint: disable=line-too-long
     expected = self.manifest_tmpl % (
         '    <!-- comment -->\n'
-        '    <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="27" extra="foo"/>\n'
+        '    <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="29" extra="foo"/>\n'
         '    <application/>\n')
 
-    output = self.raise_min_sdk_version_test(manifest_input, '28', False)
+    output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
 
     self.assertEqual(output, expected)
 
@@ -216,10 +218,10 @@
 
     # pylint: disable=line-too-long
     expected = self.manifest_tmpl % (
-        '  <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="28"/>\n'
+        '  <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="29"/>\n'
         '  <!-- comment -->\n')
 
-    output = self.raise_min_sdk_version_test(manifest_input, '28', False)
+    output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
 
     self.assertEqual(output, expected)
 
diff --git a/scripts/strip.sh b/scripts/strip.sh
index 432582f..29594dc 100755
--- a/scripts/strip.sh
+++ b/scripts/strip.sh
@@ -29,6 +29,8 @@
 #   --keep-symbols
 #   --use-llvm-strip
 
+set -o pipefail
+
 OPTSTRING=d:i:o:-:
 
 usage() {
@@ -52,7 +54,7 @@
     # ${CROSS_COMPILE}strip --strip-all does not strip .ARM.attributes,
     # so we tell llvm-strip to keep it too.
     if [ ! -z "${use_llvm_strip}" ]; then
-        "${CLANG_BIN}/llvm-strip" --strip-all -keep=.ARM.attributes "${infile}" "${outfile}.tmp"
+        "${CLANG_BIN}/llvm-strip" --strip-all -keep=.ARM.attributes "${infile}" -o "${outfile}.tmp"
     else
         "${CROSS_COMPILE}strip" --strip-all "${infile}" -o "${outfile}.tmp"
     fi
@@ -63,27 +65,28 @@
     # we have not found a use case that is broken by objcopy yet.
     REMOVE_SECTIONS=`"${CROSS_COMPILE}readelf" -S "${infile}" | awk '/.debug_/ {print "--remove-section " $2}' | xargs`
     if [ ! -z "${use_llvm_strip}" ]; then
-        "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
-    else
         "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
+    else
+        "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS}
     fi
 }
 
 do_strip_keep_mini_debug_info() {
     rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz"
+    local fail=
     if [ ! -z "${use_llvm_strip}" ]; then
-        "${CLANG_BIN}/llvm-strip" --strip-all -keep=.ARM.attributes -remove-section=.comment "${infile}" "${outfile}.tmp"
+        "${CLANG_BIN}/llvm-strip" --strip-all -keep=.ARM.attributes -remove-section=.comment "${infile}" -o "${outfile}.tmp" || fail=true
     else
-        "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp"
+        "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp" || fail=true
     fi
-    if [ "$?" == "0" ]; then
+    if [ -z $fail ]; then
         # Current prebult llvm-objcopy does not support the following flags:
         #    --only-keep-debug --rename-section --keep-symbols
         # For the following use cases, ${CROSS_COMPILE}objcopy does fine with lld linked files,
         # except the --add-section flag.
         "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug"
-        "${CROSS_COMPILE}nm" -D "${infile}" --format=posix --defined-only | awk '{ print $$1 }' | sort >"${outfile}.dynsyms"
-        "${CROSS_COMPILE}nm" "${infile}" --format=posix --defined-only | awk '{ if ($$2 == "T" || $$2 == "t" || $$2 == "D") print $$1 }' | sort > "${outfile}.funcsyms"
+        "${CROSS_COMPILE}nm" -D "${infile}" --format=posix --defined-only 2> /dev/null | awk '{ print $1 }' | sort >"${outfile}.dynsyms"
+        "${CROSS_COMPILE}nm" "${infile}" --format=posix --defined-only | awk '{ if ($2 == "T" || $2 == "t" || $2 == "D") print $1 }' | sort > "${outfile}.funcsyms"
         comm -13 "${outfile}.dynsyms" "${outfile}.funcsyms" > "${outfile}.keep_symbols"
         echo >> "${outfile}.keep_symbols" # Ensure that the keep_symbols file is not empty.
         "${CROSS_COMPILE}objcopy" --rename-section .debug_frame=saved_debug_frame "${outfile}.debug" "${outfile}.mini_debuginfo"
diff --git a/scripts/toc.sh b/scripts/toc.sh
index bd6425b..8b1d25f 100755
--- a/scripts/toc.sh
+++ b/scripts/toc.sh
@@ -22,6 +22,7 @@
 #   -i ${file}: input file (required)
 #   -o ${file}: output file (required)
 #   -d ${file}: deps file (required)
+#   --elf | --macho | --pe: format (required)
 
 OPTSTRING=d:i:o:-:
 
@@ -36,13 +37,34 @@
 do_elf() {
     ("${CROSS_COMPILE}readelf" -d "${infile}" | grep SONAME || echo "No SONAME for ${infile}") > "${outfile}.tmp"
     "${CROSS_COMPILE}readelf" --dyn-syms "${infile}" | awk '{$2=""; $3=""; print}' >> "${outfile}.tmp"
+
+    cat <<EOF > "${depsfile}"
+${outfile}: \\
+  ${CROSS_COMPILE}readelf \\
+EOF
 }
 
 do_macho() {
     "${CROSS_COMPILE}/otool" -l "${infile}" | grep LC_ID_DYLIB -A 5 > "${outfile}.tmp"
-    "${CROSS_COMPILE}/nm" -gP "${infile}" | cut -f1-2 -d" " | grep -v 'U$' >> "${outfile}.tmp"
+    "${CROSS_COMPILE}/nm" -gP "${infile}" | cut -f1-2 -d" " | (grep -v 'U$' >> "${outfile}.tmp" || true)
+
+    cat <<EOF > "${depsfile}"
+${outfile}: \\
+  ${CROSS_COMPILE}/otool \\
+  ${CROSS_COMPILE}/nm \\
+EOF
 }
 
+do_pe() {
+    "${CROSS_COMPILE}objdump" -x "${infile}" | grep "^Name" | cut -f3 -d" " > "${outfile}.tmp"
+    "${CROSS_COMPILE}nm" -g -f p "${infile}" | cut -f1-2 -d" " >> "${outfile}.tmp"
+
+    cat <<EOF > "${depsfile}"
+${outfile}: \\
+  ${CROSS_COMPILE}objdump \\
+  ${CROSS_COMPILE}nm \\
+EOF
+}
 
 while getopts $OPTSTRING opt; do
     case "$opt" in
@@ -51,6 +73,9 @@
         o) outfile="${OPTARG}" ;;
         -)
             case "${OPTARG}" in
+                elf) elf=1 ;;
+                macho) macho=1 ;;
+                pe) pe=1 ;;
                 *) echo "Unknown option --${OPTARG}"; usage ;;
             esac;;
         ?) usage ;;
@@ -58,21 +83,26 @@
     esac
 done
 
-if [ -z "${infile}" ]; then
+if [ -z "${infile:-}" ]; then
     echo "-i argument is required"
     usage
 fi
 
-if [ -z "${outfile}" ]; then
+if [ -z "${outfile:-}" ]; then
     echo "-o argument is required"
     usage
 fi
 
-if [ -z "${depsfile}" ]; then
+if [ -z "${depsfile:-}" ]; then
     echo "-d argument is required"
     usage
 fi
 
+if [ -z "${CROSS_COMPILE:-}" ]; then
+    echo "CROSS_COMPILE environment variable must be set"
+    usage
+fi
+
 rm -f "${outfile}.tmp"
 
 cat <<EOF > "${depsfile}"
@@ -80,7 +110,15 @@
   ${CROSS_COMPILE}readelf \\
 EOF
 
-do_elf
+if [ -n "${elf:-}" ]; then
+    do_elf
+elif [ -n "${macho:-}" ]; then
+    do_macho
+elif [ -n "${pe:-}" ]; then
+    do_pe
+else
+    echo "--elf, --macho or --pe is required"; usage
+fi
 
 if cmp "${outfile}" "${outfile}.tmp" > /dev/null 2> /dev/null; then
     rm -f "${outfile}.tmp"
diff --git a/tradefed/autogen.go b/tradefed/autogen.go
index 264e422..131fdc4 100644
--- a/tradefed/autogen.go
+++ b/tradefed/autogen.go
@@ -22,6 +22,10 @@
 	"android/soong/android"
 )
 
+func getTestConfigTemplate(ctx android.ModuleContext, prop *string) android.OptionalPath {
+	return ctx.ExpandOptionalSource(prop, "test_config_template")
+}
+
 func getTestConfig(ctx android.ModuleContext, prop *string) android.Path {
 	if p := ctx.ExpandOptionalSource(prop, "test_config"); p.Valid() {
 		return p.Path()
@@ -41,8 +45,7 @@
 		return p, nil
 	} else if !strings.HasPrefix(ctx.ModuleDir(), "cts/") {
 		outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".config")
-
-		return outputFile, outputFile
+		return nil, outputFile
 	} else {
 		// CTS modules can be used for test data, so test config files must be
 		// explicitly created using AndroidTest.xml
@@ -63,59 +66,86 @@
 	})
 }
 
-func AutoGenNativeTestConfig(ctx android.ModuleContext, prop *string) android.Path {
-	path, autogenPath := testConfigPath(ctx, prop)
+func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string,
+	testConfigTemplateProp *string) android.Path {
+	path, autogenPath := testConfigPath(ctx, testConfigProp)
 	if autogenPath != nil {
-		if ctx.Device() {
-			autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}")
+		templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
+		if templatePath.Valid() {
+			autogenTemplate(ctx, autogenPath, templatePath.String())
 		} else {
-			autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}")
+			if ctx.Device() {
+				autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}")
+			} else {
+				autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}")
+			}
 		}
+		return autogenPath
 	}
 	return path
 }
 
-func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, prop *string) android.Path {
-	path, autogenPath := testConfigPath(ctx, prop)
+func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string,
+	testConfigTemplateProp *string) android.Path {
+	path, autogenPath := testConfigPath(ctx, testConfigProp)
 	if autogenPath != nil {
-		autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}")
+		templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
+		if templatePath.Valid() {
+			autogenTemplate(ctx, autogenPath, templatePath.String())
+		} else {
+			autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}")
+		}
+		return autogenPath
 	}
 	return path
 }
 
-func AutoGenJavaTestConfig(ctx android.ModuleContext, prop *string) android.Path {
-	path, autogenPath := testConfigPath(ctx, prop)
+func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string) android.Path {
+	path, autogenPath := testConfigPath(ctx, testConfigProp)
 	if autogenPath != nil {
-		if ctx.Device() {
-			autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}")
+		templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
+		if templatePath.Valid() {
+			autogenTemplate(ctx, autogenPath, templatePath.String())
 		} else {
-			autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}")
+			if ctx.Device() {
+				autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}")
+			} else {
+				autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}")
+			}
 		}
+		return autogenPath
 	}
 	return path
 }
 
 var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{
-	Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} ${InstrumentationTestConfigTemplate}",
+	Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template",
 	CommandDeps: []string{
 		"${AutoGenTestConfigScript}",
 		"${EmptyTestConfig}",
-		"${InstrumentationTestConfigTemplate}",
+		"$template",
 	},
-}, "name")
+}, "name", "template")
 
-func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, prop *string, manifest android.Path) android.Path {
-	path, autogenPath := testConfigPath(ctx, prop)
+func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, manifest android.Path) android.Path {
+	path, autogenPath := testConfigPath(ctx, testConfigProp)
 	if autogenPath != nil {
+		template := "${InstrumentationTestConfigTemplate}"
+		moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp)
+		if moduleTemplate.Valid() {
+			template = moduleTemplate.String()
+		}
 		ctx.Build(pctx, android.BuildParams{
 			Rule:        autogenInstrumentationTest,
 			Description: "test config",
 			Input:       manifest,
 			Output:      autogenPath,
 			Args: map[string]string{
-				"name": ctx.ModuleName(),
+				"name":     ctx.ModuleName(),
+				"template": template,
 			},
 		})
+		return autogenPath
 	}
 	return path
 }
diff --git a/ui/build/build.go b/ui/build/build.go
index 96cfdbb..377481b 100644
--- a/ui/build/build.go
+++ b/ui/build/build.go
@@ -37,7 +37,10 @@
 
 var combinedBuildNinjaTemplate = template.Must(template.New("combined").Parse(`
 builddir = {{.OutDir}}
-{{if .HasKatiSuffix}}include {{.KatiNinjaFile}}
+pool local_pool
+ depth = {{.Parallel}}
+build _kati_always_build_: phony
+{{if .HasKatiSuffix}}include {{.KatiBuildNinjaFile}}
 {{end -}}
 include {{.SoongNinjaFile}}
 `))
@@ -71,6 +74,17 @@
 	BuildAll           = BuildProductConfig | BuildSoong | BuildKati | BuildNinja
 )
 
+func checkProblematicFiles(ctx Context) {
+	files := []string{"Android.mk", "CleanSpec.mk"}
+	for _, file := range files {
+		if _, err := os.Stat(file); !os.IsNotExist(err) {
+			absolute := absPath(ctx, file)
+			ctx.Printf("Found %s in tree root. This file needs to be removed to build.\n", file)
+			ctx.Fatalf("    rm %s\n", absolute)
+		}
+	}
+}
+
 func checkCaseSensitivity(ctx Context, config Config) {
 	outDir := config.OutDir()
 	lowerCase := filepath.Join(outDir, "casecheck.txt")
@@ -131,6 +145,8 @@
 	buildLock := BecomeSingletonOrFail(ctx, config)
 	defer buildLock.Unlock()
 
+	checkProblematicFiles(ctx)
+
 	SetupOutDir(ctx, config)
 
 	checkCaseSensitivity(ctx, config)
@@ -161,7 +177,9 @@
 
 	if what&BuildKati != 0 {
 		// Run ckati
-		runKati(ctx, config)
+		genKatiSuffix(ctx, config)
+		runKatiCleanSpec(ctx, config)
+		runKatiBuild(ctx, config)
 
 		ioutil.WriteFile(config.LastKatiSuffixFile(), []byte(config.KatiSuffix()), 0777)
 	} else {
diff --git a/ui/build/config.go b/ui/build/config.go
index 2605f5b..d470b96 100644
--- a/ui/build/config.go
+++ b/ui/build/config.go
@@ -369,14 +369,14 @@
 
 func (c *configImpl) OutDir() string {
 	if outDir, ok := c.environ.Get("OUT_DIR"); ok {
-		return outDir
+		return filepath.Clean(outDir)
 	}
 	return "out"
 }
 
 func (c *configImpl) DistDir() string {
 	if distDir, ok := c.environ.Get("DIST_DIR"); ok {
-		return distDir
+		return filepath.Clean(distDir)
 	}
 	return filepath.Join(c.OutDir(), "dist")
 }
@@ -501,8 +501,8 @@
 	return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
 }
 
-func (c *configImpl) KatiNinjaFile() string {
-	return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+".ninja")
+func (c *configImpl) KatiBuildNinjaFile() string {
+	return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
 }
 
 func (c *configImpl) SoongNinjaFile() string {
diff --git a/ui/build/dumpvars.go b/ui/build/dumpvars.go
index fadf6c6..ffea841 100644
--- a/ui/build/dumpvars.go
+++ b/ui/build/dumpvars.go
@@ -198,5 +198,5 @@
 
 	config.SetPdkBuild(make_vars["TARGET_BUILD_PDK"] == "true")
 	config.SetBuildBrokenDupRules(make_vars["BUILD_BROKEN_DUP_RULES"] == "true")
-	config.SetBuildBrokenPhonyTargets(make_vars["BUILD_BROKEN_PHONY_TARGETS"] != "false")
+	config.SetBuildBrokenPhonyTargets(make_vars["BUILD_BROKEN_PHONY_TARGETS"] == "true")
 }
diff --git a/ui/build/finder.go b/ui/build/finder.go
index 6dc35a0..3130f74 100644
--- a/ui/build/finder.go
+++ b/ui/build/finder.go
@@ -62,6 +62,7 @@
 			"Android.bp",
 			"Blueprints",
 			"CleanSpec.mk",
+			"OWNERS",
 			"TEST_MAPPING",
 		},
 	}
@@ -102,10 +103,16 @@
 		ctx.Fatalf("Could not export module list: %v", err)
 	}
 
+	owners := f.FindNamedAt(".", "OWNERS")
+	err = dumpListToFile(owners, filepath.Join(dumpDir, "OWNERS.list"))
+	if err != nil {
+		ctx.Fatalf("Could not find OWNERS: %v", err)
+	}
+
 	testMappings := f.FindNamedAt(".", "TEST_MAPPING")
 	err = dumpListToFile(testMappings, filepath.Join(dumpDir, "TEST_MAPPING.list"))
 	if err != nil {
-		ctx.Fatalf("Could not find modules: %v", err)
+		ctx.Fatalf("Could not find TEST_MAPPING: %v", err)
 	}
 
 	androidBps := f.FindNamedAt(".", "Android.bp")
diff --git a/ui/build/kati.go b/ui/build/kati.go
index b54872c..546fd1a 100644
--- a/ui/build/kati.go
+++ b/ui/build/kati.go
@@ -19,7 +19,6 @@
 	"fmt"
 	"io/ioutil"
 	"path/filepath"
-	"strconv"
 	"strings"
 
 	"android/soong/ui/status"
@@ -27,6 +26,9 @@
 
 var spaceSlashReplacer = strings.NewReplacer("/", "_", " ", "_")
 
+const katiBuildSuffix = ""
+const katiCleanspecSuffix = "-cleanspec"
+
 // genKatiSuffix creates a suffix for kati-generated files so that we can cache
 // them based on their inputs. So this should encode all common changes to Kati
 // inputs. Currently that includes the TARGET_PRODUCT, kati-processed command
@@ -49,7 +51,7 @@
 		ctx.Verbosef("Kati ninja suffix too long: %q", katiSuffix)
 		ctx.Verbosef("Replacing with: %q", shortSuffix)
 
-		if err := ioutil.WriteFile(strings.TrimSuffix(config.KatiNinjaFile(), "ninja")+"suf", []byte(katiSuffix), 0777); err != nil {
+		if err := ioutil.WriteFile(strings.TrimSuffix(config.KatiBuildNinjaFile(), "ninja")+"suf", []byte(katiSuffix), 0777); err != nil {
 			ctx.Println("Error writing suffix file:", err)
 		}
 	} else {
@@ -57,62 +59,31 @@
 	}
 }
 
-func runKati(ctx Context, config Config) {
-	genKatiSuffix(ctx, config)
-
-	runKatiCleanSpec(ctx, config)
-
-	ctx.BeginTrace("kati")
-	defer ctx.EndTrace()
-
+func runKati(ctx Context, config Config, extraSuffix string, args []string) {
 	executable := config.PrebuiltBuildTool("ckati")
-	args := []string{
+	args = append([]string{
 		"--ninja",
 		"--ninja_dir=" + config.OutDir(),
-		"--ninja_suffix=" + config.KatiSuffix(),
+		"--ninja_suffix=" + config.KatiSuffix() + extraSuffix,
+		"--no_ninja_prelude",
 		"--regen",
 		"--ignore_optional_include=" + filepath.Join(config.OutDir(), "%.P"),
 		"--detect_android_echo",
 		"--color_warnings",
 		"--gen_all_targets",
+		"--use_find_emulator",
 		"--werror_find_emulator",
 		"--no_builtin_rules",
 		"--werror_suffix_rules",
 		"--warn_real_to_phony",
 		"--warn_phony_looks_real",
 		"--kati_stats",
-		"-f", "build/make/core/main.mk",
-	}
-
-	// PDK builds still uses a few implicit rules
-	if !config.IsPdkBuild() {
-		args = append(args, "--werror_implicit_rules")
-	}
-
-	if !config.BuildBrokenDupRules() {
-		args = append(args, "--werror_overriding_commands")
-	}
-
-	if !config.BuildBrokenPhonyTargets() {
-		args = append(args, "--werror_real_to_phony", "--werror_phony_looks_real")
-	}
-
-	if !config.Environment().IsFalse("KATI_EMULATE_FIND") {
-		args = append(args, "--use_find_emulator")
-	}
-
-	args = append(args, config.KatiArgs()...)
+	}, args...)
 
 	args = append(args,
-		"BUILDING_WITH_NINJA=true",
-		"SOONG_ANDROID_MK="+config.SoongAndroidMk(),
 		"SOONG_MAKEVARS_MK="+config.SoongMakeVarsMk(),
 		"TARGET_DEVICE_DIR="+config.TargetDeviceDir())
 
-	if config.UseGoma() {
-		args = append(args, "-j"+strconv.Itoa(config.Parallel()))
-	}
-
 	cmd := Command(ctx, config, "ckati", executable, args...)
 	cmd.Sandbox = katiSandbox
 	pipe, err := cmd.StdoutPipe()
@@ -126,38 +97,46 @@
 	cmd.WaitOrFatal()
 }
 
+func runKatiBuild(ctx Context, config Config) {
+	ctx.BeginTrace("kati build")
+	defer ctx.EndTrace()
+
+	args := []string{
+		"--writable", config.OutDir() + "/",
+		"--writable", config.DistDir() + "/",
+		"-f", "build/make/core/main.mk",
+	}
+
+	// PDK builds still uses a few implicit rules
+	if !config.IsPdkBuild() {
+		args = append(args, "--werror_implicit_rules")
+	}
+
+	if !config.BuildBrokenDupRules() {
+		args = append(args, "--werror_overriding_commands")
+	}
+
+	if !config.BuildBrokenPhonyTargets() {
+		args = append(args,
+			"--werror_real_to_phony",
+			"--werror_phony_looks_real",
+			"--werror_writable")
+	}
+
+	args = append(args, config.KatiArgs()...)
+
+	args = append(args, "SOONG_ANDROID_MK="+config.SoongAndroidMk())
+
+	runKati(ctx, config, katiBuildSuffix, args)
+}
+
 func runKatiCleanSpec(ctx Context, config Config) {
 	ctx.BeginTrace("kati cleanspec")
 	defer ctx.EndTrace()
 
-	executable := config.PrebuiltBuildTool("ckati")
-	args := []string{
-		"--ninja",
-		"--ninja_dir=" + config.OutDir(),
-		"--ninja_suffix=" + config.KatiSuffix() + "-cleanspec",
-		"--regen",
-		"--detect_android_echo",
-		"--color_warnings",
-		"--gen_all_targets",
-		"--werror_find_emulator",
+	runKati(ctx, config, katiCleanspecSuffix, []string{
+		"--werror_implicit_rules",
 		"--werror_overriding_commands",
-		"--use_find_emulator",
-		"--kati_stats",
 		"-f", "build/make/core/cleanbuild.mk",
-		"BUILDING_WITH_NINJA=true",
-		"SOONG_MAKEVARS_MK=" + config.SoongMakeVarsMk(),
-		"TARGET_DEVICE_DIR=" + config.TargetDeviceDir(),
-	}
-
-	cmd := Command(ctx, config, "ckati", executable, args...)
-	cmd.Sandbox = katiCleanSpecSandbox
-	pipe, err := cmd.StdoutPipe()
-	if err != nil {
-		ctx.Fatalln("Error getting output pipe for ckati:", err)
-	}
-	cmd.Stderr = cmd.Stdout
-
-	cmd.StartOrFatal()
-	status.KatiReader(ctx.Status.StartTool(), pipe)
-	cmd.WaitOrFatal()
+	})
 }
diff --git a/zip/cmd/main.go b/zip/cmd/main.go
index 60017aa..b4f75f7 100644
--- a/zip/cmd/main.go
+++ b/zip/cmd/main.go
@@ -15,29 +15,19 @@
 package main
 
 import (
-	"bytes"
 	"flag"
 	"fmt"
-	"io"
 	"io/ioutil"
 	"os"
-	"path/filepath"
 	"runtime"
+	"runtime/pprof"
+	"runtime/trace"
+	"strconv"
 	"strings"
 
 	"android/soong/zip"
 )
 
-type byteReaderCloser struct {
-	*bytes.Reader
-	io.Closer
-}
-
-type pathMapping struct {
-	dest, src string
-	zipMethod uint16
-}
-
 type uniqueSet map[string]bool
 
 func (u *uniqueSet) String() string {
@@ -56,78 +46,67 @@
 
 type file struct{}
 
+func (file) String() string { return `""` }
+
+func (file) Set(s string) error {
+	fileArgsBuilder.File(s)
+	return nil
+}
+
 type listFiles struct{}
 
+func (listFiles) String() string { return `""` }
+
+func (listFiles) Set(s string) error {
+	fileArgsBuilder.List(s)
+	return nil
+}
+
 type dir struct{}
 
-func (f *file) String() string {
-	return `""`
-}
+func (dir) String() string { return `""` }
 
-func (f *file) Set(s string) error {
-	if *relativeRoot == "" {
-		return fmt.Errorf("must pass -C before -f")
-	}
-
-	fArgs = append(fArgs, zip.FileArg{
-		PathPrefixInZip:     filepath.Clean(*rootPrefix),
-		SourcePrefixToStrip: filepath.Clean(*relativeRoot),
-		SourceFiles:         []string{s},
-	})
-
+func (dir) Set(s string) error {
+	fileArgsBuilder.Dir(s)
 	return nil
 }
 
-func (l *listFiles) String() string {
-	return `""`
-}
+type relativeRoot struct{}
 
-func (l *listFiles) Set(s string) error {
-	if *relativeRoot == "" {
-		return fmt.Errorf("must pass -C before -l")
-	}
+func (relativeRoot) String() string { return "" }
 
-	list, err := ioutil.ReadFile(s)
-	if err != nil {
-		return err
-	}
-
-	fArgs = append(fArgs, zip.FileArg{
-		PathPrefixInZip:     filepath.Clean(*rootPrefix),
-		SourcePrefixToStrip: filepath.Clean(*relativeRoot),
-		SourceFiles:         strings.Split(string(list), "\n"),
-	})
-
+func (relativeRoot) Set(s string) error {
+	fileArgsBuilder.SourcePrefixToStrip(s)
 	return nil
 }
 
-func (d *dir) String() string {
-	return `""`
+type junkPaths struct{}
+
+func (junkPaths) IsBoolFlag() bool { return true }
+func (junkPaths) String() string   { return "" }
+
+func (junkPaths) Set(s string) error {
+	v, err := strconv.ParseBool(s)
+	fileArgsBuilder.JunkPaths(v)
+	return err
 }
 
-func (d *dir) Set(s string) error {
-	if *relativeRoot == "" {
-		return fmt.Errorf("must pass -C before -D")
-	}
+type rootPrefix struct{}
 
-	fArgs = append(fArgs, zip.FileArg{
-		PathPrefixInZip:     filepath.Clean(*rootPrefix),
-		SourcePrefixToStrip: filepath.Clean(*relativeRoot),
-		GlobDir:             filepath.Clean(s),
-	})
+func (rootPrefix) String() string { return "" }
 
+func (rootPrefix) Set(s string) error {
+	fileArgsBuilder.PathPrefixInZip(s)
 	return nil
 }
 
 var (
-	rootPrefix, relativeRoot *string
-
-	fArgs            zip.FileArgs
+	fileArgsBuilder  = zip.NewFileArgsBuilder()
 	nonDeflatedFiles = make(uniqueSet)
 )
 
 func usage() {
-	fmt.Fprintf(os.Stderr, "usage: zip -o zipfile [-m manifest] -C dir [-f|-l file]...\n")
+	fmt.Fprintf(os.Stderr, "usage: soong_zip -o zipfile [-m manifest] [-C dir] [-f|-l file] [-D dir]...\n")
 	flag.PrintDefaults()
 	os.Exit(2)
 }
@@ -149,32 +128,70 @@
 	}
 
 	flags := flag.NewFlagSet("flags", flag.ExitOnError)
+	flags.Usage = usage
 
 	out := flags.String("o", "", "file to write zip file to")
 	manifest := flags.String("m", "", "input jar manifest file name")
 	directories := flags.Bool("d", false, "include directories in zip")
-	rootPrefix = flags.String("P", "", "path prefix within the zip at which to place files")
-	relativeRoot = flags.String("C", "", "path to use as relative root of files in following -f, -l, or -D arguments")
-	parallelJobs := flags.Int("j", runtime.NumCPU(), "number of parallel threads to use")
 	compLevel := flags.Int("L", 5, "deflate compression level (0-9)")
 	emulateJar := flags.Bool("jar", false, "modify the resultant .zip to emulate the output of 'jar'")
 	writeIfChanged := flags.Bool("write_if_changed", false, "only update resultant .zip if it has changed")
+	ignoreMissingFiles := flags.Bool("ignore_missing_files", false, "continue if a requested file does not exist")
+	symlinks := flags.Bool("symlinks", true, "store symbolic links in zip instead of following them")
 
+	parallelJobs := flags.Int("parallel", runtime.NumCPU(), "number of parallel threads to use")
 	cpuProfile := flags.String("cpuprofile", "", "write cpu profile to file")
 	traceFile := flags.String("trace", "", "write trace to file")
 
+	flags.Var(&rootPrefix{}, "P", "path prefix within the zip at which to place files")
 	flags.Var(&listFiles{}, "l", "file containing list of .class files")
 	flags.Var(&dir{}, "D", "directory to include in zip")
 	flags.Var(&file{}, "f", "file to include in zip")
 	flags.Var(&nonDeflatedFiles, "s", "file path to be stored within the zip without compression")
+	flags.Var(&relativeRoot{}, "C", "path to use as relative root of files in following -f, -l, or -D arguments")
+	flags.Var(&junkPaths{}, "j", "junk paths, zip files without directory names")
 
 	flags.Parse(expandedArgs[1:])
 
-	err := zip.Run(zip.ZipArgs{
-		FileArgs:                 fArgs,
+	if flags.NArg() > 0 {
+		fmt.Fprintf(os.Stderr, "unexpected arguments %s\n", strings.Join(flags.Args(), " "))
+		flags.Usage()
+	}
+
+	if *cpuProfile != "" {
+		f, err := os.Create(*cpuProfile)
+		if err != nil {
+			fmt.Fprintln(os.Stderr, err.Error())
+			os.Exit(1)
+		}
+		defer f.Close()
+		pprof.StartCPUProfile(f)
+		defer pprof.StopCPUProfile()
+	}
+
+	if *traceFile != "" {
+		f, err := os.Create(*traceFile)
+		if err != nil {
+			fmt.Fprintln(os.Stderr, err.Error())
+			os.Exit(1)
+		}
+		defer f.Close()
+		err = trace.Start(f)
+		if err != nil {
+			fmt.Fprintln(os.Stderr, err.Error())
+			os.Exit(1)
+		}
+		defer trace.Stop()
+	}
+
+	if fileArgsBuilder.Error() != nil {
+		fmt.Fprintln(os.Stderr, fileArgsBuilder.Error())
+		os.Exit(1)
+	}
+
+	err := zip.Zip(zip.ZipArgs{
+		FileArgs:                 fileArgsBuilder.FileArgs(),
 		OutputFilePath:           *out,
-		CpuProfileFilePath:       *cpuProfile,
-		TraceFilePath:            *traceFile,
 		EmulateJar:               *emulateJar,
 		AddDirectoryEntriesToZip: *directories,
 		CompressionLevel:         *compLevel,
@@ -182,9 +199,11 @@
 		NumParallelJobs:          *parallelJobs,
 		NonDeflatedFiles:         nonDeflatedFiles,
 		WriteIfChanged:           *writeIfChanged,
+		StoreSymlinks:            *symlinks,
+		IgnoreMissingFiles:       *ignoreMissingFiles,
 	})
 	if err != nil {
-		fmt.Fprintln(os.Stderr, err.Error())
+		fmt.Fprintln(os.Stderr, "error:", err.Error())
 		os.Exit(1)
 	}
 }
diff --git a/zip/zip.go b/zip/zip.go
index a89fa9f..774966a 100644
--- a/zip/zip.go
+++ b/zip/zip.go
@@ -22,14 +22,12 @@
 	"hash/crc32"
 	"io"
 	"io/ioutil"
-	"log"
 	"os"
 	"path/filepath"
-	"runtime/pprof"
-	"runtime/trace"
 	"sort"
 	"strings"
 	"sync"
+	"syscall"
 	"time"
 	"unicode"
 
@@ -68,29 +66,112 @@
 	zipMethod uint16
 }
 
-type uniqueSet map[string]bool
-
-func (u *uniqueSet) String() string {
-	return `""`
-}
-
-func (u *uniqueSet) Set(s string) error {
-	if _, found := (*u)[s]; found {
-		return fmt.Errorf("File %q was specified twice as a file to not deflate", s)
-	} else {
-		(*u)[s] = true
-	}
-
-	return nil
-}
-
 type FileArg struct {
 	PathPrefixInZip, SourcePrefixToStrip string
 	SourceFiles                          []string
+	JunkPaths                            bool
 	GlobDir                              string
 }
 
-type FileArgs []FileArg
+type FileArgsBuilder struct {
+	state FileArg
+	err   error
+	fs    pathtools.FileSystem
+
+	fileArgs []FileArg
+}
+
+func NewFileArgsBuilder() *FileArgsBuilder {
+	return &FileArgsBuilder{
+		fs: pathtools.OsFs,
+	}
+}
+
+func (b *FileArgsBuilder) JunkPaths(v bool) *FileArgsBuilder {
+	b.state.JunkPaths = v
+	b.state.SourcePrefixToStrip = ""
+	return b
+}
+
+func (b *FileArgsBuilder) SourcePrefixToStrip(prefixToStrip string) *FileArgsBuilder {
+	b.state.JunkPaths = false
+	b.state.SourcePrefixToStrip = prefixToStrip
+	return b
+}
+
+func (b *FileArgsBuilder) PathPrefixInZip(rootPrefix string) *FileArgsBuilder {
+	b.state.PathPrefixInZip = rootPrefix
+	return b
+}
+
+func (b *FileArgsBuilder) File(name string) *FileArgsBuilder {
+	if b.err != nil {
+		return b
+	}
+
+	arg := b.state
+	arg.SourceFiles = []string{name}
+	b.fileArgs = append(b.fileArgs, arg)
+	return b
+}
+
+func (b *FileArgsBuilder) Dir(name string) *FileArgsBuilder {
+	if b.err != nil {
+		return b
+	}
+
+	arg := b.state
+	arg.GlobDir = name
+	b.fileArgs = append(b.fileArgs, arg)
+	return b
+}
+
+func (b *FileArgsBuilder) List(name string) *FileArgsBuilder {
+	if b.err != nil {
+		return b
+	}
+
+	f, err := b.fs.Open(name)
+	if err != nil {
+		b.err = err
+		return b
+	}
+	defer f.Close()
+
+	list, err := ioutil.ReadAll(f)
+	if err != nil {
+		b.err = err
+		return b
+	}
+
+	arg := b.state
+	arg.SourceFiles = strings.Split(string(list), "\n")
+	b.fileArgs = append(b.fileArgs, arg)
+	return b
+}
+
+func (b *FileArgsBuilder) Error() error {
+	if b == nil {
+		return nil
+	}
+	return b.err
+}
+
+func (b *FileArgsBuilder) FileArgs() []FileArg {
+	if b == nil {
+		return nil
+	}
+	return b.fileArgs
+}
+
+type IncorrectRelativeRootError struct {
+	RelativeRoot string
+	Path         string
+}
+
+func (x IncorrectRelativeRootError) Error() string {
+	return fmt.Sprintf("path %q is outside relative root %q", x.Path, x.RelativeRoot)
+}
 
 type ZipWriter struct {
 	time         time.Time
@@ -106,6 +187,12 @@
 
 	compressorPool sync.Pool
 	compLevel      int
+
+	followSymlinks     pathtools.ShouldFollowSymlinks
+	ignoreMissingFiles bool
+
+	stderr io.Writer
+	fs     pathtools.FileSystem
 }
 
 type zipEntry struct {
@@ -120,10 +207,8 @@
 }
 
 type ZipArgs struct {
-	FileArgs                 FileArgs
+	FileArgs                 []FileArg
 	OutputFilePath           string
-	CpuProfileFilePath       string
-	TraceFilePath            string
 	EmulateJar               bool
 	AddDirectoryEntriesToZip bool
 	CompressionLevel         int
@@ -131,6 +216,11 @@
 	NumParallelJobs          int
 	NonDeflatedFiles         map[string]bool
 	WriteIfChanged           bool
+	StoreSymlinks            bool
+	IgnoreMissingFiles       bool
+
+	Stderr     io.Writer
+	Filesystem pathtools.FileSystem
 }
 
 const NOQUOTE = '\x00'
@@ -176,63 +266,112 @@
 	return args
 }
 
-func Run(args ZipArgs) (err error) {
-	if args.CpuProfileFilePath != "" {
-		f, err := os.Create(args.CpuProfileFilePath)
-		if err != nil {
-			fmt.Fprintln(os.Stderr, err.Error())
-			os.Exit(1)
-		}
-		defer f.Close()
-		pprof.StartCPUProfile(f)
-		defer pprof.StopCPUProfile()
-	}
-
-	if args.TraceFilePath != "" {
-		f, err := os.Create(args.TraceFilePath)
-		if err != nil {
-			fmt.Fprintln(os.Stderr, err.Error())
-			os.Exit(1)
-		}
-		defer f.Close()
-		err = trace.Start(f)
-		if err != nil {
-			fmt.Fprintln(os.Stderr, err.Error())
-			os.Exit(1)
-		}
-		defer trace.Stop()
-	}
-
-	if args.OutputFilePath == "" {
-		return fmt.Errorf("output file path must be nonempty")
-	}
-
+func ZipTo(args ZipArgs, w io.Writer) error {
 	if args.EmulateJar {
 		args.AddDirectoryEntriesToZip = true
 	}
 
-	w := &ZipWriter{
-		time:         jar.DefaultTime,
-		createdDirs:  make(map[string]string),
-		createdFiles: make(map[string]string),
-		directories:  args.AddDirectoryEntriesToZip,
-		compLevel:    args.CompressionLevel,
+	// Have Glob follow symlinks if they are not being stored as symlinks in the zip file.
+	followSymlinks := pathtools.ShouldFollowSymlinks(!args.StoreSymlinks)
+
+	z := &ZipWriter{
+		time:               jar.DefaultTime,
+		createdDirs:        make(map[string]string),
+		createdFiles:       make(map[string]string),
+		directories:        args.AddDirectoryEntriesToZip,
+		compLevel:          args.CompressionLevel,
+		followSymlinks:     followSymlinks,
+		ignoreMissingFiles: args.IgnoreMissingFiles,
+		stderr:             args.Stderr,
+		fs:                 args.Filesystem,
 	}
+
+	if z.fs == nil {
+		z.fs = pathtools.OsFs
+	}
+
+	if z.stderr == nil {
+		z.stderr = os.Stderr
+	}
+
 	pathMappings := []pathMapping{}
 
+	noCompression := args.CompressionLevel == 0
+
 	for _, fa := range args.FileArgs {
-		srcs := fa.SourceFiles
+		var srcs []string
+		for _, s := range fa.SourceFiles {
+			s = strings.TrimSpace(s)
+			if s == "" {
+				continue
+			}
+
+			globbed, _, err := z.fs.Glob(s, nil, followSymlinks)
+			if err != nil {
+				return err
+			}
+			if len(globbed) == 0 {
+				err := &os.PathError{
+					Op:   "lstat",
+					Path: s,
+					Err:  os.ErrNotExist,
+				}
+				if args.IgnoreMissingFiles {
+					fmt.Fprintln(args.Stderr, "warning:", err)
+				} else {
+					return err
+				}
+			}
+			srcs = append(srcs, globbed...)
+		}
 		if fa.GlobDir != "" {
-			srcs = append(srcs, recursiveGlobFiles(fa.GlobDir)...)
+			if exists, isDir, err := z.fs.Exists(fa.GlobDir); err != nil {
+				return err
+			} else if !exists && !args.IgnoreMissingFiles {
+				err := &os.PathError{
+					Op:   "lstat",
+					Path: fa.GlobDir,
+					Err:  os.ErrNotExist,
+				}
+				if args.IgnoreMissingFiles {
+					fmt.Fprintln(args.Stderr, "warning:", err)
+				} else {
+					return err
+				}
+			} else if !isDir && !args.IgnoreMissingFiles {
+				err := &os.PathError{
+					Op:   "lstat",
+					Path: fa.GlobDir,
+					Err:  syscall.ENOTDIR,
+				}
+				if args.IgnoreMissingFiles {
+					fmt.Fprintln(args.Stderr, "warning:", err)
+				} else {
+					return err
+				}
+			}
+			globbed, _, err := z.fs.Glob(filepath.Join(fa.GlobDir, "**/*"), nil, followSymlinks)
+			if err != nil {
+				return err
+			}
+			srcs = append(srcs, globbed...)
 		}
 		for _, src := range srcs {
-			if err := fillPathPairs(fa.PathPrefixInZip,
-				fa.SourcePrefixToStrip, src, &pathMappings, args.NonDeflatedFiles); err != nil {
-				log.Fatal(err)
+			err := fillPathPairs(fa, src, &pathMappings, args.NonDeflatedFiles, noCompression)
+			if err != nil {
+				return err
 			}
 		}
 	}
 
+	return z.write(w, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.NumParallelJobs)
+}
+
+func Zip(args ZipArgs) error {
+	if args.OutputFilePath == "" {
+		return fmt.Errorf("output file path must be nonempty")
+	}
+
 	buf := &bytes.Buffer{}
 	var out io.Writer = buf
 
@@ -252,7 +391,7 @@
 		out = f
 	}
 
-	err = w.write(out, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.NumParallelJobs)
+	err := ZipTo(args, out)
 	if err != nil {
 		return err
 	}
@@ -267,20 +406,31 @@
 	return nil
 }
 
-func fillPathPairs(prefix, rel, src string, pathMappings *[]pathMapping, nonDeflatedFiles map[string]bool) error {
-	src = strings.TrimSpace(src)
-	if src == "" {
-		return nil
+func fillPathPairs(fa FileArg, src string, pathMappings *[]pathMapping,
+	nonDeflatedFiles map[string]bool, noCompression bool) error {
+
+	var dest string
+
+	if fa.JunkPaths {
+		dest = filepath.Base(src)
+	} else {
+		var err error
+		dest, err = filepath.Rel(fa.SourcePrefixToStrip, src)
+		if err != nil {
+			return err
+		}
+		if strings.HasPrefix(dest, "../") {
+			return IncorrectRelativeRootError{
+				Path:         src,
+				RelativeRoot: fa.SourcePrefixToStrip,
+			}
+		}
+
 	}
-	src = filepath.Clean(src)
-	dest, err := filepath.Rel(rel, src)
-	if err != nil {
-		return err
-	}
-	dest = filepath.Join(prefix, dest)
+	dest = filepath.Join(fa.PathPrefixInZip, dest)
 
 	zipMethod := zip.Deflate
-	if _, found := nonDeflatedFiles[dest]; found {
+	if _, found := nonDeflatedFiles[dest]; found || noCompression {
 		zipMethod = zip.Store
 	}
 	*pathMappings = append(*pathMappings,
@@ -296,13 +446,6 @@
 	sort.SliceStable(mappings, less)
 }
 
-type readerSeekerCloser interface {
-	io.Reader
-	io.ReaderAt
-	io.Closer
-	io.Seeker
-}
-
 func (z *ZipWriter) write(f io.Writer, pathMappings []pathMapping, manifest string, emulateJar bool, parallelJobs int) error {
 	z.errors = make(chan error)
 	defer close(z.errors)
@@ -449,7 +592,19 @@
 	var fileSize int64
 	var executable bool
 
-	if s, err := os.Lstat(src); err != nil {
+	var s os.FileInfo
+	var err error
+	if z.followSymlinks {
+		s, err = z.fs.Stat(src)
+	} else {
+		s, err = z.fs.Lstat(src)
+	}
+
+	if err != nil {
+		if os.IsNotExist(err) && z.ignoreMissingFiles {
+			fmt.Fprintln(z.stderr, "warning:", err)
+			return nil
+		}
 		return err
 	} else if s.IsDir() {
 		if z.directories {
@@ -480,7 +635,7 @@
 		executable = s.Mode()&0100 != 0
 	}
 
-	r, err := os.Open(src)
+	r, err := z.fs.Open(src)
 	if err != nil {
 		return err
 	}
@@ -510,7 +665,21 @@
 		return err
 	}
 
-	fh, buf, err := jar.ManifestFileContents(src)
+	var contents []byte
+	if src != "" {
+		f, err := z.fs.Open(src)
+		if err != nil {
+			return err
+		}
+
+		contents, err = ioutil.ReadAll(f)
+		f.Close()
+		if err != nil {
+			return err
+		}
+	}
+
+	fh, buf, err := jar.ManifestFileContents(contents)
 	if err != nil {
 		return err
 	}
@@ -520,7 +689,7 @@
 	return z.writeFileContents(fh, reader)
 }
 
-func (z *ZipWriter) writeFileContents(header *zip.FileHeader, r readerSeekerCloser) (err error) {
+func (z *ZipWriter) writeFileContents(header *zip.FileHeader, r pathtools.ReaderAtSeekerCloser) (err error) {
 
 	header.SetModTime(z.time)
 
@@ -790,7 +959,7 @@
 	fileHeader.SetModTime(z.time)
 	fileHeader.SetMode(0777 | os.ModeSymlink)
 
-	dest, err := os.Readlink(file)
+	dest, err := z.fs.Readlink(file)
 	if err != nil {
 		return err
 	}
@@ -815,15 +984,3 @@
 
 	return nil
 }
-
-func recursiveGlobFiles(path string) []string {
-	var files []string
-	filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
-		if !info.IsDir() {
-			files = append(files, path)
-		}
-		return nil
-	})
-
-	return files
-}
diff --git a/zip/zip_test.go b/zip/zip_test.go
index 03e7958..93c5f3d 100644
--- a/zip/zip_test.go
+++ b/zip/zip_test.go
@@ -15,10 +15,479 @@
 package zip
 
 import (
+	"bytes"
+	"hash/crc32"
+	"io"
+	"os"
 	"reflect"
+	"syscall"
 	"testing"
+
+	"android/soong/third_party/zip"
+
+	"github.com/google/blueprint/pathtools"
 )
 
+var (
+	fileA        = []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
+	fileB        = []byte("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB")
+	fileC        = []byte("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC")
+	fileEmpty    = []byte("")
+	fileManifest = []byte("Manifest-Version: 1.0\nCreated-By: soong_zip\n\n")
+
+	fileCustomManifest  = []byte("Custom manifest: true\n")
+	customManifestAfter = []byte("Manifest-Version: 1.0\nCreated-By: soong_zip\nCustom manifest: true\n\n")
+)
+
+var mockFs = pathtools.MockFs(map[string][]byte{
+	"a/a/a":            fileA,
+	"a/a/b":            fileB,
+	"a/a/c -> ../../c": nil,
+	"a/a/d -> b":       nil,
+	"c":                fileC,
+	"l":                []byte("a/a/a\na/a/b\nc\n"),
+	"l2":               []byte("missing\n"),
+	"manifest.txt":     fileCustomManifest,
+})
+
+func fh(name string, contents []byte, method uint16) zip.FileHeader {
+	return zip.FileHeader{
+		Name:               name,
+		Method:             method,
+		CRC32:              crc32.ChecksumIEEE(contents),
+		UncompressedSize64: uint64(len(contents)),
+		ExternalAttrs:      0,
+	}
+}
+
+func fhManifest(contents []byte) zip.FileHeader {
+	return zip.FileHeader{
+		Name:               "META-INF/MANIFEST.MF",
+		Method:             zip.Store,
+		CRC32:              crc32.ChecksumIEEE(contents),
+		UncompressedSize64: uint64(len(contents)),
+		ExternalAttrs:      (syscall.S_IFREG | 0700) << 16,
+	}
+}
+
+func fhLink(name string, to string) zip.FileHeader {
+	return zip.FileHeader{
+		Name:               name,
+		Method:             zip.Store,
+		CRC32:              crc32.ChecksumIEEE([]byte(to)),
+		UncompressedSize64: uint64(len(to)),
+		ExternalAttrs:      (syscall.S_IFLNK | 0777) << 16,
+	}
+}
+
+func fhDir(name string) zip.FileHeader {
+	return zip.FileHeader{
+		Name:               name,
+		Method:             zip.Store,
+		CRC32:              crc32.ChecksumIEEE(nil),
+		UncompressedSize64: 0,
+		ExternalAttrs:      (syscall.S_IFDIR|0700)<<16 | 0x10,
+	}
+}
+
+func fileArgsBuilder() *FileArgsBuilder {
+	return &FileArgsBuilder{
+		fs: mockFs,
+	}
+}
+
+func TestZip(t *testing.T) {
+	testCases := []struct {
+		name               string
+		args               *FileArgsBuilder
+		compressionLevel   int
+		emulateJar         bool
+		nonDeflatedFiles   map[string]bool
+		dirEntries         bool
+		manifest           string
+		storeSymlinks      bool
+		ignoreMissingFiles bool
+
+		files []zip.FileHeader
+		err   error
+	}{
+		{
+			name: "empty args",
+			args: fileArgsBuilder(),
+
+			files: []zip.FileHeader{},
+		},
+		{
+			name: "files",
+			args: fileArgsBuilder().
+				File("a/a/a").
+				File("a/a/b").
+				File("c"),
+			compressionLevel: 9,
+
+			files: []zip.FileHeader{
+				fh("a/a/a", fileA, zip.Deflate),
+				fh("a/a/b", fileB, zip.Deflate),
+				fh("c", fileC, zip.Deflate),
+			},
+		},
+		{
+			name: "files glob",
+			args: fileArgsBuilder().
+				SourcePrefixToStrip("a").
+				File("a/**/*"),
+			compressionLevel: 9,
+			storeSymlinks:    true,
+
+			files: []zip.FileHeader{
+				fh("a/a", fileA, zip.Deflate),
+				fh("a/b", fileB, zip.Deflate),
+				fhLink("a/c", "../../c"),
+				fhLink("a/d", "b"),
+			},
+		},
+		{
+			name: "dir",
+			args: fileArgsBuilder().
+				SourcePrefixToStrip("a").
+				Dir("a"),
+			compressionLevel: 9,
+			storeSymlinks:    true,
+
+			files: []zip.FileHeader{
+				fh("a/a", fileA, zip.Deflate),
+				fh("a/b", fileB, zip.Deflate),
+				fhLink("a/c", "../../c"),
+				fhLink("a/d", "b"),
+			},
+		},
+		{
+			name: "stored files",
+			args: fileArgsBuilder().
+				File("a/a/a").
+				File("a/a/b").
+				File("c"),
+			compressionLevel: 0,
+
+			files: []zip.FileHeader{
+				fh("a/a/a", fileA, zip.Store),
+				fh("a/a/b", fileB, zip.Store),
+				fh("c", fileC, zip.Store),
+			},
+		},
+		{
+			name: "symlinks in zip",
+			args: fileArgsBuilder().
+				File("a/a/a").
+				File("a/a/b").
+				File("a/a/c").
+				File("a/a/d"),
+			compressionLevel: 9,
+			storeSymlinks:    true,
+
+			files: []zip.FileHeader{
+				fh("a/a/a", fileA, zip.Deflate),
+				fh("a/a/b", fileB, zip.Deflate),
+				fhLink("a/a/c", "../../c"),
+				fhLink("a/a/d", "b"),
+			},
+		},
+		{
+			name: "follow symlinks",
+			args: fileArgsBuilder().
+				File("a/a/a").
+				File("a/a/b").
+				File("a/a/c").
+				File("a/a/d"),
+			compressionLevel: 9,
+			storeSymlinks:    false,
+
+			files: []zip.FileHeader{
+				fh("a/a/a", fileA, zip.Deflate),
+				fh("a/a/b", fileB, zip.Deflate),
+				fh("a/a/c", fileC, zip.Deflate),
+				fh("a/a/d", fileB, zip.Deflate),
+			},
+		},
+		{
+			name: "list",
+			args: fileArgsBuilder().
+				List("l"),
+			compressionLevel: 9,
+
+			files: []zip.FileHeader{
+				fh("a/a/a", fileA, zip.Deflate),
+				fh("a/a/b", fileB, zip.Deflate),
+				fh("c", fileC, zip.Deflate),
+			},
+		},
+		{
+			name: "prefix in zip",
+			args: fileArgsBuilder().
+				PathPrefixInZip("foo").
+				File("a/a/a").
+				File("a/a/b").
+				File("c"),
+			compressionLevel: 9,
+
+			files: []zip.FileHeader{
+				fh("foo/a/a/a", fileA, zip.Deflate),
+				fh("foo/a/a/b", fileB, zip.Deflate),
+				fh("foo/c", fileC, zip.Deflate),
+			},
+		},
+		{
+			name: "relative root",
+			args: fileArgsBuilder().
+				SourcePrefixToStrip("a").
+				File("a/a/a").
+				File("a/a/b"),
+			compressionLevel: 9,
+
+			files: []zip.FileHeader{
+				fh("a/a", fileA, zip.Deflate),
+				fh("a/b", fileB, zip.Deflate),
+			},
+		},
+		{
+			name: "multiple relative root",
+			args: fileArgsBuilder().
+				SourcePrefixToStrip("a").
+				File("a/a/a").
+				SourcePrefixToStrip("a/a").
+				File("a/a/b"),
+			compressionLevel: 9,
+
+			files: []zip.FileHeader{
+				fh("a/a", fileA, zip.Deflate),
+				fh("b", fileB, zip.Deflate),
+			},
+		},
+		{
+			name: "emulate jar",
+			args: fileArgsBuilder().
+				File("a/a/a").
+				File("a/a/b"),
+			compressionLevel: 9,
+			emulateJar:       true,
+
+			files: []zip.FileHeader{
+				fhDir("META-INF/"),
+				fhManifest(fileManifest),
+				fhDir("a/"),
+				fhDir("a/a/"),
+				fh("a/a/a", fileA, zip.Deflate),
+				fh("a/a/b", fileB, zip.Deflate),
+			},
+		},
+		{
+			name: "emulate jar with manifest",
+			args: fileArgsBuilder().
+				File("a/a/a").
+				File("a/a/b"),
+			compressionLevel: 9,
+			emulateJar:       true,
+			manifest:         "manifest.txt",
+
+			files: []zip.FileHeader{
+				fhDir("META-INF/"),
+				fhManifest(customManifestAfter),
+				fhDir("a/"),
+				fhDir("a/a/"),
+				fh("a/a/a", fileA, zip.Deflate),
+				fh("a/a/b", fileB, zip.Deflate),
+			},
+		},
+		{
+			name: "dir entries",
+			args: fileArgsBuilder().
+				File("a/a/a").
+				File("a/a/b"),
+			compressionLevel: 9,
+			dirEntries:       true,
+
+			files: []zip.FileHeader{
+				fhDir("a/"),
+				fhDir("a/a/"),
+				fh("a/a/a", fileA, zip.Deflate),
+				fh("a/a/b", fileB, zip.Deflate),
+			},
+		},
+		{
+			name: "junk paths",
+			args: fileArgsBuilder().
+				JunkPaths(true).
+				File("a/a/a").
+				File("a/a/b"),
+			compressionLevel: 9,
+
+			files: []zip.FileHeader{
+				fh("a", fileA, zip.Deflate),
+				fh("b", fileB, zip.Deflate),
+			},
+		},
+		{
+			name: "non deflated files",
+			args: fileArgsBuilder().
+				File("a/a/a").
+				File("a/a/b"),
+			compressionLevel: 9,
+			nonDeflatedFiles: map[string]bool{"a/a/a": true},
+
+			files: []zip.FileHeader{
+				fh("a/a/a", fileA, zip.Store),
+				fh("a/a/b", fileB, zip.Deflate),
+			},
+		},
+		{
+			name: "ignore missing files",
+			args: fileArgsBuilder().
+				File("a/a/a").
+				File("a/a/b").
+				File("missing"),
+			compressionLevel:   9,
+			ignoreMissingFiles: true,
+
+			files: []zip.FileHeader{
+				fh("a/a/a", fileA, zip.Deflate),
+				fh("a/a/b", fileB, zip.Deflate),
+			},
+		},
+
+		// errors
+		{
+			name: "error missing file",
+			args: fileArgsBuilder().
+				File("missing"),
+			err: os.ErrNotExist,
+		},
+		{
+			name: "error missing dir",
+			args: fileArgsBuilder().
+				Dir("missing"),
+			err: os.ErrNotExist,
+		},
+		{
+			name: "error missing file in list",
+			args: fileArgsBuilder().
+				List("l2"),
+			err: os.ErrNotExist,
+		},
+		{
+			name: "error incorrect relative root",
+			args: fileArgsBuilder().
+				SourcePrefixToStrip("b").
+				File("a/a/a"),
+			err: IncorrectRelativeRootError{},
+		},
+	}
+
+	for _, test := range testCases {
+		t.Run(test.name, func(t *testing.T) {
+			if test.args.Error() != nil {
+				t.Fatal(test.args.Error())
+			}
+
+			args := ZipArgs{}
+			args.FileArgs = test.args.FileArgs()
+			args.CompressionLevel = test.compressionLevel
+			args.EmulateJar = test.emulateJar
+			args.AddDirectoryEntriesToZip = test.dirEntries
+			args.NonDeflatedFiles = test.nonDeflatedFiles
+			args.ManifestSourcePath = test.manifest
+			args.StoreSymlinks = test.storeSymlinks
+			args.IgnoreMissingFiles = test.ignoreMissingFiles
+			args.Filesystem = mockFs
+			args.Stderr = &bytes.Buffer{}
+
+			buf := &bytes.Buffer{}
+			err := ZipTo(args, buf)
+
+			if (err != nil) != (test.err != nil) {
+				t.Fatalf("want error %v, got %v", test.err, err)
+			} else if test.err != nil {
+				if os.IsNotExist(test.err) {
+					if !os.IsNotExist(test.err) {
+						t.Fatalf("want error %v, got %v", test.err, err)
+					}
+				} else if _, wantRelativeRootErr := test.err.(IncorrectRelativeRootError); wantRelativeRootErr {
+					if _, gotRelativeRootErr := err.(IncorrectRelativeRootError); !gotRelativeRootErr {
+						t.Fatalf("want error %v, got %v", test.err, err)
+					}
+				} else {
+					t.Fatalf("want error %v, got %v", test.err, err)
+				}
+				return
+			}
+
+			br := bytes.NewReader(buf.Bytes())
+			zr, err := zip.NewReader(br, int64(br.Len()))
+			if err != nil {
+				t.Fatal(err)
+			}
+
+			var files []zip.FileHeader
+			for _, f := range zr.File {
+				r, err := f.Open()
+				if err != nil {
+					t.Fatalf("error when opening %s: %s", f.Name, err)
+				}
+
+				crc := crc32.NewIEEE()
+				len, err := io.Copy(crc, r)
+				r.Close()
+				if err != nil {
+					t.Fatalf("error when reading %s: %s", f.Name, err)
+				}
+
+				if uint64(len) != f.UncompressedSize64 {
+					t.Errorf("incorrect length for %s, want %d got %d", f.Name, f.UncompressedSize64, len)
+				}
+
+				if crc.Sum32() != f.CRC32 {
+					t.Errorf("incorrect crc for %s, want %x got %x", f.Name, f.CRC32, crc)
+				}
+
+				files = append(files, f.FileHeader)
+			}
+
+			if len(files) != len(test.files) {
+				t.Fatalf("want %d files, got %d", len(test.files), len(files))
+			}
+
+			for i := range files {
+				want := test.files[i]
+				got := files[i]
+
+				if want.Name != got.Name {
+					t.Errorf("incorrect file %d want %q got %q", i, want.Name, got.Name)
+					continue
+				}
+
+				if want.UncompressedSize64 != got.UncompressedSize64 {
+					t.Errorf("incorrect file %s length want %v got %v", want.Name,
+						want.UncompressedSize64, got.UncompressedSize64)
+				}
+
+				if want.ExternalAttrs != got.ExternalAttrs {
+					t.Errorf("incorrect file %s attrs want %x got %x", want.Name,
+						want.ExternalAttrs, got.ExternalAttrs)
+				}
+
+				if want.CRC32 != got.CRC32 {
+					t.Errorf("incorrect file %s crc want %v got %v", want.Name,
+						want.CRC32, got.CRC32)
+				}
+
+				if want.Method != got.Method {
+					t.Errorf("incorrect file %s method want %v got %v", want.Name,
+						want.Method, got.Method)
+				}
+			}
+		})
+	}
+}
+
 func TestReadRespFile(t *testing.T) {
 	testCases := []struct {
 		name, in string