Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET 8 Preview 6 MAUI project fails to build for Android in the command line as it's unable to detect the Android SDK path #16091

Closed
egvijayanand opened this issue Jul 11, 2023 · 18 comments
Labels
platform/android 🤖 s/needs-info Issue needs more info from the author t/bug Something isn't working

Comments

@egvijayanand
Copy link
Contributor

egvijayanand commented Jul 11, 2023

Description

.NET 8 Preview 6 MAUI project fails to build for Android in the command line as it's unable to detect the Android SDK path.

Android SDK is installed in the %LocalAppData%\Android\Sdk folder on my machine.

And .NET CLI is repeatedly looking for the SDK from the Program Files folder.

After installing the latest preview version of VS2022 (17.7 Preview 3.0), found that the Android SDK path is reset to Program Files, have manually modified the setting to rightly point it to the Local AppData folder.

VS Setting:
Xamarin -> Android Settings -> Android SDK Location

Post update of this setting, it is working inside VS IDE.

But it fails to build on the CLI.

Have tried setting the ANDROID_HOME and ANDROID_SDK_ROOT environment variables and even added the path to the PATH environment variable. But ended in vain as it always looks for the Android SDK from the Program Files folder (refer to the log section).

Note: Have selected the Version with bug field as Unknown/Other as the .NET 8 Preview 6 is not yet added.

Steps to Reproduce

  1. Create a new .NET MAUI project (dotnet new maui)
  2. Build the project targeting the Android platform (dotnet build -f net8.0-android)

Link to public reproduction project repository

Default template itself is suffice, no authored code

Version with bug

8.0.0-preview.6.8686

Last version that worked well

8.0.0-preview.5.8529

Affected platforms

Android

Affected platform versions

Android 34.0

Did you find any workaround?

Validate and Update, if necessary, the value of Android SDK location within VS IDE. That's the only option for now.

Refer to the comments for a workaround.

Relevant log output

C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.0.0-preview.6.359\tools\Xamarin.Android.Tooling.targets(100,5): error
 XA5207: Could not find android.jar for API level 34. This means the Android SDK platform for API level 34 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-34\android.jar missing.) [E:\MAUI\NET_8\Preview_6\MauiApp2\MauiApp2.csproj::TargetFramework=net8.0-android]
@egvijayanand egvijayanand added the t/bug Something isn't working label Jul 11, 2023
@egvijayanand
Copy link
Contributor Author

For the time being, have found a workaround for building the project for the Android platform from CLI.

The Android SDK path is controlled by the MSBuild project property named AndroidSdkDirectory.

Pass the correct SDK path as a value for this property while executing the dotnet build command as mentioned below.

dotnet build -f net8.0-android -p:AndroidSdkDirectory=%LocalAppData%\Android\Sdk

As usual for running the project, add the -t:Run property in addition.

Ideally, dotnet CLI should refer to the location from somewhere in a hierarchy, but it failed to do so in this preview version. This needs to be looked into as builds on CI solely rely on these commands.

Pro Tip: This can be written as a batch file to automate the process so that it need not be typed again and again.

%LocalAppData% is an OS environment variable that will automatically point to the current user's local application data directory.

@egvijayanand
Copy link
Contributor Author

