From bddcf1355540e14f5dba2e58b6a7b7ff0324cb65 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Wed, 4 Oct 2017 17:02:23 -0700 Subject: [PATCH 1/3] Change default jar time to match ziptime Ziptime uses Jan 01 2008, use the same in soong_zip and merge_zips. Test: manual Change-Id: Ie79a2e7864a5ed5a74833caec0a50c25a451859e --- jar/jar.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jar/jar.go b/jar/jar.go index 08fa1abda..653e5eedb 100644 --- a/jar/jar.go +++ b/jar/jar.go @@ -31,7 +31,7 @@ const ( ModuleInfoClass = "module-info.class" ) -var DefaultTime = time.Date(2009, 1, 1, 0, 0, 0, 0, time.UTC) +var DefaultTime = time.Date(2008, 1, 1, 0, 0, 0, 0, time.UTC) var MetaDirExtra = [2]byte{0xca, 0xfe} From 0cf45cdc1ec4913e2cfe89eef6295e99733ce8ab Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Wed, 4 Oct 2017 17:04:16 -0700 Subject: [PATCH 2/3] Add -stripFile argument to merge_zips Putting resources in dex jars is going to need to pull files from classpath jars that do not match *.class. Test: m -j checkbuild Change-Id: Ia37b8b9387e5f5636769afc6937b9aa3c9ec1ced --- cmd/merge_zips/merge_zips.go | 63 ++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/cmd/merge_zips/merge_zips.go b/cmd/merge_zips/merge_zips.go index a94392b64..e16c00ef9 100644 --- a/cmd/merge_zips/merge_zips.go +++ b/cmd/merge_zips/merge_zips.go @@ -29,26 +29,26 @@ import ( "android/soong/third_party/zip" ) -type stripDir struct{} +type fileList []string -func (s *stripDir) String() string { +func (f *fileList) String() string { return `""` } -func (s *stripDir) Set(dir string) error { - stripDirs = append(stripDirs, filepath.Clean(dir)) +func (f *fileList) Set(name string) error { + *f = append(*f, filepath.Clean(name)) return nil } -type zipToNotStrip struct{} +type zipsToNotStripSet map[string]bool -func (s *zipToNotStrip) String() string { +func (s zipsToNotStripSet) String() string { return `""` } -func (s *zipToNotStrip) Set(zip_path string) error { - zipsToNotStrip[zip_path] = true +func (s zipsToNotStripSet) Set(zip_path string) error { + s[zip_path] = true return nil } @@ -56,15 +56,17 @@ func (s *zipToNotStrip) Set(zip_path string) error { var ( sortEntries = flag.Bool("s", false, "sort entries (defaults to the order from the input zip files)") emulateJar = flag.Bool("j", false, "sort zip entries using jar ordering (META-INF first)") - stripDirs []string - zipsToNotStrip = make(map[string]bool) + stripDirs fileList + stripFiles fileList + zipsToNotStrip = make(zipsToNotStripSet) stripDirEntries = flag.Bool("D", false, "strip directory entries from the output zip file") manifest = flag.String("m", "", "manifest file to insert in jar") ) func init() { - flag.Var(&stripDir{}, "stripDir", "the prefix of file path to be excluded from the output zip") - flag.Var(&zipToNotStrip{}, "zipToNotStrip", "the input zip file which is not applicable for stripping") + flag.Var(&stripDirs, "stripDir", "the prefix of file path to be excluded from the output zip") + flag.Var(&stripFiles, "stripFile", "filenames to be excluded from the output zip, accepts wildcards") + flag.Var(&zipsToNotStrip, "zipToNotStrip", "the input zip file which is not applicable for stripping") } func main() { @@ -243,20 +245,9 @@ func mergeZips(readers []namedZipReader, writer *zip.Writer, manifest string, for _, namedReader := range readers { _, skipStripThisZip := zipsToNotStrip[namedReader.path] - FileLoop: for _, file := range namedReader.reader.File { - if !skipStripThisZip { - for _, dir := range stripDirs { - if strings.HasPrefix(file.Name, dir+"/") { - if emulateJar { - if file.Name != jar.MetaDir && file.Name != jar.ManifestFile { - continue FileLoop - } - } else { - continue FileLoop - } - } - } + if !skipStripThisZip && shouldStripFile(emulateJar, file.Name) { + continue } if stripDirEntries && file.FileInfo().IsDir() { @@ -310,6 +301,28 @@ func mergeZips(readers []namedZipReader, writer *zip.Writer, manifest string, return nil } +func shouldStripFile(emulateJar bool, name string) bool { + for _, dir := range stripDirs { + if strings.HasPrefix(name, dir+"/") { + if emulateJar { + if name != jar.MetaDir && name != jar.ManifestFile { + return true + } + } else { + return true + } + } + } + for _, pattern := range stripFiles { + if match, err := filepath.Match(pattern, filepath.Base(name)); err != nil { + panic(fmt.Errorf("%s: %s", err.Error(), pattern)) + } else if match { + return true + } + } + return false +} + func jarSort(files []fileMapping) { sort.SliceStable(files, func(i, j int) bool { return jar.EntryNamesLess(files[i].dest, files[j].dest) From 7db5d6332fac9c24345e37d467bef957ac3c85dd Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Wed, 4 Oct 2017 17:07:09 -0700 Subject: [PATCH 3/3] Get dex jar resources from classpath jar Dex jars were getting their resources from the res.jar files of their transitive static dependencies. This accidentally bypassed jarjar on resources, since the jarjar pass only happened once the resources jar was combined into the classpath jar. Switch to getting the resources out of the classpath jar by merging it with the dex jar while skipping *.class. Test: m -j checkbuild Test: compare ext.jar to one generated by make Change-Id: I5f6f3da738dcb0af56ab9a1bd7174ed5359de2b2 --- java/builder.go | 10 +++++++--- java/java.go | 29 ++--------------------------- 2 files changed, 9 insertions(+), 30 deletions(-) diff --git a/java/builder.go b/java/builder.go index b924d6540..ca0d2c544 100644 --- a/java/builder.go +++ b/java/builder.go @@ -99,10 +99,12 @@ var ( blueprint.RuleParams{ Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + `${config.DxCmd} --dex --output=$outDir $dxFlags $in && ` + - `${config.SoongZipCmd} -jar -o $out -C $outDir -D $outDir`, + `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` + + `${config.MergeZipsCmd} -D -stripFile "*.class" $out $outDir/classes.dex.jar $in`, CommandDeps: []string{ "${config.DxCmd}", "${config.SoongZipCmd}", + "${config.MergeZipsCmd}", }, }, "outDir", "dxFlags") @@ -298,11 +300,13 @@ func TransformDesugar(ctx android.ModuleContext, classesJar android.Path, return outputFile } -func TransformClassesJarToDexJar(ctx android.ModuleContext, classesJar android.Path, +// Converts a classes.jar file to classes*.dex, then combines the dex files with any resources +// in the classes.jar file into a dex jar. +func TransformClassesJarToDexJar(ctx android.ModuleContext, stem string, classesJar android.Path, flags javaBuilderFlags) android.Path { outDir := android.PathForModuleOut(ctx, "dex") - outputFile := android.PathForModuleOut(ctx, "classes.dex.jar") + outputFile := android.PathForModuleOut(ctx, stem) ctx.ModuleBuild(pctx, android.ModuleBuildParams{ Rule: dx, diff --git a/java/java.go b/java/java.go index 9cde82408..3726435e9 100644 --- a/java/java.go +++ b/java/java.go @@ -153,9 +153,6 @@ type Module struct { // output file containing classes.dex dexJarFile android.Path - // output files containing resources - resourceJarFiles android.Paths - // output file suitable for installing or running outputFile android.Path @@ -173,7 +170,6 @@ type Module struct { type Dependency interface { ClasspathFiles() android.Paths - ResourceJarFiles() android.Paths AidlIncludeDirs() android.Paths } @@ -380,7 +376,6 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps { case staticLibTag: deps.classpath = append(deps.classpath, dep.ClasspathFiles()...) deps.staticJars = append(deps.staticJars, dep.ClasspathFiles()...) - deps.staticJarResources = append(deps.staticJarResources, dep.ResourceJarFiles()...) case frameworkResTag: if ctx.ModuleName() == "framework" { // framework.jar has a one-off dependency on the R.java and Manifest.java files @@ -492,20 +487,14 @@ func (j *Module) compile(ctx android.ModuleContext) { } if len(resArgs) > 0 { - // Combine classes + resources into classes-full-debug.jar resourceJar := TransformResourcesToJar(ctx, resArgs, resDeps) if ctx.Failed() { return } - j.resourceJarFiles = append(j.resourceJarFiles, resourceJar) jars = append(jars, resourceJar) } - // Propagate the resources from the transitive closure of static dependencies for copying - // into dex jars - j.resourceJarFiles = append(j.resourceJarFiles, deps.staticJarResources...) - // static classpath jars have the resources in them, so the resource jars aren't necessary here jars = append(jars, deps.staticJars...) @@ -580,17 +569,12 @@ func (j *Module) compile(ctx android.ModuleContext) { return } - // Compile classes.jar into classes.dex - dexJarFile := TransformClassesJarToDexJar(ctx, desugarJar, flags) + // Compile classes.jar into classes.dex and then javalib.jar + outputFile = TransformClassesJarToDexJar(ctx, "javalib.jar", desugarJar, flags) if ctx.Failed() { return } - jars := android.Paths{dexJarFile} - jars = append(jars, j.resourceJarFiles...) - - outputFile = TransformJarsToJar(ctx, "javalib.jar", jars, android.OptionalPath{}, true) - j.dexJarFile = outputFile } ctx.CheckbuildFile(outputFile) @@ -607,10 +591,6 @@ func (j *Module) ClasspathFiles() android.Paths { return android.Paths{j.classpathFile} } -func (j *Module) ResourceJarFiles() android.Paths { - return j.resourceJarFiles -} - func (j *Module) AidlIncludeDirs() android.Paths { return j.exportAidlIncludeDirs } @@ -777,11 +757,6 @@ func (j *Import) ClasspathFiles() android.Paths { return j.classpathFiles } -func (j *Import) ResourceJarFiles() android.Paths { - // resources are in the ClasspathFiles - return nil -} - func (j *Import) AidlIncludeDirs() android.Paths { return nil }