Home | History | Annotate | Download | only in ant
      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.Task;
     21 import org.apache.tools.ant.taskdefs.ExecTask;
     22 import org.apache.tools.ant.types.Path;
     23 
     24 import java.io.File;
     25 
     26 public class ZipAlignTask extends Task {
     27 
     28     private String mExecutable;
     29     private String mInput;
     30     private String mOutput;
     31     private int mAlign = 4;
     32     private boolean mVerbose = false;
     33 
     34     /**
     35      * Sets the value of the "executable" attribute.
     36      * @param executable the value.
     37      */
     38     public void setExecutable(Path executable) {
     39         mExecutable = TaskHelper.checkSinglePath("executable", executable);
     40     }
     41 
     42     public void setInput(Path inputPath) {
     43         mInput = TaskHelper.checkSinglePath("input", inputPath);
     44     }
     45 
     46     public void setOutput(Path outputPath) {
     47         mOutput = TaskHelper.checkSinglePath("output", outputPath);
     48     }
     49 
     50     public void setAlign(int align) {
     51         mAlign = align;
     52     }
     53 
     54     public void setVerbose(boolean verbose) {
     55         mVerbose = verbose;
     56     }
     57 
     58     @Override
     59     public void execute() throws BuildException {
     60         if (mExecutable == null) {
     61             throw new BuildException("Missing attribute executable");
     62         }
     63         if (mInput == null) {
     64             throw new BuildException("Missing attribute input");
     65         }
     66         if (mOutput == null) {
     67             throw new BuildException("Missing attribute output");
     68         }
     69 
     70         // check if there's a need for the task to run.
     71         File outputFile = new File(mOutput);
     72         if (outputFile.isFile()) {
     73             File inputFile = new File(mInput);
     74             if (outputFile.lastModified() >= inputFile.lastModified()) {
     75                 System.out.println("No changes. No need to run zip-align on the apk.");
     76                 return;
     77             }
     78         }
     79 
     80         System.out.println("Running zip align on final apk...");
     81         doZipAlign();
     82     }
     83 
     84     private void doZipAlign() {
     85         ExecTask task = new ExecTask();
     86         task.setExecutable(mExecutable);
     87         task.setFailonerror(true);
     88         task.setProject(getProject());
     89         task.setOwningTarget(getOwningTarget());
     90 
     91         task.setTaskName("zip-align");
     92 
     93         // force overwrite of existing output file
     94         task.createArg().setValue("-f");
     95 
     96         // verbose flag
     97         if (mVerbose) {
     98             task.createArg().setValue("-v");
     99         }
    100 
    101         // align value
    102         task.createArg().setValue(Integer.toString(mAlign));
    103 
    104         // input
    105         task.createArg().setValue(mInput);
    106 
    107         // output
    108         task.createArg().setValue(mOutput);
    109 
    110         // execute
    111         task.execute();
    112     }
    113 }
    114