Search in sources :

Example 1 with ClassDef

use of com.taobao.atlas.dex.ClassDef 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 ClassDef

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

the class DexIndexPrinter method printClassDefs.

private void printClassDefs() {
    int index = 0;
    for (ClassDef classDef : dex.classDefs()) {
        System.out.println("class def " + index + ": " + classDef);
        index++;
    }
}
Also used : ClassDef(com.taobao.atlas.dex.ClassDef)

Aggregations

ClassDef (com.taobao.atlas.dex.ClassDef)2 Dex (com.taobao.atlas.dex.Dex)1 java.util (java.util)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 ZipOutputStream (java.util.zip.ZipOutputStream)1