Search in sources :

Example 6 with PatchException

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);
            }
        }
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) PatchException(com.taobao.android.differ.dex.PatchException)

Example 7 with PatchException

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);
    }
}
Also used : JarOutputStream(java.util.jar.JarOutputStream) PatchException(com.taobao.android.differ.dex.PatchException) Manifest(java.util.jar.Manifest)

Example 8 with PatchException

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;
}
Also used : PatchInfo(com.taobao.android.object.PatchInfo) PatchException(com.taobao.android.differ.dex.PatchException) ExecutionException(java.util.concurrent.ExecutionException) ExecutorServicesHelper(com.taobao.android.task.ExecutorServicesHelper) PatchException(com.taobao.android.differ.dex.PatchException) ExecutionException(java.util.concurrent.ExecutionException) BuildPatchInfos(com.taobao.android.object.BuildPatchInfos)

Example 9 with PatchException

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;
}
Also used : PatchBundleInfo(com.taobao.android.object.PatchBundleInfo) PatchInfo(com.taobao.android.object.PatchInfo) PatchException(com.taobao.android.differ.dex.PatchException)

Example 10 with PatchException

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);
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) PatchException(com.taobao.android.differ.dex.PatchException)

Aggregations

PatchException (com.taobao.android.differ.dex.PatchException)10 File (java.io.File)5 IOException (java.io.IOException)4 PatchInfo (com.taobao.android.object.PatchInfo)2 ZipEntry (java.util.zip.ZipEntry)2 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)2 ClassFileNameHandler (org.jf.util.ClassFileNameHandler)2 DexDiffer (com.taobao.android.differ.dex.DexDiffer)1 BuildPatchInfos (com.taobao.android.object.BuildPatchInfos)1 DexDiffInfo (com.taobao.android.object.DexDiffInfo)1 PatchBundleInfo (com.taobao.android.object.PatchBundleInfo)1 ExecutorServicesHelper (com.taobao.android.task.ExecutorServicesHelper)1 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 JarFile (java.util.jar.JarFile)1 JarOutputStream (java.util.jar.JarOutputStream)1