Here’s another quick resolution to one of the most annoying issues I’ve faced in working with Visual Studio for Mac…
The Problem
When creating a brand new, out of the box, Xamarin.Forms project, it didn’t work. This has been a common trend for Xamarin/Microsoft over the last year or so as they roll out Xamarin + Visual Studio updates. The latest culprit was that the Android project was never able to actually install the Xamarin.Forms package from nuget. This was not obvious during the actual build, since the project DID load successfully, and the build errors were along the lines of not being able to find Android Support library resources in the styles.xml. This also caused an issue where source files were not loading with intellisense or highlighting, so the error was even less obvious (see Xamarin.Tip – Fixing the Highlighting Drop In Your Xamarin.Android Projects).
When going into the nuget package manager and trying to manually add the Xamarin.Forms package, I was hit with this error:
Could not install package 'Xamarin.Android.Support.v4 23.3.0'. You are trying to install this package into a project that targets 'MonoAndroid,Version=v2.3' ...
The Solution
My first instinct was to check the Android target version in the project options to make sure that they were within the supported SDK range, and they were:
Unlike Visual Studio 2017 (or any Windows VS with Xamarin), there was no “Compile using Android Version” picker. THIS field is what is really determines the projects target framework level, but VS for Mac doesn’t have it like it should (hope you’re reading this Microsoft!).
Let’s fix it manually:
- Unload your Android project by right clicking it in the Solution explorer and clicking “Unload”
- Open the Android project .csproj in the editor by right clicking > Tools > Edit.
- For each build configuration, add an explicit
TargetFrameworkVersion
that’s compatible. In my case that wasv4.4
- Your .csproj should look like this:
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> ... <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <ProjectGuid>{2BC9AAD5-D311-499E-8B06-A63CCB65C633}</ProjectGuid> <ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> <OutputType>Library</OutputType> <RootNamespace>AppName.Droid</RootNamespace> <AssemblyName>AppName.Droid</AssemblyName> <AndroidApplication>True</AndroidApplication> <AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile> <AndroidResgenClass>Resource</AndroidResgenClass> <AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest> <MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix> <MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix> <AndroidUseLatestPlatformSdk>true</AndroidUseLatestPlatformSdk> <TargetFrameworkVersion>v4.4</TargetFrameworkVersion> <!-- THIS IS WHAT YOU NEED TO ADD OR CHANGE --> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> <DebugType>full</DebugType> <Optimize>false</Optimize> <OutputPath>bin\Debug</OutputPath> <DefineConstants>DEBUG;</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> <AndroidLinkMode>None</AndroidLinkMode> <TargetFrameworkVersion>v4.4</TargetFrameworkVersion><!-- THIS IS WHAT YOU NEED TO ADD OR CHANGE --> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <DebugSymbols>true</DebugSymbols> <DebugType>pdbonly</DebugType> <Optimize>true</Optimize> <OutputPath>bin\Release</OutputPath> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> <AndroidManagedSymbols>true</AndroidManagedSymbols> <AndroidUseSharedRuntime>false</AndroidUseSharedRuntime> <TargetFrameworkVersion>v4.4</TargetFrameworkVersion><!-- THIS IS WHAT YOU NEED TO ADD OR CHANGE --> </PropertyGroup> ... </Project>
- Close the .csproj editor
- Reload the project by right clicking in the Solution Explorer and selecting Reload
- Retry adding the Xamarin.Forms nuget package to the project
Now you should be good to go. Did this not work for you? Leave a comment below and I’d be happy to help you fix this annoying issue as well!
If you like what you see, don’t forget to follow me on twitter @Suave_Pirate, check out my GitHub, and subscribe to my blog to learn more mobile developer tips and tricks!
Interested in sponsoring developer content? Message @Suave_Pirate on twitter for details.
You can just adjust this under the Compile/Application node. There is a drop down of what you have installed. By default it goes to your latest version. So, for me it defaults to 7.0 automatically.
LikeLike
Not in VS for Mac, that’s only in VS2017/15 for Windows like I explained a bug. I talked to Pierce today about it, and there’s a bug filed for it here: https://bugzilla.xamarin.com/show_bug.cgi?id=56208
I have 7.0 installed, but it created the project without setting it at all which is the real issue. Secondarily that the dropdown in VS2017 on Windows isn’t mirrored on the Mac side of things.
LikeLike
I also want to note that this is in the current stable build for VS for Mac, I’ve been told that a fix is coming through and should be in the Alpha or Beta channel by now.
LikeLike
Let me redact that – the issue is not that the dropdown isn’t there, but that it is in a different place Options > Build > General, but there is also a bug tracked (and mentioned in the above link) for the dropdown not working or identifying installed versions: https://i1.wp.com/alexdunndev.files.wordpress.com/2017/06/screen-shot-2017-06-22-at-4-42-11-pm.png
LikeLike
It can solve bro.
LikeLike
Not sure what point your trying to get at here. Are you saying you don’t see this issue? If so, it’s because of a fix that was released with an update to Visual Studio for Mac. I stated such in a comment above and it is also mentioned in the bugzilla linked. If that’s not the case, are you seeing other problems?
LikeLike