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 util.build;
18
19import com.android.dx.command.dexer.Main;
20import java.io.IOException;
21
22public class DxBuildStep extends BuildStep {
23
24    private final boolean deleteInputFileAfterBuild;
25
26    DxBuildStep(BuildFile inputFile, BuildFile outputFile,
27            boolean deleteInputFileAfterBuild) {
28        super(inputFile, outputFile);
29        this.deleteInputFileAfterBuild = deleteInputFileAfterBuild;
30    }
31
32    @Override
33    boolean build() {
34
35        if (super.build()) {
36            Main.Arguments args = new Main.Arguments();
37
38            args.jarOutput = true;
39            args.fileNames = new String[] {inputFile.fileName.getAbsolutePath()};
40
41            args.outName = outputFile.fileName.getAbsolutePath();
42
43            int result = 0;
44            try {
45                result = Main.run(args);
46            } catch (IOException e) {
47                e.printStackTrace();
48                return false;
49            }
50
51            if (result == 0) {
52                if (deleteInputFileAfterBuild) {
53                    inputFile.fileName.delete();
54                }
55                return true;
56            } else {
57                System.err.println("exception while dexing "
58                        + inputFile.fileName.getAbsolutePath() + " to "
59                        + args.outName);
60                return false;
61            }
62        }
63        return false;
64    }
65
66    @Override
67    public int hashCode() {
68        return inputFile.hashCode() ^ outputFile.hashCode();
69    }
70
71    @Override
72    public boolean equals(Object obj) {
73        if (super.equals(obj)) {
74            DxBuildStep other = (DxBuildStep) obj;
75
76            return inputFile.equals(other.inputFile)
77                    && outputFile.equals(other.outputFile);
78        }
79        return false;
80    }
81
82
83}
84