OSDN Git Service

detect too recent version of the ant rules and display an error.
authorXavier Ducrohet <xav@android.com>
Thu, 25 Feb 2010 00:22:07 +0000 (16:22 -0800)
committerXavier Ducrohet <xav@android.com>
Thu, 25 Feb 2010 02:10:21 +0000 (18:10 -0800)
This is to solve the case where a user somehow update a platform
component but not the tools. the custom Ant tasks will detect
the revision of the ant rules and fail if it's too recent.

Change-Id: Ica91a500ca980f9ed5f3a7c4fb09b85a63b8037b

anttasks/src/com/android/ant/SetupTask.java

index 2b371a6..8facf62 100644 (file)
@@ -39,8 +39,10 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FilenameFilter;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.Properties;
 
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathExpressionException;
@@ -62,6 +64,9 @@ import javax.xml.xpath.XPathExpressionException;
  *
  */
 public final class SetupTask extends ImportTask {
+    /** current max version of the Ant rules that is supported */
+    private final static int ANT_RULES_MAX_VERSION = 2;
+
     private final static String ANDROID_RULES = "android_rules.xml";
     // additional android rules for test project - depends on android_rules.xml
     private final static String ANDROID_TEST_RULES = "android_test_rules.xml";
@@ -109,6 +114,12 @@ public final class SetupTask extends ImportTask {
             throw new BuildException(String.format("SDK Location '%s' is not valid.", sdkLocation));
         }
 
+        // display SDK Tools revision
+        int toolsRevison = getToolsRevision(sdk);
+        if (toolsRevison != -1) {
+            System.out.println("Android SDK Tools Revision " + toolsRevison);
+        }
+
         // get the target property value
         String targetHashString = antProject.getProperty(ProjectProperties.PROPERTY_TARGET);
 
@@ -159,6 +170,14 @@ public final class SetupTask extends ImportTask {
                     "Unable to resolve target '%s'", targetHashString));
         }
 
+        // check that this version of the custom Ant task can build this target
+        int antBuildVersion = androidTarget.getAntBuildRevision();
+        if (antBuildVersion > ANT_RULES_MAX_VERSION) {
+            throw new BuildException(String.format(
+                    "The project target (%1$s) requires a more recent version of the tools. Please update.",
+                    androidTarget.getName()));
+        }
+
         // check if the project is a library
         boolean isLibrary = false;
 
@@ -170,7 +189,7 @@ public final class SetupTask extends ImportTask {
         // look for referenced libraries.
         processReferencedLibraries(antProject, androidTarget);
 
-        // display it
+        // display the project info
         System.out.println("Project Target: " + androidTarget.getName());
         if (isLibrary) {
             System.out.println("Type: Android Library");
@@ -443,4 +462,29 @@ public final class SetupTask extends ImportTask {
             antProject.setProperty("android.libraries.package", sb.toString());
         }
     }
+
+    /**
+     * Returns the revision of the tools for a given SDK.
+     * @param sdkFile the {@link File} for the root folder of the SDK
+     * @return the tools revision or -1 if not found.
+     */
+    private int getToolsRevision(File sdkFile) {
+        Properties p = new Properties();
+        try{
+            // tools folder must exist, or this custom task wouldn't run!
+            File toolsFolder= new File(sdkFile, SdkConstants.FD_TOOLS);
+            File sourceProp = new File(toolsFolder, SdkConstants.FN_SOURCE_PROP);
+            p.load(new FileInputStream(sourceProp));
+            String value = p.getProperty("Pkg.Revision"); //$NON-NLS-1$
+            if (value != null) {
+                return Integer.parseInt(value);
+            }
+        } catch (FileNotFoundException e) {
+            // couldn't find the file? return -1 below.
+        } catch (IOException e) {
+            // couldn't find the file? return -1 below.
+        }
+
+        return -1;
+    }
 }