Main.java revision c2016d4073c61e87439d4fa14837b397a3c1630a
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 }, 119 excludeClasses, 120 new String[] { 121 "com/android/i18n/phonenumbers/data/*", 122 }); 123 aa.analyze(); 124 agen.generate(); 125 126 // Throw an error if any class failed to get renamed by the generator 127 // 128 // IMPORTANT: if you're building the platform and you get this error message, 129 // it means the renameClasses[] array in AsmGenerator needs to be updated: some 130 // class should have been renamed but it was not found in the input JAR files. 131 Set<String> notRenamed = agen.getClassesNotRenamed(); 132 if (notRenamed.size() > 0) { 133 // (80-column guide below for error formatting) 134 // 01234567890123456789012345678901234567890123456789012345678901234567890123456789 135 log.error( 136 "ERROR when running layoutlib_create: the following classes are referenced\n" + 137 "by tools/layoutlib/create but were not actually found in the input JAR files.\n" + 138 "This may be due to some platform classes having been renamed."); 139 for (String fqcn : notRenamed) { 140 log.error("- Class not found: %s", fqcn.replace('/', '.')); 141 } 142 for (String path : osJarPath) { 143 log.info("- Input JAR : %1$s", path); 144 } 145 return 1; 146 } 147 148 return 0; 149 } catch (IOException e) { 150 log.exception(e, "Failed to load jar"); 151 } catch (LogAbortException e) { 152 e.error(log); 153 } 154 155 return 1; 156 } 157 158 private static Set<String> getExcludedClasses(CreateInfo info) { 159 String[] refactoredClasses = info.getJavaPkgClasses(); 160 Set<String> excludedClasses = new HashSet<String>(refactoredClasses.length); 161 for (int i = 0; i < refactoredClasses.length; i+=2) { 162 excludedClasses.add(refactoredClasses[i]); 163 } 164 return excludedClasses; 165 166 } 167 168 private static int listDeps(ArrayList<String> osJarPath, Log log) { 169 DependencyFinder df = new DependencyFinder(log); 170 try { 171 List<Map<String, Set<String>>> result = df.findDeps(osJarPath); 172 if (sOptions.listAllDeps) { 173 df.printAllDeps(result); 174 } else if (sOptions.listOnlyMissingDeps) { 175 df.printMissingDeps(result); 176 } 177 } catch (IOException e) { 178 log.exception(e, "Failed to load jar"); 179 } 180 181 return 0; 182 } 183 184 /** 185 * Returns true if args where properly parsed. 186 * Returns false if program should exit with command-line usage. 187 * <p/> 188 * Note: the String[0] is an output parameter wrapped in an array, since there is no 189 * "out" parameter support. 190 */ 191 private static boolean processArgs(Log log, String[] args, 192 ArrayList<String> osJarPath, String[] osDestJar) { 193 boolean needs_dest = true; 194 for (int i = 0; i < args.length; i++) { 195 String s = args[i]; 196 if (s.equals("-v")) { 197 log.setVerbose(true); 198 } else if (s.equals("-p")) { 199 sOptions.generatePublicAccess = false; 200 } else if (s.equals("--list-deps")) { 201 sOptions.listAllDeps = true; 202 needs_dest = false; 203 } else if (s.equals("--missing-deps")) { 204 sOptions.listOnlyMissingDeps = true; 205 needs_dest = false; 206 } else if (!s.startsWith("-")) { 207 if (needs_dest && osDestJar[0] == null) { 208 osDestJar[0] = s; 209 } else { 210 osJarPath.add(s); 211 } 212 } else { 213 log.error("Unknow argument: %s", s); 214 return false; 215 } 216 } 217 218 if (osJarPath.isEmpty()) { 219 log.error("Missing parameter: path to input jar"); 220 return false; 221 } 222 if (needs_dest && osDestJar[0] == null) { 223 log.error("Missing parameter: path to output jar"); 224 return false; 225 } 226 227 sOptions.generatePublicAccess = false; 228 229 return true; 230 } 231} 232