In my last How-To – Build and run a Kotlin/Native application with Visual Studio Code on macOS I showed you how to setup, build, and run your first Kotlin/Native application using the barebone Kotlin/Native compiler inside Visual Studio Code (VSC). With Gradle, all the previous steps can be automated in just a couple of easy steps. Moreover, with Gradle, it becomes also feasible to handle the development of even large applications.
Inhalte / Contents
I'm down here at the BUILD Conference in San Francisco and Microsoft has just launched Visual Studio Code - a code-optimized editor for Windows, Mac, and Linux and a new member of the Visual Studio Family. Visual Studio for Mac is a powerful developer tool that reinforces Microsoft’s “mobile-first”, “cloud-first”, and “any developer, any platform, any device” strategy. With the author's guided expertise and extensive code samples, you will understand how to leverage the most useful tools in Visual Studio for Mac, the code editor.
Visual Studio for Mac is really working hard to get feature parity with it’s brother on Windows, but on the other hand it’s interesting to see the direction of Visual Studio Code. That IDE is getting more features as well through plugins, but hasn’t reached the level of VS yet. Visual Studio for Mac is the Microsoft IDE natively designed for the Mac. You get everything you need for mobile, web, game and cloud development with C# and F#, with rich support for Xamarin.
Code is available over at github
Installing Gradle (with Homebrew)
Because you are reading this How-To, I assume you know what Gradle is? In case you don’t, let’s stay very brief about it: Gradle is a tool that manages all the dependencies your project relies on, it configures all tools for the build process, it builds your project and: it has to be installed!
In fact, having Gradle in place, you don’t even need to follow my last tutorial (except for the short VSC part). Gradle will take care of downloading, installing and running the Kotlin/Native compiler! Nice, isn’t it?
Do you want to know more? Check out the official Gradle site here: https://gradle.org
To install Gradle you have at least two options:
- Follow the official guide for installation at https://gradle.org/install/
- Install Gradle via the fabulous package manager for Mac called Homebrew: https://brew.sh
If you choose to use Homebrew (and I sincerely encourage you to do so), just fire up a terminal window (or use the one that you have opened for installing Homebrew) and run the two commands:
The first command will update all the package definitions of the Homebrew repositories. This way you will have all the up-to-date tools available. The second command installs and registers Gradle.
Check if Gradle is working correctly by executing the following command:
Check if Gradle is working correctly by executing the following command:
To get the most out of Gradle inside VSC make sure you have the Gradle Language Support plugin installed: https://marketplace.visualstudio.com/items?itemName=naco-siren.gradle-language
Finished!
At least for the Gradle installation, now let’s move on to VSC and the first Gradle based Kotlin/Native project.
Setting up a new minimal Kotlin/Native project in VSC
Because we are using Gradle now, it is not necessary anymore to change the executor file extension mapping settings as I showed you in my previous How-To. Gradle takes care of the file discovery and compilation out of the box as long as you follow some file and folder structuring rules. If not, you will have to do some advanced Gradle configuration, which is not the focus of this tutorial.
- Fire up VSC and (for this How-To) close any workspaces, folders, and files in VSC. Start with a clean desk.
- Open an empty folder where you want to store this little example project.
- Create a new File in this directory called build.gradle
If you have the plugin vscode-icons installed (as explained), you will see a nice pictogram in front of the filename. - Insert the following minimal Gradle configuration into this file (explanation will follow further down):
- Next, create the necessary folder structure which enables Gradle to find your main source file. Starting with version 1.21 of VSC, you can simply hit the New Folder button in your Explorer-view on the left-hand side and type in src/main/kotlin inside the appearing new folder. VSC will automatically create all subfolders, i.e., src, main and kotlin.
- Create the main source file inside the kotlin subfolder, called (in this How-To at least) hello.kt
- Insert the following code snippet inside the hello.kt file:
- Save the file!
- If everything is done correctly, your Explorer-view should be similar to the following screenshot:
- To build the application open VSC’s build in Terminal-view (either via View → Integrated Terminal, or via a right-click on the top-most folder and selecting Open in Terminal)
- Execute the command
- Be patient; the first run takes very long due to all the dependencies that have to be loaded (like the Kotlin/Native compiler and dependencies if not already installed!).
- You should end up with a newly created folder structure.
While the .gradle folder contains all the meta information of your build environment, the build/konan/bin/macbook folder contains your built executable file called hello.kexe - To run your new application, you can, e.g., execute the following command via the terminal while still in the top-most folder of your project/workspace.
- Finished.
Now you can read you through the extensive Gradle documentation and create complex applications with a ton of internal and external dependencies.
A brief explanation of the minimal gradle.build file
Nevertheless, let us retake a look at our minimal build configuration to understand the basics.
Lets split it up:
This section resolves all dependencies needed for the build tool to build Kotlin/Native apps on your Mac. It loads all required libraries and builds them, if necessary, in-place. Therefore the first run takes a lot longer.
This line applies the Gradle-plugin needed for running the Kotlin/Native tools from the Gradle toolchain.
You can instruct the Kotlin/Native tools and therefore Gradle to build executables for several platforms like Windows, Linux, iOS or Android. In our case, we want a macOS file, which is labeled with the macbook tag. Visual studio for mac clip. Not the best naming choice, but maybe subject to be changed in future Kotlin/Native releases.
Finally, the artifacts section tells Gradle which output (Artefact) to produce. The string ‚hello‘ is the output file’s name. You can name your .kexe file however you like. Gradle does not care in this simple example how the source-file is called. The simple reason is, that whenever you provide only one entry point (aka. main-function) in your project, the file containing it will be used as starting point for the compilation. More complex projects also require more complex Gradle configurations. Android Studio for example, which also uses Gradle heavily for Android development, does most of the Gradle configuration for you. Maybe also VSC will be that smart in the future. For this time being, you have to do all the work yourself with Kotlin/Native and VSC. But it is nice to see that the tools are running that easily already in this early stage of development of Kotlin/Native.
To-Do: Integrating the build, run and debug tasks into VSC
While in this How-To I showed you how to use Gradle for Kotlin/Native on a Mac, the dependency on Visual Studio Code is still quite low. At this time, we use VSC solely for writing code and the Gradle settings. We also have some help on hand, like syntax highlighting and some code completion. However, you can have this functionality in merely every sophisticated code editor. Using the command line interpreter (aka. terminal window) is also nothing VSC related.
The actual benefits from a fully integrated development environment (IDE) start when you can build, run and debug code from within the IDE. I will try to cover those points in one of my future posts.
If you like what I do and want to support me, please consider buying your next tech over one of the following partner links. No drawbacks for you, but some free coffee for me. ☕
Stay productive,
Martin
Martin
Visual Studio For Mac Professional
Last month, Microsoft announced the release ofVisual Studio for Mac: a full-featured development environment to help developers on the Mac create apps, games, and services for mobile, cloud, and web. It’s natively designed for macOS, so both the design – from the toolbar to the file dialogs – and the developer workflow should feel right at home to Mac users. It is also a best-in-class advanced C# code editor – with IntelliSense and a refactoring experience that includes a preview of the proposed code changes.
Mobile and web developers working on the Mac will appreciate the additional features that Visual Studio for Mac provides C# developers, and developers that have used Visual Studio on Windows will feel instantly at home with the familiar solution explorer and menu options. Visual Studio for Mac features first-class support for NuGet – the .NET package manager – which provides access to thousands of prepackaged code libraries; you can also code in F#, and yes, C# 7 features are fully supported!
Cross-platform capabilities don’t end there – Visual Studio for Mac shares the same solution format as its Windows counterpart. Teams with developers on both Mac and Windows can open and work on the same projects, sharing code across platforms and apps. Built-in version control makes it easy to work with small or large teams, on local and remote Git repositories (including GitHub and BitBucket).
Mobile Development
Visual Studio for Mac has a heritage in Xamarin Studio, and thus supports cross-platform application development for iOS, Android, and macOS with Xamarin. By installing the iOS and Android SDKs, you can build cross-platform mobile apps using C#, with complete access to the underlying native APIs (including tvOS and watchOS).
It includes drag-and-drop user interface designers for both iOS and Android, giving you the ability to interactively create native iOS Storyboards and Android XML layouts. Or, if you prefer, you can use Xamarin.Forms XAML to create a re-usable cross-platform user-interface (with a real-time preview option). Whichever option you end up choosing, apps using Xamarin always render native controls and run at native speed.
To make getting started with mobile development easy – we also announced the preview of Xamarin Live Players for iOS and Android, enabling you to start experimenting in seconds. Just pair the app on your phone with Visual Studio for Mac using a QR code and instantly see your app running and you can make live edits along the way. When you want to build complete apps, you can use the simulators and emulators available or test on real phones. Visual Studio for Mac can even help you build and deploy your finished apps to the App Store and Google Play–the archive for publishing build option will guide you through the code-signing and uploading process.
Web and Cloud
Visual Studio for Mac isn’t just for mobile, however. The web editing experience on Visual Studio for Mac comes directly from code ported from Visual Studio (on Windows). It includes support for developing .NET Core apps and ASP.NET Core back-ends, which can be deployed to Windows, Linux, or on Microsoft Azure. The editor also supports full HTML, CSS, and JavaScript syntax highlighting and IntelliSense for your web app’s front-end.
Xamarin Platform C# on iOS, Android, Mac & Windows; Visual Studio App Center Ship higher-quality apps faster with confidence; Xamarin University Unlimited, live. Installing VS for Mac in Mac OS 10.10.5. Baronchng US Member. X code.
To build for the cloud, the Connected Services feature helps add Azure functionality to mobile apps without leaving the IDE, and .NET Core web apps can be published directly to Microsoft Azure. There’s more cool stuff in the pipeline, including Azure Functions support and the ability to deploy using Docker containers, both of which are currently available in preview.
Games too
Additionally, Visual Studio for Mac includes the ability to build games using Unity, the most popular gaming engine around. You can directly edit your Unity scripts with the same world-class C# editing experience, including full syntax highlighting and IntelliSense. Debugging is also just a button away, with full debugger support for Unity games. For mobile games, you can also use Xamarin for access to native gaming APIs like SpriteKit, or cross-platform options like CocosSharp and UrhoSharp.
Try it and let us know what you think
Get started by downloading the Community edition of Visual Studio for Mac for free to begin developing ASP.NET Core web apps, Unity games, and Android and iOS mobile apps, all in C#!
We’re very proud of this release and we want to hear what you think – please, send us your feedback! Leave a comment below, use Visual Studio for Mac’s “Report a Problem” or “Provide a Suggestion” dialog (within the Help menu) to provide feedback, or join the conversation in the Visual Studio for Mac community forums.