Skip to content

Commit 56e31f5

Browse files
dellis1972jonathanpeppers
authored andcommitted
[Xamarin.Android.Build.Tasks] Avoid XA5207 for design-time builds (#7434)
Fixes: #7405 Raising an XA5207 error during a [design time build][0] in Visual Studio (Windows) can cause problems in the auto sdk installer: error XA5207: Could not find android.jar for API level 31. This means the Android SDK platform for API level 31 is not installed. Either install it in the Android SDK Manager (Tools > Android > Android SDK Manager…), or change the Xamarin.Android project to target an API version that is installed. (C:\Program Files (x86)\Android\android-sdk\platforms\android-31\android.jar missing.) The design time build is run for all frameworks; you can end up in a position where you have the Windows framework selected in the IDE and the auto sdk installer will try to install an Android framework. Its very confusing for the user. Avoid raising error XA5207 during design time builds. [0]: https://github.com/dotnet/project-system/blob/3f5fb23d3fa91719c93b68f56e99aba6b18939aa/docs/design-time-builds.md
1 parent 0e33557 commit 56e31f5

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class GetJavaPlatformJar : AndroidTask
2323

2424
public string AndroidManifest { get; set; }
2525

26+
public bool DesignTimeBuild { get; set; }
27+
2628
[Output]
2729
public string JavaPlatformJarPath { get; set; }
2830

@@ -84,12 +86,11 @@ public override bool RunTask ()
8486
}
8587

8688
platform = GetTargetSdkVersion (platform, target_sdk);
87-
JavaPlatformJarPath = MonoAndroidHelper.TryGetAndroidJarPath (Log, platform);
89+
JavaPlatformJarPath = MonoAndroidHelper.TryGetAndroidJarPath (Log, platform, designTimeBuild: DesignTimeBuild);
90+
TargetSdkVersion = MonoAndroidHelper.SupportedVersions.GetApiLevelFromId (platform).ToString ();
8891
if (JavaPlatformJarPath == null)
8992
return !Log.HasLoggedErrors;
9093

91-
TargetSdkVersion = MonoAndroidHelper.SupportedVersions.GetApiLevelFromId (platform).ToString ();
92-
9394
return !Log.HasLoggedErrors;
9495
}
9596

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,8 @@ public void IfAndroidJarDoesNotExistThrowXA5207 ()
13651365
if (!Builder.UseDotNet)
13661366
proj.TargetFrameworkVersion = builder.LatestTargetFrameworkVersion ();
13671367
builder.ThrowOnBuildFailure = false;
1368+
Assert.IsTrue (builder.DesignTimeBuild (proj), "DesignTime build should succeed.");
1369+
Assert.IsFalse (builder.LastBuildOutput.ContainsText ("error XA5207:"), "XA5207 should not have been raised.");
13681370
builder.Target = "AndroidPrepareForBuild";
13691371
Assert.IsFalse (builder.Build (proj, parameters: new string [] {
13701372
$"AndroidSdkBuildToolsVersion=24.0.1",

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,15 @@ public static IEnumerable<string> Executables (string executable)
443443
yield return executable;
444444
}
445445

446-
public static string TryGetAndroidJarPath (TaskLoggingHelper log, string platform)
446+
public static string TryGetAndroidJarPath (TaskLoggingHelper log, string platform, bool designTimeBuild = false)
447447
{
448448
var platformPath = MonoAndroidHelper.AndroidSdk.TryGetPlatformDirectoryFromApiLevel (platform, MonoAndroidHelper.SupportedVersions);
449449
if (platformPath == null) {
450-
var expectedPath = MonoAndroidHelper.AndroidSdk.GetPlatformDirectoryFromId (platform);
451-
var sdkManagerMenuPath = OS.IsWindows ? Properties.Resources.XA5207_SDK_Manager_Windows : Properties.Resources.XA5207_SDK_Manager_macOS;
452-
log.LogCodedError ("XA5207", Properties.Resources.XA5207, platform, Path.Combine (expectedPath, "android.jar"), sdkManagerMenuPath);
450+
if (!designTimeBuild) {
451+
var expectedPath = MonoAndroidHelper.AndroidSdk.GetPlatformDirectoryFromId (platform);
452+
var sdkManagerMenuPath = OS.IsWindows ? Properties.Resources.XA5207_SDK_Manager_Windows : Properties.Resources.XA5207_SDK_Manager_macOS;
453+
log.LogCodedError ("XA5207", Properties.Resources.XA5207, platform, Path.Combine (expectedPath, "android.jar"), sdkManagerMenuPath);
454+
}
453455
return null;
454456
}
455457
return Path.Combine (platformPath, "android.jar");

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Tooling.targets

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ projects.
9999
<Target Name="_GetJavaPlatformJar">
100100
<GetJavaPlatformJar
101101
AndroidSdkPlatform="$(_AndroidApiLevel)"
102-
AndroidManifest="$(_AndroidManifestAbs)">
102+
AndroidManifest="$(_AndroidManifestAbs)"
103+
DesignTimeBuild="$(DesignTimeBuild)"
104+
>
103105
<Output TaskParameter="JavaPlatformJarPath" PropertyName="JavaPlatformJarPath" />
104106
<Output TaskParameter="TargetSdkVersion" PropertyName="_AndroidTargetSdkVersion" />
105107
</GetJavaPlatformJar>

0 commit comments

Comments
 (0)