androidscalamergedexsbt-android-plugin

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/...

Solution

  • 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