Merge "Add a host prebuilt module type for usr/share."
diff --git a/Android.bp b/Android.bp
index ee51942..e9597f4 100644
--- a/Android.bp
+++ b/Android.bp
@@ -253,6 +253,7 @@
         "java/dex.go",
         "java/dexpreopt.go",
         "java/dexpreopt_bootjars.go",
+        "java/dexpreopt_config.go",
         "java/droiddoc.go",
         "java/gen.go",
         "java/genrule.go",
diff --git a/android/config.go b/android/config.go
index 7d2e829..24be10a 100644
--- a/android/config.go
+++ b/android/config.go
@@ -775,26 +775,10 @@
 	return c.productVariables.BootJars
 }
 
-func (c *config) PreoptBootJars() []string {
-	return c.productVariables.PreoptBootJars
-}
-
-func (c *config) DisableDexPreopt() bool {
-	return Bool(c.productVariables.DisableDexPreopt)
-}
-
-func (c *config) DisableDexPreoptForModule(name string) bool {
-	return Bool(c.productVariables.DisableDexPreopt) || InList(name, c.productVariables.DisableDexPreoptModules)
-}
-
 func (c *config) DexpreoptGlobalConfig() string {
 	return String(c.productVariables.DexpreoptGlobalConfig)
 }
 
-func (c *config) DexPreoptProfileDir() string {
-	return String(c.productVariables.DexPreoptProfileDir)
-}
-
 func (c *config) FrameworksBaseDirExists(ctx PathContext) bool {
 	return ExistentPathForSource(ctx, "frameworks", "base").Valid()
 }
diff --git a/android/paths.go b/android/paths.go
index 3366db1..afde55e 100644
--- a/android/paths.go
+++ b/android/paths.go
@@ -712,7 +712,7 @@
 		reportPathErrorf(ctx, "extension %q cannot contain /", ext)
 	}
 	ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext))
-	ret.rel = p.rel
+	ret.rel = pathtools.ReplaceExtension(p.rel, ext)
 	return ret
 }
 
@@ -724,7 +724,7 @@
 	}
 
 	ret := PathForOutput(ctx, filepath.Dir(p.path), path)
-	ret.rel = p.rel
+	ret.rel = filepath.Join(filepath.Dir(p.rel), path)
 	return ret
 }
 
diff --git a/android/paths_test.go b/android/paths_test.go
index 3b6d2ec..20a00a0 100644
--- a/android/paths_test.go
+++ b/android/paths_test.go
@@ -696,22 +696,26 @@
 	ctx := &configErrorWrapper{
 		config: TestConfig("out", nil),
 	}
-	p := PathForOutput(ctx, "system/framework/boot.art")
+	p := PathForOutput(ctx, "system/framework").Join(ctx, "boot.art")
 	p2 := p.ReplaceExtension(ctx, "oat")
 	fmt.Println(p, p2)
+	fmt.Println(p.Rel(), p2.Rel())
 
 	// Output:
 	// out/system/framework/boot.art out/system/framework/boot.oat
+	// boot.art boot.oat
 }
 
 func ExampleOutputPath_FileInSameDir() {
 	ctx := &configErrorWrapper{
 		config: TestConfig("out", nil),
 	}
-	p := PathForOutput(ctx, "system/framework/boot.art")
+	p := PathForOutput(ctx, "system/framework").Join(ctx, "boot.art")
 	p2 := p.InSameDir(ctx, "oat", "arm", "boot.vdex")
 	fmt.Println(p, p2)
+	fmt.Println(p.Rel(), p2.Rel())
 
 	// Output:
 	// out/system/framework/boot.art out/system/framework/oat/arm/boot.vdex
+	// boot.art oat/arm/boot.vdex
 }
diff --git a/android/rule_builder.go b/android/rule_builder.go
index 3b86947..5edd7b6 100644
--- a/android/rule_builder.go
+++ b/android/rule_builder.go
@@ -16,7 +16,6 @@
 
 import (
 	"fmt"
-	"path/filepath"
 	"sort"
 	"strings"
 
@@ -29,7 +28,7 @@
 type RuleBuilder struct {
 	commands       []*RuleBuilderCommand
 	installs       RuleBuilderInstalls
-	temporariesSet map[string]bool
+	temporariesSet map[WritablePath]bool
 	restat         bool
 	missingDeps    []string
 }
@@ -37,13 +36,14 @@
 // NewRuleBuilder returns a newly created RuleBuilder.
 func NewRuleBuilder() *RuleBuilder {
 	return &RuleBuilder{
-		temporariesSet: make(map[string]bool),
+		temporariesSet: make(map[WritablePath]bool),
 	}
 }
 
 // RuleBuilderInstall is a tuple of install from and to locations.
 type RuleBuilderInstall struct {
-	From, To string
+	From Path
+	To   string
 }
 
 type RuleBuilderInstalls []RuleBuilderInstall
@@ -56,7 +56,7 @@
 		if i != 0 {
 			sb.WriteRune(' ')
 		}
-		sb.WriteString(install.From)
+		sb.WriteString(install.From.String())
 		sb.WriteRune(':')
 		sb.WriteString(install.To)
 	}
@@ -80,7 +80,7 @@
 
 // Install associates an output of the rule with an install location, which can be retrieved later using
 // RuleBuilder.Installs.
