Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to merge two or more .dex files into one .dex file using Scala?

I am doing some hacking on Jan Berkel's SBT Android Plugin and I was wandering if there is a way to merge multiple .dex files into one .dex file that will contain all of them.

For example, if I have this:

classes1.dex
classes2.dex
classes3.dex

Is there any way to merge them using Scala (in some acceptable time) to one single classes.dex file that will contain all 3 of them and have a following structure:

classes.dex
|-- classes1/...
|-- classes2/...
\-- classes3/...
like image 714
ioreskovic Avatar asked Jun 29 '12 06:06

ioreskovic


1 Answers

OK, it seems I found something.

import com.android.dx.io.DexBuffer
import com.android.dx.DexMerger
import com.android.dx.merge.CollisionPolicy
...
val dexA = DexBuffer(File(classes1DexFilePath))
val dexB = DexBuffer(File(classes2DexFilePath))
val dexMerger = DexMerger(dexA, dexB, CollisionPolicy.FAIL)
val dexM = dexMerger.merge()
dexM.writeTo(File(classesDexFilePath))

Could anyone verify this is indeed working?

Also, if this works, then merging more than 2 dex files should be the same as Max(Max(A, B), C), providing you write a method that with a prototype
DexBuffer merge(DexBuffer dexA, DexBuffer dexB)

Sources:
DexMerger
DexBuffer
CollisionPolicy

like image 60
ioreskovic Avatar answered Sep 24 '22 21:09

ioreskovic