[Xamarin.Android.Build.Tasks] NRE getting android.jar path (#976)

Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=60324

If an `android.jar` file does *not* exist for a target platform we
should raise a normal error. Instead we throw a
`NullReferenceException`.

	…/xamarin-android/bin/Debug/lib/xamarin.android/xbuild/Xamarin/Android/Xamarin.Android.Common.targets: error : Error executing task GetJavaPlatformJar: Value cannot be null.
	Parameter name: path1
	Error executing task GetJavaPlatformJar: System.ArgumentNullException: Value cannot be null.
	Parameter name: path1
	  at System.IO.Path.Combine (System.String path1, System.String path2) [0x00003] in <4656b2b94f914437bce672312dd9e44b>:0 
	  at Xamarin.Android.Tasks.GetJavaPlatformJar.Execute () [0x00188] in <3fa51b5c8b554c02b4c7f549bcabde77>:0 
	  at Microsoft.Build.BuildEngine.TaskEngine.Execute () [0x00000] in <cbad9a49d2cc4e87ad2241615dd4666c>:0 
	  at Microsoft.Build.BuildEngine.BuildTask.Execute () [0x0008d] in <cbad9a49d2cc4e87ad2241615dd4666c>:0 

Oops.

The cause is that
`AndroidSdkInfo.TryGetPlatformDirectoryFromApiLevel()` can return
`null`, which is by design. We need to handle that in the places
where it's called, and provide an XA5207 error message when
`TryGetPlatformDirectoryFromApiLevel()` returns `null`.
This commit is contained in:
Dean Ellis 2017-11-01 14:43:56 +00:00 коммит произвёл Jonathan Pryor
Родитель 386e6cbe1b
Коммит 7f45a6d20b
3 изменённых файлов: 23 добавлений и 18 удалений

Просмотреть файл

@ -69,24 +69,16 @@ namespace Xamarin.Android.Tasks
}
platform = GetTargetSdkVersion (platform, target_sdk);
JavaPlatformJarPath = Path.Combine (MonoAndroidHelper.AndroidSdk.TryGetPlatformDirectoryFromApiLevel (platform, MonoAndroidHelper.SupportedVersions), "android.jar");
if (!File.Exists (JavaPlatformJarPath)) {
Log.LogError ("Could not find android.jar for API Level {0}. " +
"This means the Android SDK platform for API Level {0} is not installed. " +
"Either install it in the Android SDK Manager (Tools > Open Android SDK Manager...), " +
"or change your Xamarin.Android project to target an API version that is installed. " +
"({1} missing.)",
platform, JavaPlatformJarPath);
return false;
}
JavaPlatformJarPath = MonoAndroidHelper.TryGetAndroidJarPath (Log, platform);
if (JavaPlatformJarPath == null)
return !Log.HasLoggedErrors;
TargetSdkVersion = platform;
Log.LogDebugMessage (" [Output] JavaPlatformJarPath: {0}", JavaPlatformJarPath);
Log.LogDebugMessage (" [Output] TargetSdkVersion: {0}", TargetSdkVersion);
return true;
return !Log.HasLoggedErrors;
}
string GetTargetSdkVersion (string target, XAttribute target_sdk)

Просмотреть файл

@ -68,12 +68,9 @@ namespace Xamarin.Android.Tasks
}
// Ensure that the user has the platform they are targeting installed
var jarpath = Path.Combine (MonoAndroidHelper.AndroidSdk.TryGetPlatformDirectoryFromApiLevel (AndroidApiLevel, MonoAndroidHelper.SupportedVersions), "android.jar");
if (!File.Exists (jarpath)) {
Log.LogError ("Could not find android.jar for API Level {0}. This means the Android SDK platform for API Level {0} is not installed. Either install it in the Android SDK Manager, or change your Android Bindings project to target an API version that is installed. ({1} missing.)", AndroidApiLevel, jarpath);
return false;
}
var jarpath = MonoAndroidHelper.TryGetAndroidJarPath (Log, AndroidApiLevel);
if (jarpath == null)
return !Log.HasLoggedErrors;
// Ensure that all requested jars exist
foreach (var jar in SourceJars)

Просмотреть файл

@ -530,5 +530,21 @@ namespace Xamarin.Android.Tasks
foreach (var ext in pathExts)
yield return Path.ChangeExtension (executable, ext);
}
public static string TryGetAndroidJarPath (TaskLoggingHelper log, string platform)
{
var platformPath = MonoAndroidHelper.AndroidSdk.TryGetPlatformDirectoryFromApiLevel (platform, MonoAndroidHelper.SupportedVersions);
if (platformPath == null) {
var expectedPath = MonoAndroidHelper.AndroidSdk.GetPlatformDirectoryFromId (platform);
log.LogError ("XA5207", "Could not find android.jar for API Level {0}. " +
"This means the Android SDK platform for API Level {0} is not installed. " +
"Either install it in the Android SDK Manager (Tools > Open Android SDK Manager...), " +
"or change your Xamarin.Android project to target an API version that is installed. " +
"({1} missing.)",
platform, Path.Combine (expectedPath, "android.jar"));
return null;
}
return Path.Combine (platformPath, "android.jar");
}
}
}