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