Merge "Revert "[strip.sh] Move remaining GNU binutils usage to llvm binutils""
diff --git a/android/Android.bp b/android/Android.bp
index 9f6ae02..2406321 100644
--- a/android/Android.bp
+++ b/android/Android.bp
@@ -13,6 +13,7 @@
         "soong-android-soongconfig",
         "soong-bazel",
         "soong-cquery",
+        "soong-remoteexec",
         "soong-shared",
         "soong-ui-metrics_proto",
     ],
diff --git a/android/android_test.go b/android/android_test.go
index 68cb705..2a697fb 100644
--- a/android/android_test.go
+++ b/android/android_test.go
@@ -15,34 +15,12 @@
 package android
 
 import (
-	"io/ioutil"
 	"os"
 	"testing"
 )
 
-var buildDir string
-
-func setUp() {
-	var err error
-	buildDir, err = ioutil.TempDir("", "soong_android_test")
-	if err != nil {
-		panic(err)
-	}
-}
-
-func tearDown() {
-	os.RemoveAll(buildDir)
-}
-
 func TestMain(m *testing.M) {
-	run := func() int {
-		setUp()
-		defer tearDown()
-
-		return m.Run()
-	}
-
-	os.Exit(run())
+	os.Exit(m.Run())
 }
 
-var emptyTestFixtureFactory = NewFixtureFactory(&buildDir)
+var emptyTestFixtureFactory = NewFixtureFactory(nil)
diff --git a/android/config.go b/android/config.go
index e207ca8..19706f5 100644
--- a/android/config.go
+++ b/android/config.go
@@ -35,6 +35,7 @@
 	"github.com/google/blueprint/proptools"
 
 	"android/soong/android/soongconfig"
+	"android/soong/remoteexec"
 )
 
 // Bool re-exports proptools.Bool for the android package.
@@ -1774,3 +1775,7 @@
 func (c *config) UpdatableBootJars() ConfiguredJarList {
 	return c.productVariables.UpdatableBootJars
 }
+
+func (c *config) RBEWrapper() string {
+	return c.GetenvWithDefault("RBE_WRAPPER", remoteexec.DefaultWrapperPath)
+}
diff --git a/android/defs.go b/android/defs.go
index 1a7c459..b3ff376 100644
--- a/android/defs.go
+++ b/android/defs.go
@@ -124,6 +124,10 @@
 
 func init() {
 	pctx.Import("github.com/google/blueprint/bootstrap")
+
+	pctx.VariableFunc("RBEWrapper", func(ctx PackageVarContext) string {
+		return ctx.Config().RBEWrapper()
+	})
 }
 
 var (
diff --git a/android/package_ctx.go b/android/package_ctx.go
index 6d0fcb3..c19debb 100644
--- a/android/package_ctx.go
+++ b/android/package_ctx.go
@@ -19,6 +19,8 @@
 	"strings"
 
 	"github.com/google/blueprint"
+
+	"android/soong/remoteexec"
 )
 
 // PackageContext is a wrapper for blueprint.PackageContext that adds
@@ -260,3 +262,40 @@
 		return params, nil
 	}, argNames...)
 }
+
+// RemoteStaticRules returns a pair of rules based on the given RuleParams, where the first rule is a
+// locally executable rule and the second rule is a remotely executable rule. commonArgs are args
+// used for both the local and remotely executable rules. reArgs are used only for remote
+// execution.
+func (p PackageContext) RemoteStaticRules(name string, ruleParams blueprint.RuleParams, reParams *remoteexec.REParams, commonArgs []string, reArgs []string) (blueprint.Rule, blueprint.Rule) {
+	ruleParamsRE := ruleParams
+	ruleParams.Command = strings.ReplaceAll(ruleParams.Command, "$reTemplate", "")
+	ruleParamsRE.Command = strings.ReplaceAll(ruleParamsRE.Command, "$reTemplate", reParams.Template())
+
+	return p.AndroidStaticRule(name, ruleParams, commonArgs...),
+		p.AndroidRemoteStaticRule(name+"RE", RemoteRuleSupports{RBE: true}, ruleParamsRE, append(commonArgs, reArgs...)...)
+}
+
+// MultiCommandStaticRules returns a pair of rules based on the given RuleParams, where the first
+// rule is a locally executable rule and the second rule is a remotely executable rule. This
+// function supports multiple remote execution wrappers placed in the template when commands are
+// chained together with &&. commonArgs are args used for both the local and remotely executable
+// rules. reArgs are args used only for remote execution.
+func (p PackageContext) MultiCommandRemoteStaticRules(name string, ruleParams blueprint.RuleParams, reParams map[string]*remoteexec.REParams, commonArgs []string, reArgs []string) (blueprint.Rule, blueprint.Rule) {
+	ruleParamsRE := ruleParams
+	for k, v := range reParams {
+		ruleParams.Command = strings.ReplaceAll(ruleParams.Command, k, "")
+		ruleParamsRE.Command = strings.ReplaceAll(ruleParamsRE.Command, k, v.Template())
+	}
+
+	return p.AndroidStaticRule(name, ruleParams, commonArgs...),
+		p.AndroidRemoteStaticRule(name+"RE", RemoteRuleSupports{RBE: true}, ruleParamsRE, append(commonArgs, reArgs...)...)
+}
+
+// StaticVariableWithEnvOverride creates a static variable that evaluates to the value of the given
+// environment variable if set, otherwise the given default.
+func (p PackageContext) StaticVariableWithEnvOverride(name, envVar, defaultVal string) blueprint.Variable {
+	return p.VariableFunc(name, func(ctx PackageVarContext) string {
+		return ctx.Config().GetenvWithDefault(envVar, defaultVal)
+	})
+}
diff --git a/android/path_properties_test.go b/android/path_properties_test.go
index 85c96ee..8726ea7 100644
--- a/android/path_properties_test.go
+++ b/android/path_properties_test.go
@@ -15,7 +15,6 @@
 package android
 
 import (
-	"reflect"
 	"testing"
 )
 
