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