Main.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
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
25public class Main {
26
27    public static void main(String[] args) {
28
29        Log log = new Log();
30
31        ArrayList<String> osJarPath = new ArrayList<String>();
32        String[] osDestJar = { null };
33
34        if (!processArgs(log, args, osJarPath, osDestJar)) {
35            log.error("Usage: layoutlib_create [-v] output.jar input.jar ...");
36            System.exit(1);
37        }
38
39        log.info("Output: %1$s", osDestJar[0]);
40        for (String path : osJarPath) {
41            log.info("Input :      %1$s", path);
42        }
43
44        try {
45            AsmGenerator agen = new AsmGenerator(log, osDestJar[0],
46                    new Class<?>[] {  // classes to inject in the final JAR
47                        OverrideMethod.class,
48                        MethodListener.class,
49                        MethodAdapter.class
50                    },
51                    new String[] {  // methods to force override
52                        "android.view.View#isInEditMode",
53                        "android.content.res.Resources$Theme#obtainStyledAttributes",
54                    },
55                    new String[] {  // classes to rename (so that we can replace them in layoutlib)
56                        // original-platform-class-name ======> renamed-class-name
57                        "android.graphics.Matrix",              "android.graphics._Original_Matrix",
58                        "android.graphics.Paint",               "android.graphics._Original_Paint",
59                        "android.graphics.Typeface",            "android.graphics._Original_Typeface",
60                        "android.graphics.Bitmap",              "android.graphics._Original_Bitmap",
61                        "android.graphics.Path",                "android.graphics._Original_Path",
62                        "android.graphics.PorterDuffXfermode",  "android.graphics._Original_PorterDuffXfermode",
63                        "android.graphics.Shader",              "android.graphics._Original_Shader",
64                        "android.graphics.LinearGradient",      "android.graphics._Original_LinearGradient",
65                        "android.graphics.BitmapShader",        "android.graphics._Original_BitmapShader",
66                        "android.graphics.ComposeShader",       "android.graphics._Original_ComposeShader",
67                        "android.graphics.RadialGradient",      "android.graphics._Original_RadialGradient",
68                        "android.graphics.SweepGradient",       "android.graphics._Original_SweepGradient",
69                        "android.util.FloatMath",               "android.util._Original_FloatMath",
70                        "android.view.SurfaceView",             "android.view._Original_SurfaceView",
71                    },
72                    new String[] { // methods deleted from their return type.
73                        "android.graphics.Paint", // class to delete method from
74                        "android.graphics.Paint$Align", // list of type identifying methods to delete
75                        "android.graphics.Paint$Style",
76                        "android.graphics.Paint$Join",
77                        "android.graphics.Paint$Cap",
78                        "android.graphics.Paint$FontMetrics",
79                        "android.graphics.Paint$FontMetricsInt",
80                        null }
81            );
82
83            AsmAnalyzer aa = new AsmAnalyzer(log, osJarPath, agen,
84                    new String[] { "android.view.View" },   // derived from
85                    new String[] {                          // include classes
86                        "android.*", // for android.R
87                        "android.util.*",
88                        "com.android.internal.util.*",
89                        "android.view.*",
90                        "android.widget.*",
91                        "com.android.internal.widget.*",
92                        "android.text.**",
93                        "android.graphics.*",
94                        "android.graphics.drawable.*",
95                        "android.content.*",
96                        "android.content.res.*",
97                        "org.apache.harmony.xml.*",
98                        "com.android.internal.R**",
99                        "android.pim.*", // for datepicker
100                        "android.os.*",  // for android.os.Handler
101                        });
102            aa.analyze();
103            agen.generate();
104
105            // Throw an error if any class failed to get renamed by the generator
106            //
107            // IMPORTANT: if you're building the platform and you get this error message,
108            // it means the renameClasses[] array in AsmGenerator needs to be updated: some
109            // class should have been renamed but it was not found in the input JAR files.
110            Set<String> notRenamed = agen.getClassesNotRenamed();
111            if (notRenamed.size() > 0) {
112                // (80-column guide below for error formatting)
113                // 01234567890123456789012345678901234567890123456789012345678901234567890123456789
114                log.error(
115                  "ERROR when running layoutlib_create: the following classes are referenced\n" +
116                  "by tools/layoutlib/create but were not actually found in the input JAR files.\n" +
117                  "This may be due to some platform classes having been renamed.");
118                for (String fqcn : notRenamed) {
119                    log.error("- Class not found: %s", fqcn.replace('/', '.'));
120                }
121                for (String path : osJarPath) {
122                    log.info("- Input JAR : %1$s", path);
123                }
124                System.exit(1);
125            }
126
127            System.exit(0);
128        } catch (IOException e) {
129            log.exception(e, "Failed to load jar");
130        } catch (LogAbortException e) {
131            e.error(log);
132        }
133
134        System.exit(1);
135    }
136
137    /**
138     * Returns true if args where properly parsed.
139     * Returns false if program should exit with command-line usage.
140     * <p/>
141     * Note: the String[0] is an output parameter wrapped in an array, since there is no
142     * "out" parameter support.
143     */
144    private static boolean processArgs(Log log, String[] args,
145            ArrayList<String> osJarPath, String[] osDestJar) {
146        for (int i = 0; i < args.length; i++) {
147            String s = args[i];
148            if (s.equals("-v")) {
149                log.setVerbose(true);
150            } else if (!s.startsWith("-")) {
151                if (osDestJar[0] == null) {
152                    osDestJar[0] = s;
153                } else {
154                    osJarPath.add(s);
155                }
156            } else {
157                log.error("Unknow argument: %s", s);
158                return false;
159            }
160        }
161
162        if (osJarPath.isEmpty()) {
163            log.error("Missing parameter: path to input jar");
164            return false;
165        }
166        if (osDestJar[0] == null) {
167            log.error("Missing parameter: path to output jar");
168            return false;
169        }
170
171        return true;
172    }
173
174}
175