@@ -158,23 +157,18 @@
 				}
 			`
 
-			config := TestArchConfig(buildDir, nil, bp, nil)
-			ctx := NewTestArchContext(config)
+			result := emptyTestFixtureFactory.RunTest(t,
+				PrepareForTestWithArchMutator,
+				PrepareForTestWithFilegroup,
+				FixtureRegisterWithContext(func(ctx RegistrationContext) {
+					ctx.RegisterModuleType("test", pathDepsMutatorTestModuleFactory)
+				}),
+				FixtureWithRootAndroidBp(bp),
+			)
 
-			ctx.RegisterModuleType("test", pathDepsMutatorTestModuleFactory)
-			ctx.RegisterModuleType("filegroup", FileGroupFactory)
+			m := result.Module("foo", "android_arm64_armv8-a").(*pathDepsMutatorTestModule)
 
-			ctx.Register()
-			_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
-			FailIfErrored(t, errs)
-			_, errs = ctx.PrepareBuildActions(config)
-			FailIfErrored(t, errs)
-
-			m := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*pathDepsMutatorTestModule)
-
-			if g, w := m.sourceDeps, test.deps; !reflect.DeepEqual(g, w) {
-				t.Errorf("want deps %q, got %q", w, g)
-			}
+			AssertDeepEquals(t, "deps", test.deps, m.sourceDeps)
 		})
 	}
 }
diff --git a/android/rule_builder.go b/android/rule_builder.go
index 75f1b5d..0d8e2b7 100644
--- a/android/rule_builder.go
+++ b/android/rule_builder.go
@@ -27,6 +27,7 @@
 	"github.com/google/blueprint/proptools"
 
 	"android/soong/cmd/sbox/sbox_proto"
+	"android/soong/remoteexec"
 	"android/soong/shared"
 )
 
@@ -48,8 +49,10 @@
 	sbox             bool
 	highmem          bool
 	remoteable       RemoteRuleSupports
+	rbeParams        *remoteexec.REParams
 	outDir           WritablePath
 	sboxTools        bool
+	sboxInputs       bool
 	sboxManifestPath WritablePath
 	missingDeps      []string
 }
@@ -119,6 +122,18 @@
 	return r
 }
 
+// Rewrapper marks the rule as running inside rewrapper using the given params in order to support
+// running on RBE.  During RuleBuilder.Build the params will be combined with the inputs, outputs
+// and tools known to RuleBuilder to prepend an appropriate rewrapper command line to the rule's
+// command line.
+func (r *RuleBuilder) Rewrapper(params *remoteexec.REParams) *RuleBuilder {
+	if !r.sboxInputs {
+		panic(fmt.Errorf("RuleBuilder.Rewrapper must be called after RuleBuilder.SandboxInputs"))
+	}
+	r.rbeParams = params
+	return r
+}
+
 // Sbox marks the rule as needing to be wrapped by sbox. The outputDir should point to the output
 // directory that sbox will wipe. It should not be written to by any other rule. manifestPath should
 // point to a location where sbox's manifest will be written and must be outside outputDir. sbox
@@ -155,6 +170,25 @@
 	return r
 }
 
+// SandboxInputs enables input sandboxing for the rule by copying any referenced inputs into the
+// sandbox.  It also implies SandboxTools().
+//
+// Sandboxing inputs requires RuleBuilder to be aware of all references to input paths.  Paths
+// that are passed to RuleBuilder outside of the methods that expect inputs, for example
+// FlagWithArg, must use RuleBuilderCommand.PathForInput to translate the path to one that matches
+// the sandbox layout.
+func (r *RuleBuilder) SandboxInputs() *RuleBuilder {
+	if !r.sbox {
+		panic("SandboxInputs() must be called after Sbox()")
+	}
+	if len(r.commands) > 0 {
+		panic("SandboxInputs() may not be called after Command()")
+	}
+	r.sboxTools = true
+	r.sboxInputs = true
+	return r
+}
+
 // Install associates an output of the rule with an install location, which can be retrieved later using
 // RuleBuilder.Installs.
 func (r *RuleBuilder) Install(from Path, to string) {
@@ -425,6 +459,26 @@
 		Inputs(depFiles.Paths())
 }
 
+// composeRspFileContent returns a string that will serve as the contents of the rsp file to pass
+// the listed input files to the command running in the sandbox.
+func (r *RuleBuilder) composeRspFileContent(rspFileInputs Paths) string {
+	if r.sboxInputs {
+		if len(rspFileInputs) > 0 {
+			// When SandboxInputs is used the paths need to be rewritten to be relative to the sandbox
+			// directory so that they are valid after sbox chdirs into the sandbox directory.
+			return proptools.NinjaEscape(strings.Join(r.sboxPathsForInputsRel(rspFileInputs), " "))
+		} else {
+			// If the list of inputs is empty fall back to "$in" so that the rspfilecontent Ninja
+			// variable is set to something non-empty, otherwise ninja will complain.  The inputs
+			// will be empty (all the non-rspfile inputs are implicits), so $in will evaluate to
+			// an empty string.
+			return "$in"
+		}
+	} else {
+		return "$in"
+	}
+}
+
 // 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(name string, desc string) {
@@ -511,6 +565,27 @@
 			}
 		}
 
+		// If sandboxing inputs is enabled, add copy rules to the manifest to copy each input
+		// into the sbox directory.
+		if r.sboxInputs {
+			for _, input := range inputs {
+				command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{
+					From: proto.String(input.String()),
+					To:   proto.String(r.sboxPathForInputRel(input)),
+				})
+			}
+
+			// If using an rsp file copy it into the sbox directory.
+			if rspFilePath != nil {
+				command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{
+					From: proto.String(rspFilePath.String()),
+					To:   proto.String(r.sboxPathForInputRel(rspFilePath)),
+				})
+			}
+
+			command.Chdir = proto.Bool(true)
+		}
+
 		// Add copy rules to the manifest to copy each output file from the sbox directory.
 		// to the output directory after running the commands.
 		sboxOutputs := make([]string, len(outputs))
@@ -564,6 +639,25 @@
 		commandString = sboxCmd.buf.String()
 		tools = append(tools, sboxCmd.tools...)
 		inputs = append(inputs, sboxCmd.inputs...)
+
+		if r.rbeParams != nil {
+			var remoteInputs []string
+			remoteInputs = append(remoteInputs, inputs.Strings()...)
+			remoteInputs = append(remoteInputs, tools.Strings()...)
+			remoteInputs = append(remoteInputs, rspFileInputs.Strings()...)
+			if rspFilePath != nil {
+				remoteInputs = append(remoteInputs, rspFilePath.String())
+			}
+			inputsListFile := r.sboxManifestPath.ReplaceExtension(r.ctx, "rbe_inputs.list")
+			inputsListContents := rspFileForInputs(remoteInputs)
+			WriteFileRule(r.ctx, inputsListFile, inputsListContents)
+			inputs = append(inputs, inputsListFile)
+
+			r.rbeParams.OutputFiles = outputs.Strings()
+			r.rbeParams.RSPFile = inputsListFile.String()
+			rewrapperCommand := r.rbeParams.NoVarTemplate(r.ctx.Config().RBEWrapper())
+			commandString = rewrapperCommand + " bash -c '" + strings.ReplaceAll(commandString, `'`, `'\''`) + "'"
+		}
 	} else {
 		// If not using sbox the rule will run the command directly, put the hash of the
 		// list of input files in a comment at the end of the command line to ensure ninja
@@ -580,7 +674,7 @@
 	var rspFile, rspFileContent string
 	if rspFilePath != nil {
 		rspFile = rspFilePath.String()
-		rspFileContent = "$in"
+		rspFileContent = r.composeRspFileContent(rspFileInputs)
 	}
 
 	var pool blueprint.Pool
@@ -636,29 +730,45 @@
 }
 
 func (c *RuleBuilderCommand) addInput(path Path) string {
-	if c.rule.sbox {
-		if rel, isRel, _ := maybeRelErr(c.rule.outDir.String(), path.String()); isRel {
-			return filepath.Join(sboxOutDir, rel)
-		}
-	}
 	c.inputs = append(c.inputs, path)
-	return path.String()
+	return c.PathForInput(path)
 }
 
-func (c *RuleBuilderCommand) addImplicit(path Path) string {
-	if c.rule.sbox {
-		if rel, isRel, _ := maybeRelErr(c.rule.outDir.String(), path.String()); isRel {
-			return filepath.Join(sboxOutDir, rel)
-		}
-	}
+func (c *RuleBuilderCommand) addImplicit(path Path) {
 	c.implicits = append(c.implicits, path)
-	return path.String()
 }
 
 func (c *RuleBuilderCommand) addOrderOnly(path Path) {
 	c.orderOnlys = append(c.orderOnlys, path)
 }
 
+// PathForInput takes an input path and returns the appropriate path to use on the command line.  If
+// sbox was enabled via a call to RuleBuilder.Sbox() and the path was an output path it returns a
+// path with the placeholder prefix used for outputs in sbox.  If sbox is not enabled it returns the
+// original path.
+func (c *RuleBuilderCommand) PathForInput(path Path) string {
+	if c.rule.sbox {
+		rel, inSandbox := c.rule._sboxPathForInputRel(path)
+		if inSandbox {
+			rel = filepath.Join(sboxSandboxBaseDir, rel)
+		}
+		return rel
+	}
+	return path.String()
+}
+
+// PathsForInputs takes a list of input paths and returns the appropriate paths to use on the
+// command line.  If sbox was enabled via a call to RuleBuilder.Sbox() a path was an output path, it
+// returns the path with the placeholder prefix used for outputs in sbox.  If sbox is not enabled it
+// returns the original paths.
+func (c *RuleBuilderCommand) PathsForInputs(paths Paths) []string {
+	ret := make([]string, len(paths))
+	for i, path := range paths {
+		ret[i] = c.PathForInput(path)
+	}
+	return ret
+}
+
 // PathForOutput takes an output path and returns the appropriate path to use on the command
 // line.  If sbox was enabled via a call to RuleBuilder.Sbox(), it returns a path with the
 // placeholder prefix used for outputs in sbox.  If sbox is not enabled it returns the
@@ -690,6 +800,37 @@
 	return filepath.Join(sboxToolsSubDir, "src", path.String())
 }
 
+func (r *RuleBuilder) _sboxPathForInputRel(path Path) (rel string, inSandbox bool) {
+	// Errors will be handled in RuleBuilder.Build where we have a context to report them
+	rel, isRelSboxOut, _ := maybeRelErr(r.outDir.String(), path.String())
+	if isRelSboxOut {
+		return filepath.Join(sboxOutSubDir, rel), true
+	}
+	if r.sboxInputs {
+		// When sandboxing inputs all inputs have to be copied into the sandbox.  Input files that
+		// are outputs of other rules could be an arbitrary absolute path if OUT_DIR is set, so they
+		// will be copied to relative paths under __SBOX_OUT_DIR__/out.
+		rel, isRelOut, _ := maybeRelErr(PathForOutput(r.ctx).String(), path.String())
+		if isRelOut {
+			return filepath.Join(sboxOutSubDir, rel), true
+		}
+	}
+	return path.String(), false
+}
+
+func (r *RuleBuilder) sboxPathForInputRel(path Path) string {
+	rel, _ := r._sboxPathForInputRel(path)
+	return rel
+}
+
+func (r *RuleBuilder) sboxPathsForInputsRel(paths Paths) []string {
+	ret := make([]string, len(paths))
+	for i, path := range paths {
+		ret[i] = r.sboxPathForInputRel(path)
+	}
+	return ret
+}
+
 // SboxPathForPackagedTool takes a PackageSpec for a tool and returns the corresponding path for the
 // tool after copying it into the sandbox.  This can be used  on the RuleBuilder command line to
 // reference the tool.
@@ -1053,7 +1194,7 @@
 		}
 	}
 
-	c.FlagWithArg(flag, rspFile.String())
+	c.FlagWithArg(flag, c.PathForInput(rspFile))
 	return c
 }
 
@@ -1122,3 +1263,14 @@
 	return nil
 }
 func (builderContextForTests) Build(PackageContext, BuildParams) {}
+
+func rspFileForInputs(paths []string) string {
+	s := strings.Builder{}
+	for i, path := range paths {
+		if i != 0 {
+			s.WriteByte(' ')
+		}
+		s.WriteString(proptools.ShellEscape(path))
+	}
+	return s.String()
+}
diff --git a/android/rule_builder_test.go b/android/rule_builder_test.go
index bd35820..3415aed 100644
--- a/android/rule_builder_test.go
+++ b/android/rule_builder_test.go
@@ -296,35 +296,40 @@
 		"input3":     nil,
 	}
 
-	pathCtx := PathContextForTesting(TestConfig("out", nil, "", fs))
+	pathCtx := PathContextForTesting(TestConfig("out_local", nil, "", fs))
 	ctx := builderContextForTests{
 		PathContext: pathCtx,
 	}
 
 	addCommands := func(rule *RuleBuilder) {
 		cmd := rule.Command().
-			DepFile(PathForOutput(ctx, "DepFile")).
+			DepFile(PathForOutput(ctx, "module/DepFile")).
 			Flag("Flag").
 			FlagWithArg("FlagWithArg=", "arg").
-			FlagWithDepFile("FlagWithDepFile=", PathForOutput(ctx, "depfile")).
+			FlagWithDepFile("FlagWithDepFile=", PathForOutput(ctx, "module/depfile")).
 			FlagWithInput("FlagWithInput=", PathForSource(ctx, "input")).
-			FlagWithOutput("FlagWithOutput=", PathForOutput(ctx, "output")).
+			FlagWithOutput("FlagWithOutput=", PathForOutput(ctx, "module/output")).
+			FlagWithRspFileInputList("FlagWithRspFileInputList=", PathForOutput(ctx, "rsp"),
+				Paths{
+					PathForSource(ctx, "RspInput"),
+					PathForOutput(ctx, "other/RspOutput2"),
+				}).
 			Implicit(PathForSource(ctx, "Implicit")).
-			ImplicitDepFile(PathForOutput(ctx, "ImplicitDepFile")).
-			ImplicitOutput(PathForOutput(ctx, "ImplicitOutput")).
+			ImplicitDepFile(PathForOutput(ctx, "module/ImplicitDepFile")).
+			ImplicitOutput(PathForOutput(ctx, "module/ImplicitOutput")).
 			Input(PathForSource(ctx, "Input")).
-			Output(PathForOutput(ctx, "Output")).
+			Output(PathForOutput(ctx, "module/Output")).
 			OrderOnly(PathForSource(ctx, "OrderOnly")).
-			SymlinkOutput(PathForOutput(ctx, "SymlinkOutput")).
-			ImplicitSymlinkOutput(PathForOutput(ctx, "ImplicitSymlinkOutput")).
+			SymlinkOutput(PathForOutput(ctx, "module/SymlinkOutput")).
+			ImplicitSymlinkOutput(PathForOutput(ctx, "module/ImplicitSymlinkOutput")).
 			Text("Text").
 			Tool(PathForSource(ctx, "Tool"))
 
 		rule.Command().
 			Text("command2").
-			DepFile(PathForOutput(ctx, "depfile2")).
+			DepFile(PathForOutput(ctx, "module/depfile2")).
 			Input(PathForSource(ctx, "input2")).
-			Output(PathForOutput(ctx, "output2")).
+			Output(PathForOutput(ctx, "module/output2")).
 			OrderOnlys(PathsForSource(ctx, []string{"OrderOnlys"})).
 			Tool(PathForSource(ctx, "tool2"))
 
@@ -337,32 +342,46 @@
 		rule.Command().
 			Text("command3").
 			Input(PathForSource(ctx, "input3")).
-			Input(PathForOutput(ctx, "output2")).
-			Output(PathForOutput(ctx, "output3"))
+			Input(PathForOutput(ctx, "module/output2")).
+			Output(PathForOutput(ctx, "module/output3")).
+			Text(cmd.PathForInput(PathForSource(ctx, "input3"))).
+			Text(cmd.PathForOutput(PathForOutput(ctx, "module/output2")))
 	}
 
 	wantInputs := PathsForSource(ctx, []string{"Implicit", "Input", "input", "input2", "input3"})
-	wantOutputs := PathsForOutput(ctx, []string{"ImplicitOutput", "ImplicitSymlinkOutput", "Output", "SymlinkOutput", "output", "output2", "output3"})
-	wantDepFiles := PathsForOutput(ctx, []string{"DepFile", "depfile", "ImplicitDepFile", "depfile2"})
+	wantRspFileInputs := Paths{PathForSource(ctx, "RspInput"),
+		PathForOutput(ctx, "other/RspOutput2")}
+	wantOutputs := PathsForOutput(ctx, []string{
+		"module/ImplicitOutput", "module/ImplicitSymlinkOutput", "module/Output", "module/SymlinkOutput",
+		"module/output", "module/output2", "module/output3"})
+	wantDepFiles := PathsForOutput(ctx, []string{
+		"module/DepFile", "module/depfile", "module/ImplicitDepFile", "module/depfile2"})
 	wantTools := PathsForSource(ctx, []string{"Tool", "tool2"})
 	wantOrderOnlys := PathsForSource(ctx, []string{"OrderOnly", "OrderOnlys"})
-	wantSymlinkOutputs := PathsForOutput(ctx, []string{"ImplicitSymlinkOutput", "SymlinkOutput"})
+	wantSymlinkOutputs := PathsForOutput(ctx, []string{
+		"module/ImplicitSymlinkOutput", "module/SymlinkOutput"})
 
 	t.Run("normal", func(t *testing.T) {
 		rule := NewRuleBuilder(pctx, ctx)
 		addCommands(rule)
 
 		wantCommands := []string{
-			"out/DepFile Flag FlagWithArg=arg FlagWithDepFile=out/depfile FlagWithInput=input FlagWithOutput=out/output Input out/Output out/SymlinkOutput Text Tool after command2 old cmd",
-			"command2 out/depfile2 input2 out/output2 tool2",
-			"command3 input3 out/output2 out/output3",
+			"out_local/module/DepFile Flag FlagWithArg=arg FlagWithDepFile=out_local/module/depfile " +
+				"FlagWithInput=input FlagWithOutput=out_local/module/output FlagWithRspFileInputList=out_local/rsp " +
+				"Input out_local/module/Output out_local/module/SymlinkOutput Text Tool after command2 old cmd",
+			"command2 out_local/module/depfile2 input2 out_local/module/output2 tool2",
+			"command3 input3 out_local/module/output2 out_local/module/output3 input3 out_local/module/output2",
 		}
 
-		wantDepMergerCommand := "out/host/" + ctx.Config().PrebuiltOS() + "/bin/dep_fixer out/DepFile out/depfile out/ImplicitDepFile out/depfile2"
+		wantDepMergerCommand := "out_local/host/" + ctx.Config().PrebuiltOS() + "/bin/dep_fixer " +
+			"out_local/module/DepFile out_local/module/depfile out_local/module/ImplicitDepFile out_local/module/depfile2"
+
+		wantRspFileContent := "$in"
 
 		AssertDeepEquals(t, "rule.Commands()", wantCommands, rule.Commands())
 
 		AssertDeepEquals(t, "rule.Inputs()", wantInputs, rule.Inputs())
+		AssertDeepEquals(t, "rule.RspfileInputs()", wantRspFileInputs, rule.RspFileInputs())
 		AssertDeepEquals(t, "rule.Outputs()", wantOutputs, rule.Outputs())
 		AssertDeepEquals(t, "rule.SymlinkOutputs()", wantSymlinkOutputs, rule.SymlinkOutputs())
 		AssertDeepEquals(t, "rule.DepFiles()", wantDepFiles, rule.DepFiles())
@@ -370,54 +389,74 @@
 		AssertDeepEquals(t, "rule.OrderOnlys()", wantOrderOnlys, rule.OrderOnlys())
 
 		AssertSame(t, "rule.depFileMergerCmd()", wantDepMergerCommand, rule.depFileMergerCmd(rule.DepFiles()).String())
+
+		AssertSame(t, "rule.composeRspFileContent()", wantRspFileContent, rule.composeRspFileContent(rule.RspFileInputs()))
 	})
 
 	t.Run("sbox", func(t *testing.T) {
-		rule := NewRuleBuilder(pctx, ctx).Sbox(PathForOutput(ctx, ""),
+		rule := NewRuleBuilder(pctx, ctx).Sbox(PathForOutput(ctx, "module"),
 			PathForOutput(ctx, "sbox.textproto"))
 		addCommands(rule)
 
 		wantCommands := []string{
-			"__SBOX_SANDBOX_DIR__/out/DepFile Flag FlagWithArg=arg FlagWithDepFile=__SBOX_SANDBOX_DIR__/out/depfile FlagWithInput=input FlagWithOutput=__SBOX_SANDBOX_DIR__/out/output Input __SBOX_SANDBOX_DIR__/out/Output __SBOX_SANDBOX_DIR__/out/SymlinkOutput Text Tool after command2 old cmd",
+			"__SBOX_SANDBOX_DIR__/out/DepFile Flag FlagWithArg=arg FlagWithDepFile=__SBOX_SANDBOX_DIR__/out/depfile " +
+				"FlagWithInput=input FlagWithOutput=__SBOX_SANDBOX_DIR__/out/output " +
+				"FlagWithRspFileInputList=out_local/rsp Input __SBOX_SANDBOX_DIR__/out/Output " +
+				"__SBOX_SANDBOX_DIR__/out/SymlinkOutput Text Tool after command2 old cmd",
 			"command2 __SBOX_SANDBOX_DIR__/out/depfile2 input2 __SBOX_SANDBOX_DIR__/out/output2 tool2",
-			"command3 input3 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/out/output3",
+			"command3 input3 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/out/output3 input3 __SBOX_SANDBOX_DIR__/out/output2",
 		}
 
-		wantDepMergerCommand := "out/host/" + ctx.Config().PrebuiltOS() + "/bin/dep_fixer __SBOX_SANDBOX_DIR__/out/DepFile __SBOX_SANDBOX_DIR__/out/depfile __SBOX_SANDBOX_DIR__/out/ImplicitDepFile __SBOX_SANDBOX_DIR__/out/depfile2"
+		wantDepMergerCommand := "out_local/host/" + ctx.Config().PrebuiltOS() + "/bin/dep_fixer __SBOX_SANDBOX_DIR__/out/DepFile __SBOX_SANDBOX_DIR__/out/depfile __SBOX_SANDBOX_DIR__/out/ImplicitDepFile __SBOX_SANDBOX_DIR__/out/depfile2"
+
+		wantRspFileContent := "$in"
 
 		AssertDeepEquals(t, "rule.Commands()", wantCommands, rule.Commands())
 
 		AssertDeepEquals(t, "rule.Inputs()", wantInputs, rule.Inputs())
+		AssertDeepEquals(t, "rule.RspfileInputs()", wantRspFileInputs, rule.RspFileInputs())
 		AssertDeepEquals(t, "rule.Outputs()", wantOutputs, rule.Outputs())
+		AssertDeepEquals(t, "rule.SymlinkOutputs()", wantSymlinkOutputs, rule.SymlinkOutputs())
 		AssertDeepEquals(t, "rule.DepFiles()", wantDepFiles, rule.DepFiles())
 		AssertDeepEquals(t, "rule.Tools()", wantTools, rule.Tools())
 		AssertDeepEquals(t, "rule.OrderOnlys()", wantOrderOnlys, rule.OrderOnlys())
 
 		AssertSame(t, "rule.depFileMergerCmd()", wantDepMergerCommand, rule.depFileMergerCmd(rule.DepFiles()).String())
+
+		AssertSame(t, "rule.composeRspFileContent()", wantRspFileContent, rule.composeRspFileContent(rule.RspFileInputs()))
 	})
 
 	t.Run("sbox tools", func(t *testing.T) {
-		rule := NewRuleBuilder(pctx, ctx).Sbox(PathForOutput(ctx, ""),
+		rule := NewRuleBuilder(pctx, ctx).Sbox(PathForOutput(ctx, "module"),
 			PathForOutput(ctx, "sbox.textproto")).SandboxTools()
 		addCommands(rule)
 
 		wantCommands := []string{
-			"__SBOX_SANDBOX_DIR__/out/DepFile Flag FlagWithArg=arg FlagWithDepFile=__SBOX_SANDBOX_DIR__/out/depfile FlagWithInput=input FlagWithOutput=__SBOX_SANDBOX_DIR__/out/output Input __SBOX_SANDBOX_DIR__/out/Output __SBOX_SANDBOX_DIR__/out/SymlinkOutput Text __SBOX_SANDBOX_DIR__/tools/src/Tool after command2 old cmd",
+			"__SBOX_SANDBOX_DIR__/out/DepFile Flag FlagWithArg=arg FlagWithDepFile=__SBOX_SANDBOX_DIR__/out/depfile " +
+				"FlagWithInput=input FlagWithOutput=__SBOX_SANDBOX_DIR__/out/output " +
+				"FlagWithRspFileInputList=out_local/rsp Input __SBOX_SANDBOX_DIR__/out/Output " +
+				"__SBOX_SANDBOX_DIR__/out/SymlinkOutput Text __SBOX_SANDBOX_DIR__/tools/src/Tool after command2 old cmd",
 			"command2 __SBOX_SANDBOX_DIR__/out/depfile2 input2 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/tools/src/tool2",
-			"command3 input3 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/out/output3",
+			"command3 input3 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/out/output3 input3 __SBOX_SANDBOX_DIR__/out/output2",
 		}
 
 		wantDepMergerCommand := "__SBOX_SANDBOX_DIR__/tools/out/bin/dep_fixer __SBOX_SANDBOX_DIR__/out/DepFile __SBOX_SANDBOX_DIR__/out/depfile __SBOX_SANDBOX_DIR__/out/ImplicitDepFile __SBOX_SANDBOX_DIR__/out/depfile2"
 
+		wantRspFileContent := "$in"
+
 		AssertDeepEquals(t, "rule.Commands()", wantCommands, rule.Commands())
 
 		AssertDeepEquals(t, "rule.Inputs()", wantInputs, rule.Inputs())
+		AssertDeepEquals(t, "rule.RspfileInputs()", wantRspFileInputs, rule.RspFileInputs())
 		AssertDeepEquals(t, "rule.Outputs()", wantOutputs, rule.Outputs())
+		AssertDeepEquals(t, "rule.SymlinkOutputs()", wantSymlinkOutputs, rule.SymlinkOutputs())
 		AssertDeepEquals(t, "rule.DepFiles()", wantDepFiles, rule.DepFiles())
 		AssertDeepEquals(t, "rule.Tools()", wantTools, rule.Tools())
 		AssertDeepEquals(t, "rule.OrderOnlys()", wantOrderOnlys, rule.OrderOnlys())
 
 		AssertSame(t, "rule.depFileMergerCmd()", wantDepMergerCommand, rule.depFileMergerCmd(rule.DepFiles()).String())
+
+		AssertSame(t, "rule.composeRspFileContent()", wantRspFileContent, rule.composeRspFileContent(rule.RspFileInputs()))
 	})
 }
 
diff --git a/cc/builder.go b/cc/builder.go
index 9cd78d5..273cdd3 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -59,7 +59,7 @@
 
 	// Rules to invoke ld to link binaries. Uses a .rsp file to list dependencies, as there may
 	// be many.
-	ld, ldRE = remoteexec.StaticRules(pctx, "ld",
+	ld, ldRE = pctx.RemoteStaticRules("ld",
 		blueprint.RuleParams{
 			Command: "$reTemplate$ldCmd ${crtBegin} @${out}.rsp " +
 				"${libFlags} ${crtEnd} -o ${out} ${ldFlags} ${extraLibFlags}",
@@ -80,7 +80,7 @@
 		}, []string{"ldCmd", "crtBegin", "libFlags", "crtEnd", "ldFlags", "extraLibFlags"}, []string{"implicitInputs", "implicitOutputs"})
 
 	// Rules for .o files to combine to other .o files, using ld partial linking.
-	partialLd, partialLdRE = remoteexec.StaticRules(pctx, "partialLd",
+	partialLd, partialLdRE = pctx.RemoteStaticRules("partialLd",
 		blueprint.RuleParams{
 			// Without -no-pie, clang 7.0 adds -pie to link Android files,
 			// but -r and -pie cannot be used together.
@@ -189,7 +189,7 @@
 		"crossCompile", "format")
 
 	// Rule for invoking clang-tidy (a clang-based linter).
-	clangTidy, clangTidyRE = remoteexec.StaticRules(pctx, "clangTidy",
+	clangTidy, clangTidyRE = pctx.RemoteStaticRules("clangTidy",
 		blueprint.RuleParams{
 			Command:     "rm -f $out && $reTemplate${config.ClangBin}/clang-tidy $tidyFlags $in -- $cFlags && touch $out",
 			CommandDeps: []string{"${config.ClangBin}/clang-tidy"},
@@ -228,7 +228,7 @@
 	_ = pctx.SourcePathVariable("sAbiDumper", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/header-abi-dumper")
 
 	// -w has been added since header-abi-dumper does not need to produce any sort of diagnostic information.
-	sAbiDump, sAbiDumpRE = remoteexec.StaticRules(pctx, "sAbiDump",
+	sAbiDump, sAbiDumpRE = pctx.RemoteStaticRules("sAbiDump",
 		blueprint.RuleParams{
 			Command:     "rm -f $out && $reTemplate$sAbiDumper -o ${out} $in $exportDirs -- $cFlags -w -isystem prebuilts/clang-tools/${config.HostPrebuiltTag}/clang-headers",
 			CommandDeps: []string{"$sAbiDumper"},
@@ -246,7 +246,7 @@
 
 	// Rule to combine .dump sAbi dump files from multiple source files into a single .ldump
 	// sAbi dump file.
-	sAbiLink, sAbiLinkRE = remoteexec.StaticRules(pctx, "sAbiLink",
+	sAbiLink, sAbiLinkRE = pctx.RemoteStaticRules("sAbiLink",
 		blueprint.RuleParams{
 			Command:        "$reTemplate$sAbiLinker -o ${out} $symbolFilter -arch $arch  $exportedHeaderFlags @${out}.rsp ",
 			CommandDeps:    []string{"$sAbiLinker"},
@@ -331,7 +331,6 @@
 	pctx.StaticVariable("relPwd", PwdPrefix())
 
 	pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
-	pctx.Import("android/soong/remoteexec")
 }
 
 // builderFlags contains various types of command line flags (and settings) for use in building
diff --git a/cc/config/global.go b/cc/config/global.go
index e60bb3d..cb7d17d 100644
--- a/cc/config/global.go
+++ b/cc/config/global.go
@@ -270,13 +270,13 @@
 		return ""
 	})
 
-	pctx.VariableFunc("RECXXPool", remoteexec.EnvOverrideFunc("RBE_CXX_POOL", remoteexec.DefaultPool))
-	pctx.VariableFunc("RECXXLinksPool", remoteexec.EnvOverrideFunc("RBE_CXX_LINKS_POOL", remoteexec.DefaultPool))
-	pctx.VariableFunc("REClangTidyPool", remoteexec.EnvOverrideFunc("RBE_CLANG_TIDY_POOL", remoteexec.DefaultPool))
-	pctx.VariableFunc("RECXXLinksExecStrategy", remoteexec.EnvOverrideFunc("RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
-	pctx.VariableFunc("REClangTidyExecStrategy", remoteexec.EnvOverrideFunc("RBE_CLANG_TIDY_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
-	pctx.VariableFunc("REAbiDumperExecStrategy", remoteexec.EnvOverrideFunc("RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
-	pctx.VariableFunc("REAbiLinkerExecStrategy", remoteexec.EnvOverrideFunc("RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
+	pctx.StaticVariableWithEnvOverride("RECXXPool", "RBE_CXX_POOL", remoteexec.DefaultPool)
+	pctx.StaticVariableWithEnvOverride("RECXXLinksPool", "RBE_CXX_LINKS_POOL", remoteexec.DefaultPool)
+	pctx.StaticVariableWithEnvOverride("REClangTidyPool", "RBE_CLANG_TIDY_POOL", remoteexec.DefaultPool)
+	pctx.StaticVariableWithEnvOverride("RECXXLinksExecStrategy", "RBE_CXX_LINKS_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
+	pctx.StaticVariableWithEnvOverride("REClangTidyExecStrategy", "RBE_CLANG_TIDY_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
+	pctx.StaticVariableWithEnvOverride("REAbiDumperExecStrategy", "RBE_ABI_DUMPER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
+	pctx.StaticVariableWithEnvOverride("REAbiLinkerExecStrategy", "RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
 }
 
 var HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
diff --git a/cc/library_headers.go b/cc/library_headers.go
index dc851a5..115b775 100644
--- a/cc/library_headers.go
+++ b/cc/library_headers.go
@@ -90,15 +90,11 @@
 		return
 	}
 
-	lib, ok := module.linker.(*libraryDecorator)
-	if !ok {
-		// Not a cc_library module
+	if ctx.ModuleType() != "cc_library_headers" {
 		return
 	}
-	if !lib.header() {
-		// Not a cc_library_headers module
-		return
-	}
+
+	lib, _ := module.linker.(*libraryDecorator)
 
 	// list of directories that will be added to the include path (using -I) for this
 	// module and any module that links against this module.
diff --git a/java/Android.bp b/java/Android.bp
index 56cc401..c67379c 100644
--- a/java/Android.bp
+++ b/java/Android.bp
@@ -28,6 +28,7 @@
         "app.go",
         "app_import.go",
         "app_set.go",
+        "base.go",
         "boot_image.go",
         "boot_jars.go",
         "builder.go",
diff --git a/java/app_builder.go b/java/app_builder.go
index b53c15a..4a18dca 100644
--- a/java/app_builder.go
+++ b/java/app_builder.go
@@ -30,7 +30,7 @@
 )
 
 var (
-	Signapk, SignapkRE = remoteexec.StaticRules(pctx, "signapk",
+	Signapk, SignapkRE = pctx.RemoteStaticRules("signapk",
 		blueprint.RuleParams{
 			Command: `rm -f $out && $reTemplate${config.JavaCmd} ${config.JavaVmFlags} -Djava.library.path=$$(dirname ${config.SignapkJniLibrary}) ` +
 				`-jar ${config.SignapkCmd} $flags $certificates $in $out`,
diff --git a/java/base.go b/java/base.go
new file mode 100644
index 0000000..bd394af
--- /dev/null
+++ b/java/base.go
@@ -0,0 +1,1786 @@
+// Copyright 2021 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 (
+	"fmt"
+	"path/filepath"
+	"strconv"
+	"strings"
+
+	"github.com/google/blueprint/pathtools"
+	"github.com/google/blueprint/proptools"
+
+	"android/soong/android"
+	"android/soong/dexpreopt"
+	"android/soong/java/config"
+)
+
+// This file contains the definition and the implementation of the base module that most
+// source-based Java module structs embed.
+
+// TODO:
+// Autogenerated files:
+//  Renderscript
+// Post-jar passes:
+//  Proguard
+// Rmtypedefs
+// DroidDoc
+// Findbugs
+
+// Properties that are common to most Java modules, i.e. whether it's a host or device module.
+type CommonProperties struct {
+	// list of source files used to compile the Java module.  May be .java, .kt, .logtags, .proto,
+	// or .aidl files.
+	Srcs []string `android:"path,arch_variant"`
+
+	// list Kotlin of source files containing Kotlin code that should be treated as common code in
+	// a codebase that supports Kotlin multiplatform.  See
+	// https://kotlinlang.org/docs/reference/multiplatform.html.  May be only be .kt files.
+	Common_srcs []string `android:"path,arch_variant"`
+
+	// list of source files that should not be used to build the Java module.
+	// This is most useful in the arch/multilib variants to remove non-common files
+	Exclude_srcs []string `android:"path,arch_variant"`
+
+	// list of directories containing Java resources
+	Java_resource_dirs []string `android:"arch_variant"`
+
+	// list of directories that should be excluded from java_resource_dirs
+	Exclude_java_resource_dirs []string `android:"arch_variant"`
+
+	// list of files to use as Java resources
+	Java_resources []string `android:"path,arch_variant"`
+
+	// list of files that should be excluded from java_resources and java_resource_dirs
+	Exclude_java_resources []string `android:"path,arch_variant"`
+
+	// list of module-specific flags that will be used for javac compiles
+	Javacflags []string `android:"arch_variant"`
+
+	// list of module-specific flags that will be used for kotlinc compiles
+	Kotlincflags []string `android:"arch_variant"`
+
+	// list of java libraries that will be in the classpath
+	Libs []string `android:"arch_variant"`
+
+	// list of java libraries that will be compiled into the resulting jar
+	Static_libs []string `android:"arch_variant"`
+
+	// manifest file to be included in resulting jar
+	Manifest *string `android:"path"`
+
+	// if not blank, run jarjar using the specified rules file
+	Jarjar_rules *string `android:"path,arch_variant"`
+
+	// If not blank, set the java version passed to javac as -source and -target
+	Java_version *string
+
+	// If set to true, allow this module to be dexed and installed on devices.  Has no
+	// effect on host modules, which are always considered installable.
+	Installable *bool
+
+	// If set to true, include sources used to compile the module in to the final jar
+	Include_srcs *bool
+
+	// If not empty, classes are restricted to the specified packages and their sub-packages.
+	// This restriction is checked after applying jarjar rules and including static libs.
+	Permitted_packages []string
+
+	// List of modules to use as annotation processors
+	Plugins []string
+
+	// List of modules to export to libraries that directly depend on this library as annotation
+	// processors.  Note that if the plugins set generates_api: true this will disable the turbine
+	// optimization on modules that depend on this module, which will reduce parallelism and cause
+	// more recompilation.
+	Exported_plugins []string
+
+	// The number of Java source entries each Javac instance can process
+	Javac_shard_size *int64
+
+	// Add host jdk tools.jar to bootclasspath
+	Use_tools_jar *bool
+
+	Openjdk9 struct {
+		// List of source files that should only be used when passing -source 1.9 or higher
+		Srcs []string `android:"path"`
+
+		// List of javac flags that should only be used when passing -source 1.9 or higher
+		Javacflags []string
+	}
+
+	// When compiling language level 9+ .java code in packages that are part of
+	// a system module, patch_module names the module that your sources and
+	// dependencies should be patched into. The Android runtime currently
+	// doesn't implement the JEP 261 module system so this option is only
+	// supported at compile time. It should only be needed to compile tests in
+	// packages that exist in libcore and which are inconvenient to move
+	// elsewhere.
+	Patch_module *string `android:"arch_variant"`
+
+	Jacoco struct {
+		// List of classes to include for instrumentation with jacoco to collect coverage
+		// information at runtime when building with coverage enabled.  If unset defaults to all
+		// classes.
+		// Supports '*' as the last character of an entry in the list as a wildcard match.
+		// If preceded by '.' it matches all classes in the package and subpackages, otherwise
+		// it matches classes in the package that have the class name as a prefix.
+		Include_filter []string
+
+		// List of classes to exclude from instrumentation with jacoco to collect coverage
+		// information at runtime when building with coverage enabled.  Overrides classes selected
+		// by the include_filter property.
+		// Supports '*' as the last character of an entry in the list as a wildcard match.
+		// If preceded by '.' it matches all classes in the package and subpackages, otherwise
+		// it matches classes in the package that have the class name as a prefix.
+		Exclude_filter []string
+	}
+
+	Errorprone struct {
+		// List of javac flags that should only be used when running errorprone.
+		Javacflags []string
+
+		// List of java_plugin modules that provide extra errorprone checks.
+		Extra_check_modules []string
+	}
+
+	Proto struct {
+		// List of extra options that will be passed to the proto generator.
+		Output_params []string
+	}
+
+	Instrument bool `blueprint:"mutated"`
+
+	// List of files to include in the META-INF/services folder of the resulting jar.
+	Services []string `android:"path,arch_variant"`
+
+	// If true, package the kotlin stdlib into the jar.  Defaults to true.
+	Static_kotlin_stdlib *bool `android:"arch_variant"`
+
+	// A list of java_library instances that provide additional hiddenapi annotations for the library.
+	Hiddenapi_additional_annotations []string
+}
+
+// Properties that are specific to device modules. Host module factories should not add these when
+// constructing a new module.
+type DeviceProperties struct {
+	// if not blank, set to the version of the sdk to compile against.
+	// Defaults to compiling against the current platform.
+	Sdk_version *string
+
+	// if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
+	// Defaults to sdk_version if not set.
+	Min_sdk_version *string
+
+	// if not blank, set the targetSdkVersion in the AndroidManifest.xml.
+	// Defaults to sdk_version if not set.
+	Target_sdk_version *string
+
+	// Whether to compile against the platform APIs instead of an SDK.
+	// If true, then sdk_version must be empty. The value of this field
+	// is ignored when module's type isn't android_app.
+	Platform_apis *bool
+
+	Aidl struct {
+		// Top level directories to pass to aidl tool
+		Include_dirs []string
+
+		// Directories rooted at the Android.bp file to pass to aidl tool
+		Local_include_dirs []string
+
+		// directories that should be added as include directories for any aidl sources of modules
+		// that depend on this module, as well as to aidl for this module.
+		Export_include_dirs []string
+
+		// whether to generate traces (for systrace) for this interface
+		Generate_traces *bool
+
+		// whether to generate Binder#GetTransaction name method.
+		Generate_get_transaction_name *bool
+
+		// list of flags that will be passed to the AIDL compiler
+		Flags []string
+	}
+
+	// If true, export a copy of the module as a -hostdex module for host testing.
+	Hostdex *bool
+
+	Target struct {
+		Hostdex struct {
+			// Additional required dependencies to add to -hostdex modules.
+			Required []string
+		}
+	}
+
+	// When targeting 1.9 and above, override the modules to use with --system,
+	// otherwise provides defaults libraries to add to the bootclasspath.
+	System_modules *string
+
+	// The name of the module as used in build configuration.
+	//
+	// Allows a library to separate its actual name from the name used in
+	// build configuration, e.g.ctx.Config().BootJars().
+	ConfigurationName *string `blueprint:"mutated"`
+
+	// set the name of the output
+	Stem *string
+
+	IsSDKLibrary bool `blueprint:"mutated"`
+
+	// If true, generate the signature file of APK Signing Scheme V4, along side the signed APK file.
+	// Defaults to false.
+	V4_signature *bool
+
+	// Only for libraries created by a sysprop_library module, SyspropPublicStub is the name of the
+	// public stubs library.
+	SyspropPublicStub string `blueprint:"mutated"`
+}
+
+// Functionality common to Module and Import
+//
+// It is embedded in Module so its functionality can be used by methods in Module
+// but it is currently only initialized by Import and Library.
+type embeddableInModuleAndImport struct {
+
+	// Functionality related to this being used as a component of a java_sdk_library.
+	EmbeddableSdkLibraryComponent
+}
+
+func (e *embeddableInModuleAndImport) initModuleAndImport(moduleBase *android.ModuleBase) {
+	e.initSdkLibraryComponent(moduleBase)
+}
+
+// Module/Import's DepIsInSameApex(...) delegates to this method.
+//
+// This cannot implement DepIsInSameApex(...) directly as that leads to ambiguity with
+// the one provided by ApexModuleBase.
+func (e *embeddableInModuleAndImport) depIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
+	// dependencies other than the static linkage are all considered crossing APEX boundary
+	if staticLibTag == ctx.OtherModuleDependencyTag(dep) {
+		return true
+	}
+	return false
+}
+
+// Module contains the properties and members used by all java module types
+type Module struct {
+	android.ModuleBase
+	android.DefaultableModuleBase
+	android.ApexModuleBase
+	android.SdkBase
+
+	// Functionality common to Module and Import.
+	embeddableInModuleAndImport
+
+	properties       CommonProperties
+	protoProperties  android.ProtoProperties
+	deviceProperties DeviceProperties
+
+	// jar file containing header classes including static library dependencies, suitable for
+	// inserting into the bootclasspath/classpath of another compile
+	headerJarFile android.Path
+
+	// jar file containing implementation classes including static library dependencies but no
+	// resources
+	implementationJarFile android.Path
+
+	// jar file containing only resources including from static library dependencies
+	resourceJar android.Path
+
+	// args and dependencies to package source files into a srcjar
+	srcJarArgs []string
+	srcJarDeps android.Paths
+
+	// jar file containing implementation classes and resources including static library
+	// dependencies
+	implementationAndResourcesJar android.Path
+
+	// output file containing classes.dex and resources
+	dexJarFile android.Path
+
+	// output file containing uninstrumented classes that will be instrumented by jacoco
+	jacocoReportClassesFile android.Path
+
+	// output file of the module, which may be a classes jar or a dex jar
+	outputFile       android.Path
+	extraOutputFiles android.Paths
+
+	exportAidlIncludeDirs android.Paths
+
+	logtagsSrcs android.Paths
+
+	// installed file for binary dependency
+	installFile android.Path
+
+	// list of .java files and srcjars that was passed to javac
+	compiledJavaSrcs android.Paths
+	compiledSrcJars  android.Paths
+
+	// manifest file to use instead of properties.Manifest
+	overrideManifest android.OptionalPath
+
+	// map of SDK version to class loader context
+	classLoaderContexts dexpreopt.ClassLoaderContextMap
+
+	// list of plugins that this java module is exporting
+	exportedPluginJars android.Paths
+
+	// list of plugins that this java module is exporting
+	exportedPluginClasses []string
+
+	// if true, the exported plugins generate API and require disabling turbine.
+	exportedDisableTurbine bool
+
+	// list of source files, collected from srcFiles with unique java and all kt files,
+	// will be used by android.IDEInfo struct
+	expandIDEInfoCompiledSrcs []string
+
+	// expanded Jarjar_rules
+	expandJarjarRules android.Path
+
+	// list of additional targets for checkbuild
+	additionalCheckedModules android.Paths
+
+	// Extra files generated by the module type to be added as java resources.
+	extraResources android.Paths
+
+	hiddenAPI
+	dexer
+	dexpreopter
+	usesLibrary
+	linter
+
+	// list of the xref extraction files
+	kytheFiles android.Paths
+
+	// Collect the module directory for IDE info in java/jdeps.go.
+	modulePaths []string
+
+	hideApexVariantFromMake bool
+}
+
+func (j *Module) CheckStableSdkVersion() error {
+	sdkVersion := j.sdkVersion()
+	if sdkVersion.stable() {
+		return nil
+	}
+	if sdkVersion.kind == sdkCorePlatform {
+		if useLegacyCorePlatformApiByName(j.BaseModuleName()) {
+			return fmt.Errorf("non stable SDK %v - uses legacy core platform", sdkVersion)
+		} else {
+			// Treat stable core platform as stable.
+			return nil
+		}
+	} else {
+		return fmt.Errorf("non stable SDK %v", sdkVersion)
+	}
+}
+
+// checkSdkVersions enforces restrictions around SDK dependencies.
+func (j *Module) checkSdkVersions(ctx android.ModuleContext) {
+	if j.RequiresStableAPIs(ctx) {
+		if sc, ok := ctx.Module().(sdkContext); ok {
+			if !sc.sdkVersion().specified() {
+				ctx.PropertyErrorf("sdk_version",
+					"sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
+			}
+		}
+	}
+
+	// Make sure this module doesn't statically link to modules with lower-ranked SDK link type.
+	// See rank() for details.
+	ctx.VisitDirectDeps(func(module android.Module) {
+		tag := ctx.OtherModuleDependencyTag(module)
+		switch module.(type) {
+		// TODO(satayev): cover other types as well, e.g. imports
+		case *Library, *AndroidLibrary:
+			switch tag {
+			case bootClasspathTag, libTag, staticLibTag, java9LibTag:
+				j.checkSdkLinkType(ctx, module.(moduleWithSdkDep), tag.(dependencyTag))
+			}
+		}
+	})
+}
+
+func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
+	if sc, ok := ctx.Module().(sdkContext); ok {
+		usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
+		sdkVersionSpecified := sc.sdkVersion().specified()
+		if usePlatformAPI && sdkVersionSpecified {
+			ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
+		} else if !usePlatformAPI && !sdkVersionSpecified {
+			ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
+		}
+
+	}
+}
+
+func (j *Module) addHostProperties() {
+	j.AddProperties(
+		&j.properties,
+		&j.protoProperties,
+		&j.usesLibraryProperties,
+	)
+}
+
+func (j *Module) addHostAndDeviceProperties() {
+	j.addHostProperties()
+	j.AddProperties(
+		&j.deviceProperties,
+		&j.dexer.dexProperties,
+		&j.dexpreoptProperties,
+		&j.linter.properties,
+	)
+}
+
+func (j *Module) OutputFiles(tag string) (android.Paths, error) {
+	switch tag {
+	case "":
+		return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
+	case android.DefaultDistTag:
+		return android.Paths{j.outputFile}, nil
+	case ".jar":
+		return android.Paths{j.implementationAndResourcesJar}, nil
+	case ".proguard_map":
+		if j.dexer.proguardDictionary.Valid() {
+			return android.Paths{j.dexer.proguardDictionary.Path()}, nil
+		}
+		return nil, fmt.Errorf("%q was requested, but no output file was found.", tag)
+	default:
+		return nil, fmt.Errorf("unsupported module reference tag %q", tag)
+	}
+}
+
+var _ android.OutputFileProducer = (*Module)(nil)
+
+func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
+	initJavaModule(module, hod, false)
+}
+
+func InitJavaModuleMultiTargets(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
+	initJavaModule(module, hod, true)
+}
+
+func initJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported, multiTargets bool) {
+	multilib := android.MultilibCommon
+	if multiTargets {
+		android.InitAndroidMultiTargetsArchModule(module, hod, multilib)
+	} else {
+		android.InitAndroidArchModule(module, hod, multilib)
+	}
+	android.InitDefaultableModule(module)
+}
+
+func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
+	return j.properties.Instrument &&
+		ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") &&
+		ctx.DeviceConfig().JavaCoverageEnabledForPath(ctx.ModuleDir())
+}
+
+func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
+	return j.shouldInstrument(ctx) &&
+		(ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
+			ctx.Config().UnbundledBuild())
+}
+
+func (j *Module) shouldInstrumentInApex(ctx android.BaseModuleContext) bool {
+	// Force enable the instrumentation for java code that is built for APEXes ...
+	// except for the jacocoagent itself (because instrumenting jacocoagent using jacocoagent
+	// doesn't make sense) or framework libraries (e.g. libraries found in the InstrumentFrameworkModules list) unless EMMA_INSTRUMENT_FRAMEWORK is true.
+	apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
+	isJacocoAgent := ctx.ModuleName() == "jacocoagent"
+	if j.DirectlyInAnyApex() && !isJacocoAgent && !apexInfo.IsForPlatform() {
+		if !inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
+			return true
+		} else if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
+			return true
+		}
+	}
+	return false
+}
+
+func (j *Module) sdkVersion() sdkSpec {
+	return sdkSpecFrom(String(j.deviceProperties.Sdk_version))
+}
+
+func (j *Module) systemModules() string {
+	return proptools.String(j.deviceProperties.System_modules)
+}
+
+func (j *Module) minSdkVersion() sdkSpec {
+	if j.deviceProperties.Min_sdk_version != nil {
+		return sdkSpecFrom(*j.deviceProperties.Min_sdk_version)
+	}
+	return j.sdkVersion()
+}
+
+func (j *Module) targetSdkVersion() sdkSpec {
+	if j.deviceProperties.Target_sdk_version != nil {
+		return sdkSpecFrom(*j.deviceProperties.Target_sdk_version)
+	}
+	return j.sdkVersion()
+}
+
+func (j *Module) MinSdkVersion() string {
+	return j.minSdkVersion().version.String()
+}
+
+func (j *Module) AvailableFor(what string) bool {
+	if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
+		// Exception: for hostdex: true libraries, the platform variant is created
+		// even if it's not marked as available to platform. In that case, the platform
+		// variant is used only for the hostdex and not installed to the device.
+		return true
+	}
+	return j.ApexModuleBase.AvailableFor(what)
+}
+
+func (j *Module) deps(ctx android.BottomUpMutatorContext) {
+	if ctx.Device() {
+		j.linter.deps(ctx)
+
+		sdkDeps(ctx, sdkContext(j), j.dexer)
+
+		if j.deviceProperties.SyspropPublicStub != "" {
+			// This is a sysprop implementation library that has a corresponding sysprop public
+			// stubs library, and a dependency on it so that dependencies on the implementation can
+			// be forwarded to the public stubs library when necessary.
+			ctx.AddVariationDependencies(nil, syspropPublicStubDepTag, j.deviceProperties.SyspropPublicStub)
+		}
+	}
+
+	libDeps := ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
+	ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
+
+	// Add dependency on libraries that provide additional hidden api annotations.
+	ctx.AddVariationDependencies(nil, hiddenApiAnnotationsTag, j.properties.Hiddenapi_additional_annotations...)
+
+	if ctx.DeviceConfig().VndkVersion() != "" && ctx.Config().EnforceInterPartitionJavaSdkLibrary() {
+		// Require java_sdk_library at inter-partition java dependency to ensure stable
+		// interface between partitions. If inter-partition java_library dependency is detected,
+		// raise build error because java_library doesn't have a stable interface.
+		//
+		// Inputs:
+		//    PRODUCT_ENFORCE_INTER_PARTITION_JAVA_SDK_LIBRARY
+		//      if true, enable enforcement
+		//    PRODUCT_INTER_PARTITION_JAVA_LIBRARY_ALLOWLIST
+		//      exception list of java_library names to allow inter-partition dependency
+		for idx := range j.properties.Libs {
+			if libDeps[idx] == nil {
+				continue
+			}
+
+			if javaDep, ok := libDeps[idx].(javaSdkLibraryEnforceContext); ok {
+				// java_sdk_library is always allowed at inter-partition dependency.
+				// So, skip check.
+				if _, ok := javaDep.(*SdkLibrary); ok {
+					continue
+				}
+
+				j.checkPartitionsForJavaDependency(ctx, "libs", javaDep)
+			}
+		}
+	}
+
+	// For library dependencies that are component libraries (like stubs), add the implementation
+	// as a dependency (dexpreopt needs to be against the implementation library, not stubs).
+	for _, dep := range libDeps {
+		if dep != nil {
+			if component, ok := dep.(SdkLibraryComponentDependency); ok {
+				if lib := component.OptionalSdkLibraryImplementation(); lib != nil {
+					ctx.AddVariationDependencies(nil, usesLibTag, *lib)
+				}
+			}
+		}
+	}
+
+	ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
+	ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), errorpronePluginTag, j.properties.Errorprone.Extra_check_modules...)
+	ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
+
+	android.ProtoDeps(ctx, &j.protoProperties)
+	if j.hasSrcExt(".proto") {
+		protoDeps(ctx, &j.protoProperties)
+	}
+
+	if j.hasSrcExt(".kt") {
+		// TODO(ccross): move this to a mutator pass that can tell if generated sources contain
+		// Kotlin files
+		ctx.AddVariationDependencies(nil, kotlinStdlibTag,
+			"kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
+		if len(j.properties.Plugins) > 0 {
+			ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
+		}
+	}
+
+	// Framework libraries need special handling in static coverage builds: they should not have
+	// static dependency on jacoco, otherwise there would be multiple conflicting definitions of
+	// the same jacoco classes coming from different bootclasspath jars.
+	if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
+		if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
+			j.properties.Instrument = true
+		}
+	} else if j.shouldInstrumentStatic(ctx) {
+		ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
+	}
+}
+
+func hasSrcExt(srcs []string, ext string) bool {
+	for _, src := range srcs {
+		if filepath.Ext(src) == ext {
+			return true
+		}
+	}
+
+	return false
+}
+
+func (j *Module) hasSrcExt(ext string) bool {
+	return hasSrcExt(j.properties.Srcs, ext)
+}
+
+func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
+	aidlIncludeDirs android.Paths) (string, android.Paths) {
+
+	aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
+	aidlIncludes = append(aidlIncludes,
+		android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
+	aidlIncludes = append(aidlIncludes,
+		android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
+
+	var flags []string
+	var deps android.Paths
+
+	flags = append(flags, j.deviceProperties.Aidl.Flags...)
+
+	if aidlPreprocess.Valid() {
+		flags = append(flags, "-p"+aidlPreprocess.String())
+		deps = append(deps, aidlPreprocess.Path())
+	} else if len(aidlIncludeDirs) > 0 {
+		flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
+	}
+
+	if len(j.exportAidlIncludeDirs) > 0 {
+		flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
+	}
+
+	if len(aidlIncludes) > 0 {
+		flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
+	}
+
+	flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
+	if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
+		flags = append(flags, "-I"+src.String())
+	}
+
+	if Bool(j.deviceProperties.Aidl.Generate_traces) {
+		flags = append(flags, "-t")
+	}
+
+	if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
+		flags = append(flags, "--transaction_names")
+	}
+
+	return strings.Join(flags, " "), deps
+}
+
+func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
+
+	var flags javaBuilderFlags
+
+	// javaVersion flag.
+	flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
+
+	if ctx.Config().RunErrorProne() {
+		if config.ErrorProneClasspath == nil && ctx.Config().TestProductVariables == nil {
+			ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
+		}
+
+		errorProneFlags := []string{
+			"-Xplugin:ErrorProne",
+			"${config.ErrorProneChecks}",
+		}
+		errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
+
+		flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
+			"'" + strings.Join(errorProneFlags, " ") + "'"
+		flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
+	}
+
+	// classpath
+	flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
+	flags.classpath = append(flags.classpath, deps.classpath...)
+	flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
+	flags.processorPath = append(flags.processorPath, deps.processorPath...)
+	flags.errorProneProcessorPath = append(flags.errorProneProcessorPath, deps.errorProneProcessorPath...)
+
+	flags.processors = append(flags.processors, deps.processorClasses...)
+	flags.processors = android.FirstUniqueStrings(flags.processors)
+
+	if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
+		decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
+		// Give host-side tools a version of OpenJDK's standard libraries
+		// close to what they're targeting. As of Dec 2017, AOSP is only
+		// bundling OpenJDK 8 and 9, so nothing < 8 is available.
+		//
+		// When building with OpenJDK 8, the following should have no
+		// effect since those jars would be available by default.
+		//
+		// When building with OpenJDK 9 but targeting a version < 1.8,
+		// putting them on the bootclasspath means that:
+		// a) code can't (accidentally) refer to OpenJDK 9 specific APIs
+		// b) references to existing APIs are not reinterpreted in an
+		//    OpenJDK 9-specific way, eg. calls to subclasses of
+		//    java.nio.Buffer as in http://b/70862583
+		java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
+		flags.bootClasspath = append(flags.bootClasspath,
+			android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
+			android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
+		if Bool(j.properties.Use_tools_jar) {
+			flags.bootClasspath = append(flags.bootClasspath,
+				android.PathForSource(ctx, java8Home, "lib/tools.jar"))
+		}
+	}
+
+	// systemModules
+	flags.systemModules = deps.systemModules
+
+	// aidl flags.
+	flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
+
+	return flags
+}
+
+func (j *Module) collectJavacFlags(
+	ctx android.ModuleContext, flags javaBuilderFlags, srcFiles android.Paths) javaBuilderFlags {
+	// javac flags.
+	javacFlags := j.properties.Javacflags
+
+	if ctx.Config().MinimizeJavaDebugInfo() && !ctx.Host() {
+		// For non-host binaries, override the -g flag passed globally to remove
+		// local variable debug info to reduce disk and memory usage.
+		javacFlags = append(javacFlags, "-g:source,lines")
+	}
+	javacFlags = append(javacFlags, "-Xlint:-dep-ann")
+
+	if flags.javaVersion.usesJavaModules() {
+		javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
+
+		if j.properties.Patch_module != nil {
+			// Manually specify build directory in case it is not under the repo root.
+			// (javac doesn't seem to expand into symbolic links when searching for patch-module targets, so
+			// just adding a symlink under the root doesn't help.)
+			patchPaths := []string{".", ctx.Config().BuildDir()}
+
+			// b/150878007
+			//
+			// Workaround to support *Bazel-executed* JDK9 javac in Bazel's
+			// execution root for --patch-module. If this javac command line is
+			// invoked within Bazel's execution root working directory, the top
+			// level directories (e.g. libcore/, tools/, frameworks/) are all
+			// symlinks. JDK9 javac does not traverse into symlinks, which causes
+			// --patch-module to fail source file lookups when invoked in the
+			// execution root.
+			//
+			// Short of patching javac or enumerating *all* directories as possible
+			// input dirs, manually add the top level dir of the source files to be
+			// compiled.
+			topLevelDirs := map[string]bool{}
+			for _, srcFilePath := range srcFiles {
+				srcFileParts := strings.Split(srcFilePath.String(), "/")
+				// Ignore source files that are already in the top level directory
+				// as well as generated files in the out directory. The out
+				// directory may be an absolute path, which means srcFileParts[0] is the
+				// empty string, so check that as well. Note that "out" in Bazel's execution
+				// root is *not* a symlink, which doesn't cause problems for --patch-modules
+				// anyway, so it's fine to not apply this workaround for generated
+				// source files.
+				if len(srcFileParts) > 1 &&
+					srcFileParts[0] != "" &&
+					srcFileParts[0] != "out" {
+					topLevelDirs[srcFileParts[0]] = true
+				}
+			}
+			patchPaths = append(patchPaths, android.SortedStringKeys(topLevelDirs)...)
+
+			classPath := flags.classpath.FormJavaClassPath("")
+			if classPath != "" {
+				patchPaths = append(patchPaths, classPath)
+			}
+			javacFlags = append(
+				javacFlags,
+				"--patch-module="+String(j.properties.Patch_module)+"="+strings.Join(patchPaths, ":"))
+		}
+	}
+
+	if len(javacFlags) > 0 {
+		// optimization.
+		ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
+		flags.javacFlags = "$javacFlags"
+	}
+
+	return flags
+}
+
+func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
+	j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
+
+	deps := j.collectDeps(ctx)
+	flags := j.collectBuilderFlags(ctx, deps)
+
+	if flags.javaVersion.usesJavaModules() {
+		j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
+	}
+	srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
+	if hasSrcExt(srcFiles.Strings(), ".proto") {
+		flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
+	}
+
+	kotlinCommonSrcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Common_srcs, nil)
+	if len(kotlinCommonSrcFiles.FilterOutByExt(".kt")) > 0 {
+		ctx.PropertyErrorf("common_srcs", "common_srcs must be .kt files")
+	}
+
+	srcFiles = j.genSources(ctx, srcFiles, flags)
+
+	// Collect javac flags only after computing the full set of srcFiles to
+	// ensure that the --patch-module lookup paths are complete.
+	flags = j.collectJavacFlags(ctx, flags, srcFiles)
+
+	srcJars := srcFiles.FilterByExt(".srcjar")
+	srcJars = append(srcJars, deps.srcJars...)
+	if aaptSrcJar != nil {
+		srcJars = append(srcJars, aaptSrcJar)
+	}
+
+	if j.properties.Jarjar_rules != nil {
+		j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
+	}
+
+	jarName := ctx.ModuleName() + ".jar"
+
+	javaSrcFiles := srcFiles.FilterByExt(".java")
+	var uniqueSrcFiles android.Paths
+	set := make(map[string]bool)
+	for _, v := range javaSrcFiles {
+		if _, found := set[v.String()]; !found {
+			set[v.String()] = true
+			uniqueSrcFiles = append(uniqueSrcFiles, v)
+		}
+	}
+
+	// Collect .java files for AIDEGen
+	j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
+
+	var kotlinJars android.Paths
+
+	if srcFiles.HasExt(".kt") {
+		// user defined kotlin flags.
+		kotlincFlags := j.properties.Kotlincflags
+		CheckKotlincFlags(ctx, kotlincFlags)
+
+		// Dogfood the JVM_IR backend.
+		kotlincFlags = append(kotlincFlags, "-Xuse-ir")
+
+		// If there are kotlin files, compile them first but pass all the kotlin and java files
+		// kotlinc will use the java files to resolve types referenced by the kotlin files, but
+		// won't emit any classes for them.
+		kotlincFlags = append(kotlincFlags, "-no-stdlib")
+		if ctx.Device() {
+			kotlincFlags = append(kotlincFlags, "-no-jdk")
+		}
+		if len(kotlincFlags) > 0 {
+			// optimization.
+			ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
+			flags.kotlincFlags += "$kotlincFlags"
+		}
+
+		var kotlinSrcFiles android.Paths
+		kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
+		kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
+
+		// Collect .kt files for AIDEGen
+		j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
+		j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, kotlinCommonSrcFiles.Strings()...)
+
+		flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
+		flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
+
+		flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
+		flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
+
+		if len(flags.processorPath) > 0 {
+			// Use kapt for annotation processing
+			kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
+			kaptResJar := android.PathForModuleOut(ctx, "kapt", "kapt-res.jar")
+			kotlinKapt(ctx, kaptSrcJar, kaptResJar, kotlinSrcFiles, kotlinCommonSrcFiles, srcJars, flags)
+			srcJars = append(srcJars, kaptSrcJar)
+			kotlinJars = append(kotlinJars, kaptResJar)
+			// Disable annotation processing in javac, it's already been handled by kapt
+			flags.processorPath = nil
+			flags.processors = nil
+		}
+
+		kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
+		kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, kotlinCommonSrcFiles, srcJars, flags)
+		if ctx.Failed() {
+			return
+		}
+
+		// Make javac rule depend on the kotlinc rule
+		flags.classpath = append(flags.classpath, kotlinJar)
+
+		kotlinJars = append(kotlinJars, kotlinJar)
+		// Jar kotlin classes into the final jar after javac
+		if BoolDefault(j.properties.Static_kotlin_stdlib, true) {
+			kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
+		}
+	}
+
+	jars := append(android.Paths(nil), kotlinJars...)
+
+	// Store the list of .java files that was passed to javac
+	j.compiledJavaSrcs = uniqueSrcFiles
+	j.compiledSrcJars = srcJars
+
+	enableSharding := false
+	var headerJarFileWithoutJarjar android.Path
+	if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
+		if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
+			enableSharding = true
+			// Formerly, there was a check here that prevented annotation processors
+			// from being used when sharding was enabled, as some annotation processors
+			// do not function correctly in sharded environments. It was removed to
+			// allow for the use of annotation processors that do function correctly
+			// with sharding enabled. See: b/77284273.
+		}
+		headerJarFileWithoutJarjar, j.headerJarFile =
+			j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
+		if ctx.Failed() {
+			return
+		}
+	}
+	if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
+		var extraJarDeps android.Paths
+		if ctx.Config().RunErrorProne() {
+			// If error-prone is enabled, add an additional rule to compile the java files into
+			// a separate set of classes (so that they don't overwrite the normal ones and require
+			// a rebuild when error-prone is turned off).
+			// TODO(ccross): Once we always compile with javac9 we may be able to conditionally
+			//    enable error-prone without affecting the output class files.
+			errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
+			RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
+			extraJarDeps = append(extraJarDeps, errorprone)
+		}
+
+		if enableSharding {
+			flags.classpath = append(flags.classpath, headerJarFileWithoutJarjar)
+			shardSize := int(*(j.properties.Javac_shard_size))
+			var shardSrcs []android.Paths
+			if len(uniqueSrcFiles) > 0 {
+				shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
+				for idx, shardSrc := range shardSrcs {
+					classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
+						nil, flags, extraJarDeps)
+					jars = append(jars, classes)
+				}
+			}
+			if len(srcJars) > 0 {
+				classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
+					nil, srcJars, flags, extraJarDeps)
+				jars = append(jars, classes)
+			}
+		} else {
+			classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
+			jars = append(jars, classes)
+		}
+		if ctx.Failed() {
+			return
+		}
+	}
+
+	j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
+
+	var includeSrcJar android.WritablePath
+	if Bool(j.properties.Include_srcs) {
+		includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
+		TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
+	}
+
+	dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
+		j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
+	fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
+	extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
+
+	var resArgs []string
+	var resDeps android.Paths
+
+	resArgs = append(resArgs, dirArgs...)
+	resDeps = append(resDeps, dirDeps...)
+
+	resArgs = append(resArgs, fileArgs...)
+	resDeps = append(resDeps, fileDeps...)
+
+	resArgs = append(resArgs, extraArgs...)
+	resDeps = append(resDeps, extraDeps...)
+
+	if len(resArgs) > 0 {
+		resourceJar := android.PathForModuleOut(ctx, "res", jarName)
+		TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
+		j.resourceJar = resourceJar
+		if ctx.Failed() {
+			return
+		}
+	}
+
+	var resourceJars android.Paths
+	if j.resourceJar != nil {
+		resourceJars = append(resourceJars, j.resourceJar)
+	}
+	if Bool(j.properties.Include_srcs) {
+		resourceJars = append(resourceJars, includeSrcJar)
+	}
+	resourceJars = append(resourceJars, deps.staticResourceJars...)
+
+	if len(resourceJars) > 1 {
+		combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
+		TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
+			false, nil, nil)
+		j.resourceJar = combinedJar
+	} else if len(resourceJars) == 1 {
+		j.resourceJar = resourceJars[0]
+	}
+
+	if len(deps.staticJars) > 0 {
+		jars = append(jars, deps.staticJars...)
+	}
+
+	manifest := j.overrideManifest
+	if !manifest.Valid() && j.properties.Manifest != nil {
+		manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
+	}
+
+	services := android.PathsForModuleSrc(ctx, j.properties.Services)
+	if len(services) > 0 {
+		servicesJar := android.PathForModuleOut(ctx, "services", jarName)
+		var zipargs []string
+		for _, file := range services {
+			serviceFile := file.String()
+			zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
+		}
+		rule := zip
+		args := map[string]string{
+			"jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
+		}
+		if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ZIP") {
+			rule = zipRE
+			args["implicits"] = strings.Join(services.Strings(), ",")
+		}
+		ctx.Build(pctx, android.BuildParams{
+			Rule:      rule,
+			Output:    servicesJar,
+			Implicits: services,
+			Args:      args,
+		})
+		jars = append(jars, servicesJar)
+	}
+
+	// Combine the classes built from sources, any manifests, and any static libraries into
+	// classes.jar. If there is only one input jar this step will be skipped.
+	var outputFile android.OutputPath
+
+	if len(jars) == 1 && !manifest.Valid() {
+		// Optimization: skip the combine step as there is nothing to do
+		// TODO(ccross): this leaves any module-info.class files, but those should only come from
+		// prebuilt dependencies until we support modules in the platform build, so there shouldn't be
+		// any if len(jars) == 1.
+
+		// Transform the single path to the jar into an OutputPath as that is required by the following
+		// code.
+		if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
+			// The path contains an embedded OutputPath so reuse that.
+			outputFile = moduleOutPath.OutputPath
+		} else if outputPath, ok := jars[0].(android.OutputPath); ok {
+			// The path is an OutputPath so reuse it directly.
+			outputFile = outputPath
+		} else {
+			// The file is not in the out directory so create an OutputPath into which it can be copied
+			// and which the following code can use to refer to it.
+			combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
+			ctx.Build(pctx, android.BuildParams{
+				Rule:   android.Cp,
+				Input:  jars[0],
+				Output: combinedJar,
+			})
+			outputFile = combinedJar.OutputPath
+		}
+	} else {
+		combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
+		TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
+			false, nil, nil)
+		outputFile = combinedJar.OutputPath
+	}
+
+	// jarjar implementation jar if necessary
+	if j.expandJarjarRules != nil {
+		// Transform classes.jar into classes-jarjar.jar
+		jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName).OutputPath
+		TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
+		outputFile = jarjarFile
+
+		// jarjar resource jar if necessary
+		if j.resourceJar != nil {
+			resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
+			TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
+			j.resourceJar = resourceJarJarFile
+		}
+
+		if ctx.Failed() {
+			return
+		}
+	}
+
+	// Check package restrictions if necessary.
+	if len(j.properties.Permitted_packages) > 0 {
+		// Check packages and copy to package-checked file.
+		pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
+		CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
+		j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
+
+		if ctx.Failed() {
+			return
+		}
+	}
+
+	j.implementationJarFile = outputFile
+	if j.headerJarFile == nil {
+		j.headerJarFile = j.implementationJarFile
+	}
+
+	if j.shouldInstrumentInApex(ctx) {
+		j.properties.Instrument = true
+	}
+
+	if j.shouldInstrument(ctx) {
+		outputFile = j.instrument(ctx, flags, outputFile, jarName)
+	}
+
+	// merge implementation jar with resources if necessary
+	implementationAndResourcesJar := outputFile
+	if j.resourceJar != nil {
+		jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
+		combinedJar := android.PathForModuleOut(ctx, "withres", jarName).OutputPath
+		TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
+			false, nil, nil)
+		implementationAndResourcesJar = combinedJar
+	}
+
+	j.implementationAndResourcesJar = implementationAndResourcesJar
+
+	// Enable dex compilation for the APEX variants, unless it is disabled explicitly
+	apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
+	if j.DirectlyInAnyApex() && !apexInfo.IsForPlatform() {
+		if j.dexProperties.Compile_dex == nil {
+			j.dexProperties.Compile_dex = proptools.BoolPtr(true)
+		}
+		if j.deviceProperties.Hostdex == nil {
+			j.deviceProperties.Hostdex = proptools.BoolPtr(true)
+		}
+	}
+
+	if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.dexProperties.Compile_dex)) {
+		if j.hasCode(ctx) {
+			if j.shouldInstrumentStatic(ctx) {
+				j.dexer.extraProguardFlagFiles = append(j.dexer.extraProguardFlagFiles,
+					android.PathForSource(ctx, "build/make/core/proguard.jacoco.flags"))
+			}
+			// Dex compilation
+			var dexOutputFile android.OutputPath
+			dexOutputFile = j.dexer.compileDex(ctx, flags, j.minSdkVersion(), outputFile, jarName)
+			if ctx.Failed() {
+				return
+			}
+
+			// Hidden API CSV generation and dex encoding
+			dexOutputFile = j.hiddenAPIExtractAndEncode(ctx, dexOutputFile, j.implementationJarFile,
+				proptools.Bool(j.dexProperties.Uncompress_dex))
+
+			// merge dex jar with resources if necessary
+			if j.resourceJar != nil {
+				jars := android.Paths{dexOutputFile, j.resourceJar}
+				combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName).OutputPath
+				TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
+					false, nil, nil)
+				if *j.dexProperties.Uncompress_dex {
+					combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName).OutputPath
+					TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
+					dexOutputFile = combinedAlignedJar
+				} else {
+					dexOutputFile = combinedJar
+				}
+			}
+
+			j.dexJarFile = dexOutputFile
+
+			// Dexpreopting
+			j.dexpreopt(ctx, dexOutputFile)
+
+			outputFile = dexOutputFile
+		} else {
+			// There is no code to compile into a dex jar, make sure the resources are propagated
+			// to the APK if this is an app.
+			outputFile = implementationAndResourcesJar
+			j.dexJarFile = j.resourceJar
+		}
+
+		if ctx.Failed() {
+			return
+		}
+	} else {
+		outputFile = implementationAndResourcesJar
+	}
+
+	if ctx.Device() {
+		lintSDKVersionString := func(sdkSpec sdkSpec) string {
+			if v := sdkSpec.version; v.isNumbered() {
+				return v.String()
+			} else {
+				return ctx.Config().DefaultAppTargetSdk(ctx).String()
+			}
+		}
+
+		j.linter.name = ctx.ModuleName()
+		j.linter.srcs = srcFiles
+		j.linter.srcJars = srcJars
+		j.linter.classpath = append(append(android.Paths(nil), flags.bootClasspath...), flags.classpath...)
+		j.linter.classes = j.implementationJarFile
+		j.linter.minSdkVersion = lintSDKVersionString(j.minSdkVersion())
+		j.linter.targetSdkVersion = lintSDKVersionString(j.targetSdkVersion())
+		j.linter.compileSdkVersion = lintSDKVersionString(j.sdkVersion())
+		j.linter.javaLanguageLevel = flags.javaVersion.String()
+		j.linter.kotlinLanguageLevel = "1.3"
+		if !apexInfo.IsForPlatform() && ctx.Config().UnbundledBuildApps() {
+			j.linter.buildModuleReportZip = true
+		}
+		j.linter.lint(ctx)
+	}
+
+	ctx.CheckbuildFile(outputFile)
+
+	ctx.SetProvider(JavaInfoProvider, JavaInfo{
+		HeaderJars:                     android.PathsIfNonNil(j.headerJarFile),
+		ImplementationAndResourcesJars: android.PathsIfNonNil(j.implementationAndResourcesJar),
+		ImplementationJars:             android.PathsIfNonNil(j.implementationJarFile),
+		ResourceJars:                   android.PathsIfNonNil(j.resourceJar),
+		AidlIncludeDirs:                j.exportAidlIncludeDirs,
+		SrcJarArgs:                     j.srcJarArgs,
+		SrcJarDeps:                     j.srcJarDeps,
+		ExportedPlugins:                j.exportedPluginJars,
+		ExportedPluginClasses:          j.exportedPluginClasses,
+		ExportedPluginDisableTurbine:   j.exportedDisableTurbine,
+		JacocoReportClassesFile:        j.jacocoReportClassesFile,
+	})
+
+	// Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
+	j.outputFile = outputFile.WithoutRel()
+}
+
+func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
+	srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
+
+	kzipName := pathtools.ReplaceExtension(jarName, "kzip")
+	if idx >= 0 {
+		kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
+		jarName += strconv.Itoa(idx)
+	}
+
+	classes := android.PathForModuleOut(ctx, "javac", jarName).OutputPath
+	TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
+
+	if ctx.Config().EmitXrefRules() {
+		extractionFile := android.PathForModuleOut(ctx, kzipName)
+		emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
+		j.kytheFiles = append(j.kytheFiles, extractionFile)
+	}
+
+	return classes
+}
+
+// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
+// since some of these flags may be used internally.
+func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
+	for _, flag := range flags {
+		flag = strings.TrimSpace(flag)
+
+		if !strings.HasPrefix(flag, "-") {
+			ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
+		} else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
+			ctx.PropertyErrorf("kotlincflags",
+				"Bad flag: `%s`, only use internal compiler for consistency.", flag)
+		} else if inList(flag, config.KotlincIllegalFlags) {
+			ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
+		} else if flag == "-include-runtime" {
+			ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
+		} else {
+			args := strings.Split(flag, " ")
+			if args[0] == "-kotlin-home" {
+				ctx.PropertyErrorf("kotlincflags",
+					"Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
+			}
+		}
+	}
+}
+
+func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
+	deps deps, flags javaBuilderFlags, jarName string,
+	extraJars android.Paths) (headerJar, jarjarHeaderJar android.Path) {
+
+	var jars android.Paths
+	if len(srcFiles) > 0 || len(srcJars) > 0 {
+		// Compile java sources into turbine.jar.
+		turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
+		TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
+		if ctx.Failed() {
+			return nil, nil
+		}
+		jars = append(jars, turbineJar)
+	}
+
+	jars = append(jars, extraJars...)
+
+	// Combine any static header libraries into classes-header.jar. If there is only
+	// one input jar this step will be skipped.
+	jars = append(jars, deps.staticHeaderJars...)
+
+	// we cannot skip the combine step for now if there is only one jar
+	// since we have to strip META-INF/TRANSITIVE dir from turbine.jar
+	combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
+	TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
+		false, nil, []string{"META-INF/TRANSITIVE"})
+	headerJar = combinedJar
+	jarjarHeaderJar = combinedJar
+
+	if j.expandJarjarRules != nil {
+		// Transform classes.jar into classes-jarjar.jar
+		jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
+		TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
+		jarjarHeaderJar = jarjarFile
+		if ctx.Failed() {
+			return nil, nil
+		}
+	}
+
+	return headerJar, jarjarHeaderJar
+}
+
+func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
+	classesJar android.Path, jarName string) android.OutputPath {
+
+	specs := j.jacocoModuleToZipCommand(ctx)
+
+	jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
+	instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName).OutputPath
+
+	jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
+
+	j.jacocoReportClassesFile = jacocoReportClassesFile
+
+	return instrumentedJar
+}
+
+func (j *Module) HeaderJars() android.Paths {
+	if j.headerJarFile == nil {
+		return nil
+	}
+	return android.Paths{j.headerJarFile}
+}
+
+func (j *Module) ImplementationJars() android.Paths {
+	if j.implementationJarFile == nil {
+		return nil
+	}
+	return android.Paths{j.implementationJarFile}
+}
+
+func (j *Module) DexJarBuildPath() android.Path {
+	return j.dexJarFile
+}
+
+func (j *Module) DexJarInstallPath() android.Path {
+	return j.installFile
+}
+
+func (j *Module) ImplementationAndResourcesJars() android.Paths {
+	if j.implementationAndResourcesJar == nil {
+		return nil
+	}
+	return android.Paths{j.implementationAndResourcesJar}
+}
+
+func (j *Module) AidlIncludeDirs() android.Paths {
+	// exportAidlIncludeDirs is type android.Paths already
+	return j.exportAidlIncludeDirs
+}
+
+func (j *Module) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
+	return j.classLoaderContexts
+}
+
+// Collect information for opening IDE project files in java/jdeps.go.
+func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
+	dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
+	dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
+	dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
+	dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
+	if j.expandJarjarRules != nil {
+		dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
+	}
+	dpInfo.Paths = append(dpInfo.Paths, j.modulePaths...)
+}
+
+func (j *Module) CompilerDeps() []string {
+	jdeps := []string{}
+	jdeps = append(jdeps, j.properties.Libs...)
+	jdeps = append(jdeps, j.properties.Static_libs...)
+	return jdeps
+}
+
+func (j *Module) hasCode(ctx android.ModuleContext) bool {
+	srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
+	return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
+}
+
+// Implements android.ApexModule
+func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
+	return j.depIsInSameApex(ctx, dep)
+}
+
+// Implements android.ApexModule
+func (j *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
+	sdkVersion android.ApiLevel) error {
+	sdkSpec := j.minSdkVersion()
+	if !sdkSpec.specified() {
+		return fmt.Errorf("min_sdk_version is not specified")
+	}
+	if sdkSpec.kind == sdkCore {
+		return nil
+	}
+	ver, err := sdkSpec.effectiveVersion(ctx)
+	if err != nil {
+		return err
+	}
+	if ver.ApiLevel(ctx).GreaterThan(sdkVersion) {
+		return fmt.Errorf("newer SDK(%v)", ver)
+	}
+	return nil
+}
+
+func (j *Module) Stem() string {
+	return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
+}
+
+// ConfigurationName returns the name of the module as used in build configuration.
+//
+// This is usually the same as BaseModuleName() except for the <x>.impl libraries created by
+// java_sdk_library in which case this is the BaseModuleName() without the ".impl" suffix,
+// i.e. just <x>.
+func (j *Module) ConfigurationName() string {
+	return proptools.StringDefault(j.deviceProperties.ConfigurationName, j.BaseModuleName())
+}
+
+func (j *Module) JacocoReportClassesFile() android.Path {
+	return j.jacocoReportClassesFile
+}
+
+func (j *Module) IsInstallable() bool {
+	return Bool(j.properties.Installable)
+}
+
+type sdkLinkType int
+
+const (
+	// TODO(jiyong) rename these for better readability. Make the allowed
+	// and disallowed link types explicit
+	// order is important here. See rank()
+	javaCore sdkLinkType = iota
+	javaSdk
+	javaSystem
+	javaModule
+	javaSystemServer
+	javaPlatform
+)
+
+func (lt sdkLinkType) String() string {
+	switch lt {
+	case javaCore:
+		return "core Java API"
+	case javaSdk:
+		return "Android API"
+	case javaSystem:
+		return "system API"
+	case javaModule:
+		return "module API"
+	case javaSystemServer:
+		return "system server API"
+	case javaPlatform:
+		return "private API"
+	default:
+		panic(fmt.Errorf("unrecognized linktype: %d", lt))
+	}
+}
+
+// rank determines the total order among sdkLinkType. An SDK link type of rank A can link to
+// another SDK link type of rank B only when B <= A. For example, a module linking to Android SDK
+// can't statically depend on modules that use Platform API.
+func (lt sdkLinkType) rank() int {
+	return int(lt)
+}
+
+type moduleWithSdkDep interface {
+	android.Module
+	getSdkLinkType(name string) (ret sdkLinkType, stubs bool)
+}
+
+func (m *Module) getSdkLinkType(name string) (ret sdkLinkType, stubs bool) {
+	switch name {
+	case "core.current.stubs", "legacy.core.platform.api.stubs", "stable.core.platform.api.stubs",
+		"stub-annotations", "private-stub-annotations-jar",
+		"core-lambda-stubs", "core-generated-annotation-stubs":
+		return javaCore, true
+	case "android_stubs_current":
+		return javaSdk, true
+	case "android_system_stubs_current":
+		return javaSystem, true
+	case "android_module_lib_stubs_current":
+		return javaModule, true
+	case "android_system_server_stubs_current":
+		return javaSystemServer, true
+	case "android_test_stubs_current":
+		return javaSystem, true
+	}
+
+	if stub, linkType := moduleStubLinkType(name); stub {
+		return linkType, true
+	}
+
+	ver := m.sdkVersion()
+	switch ver.kind {
+	case sdkCore:
+		return javaCore, false
+	case sdkSystem:
+		return javaSystem, false
+	case sdkPublic:
+		return javaSdk, false
+	case sdkModule:
+		return javaModule, false
+	case sdkSystemServer:
+		return javaSystemServer, false
+	case sdkPrivate, sdkNone, sdkCorePlatform, sdkTest:
+		return javaPlatform, false
+	}
+
+	if !ver.valid() {
+		panic(fmt.Errorf("sdk_version is invalid. got %q", ver.raw))
+	}
+	return javaSdk, false
+}
+
+// checkSdkLinkType make sures the given dependency doesn't have a lower SDK link type rank than
+// this module's. See the comment on rank() for details and an example.
+func (j *Module) checkSdkLinkType(
+	ctx android.ModuleContext, dep moduleWithSdkDep, tag dependencyTag) {
+	if ctx.Host() {
+		return
+	}
+
+	myLinkType, stubs := j.getSdkLinkType(ctx.ModuleName())
+	if stubs {
+		return
+	}
+	depLinkType, _ := dep.getSdkLinkType(ctx.OtherModuleName(dep))
+
+	if myLinkType.rank() < depLinkType.rank() {
+		ctx.ModuleErrorf("compiles against %v, but dependency %q is compiling against %v. "+
+			"In order to fix this, consider adjusting sdk_version: OR platform_apis: "+
+			"property of the source or target module so that target module is built "+
+			"with the same or smaller API set when compared to the source.",
+			myLinkType, ctx.OtherModuleName(dep), depLinkType)
+	}
+}
+
+func (j *Module) collectDeps(ctx android.ModuleContext) deps {
+	var deps deps
+
+	if ctx.Device() {
+		sdkDep := decodeSdkDep(ctx, sdkContext(j))
+		if sdkDep.invalidVersion {
+			ctx.AddMissingDependencies(sdkDep.bootclasspath)
+			ctx.AddMissingDependencies(sdkDep.java9Classpath)
+		} else if sdkDep.useFiles {
+			// sdkDep.jar is actually equivalent to turbine header.jar.
+			deps.classpath = append(deps.classpath, sdkDep.jars...)
+			deps.aidlPreprocess = sdkDep.aidl
+		} else {
+			deps.aidlPreprocess = sdkDep.aidl
+		}
+	}
+
+	sdkLinkType, _ := j.getSdkLinkType(ctx.ModuleName())
+
+	ctx.VisitDirectDeps(func(module android.Module) {
+		otherName := ctx.OtherModuleName(module)
+		tag := ctx.OtherModuleDependencyTag(module)
+
+		if IsJniDepTag(tag) {
+			// Handled by AndroidApp.collectAppDeps
+			return
+		}
+		if tag == certificateTag {
+			// Handled by AndroidApp.collectAppDeps
+			return
+		}
+
+		if dep, ok := module.(SdkLibraryDependency); ok {
+			switch tag {
+			case libTag:
+				deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
+			case staticLibTag:
+				ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
+			}
+		} else if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
+			dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
+			if sdkLinkType != javaPlatform &&
+				ctx.OtherModuleHasProvider(module, SyspropPublicStubInfoProvider) {
+				// dep is a sysprop implementation library, but this module is not linking against
+				// the platform, so it gets the sysprop public stubs library instead.  Replace
+				// dep with the JavaInfo from the SyspropPublicStubInfoProvider.
+				syspropDep := ctx.OtherModuleProvider(module, SyspropPublicStubInfoProvider).(SyspropPublicStubInfo)
+				dep = syspropDep.JavaInfo
+			}
+			switch tag {
+			case bootClasspathTag:
+				deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars...)
+			case libTag, instrumentationForTag:
+				deps.classpath = append(deps.classpath, dep.HeaderJars...)
+				deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs...)
+				addPlugins(&deps, dep.ExportedPlugins, dep.ExportedPluginClasses...)
+				deps.disableTurbine = deps.disableTurbine || dep.ExportedPluginDisableTurbine
+			case java9LibTag:
+				deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars...)
+			case staticLibTag:
+				deps.classpath = append(deps.classpath, dep.HeaderJars...)
+				deps.staticJars = append(deps.staticJars, dep.ImplementationJars...)
+				deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars...)
+				deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars...)
+				deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs...)
+				addPlugins(&deps, dep.ExportedPlugins, dep.ExportedPluginClasses...)
+				// Turbine doesn't run annotation processors, so any module that uses an
+				// annotation processor that generates API is incompatible with the turbine
+				// optimization.
+				deps.disableTurbine = deps.disableTurbine || dep.ExportedPluginDisableTurbine
+			case pluginTag:
+				if plugin, ok := module.(*Plugin); ok {
+					if plugin.pluginProperties.Processor_class != nil {
+						addPlugins(&deps, dep.ImplementationAndResourcesJars, *plugin.pluginProperties.Processor_class)
+					} else {
+						addPlugins(&deps, dep.ImplementationAndResourcesJars)
+					}
+					// Turbine doesn't run annotation processors, so any module that uses an
+					// annotation processor that generates API is incompatible with the turbine
+					// optimization.
+					deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
+				} else {
+					ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
+				}
+			case errorpronePluginTag:
+				if _, ok := module.(*Plugin); ok {
+					deps.errorProneProcessorPath = append(deps.errorProneProcessorPath, dep.ImplementationAndResourcesJars...)
+				} else {
+					ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
+				}
+			case exportedPluginTag:
+				if plugin, ok := module.(*Plugin); ok {
+					j.exportedPluginJars = append(j.exportedPluginJars, dep.ImplementationAndResourcesJars...)
+					if plugin.pluginProperties.Processor_class != nil {
+						j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
+					}
+					// Turbine doesn't run annotation processors, so any module that uses an
+					// annotation processor that generates API is incompatible with the turbine
+					// optimization.
+					j.exportedDisableTurbine = Bool(plugin.pluginProperties.Generates_api)
+				} else {
+					ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
+				}
+			case kotlinStdlibTag:
+				deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars...)
+			case kotlinAnnotationsTag:
+				deps.kotlinAnnotations = dep.HeaderJars
+			case syspropPublicStubDepTag:
+				// This is a sysprop implementation library, forward the JavaInfoProvider from
+				// the corresponding sysprop public stub library as SyspropPublicStubInfoProvider.
+				ctx.SetProvider(SyspropPublicStubInfoProvider, SyspropPublicStubInfo{
+					JavaInfo: dep,
+				})
+			}
+		} else if dep, ok := module.(android.SourceFileProducer); ok {
+			switch tag {
+			case libTag:
+				checkProducesJars(ctx, dep)
+				deps.classpath = append(deps.classpath, dep.Srcs()...)
+			case staticLibTag:
+				checkProducesJars(ctx, dep)
+				deps.classpath = append(deps.classpath, dep.Srcs()...)
+				deps.staticJars = append(deps.staticJars, dep.Srcs()...)
+				deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
+			}
+		} else {
+			switch tag {
+			case bootClasspathTag:
+				// If a system modules dependency has been added to the bootclasspath
+				// then add its libs to the bootclasspath.
+				sm := module.(SystemModulesProvider)
+				deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
+
+			case systemModulesTag:
+				if deps.systemModules != nil {
+					panic("Found two system module dependencies")
+				}
+				sm := module.(SystemModulesProvider)
+				outputDir, outputDeps := sm.OutputDirAndDeps()
+				deps.systemModules = &systemModules{outputDir, outputDeps}
+			}
+		}
+
+		addCLCFromDep(ctx, module, j.classLoaderContexts)
+	})
+
+	return deps
+}
+
+func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
+	deps.processorPath = append(deps.processorPath, pluginJars...)
+	deps.processorClasses = append(deps.processorClasses, pluginClasses...)
+}
+
+// TODO(b/132357300) Generalize SdkLibrarComponentDependency to non-SDK libraries and merge with
+// this interface.
+type ProvidesUsesLib interface {
+	ProvidesUsesLib() *string
+}
+
+func (j *Module) ProvidesUsesLib() *string {
+	return j.usesLibraryProperties.Provides_uses_lib
+}
diff --git a/java/builder.go b/java/builder.go
index 33206ce..fc740a8 100644
--- a/java/builder.go
+++ b/java/builder.go
@@ -40,7 +40,7 @@
 	// (if the rule produces .class files) or a .srcjar file (if the rule produces .java files).
 	// .srcjar files are unzipped into a temporary directory when compiled with javac.
 	// TODO(b/143658984): goma can't handle the --system argument to javac.
-	javac, javacRE = remoteexec.MultiCommandStaticRules(pctx, "javac",
+	javac, javacRE = pctx.MultiCommandRemoteStaticRules("javac",
 		blueprint.RuleParams{
 			Command: `rm -rf "$outDir" "$annoDir" "$srcJarDir" "$out" && mkdir -p "$outDir" "$annoDir" "$srcJarDir" && ` +
 				`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
