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.Arrays; 22import java.util.HashSet; 23import java.util.List; 24import java.util.Map; 25import java.util.Set; 26 27 28/** 29 * Entry point for the layoutlib_create tool. 30 * <p/> 31 * The tool does not currently rely on any external configuration file. 32 * Instead the configuration is mostly done via the {@link CreateInfo} class. 33 * <p/> 34 * For a complete description of the tool and its implementation, please refer to 35 * the "README.txt" file at the root of this project. 36 * <p/> 37 * For a quick test, invoke this as follows: 38 * <pre> 39 * $ make layoutlib 40 * </pre> 41 * which does: 42 * <pre> 43 * $ make layoutlib_create <bunch of framework jars> 44 * $ java -jar out/host/linux-x86/framework/layoutlib_create.jar \ 45 * out/host/common/obj/JAVA_LIBRARIES/temp_layoutlib_intermediates/javalib.jar \ 46 * out/target/common/obj/JAVA_LIBRARIES/core_intermediates/classes.jar \ 47 * out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar 48 * </pre> 49 */ 50public class Main { 51 52 public static class Options { 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] 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 = info.getExcludedClasses(); 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 "com.android.internal.view.menu.ActionMenu", 120 }, 121 excludeClasses, 122 new String[] { 123 "com/android/i18n/phonenumbers/data/*", 124 }); 125 aa.analyze(); 126 agen.generate(); 127 128 // Throw an error if any class failed to get renamed by the generator 129 // 130 // IMPORTANT: if you're building the platform and you get this error message, 131 // it means the renameClasses[] array in AsmGenerator needs to be updated: some 132 // class should have been renamed but it was not found in the input JAR files. 133 Set<String> notRenamed = agen.getClassesNotRenamed(); 134 if (notRenamed.size() > 0) { 135 // (80-column guide below for error formatting) 136 // 01234567890123456789012345678901234567890123456789012345678901234567890123456789 137 log.error( 138 "ERROR when running layoutlib_create: the following classes are referenced\n" + 139 "by tools/layoutlib/create but were not actually found in the input JAR files.\n" + 140 "This may be due to some platform classes having been renamed."); 141 for (String fqcn : notRenamed) { 142 log.error("- Class not found: %s", fqcn.replace('/', '.')); 143 } 144 for (String path : osJarPath) { 145 log.info("- Input JAR : %1$s", path); 146 } 147 return 1; 148 } 149 150 return 0; 151 } catch (IOException e) { 152 log.exception(e, "Failed to load jar"); 153 } catch (LogAbortException e) { 154 e.error(log); 155 } 156 157 return 1; 158 } 159 160 private static int listDeps(ArrayList<String> osJarPath, Log log) { 161 DependencyFinder df = new DependencyFinder(log); 162 try { 163 List<Map<String, Set<String>>> result = df.findDeps(osJarPath); 164 if (sOptions.listAllDeps) { 165 df.printAllDeps(result); 166 } else if (sOptions.listOnlyMissingDeps) { 167 df.printMissingDeps(result); 168 } 169 } catch (IOException e) { 170 log.exception(e, "Failed to load jar"); 171 } 172 173 return 0; 174 } 175 176 /** 177 * Returns true if args where properly parsed. 178 * Returns false if program should exit with command-line usage. 179 * <p/> 180 * Note: the String[0] is an output parameter wrapped in an array, since there is no 181 * "out" parameter support. 182 */ 183 private static boolean processArgs(Log log, String[] args, 184 ArrayList<String> osJarPath, String[] osDestJar) { 185 boolean needs_dest = true; 186 for (String s : args) { 187 if (s.equals("-v")) { 188 log.setVerbose(true); 189 } else if (s.equals("--list-deps")) { 190 sOptions.listAllDeps = true; 191 needs_dest = false; 192 } else if (s.equals("--missing-deps")) { 193 sOptions.listOnlyMissingDeps = true; 194 needs_dest = false; 195 } else if (!s.startsWith("-")) { 196 if (needs_dest && osDestJar[0] == null) { 197 osDestJar[0] = s; 198 } else { 199 osJarPath.add(s); 200 } 201 } else { 202 log.error("Unknown argument: %s", s); 203 return false; 204 } 205 } 206 207 if (osJarPath.isEmpty()) { 208 log.error("Missing parameter: path to input jar"); 209 return false; 210 } 211 if (needs_dest && osDestJar[0] == null) { 212 log.error("Missing parameter: path to output jar"); 213 return false; 214 } 215 216 return true; 217 } 218} 219