-func (r *RuleBuilder) Install(from, to string) {
+func (r *RuleBuilder) Install(from Path, to string) {
 	r.installs = append(r.installs, RuleBuilderInstall{from, to})
 }
 
@@ -95,19 +95,22 @@
 
 // Temporary marks an output of a command as an intermediate file that will be used as an input to another command
 // in the same rule, and should not be listed in Outputs.
-func (r *RuleBuilder) Temporary(path string) {
+func (r *RuleBuilder) Temporary(path WritablePath) {
 	r.temporariesSet[path] = true
 }
 
 // DeleteTemporaryFiles adds a command to the rule that deletes any outputs that have been marked using Temporary
 // when the rule runs.  DeleteTemporaryFiles should be called after all calls to Temporary.
 func (r *RuleBuilder) DeleteTemporaryFiles() {
-	var temporariesList []string
+	var temporariesList WritablePaths
 
 	for intermediate := range r.temporariesSet {
 		temporariesList = append(temporariesList, intermediate)
 	}
-	sort.Strings(temporariesList)
+
+	sort.Slice(temporariesList, func(i, j int) bool {
+		return temporariesList[i].String() < temporariesList[j].String()
+	})
 
 	r.Command().Text("rm").Flag("-f").Outputs(temporariesList)
 }
@@ -115,32 +118,35 @@
 // Inputs returns the list of paths that were passed to the RuleBuilderCommand methods that take input paths, such
 // as RuleBuilderCommand.Input, RuleBuilderComand.Implicit, or RuleBuilderCommand.FlagWithInput.  Inputs to a command
 // that are also outputs of another command in the same RuleBuilder are filtered out.
-func (r *RuleBuilder) Inputs() []string {
+func (r *RuleBuilder) Inputs() Paths {
 	outputs := r.outputSet()
 
-	inputs := make(map[string]bool)
+	inputs := make(map[string]Path)
 	for _, c := range r.commands {
 		for _, input := range c.inputs {
-			if !outputs[input] {
-				inputs[input] = true
+			if _, isOutput := outputs[input.String()]; !isOutput {
+				inputs[input.String()] = input
 			}
 		}
 	}
 
-	var inputList []string
-	for input := range inputs {
+	var inputList Paths
+	for _, input := range inputs {
 		inputList = append(inputList, input)
 	}
-	sort.Strings(inputList)
+
+	sort.Slice(inputList, func(i, j int) bool {
+		return inputList[i].String() < inputList[j].String()
+	})
 
 	return inputList
 }
 
-func (r *RuleBuilder) outputSet() map[string]bool {
-	outputs := make(map[string]bool)
+func (r *RuleBuilder) outputSet() map[string]WritablePath {
+	outputs := make(map[string]WritablePath)
 	for _, c := range r.commands {
 		for _, output := range c.outputs {
-			outputs[output] = true
+			outputs[output.String()] = output
 		}
 	}
 	return outputs
@@ -148,16 +154,20 @@
 
 // Outputs returns the list of paths that were passed to the RuleBuilderCommand methods that take output paths, such
 // as RuleBuilderCommand.Output, RuleBuilderCommand.ImplicitOutput, or RuleBuilderCommand.FlagWithInput.
-func (r *RuleBuilder) Outputs() []string {
+func (r *RuleBuilder) Outputs() WritablePaths {
 	outputs := r.outputSet()
 
-	var outputList []string
-	for output := range outputs {
+	var outputList WritablePaths
+	for _, output := range outputs {
 		if !r.temporariesSet[output] {
 			outputList = append(outputList, output)
 		}
 	}
-	sort.Strings(outputList)
+
+	sort.Slice(outputList, func(i, j int) bool {
+		return outputList[i].String() < outputList[j].String()
+	})
+
 	return outputList
 }
 
@@ -166,11 +176,11 @@
 	return append(RuleBuilderInstalls(nil), r.installs...)
 }
 
-func (r *RuleBuilder) toolsSet() map[string]bool {
-	tools := make(map[string]bool)
+func (r *RuleBuilder) toolsSet() map[string]Path {
+	tools := make(map[string]Path)
 	for _, c := range r.commands {
 		for _, tool := range c.tools {
-			tools[tool] = true
+			tools[tool.String()] = tool
 		}
 	}
 
@@ -178,14 +188,18 @@
 }
 
 // Tools returns the list of paths that were passed to the RuleBuilderCommand.Tool method.
-func (r *RuleBuilder) Tools() []string {
+func (r *RuleBuilder) Tools() Paths {
 	toolsSet := r.toolsSet()
 
-	var toolsList []string
-	for tool := range toolsSet {
+	var toolsList Paths
+	for _, tool := range toolsSet {
 		toolsList = append(toolsList, tool)
 	}
-	sort.Strings(toolsList)
+
+	sort.Slice(toolsList, func(i, j int) bool {
+		return toolsList[i].String() < toolsList[j].String()
+	})
+
 	return toolsList
 }
 
@@ -211,45 +225,10 @@
 // Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for
 // Outputs.
 func (r *RuleBuilder) Build(pctx PackageContext, ctx BuilderContext, name string, desc string) {
-	// TODO: convert RuleBuilder arguments and storage to Paths
-	mctx, _ := ctx.(ModuleContext)
-	var inputs Paths
-	for _, input := range r.Inputs() {
-		// Module output paths
-		if mctx != nil {
-			rel, isRel := MaybeRel(ctx, PathForModuleOut(mctx).String(), input)
-			if isRel {
-				inputs = append(inputs, PathForModuleOut(mctx, rel))
-				continue
-			}
-		}
-
-		// Other output paths
-		rel, isRel := MaybeRel(ctx, PathForOutput(ctx).String(), input)
-		if isRel {
-			inputs = append(inputs, PathForOutput(ctx, rel))
-			continue
-		}
-
-		// TODO: remove this once boot image is moved to where PathForOutput can find it.
-		inputs = append(inputs, &unknownRulePath{input})
-	}
-
-	var outputs WritablePaths
-	for _, output := range r.Outputs() {
-		if mctx != nil {
-			rel := Rel(ctx, PathForModuleOut(mctx).String(), output)
-			outputs = append(outputs, PathForModuleOut(mctx, rel))
-		} else {
-			rel := Rel(ctx, PathForOutput(ctx).String(), output)
-			outputs = append(outputs, PathForOutput(ctx, rel))
-		}
-	}
-
 	if len(r.missingDeps) > 0 {
 		ctx.Build(pctx, BuildParams{
 			Rule:        ErrorRule,
-			Outputs:     outputs,
+			Outputs:     r.Outputs(),
 			Description: desc,
 			Args: map[string]string{
 				"error": "missing dependencies: " + strings.Join(r.missingDeps, ", "),
@@ -262,10 +241,10 @@
 		ctx.Build(pctx, BuildParams{
 			Rule: ctx.Rule(pctx, name, blueprint.RuleParams{
 				Command:     strings.Join(proptools.NinjaEscape(r.Commands()), " && "),
-				CommandDeps: r.Tools(),
+				CommandDeps: r.Tools().Strings(),
 			}),
-			Implicits:   inputs,
-			Outputs:     outputs,
+			Implicits:   r.Inputs(),
+			Outputs:     r.Outputs(),
 			Description: desc,
 		})
 	}
@@ -277,9 +256,9 @@
 // space as a separator from the previous method.
 type RuleBuilderCommand struct {
 	buf     []byte
-	inputs  []string
-	outputs []string
-	tools   []string
+	inputs  Paths
+	outputs WritablePaths
+	tools   Paths
 }
 
 // Text adds the specified raw text to the command line.  The text should not contain input or output paths or the
@@ -329,21 +308,21 @@
 
 // Tool adds the specified tool path to the command line.  The path will be also added to the dependencies returned by
 // RuleBuilder.Tools.
-func (c *RuleBuilderCommand) Tool(path string) *RuleBuilderCommand {
+func (c *RuleBuilderCommand) Tool(path Path) *RuleBuilderCommand {
 	c.tools = append(c.tools, path)
-	return c.Text(path)
+	return c.Text(path.String())
 }
 
 // Input adds the specified input path to the command line.  The path will also be added to the dependencies returned by
 // RuleBuilder.Inputs.
-func (c *RuleBuilderCommand) Input(path string) *RuleBuilderCommand {
+func (c *RuleBuilderCommand) Input(path Path) *RuleBuilderCommand {
 	c.inputs = append(c.inputs, path)
-	return c.Text(path)
+	return c.Text(path.String())
 }
 
 // Inputs adds the specified input paths to the command line, separated by spaces.  The paths will also be added to the
 // dependencies returned by RuleBuilder.Inputs.
-func (c *RuleBuilderCommand) Inputs(paths []string) *RuleBuilderCommand {
+func (c *RuleBuilderCommand) Inputs(paths Paths) *RuleBuilderCommand {
 	for _, path := range paths {
 		c.Input(path)
 	}
@@ -352,28 +331,28 @@
 
 // Implicit adds the specified input path to the dependencies returned by RuleBuilder.Inputs without modifying the
 // command line.
-func (c *RuleBuilderCommand) Implicit(path string) *RuleBuilderCommand {
+func (c *RuleBuilderCommand) Implicit(path Path) *RuleBuilderCommand {
 	c.inputs = append(c.inputs, path)
 	return c
 }
 
 // Implicits adds the specified input paths to the dependencies returned by RuleBuilder.Inputs without modifying the
 // command line.
-func (c *RuleBuilderCommand) Implicits(paths []string) *RuleBuilderCommand {
+func (c *RuleBuilderCommand) Implicits(paths Paths) *RuleBuilderCommand {
 	c.inputs = append(c.inputs, paths...)
 	return c
 }
 
 // Output adds the specified output path to the command line.  The path will also be added to the outputs returned by
 // RuleBuilder.Outputs.
-func (c *RuleBuilderCommand) Output(path string) *RuleBuilderCommand {
+func (c *RuleBuilderCommand) Output(path WritablePath) *RuleBuilderCommand {
 	c.outputs = append(c.outputs, path)
-	return c.Text(path)
+	return c.Text(path.String())
 }
 
 // Outputs adds the specified output paths to the command line, separated by spaces.  The paths will also be added to
 // the outputs returned by RuleBuilder.Outputs.
-func (c *RuleBuilderCommand) Outputs(paths []string) *RuleBuilderCommand {
+func (c *RuleBuilderCommand) Outputs(paths WritablePaths) *RuleBuilderCommand {
 	for _, path := range paths {
 		c.Output(path)
 	}
@@ -382,37 +361,37 @@
 
 // ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying
 // the command line.
-func (c *RuleBuilderCommand) ImplicitOutput(path string) *RuleBuilderCommand {
+func (c *RuleBuilderCommand) ImplicitOutput(path WritablePath) *RuleBuilderCommand {
 	c.outputs = append(c.outputs, path)
 	return c
 }
 
 // ImplicitOutputs adds the specified output paths to the dependencies returned by RuleBuilder.Outputs without modifying
 // the command line.
-func (c *RuleBuilderCommand) ImplicitOutputs(paths []string) *RuleBuilderCommand {
+func (c *RuleBuilderCommand) ImplicitOutputs(paths WritablePaths) *RuleBuilderCommand {
 	c.outputs = append(c.outputs, paths...)
 	return c
 }
 
 // FlagWithInput adds the specified flag and input path to the command line, with no separator between them.  The path
 // will also be added to the dependencies returned by RuleBuilder.Inputs.
-func (c *RuleBuilderCommand) FlagWithInput(flag, path string) *RuleBuilderCommand {
+func (c *RuleBuilderCommand) FlagWithInput(flag string, path Path) *RuleBuilderCommand {
 	c.inputs = append(c.inputs, path)
-	return c.Text(flag + path)
+	return c.Text(flag + path.String())
 }
 
 // FlagWithInputList adds the specified flag and input paths to the command line, with the inputs joined by sep
 // and no separator between the flag and inputs.  The input paths will also be added to the dependencies returned by
 // RuleBuilder.Inputs.
-func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths []string, sep string) *RuleBuilderCommand {
+func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths Paths, sep string) *RuleBuilderCommand {
 	c.inputs = append(c.inputs, paths...)
-	return c.FlagWithList(flag, paths, sep)
+	return c.FlagWithList(flag, paths.Strings(), sep)
 }
 
 // FlagForEachInput adds the specified flag joined with each input path to the command line.  The input paths will also
 // be added to the dependencies returned by RuleBuilder.Inputs.  The result is identical to calling FlagWithInput for
 // each input path.
-func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths []string) *RuleBuilderCommand {
+func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths Paths) *RuleBuilderCommand {
 	for _, path := range paths {
 		c.FlagWithInput(flag, path)
 	}
@@ -421,23 +400,12 @@
 
 // FlagWithOutput adds the specified flag and output path to the command line, with no separator between them.  The path
 // will also be added to the outputs returned by RuleBuilder.Outputs.
-func (c *RuleBuilderCommand) FlagWithOutput(flag, path string) *RuleBuilderCommand {
+func (c *RuleBuilderCommand) FlagWithOutput(flag string, path WritablePath) *RuleBuilderCommand {
 	c.outputs = append(c.outputs, path)
-	return c.Text(flag + path)
+	return c.Text(flag + path.String())
 }
 
 // String returns the command line.
 func (c *RuleBuilderCommand) String() string {
 	return string(c.buf)
 }
-
-type unknownRulePath struct {
-	path string
-}
-
-var _ Path = (*unknownRulePath)(nil)
-
-func (p *unknownRulePath) String() string { return p.path }
-func (p *unknownRulePath) Ext() string    { return filepath.Ext(p.path) }
-func (p *unknownRulePath) Base() string   { return filepath.Base(p.path) }
-func (p *unknownRulePath) Rel() string    { return p.path }
diff --git a/android/rule_builder_test.go b/android/rule_builder_test.go
index f947348..f738faf 100644
--- a/android/rule_builder_test.go
+++ b/android/rule_builder_test.go
@@ -24,10 +24,30 @@
 	"testing"
 )
 
+func pathContext() PathContext {
+	return PathContextForTesting(TestConfig("out", nil),
+		map[string][]byte{
+			"ld":      nil,
+			"a.o":     nil,
+			"b.o":     nil,
+			"cp":      nil,
+			"a":       nil,
+			"b":       nil,
+			"ls":      nil,
+			"turbine": nil,
+			"java":    nil,
+		})
+}
+
 func ExampleRuleBuilder() {
 	rule := NewRuleBuilder()
 
-	rule.Command().Tool("ld").Inputs([]string{"a.o", "b.o"}).FlagWithOutput("-o ", "linked")
+	ctx := pathContext()
+
+	rule.Command().
+		Tool(PathForSource(ctx, "ld")).
+		Inputs(PathsForTesting("a.o", "b.o")).
+		FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
 	rule.Command().Text("echo success")
 
 	// To add the command to the build graph:
@@ -39,18 +59,26 @@
 	fmt.Printf("outputs: %q\n", rule.Outputs())
 
 	// Output:
-	// commands: "ld a.o b.o -o linked && echo success"
+	// commands: "ld a.o b.o -o out/linked && echo success"
 	// tools: ["ld"]
 	// inputs: ["a.o" "b.o"]
-	// outputs: ["linked"]
+	// outputs: ["out/linked"]
 }
 
 func ExampleRuleBuilder_Temporary() {
 	rule := NewRuleBuilder()
 
-	rule.Command().Tool("cp").Input("a").Output("b")
-	rule.Command().Tool("cp").Input("b").Output("c")
-	rule.Temporary("b")
+	ctx := pathContext()
+
+	rule.Command().
+		Tool(PathForSource(ctx, "cp")).
+		Input(PathForSource(ctx, "a")).
+		Output(PathForOutput(ctx, "b"))
+	rule.Command().
+		Tool(PathForSource(ctx, "cp")).
+		Input(PathForOutput(ctx, "b")).
+		Output(PathForOutput(ctx, "c"))
+	rule.Temporary(PathForOutput(ctx, "b"))
 
 	fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
 	fmt.Printf("tools: %q\n", rule.Tools())
@@ -58,18 +86,26 @@
 	fmt.Printf("outputs: %q\n", rule.Outputs())
 
 	// Output:
-	// commands: "cp a b && cp b c"
+	// commands: "cp a out/b && cp out/b out/c"
 	// tools: ["cp"]
 	// inputs: ["a"]
-	// outputs: ["c"]
+	// outputs: ["out/c"]
 }
 
 func ExampleRuleBuilder_DeleteTemporaryFiles() {
 	rule := NewRuleBuilder()
 
-	rule.Command().Tool("cp").Input("a").Output("b")
-	rule.Command().Tool("cp").Input("b").Output("c")
-	rule.Temporary("b")
+	ctx := pathContext()
+
+	rule.Command().
+		Tool(PathForSource(ctx, "cp")).
+		Input(PathForSource(ctx, "a")).
+		Output(PathForOutput(ctx, "b"))
+	rule.Command().
+		Tool(PathForSource(ctx, "cp")).
+		Input(PathForOutput(ctx, "b")).
+		Output(PathForOutput(ctx, "c"))
+	rule.Temporary(PathForOutput(ctx, "b"))
 	rule.DeleteTemporaryFiles()
 
 	fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
@@ -78,93 +114,112 @@
 	fmt.Printf("outputs: %q\n", rule.Outputs())
 
 	// Output:
-	// commands: "cp a b && cp b c && rm -f b"
+	// commands: "cp a out/b && cp out/b out/c && rm -f out/b"
 	// tools: ["cp"]
 	// inputs: ["a"]
-	// outputs: ["c"]
+	// outputs: ["out/c"]
 }
 
 func ExampleRuleBuilder_Installs() {
 	rule := NewRuleBuilder()
 
-	rule.Command().Tool("ld").Inputs([]string{"a.o", "b.o"}).FlagWithOutput("-o ", "linked")
-	rule.Install("linked", "/bin/linked")
-	rule.Install("linked", "/sbin/linked")
+	ctx := pathContext()
+
+	out := PathForOutput(ctx, "linked")
+
+	rule.Command().
+		Tool(PathForSource(ctx, "ld")).
+		Inputs(PathsForTesting("a.o", "b.o")).
+		FlagWithOutput("-o ", out)
+	rule.Install(out, "/bin/linked")
+	rule.Install(out, "/sbin/linked")
 
 	fmt.Printf("rule.Installs().String() = %q\n", rule.Installs().String())
 
 	// Output:
-	// rule.Installs().String() = "linked:/bin/linked linked:/sbin/linked"
+	// rule.Installs().String() = "out/linked:/bin/linked out/linked:/sbin/linked"
 }
 
 func ExampleRuleBuilderCommand() {
 	rule := NewRuleBuilder()
 
+	ctx := pathContext()
+
 	// chained
-	rule.Command().Tool("ld").Inputs([]string{"a.o", "b.o"}).FlagWithOutput("-o ", "linked")
+	rule.Command().
+		Tool(PathForSource(ctx, "ld")).
+		Inputs(PathsForTesting("a.o", "b.o")).
+		FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
 
 	// unchained
 	cmd := rule.Command()
-	cmd.Tool("ld")
-	cmd.Inputs([]string{"a.o", "b.o"})
-	cmd.FlagWithOutput("-o ", "linked")
+	cmd.Tool(PathForSource(ctx, "ld"))
+	cmd.Inputs(PathsForTesting("a.o", "b.o"))
+	cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
 
 	// mixed:
-	cmd = rule.Command().Tool("ld")
-	cmd.Inputs([]string{"a.o", "b.o"})
-	cmd.FlagWithOutput("-o ", "linked")
+	cmd = rule.Command().Tool(PathForSource(ctx, "ld"))
+	cmd.Inputs(PathsForTesting("a.o", "b.o"))
+	cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
 }
 
 func ExampleRuleBuilderCommand_Flag() {
+	ctx := pathContext()
 	fmt.Println(NewRuleBuilder().Command().
-		Tool("ls").Flag("-l"))
+		Tool(PathForSource(ctx, "ls")).Flag("-l"))
 	// Output:
 	// ls -l
 }
 
 func ExampleRuleBuilderCommand_FlagWithArg() {
+	ctx := pathContext()
 	fmt.Println(NewRuleBuilder().Command().
-		Tool("ls").
+		Tool(PathForSource(ctx, "ls")).
 		FlagWithArg("--sort=", "time"))
 	// Output:
 	// ls --sort=time
 }
 
 func ExampleRuleBuilderCommand_FlagForEachArg() {
+	ctx := pathContext()
 	fmt.Println(NewRuleBuilder().Command().
-		Tool("ls").
+		Tool(PathForSource(ctx, "ls")).
 		FlagForEachArg("--sort=", []string{"time", "size"}))
 	// Output:
 	// ls --sort=time --sort=size
 }
 
 func ExampleRuleBuilderCommand_FlagForEachInput() {
+	ctx := pathContext()
 	fmt.Println(NewRuleBuilder().Command().
-		Tool("turbine").
-		FlagForEachInput("--classpath ", []string{"a.jar", "b.jar"}))
+		Tool(PathForSource(ctx, "turbine")).
+		FlagForEachInput("--classpath ", PathsForTesting("a.jar", "b.jar")))
 	// Output:
 	// turbine --classpath a.jar --classpath b.jar
 }
 
 func ExampleRuleBuilderCommand_FlagWithInputList() {
+	ctx := pathContext()
 	fmt.Println(NewRuleBuilder().Command().
-		Tool("java").
-		FlagWithInputList("-classpath=", []string{"a.jar", "b.jar"}, ":"))
+		Tool(PathForSource(ctx, "java")).
+		FlagWithInputList("-classpath=", PathsForTesting("a.jar", "b.jar"), ":"))
 	// Output:
 	// java -classpath=a.jar:b.jar
 }
 
 func ExampleRuleBuilderCommand_FlagWithInput() {
+	ctx := pathContext()
 	fmt.Println(NewRuleBuilder().Command().
-		Tool("java").
-		FlagWithInput("-classpath=", "a"))
+		Tool(PathForSource(ctx, "java")).
+		FlagWithInput("-classpath=", PathForSource(ctx, "a")))
 	// Output:
 	// java -classpath=a
 }
 
 func ExampleRuleBuilderCommand_FlagWithList() {
+	ctx := pathContext()
 	fmt.Println(NewRuleBuilder().Command().
-		Tool("ls").
+		Tool(PathForSource(ctx, "ls")).
 		FlagWithList("--sort=", []string{"time", "size"}, ","))
 	// Output:
 	// ls --sort=time,size
@@ -173,23 +228,35 @@
 func TestRuleBuilder(t *testing.T) {
 	rule := NewRuleBuilder()
 
+	fs := map[string][]byte{
+		"input":    nil,
+		"Implicit": nil,
+		"Input":    nil,
+		"Tool":     nil,
+		"input2":   nil,
+		"tool2":    nil,
+		"input3":   nil,
+	}
+
+	ctx := PathContextForTesting(TestConfig("out", nil), fs)
+
 	cmd := rule.Command().
 		Flag("Flag").
 		FlagWithArg("FlagWithArg=", "arg").
-		FlagWithInput("FlagWithInput=", "input").
-		FlagWithOutput("FlagWithOutput=", "output").
-		Implicit("Implicit").
-		ImplicitOutput("ImplicitOutput").
-		Input("Input").
-		Output("Output").
+		FlagWithInput("FlagWithInput=", PathForSource(ctx, "input")).
+		FlagWithOutput("FlagWithOutput=", PathForOutput(ctx, "output")).
+		Implicit(PathForSource(ctx, "Implicit")).
+		ImplicitOutput(PathForOutput(ctx, "ImplicitOutput")).
+		Input(PathForSource(ctx, "Input")).
+		Output(PathForOutput(ctx, "Output")).
 		Text("Text").
-		Tool("Tool")
+		Tool(PathForSource(ctx, "Tool"))
 
 	rule.Command().
 		Text("command2").
-		Input("input2").
-		Output("output2").
-		Tool("tool2")
+		Input(PathForSource(ctx, "input2")).
+		Output(PathForOutput(ctx, "output2")).
+		Tool(PathForSource(ctx, "tool2"))
 
 	// Test updates to the first command after the second command has been started
 	cmd.Text("after command2")
@@ -199,18 +266,18 @@
 	// Test a command that uses the output of a previous command as an input
 	rule.Command().
 		Text("command3").
-		Input("input3").
-		Input("output2").
-		Output("output3")
+		Input(PathForSource(ctx, "input3")).
+		Input(PathForOutput(ctx, "output2")).
+		Output(PathForOutput(ctx, "output3"))
 
 	wantCommands := []string{
-		"Flag FlagWithArg=arg FlagWithInput=input FlagWithOutput=output Input Output Text Tool after command2 old cmd",
-		"command2 input2 output2 tool2",
-		"command3 input3 output2 output3",
+		"Flag FlagWithArg=arg FlagWithInput=input FlagWithOutput=out/output Input out/Output Text Tool after command2 old cmd",
+		"command2 input2 out/output2 tool2",
+		"command3 input3 out/output2 out/output3",
 	}
-	wantInputs := []string{"Implicit", "Input", "input", "input2", "input3"}
-	wantOutputs := []string{"ImplicitOutput", "Output", "output", "output2", "output3"}
-	wantTools := []string{"Tool", "tool2"}
+	wantInputs := PathsForSource(ctx, []string{"Implicit", "Input", "input", "input2", "input3"})
+	wantOutputs := PathsForOutput(ctx, []string{"ImplicitOutput", "Output", "output", "output2", "output3"})
+	wantTools := PathsForSource(ctx, []string{"Tool", "tool2"})
 
 	if !reflect.DeepEqual(rule.Commands(), wantCommands) {
 		t.Errorf("\nwant rule.Commands() = %#v\n                   got %#v", wantCommands, rule.Commands())
@@ -262,7 +329,7 @@
 func testRuleBuilder_Build(ctx BuilderContext, in Path, out WritablePath) {
 	rule := NewRuleBuilder()
 
-	rule.Command().Tool("cp").Input(in.String()).Output(out.String())
+	rule.Command().Tool(PathForSource(ctx, "cp")).Input(in).Output(out)
 
 	rule.Build(pctx, ctx, "rule", "desc")
 }
diff --git a/android/variable.go b/android/variable.go
index dc880b8..5ee888f 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -200,12 +200,7 @@
 	UncompressPrivAppDex             *bool    `json:",omitempty"`
 	ModulesLoadedByPrivilegedModules []string `json:",omitempty"`
 
-	BootJars       []string `json:",omitempty"`
-	PreoptBootJars []string `json:",omitempty"`
-
-	DisableDexPreopt        *bool    `json:",omitempty"`
-	DisableDexPreoptModules []string `json:",omitempty"`
-	DexPreoptProfileDir     *string  `json:",omitempty"`
+	BootJars []string `json:",omitempty"`
 
 	IntegerOverflowExcludePaths []string `json:",omitempty"`
 
diff --git a/android/writedocs.go b/android/writedocs.go
index 9737030..7262ad8 100644
--- a/android/writedocs.go
+++ b/android/writedocs.go
@@ -53,15 +53,19 @@
 	primaryBuilder := primaryBuilderPath(ctx)
 	soongDocs := ctx.Rule(pctx, "soongDocs",
 		blueprint.RuleParams{
-			Command: fmt.Sprintf("%s --soong_docs %s %s",
+			Command: fmt.Sprintf("rm -f ${outDir}/* && %s --soong_docs %s %s",
 				primaryBuilder.String(), docsFile.String(), strings.Join(os.Args[1:], " ")),
 			CommandDeps: []string{primaryBuilder.String()},
 			Description: fmt.Sprintf("%s docs $out", primaryBuilder.Base()),
-		})
+		},
+		"outDir")
 
 	ctx.Build(pctx, BuildParams{
 		Rule:   soongDocs,
 		Output: docsFile,
+		Args: map[string]string{
+			"outDir": PathForOutput(ctx, "docs").String(),
+		},
 	})
 
 	// Add a phony target for building the documentation
diff --git a/androidmk/cmd/androidmk/android.go b/androidmk/cmd/androidmk/android.go
index a5dfcd9..c66acdc 100644
--- a/androidmk/cmd/androidmk/android.go
+++ b/androidmk/cmd/androidmk/android.go
@@ -42,21 +42,26 @@
 
 var rewriteProperties = map[string](func(variableAssignmentContext) error){
 	// custom functions
-	"LOCAL_32_BIT_ONLY":           local32BitOnly,
-	"LOCAL_AIDL_INCLUDES":         localAidlIncludes,
-	"LOCAL_C_INCLUDES":            localIncludeDirs,
-	"LOCAL_EXPORT_C_INCLUDE_DIRS": exportIncludeDirs,
-	"LOCAL_LDFLAGS":               ldflags,
-	"LOCAL_MODULE_CLASS":          prebuiltClass,
-	"LOCAL_MODULE_STEM":           stem,
-	"LOCAL_MODULE_HOST_OS":        hostOs,
-	"LOCAL_SANITIZE":              sanitize(""),
-	"LOCAL_SANITIZE_DIAG":         sanitize("diag."),
-	"LOCAL_STRIP_MODULE":          strip(),
-	"LOCAL_CFLAGS":                cflags,
-	"LOCAL_UNINSTALLABLE_MODULE":  invert("installable"),
-	"LOCAL_PROGUARD_ENABLED":      proguardEnabled,
-	"LOCAL_MODULE_PATH":           prebuiltModulePath,
+	"LOCAL_32_BIT_ONLY":             local32BitOnly,
+	"LOCAL_ADDITIONAL_CERTIFICATES": localizePathList("additional_certificates"),
+	"LOCAL_AIDL_INCLUDES":           localAidlIncludes,
+	"LOCAL_ASSET_DIR":               localizePathList("asset_dirs"),
+	"LOCAL_C_INCLUDES":              localIncludeDirs,
+	"LOCAL_CERTIFICATE":             localizePath("certificate"),
+	"LOCAL_EXPORT_C_INCLUDE_DIRS":   exportIncludeDirs,
+	"LOCAL_JARJAR_RULES":            localizePath("jarjar_rules"),
+	"LOCAL_LDFLAGS":                 ldflags,
+	"LOCAL_MODULE_CLASS":            prebuiltClass,
+	"LOCAL_MODULE_STEM":             stem,
+	"LOCAL_MODULE_HOST_OS":          hostOs,
+	"LOCAL_RESOURCE_DIR":            localizePathList("resource_dirs"),
+	"LOCAL_SANITIZE":                sanitize(""),
+	"LOCAL_SANITIZE_DIAG":           sanitize("diag."),
+	"LOCAL_STRIP_MODULE":            strip(),
+	"LOCAL_CFLAGS":                  cflags,
+	"LOCAL_UNINSTALLABLE_MODULE":    invert("installable"),
+	"LOCAL_PROGUARD_ENABLED":        proguardEnabled,
+	"LOCAL_MODULE_PATH":             prebuiltModulePath,
 
 	// composite functions
 	"LOCAL_MODULE_TAGS": includeVariableIf(bpVariable{"tags", bpparser.ListType}, not(valueDumpEquals("optional"))),
@@ -72,6 +77,7 @@
 	"LOCAL_JAR_EXCLUDE_FILES":       skip, // Soong never excludes files from jars
 
 	"LOCAL_ANNOTATION_PROCESSOR_CLASSES": skip, // Soong gets the processor classes from the plugin
+	"LOCAL_CTS_TEST_PACKAGE":             skip, // Obsolete
 }
 
 // adds a group of properties all having the same type
@@ -92,8 +98,6 @@
 			"LOCAL_MIN_SDK_VERSION":         "min_sdk_version",
 			"LOCAL_NDK_STL_VARIANT":         "stl",
 			"LOCAL_JAR_MANIFEST":            "manifest",
-			"LOCAL_JARJAR_RULES":            "jarjar_rules",
-			"LOCAL_CERTIFICATE":             "certificate",
 			"LOCAL_PACKAGE_NAME":            "name",
 			"LOCAL_MODULE_RELATIVE_PATH":    "relative_install_path",
 			"LOCAL_PROTOC_OPTIMIZE_TYPE":    "proto.type",
@@ -139,7 +143,6 @@
 			"LOCAL_RENDERSCRIPT_FLAGS":    "renderscript.flags",
 
 			"LOCAL_JAVA_RESOURCE_DIRS":    "java_resource_dirs",
-			"LOCAL_RESOURCE_DIR":          "resource_dirs",
 			"LOCAL_JAVACFLAGS":            "javacflags",
 			"LOCAL_ERROR_PRONE_FLAGS":     "errorprone.javacflags",
 			"LOCAL_DX_FLAGS":              "dxflags",
@@ -160,7 +163,6 @@
 			// java_library_static to android_library.
 			"LOCAL_SHARED_ANDROID_LIBRARIES": "android_libs",
 			"LOCAL_STATIC_ANDROID_LIBRARIES": "android_static_libs",
-			"LOCAL_ADDITIONAL_CERTIFICATES":  "additional_certificates",
 
 			// Jacoco filters:
 			"LOCAL_JACK_COVERAGE_INCLUDE_FILTER": "jacoco.include_filter",
@@ -388,6 +390,64 @@
 	return splitAndAssign(ctx, classifyLocalOrGlobalPath, map[string]string{"global": "aidl.include_dirs", "local": "aidl.local_include_dirs"})
 }
 
+func localizePathList(attribute string) func(ctx variableAssignmentContext) error {
+	return func(ctx variableAssignmentContext) error {
+		paths, err := localizePaths(ctx)
+		if err == nil {
+			err = setVariable(ctx.file, ctx.append, ctx.prefix, attribute, paths, true)
+		}
+		return err
+	}
+}
+
+func localizePath(attribute string) func(ctx variableAssignmentContext) error {
+	return func(ctx variableAssignmentContext) error {
+		paths, err := localizePaths(ctx)
+		if err == nil {
+			pathList, ok := paths.(*bpparser.List)
+			if !ok {
+				panic("Expected list")
+			}
+			switch len(pathList.Values) {
+			case 0:
+				err = setVariable(ctx.file, ctx.append, ctx.prefix, attribute, &bpparser.List{}, true)
+			case 1:
+				err = setVariable(ctx.file, ctx.append, ctx.prefix, attribute, pathList.Values[0], true)
+			default:
+				err = fmt.Errorf("Expected single value for %s", attribute)
+			}
+		}
+		return err
+	}
+}
+
+// Convert the "full" paths (that is, from the top of the source tree) to the relative one
+// (from the directory containing the blueprint file) and set given attribute to it.
+// This is needed for some of makefile variables (e.g., LOCAL_RESOURCE_DIR).
+// At the moment only the paths of the `$(LOCAL_PATH)/foo/bar` format can be converted
+// (to `foo/bar` in this case) as we cannot convert a literal path without
+// knowing makefiles's location in the source tree. We just issue a warning in the latter case.
+func localizePaths(ctx variableAssignmentContext) (bpparser.Expression, error) {
+	bpvalue, err := makeVariableToBlueprint(ctx.file, ctx.mkvalue, bpparser.ListType)
+	var result bpparser.Expression
+	if err != nil {
+		return result, err
+	}
+	classifiedPaths, err := splitBpList(bpvalue, classifyLocalOrGlobalPath)
+	if err != nil {
+		return result, err
+	}
+	for pathClass, path := range classifiedPaths {
+		switch pathClass {
+		case "local":
+			result = path
+		default:
+			err = fmt.Errorf("Only $(LOCAL_PATH)/.. values are allowed")
+		}
+	}
+	return result, err
+}
+
 func stem(ctx variableAssignmentContext) error {
 	val, err := makeVariableToBlueprint(ctx.file, ctx.mkvalue, bpparser.StringType)
 	if err != nil {
diff --git a/androidmk/cmd/androidmk/androidmk_test.go b/androidmk/cmd/androidmk/androidmk_test.go
index 618dd42..bc249d0 100644
--- a/androidmk/cmd/androidmk/androidmk_test.go
+++ b/androidmk/cmd/androidmk/androidmk_test.go
@@ -112,6 +112,32 @@
 }`,
 	},
 	{
+		desc: "Convert to local path",
+		in: `
+include $(CLEAR_VARS)
+LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res $(LOCAL_PATH)/res2
+LOCAL_ASSET_DIR := $(LOCAL_PATH)/asset
+LOCAL_JARJAR_RULES := $(LOCAL_PATH)/jarjar-rules.txt
+LOCAL_CERTIFICATE := $(LOCAL_PATH)/cert
+LOCAL_ADDITIONAL_CERTIFICATES := $(LOCAL_PATH)/cert1 $(LOCAL_PATH)/cert2
+include $(BUILD_PACKAGE)
+	`,
+		expected: `
+android_app {
+	resource_dirs: [
+		"res",
+		"res2",
+	],
+	asset_dirs: ["asset"],
+	jarjar_rules: "jarjar-rules.txt",
+	certificate: "cert",
+	additional_certificates: [
+		"cert1",
+		"cert2",
+	],
+}`,
+	},
+	{
 		desc: "LOCAL_MODULE_STEM",
 		in: `
 include $(CLEAR_VARS)
@@ -642,7 +668,7 @@
 		in: `
 			include $(CLEAR_VARS)
 			LOCAL_SRC_FILES := test.java
-			LOCAL_RESOURCE_DIR := res
+			LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
 			LOCAL_JACK_COVERAGE_INCLUDE_FILTER := foo.*
 			include $(BUILD_STATIC_JAVA_LIBRARY)
 
@@ -758,6 +784,7 @@
 include $(CLEAR_VARS)
 LOCAL_PACKAGE_NAME := FooTest
 LOCAL_COMPATIBILITY_SUITE := cts
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
 include $(BUILD_CTS_SUPPORT_PACKAGE)
 `,
 		expected: `
@@ -765,6 +792,7 @@
     name: "FooTest",
     defaults: ["cts_support_defaults"],
     test_suites: ["cts"],
+
 }
 `,
 	},
@@ -774,6 +802,7 @@
 include $(CLEAR_VARS)
 LOCAL_PACKAGE_NAME := FooTest
 LOCAL_COMPATIBILITY_SUITE := cts
+LOCAL_CTS_TEST_PACKAGE := foo.bar
 include $(BUILD_CTS_PACKAGE)
 `,
 		expected: `
@@ -781,6 +810,7 @@
     name: "FooTest",
     defaults: ["cts_defaults"],
     test_suites: ["cts"],
+
 }
 `,
 	},
diff --git a/apex/apex.go b/apex/apex.go
index 408415e..9ab5187 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -389,6 +389,9 @@
 	// list of files to be included in this apex
 	filesInfo []apexFile
 
+	// list of module names that this APEX is depending on
+	externalDeps []string
+
 	flattened bool
 
 	testApex bool
@@ -730,7 +733,18 @@
 			// indirect dependencies
 			if am, ok := child.(android.ApexModule); ok && am.CanHaveApexVariants() && am.IsInstallableToApex() {
 				if cc, ok := child.(*cc.Module); ok {
-					if cc.IsStubs() || cc.HasStubsVariants() {
+					if !a.Host() && (cc.IsStubs() || cc.HasStubsVariants()) {
+						// If the dependency is a stubs lib, don't include it in this APEX,
+						// but make sure that the lib is installed on the device.
+						// In case no APEX is having the lib, the lib is installed to the system
+						// partition.
+						//
+						// Always include if we are a host-apex however since those won't have any
+						// system libraries.
+						if !android.DirectlyInAnyApex(ctx, cc.Name()) && !android.InList(cc.Name(), a.externalDeps) {
+							a.externalDeps = append(a.externalDeps, cc.Name())
+						}
+						// Don't track further
 						return false
 					}
 					depName := ctx.OtherModuleName(child)
@@ -992,7 +1006,10 @@
 		if ctx.Config().FlattenApex() {
 			for _, fi := range a.filesInfo {
 				dir := filepath.Join("apex", ctx.ModuleName(), fi.installDir)
-				ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
+				target := ctx.InstallFile(android.PathForModuleInstall(ctx, dir), fi.builtFile.Base(), fi.builtFile)
+				for _, sym := range fi.symlinks {
+					ctx.InstallSymlink(android.PathForModuleInstall(ctx, dir), sym, target)
+				}
 			}
 		}
 	}
@@ -1031,6 +1048,9 @@
 			// /system/apex/<name>/{lib|framework|...}
 			fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(OUT_DIR)",
 				a.installDir.RelPathString(), name, fi.installDir))
+			if len(fi.symlinks) > 0 {
+				fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
+			}
 		} else {
 			// /apex/<name>/{lib|framework|...}
 			fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join("$(PRODUCT_OUT)",
@@ -1126,6 +1146,9 @@
 				if len(moduleNames) > 0 {
 					fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
 				}
+				if len(a.externalDeps) > 0 {
+					fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.externalDeps, " "))
+				}
 				fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
 
 				if apexType == imageApex {
diff --git a/bpfix/bpfix/bpfix.go b/bpfix/bpfix/bpfix.go
index 11f1877..ba6435e 100644
--- a/bpfix/bpfix/bpfix.go
+++ b/bpfix/bpfix/bpfix.go
@@ -98,6 +98,10 @@
 		name: "removeTags",
 		fix:  runPatchListMod(removeTags),
 	},
+	{
+		name: "rewriteAndroidTest",
+		fix:  rewriteAndroidTest,
+	},
 }
 
 func NewFixRequest() FixRequest {
@@ -561,6 +565,30 @@
 	return nil
 }
 
+func rewriteAndroidTest(f *Fixer) error {
+	for _, def := range f.tree.Defs {
+		mod, ok := def.(*parser.Module)
+		if !(ok && mod.Type == "android_test") {
+			continue
+		}
+		// The rewriter converts LOCAL_MODULE_PATH attribute into a struct attribute
+		// 'local_module_path'. For the android_test module, it should be  $(TARGET_OUT_DATA_APPS),
+		// that is, `local_module_path: { var: "TARGET_OUT_DATA_APPS"}`
+		const local_module_path = "local_module_path"
+		if prop_local_module_path, ok := mod.GetProperty(local_module_path); ok {
+			removeProperty(mod, local_module_path)
+			prefixVariableName := getStringProperty(prop_local_module_path, "var")
+			path := getStringProperty(prop_local_module_path, "fixed")
+			if prefixVariableName == "TARGET_OUT_DATA_APPS" && path == "" {
+				continue
+			}
+			return indicateAttributeError(mod, "filename",
+				"Only LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS) is allowed for the android_test")
+		}
+	}
+	return nil
+}
+
 func runPatchListMod(modFunc func(mod *parser.Module, buf []byte, patchlist *parser.PatchList) error) func(*Fixer) error {
 	return func(f *Fixer) error {
 		// Make sure all the offsets are accurate
diff --git a/bpfix/bpfix/bpfix_test.go b/bpfix/bpfix/bpfix_test.go
index 1394223..459cd36 100644
--- a/bpfix/bpfix/bpfix_test.go
+++ b/bpfix/bpfix/bpfix_test.go
@@ -751,3 +751,36 @@
 		})
 	}
 }
+
+func TestRewriteAndroidTest(t *testing.T) {
+	tests := []struct {
+		name string
+		in   string
+		out  string
+	}{
+		{
+			name: "android_test valid module path",
+			in: `
+				android_test {
+					name: "foo",
+					local_module_path: {
+						var: "TARGET_OUT_DATA_APPS",
+					},
+				}
+			`,
+			out: `
+				android_test {
+					name: "foo",
+
+				}
+			`,
+		},
+	}
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			runPass(t, test.in, test.out, func(fixer *Fixer) error {
+				return rewriteAndroidTest(fixer)
+			})
+		})
+	}
+}
diff --git a/cc/cc.go b/cc/cc.go
index 7b19e98..ddc47ea 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -1225,12 +1225,18 @@
 		return
 	}
 
-	actx.AddVariationDependencies([]blueprint.Variation{
-		{Mutator: "link", Variation: "static"},
-	}, wholeStaticDepTag, deps.WholeStaticLibs...)
-
 	syspropImplLibraries := syspropImplLibraries(actx.Config())
 
+	for _, lib := range deps.WholeStaticLibs {
+		depTag := wholeStaticDepTag
+		if impl, ok := syspropImplLibraries[lib]; ok {
+			lib = impl
+		}
+		actx.AddVariationDependencies([]blueprint.Variation{
+			{Mutator: "link", Variation: "static"},
+		}, depTag, lib)
+	}
+
 	for _, lib := range deps.StaticLibs {
 		depTag := staticDepTag
 		if inList(lib, deps.ReexportStaticLibHeaders) {
diff --git a/cmd/soong_build/writedocs.go b/cmd/soong_build/writedocs.go
index 74c854a..e86ef82 100644
--- a/cmd/soong_build/writedocs.go
+++ b/cmd/soong_build/writedocs.go
@@ -19,6 +19,7 @@
 	"bytes"
 	"html/template"
 	"io/ioutil"
+	"path/filepath"
 	"reflect"
 	"sort"
 
@@ -26,6 +27,11 @@
 	"github.com/google/blueprint/bootstrap/bpdoc"
 )
 
+type perPackageTemplateData struct {
+	Name    string
+	Modules []moduleTypeTemplateData
+}
+
 type moduleTypeTemplateData struct {
 	Name       string
 	Synopsis   string
@@ -44,22 +50,7 @@
 }
 
 // For each module type, extract its documentation and convert it to the template data.
-func moduleTypeDocsToTemplates(ctx *android.Context) ([]moduleTypeTemplateData, error) {
-	moduleTypeFactories := android.ModuleTypeFactories()
-	bpModuleTypeFactories := make(map[string]reflect.Value)
-	for moduleType, factory := range moduleTypeFactories {
-		bpModuleTypeFactories[moduleType] = reflect.ValueOf(factory)
-	}
-
-	packages, err := bootstrap.ModuleTypeDocs(ctx.Context, bpModuleTypeFactories)
-	if err != nil {
-		return []moduleTypeTemplateData{}, err
-	}
-	var moduleTypeList []*bpdoc.ModuleType
-	for _, pkg := range packages {
-		moduleTypeList = append(moduleTypeList, pkg.ModuleTypes...)
-	}
-
+func moduleTypeDocsToTemplates(moduleTypeList []*bpdoc.ModuleType) []moduleTypeTemplateData {
 	result := make([]moduleTypeTemplateData, 0)
 
 	// Combine properties from all PropertyStruct's and reorder them -- first the ones
@@ -101,39 +92,113 @@
 		result = append(result, item)
 	}
 	sort.Slice(result, func(i, j int) bool { return result[i].Name < result[j].Name })
-	return result, err
+	return result
 }
 
 func writeDocs(ctx *android.Context, filename string) error {
-	buf := &bytes.Buffer{}
+	moduleTypeFactories := android.ModuleTypeFactories()
+	bpModuleTypeFactories := make(map[string]reflect.Value)
+	for moduleType, factory := range moduleTypeFactories {
+		bpModuleTypeFactories[moduleType] = reflect.ValueOf(factory)
+	}
 
-	// We need a module name getter/setter function because I couldn't
-	// find a way to keep it in a variable defined within the template.
-	currentModuleName := ""
-	data, err := moduleTypeDocsToTemplates(ctx)
+	packages, err := bootstrap.ModuleTypeDocs(ctx.Context, bpModuleTypeFactories)
 	if err != nil {
 		return err
 	}
-	tmpl, err := template.New("file").Funcs(map[string]interface{}{
-		"setModule": func(moduleName string) string {
-			currentModuleName = moduleName
-			return ""
-		},
-		"getModule": func() string {
-			return currentModuleName
-		},
-	}).Parse(fileTemplate)
-	if err == nil {
-		err = tmpl.Execute(buf, data)
-	}
+
+	// Produce the top-level, package list page first.
+	tmpl := template.Must(template.Must(template.New("file").Parse(packageListTemplate)).Parse(copyBaseUrl))
+	buf := &bytes.Buffer{}
+	err = tmpl.Execute(buf, packages)
 	if err == nil {
 		err = ioutil.WriteFile(filename, buf.Bytes(), 0666)
 	}
+
+	// Now, produce per-package module lists with detailed information.
+	for _, pkg := range packages {
+		// We need a module name getter/setter function because I couldn't
+		// find a way to keep it in a variable defined within the template.
+		currentModuleName := ""
+		tmpl := template.Must(
+			template.Must(template.New("file").Funcs(map[string]interface{}{
+				"setModule": func(moduleName string) string {
+					currentModuleName = moduleName
+					return ""
+				},
+				"getModule": func() string {
+					return currentModuleName
+				},
+			}).Parse(perPackageTemplate)).Parse(copyBaseUrl))
+		buf := &bytes.Buffer{}
+		modules := moduleTypeDocsToTemplates(pkg.ModuleTypes)
+		data := perPackageTemplateData{Name: pkg.Name, Modules: modules}
+		err = tmpl.Execute(buf, data)
+		if err != nil {
+			return err
+		}
+		pkgFileName := filepath.Join(filepath.Dir(filename), pkg.Name+".html")
+		err = ioutil.WriteFile(pkgFileName, buf.Bytes(), 0666)
+		if err != nil {
+			return err
+		}
+	}
 	return err
 }
 
+// TODO(jungjw): Consider ordering by name.
 const (
-	fileTemplate = `
+	packageListTemplate = `
+<html>
+<head>
+<title>Build Docs</title>
+<link rel="stylesheet" href="https://www.gstatic.com/devrel-devsite/vc67ef93e81a468795c57df87eca3f8427d65cbe85f09fbb51c82a12b89aa3d7e/androidsource/css/app.css">
+<style>
+#main {
+  padding: 48px;
+}
+
+table{
+  table-layout: fixed;
+}
+
+td {
+  word-wrap:break-word;
+}
+</style>
+{{template "copyBaseUrl"}}
+</head>
+<body>
+<div id="main">
+<H1>Soong Modules Reference</H1>
+The latest versions of Android use the Soong build system, which greatly simplifies build
+configuration over the previous Make-based system. This site contains the generated reference
+files for the Soong build system.
+
+<table class="module_types" summary="Table of Soong module types sorted by package">
+  <thead>
+    <tr>
+      <th style="width:20%">Package</th>
+      <th style="width:80%">Module types</th>
+    </tr>
+  </thead>
+  <tbody>
+    {{range $pkg := .}}
+      <tr>
+        <td>{{.Path}}</td>
+        <td>
+        {{range $i, $mod := .ModuleTypes}}{{if $i}}, {{end}}<a href="{{$pkg.Name}}.html#{{$mod.Name}}">{{$mod.Name}}</a>{{end}}
+        </td>
+      </tr>
+    {{end}}
+  </tbody>
+</table>
+</div>
+</body>
+</html>
+`
+
+	perPackageTemplate = `
 <html>
 <head>
 <title>Build Docs</title>
@@ -170,25 +235,18 @@
   color: white;
 }
 </style>
+{{template "copyBaseUrl"}}
 </head>
 <body>
 {{- /* Fixed sidebar with module types */ -}}
 <ul>
-<li><h3>Module Types:</h3></li>
-{{range $moduleType := .}}<li><a href="#{{$moduleType.Name}}">{{$moduleType.Name}}</a></li>
+<li><h3>{{.Name}} package</h3></li>
+{{range $moduleType := .Modules}}<li><a href="#{{$moduleType.Name}}">{{$moduleType.Name}}</a></li>
 {{end -}}
 </ul>
 {{/* Main panel with H1 section per module type */}}
 <div style="margin-left:30ch;padding:1px 16px;">
-<H1>Soong Modules Reference</H1>
-The latest versions of Android use the Soong build system, which greatly simplifies build
-configuration over the previous Make-based system. This site contains the generated reference
-files for the Soong build system.
-<p>
-See the <a href=https://source.android.com/setup/build/build-system>Android Build System</a>
-description for an overview of Soong and examples for its use.
-
-{{range $imodule, $moduleType := .}}
+{{range $moduleType := .Modules}}
 	{{setModule $moduleType.Name}}
 	<p>
   <h2 id="{{$moduleType.Name}}">{{$moduleType.Name}}</h2>
@@ -225,7 +283,6 @@
     {{- end}}
   {{- end -}}
 {{- end -}}
-
 </div>
 <script>
   accordions = document.getElementsByClassName('accordion');
@@ -245,4 +302,23 @@
 </script>
 </body>
 `
+
+	copyBaseUrl = `
+{{define "copyBaseUrl"}}
+<script type="text/javascript">
+window.addEventListener('message', (e) => {
+  if (e != null && e.data != null && e.data.type === "SET_BASE" && e.data.base != null) {
+    const existingBase = document.querySelector('base');
+    if (existingBase != null) {
+      existingBase.parentElement.removeChild(existingBase);
+    }
+
+    const base = document.createElement('base');
+    base.setAttribute('href', e.data.base);
+    document.head.appendChild(base);
+  }
+});
+</script>
+{{end}}
+`
 )
diff --git a/dexpreopt/config.go b/dexpreopt/config.go
index ee3cc8d..15008b9 100644
--- a/dexpreopt/config.go
+++ b/dexpreopt/config.go
@@ -17,6 +17,7 @@
 import (
 	"encoding/json"
 	"io/ioutil"
+	"strings"
 
 	"android/soong/android"
 )
@@ -25,14 +26,18 @@
 type GlobalConfig struct {
 	DefaultNoStripping bool // don't strip dex files by default
 
+	DisablePreopt        bool     // disable preopt for all modules
 	DisablePreoptModules []string // modules with preopt disabled by product-specific config
 
 	OnlyPreoptBootImageAndSystemServer bool // only preopt jars in the boot image or system server
 
+	GenerateApexImage bool // generate an extra boot image only containing jars from the runtime apex
+
 	HasSystemOther        bool     // store odex files that match PatternsOnSystemOther on the system_other partition
 	PatternsOnSystemOther []string // patterns (using '%' to denote a prefix match) to put odex on the system_other partition
 
-	DisableGenerateProfile bool // don't generate profiles
+	DisableGenerateProfile bool   // don't generate profiles
+	ProfileDir             string // directory to find profiles in
 
 	BootJars []string // modules for jars that form the boot class path
 
@@ -74,12 +79,13 @@
 	InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
 
 	// Only used for boot image
-	DirtyImageObjects string   // path to a dirty-image-objects file
-	PreloadedClasses  string   // path to a preloaded-classes file
-	BootImageProfiles []string // path to a boot-image-profile.txt file
-	BootFlags         string   // extra flags to pass to dex2oat for the boot image
-	Dex2oatImageXmx   string   // max heap size for dex2oat for the boot image
-	Dex2oatImageXms   string   // initial heap size for dex2oat for the boot image
+	DirtyImageObjects      android.OptionalPath // path to a dirty-image-objects file
+	PreloadedClasses       android.OptionalPath // path to a preloaded-classes file
+	BootImageProfiles      android.Paths        // path to a boot-image-profile.txt file
+	UseProfileForBootImage bool                 // whether a profile should be used to compile the boot image
+	BootFlags              string               // extra flags to pass to dex2oat for the boot image
+	Dex2oatImageXmx        string               // max heap size for dex2oat for the boot image
+	Dex2oatImageXms        string               // initial heap size for dex2oat for the boot image
 
 	Tools Tools // paths to tools possibly used by the generated commands
 }
@@ -87,38 +93,38 @@
 // Tools contains paths to tools possibly used by the generated commands.  If you add a new tool here you MUST add it
 // to the order-only dependency list in DEXPREOPT_GEN_DEPS.
 type Tools struct {
-	Profman  string
-	Dex2oat  string
-	Aapt     string
-	SoongZip string
-	Zip2zip  string
+	Profman  android.Path
+	Dex2oat  android.Path
+	Aapt     android.Path
+	SoongZip android.Path
+	Zip2zip  android.Path
 
-	VerifyUsesLibraries string
-	ConstructContext    string
+	VerifyUsesLibraries android.Path
+	ConstructContext    android.Path
 }
 
 type ModuleConfig struct {
 	Name            string
 	DexLocation     string // dex location on device
-	BuildPath       string
-	DexPath         string
+	BuildPath       android.OutputPath
+	DexPath         android.Path
 	UncompressedDex bool
 	HasApkLibraries bool
 	PreoptFlags     []string
 
-	ProfileClassListing  string
+	ProfileClassListing  android.OptionalPath
 	ProfileIsTextListing bool
 
 	EnforceUsesLibraries  bool
 	OptionalUsesLibraries []string
 	UsesLibraries         []string
-	LibraryPaths          map[string]string
+	LibraryPaths          map[string]android.Path
 
 	Archs           []android.ArchType
-	DexPreoptImages []string
+	DexPreoptImages []android.Path
 
-	PreoptBootClassPathDexFiles     []string // file paths of boot class path files
-	PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
+	PreoptBootClassPathDexFiles     android.Paths // file paths of boot class path files
+	PreoptBootClassPathDexLocations []string      // virtual locations of boot class path files
 
 	PreoptExtractedApk bool // Overrides OnlyPreoptModules
 
@@ -128,24 +134,137 @@
 	PresignedPrebuilt bool
 
 	NoStripping     bool
-	StripInputPath  string
-	StripOutputPath string
+	StripInputPath  android.Path
+	StripOutputPath android.WritablePath
 }
 
-func LoadGlobalConfig(path string) (GlobalConfig, error) {
-	config := GlobalConfig{}
-	err := loadConfig(path, &config)
-	return config, err
+func constructPath(ctx android.PathContext, path string) android.Path {
+	buildDirPrefix := ctx.Config().BuildDir() + "/"
+	if path == "" {
+		return nil
+	} else if strings.HasPrefix(path, buildDirPrefix) {
+		return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
+	} else {
+		return android.PathForSource(ctx, path)
+	}
 }
 
-func LoadModuleConfig(path string) (ModuleConfig, error) {
-	config := ModuleConfig{}
-	err := loadConfig(path, &config)
-	return config, err
+func constructPaths(ctx android.PathContext, paths []string) android.Paths {
+	var ret android.Paths
+	for _, path := range paths {
+		ret = append(ret, constructPath(ctx, path))
+	}
+	return ret
 }
 
-func loadConfig(path string, config interface{}) error {
-	data, err := ioutil.ReadFile(path)
+func constructPathMap(ctx android.PathContext, paths map[string]string) map[string]android.Path {
+	ret := map[string]android.Path{}
+	for key, path := range paths {
+		ret[key] = constructPath(ctx, path)
+	}
+	return ret
+}
+
+func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
+	if path == "" {
+		return nil
+	}
+	return constructPath(ctx, path).(android.WritablePath)
+}
+
+// LoadGlobalConfig reads the global dexpreopt.config file into a GlobalConfig struct.  It is used directly in Soong
+// and in dexpreopt_gen called from Make to read the $OUT/dexpreopt.config written by Make.
+func LoadGlobalConfig(ctx android.PathContext, path string) (GlobalConfig, error) {
+	type GlobalJSONConfig struct {
+		GlobalConfig
+
+		// Copies of entries in GlobalConfig that are not constructable without extra parameters.  They will be
+		// used to construct the real value manually below.
+		DirtyImageObjects string
+		PreloadedClasses  string
+		BootImageProfiles []string
+
+		Tools struct {
+			Profman  string
+			Dex2oat  string
+			Aapt     string
+			SoongZip string
+			Zip2zip  string
+
+			VerifyUsesLibraries string
+			ConstructContext    string
+		}
+	}
+
+	config := GlobalJSONConfig{}
+	err := loadConfig(ctx, path, &config)
+	if err != nil {
+		return config.GlobalConfig, err
+	}
+
+	// Construct paths that require a PathContext.
+	config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
+	config.GlobalConfig.PreloadedClasses = android.OptionalPathForPath(constructPath(ctx, config.PreloadedClasses))
+	config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
+
+	config.GlobalConfig.Tools.Profman = constructPath(ctx, config.Tools.Profman)
+	config.GlobalConfig.Tools.Dex2oat = constructPath(ctx, config.Tools.Dex2oat)
+	config.GlobalConfig.Tools.Aapt = constructPath(ctx, config.Tools.Aapt)
+	config.GlobalConfig.Tools.SoongZip = constructPath(ctx, config.Tools.SoongZip)
+	config.GlobalConfig.Tools.Zip2zip = constructPath(ctx, config.Tools.Zip2zip)
+	config.GlobalConfig.Tools.VerifyUsesLibraries = constructPath(ctx, config.Tools.VerifyUsesLibraries)
+	config.GlobalConfig.Tools.ConstructContext = constructPath(ctx, config.Tools.ConstructContext)
+
+	return config.GlobalConfig, nil
+}
+
+// LoadModuleConfig reads a per-module dexpreopt.config file into a ModuleConfig struct.  It is not used in Soong, which
+// receives a ModuleConfig struct directly from java/dexpreopt.go.  It is used in dexpreopt_gen called from oMake to
+// read the module dexpreopt.config written by Make.
+func LoadModuleConfig(ctx android.PathContext, path string) (ModuleConfig, error) {
+	type ModuleJSONConfig struct {
+		ModuleConfig
+
+		// Copies of entries in ModuleConfig that are not constructable without extra parameters.  They will be
+		// used to construct the real value manually below.
+		BuildPath                   string
+		DexPath                     string
+		ProfileClassListing         string
+		LibraryPaths                map[string]string
+		DexPreoptImages             []string
+		PreoptBootClassPathDexFiles []string
+		StripInputPath              string
+		StripOutputPath             string
+	}
+
+	config := ModuleJSONConfig{}
+
+	err := loadConfig(ctx, path, &config)
+	if err != nil {
+		return config.ModuleConfig, err
+	}
+
+	// Construct paths that require a PathContext.
+	config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
+	config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
+	config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
+	config.ModuleConfig.LibraryPaths = constructPathMap(ctx, config.LibraryPaths)
+	config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
+	config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
+	config.ModuleConfig.StripInputPath = constructPath(ctx, config.StripInputPath)
+	config.ModuleConfig.StripOutputPath = constructWritablePath(ctx, config.StripOutputPath)
+
+	return config.ModuleConfig, nil
+}
+
+func loadConfig(ctx android.PathContext, path string, config interface{}) error {
+	r, err := ctx.Fs().Open(path)
+	if err != nil {
+		return err
+	}
+	defer r.Close()
+
+	data, err := ioutil.ReadAll(r)
 	if err != nil {
 		return err
 	}
@@ -157,3 +276,58 @@
 
 	return nil
 }
+
+func GlobalConfigForTests(ctx android.PathContext) GlobalConfig {
+	return GlobalConfig{
+		DefaultNoStripping:                 false,
+		DisablePreopt:                      false,
+		DisablePreoptModules:               nil,
+		OnlyPreoptBootImageAndSystemServer: false,
+		HasSystemOther:                     false,
+		PatternsOnSystemOther:              nil,
+		DisableGenerateProfile:             false,
+		ProfileDir:                         "",
+		BootJars:                           nil,
+		RuntimeApexJars:                    nil,
+		ProductUpdatableBootModules:        nil,
+		ProductUpdatableBootLocations:      nil,
+		SystemServerJars:                   nil,
+		SystemServerApps:                   nil,
+		SpeedApps:                          nil,
+		PreoptFlags:                        nil,
+		DefaultCompilerFilter:              "",
+		SystemServerCompilerFilter:         "",
+		GenerateDMFiles:                    false,
+		NeverAllowStripping:                false,
+		NoDebugInfo:                        false,
+		AlwaysSystemServerDebugInfo:        false,
+		NeverSystemServerDebugInfo:         false,
+		AlwaysOtherDebugInfo:               false,
+		NeverOtherDebugInfo:                false,
+		MissingUsesLibraries:               nil,
+		IsEng:                              false,
+		SanitizeLite:                       false,
+		DefaultAppImages:                   false,
+		Dex2oatXmx:                         "",
+		Dex2oatXms:                         "",
+		EmptyDirectory:                     "empty_dir",
+		CpuVariant:                         nil,
+		InstructionSetFeatures:             nil,
+		DirtyImageObjects:                  android.OptionalPath{},
+		PreloadedClasses:                   android.OptionalPath{},
+		BootImageProfiles:                  nil,
+		UseProfileForBootImage:             false,
+		BootFlags:                          "",
+		Dex2oatImageXmx:                    "",
+		Dex2oatImageXms:                    "",
+		Tools: Tools{
+			Profman:             android.PathForTesting("profman"),
+			Dex2oat:             android.PathForTesting("dex2oat"),
+			Aapt:                android.PathForTesting("aapt"),
+			SoongZip:            android.PathForTesting("soong_zip"),
+			Zip2zip:             android.PathForTesting("zip2zip"),
+			VerifyUsesLibraries: android.PathForTesting("verify_uses_libraries.sh"),
+			ConstructContext:    android.PathForTesting("construct_context.sh"),
+		},
+	}
+}
diff --git a/dexpreopt/dexpreopt.go b/dexpreopt/dexpreopt.go
index 7fdfb49..9e333c1 100644
--- a/dexpreopt/dexpreopt.go
+++ b/dexpreopt/dexpreopt.go
@@ -37,6 +37,7 @@
 import (
 	"fmt"
 	"path/filepath"
+	"runtime"
 	"strings"
 
 	"android/soong/android"
@@ -52,7 +53,9 @@
 func GenerateStripRule(global GlobalConfig, module ModuleConfig) (rule *android.RuleBuilder, err error) {
 	defer func() {
 		if r := recover(); r != nil {
-			if e, ok := r.(error); ok {
+			if _, ok := r.(runtime.Error); ok {
+				panic(r)
+			} else if e, ok := r.(error); ok {
 				err = e
 				rule = nil
 			} else {
@@ -86,10 +89,14 @@
 
 // GenerateDexpreoptRule generates a set of commands that will preopt a module based on a GlobalConfig and a
 // ModuleConfig.  The produced files and their install locations will be available through rule.Installs().
-func GenerateDexpreoptRule(global GlobalConfig, module ModuleConfig) (rule *android.RuleBuilder, err error) {
+func GenerateDexpreoptRule(ctx android.PathContext,
+	global GlobalConfig, module ModuleConfig) (rule *android.RuleBuilder, err error) {
+
 	defer func() {
 		if r := recover(); r != nil {
-			if e, ok := r.(error); ok {
+			if _, ok := r.(runtime.Error); ok {
+				panic(r)
+			} else if e, ok := r.(error); ok {
 				err = e
 				rule = nil
 			} else {
@@ -100,11 +107,11 @@
 
 	rule = android.NewRuleBuilder()
 
-	generateProfile := module.ProfileClassListing != "" && !global.DisableGenerateProfile
+	generateProfile := module.ProfileClassListing.Valid() && !global.DisableGenerateProfile
 
-	var profile string
+	var profile android.WritablePath
 	if generateProfile {
-		profile = profileCommand(global, module, rule)
+		profile = profileCommand(ctx, global, module, rule)
 	}
 
 	if !dexpreoptDisabled(global, module) {
@@ -118,7 +125,7 @@
 
 			for i, arch := range module.Archs {
 				image := module.DexPreoptImages[i]
-				dexpreoptCommand(global, module, rule, arch, profile, image, appImage, generateDM)
+				dexpreoptCommand(ctx, global, module, rule, arch, profile, image, appImage, generateDM)
 			}
 		}
 	}
@@ -143,8 +150,10 @@
 	return false
 }
 
-func profileCommand(global GlobalConfig, module ModuleConfig, rule *android.RuleBuilder) string {
-	profilePath := filepath.Join(filepath.Dir(module.BuildPath), "profile.prof")
+func profileCommand(ctx android.PathContext, global GlobalConfig, module ModuleConfig,
+	rule *android.RuleBuilder) android.WritablePath {
+
+	profilePath := module.BuildPath.InSameDir(ctx, "profile.prof")
 	profileInstalledPath := module.DexLocation + ".prof"
 
 	if !module.ProfileIsTextListing {
@@ -158,13 +167,13 @@
 	if module.ProfileIsTextListing {
 		// The profile is a test listing of classes (used for framework jars).
 		// We need to generate the actual binary profile before being able to compile.
-		cmd.FlagWithInput("--create-profile-from=", module.ProfileClassListing)
+		cmd.FlagWithInput("--create-profile-from=", module.ProfileClassListing.Path())
 	} else {
 		// The profile is binary profile (used for apps). Run it through profman to
 		// ensure the profile keys match the apk.
 		cmd.
 			Flag("--copy-and-update-profile-key").
-			FlagWithInput("--profile-file=", module.ProfileClassListing)
+			FlagWithInput("--profile-file=", module.ProfileClassListing.Path())
 	}
 
 	cmd.
@@ -180,8 +189,8 @@
 	return profilePath
 }
 
-func dexpreoptCommand(global GlobalConfig, module ModuleConfig, rule *android.RuleBuilder,
-	arch android.ArchType, profile, bootImage string, appImage, generateDM bool) {
+func dexpreoptCommand(ctx android.PathContext, global GlobalConfig, module ModuleConfig, rule *android.RuleBuilder,
+	arch android.ArchType, profile, bootImage android.Path, appImage, generateDM bool) {
 
 	// HACK: make soname in Soong-generated .odex files match Make.
 	base := filepath.Base(module.DexLocation)
@@ -199,21 +208,21 @@
 			pathtools.ReplaceExtension(filepath.Base(path), "odex"))
 	}
 
-	odexPath := toOdexPath(filepath.Join(filepath.Dir(module.BuildPath), base))
+	odexPath := module.BuildPath.InSameDir(ctx, "oat", arch.String(), pathtools.ReplaceExtension(base, "odex"))
 	odexInstallPath := toOdexPath(module.DexLocation)
 	if odexOnSystemOther(module, global) {
 		odexInstallPath = strings.Replace(odexInstallPath, SystemPartition, SystemOtherPartition, 1)
 	}
 
-	vdexPath := pathtools.ReplaceExtension(odexPath, "vdex")
+	vdexPath := odexPath.ReplaceExtension(ctx, "vdex")
 	vdexInstallPath := pathtools.ReplaceExtension(odexInstallPath, "vdex")
 
-	invocationPath := pathtools.ReplaceExtension(odexPath, "invocation")
+	invocationPath := odexPath.ReplaceExtension(ctx, "invocation")
 
 	// bootImage is .../dex_bootjars/system/framework/arm64/boot.art, but dex2oat wants
 	// .../dex_bootjars/system/framework/boot.art on the command line
 	var bootImageLocation string
-	if bootImage != "" {
+	if bootImage != nil {
 		bootImageLocation = PathToLocation(bootImage, arch)
 	}
 
@@ -227,19 +236,21 @@
 	var filteredOptionalUsesLibs []string
 
 	// The class loader context using paths in the build
-	var classLoaderContextHost []string
+	var classLoaderContextHost android.Paths
 
 	// The class loader context using paths as they will be on the device
 	var classLoaderContextTarget []string
 
 	// Extra paths that will be appended to the class loader if the APK manifest has targetSdkVersion < 28
-	var conditionalClassLoaderContextHost28 []string
+	var conditionalClassLoaderContextHost28 android.Paths
 	var conditionalClassLoaderContextTarget28 []string
 
 	// Extra paths that will be appended to the class loader if the APK manifest has targetSdkVersion < 29
-	var conditionalClassLoaderContextHost29 []string
+	var conditionalClassLoaderContextHost29 android.Paths
 	var conditionalClassLoaderContextTarget29 []string
 
+	var classLoaderContextHostString string
+
 	if module.EnforceUsesLibraries {
 		verifyUsesLibs = copyOf(module.UsesLibraries)
 		verifyOptionalUsesLibs = copyOf(module.OptionalUsesLibraries)
@@ -281,31 +292,41 @@
 			pathForLibrary(module, hidlBase))
 		conditionalClassLoaderContextTarget29 = append(conditionalClassLoaderContextTarget29,
 			filepath.Join("/system/framework", hidlBase+".jar"))
+
+		classLoaderContextHostString = strings.Join(classLoaderContextHost.Strings(), ":")
 	} else {
 		// Pass special class loader context to skip the classpath and collision check.
 		// This will get removed once LOCAL_USES_LIBRARIES is enforced.
 		// Right now LOCAL_USES_LIBRARIES is opt in, for the case where it's not specified we still default
 		// to the &.
-		classLoaderContextHost = []string{`\&`}
+		classLoaderContextHostString = `\&`
 	}
 
-	rule.Command().FlagWithArg("mkdir -p ", filepath.Dir(odexPath))
+	rule.Command().FlagWithArg("mkdir -p ", filepath.Dir(odexPath.String()))
 	rule.Command().FlagWithOutput("rm -f ", odexPath)
 	// Set values in the environment of the rule.  These may be modified by construct_context.sh.
-	rule.Command().FlagWithArg("class_loader_context_arg=--class-loader-context=",
-		strings.Join(classLoaderContextHost, ":"))
+	rule.Command().FlagWithArg("class_loader_context_arg=--class-loader-context=", classLoaderContextHostString)
 	rule.Command().Text(`stored_class_loader_context_arg=""`)
 
 	if module.EnforceUsesLibraries {
 		rule.Command().Textf(`uses_library_names="%s"`, strings.Join(verifyUsesLibs, " "))
 		rule.Command().Textf(`optional_uses_library_names="%s"`, strings.Join(verifyOptionalUsesLibs, " "))
 		rule.Command().Textf(`aapt_binary="%s"`, global.Tools.Aapt)
-		rule.Command().Textf(`dex_preopt_host_libraries="%s"`, strings.Join(classLoaderContextHost, " "))
-		rule.Command().Textf(`dex_preopt_target_libraries="%s"`, strings.Join(classLoaderContextTarget, " "))
-		rule.Command().Textf(`conditional_host_libs_28="%s"`, strings.Join(conditionalClassLoaderContextHost28, " "))
-		rule.Command().Textf(`conditional_target_libs_28="%s"`, strings.Join(conditionalClassLoaderContextTarget28, " "))
-		rule.Command().Textf(`conditional_host_libs_29="%s"`, strings.Join(conditionalClassLoaderContextHost29, " "))
-		rule.Command().Textf(`conditional_target_libs_29="%s"`, strings.Join(conditionalClassLoaderContextTarget29, " "))
+		rule.Command().Textf(`dex_preopt_host_libraries="%s"`,
+			strings.Join(classLoaderContextHost.Strings(), " ")).
+			Implicits(classLoaderContextHost)
+		rule.Command().Textf(`dex_preopt_target_libraries="%s"`,
+			strings.Join(classLoaderContextTarget, " "))
+		rule.Command().Textf(`conditional_host_libs_28="%s"`,
+			strings.Join(conditionalClassLoaderContextHost28.Strings(), " ")).
+			Implicits(conditionalClassLoaderContextHost28)
+		rule.Command().Textf(`conditional_target_libs_28="%s"`,
+			strings.Join(conditionalClassLoaderContextTarget28, " "))
+		rule.Command().Textf(`conditional_host_libs_29="%s"`,
+			strings.Join(conditionalClassLoaderContextHost29.Strings(), " ")).
+			Implicits(conditionalClassLoaderContextHost29)
+		rule.Command().Textf(`conditional_target_libs_29="%s"`,
+			strings.Join(conditionalClassLoaderContextTarget29, " "))
 		rule.Command().Text("source").Tool(global.Tools.VerifyUsesLibraries).Input(module.DexPath)
 		rule.Command().Text("source").Tool(global.Tools.ConstructContext)
 	}
@@ -364,7 +385,7 @@
 			// Apps loaded into system server, and apps the product default to being compiled with the
 			// 'speed' compiler filter.
 			compilerFilter = "speed"
-		} else if profile != "" {
+		} else if profile != nil {
 			// For non system server jars, use speed-profile when we have a profile.
 			compilerFilter = "speed-profile"
 		} else if global.DefaultCompilerFilter != "" {
@@ -377,9 +398,9 @@
 
 	if generateDM {
 		cmd.FlagWithArg("--copy-dex-files=", "false")
-		dmPath := filepath.Join(filepath.Dir(module.BuildPath), "generated.dm")
+		dmPath := module.BuildPath.InSameDir(ctx, "generated.dm")
 		dmInstalledPath := pathtools.ReplaceExtension(module.DexLocation, "dm")
-		tmpPath := filepath.Join(filepath.Dir(module.BuildPath), "primary.vdex")
+		tmpPath := module.BuildPath.InSameDir(ctx, "primary.vdex")
 		rule.Command().Text("cp -f").Input(vdexPath).Output(tmpPath)
 		rule.Command().Tool(global.Tools.SoongZip).
 			FlagWithArg("-L", "9").
@@ -428,15 +449,15 @@
 	cmd.FlagWithArg("--compilation-reason=", "prebuilt")
 
 	if appImage {
-		appImagePath := pathtools.ReplaceExtension(odexPath, "art")
+		appImagePath := odexPath.ReplaceExtension(ctx, "art")
 		appImageInstallPath := pathtools.ReplaceExtension(odexInstallPath, "art")
 		cmd.FlagWithOutput("--app-image-file=", appImagePath).
 			FlagWithArg("--image-format=", "lz4")
 		rule.Install(appImagePath, appImageInstallPath)
 	}
 
-	if profile != "" {
-		cmd.FlagWithArg("--profile-file=", profile)
+	if profile != nil {
+		cmd.FlagWithInput("--profile-file=", profile)
 	}
 
 	rule.Install(odexPath, odexInstallPath)
@@ -522,17 +543,17 @@
 }
 
 // PathToLocation converts .../system/framework/arm64/boot.art to .../system/framework/boot.art
-func PathToLocation(path string, arch android.ArchType) string {
-	pathArch := filepath.Base(filepath.Dir(path))
+func PathToLocation(path android.Path, arch android.ArchType) string {
+	pathArch := filepath.Base(filepath.Dir(path.String()))
 	if pathArch != arch.String() {
 		panic(fmt.Errorf("last directory in %q must be %q", path, arch.String()))
 	}
-	return filepath.Join(filepath.Dir(filepath.Dir(path)), filepath.Base(path))
+	return filepath.Join(filepath.Dir(filepath.Dir(path.String())), filepath.Base(path.String()))
 }
 
-func pathForLibrary(module ModuleConfig, lib string) string {
-	path := module.LibraryPaths[lib]
-	if path == "" {
+func pathForLibrary(module ModuleConfig, lib string) android.Path {
+	path, ok := module.LibraryPaths[lib]
+	if !ok {
 		panic(fmt.Errorf("unknown library path for %q", lib))
 	}
 	return path
diff --git a/dexpreopt/dexpreopt_gen/dexpreopt_gen.go b/dexpreopt/dexpreopt_gen/dexpreopt_gen.go
index cc3c1f1..c72f684 100644
--- a/dexpreopt/dexpreopt_gen/dexpreopt_gen.go
+++ b/dexpreopt/dexpreopt_gen/dexpreopt_gen.go
@@ -21,6 +21,7 @@
 	"os"
 	"path/filepath"
 	"runtime"
+	"strings"
 
 	"android/soong/android"
 	"android/soong/dexpreopt"
@@ -33,8 +34,17 @@
 	stripScriptPath     = flag.String("strip_script", "", "path to output strip script")
 	globalConfigPath    = flag.String("global", "", "path to global configuration file")
 	moduleConfigPath    = flag.String("module", "", "path to module configuration file")
+	outDir              = flag.String("out_dir", "", "path to output directory")
 )
 
+type pathContext struct {
+	config android.Config
+}
+
+func (x *pathContext) Fs() pathtools.FileSystem   { return pathtools.OsFs }
+func (x *pathContext) Config() android.Config     { return x.config }
+func (x *pathContext) AddNinjaFileDeps(...string) {}
+
 func main() {
 	flag.Parse()
 
@@ -66,18 +76,26 @@
 		usage("path to module configuration file is required")
 	}
 
-	globalConfig, err := dexpreopt.LoadGlobalConfig(*globalConfigPath)
+	ctx := &pathContext{android.TestConfig(*outDir, nil)}
+
+	globalConfig, err := dexpreopt.LoadGlobalConfig(ctx, *globalConfigPath)
 	if err != nil {
 		fmt.Fprintf(os.Stderr, "error loading global config %q: %s\n", *globalConfigPath, err)
 		os.Exit(2)
 	}
 
-	moduleConfig, err := dexpreopt.LoadModuleConfig(*moduleConfigPath)
+	moduleConfig, err := dexpreopt.LoadModuleConfig(ctx, *moduleConfigPath)
 	if err != nil {
 		fmt.Fprintf(os.Stderr, "error loading module config %q: %s\n", *moduleConfigPath, err)
 		os.Exit(2)
 	}
 
+	// This shouldn't be using *PathForTesting, but it's outside of soong_build so its OK for now.
+	moduleConfig.StripInputPath = android.PathForTesting("$1")
+	moduleConfig.StripOutputPath = android.WritablePathForTesting("$2")
+
+	moduleConfig.DexPath = android.PathForTesting("$1")
+
 	defer func() {
 		if r := recover(); r != nil {
 			switch x := r.(type) {
@@ -92,30 +110,30 @@
 		}
 	}()
 
-	writeScripts(globalConfig, moduleConfig, *dexpreoptScriptPath, *stripScriptPath)
+	writeScripts(ctx, globalConfig, moduleConfig, *dexpreoptScriptPath, *stripScriptPath)
 }
 
-func writeScripts(global dexpreopt.GlobalConfig, module dexpreopt.ModuleConfig,
+func writeScripts(ctx android.PathContext, global dexpreopt.GlobalConfig, module dexpreopt.ModuleConfig,
 	dexpreoptScriptPath, stripScriptPath string) {
-	dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(global, module)
+	dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, global, module)
 	if err != nil {
 		panic(err)
 	}
 
-	installDir := filepath.Join(filepath.Dir(module.BuildPath), "dexpreopt_install")
+	installDir := module.BuildPath.InSameDir(ctx, "dexpreopt_install")
 
-	dexpreoptRule.Command().FlagWithArg("rm -rf ", installDir)
-	dexpreoptRule.Command().FlagWithArg("mkdir -p ", installDir)
+	dexpreoptRule.Command().FlagWithArg("rm -rf ", installDir.String())
+	dexpreoptRule.Command().FlagWithArg("mkdir -p ", installDir.String())
 
 	for _, install := range dexpreoptRule.Installs() {
-		installPath := filepath.Join(installDir, install.To)
-		dexpreoptRule.Command().Text("mkdir -p").Flag(filepath.Dir(installPath))
+		installPath := installDir.Join(ctx, strings.TrimPrefix(install.To, "/"))
+		dexpreoptRule.Command().Text("mkdir -p").Flag(filepath.Dir(installPath.String()))
 		dexpreoptRule.Command().Text("cp -f").Input(install.From).Output(installPath)
 	}
 	dexpreoptRule.Command().Tool(global.Tools.SoongZip).
-		FlagWithOutput("-o ", "$2").
-		FlagWithArg("-C ", installDir).
-		FlagWithArg("-D ", installDir)
+		FlagWithArg("-o ", "$2").
+		FlagWithArg("-C ", installDir.String()).
+		FlagWithArg("-D ", installDir.String())
 
 	stripRule, err := dexpreopt.GenerateStripRule(global, module)
 	if err != nil {
@@ -139,7 +157,7 @@
 		for _, input := range rule.Inputs() {
 			// Assume the rule that ran the script already has a dependency on the input file passed on the
 			// command line.
-			if input != "$1" {
+			if input.String() != "$1" {
 				fmt.Fprintf(depFile, `    %s \`+"\n", input)
 			}
 		}
@@ -159,13 +177,13 @@
 	}
 
 	// The written scripts will assume the input is $1 and the output is $2
-	if module.DexPath != "$1" {
+	if module.DexPath.String() != "$1" {
 		panic(fmt.Errorf("module.DexPath must be '$1', was %q", module.DexPath))
 	}
-	if module.StripInputPath != "$1" {
+	if module.StripInputPath.String() != "$1" {
 		panic(fmt.Errorf("module.StripInputPath must be '$1', was %q", module.StripInputPath))
 	}
-	if module.StripOutputPath != "$2" {
+	if module.StripOutputPath.String() != "$2" {
 		panic(fmt.Errorf("module.StripOutputPath must be '$2', was %q", module.StripOutputPath))
 	}
 
diff --git a/dexpreopt/dexpreopt_test.go b/dexpreopt/dexpreopt_test.go
index 949f91f..6dfa9d2 100644
--- a/dexpreopt/dexpreopt_test.go
+++ b/dexpreopt/dexpreopt_test.go
@@ -21,114 +21,61 @@
 	"testing"
 )
 
-var testGlobalConfig = GlobalConfig{
-	DefaultNoStripping:                 false,
-	DisablePreoptModules:               nil,
-	OnlyPreoptBootImageAndSystemServer: false,
-	HasSystemOther:                     false,
-	PatternsOnSystemOther:              nil,
-	DisableGenerateProfile:             false,
-	BootJars:                           nil,
-	RuntimeApexJars:                    nil,
-	ProductUpdatableBootModules:        nil,
-	ProductUpdatableBootLocations:      nil,
-	SystemServerJars:                   nil,
-	SystemServerApps:                   nil,
-	SpeedApps:                          nil,
-	PreoptFlags:                        nil,
-	DefaultCompilerFilter:              "",
-	SystemServerCompilerFilter:         "",
-	GenerateDMFiles:                    false,
-	NeverAllowStripping:                false,
-	NoDebugInfo:                        false,
-	AlwaysSystemServerDebugInfo:        false,
-	NeverSystemServerDebugInfo:         false,
-	AlwaysOtherDebugInfo:               false,
-	NeverOtherDebugInfo:                false,
-	MissingUsesLibraries:               nil,
-	IsEng:                              false,
-	SanitizeLite:                       false,
-	DefaultAppImages:                   false,
-	Dex2oatXmx:                         "",
-	Dex2oatXms:                         "",
-	EmptyDirectory:                     "",
-	CpuVariant:                         nil,
-	InstructionSetFeatures:             nil,
-	DirtyImageObjects:                  "",
-	PreloadedClasses:                   "",
-	BootImageProfiles:                  nil,
-	BootFlags:                          "",
-	Dex2oatImageXmx:                    "",
-	Dex2oatImageXms:                    "",
-	Tools: Tools{
-		Profman:             "profman",
-		Dex2oat:             "dex2oat",
-		Aapt:                "aapt",
-		SoongZip:            "soong_zip",
-		Zip2zip:             "zip2zip",
-		VerifyUsesLibraries: "verify_uses_libraries.sh",
-		ConstructContext:    "construct_context.sh",
-	},
-}
-
-var testModuleConfig = ModuleConfig{
-	Name:                            "",
-	DexLocation:                     "",
-	BuildPath:                       "",
-	DexPath:                         "",
-	UncompressedDex:                 false,
-	HasApkLibraries:                 false,
-	PreoptFlags:                     nil,
-	ProfileClassListing:             "",
-	ProfileIsTextListing:            false,
-	EnforceUsesLibraries:            false,
-	OptionalUsesLibraries:           nil,
-	UsesLibraries:                   nil,
-	LibraryPaths:                    nil,
-	Archs:                           []android.ArchType{android.Arm},
-	DexPreoptImages:                 []string{"system/framework/arm/boot.art"},
-	PreoptBootClassPathDexFiles:     nil,
-	PreoptBootClassPathDexLocations: nil,
-	PreoptExtractedApk:              false,
-	NoCreateAppImage:                false,
-	ForceCreateAppImage:             false,
-	PresignedPrebuilt:               false,
-	NoStripping:                     false,
-	StripInputPath:                  "",
-	StripOutputPath:                 "",
+func testModuleConfig(ctx android.PathContext) ModuleConfig {
+	return ModuleConfig{
+		Name:                            "test",
+		DexLocation:                     "/system/app/test/test.apk",
+		BuildPath:                       android.PathForOutput(ctx, "test/test.apk"),
+		DexPath:                         android.PathForOutput(ctx, "test/dex/test.jar"),
+		UncompressedDex:                 false,
+		HasApkLibraries:                 false,
+		PreoptFlags:                     nil,
+		ProfileClassListing:             android.OptionalPath{},
+		ProfileIsTextListing:            false,
+		EnforceUsesLibraries:            false,
+		OptionalUsesLibraries:           nil,
+		UsesLibraries:                   nil,
+		LibraryPaths:                    nil,
+		Archs:                           []android.ArchType{android.Arm},
+		DexPreoptImages:                 android.Paths{android.PathForTesting("system/framework/arm/boot.art")},
+		PreoptBootClassPathDexFiles:     nil,
+		PreoptBootClassPathDexLocations: nil,
+		PreoptExtractedApk:              false,
+		NoCreateAppImage:                false,
+		ForceCreateAppImage:             false,
+		PresignedPrebuilt:               false,
+		NoStripping:                     false,
+		StripInputPath:                  android.PathForOutput(ctx, "unstripped/test.apk"),
+		StripOutputPath:                 android.PathForOutput(ctx, "stripped/test.apk"),
+	}
 }
 
 func TestDexPreopt(t *testing.T) {
-	global, module := testGlobalConfig, testModuleConfig
+	ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
+	global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
 
-	module.Name = "test"
-	module.DexLocation = "/system/app/test/test.apk"
-	module.BuildPath = "out/test/test.apk"
-
-	rule, err := GenerateDexpreoptRule(global, module)
+	rule, err := GenerateDexpreoptRule(ctx, global, module)
 	if err != nil {
-		t.Error(err)
+		t.Fatal(err)
 	}
 
 	wantInstalls := android.RuleBuilderInstalls{
-		{"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},
-		{"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"},
+		{android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
+		{android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
 	}
 
-	if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
+	if rule.Installs().String() != wantInstalls.String() {
 		t.Errorf("\nwant installs:\n   %v\ngot:\n   %v", wantInstalls, rule.Installs())
 	}
 }
 
 func TestDexPreoptStrip(t *testing.T) {
 	// Test that we panic if we strip in a configuration where stripping is not allowed.
-	global, module := testGlobalConfig, testModuleConfig
+	ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
+	global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
 
 	global.NeverAllowStripping = true
 	module.NoStripping = false
-	module.Name = "test"
-	module.DexLocation = "/system/app/test/test.apk"
-	module.BuildPath = "out/test/test.apk"
 
 	_, err := GenerateStripRule(global, module)
 	if err == nil {
@@ -137,51 +84,46 @@
 }
 
 func TestDexPreoptSystemOther(t *testing.T) {
-	global, module := testGlobalConfig, testModuleConfig
+	ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
+	global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
 
 	global.HasSystemOther = true
 	global.PatternsOnSystemOther = []string{"app/%"}
 
-	module.Name = "test"
-	module.DexLocation = "/system/app/test/test.apk"
-	module.BuildPath = "out/test/test.apk"
-
-	rule, err := GenerateDexpreoptRule(global, module)
+	rule, err := GenerateDexpreoptRule(ctx, global, module)
 	if err != nil {
-		t.Error(err)
+		t.Fatal(err)
 	}
 
 	wantInstalls := android.RuleBuilderInstalls{
-		{"out/test/oat/arm/package.odex", "/system_other/app/test/oat/arm/test.odex"},
-		{"out/test/oat/arm/package.vdex", "/system_other/app/test/oat/arm/test.vdex"},
+		{android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system_other/app/test/oat/arm/test.odex"},
+		{android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system_other/app/test/oat/arm/test.vdex"},
 	}
 
-	if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
+	if rule.Installs().String() != wantInstalls.String() {
 		t.Errorf("\nwant installs:\n   %v\ngot:\n   %v", wantInstalls, rule.Installs())
 	}
 }
 
 func TestDexPreoptProfile(t *testing.T) {
-	global, module := testGlobalConfig, testModuleConfig
+	ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
+	global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
 
-	module.Name = "test"
-	module.DexLocation = "/system/app/test/test.apk"
-	module.BuildPath = "out/test/test.apk"
-	module.ProfileClassListing = "profile"
+	module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile"))
 
-	rule, err := GenerateDexpreoptRule(global, module)
+	rule, err := GenerateDexpreoptRule(ctx, global, module)
 	if err != nil {
-		t.Error(err)
+		t.Fatal(err)
 	}
 
 	wantInstalls := android.RuleBuilderInstalls{
-		{"out/test/profile.prof", "/system/app/test/test.apk.prof"},
-		{"out/test/oat/arm/package.art", "/system/app/test/oat/arm/test.art"},
-		{"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},
-		{"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"},
+		{android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"},
+		{android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"},
+		{android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
+		{android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
 	}
 
-	if !reflect.DeepEqual(rule.Installs(), wantInstalls) {
+	if rule.Installs().String() != wantInstalls.String() {
 		t.Errorf("\nwant installs:\n   %v\ngot:\n   %v", wantInstalls, rule.Installs())
 	}
 }
@@ -212,29 +154,24 @@
 	for _, test := range tests {
 		t.Run(test.name, func(t *testing.T) {
 
-			global, module := testGlobalConfig, testModuleConfig
-
-			module.Name = "test"
-			module.DexLocation = "/system/app/test/test.apk"
-			module.BuildPath = "out/test/test.apk"
-			module.StripInputPath = "$1"
-			module.StripOutputPath = "$2"
+			ctx := android.PathContextForTesting(android.TestConfig("out", nil), nil)
+			global, module := GlobalConfigForTests(ctx), testModuleConfig(ctx)
 
 			test.setup(&global, &module)
 
 			rule, err := GenerateStripRule(global, module)
 			if err != nil {
-				t.Error(err)
+				t.Fatal(err)
 			}
 
 			if test.strip {
-				want := `zip2zip -i $1 -o $2 -x "classes*.dex"`
+				want := `zip2zip -i out/unstripped/test.apk -o out/stripped/test.apk -x "classes*.dex"`
 				if len(rule.Commands()) < 1 || !strings.Contains(rule.Commands()[0], want) {
 					t.Errorf("\nwant commands[0] to have:\n   %v\ngot:\n   %v", want, rule.Commands()[0])
 				}
 			} else {
 				wantCommands := []string{
-					"cp -f $1 $2",
+					"cp -f out/unstripped/test.apk out/stripped/test.apk",
 				}
 				if !reflect.DeepEqual(rule.Commands(), wantCommands) {
 					t.Errorf("\nwant commands:\n   %v\ngot:\n   %v", wantCommands, rule.Commands())
diff --git a/java/dexpreopt.go b/java/dexpreopt.go
index 0a56529..ecb2421 100644
--- a/java/dexpreopt.go
+++ b/java/dexpreopt.go
@@ -56,11 +56,13 @@
 }
 
 func (d *dexpreopter) dexpreoptDisabled(ctx android.ModuleContext) bool {
-	if ctx.Config().DisableDexPreopt() {
+	global := dexpreoptGlobalConfig(ctx)
+
+	if global.DisablePreopt {
 		return true
 	}
 
-	if ctx.Config().DisableDexPreoptForModule(ctx.ModuleName()) {
+	if inList(ctx.ModuleName(), global.DisablePreoptModules) {
 		return true
 	}
 
@@ -85,22 +87,6 @@
 	return false
 }
 
-var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig")
-
-func dexpreoptGlobalConfig(ctx android.PathContext) dexpreopt.GlobalConfig {
-	return ctx.Config().Once(dexpreoptGlobalConfigKey, func() interface{} {
-		if f := ctx.Config().DexpreoptGlobalConfig(); f != "" {
-			ctx.AddNinjaFileDeps(f)
-			globalConfig, err := dexpreopt.LoadGlobalConfig(f)
-			if err != nil {
-				panic(err)
-			}
-			return globalConfig
-		}
-		return dexpreopt.GlobalConfig{}
-	}).(dexpreopt.GlobalConfig)
-}
-
 func odexOnSystemOther(ctx android.ModuleContext, installPath android.OutputPath) bool {
 	return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), dexpreoptGlobalConfig(ctx))
 }
@@ -110,7 +96,8 @@
 		return dexJarFile
 	}
 
-	info := dexpreoptBootJarsInfo(ctx)
+	global := dexpreoptGlobalConfig(ctx)
+	bootImage := defaultBootImageConfig(ctx)
 
 	var archs []android.ArchType
 	for _, a := range ctx.MultiTargets() {
@@ -121,7 +108,7 @@
 		for _, target := range ctx.Config().Targets[android.Android] {
 			archs = append(archs, target.Arch.ArchType)
 		}
-		if inList(ctx.ModuleName(), info.global.SystemServerJars) && !d.isSDKLibrary {
+		if inList(ctx.ModuleName(), global.SystemServerJars) && !d.isSDKLibrary {
 			// If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
 			archs = archs[:1]
 		}
@@ -131,17 +118,15 @@
 		archs = archs[:1]
 	}
 
-	var images []string
+	var images android.Paths
 	for _, arch := range archs {
-		images = append(images, info.images[arch].String())
+		images = append(images, bootImage.images[arch])
 	}
 
 	dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
 
 	strippedDexJarFile := android.PathForModuleOut(ctx, "dexpreopt", dexJarFile.Base())
 
-	deps := android.Paths{dexJarFile}
-
 	var profileClassListing android.OptionalPath
 	profileIsTextListing := false
 	if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
@@ -153,24 +138,20 @@
 			profileIsTextListing = true
 		} else {
 			profileClassListing = android.ExistentPathForSource(ctx,
-				ctx.Config().DexPreoptProfileDir(), ctx.ModuleName()+".prof")
+				global.ProfileDir, ctx.ModuleName()+".prof")
 		}
 	}
 
-	if profileClassListing.Valid() {
-		deps = append(deps, profileClassListing.Path())
-	}
-
 	dexpreoptConfig := dexpreopt.ModuleConfig{
 		Name:            ctx.ModuleName(),
 		DexLocation:     dexLocation,
-		BuildPath:       android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").String(),
-		DexPath:         dexJarFile.String(),
+		BuildPath:       android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
+		DexPath:         dexJarFile,
 		UncompressedDex: d.uncompressedDex,
 		HasApkLibraries: false,
 		PreoptFlags:     nil,
 
-		ProfileClassListing:  profileClassListing.String(),
+		ProfileClassListing:  profileClassListing,
 		ProfileIsTextListing: profileIsTextListing,
 
 		EnforceUsesLibraries:  false,
@@ -181,8 +162,8 @@
 		Archs:           archs,
 		DexPreoptImages: images,
 
-		PreoptBootClassPathDexFiles:     info.preoptBootDex.Strings(),
-		PreoptBootClassPathDexLocations: info.preoptBootLocations,
+		PreoptBootClassPathDexFiles:     bootImage.dexPaths.Paths(),
+		PreoptBootClassPathDexLocations: bootImage.dexLocations,
 
 		PreoptExtractedApk: false,
 
@@ -190,11 +171,11 @@
 		ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
 
 		NoStripping:     Bool(d.dexpreoptProperties.Dex_preopt.No_stripping),
-		StripInputPath:  dexJarFile.String(),
-		StripOutputPath: strippedDexJarFile.String(),
+		StripInputPath:  dexJarFile,
+		StripOutputPath: strippedDexJarFile.OutputPath,
 	}
 
-	dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(info.global, dexpreoptConfig)
+	dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, global, dexpreoptConfig)
 	if err != nil {
 		ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
 		return dexJarFile
@@ -204,7 +185,7 @@
 
 	d.builtInstalled = dexpreoptRule.Installs().String()
 
-	stripRule, err := dexpreopt.GenerateStripRule(info.global, dexpreoptConfig)
+	stripRule, err := dexpreopt.GenerateStripRule(global, dexpreoptConfig)
 	if err != nil {
 		ctx.ModuleErrorf("error generating dexpreopt strip rule: %s", err.Error())
 		return dexJarFile
diff --git a/java/dexpreopt_bootjars.go b/java/dexpreopt_bootjars.go
index 05868da..a35e011 100644
--- a/java/dexpreopt_bootjars.go
+++ b/java/dexpreopt_bootjars.go
@@ -48,56 +48,36 @@
 // The location is passed as an argument to the ART tools like dex2oat instead of the real path. The ART tools
 // will then reconstruct the real path, so the rules must have a dependency on the real path.
 
-type bootJarsInfo struct {
-	dir        android.OutputPath
-	symbolsDir android.OutputPath
-	images     map[android.ArchType]android.OutputPath
-	installs   map[android.ArchType]android.RuleBuilderInstalls
-
-	vdexInstalls       map[android.ArchType]android.RuleBuilderInstalls
-	unstrippedInstalls map[android.ArchType]android.RuleBuilderInstalls
-	profileInstalls    android.RuleBuilderInstalls
-
-	global dexpreopt.GlobalConfig
-
-	preoptBootModules     []string
-	preoptBootLocations   []string
-	preoptBootDex         android.WritablePaths
-	allBootModules        []string
-	allBootLocations      []string
-	bootclasspath         string
-	systemServerClasspath string
+type bootImageConfig struct {
+	name         string
+	modules      []string
+	dexLocations []string
+	dexPaths     android.WritablePaths
+	dir          android.OutputPath
+	symbolsDir   android.OutputPath
+	images       map[android.ArchType]android.OutputPath
 }
 
-var dexpreoptBootJarsInfoKey = android.NewOnceKey("dexpreoptBootJarsInfoKey")
+type bootImage struct {
+	bootImageConfig
 
-// dexpreoptBootJarsInfo creates all the paths for singleton files the first time it is called, which may be
-// from a ModuleContext that needs to reference a file that will be created by a singleton rule that hasn't
-// yet been created.
-func dexpreoptBootJarsInfo(ctx android.PathContext) *bootJarsInfo {
-	return ctx.Config().Once(dexpreoptBootJarsInfoKey, func() interface{} {
+	installs           map[android.ArchType]android.RuleBuilderInstalls
+	vdexInstalls       map[android.ArchType]android.RuleBuilderInstalls
+	unstrippedInstalls map[android.ArchType]android.RuleBuilderInstalls
 
-		info := &bootJarsInfo{
-			dir:        android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars"),
-			symbolsDir: android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars_unstripped"),
-			images:     make(map[android.ArchType]android.OutputPath),
-			installs:   make(map[android.ArchType]android.RuleBuilderInstalls),
+	profileInstalls android.RuleBuilderInstalls
+}
 
-			vdexInstalls:       make(map[android.ArchType]android.RuleBuilderInstalls),
-			unstrippedInstalls: make(map[android.ArchType]android.RuleBuilderInstalls),
-		}
+func newBootImage(ctx android.PathContext, config bootImageConfig) *bootImage {
+	image := &bootImage{
+		bootImageConfig: config,
 
-		for _, target := range ctx.Config().Targets[android.Android] {
-			info.images[target.Arch.ArchType] = info.dir.Join(ctx,
-				"system/framework", target.Arch.ArchType.String(), "boot.art")
-		}
+		installs:           make(map[android.ArchType]android.RuleBuilderInstalls),
+		vdexInstalls:       make(map[android.ArchType]android.RuleBuilderInstalls),
+		unstrippedInstalls: make(map[android.ArchType]android.RuleBuilderInstalls),
+	}
 
-		info.global = dexpreoptGlobalConfig(ctx)
-		computeBootClasspath(ctx, info)
-		computeSystemServerClasspath(ctx, info)
-
-		return info
-	}).(*bootJarsInfo)
+	return image
 }
 
 func concat(lists ...[]string) []string {
@@ -112,60 +92,8 @@
 	return ret
 }
 
-func computeBootClasspath(ctx android.PathContext, info *bootJarsInfo) {
-	runtimeModules := info.global.RuntimeApexJars
-	nonFrameworkModules := concat(runtimeModules, info.global.ProductUpdatableBootModules)
-	frameworkModules := android.RemoveListFromList(info.global.BootJars, nonFrameworkModules)
-
-	var nonUpdatableBootModules []string
-	var nonUpdatableBootLocations []string
-
-	for _, m := range runtimeModules {
-		nonUpdatableBootModules = append(nonUpdatableBootModules, m)
-		nonUpdatableBootLocations = append(nonUpdatableBootLocations,
-			filepath.Join("/apex/com.android.runtime/javalib", m+".jar"))
-	}
-
-	for _, m := range frameworkModules {
-		nonUpdatableBootModules = append(nonUpdatableBootModules, m)
-		nonUpdatableBootLocations = append(nonUpdatableBootLocations,
-			filepath.Join("/system/framework", m+".jar"))
-	}
-
-	// The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
-	// the bootclasspath modules have been compiled.  Set up known paths for them, the singleton rules will copy
-	// them there.
-	// TODO: use module dependencies instead
-	var nonUpdatableBootDex android.WritablePaths
-	for _, m := range nonUpdatableBootModules {
-		nonUpdatableBootDex = append(nonUpdatableBootDex,
-			android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars_input", m+".jar"))
-	}
-
-	allBootModules := concat(nonUpdatableBootModules, info.global.ProductUpdatableBootModules)
-	allBootLocations := concat(nonUpdatableBootLocations, info.global.ProductUpdatableBootLocations)
-
-	bootclasspath := strings.Join(allBootLocations, ":")
-
-	info.preoptBootModules = nonUpdatableBootModules
-	info.preoptBootLocations = nonUpdatableBootLocations
-	info.preoptBootDex = nonUpdatableBootDex
-	info.allBootModules = allBootModules
-	info.allBootLocations = allBootLocations
-	info.bootclasspath = bootclasspath
-}
-
-func computeSystemServerClasspath(ctx android.PathContext, info *bootJarsInfo) {
-	var systemServerClasspathLocations []string
-	for _, m := range info.global.SystemServerJars {
-		systemServerClasspathLocations = append(systemServerClasspathLocations,
-			filepath.Join("/system/framework", m+".jar"))
-	}
-
-	info.systemServerClasspath = strings.Join(systemServerClasspathLocations, ":")
-}
 func dexpreoptBootJarsFactory() android.Singleton {
-	return dexpreoptBootJars{}
+	return &dexpreoptBootJars{}
 }
 
 func skipDexpreoptBootJars(ctx android.PathContext) bool {
@@ -181,15 +109,18 @@
 	return false
 }
 
-type dexpreoptBootJars struct{}
+type dexpreoptBootJars struct {
+	defaultBootImage *bootImage
+	otherImages      []*bootImage
+}
 
 // dexpreoptBoot singleton rules
-func (dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
+func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
 	if skipDexpreoptBootJars(ctx) {
 		return
 	}
 
-	info := dexpreoptBootJarsInfo(ctx)
+	global := dexpreoptGlobalConfig(ctx)
 
 	// Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
 	// and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
@@ -197,17 +128,30 @@
 	//       on ASAN settings.
 	if len(ctx.Config().SanitizeDevice()) == 1 &&
 		ctx.Config().SanitizeDevice()[0] == "address" &&
-		info.global.SanitizeLite {
+		global.SanitizeLite {
 		return
 	}
 
-	bootDexJars := make(android.Paths, len(info.preoptBootModules))
+	// Always create the default boot image first, to get a unique profile rule for all images.
+	d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
+	if global.GenerateApexImage {
+		d.otherImages = append(d.otherImages, buildBootImage(ctx, apexBootImageConfig(ctx)))
+	}
+}
+
+// buildBootImage takes a bootImageConfig, creates rules to build it, and returns a *bootImage.
+func buildBootImage(ctx android.SingletonContext, config bootImageConfig) *bootImage {
+	global := dexpreoptGlobalConfig(ctx)
+
+	image := newBootImage(ctx, config)
+
+	bootDexJars := make(android.Paths, len(image.modules))
 
 	ctx.VisitAllModules(func(module android.Module) {
 		// Collect dex jar paths for the modules listed above.
 		if j, ok := module.(Dependency); ok {
 			name := ctx.ModuleName(module)
-			if i := android.IndexList(name, info.preoptBootModules); i != -1 {
+			if i := android.IndexList(name, image.modules); i != -1 {
 				bootDexJars[i] = j.DexJar()
 			}
 		}
@@ -218,11 +162,11 @@
 	for i := range bootDexJars {
 		if bootDexJars[i] == nil {
 			if ctx.Config().AllowMissingDependencies() {
-				missingDeps = append(missingDeps, info.preoptBootModules[i])
+				missingDeps = append(missingDeps, image.modules[i])
 				bootDexJars[i] = android.PathForOutput(ctx, "missing")
 			} else {
 				ctx.Errorf("failed to find dex jar path for module %q",
-					info.preoptBootModules[i])
+					image.modules[i])
 			}
 		}
 	}
@@ -234,32 +178,36 @@
 		ctx.Build(pctx, android.BuildParams{
 			Rule:   android.Cp,
 			Input:  bootDexJars[i],
-			Output: info.preoptBootDex[i],
+			Output: image.dexPaths[i],
 		})
 	}
 
-	profile := bootImageProfileRule(ctx, info, missingDeps)
+	profile := bootImageProfileRule(ctx, image, missingDeps)
 
-	if !ctx.Config().DisableDexPreopt() {
+	if !global.DisablePreopt {
 		targets := ctx.Config().Targets[android.Android]
 		if ctx.Config().SecondArchIsTranslated() {
 			targets = targets[:1]
 		}
 
 		for _, target := range targets {
-			dexPreoptBootImageRule(ctx, info, target.Arch.ArchType, profile, missingDeps)
+			buildBootImageRuleForArch(ctx, image, target.Arch.ArchType, profile, missingDeps)
 		}
 	}
+
+	return image
 }
 
-func dexPreoptBootImageRule(ctx android.SingletonContext, info *bootJarsInfo,
+func buildBootImageRuleForArch(ctx android.SingletonContext, image *bootImage,
 	arch android.ArchType, profile android.Path, missingDeps []string) {
 
-	symbolsDir := info.symbolsDir.Join(ctx, "system/framework", arch.String())
-	symbolsFile := symbolsDir.Join(ctx, "boot.oat")
-	outputDir := info.dir.Join(ctx, "system/framework", arch.String())
-	outputPath := info.images[arch]
-	oatLocation := pathtools.ReplaceExtension(dexpreopt.PathToLocation(outputPath.String(), arch), "oat")
+	global := dexpreoptGlobalConfig(ctx)
+
+	symbolsDir := image.symbolsDir.Join(ctx, "system/framework", arch.String())
+	symbolsFile := symbolsDir.Join(ctx, image.name+".oat")
+	outputDir := image.dir.Join(ctx, "system/framework", arch.String())
+	outputPath := image.images[arch]
+	oatLocation := pathtools.ReplaceExtension(dexpreopt.PathToLocation(outputPath, arch), "oat")
 
 	rule := android.NewRuleBuilder()
 	rule.MissingDeps(missingDeps)
@@ -287,43 +235,43 @@
 
 	invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
 
-	cmd.Tool(info.global.Tools.Dex2oat).
+	cmd.Tool(global.Tools.Dex2oat).
 		Flag("--avoid-storing-invocation").
-		FlagWithOutput("--write-invocation-to=", invocationPath.String()).ImplicitOutput(invocationPath.String()).
-		Flag("--runtime-arg").FlagWithArg("-Xms", info.global.Dex2oatImageXms).
-		Flag("--runtime-arg").FlagWithArg("-Xmx", info.global.Dex2oatImageXmx)
+		FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
+		Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
+		Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
 
-	if profile == nil {
-		cmd.FlagWithArg("--image-classes=", info.global.PreloadedClasses)
-	} else {
+	if profile != nil {
 		cmd.FlagWithArg("--compiler-filter=", "speed-profile")
-		cmd.FlagWithInput("--profile-file=", profile.String())
+		cmd.FlagWithInput("--profile-file=", profile)
+	} else if global.PreloadedClasses.Valid() {
+		cmd.FlagWithInput("--image-classes=", global.PreloadedClasses.Path())
 	}
 
-	if info.global.DirtyImageObjects != "" {
-		cmd.FlagWithArg("--dirty-image-objects=", info.global.DirtyImageObjects)
+	if global.DirtyImageObjects.Valid() {
+		cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
 	}
 
 	cmd.
-		FlagForEachInput("--dex-file=", info.preoptBootDex.Strings()).
-		FlagForEachArg("--dex-location=", info.preoptBootLocations).
+		FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
+		FlagForEachArg("--dex-location=", image.dexLocations).
 		Flag("--generate-debug-info").
 		Flag("--generate-build-id").
-		FlagWithArg("--oat-symbols=", symbolsFile.String()).
+		FlagWithOutput("--oat-symbols=", symbolsFile).
 		Flag("--strip").
-		FlagWithOutput("--oat-file=", outputPath.ReplaceExtension(ctx, "oat").String()).
+		FlagWithOutput("--oat-file=", outputPath.ReplaceExtension(ctx, "oat")).
 		FlagWithArg("--oat-location=", oatLocation).
-		FlagWithOutput("--image=", outputPath.String()).
+		FlagWithOutput("--image=", outputPath).
 		FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress()).
 		FlagWithArg("--instruction-set=", arch.String()).
-		FlagWithArg("--instruction-set-variant=", info.global.CpuVariant[arch]).
-		FlagWithArg("--instruction-set-features=", info.global.InstructionSetFeatures[arch]).
-		FlagWithArg("--android-root=", info.global.EmptyDirectory).
+		FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
+		FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
+		FlagWithArg("--android-root=", global.EmptyDirectory).
 		FlagWithArg("--no-inline-from=", "core-oj.jar").
 		Flag("--abort-on-hard-verifier-error")
 
-	if info.global.BootFlags != "" {
-		cmd.Flag(info.global.BootFlags)
+	if global.BootFlags != "" {
+		cmd.Flag(global.BootFlags)
 	}
 
 	if extraFlags != "" {
@@ -340,12 +288,12 @@
 	var unstrippedInstalls android.RuleBuilderInstalls
 
 	// dex preopt on the bootclasspath produces multiple files.  The first dex file
-	// is converted into to boot.art (to match the legacy assumption that boot.art
-	// exists), and the rest are converted to boot-<name>.art.
+	// is converted into to 'name'.art (to match the legacy assumption that 'name'.art
+	// exists), and the rest are converted to 'name'-<jar>.art.
 	// In addition, each .art file has an associated .oat and .vdex file, and an
 	// unstripped .oat file
-	for i, m := range info.preoptBootModules {
-		name := "boot"
+	for i, m := range image.modules {
+		name := image.name
 		if i != 0 {
 			name += "-" + m
 		}
@@ -358,106 +306,107 @@
 		extraFiles = append(extraFiles, art, oat, vdex, unstrippedOat)
 
 		// Install the .oat and .art files.
-		rule.Install(art.String(), filepath.Join(installDir, art.Base()))
-		rule.Install(oat.String(), filepath.Join(installDir, oat.Base()))
+		rule.Install(art, filepath.Join(installDir, art.Base()))
+		rule.Install(oat, filepath.Join(installDir, oat.Base()))
 
 		// The vdex files are identical between architectures, install them to a shared location.  The Make rules will
 		// only use the install rules for one architecture, and will create symlinks into the architecture-specific
 		// directories.
 		vdexInstalls = append(vdexInstalls,
-			android.RuleBuilderInstall{vdex.String(), filepath.Join(vdexInstallDir, vdex.Base())})
+			android.RuleBuilderInstall{vdex, filepath.Join(vdexInstallDir, vdex.Base())})
 
 		// Install the unstripped oat files.  The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
 		unstrippedInstalls = append(unstrippedInstalls,
-			android.RuleBuilderInstall{unstrippedOat.String(), filepath.Join(installDir, unstrippedOat.Base())})
+			android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
 	}
 
-	cmd.ImplicitOutputs(extraFiles.Strings())
+	cmd.ImplicitOutputs(extraFiles)
 
-	rule.Build(pctx, ctx, "bootJarsDexpreopt_"+arch.String(), "dexpreopt boot jars "+arch.String())
+	rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+arch.String(), "dexpreopt "+image.name+" jars "+arch.String())
 
 	// save output and installed files for makevars
-	info.installs[arch] = rule.Installs()
-	info.vdexInstalls[arch] = vdexInstalls
-	info.unstrippedInstalls[arch] = unstrippedInstalls
+	image.installs[arch] = rule.Installs()
+	image.vdexInstalls[arch] = vdexInstalls
+	image.unstrippedInstalls[arch] = unstrippedInstalls
 }
 
 const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
 It is likely that the boot classpath is inconsistent.
 Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
 
-func bootImageProfileRule(ctx android.SingletonContext, info *bootJarsInfo, missingDeps []string) android.WritablePath {
-	if len(info.global.BootImageProfiles) == 0 {
+func bootImageProfileRule(ctx android.SingletonContext, image *bootImage, missingDeps []string) android.WritablePath {
+	global := dexpreoptGlobalConfig(ctx)
+
+	if !global.UseProfileForBootImage || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
 		return nil
 	}
+	return ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
+		tools := global.Tools
 
-	tools := info.global.Tools
+		rule := android.NewRuleBuilder()
+		rule.MissingDeps(missingDeps)
 
-	rule := android.NewRuleBuilder()
-	rule.MissingDeps(missingDeps)
-
-	var bootImageProfile string
-	if len(info.global.BootImageProfiles) > 1 {
-		combinedBootImageProfile := info.dir.Join(ctx, "boot-image-profile.txt")
-		rule.Command().Text("cat").Inputs(info.global.BootImageProfiles).Output(combinedBootImageProfile.String())
-		bootImageProfile = combinedBootImageProfile.String()
-	} else {
-		bootImageProfile = info.global.BootImageProfiles[0]
-	}
-
-	profile := info.dir.Join(ctx, "boot.prof")
-
-	rule.Command().
-		Text(`ANDROID_LOG_TAGS="*:e"`).
-		Tool(tools.Profman).
-		FlagWithArg("--create-profile-from=", bootImageProfile).
-		FlagForEachInput("--apk=", info.preoptBootDex.Strings()).
-		FlagForEachArg("--dex-location=", info.preoptBootLocations).
-		FlagWithOutput("--reference-profile-file=", profile.String())
-
-	rule.Install(profile.String(), "/system/etc/boot-image.prof")
-
-	rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
-
-	info.profileInstalls = rule.Installs()
-
-	return profile
-}
-
-func init() {
-	android.RegisterMakeVarsProvider(pctx, bootImageMakeVars)
-}
-
-// Export paths to Make.  INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/.
-// Both paths are used to call dist-for-goals.
-func bootImageMakeVars(ctx android.MakeVarsContext) {
-	if skipDexpreoptBootJars(ctx) {
-		return
-	}
-
-	info := dexpreoptBootJarsInfo(ctx)
-	for arch, _ := range info.images {
-		ctx.Strict("DEXPREOPT_IMAGE_"+arch.String(), info.images[arch].String())
-
-		var builtInstalled []string
-		for _, install := range info.installs[arch] {
-			builtInstalled = append(builtInstalled, install.From+":"+install.To)
+		var bootImageProfile android.Path
+		if len(global.BootImageProfiles) > 1 {
+			combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
+			rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
+			bootImageProfile = combinedBootImageProfile
+		} else if len(global.BootImageProfiles) == 1 {
+			bootImageProfile = global.BootImageProfiles[0]
+		} else {
+			// If not set, use the default.  Some branches like master-art-host don't have frameworks/base, so manually
+			// handle the case that the default is missing.  Those branches won't attempt to build the profile rule,
+			// and if they do they'll get a missing deps error.
+			defaultProfile := "frameworks/base/config/boot-image-profile.txt"
+			path := android.ExistentPathForSource(ctx, defaultProfile)
+			if path.Valid() {
+				bootImageProfile = path.Path()
+			} else {
+				missingDeps = append(missingDeps, defaultProfile)
+				bootImageProfile = android.PathForOutput(ctx, "missing")
+			}
 		}
 
-		var unstrippedBuiltInstalled []string
-		for _, install := range info.unstrippedInstalls[arch] {
-			unstrippedBuiltInstalled = append(unstrippedBuiltInstalled, install.From+":"+install.To)
+		profile := image.dir.Join(ctx, "boot.prof")
+
+		rule.Command().
+			Text(`ANDROID_LOG_TAGS="*:e"`).
+			Tool(tools.Profman).
+			FlagWithInput("--create-profile-from=", bootImageProfile).
+			FlagForEachInput("--apk=", image.dexPaths.Paths()).
+			FlagForEachArg("--dex-location=", image.dexLocations).
+			FlagWithOutput("--reference-profile-file=", profile)
+
+		rule.Install(profile, "/system/etc/boot-image.prof")
+
+		rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
+
+		image.profileInstalls = rule.Installs()
+
+		return profile
+	}).(android.WritablePath)
+}
+
+var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
+
+// Export paths for default boot image to Make
+func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
+	image := d.defaultBootImage
+	if image != nil {
+		ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
+		ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPaths.Strings(), " "))
+		ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocations, " "))
+
+		var imageNames []string
+		for _, current := range append(d.otherImages, image) {
+			imageNames = append(imageNames, current.name)
+			for arch, _ := range current.images {
+				ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.vdexInstalls[arch].String())
+				ctx.Strict("DEXPREOPT_IMAGE_"+current.name+"_"+arch.String(), current.images[arch].String())
+				ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.installs[arch].String())
+				ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.unstrippedInstalls[arch].String())
+			}
 		}
-
-		ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+arch.String(), info.installs[arch].String())
-		ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+arch.String(), info.unstrippedInstalls[arch].String())
-		ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+arch.String(), info.vdexInstalls[arch].String())
+		ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
 	}
-
-	ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", info.profileInstalls.String())
-
-	ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(info.preoptBootDex.Strings(), " "))
-	ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(info.preoptBootLocations, " "))
-	ctx.Strict("PRODUCT_BOOTCLASSPATH", info.bootclasspath)
-	ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", info.systemServerClasspath)
 }
diff --git a/java/dexpreopt_config.go b/java/dexpreopt_config.go
new file mode 100644
index 0000000..409b4b1
--- /dev/null
+++ b/java/dexpreopt_config.go
@@ -0,0 +1,203 @@
+// Copyright 2019 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package java
+
+import (
+	"android/soong/android"
+	"android/soong/dexpreopt"
+	"path/filepath"
+	"strings"
+)
+
+// dexpreoptGlobalConfig returns the global dexpreopt.config.  It is loaded once the first time it is called for any
+// ctx.Config(), and returns the same data for all future calls with the same ctx.Config().  A value can be inserted
+// for tests using setDexpreoptTestGlobalConfig.
+func dexpreoptGlobalConfig(ctx android.PathContext) dexpreopt.GlobalConfig {
+	return ctx.Config().Once(dexpreoptGlobalConfigKey, func() interface{} {
+		if f := ctx.Config().DexpreoptGlobalConfig(); f != "" {
+			ctx.AddNinjaFileDeps(f)
+			globalConfig, err := dexpreopt.LoadGlobalConfig(ctx, f)
+			if err != nil {
+				panic(err)
+			}
+			return globalConfig
+		}
+
+		// No global config filename set, see if there is a test config set
+		return ctx.Config().Once(dexpreoptTestGlobalConfigKey, func() interface{} {
+			// Nope, return a config with preopting disabled
+			return dexpreopt.GlobalConfig{
+				DisablePreopt: true,
+			}
+		})
+	}).(dexpreopt.GlobalConfig)
+}
+
+// setDexpreoptTestGlobalConfig sets a GlobalConfig that future calls to dexpreoptGlobalConfig will return.  It must
+// be called before the first call to dexpreoptGlobalConfig for the config.
+func setDexpreoptTestGlobalConfig(config android.Config, globalConfig dexpreopt.GlobalConfig) {
+	config.Once(dexpreoptTestGlobalConfigKey, func() interface{} { return globalConfig })
+}
+
+var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig")
+var dexpreoptTestGlobalConfigKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
+
+// systemServerClasspath returns the on-device locations of the modules in the system server classpath.  It is computed
+// once the first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same
+// ctx.Config().
+func systemServerClasspath(ctx android.PathContext) []string {
+	return ctx.Config().OnceStringSlice(systemServerClasspathKey, func() []string {
+		global := dexpreoptGlobalConfig(ctx)
+
+		var systemServerClasspathLocations []string
+		for _, m := range global.SystemServerJars {
+			systemServerClasspathLocations = append(systemServerClasspathLocations,
+				filepath.Join("/system/framework", m+".jar"))
+		}
+		return systemServerClasspathLocations
+	})
+}
+
+var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath")
+
+// defaultBootImageConfig returns the bootImageConfig that will be used to dexpreopt modules.  It is computed once the
+// first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same
+// ctx.Config().
+func defaultBootImageConfig(ctx android.PathContext) bootImageConfig {
+	return ctx.Config().Once(defaultBootImageConfigKey, func() interface{} {
+		global := dexpreoptGlobalConfig(ctx)
+
+		runtimeModules := global.RuntimeApexJars
+		nonFrameworkModules := concat(runtimeModules, global.ProductUpdatableBootModules)
+		frameworkModules := android.RemoveListFromList(global.BootJars, nonFrameworkModules)
+
+		var nonUpdatableBootModules []string
+		var nonUpdatableBootLocations []string
+
+		for _, m := range runtimeModules {
+			nonUpdatableBootModules = append(nonUpdatableBootModules, m)
+			nonUpdatableBootLocations = append(nonUpdatableBootLocations,
+				filepath.Join("/apex/com.android.runtime/javalib", m+".jar"))
+		}
+
+		for _, m := range frameworkModules {
+			nonUpdatableBootModules = append(nonUpdatableBootModules, m)
+			nonUpdatableBootLocations = append(nonUpdatableBootLocations,
+				filepath.Join("/system/framework", m+".jar"))
+		}
+
+		// The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
+		// the bootclasspath modules have been compiled.  Set up known paths for them, the singleton rules will copy
+		// them there.
+		// TODO: use module dependencies instead
+		var nonUpdatableBootDexPaths android.WritablePaths
+		for _, m := range nonUpdatableBootModules {
+			nonUpdatableBootDexPaths = append(nonUpdatableBootDexPaths,
+				android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars_input", m+".jar"))
+		}
+
+		dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars")
+		symbolsDir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars_unstripped")
+		images := make(map[android.ArchType]android.OutputPath)
+
+		for _, target := range ctx.Config().Targets[android.Android] {
+			images[target.Arch.ArchType] = dir.Join(ctx,
+				"system/framework", target.Arch.ArchType.String()).Join(ctx, "boot.art")
+		}
+
+		return bootImageConfig{
+			name:         "boot",
+			modules:      nonUpdatableBootModules,
+			dexLocations: nonUpdatableBootLocations,
+			dexPaths:     nonUpdatableBootDexPaths,
+			dir:          dir,
+			symbolsDir:   symbolsDir,
+			images:       images,
+		}
+	}).(bootImageConfig)
+}
+
+var defaultBootImageConfigKey = android.NewOnceKey("defaultBootImageConfig")
+
+func apexBootImageConfig(ctx android.PathContext) bootImageConfig {
+	return ctx.Config().Once(apexBootImageConfigKey, func() interface{} {
+		global := dexpreoptGlobalConfig(ctx)
+
+		runtimeModules := global.RuntimeApexJars
+
+		var runtimeBootLocations []string
+
+		for _, m := range runtimeModules {
+			runtimeBootLocations = append(runtimeBootLocations,
+				filepath.Join("/apex/com.android.runtime/javalib", m+".jar"))
+		}
+
+		// The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
+		// the bootclasspath modules have been compiled.  Set up known paths for them, the singleton rules will copy
+		// them there.
+		// TODO: use module dependencies instead
+		var runtimeBootDexPaths android.WritablePaths
+		for _, m := range runtimeModules {
+			runtimeBootDexPaths = append(runtimeBootDexPaths,
+				android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars_input", m+".jar"))
+		}
+
+		dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars")
+		symbolsDir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars_unstripped")
+		images := make(map[android.ArchType]android.OutputPath)
+
+		for _, target := range ctx.Config().Targets[android.Android] {
+			images[target.Arch.ArchType] = dir.Join(ctx,
+				"system/framework", target.Arch.ArchType.String(), "apex.art")
+		}
+
+		return bootImageConfig{
+			name:         "apex",
+			modules:      runtimeModules,
+			dexLocations: runtimeBootLocations,
+			dexPaths:     runtimeBootDexPaths,
+			dir:          dir,
+			symbolsDir:   symbolsDir,
+			images:       images,
+		}
+	}).(bootImageConfig)
+}
+
+var apexBootImageConfigKey = android.NewOnceKey("apexBootImageConfig")
+
+func defaultBootclasspath(ctx android.PathContext) []string {
+	return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
+		global := dexpreoptGlobalConfig(ctx)
+		image := defaultBootImageConfig(ctx)
+		bootclasspath := append(copyOf(image.dexLocations), global.ProductUpdatableBootLocations...)
+		return bootclasspath
+	})
+}
+
+var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
+
+var copyOf = android.CopyOf
+
+func init() {
+	android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
+}
+
+func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
+	ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
+	ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocations, ":"))
+	ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
+
+	ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
+}
diff --git a/java/hiddenapi.go b/java/hiddenapi.go
index 01e2c5e..104cd76 100644
--- a/java/hiddenapi.go
+++ b/java/hiddenapi.go
@@ -15,8 +15,6 @@
 package java
 
 import (
-	"path/filepath"
-
 	"github.com/google/blueprint"
 
 	"android/soong/android"
@@ -175,14 +173,3 @@
 		TransformZipAlign(ctx, output, tmpOutput)
 	}
 }
-
-type hiddenAPIPath struct {
-	path string
-}
-
-var _ android.Path = (*hiddenAPIPath)(nil)
-
-func (p *hiddenAPIPath) String() string { return p.path }
-func (p *hiddenAPIPath) Ext() string    { return filepath.Ext(p.path) }
-func (p *hiddenAPIPath) Base() string   { return filepath.Base(p.path) }
-func (p *hiddenAPIPath) Rel() string    { return p.path }
diff --git a/java/hiddenapi_singleton.go b/java/hiddenapi_singleton.go
index adbd356..23f6cb0 100644
--- a/java/hiddenapi_singleton.go
+++ b/java/hiddenapi_singleton.go
@@ -111,6 +111,9 @@
 	publicStubModules = append(publicStubModules, ctx.Config().ProductHiddenAPIStubs()...)
 	systemStubModules = append(systemStubModules, ctx.Config().ProductHiddenAPIStubsSystem()...)
 	testStubModules = append(testStubModules, ctx.Config().ProductHiddenAPIStubsTest()...)
+	if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") {
+		publicStubModules = append(publicStubModules, "jacoco-stubs")
+	}
 
 	publicStubPaths := make(android.Paths, len(publicStubModules))
 	systemStubPaths := make(android.Paths, len(systemStubModules))
@@ -170,14 +173,14 @@
 	rule.MissingDeps(missingDeps)
 
 	rule.Command().
-		Tool(pctx.HostBinToolPath(ctx, "hiddenapi").String()).
+		Tool(pctx.HostBinToolPath(ctx, "hiddenapi")).
 		Text("list").
-		FlagForEachInput("--boot-dex=", bootDexJars.Strings()).
-		FlagWithInputList("--public-stub-classpath=", publicStubPaths.Strings(), ":").
-		FlagWithInputList("--public-stub-classpath=", systemStubPaths.Strings(), ":").
-		FlagWithInputList("--public-stub-classpath=", testStubPaths.Strings(), ":").
-		FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths.Strings(), ":").
-		FlagWithOutput("--out-api-flags=", tempPath.String())
+		FlagForEachInput("--boot-dex=", bootDexJars).
+		FlagWithInputList("--public-stub-classpath=", publicStubPaths, ":").
+		FlagWithInputList("--public-stub-classpath=", systemStubPaths, ":").
+		FlagWithInputList("--public-stub-classpath=", testStubPaths, ":").
+		FlagWithInputList("--core-platform-stub-classpath=", corePlatformStubPaths, ":").
+		FlagWithOutput("--out-api-flags=", tempPath)
 
 	commitChangeForRestat(rule, tempPath, outputPath)
 
@@ -214,20 +217,20 @@
 	stubFlags := hiddenAPISingletonPaths(ctx).stubFlags
 
 	rule.Command().
-		Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/generate_hiddenapi_lists.py").String()).
-		FlagWithInput("--csv ", stubFlags.String()).
-		Inputs(flagsCSV.Strings()).
+		Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/generate_hiddenapi_lists.py")).
+		FlagWithInput("--csv ", stubFlags).
+		Inputs(flagsCSV).
 		FlagWithInput("--greylist ",
-			android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist.txt").String()).
+			android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist.txt")).
 		FlagWithInput("--greylist-ignore-conflicts ",
-			greylistIgnoreConflicts.String()).
+			greylistIgnoreConflicts).
 		FlagWithInput("--greylist-max-p ",
-			android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-p.txt").String()).
+			android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-p.txt")).
 		FlagWithInput("--greylist-max-o-ignore-conflicts ",
-			android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-o.txt").String()).
+			android.PathForSource(ctx, "frameworks/base/config/hiddenapi-greylist-max-o.txt")).
 		FlagWithInput("--blacklist ",
-			android.PathForSource(ctx, "frameworks/base/config/hiddenapi-force-blacklist.txt").String()).
-		FlagWithOutput("--output ", tempPath.String())
+			android.PathForSource(ctx, "frameworks/base/config/hiddenapi-force-blacklist.txt")).
+		FlagWithOutput("--output ", tempPath)
 
 	commitChangeForRestat(rule, tempPath, outputPath)
 
@@ -243,8 +246,8 @@
 
 	outputPath := hiddenAPISingletonPaths(ctx).flags
 
-	rule.Command().Text("rm").Flag("-f").Output(outputPath.String())
-	rule.Command().Text("touch").Output(outputPath.String())
+	rule.Command().Text("rm").Flag("-f").Output(outputPath)
+	rule.Command().Text("touch").Output(outputPath)
 
 	rule.Build(pctx, ctx, "emptyHiddenAPIFlagsFile", "empty hiddenapi flags")
 
@@ -269,10 +272,10 @@
 	outputPath := hiddenAPISingletonPaths(ctx).metadata
 
 	rule.Command().
-		Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/merge_csv.py").String()).
-		Inputs(metadataCSV.Strings()).
+		Tool(android.PathForSource(ctx, "frameworks/base/tools/hiddenapi/merge_csv.py")).
+		Inputs(metadataCSV).
 		Text(">").
-		Output(outputPath.String())
+		Output(outputPath)
 
 	rule.Build(pctx, ctx, "hiddenAPIGreylistMetadataFile", "hiddenapi greylist metadata")
 
@@ -284,15 +287,15 @@
 // the rule.
 func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) {
 	rule.Restat()
-	rule.Temporary(tempPath.String())
+	rule.Temporary(tempPath)
 	rule.Command().
 		Text("(").
 		Text("if").
-		Text("cmp -s").Input(tempPath.String()).Output(outputPath.String()).Text(";").
+		Text("cmp -s").Input(tempPath).Output(outputPath).Text(";").
 		Text("then").
-		Text("rm").Input(tempPath.String()).Text(";").
+		Text("rm").Input(tempPath).Text(";").
 		Text("else").
-		Text("mv").Input(tempPath.String()).Output(outputPath.String()).Text(";").
+		Text("mv").Input(tempPath).Output(outputPath).Text(";").
 		Text("fi").
 		Text(")")
 }
diff --git a/java/testing.go b/java/testing.go
index 6febfa1..6c4020c 100644
--- a/java/testing.go
+++ b/java/testing.go
@@ -18,6 +18,7 @@
 	"fmt"
 
 	"android/soong/android"
+	"android/soong/dexpreopt"
 )
 
 func TestConfig(buildDir string, env map[string]string) android.Config {
@@ -30,6 +31,9 @@
 	config := android.TestArchConfig(buildDir, env)
 	config.TestProductVariables.DeviceSystemSdkVersions = []string{"14", "15"}
 
+	pathCtx := android.PathContextForTesting(config, nil)
+	setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
+
 	return config
 }
 
diff --git a/sysprop/sysprop_library.go b/sysprop/sysprop_library.go
index 4069e78..643fe8e 100644
--- a/sysprop/sysprop_library.go
+++ b/sysprop/sysprop_library.go
@@ -42,9 +42,10 @@
 }
 
 type commonProperties struct {
-	Srcs             []string
-	Recovery         *bool
-	Vendor_available *bool
+	Srcs               []string
+	Recovery           *bool
+	Recovery_available *bool
+	Vendor_available   *bool
 }
 
 var (
diff --git a/sysprop/sysprop_test.go b/sysprop/sysprop_test.go
index 745e424..79b0f4e 100644
--- a/sysprop/sysprop_test.go
+++ b/sysprop/sysprop_test.go
@@ -73,6 +73,7 @@
 	})
 
 	ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
+	ctx.RegisterModuleType("cc_library_static", android.ModuleFactoryAdaptor(cc.LibraryFactory))
 	ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
 	ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
 	ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
@@ -251,6 +252,12 @@
 			static_libs: ["sysprop-platform"],
 		}
 
+		cc_library_static {
+			name: "cc-client-platform-static",
+			srcs: ["d.cpp"],
+			whole_static_libs: ["sysprop-platform"],
+		}
+
 		cc_library {
 			name: "cc-client-product",
 			srcs: ["d.cpp"],
@@ -300,12 +307,21 @@
 	platformClient := ctx.ModuleForTests("cc-client-platform", coreVariant)
 	platformFlags := platformClient.Rule("cc").Args["cFlags"]
 
-	// Platform should use platform's internal header
+	// platform should use platform's internal header
 	if !strings.Contains(platformFlags, platformInternalPath) {
 		t.Errorf("flags for platform must contain %#v, but was %#v.",
 			platformInternalPath, platformFlags)
 	}
 
+	platformStaticClient := ctx.ModuleForTests("cc-client-platform-static", coreVariant)
+	platformStaticFlags := platformStaticClient.Rule("cc").Args["cFlags"]
+
+	// platform-static should use platform's internal header
+	if !strings.Contains(platformStaticFlags, platformInternalPath) {
+		t.Errorf("flags for platform-static must contain %#v, but was %#v.",
+			platformInternalPath, platformStaticFlags)
+	}
+
 	productClient := ctx.ModuleForTests("cc-client-product", coreVariant)
 	productFlags := productClient.Rule("cc").Args["cFlags"]