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 import com.android.sdklib.internal.project.ProjectProperties; 21 import com.android.xml.AndroidManifest; 22 import com.android.xml.AndroidXPathFactory; 23 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.Project; 26 import org.apache.tools.ant.Task; 27 import org.xml.sax.InputSource; 28 29 import java.io.File; 30 import java.io.FileInputStream; 31 import java.io.FileNotFoundException; 32 33 import javax.xml.xpath.XPath; 34 import javax.xml.xpath.XPathExpressionException; 35 36 /** 37 * Task to query the type of the current project. 38 * 39 * Out params: 40 * 41 * <code>projectTypeOut</code>: String value containing the type of the project. Possible values 42 * are 'app', 'library', 'test', 'test-app' 43 * 44 */ 45 public class GetTypeTask extends Task { 46 47 private String mProjectTypeOut; 48 49 public void setProjectTypeOut(String projectTypeOut) { 50 mProjectTypeOut = projectTypeOut; 51 } 52 53 @Override 54 public void execute() throws BuildException { 55 if (mProjectTypeOut == null) { 56 throw new BuildException("Missing attribute projectTypeOut"); 57 } 58 59 Project antProject = getProject(); 60 61 String libraryProp = antProject.getProperty(ProjectProperties.PROPERTY_LIBRARY); 62 if (libraryProp != null) { 63 if (Boolean.valueOf(libraryProp).booleanValue()) { 64 System.out.println("Project Type: Android Library"); 65 66 antProject.setProperty(mProjectTypeOut, "library"); 67 return; 68 } 69 } 70 71 if (antProject.getProperty(ProjectProperties.PROPERTY_TESTED_PROJECT) != null) { 72 System.out.println("Project Type: Test Application"); 73 74 antProject.setProperty(mProjectTypeOut, "test"); 75 return; 76 } 77 78 // we also need to check if the Manifest doesn't have some instrumentation which 79 // means the app is a self-contained test project. 80 try { 81 File manifest = new File(antProject.getBaseDir(), SdkConstants.FN_ANDROID_MANIFEST_XML); 82 XPath xPath = AndroidXPathFactory.newXPath(); 83 84 // check the present of /manifest/instrumentation/ 85 String value = xPath.evaluate( 86 "/" + AndroidManifest.NODE_MANIFEST + 87 "/" + AndroidManifest.NODE_INSTRUMENTATION + 88 "/@" + AndroidXPathFactory.DEFAULT_NS_PREFIX + 89 ":" + AndroidManifest.ATTRIBUTE_TARGET_PACKAGE, 90 new InputSource(new FileInputStream(manifest))); 91 92 if (value != null && value.length() > 0) { 93 System.out.println("Project Type: Self-Tested Application"); 94 95 antProject.setProperty(mProjectTypeOut, "test-app"); 96 return; 97 } 98 } catch (XPathExpressionException e) { 99 throw new BuildException(e); 100 } catch (FileNotFoundException e) { 101 throw new BuildException(e); 102 } 103 104 // default case 105 System.out.println("Project Type: Application"); 106 107 antProject.setProperty(mProjectTypeOut, "app"); 108 } 109 } 110