use of com.taobao.android.dex.Dex in project atlas by alibaba.
the class DexMerger method mergeClassDefs.
private void mergeClassDefs() {
SortableType[] types = getSortedTypes();
contentsOut.classDefs.off = idsDefsOut.getPosition();
contentsOut.classDefs.size = types.length;
for (SortableType type : types) {
Dex in = type.getDex();
IndexMap indexMap = indexMaps.get(in);
transformClassDef(in, type.getClassDef(), indexMap);
}
}
use of com.taobao.android.dex.Dex in project atlas by alibaba.
the class DexMerger method getSortedTypes.
/**
* Returns the union of classes from both files, sorted in order such that
* a class is always preceded by its supertype and implemented interfaces.
*/
private SortableType[] getSortedTypes() {
// size is pessimistic; doesn't include arrays
SortableType[] sortableTypes = new SortableType[contentsOut.typeIds.size];
for (Dex d : dexs) {
readSortableTypes(sortableTypes, d, indexMaps.get(d));
}
/*
* Populate the depths of each sortable type. This makes D iterations
* through all N types, where 'D' is the depth of the deepest type. For
* example, the deepest class in libcore is Xalan's KeyIterator, which
* is 11 types deep.
*/
while (true) {
boolean allDone = true;
for (SortableType sortableType : sortableTypes) {
if (sortableType != null && !sortableType.isDepthAssigned()) {
allDone &= sortableType.tryAssignDepth(sortableTypes);
}
}
if (allDone) {
break;
}
}
// Now that all types have depth information, the result can be sorted
Arrays.sort(sortableTypes, SortableType.NULLS_LAST_ORDER);
// Strip nulls from the end
int firstNull = Arrays.asList(sortableTypes).indexOf(null);
return firstNull != -1 ? Arrays.copyOfRange(sortableTypes, 0, firstNull) : sortableTypes;
}
use of com.taobao.android.dex.Dex in project atlas by alibaba.
the class Main method mergeIncremental.
/**
* Merges the dex files {@code update} and {@code base}, preferring
* {@code update}'s definition for types defined in both dex files.
*
* @param base a file to find the previous dex file. May be a .dex file, a
* jar file possibly containing a .dex file, or null.
* @return the bytes of the merged dex file, or null if both the update
* and the base dex do not exist.
*/
private static byte[] mergeIncremental(byte[] update, File base) throws IOException {
Dex dexA = null;
Dex dexB = null;
if (update != null) {
dexA = new Dex(update);
}
if (base.exists()) {
dexB = new Dex(base);
}
Dex result;
if (dexA == null && dexB == null) {
return null;
} else if (dexA == null) {
result = dexB;
} else if (dexB == null) {
result = dexA;
} else {
result = new DexMerger(ImmutableList.of(dexA, dexB), CollisionPolicy.KEEP_FIRST).merge();
}
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
result.writeTo(bytesOut);
return bytesOut.toByteArray();
}
use of com.taobao.android.dex.Dex in project atlas by alibaba.
the class DexBuilderUtils method mergeDex.
/**
* 合并dex
* @param dexs
* @param outDexFile
* @param policy
* @return
* @throws IOException
*/
public static File mergeDex(List<File> dexs, File outDexFile, CollisionPolicy policy) throws IOException {
List<Dex> dexList = new ArrayList<Dex>();
for (File dexFile : dexs) {
dexList.add(new Dex(dexFile));
}
// Dex[] dexArray = new Dex[dexList.size()];
// dexList.toArray(dexArray);
DexMerger merger = new DexMerger(dexList, policy);
Dex outDex = merger.merge();
outDex.writeTo(outDexFile);
return outDexFile;
}
use of com.taobao.android.dex.Dex in project atlas by alibaba.
the class MergePatch method mergeCode.
private void mergeCode(File dexFile) throws IOException {
Dex dexA = null;
Dex dexB = null;
for (File file : patchs) {
if (!dexFile.exists() && dexA == null) {
dexA = getDexFromJar(file);
continue;
} else if (dexFile.exists()) {
dexA = new Dex(dexFile);
}
dexB = getDexFromJar(file);
Dex[] dexs = new Dex[2];
dexs[0] = dexA;
dexs[1] = dexB;
// List<Dex>dexes = new ArrayList<>();
List<Dex> dexes = Arrays.asList(dexs);
DexMerger dexMerger = new DexMerger(dexes, CollisionPolicy.FAIL);
dexMerger.merge().writeTo(dexFile);
}
}
Aggregations