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 
17 package util.build;
18 
19 import com.android.jack.Jack;
20 import com.android.jack.Main;
21 import com.android.jack.Options;
22 
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.List;
26 
27 public class JackDexBuildStep extends BuildStep {
28 
29     private final boolean deleteInputFileAfterBuild;
30 
JackDexBuildStep(BuildFile inputFile, BuildFile outputFile, boolean deleteInputFileAfterBuild)31     JackDexBuildStep(BuildFile inputFile, BuildFile outputFile,
32             boolean deleteInputFileAfterBuild) {
33         super(inputFile, outputFile);
34         this.deleteInputFileAfterBuild = deleteInputFileAfterBuild;
35     }
36 
37     @Override
build()38     boolean build() {
39 
40         if (super.build()) {
41             String outputFilePath = outputFile.fileName.getAbsolutePath();
42             if (outputFilePath.endsWith(".dex")) {
43               throw new AssertionError(
44                   "DexBuildStep does not support dex output outside of an archive");
45             }
46 
47             File outDir = outputFile.fileName.getParentFile();
48             if (!outDir.exists() && !outDir.mkdirs()) {
49                 System.err.println("failed to create output dir: "
50                         + outDir.getAbsolutePath());
51                 return false;
52             }
53 
54             List<String> commandLine = new ArrayList<String>(4);
55             commandLine.add("--verbose");
56             commandLine.add("error");
57             commandLine.add("--output-dex-zip");
58             commandLine.add(outputFilePath);
59             commandLine.add("--import");
60             commandLine.add(inputFile.fileName.getAbsolutePath());
61 
62             try {
63                Options options = Main.parseCommandLine(commandLine);
64                Jack.checkAndRun(options);
65                 if (deleteInputFileAfterBuild) {
66                     inputFile.fileName.delete();
67                 }
68                 return true;
69             } catch (Throwable ex) {
70                 System.err.println("exception while dexing "
71                         + inputFile.fileName.getAbsolutePath() + " to "
72                         + outputFile.fileName.getAbsolutePath());
73                 ex.printStackTrace();
74             }
75         }
76         return false;
77     }
78 
79     @Override
hashCode()80     public int hashCode() {
81         return inputFile.hashCode() ^ outputFile.hashCode();
82     }
83 
84     @Override
equals(Object obj)85     public boolean equals(Object obj) {
86         if (super.equals(obj)) {
87             JackDexBuildStep other = (JackDexBuildStep) obj;
88 
89             return inputFile.equals(other.inputFile)
90                     && outputFile.equals(other.outputFile);
91         }
92         return false;
93     }
94 
95 
96 }
97