Add support for using prebuilt libraries as static dependencies

Extract files out of prebuilt libraries and create list files
for classes and resources in order to allow including them in
other jars.

Change-Id: I9269d1fd6e0f570811a00bf319098ac1f7cdc816
diff --git a/java/builder.go b/java/builder.go
index 2f9d701..2c72cab 100644
--- a/java/builder.go
+++ b/java/builder.go
@@ -61,6 +61,16 @@
 			Description: "dex $out",
 		},
 		"outDir", "dxFlags")
+
+	extractPrebuilt = pctx.StaticRule("extractPrebuilt",
+		blueprint.RuleParams{
+			Command: `rm -rf $outDir && unzip -qo $in -d $outDir && ` +
+				`find $outDir -name "*.class" > $classFile && ` +
+				`find $outDir -type f -a \! -name "*.class" -a \! -name "MANIFEST.MF" > $resourceFile || ` +
+				`(rm -rf $outDir; exit 42)`,
+			Description: "extract java prebuilt $outDir",
+		},
+		"outDir", "classFile", "resourceFile")
 )
 
 func init() {
@@ -187,3 +197,24 @@
 
 	return outputFile
 }
+
+func TransformPrebuiltJarToClasses(ctx common.AndroidModuleContext,
+	prebuilt string) (classJarSpec, resourceJarSpec jarSpec) {
+
+	classDir := filepath.Join(common.ModuleOutDir(ctx), "classes")
+	classFileList := filepath.Join(classDir, "classes.list")
+	resourceFileList := filepath.Join(classDir, "resources.list")
+
+	ctx.Build(pctx, blueprint.BuildParams{
+		Rule:    extractPrebuilt,
+		Outputs: []string{classFileList, resourceFileList},
+		Inputs:  []string{prebuilt},
+		Args: map[string]string{
+			"outDir":       classDir,
+			"classFile":    classFileList,
+			"resourceFile": resourceFileList,
+		},
+	})
+
+	return jarSpec{classFileList, classDir}, jarSpec{resourceFileList, classDir}
+}
diff --git a/java/java.go b/java/java.go
index 67bde04..8b7e02d 100644
--- a/java/java.go
+++ b/java/java.go
@@ -361,7 +361,8 @@
 		Srcs []string
 	}
 
-	classpathFile string
+	classpathFile                   string
+	classJarSpecs, resourceJarSpecs []jarSpec
 }
 
 func (j *JavaPrebuilt) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
@@ -369,7 +370,15 @@
 		ctx.ModuleErrorf("expected exactly one jar in srcs")
 		return
 	}
-	j.classpathFile = filepath.Join(common.ModuleSrcDir(ctx), j.properties.Srcs[0])
+	prebuilt := filepath.Join(common.ModuleSrcDir(ctx), j.properties.Srcs[0])
+
+	classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, prebuilt)
+
+	j.classpathFile = prebuilt
+	j.classJarSpecs = []jarSpec{classJarSpec}
+	j.resourceJarSpecs = []jarSpec{resourceJarSpec}
+
+	ctx.InstallFileName("framework", ctx.ModuleName()+".jar", j.classpathFile)
 }
 
 var _ JavaDependency = (*JavaPrebuilt)(nil)
@@ -379,11 +388,11 @@
 }
 
 func (j *JavaPrebuilt) ClassJarSpecs() []jarSpec {
-	return nil
+	return j.classJarSpecs
 }
 
 func (j *JavaPrebuilt) ResourceJarSpecs() []jarSpec {
-	return nil
+	return j.resourceJarSpecs
 }
 
 func JavaPrebuiltFactory() (blueprint.Module, []interface{}) {