diff options
Diffstat (limited to 'dx/src/com/android/dx/merge/DexMerger.java')
-rw-r--r-- | dx/src/com/android/dx/merge/DexMerger.java | 161 |
1 files changed, 81 insertions, 80 deletions
diff --git a/dx/src/com/android/dx/merge/DexMerger.java b/dx/src/com/android/dx/merge/DexMerger.java index 319153f39..329920e31 100644 --- a/dx/src/com/android/dx/merge/DexMerger.java +++ b/dx/src/com/android/dx/merge/DexMerger.java @@ -16,18 +16,19 @@ package com.android.dx.merge; -import com.android.dx.dex.SizeOf; -import com.android.dx.dex.TableOfContents; -import com.android.dx.io.Annotation; -import com.android.dx.io.ClassData; -import com.android.dx.io.ClassDef; -import com.android.dx.io.Code; -import com.android.dx.io.DexBuffer; +import com.android.dex.Annotation; +import com.android.dex.ClassData; +import com.android.dex.ClassDef; +import com.android.dex.Code; +import com.android.dex.Dex; +import com.android.dex.DexException; +import com.android.dex.FieldId; +import com.android.dex.MethodId; +import com.android.dex.ProtoId; +import com.android.dex.SizeOf; +import com.android.dex.TableOfContents; +import com.android.dex.TypeList; import com.android.dx.io.DexHasher; -import com.android.dx.io.FieldId; -import com.android.dx.io.MethodId; -import com.android.dx.io.ProtoId; -import com.android.dx.util.DexException; import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -39,43 +40,43 @@ import java.util.List; * Combine two dex files into one. */ public final class DexMerger { - private final DexBuffer dexA; - private final DexBuffer dexB; + private final Dex dexA; + private final Dex dexB; private final CollisionPolicy collisionPolicy; private final WriterSizes writerSizes; - private final DexBuffer dexOut = new DexBuffer(); + private final Dex dexOut = new Dex(); - private final DexBuffer.Section headerOut; + private final Dex.Section headerOut; /** All IDs and definitions sections */ - private final DexBuffer.Section idsDefsOut; + private final Dex.Section idsDefsOut; - private final DexBuffer.Section mapListOut; + private final Dex.Section mapListOut; - private final DexBuffer.Section typeListOut; + private final Dex.Section typeListOut; - private final DexBuffer.Section classDataOut; + private final Dex.Section classDataOut; - private final DexBuffer.Section codeOut; + private final Dex.Section codeOut; - private final DexBuffer.Section stringDataOut; + private final Dex.Section stringDataOut; - private final DexBuffer.Section debugInfoOut; + private final Dex.Section debugInfoOut; - private final DexBuffer.Section encodedArrayOut; + private final Dex.Section encodedArrayOut; /** annotations directory on a type */ - private final DexBuffer.Section annotationsDirectoryOut; + private final Dex.Section annotationsDirectoryOut; /** sets of annotations on a member, parameter or type */ - private final DexBuffer.Section annotationSetOut; + private final Dex.Section annotationSetOut; /** parameter lists */ - private final DexBuffer.Section annotationSetRefListOut; + private final Dex.Section annotationSetRefListOut; /** individual annotations, each containing zero or more fields */ - private final DexBuffer.Section annotationOut; + private final Dex.Section annotationOut; private final TableOfContents contentsOut; @@ -87,12 +88,12 @@ public final class DexMerger { /** minimum number of wasted bytes before it's worthwhile to compact the result */ private int compactWasteThreshold = 1024 * 1024; // 1MiB - public DexMerger(DexBuffer dexA, DexBuffer dexB, CollisionPolicy collisionPolicy) + public DexMerger(Dex dexA, Dex dexB, CollisionPolicy collisionPolicy) throws IOException { this(dexA, dexB, collisionPolicy, new WriterSizes(dexA, dexB)); } - private DexMerger(DexBuffer dexA, DexBuffer dexB, CollisionPolicy collisionPolicy, + private DexMerger(Dex dexA, Dex dexB, CollisionPolicy collisionPolicy, WriterSizes writerSizes) throws IOException { this.dexA = dexA; this.dexB = dexB; @@ -156,7 +157,7 @@ public final class DexMerger { this.compactWasteThreshold = compactWasteThreshold; } - private DexBuffer mergeDexBuffers() throws IOException { + private Dex mergeDexes() throws IOException { mergeStringIds(); mergeTypeIds(); mergeTypeLists(); @@ -181,9 +182,9 @@ public final class DexMerger { return dexOut; } - public DexBuffer merge() throws IOException { + public Dex merge() throws IOException { long start = System.nanoTime(); - DexBuffer result = mergeDexBuffers(); + Dex result = mergeDexes(); /* * We use pessimistic sizes when merging dex files. If those sizes @@ -194,8 +195,8 @@ public final class DexMerger { int wastedByteCount = writerSizes.size() - compactedSizes.size(); if (wastedByteCount > + compactWasteThreshold) { DexMerger compacter = new DexMerger( - dexOut, new DexBuffer(), CollisionPolicy.FAIL, compactedSizes); - result = compacter.mergeDexBuffers(); + dexOut, new Dex(), CollisionPolicy.FAIL, compactedSizes); + result = compacter.mergeDexes(); System.out.printf("Result compacted from %.1fKiB to %.1fKiB to save %.1fKiB%n", dexOut.getLength() / 1024f, result.getLength() / 1024f, @@ -221,9 +222,9 @@ public final class DexMerger { * merged dex file. Populates maps from old to new indices in the process. */ abstract class IdMerger<T extends Comparable<T>> { - private final DexBuffer.Section out; + private final Dex.Section out; - protected IdMerger(DexBuffer.Section out) { + protected IdMerger(Dex.Section out) { this.out = out; } @@ -236,8 +237,8 @@ public final class DexMerger { TableOfContents.Section bSection = getSection(dexB.getTableOfContents()); getSection(contentsOut).off = out.getPosition(); - DexBuffer.Section inA = aSection.exists() ? dexA.open(aSection.off) : null; - DexBuffer.Section inB = bSection.exists() ? dexB.open(bSection.off) : null; + Dex.Section inA = aSection.exists() ? dexA.open(aSection.off) : null; + Dex.Section inB = bSection.exists() ? dexB.open(bSection.off) : null; int aOffset = -1; int bOffset = -1; int aIndex = 0; @@ -320,14 +321,14 @@ public final class DexMerger { getSection(contentsOut).size = outCount; } - private List<UnsortedValue> readUnsortedValues(DexBuffer source, IndexMap indexMap) { + private List<UnsortedValue> readUnsortedValues(Dex source, IndexMap indexMap) { TableOfContents.Section section = getSection(source.getTableOfContents()); if (!section.exists()) { return Collections.emptyList(); } List<UnsortedValue> result = new ArrayList<UnsortedValue>(); - DexBuffer.Section in = source.open(section.off); + Dex.Section in = source.open(section.off); for (int i = 0; i < section.size; i++) { int offset = in.getPosition(); T value = read(in, indexMap, 0); @@ -337,18 +338,18 @@ public final class DexMerger { } abstract TableOfContents.Section getSection(TableOfContents tableOfContents); - abstract T read(DexBuffer.Section in, IndexMap indexMap, int index); + abstract T read(Dex.Section in, IndexMap indexMap, int index); abstract void updateIndex(int offset, IndexMap indexMap, int oldIndex, int newIndex); abstract void write(T value); class UnsortedValue implements Comparable<UnsortedValue> { - final DexBuffer source; + final Dex source; final IndexMap indexMap; final T value; final int index; final int offset; - UnsortedValue(DexBuffer source, IndexMap indexMap, T value, int index, int offset) { + UnsortedValue(Dex source, IndexMap indexMap, T value, int index, int offset) { this.source = source; this.indexMap = indexMap; this.value = value; @@ -362,10 +363,10 @@ public final class DexMerger { } } - private IndexMap getIndexMap(DexBuffer dexBuffer) { - if (dexBuffer == dexA) { + private IndexMap getIndexMap(Dex dex) { + if (dex == dexA) { return aIndexMap; - } else if (dexBuffer == dexB) { + } else if (dex == dexB) { return bIndexMap; } else { throw new IllegalArgumentException(); @@ -378,7 +379,7 @@ public final class DexMerger { return tableOfContents.stringIds; } - @Override String read(DexBuffer.Section in, IndexMap indexMap, int index) { + @Override String read(Dex.Section in, IndexMap indexMap, int index) { return in.readString(); } @@ -400,7 +401,7 @@ public final class DexMerger { return tableOfContents.typeIds; } - @Override Integer read(DexBuffer.Section in, IndexMap indexMap, int index) { + @Override Integer read(Dex.Section in, IndexMap indexMap, int index) { int stringIndex = in.readInt(); return indexMap.adjustString(stringIndex); } @@ -424,7 +425,7 @@ public final class DexMerger { return tableOfContents.typeLists; } - @Override TypeList read(DexBuffer.Section in, IndexMap indexMap, int index) { + @Override TypeList read(Dex.Section in, IndexMap indexMap, int index) { return indexMap.adjustTypeList(in.readTypeList()); } @@ -444,7 +445,7 @@ public final class DexMerger { return tableOfContents.protoIds; } - @Override ProtoId read(DexBuffer.Section in, IndexMap indexMap, int index) { + @Override ProtoId read(Dex.Section in, IndexMap indexMap, int index) { return indexMap.adjust(in.readProtoId()); } @@ -467,7 +468,7 @@ public final class DexMerger { return tableOfContents.fieldIds; } - @Override FieldId read(DexBuffer.Section in, IndexMap indexMap, int index) { + @Override FieldId read(Dex.Section in, IndexMap indexMap, int index) { return indexMap.adjust(in.readFieldId()); } @@ -490,7 +491,7 @@ public final class DexMerger { return tableOfContents.methodIds; } - @Override MethodId read(DexBuffer.Section in, IndexMap indexMap, int index) { + @Override MethodId read(Dex.Section in, IndexMap indexMap, int index) { return indexMap.adjust(in.readMethodId()); } @@ -513,7 +514,7 @@ public final class DexMerger { return tableOfContents.annotations; } - @Override Annotation read(DexBuffer.Section in, IndexMap indexMap, int index) { + @Override Annotation read(Dex.Section in, IndexMap indexMap, int index) { return indexMap.adjust(in.readAnnotation()); } @@ -533,7 +534,7 @@ public final class DexMerger { contentsOut.classDefs.size = types.length; for (SortableType type : types) { - DexBuffer in = type.getBuffer(); + Dex in = type.getDex(); IndexMap indexMap = (in == dexA) ? aIndexMap : bIndexMap; transformClassDef(in, type.getClassDef(), indexMap); } @@ -581,7 +582,7 @@ public final class DexMerger { * Reads just enough data on each class so that we can sort it and then find * it later. */ - private void readSortableTypes(SortableType[] sortableTypes, DexBuffer buffer, + private void readSortableTypes(SortableType[] sortableTypes, Dex buffer, IndexMap indexMap) { for (ClassDef classDef : buffer.classDefs()) { SortableType sortableType = indexMap.adjust(new SortableType(buffer, classDef)); @@ -612,40 +613,40 @@ public final class DexMerger { transformStaticValues(dexB, bIndexMap); } - private void transformAnnotationSets(DexBuffer in, IndexMap indexMap) { + private void transformAnnotationSets(Dex in, IndexMap indexMap) { TableOfContents.Section section = in.getTableOfContents().annotationSets; if (section.exists()) { - DexBuffer.Section setIn = in.open(section.off); + Dex.Section setIn = in.open(section.off); for (int i = 0; i < section.size; i++) { transformAnnotationSet(indexMap, setIn); } } } - private void transformAnnotationSetRefLists(DexBuffer in, IndexMap indexMap) { + private void transformAnnotationSetRefLists(Dex in, IndexMap indexMap) { TableOfContents.Section section = in.getTableOfContents().annotationSetRefLists; if (section.exists()) { - DexBuffer.Section setIn = in.open(section.off); + Dex.Section setIn = in.open(section.off); for (int i = 0; i < section.size; i++) { transformAnnotationSetRefList(indexMap, setIn); } } } - private void transformAnnotationDirectories(DexBuffer in, IndexMap indexMap) { + private void transformAnnotationDirectories(Dex in, IndexMap indexMap) { TableOfContents.Section section = in.getTableOfContents().annotationsDirectories; if (section.exists()) { - DexBuffer.Section directoryIn = in.open(section.off); + Dex.Section directoryIn = in.open(section.off); for (int i = 0; i < section.size; i++) { transformAnnotationDirectory(directoryIn, indexMap); } } } - private void transformStaticValues(DexBuffer in, IndexMap indexMap) { + private void transformStaticValues(Dex in, IndexMap indexMap) { TableOfContents.Section section = in.getTableOfContents().encodedArrays; if (section.exists()) { - DexBuffer.Section staticValuesIn = in.open(section.off); + Dex.Section staticValuesIn = in.open(section.off); for (int i = 0; i < section.size; i++) { transformStaticValues(staticValuesIn, indexMap); } @@ -656,7 +657,7 @@ public final class DexMerger { * Reads a class_def_item beginning at {@code in} and writes the index and * data. */ - private void transformClassDef(DexBuffer in, ClassDef classDef, IndexMap indexMap) { + private void transformClassDef(Dex in, ClassDef classDef, IndexMap indexMap) { idsDefsOut.assertFourByteAligned(); idsDefsOut.writeInt(classDef.getTypeIndex()); idsDefsOut.writeInt(classDef.getAccessFlags()); @@ -686,7 +687,7 @@ public final class DexMerger { * Transform all annotations on a class. */ private void transformAnnotationDirectory( - DexBuffer.Section directoryIn, IndexMap indexMap) { + Dex.Section directoryIn, IndexMap indexMap) { contentsOut.annotationsDirectories.size++; annotationsDirectoryOut.assertFourByteAligned(); indexMap.putAnnotationDirectoryOffset( @@ -734,7 +735,7 @@ public final class DexMerger { /** * Transform all annotations on a single type, member or parameter. */ - private void transformAnnotationSet(IndexMap indexMap, DexBuffer.Section setIn) { + private void transformAnnotationSet(IndexMap indexMap, Dex.Section setIn) { contentsOut.annotationSets.size++; annotationSetOut.assertFourByteAligned(); indexMap.putAnnotationSetOffset(setIn.getPosition(), annotationSetOut.getPosition()); @@ -750,7 +751,7 @@ public final class DexMerger { /** * Transform all annotation set ref lists. */ - private void transformAnnotationSetRefList(IndexMap indexMap, DexBuffer.Section refListIn) { + private void transformAnnotationSetRefList(IndexMap indexMap, Dex.Section refListIn) { contentsOut.annotationSetRefLists.size++; annotationSetRefListOut.assertFourByteAligned(); indexMap.putAnnotationSetRefListOffset( @@ -763,7 +764,7 @@ public final class DexMerger { } } - private void transformClassData(DexBuffer in, ClassData classData, IndexMap indexMap) { + private void transformClassData(Dex in, ClassData classData, IndexMap indexMap) { contentsOut.classDatas.size++; ClassData.Field[] staticFields = classData.getStaticFields(); @@ -792,7 +793,7 @@ public final class DexMerger { } } - private void transformMethods(DexBuffer in, IndexMap indexMap, ClassData.Method[] methods) { + private void transformMethods(Dex in, IndexMap indexMap, ClassData.Method[] methods) { int lastOutMethodIndex = 0; for (ClassData.Method method : methods) { int outMethodIndex = indexMap.adjustMethod(method.getMethodIndex()); @@ -811,7 +812,7 @@ public final class DexMerger { } } - private void transformCode(DexBuffer in, Code code, IndexMap indexMap) { + private void transformCode(Dex in, Code code, IndexMap indexMap) { contentsOut.codes.size++; codeOut.assertFourByteAligned(); @@ -849,7 +850,7 @@ public final class DexMerger { * Unfortunately they're in the opposite order in the dex file so we * need to transform them out-of-order. */ - DexBuffer.Section triesSection = dexOut.open(codeOut.getPosition()); + Dex.Section triesSection = dexOut.open(codeOut.getPosition()); codeOut.skip(tries.length * SizeOf.TRY_ITEM); int[] offsets = transformCatchHandlers(indexMap, catchHandlers); transformTries(triesSection, tries, offsets); @@ -870,7 +871,7 @@ public final class DexMerger { return offsets; } - private void transformTries(DexBuffer.Section out, Code.Try[] tries, + private void transformTries(Dex.Section out, Code.Try[] tries, int[] catchHandlerOffsets) { for (Code.Try tryItem : tries) { out.writeInt(tryItem.getStartAddress()); @@ -890,7 +891,7 @@ public final class DexMerger { private static final byte DBG_SET_EPILOGUE_BEGIN = 0x08; private static final byte DBG_SET_FILE = 0x09; - private void transformDebugInfoItem(DexBuffer.Section in, IndexMap indexMap) { + private void transformDebugInfoItem(Dex.Section in, IndexMap indexMap) { contentsOut.debugInfos.size++; int lineStart = in.readUleb128(); debugInfoOut.writeUleb128(lineStart); @@ -982,7 +983,7 @@ public final class DexMerger { } } - private void transformStaticValues(DexBuffer.Section in, IndexMap indexMap) { + private void transformStaticValues(Dex.Section in, IndexMap indexMap) { contentsOut.encodedArrays.size++; indexMap.putStaticValuesOffset(in.getPosition(), encodedArrayOut.getPosition()); indexMap.adjustEncodedArray(in.readEncodedArray()).writeTo(encodedArrayOut); @@ -1017,7 +1018,7 @@ public final class DexMerger { /** * Compute sizes for merging a and b. */ - public WriterSizes(DexBuffer a, DexBuffer b) { + public WriterSizes(Dex a, Dex b) { plus(a.getTableOfContents(), false); plus(b.getTableOfContents(), false); } @@ -1071,8 +1072,8 @@ public final class DexMerger { debugInfo += contents.debugInfos.byteCount * 2; } - typeList = DexBuffer.fourByteAlign(typeList); - code = DexBuffer.fourByteAlign(code); + typeList = Dex.fourByteAlign(typeList); + code = Dex.fourByteAlign(code); } public int size() { @@ -1088,9 +1089,9 @@ public final class DexMerger { return; } - DexBuffer dexA = new DexBuffer(new File(args[1])); - DexBuffer dexB = new DexBuffer(new File(args[2])); - DexBuffer merged = new DexMerger(dexA, dexB, CollisionPolicy.KEEP_FIRST).merge(); + Dex dexA = new Dex(new File(args[1])); + Dex dexB = new Dex(new File(args[2])); + Dex merged = new DexMerger(dexA, dexB, CollisionPolicy.KEEP_FIRST).merge(); merged.writeTo(new File(args[0])); } |