FileDocCategorySizeDatePackage
SetupTask.javaAPI DocAndroid 1.5 API8558Wed May 06 22:41:08 BST 2009com.android.ant

SetupTask

public final class SetupTask extends org.apache.tools.ant.taskdefs.ImportTask
Setup/Import Ant task. This task accomplishes:
  • Gets the project target hash string from {@link ProjectProperties#PROPERTY_TARGET}, and resolves it to get the project's {@link IAndroidTarget}.
  • Sets up properties so that aapt can find the android.jar in the resolved target.
  • Sets up the boot classpath ref so that the javac task knows where to find the libraries. This includes the default android.jar from the resolved target but also optional libraries provided by the target (if any, when the target is an add-on).
  • Imports the build rules located in the resolved target so that the build actually does something. This can be disabled with the attribute import set to false
This is used in build.xml/template.

Fields Summary
private static final String
ANDROID_RULES
private static final String
PROPERTY_ANDROID_JAR
private static final String
PROPERTY_ANDROID_AIDL
private static final String
PROPERTY_AAPT
private static final String
PROPERTY_AIDL
private static final String
PROPERTY_DX
private static final String
REF_CLASSPATH
private boolean
mDoImport
Constructors Summary
Methods Summary
public voidexecute()


    
         
        Project antProject = getProject();
        
        // get the SDK location
        String sdkLocation = antProject.getProperty(ProjectProperties.PROPERTY_SDK);
        
        // check if it's valid and exists
        if (sdkLocation == null || sdkLocation.length() == 0) {
            throw new BuildException("SDK Location is not set.");
        }
        
        File sdk = new File(sdkLocation);
        if (sdk.isDirectory() == false) {
            throw new BuildException(String.format("SDK Location '%s' is not valid.", sdkLocation));
        }

        // get the target property value
        String targetHashString = antProject.getProperty(ProjectProperties.PROPERTY_TARGET);
        if (targetHashString == null) {
            throw new BuildException("Android Target is not set.");
        }

        // load up the sdk targets.
        final ArrayList<String> messages = new ArrayList<String>();
        SdkManager manager = SdkManager.createManager(sdkLocation, new ISdkLog() {
            public void error(Throwable t, String errorFormat, Object... args) {
                if (errorFormat != null) {
                    messages.add(String.format("Error: " + errorFormat, args));
                }
                if (t != null) {
                    messages.add("Error: " + t.getMessage());
                }
            }

            public void printf(String msgFormat, Object... args) {
                messages.add(String.format(msgFormat, args));
            }

            public void warning(String warningFormat, Object... args) {
                messages.add(String.format("Warning: " + warningFormat, args));
            }
        });

        if (manager == null) {
            // since we failed to parse the SDK, lets display the parsing output.
            for (String msg : messages) {
                System.out.println(msg);
            }
            throw new BuildException("Failed to parse SDK content.");
        }

        // resolve it
        IAndroidTarget androidTarget = manager.getTargetFromHashString(targetHashString);
        
        if (androidTarget == null) {
            throw new BuildException(String.format(
                    "Unable to resolve target '%s'", targetHashString));
        }
        
        // display it
        System.out.println("Project Target: " + androidTarget.getName());
        if (androidTarget.isPlatform() == false) {
            System.out.println("Vendor: " + androidTarget.getVendor());
            System.out.println("Platform Version: " + androidTarget.getApiVersionName());
        }
        System.out.println("API level: " + androidTarget.getApiVersionNumber());
        
        // sets up the properties to find android.jar/framework.aidl/target tools
        String androidJar = androidTarget.getPath(IAndroidTarget.ANDROID_JAR);
        antProject.setProperty(PROPERTY_ANDROID_JAR, androidJar);

        antProject.setProperty(PROPERTY_ANDROID_AIDL,
                androidTarget.getPath(IAndroidTarget.ANDROID_AIDL));
        antProject.setProperty(PROPERTY_AAPT, androidTarget.getPath(IAndroidTarget.AAPT));
        antProject.setProperty(PROPERTY_AIDL, androidTarget.getPath(IAndroidTarget.AIDL));
        antProject.setProperty(PROPERTY_DX, androidTarget.getPath(IAndroidTarget.DX));

        // sets up the boot classpath

        // create the Path object
        Path bootclasspath = new Path(antProject);

        // create a PathElement for the framework jar
        PathElement element = bootclasspath.createPathElement();
        element.setPath(androidJar);
        
        // create PathElement for each optional library.
        IOptionalLibrary[] libraries = androidTarget.getOptionalLibraries();
        if (libraries != null) {
            HashSet<String> visitedJars = new HashSet<String>();
            for (IOptionalLibrary library : libraries) {
                String jarPath = library.getJarPath();
                if (visitedJars.contains(jarPath) == false) {
                    visitedJars.add(jarPath);

                    element = bootclasspath.createPathElement();
                    element.setPath(library.getJarPath());
                }
            }
        }
        
        // finally sets the path in the project with a reference
        antProject.addReference(REF_CLASSPATH, bootclasspath);

        // find the file to import, and import it.
        String templateFolder = androidTarget.getPath(IAndroidTarget.TEMPLATES);
        
        // Now the import section. This is only executed if the task actually has to import a file.
        if (mDoImport) {
            // make sure the file exists.
            File templates = new File(templateFolder);
            if (templates.isDirectory() == false) {
                throw new BuildException(String.format("Template directory '%s' is missing.",
                        templateFolder));
            }
            
            // now check the rules file exists.
            File rules = new File(templateFolder, ANDROID_RULES);
            if (rules.isFile() == false) {
                throw new BuildException(String.format("Build rules file '%s' is missing.",
                        templateFolder));
           }
            
            // set the file location to import
            setFile(rules.getAbsolutePath());
            
            // and import
            super.execute();
        }
    
public voidsetImport(boolean value)
Sets the value of the "import" attribute.

param
value the value.

        mDoImport = value;