Evaluate: Support dex in newer Android build tools
This commit is contained in:
@@ -18,28 +18,44 @@ package org.jetbrains.kotlin.android.debugger
|
|||||||
|
|
||||||
import com.android.dx.cf.direct.DirectClassFile
|
import com.android.dx.cf.direct.DirectClassFile
|
||||||
import com.android.dx.cf.direct.StdAttributeFactory
|
import com.android.dx.cf.direct.StdAttributeFactory
|
||||||
import com.android.dx.command.dexer.Main
|
import com.android.dx.dex.DexOptions
|
||||||
|
import com.android.dx.dex.cf.CfOptions
|
||||||
import com.android.dx.dex.cf.CfTranslator
|
import com.android.dx.dex.cf.CfTranslator
|
||||||
|
import com.android.dx.dex.file.ClassDefItem
|
||||||
import com.android.dx.dex.file.DexFile
|
import com.android.dx.dex.file.DexFile
|
||||||
import org.jetbrains.kotlin.idea.debugger.evaluate.classLoading.ClassToLoad
|
import org.jetbrains.kotlin.idea.debugger.evaluate.classLoading.ClassToLoad
|
||||||
|
import java.lang.reflect.Modifier
|
||||||
|
|
||||||
class AndroidDexWrapper {
|
class AndroidDexWrapper {
|
||||||
@Suppress("unused") // Used in AndroidOClassLoadingAdapter#dex
|
@Suppress("unused") // Used in AndroidOClassLoadingAdapter#dex
|
||||||
fun dex(classes: Collection<ClassToLoad>): ByteArray? {
|
fun dex(classes: Collection<ClassToLoad>): ByteArray? {
|
||||||
val dexArguments = Main.Arguments().apply { parse(arrayOf("testArgs")) }
|
val dexOptions = DexOptions()
|
||||||
|
val cfOptions = CfOptions()
|
||||||
|
|
||||||
val dexFile = DexFile(dexArguments.dexOptions)
|
val dexFile = DexFile(dexOptions)
|
||||||
|
|
||||||
|
val methodWithContext = CfTranslator::class.java.declaredMethods
|
||||||
|
.singleOrNull { it.name == "translate" && Modifier.isStatic(it.modifiers) && it.parameterCount == 6 }
|
||||||
|
|
||||||
|
val dxContext = methodWithContext?.let { Class.forName("com.android.dx.command.dexer.DxContext").newInstance() }
|
||||||
|
|
||||||
for ((_, relativeFileName, bytes) in classes) {
|
for ((_, relativeFileName, bytes) in classes) {
|
||||||
val cf = DirectClassFile(bytes, relativeFileName, true)
|
val cf = DirectClassFile(bytes, relativeFileName, true)
|
||||||
cf.setAttributeFactory(StdAttributeFactory.THE_ONE)
|
cf.setAttributeFactory(StdAttributeFactory.THE_ONE)
|
||||||
val classDef = CfTranslator.translate(
|
|
||||||
|
val classDef = if (methodWithContext != null) {
|
||||||
|
methodWithContext(
|
||||||
|
null,
|
||||||
|
dxContext,
|
||||||
cf,
|
cf,
|
||||||
bytes,
|
bytes,
|
||||||
dexArguments.cfOptions,
|
cfOptions,
|
||||||
dexArguments.dexOptions,
|
dexOptions,
|
||||||
dexFile
|
dexFile
|
||||||
)
|
) as ClassDefItem
|
||||||
|
} else {
|
||||||
|
CfTranslator.translate(cf, bytes, cfOptions, dexOptions, dexFile)
|
||||||
|
}
|
||||||
|
|
||||||
dexFile.add(classDef)
|
dexFile.add(classDef)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user