Shift-left benefits for mobile app development is not just a tool problem

You might know Kotlin as a modern programming language and as the official language for building Android apps in 2021. What then is Kotlin/Native?
Kotlin/Native makes it possible to compile Kotlin code to native binaries that are capable of running without a virtual machine. Kotlin/Native is based on LLVM infrastructure. With Kotlin/Native, it is possible to develop for multiple platforms. For example, it’s possible to develop for iOS using Kotlin/Native.
In this post, we’ll take a deep dive into Kotlin/Native setup. We’ll look at two ways to do this and close with the pros and cons of each method. So let’s get started with Kotlin/Native setup using a command-line compiler.
Here we’ll set up Kotlin/Native using the command-line compiler.
First, we need to download the Kotlin/Native compiler. It’s available for Windows, macOS, and Linux. Click here to get the compiler from GitHub. Make sure you get the correct version for your machine. For example, if you are on are using a macOS device with an Intel-based processor, download the kotlin-native-macos-x86_64 asset.
After downloading the compressed file, extract its content to a folder on your device. For this example, in this tutorial we’ll be extracting to “home/development.”
Once you’re done extracting the archive, note the path to the “bin” folder in the extracted content. Next, add the path to your machine’s environment variable. The process for doing this varies for different operating systems. However, a quick search on the internet for “how to add an environment variable” for your operating system should help. For our example, we’re using a macOS device with a zsh-based terminal. Here are the instructions if yo’ure using the same operating system.
Open the terminal and run the following commands:
sudo nano ~/.zshenv
The above command will open the .zshenv configuration file in the nano text editor. Next, add the path for bin noted earlier on a new line using the following code:
export PATH=$PATH:{full/path/to/kotlin/native/compiler/bin/}
Note: Replace “{full/path/to/kotlin/native/compiler/bin/}” with the actual path to the file on your machine. (And don’t include the braces.)
Finally, restart the terminal so that the changes to the environment variable can be loaded.
At this point, we have the environment variables set. Hence, we can now write some Kotlin code to test our setup. We’ll be using nano to write the code in this step. However, you can use any text editor of your choice.
But before doing that, create a new directory to store the Kotlin file. Then change your working directory to the new directory. Run the following command in the terminal to change your working directory:
cd {/path/to/folder}
Once again, replace “{/path/to/folder}” with the actual path for the new folder you created.
Once all that is done, run the following command to create a new Kotlin file using nano:
nano hello.kt
Then paste the following Kotlin code in nano”
fun main() {
println("Hello Kotlin!")
}
After that, press control + x and accept the save prompt to save your code.
Now let’s test our code. Open the terminal, make your working directory the place where you saved the code, then run the following command:
kotlinc-native hello.kt -o hello
If you get a successful build, you’ve run your first Kotlin/Native program.
Now let’s create a project using the IntelliJ IDE.
The only requirement before getting started here is the IntelliJ IDE itself. You can download IntelliJ for free here.
The following steps were performed using IntelliJ version 2021.2.1. You might notice slight differences in the user interface if you are on a different version of IntelliJ. However, any differences should not cause any issues.
First, start by creating a new Kotlin/Native project. To do that, navigate to File -> New -> Project in IntelliJ. The new project window will open.
Select Kotlin from the left pane and Native Application on the middle pane. After that, enter the project name, then click Next.
The following screen should show up after the last screen.
Select native Your system Target, then click Finish. After that, wait for the IDE to set up your new project. This initial setup process may take some time, depending on your internet connection.
Before we continue, let’s talk about the file structure for our new project. Your Kotlin code can be found inside the src folder. The default entry point for our Kotlin/Native app is Main.kt (located in “project/src/nativeMain/kotlin”).
Open the Main.kt file, then replace the entire content with the following code:
fun main() {
println("Hello Kotlin!")
}
The above code snippet is a simple program that prints “Hello Kotlin”.
A quick way to run the code is to click on the button with a green play icon next to the main method in Main.kt and then wait for your code to build. The output from the program will be visible in the Run window of IntelliJ.
You can write automated tests for your Kotlin/Native app in the tests subset directory (for example, nativeTest). Also, you can test an app built with Kotlin/Native using Waldo, a no-code testing solution. You can try Waldo for free here.
You can set up a Kotlin/Native project using the command-line compiler, the IntelliJ IDE, or the Gradle build tool. However, each method has its strengths and weaknesses. So far in this post, we’ve looked at the first two methods. In this section, we’ll compare all three methods.
Pros
Cons
Pros
Cons
Pros
Cons
With Kotlin/Native, it’s possible to extend the universe of the Kotlin programming language beyond Android. For example, you can build native apps for both macOS and iOS using Kotlin/Native.
Kotlin/Native has been around for some time, and there are plenty of learning resources available. Check out the official sample projects repository here to learn more.
This post was written by Pius Aboyi. Pius is a mobile and web developer with over 4 years of experience building for the Android platform. He writes code in Java, Kotlin, and PHP. He loves writing about tech and creating how-to tutorials for developers.