private Dex mergeDexes() throws IOException {
    mergeStringIds();
    mergeTypeIds();
    mergeTypeLists();
    mergeProtoIds();
    mergeFieldIds();
    mergeMethodIds();
    mergeMethodHandles();
    mergeAnnotations();
    unionAnnotationSetsAndDirectories();
    mergeCallSiteIds();
    mergeClassDefs();

    // computeSizesFromOffsets expects sections sorted by offset, so make it so
    Arrays.sort(contentsOut.sections);

    // write the header
    contentsOut.header.off = 0;
    contentsOut.header.size = 1;
    contentsOut.fileSize = dexOut.getLength();
    contentsOut.computeSizesFromOffsets();
    contentsOut.writeHeader(headerOut, mergeApiLevels());
    contentsOut.writeMap(mapListOut);

    // generate and write the hashes
    dexOut.writeHashes();

    return dexOut;
  }
minimum number of wasted bytes before it's worthwhile to compact the result
android/android_framework/dex-maker/src/main/java/com/android/dx/merge/DexMerger.java