Use protoc-gen-javalite for java lite protos

Protobuf 3.5.2 does not natively support lite protos, instead they
are generated by the protoc-gen-javalite plugin compiled from
external/protobuf-javalite.

Bug: 117607748
Test: m checkbuild
Change-Id: I95c2d873f19d4c00e9dc312d7fdbe98cae250a8b
diff --git a/android/config.go b/android/config.go
index 695a298..cca1c7a 100644
--- a/android/config.go
+++ b/android/config.go
@@ -351,6 +351,10 @@
 
 var _ bootstrap.ConfigBlueprintToolLocation = (*config)(nil)
 
+func (c *config) HostToolPath(ctx PathContext, tool string) Path {
+	return PathForOutput(ctx, "host", c.PrebuiltOS(), "bin", tool)
+}
+
 // HostSystemTool looks for non-hermetic tools from the system we're running on.
 // Generally shouldn't be used, but useful to find the XCode SDK, etc.
 func (c *config) HostSystemTool(name string) string {
diff --git a/java/builder.go b/java/builder.go
index cefb916..c52e942 100644
--- a/java/builder.go
+++ b/java/builder.go
@@ -161,6 +161,7 @@
 	kotlincClasspath classpath
 
 	protoFlags       []string
+	protoDeps        android.Paths
 	protoOutTypeFlag string // The flag itself: --java_out
 	protoOutParams   string // Parameters to that flag: --java_out=$protoOutParams:$outDir
 	protoRoot        bool
diff --git a/java/proto.go b/java/proto.go
index 8028039..0bb37c5 100644
--- a/java/proto.go
+++ b/java/proto.go
@@ -59,6 +59,7 @@
 		Description: "protoc " + protoFile.Rel(),
 		Output:      srcJarFile,
 		Input:       protoFile,
+		Implicits:   flags.protoDeps,
 		Args: map[string]string{
 			"protoBase":      protoBase,
 			"protoOut":       flags.protoOutTypeFlag,
@@ -93,14 +94,16 @@
 func protoFlags(ctx android.ModuleContext, j *CompilerProperties, p *android.ProtoProperties,
 	flags javaBuilderFlags) javaBuilderFlags {
 
+	var plugin string
+
 	switch String(p.Proto.Type) {
 	case "micro":
 		flags.protoOutTypeFlag = "--javamicro_out"
 	case "nano":
 		flags.protoOutTypeFlag = "--javanano_out"
 	case "lite":
-		flags.protoOutTypeFlag = "--java_out"
-		flags.protoOutParams = "lite"
+		plugin = "protoc-gen-javalite"
+		flags.protoOutTypeFlag = "--javalite_out"
 	case "full", "":
 		flags.protoOutTypeFlag = "--java_out"
 	default:
@@ -108,15 +111,15 @@
 			String(p.Proto.Type))
 	}
 
-	if len(j.Proto.Output_params) > 0 {
-		if flags.protoOutParams != "" {
-			flags.protoOutParams += ","
-		}
-		flags.protoOutParams += strings.Join(j.Proto.Output_params, ",")
-	}
-
+	flags.protoOutParams = strings.Join(j.Proto.Output_params, ",")
 	flags.protoFlags = android.ProtoFlags(ctx, p)
 	flags.protoRoot = android.ProtoCanonicalPathFromRoot(ctx, p)
 
+	if plugin != "" {
+		path := ctx.Config().HostToolPath(ctx, plugin)
+		flags.protoDeps = append(flags.protoDeps, path)
+		flags.protoFlags = append(flags.protoFlags, "--plugin="+path.String())
+	}
+
 	return flags
 }