Permalink
Browse files

Revert "Revert "Search for the Android SDK using various methods""

This reverts commit 32157e1.
  • Loading branch information...
1 parent 5e5f14b commit 828b5524de2393f92fdd52204c16a75e608db62f Phil Goodwin committed Jan 21, 2011
View
@@ -29,8 +29,9 @@
should be checked in in Version Control Systems. -->
<property file="default.properties"/>
- <exec executable="./find-android.bat" outputproperty="sdk.dir" failifexecutionfails="false"/>
- <exec executable="./find-android.sh" outputproperty="sdk.dir" failifexecutionfails="false"/>
+ <exec executable="./find-android.bat" failifexecutionfails="false"/>
+ <exec executable="./find-android.sh" failifexecutionfails="false"/>
+ <property file="local.properties"/>
<!-- Custom Android task to deal with the project target, and import the proper rules.
This requires ant 1.6.0 or above. -->
View
@@ -1,4 +1,6 @@
@echo off
for %%i in (android.bat) do set x=%%~dp$PATH:i
set x=%x:\tools=%
-@echo %x%
+
+# not yet tested!
+@echo "sdk.dir=%x%" >> local.properties
View
@@ -1,3 +1,4 @@
#!/bin/sh
-which android | sed 's/\/tools\/android$//'
+ANDROID_HOME=`which android | sed 's/\/tools\/android$//'`
+echo "sdk.dir=$ANDROID_HOME" >> local.properties
@@ -164,6 +164,7 @@ protected RobolectricTestRunner(Class<?> testClass, ClassHandler classHandler, R
delegateLoadingOf(RealObject.class.getName());
delegateLoadingOf(ShadowWrangler.class.getName());
delegateLoadingOf(RobolectricConfig.class.getName());
+ delegateLoadingOf(android.R.class.getName());
Class<?> delegateClass = classLoader.bootstrap(this.getClass());
try {
@@ -1,15 +1,21 @@
package com.xtremelabs.robolectric.res;
+import android.R;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
+import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
+import java.io.FileInputStream;
+import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
+import java.util.Properties;
import static com.xtremelabs.robolectric.Robolectric.shadowOf;
@@ -51,6 +57,9 @@ private void init() {
try {
if (resourceDir != null) {
+ String resourcePath = getPathToAndroidResources();
+ File systemResourceDir = new File(resourcePath);
+
DocumentLoader stringResourcesDocumentLoader = new DocumentLoader(stringResourceLoader);
File valuesResourceDir = new File(resourceDir, "values");
stringResourcesDocumentLoader.loadResourceXmlDir(valuesResourceDir);
@@ -87,6 +96,77 @@ public boolean accept(File file) {
isInitialized = true;
}
+ private String getPathToAndroidResources() {
+ String resourcePath;
+ if ((resourcePath = getAndroidResourcePathFromRClass()) != null) {
+ return resourcePath;
+ } else if ((resourcePath = getAndroidResourcePathFromLocalProperties()) != null) {
+ return resourcePath;
+ } else if ((resourcePath = getAndroidResourcePathFromSystemEnvironment()) != null) {
+ return resourcePath;
+ } else if ((resourcePath = getAndroidResourcePathByExecingWhichAndroid()) != null) {
+ return resourcePath;
+ }
+
+// throw new RuntimeException("Unable to find path to android SDK");
+ return null;
+ }
+
+ private String getAndroidResourcePathFromRClass() {
+ String resourcePath = R.class.getResource("/res/layout").toString();
+ if (resourcePath.startsWith("jar:file:") && resourcePath.indexOf("android.jar!")!=-1) {
+ return resourcePath.substring(new String("jar:file:").length(), resourcePath.indexOf("android.jar!")) + "data/res";
+ }
+ return null;
+ }
+
+ private String getAndroidResourcePathFromSystemEnvironment() {
+ // Hand tested
+ String resourcePath = System.getenv().get("ANDROID_HOME");
+ if (resourcePath != null) {
+ return new File(resourcePath, getAndroidResourceSubPath()).toString();
+ }
+ return null;
+ }
+
+ private String getAndroidResourcePathFromLocalProperties() {
+ // Hand tested
+ File rootDir = resourceDir.getParentFile();
+ File localPropertiesFile = new File(rootDir, "local.properties");
+ if (localPropertiesFile.exists()) {
+ Properties localProperties = new Properties();
+ try {
+ localProperties.load(new FileInputStream(localPropertiesFile));
+ String resourcePath = localProperties.getProperty("sdk.dir");
+ if (resourcePath != null) {
+ return new File(resourcePath, getAndroidResourceSubPath()).toString();
+ }
+ } catch (IOException e) {
+ // fine, we'll try something else
+ }
+ }
+ return null;
+ }
+
+ private String getAndroidResourcePathByExecingWhichAndroid() {
+ // Hand tested
+ try {
+ Process process = Runtime.getRuntime().exec(new String[] {"which", "android"});
+ String resourcePath = new BufferedReader(new InputStreamReader(process.getInputStream())).readLine();
+ if (resourcePath != null && resourcePath.endsWith("tools/android")) {
+ return new File(resourcePath.substring(resourcePath.indexOf("tools/android")), getAndroidResourceSubPath()).toString();
+ }
+ } catch (IOException e) {
+ // fine we'll try something else
+ }
+ return null;
+ }
+
+ private String getAndroidResourceSubPath() {
+ // TODO: Use the targetSDKVersion from the Android Manifest instead of a hard-coded "9"
+ return "platforms/android-9/data/res";
+ }
+
boolean isLayoutDirectory(String path) {
return path.contains(File.separator + "layout");
}

0 comments on commit 828b552

Please sign in to comment.