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