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 17 package com.android.tools.layoutlib.create; 18 19 import org.objectweb.asm.Opcodes; 20 21 import java.io.IOException; 22 import java.util.ArrayList; 23 import java.util.List; 24 import java.util.Map; 25 import 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 */ 50 public class Main { 51 52 public static class Options { 53 public boolean listAllDeps = false; 54 public boolean listOnlyMissingDeps = false; 55 } 56 57 public static final int ASM_VERSION = Opcodes.ASM5; 58 59 public static final Options sOptions = new Options(); 60 main(String[] args)61 public static void main(String[] args) { 62 63 Log log = new Log(); 64 65 ArrayList<String> osJarPath = new ArrayList<>(); 66 String[] osDestJar = { null }; 67 68 if (!processArgs(log, args, osJarPath, osDestJar)) { 69 log.error("Usage: layoutlib_create [-v] output.jar input.jar ..."); 70 log.error("Usage: layoutlib_create [-v] [--list-deps|--missing-deps] input.jar ..."); 71 System.exit(1); 72 } 73 74 if (sOptions.listAllDeps || sOptions.listOnlyMissingDeps) { 75 System.exit(listDeps(osJarPath, log)); 76 77 } else { 78 System.exit(createLayoutLib(osDestJar[0], osJarPath, log)); 79 } 80 81 82 System.exit(1); 83 } 84 createLayoutLib(String osDestJar, ArrayList<String> osJarPath, Log log)85 private static int createLayoutLib(String osDestJar, ArrayList<String> osJarPath, Log log) { 86 log.info("Output: %1$s", osDestJar); 87 for (String path : osJarPath) { 88 log.info("Input : %1$s", path); 89 } 90 91 try { 92 CreateInfo info = new CreateInfo(); 93 Set<String> excludeClasses = info.getExcludedClasses(); 94 AsmGenerator agen = new AsmGenerator(log, osDestJar, info); 95 96 AsmAnalyzer aa = new AsmAnalyzer(log, osJarPath, agen, 97 new String[] { // derived from 98 "android.view.View", 99 "android.app.Fragment" 100 }, 101 new String[] { // include classes 102 "android.*", // for android.R 103 "android.util.*", 104 "com.android.internal.util.*", 105 "android.view.*", 106 "android.widget.*", 107 "com.android.internal.widget.*", 108 "android.text.**", 109 "android.graphics.*", 110 "android.graphics.drawable.**", 111 "android.content.*", 112 "android.content.res.*", 113 "android.preference.*", 114 "org.apache.harmony.xml.*", 115 "com.android.internal.R**", 116 "android.pim.*", // for datepicker 117 "android.os.*", // for android.os.Handler 118 "android.database.ContentObserver", // for Digital clock 119 "com.android.i18n.phonenumbers.*", // for TextView with autolink attribute 120 "android.app.DatePickerDialog", // b.android.com/28318 121 "android.app.TimePickerDialog", // b.android.com/61515 122 "com.android.internal.view.menu.ActionMenu", 123 "android.icu.**", // needed by LayoutLib 124 "android.annotation.NonNull", // annotations 125 "android.annotation.Nullable", // annotations 126 "com.android.internal.transition.EpicenterTranslateClipReveal", 127 }, 128 excludeClasses, 129 new String[] { 130 "com/android/i18n/phonenumbers/data/*", 131 "android/icu/impl/data/**" 132 }); 133 aa.analyze(); 134 agen.generate(); 135 136 // Throw an error if any class failed to get renamed by the generator 137 // 138 // IMPORTANT: if you're building the platform and you get this error message, 139 // it means the renameClasses[] array in AsmGenerator needs to be updated: some 140 // class should have been renamed but it was not found in the input JAR files. 141 Set<String> notRenamed = agen.getClassesNotRenamed(); 142 if (notRenamed.size() > 0) { 143 // (80-column guide below for error formatting) 144 // 01234567890123456789012345678901234567890123456789012345678901234567890123456789 145 log.error( 146 "ERROR when running layoutlib_create: the following classes are referenced\n" + 147 "by tools/layoutlib/create but were not actually found in the input JAR files.\n" + 148 "This may be due to some platform classes having been renamed."); 149 for (String fqcn : notRenamed) { 150 log.error("- Class not found: %s", fqcn.replace('/', '.')); 151 } 152 for (String path : osJarPath) { 153 log.info("- Input JAR : %1$s", path); 154 } 155 return 1; 156 } 157 158 return 0; 159 } catch (IOException e) { 160 log.exception(e, "Failed to load jar"); 161 } catch (LogAbortException e) { 162 e.error(log); 163 } 164 165 return 1; 166 } 167 listDeps(ArrayList<String> osJarPath, Log log)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 */ processArgs(Log log, String[] args, ArrayList<String> osJarPath, String[] osDestJar)191 private static boolean processArgs(Log log, String[] args, 192 ArrayList<String> osJarPath, String[] osDestJar) { 193 boolean needs_dest = true; 194 for (String s : args) { 195 if (s.equals("-v")) { 196 log.setVerbose(true); 197 } else if (s.equals("--list-deps")) { 198 sOptions.listAllDeps = true; 199 needs_dest = false; 200 } else if (s.equals("--missing-deps")) { 201 sOptions.listOnlyMissingDeps = true; 202 needs_dest = false; 203 } else if (!s.startsWith("-")) { 204 if (needs_dest && osDestJar[0] == null) { 205 osDestJar[0] = s; 206 } else { 207 osJarPath.add(s); 208 } 209 } else { 210 log.error("Unknown argument: %s", s); 211 return false; 212 } 213 } 214 215 if (osJarPath.isEmpty()) { 216 log.error("Missing parameter: path to input jar"); 217 return false; 218 } 219 if (needs_dest && osDestJar[0] == null) { 220 log.error("Missing parameter: path to output jar"); 221 return false; 222 } 223 224 return true; 225 } 226 } 227