1 /* 2 * Copyright (C) 2011 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 com.android.ant; 18 19 import org.apache.tools.ant.BuildException; 20 import org.apache.tools.ant.taskdefs.ExecTask; 21 import org.apache.tools.ant.types.Path; 22 23 public class ZipAlignTask extends SingleInputOutputTask { 24 25 private String mExecutable; 26 private int mAlign = 4; 27 private boolean mVerbose = false; 28 29 /** 30 * Sets the value of the "executable" attribute. 31 * @param executable the value. 32 */ 33 public void setExecutable(Path executable) { 34 mExecutable = TaskHelper.checkSinglePath("executable", executable); 35 } 36 37 public void setAlign(int align) { 38 mAlign = align; 39 } 40 41 public void setVerbose(boolean verbose) { 42 mVerbose = verbose; 43 } 44 45 @Override 46 public void createOutput() throws BuildException { 47 if (mExecutable == null) { 48 throw new BuildException("Missing attribute executable"); 49 } 50 51 System.out.println("Running zip align on final apk..."); 52 doZipAlign(); 53 } 54 55 private void doZipAlign() { 56 ExecTask task = new ExecTask(); 57 task.setExecutable(mExecutable); 58 task.setFailonerror(true); 59 task.setProject(getProject()); 60 task.setOwningTarget(getOwningTarget()); 61 62 task.setTaskName("zip-align"); 63 64 // force overwrite of existing output file 65 task.createArg().setValue("-f"); 66 67 // verbose flag 68 if (mVerbose) { 69 task.createArg().setValue("-v"); 70 } 71 72 // align value 73 task.createArg().setValue(Integer.toString(mAlign)); 74 75 // input 76 task.createArg().setValue(getInput()); 77 78 // output 79 task.createArg().setValue(getOutput()); 80 81 // execute 82 task.execute(); 83 } 84 } 85