Skip to content

Commit

Permalink
#24 Redirect dx log to IntelliJ & stop plugin execution on dx errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ollide committed Apr 25, 2021
1 parent ad1339b commit bcc4082
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
26 changes: 19 additions & 7 deletions src/main/kotlin/org/ollide/java2smali/Class2DexHelper.kt
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
package org.ollide.java2smali

import com.android.dx.command.dexer.DxContext
import com.android.dx.command.dexer.Main

import java.io.IOException
import com.intellij.openapi.diagnostic.Logger
import org.apache.log4j.Level
import org.ollide.java2smali.log.LogOutputStream

object Class2DexHelper {

/**
* Uses the dx tool from the Android Build Tools (19.0.1) to create
* Uses the dx tool from the Android Build Tools to create
* a .dex version of a compiled java file (.class)
*
* @param inputClassFilePaths full paths to the compiled .class file
* @param outputDexPath this will be the dex output file's path and name
* @throws IOException
* @return `true` if dx ran successfully, otherwise `false`
*/
@Throws(IOException::class)
fun dexClassFile(inputClassFilePaths: Array<String>, outputDexPath: String) {
fun dexClassFile(inputClassFilePaths: Array<String>, outputDexPath: String): Boolean {
val dxContext = DxContext(LogOutputStream(LOG, Level.INFO), LogOutputStream(LOG, Level.ERROR))

val arguments = Main.Arguments()
arguments.outName = outputDexPath
arguments.strictNameCheck = false
arguments.fileNames = inputClassFilePaths

Main.run(arguments)
return try {
val returnCode = Main(dxContext).runDx(arguments)
returnCode == 0
} catch (e: Exception) {
LOG.error("Failed to run dx", e)
false
}
}

private val LOG = Logger.getInstance(Class2DexHelper::class.java)

}
15 changes: 4 additions & 11 deletions src/main/kotlin/org/ollide/java2smali/DexCompiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.intellij.util.SmartList
import com.intellij.util.containers.OrderedSet
import com.intellij.util.io.URLUtil
import java.io.File
import java.io.IOException
import java.nio.file.Paths

class DexCompiler(private val vFile: VirtualFile, private val project: Project, private val module: Module) {
Expand Down Expand Up @@ -59,7 +58,10 @@ class DexCompiler(private val vFile: VirtualFile, private val project: Project,

// CLASS -> DEX
val targetFiles = getClassFiles(fileOutputDirectory, fileName)
compileDexFile(targetFiles, dexFilePath)
val successfulDex = Class2DexHelper.dexClassFile(targetFiles, dexFilePath)
if (!successfulDex) {
return
}

// DEX -> SMALI
val outputDir = getSourceRootFile().path
Expand Down Expand Up @@ -144,15 +146,6 @@ class DexCompiler(private val vFile: VirtualFile, private val project: Project,
return OrderedSet(outputPaths)
}

private fun compileDexFile(compiledPaths: Array<String>, dexFile: String) {
try {
Class2DexHelper.dexClassFile(compiledPaths, dexFile)
} catch (e: IOException) {
e.printStackTrace()
return
}
}

private fun getSourceRootFile(): VirtualFile {
return ProjectRootManager.getInstance(project).fileIndex.getSourceRootForFile(vFile) as VirtualFile
}
Expand Down
32 changes: 32 additions & 0 deletions src/main/kotlin/org/ollide/java2smali/log/LogOutputStream.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.ollide.java2smali.log

import com.intellij.openapi.diagnostic.Logger
import org.apache.log4j.Level
import java.io.OutputStream

/**
* Wraps IntelliJ's [com.intellij.openapi.diagnostic.Logger] as an [java.io.OutputStream].
*/
class LogOutputStream(private val logger: Logger, private val level: Level) : OutputStream() {

private var msg = ""

override fun write(b: Int) {
val bytes = ByteArray(1)
bytes[0] = (b and 0xff).toByte()
msg += String(bytes)
if (msg.endsWith("\n")) {
msg = msg.substring(0, msg.length - 1)
flush()
}
}

override fun flush() {
if (level === Level.ERROR || level === Level.FATAL) {
logger.error(msg)
} else {
logger.info(msg)
}
msg = ""
}
}

0 comments on commit bcc4082

Please sign in to comment.