• 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 java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 
26 
27 /**
28  * Entry point for the layoutlib_create tool.
29  * <p/>
30  * The tool does not currently rely on any external configuration file.
31  * Instead the configuration is mostly done via the {@link CreateInfo} class.
32  * <p/>
33  * For a complete description of the tool and its implementation, please refer to
34  * the "README.txt" file at the root of this project.
35  * <p/>
36  * For a quick test, invoke this as follows:
37  * <pre>
38  * $ make layoutlib
39  * </pre>
40  * which does:
41  * <pre>
42  * $ make layoutlib_create &lt;bunch of framework jars&gt;
43  * $ java -jar out/host/linux-x86/framework/layoutlib_create.jar \
44  *        out/host/common/obj/JAVA_LIBRARIES/temp_layoutlib_intermediates/javalib.jar \
45  *        out/target/common/obj/JAVA_LIBRARIES/core_intermediates/classes.jar \
46  *        out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar
47  * </pre>
48  */
49 public class Main {
50 
51     public static class Options {
52         public boolean generatePublicAccess = true;
53         public boolean listAllDeps = false;
54         public boolean listOnlyMissingDeps = false;
55     }
56 
57     public static final Options sOptions = new Options();
58 
main(String[] args)59     public static void main(String[] args) {
60 
61         Log log = new Log();
62 
63         ArrayList<String> osJarPath = new ArrayList<String>();
64         String[] osDestJar = { null };
65 
66         if (!processArgs(log, args, osJarPath, osDestJar)) {
67             log.error("Usage: layoutlib_create [-v] [-p] output.jar input.jar ...");
68             log.error("Usage: layoutlib_create [-v] [--list-deps|--missing-deps] input.jar ...");
69             System.exit(1);
70         }
71 
72         if (sOptions.listAllDeps || sOptions.listOnlyMissingDeps) {
73             System.exit(listDeps(osJarPath, log));
74 
75         } else {
76             System.exit(createLayoutLib(osDestJar[0], osJarPath, log));
77         }
78 
79 
80         System.exit(1);
81     }
82 
createLayoutLib(String osDestJar, ArrayList<String> osJarPath, Log log)83     private static int createLayoutLib(String osDestJar, ArrayList<String> osJarPath, Log log) {
84         log.info("Output: %1$s", osDestJar);
85         for (String path : osJarPath) {
86             log.info("Input :      %1$s", path);
87         }
88 
89         try {
90             CreateInfo info = new CreateInfo();
91             Set<String> excludeClasses = getExcludedClasses(info);
92             AsmGenerator agen = new AsmGenerator(log, osDestJar, info);
93 
94             AsmAnalyzer aa = new AsmAnalyzer(log, osJarPath, agen,
95                     new String[] {                          // derived from
96                         "android.view.View",
97                         "android.app.Fragment"
98                     },
99                     new String[] {                          // include classes
100                         "android.*", // for android.R
101                         "android.util.*",
102                         "com.android.internal.util.*",
103                         "android.view.*",
104                         "android.widget.*",
105                         "com.android.internal.widget.*",
106                         "android.text.**",
107                         "android.graphics.*",
108                         "android.graphics.drawable.*",
109                         "android.content.*",
110                         "android.content.res.*",
111                         "org.apache.harmony.xml.*",
112                         "com.android.internal.R**",
113                         "android.pim.*", // for datepicker
114                         "android.os.*",  // for android.os.Handler
115                         "android.database.ContentObserver", // for Digital clock
116                         "com.android.i18n.phonenumbers.*",  // for TextView with autolink attribute
117                     },
118                     excludeClasses,
119                     new String[] {
120                         "com/android/i18n/phonenumbers/data/*",
121                     });
122             aa.analyze();
123             agen.generate();
124 
125             // Throw an error if any class failed to get renamed by the generator
126             //
127             // IMPORTANT: if you're building the platform and you get this error message,
128             // it means the renameClasses[] array in AsmGenerator needs to be updated: some
129             // class should have been renamed but it was not found in the input JAR files.
130             Set<String> notRenamed = agen.getClassesNotRenamed();
131             if (notRenamed.size() > 0) {
132                 // (80-column guide below for error formatting)
133                 // 01234567890123456789012345678901234567890123456789012345678901234567890123456789
134                 log.error(
135                   "ERROR when running layoutlib_create: the following classes are referenced\n" +
136                   "by tools/layoutlib/create but were not actually found in the input JAR files.\n" +
137                   "This may be due to some platform classes having been renamed.");
138                 for (String fqcn : notRenamed) {
139                     log.error("- Class not found: %s", fqcn.replace('/', '.'));
140                 }
141                 for (String path : osJarPath) {
142                     log.info("- Input JAR : %1$s", path);
143                 }
144                 return 1;
145             }
146 
147             return 0;
148         } catch (IOException e) {
149             log.exception(e, "Failed to load jar");
150         } catch (LogAbortException e) {
151             e.error(log);
152         }
153 
154         return 1;
155     }
156 
getExcludedClasses(CreateInfo info)157     private static Set<String> getExcludedClasses(CreateInfo info) {
158         String[] refactoredClasses = info.getJavaPkgClasses();
159         Set<String> excludedClasses = new HashSet<String>(refactoredClasses.length);
160         for (int i = 0; i < refactoredClasses.length; i+=2) {
161             excludedClasses.add(refactoredClasses[i]);
162         }
163         return excludedClasses;
164 
165     }
166 
listDeps(ArrayList<String> osJarPath, Log log)167     private static int listDeps(ArrayList<String> osJarPath, Log log) {
168         DependencyFinder df = new DependencyFinder(log);
169         try {
170             List<Map<String, Set<String>>> result = df.findDeps(osJarPath);
171             if (sOptions.listAllDeps) {
172                 df.printAllDeps(result);
173             } else if (sOptions.listOnlyMissingDeps) {
174                 df.printMissingDeps(result);
175             }
176         } catch (IOException e) {
177             log.exception(e, "Failed to load jar");
178         }
179 
180         return 0;
181     }
182 
183     /**
184      * Returns true if args where properly parsed.
185      * Returns false if program should exit with command-line usage.
186      * <p/>
187      * Note: the String[0] is an output parameter wrapped in an array, since there is no
188      * "out" parameter support.
189      */
processArgs(Log log, String[] args, ArrayList<String> osJarPath, String[] osDestJar)190     private static boolean processArgs(Log log, String[] args,
191             ArrayList<String> osJarPath, String[] osDestJar) {
192         boolean needs_dest = true;
193         for (int i = 0; i < args.length; i++) {
194             String s = args[i];
195             if (s.equals("-v")) {
196                 log.setVerbose(true);
197             } else if (s.equals("-p")) {
198                 sOptions.generatePublicAccess = false;
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("Unknow 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         sOptions.generatePublicAccess = false;
227 
228         return true;
229     }
230 }
231