Make dexpreopt tools available for all java tests

Bug: 177892522
Test: m nothing
Change-Id: I594bddae692ef76dc8c3da504934d0151b13d4c9
diff --git a/apex/apex_test.go b/apex/apex_test.go
index b1e8480..56487ce 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -5925,7 +5925,6 @@
 
 	bp += cc.GatherRequiredDepsForTest(android.Android)
 	bp += java.GatherRequiredDepsForTest()
-	bp += dexpreopt.BpToolModulesForTest()
 
 	fs := map[string][]byte{
 		"a.java":                             nil,
@@ -5957,7 +5956,6 @@
 
 	ctx.Register()
 
-	dexpreopt.RegisterToolModulesForTest(ctx)
 	pathCtx := android.PathContextForTesting(config)
 	dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
 	transformDexpreoptConfig(dexpreoptConfig)
diff --git a/dexpreopt/testing.go b/dexpreopt/testing.go
index b572eb3..bccbfc1 100644
--- a/dexpreopt/testing.go
+++ b/dexpreopt/testing.go
@@ -34,7 +34,7 @@
 	return module
 }
 
-func RegisterToolModulesForTest(ctx *android.TestContext) {
+func RegisterToolModulesForTest(ctx android.RegistrationContext) {
 	ctx.RegisterModuleType("dummy_tool_binary", dummyToolBinaryFactory)
 }
 
diff --git a/java/java_test.go b/java/java_test.go
index 7b89848..e7776c3 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -59,8 +59,6 @@
 }
 
 func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
-	bp += dexpreopt.BpToolModulesForTest()
-
 	return TestConfig(buildDir, env, bp, fs)
 }
 
@@ -84,8 +82,6 @@
 	// Register module types and mutators from cc needed for JNI testing
 	cc.RegisterRequiredBuildComponentsForTest(ctx)
 
-	dexpreopt.RegisterToolModulesForTest(ctx)
-
 	ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
 		ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
 	})
diff --git a/java/testing.go b/java/testing.go
index 0b1e2eb..f569834 100644
--- a/java/testing.go
+++ b/java/testing.go
@@ -113,6 +113,9 @@
 	RegisterSdkLibraryBuildComponents(ctx)
 	RegisterStubsBuildComponents(ctx)
 	RegisterSystemModulesBuildComponents(ctx)
+
+	// Make sure that any tool related module types needed by dexpreopt have been registered.
+	dexpreopt.RegisterToolModulesForTest(ctx)
 }
 
 // Gather the module definitions needed by tests that depend upon code from this package.
@@ -204,6 +207,9 @@
 		`, extra)
 	}
 
+	// Make sure that any tools needed for dexpreopting are defined.
+	bp += dexpreopt.BpToolModulesForTest()
+
 	// Make sure that the dex_bootjars singleton module is instantiated for the tests.
 	bp += `
 		dex_bootjars {