Skip to content

Commit 7f45a6d

Browse files
dellis1972jonpryor
authored andcommitted
[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`.
1 parent 386e6cb commit 7f45a6d

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

src/Xamarin.Android.Build.Tasks/Tasks/GetJavaPlatformJar.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,16 @@ public override bool Execute ()
6969
}
7070

7171
platform = GetTargetSdkVersion (platform, target_sdk);
72-
JavaPlatformJarPath = Path.Combine (MonoAndroidHelper.AndroidSdk.TryGetPlatformDirectoryFromApiLevel (platform, MonoAndroidHelper.SupportedVersions), "android.jar");
73-
74-
if (!File.Exists (JavaPlatformJarPath)) {
75-
Log.LogError ("Could not find android.jar for API Level {0}. " +
76-
"This means the Android SDK platform for API Level {0} is not installed. " +
77-
"Either install it in the Android SDK Manager (Tools > Open Android SDK Manager...), " +
78-
"or change your Xamarin.Android project to target an API version that is installed. " +
79-
"({1} missing.)",
80-
platform, JavaPlatformJarPath);
81-
return false;
82-
}
72+
JavaPlatformJarPath = MonoAndroidHelper.TryGetAndroidJarPath (Log, platform);
73+
if (JavaPlatformJarPath == null)
74+
return !Log.HasLoggedErrors;
8375

8476
TargetSdkVersion = platform;
8577

8678
Log.LogDebugMessage (" [Output] JavaPlatformJarPath: {0}", JavaPlatformJarPath);
8779
Log.LogDebugMessage (" [Output] TargetSdkVersion: {0}", TargetSdkVersion);
8880

89-
return true;
81+
return !Log.HasLoggedErrors;
9082
}
9183

9284
string GetTargetSdkVersion (string target, XAttribute target_sdk)

src/Xamarin.Android.Build.Tasks/Tasks/JarToXml.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,9 @@ public override bool Execute ()
6868
}
6969

7070
// Ensure that the user has the platform they are targeting installed
71-
var jarpath = Path.Combine (MonoAndroidHelper.AndroidSdk.TryGetPlatformDirectoryFromApiLevel (AndroidApiLevel, MonoAndroidHelper.SupportedVersions), "android.jar");
72-
73-
if (!File.Exists (jarpath)) {
74-
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);
75-
return false;
76-
}
71+
var jarpath = MonoAndroidHelper.TryGetAndroidJarPath (Log, AndroidApiLevel);
72+
if (jarpath == null)
73+
return !Log.HasLoggedErrors;
7774

7875
// Ensure that all requested jars exist
7976
foreach (var jar in SourceJars)

src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,5 +530,21 @@ public static IEnumerable<string> Executables (string executable)
530530
foreach (var ext in pathExts)
531531
yield return Path.ChangeExtension (executable, ext);
532532
}
533+
534+
public static string TryGetAndroidJarPath (TaskLoggingHelper log, string platform)
535+
{
536+
var platformPath = MonoAndroidHelper.AndroidSdk.TryGetPlatformDirectoryFromApiLevel (platform, MonoAndroidHelper.SupportedVersions);
537+
if (platformPath == null) {
538+
var expectedPath = MonoAndroidHelper.AndroidSdk.GetPlatformDirectoryFromId (platform);
539+
log.LogError ("XA5207", "Could not find android.jar for API Level {0}. " +
540+
"This means the Android SDK platform for API Level {0} is not installed. " +
541+
"Either install it in the Android SDK Manager (Tools > Open Android SDK Manager...), " +
542+
"or change your Xamarin.Android project to target an API version that is installed. " +
543+
"({1} missing.)",
544+
platform, Path.Combine (expectedPath, "android.jar"));
545+
return null;
546+
}
547+
return Path.Combine (platformPath, "android.jar");
548+
}
533549
}
534550
}

0 commit comments

Comments
 (0)