@@ -129,7 +129,7 @@
 		},
 		"abis", "allow-prereleased", "screen-densities", "sdk-version", "stem", "apkcerts", "partition")
 
-	turbine, turbineRE = remoteexec.StaticRules(pctx, "turbine",
+	turbine, turbineRE = pctx.RemoteStaticRules("turbine",
 		blueprint.RuleParams{
 			Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
 				`$reTemplate${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.TurbineJar} --output $out.tmp ` +
@@ -157,7 +157,7 @@
 			Platform:          map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
 		}, []string{"javacFlags", "bootClasspath", "classpath", "srcJars", "outDir", "javaVersion"}, []string{"implicits"})
 
-	jar, jarRE = remoteexec.StaticRules(pctx, "jar",
+	jar, jarRE = pctx.RemoteStaticRules("jar",
 		blueprint.RuleParams{
 			Command:        `$reTemplate${config.SoongZipCmd} -jar -o $out @$out.rsp`,
 			CommandDeps:    []string{"${config.SoongZipCmd}"},
@@ -172,7 +172,7 @@
 			Platform:     map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
 		}, []string{"jarArgs"}, nil)
 
-	zip, zipRE = remoteexec.StaticRules(pctx, "zip",
+	zip, zipRE = pctx.RemoteStaticRules("zip",
 		blueprint.RuleParams{
 			Command:        `${config.SoongZipCmd} -o $out @$out.rsp`,
 			CommandDeps:    []string{"${config.SoongZipCmd}"},
@@ -244,7 +244,6 @@
 func init() {
 	pctx.Import("android/soong/android")
 	pctx.Import("android/soong/java/config")
-	pctx.Import("android/soong/remoteexec")
 }
 
 type javaBuilderFlags struct {
diff --git a/java/config/config.go b/java/config/config.go
index 31e2b0f..30c6f91 100644
--- a/java/config/config.go
+++ b/java/config/config.go
@@ -149,14 +149,14 @@
 	pctx.HostBinToolVariable("SoongJavacWrapper", "soong_javac_wrapper")
 	pctx.HostBinToolVariable("DexpreoptGen", "dexpreopt_gen")
 
-	pctx.VariableFunc("REJavaPool", remoteexec.EnvOverrideFunc("RBE_JAVA_POOL", "java16"))
-	pctx.VariableFunc("REJavacExecStrategy", remoteexec.EnvOverrideFunc("RBE_JAVAC_EXEC_STRATEGY", remoteexec.RemoteLocalFallbackExecStrategy))
-	pctx.VariableFunc("RED8ExecStrategy", remoteexec.EnvOverrideFunc("RBE_D8_EXEC_STRATEGY", remoteexec.RemoteLocalFallbackExecStrategy))
-	pctx.VariableFunc("RER8ExecStrategy", remoteexec.EnvOverrideFunc("RBE_R8_EXEC_STRATEGY", remoteexec.RemoteLocalFallbackExecStrategy))
-	pctx.VariableFunc("RETurbineExecStrategy", remoteexec.EnvOverrideFunc("RBE_TURBINE_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
-	pctx.VariableFunc("RESignApkExecStrategy", remoteexec.EnvOverrideFunc("RBE_SIGNAPK_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
-	pctx.VariableFunc("REJarExecStrategy", remoteexec.EnvOverrideFunc("RBE_JAR_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
-	pctx.VariableFunc("REZipExecStrategy", remoteexec.EnvOverrideFunc("RBE_ZIP_EXEC_STRATEGY", remoteexec.LocalExecStrategy))
+	pctx.StaticVariableWithEnvOverride("REJavaPool", "RBE_JAVA_POOL", "java16")
+	pctx.StaticVariableWithEnvOverride("REJavacExecStrategy", "RBE_JAVAC_EXEC_STRATEGY", remoteexec.RemoteLocalFallbackExecStrategy)
+	pctx.StaticVariableWithEnvOverride("RED8ExecStrategy", "RBE_D8_EXEC_STRATEGY", remoteexec.RemoteLocalFallbackExecStrategy)
+	pctx.StaticVariableWithEnvOverride("RER8ExecStrategy", "RBE_R8_EXEC_STRATEGY", remoteexec.RemoteLocalFallbackExecStrategy)
+	pctx.StaticVariableWithEnvOverride("RETurbineExecStrategy", "RBE_TURBINE_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
+	pctx.StaticVariableWithEnvOverride("RESignApkExecStrategy", "RBE_SIGNAPK_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
+	pctx.StaticVariableWithEnvOverride("REJarExecStrategy", "RBE_JAR_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
+	pctx.StaticVariableWithEnvOverride("REZipExecStrategy", "RBE_ZIP_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
 
 	pctx.HostJavaToolVariable("JacocoCLIJar", "jacoco-cli.jar")
 
diff --git a/java/dex.go b/java/dex.go
index b2a998f..b042f13 100644
--- a/java/dex.go
+++ b/java/dex.go
@@ -83,7 +83,7 @@
 	return BoolDefault(d.dexProperties.Optimize.Enabled, d.dexProperties.Optimize.EnabledByDefault)
 }
 
-var d8, d8RE = remoteexec.MultiCommandStaticRules(pctx, "d8",
+var d8, d8RE = pctx.MultiCommandRemoteStaticRules("d8",
 	blueprint.RuleParams{
 		Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
 			`$d8Template${config.D8Cmd} ${config.DexFlags} --output $outDir $d8Flags $in && ` +
@@ -111,7 +111,7 @@
 		},
 	}, []string{"outDir", "d8Flags", "zipFlags"}, nil)
 
-var r8, r8RE = remoteexec.MultiCommandStaticRules(pctx, "r8",
+var r8, r8RE = pctx.MultiCommandRemoteStaticRules("r8",
 	blueprint.RuleParams{
 		Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
 			`rm -f "$outDict" && rm -rf "${outUsageDir}" && ` +
diff --git a/java/dexpreopt_bootjars_test.go b/java/dexpreopt_bootjars_test.go
index 1b910fa..aadf6ad 100644
--- a/java/dexpreopt_bootjars_test.go
+++ b/java/dexpreopt_bootjars_test.go
@@ -44,8 +44,11 @@
 	`
 
 	result := javaFixtureFactory.
-		Extend(dexpreopt.FixtureSetBootJars("platform:foo", "platform:bar", "platform:baz")).
-		RunTestWithBp(t, bp)
+		Extend(
+			PrepareForTestWithJavaSdkLibraryFiles,
+			FixtureWithLastReleaseApis("foo"),
+			dexpreopt.FixtureSetBootJars("platform:foo", "platform:bar", "platform:baz"),
+		).RunTestWithBp(t, bp)
 
 	dexpreoptBootJars := result.SingletonForTests("dex_bootjars")
 	rule := dexpreoptBootJars.Output(ruleFile)
diff --git a/java/droiddoc.go b/java/droiddoc.go
index da13c62..a892b36 100644
--- a/java/droiddoc.go
+++ b/java/droiddoc.go
@@ -1235,7 +1235,7 @@
 			ToolchainInputs:      []string{config.JavaCmd(ctx).String()},
 			Platform:             map[string]string{remoteexec.PoolKey: pool},
 			EnvironmentVariables: []string{"ANDROID_SDK_HOME"},
-		}).NoVarTemplate(ctx.Config()))
+		}).NoVarTemplate(ctx.Config().RBEWrapper()))
 	}
 
 	cmd.BuiltTool("metalava").
@@ -1678,14 +1678,17 @@
 func zipSyncCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
 	srcJarDir android.ModuleOutPath, srcJars android.Paths) android.OutputPath {
 
-	rule.Command().Text("rm -rf").Text(srcJarDir.String())
-	rule.Command().Text("mkdir -p").Text(srcJarDir.String())
+	cmd := rule.Command()
+	cmd.Text("rm -rf").Text(cmd.PathForOutput(srcJarDir))
+	cmd = rule.Command()
+	cmd.Text("mkdir -p").Text(cmd.PathForOutput(srcJarDir))
 	srcJarList := srcJarDir.Join(ctx, "list")
 
 	rule.Temporary(srcJarList)
 
-	rule.Command().BuiltTool("zipsync").
-		FlagWithArg("-d ", srcJarDir.String()).
+	cmd = rule.Command()
+	cmd.BuiltTool("zipsync").
+		FlagWithArg("-d ", cmd.PathForOutput(srcJarDir)).
 		FlagWithOutput("-l ", srcJarList).
 		FlagWithArg("-f ", `"*.java"`).
 		Inputs(srcJars)
diff --git a/java/gen.go b/java/gen.go
index 4f928d5..445a2d8 100644
--- a/java/gen.go
+++ b/java/gen.go
@@ -174,6 +174,12 @@
 	logtags() android.Paths
 }
 
+func (j *Module) logtags() android.Paths {
+	return j.logtagsSrcs
+}
+
+var _ logtagsProducer = (*Module)(nil)
+
 type logtagsSingleton struct{}
 
 func (l *logtagsSingleton) GenerateBuildActions(ctx android.SingletonContext) {
diff --git a/java/java.go b/java/java.go
index 8c714ee..9786947 100644
--- a/java/java.go
+++ b/java/java.go
@@ -21,11 +21,9 @@
 import (
 	"fmt"
 	"path/filepath"
-	"strconv"
 	"strings"
 
 	"github.com/google/blueprint"
-	"github.com/google/blueprint/pathtools"
 	"github.com/google/blueprint/proptools"
 
 	"android/soong/android"
@@ -122,441 +120,6 @@
 
 }
 
-// TODO:
-// Autogenerated files:
-//  Renderscript
-// Post-jar passes:
-//  Proguard
-// Rmtypedefs
-// DroidDoc
-// Findbugs
-
-// Properties that are common to most Java modules, i.e. whether it's a host or device module.
-type CommonProperties struct {
-	// list of source files used to compile the Java module.  May be .java, .kt, .logtags, .proto,
-	// or .aidl files.
-	Srcs []string `android:"path,arch_variant"`
-
-	// list Kotlin of source files containing Kotlin code that should be treated as common code in
-	// a codebase that supports Kotlin multiplatform.  See
-	// https://kotlinlang.org/docs/reference/multiplatform.html.  May be only be .kt files.
-	Common_srcs []string `android:"path,arch_variant"`
-
-	// list of source files that should not be used to build the Java module.
-	// This is most useful in the arch/multilib variants to remove non-common files
-	Exclude_srcs []string `android:"path,arch_variant"`
-
-	// list of directories containing Java resources
-	Java_resource_dirs []string `android:"arch_variant"`
-
-	// list of directories that should be excluded from java_resource_dirs
-	Exclude_java_resource_dirs []string `android:"arch_variant"`
-
-	// list of files to use as Java resources
-	Java_resources []string `android:"path,arch_variant"`
-
-	// list of files that should be excluded from java_resources and java_resource_dirs
-	Exclude_java_resources []string `android:"path,arch_variant"`
-
-	// list of module-specific flags that will be used for javac compiles
-	Javacflags []string `android:"arch_variant"`
-
-	// list of module-specific flags that will be used for kotlinc compiles
-	Kotlincflags []string `android:"arch_variant"`
-
-	// list of of java libraries that will be in the classpath
-	Libs []string `android:"arch_variant"`
-
-	// list of java libraries that will be compiled into the resulting jar
-	Static_libs []string `android:"arch_variant"`
-
-	// manifest file to be included in resulting jar
-	Manifest *string `android:"path"`
-
-	// if not blank, run jarjar using the specified rules file
-	Jarjar_rules *string `android:"path,arch_variant"`
-
-	// If not blank, set the java version passed to javac as -source and -target
-	Java_version *string
-
-	// If set to true, allow this module to be dexed and installed on devices.  Has no
-	// effect on host modules, which are always considered installable.
-	Installable *bool
-
-	// If set to true, include sources used to compile the module in to the final jar
-	Include_srcs *bool
-
-	// If not empty, classes are restricted to the specified packages and their sub-packages.
-	// This restriction is checked after applying jarjar rules and including static libs.
-	Permitted_packages []string
-
-	// List of modules to use as annotation processors
-	Plugins []string
-
-	// List of modules to export to libraries that directly depend on this library as annotation
-	// processors.  Note that if the plugins set generates_api: true this will disable the turbine
-	// optimization on modules that depend on this module, which will reduce parallelism and cause
-	// more recompilation.
-	Exported_plugins []string
-
-	// The number of Java source entries each Javac instance can process
-	Javac_shard_size *int64
-
-	// Add host jdk tools.jar to bootclasspath
-	Use_tools_jar *bool
-
-	Openjdk9 struct {
-		// List of source files that should only be used when passing -source 1.9 or higher
-		Srcs []string `android:"path"`
-
-		// List of javac flags that should only be used when passing -source 1.9 or higher
-		Javacflags []string
-	}
-
-	// When compiling language level 9+ .java code in packages that are part of
-	// a system module, patch_module names the module that your sources and
-	// dependencies should be patched into. The Android runtime currently
-	// doesn't implement the JEP 261 module system so this option is only
-	// supported at compile time. It should only be needed to compile tests in
-	// packages that exist in libcore and which are inconvenient to move
-	// elsewhere.
-	Patch_module *string `android:"arch_variant"`
-
-	Jacoco struct {
-		// List of classes to include for instrumentation with jacoco to collect coverage
-		// information at runtime when building with coverage enabled.  If unset defaults to all
-		// classes.
-		// Supports '*' as the last character of an entry in the list as a wildcard match.
-		// If preceded by '.' it matches all classes in the package and subpackages, otherwise
-		// it matches classes in the package that have the class name as a prefix.
-		Include_filter []string
-
-		// List of classes to exclude from instrumentation with jacoco to collect coverage
-		// information at runtime when building with coverage enabled.  Overrides classes selected
-		// by the include_filter property.
-		// Supports '*' as the last character of an entry in the list as a wildcard match.
-		// If preceded by '.' it matches all classes in the package and subpackages, otherwise
-		// it matches classes in the package that have the class name as a prefix.
-		Exclude_filter []string
-	}
-
-	Errorprone struct {
-		// List of javac flags that should only be used when running errorprone.
-		Javacflags []string
-
-		// List of java_plugin modules that provide extra errorprone checks.
-		Extra_check_modules []string
-	}
-
-	Proto struct {
-		// List of extra options that will be passed to the proto generator.
-		Output_params []string
-	}
-
-	Instrument bool `blueprint:"mutated"`
-
-	// List of files to include in the META-INF/services folder of the resulting jar.
-	Services []string `android:"path,arch_variant"`
-
-	// If true, package the kotlin stdlib into the jar.  Defaults to true.
-	Static_kotlin_stdlib *bool `android:"arch_variant"`
-
-	// A list of java_library instances that provide additional hiddenapi annotations for the library.
-	Hiddenapi_additional_annotations []string
-}
-
-// Properties that are specific to device modules. Host module factories should not add these when
-// constructing a new module.
-type DeviceProperties struct {
-	// if not blank, set to the version of the sdk to compile against.
-	// Defaults to compiling against the current platform.
-	Sdk_version *string
-
-	// if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
-	// Defaults to sdk_version if not set.
-	Min_sdk_version *string
-
-	// if not blank, set the targetSdkVersion in the AndroidManifest.xml.
-	// Defaults to sdk_version if not set.
-	Target_sdk_version *string
-
-	// Whether to compile against the platform APIs instead of an SDK.
-	// If true, then sdk_version must be empty. The value of this field
-	// is ignored when module's type isn't android_app.
-	Platform_apis *bool
-
-	Aidl struct {
-		// Top level directories to pass to aidl tool
-		Include_dirs []string
-
-		// Directories rooted at the Android.bp file to pass to aidl tool
-		Local_include_dirs []string
-
-		// directories that should be added as include directories for any aidl sources of modules
-		// that depend on this module, as well as to aidl for this module.
-		Export_include_dirs []string
-
-		// whether to generate traces (for systrace) for this interface
-		Generate_traces *bool
-
-		// whether to generate Binder#GetTransaction name method.
-		Generate_get_transaction_name *bool
-
-		// list of flags that will be passed to the AIDL compiler
-		Flags []string
-	}
-
-	// If true, export a copy of the module as a -hostdex module for host testing.
-	Hostdex *bool
-
-	Target struct {
-		Hostdex struct {
-			// Additional required dependencies to add to -hostdex modules.
-			Required []string
-		}
-	}
-
-	// When targeting 1.9 and above, override the modules to use with --system,
-	// otherwise provides defaults libraries to add to the bootclasspath.
-	System_modules *string
-
-	// The name of the module as used in build configuration.
-	//
-	// Allows a library to separate its actual name from the name used in
-	// build configuration, e.g.ctx.Config().BootJars().
-	ConfigurationName *string `blueprint:"mutated"`
-
-	// set the name of the output
-	Stem *string
-
-	IsSDKLibrary bool `blueprint:"mutated"`
-
-	// If true, generate the signature file of APK Signing Scheme V4, along side the signed APK file.
-	// Defaults to false.
-	V4_signature *bool
-
-	// Only for libraries created by a sysprop_library module, SyspropPublicStub is the name of the
-	// public stubs library.
-	SyspropPublicStub string `blueprint:"mutated"`
-}
-
-// Functionality common to Module and Import
-//
-// It is embedded in Module so its functionality can be used by methods in Module
-// but it is currently only initialized by Import and Library.
-type embeddableInModuleAndImport struct {
-
-	// Functionality related to this being used as a component of a java_sdk_library.
-	EmbeddableSdkLibraryComponent
-}
-
-func (e *embeddableInModuleAndImport) initModuleAndImport(moduleBase *android.ModuleBase) {
-	e.initSdkLibraryComponent(moduleBase)
-}
-
-// Module/Import's DepIsInSameApex(...) delegates to this method.
-//
-// This cannot implement DepIsInSameApex(...) directly as that leads to ambiguity with
-// the one provided by ApexModuleBase.
-func (e *embeddableInModuleAndImport) depIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
-	// dependencies other than the static linkage are all considered crossing APEX boundary
-	if staticLibTag == ctx.OtherModuleDependencyTag(dep) {
-		return true
-	}
-	return false
-}
-
-// Module contains the properties and members used by all java module types
-type Module struct {
-	android.ModuleBase
-	android.DefaultableModuleBase
-	android.ApexModuleBase
-	android.SdkBase
-
-	// Functionality common to Module and Import.
-	embeddableInModuleAndImport
-
-	properties       CommonProperties
-	protoProperties  android.ProtoProperties
-	deviceProperties DeviceProperties
-
-	// jar file containing header classes including static library dependencies, suitable for
-	// inserting into the bootclasspath/classpath of another compile
-	headerJarFile android.Path
-
-	// jar file containing implementation classes including static library dependencies but no
-	// resources
-	implementationJarFile android.Path
-
-	// jar file containing only resources including from static library dependencies
-	resourceJar android.Path
-
-	// args and dependencies to package source files into a srcjar
-	srcJarArgs []string
-	srcJarDeps android.Paths
-
-	// jar file containing implementation classes and resources including static library
-	// dependencies
-	implementationAndResourcesJar android.Path
-
-	// output file containing classes.dex and resources
-	dexJarFile android.Path
-
-	// output file containing uninstrumented classes that will be instrumented by jacoco
-	jacocoReportClassesFile android.Path
-
-	// output file of the module, which may be a classes jar or a dex jar
-	outputFile       android.Path
-	extraOutputFiles android.Paths
-
-	exportAidlIncludeDirs android.Paths
-
-	logtagsSrcs android.Paths
-
-	// installed file for binary dependency
-	installFile android.Path
-
-	// list of .java files and srcjars that was passed to javac
-	compiledJavaSrcs android.Paths
-	compiledSrcJars  android.Paths
-
-	// manifest file to use instead of properties.Manifest
-	overrideManifest android.OptionalPath
-
-	// map of SDK version to class loader context
-	classLoaderContexts dexpreopt.ClassLoaderContextMap
-
-	// list of plugins that this java module is exporting
-	exportedPluginJars android.Paths
-
-	// list of plugins that this java module is exporting
-	exportedPluginClasses []string
-
-	// if true, the exported plugins generate API and require disabling turbine.
-	exportedDisableTurbine bool
-
-	// list of source files, collected from srcFiles with unique java and all kt files,
-	// will be used by android.IDEInfo struct
-	expandIDEInfoCompiledSrcs []string
-
-	// expanded Jarjar_rules
-	expandJarjarRules android.Path
-
-	// list of additional targets for checkbuild
-	additionalCheckedModules android.Paths
-
-	// Extra files generated by the module type to be added as java resources.
-	extraResources android.Paths
-
-	hiddenAPI
-	dexer
-	dexpreopter
-	usesLibrary
-	linter
-
-	// list of the xref extraction files
-	kytheFiles android.Paths
-
-	// Collect the module directory for IDE info in java/jdeps.go.
-	modulePaths []string
-
-	hideApexVariantFromMake bool
-}
-
-func (j *Module) CheckStableSdkVersion() error {
-	sdkVersion := j.sdkVersion()
-	if sdkVersion.stable() {
-		return nil
-	}
-	if sdkVersion.kind == sdkCorePlatform {
-		if useLegacyCorePlatformApiByName(j.BaseModuleName()) {
-			return fmt.Errorf("non stable SDK %v - uses legacy core platform", sdkVersion)
-		} else {
-			// Treat stable core platform as stable.
-			return nil
-		}
-	} else {
-		return fmt.Errorf("non stable SDK %v", sdkVersion)
-	}
-}
-
-// checkSdkVersions enforces restrictions around SDK dependencies.
-func (j *Module) checkSdkVersions(ctx android.ModuleContext) {
-	if j.RequiresStableAPIs(ctx) {
-		if sc, ok := ctx.Module().(sdkContext); ok {
-			if !sc.sdkVersion().specified() {
-				ctx.PropertyErrorf("sdk_version",
-					"sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
-			}
-		}
-	}
-
-	// Make sure this module doesn't statically link to modules with lower-ranked SDK link type.
-	// See rank() for details.
-	ctx.VisitDirectDeps(func(module android.Module) {
-		tag := ctx.OtherModuleDependencyTag(module)
-		switch module.(type) {
-		// TODO(satayev): cover other types as well, e.g. imports
-		case *Library, *AndroidLibrary:
-			switch tag {
-			case bootClasspathTag, libTag, staticLibTag, java9LibTag:
-				j.checkSdkLinkType(ctx, module.(moduleWithSdkDep), tag.(dependencyTag))
-			}
-		}
-	})
-}
-
-func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
-	if sc, ok := ctx.Module().(sdkContext); ok {
-		usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
-		sdkVersionSpecified := sc.sdkVersion().specified()
-		if usePlatformAPI && sdkVersionSpecified {
-			ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
-		} else if !usePlatformAPI && !sdkVersionSpecified {
-			ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
-		}
-
-	}
-}
-
-func (j *Module) addHostProperties() {
-	j.AddProperties(
-		&j.properties,
-		&j.protoProperties,
-		&j.usesLibraryProperties,
-	)
-}
-
-func (j *Module) addHostAndDeviceProperties() {
-	j.addHostProperties()
-	j.AddProperties(
-		&j.deviceProperties,
-		&j.dexer.dexProperties,
-		&j.dexpreoptProperties,
-		&j.linter.properties,
-	)
-}
-
-func (j *Module) OutputFiles(tag string) (android.Paths, error) {
-	switch tag {
-	case "":
-		return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
-	case android.DefaultDistTag:
-		return android.Paths{j.outputFile}, nil
-	case ".jar":
-		return android.Paths{j.implementationAndResourcesJar}, nil
-	case ".proguard_map":
-		if j.dexer.proguardDictionary.Valid() {
-			return android.Paths{j.dexer.proguardDictionary.Path()}, nil
-		}
-		return nil, fmt.Errorf("%q was requested, but no output file was found.", tag)
-	default:
-		return nil, fmt.Errorf("unsupported module reference tag %q", tag)
-	}
-}
-
-var _ android.OutputFileProducer = (*Module)(nil)
-
 // JavaInfo contains information about a java module for use by modules that depend on it.
 type JavaInfo struct {
 	// HeaderJars is a list of jars that can be passed as the javac classpath in order to link
@@ -627,6 +190,7 @@
 	ClassLoaderContexts() dexpreopt.ClassLoaderContextMap
 }
 
+// TODO(jungjw): Move this to kythe.go once it's created.
 type xref interface {
 	XrefJavaFiles() android.Paths
 }
@@ -635,24 +199,6 @@
 	return j.kytheFiles
 }
 
-func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
-	initJavaModule(module, hod, false)
-}
-
-func InitJavaModuleMultiTargets(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
-	initJavaModule(module, hod, true)
-}
-
-func initJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported, multiTargets bool) {
-	multilib := android.MultilibCommon
-	if multiTargets {
-		android.InitAndroidMultiTargetsArchModule(module, hod, multilib)
-	} else {
-		android.InitAndroidArchModule(module, hod, multilib)
-	}
-	android.InitDefaultableModule(module)
-}
-
 type dependencyTag struct {
 	blueprint.BaseDependencyTag
 	name string
@@ -758,70 +304,6 @@
 	unstrippedFile android.Path
 }
 
-func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
-	return j.properties.Instrument &&
-		ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") &&
-		ctx.DeviceConfig().JavaCoverageEnabledForPath(ctx.ModuleDir())
-}
-
-func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
-	return j.shouldInstrument(ctx) &&
-		(ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
-			ctx.Config().UnbundledBuild())
-}
-
-func (j *Module) shouldInstrumentInApex(ctx android.BaseModuleContext) bool {
-	// Force enable the instrumentation for java code that is built for APEXes ...
-	// except for the jacocoagent itself (because instrumenting jacocoagent using jacocoagent
-	// doesn't make sense) or framework libraries (e.g. libraries found in the InstrumentFrameworkModules list) unless EMMA_INSTRUMENT_FRAMEWORK is true.
-	apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
-	isJacocoAgent := ctx.ModuleName() == "jacocoagent"
-	if j.DirectlyInAnyApex() && !isJacocoAgent && !apexInfo.IsForPlatform() {
-		if !inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
-			return true
-		} else if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
-			return true
-		}
-	}
-	return false
-}
-
-func (j *Module) sdkVersion() sdkSpec {
-	return sdkSpecFrom(String(j.deviceProperties.Sdk_version))
-}
-
-func (j *Module) systemModules() string {
-	return proptools.String(j.deviceProperties.System_modules)
-}
-
-func (j *Module) minSdkVersion() sdkSpec {
-	if j.deviceProperties.Min_sdk_version != nil {
-		return sdkSpecFrom(*j.deviceProperties.Min_sdk_version)
-	}
-	return j.sdkVersion()
-}
-
-func (j *Module) targetSdkVersion() sdkSpec {
-	if j.deviceProperties.Target_sdk_version != nil {
-		return sdkSpecFrom(*j.deviceProperties.Target_sdk_version)
-	}
-	return j.sdkVersion()
-}
-
-func (j *Module) MinSdkVersion() string {
-	return j.minSdkVersion().version.String()
-}
-
-func (j *Module) AvailableFor(what string) bool {
-	if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
-		// Exception: for hostdex: true libraries, the platform variant is created
-		// even if it's not marked as available to platform. In that case, the platform
-		// variant is used only for the hostdex and not installed to the device.
-		return true
-	}
-	return j.ApexModuleBase.AvailableFor(what)
-}
-
 func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext sdkContext, d dexer) {
 	sdkDep := decodeSdkDep(ctx, sdkContext)
 	if sdkDep.useModule {
@@ -840,155 +322,6 @@
 	}
 }
 
-func (j *Module) deps(ctx android.BottomUpMutatorContext) {
-	if ctx.Device() {
-		j.linter.deps(ctx)
-
-		sdkDeps(ctx, sdkContext(j), j.dexer)
-
-		if j.deviceProperties.SyspropPublicStub != "" {
-			// This is a sysprop implementation library that has a corresponding sysprop public
-			// stubs library, and a dependency on it so that dependencies on the implementation can
-			// be forwarded to the public stubs library when necessary.
-			ctx.AddVariationDependencies(nil, syspropPublicStubDepTag, j.deviceProperties.SyspropPublicStub)
-		}
-	}
-
-	libDeps := ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
-	ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
-
-	// Add dependency on libraries that provide additional hidden api annotations.
-	ctx.AddVariationDependencies(nil, hiddenApiAnnotationsTag, j.properties.Hiddenapi_additional_annotations...)
-
-	if ctx.DeviceConfig().VndkVersion() != "" && ctx.Config().EnforceInterPartitionJavaSdkLibrary() {
-		// Require java_sdk_library at inter-partition java dependency to ensure stable
-		// interface between partitions. If inter-partition java_library dependency is detected,
-		// raise build error because java_library doesn't have a stable interface.
-		//
-		// Inputs:
-		//    PRODUCT_ENFORCE_INTER_PARTITION_JAVA_SDK_LIBRARY
-		//      if true, enable enforcement
-		//    PRODUCT_INTER_PARTITION_JAVA_LIBRARY_ALLOWLIST
-		//      exception list of java_library names to allow inter-partition dependency
-		for idx := range j.properties.Libs {
-			if libDeps[idx] == nil {
-				continue
-			}
-
-			if javaDep, ok := libDeps[idx].(javaSdkLibraryEnforceContext); ok {
-				// java_sdk_library is always allowed at inter-partition dependency.
-				// So, skip check.
-				if _, ok := javaDep.(*SdkLibrary); ok {
-					continue
-				}
-
-				j.checkPartitionsForJavaDependency(ctx, "libs", javaDep)
-			}
-		}
-	}
-
-	// For library dependencies that are component libraries (like stubs), add the implementation
-	// as a dependency (dexpreopt needs to be against the implementation library, not stubs).
-	for _, dep := range libDeps {
-		if dep != nil {
-			if component, ok := dep.(SdkLibraryComponentDependency); ok {
-				if lib := component.OptionalSdkLibraryImplementation(); lib != nil {
-					ctx.AddVariationDependencies(nil, usesLibTag, *lib)
-				}
-			}
-		}
-	}
-
-	ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
-	ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), errorpronePluginTag, j.properties.Errorprone.Extra_check_modules...)
-	ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
-
-	android.ProtoDeps(ctx, &j.protoProperties)
-	if j.hasSrcExt(".proto") {
-		protoDeps(ctx, &j.protoProperties)
-	}
-
-	if j.hasSrcExt(".kt") {
-		// TODO(ccross): move this to a mutator pass that can tell if generated sources contain
-		// Kotlin files
-		ctx.AddVariationDependencies(nil, kotlinStdlibTag,
-			"kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
-		if len(j.properties.Plugins) > 0 {
-			ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
-		}
-	}
-
-	// Framework libraries need special handling in static coverage builds: they should not have
-	// static dependency on jacoco, otherwise there would be multiple conflicting definitions of
-	// the same jacoco classes coming from different bootclasspath jars.
-	if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
-		if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
-			j.properties.Instrument = true
-		}
-	} else if j.shouldInstrumentStatic(ctx) {
-		ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
-	}
-}
-
-func hasSrcExt(srcs []string, ext string) bool {
-	for _, src := range srcs {
-		if filepath.Ext(src) == ext {
-			return true
-		}
-	}
-
-	return false
-}
-
-func (j *Module) hasSrcExt(ext string) bool {
-	return hasSrcExt(j.properties.Srcs, ext)
-}
-
-func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
-	aidlIncludeDirs android.Paths) (string, android.Paths) {
-
-	aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
-	aidlIncludes = append(aidlIncludes,
-		android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
-	aidlIncludes = append(aidlIncludes,
-		android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
-
-	var flags []string
-	var deps android.Paths
-
-	flags = append(flags, j.deviceProperties.Aidl.Flags...)
-
-	if aidlPreprocess.Valid() {
-		flags = append(flags, "-p"+aidlPreprocess.String())
-		deps = append(deps, aidlPreprocess.Path())
-	} else if len(aidlIncludeDirs) > 0 {
-		flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
-	}
-
-	if len(j.exportAidlIncludeDirs) > 0 {
-		flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
-	}
-
-	if len(aidlIncludes) > 0 {
-		flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
-	}
-
-	flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
-	if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
-		flags = append(flags, "-I"+src.String())
-	}
-
-	if Bool(j.deviceProperties.Aidl.Generate_traces) {
-		flags = append(flags, "-t")
-	}
-
-	if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
-		flags = append(flags, "--transaction_names")
-	}
-
-	return strings.Join(flags, " "), deps
-}
-
 type deps struct {
 	classpath               classpath
 	java9Classpath          classpath
@@ -1019,272 +352,6 @@
 	}
 }
 
-type sdkLinkType int
-
-const (
-	// TODO(jiyong) rename these for better readability. Make the allowed
-	// and disallowed link types explicit
-	// order is important here. See rank()
-	javaCore sdkLinkType = iota
-	javaSdk
-	javaSystem
-	javaModule
-	javaSystemServer
-	javaPlatform
-)
-
-func (lt sdkLinkType) String() string {
-	switch lt {
-	case javaCore:
-		return "core Java API"
-	case javaSdk:
-		return "Android API"
-	case javaSystem:
-		return "system API"
-	case javaModule:
-		return "module API"
-	case javaSystemServer:
-		return "system server API"
-	case javaPlatform:
-		return "private API"
-	default:
-		panic(fmt.Errorf("unrecognized linktype: %d", lt))
-	}
-}
-
-// rank determines the total order among sdkLinkType. An SDK link type of rank A can link to
-// another SDK link type of rank B only when B <= A. For example, a module linking to Android SDK
-// can't statically depend on modules that use Platform API.
-func (lt sdkLinkType) rank() int {
-	return int(lt)
-}
-
-type moduleWithSdkDep interface {
-	android.Module
-	getSdkLinkType(name string) (ret sdkLinkType, stubs bool)
-}
-
-func (m *Module) getSdkLinkType(name string) (ret sdkLinkType, stubs bool) {
-	switch name {
-	case "core.current.stubs", "legacy.core.platform.api.stubs", "stable.core.platform.api.stubs",
-		"stub-annotations", "private-stub-annotations-jar",
-		"core-lambda-stubs", "core-generated-annotation-stubs":
-		return javaCore, true
-	case "android_stubs_current":
-		return javaSdk, true
-	case "android_system_stubs_current":
-		return javaSystem, true
-	case "android_module_lib_stubs_current":
-		return javaModule, true
-	case "android_system_server_stubs_current":
-		return javaSystemServer, true
-	case "android_test_stubs_current":
-		return javaSystem, true
-	}
-
-	if stub, linkType := moduleStubLinkType(name); stub {
-		return linkType, true
-	}
-
-	ver := m.sdkVersion()
-	switch ver.kind {
-	case sdkCore:
-		return javaCore, false
-	case sdkSystem:
-		return javaSystem, false
-	case sdkPublic:
-		return javaSdk, false
-	case sdkModule:
-		return javaModule, false
-	case sdkSystemServer:
-		return javaSystemServer, false
-	case sdkPrivate, sdkNone, sdkCorePlatform, sdkTest:
-		return javaPlatform, false
-	}
-
-	if !ver.valid() {
-		panic(fmt.Errorf("sdk_version is invalid. got %q", ver.raw))
-	}
-	return javaSdk, false
-}
-
-// checkSdkLinkType make sures the given dependency doesn't have a lower SDK link type rank than
-// this module's. See the comment on rank() for details and an example.
-func (j *Module) checkSdkLinkType(
-	ctx android.ModuleContext, dep moduleWithSdkDep, tag dependencyTag) {
-	if ctx.Host() {
-		return
-	}
-
-	myLinkType, stubs := j.getSdkLinkType(ctx.ModuleName())
-	if stubs {
-		return
-	}
-	depLinkType, _ := dep.getSdkLinkType(ctx.OtherModuleName(dep))
-
-	if myLinkType.rank() < depLinkType.rank() {
-		ctx.ModuleErrorf("compiles against %v, but dependency %q is compiling against %v. "+
-			"In order to fix this, consider adjusting sdk_version: OR platform_apis: "+
-			"property of the source or target module so that target module is built "+
-			"with the same or smaller API set when compared to the source.",
-			myLinkType, ctx.OtherModuleName(dep), depLinkType)
-	}
-}
-
-func (j *Module) collectDeps(ctx android.ModuleContext) deps {
-	var deps deps
-
-	if ctx.Device() {
-		sdkDep := decodeSdkDep(ctx, sdkContext(j))
-		if sdkDep.invalidVersion {
-			ctx.AddMissingDependencies(sdkDep.bootclasspath)
-			ctx.AddMissingDependencies(sdkDep.java9Classpath)
-		} else if sdkDep.useFiles {
-			// sdkDep.jar is actually equivalent to turbine header.jar.
-			deps.classpath = append(deps.classpath, sdkDep.jars...)
-			deps.aidlPreprocess = sdkDep.aidl
-		} else {
-			deps.aidlPreprocess = sdkDep.aidl
-		}
-	}
-
-	sdkLinkType, _ := j.getSdkLinkType(ctx.ModuleName())
-
-	ctx.VisitDirectDeps(func(module android.Module) {
-		otherName := ctx.OtherModuleName(module)
-		tag := ctx.OtherModuleDependencyTag(module)
-
-		if IsJniDepTag(tag) {
-			// Handled by AndroidApp.collectAppDeps
-			return
-		}
-		if tag == certificateTag {
-			// Handled by AndroidApp.collectAppDeps
-			return
-		}
-
-		if dep, ok := module.(SdkLibraryDependency); ok {
-			switch tag {
-			case libTag:
-				deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
-			case staticLibTag:
-				ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
-			}
-		} else if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
-			dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
-			if sdkLinkType != javaPlatform &&
-				ctx.OtherModuleHasProvider(module, SyspropPublicStubInfoProvider) {
-				// dep is a sysprop implementation library, but this module is not linking against
-				// the platform, so it gets the sysprop public stubs library instead.  Replace
-				// dep with the JavaInfo from the SyspropPublicStubInfoProvider.
-				syspropDep := ctx.OtherModuleProvider(module, SyspropPublicStubInfoProvider).(SyspropPublicStubInfo)
-				dep = syspropDep.JavaInfo
-			}
-			switch tag {
-			case bootClasspathTag:
-				deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars...)
-			case libTag, instrumentationForTag:
-				deps.classpath = append(deps.classpath, dep.HeaderJars...)
-				deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs...)
-				addPlugins(&deps, dep.ExportedPlugins, dep.ExportedPluginClasses...)
-				deps.disableTurbine = deps.disableTurbine || dep.ExportedPluginDisableTurbine
-			case java9LibTag:
-				deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars...)
-			case staticLibTag:
-				deps.classpath = append(deps.classpath, dep.HeaderJars...)
-				deps.staticJars = append(deps.staticJars, dep.ImplementationJars...)
-				deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars...)
-				deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars...)
-				deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs...)
-				addPlugins(&deps, dep.ExportedPlugins, dep.ExportedPluginClasses...)
-				// Turbine doesn't run annotation processors, so any module that uses an
-				// annotation processor that generates API is incompatible with the turbine
-				// optimization.
-				deps.disableTurbine = deps.disableTurbine || dep.ExportedPluginDisableTurbine
-			case pluginTag:
-				if plugin, ok := module.(*Plugin); ok {
-					if plugin.pluginProperties.Processor_class != nil {
-						addPlugins(&deps, dep.ImplementationAndResourcesJars, *plugin.pluginProperties.Processor_class)
-					} else {
-						addPlugins(&deps, dep.ImplementationAndResourcesJars)
-					}
-					// Turbine doesn't run annotation processors, so any module that uses an
-					// annotation processor that generates API is incompatible with the turbine
-					// optimization.
-					deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
-				} else {
-					ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
-				}
-			case errorpronePluginTag:
-				if _, ok := module.(*Plugin); ok {
-					deps.errorProneProcessorPath = append(deps.errorProneProcessorPath, dep.ImplementationAndResourcesJars...)
-				} else {
-					ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
-				}
-			case exportedPluginTag:
-				if plugin, ok := module.(*Plugin); ok {
-					j.exportedPluginJars = append(j.exportedPluginJars, dep.ImplementationAndResourcesJars...)
-					if plugin.pluginProperties.Processor_class != nil {
-						j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
-					}
-					// Turbine doesn't run annotation processors, so any module that uses an
-					// annotation processor that generates API is incompatible with the turbine
-					// optimization.
-					j.exportedDisableTurbine = Bool(plugin.pluginProperties.Generates_api)
-				} else {
-					ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
-				}
-			case kotlinStdlibTag:
-				deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars...)
-			case kotlinAnnotationsTag:
-				deps.kotlinAnnotations = dep.HeaderJars
-			case syspropPublicStubDepTag:
-				// This is a sysprop implementation library, forward the JavaInfoProvider from
-				// the corresponding sysprop public stub library as SyspropPublicStubInfoProvider.
-				ctx.SetProvider(SyspropPublicStubInfoProvider, SyspropPublicStubInfo{
-					JavaInfo: dep,
-				})
-			}
-		} else if dep, ok := module.(android.SourceFileProducer); ok {
-			switch tag {
-			case libTag:
-				checkProducesJars(ctx, dep)
-				deps.classpath = append(deps.classpath, dep.Srcs()...)
-			case staticLibTag:
-				checkProducesJars(ctx, dep)
-				deps.classpath = append(deps.classpath, dep.Srcs()...)
-				deps.staticJars = append(deps.staticJars, dep.Srcs()...)
-				deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
-			}
-		} else {
-			switch tag {
-			case bootClasspathTag:
-				// If a system modules dependency has been added to the bootclasspath
-				// then add its libs to the bootclasspath.
-				sm := module.(SystemModulesProvider)
-				deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
-
-			case systemModulesTag:
-				if deps.systemModules != nil {
-					panic("Found two system module dependencies")
-				}
-				sm := module.(SystemModulesProvider)
-				outputDir, outputDeps := sm.OutputDirAndDeps()
-				deps.systemModules = &systemModules{outputDir, outputDeps}
-			}
-		}
-
-		addCLCFromDep(ctx, module, j.classLoaderContexts)
-	})
-
-	return deps
-}
-
-func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
-	deps.processorPath = append(deps.processorPath, pluginJars...)
-	deps.processorClasses = append(deps.processorClasses, pluginClasses...)
-}
-
 func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
 	if javaVersion != "" {
 		return normalizeJavaVersion(ctx, javaVersion)
@@ -1344,824 +411,6 @@
 	}
 }
 
-func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
-
-	var flags javaBuilderFlags
-
-	// javaVersion flag.
-	flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
-
-	if ctx.Config().RunErrorProne() {
-		if config.ErrorProneClasspath == nil && ctx.Config().TestProductVariables == nil {
-			ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
-		}
-
-		errorProneFlags := []string{
-			"-Xplugin:ErrorProne",
-			"${config.ErrorProneChecks}",
-		}
-		errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
-
-		flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
-			"'" + strings.Join(errorProneFlags, " ") + "'"
-		flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
-	}
-
-	// classpath
-	flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
-	flags.classpath = append(flags.classpath, deps.classpath...)
-	flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
-	flags.processorPath = append(flags.processorPath, deps.processorPath...)
-	flags.errorProneProcessorPath = append(flags.errorProneProcessorPath, deps.errorProneProcessorPath...)
-
-	flags.processors = append(flags.processors, deps.processorClasses...)
-	flags.processors = android.FirstUniqueStrings(flags.processors)
-
-	if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
-		decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
-		// Give host-side tools a version of OpenJDK's standard libraries
-		// close to what they're targeting. As of Dec 2017, AOSP is only
-		// bundling OpenJDK 8 and 9, so nothing < 8 is available.
-		//
-		// When building with OpenJDK 8, the following should have no
-		// effect since those jars would be available by default.
-		//
-		// When building with OpenJDK 9 but targeting a version < 1.8,
-		// putting them on the bootclasspath means that:
-		// a) code can't (accidentally) refer to OpenJDK 9 specific APIs
-		// b) references to existing APIs are not reinterpreted in an
-		//    OpenJDK 9-specific way, eg. calls to subclasses of
-		//    java.nio.Buffer as in http://b/70862583
-		java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
-		flags.bootClasspath = append(flags.bootClasspath,
-			android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
-			android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
-		if Bool(j.properties.Use_tools_jar) {
-			flags.bootClasspath = append(flags.bootClasspath,
-				android.PathForSource(ctx, java8Home, "lib/tools.jar"))
-		}
-	}
-
-	// systemModules
-	flags.systemModules = deps.systemModules
-
-	// aidl flags.
-	flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
-
-	return flags
-}
-
-func (j *Module) collectJavacFlags(
-	ctx android.ModuleContext, flags javaBuilderFlags, srcFiles android.Paths) javaBuilderFlags {
-	// javac flags.
-	javacFlags := j.properties.Javacflags
-
-	if ctx.Config().MinimizeJavaDebugInfo() && !ctx.Host() {
-		// For non-host binaries, override the -g flag passed globally to remove
-		// local variable debug info to reduce disk and memory usage.
-		javacFlags = append(javacFlags, "-g:source,lines")
-	}
-	javacFlags = append(javacFlags, "-Xlint:-dep-ann")
-
-	if flags.javaVersion.usesJavaModules() {
-		javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
-
-		if j.properties.Patch_module != nil {
-			// Manually specify build directory in case it is not under the repo root.
-			// (javac doesn't seem to expand into symbolic links when searching for patch-module targets, so
-			// just adding a symlink under the root doesn't help.)
-			patchPaths := []string{".", ctx.Config().BuildDir()}
-
-			// b/150878007
-			//
-			// Workaround to support *Bazel-executed* JDK9 javac in Bazel's
-			// execution root for --patch-module. If this javac command line is
-			// invoked within Bazel's execution root working directory, the top
-			// level directories (e.g. libcore/, tools/, frameworks/) are all
-			// symlinks. JDK9 javac does not traverse into symlinks, which causes
-			// --patch-module to fail source file lookups when invoked in the
-			// execution root.
-			//
-			// Short of patching javac or enumerating *all* directories as possible
-			// input dirs, manually add the top level dir of the source files to be
-			// compiled.
-			topLevelDirs := map[string]bool{}
-			for _, srcFilePath := range srcFiles {
-				srcFileParts := strings.Split(srcFilePath.String(), "/")
-				// Ignore source files that are already in the top level directory
-				// as well as generated files in the out directory. The out
-				// directory may be an absolute path, which means srcFileParts[0] is the
-				// empty string, so check that as well. Note that "out" in Bazel's execution
-				// root is *not* a symlink, which doesn't cause problems for --patch-modules
-				// anyway, so it's fine to not apply this workaround for generated
-				// source files.
-				if len(srcFileParts) > 1 &&
-					srcFileParts[0] != "" &&
-					srcFileParts[0] != "out" {
-					topLevelDirs[srcFileParts[0]] = true
-				}
-			}
-			patchPaths = append(patchPaths, android.SortedStringKeys(topLevelDirs)...)
-
-			classPath := flags.classpath.FormJavaClassPath("")
-			if classPath != "" {
-				patchPaths = append(patchPaths, classPath)
-			}
-			javacFlags = append(
-				javacFlags,
-				"--patch-module="+String(j.properties.Patch_module)+"="+strings.Join(patchPaths, ":"))
-		}
-	}
-
-	if len(javacFlags) > 0 {
-		// optimization.
-		ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
-		flags.javacFlags = "$javacFlags"
-	}
-
-	return flags
-}
-
-func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
-	j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
-
-	deps := j.collectDeps(ctx)
-	flags := j.collectBuilderFlags(ctx, deps)
-
-	if flags.javaVersion.usesJavaModules() {
-		j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
-	}
-	srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
-	if hasSrcExt(srcFiles.Strings(), ".proto") {
-		flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
-	}
-
-	kotlinCommonSrcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Common_srcs, nil)
-	if len(kotlinCommonSrcFiles.FilterOutByExt(".kt")) > 0 {
-		ctx.PropertyErrorf("common_srcs", "common_srcs must be .kt files")
-	}
-
-	srcFiles = j.genSources(ctx, srcFiles, flags)
-
-	// Collect javac flags only after computing the full set of srcFiles to
-	// ensure that the --patch-module lookup paths are complete.
-	flags = j.collectJavacFlags(ctx, flags, srcFiles)
-
-	srcJars := srcFiles.FilterByExt(".srcjar")
-	srcJars = append(srcJars, deps.srcJars...)
-	if aaptSrcJar != nil {
-		srcJars = append(srcJars, aaptSrcJar)
-	}
-
-	if j.properties.Jarjar_rules != nil {
-		j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
-	}
-
-	jarName := ctx.ModuleName() + ".jar"
-
-	javaSrcFiles := srcFiles.FilterByExt(".java")
-	var uniqueSrcFiles android.Paths
-	set := make(map[string]bool)
-	for _, v := range javaSrcFiles {
-		if _, found := set[v.String()]; !found {
-			set[v.String()] = true
-			uniqueSrcFiles = append(uniqueSrcFiles, v)
-		}
-	}
-
-	// Collect .java files for AIDEGen
-	j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
-
-	var kotlinJars android.Paths
-
-	if srcFiles.HasExt(".kt") {
-		// user defined kotlin flags.
-		kotlincFlags := j.properties.Kotlincflags
-		CheckKotlincFlags(ctx, kotlincFlags)
-
-		// Dogfood the JVM_IR backend.
-		kotlincFlags = append(kotlincFlags, "-Xuse-ir")
-
-		// If there are kotlin files, compile them first but pass all the kotlin and java files
-		// kotlinc will use the java files to resolve types referenced by the kotlin files, but
-		// won't emit any classes for them.
-		kotlincFlags = append(kotlincFlags, "-no-stdlib")
-		if ctx.Device() {
-			kotlincFlags = append(kotlincFlags, "-no-jdk")
-		}
-		if len(kotlincFlags) > 0 {
-			// optimization.
-			ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
-			flags.kotlincFlags += "$kotlincFlags"
-		}
-
-		var kotlinSrcFiles android.Paths
-		kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
-		kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
-
-		// Collect .kt files for AIDEGen
-		j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
-		j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, kotlinCommonSrcFiles.Strings()...)
-
-		flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
-		flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
-
-		flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
-		flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
-
-		if len(flags.processorPath) > 0 {
-			// Use kapt for annotation processing
-			kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
-			kaptResJar := android.PathForModuleOut(ctx, "kapt", "kapt-res.jar")
-			kotlinKapt(ctx, kaptSrcJar, kaptResJar, kotlinSrcFiles, kotlinCommonSrcFiles, srcJars, flags)
-			srcJars = append(srcJars, kaptSrcJar)
-			kotlinJars = append(kotlinJars, kaptResJar)
-			// Disable annotation processing in javac, it's already been handled by kapt
-			flags.processorPath = nil
-			flags.processors = nil
-		}
-
-		kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
-		kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, kotlinCommonSrcFiles, srcJars, flags)
-		if ctx.Failed() {
-			return
-		}
-
-		// Make javac rule depend on the kotlinc rule
-		flags.classpath = append(flags.classpath, kotlinJar)
-
-		kotlinJars = append(kotlinJars, kotlinJar)
-		// Jar kotlin classes into the final jar after javac
-		if BoolDefault(j.properties.Static_kotlin_stdlib, true) {
-			kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
-		}
-	}
-
-	jars := append(android.Paths(nil), kotlinJars...)
-
-	// Store the list of .java files that was passed to javac
-	j.compiledJavaSrcs = uniqueSrcFiles
-	j.compiledSrcJars = srcJars
-
-	enableSharding := false
-	var headerJarFileWithoutJarjar android.Path
-	if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
-		if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
-			enableSharding = true
-			// Formerly, there was a check here that prevented annotation processors
-			// from being used when sharding was enabled, as some annotation processors
-			// do not function correctly in sharded environments. It was removed to
-			// allow for the use of annotation processors that do function correctly
-			// with sharding enabled. See: b/77284273.
-		}
-		headerJarFileWithoutJarjar, j.headerJarFile =
-			j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
-		if ctx.Failed() {
-			return
-		}
-	}
-	if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
-		var extraJarDeps android.Paths
-		if ctx.Config().RunErrorProne() {
-			// If error-prone is enabled, add an additional rule to compile the java files into
-			// a separate set of classes (so that they don't overwrite the normal ones and require
-			// a rebuild when error-prone is turned off).
-			// TODO(ccross): Once we always compile with javac9 we may be able to conditionally
-			//    enable error-prone without affecting the output class files.
-			errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
-			RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
-			extraJarDeps = append(extraJarDeps, errorprone)
-		}
-
-		if enableSharding {
-			flags.classpath = append(flags.classpath, headerJarFileWithoutJarjar)
-			shardSize := int(*(j.properties.Javac_shard_size))
-			var shardSrcs []android.Paths
-			if len(uniqueSrcFiles) > 0 {
-				shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
-				for idx, shardSrc := range shardSrcs {
-					classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
-						nil, flags, extraJarDeps)
-					jars = append(jars, classes)
-				}
-			}
-			if len(srcJars) > 0 {
-				classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
-					nil, srcJars, flags, extraJarDeps)
-				jars = append(jars, classes)
-			}
-		} else {
-			classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
-			jars = append(jars, classes)
-		}
-		if ctx.Failed() {
-			return
-		}
-	}
-
-	j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
-
-	var includeSrcJar android.WritablePath
-	if Bool(j.properties.Include_srcs) {
-		includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
-		TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
-	}
-
-	dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
-		j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
-	fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
-	extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
-
-	var resArgs []string
-	var resDeps android.Paths
-
-	resArgs = append(resArgs, dirArgs...)
-	resDeps = append(resDeps, dirDeps...)
-
-	resArgs = append(resArgs, fileArgs...)
-	resDeps = append(resDeps, fileDeps...)
-
-	resArgs = append(resArgs, extraArgs...)
-	resDeps = append(resDeps, extraDeps...)
-
-	if len(resArgs) > 0 {
-		resourceJar := android.PathForModuleOut(ctx, "res", jarName)
-		TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
-		j.resourceJar = resourceJar
-		if ctx.Failed() {
-			return
-		}
-	}
-
-	var resourceJars android.Paths
-	if j.resourceJar != nil {
-		resourceJars = append(resourceJars, j.resourceJar)
-	}
-	if Bool(j.properties.Include_srcs) {
-		resourceJars = append(resourceJars, includeSrcJar)
-	}
-	resourceJars = append(resourceJars, deps.staticResourceJars...)
-
-	if len(resourceJars) > 1 {
-		combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
-		TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
-			false, nil, nil)
-		j.resourceJar = combinedJar
-	} else if len(resourceJars) == 1 {
-		j.resourceJar = resourceJars[0]
-	}
-
-	if len(deps.staticJars) > 0 {
-		jars = append(jars, deps.staticJars...)
-	}
-
-	manifest := j.overrideManifest
-	if !manifest.Valid() && j.properties.Manifest != nil {
-		manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
-	}
-
-	services := android.PathsForModuleSrc(ctx, j.properties.Services)
-	if len(services) > 0 {
-		servicesJar := android.PathForModuleOut(ctx, "services", jarName)
-		var zipargs []string
-		for _, file := range services {
-			serviceFile := file.String()
-			zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
-		}
-		rule := zip
-		args := map[string]string{
-			"jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
-		}
-		if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ZIP") {
-			rule = zipRE
-			args["implicits"] = strings.Join(services.Strings(), ",")
-		}
-		ctx.Build(pctx, android.BuildParams{
-			Rule:      rule,
-			Output:    servicesJar,
-			Implicits: services,
-			Args:      args,
-		})
-		jars = append(jars, servicesJar)
-	}
-
-	// Combine the classes built from sources, any manifests, and any static libraries into
-	// classes.jar. If there is only one input jar this step will be skipped.
-	var outputFile android.OutputPath
-
-	if len(jars) == 1 && !manifest.Valid() {
-		// Optimization: skip the combine step as there is nothing to do
-		// TODO(ccross): this leaves any module-info.class files, but those should only come from
-		// prebuilt dependencies until we support modules in the platform build, so there shouldn't be
-		// any if len(jars) == 1.
-
-		// Transform the single path to the jar into an OutputPath as that is required by the following
-		// code.
-		if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
-			// The path contains an embedded OutputPath so reuse that.
-			outputFile = moduleOutPath.OutputPath
-		} else if outputPath, ok := jars[0].(android.OutputPath); ok {
-			// The path is an OutputPath so reuse it directly.
-			outputFile = outputPath
-		} else {
-			// The file is not in the out directory so create an OutputPath into which it can be copied
-			// and which the following code can use to refer to it.
-			combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
-			ctx.Build(pctx, android.BuildParams{
-				Rule:   android.Cp,
-				Input:  jars[0],
-				Output: combinedJar,
-			})
-			outputFile = combinedJar.OutputPath
-		}
-	} else {
-		combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
-		TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
-			false, nil, nil)
-		outputFile = combinedJar.OutputPath
-	}
-
-	// jarjar implementation jar if necessary
-	if j.expandJarjarRules != nil {
-		// Transform classes.jar into classes-jarjar.jar
-		jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName).OutputPath
-		TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
-		outputFile = jarjarFile
-
-		// jarjar resource jar if necessary
-		if j.resourceJar != nil {
-			resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
-			TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
-			j.resourceJar = resourceJarJarFile
-		}
-
-		if ctx.Failed() {
-			return
-		}
-	}
-
-	// Check package restrictions if necessary.
-	if len(j.properties.Permitted_packages) > 0 {
-		// Check packages and copy to package-checked file.
-		pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
-		CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
-		j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
-
-		if ctx.Failed() {
-			return
-		}
-	}
-
-	j.implementationJarFile = outputFile
-	if j.headerJarFile == nil {
-		j.headerJarFile = j.implementationJarFile
-	}
-
-	if j.shouldInstrumentInApex(ctx) {
-		j.properties.Instrument = true
-	}
-
-	if j.shouldInstrument(ctx) {
-		outputFile = j.instrument(ctx, flags, outputFile, jarName)
-	}
-
-	// merge implementation jar with resources if necessary
-	implementationAndResourcesJar := outputFile
-	if j.resourceJar != nil {
-		jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
-		combinedJar := android.PathForModuleOut(ctx, "withres", jarName).OutputPath
-		TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
-			false, nil, nil)
-		implementationAndResourcesJar = combinedJar
-	}
-
-	j.implementationAndResourcesJar = implementationAndResourcesJar
-
-	// Enable dex compilation for the APEX variants, unless it is disabled explicitly
-	apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
-	if j.DirectlyInAnyApex() && !apexInfo.IsForPlatform() {
-		if j.dexProperties.Compile_dex == nil {
-			j.dexProperties.Compile_dex = proptools.BoolPtr(true)
-		}
-		if j.deviceProperties.Hostdex == nil {
-			j.deviceProperties.Hostdex = proptools.BoolPtr(true)
-		}
-	}
-
-	if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.dexProperties.Compile_dex)) {
-		if j.hasCode(ctx) {
-			if j.shouldInstrumentStatic(ctx) {
-				j.dexer.extraProguardFlagFiles = append(j.dexer.extraProguardFlagFiles,
-					android.PathForSource(ctx, "build/make/core/proguard.jacoco.flags"))
-			}
-			// Dex compilation
-			var dexOutputFile android.OutputPath
-			dexOutputFile = j.dexer.compileDex(ctx, flags, j.minSdkVersion(), outputFile, jarName)
-			if ctx.Failed() {
-				return
-			}
-
-			// Hidden API CSV generation and dex encoding
-			dexOutputFile = j.hiddenAPIExtractAndEncode(ctx, dexOutputFile, j.implementationJarFile,
-				proptools.Bool(j.dexProperties.Uncompress_dex))
-
-			// merge dex jar with resources if necessary
-			if j.resourceJar != nil {
-				jars := android.Paths{dexOutputFile, j.resourceJar}
-				combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName).OutputPath
-				TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
-					false, nil, nil)
-				if *j.dexProperties.Uncompress_dex {
-					combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName).OutputPath
-					TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
-					dexOutputFile = combinedAlignedJar
-				} else {
-					dexOutputFile = combinedJar
-				}
-			}
-
-			j.dexJarFile = dexOutputFile
-
-			// Dexpreopting
-			j.dexpreopt(ctx, dexOutputFile)
-
-			outputFile = dexOutputFile
-		} else {
-			// There is no code to compile into a dex jar, make sure the resources are propagated
-			// to the APK if this is an app.
-			outputFile = implementationAndResourcesJar
-			j.dexJarFile = j.resourceJar
-		}
-
-		if ctx.Failed() {
-			return
-		}
-	} else {
-		outputFile = implementationAndResourcesJar
-	}
-
-	if ctx.Device() {
-		lintSDKVersionString := func(sdkSpec sdkSpec) string {
-			if v := sdkSpec.version; v.isNumbered() {
-				return v.String()
-			} else {
-				return ctx.Config().DefaultAppTargetSdk(ctx).String()
-			}
-		}
-
-		j.linter.name = ctx.ModuleName()
-		j.linter.srcs = srcFiles
-		j.linter.srcJars = srcJars
-		j.linter.classpath = append(append(android.Paths(nil), flags.bootClasspath...), flags.classpath...)
-		j.linter.classes = j.implementationJarFile
-		j.linter.minSdkVersion = lintSDKVersionString(j.minSdkVersion())
-		j.linter.targetSdkVersion = lintSDKVersionString(j.targetSdkVersion())
-		j.linter.compileSdkVersion = lintSDKVersionString(j.sdkVersion())
-		j.linter.javaLanguageLevel = flags.javaVersion.String()
-		j.linter.kotlinLanguageLevel = "1.3"
-		if !apexInfo.IsForPlatform() && ctx.Config().UnbundledBuildApps() {
-			j.linter.buildModuleReportZip = true
-		}
-		j.linter.lint(ctx)
-	}
-
-	ctx.CheckbuildFile(outputFile)
-
-	ctx.SetProvider(JavaInfoProvider, JavaInfo{
-		HeaderJars:                     android.PathsIfNonNil(j.headerJarFile),
-		ImplementationAndResourcesJars: android.PathsIfNonNil(j.implementationAndResourcesJar),
-		ImplementationJars:             android.PathsIfNonNil(j.implementationJarFile),
-		ResourceJars:                   android.PathsIfNonNil(j.resourceJar),
-		AidlIncludeDirs:                j.exportAidlIncludeDirs,
-		SrcJarArgs:                     j.srcJarArgs,
-		SrcJarDeps:                     j.srcJarDeps,
-		ExportedPlugins:                j.exportedPluginJars,
-		ExportedPluginClasses:          j.exportedPluginClasses,
-		ExportedPluginDisableTurbine:   j.exportedDisableTurbine,
-		JacocoReportClassesFile:        j.jacocoReportClassesFile,
-	})
-
-	// Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
-	j.outputFile = outputFile.WithoutRel()
-}
-
-func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
-	srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
-
-	kzipName := pathtools.ReplaceExtension(jarName, "kzip")
-	if idx >= 0 {
-		kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
-		jarName += strconv.Itoa(idx)
-	}
-
-	classes := android.PathForModuleOut(ctx, "javac", jarName).OutputPath
-	TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
-
-	if ctx.Config().EmitXrefRules() {
-		extractionFile := android.PathForModuleOut(ctx, kzipName)
-		emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
-		j.kytheFiles = append(j.kytheFiles, extractionFile)
-	}
-
-	return classes
-}
-
-// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
-// since some of these flags may be used internally.
-func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
-	for _, flag := range flags {
-		flag = strings.TrimSpace(flag)
-
-		if !strings.HasPrefix(flag, "-") {
-			ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
-		} else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
-			ctx.PropertyErrorf("kotlincflags",
-				"Bad flag: `%s`, only use internal compiler for consistency.", flag)
-		} else if inList(flag, config.KotlincIllegalFlags) {
-			ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
-		} else if flag == "-include-runtime" {
-			ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
-		} else {
-			args := strings.Split(flag, " ")
-			if args[0] == "-kotlin-home" {
-				ctx.PropertyErrorf("kotlincflags",
-					"Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
-			}
-		}
-	}
-}
-
-func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
-	deps deps, flags javaBuilderFlags, jarName string,
-	extraJars android.Paths) (headerJar, jarjarHeaderJar android.Path) {
-
-	var jars android.Paths
-	if len(srcFiles) > 0 || len(srcJars) > 0 {
-		// Compile java sources into turbine.jar.
-		turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
-		TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
-		if ctx.Failed() {
-			return nil, nil
-		}
-		jars = append(jars, turbineJar)
-	}
-
-	jars = append(jars, extraJars...)
-
-	// Combine any static header libraries into classes-header.jar. If there is only
-	// one input jar this step will be skipped.
-	jars = append(jars, deps.staticHeaderJars...)
-
-	// we cannot skip the combine step for now if there is only one jar
-	// since we have to strip META-INF/TRANSITIVE dir from turbine.jar
-	combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
-	TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
-		false, nil, []string{"META-INF/TRANSITIVE"})
-	headerJar = combinedJar
-	jarjarHeaderJar = combinedJar
-
-	if j.expandJarjarRules != nil {
-		// Transform classes.jar into classes-jarjar.jar
-		jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
-		TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
-		jarjarHeaderJar = jarjarFile
-		if ctx.Failed() {
-			return nil, nil
-		}
-	}
-
-	return headerJar, jarjarHeaderJar
-}
-
-func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
-	classesJar android.Path, jarName string) android.OutputPath {
-
-	specs := j.jacocoModuleToZipCommand(ctx)
-
-	jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
-	instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName).OutputPath
-
-	jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
-
-	j.jacocoReportClassesFile = jacocoReportClassesFile
-
-	return instrumentedJar
-}
-
-func (j *Module) HeaderJars() android.Paths {
-	if j.headerJarFile == nil {
-		return nil
-	}
-	return android.Paths{j.headerJarFile}
-}
-
-func (j *Module) ImplementationJars() android.Paths {
-	if j.implementationJarFile == nil {
-		return nil
-	}
-	return android.Paths{j.implementationJarFile}
-}
-
-func (j *Module) DexJarBuildPath() android.Path {
-	return j.dexJarFile
-}
-
-func (j *Module) DexJarInstallPath() android.Path {
-	return j.installFile
-}
-
-func (j *Module) ImplementationAndResourcesJars() android.Paths {
-	if j.implementationAndResourcesJar == nil {
-		return nil
-	}
-	return android.Paths{j.implementationAndResourcesJar}
-}
-
-func (j *Module) AidlIncludeDirs() android.Paths {
-	// exportAidlIncludeDirs is type android.Paths already
-	return j.exportAidlIncludeDirs
-}
-
-func (j *Module) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
-	return j.classLoaderContexts
-}
-
-var _ logtagsProducer = (*Module)(nil)
-
-func (j *Module) logtags() android.Paths {
-	return j.logtagsSrcs
-}
-
-// Collect information for opening IDE project files in java/jdeps.go.
-func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
-	dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
-	dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
-	dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
-	dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
-	if j.expandJarjarRules != nil {
-		dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
-	}
-	dpInfo.Paths = append(dpInfo.Paths, j.modulePaths...)
-}
-
-func (j *Module) CompilerDeps() []string {
-	jdeps := []string{}
-	jdeps = append(jdeps, j.properties.Libs...)
-	jdeps = append(jdeps, j.properties.Static_libs...)
-	return jdeps
-}
-
-func (j *Module) hasCode(ctx android.ModuleContext) bool {
-	srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
-	return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
-}
-
-// Implements android.ApexModule
-func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
-	return j.depIsInSameApex(ctx, dep)
-}
-
-// Implements android.ApexModule
-func (j *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
-	sdkVersion android.ApiLevel) error {
-	sdkSpec := j.minSdkVersion()
-	if !sdkSpec.specified() {
-		return fmt.Errorf("min_sdk_version is not specified")
-	}
-	if sdkSpec.kind == sdkCore {
-		return nil
-	}
-	ver, err := sdkSpec.effectiveVersion(ctx)
-	if err != nil {
-		return err
-	}
-	if ver.ApiLevel(ctx).GreaterThan(sdkVersion) {
-		return fmt.Errorf("newer SDK(%v)", ver)
-	}
-	return nil
-}
-
-func (j *Module) Stem() string {
-	return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
-}
-
-// ConfigurationName returns the name of the module as used in build configuration.
-//
-// This is usually the same as BaseModuleName() except for the <x>.impl libraries created by
-// java_sdk_library in which case this is the BaseModuleName() without the ".impl" suffix,
-// i.e. just <x>.
-func (j *Module) ConfigurationName() string {
-	return proptools.StringDefault(j.deviceProperties.ConfigurationName, j.BaseModuleName())
-}
-
-func (j *Module) JacocoReportClassesFile() android.Path {
-	return j.jacocoReportClassesFile
-}
-
-func (j *Module) IsInstallable() bool {
-	return Bool(j.properties.Installable)
-}
-
 //
 // Java libraries (.jar file)
 //
@@ -3412,16 +1661,6 @@
 var String = proptools.String
 var inList = android.InList
 
-// TODO(b/132357300) Generalize SdkLibrarComponentDependency to non-SDK libraries and merge with
-// this interface.
-type ProvidesUsesLib interface {
-	ProvidesUsesLib() *string
-}
-
-func (j *Module) ProvidesUsesLib() *string {
-	return j.usesLibraryProperties.Provides_uses_lib
-}
-
 // Add class loader context (CLC) of a given dependency to the current CLC.
 func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
 	clcMap dexpreopt.ClassLoaderContextMap) {
diff --git a/java/java_test.go b/java/java_test.go
index b68945f..99a96e1 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -25,12 +25,12 @@
 	"strings"
 	"testing"
 
-	"android/soong/genrule"
 	"github.com/google/blueprint/proptools"
 
 	"android/soong/android"
 	"android/soong/cc"
 	"android/soong/dexpreopt"
+	"android/soong/genrule"
 	"android/soong/python"
 )
 
@@ -64,8 +64,6 @@
 		ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
 		ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
 	}),
-	javaMockFS().AddToFixture(),
-	PrepareForTestWithJavaSdkLibraryFiles,
 	dexpreopt.PrepareForTestWithDexpreopt,
 )
 
@@ -245,7 +243,14 @@
 // defaultModuleToPath constructs a path to the turbine generate jar for a default test module that
 // is defined in PrepareForIntegrationTestWithJava
 func defaultModuleToPath(name string) string {
-	return filepath.Join(buildDir, ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar")
+	switch {
+	case name == `""`:
+		return name
+	case strings.HasSuffix(name, ".jar"):
+		return name
+	default:
+		return filepath.Join(buildDir, ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar")
+	}
 }
 
 func TestJavaLinkType(t *testing.T) {
@@ -1296,9 +1301,9 @@
 	})
 
 	foo := ctx.ModuleForTests("foo", "android_common")
-	rule := foo.Rule("lint")
 
-	if !strings.Contains(rule.RuleParams.Command, "--baseline lint-baseline.xml") {
+	sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
+	if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml") {
 		t.Error("did not pass --baseline flag")
 	}
 }
@@ -1318,9 +1323,9 @@
        `, map[string][]byte{})
 
 	foo := ctx.ModuleForTests("foo", "android_common")
-	rule := foo.Rule("lint")
 
-	if strings.Contains(rule.RuleParams.Command, "--baseline") {
+	sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
+	if strings.Contains(*sboxProto.Commands[0].Command, "--baseline") {
 		t.Error("passed --baseline flag for non existent file")
 	}
 }
@@ -1376,9 +1381,9 @@
 	})
 
 	foo := ctx.ModuleForTests("foo", "android_common")
-	rule := foo.Rule("lint")
 
-	if !strings.Contains(rule.RuleParams.Command, "--baseline mybaseline.xml") {
+	sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
+	if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline mybaseline.xml") {
 		t.Error("did not use the correct file for baseline")
 	}
 }
@@ -1420,7 +1425,9 @@
 }
 
 func TestTurbine(t *testing.T) {
-	ctx, _ := testJava(t, `
+	result := javaFixtureFactory.
+		Extend(FixtureWithPrebuiltApis(map[string][]string{"14": {"foo"}})).
+		RunTestWithBp(t, `
 		java_library {
 			name: "foo",
 			srcs: ["a.java"],
@@ -1442,30 +1449,20 @@
 		}
 		`)
 
-	fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
-	barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
-	barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
-	barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
-	bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
+	fooTurbine := result.ModuleForTests("foo", "android_common").Rule("turbine")
+	barTurbine := result.ModuleForTests("bar", "android_common").Rule("turbine")
+	barJavac := result.ModuleForTests("bar", "android_common").Rule("javac")
+	barTurbineCombined := result.ModuleForTests("bar", "android_common").Description("for turbine")
+	bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
 
-	if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
-		t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
-	}
+	android.AssertArrayString(t, "foo inputs", []string{"a.java"}, fooTurbine.Inputs.Strings())
 
 	fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
-	if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
-		t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
-	}
-	if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
-		t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
-	}
-	if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
-		t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
-	}
-	if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
-		t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
-			"prebuilts/sdk/14/public/android.jar")
-	}
+	barTurbineJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine", "bar.jar")
+	android.AssertStringDoesContain(t, "bar turbine classpath", barTurbine.Args["classpath"], fooHeaderJar)
+	android.AssertStringDoesContain(t, "bar javac classpath", barJavac.Args["classpath"], fooHeaderJar)
+	android.AssertArrayString(t, "bar turbine combineJar", []string{barTurbineJar, fooHeaderJar}, barTurbineCombined.Inputs.Strings())
+	android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar")
 }
 
 func TestSharding(t *testing.T) {
diff --git a/java/lint.go b/java/lint.go
index fccd1a5..938e2b0 100644
--- a/java/lint.go
+++ b/java/lint.go
@@ -218,7 +218,7 @@
 		// The list of resources may be too long to put on the command line, but
 		// we can't use the rsp file because it is already being used for srcs.
 		// Insert a second rule to write out the list of resources to a file.
-		resourcesList = android.PathForModuleOut(ctx, "lint", "resources.list")
+		resourcesList = android.PathForModuleOut(ctx, "resources.list")
 		resListRule := android.NewRuleBuilder(pctx, ctx)
 		resListRule.Command().Text("cp").
 			FlagWithRspFileInputList("", resourcesList.ReplaceExtension(ctx, "rsp"), l.resources).
@@ -233,7 +233,7 @@
 	cacheDir := android.PathForModuleOut(ctx, "lint", "cache")
 	homeDir := android.PathForModuleOut(ctx, "lint", "home")
 
-	srcJarDir := android.PathForModuleOut(ctx, "lint-srcjars")
+	srcJarDir := android.PathForModuleOut(ctx, "lint", "srcjars")
 	srcJarList := zipSyncCmd(ctx, rule, srcJarDir, l.srcJars)
 	// TODO(ccross): this is a little fishy.  The files extracted from the srcjars are referenced
 	// by the project.xml and used by the later lint rule, but the lint rule depends on the srcjars,
@@ -248,6 +248,7 @@
 		FlagWithRspFileInputList("", srcsListRsp, l.srcs).
 		Output(srcsList)
 	trackRSPDependency(l.srcs, srcsList)
+	rule.Temporary(srcsList)
 
 	cmd := rule.Command().
 		BuiltTool("lint-project-xml").
@@ -262,11 +263,11 @@
 		cmd.Flag("--test")
 	}
 	if l.manifest != nil {
-		cmd.FlagWithArg("--manifest ", l.manifest.String())
+		cmd.FlagWithArg("--manifest ", cmd.PathForInput(l.manifest))
 		trackInputDependency(l.manifest)
 	}
 	if l.mergedManifest != nil {
-		cmd.FlagWithArg("--merged_manifest ", l.mergedManifest.String())
+		cmd.FlagWithArg("--merged_manifest ", cmd.PathForInput(l.mergedManifest))
 		trackInputDependency(l.mergedManifest)
 	}
 
@@ -279,23 +280,17 @@
 	}
 
 	if l.classes != nil {
-		cmd.FlagWithArg("--classes ", l.classes.String())
+		cmd.FlagWithArg("--classes ", cmd.PathForInput(l.classes))
 		trackInputDependency(l.classes)
 	}
 
-	cmd.FlagForEachArg("--classpath ", l.classpath.Strings())
+	cmd.FlagForEachArg("--classpath ", cmd.PathsForInputs(l.classpath))
 	trackInputDependency(l.classpath...)
 
-	cmd.FlagForEachArg("--extra_checks_jar ", l.extraLintCheckJars.Strings())
+	cmd.FlagForEachArg("--extra_checks_jar ", cmd.PathsForInputs(l.extraLintCheckJars))
 	trackInputDependency(l.extraLintCheckJars...)
 
-	if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_LINT") &&
-		lintRBEExecStrategy(ctx) != remoteexec.LocalExecStrategy {
-		// TODO(b/181912787): remove these and use "." instead.
-		cmd.FlagWithArg("--root_dir ", "/b/f/w")
-	} else {
-		cmd.FlagWithArg("--root_dir ", "$PWD")
-	}
+	cmd.FlagWithArg("--root_dir ", "$PWD")
 
 	// The cache tag in project.xml is relative to the root dir, or the project.xml file if
 	// the root dir is not set.
@@ -325,7 +320,7 @@
 
 // generateManifest adds a command to the rule to write a simple manifest that contains the
 // minSdkVersion and targetSdkVersion for modules (like java_library) that don't have a manifest.
-func (l *linter) generateManifest(ctx android.ModuleContext, rule *android.RuleBuilder) android.Path {
+func (l *linter) generateManifest(ctx android.ModuleContext, rule *android.RuleBuilder) android.WritablePath {
 	manifestPath := android.PathForModuleOut(ctx, "lint", "AndroidManifest.xml")
 
 	rule.Command().Text("(").
@@ -356,18 +351,36 @@
 		}
 	}
 
-	rule := android.NewRuleBuilder(pctx, ctx)
+	rule := android.NewRuleBuilder(pctx, ctx).
+		Sbox(android.PathForModuleOut(ctx, "lint"),
+			android.PathForModuleOut(ctx, "lint.sbox.textproto")).
+		SandboxInputs()
+
+	if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_LINT") {
+		pool := ctx.Config().GetenvWithDefault("RBE_LINT_POOL", "java16")
+		rule.Remoteable(android.RemoteRuleSupports{RBE: true})
+		rule.Rewrapper(&remoteexec.REParams{
+			Labels:          map[string]string{"type": "tool", "name": "lint"},
+			ExecStrategy:    lintRBEExecStrategy(ctx),
+			ToolchainInputs: []string{config.JavaCmd(ctx).String()},
+			EnvironmentVariables: []string{
+				"LANG",
+			},
+			Platform: map[string]string{remoteexec.PoolKey: pool},
+		})
+	}
 
 	if l.manifest == nil {
 		manifest := l.generateManifest(ctx, rule)
 		l.manifest = manifest
+		rule.Temporary(manifest)
 	}
 
 	lintPaths := l.writeLintProjectXML(ctx, rule)
 
-	html := android.PathForModuleOut(ctx, "lint-report.html")
-	text := android.PathForModuleOut(ctx, "lint-report.txt")
-	xml := android.PathForModuleOut(ctx, "lint-report.xml")
+	html := android.PathForModuleOut(ctx, "lint", "lint-report.html")
+	text := android.PathForModuleOut(ctx, "lint", "lint-report.txt")
+	xml := android.PathForModuleOut(ctx, "lint", "lint-report.xml")
 
 	depSetsBuilder := NewLintDepSetBuilder().Direct(html, text, xml)
 
@@ -397,43 +410,7 @@
 		FlagWithInput("SDK_ANNOTATIONS=", annotationsZipPath).
 		FlagWithInput("LINT_OPTS=-DLINT_API_DATABASE=", apiVersionsXMLPath)
 
-	if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_LINT") {
-		pool := ctx.Config().GetenvWithDefault("RBE_LINT_POOL", "java16")
-		// TODO(b/181912787): this should be local fallback once the hack that passes /b/f/w in project.xml
-		// is removed.
-		execStrategy := lintRBEExecStrategy(ctx)
-		labels := map[string]string{"type": "tool", "name": "lint"}
-		rule.Remoteable(android.RemoteRuleSupports{RBE: true})
-		remoteInputs := lintPaths.remoteInputs
-		remoteInputs = append(remoteInputs,
-			lintPaths.projectXML,
-			lintPaths.configXML,
-			lintPaths.homeDir,
-			lintPaths.cacheDir,
-			ctx.Config().HostJavaToolPath(ctx, "lint.jar"),
-			annotationsZipPath,
-			apiVersionsXMLPath,
-		)
-
-		cmd.Text((&remoteexec.REParams{
-			Labels:          labels,
-			ExecStrategy:    execStrategy,
-			ToolchainInputs: []string{config.JavaCmd(ctx).String()},
-			Inputs:          remoteInputs.Strings(),
-			OutputFiles:     android.Paths{html, text, xml}.Strings(),
-			RSPFile:         strings.Join(lintPaths.remoteRSPInputs.Strings(), ","),
-			EnvironmentVariables: []string{
-				"JAVA_OPTS",
-				"ANDROID_SDK_HOME",
-				"SDK_ANNOTATIONS",
-				"LINT_OPTS",
-				"LANG",
-			},
-			Platform: map[string]string{remoteexec.PoolKey: pool},
-		}).NoVarTemplate(ctx.Config()))
-	}
-
-	cmd.BuiltTool("lint").
+	cmd.BuiltTool("lint").ImplicitTool(ctx.Config().HostJavaToolPath(ctx, "lint.jar")).
 		Flag("--quiet").
 		FlagWithInput("--project ", lintPaths.projectXML).
 		FlagWithInput("--config ", lintPaths.configXML).
@@ -450,6 +427,9 @@
 		Implicit(apiVersionsXMLPath).
 		Implicits(lintPaths.deps)
 
+	rule.Temporary(lintPaths.projectXML)
+	rule.Temporary(lintPaths.configXML)
+
 	if checkOnly := ctx.Config().Getenv("ANDROID_LINT_CHECK"); checkOnly != "" {
 		cmd.FlagWithArg("--check ", checkOnly)
 	}
diff --git a/java/sdk_test.go b/java/sdk_test.go
index dc90ea3..37875a4 100644
--- a/java/sdk_test.go
+++ b/java/sdk_test.go
@@ -27,6 +27,7 @@
 )
 
 func TestClasspath(t *testing.T) {
+	const frameworkAidl = "-I" + defaultJavaDir + "/framework/aidl"
 	var classpathTestcases = []struct {
 		name       string
 		unbundled  bool
@@ -52,7 +53,7 @@
 			system:         config.StableCorePlatformSystemModules,
 			java8classpath: config.FrameworkLibraries,
 			java9classpath: config.FrameworkLibraries,
-			aidl:           "-Iframework/aidl",
+			aidl:           frameworkAidl,
 		},
 		{
 			name:           `sdk_version:"core_platform"`,
@@ -69,7 +70,7 @@
 			system:         config.StableCorePlatformSystemModules,
 			java8classpath: config.FrameworkLibraries,
 			java9classpath: config.FrameworkLibraries,
-			aidl:           "-Iframework/aidl",
+			aidl:           frameworkAidl,
 		},
 		{
 
@@ -263,7 +264,7 @@
 			convertModulesToPaths := func(cp []string) []string {
 				ret := make([]string, len(cp))
 				for i, e := range cp {
-					ret[i] = moduleToPath(e)
+					ret[i] = defaultModuleToPath(e)
 				}
 				return ret
 			}
@@ -299,6 +300,8 @@
 				dir := ""
 				if strings.HasPrefix(testcase.system, "sdk_public_") {
 					dir = "prebuilts/sdk"
+				} else {
+					dir = defaultJavaDir
 				}
 				system = "--system=" + filepath.Join(buildDir, ".intermediates", dir, testcase.system, "android_common", "system")
 				// The module-relative parts of these paths are hardcoded in system_modules.go:
@@ -309,8 +312,8 @@
 				}
 			}
 
-			checkClasspath := func(t *testing.T, ctx *android.TestContext, isJava8 bool) {
-				foo := ctx.ModuleForTests("foo", variant)
+			checkClasspath := func(t *testing.T, result *android.TestResult, isJava8 bool) {
+				foo := result.ModuleForTests("foo", variant)
 				javac := foo.Rule("javac")
 				var deps []string
 
@@ -349,78 +352,68 @@
 				}
 			}
 
+			fixtureFactory := javaFixtureFactory.Extend(
+				FixtureWithPrebuiltApis(map[string][]string{
+					"29":      {},
+					"30":      {},
+					"current": {},
+				}),
+				android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
+					if testcase.unbundled {
+						variables.Unbundled_build = proptools.BoolPtr(true)
+						variables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
+					}
+				}),
+				android.FixtureModifyEnv(func(env map[string]string) {
+					if env["ANDROID_JAVA8_HOME"] == "" {
+						env["ANDROID_JAVA8_HOME"] = "jdk8"
+					}
+				}),
+			)
+
 			// Test with legacy javac -source 1.8 -target 1.8
 			t.Run("Java language level 8", func(t *testing.T) {
-				config := testConfig(nil, bpJava8, nil)
-				if testcase.unbundled {
-					config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
-					config.TestProductVariables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
-				}
-				ctx := testContext(config)
-				run(t, ctx, config)
+				result := fixtureFactory.RunTestWithBp(t, bpJava8)
 
-				checkClasspath(t, ctx, true /* isJava8 */)
+				checkClasspath(t, result, true /* isJava8 */)
 
 				if testcase.host != android.Host {
-					aidl := ctx.ModuleForTests("foo", variant).Rule("aidl")
+					aidl := result.ModuleForTests("foo", variant).Rule("aidl")
 
-					if g, w := aidl.RuleParams.Command, testcase.aidl+" -I."; !strings.Contains(g, w) {
-						t.Errorf("want aidl command to contain %q, got %q", w, g)
-					}
+					android.AssertStringDoesContain(t, "aidl command", aidl.RuleParams.Command, testcase.aidl+" -I.")
 				}
 			})
 
 			// Test with default javac -source 9 -target 9
 			t.Run("Java language level 9", func(t *testing.T) {
-				config := testConfig(nil, bp, nil)
-				if testcase.unbundled {
-					config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
-					config.TestProductVariables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
-				}
-				ctx := testContext(config)
-				run(t, ctx, config)
+				result := fixtureFactory.RunTestWithBp(t, bp)
 
-				checkClasspath(t, ctx, false /* isJava8 */)
+				checkClasspath(t, result, false /* isJava8 */)
 
 				if testcase.host != android.Host {
-					aidl := ctx.ModuleForTests("foo", variant).Rule("aidl")
+					aidl := result.ModuleForTests("foo", variant).Rule("aidl")
 
-					if g, w := aidl.RuleParams.Command, testcase.aidl+" -I."; !strings.Contains(g, w) {
-						t.Errorf("want aidl command to contain %q, got %q", w, g)
-					}
+					android.AssertStringDoesContain(t, "aidl command", aidl.RuleParams.Command, testcase.aidl+" -I.")
 				}
 			})
 
+			prepareWithPlatformVersionRel := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
+				variables.Platform_sdk_codename = proptools.StringPtr("REL")
+				variables.Platform_sdk_final = proptools.BoolPtr(true)
+			})
+
 			// Test again with PLATFORM_VERSION_CODENAME=REL, javac -source 8 -target 8
 			t.Run("REL + Java language level 8", func(t *testing.T) {
-				config := testConfig(nil, bpJava8, nil)
-				config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("REL")
-				config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(true)
+				result := fixtureFactory.Extend(prepareWithPlatformVersionRel).RunTestWithBp(t, bpJava8)
 
-				if testcase.unbundled {
-					config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
-					config.TestProductVariables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
-				}
-				ctx := testContext(config)
-				run(t, ctx, config)
-
-				checkClasspath(t, ctx, true /* isJava8 */)
+				checkClasspath(t, result, true /* isJava8 */)
 			})
 
 			// Test again with PLATFORM_VERSION_CODENAME=REL, javac -source 9 -target 9
 			t.Run("REL + Java language level 9", func(t *testing.T) {
-				config := testConfig(nil, bp, nil)
-				config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("REL")
-				config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(true)
+				result := fixtureFactory.Extend(prepareWithPlatformVersionRel).RunTestWithBp(t, bp)
 
-				if testcase.unbundled {
-					config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
-					config.TestProductVariables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
-				}
-				ctx := testContext(config)
-				run(t, ctx, config)
-
-				checkClasspath(t, ctx, false /* isJava8 */)
+				checkClasspath(t, result, false /* isJava8 */)
 			})
 		})
 	}
diff --git a/java/testing.go b/java/testing.go
index 896bcf8..6fc3b03 100644
--- a/java/testing.go
+++ b/java/testing.go
@@ -24,8 +24,6 @@
 	"android/soong/android"
 	"android/soong/cc"
 	"android/soong/dexpreopt"
-	"android/soong/python"
-
 	"github.com/google/blueprint"
 )
 
@@ -62,9 +60,7 @@
 )
 
 // Prepare a fixture with the standard files required by a java_sdk_library module.
-var PrepareForTestWithJavaSdkLibraryFiles = android.FixtureMergeMockFs(javaSdkLibraryFiles)
-
-var javaSdkLibraryFiles = android.MockFS{
+var PrepareForTestWithJavaSdkLibraryFiles = android.FixtureMergeMockFs(android.MockFS{
 	"api/current.txt":               nil,
 	"api/removed.txt":               nil,
 	"api/system-current.txt":        nil,
@@ -75,7 +71,7 @@
 	"api/module-lib-removed.txt":    nil,
 	"api/system-server-current.txt": nil,
 	"api/system-server-removed.txt": nil,
-}
+})
 
 // FixtureWithLastReleaseApis creates a preparer that creates prebuilt versions of the specified
 // modules for the `last` API release. By `last` it just means last in the list of supplied versions
@@ -127,49 +123,15 @@
 		mockFS.Merge(prebuiltApisFilesForLibs([]string{release}, libs))
 	}
 	return android.GroupFixturePreparers(
-		// A temporary measure to discard the definitions provided by default by javaMockFS() to allow
-		// the changes that use this preparer to fix tests to be separated from the change to remove
-		// javaMockFS().
-		android.FixtureModifyMockFS(func(fs android.MockFS) {
-			for k, _ := range fs {
-				if strings.HasPrefix(k, "prebuilts/sdk/") {
-					delete(fs, k)
-				}
-			}
-		}),
 		android.FixtureAddTextFile(path, bp),
 		android.FixtureMergeMockFs(mockFS),
 	)
 }
 
-func javaMockFS() android.MockFS {
-	mockFS := android.MockFS{
-		"prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
-		"prebuilts/sdk/Android.bp":                  []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "30", "current"], imports_sdk_version: "none", imports_compile_dex:true,}`),
-
-		"bin.py": nil,
-		python.StubTemplateHost: []byte(`PYTHON_BINARY = '%interpreter%'
-		MAIN_FILE = '%main%'`),
-	}
-
-	levels := []string{"14", "28", "29", "30", "current"}
-	libs := []string{
-		"android", "foo", "bar", "sdklib", "barney", "betty", "foo-shared_library",
-		"foo-no_shared_library", "core-for-system-modules", "quuz", "qux", "fred",
-		"runtime-library",
-	}
-	for k, v := range prebuiltApisFilesForLibs(levels, libs) {
-		mockFS[k] = v
-	}
-
-	return mockFS
-}
-
 func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) android.Config {
 	bp += GatherRequiredDepsForTest()
 
-	mockFS := javaMockFS()
-	mockFS.Merge(javaSdkLibraryFiles)
+	mockFS := android.MockFS{}
 
 	cc.GatherRequiredFilesForTest(mockFS)
 
diff --git a/remoteexec/Android.bp b/remoteexec/Android.bp
index 9f75df5..0d55168 100644
--- a/remoteexec/Android.bp
+++ b/remoteexec/Android.bp
@@ -5,10 +5,6 @@
 bootstrap_go_package {
     name: "soong-remoteexec",
     pkgPath: "android/soong/remoteexec",
-    deps: [
-        "blueprint",
-        "soong-android",
-    ],
     srcs: [
         "remoteexec.go",
     ],
diff --git a/remoteexec/remoteexec.go b/remoteexec/remoteexec.go
index 5f0426a..166f68c 100644
--- a/remoteexec/remoteexec.go
+++ b/remoteexec/remoteexec.go
@@ -17,10 +17,6 @@
 import (
 	"sort"
 	"strings"
-
-	"android/soong/android"
-
-	"github.com/google/blueprint"
 )
 
 const (
@@ -56,7 +52,6 @@
 var (
 	defaultLabels       = map[string]string{"type": "tool"}
 	defaultExecStrategy = LocalExecStrategy
-	pctx                = android.NewPackageContext("android/soong/remoteexec")
 )
 
 // REParams holds information pertinent to the remote execution of a rule.
@@ -87,28 +82,18 @@
 }
 
 func init() {
-	pctx.VariableFunc("Wrapper", func(ctx android.PackageVarContext) string {
-		return wrapper(ctx.Config())
-	})
-}
-
-func wrapper(cfg android.Config) string {
-	if override := cfg.Getenv("RBE_WRAPPER"); override != "" {
-		return override
-	}
-	return DefaultWrapperPath
 }
 
 // Template generates the remote execution wrapper template to be added as a prefix to the rule's
 // command.
 func (r *REParams) Template() string {
-	return "${remoteexec.Wrapper}" + r.wrapperArgs()
+	return "${android.RBEWrapper}" + r.wrapperArgs()
 }
 
 // NoVarTemplate generates the remote execution wrapper template without variables, to be used in
 // RuleBuilder.
-func (r *REParams) NoVarTemplate(cfg android.Config) string {
-	return wrapper(cfg) + r.wrapperArgs()
+func (r *REParams) NoVarTemplate(wrapper string) string {
+	return wrapper + r.wrapperArgs()
 }
 
 func (r *REParams) wrapperArgs() string {
@@ -171,43 +156,3 @@
 
 	return args + " -- "
 }
-
-// StaticRules returns a pair of rules based on the given RuleParams, where the first rule is a
-// locally executable rule and the second rule is a remotely executable rule. commonArgs are args
-// used for both the local and remotely executable rules. reArgs are used only for remote
-// execution.
-func StaticRules(ctx android.PackageContext, name string, ruleParams blueprint.RuleParams, reParams *REParams, commonArgs []string, reArgs []string) (blueprint.Rule, blueprint.Rule) {
-	ruleParamsRE := ruleParams
-	ruleParams.Command = strings.ReplaceAll(ruleParams.Command, "$reTemplate", "")
-	ruleParamsRE.Command = strings.ReplaceAll(ruleParamsRE.Command, "$reTemplate", reParams.Template())
-
-	return ctx.AndroidStaticRule(name, ruleParams, commonArgs...),
-		ctx.AndroidRemoteStaticRule(name+"RE", android.RemoteRuleSupports{RBE: true}, ruleParamsRE, append(commonArgs, reArgs...)...)
-}
-
-// MultiCommandStaticRules returns a pair of rules based on the given RuleParams, where the first
-// rule is a locally executable rule and the second rule is a remotely executable rule. This
-// function supports multiple remote execution wrappers placed in the template when commands are
-// chained together with &&. commonArgs are args used for both the local and remotely executable
-// rules. reArgs are args used only for remote execution.
-func MultiCommandStaticRules(ctx android.PackageContext, name string, ruleParams blueprint.RuleParams, reParams map[string]*REParams, commonArgs []string, reArgs []string) (blueprint.Rule, blueprint.Rule) {
-	ruleParamsRE := ruleParams
-	for k, v := range reParams {
-		ruleParams.Command = strings.ReplaceAll(ruleParams.Command, k, "")
-		ruleParamsRE.Command = strings.ReplaceAll(ruleParamsRE.Command, k, v.Template())
-	}
-
-	return ctx.AndroidStaticRule(name, ruleParams, commonArgs...),
-		ctx.AndroidRemoteStaticRule(name+"RE", android.RemoteRuleSupports{RBE: true}, ruleParamsRE, append(commonArgs, reArgs...)...)
-}
-
-// EnvOverrideFunc retrieves a variable func that evaluates to the value of the given environment
-// variable if set, otherwise the given default.
-func EnvOverrideFunc(envVar, defaultVal string) func(ctx android.PackageVarContext) string {
-	return func(ctx android.PackageVarContext) string {
-		if override := ctx.Config().Getenv(envVar); override != "" {
-			return override
-		}
-		return defaultVal
-	}
-}
diff --git a/remoteexec/remoteexec_test.go b/remoteexec/remoteexec_test.go
index 56985d3..875aa6a 100644
--- a/remoteexec/remoteexec_test.go
+++ b/remoteexec/remoteexec_test.go
@@ -17,8 +17,6 @@
 import (
 	"fmt"
 	"testing"
-
-	"android/soong/android"
 )
 
 func TestTemplate(t *testing.T) {
@@ -38,7 +36,7 @@
 					PoolKey:           "default",
 				},
 			},
-			want: fmt.Sprintf("${remoteexec.Wrapper} --labels=compiler=clang,lang=cpp,type=compile --platform=\"Pool=default,container-image=%s\" --exec_strategy=local --inputs=$in --output_files=$out -- ", DefaultImage),
+			want: fmt.Sprintf("${android.RBEWrapper} --labels=compiler=clang,lang=cpp,type=compile --platform=\"Pool=default,container-image=%s\" --exec_strategy=local --inputs=$in --output_files=$out -- ", DefaultImage),
 		},
 		{
 			name: "all params",
@@ -54,7 +52,7 @@
 					PoolKey:           "default",
 				},
 			},
-			want: fmt.Sprintf("${remoteexec.Wrapper} --labels=compiler=clang,lang=cpp,type=compile --platform=\"Pool=default,container-image=%s\" --exec_strategy=remote --inputs=$in --input_list_paths=$out.rsp --output_files=$out --toolchain_inputs=clang++ -- ", DefaultImage),
+			want: fmt.Sprintf("${android.RBEWrapper} --labels=compiler=clang,lang=cpp,type=compile --platform=\"Pool=default,container-image=%s\" --exec_strategy=remote --inputs=$in --input_list_paths=$out.rsp --output_files=$out --toolchain_inputs=clang++ -- ", DefaultImage),
 		},
 	}
 	for _, test := range tests {
@@ -77,7 +75,7 @@
 		},
 	}
 	want := fmt.Sprintf("prebuilts/remoteexecution-client/live/rewrapper --labels=compiler=clang,lang=cpp,type=compile --platform=\"Pool=default,container-image=%s\" --exec_strategy=local --inputs=$in --output_files=$out -- ", DefaultImage)
-	if got := params.NoVarTemplate(android.NullConfig("")); got != want {
+	if got := params.NoVarTemplate(DefaultWrapperPath); got != want {
 		t.Errorf("NoVarTemplate() returned\n%s\nwant\n%s", got, want)
 	}
 }
@@ -92,7 +90,7 @@
 			PoolKey:           "default",
 		},
 	}
-	want := fmt.Sprintf("${remoteexec.Wrapper} --labels=compiler=clang,lang=cpp,type=compile --platform=\"Pool=default,container-image=%s\" --exec_strategy=local --inputs=$in --output_files=$out -- ", DefaultImage)
+	want := fmt.Sprintf("${android.RBEWrapper} --labels=compiler=clang,lang=cpp,type=compile --platform=\"Pool=default,container-image=%s\" --exec_strategy=local --inputs=$in --output_files=$out -- ", DefaultImage)
 	for i := 0; i < 1000; i++ {
 		if got := r.Template(); got != want {
 			t.Fatalf("Template() returned\n%s\nwant\n%s", got, want)
diff --git a/scripts/Android.bp b/scripts/Android.bp
index 310c959..9e8a602 100644
--- a/scripts/Android.bp
+++ b/scripts/Android.bp
@@ -55,7 +55,9 @@
     libs: [
         "manifest_utils",
     ],
-    test_suites: ["general-tests"],
+    test_options: {
+        unit_test: true,
+    },
 }
 
 python_library_host {
@@ -110,7 +112,9 @@
     libs: [
         "manifest_utils",
     ],
-    test_suites: ["general-tests"],
+    test_options: {
+        unit_test: true,
+    },
 }
 
 python_binary_host {
diff --git a/scripts/TEST_MAPPING b/scripts/TEST_MAPPING
deleted file mode 100644
index 1b0a229..0000000
--- a/scripts/TEST_MAPPING
+++ /dev/null
@@ -1,12 +0,0 @@
-{
-  "presubmit" : [
-    {
-      "name": "manifest_check_test",
-      "host": true
-    },
-    {
-      "name": "manifest_fixer_test",
-      "host": true
-    }    
-  ]
-}
diff --git a/scripts/manifest_check.py b/scripts/manifest_check.py
index 973a675..1343f35 100755
--- a/scripts/manifest_check.py
+++ b/scripts/manifest_check.py
@@ -19,6 +19,7 @@
 from __future__ import print_function
 
 import argparse
+import json
 import re
 import subprocess
 import sys
@@ -61,6 +62,10 @@
                       dest='extract_target_sdk_version',
                       action='store_true',
                       help='print the targetSdkVersion from the manifest')
+  parser.add_argument('--dexpreopt-config',
+                      dest='dexpreopt_configs',
+                      action='append',
+                      help='a paths to a dexpreopt.config of some library')
   parser.add_argument('--aapt',
                       dest='aapt',
                       help='path to aapt executable')
@@ -85,12 +90,6 @@
   else:
     manifest_required, manifest_optional = extract_uses_libs_xml(manifest)
 
-  if required is None:
-    required = []
-
-  if optional is None:
-    optional = []
-
   err = []
   if manifest_required != required:
     err.append('Expected required <uses-library> tags "%s", got "%s"' %
@@ -222,6 +221,35 @@
   return target_attr.value
 
 
+def load_dexpreopt_configs(configs):
+  """Load dexpreopt.config files and map module names to library names."""
+  module_to_libname = {}
+
+  if configs is None:
+    configs = []
+
+  for config in configs:
+    with open(config, 'r') as f:
+      contents = json.load(f)
+    module_to_libname[contents['Name']] = contents['ProvidesUsesLibrary']
+
+  return module_to_libname
+
+
+def translate_libnames(modules, module_to_libname):
+  """Translate module names into library names using the mapping."""
+  if modules is None:
+    modules = []
+
+  libnames = []
+  for name in modules:
+    if name in module_to_libname:
+      name = module_to_libname[name]
+    libnames.append(name)
+
+  return libnames
+
+
 def main():
   """Program entry point."""
   try:
@@ -237,12 +265,20 @@
       manifest = minidom.parse(args.input)
 
     if args.enforce_uses_libraries:
+      # Load dexpreopt.config files and build a mapping from module names to
+      # library names. This is necessary because build system addresses
+      # libraries by their module name (`uses_libs`, `optional_uses_libs`,
+      # `LOCAL_USES_LIBRARIES`, `LOCAL_OPTIONAL_LIBRARY_NAMES` all contain
+      # module names), while the manifest addresses libraries by their name.
+      mod_to_lib = load_dexpreopt_configs(args.dexpreopt_configs)
+      required = translate_libnames(args.uses_libraries, mod_to_lib)
+      optional = translate_libnames(args.optional_uses_libraries, mod_to_lib)
+
       # Check if the <uses-library> lists in the build system agree with those
       # in the manifest. Raise an exception on mismatch, unless the script was
       # passed a special parameter to suppress exceptions.
-      errmsg = enforce_uses_libraries(manifest, args.uses_libraries,
-        args.optional_uses_libraries, args.enforce_uses_libraries_relax,
-        is_apk)
+      errmsg = enforce_uses_libraries(manifest, required, optional,
+        args.enforce_uses_libraries_relax, is_apk)
 
       # Create a status file that is empty on success, or contains an error
       # message on failure. When exceptions are suppressed, dexpreopt command