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