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