• 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.Set;
22 
23 
24 
25 public class Main {
26 
main(String[] args)27     public static void main(String[] args) {
28 
29         Log log = new Log();
30 
31         ArrayList<String> osJarPath = new ArrayList<String>();
32         String[] osDestJar = { null };
33 
34         if (!processArgs(log, args, osJarPath, osDestJar)) {
35             log.error("Usage: layoutlib_create [-v] output.jar input.jar ...");
36             System.exit(1);
37         }
38 
39         log.info("Output: %1$s", osDestJar[0]);
40         for (String path : osJarPath) {
41             log.info("Input :      %1$s", path);
42         }
43 
44         try {
45             AsmGenerator agen = new AsmGenerator(log, osDestJar[0],
46                     CreateInfo.INJECTED_CLASSES,
47                     CreateInfo.OVERRIDDEN_METHODS,
48                     CreateInfo.RENAMED_CLASSES,
49                     CreateInfo.REMOVED_METHODS
50             );
51 
52             AsmAnalyzer aa = new AsmAnalyzer(log, osJarPath, agen,
53                     new String[] { "android.view.View" },   // derived from
54                     new String[] {                          // include classes
55                         "android.*", // for android.R
56                         "android.util.*",
57                         "com.android.internal.util.*",
58                         "android.view.*",
59                         "android.widget.*",
60                         "com.android.internal.widget.*",
61                         "android.text.**",
62                         "android.graphics.*",
63                         "android.graphics.drawable.*",
64                         "android.content.*",
65                         "android.content.res.*",
66                         "org.apache.harmony.xml.*",
67                         "com.android.internal.R**",
68                         "android.pim.*", // for datepicker
69                         "android.os.*",  // for android.os.Handler
70                         });
71             aa.analyze();
72             agen.generate();
73 
74             // Throw an error if any class failed to get renamed by the generator
75             //
76             // IMPORTANT: if you're building the platform and you get this error message,
77             // it means the renameClasses[] array in AsmGenerator needs to be updated: some
78             // class should have been renamed but it was not found in the input JAR files.
79             Set<String> notRenamed = agen.getClassesNotRenamed();
80             if (notRenamed.size() > 0) {
81                 // (80-column guide below for error formatting)
82                 // 01234567890123456789012345678901234567890123456789012345678901234567890123456789
83                 log.error(
84                   "ERROR when running layoutlib_create: the following classes are referenced\n" +
85                   "by tools/layoutlib/create but were not actually found in the input JAR files.\n" +
86                   "This may be due to some platform classes having been renamed.");
87                 for (String fqcn : notRenamed) {
88                     log.error("- Class not found: %s", fqcn.replace('/', '.'));
89                 }
90                 for (String path : osJarPath) {
91                     log.info("- Input JAR : %1$s", path);
92                 }
93                 System.exit(1);
94             }
95 
96             System.exit(0);
97         } catch (IOException e) {
98             log.exception(e, "Failed to load jar");
99         } catch (LogAbortException e) {
100             e.error(log);
101         }
102 
103         System.exit(1);
104     }
105 
106     /**
107      * Returns true if args where properly parsed.
108      * Returns false if program should exit with command-line usage.
109      * <p/>
110      * Note: the String[0] is an output parameter wrapped in an array, since there is no
111      * "out" parameter support.
112      */
processArgs(Log log, String[] args, ArrayList<String> osJarPath, String[] osDestJar)113     private static boolean processArgs(Log log, String[] args,
114             ArrayList<String> osJarPath, String[] osDestJar) {
115         for (int i = 0; i < args.length; i++) {
116             String s = args[i];
117             if (s.equals("-v")) {
118                 log.setVerbose(true);
119             } else if (!s.startsWith("-")) {
120                 if (osDestJar[0] == null) {
121                     osDestJar[0] = s;
122                 } else {
123                     osJarPath.add(s);
124                 }
125             } else {
126                 log.error("Unknow argument: %s", s);
127                 return false;
128             }
129         }
130 
131         if (osJarPath.isEmpty()) {
132             log.error("Missing parameter: path to input jar");
133             return false;
134         }
135         if (osDestJar[0] == null) {
136             log.error("Missing parameter: path to output jar");
137             return false;
138         }
139 
140         return true;
141     }
142 
143 }
144