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 "com.android.internal.graphics.drawable.AnimationScaleListDrawable", 128 }, 129 excludeClasses, 130 new String[] { 131 "com/android/i18n/phonenumbers/data/*", 132 "android/icu/impl/data/**" 133 }); 134 aa.analyze(); 135 agen.generate(); 136 137 // Throw an error if any class failed to get renamed by the generator 138 // 139 // IMPORTANT: if you're building the platform and you get this error message, 140 // it means the renameClasses[] array in AsmGenerator needs to be updated: some 141 // class should have been renamed but it was not found in the input JAR files. 142 Set<String> notRenamed = agen.getClassesNotRenamed(); 143 if (notRenamed.size() > 0) { 144 // (80-column guide below for error formatting) 145 // 01234567890123456789012345678901234567890123456789012345678901234567890123456789 146 log.error( 147 "ERROR when running layoutlib_create: the following classes are referenced\n" + 148 "by tools/layoutlib/create but were not actually found in the input JAR files.\n" + 149 "This may be due to some platform classes having been renamed."); 150 for (String fqcn : notRenamed) { 151 log.error("- Class not found: %s", fqcn.replace('/', '.')); 152 } 153 for (String path : osJarPath) { 154 log.info("- Input JAR : %1$s", path); 155 } 156 return 1; 157 } 158 159 return 0; 160 } catch (IOException e) { 161 log.exception(e, "Failed to load jar"); 162 } catch (LogAbortException e) { 163 e.error(log); 164 } 165 166 return 1; 167 } 168 listDeps(ArrayList<String> osJarPath, Log log)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 */ processArgs(Log log, String[] args, ArrayList<String> osJarPath, String[] osDestJar)192 private static boolean processArgs(Log log, String[] args, 193 ArrayList<String> osJarPath, String[] osDestJar) { 194 boolean needs_dest = true; 195 for (String s : args) { 196 if (s.equals("-v")) { 197 log.setVerbose(true); 198 } else if (s.equals("--list-deps")) { 199 sOptions.listAllDeps = true; 200 needs_dest = false; 201 } else if (s.equals("--missing-deps")) { 202 sOptions.listOnlyMissingDeps = true; 203 needs_dest = false; 204 } else if (!s.startsWith("-")) { 205 if (needs_dest && osDestJar[0] == null) { 206 osDestJar[0] = s; 207 } else { 208 osJarPath.add(s); 209 } 210 } else { 211 log.error("Unknown argument: %s", s); 212 return false; 213 } 214 } 215 216 if (osJarPath.isEmpty()) { 217 log.error("Missing parameter: path to input jar"); 218 return false; 219 } 220 if (needs_dest && osDestJar[0] == null) { 221 log.error("Missing parameter: path to output jar"); 222 return false; 223 } 224 225 return true; 226 } 227 } 228