diff --git a/.gitignore b/.gitignore index 3065caeca..88fcea2a8 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ out/ bin/ target/ build/ +classes/ idea/ .gradle/ gradle.properties diff --git a/.travis.yml b/.travis.yml index 4fc05dda5..2d37e4d7b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: java jdk: - oraclejdk8 - - oraclejdk7 before_install: - chmod +x gradlew diff --git a/jadx-core/build.gradle b/jadx-core/build.gradle index 7b1aa7f70..a5f47fde1 100644 --- a/jadx-core/build.gradle +++ b/jadx-core/build.gradle @@ -3,7 +3,7 @@ ext.jadxClasspath = 'clsp-data/android-5.1.jar' dependencies { runtime files(jadxClasspath) - compile files('lib/dx.jar') + compile files('lib/dx-1.13.jar') compile 'commons-io:commons-io:2.4' compile 'org.ow2.asm:asm:5.0.3' compile 'com.intellij:annotations:12.0' diff --git a/jadx-core/lib/dx-1.13.jar b/jadx-core/lib/dx-1.13.jar new file mode 100644 index 000000000..37773c811 Binary files /dev/null and b/jadx-core/lib/dx-1.13.jar differ diff --git a/jadx-core/src/main/java/jadx/core/utils/files/JavaToDex.java b/jadx-core/src/main/java/jadx/core/utils/files/JavaToDex.java index 95263d6b5..eb9032dfa 100644 --- a/jadx-core/src/main/java/jadx/core/utils/files/JavaToDex.java +++ b/jadx-core/src/main/java/jadx/core/utils/files/JavaToDex.java @@ -1,19 +1,14 @@ package jadx.core.utils.files; -import jadx.core.utils.exceptions.JadxException; - import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.io.UnsupportedEncodingException; -import java.lang.reflect.Field; -import com.android.dx.command.DxConsole; +import com.android.dx.command.dexer.DxContext; import com.android.dx.command.dexer.Main; import com.android.dx.command.dexer.Main.Arguments; -import static com.android.dx.command.dexer.Main.run; +import jadx.core.utils.exceptions.JadxException; + import static jadx.core.utils.files.FileUtils.close; -import static java.lang.System.setOut; public class JavaToDex { @@ -36,41 +31,22 @@ public class JavaToDex { private String dxErrors; public byte[] convert(String javaFile) throws JadxException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream errOut = new ByteArrayOutputStream(); try { - DxConsole.err = new PrintStream(errOut, true, CHARSET_NAME); - } catch (UnsupportedEncodingException e) { - throw new JadxException(e.getMessage(), e); - } - PrintStream oldOut = System.out; - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try { - setOut(new PrintStream(baos, true, CHARSET_NAME)); + DxContext context = new DxContext(out, errOut); DxArgs args = new DxArgs("-", new String[]{javaFile}); - resetOutDexVar(); - run(args); - } catch (Throwable e) { + int result = (new Main(context)).runDx(args); + if (result != 0) { + throw new JadxException("Java to dex conversion error, code: " + result); + } + dxErrors = errOut.toString(CHARSET_NAME); + return out.toByteArray(); + } catch (Exception e) { throw new JadxException("dx exception: " + e.getMessage(), e); } finally { - close(baos); - System.setOut(oldOut); - } - try { - // errOut also contains warnings - dxErrors = errOut.toString(CHARSET_NAME); - } catch (UnsupportedEncodingException e) { - throw new JadxException("Can't save error output", e); - } - return baos.toByteArray(); - } - - private void resetOutDexVar() throws JadxException { - try { - Field outputDex = Main.class.getDeclaredField("outputDex"); - outputDex.setAccessible(true); - outputDex.set(null, null); - } catch (Exception e) { - throw new JadxException("Failed to reset outputDex field", e); + close(out); + close(errOut); } }