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