Skip to content

Commit c020626

Browse files
dellis1972jonathanpeppers
authored andcommitted
[Xamarin.Android.Build.Tasks] Update XA5207 for VSCode (#8494)
Context: #8413 Context: #8522 The [XA5207][0] error message contained IDE-specific instructions: # Visual Studio error XA5207: Could not find android.jar for API Level 28. This means the Android SDK platform for API Level 28 is not installed. Either install it in the Android SDK Manager (Tools > Android > Android SDK Manager...), or change your Xamarin.Android project to target an API version that is installed. # Visual Studio for Mac error XA5207: Could not find android.jar for API Level 28. This means the Android SDK platform for API Level 28 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. [Visual Studio for Mac is now deprecated][1]: > Visual Studio for Mac is scheduled for retirement by August 31, 2024 Visual Studio Code is now supported, via [C# Dev Kit][2] and the [.NET MAUI][3] extensions. ([Announcement][4].) These changes mean that our error messages are obsolete (building on macOS) or misleading (building within VSCode on Windows). Update the XA5207 error generation logic to take these changes into consideration. Visual Studio (Windows) will retain the existing error message text. The Visual Studio for Mac instructions are replaced with instructions for command-line builds, for use from Visual Studio Code: # Visual Studio error XA5207: Could not find android.jar for API Level 28. This means the Android SDK platform for API Level 28 is not installed; it was expected to be in `…`. Either install it in the Android SDK Manager (Tools > Android > Android SDK Manager...), or change the .NET Android project to target an API version that is installed. See https://aka.ms/xa5207 for more details. # Visual Studio Code error XA5207: Could not find android.jar for API Level 28. This means the Android SDK platform for API Level 28 is not installed; it was expected to be in `…`. You can install the missing API level by running `dotnet build -t:InstallAndroidDependencies -f net8.0-android "-p:AndroidSdkDirectory=…"`, or change the project to target an API version that is installed. See https://aka.ms/xa5207 for more details. Update some of the documentation around the `InstallAndroidDependencies` target to mention `dotnet build` options which are required in order for the target to function correctly. Finally, update various error messages, replacing "Xamarin.Android" with ".NET Android". [0]: https://github.com/xamarin/xamarin-android/blob/40cc8eaf78eb9f95abcc0e966ab274cef1692acc/Documentation/guides/messages/xa5207.md [1]: https://learn.microsoft.com/en-us/visualstudio/mac/what-happened-to-vs-for-mac?view=vsmac-2022 [2]: https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit [3]: https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.dotnet-maui [4]: https://devblogs.microsoft.com/visualstudio/announcing-the-dotnet-maui-extension-for-visual-studio-code/
1 parent b0aab54 commit c020626

File tree

8 files changed

+85
-30
lines changed

8 files changed

+85
-30
lines changed

Documentation/guides/building-apps/build-targets.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ MSBuild /t:Install ProjectName.csproj /p:AdbTarget=-e
9191
Calls the [`GetAndroidDependencies`](#getandroiddependencies) target, then installs
9292
the Android SDK packages specified in the `@(AndroidDependency)` item group.
9393

94+
```dotnetcli
95+
dotnet build -t:InstallAndroidDependencies -f net8.0-android "-p:AndroidSdkDirectory=<path to sdk>" "-p:JavaSdkDirectory=<path to java sdk>"
96+
```
97+
98+
The `-f net8.0-android` is required as this target is a .NET Android specific target. If you omit this argument
99+
you will get the following error:
100+
101+
```
102+
error MSB4057: The target "InstallAndroidDependencies" does not exist in the project.
103+
```
104+
105+
The `AndroidSdkDirectory` and `JavaSdkDirectory` properties are required as we need to know where to install the required components. These directories can be empty or existing. Sdk components
106+
will be installed on top on an existing sdk installation.
107+
94108
The [`$(AndroidManifestType)`](~/android/deploy-test/building-apps/build-properties.md#androidmanifesttype)
95109
MSBuild property controls which
96110
[Visual Studio SDK Manager repository](~/android/get-started/installation/android-sdk.md?tabs=windows#repository-selection)

Documentation/guides/messages/xa5207.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,35 @@ ms.date: 06/26/2019
77

88
## Example messages
99

10-
```
10+
```dotnetcli
1111
XA5207: Could not find android.jar for API Level 28. This means the Android SDK platform for API Level 28 is not installed. Either install it in the Android SDK Manager (Tools > Android > Android SDK Manager...), or change your Xamarin.Android project to target an API version that is installed.
1212
```
1313

1414
## Issue
1515

16-
In order to build a project, the Android SDK Platform matching the target API level must be installed.
16+
In order to build a project, the Android SDK Platform matching the target API level must be installed.
1717

1818
## Solution
1919

20-
Use the Android SDK Manager to install the Android SDK Platform for the desired API level.
20+
Use the Android SDK Manager (Tools &gt; Android &gt; Android SDK Manager...) to install the Android SDK Platform for the desired API level. Alternatively you can install the missing API level by running the following command from a terminal or command prompt:
21+
22+
```dotnetcli
23+
dotnet build -t:InstallAndroidDependencies -f net8.0-android "-p:AndroidSdkDirectory=<path to sdk directory>"
24+
```
25+
26+
Part of the new .net android system is when upgrading projects you will automatically be
27+
upgraded to the latest API level. For example net7.0-android allowed you to target API 33,
28+
but net8.0-android will automatically target API 34. If you want to keep your current
29+
target API level you will need to add the 'uses-sdk' `android:targetSdkVersion` to your `AndroidManifest.xml` file.
30+
31+
```xml
32+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33+
android:versionCode="1"
34+
android:versionName="1.0"
35+
package="com.companyname.myapp">
36+
<uses-sdk android:targetSdkVersion="33">
37+
<application android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" />
38+
</manifest>
39+
```
40+
41+
You might then need to run the `InstallAndroidDependencies` target as mentioned above to ensure that the required API level is installed.

src/Xamarin.Android.Build.Tasks/Properties/Resources.Designer.cs

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Xamarin.Android.Build.Tasks/Properties/Resources.resx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ The capitalized word "Portable" that appears earlier in the message is plain tex
416416
<data name="XA1019" xml:space="preserve">
417417
<value>`LibraryProjectProperties` file `{0}` is located in a parent directory of the bindings project's intermediate output directory. Please adjust the path to use the original `project.properties` file directly from the Android library project directory.</value>
418418
<comment>The following are literal names and should not be translated: LibraryProjectProperties, project.properties, Android
419-
In this message, the term "binding" means a piece of generated code that makes it easy to access an Android API written in Java from a Xamarin.Android project written in C# or F#.
419+
In this message, the term "binding" means a piece of generated code that makes it easy to access an Android API written in Java from a .NET Android project written in C# or F#.
420420
{0} - The path of the LibraryProjectProperties file</comment>
421421
</data>
422422
<data name="XA1020" xml:space="preserve">
@@ -438,8 +438,8 @@ In this message, the term "binding" means a piece of generated code that makes i
438438
<comment>The following are literal names and should not be translated: DX, DEX, d8, AndroidDexTool.</comment>
439439
</data>
440440
<data name="XA1024" xml:space="preserve">
441-
<value>Ignoring configuration file '{0}'. .NET configuration files are not supported in Xamarin.Android projects that target .NET 6 or higher.</value>
442-
<comment>The following are literal names and should not be translated: .NET, Xamarin.Android.
441+
<value>Ignoring configuration file '{0}'. .NET configuration files are not supported in .NET Android projects that target .NET 6 or higher.</value>
442+
<comment>The following are literal names and should not be translated: .NET, .NET Android.
443443
{0} - The file name such as 'Foo.dll.config'</comment>
444444
</data>
445445
<data name="XA1025" xml:space="preserve">
@@ -451,7 +451,7 @@ In this message, the term "binding" means a piece of generated code that makes i
451451
<comment>The following are literal names and should not be translated: AAPT, AAPT2, Android, AndroidUseAapt2, true.</comment>
452452
</data>
453453
<data name="XA1026_dotnet" xml:space="preserve">
454-
<value>Using AAPT is not supported in Xamarin.Android projects that target .NET 6 or higher. Please enable 'Use incremental Android packaging system (aapt2)' in the Visual Studio project property pages or edit the project file in a text editor and set the 'AndroidUseAapt2' MSBuild property to 'true'.</value>
454+
<value>Using AAPT is not supported in .NET Android projects that target .NET 6 or higher. Please enable 'Use incremental Android packaging system (aapt2)' in the Visual Studio project property pages or edit the project file in a text editor and set the 'AndroidUseAapt2' MSBuild property to 'true'.</value>
455455
<comment>The following are literal names and should not be translated: AAPT, Android, AndroidUseAapt2, true.</comment>
456456
</data>
457457
<data name="XA1027" xml:space="preserve">
@@ -505,7 +505,7 @@ Either change the value in the AndroidManifest.xml to match the $(SupportedOSPla
505505
{1} - The SupportedOSPlatformVersion property value</comment>
506506
</data>
507507
<data name="XA2000" xml:space="preserve">
508-
<value>Use of AppDomain.CreateDomain() detected in assembly: {0}. .NET 6 and higher will only support a single AppDomain, so this API will no longer be available in Xamarin.Android once .NET 6 is released.</value>
508+
<value>Use of AppDomain.CreateDomain() detected in assembly: {0}. .NET 6 and higher will only support a single AppDomain, so this API will no longer be available in .NET Android once .NET 6 is released.</value>
509509
<comment>The following are literal names and should not be translated: AppDomain.CreateDomain(), AppDomain
510510
{0} - The name of the assembly</comment>
511511
</data>
@@ -704,12 +704,12 @@ In this message, "root element" refers to the root element of an XML file.
704704
<comment>{0} - The exception message and stack trace of the associated exception</comment>
705705
</data>
706706
<data name="XA4231" xml:space="preserve">
707-
<value>The Android class parser value '{0}' is deprecated and will be removed in a future version of Xamarin.Android. Update the project properties to use 'class-parse'.</value>
707+
<value>The Android class parser value '{0}' is deprecated and will be removed in a future version of .NET Android. Update the project properties to use 'class-parse'.</value>
708708
<comment>The following are literal names and should not be translated: class-parse
709709
{0} - The name of the current class parser value</comment>
710710
</data>
711711
<data name="XA4232" xml:space="preserve">
712-
<value>The Android code generation target '{0}' is deprecated and will be removed in a future version of Xamarin.Android. Update the project properties to use 'XAJavaInterop1'.</value>
712+
<value>The Android code generation target '{0}' is deprecated and will be removed in a future version of .NET Android. Update the project properties to use 'XAJavaInterop1'.</value>
713713
<comment>The following are literal names and should not be translated: XAJavaInterop1
714714
{0} - The name of the current code generation target</comment>
715715
</data>
@@ -790,7 +790,7 @@ The following are literal names and should not be translated: ABI, 'libs/armeabi
790790
"Wear" is a short version of the full product name "Wear OS" and so should not be translated.</comment>
791791
</data>
792792
<data name="XA4312" xml:space="preserve">
793-
<value>Referencing the Android Wear application project '{0}' from an Android application project is deprecated and will no longer be supported in a future version of Xamarin.Android. Remove the Android Wear application project reference from the Android application project and distribute the Wear application as a standalone application instead.</value>
793+
<value>Referencing the Android Wear application project '{0}' from an Android application project is deprecated and will no longer be supported in a future version of .NET Android. Remove the Android Wear application project reference from the Android application project and distribute the Wear application as a standalone application instead.</value>
794794
<comment>The following are literal names and should not be translated: Android Wear, Android, Wear.
795795
{0} - The referenced Android Wear project.</comment>
796796
</data>
@@ -861,19 +861,23 @@ Remove the '{0}' reference from your project and add the '{1}' NuGet package ins
861861
{0} - The missing tool name</comment>
862862
</data>
863863
<data name="XA5207" xml:space="preserve">
864-
<value>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 ({2}), or change the Xamarin.Android project to target an API version that is installed. ({1} missing.)</value>
864+
<value>Could not find android.jar for API level {0}. This means the Android SDK platform for API level {0} is not installed; it was expected to be in `{1}`.
865+
{2}
866+
See https://aka.ms/xa5207 for more details.</value>
865867
<comment>The following are literal names and should not be translated: android.jar
866868
{0} - The API level name
867869
{1} - The expected path of the android.jar file
868-
{2} - The menu location in Visual Studio that can be used to launch the Android SDK Manager</comment>
869-
</data>
870-
<data name="XA5207_SDK_Manager_macOS" xml:space="preserve">
871-
<value>Tools &gt; Open Android SDK Manager...</value>
872-
<comment>This string is the location of a menu command in Visual Studio for Mac.</comment>
870+
{2} - The instructions to install the missing component</comment>
873871
</data>
874872
<data name="XA5207_SDK_Manager_Windows" xml:space="preserve">
875-
<value>Tools &gt; Android &gt; Android SDK Manager...</value>
876-
<comment>This string is the location of a menu command in Visual Studio.</comment>
873+
<value>Either install it in the Android SDK Manager (Tools &gt; Android &gt; Android SDK Manager...), or change the .NET Android project to target an API version that is installed.</value>
874+
<comment>This string is the instrucitons to install the component</comment>
875+
</data>
876+
<data name="XA5207_SDK_Manager_CLI" xml:space="preserve">
877+
<value>You can install the missing API level by running `dotnet build -t:InstallAndroidDependencies -f {0} "-p:AndroidSdkDirectory={1}"`, or change the project to target an API version that is installed.</value>
878+
<comment>This string is the instrucitons to install the component
879+
{0} - The TargetFramework the app is targeting.
880+
{1} - The current AndroidSdkDirectory path.</comment>
877881
</data>
878882
<data name="XA5211" xml:space="preserve">
879883
<value>Embedded Wear app package name differs from handheld app package name ({0} != {1}).</value>

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ public class GetJavaPlatformJar : AndroidTask
2525

2626
public bool DesignTimeBuild { get; set; }
2727

28+
public bool BuildingInsideVisualStudio { get; set; }
29+
2830
public string SupportedOSPlatformVersion { get; set; }
2931

32+
public string TargetFramework { get; set; }
33+
34+
public string AndroidSdkDirectory { get; set; }
35+
3036
[Output]
3137
public string JavaPlatformJarPath { get; set; }
3238

@@ -95,7 +101,9 @@ public override bool RunTask ()
95101
}
96102

97103
platform = GetTargetSdkVersion (platform, target_sdk);
98-
JavaPlatformJarPath = MonoAndroidHelper.TryGetAndroidJarPath (Log, platform, designTimeBuild: DesignTimeBuild);
104+
JavaPlatformJarPath = MonoAndroidHelper.TryGetAndroidJarPath (Log, platform,
105+
designTimeBuild: DesignTimeBuild, buildingInsideVisualStudio: BuildingInsideVisualStudio,
106+
targetFramework: TargetFramework, androidSdkDirectory: AndroidSdkDirectory);
99107
TargetSdkVersion = MonoAndroidHelper.SupportedVersions.GetApiLevelFromId (platform).ToString ();
100108
if (JavaPlatformJarPath == null)
101109
return !Log.HasLoggedErrors;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ public void BuildInDesignTimeMode ([Values(false, true)] bool useManagedParser)
801801
}
802802

803803
[Test]
804-
public void IfAndroidJarDoesNotExistThrowXA5207 ()
804+
public void IfAndroidJarDoesNotExistThrowXA5207 ([Values(true, false)] bool buildingInsideVisualStudio)
805805
{
806806
var path = Path.Combine ("temp", TestName);
807807
var AndroidSdkDirectory = CreateFauxAndroidSdkDirectory (Path.Combine (path, "android-sdk"), "24.0.1", new ApiInfo [] { new ApiInfo { Id = "30" } });
@@ -814,6 +814,7 @@ public void IfAndroidJarDoesNotExistThrowXA5207 ()
814814
if (!Builder.UseDotNet)
815815
proj.TargetFrameworkVersion = builder.LatestTargetFrameworkVersion ();
816816
builder.ThrowOnBuildFailure = false;
817+
builder.BuildingInsideVisualStudio = buildingInsideVisualStudio;
817818
Assert.IsTrue (builder.DesignTimeBuild (proj), "DesignTime build should succeed.");
818819
Assert.IsFalse (builder.LastBuildOutput.ContainsText ("error XA5207:"), "XA5207 should not have been raised.");
819820
builder.Target = "AndroidPrepareForBuild";
@@ -823,6 +824,10 @@ public void IfAndroidJarDoesNotExistThrowXA5207 ()
823824
}), "Build should have failed");
824825
Assert.IsTrue (builder.LastBuildOutput.ContainsText ("error XA5207:"), "XA5207 should have been raised.");
825826
Assert.IsTrue (builder.LastBuildOutput.ContainsText ($"Could not find android.jar for API level {proj.TargetSdkVersion}"), "XA5207 should have had a good error message.");
827+
if (buildingInsideVisualStudio)
828+
Assert.IsTrue (builder.LastBuildOutput.ContainsText ($"Either install it in the Android SDK Manager"), "XA5207 should have an error message for Visual Studio.");
829+
else
830+
Assert.IsTrue (builder.LastBuildOutput.ContainsText ($"You can install the missing API level by running"), "XA5207 should have an error message for the command line.");
826831
}
827832
Directory.Delete (AndroidSdkDirectory, recursive: true);
828833
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,14 +482,14 @@ public static IEnumerable<string> Executables (string executable)
482482
yield return executable;
483483
}
484484

485-
public static string TryGetAndroidJarPath (TaskLoggingHelper log, string platform, bool designTimeBuild = false)
485+
public static string TryGetAndroidJarPath (TaskLoggingHelper log, string platform, bool designTimeBuild = false, bool buildingInsideVisualStudio = false, string targetFramework = "", string androidSdkDirectory = "")
486486
{
487487
var platformPath = MonoAndroidHelper.AndroidSdk.TryGetPlatformDirectoryFromApiLevel (platform, MonoAndroidHelper.SupportedVersions);
488488
if (platformPath == null) {
489489
if (!designTimeBuild) {
490490
var expectedPath = MonoAndroidHelper.AndroidSdk.GetPlatformDirectoryFromId (platform);
491-
var sdkManagerMenuPath = OS.IsWindows ? Properties.Resources.XA5207_SDK_Manager_Windows : Properties.Resources.XA5207_SDK_Manager_macOS;
492-
log.LogCodedError ("XA5207", Properties.Resources.XA5207, platform, Path.Combine (expectedPath, "android.jar"), sdkManagerMenuPath);
491+
var sdkManagerMenuPath = buildingInsideVisualStudio ? Properties.Resources.XA5207_SDK_Manager_Windows : Properties.Resources.XA5207_SDK_Manager_CLI;
492+
log.LogCodedError ("XA5207", Properties.Resources.XA5207, platform, Path.Combine (expectedPath, "android.jar"), string.Format (sdkManagerMenuPath, targetFramework, androidSdkDirectory));
493493
}
494494
return null;
495495
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,12 @@ projects.
9999
<Target Name="_GetJavaPlatformJar">
100100
<GetJavaPlatformJar
101101
AndroidSdkPlatform="$(_AndroidApiLevel)"
102+
AndroidSdkDirectory="$(AndroidSdkDirectory)"
102103
AndroidManifest="$(_AndroidManifestAbs)"
103104
DesignTimeBuild="$(DesignTimeBuild)"
105+
BuildingInsideVisualStudio="$(BuildingInsideVisualStudio)"
104106
SupportedOSPlatformVersion="$(SupportedOSPlatformVersion)"
107+
TargetFramework="$(TargetFramework)"
105108
>
106109
<Output TaskParameter="JavaPlatformJarPath" PropertyName="JavaPlatformJarPath" />
107110
<Output TaskParameter="TargetSdkVersion" PropertyName="_AndroidTargetSdkVersion" />

0 commit comments

Comments
 (0)