-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#24 Redirect dx log to IntelliJ & stop plugin execution on dx errors
- Loading branch information
Showing
3 changed files
with
55 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/org/ollide/java2smali/log/LogOutputStream.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "" | ||
} | ||
} |