Merge "Use prebuilts/jdk/jdk9 if EXPERIMENTAL_USE_OPENJDK9 is set"
diff --git a/java/java.go b/java/java.go
index 3726435..bab77c5 100644
--- a/java/java.go
+++ b/java/java.go
@@ -481,7 +481,7 @@
 	resDeps = append(resDeps, fileDeps...)
 
 	if proptools.Bool(j.properties.Include_srcs) {
-		srcArgs, srcDeps := ResourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
+		srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
 		resArgs = append(resArgs, srcArgs...)
 		resDeps = append(resDeps, srcDeps...)
 	}
diff --git a/java/java_test.go b/java/java_test.go
index a4a3f52..c2c7ee2 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -408,16 +408,16 @@
 		args  string
 	}{
 		{
-			// Test that a module with java_resource_dirs includes a file list file
+			// Test that a module with java_resource_dirs includes the files
 			name: "resource dirs",
 			prop: `java_resource_dirs: ["res"]`,
-			args: "-C res -l ",
+			args: "-C res -f res/a -f res/b",
 		},
 		{
 			// Test that a module with java_resources includes the files
 			name: "resource files",
 			prop: `java_resources: ["res/a", "res/b"]`,
-			args: "-C . -f res/a -C . -f res/b",
+			args: "-C . -f res/a -f res/b",
 		},
 		{
 			// Test that a module with a filegroup in java_resources includes the files with the
@@ -430,13 +430,13 @@
 					path: "res",
 					srcs: ["res/a", "res/b"],
 				}`,
-			args: "-C res -f res/a -C res -f res/b",
+			args: "-C res -f res/a -f res/b",
 		},
 		{
 			// Test that a module with "include_srcs: true" includes its source files in the resources jar
 			name: "include sources",
 			prop: `include_srcs: true`,
-			args: "-C . -f a.java -C . -f b.java -C . -f c.java",
+			args: "-C . -f a.java -f b.java -f c.java",
 		},
 	}
 
@@ -462,8 +462,8 @@
 					foo.Inputs.Strings(), fooRes.Output.String())
 			}
 
-			if !strings.Contains(fooRes.Args["jarArgs"], test.args) {
-				t.Errorf("foo resource jar args %q does not contain %q",
+			if fooRes.Args["jarArgs"] != test.args {
+				t.Errorf("foo resource jar args %q is not %q",
 					fooRes.Args["jarArgs"], test.args)
 			}
 		})
@@ -489,7 +489,7 @@
 
 	fooRes := ctx.ModuleForTests("foo", "android_common").Output("res.jar")
 
-	expected := "-C res -l " + fooRes.Implicits[0].String()
+	expected := "-C res -f res/a -f res/b"
 	if fooRes.Args["jarArgs"] != expected {
 		t.Errorf("foo resource jar args %q is not %q",
 			fooRes.Args["jarArgs"], expected)
diff --git a/java/resources.go b/java/resources.go
index 85ebd52..a596fd7 100644
--- a/java/resources.go
+++ b/java/resources.go
@@ -19,8 +19,6 @@
 	"path/filepath"
 	"strings"
 
-	"github.com/google/blueprint/bootstrap"
-
 	"android/soong/android"
 )
 
@@ -33,15 +31,6 @@
 	"**/*~",
 }
 
-func isStringInSlice(str string, slice []string) bool {
-	for _, s := range slice {
-		if s == str {
-			return true
-		}
-	}
-	return false
-}
-
 func ResourceDirsToJarArgs(ctx android.ModuleContext,
 	resourceDirs, excludeDirs []string) (args []string, deps android.Paths) {
 	var excludes []string
@@ -53,40 +42,64 @@
 
 	excludes = append(excludes, resourceExcludes...)
 
-	for _, resourceDir := range resourceDirs {
-		if isStringInSlice(resourceDir, excludeDirs) {
-			continue
-		}
-		resourceDir := android.PathForModuleSrc(ctx, resourceDir)
-		dirs := ctx.Glob(resourceDir.String(), nil)
-		for _, dir := range dirs {
-			fileListFile := android.ResPathWithName(ctx, dir, "resources.list")
-			depFile := fileListFile.String() + ".d"
+	for _, dir := range resourceDirs {
+		dir := android.PathForModuleSrc(ctx, dir).String()
+		files := ctx.Glob(filepath.Join(dir, "**/*"), excludes)
 
-			pattern := filepath.Join(dir.String(), "**/*")
-			bootstrap.GlobFile(ctx, pattern, excludes, fileListFile.String(), depFile)
-			args = append(args,
-				"-C", dir.String(),
-				"-l", fileListFile.String())
-			deps = append(deps, fileListFile)
+		deps = append(deps, files...)
+
+		if len(files) > 0 {
+			args = append(args, "-C", dir)
+
+			for _, f := range files {
+				path := f.String()
+				if !strings.HasPrefix(path, dir) {
+					panic(fmt.Errorf("path %q does not start with %q", path, dir))
+				}
+				args = append(args, "-f", path)
+			}
 		}
 	}
 
 	return args, deps
 }
 
+// Convert java_resources properties to arguments to soong_zip -jar, ignoring common patterns
+// that should not be treated as resources (including *.java).
 func ResourceFilesToJarArgs(ctx android.ModuleContext,
 	res, exclude []string) (args []string, deps android.Paths) {
+
+	exclude = append([]string(nil), exclude...)
+	exclude = append(exclude, resourceExcludes...)
+	return resourceFilesToJarArgs(ctx, res, exclude)
+}
+
+// Convert java_resources properties to arguments to soong_zip -jar, keeping files that should
+// normally not used as resources like *.java
+func SourceFilesToJarArgs(ctx android.ModuleContext,
+	res, exclude []string) (args []string, deps android.Paths) {
+
+	return resourceFilesToJarArgs(ctx, res, exclude)
+}
+
+func resourceFilesToJarArgs(ctx android.ModuleContext,
+	res, exclude []string) (args []string, deps android.Paths) {
+
 	files := ctx.ExpandSources(res, exclude)
 
-	for _, f := range files {
+	lastDir := ""
+	for i, f := range files {
 		rel := f.Rel()
 		path := f.String()
 		if !strings.HasSuffix(path, rel) {
 			panic(fmt.Errorf("path %q does not end with %q", path, rel))
 		}
-		path = filepath.Clean(strings.TrimSuffix(path, rel))
-		args = append(args, "-C", filepath.Clean(path), "-f", f.String())
+		dir := filepath.Clean(strings.TrimSuffix(path, rel))
+		if i == 0 || dir != lastDir {
+			args = append(args, "-C", dir)
+		}
+		args = append(args, "-f", path)
+		lastDir = dir
 	}
 
 	return args, files