From c51d184216d8fa0ea0fd6f0f7eb7959afd8faae2 Mon Sep 17 00:00:00 2001 From: Xavier Ducrohet Date: Fri, 25 Sep 2009 15:52:52 -0700 Subject: [PATCH] Update the project creation (from the command line): - Make the distinction between the activity class name, manifest entry, fully-qualified name, and tested activity for the template place-holders. Test activity names now directly contain the full name (including the "Test" prefix) instead of the template adding it. This is required by the new 'create test-project' - New action: create test-project This requires a path to the main project. It reads the package, activity name and target from the main project. The activity is read from the manifest and can be in a more complex form than previously expected (for instance .subpackage.MyClass, instead of simply MyClass). This is what required the re-work the activity related template place holders. Options: -m --main Location path of the project to test, relative to the new project [required] -n --name Project name -p --path Location path of the new project [required] Example: for 2 projects MyProject and MyTests located in the same folder, calling from their parent folder. android create test-project -p MyTests -m ../MyProject - build.properties now only gets application.package for older targets as the new one get it directly from XPath - Remove AndroidXPathFactory from the anttasks project as it was already in sdklib which is a dependency. - Removed IntelliJ templates for the SDK. We haven't supported them for a while, and now that IntelliJ has built-in support for Android, it's not that useful anymore. While there is the command line parameters for 'update test-project' it's not yet implemented. Change-Id: I663d4cb7f439bb2abfe866f893e58f4d13aff975 --- build/sdk.atree | 3 - .../com/android/ant/AndroidXPathFactory.java | 89 ---- .../src/com/android/ant/SetupTask.java | 17 +- .../src/com/android/ant/XPathTask.java | 2 + .../project/AndroidManifestParser.java | 94 ++-- tools/scripts/AndroidManifest.template | 2 +- tools/scripts/AndroidManifest.tests.template | 2 +- tools/scripts/iml.template | 14 - tools/scripts/ipr.template | 232 --------- tools/scripts/iws.template | 470 ------------------ tools/scripts/java_file.template | 2 +- tools/scripts/java_tests_file.template | 8 +- .../sdkmanager/CommandLineProcessor.java | 2 +- .../app/src/com/android/sdkmanager/Main.java | 119 ++++- .../android/sdkmanager/SdkCommandLine.java | 77 ++- .../internal/project/ProjectCreator.java | 152 ++++-- .../internal/project/ProjectProperties.java | 6 +- ...estConstants.java => AndroidManifest.java} | 39 +- .../sdklib/xml/AndroidXPathFactory.java | 18 +- 19 files changed, 387 insertions(+), 961 deletions(-) delete mode 100644 tools/anttasks/src/com/android/ant/AndroidXPathFactory.java delete mode 100644 tools/scripts/iml.template delete mode 100644 tools/scripts/ipr.template delete mode 100644 tools/scripts/iws.template rename tools/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/{ManifestConstants.java => AndroidManifest.java} (60%) diff --git a/build/sdk.atree b/build/sdk.atree index fc157fdd4..20542a26f 100644 --- a/build/sdk.atree +++ b/build/sdk.atree @@ -51,9 +51,6 @@ obj/framework.aidl platforms/${PLATFORM_NAME}/framework.aidl # sdk scripts development/tools/scripts/AndroidManifest.template platforms/${PLATFORM_NAME}/templates/AndroidManifest.template development/tools/scripts/AndroidManifest.tests.template platforms/${PLATFORM_NAME}/templates/AndroidManifest.tests.template -development/tools/scripts/iml.template platforms/${PLATFORM_NAME}/templates/iml.template -development/tools/scripts/ipr.template platforms/${PLATFORM_NAME}/templates/ipr.template -development/tools/scripts/iws.template platforms/${PLATFORM_NAME}/templates/iws.template development/tools/scripts/java_file.template platforms/${PLATFORM_NAME}/templates/java_file.template development/tools/scripts/java_tests_file.template platforms/${PLATFORM_NAME}/templates/java_tests_file.template development/tools/scripts/layout.template platforms/${PLATFORM_NAME}/templates/layout.template diff --git a/tools/anttasks/src/com/android/ant/AndroidXPathFactory.java b/tools/anttasks/src/com/android/ant/AndroidXPathFactory.java deleted file mode 100644 index 45ccf4254..000000000 --- a/tools/anttasks/src/com/android/ant/AndroidXPathFactory.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (C) 2009 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 java.util.Iterator; - -import javax.xml.XMLConstants; -import javax.xml.namespace.NamespaceContext; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathFactory; - -/** - * XPath factory with automatic support for the android namespace. - */ -class AndroidXPathFactory { - public final static String DEFAULT_NS_PREFIX = "android"; //$NON-NLS-1$ - - private final static XPathFactory sFactory = XPathFactory.newInstance(); - - /** Namespace context for Android resource XML files. */ - private static class AndroidNamespaceContext implements NamespaceContext { - private String mAndroidPrefix; - - /** - * Construct the context with the prefix associated with the android namespace. - * @param androidPrefix the Prefix - */ - public AndroidNamespaceContext(String androidPrefix) { - mAndroidPrefix = androidPrefix; - } - - public String getNamespaceURI(String prefix) { - if (prefix != null) { - if (prefix.equals(mAndroidPrefix)) { - return SdkConstants.NS_RESOURCES; - } - } - - return XMLConstants.NULL_NS_URI; - } - - public String getPrefix(String namespaceURI) { - // This isn't necessary for our use. - assert false; - return null; - } - - public Iterator getPrefixes(String namespaceURI) { - // This isn't necessary for our use. - assert false; - return null; - } - } - - /** - * Creates a new XPath object, specifying which prefix in the query is used for the - * android namespace. - * @param androidPrefix The namespace prefix. - */ - public static XPath newXPath(String androidPrefix) { - XPath xpath = sFactory.newXPath(); - xpath.setNamespaceContext(new AndroidNamespaceContext(androidPrefix)); - return xpath; - } - - /** - * Creates a new XPath object using the default prefix for the android namespace. - * @see #DEFAULT_NS_PREFIX - */ - public static XPath newXPath() { - return newXPath(DEFAULT_NS_PREFIX); - } -} diff --git a/tools/anttasks/src/com/android/ant/SetupTask.java b/tools/anttasks/src/com/android/ant/SetupTask.java index 12011083f..f13cccdc7 100644 --- a/tools/anttasks/src/com/android/ant/SetupTask.java +++ b/tools/anttasks/src/com/android/ant/SetupTask.java @@ -23,7 +23,7 @@ import com.android.sdklib.SdkManager; import com.android.sdklib.IAndroidTarget.IOptionalLibrary; import com.android.sdklib.internal.project.ProjectProperties; import com.android.sdklib.xml.AndroidXPathFactory; -import com.android.sdklib.xml.ManifestConstants; +import com.android.sdklib.xml.AndroidManifest; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; @@ -264,10 +264,11 @@ public final class SetupTask extends ImportTask { XPath xPath = AndroidXPathFactory.newXPath(); - String value = xPath.evaluate("/" + ManifestConstants.NODE_MANIFEST +"/" + - ManifestConstants.NODE_USES_SDK + "/@" + - AndroidXPathFactory.DEFAULT_NS_PREFIX + ":" + - ManifestConstants.ATTRIBUTE_MIN_SDK_VERSION, + String value = xPath.evaluate( + "/" + AndroidManifest.NODE_MANIFEST + + "/" + AndroidManifest.NODE_USES_SDK + + "/@" + AndroidXPathFactory.DEFAULT_NS_PREFIX + ":" + + AndroidManifest.ATTRIBUTE_MIN_SDK_VERSION, new InputSource(new FileInputStream(manifest))); if (androidVersion.isPreview()) { @@ -290,19 +291,19 @@ public final class SetupTask extends ImportTask { // looks like it's not a number: error! throw new BuildException(String.format( "Attribute %1$s in AndroidManifest.xml must be an Integer!", - ManifestConstants.ATTRIBUTE_MIN_SDK_VERSION)); + AndroidManifest.ATTRIBUTE_MIN_SDK_VERSION)); } int projectApiLevel = androidVersion.getApiLevel(); if (minSdkValue < projectApiLevel) { System.out.println(String.format( "WARNING: Attribute %1$s in AndroidManifest.xml (%2$d) is lower than the project target API level (%3$d)", - ManifestConstants.ATTRIBUTE_MIN_SDK_VERSION, + AndroidManifest.ATTRIBUTE_MIN_SDK_VERSION, minSdkValue, projectApiLevel)); } else if (minSdkValue > androidVersion.getApiLevel()) { System.out.println(String.format( "WARNING: Attribute %1$s in AndroidManifest.xml (%2$d) is higher than the project target API level (%3$d)", - ManifestConstants.ATTRIBUTE_MIN_SDK_VERSION, + AndroidManifest.ATTRIBUTE_MIN_SDK_VERSION, minSdkValue, projectApiLevel)); } } else { diff --git a/tools/anttasks/src/com/android/ant/XPathTask.java b/tools/anttasks/src/com/android/ant/XPathTask.java index 73cf86a55..b9cfb7108 100644 --- a/tools/anttasks/src/com/android/ant/XPathTask.java +++ b/tools/anttasks/src/com/android/ant/XPathTask.java @@ -16,6 +16,8 @@ package com.android.ant; +import com.android.sdklib.xml.AndroidXPathFactory; + import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.Path; diff --git a/tools/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/project/AndroidManifestParser.java b/tools/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/project/AndroidManifestParser.java index 10e727cb7..f03749ccd 100644 --- a/tools/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/project/AndroidManifestParser.java +++ b/tools/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/project/AndroidManifestParser.java @@ -20,7 +20,7 @@ import com.android.ide.eclipse.adt.AdtPlugin; import com.android.ide.eclipse.adt.AndroidConstants; import com.android.ide.eclipse.adt.internal.project.XmlErrorHandler.XmlErrorListener; import com.android.sdklib.SdkConstants; -import com.android.sdklib.xml.ManifestConstants; +import com.android.sdklib.xml.AndroidManifest; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IMarker; @@ -285,55 +285,55 @@ public class AndroidManifestParser { String value; switch (mValidLevel) { case LEVEL_MANIFEST: - if (ManifestConstants.NODE_MANIFEST.equals(localName)) { + if (AndroidManifest.NODE_MANIFEST.equals(localName)) { // lets get the package name. mPackage = getAttributeValue(attributes, - ManifestConstants.ATTRIBUTE_PACKAGE, + AndroidManifest.ATTRIBUTE_PACKAGE, false /* hasNamespace */); mValidLevel++; } break; case LEVEL_APPLICATION: - if (ManifestConstants.NODE_APPLICATION.equals(localName)) { + if (AndroidManifest.NODE_APPLICATION.equals(localName)) { value = getAttributeValue(attributes, - ManifestConstants.ATTRIBUTE_PROCESS, + AndroidManifest.ATTRIBUTE_PROCESS, true /* hasNamespace */); if (value != null) { addProcessName(value); } value = getAttributeValue(attributes, - ManifestConstants.ATTRIBUTE_DEBUGGABLE, + AndroidManifest.ATTRIBUTE_DEBUGGABLE, true /* hasNamespace*/); if (value != null) { mDebuggable = Boolean.parseBoolean(value); } mValidLevel++; - } else if (ManifestConstants.NODE_USES_SDK.equals(localName)) { + } else if (AndroidManifest.NODE_USES_SDK.equals(localName)) { mApiLevelRequirement = getAttributeValue(attributes, - ManifestConstants.ATTRIBUTE_MIN_SDK_VERSION, + AndroidManifest.ATTRIBUTE_MIN_SDK_VERSION, true /* hasNamespace */); - } else if (ManifestConstants.NODE_INSTRUMENTATION.equals(localName)) { + } else if (AndroidManifest.NODE_INSTRUMENTATION.equals(localName)) { processInstrumentationNode(attributes); } break; case LEVEL_ACTIVITY: - if (ManifestConstants.NODE_ACTIVITY.equals(localName)) { + if (AndroidManifest.NODE_ACTIVITY.equals(localName)) { processActivityNode(attributes); mValidLevel++; - } else if (ManifestConstants.NODE_SERVICE.equals(localName)) { + } else if (AndroidManifest.NODE_SERVICE.equals(localName)) { processNode(attributes, AndroidConstants.CLASS_SERVICE); mValidLevel++; - } else if (ManifestConstants.NODE_RECEIVER.equals(localName)) { + } else if (AndroidManifest.NODE_RECEIVER.equals(localName)) { processNode(attributes, AndroidConstants.CLASS_BROADCASTRECEIVER); mValidLevel++; - } else if (ManifestConstants.NODE_PROVIDER.equals(localName)) { + } else if (AndroidManifest.NODE_PROVIDER.equals(localName)) { processNode(attributes, AndroidConstants.CLASS_CONTENTPROVIDER); mValidLevel++; - } else if (ManifestConstants.NODE_USES_LIBRARY.equals(localName)) { + } else if (AndroidManifest.NODE_USES_LIBRARY.equals(localName)) { value = getAttributeValue(attributes, - ManifestConstants.ATTRIBUTE_NAME, + AndroidManifest.ATTRIBUTE_NAME, true /* hasNamespace */); if (value != null) { mLibraries.add(value); @@ -343,26 +343,26 @@ public class AndroidManifestParser { case LEVEL_INTENT_FILTER: // only process this level if we are in an activity if (mCurrentActivity != null && - ManifestConstants.NODE_INTENT.equals(localName)) { + AndroidManifest.NODE_INTENT.equals(localName)) { mCurrentActivity.resetIntentFilter(); mValidLevel++; } break; case LEVEL_CATEGORY: if (mCurrentActivity != null) { - if (ManifestConstants.NODE_ACTION.equals(localName)) { + if (AndroidManifest.NODE_ACTION.equals(localName)) { // get the name attribute String action = getAttributeValue(attributes, - ManifestConstants.ATTRIBUTE_NAME, + AndroidManifest.ATTRIBUTE_NAME, true /* hasNamespace */); if (action != null) { mCurrentActivity.setHasAction(true); mCurrentActivity.setHasMainAction( ACTION_MAIN.equals(action)); } - } else if (ManifestConstants.NODE_CATEGORY.equals(localName)) { + } else if (AndroidManifest.NODE_CATEGORY.equals(localName)) { String category = getAttributeValue(attributes, - ManifestConstants.ATTRIBUTE_NAME, + AndroidManifest.ATTRIBUTE_NAME, true /* hasNamespace */); if (CATEGORY_LAUNCHER.equals(category)) { mCurrentActivity.setHasLauncherCategory(true); @@ -462,14 +462,14 @@ public class AndroidManifestParser { */ private void processActivityNode(Attributes attributes) { // lets get the activity name, and add it to the list - String activityName = getAttributeValue(attributes, ManifestConstants.ATTRIBUTE_NAME, + String activityName = getAttributeValue(attributes, AndroidManifest.ATTRIBUTE_NAME, true /* hasNamespace */); if (activityName != null) { - activityName = combinePackageAndClassName(mPackage, activityName); + activityName = AndroidManifest.combinePackageAndClassName(mPackage, activityName); // get the exported flag. String exportedStr = getAttributeValue(attributes, - ManifestConstants.ATTRIBUTE_EXPORTED, true); + AndroidManifest.ATTRIBUTE_EXPORTED, true); boolean exported = exportedStr == null || exportedStr.toLowerCase().equals("true"); // $NON-NLS-1$ mCurrentActivity = new Activity(activityName, exported); @@ -485,7 +485,7 @@ public class AndroidManifestParser { mCurrentActivity = null; } - String processName = getAttributeValue(attributes, ManifestConstants.ATTRIBUTE_PROCESS, + String processName = getAttributeValue(attributes, AndroidManifest.ATTRIBUTE_PROCESS, true /* hasNamespace */); if (processName != null) { addProcessName(processName); @@ -500,17 +500,17 @@ public class AndroidManifestParser { */ private void processNode(Attributes attributes, String superClassName) { // lets get the class name, and check it if required. - String serviceName = getAttributeValue(attributes, ManifestConstants.ATTRIBUTE_NAME, + String serviceName = getAttributeValue(attributes, AndroidManifest.ATTRIBUTE_NAME, true /* hasNamespace */); if (serviceName != null) { - serviceName = combinePackageAndClassName(mPackage, serviceName); + serviceName = AndroidManifest.combinePackageAndClassName(mPackage, serviceName); if (mMarkErrors) { checkClass(serviceName, superClassName, false /* testVisibility */); } } - String processName = getAttributeValue(attributes, ManifestConstants.ATTRIBUTE_PROCESS, + String processName = getAttributeValue(attributes, AndroidManifest.ATTRIBUTE_PROCESS, true /* hasNamespace */); if (processName != null) { addProcessName(processName); @@ -525,12 +525,13 @@ public class AndroidManifestParser { private void processInstrumentationNode(Attributes attributes) { // lets get the class name, and check it if required. String instrumentationName = getAttributeValue(attributes, - ManifestConstants.ATTRIBUTE_NAME, + AndroidManifest.ATTRIBUTE_NAME, true /* hasNamespace */); if (instrumentationName != null) { - String instrClassName = combinePackageAndClassName(mPackage, instrumentationName); + String instrClassName = AndroidManifest.combinePackageAndClassName(mPackage, + instrumentationName); String targetPackage = getAttributeValue(attributes, - ManifestConstants.ATTRIBUTE_TARGET_PACKAGE, + AndroidManifest.ATTRIBUTE_TARGET_PACKAGE, true /* hasNamespace */); mInstrumentations.add(new Instrumentation(instrClassName, targetPackage)); if (mMarkErrors) { @@ -953,39 +954,6 @@ public class AndroidManifestParser { return (IFile) r; } - /** - * Combines a java package, with a class value from the manifest to make a fully qualified - * class name - * @param javaPackage the java package from the manifest. - * @param className the class name from the manifest. - * @return the fully qualified class name. - */ - public static String combinePackageAndClassName(String javaPackage, String className) { - if (className == null || className.length() == 0) { - return javaPackage; - } - if (javaPackage == null || javaPackage.length() == 0) { - return className; - } - - // the class name can be a subpackage (starts with a '.' - // char), a simple class name (no dot), or a full java package - boolean startWithDot = (className.charAt(0) == '.'); - boolean hasDot = (className.indexOf('.') != -1); - if (startWithDot || hasDot == false) { - - // add the concatenation of the package and class name - if (startWithDot) { - return javaPackage + className; - } else { - return javaPackage + '.' + className; - } - } else { - // just add the class as it should be a fully qualified java name. - return className; - } - } - /** * Given a fully qualified activity name (e.g. com.foo.test.MyClass) and given a project * package base name (e.g. com.foo), returns the relative activity name that would be used diff --git a/tools/scripts/AndroidManifest.template b/tools/scripts/AndroidManifest.template index 2b06e76ac..9b07072f5 100644 --- a/tools/scripts/AndroidManifest.template +++ b/tools/scripts/AndroidManifest.template @@ -4,7 +4,7 @@ android:versionCode="1" android:versionName="1.0"> - diff --git a/tools/scripts/AndroidManifest.tests.template b/tools/scripts/AndroidManifest.tests.template index 1f7d827fe..c74ff6dbf 100644 --- a/tools/scripts/AndroidManifest.tests.template +++ b/tools/scripts/AndroidManifest.tests.template @@ -17,5 +17,5 @@ --> + android:label="Tests for PACKAGE"/> diff --git a/tools/scripts/iml.template b/tools/scripts/iml.template deleted file mode 100644 index c4fe3a332..000000000 --- a/tools/scripts/iml.template +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - diff --git a/tools/scripts/ipr.template b/tools/scripts/ipr.template deleted file mode 100644 index cb3d65e2c..000000000 --- a/tools/scripts/ipr.template +++ /dev/null @@ -1,232 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tools/scripts/iws.template b/tools/scripts/iws.template deleted file mode 100644 index 67d2053a1..000000000 --- a/tools/scripts/iws.template +++ /dev/null @@ -1,470 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/tools/scripts/java_file.template b/tools/scripts/java_file.template index aeb541f7b..19714a8e3 100644 --- a/tools/scripts/java_file.template +++ b/tools/scripts/java_file.template @@ -3,7 +3,7 @@ package PACKAGE; import android.app.Activity; import android.os.Bundle; -public class ACTIVITY_NAME extends Activity +public class ACTIVITY_CLASS_NAME extends Activity { /** Called when the activity is first created. */ @Override diff --git a/tools/scripts/java_tests_file.template b/tools/scripts/java_tests_file.template index c6fa8736f..08d6f9b5d 100644 --- a/tools/scripts/java_tests_file.template +++ b/tools/scripts/java_tests_file.template @@ -9,13 +9,13 @@ import android.test.ActivityInstrumentationTestCase2; *

* To run this test, you can type: * adb shell am instrument -w \ - * -e class PACKAGE.ACTIVITY_NAMETest \ + * -e class ACTIVITY_FQ_NAME \ * PACKAGE.tests/android.test.InstrumentationTestRunner */ -public class ACTIVITY_NAMETest extends ActivityInstrumentationTestCase2 { +public class ACTIVITY_CLASS_NAME extends ActivityInstrumentationTestCase2 { - public ACTIVITY_NAMETest() { - super("PACKAGE", ACTIVITY_NAME.class); + public ACTIVITY_CLASS_NAME() { + super("PACKAGE", ACTIVITY_TESTED_CLASS_NAME.class); } } \ No newline at end of file diff --git a/tools/sdkmanager/app/src/com/android/sdkmanager/CommandLineProcessor.java b/tools/sdkmanager/app/src/com/android/sdkmanager/CommandLineProcessor.java index 5da564517..2e21f7b94 100644 --- a/tools/sdkmanager/app/src/com/android/sdkmanager/CommandLineProcessor.java +++ b/tools/sdkmanager/app/src/com/android/sdkmanager/CommandLineProcessor.java @@ -460,7 +460,7 @@ class CommandLineProcessor { stdout("\nValid actions are composed of a verb and an optional direct object:"); for (String[] action : mActions) { - stdout("- %1$6s %2$-7s: %3$s", + stdout("- %1$6s %2$-12s: %3$s", action[ACTION_VERB_INDEX], action[ACTION_OBJECT_INDEX], action[ACTION_DESC_INDEX]); diff --git a/tools/sdkmanager/app/src/com/android/sdkmanager/Main.java b/tools/sdkmanager/app/src/com/android/sdkmanager/Main.java index fa2870dcb..c691af726 100644 --- a/tools/sdkmanager/app/src/com/android/sdkmanager/Main.java +++ b/tools/sdkmanager/app/src/com/android/sdkmanager/Main.java @@ -28,17 +28,27 @@ import com.android.sdklib.internal.avd.HardwareProperties; import com.android.sdklib.internal.avd.AvdManager.AvdInfo; import com.android.sdklib.internal.avd.HardwareProperties.HardwareProperty; import com.android.sdklib.internal.project.ProjectCreator; +import com.android.sdklib.internal.project.ProjectProperties; import com.android.sdklib.internal.project.ProjectCreator.OutputLevel; +import com.android.sdklib.internal.project.ProjectProperties.PropertyType; +import com.android.sdklib.xml.AndroidXPathFactory; import com.android.sdkmanager.internal.repository.AboutPage; import com.android.sdkmanager.internal.repository.SettingsPage; import com.android.sdkuilib.repository.UpdaterWindow; +import org.xml.sax.InputSource; + import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathExpressionException; + /** * Main class for the 'android' application. */ @@ -221,6 +231,10 @@ public class Main { SdkCommandLine.OBJECT_PROJECT.equals(directObject)) { createProject(); + } else if (SdkCommandLine.VERB_CREATE.equals(verb) && + SdkCommandLine.OBJECT_TEST_PROJECT.equals(directObject)) { + createTestProject(); + } else if (SdkCommandLine.VERB_UPDATE.equals(verb) && SdkCommandLine.OBJECT_PROJECT.equals(directObject)) { updateProject(); @@ -314,9 +328,112 @@ public class Main { packageName, activityName, target, - false /* isTestProject*/); + null /*pathToMain*/); } + /** + * Creates a new Android test project based on command-line parameters + */ + private void createTestProject() { + + String projectDir = getProjectLocation(mSdkCommandLine.getParamLocationPath()); + + // first check the path of the parent project, and make sure it's valid. + String pathToMainProject = mSdkCommandLine.getParamTestProjectMain(); + + File parentProject = new File(pathToMainProject); + if (parentProject.isAbsolute() == false) { + // if the path is not absolute, we need to resolve it based on the + // destination path of the project + parentProject = new File(projectDir, pathToMainProject); + } + + if (parentProject.isDirectory() == false) { + errorAndExit("Main project's directory does not exist: %1$s", + pathToMainProject); + } + + // now look for a manifest in there + File manifest = new File(parentProject, SdkConstants.FN_ANDROID_MANIFEST_XML); + if (manifest.isFile() == false) { + errorAndExit("No AndroidManifest.xml file found in the main project directory: %1$s", + parentProject.getAbsolutePath()); + } + + // now query the manifest for the package file. + XPath xpath = AndroidXPathFactory.newXPath(); + String packageName, activityName; + + try { + packageName = xpath.evaluate("/manifest/@package", + new InputSource(new FileInputStream(manifest))); + + mSdkLog.printf("Found main project package: %1$s\n", packageName); + + // now get the name of the first activity we find + activityName = xpath.evaluate("/manifest/application/activity[1]/@android:name", + new InputSource(new FileInputStream(manifest))); + // xpath will return empty string when there's no match + if (activityName == null || activityName.length() == 0) { + activityName = null; + } else { + mSdkLog.printf("Found main project activity: %1$s\n", activityName); + } + } catch (FileNotFoundException e) { + // this shouldn't happen as we test it above. + errorAndExit("No AndroidManifest.xml file found in main project."); + return; // this is not strictly needed because errorAndExit will stop the execution, + // but this makes the java compiler happy, wrt to uninitialized variables. + } catch (XPathExpressionException e) { + // looks like the main manifest is not valid. + errorAndExit("Unable to parse main project manifest to get information."); + return; // this is not strictly needed because errorAndExit will stop the execution, + // but this makes the java compiler happy, wrt to uninitialized variables. + } + + // now get the target hash + ProjectProperties p = ProjectProperties.load(parentProject.getAbsolutePath(), + PropertyType.DEFAULT); + String targetHash = p.getProperty(ProjectProperties.PROPERTY_TARGET); + if (targetHash == null) { + errorAndExit("Couldn't find the main project target"); + } + + // and resolve it. + IAndroidTarget target = mSdkManager.getTargetFromHashString(targetHash); + if (target == null) { + errorAndExit( + "Unable to resolve main project target '%1$s'. You may want to install the platform in your SDK.", + targetHash); + } + + mSdkLog.printf("Found main project target: %1$s\n", target.getFullName()); + + ProjectCreator creator = new ProjectCreator(mOsSdkFolder, + mSdkCommandLine.isVerbose() ? OutputLevel.VERBOSE : + mSdkCommandLine.isSilent() ? OutputLevel.SILENT : + OutputLevel.NORMAL, + mSdkLog); + + String projectName = mSdkCommandLine.getParamName(); + + if (projectName != null && + !ProjectCreator.RE_PROJECT_NAME.matcher(projectName).matches()) { + errorAndExit( + "Project name '%1$s' contains invalid characters.\nAllowed characters are: %2$s", + projectName, ProjectCreator.CHARS_PROJECT_NAME); + return; + } + + creator.createProject(projectDir, + projectName, + packageName, + activityName, + target, + pathToMainProject); + } + + /** * Updates an existing Android project based on command-line parameters */ diff --git a/tools/sdkmanager/app/src/com/android/sdkmanager/SdkCommandLine.java b/tools/sdkmanager/app/src/com/android/sdkmanager/SdkCommandLine.java index a23a65c00..35bbd0dee 100644 --- a/tools/sdkmanager/app/src/com/android/sdkmanager/SdkCommandLine.java +++ b/tools/sdkmanager/app/src/com/android/sdkmanager/SdkCommandLine.java @@ -31,28 +31,30 @@ class SdkCommandLine extends CommandLineProcessor { public final static String VERB_DELETE = "delete"; public final static String VERB_UPDATE = "update"; - public static final String OBJECT_AVD = "avd"; - public static final String OBJECT_AVDS = "avds"; - public static final String OBJECT_TARGET = "target"; - public static final String OBJECT_TARGETS = "targets"; - public static final String OBJECT_PROJECT = "project"; - public static final String OBJECT_ADB = "adb"; + public static final String OBJECT_AVD = "avd"; + public static final String OBJECT_AVDS = "avds"; + public static final String OBJECT_TARGET = "target"; + public static final String OBJECT_TARGETS = "targets"; + public static final String OBJECT_PROJECT = "project"; + public static final String OBJECT_TEST_PROJECT = "test-project"; + public static final String OBJECT_ADB = "adb"; - public static final String ARG_ALIAS = "alias"; - public static final String ARG_ACTIVITY = "activity"; + public static final String ARG_ALIAS = "alias"; + public static final String ARG_ACTIVITY = "activity"; - public static final String KEY_ACTIVITY = ARG_ACTIVITY; - public static final String KEY_PACKAGE = "package"; - public static final String KEY_MODE = "mode"; - public static final String KEY_TARGET_ID = OBJECT_TARGET; - public static final String KEY_NAME = "name"; - public static final String KEY_PATH = "path"; - public static final String KEY_FILTER = "filter"; - public static final String KEY_SKIN = "skin"; - public static final String KEY_SDCARD = "sdcard"; - public static final String KEY_FORCE = "force"; - public static final String KEY_RENAME = "rename"; - public static final String KEY_SUBPROJECTS = "subprojects"; + public static final String KEY_ACTIVITY = ARG_ACTIVITY; + public static final String KEY_PACKAGE = "package"; + public static final String KEY_MODE = "mode"; + public static final String KEY_TARGET_ID = OBJECT_TARGET; + public static final String KEY_NAME = "name"; + public static final String KEY_PATH = "path"; + public static final String KEY_FILTER = "filter"; + public static final String KEY_SKIN = "skin"; + public static final String KEY_SDCARD = "sdcard"; + public static final String KEY_FORCE = "force"; + public static final String KEY_RENAME = "rename"; + public static final String KEY_SUBPROJECTS = "subprojects"; + public static final String KEY_MAIN_PROJECT = "main"; /** * Action definitions for SdkManager command line. @@ -89,6 +91,11 @@ class SdkCommandLine extends CommandLineProcessor { { VERB_UPDATE, OBJECT_PROJECT, "Updates an Android Project (must have an AndroidManifest.xml)." }, + { VERB_CREATE, OBJECT_TEST_PROJECT, + "Creates a new Android Test Project." }, + { VERB_UPDATE, OBJECT_TEST_PROJECT, + "Updates an Android Test Project (must have an AndroidManifest.xml)." }, + { VERB_UPDATE, OBJECT_ADB, "Updates adb to support the USB devices declared in the SDK add-ons." }, }; @@ -167,6 +174,18 @@ class SdkCommandLine extends CommandLineProcessor { VERB_CREATE, OBJECT_PROJECT, "n", KEY_NAME, "Project name", null); + // --- create test-project --- + define(Mode.STRING, true, + VERB_CREATE, OBJECT_TEST_PROJECT, + "p", KEY_PATH, + "Location path of new project", null); + define(Mode.STRING, false, + VERB_CREATE, OBJECT_TEST_PROJECT, "n", KEY_NAME, + "Project name", null); + define(Mode.STRING, true, + VERB_CREATE, OBJECT_TEST_PROJECT, "m", KEY_MAIN_PROJECT, + "Location path of the project to test, relative to the new project", null); + // --- update project --- define(Mode.STRING, true, @@ -185,6 +204,17 @@ class SdkCommandLine extends CommandLineProcessor { VERB_UPDATE, OBJECT_PROJECT, "s", KEY_SUBPROJECTS, "Also update any projects in sub-folders, such as test projects.", false); + + // --- update test project --- + + define(Mode.STRING, true, + VERB_UPDATE, OBJECT_TEST_PROJECT, + "p", KEY_PATH, + "Location path of the project", null); + define(Mode.STRING, true, + VERB_UPDATE, OBJECT_TEST_PROJECT, + "m", KEY_MAIN_PROJECT, + "Location path of the project to test", null); } @Override @@ -255,4 +285,11 @@ class SdkCommandLine extends CommandLineProcessor { public boolean getParamSubProject() { return ((Boolean) getValue(null, OBJECT_PROJECT, KEY_SUBPROJECTS)).booleanValue(); } + + // -- some helpers for test-project action flags + + /** Helper to retrieve the --main value. */ + public String getParamTestProjectMain() { + return ((String) getValue(null, OBJECT_TEST_PROJECT, KEY_MAIN_PROJECT)); + } } diff --git a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/project/ProjectCreator.java b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/project/ProjectCreator.java index 3e15e1591..c4ab013f7 100644 --- a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/project/ProjectCreator.java +++ b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/project/ProjectCreator.java @@ -20,6 +20,7 @@ import com.android.sdklib.IAndroidTarget; import com.android.sdklib.ISdkLog; import com.android.sdklib.SdkConstants; import com.android.sdklib.internal.project.ProjectProperties.PropertyType; +import com.android.sdklib.xml.AndroidManifest; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; @@ -44,8 +45,7 @@ import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; /** - * Creates the basic files needed to get an Android project up and running. Also - * allows creation of IntelliJ project files. + * Creates the basic files needed to get an Android project up and running. * * @hide */ @@ -55,13 +55,22 @@ public class ProjectCreator { private final static String PH_JAVA_FOLDER = "PACKAGE_PATH"; /** Package name substitution string used in template files, i.e. "PACKAGE" */ private final static String PH_PACKAGE = "PACKAGE"; - /** Activity name substitution string used in template files, i.e. "ACTIVITY_NAME". */ + /** Activity name substitution string used in template files, i.e. "ACTIVITY_NAME". + * @deprecated This is only used for older templates. For new ones see + * {@link #PH_ACTIVITY_ENTRY_NAME}, and {@link #PH_ACTIVITY_CLASS_NAME}. */ private final static String PH_ACTIVITY_NAME = "ACTIVITY_NAME"; + /** Activity name substitution string used in manifest templates, i.e. "ACTIVITY_ENTRY_NAME".*/ + private final static String PH_ACTIVITY_ENTRY_NAME = "ACTIVITY_ENTRY_NAME"; + /** Activity name substitution string used in class templates, i.e. "ACTIVITY_CLASS_NAME".*/ + private final static String PH_ACTIVITY_CLASS_NAME = "ACTIVITY_CLASS_NAME"; + /** Activity FQ-name substitution string used in class templates, i.e. "ACTIVITY_FQ_NAME".*/ + private final static String PH_ACTIVITY_FQ_NAME = "ACTIVITY_FQ_NAME"; + /** Original Activity class name substitution string used in class templates, i.e. + * "ACTIVITY_TESTED_CLASS_NAME".*/ + private final static String PH_ACTIVITY_TESTED_CLASS_NAME = "ACTIVITY_TESTED_CLASS_NAME"; /** Project name substitution string used in template files, i.e. "PROJECT_NAME". */ private final static String PH_PROJECT_NAME = "PROJECT_NAME"; - private final static String FOLDER_TESTS = "tests"; - /** Pattern for characters accepted in a project name. Since this will be used as a * directory name, we're being a bit conservative on purpose: dot and space cannot be used. */ public static final Pattern RE_PROJECT_NAME = Pattern.compile("[a-zA-Z0-9_]+"); @@ -135,17 +144,16 @@ public class ProjectCreator { * {@link #RE_PROJECT_NAME} regex. * @param packageName the package of the project. The name must match the * {@link #RE_PACKAGE_NAME} regex. - * @param activityName the activity of the project as it will appear in the manifest. Can be + * @param activityEntry the activity of the project as it will appear in the manifest. Can be * null if no activity should be created. The name must match the * {@link #RE_ACTIVITY_NAME} regex. * @param target the project target. - * @param isTestProject whether the project to create is a test project. Caller should - * initially call this will false. The method will call itself back to create - * a test project as needed. + * @param pathToMainProject if non-null the project will be setup to test a main project + * located at the given path. */ public void createProject(String folderPath, String projectName, - String packageName, String activityName, IAndroidTarget target, - boolean isTestProject) { + String packageName, String activityEntry, IAndroidTarget target, + String pathToMainProject) { // create project folder if it does not exist File projectFolder = new File(folderPath); @@ -185,6 +193,8 @@ public class ProjectCreator { } try { + boolean isTestProject = pathToMainProject != null; + // first create the project properties. // location of the SDK goes in localProperty @@ -202,9 +212,16 @@ public class ProjectCreator { // create a build.properties file with just the application package ProjectProperties buildProperties = ProjectProperties.create(folderPath, PropertyType.BUILD); - buildProperties.setProperty(ProjectProperties.PROPERTY_APP_PACKAGE, packageName); - if (isTestProject == true) { - buildProperties.setProperty(ProjectProperties.PROPERTY_TESTED_PROJECT, ".."); + + // only put application.package for older target where the rules file didn't. + // grab it through xpath + if (target.getVersion().getApiLevel() < 4) { + buildProperties.setProperty(ProjectProperties.PROPERTY_APP_PACKAGE, packageName); + } + + if (isTestProject) { + buildProperties.setProperty(ProjectProperties.PROPERTY_TESTED_PROJECT, + pathToMainProject); } buildProperties.save(); @@ -221,19 +238,76 @@ public class ProjectCreator { // put this path in the place-holder map for project files that needs to list // files manually. keywords.put(PH_JAVA_FOLDER, packagePath); - keywords.put(PH_PACKAGE, packageName); - if (activityName != null) { - keywords.put(PH_ACTIVITY_NAME, activityName); + + + // compute some activity related information + String fqActivityName = null, activityPath = null, activityClassName = null; + String originalActivityEntry = activityEntry; + String originalActivityClassName = null; + if (activityEntry != null) { + if (isTestProject) { + // append Test so that it doesn't collide with the main project activity. + activityEntry += "Test"; + + // get the classname from the original activity entry. + int pos = originalActivityEntry.lastIndexOf('.'); + if (pos != -1) { + originalActivityClassName = originalActivityEntry.substring(pos + 1); + } else { + originalActivityClassName = originalActivityEntry; + } + } + + // get the fully qualified name of the activity + fqActivityName = AndroidManifest.combinePackageAndClassName(packageName, + activityEntry); + + // get the activity path (replace the . to /) + activityPath = stripString(fqActivityName.replace(".", File.separator), + File.separatorChar); + + // remove the last segment, so that we only have the path to the activity, but + // not the activity filename itself. + activityPath = activityPath.substring(0, + activityPath.lastIndexOf(File.separatorChar)); + + // finally, get the class name for the activity + activityClassName = fqActivityName.substring(fqActivityName.lastIndexOf('.') + 1); + } + + // at this point we have the following for the activity: + // activityEntry: this is the manifest entry. For instance .MyActivity + // fqActivityName: full-qualified class name: com.foo.MyActivity + // activityClassName: only the classname: MyActivity + // originalActivityClassName: the classname of the activity being tested (if applicable) + + // Add whatever activity info is needed in the place-holder map. + // Older templates only expect ACTIVITY_NAME to be the same (and unmodified for tests). + if (target.getVersion().getApiLevel() < 4) { // legacy + if (originalActivityEntry != null) { + keywords.put(PH_ACTIVITY_NAME, originalActivityEntry); + } + } else { + // newer templates make a difference between the manifest entries, classnames, + // as well as the main and test classes. + if (activityEntry != null) { + keywords.put(PH_ACTIVITY_ENTRY_NAME, activityEntry); + keywords.put(PH_ACTIVITY_CLASS_NAME, activityClassName); + keywords.put(PH_ACTIVITY_FQ_NAME, fqActivityName); + if (originalActivityClassName != null) { + keywords.put(PH_ACTIVITY_TESTED_CLASS_NAME, originalActivityClassName); + } + } } // Take the project name from the command line if there's one if (projectName != null) { keywords.put(PH_PROJECT_NAME, projectName); } else { - if (activityName != null) { - // Use the activity as project name - keywords.put(PH_PROJECT_NAME, activityName); + if (activityClassName != null) { + // Use the activity class name as project name + keywords.put(PH_PROJECT_NAME, activityClassName); } else { // We need a project name. Just pick up the basename of the project // directory. @@ -242,21 +316,21 @@ public class ProjectCreator { } } - // create the source folder and the java package folders. - String srcFolderPath = SdkConstants.FD_SOURCES + File.separator + packagePath; - File sourceFolder = createDirs(projectFolder, srcFolderPath); - String javaTemplate = "java_file.template"; - String activityFileName = activityName + ".java"; - if (isTestProject) { - javaTemplate = "java_tests_file.template"; - activityFileName = activityName + "Test.java"; - } - installTemplate(javaTemplate, new File(sourceFolder, activityFileName), - keywords, target); + // create the source folder for the activity + if (activityClassName != null) { + String srcActivityFolderPath = SdkConstants.FD_SOURCES + File.separator + activityPath; + File sourceFolder = createDirs(projectFolder, srcActivityFolderPath); - // create the generate source folder - srcFolderPath = SdkConstants.FD_GEN_SOURCES + File.separator + packagePath; - sourceFolder = createDirs(projectFolder, srcFolderPath); + String javaTemplate = isTestProject ? "java_tests_file.template" + : "java_file.template"; + String activityFileName = activityClassName + ".java"; + + installTemplate(javaTemplate, new File(sourceFolder, activityFileName), + keywords, target); + } else { + // we should at least create 'src' + createDirs(projectFolder, SdkConstants.FD_SOURCES); + } // create other useful folders File resourceFodler = createDirs(projectFolder, SdkConstants.FD_RESOURCES); @@ -287,16 +361,6 @@ public class ProjectCreator { installTemplate("build.template", new File(projectFolder, SdkConstants.FN_BUILD_XML), keywords); - - // if this is not a test project, then we create one. - if (isTestProject == false) { - // create the test project folder. - createDirs(projectFolder, FOLDER_TESTS); - File testProjectFolder = new File(folderPath, FOLDER_TESTS); - - createProject(testProjectFolder.getAbsolutePath(), projectName, packageName, - activityName, target, true /*isTestProject*/); - } } catch (ProjectCreateException e) { mLog.error(e, null); } catch (IOException e) { diff --git a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/project/ProjectProperties.java b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/project/ProjectProperties.java index 694e2850c..6e29e5a07 100644 --- a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/project/ProjectProperties.java +++ b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/project/ProjectProperties.java @@ -251,8 +251,10 @@ public final class ProjectProperties { writer.write(comment); } String value = entry.getValue(); - value = value.replaceAll("\\\\", "\\\\\\\\"); - writer.write(String.format("%s=%s\n", entry.getKey(), value)); + if (value != null) { + value = value.replaceAll("\\\\", "\\\\\\\\"); + writer.write(String.format("%s=%s\n", entry.getKey(), value)); + } } // close the file to flush diff --git a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/ManifestConstants.java b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/AndroidManifest.java similarity index 60% rename from tools/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/ManifestConstants.java rename to tools/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/AndroidManifest.java index 2e20f02ec..c4fa8bcc5 100644 --- a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/ManifestConstants.java +++ b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/AndroidManifest.java @@ -17,10 +17,10 @@ package com.android.sdklib.xml; /** - * Constants for nodes and attributes of the AndroidManifest.xml file. + * Helper and Constants for the AndroidManifest.xml file. * */ -public final class ManifestConstants { +public final class AndroidManifest { public final static String NODE_MANIFEST = "manifest"; //$NON-NLS-1$ public final static String NODE_APPLICATION = "application"; //$NON-NLS-1$ @@ -42,4 +42,39 @@ public final class ManifestConstants { public final static String ATTRIBUTE_MIN_SDK_VERSION = "minSdkVersion"; //$NON-NLS-$ public final static String ATTRIBUTE_TARGET_PACKAGE = "targetPackage"; //$NON-NLS-1$ public final static String ATTRIBUTE_EXPORTED = "exported"; //$NON-NLS-1$ + + + /** + * Combines a java package, with a class value from the manifest to make a fully qualified + * class name + * @param javaPackage the java package from the manifest. + * @param className the class name from the manifest. + * @return the fully qualified class name. + */ + public static String combinePackageAndClassName(String javaPackage, String className) { + if (className == null || className.length() == 0) { + return javaPackage; + } + if (javaPackage == null || javaPackage.length() == 0) { + return className; + } + + // the class name can be a subpackage (starts with a '.' + // char), a simple class name (no dot), or a full java package + boolean startWithDot = (className.charAt(0) == '.'); + boolean hasDot = (className.indexOf('.') != -1); + if (startWithDot || hasDot == false) { + + // add the concatenation of the package and class name + if (startWithDot) { + return javaPackage + className; + } else { + return javaPackage + '.' + className; + } + } else { + // just add the class as it should be a fully qualified java name. + return className; + } + } + } diff --git a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/AndroidXPathFactory.java b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/AndroidXPathFactory.java index fc34aeb52..641cd8104 100644 --- a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/AndroidXPathFactory.java +++ b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/AndroidXPathFactory.java @@ -18,7 +18,9 @@ package com.android.sdklib.xml; import com.android.sdklib.SdkConstants; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import javax.xml.XMLConstants; import javax.xml.namespace.NamespaceContext; @@ -39,7 +41,8 @@ public class AndroidXPathFactory { private final static AndroidNamespaceContext sThis = new AndroidNamespaceContext( DEFAULT_NS_PREFIX); - private String mAndroidPrefix; + private final String mAndroidPrefix; + private final List mAndroidPrefixes = new ArrayList(); /** * Returns the default {@link AndroidNamespaceContext}. @@ -54,6 +57,7 @@ public class AndroidXPathFactory { */ public AndroidNamespaceContext(String androidPrefix) { mAndroidPrefix = androidPrefix; + mAndroidPrefixes.add(mAndroidPrefix); } public String getNamespaceURI(String prefix) { @@ -67,14 +71,18 @@ public class AndroidXPathFactory { } public String getPrefix(String namespaceURI) { - // This isn't necessary for our use. - assert false; + if (SdkConstants.NS_RESOURCES.equals(namespaceURI)) { + return mAndroidPrefix; + } + return null; } public Iterator getPrefixes(String namespaceURI) { - // This isn't necessary for our use. - assert false; + if (SdkConstants.NS_RESOURCES.equals(namespaceURI)) { + return mAndroidPrefixes.iterator(); + } + return null; } }