Search in sources :

Example 1 with DexMergeClient

use of com.taobao.atlas.dexmerge.DexMergeClient in project atlas by alibaba.

the class PatchMerger method merge.

public void merge() throws MergeException, IOException {
    try {
        File outputDirectory = updateInfo.workDir;
        File oringnalDir = new File(outputDirectory.getParentFile(), "orignal_" + updateInfo.baseVersion);
        if (!outputDirectory.exists()) {
            outputDirectory.mkdirs();
        }
        if (!oringnalDir.exists()) {
            oringnalDir.mkdirs();
        }
        ZipFile patchZip = new ZipFile(patchFile);
        Pair<File, String>[] updateBundles = new Pair[updateInfo.updateBundles.size()];
        List<MergeObject> toMergeList = new ArrayList<MergeObject>();
        //find original bundle and store in pair struct
        for (int x = 0; x < updateInfo.updateBundles.size(); x++) {
            UpdateInfo.Item item = updateInfo.updateBundles.get(x);
            String bundleName = item.name;
            String entryNamePrefix = String.format("%s%s", "lib", bundleName.replace(".", "_"));
            String entryName = entryNamePrefix + ".so";
            ZipEntry entry = patchZip.getEntry(entryName);
            if (patchZip.getEntry(entryName) != null && !supportMerge(bundleName)) {
                //全量部署
                File targetBundle = new File(outputDirectory, entryName);
                OutputStream outputStream = new FileOutputStream(targetBundle);
                InputStream inputStream = patchZip.getInputStream(entry);
                copyStream(inputStream, outputStream);
                mergeOutputs.put(bundleName, new Pair<>(targetBundle.getAbsolutePath(), item.version));
            } else {
                //差量部署
                File originalBundle = findOriginalBundleFile(bundleName, oringnalDir.getAbsolutePath(), item);
                if (originalBundle != null && originalBundle.exists()) {
                    updateBundles[x] = new Pair<File, String>(originalBundle, bundleName);
                    File targetBundle = new File(outputDirectory, "lib" + bundleName.replace(".", "_") + ".so");
                    if (targetBundle.exists()) {
                        targetBundle.delete();
                    }
                    toMergeList.add(new MergeObject(updateBundles[x].first.getAbsolutePath(), updateBundles[x].second, targetBundle.getAbsolutePath()));
                    mergeOutputs.put(bundleName, new Pair<>(targetBundle.getAbsolutePath(), item.version));
                }
            }
        }
        if (toMergeList.size() > 0) {
            DexMergeClient dexMergeClient = getMergeClient();
            boolean mergeFinish = dexMergeClient.dexMerge(patchFile.getAbsolutePath(), toMergeList, true);
            dexMergeClient.unPrepare();
            if (!mergeFinish) {
                throw new MergeException("merge failed!");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new MergeException("merge failed!");
    } finally {
        if (apkZip != null) {
            try {
                apkZip.close();
            } catch (Throwable e) {
            }
        }
    }
}
Also used : MergeObject(com.taobao.atlas.dexmerge.MergeObject) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) MergeException(com.taobao.atlas.update.exception.MergeException) DexMergeClient(com.taobao.atlas.dexmerge.DexMergeClient) ZipFile(java.util.zip.ZipFile) MergeException(com.taobao.atlas.update.exception.MergeException) ZipFile(java.util.zip.ZipFile) UpdateInfo(com.taobao.atlas.update.model.UpdateInfo) Pair(android.util.Pair)

Example 2 with DexMergeClient

use of com.taobao.atlas.dexmerge.DexMergeClient in project atlas by alibaba.

the class PatchMerger method getMergeClient.

private DexMergeClient getMergeClient() {
    DexMergeClient mergeClient = new DexMergeClient(mergeCallback);
    boolean result = mergeClient.prepare();
    if (result) {
        return mergeClient;
    }
    throw new RuntimeException("prepare client error");
}
Also used : DexMergeClient(com.taobao.atlas.dexmerge.DexMergeClient)

Aggregations

DexMergeClient (com.taobao.atlas.dexmerge.DexMergeClient)2 Pair (android.util.Pair)1 MergeObject (com.taobao.atlas.dexmerge.MergeObject)1 MergeException (com.taobao.atlas.update.exception.MergeException)1 UpdateInfo (com.taobao.atlas.update.model.UpdateInfo)1 ArrayList (java.util.ArrayList)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1