PageRenderTime 66ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/dex-tools/src/main/java/com/googlecode/dex2jar/tools/Dex2jarCmd.java

https://code.google.com/p/dex2jar/
Java | 133 lines | 96 code | 21 blank | 16 comment | 26 complexity | b597b748eaa3c38929ecf5e6daed0f38 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. /*
  2. * dex2jar - Tools to work with android .dex and java .class files
  3. * Copyright (c) 2009-2012 Panxiaobo
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.googlecode.dex2jar.tools;
  18. import java.io.File;
  19. import java.util.Map;
  20. import org.apache.commons.io.FilenameUtils;
  21. import com.googlecode.dex2jar.Method;
  22. import com.googlecode.dex2jar.ir.ET;
  23. import com.googlecode.dex2jar.reader.DexFileReader;
  24. import com.googlecode.dex2jar.v3.Dex2jar;
  25. import com.googlecode.dex2jar.v3.DexExceptionHandlerImpl;
  26. import com.googlecode.dex2jar.v3.Main;
  27. public class Dex2jarCmd extends BaseCmd {
  28. public static void main(String[] args) {
  29. new Dex2jarCmd().doMain(args);
  30. }
  31. @Opt(opt = "e", longOpt = "exception-file", description = "detail exception file, default is $current_dir/[file-name]-error.zip", argName = "file")
  32. private File exceptionFile;
  33. @Opt(opt = "f", longOpt = "force", hasArg = false, description = "force overwrite")
  34. private boolean forceOverwrite = false;
  35. @Opt(opt = "n", longOpt = "not-handle-exception", hasArg = false, description = "not handle any exception throwed by dex2jar")
  36. private boolean notHandleException = false;
  37. @Opt(opt = "o", longOpt = "output", description = "output .jar file, default is $current_dir/[file-name]-dex2jar.jar", argName = "out-jar-file")
  38. private File output;
  39. @Opt(opt = "r", longOpt = "reuse-reg", hasArg = false, description = "reuse regiter while generate java .class file")
  40. private boolean reuseReg = false;
  41. @Opt(opt = "s", hasArg = false, description = "same with --topological-sort/-ts")
  42. private boolean topologicalSort1 = false;
  43. @Opt(opt = "v", longOpt = "verbose", hasArg = false, description = "show progress")
  44. private boolean verbose = false;
  45. @Opt(opt = "ts", longOpt = "topological-sort", hasArg = false, description = "sort block by topological, that will generate more readable code")
  46. private boolean topologicalSort = false;
  47. @Opt(opt = "d", longOpt = "debug-info", hasArg = false, description = "translate debug info")
  48. private boolean debugInfo = false;
  49. @Opt(opt = "p", longOpt = "print-ir", hasArg = false, description = "print ir to Syste.out")
  50. private boolean printIR = false;
  51. @Opt(opt = "os", longOpt = "optmize-synchronized", hasArg = false, description = "optmize-synchronized")
  52. private boolean optmizeSynchronized = false;
  53. public Dex2jarCmd() {
  54. super("d2j-dex2jar [options] <file0> [file1 ... fileN]", "convert dex to jar");
  55. }
  56. @Override
  57. protected void doCommandLine() throws Exception {
  58. if (remainingArgs.length == 0) {
  59. usage();
  60. return;
  61. }
  62. if ((exceptionFile != null || output != null) && remainingArgs.length != 1) {
  63. System.err.println("-e/-o can only used with one file");
  64. return;
  65. }
  66. if (debugInfo && reuseReg) {
  67. System.err.println("-d/-r can not use together");
  68. return;
  69. }
  70. if (output != null) {
  71. if (output.exists() && !forceOverwrite) {
  72. System.err.println(output + " exists, use --force to overwrite");
  73. return;
  74. }
  75. } else {
  76. for (String fileName : remainingArgs) {
  77. File file = new File(FilenameUtils.getBaseName(fileName) + "-dex2jar.jar");
  78. if (file.exists() && !forceOverwrite) {
  79. System.err.println(file + " exists, use --force to overwrite");
  80. return;
  81. }
  82. }
  83. }
  84. for (String fileName : remainingArgs) {
  85. File file = output == null ? new File(FilenameUtils.getBaseName(fileName) + "-dex2jar.jar") : output;
  86. System.err.println("dex2jar " + fileName + " -> " + file);
  87. DexFileReader reader = new DexFileReader(new File(fileName));
  88. DexExceptionHandlerImpl handler = notHandleException ? null : new DexExceptionHandlerImpl()
  89. .skipDebug(!debugInfo);
  90. Dex2jar.from(reader).withExceptionHandler(handler).reUseReg(reuseReg)
  91. .topoLogicalSort(topologicalSort || topologicalSort1).skipDebug(!debugInfo)
  92. .optimizeSynchronized(this.optmizeSynchronized).printIR(printIR).verbose(verbose).to(file);
  93. if (!notHandleException) {
  94. Map<Method, Exception> exceptions = handler.getExceptions();
  95. if (exceptions != null && exceptions.size() > 0) {
  96. File errorFile = exceptionFile == null ? new File(FilenameUtils.getBaseName(fileName)
  97. + "-error.zip") : exceptionFile;
  98. handler.dumpException(reader, errorFile);
  99. System.err.println("Detail Error Information in File " + errorFile);
  100. System.err
  101. .println("Please report this file to http://code.google.com/p/dex2jar/issues/entry if possible.");
  102. }
  103. }
  104. }
  105. }
  106. @Override
  107. protected String getVersionString() {
  108. return "reader-" + DexFileReader.class.getPackage().getImplementationVersion() + ", translator-"
  109. + Main.class.getPackage().getImplementationVersion() + ", ir-"
  110. + ET.class.getPackage().getImplementationVersion();
  111. }
  112. }