• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 com.android.SdkConstants;
20 
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Project;
23 import org.apache.tools.ant.Task;
24 import org.apache.tools.ant.util.DeweyDecimal;
25 
26 import java.io.File;
27 
28 /**
29  * Checks the Ant environment to make sure Android builds
30  * can run.
31  *
32  * No parameters are neeed.
33  *
34  */
35 public class CheckEnvTask extends Task {
36 
37     private final static String ANT_MIN_VERSION = "1.8.0";
38 
39     @Override
execute()40     public void execute() {
41 
42         Project antProject = getProject();
43 
44         // check the Ant version
45         DeweyDecimal version = getAntVersion(antProject);
46         DeweyDecimal atLeast = new DeweyDecimal(ANT_MIN_VERSION);
47         if (atLeast.isGreaterThan(version)) {
48             throw new BuildException(
49                     "The Android Ant-based build system requires Ant " +
50                     ANT_MIN_VERSION +
51                     " or later. Current version is " +
52                     version);
53         }
54 
55         // get the SDK location
56         File sdkDir = TaskHelper.getSdkLocation(antProject);
57 
58         // detect that the platform tools is there.
59         File platformTools = new File(sdkDir, SdkConstants.FD_PLATFORM_TOOLS);
60         if (platformTools.isDirectory() == false) {
61             throw new BuildException(String.format(
62                     "SDK Platform Tools component is missing. " +
63                     "Please install it with the SDK Manager (%1$s%2$c%3$s)",
64                     SdkConstants.FD_TOOLS,
65                     File.separatorChar,
66                     SdkConstants.androidCmdName()));
67         }
68 
69         // display SDK Tools revision
70         DeweyDecimal toolsRevison = TaskHelper.getToolsRevision(sdkDir);
71         if (toolsRevison != null) {
72             System.out.println("Android SDK Tools Revision " + toolsRevison);
73             System.out.println("Installed at " + sdkDir.getAbsolutePath());
74         }
75     }
76 
77     /**
78      * Returns the Ant version as a {@link DeweyDecimal} object.
79      *
80      * This is based on the implementation of
81      * org.apache.tools.ant.taskdefs.condition.AntVersion.getVersion()
82      *
83      * @param antProject the current ant project.
84      * @return the ant version.
85      */
getAntVersion(Project antProject)86     private DeweyDecimal getAntVersion(Project antProject) {
87         char[] versionString = antProject.getProperty("ant.version").toCharArray();
88         StringBuilder sb = new StringBuilder();
89         boolean foundFirstDigit = false;
90         for (int i = 0; i < versionString.length; i++) {
91             if (Character.isDigit(versionString[i])) {
92                 sb.append(versionString[i]);
93                 foundFirstDigit = true;
94             }
95             if (versionString[i] == '.' && foundFirstDigit) {
96                 sb.append(versionString[i]);
97             }
98             if (Character.isLetter(versionString[i]) && foundFirstDigit) {
99                 break;
100             }
101         }
102         return new DeweyDecimal(sb.toString());
103     }
104 
105 }
106