aboutsummaryrefslogtreecommitdiffstats
path: root/anttasks/src/com/android/ant/GetTypeTask.java
diff options
context:
space:
mode:
authorXavier Ducrohet <xav@android.com>2012-03-29 19:28:25 -0700
committerXavier Ducrohet <xav@android.com>2012-04-16 12:54:55 -0700
commit1daa8f999d87443d14f698ca8ccc103e3309fa3e (patch)
tree29f034c67acbecc4933e35a46484edf1f9c9d055 /anttasks/src/com/android/ant/GetTypeTask.java
parent4a8a17e5f23eace570b54084318b30bdd593fbc0 (diff)
downloadsdk-1daa8f999d87443d14f698ca8ccc103e3309fa3e.zip
sdk-1daa8f999d87443d14f698ca8ccc103e3309fa3e.tar.gz
sdk-1daa8f999d87443d14f698ca8ccc103e3309fa3e.tar.bz2
Fix "ant test" + misc clean up / reorganization of build.xml
- Split NewSetupTask in several tasks to make things more flexible. Particularly this allows more targets to get access to the project type (app, lib, test, ...) as it's not so computive intensive. - Fix test project to give them access to the full tested project's classpath. - Fix support for projects that test themselves. - Make sure library projects are instrumented when using the emma target. Change-Id: Ia0c9564eacee2521e7cbd5154b8a85ea287ad4fd
Diffstat (limited to 'anttasks/src/com/android/ant/GetTypeTask.java')
-rw-r--r--anttasks/src/com/android/ant/GetTypeTask.java109
1 files changed, 109 insertions, 0 deletions
diff --git a/anttasks/src/com/android/ant/GetTypeTask.java b/anttasks/src/com/android/ant/GetTypeTask.java
new file mode 100644
index 0000000..b11e9f2
--- /dev/null
+++ b/anttasks/src/com/android/ant/GetTypeTask.java
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.ant;
+
+import com.android.sdklib.SdkConstants;
+import com.android.sdklib.internal.project.ProjectProperties;
+import com.android.sdklib.xml.AndroidManifest;
+import com.android.sdklib.xml.AndroidXPathFactory;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.Task;
+import org.xml.sax.InputSource;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathExpressionException;
+
+/**
+ * Task to query the type of the current project.
+ *
+ * Out params:
+ *
+ * <code>projectTypeOut</code>: String value containing the type of the project. Possible values
+ * are 'app', 'library', 'test', 'test-app'
+ *
+ */
+public class GetTypeTask extends Task {
+
+ private String mProjectTypeOut;
+
+ public void setProjectTypeOut(String projectTypeOut) {
+ mProjectTypeOut = projectTypeOut;
+ }
+
+ @Override
+ public void execute() throws BuildException {
+ if (mProjectTypeOut == null) {
+ throw new BuildException("Missing attribute projectTypeOut");
+ }
+
+ Project antProject = getProject();
+
+ String libraryProp = antProject.getProperty(ProjectProperties.PROPERTY_LIBRARY);
+ if (libraryProp != null) {
+ if (Boolean.valueOf(libraryProp).booleanValue()) {
+ System.out.println("Project Type: Android Library");
+
+ antProject.setProperty(mProjectTypeOut, "library");
+ return;
+ }
+ }
+
+ if (antProject.getProperty(ProjectProperties.PROPERTY_TESTED_PROJECT) != null) {
+ System.out.println("Project Type: Test Application");
+
+ antProject.setProperty(mProjectTypeOut, "test");
+ return;
+ }
+
+ // we also need to check if the Manifest doesn't have some instrumentation which
+ // means the app is a self-contained test project.
+ try {
+ File manifest = new File(antProject.getBaseDir(), SdkConstants.FN_ANDROID_MANIFEST_XML);
+ XPath xPath = AndroidXPathFactory.newXPath();
+
+ // check the present of /manifest/instrumentation/
+ String value = xPath.evaluate(
+ "/" + AndroidManifest.NODE_MANIFEST +
+ "/" + AndroidManifest.NODE_INSTRUMENTATION +
+ "/@" + AndroidXPathFactory.DEFAULT_NS_PREFIX +
+ ":" + AndroidManifest.ATTRIBUTE_TARGET_PACKAGE,
+ new InputSource(new FileInputStream(manifest)));
+
+ if (value != null && value.length() > 0) {
+ System.out.println("Project Type: Self-Tested Application");
+
+ antProject.setProperty(mProjectTypeOut, "test-app");
+ return;
+ }
+ } catch (XPathExpressionException e) {
+ throw new BuildException(e);
+ } catch (FileNotFoundException e) {
+ throw new BuildException(e);
+ }
+
+ // default case
+ System.out.println("Project Type: Application");
+
+ antProject.setProperty(mProjectTypeOut, "app");
+ }
+}