Skip to content

Commit 61b3235

Browse files
jonathanpeppersjonpryor
authored andcommitted
[tests] fix failures related to AndroidSdkDirectory property (#977)
The introduction of `xabuild` in commit f9d15dd omitted a design requirement: `xabuild` needs to be runnable on a machine which does *does not* have a xamarin-android build environment. The idea is that e.g. Windows developers could download a Jenkins-built `oss-xamarin.android*.zip` file, extract it, and run `bin\Debug\bin\xabuild.exe` to use the extract artifacts *without* requiring system-wide installation via `setup-windows.exe`. This not-very-well-stated requirement brought along two mistakes: 1. `xabuild.exe` should *not* set the `$(AndroidSdkDirectory)` and `$(AndroidNdkDirectory)` MSBuild properties. It *can't*; it could be executed on ~any machine, in particular one which hasn't built xamarin-android, so setting these properties to "build-tree" values doesn't make sense and will fail. 2. Even if (1) weren't the case, commit f9d15dd still allowed the `Xamarin.Android.Build.Tests` unit tests to use `xbuild` as the MSBuild engine, via the `tools/scripts/xabuild` script. This script *does not* always set `$(AndroidSdkDirectory)` or `$(AndroidNdkDirectory)`. As a result of (2), many unit tests *fail*: Error executing task ResolveSdks: System.InvalidOperationException: Could not determine Android SDK location. Please provide `androidSdkPath`. at Xamarin.Android.Tools.AndroidSdkInfo..ctor (System.Action`2[T1,T2] logger, System.String androidSdkPath, System.String androidNdkPath, System.String javaSdkPath) [0x0008e] in …/xamarin-android/external/xamarin-android-tools/src/Xamarin.Android.Tools.AndroidSdk/AndroidSdkInfo.cs:28 at Xamarin.Android.Tasks.MonoAndroidHelper.RefreshAndroidSdk (System.String sdkPath, System.String ndkPath, System.String javaPath) [0x00021] in …/xamarin-android/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs:111 at Xamarin.Android.Tasks.ResolveSdks.RunTask () [0x001a0] in …/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs:157 at Xamarin.Android.Tasks.ResolveSdks.Execute () [0x00002] in …/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs:130 at Microsoft.Build.BuildEngine.TaskEngine.Execute () [0x00000] in …/mcs/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/TaskEngine.cs:134 at Microsoft.Build.BuildEngine.BuildTask.Execute () [0x0008d] in …/mcs/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/BuildTask.cs:101 Update `xabuild` to *never* explicitly set `$(AndroidSdkDirectory)` and `$(AndroidNdkDirectory)`, and update `Xamarin.Android.Build.Tests` to *always* explicitly provide these MSBuild properties. This allows unit test execution to be more reliable, and will prevent `xabuild.exe` from looking at "random" non-existent directories.
1 parent 6687dac commit 61b3235

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/Builder.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ protected bool BuildInternal (string projectOrSolution, string target, string []
185185
buildLogFullPath, Verbosity.ToString ().ToLower ());
186186

187187
var start = DateTime.UtcNow;
188+
var homeDirectory = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
189+
var androidSdkToolPath = Path.Combine (homeDirectory, "android-toolchain");
190+
var sdkPath = Environment.GetEnvironmentVariable ("ANDROID_SDK_PATH");
191+
if (String.IsNullOrEmpty (sdkPath))
192+
sdkPath = GetPathFromRegistry ("AndroidSdkDirectory");
193+
if (String.IsNullOrEmpty (sdkPath))
194+
sdkPath = Path.GetFullPath (Path.Combine (androidSdkToolPath, "sdk"));
195+
var ndkPath = Environment.GetEnvironmentVariable ("ANDROID_NDK_PATH");
196+
if (String.IsNullOrEmpty (ndkPath))
197+
ndkPath = GetPathFromRegistry ("AndroidNdkDirectory");
198+
if (String.IsNullOrEmpty (ndkPath))
199+
ndkPath = Path.GetFullPath (Path.Combine (androidSdkToolPath, "ndk"));
200+
188201
var args = new StringBuilder ();
189202
var psi = new ProcessStartInfo (XABuildExe);
190203
args.AppendFormat ("{0} /t:{1} {2}",
@@ -193,6 +206,12 @@ protected bool BuildInternal (string projectOrSolution, string target, string []
193206
args.Append (" /p:BuildingOutOfProcess=true");
194207
else
195208
args.Append (" /p:UseHostCompilerIfAvailable=false /p:BuildingInsideVisualStudio=true");
209+
if (Directory.Exists (sdkPath)) {
210+
args.AppendFormat (" /p:AndroidSdkDirectory=\"{0}\" ", sdkPath);
211+
}
212+
if (Directory.Exists (ndkPath)) {
213+
args.AppendFormat (" /p:AndroidNdkDirectory=\"{0}\" ", ndkPath);
214+
}
196215
if (parameters != null) {
197216
foreach (var param in parameters) {
198217
args.AppendFormat (" /p:{0}", param);

tools/xabuild/XABuild.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ static XmlDocument CreateConfig (XABuildPaths paths)
6464
SetProperty (toolsets, "MSBuildExtensionsPath", paths.MSBuildExtensionsPath);
6565
SetProperty (toolsets, "MSBuildExtensionsPath32", paths.MSBuildExtensionsPath);
6666
SetProperty (toolsets, "RoslynTargetsPath", Path.Combine (paths.MSBuildBin, "Roslyn"));
67-
SetProperty (toolsets, "AndroidSdkDirectory", paths.AndroidSdkDirectory);
68-
SetProperty (toolsets, "AndroidNdkDirectory", paths.AndroidNdkDirectory);
6967
SetProperty (toolsets, "MonoAndroidToolsDirectory", paths.MonoAndroidToolsDirectory);
7068
SetProperty (toolsets, "TargetFrameworkRootPath", paths.FrameworksDirectory + Path.DirectorySeparatorChar); //NOTE: Must include trailing \
7169

tools/xabuild/XABuildPaths.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ class XABuildPaths
8383

8484
public string MonoAndroidToolsDirectory { get; private set; }
8585

86-
public string AndroidSdkDirectory { get; private set; }
87-
88-
public string AndroidNdkDirectory { get; private set; }
89-
9086
public XABuildPaths ()
9187
{
9288
IsWindows = Environment.OSVersion.Platform == PlatformID.Win32NT;
@@ -96,7 +92,6 @@ public XABuildPaths ()
9692
XamarinAndroidBuildOutput = Path.GetFullPath (Path.Combine (XABuildDirectory, ".."));
9793

9894
string programFiles = Environment.GetFolderPath (Environment.SpecialFolder.ProgramFilesX86);
99-
string userProfile = Environment.GetFolderPath (Environment.SpecialFolder.UserProfile);
10095
string prefix = Path.Combine (XamarinAndroidBuildOutput, "lib", "xamarin.android");
10196

10297
if (IsWindows) {
@@ -130,8 +125,6 @@ public XABuildPaths ()
130125
FrameworksDirectory = Path.Combine (prefix, "xbuild-frameworks");
131126
MSBuildExtensionsPath = Path.Combine (prefix, "xbuild");
132127
MonoAndroidToolsDirectory = Path.Combine (prefix, "xbuild", "Xamarin", "Android");
133-
AndroidSdkDirectory = Path.Combine (userProfile, "android-toolchain", "sdk");
134-
AndroidNdkDirectory = Path.Combine (userProfile, "android-toolchain", "ndk");
135128
MSBuildExeTempPath = Path.GetTempFileName ();
136129
XABuildConfig = MSBuildExeTempPath + ".config";
137130
}

0 commit comments

Comments
 (0)