use of com.taobao.android.differ.dex.PatchException in project atlas by alibaba.
the class PatchFileBuilder method addDirectory.
/**
* Adds a directory to a with a directory prefix.
*
* @param jos ZipArchiver to use to archive the file.
* @param directory The directory to add.
* @param prefix An optional prefix for where in the Jar file the directory's contents should go.
*/
protected void addDirectory(JarOutputStream jos, File directory, String prefix) throws PatchException {
if (directory != null && directory.exists()) {
Collection<File> files = FileUtils.listFiles(directory, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
byte[] buf = new byte[8064];
for (File file : files) {
if (file.isDirectory()) {
continue;
}
String path = prefix + File.separator + PathUtils.toRelative(directory, file.getAbsolutePath());
InputStream in = null;
try {
in = new FileInputStream(file);
ZipEntry fileEntry = new ZipEntry(path);
jos.putNextEntry(fileEntry);
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
jos.write(buf, 0, len);
}
// Complete the entry
jos.closeEntry();
in.close();
} catch (IOException e) {
throw new PatchException(e.getMessage(), e);
}
}
}
}
use of com.taobao.android.differ.dex.PatchException in project atlas by alibaba.
the class PatchFileBuilder method zipBundleSo.
/**
* 生成主dex的so
*
* @param bundleFolder
* @param soOutputFile
*/
private void zipBundleSo(File bundleFolder, File soOutputFile) throws PatchException {
try {
Manifest manifest = createManifest();
FileOutputStream fileOutputStream = new FileOutputStream(soOutputFile);
JarOutputStream jos = new JarOutputStream(new BufferedOutputStream(fileOutputStream), manifest);
// Add ZIP entry to output stream.
// jos.setComment(patchVersion+"@"+targetVersion);
File[] files = bundleFolder.listFiles();
for (File file : files) {
if (file.isDirectory()) {
addDirectory(jos, file, file.getName());
} else {
addFile(jos, file);
}
}
IOUtils.closeQuietly(jos);
if (null != fileOutputStream)
IOUtils.closeQuietly(fileOutputStream);
} catch (IOException e) {
throw new PatchException(e.getMessage(), e);
}
}
use of com.taobao.android.differ.dex.PatchException in project atlas by alibaba.
the class PatchFileBuilder method createHistoryTPatches.
/**
* 创建历史版本的tpatch
*/
public BuildPatchInfos createHistoryTPatches(boolean diffBundleDex, final ILogger logger) throws PatchException {
final BuildPatchInfos buildPatchInfos = new BuildPatchInfos();
buildPatchInfos.setBaseVersion(baseVersion);
List<PatchInfo> patchInfos = historyBuildPatchInfos.getPatches();
String taskName = "CreateHisPatch";
ExecutorServicesHelper executorServicesHelper = new ExecutorServicesHelper();
for (final PatchInfo patchInfo : patchInfos) {
if (patchInfo.getPatchVersion().split("\\.").length > 3 && TPatchTool.pName.equals("taobao4android")) {
continue;
}
executorServicesHelper.submitTask(taskName, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
if (null != logger) {
logger.info("[CreateHisPatch]" + patchInfo.getPatchVersion() + "....");
}
try {
hisPatchInfos.put(patchInfo.getPatchVersion(), patchInfo);
PatchInfo newPatchInfo = createHisTPatch(patchInfo.getPatchVersion(), logger);
buildPatchInfos.getPatches().add(newPatchInfo);
} catch (IOException e) {
throw new PatchException(e.getMessage(), e);
}
return true;
}
});
}
try {
executorServicesHelper.waitTaskCompleted(taskName);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
executorServicesHelper.stop();
buildPatchInfos.getPatches().add(currentBuildPatchInfo);
buildPatchInfos.setDiffBundleDex(diffBundleDex);
return buildPatchInfos;
}
use of com.taobao.android.differ.dex.PatchException in project atlas by alibaba.
the class PatchFileBuilder method processBundlePatch.
/**
* 处理各自bundle的patch文件
*
* @param hisPatchInfo
* @param bundlePatchs
*/
private PatchInfo processBundlePatch(PatchInfo hisPatchInfo, List<BundlePatch> bundlePatchs, File curTPatchUnzipFolder) throws IOException, PatchException {
String patchName = "patch-" + currentBuildPatchInfo.getPatchVersion() + "@" + hisPatchInfo.getPatchVersion();
PatchInfo patchInfo = new PatchInfo();
patchInfo.setFileName(patchName + ".tpatch");
patchInfo.setTargetVersion(hisPatchInfo.getPatchVersion());
patchInfo.setPatchVersion(currentBuildPatchInfo.getPatchVersion());
File hisTPatchFile = new File(tPatchTmpFolder, hisPatchInfo.getPatchVersion() + "-download.tpatch");
File hisTPatchUnzipFolder = new File(tPatchTmpFolder, hisPatchInfo.getPatchVersion());
File destTPathTmpFolder = new File(tPatchTmpFolder, patchName);
if (!destTPathTmpFolder.exists()) {
destTPathTmpFolder.mkdirs();
}
for (BundlePatch bundlePatch : bundlePatchs) {
boolean addToPatch = true;
String bundleName = "lib" + bundlePatch.pkgName.replace('.', '_');
if (bundlePatch.mainBundle) {
continue;
} else if (noPatchBundles.contains(bundlePatch.pkgName)) {
File currentBundle = new File(curTPatchUnzipFolder, "lib" + bundlePatch.pkgName.replace(".", "_") + ".so");
if (!currentBundle.exists()) {
continue;
}
FileUtils.copyFileToDirectory(currentBundle, destTPathTmpFolder);
PatchBundleInfo patchBundleInfo = new PatchBundleInfo();
patchBundleInfo.setApplicationName(bundlePatch.applicationName);
patchBundleInfo.setArtifactId(bundlePatch.artifactId);
patchBundleInfo.setMainBundle(false);
patchBundleInfo.setNewBundle(bundlePatch.newBundle);
patchBundleInfo.setName(bundleName);
patchBundleInfo.setPkgName(bundlePatch.pkgName);
patchBundleInfo.setVersion(bundlePatch.version);
patchBundleInfo.setBaseVersion(bundlePatch.baseVersion);
patchBundleInfo.setDependency(bundlePatch.dependency);
patchInfo.getBundles().add(patchBundleInfo);
continue;
}
File curBundleFolder = new File(curTPatchUnzipFolder, bundleName);
File bundleDestFolder = new File(destTPathTmpFolder, bundleName);
PatchBundleInfo patchBundleInfo = new PatchBundleInfo();
patchBundleInfo.setApplicationName(bundlePatch.applicationName);
patchBundleInfo.setArtifactId(bundlePatch.artifactId);
patchBundleInfo.setMainBundle(false);
patchBundleInfo.setNewBundle(bundlePatch.newBundle);
patchBundleInfo.setName(bundleName);
patchBundleInfo.setPkgName(bundlePatch.pkgName);
patchBundleInfo.setVersion(bundlePatch.version);
patchBundleInfo.setBaseVersion(bundlePatch.baseVersion);
patchBundleInfo.setDependency(bundlePatch.dependency);
switch(bundlePatch.bundlePolicy) {
case ADD:
bundleDestFolder.mkdirs();
FileUtils.copyDirectory(curBundleFolder, bundleDestFolder);
break;
case REMOVE:
addToPatch = false;
// donothing
break;
case // donothing
ROLLBACK:
break;
case MERGE:
downloadTPathAndUnzip(hisPatchInfo.getDownloadUrl(), hisTPatchFile, hisTPatchUnzipFolder);
File hisBundleFolder = new File(hisTPatchUnzipFolder, bundleName);
if (!hisBundleFolder.exists()) {
//如果历史的文件不存在,就直接覆盖
throw new PatchException("The bundle:" + bundleName + " does not existed in tpatch:" + hisPatchInfo.getDownloadUrl());
// bundleDestFolder.mkdirs();
// FileUtils.copyDirectory(curBundleFolder, bundleDestFolder);
} else {
File fullAwbFile = awbMaps.get(bundlePatch.artifactId);
copyDiffFiles(fullAwbFile, curBundleFolder, hisBundleFolder, bundleDestFolder);
if (!bundleDestFolder.exists() || bundleDestFolder.listFiles().length == 0) {
addToPatch = false;
}
}
break;
}
if (addToPatch) {
patchInfo.getBundles().add(patchBundleInfo);
}
}
return patchInfo;
}
use of com.taobao.android.differ.dex.PatchException in project atlas by alibaba.
the class PatchFileBuilder method addFile.
/**
* 往jar文件里增加文件
*
* @param jos
* @param file
*/
private void addFile(JarOutputStream jos, File file) throws PatchException {
byte[] buf = new byte[8064];
String path = file.getName();
InputStream in = null;
try {
in = new FileInputStream(file);
ZipEntry fileEntry = new ZipEntry(path);
jos.putNextEntry(fileEntry);
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
jos.write(buf, 0, len);
}
// Complete the entry
jos.closeEntry();
in.close();
} catch (IOException e) {
throw new PatchException(e.getMessage(), e);
}
}
Aggregations