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

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


I found a way which don't need to compile from source, just use the SDK compiled jar.

java -cp dx.jar com.android.dx.merge.DexMerger output.dex input1.dex input2.dex

The dx.jar is located in Android SDK dir, like android\sdk\build-tools\26.0.2\lib. The dex file have 64k method limit, so it can't hold so much in one dex file.