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