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 java.io.File; 20 import java.io.IOException; 21 import java.util.ArrayList; 22 import java.util.List; 23 24 public class JackDexBuildStep extends BuildStep { 25 26 private final boolean deleteInputFileAfterBuild; 27 28 JackDexBuildStep(BuildFile inputFile, BuildFile outputFile, 29 boolean deleteInputFileAfterBuild) { 30 super(inputFile, outputFile); 31 this.deleteInputFileAfterBuild = deleteInputFileAfterBuild; 32 } 33 34 @Override 35 boolean build() { 36 37 if (super.build()) { 38 String outputFilePath = outputFile.fileName.getAbsolutePath(); 39 if (outputFilePath.endsWith(".dex")) { 40 throw new AssertionError( 41 "JackDexBuildStep does not support dex output outside of an archive"); 42 } 43 44 File outDir = outputFile.fileName.getParentFile(); 45 if (!outDir.exists() && !outDir.mkdirs()) { 46 System.err.println("failed to create output dir: " 47 + outDir.getAbsolutePath()); 48 return false; 49 } 50 51 File tmpOutDir = new File(outDir, outputFile.fileName.getName() + ".dexTmp"); 52 if (!tmpOutDir.exists() && !tmpOutDir.mkdirs()) { 53 System.err.println("failed to create temp dir: " 54 + tmpOutDir.getAbsolutePath()); 55 return false; 56 } 57 File tmpDex = new File(tmpOutDir, "classes.dex"); 58 59 try { 60 List<String> commandLine = new ArrayList<String>(4); 61 commandLine.add("--verbose"); 62 commandLine.add("error"); 63 commandLine.add("--output-dex"); 64 commandLine.add(tmpOutDir.getAbsolutePath()); 65 commandLine.add("--import"); 66 commandLine.add(inputFile.fileName.getAbsolutePath()); 67 68 ExecuteFile exec = new ExecuteFile(JackBuildDalvikSuite.JACK, 69 commandLine.toArray(new String[commandLine.size()])); 70 exec.setErr(System.err); 71 exec.setOut(System.out); 72 if (!exec.run()) { 73 return false; 74 } 75 76 JarBuildStep jarStep = new JarBuildStep( 77 new BuildFile(tmpDex), 78 "classes.dex", 79 outputFile, 80 /* deleteInputFileAfterBuild = */ true); 81 if (!jarStep.build()) { 82 throw new IOException("Failed to make jar: " + outputFile.getPath()); 83 } 84 if (deleteInputFileAfterBuild) { 85 inputFile.fileName.delete(); 86 } 87 return true; 88 } catch (Throwable ex) { 89 System.err.println("exception while dexing " 90 + inputFile.fileName.getAbsolutePath() + " to " 91 + outputFile.fileName.getAbsolutePath()); 92 ex.printStackTrace(); 93 } finally { 94 tmpDex.delete(); 95 tmpOutDir.delete(); 96 } 97 } 98 return false; 99 } 100 101 @Override 102 public int hashCode() { 103 return inputFile.hashCode() ^ outputFile.hashCode(); 104 } 105 106 @Override 107 public boolean equals(Object obj) { 108 if (super.equals(obj)) { 109 JackDexBuildStep other = (JackDexBuildStep) obj; 110 111 return inputFile.equals(other.inputFile) 112 && outputFile.equals(other.outputFile); 113 } 114 return false; 115 } 116 117 118 } 119