Seems like I found the core problem, .NET CLI is proactively making use of a single quote (') assuming that there would be a space in the path of the Android SDK directory (maybe due to the influence of the Program Files folder). But when a single quote is used within the CLI, it is complaining. This could be due to the way MSBuild parameters are being parsed.

The below command with the value within the single quotes is failing with the same error as like without the AndroidSdkDirectory property mentioned.

dotnet build -f net8.0-android -p:AndroidSdkDirectory='%LocalAppData%\Android\Sdk'

Whereas the value within the double quotes works perfectly fine.

dotnet build -f net8.0-android -p:AndroidSdkDirectory="%LocalAppData%\Android\Sdk"

@maddymontaquila
Copy link
Member

cc @jonathanpeppers @jonpryor

@egvijayanand
Copy link
Contributor Author

Referencing this comment as it describes this issue in simple terms.

microsoft/vscode-dotnettools#250 (comment)

@jonathanpeppers
Copy link
Member

If the issue is indeed changing JavaScript code to use double quotes (instead of single). We can probably close the issue here.

@egvijayanand did you notice a problem in ordering of finding the Android SDK? (code for this is here)

I wonder if the logic is always going to prefer the value set in Visual Studio, and this would prevent you from using environment variables like $ANDROID_HOME, $ANDROID_SDK_ROOT, etc. This logic has been here since Xamarin, and there was no VS Code support back then.

@mattleibow mattleibow added the s/needs-info Issue needs more info from the author label Nov 8, 2023
@ghost
Copy link

ghost commented Nov 8, 2023

Hi @egvijayanand. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@ghost ghost added the s/no-recent-activity Issue has had no recent activity label Nov 13, 2023
@ghost
Copy link

ghost commented Nov 13, 2023

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

@ghost ghost closed this as completed Nov 16, 2023
@SoumyadipYT-OSS
Copy link

SoumyadipYT-OSS commented Nov 22, 2023

I am facing another issue when I run the command dotnet build in the dotnetMAUI App project in visual studio 2022

Screenshot (20)

Unable to understand this issue. @egvijayanand @maddymontaquila @jonathanpeppers

@ghost ghost removed the s/no-recent-activity Issue has had no recent activity label Nov 22, 2023
@egvijayanand
Copy link
Contributor Author

Hi @SoumyadipYT-OSS,

If you're running .NET MAUI on .NET 8, you need to have Android SDK 34.

First check the location where Android SDK is pointing to: VS2022 -> Tools -> Options -> Xamarin -> SDK Location

Then check whether this folder ...\android-sdk\platforms\android-34 exists over there.

If not, install and then try.

@SoumyadipYT-OSS
Copy link

Hi @egvijayanand,
I am using .NET MAUI on .NET 8 framework, and already have Android SDK 34. But still after running the command "dotnet build" in the output it shows an error.

@egvijayanand
Copy link
Contributor Author

Try this:

dotnet build -f net8.0-android -p:AndroidSdkDirectory=<path_here>

You need to pass the location of the SDK directory as the value to the MSBuild property. If the path contains space, surround it with quotes.

@SoumyadipYT-OSS
Copy link

After implementing this code
I have written in the powershell developer terminal:
dotnet build -f net8.0-android -p:AndroidSdkDirectory=E:\Android\android-sdk\platforms\android-34

C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.0.43\tools\Xamarin.Android.Toolin
g.targets(100,5): error XA5207: Could not find android.jar for API level 34. This means the Andr
oid SDK platform for API level 34 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. (E:\Android\android-sdk\platforms\android-34\platforms\android-34
\android.jar missing.) [D:\dotnet MAUI practice\ToDo_app\MauiApp2\MauiApp2.csproj::TargetFramewo
rk=net8.0-android]

Build FAILED.

C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.0.43\tools\Xamarin.Android.Toolin
g.targets(100,5): error XA5207: Could not find android.jar for API level 34. This means the Andr
oid SDK platform for API level 34 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. (E:\Android\android-sdk\platforms\android-34\platforms\android-34
\android.jar missing.) [D:\dotnet MAUI practice\ToDo_app\MauiApp2\MauiApp2.csproj::TargetFramewo
rk=net8.0-android]
0 Warning(s)
1 Error(s)

@egvijayanand
Copy link
Contributor Author

dotnet build -f net8.0-android -p:AndroidSdkDirectory=E:\Android\android-sdk\platforms\android-34

AndroidSdkDirectory should point only to the root directory.

dotnet build -f net8.0-android -p:AndroidSdkDirectory=E:\Android\android-sdk

@egvijayanand
Copy link
Contributor Author

And what's your Xamarin Setting has inside VS2022 @SoumyadipYT-OSS , if it's the same path. Then @jonathanpeppers kindly reopen the issue. It's not yet resolved.

@SoumyadipYT-OSS
Copy link

@egvijayanand how to find the path of Xamarin Setting has inside VisualStudio2022?

@egvijayanand
Copy link
Contributor Author

Check earlier comment👆

@SoumyadipYT-OSS
Copy link

@egvijayanand Thank you issue solved. Have a great day

@github-actions github-actions bot locked and limited conversation to collaborators Dec 26, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
platform/android 🤖 s/needs-info Issue needs more info from the author t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants