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.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Set;
29 import java.util.stream.Collectors;
30 
31 import com.google.common.io.Files;
32 
33 
34 /**
35  * Entry point for the layoutlib_create tool.
36  * <p/>
37  * The tool does not currently rely on any external configuration file.
38  * Instead the configuration is mostly done via the {@link CreateInfo} class.
39  * <p/>
40  * For a complete description of the tool and its implementation, please refer to
41  * the "README.txt" file at the root of this project.
42  * <p/>
43  * For a quick test, invoke this as follows:
44  * <pre>
45  * $ make layoutlib
46  * </pre>
47  * which does:
48  * <pre>
49  * $ make layoutlib_create &lt;bunch of framework jars&gt;
50  * $ java -jar out/host/linux-x86/framework/layoutlib_create.jar \
51  *        out/host/common/obj/JAVA_LIBRARIES/temp_layoutlib_intermediates/javalib.jar \
52  *        out/target/common/obj/JAVA_LIBRARIES/core-libart_intermediates/classes.jar \
53  *        out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar
54  * </pre>
55  */
56 public class Main {
57 
58     private static class Options {
59         private boolean listAllDeps = false;
60         private boolean listOnlyMissingDeps = false;
61         private boolean createStubLib = false;
62     }
63 
64     public static final int ASM_VERSION = Opcodes.ASM9;
65 
66     private static final Options sOptions = new Options();
67 
main(String[] args)68     public static void main(String[] args) {
69 
70         Log log = new Log();
71 
72         ArrayList<String> osJarPath = new ArrayList<>();
73         String[] osDestJar = { null };
74 
75         if (!processArgs(log, args, osJarPath, osDestJar)) {
76             log.error("Usage: layoutlib_create [-v] [--create-stub] output.jar input.jar ...");
77             log.error("Usage: layoutlib_create [-v] [--list-deps|--missing-deps] input.jar ...");
78             System.exit(1);
79         }
80 
81         if (sOptions.listAllDeps || sOptions.listOnlyMissingDeps) {
82             System.exit(listDeps(osJarPath, log));
83 
84         } else {
85             System.exit(createLayoutLib(osDestJar[0], osJarPath, log));
86         }
87 
88 
89         System.exit(1);
90     }
91 
createLayoutLib(String osDestJar, ArrayList<String> osJarPath, Log log)92     private static int createLayoutLib(String osDestJar, ArrayList<String> osJarPath, Log log) {
93         log.info("Output: %1$s", osDestJar);
94         for (String path : osJarPath) {
95             log.info("Input :      %1$s", path);
96         }
97 
98         try {
99             ICreateInfo info = new CreateInfo();
100             AsmGenerator agen = new AsmGenerator(log, info);
101 
102             AsmAnalyzer aa = new AsmAnalyzer(log, osJarPath,
103                     new String[] {                          // derived from
104                         "android.app.Fragment",
105                         "android.view.View",
106                     },
107                     new String[] {                          // include classes
108                         "android.*", // for android.R
109                         "android.annotation.NonNull",       // annotations
110                         "android.annotation.Nullable",      // annotations
111                         "android.app.ApplicationErrorReport", // needed for Glance LazyList
112                         "android.app.DatePickerDialog",     // b.android.com/28318
113                         "android.app.TimePickerDialog",     // b.android.com/61515
114                         "android.content.*",
115                         "android.content.res.*",
116                         "android.database.ContentObserver", // for Digital clock
117                         "android.graphics.*",
118                         "android.graphics.drawable.**",
119                         "android.icu.**",                   // needed by LayoutLib
120                         "android.os.*",  // for android.os.Handler
121                         "android.os.ext.*", // for android.os.ext.SdkExtensions, needed by Compose
122                         "android.pim.*", // for datepicker
123                         "android.preference.*",
124                         "android.service.wallpaper.*",      // needed for Wear OS watch faces
125                         "android.text.**",
126                         "android.util.*",
127                         "android.view.*",
128                         "android.widget.*",
129                         "com.android.i18n.phonenumbers.*",  // for TextView with autolink attribute
130                         "com.android.internal.R**",
131                         "com.android.internal.graphics.drawable.AnimationScaleListDrawable",
132                         "com.android.internal.transition.EpicenterTranslateClipReveal",
133                         "com.android.internal.util.*",
134                         "com.android.internal.view.menu.ActionMenu",
135                         "com.android.internal.widget.*",
136                         "com.android.systemui.monet.*",     // needed for dynamic theming
137                         "com.google.android.apps.common.testing.accessibility.**",
138                         "com.google.android.libraries.accessibility.**",
139                         "libcore.icu.ICU",                  // needed by ICU_Delegate in LayoutLib
140                         "libcore.io.*",                     // needed to load /usr/share/zoneinfo
141                         "org.apache.harmony.xml.*",
142                     },
143                     info.getExcludedClasses(),
144                     new String[] {
145                         "com/android/i18n/phonenumbers/data/*",
146                         "android/icu/impl/data/**"
147                     }, info.getMethodReplacers());
148             agen.setAnalysisResult(aa.analyze());
149 
150             Map<String, byte[]> outputClasses = agen.generate();
151             JarUtil.createJar(new FileOutputStream(osDestJar), outputClasses);
152             log.info("Created JAR file %s", osDestJar);
153 
154             if (sOptions.createStubLib) {
155                 File osDestJarFile = new File(osDestJar);
156                 String extension = Files.getFileExtension(osDestJarFile.getName());
157                 if (!extension.isEmpty()) {
158                     extension = '.' + extension;
159                 }
160                 String stubDestJarFile = osDestJarFile.getParent() + File.separatorChar +
161                         Files.getNameWithoutExtension(osDestJarFile.getName()) + "-stubs" +
162                         extension;
163 
164                 Map<String, byte[]> toStubClasses = outputClasses.entrySet().stream().filter(entry -> entry.getKey().startsWith("android/")).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
165                 JarUtil.createJar(new FileOutputStream(stubDestJarFile), toStubClasses,
166                         input -> StubClassAdapter.stubClass(log, input));
167                 log.info("Created stub JAR file %s", stubDestJarFile);
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