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