Main.java revision 0cec7ba395236edb324ccb9217abdeeabd9d1dc0
1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.dx.command; 18 19import com.android.dx.Version; 20 21/** 22 * Main class for dx. It recognizes enough options to be able to dispatch 23 * to the right "actual" main. 24 */ 25public class Main { 26 private static final String USAGE_MESSAGE = 27 "usage:\n" + 28 " dx --dex [--debug] [--verbose] [--positions=<style>] [--no-locals]\n" + 29 " [--no-optimize] [--statistics] [--[no-]optimize-list=<file>] [--no-strict]\n" + 30 " [--keep-classes] [--output=<file>] [--dump-to=<file>] [--dump-width=<n>]\n" + 31 " [--dump-method=<name>[*]] [--verbose-dump] [--no-files] [--core-library]\n" + 32 " [--num-threads=<n>] [--incremental] [--force-jumbo] [--no-warning]\n" + 33 " [--multi-dex [--main-dex-list=<file> [--minimal-main-dex]]\n" + 34 " [--input-list=<file>]\n" + 35 " [<file>.class | <file>.{zip,jar,apk} | <directory>] ...\n" + 36 " Convert a set of classfiles into a dex file, optionally embedded in a\n" + 37 " jar/zip. Output name must end with one of: .dex .jar .zip .apk or be a\n" + 38 " directory.\n" + 39 " Positions options: none, important, lines.\n" + 40 " --multi-dex: allows to generate several dex files if needed. This option is\n" + 41 " exclusive with --incremental, causes --num-threads to be ignored and only\n" + 42 " supports folder or archive output.\n" + 43 " --main-dex-list=<file>: <file> is a list of class file names, classes\n" + 44 " defined by those class files are put in classes.dex.\n" + 45 " --minimal-main-dex: only classes selected by --main-dex-list are to be put\n" + 46 " in the main dex.\n" + 47 " --input-list: <file> is a list of inputs.\n" + 48 " Each line in <file> must end with one of: .class .jar .zip .apk or be a\n" + 49 " directory.\n" + 50 " dx --annotool --annotation=<class> [--element=<element types>]\n" + 51 " [--print=<print types>]\n" + 52 " dx --dump [--debug] [--strict] [--bytes] [--optimize]\n" + 53 " [--basic-blocks | --rop-blocks | --ssa-blocks | --dot] [--ssa-step=<step>]\n" + 54 " [--width=<n>] [<file>.class | <file>.txt] ...\n" + 55 " Dump classfiles, or transformations thereof, in a human-oriented format.\n" + 56 " dx --find-usages <file.dex> <declaring type> <member>\n" + 57 " Find references and declarations to a field or method.\n" + 58 " <declaring type> is a class name in internal form, like Ljava/lang/Object;\n" + 59 " <member> is a field or method name, like hashCode.\n" + 60 " dx -J<option> ... <arguments, in one of the above forms>\n" + 61 " Pass VM-specific options to the virtual machine that runs dx.\n" + 62 " dx --version\n" + 63 " Print the version of this tool (" + Version.VERSION + ").\n" + 64 " dx --help\n" + 65 " Print this message."; 66 67 /** 68 * This class is uninstantiable. 69 */ 70 private Main() { 71 // This space intentionally left blank. 72 } 73 74 /** 75 * Run! 76 */ 77 public static void main(String[] args) { 78 boolean gotCmd = false; 79 boolean showUsage = false; 80 81 try { 82 for (int i = 0; i < args.length; i++) { 83 String arg = args[i]; 84 if (arg.equals("--") || !arg.startsWith("--")) { 85 gotCmd = false; 86 showUsage = true; 87 break; 88 } 89 90 gotCmd = true; 91 if (arg.equals("--dex")) { 92 com.android.dx.command.dexer.Main.main(without(args, i)); 93 break; 94 } else if (arg.equals("--dump")) { 95 com.android.dx.command.dump.Main.main(without(args, i)); 96 break; 97 } else if (arg.equals("--annotool")) { 98 com.android.dx.command.annotool.Main.main( 99 without(args, i)); 100 break; 101 } else if (arg.equals("--find-usages")) { 102 com.android.dx.command.findusages.Main.main(without(args, i)); 103 break; 104 } else if (arg.equals("--version")) { 105 version(); 106 break; 107 } else if (arg.equals("--help")) { 108 showUsage = true; 109 break; 110 } else { 111 gotCmd = false; 112 } 113 } 114 } catch (UsageException ex) { 115 showUsage = true; 116 } catch (RuntimeException ex) { 117 System.err.println("\nUNEXPECTED TOP-LEVEL EXCEPTION:"); 118 ex.printStackTrace(); 119 System.exit(2); 120 } catch (Throwable ex) { 121 System.err.println("\nUNEXPECTED TOP-LEVEL ERROR:"); 122 ex.printStackTrace(); 123 if ((ex instanceof NoClassDefFoundError) 124 || (ex instanceof NoSuchMethodError)) { 125 System.err.println( 126 "Note: You may be using an incompatible " + 127 "virtual machine or class library.\n" + 128 "(This program is known to be incompatible " + 129 "with recent releases of GCJ.)"); 130 } 131 System.exit(3); 132 } 133 134 if (!gotCmd) { 135 System.err.println("error: no command specified"); 136 showUsage = true; 137 } 138 139 if (showUsage) { 140 usage(); 141 System.exit(1); 142 } 143 } 144 145 /** 146 * Prints the version message. 147 */ 148 private static void version() { 149 System.err.println("dx version " + Version.VERSION); 150 System.exit(0); 151 } 152 153 /** 154 * Prints the usage message. 155 */ 156 private static void usage() { 157 System.err.println(USAGE_MESSAGE); 158 } 159 160 /** 161 * Returns a copy of the given args array, but without the indicated 162 * element. 163 * 164 * @param orig {@code non-null;} original array 165 * @param n which element to omit 166 * @return {@code non-null;} new array 167 */ 168 private static String[] without(String[] orig, int n) { 169 int len = orig.length - 1; 170 String[] newa = new String[len]; 171 System.arraycopy(orig, 0, newa, 0, n); 172 System.arraycopy(orig, n + 1, newa, n, len - n); 173 return newa; 174 } 175} 176