Search in sources :

Example 1 with Dex

use of com.taobao.atlas.dex.Dex in project atlas by alibaba.

the class MergeTool method createNewMainApkInternal.

private static void createNewMainApkInternal(ZipFile sourceZip, List<ZipEntry> entryList, File tempFile, boolean isDiff, MergeExcutorServices.PrepareCallBack prepareCallBack) throws IOException {
    byte[] buffer = new byte[BUFFEREDSIZE];
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(tempFile)));
    BufferedOutputStream bo = new BufferedOutputStream(out);
    InputStream in;
    //先写入source中未变的文件
    java.util.Enumeration e = sourceZip.entries();
    int i = 1;
    File mainDexFile = new File(tempFile.getParentFile(), "libcom_taobao_maindex.zip");
    inputStreamToFile(MergeExcutorServices.sZipPatch.getInputStream(entryList.get(0)), mainDexFile);
    ZipFile zipFile = new ZipFile(mainDexFile);
    Dex patchDex = new Dex(zipFile.getInputStream(zipFile.getEntry("classes.dex")));
    Iterator<ClassDef> iterators = patchDex.classDefs().iterator();
    List<String> patchClassNames = new ArrayList<String>();
    while (iterators.hasNext()) {
        ClassDef classDef = iterators.next();
        int typeIndex = classDef.getTypeIndex();
        Log.e("MergeTool", "merge class:" + patchDex.typeNames().get(typeIndex));
        patchClassNames.add(patchDex.typeNames().get(typeIndex));
    }
    while (e.hasMoreElements()) {
        ZipEntry zipEnt = (ZipEntry) e.nextElement();
        String name = zipEnt.getName();
        if (isDiff && name.endsWith(".dex")) {
            i++;
            InputStream inputStream = sourceZip.getInputStream(zipEnt);
            Dex dex = processDex(inputStream, patchClassNames);
            ZipEntry zipEntry = new ZipEntry(name);
            out.putNextEntry(zipEntry);
            write(new ByteArrayInputStream(dex.getBytes()), out, buffer);
            bo.flush();
        }
    }
    Enumeration entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry zipEnt = (ZipEntry) entries.nextElement();
        String name = zipEnt.getName();
        if (name.endsWith(".dex")) {
            ZipEntry zipEntry = new ZipEntry(String.format("%s%s%s", "classes", i, ".dex"));
            out.putNextEntry(zipEntry);
            write(zipFile.getInputStream(zipEnt), out, buffer);
            bo.flush();
            i++;
            continue;
        }
        ZipEntry newEntry = new ZipEntry(name);
        if (name.contains("raw/") || name.contains("assets/")) {
            newEntry.setMethod(ZipEntry.STORED);
            newEntry.setCrc(zipEnt.getCrc());
            newEntry.setSize(zipEnt.getSize());
        }
        out.putNextEntry(newEntry);
        in = zipFile.getInputStream(zipEnt);
        write(in, out, buffer);
        bo.flush();
    }
    mainDexFile.delete();
    zipFile.close();
    closeQuitely(out);
    closeQuitely(bo);
}
Also used : ZipEntry(java.util.zip.ZipEntry) ClassDef(com.taobao.atlas.dex.ClassDef) java.util(java.util) ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) Dex(com.taobao.atlas.dex.Dex) ZipFile(java.util.zip.ZipFile)

Example 2 with Dex

use of com.taobao.atlas.dex.Dex in project atlas by alibaba.

the class MergeExcutorServices method dexMergeInternal.

private void dexMergeInternal(InputStream[] inputStreams, OutputStream newDexStream, String bundleName) {
    FileOutputStream fileOutputStream = null;
    if (inputStreams[0] == null || inputStreams[1] == null) {
        try {
            mCallback.onMergeFinish(bundleName, false, "argNUll");
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return;
    }
    try {
        //方式一
        //            DexPatchApplier dexPatchApplier = new DexPatchApplier(inputStreams[0],inputStreams[1]);
        //            dexPatchApplier.executeAndSaveTo(newDexStream);
        //方式二
        Dex dex1 = new Dex(inputStreams[1]);
        Dex dex2 = new Dex(inputStreams[0]);
        List<Dex> dexs = new ArrayList<Dex>();
        dexs.add(dex1);
        dexs.add(dex2);
        DexMerger mDexMerge = new DexMerger(new Dex[] { dex1, dex2 }, CollisionPolicy.KEEP_FIRST);
        mDexMerge.setCompactWasteThreshold(1);
        Dex outDex = mDexMerge.merge();
        outDex.writeTo(newDexStream);
        newDexStream.flush();
        mCallback.onMergeFinish(bundleName, true, "Success");
        successCount.incrementAndGet();
    } catch (Throwable e) {
        e.printStackTrace();
        try {
            mCallback.onMergeFinish(bundleName, false, "IOException 2");
        } catch (RemoteException e1) {
            e1.printStackTrace();
        }
    } finally {
        if (fileOutputStream != null) {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : Dex(com.taobao.atlas.dex.Dex) ArrayList(java.util.ArrayList) DexMerger(com.taobao.atlas.dexmerge.dx.merge.DexMerger) RemoteException(android.os.RemoteException)

Example 3 with Dex

use of com.taobao.atlas.dex.Dex in project atlas by alibaba.

the class MergeTool method processDex.

private static Dex processDex(InputStream inputStream, List<String> patchClassNames) throws IOException {
    Dex oringnalDex = new Dex(inputStream);
    DexMerger dexMerger = new DexMerger(new Dex[] { oringnalDex, new Dex(0) }, CollisionPolicy.FAIL);
    dexMerger.setRemoveTypeClasses(patchClassNames);
    dexMerger.setCompactWasteThreshold(1);
    Dex dex = dexMerger.merge();
    return dex;
}
Also used : Dex(com.taobao.atlas.dex.Dex) DexMerger(com.taobao.atlas.dexmerge.dx.merge.DexMerger)

Aggregations

Dex (com.taobao.atlas.dex.Dex)3 DexMerger (com.taobao.atlas.dexmerge.dx.merge.DexMerger)2 RemoteException (android.os.RemoteException)1 ClassDef (com.taobao.atlas.dex.ClassDef)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 ZipOutputStream (java.util.zip.ZipOutputStream)1