Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved if the "Unzip" an empty file, it will compile failed #242

Merged
merged 1 commit into from
Jul 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import org.gradle.api.Project

import java.nio.file.Files
import java.nio.file.Paths
import java.util.zip.ZipFile

import static com.android.builder.model.AndroidProject.FD_INTERMEDIATES;

/**
Expand Down Expand Up @@ -83,13 +85,14 @@ public class Util {
String jarZipDir = project.getBuildDir().path +
File.separator + FD_INTERMEDIATES + File.separator + "exploded-aar" +
File.separator + Hashing.sha1().hashString(jarPath, Charsets.UTF_16LE).toString() + File.separator + "class";
unzip(jarPath, jarZipDir)
def jarZip = jarZipDir + ".jar"
includeJars << jarZip
classPath << jarZipDir
visitor.setBaseDir(jarZipDir)
Files.walkFileTree(Paths.get(jarZipDir), visitor)
map.put(jarPath, jarZip)
if (unzip(jarPath, jarZipDir)) {
def jarZip = jarZipDir + ".jar"
includeJars << jarZip
classPath << jarZipDir
visitor.setBaseDir(jarZipDir)
Files.walkFileTree(Paths.get(jarZipDir), visitor)
map.put(jarPath, jarZip)
}

} else {

Expand All @@ -98,12 +101,12 @@ public class Util {
/* 将 jar 包解压,并将解压后的目录加入 classpath */
// println ">>> 解压Jar${jarPath}"
String jarZipDir = jar.getParent() + File.separatorChar + jar.getName().replace('.jar', '')
unzip(jarPath, jarZipDir)

classPath << jarZipDir
if (unzip(jarPath, jarZipDir)) {
classPath << jarZipDir

visitor.setBaseDir(jarZipDir)
Files.walkFileTree(Paths.get(jarZipDir), visitor)
visitor.setBaseDir(jarZipDir)
Files.walkFileTree(Paths.get(jarZipDir), visitor)
}

// 删除 jar
FileUtils.forceDelete(jar)
Expand All @@ -130,8 +133,15 @@ public class Util {
/**
* 解压 zipFilePath 到 目录 dirPath
*/
def private static unzip(String zipFilePath, String dirPath) {
def private static boolean unzip(String zipFilePath, String dirPath) {
// 若这个Zip包是空内容的(如引入了Bugly就会出现),则直接忽略
if (isZipEmpty(zipFilePath)) {
println ">>> Zip file is empty! Ignore";
return false;
}

new AntBuilder().unzip(src: zipFilePath, dest: dirPath, overwrite: 'true')
return true;
}

/**
Expand Down Expand Up @@ -182,4 +192,14 @@ public class Util {
}
println()
}

def static boolean isZipEmpty(String zipFilePath) {
ZipFile z;
try {
z = new ZipFile(zipFilePath)
return z.size() == 0
} finally {
z.close();
}
}
}