Main.java revision bc101806249eb883f89c4a770a8c27f9ac315837
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[] { "android.view.View" },   // derived from
70                    new String[] {                          // include classes
71                        "android.*", // for android.R
72                        "android.util.*",
73                        "com.android.internal.util.*",
74                        "android.view.*",
75                        "android.widget.*",
76                        "com.android.internal.widget.*",
77                        "android.text.**",
78                        "android.graphics.*",
79                        "android.graphics.drawable.*",
80                        "android.content.*",
81                        "android.content.res.*",
82                        "org.apache.harmony.xml.*",
83                        "com.android.internal.R**",
84                        "android.pim.*", // for datepicker
85                        "android.os.*",  // for android.os.Handler
86                        });
87            aa.analyze();
88            agen.generate();
89
90            // Throw an error if any class failed to get renamed by the generator
91            //
92            // IMPORTANT: if you're building the platform and you get this error message,
93            // it means the renameClasses[] array in AsmGenerator needs to be updated: some
94            // class should have been renamed but it was not found in the input JAR files.
95            Set<String> notRenamed = agen.getClassesNotRenamed();
96            if (notRenamed.size() > 0) {
97                // (80-column guide below for error formatting)
98                // 01234567890123456789012345678901234567890123456789012345678901234567890123456789
99                log.error(
100                  "ERROR when running layoutlib_create: the following classes are referenced\n" +
101                  "by tools/layoutlib/create but were not actually found in the input JAR files.\n" +
102                  "This may be due to some platform classes having been renamed.");
103                for (String fqcn : notRenamed) {
104                    log.error("- Class not found: %s", fqcn.replace('/', '.'));
105                }
106                for (String path : osJarPath) {
107                    log.info("- Input JAR : %1$s", path);
108                }
109                System.exit(1);
110            }
111
112            System.exit(0);
113        } catch (IOException e) {
114            log.exception(e, "Failed to load jar");
115        } catch (LogAbortException e) {
116            e.error(log);
117        }
118
119        System.exit(1);
120    }
121
122    /**
123     * Returns true if args where properly parsed.
124     * Returns false if program should exit with command-line usage.
125     * <p/>
126     * Note: the String[0] is an output parameter wrapped in an array, since there is no
127     * "out" parameter support.
128     */
129    private static boolean processArgs(Log log, String[] args,
130            ArrayList<String> osJarPath, String[] osDestJar) {
131        for (int i = 0; i < args.length; i++) {
132            String s = args[i];
133            if (s.equals("-v")) {
134                log.setVerbose(true);
135            } else if (!s.startsWith("-")) {
136                if (osDestJar[0] == null) {
137                    osDestJar[0] = s;
138                } else {
139                    osJarPath.add(s);
140                }
141            } else {
142                log.error("Unknow argument: %s", s);
143                return false;
144            }
145        }
146
147        if (osJarPath.isEmpty()) {
148            log.error("Missing parameter: path to input jar");
149            return false;
150        }
151        if (osDestJar[0] == null) {
152            log.error("Missing parameter: path to output jar");
153            return false;
154        }
155
156        return true;
157    }
158
159}
160