Visual Studio 2022 takes on a lot of the effort for setting up your cross-platform development environment. So what do you do when you don't have VS22? In this post I'll cover how you can still get your environment setup for .NET MAUI development.
Pre-requisites
- Xcode 13 latest version - available in the Mac App Store and from https://developer.apple.com
-
Android Studio
- Latest Android SDK - once installed, make sure you have the latest SDK from the SDK Manager
- Android Emulator (optional) - once installed, create and start an Android emulator. Optionally, you can use an Android device that is configured for development
- OpenJDK 11 from Microsoft
-
VS Code (optional) - you'll want something to edit your code
- Omnisharp extension for C# intellisense
Install .NET 6 with .NET MAUI
- Download and run the .NET 6 installer from dot.net.
- Open Terminal or your favorite command line app and check that you're ready to install .NET MAUI.
> dotnet --version
Option A) Install .NET MAUI using workload install command:
> sudo dotnet workload install maui
This command will get you the latest released version of .NET MAUI plus the platform SDKs for Android, iOS, macOS, and Windows.
Option B) Pass in additional parameters to the same command in order to get a specific branch build.
> sudo dotnet workload install maui --from-rollback-file https://aka.ms/dotnet/maui/preview.11.json --source https://aka.ms/dotnet6/nuget/index.json --source https://api.nuget.org/v3/index.json
Your app will use the newest version available on your system. If you wish to pin your project to a specific version of .NET MAUI such as preview 11, add this to your ".csproj":
<MauiVersion>6.0.101-preview.11.2349</MauiVersion>
These commands can be found in the contributor's development guide on dotnet/maui
That's it! You now have the building blocks to create, build, and run a .NET MAUI app. Let's do that.
New App
To create a new app run:
> dotnet new maui -n "MyMauiApp"
CD into the "MyMauiApp" directory and run the app:
> dotnet build -t:Run -f net6.0-maccatalyst
This will restore the project dependencies, compile the app, and launch it. The -f
parameter is the "target framework moniker". Options include:
- net6.0-android
- net6.0-ios
- net6.0-maccatalyst
To run Windows on Windows you need Visual Studio 2022.
Targeting iOS
In order to target an iOS emulator, you need to provide the device id (UUID). Open Xcode and then go to Windows > Devices and Simulators. Right-click the simulator you want to use and copy the "Device Identifier".
Now append the value to the parameter -p:_DeviceName=:v2:uuid=
:
> dotnet build -t:Run -f net6.0-ios -p:_DeviceName=:v2:udid=02C556DA-64B8-440B-8F06-F8C56BB7CC22
Targeting Android
Before building the Android app, start an emulator or connect to a device using adb connect
. Then you can run:
> dotnet build -t:Run -f net6.0-android
Troubleshooting tip: if you get an error indicating you need to set the JavaSdkDirectory in Visual Studio, try adding the following to a file "Directory.Build.props".
<Project>
<PropertyGroup>
<JavaSdkDirectory Condition="'$(JavaSdkDirectory)'=='' and '$(JAVA_HOME_8_X64)'!=''">$(JAVA_HOME_8_X64)</JavaSdkDirectory>
</PropertyGroup>
</Project>
Conclusion
Now you are up and running with .NET MAUI for building Android, iOS, and macOS apps on macOS. This doesn't provide you with modern productivity features like C# and XAML hot reload. Those come standard with Visual Studio 2022 (and the upcoming Mac version), or you can navigate your way through configuring any number of other hot reload options available such as LiveSharp or Reloadify3000. Personally, I'll be using Comet which rides atop .NET MAUI and is built around hot reload.
Top comments (3)
Thanks for the great article!
Might be helpful links for someone:
Sample native projects on GitHub
Also, you can see the list of available simulators via terminal command
xcrun simctl
:Run on iPhone 11 for example:
As a suggestion I would like to see an article for .Net MAUI where I could set up a Mac as a build server for my PC version of Visual Studio. There is an old article for Xamarin but to-date I haven't been able to make it work.
"uuid" should be "udid" :) Other than that great article!