Main.java revision 86e5218220a0e01f7eb574a2480bd91f9ebfae35
1/* 2 * Copyright (C) 2008 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.tools.layoutlib.create; 18 19import java.io.IOException; 20import java.util.ArrayList; 21import java.util.HashSet; 22import java.util.List; 23import java.util.Map; 24import java.util.Set; 25 26 27/** 28 * Entry point for the layoutlib_create tool. 29 * <p/> 30 * The tool does not currently rely on any external configuration file. 31 * Instead the configuration is mostly done via the {@link CreateInfo} class. 32 * <p/> 33 * For a complete description of the tool and its implementation, please refer to 34 * the "README.txt" file at the root of this project. 35 * <p/> 36 * For a quick test, invoke this as follows: 37 * <pre> 38 * $ make layoutlib 39 * </pre> 40 * which does: 41 * <pre> 42 * $ make layoutlib_create <bunch of framework jars> 43 * $ java -jar out/host/linux-x86/framework/layoutlib_create.jar \ 44 * out/host/common/obj/JAVA_LIBRARIES/temp_layoutlib_intermediates/javalib.jar \ 45 * out/target/common/obj/JAVA_LIBRARIES/core_intermediates/classes.jar \ 46 * out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar 47 * </pre> 48 */ 49public class Main { 50 51 public static class Options { 52 public boolean generatePublicAccess = true; 53 public boolean listAllDeps = false; 54 public boolean listOnlyMissingDeps = false; 55 } 56 57 public static final Options sOptions = new Options(); 58 59 public static void main(String[] args) { 60 61 Log log = new Log(); 62 63 ArrayList<String> osJarPath = new ArrayList<String>(); 64 String[] osDestJar = { null }; 65 66 if (!processArgs(log, args, osJarPath, osDestJar)) { 67 log.error("Usage: layoutlib_create [-v] [-p] output.jar input.jar ..."); 68 log.error("Usage: layoutlib_create [-v] [--list-deps|--missing-deps] input.jar ..."); 69 System.exit(1); 70 } 71 72 if (sOptions.listAllDeps || sOptions.listOnlyMissingDeps) { 73 System.exit(listDeps(osJarPath, log)); 74 75 } else { 76 System.exit(createLayoutLib(osDestJar[0], osJarPath, log)); 77 } 78 79 80 System.exit(1); 81 } 82 83 private static int createLayoutLib(String osDestJar, ArrayList<String> osJarPath, Log log) { 84 log.info("Output: %1$s", osDestJar); 85 for (String path : osJarPath) { 86 log.info("Input : %1$s", path); 87 } 88 89 try { 90 CreateInfo info = new CreateInfo(); 91 Set<String> excludeClasses = getExcludedClasses(info); 92 AsmGenerator agen = new AsmGenerator(log, osDestJar, info); 93 94 AsmAnalyzer aa = new AsmAnalyzer(log, osJarPath, agen, 95 new String[] { // derived from 96 "android.view.View", 97 "android.app.Fragment" 98 }, 99 new String[] { // include classes 100 "android.*", // for android.R 101 "android.util.*", 102 "com.android.internal.util.*", 103 "android.view.*", 104 "android.widget.*", 105 "com.android.internal.widget.*", 106 "android.text.**", 107 "android.graphics.*", 108 "android.graphics.drawable.*", 109 "android.content.*", 110 "android.content.res.*", 111 "org.apache.harmony.xml.*", 112 "com.android.internal.R**", 113 "android.pim.*", // for datepicker 114 "android.os.*", // for android.os.Handler 115 "android.database.ContentObserver", // for Digital clock 116 "com.android.i18n.phonenumbers.*", // for TextView with autolink attribute 117 "android.app.DatePickerDialog", // b.android.com/28318 118 "android.app.TimePickerDialog", // b.android.com/61515 119 }, 120 excludeClasses, 121 new String[] { 122 "com/android/i18n/phonenumbers/data/*", 123 }); 124 aa.analyze(); 125 agen.generate(); 126 127 // Throw an error if any class failed to get renamed by the generator 128 // 129 // IMPORTANT: if you're building the platform and you get this error message, 130 // it means the renameClasses[] array in AsmGenerator needs to be updated: some 131 // class should have been renamed but it was not found in the input JAR files. 132 Set<String> notRenamed = agen.getClassesNotRenamed(); 133 if (notRenamed.size() > 0) { 134 // (80-column guide below for error formatting) 135 // 01234567890123456789012345678901234567890123456789012345678901234567890123456789 136 log.error( 137 "ERROR when running layoutlib_create: the following classes are referenced\n" + 138 "by tools/layoutlib/create but were not actually found in the input JAR files.\n" + 139 "This may be due to some platform classes having been renamed."); 140 for (String fqcn : notRenamed) { 141 log.error("- Class not found: %s", fqcn.replace('/', '.')); 142 } 143 for (String path : osJarPath) { 144 log.info("- Input JAR : %1$s", path); 145 } 146 return 1; 147 } 148 149 return 0; 150 } catch (IOException e) { 151 log.exception(e, "Failed to load jar"); 152 } catch (LogAbortException e) { 153 e.error(log); 154 } 155 156 return 1; 157 } 158 159 private static Set<String> getExcludedClasses(CreateInfo info) { 160 String[] refactoredClasses = info.getJavaPkgClasses(); 161 Set<String> excludedClasses = new HashSet<String>(refactoredClasses.length); 162 for (int i = 0; i < refactoredClasses.length; i+=2) { 163 excludedClasses.add(refactoredClasses[i]); 164 } 165 return excludedClasses; 166 167 } 168 169 private static int listDeps(ArrayList<String> osJarPath, Log log) { 170 DependencyFinder df = new DependencyFinder(log); 171 try { 172 List<Map<String, Set<String>>> result = df.findDeps(osJarPath); 173 if (sOptions.listAllDeps) { 174 df.printAllDeps(result); 175 } else if (sOptions.listOnlyMissingDeps) { 176 df.printMissingDeps(result); 177 } 178 } catch (IOException e) { 179 log.exception(e, "Failed to load jar"); 180 } 181 182 return 0; 183 } 184 185 /** 186 * Returns true if args where properly parsed. 187 * Returns false if program should exit with command-line usage. 188 * <p/> 189 * Note: the String[0] is an output parameter wrapped in an array, since there is no 190 * "out" parameter support. 191 */ 192 private static boolean processArgs(Log log, String[] args, 193 ArrayList<String> osJarPath, String[] osDestJar) { 194 boolean needs_dest = true; 195 for (int i = 0; i < args.length; i++) { 196 String s = args[i]; 197 if (s.equals("-v")) { 198 log.setVerbose(true); 199 } else if (s.equals("-p")) { 200 sOptions.generatePublicAccess = false; 201 } else if (s.equals("--list-deps")) { 202 sOptions.listAllDeps = true; 203 needs_dest = false; 204 } else if (s.equals("--missing-deps")) { 205 sOptions.listOnlyMissingDeps = true; 206 needs_dest = false; 207 } else if (!s.startsWith("-")) { 208 if (needs_dest && osDestJar[0] == null) { 209 osDestJar[0] = s; 210 } else { 211 osJarPath.add(s); 212 } 213 } else { 214 log.error("Unknow argument: %s", s); 215 return false; 216 } 217 } 218 219 if (osJarPath.isEmpty()) { 220 log.error("Missing parameter: path to input jar"); 221 return false; 222 } 223 if (needs_dest && osDestJar[0] == null) { 224 log.error("Missing parameter: path to output jar"); 225 return false; 226 } 227 228 sOptions.generatePublicAccess = false; 229 230 return true; 231 } 232} 233