Android Studio Kotlin Multiplatform Project Setup and Configuration

Author

Reads 219

Programming Code on Screen
Credit: pexels.com, Programming Code on Screen

Setting up an Android Studio Kotlin Multiplatform project is a straightforward process that involves creating a new project and selecting the Kotlin Multiplatform Mobile template.

To get started, you'll need to have Android Studio installed on your computer, along with the Kotlin plugin.

The project structure for a Kotlin Multiplatform Mobile project consists of three main modules: the shared module, the Android module, and the iOS module.

The shared module contains the common code that can be shared between the Android and iOS apps, while the Android and iOS modules contain platform-specific code.

Getting Started

To get started with Android Studio Kotlin Multiplatform, you'll need the latest edition of Android Studio. You can create a new module by clicking on "File" then "New" then "New Module" and finally "Kotlin Multiplatform Shared Module".

Your project structure will resemble a multimodule project with separate modules for androidApp, iosApp, and shared. The shared module will contain the logic and shareable portion of the application, including network and local database implementation.

You can further divide the shared module into androidMain, iosMain, and commonMain, with androidMain and iosMain containing platform-specific implementation.

Get Started with KMP Template

Mobile phone on wooden table showing installation screen for WHO Info app from World Health Organization.
Credit: pexels.com, Mobile phone on wooden table showing installation screen for WHO Info app from World Health Organization.

To get started with Kotlin Multiplatform (KMP), you'll need the latest edition of Android Studio.

The KMP Shared Module Template is available within Android Studio when you create a new module. Click on "File" then "New" then "New Module" and finally "Kotlin Multiplatform Shared Module" to add a KMP Shared Module to your Android app.

You'll have a project structure like a multimodule project, with modules for androidApp, iosApp, and shared. The shared module will have the logic and shareable portion of the application, the network, the local database, and their respective implementation.

Kotlin Multiplatform has revolutionized cross-platform app development by enabling developers to share code across multiple platforms. This guide will help you set up your first Kotlin Multiplatform project for efficient cross-platform app development.

GitHub - Breezy: Weather Tracking App with Compose

Breezy is a weather tracking app built with Jetpack Compose, a modern UI toolkit for Android.

Jetpack Compose is designed to simplify the process of building UI components and layouts.

Credit: youtube.com, The Best Open-Source Android Weather App 2024 | Breezy Weather

Compose is a declarative API, meaning you describe what you want to see on the screen, and the system takes care of the details.

Compose also provides a powerful animation system that makes it easy to create smooth and engaging animations.

Breezy uses a custom layout to display the weather data, which is composed of several smaller components.

The app's UI is designed to be clean and minimalistic, making it easy to focus on the weather data.

The app's layout is defined using a combination of Compose's layout functions, such as Column and Row.

The Column and Row functions are used to create a grid-like layout that makes it easy to display multiple pieces of information.

The app's UI is highly customizable, making it easy to adapt to different screen sizes and orientations.

The app's layout is also responsive, meaning it adapts to different screen sizes and orientations.

Breezy uses a variety of Compose's built-in components, such as Text and Image, to display the weather data.

The Text component is used to display the weather forecast, temperature, and other relevant information.

Engineer Developing App
Credit: pexels.com, Engineer Developing App

The Image component is used to display the current weather icon.

Compose's built-in components make it easy to create a consistent and visually appealing UI.

Breezy's UI is designed to be intuitive and easy to use, making it a great starting point for anyone looking to build a weather app with Compose.

Environment Setup

To set up your environment for Android Studio Kotlin Multiplatform, you'll need to install the necessary tools. We recommend installing the latest stable versions for compatibility and better performance.

First, you'll need to install the Kotlin Multiplatform Mobile Plugin. This plugin will do the heavy lifting and set up the project for you. You can find the installation instructions in the official documentation, but if you're using Android Studio, you can follow these steps:

1. Install Kotlin Multiplatform Mobile Plugin

2. Enable Kotlin Multiplatform Plugin

To enable the plugin, follow these steps:

  • Open Android Studio
  • Go to File > Settings (on Windows/Linux) or Android Studio > Preferences (on macOS)
  • Navigate to Plugins
  • Search for "Kotlin Multiplatform"
  • Install the plugin and restart Android Studio

By following these steps, you'll be able to set up your environment for Android Studio Kotlin Multiplatform.

Creating a New Project

Credit: youtube.com, Make Apps for Android, iOS and Desktop in Android Studio! Kotlin Multiplatform Setup Made Easy

To create a new project in Android Studio for Kotlin Multiplatform, start by clicking on "File" and then "New" and then "New Project". Select "Kotlin Multiplatform App" from the template section.

You'll see a new template in this section, select "Kotlin Multiplatfrom App" and click on next. Give your app a name, in our case, "Breezy", and select a save location and minimum Android SDK version as shown in the screenshot.

The generated project includes three modules: shared, androidApp, and iosApp. The shared module includes common logic between all modules.

Here are the three modules in brief:

Note that you can change the package name and minimum SDK version according to your needs. Also, make sure to keep the iOS framework distribution as "CocoaPods Dependency Manager" for now.

Understanding the Structure

A Kotlin Multiplatform project has a clear structure that you need to understand to get started. It's divided into shared and platform-specific code, with the shared code being the common logic that can be used across multiple platforms.

The shared module is further divided into androidMain, iosMain, and commonMain, with androidMain and iosMain containing platform-specific implementation.

You'll also have modules for androidApp and iosApp, which will contain the platform-specific code for those platforms.

Key Concepts and Features

Credit: youtube.com, What Is Kotlin Multiplatform And How Does It Work? - KMP for Beginners

In Kotlin Multiplatform, shared code is the foundation of cross-platform app development, compiling for multiple platforms and reducing redundancy and development time. This code is located in the commonMain directory.

You can use platform-specific code that is compiled only for the designated platform, ensuring optimal performance and user experience tailored to each environment. This is done through platform-specific source sets, which allow the use of platform-specific APIs.

Here are some key concepts and features of Kotlin Multiplatform:

  • Common Code: Shared Kotlin code located in the commonMain directory.
  • Targets: Platforms (e.g., JVM, JS, Android, iOS) that your cross-platform app development project will support.
  • Source Sets: Group source files with specific targets, dependencies, and compiler options.
  • Platform-Specific Source Sets: Allow the use of platform-specific APIs, such as Android libraries in androidMain.
  • Intermediate Source Sets: Compile to some, but not all, targets, helping avoid code duplication.

Kotlin Multiplatform's robust interoperability features empower developers to seamlessly access platform-specific APIs and frameworks, significantly enhancing the functionality and integration of applications across various platforms.

Key Features

Kotlin Multiplatform allows developers to write a single codebase that is reusable across multiple platforms, significantly reducing redundancy and development time.

This is made possible through shared code, which is compiled into platform-specific binaries. In fact, Kotlin Multiplatform compiles both shared code and platform-specific code into platform-specific binaries, facilitating easy deployment across various platforms.

A vibrant image of a red locker door with a key in the lock, featuring bold primary colors.
Credit: pexels.com, A vibrant image of a red locker door with a key in the lock, featuring bold primary colors.

Here are the key features of Kotlin Multiplatform:

  • Shared Code: Reusable across multiple platforms.
  • Platform-Specific Code: Compiled only for the designated platform.
  • Compilation: Platform-specific binaries are generated.
  • Interoperability: Seamless access to platform-specific APIs and frameworks.

Platform-specific code can be used to create optimal performance and user experience tailored to each environment. This is achieved by compiling platform-specific code only for the designated platform, ensuring that the code is optimized for that specific environment.

Kotlin Multiplatform's robust interoperability features empower developers to seamlessly access platform-specific APIs and frameworks, significantly enhancing the functionality and integration of applications across various platforms.

Moko Shared ViewModel with Koin DI

Moko Shared ViewModel with Koin DI is a key concept in Kotlin Multiplatform development. The main goal is to give you an overall understanding of the project, as seen in the example "Getting Started with Kotlin Multiplatform[2/n]: Moko Shared View Model & Koin DI".

This step-by-step approach will help you understand the basics of the KMM. The example project will guide you through the process.

Koin DI is a dependency injection framework that simplifies the process of managing dependencies. It's used in conjunction with Moko Shared ViewModel to create a robust and maintainable architecture.

Credit: youtube.com, How to Share ViewModels in Compose Multiplatform (with Dependency Injection!)

By using Moko Shared ViewModel with Koin DI, you can decouple your view models from the UI and make them more reusable. This approach is especially useful when working with complex applications.

In the example project, you'll see how to set up Koin DI and Moko Shared ViewModel. This hands-on experience will help you understand the concepts and apply them to your own projects.

Built With

The tech stack behind our project is impressive, to say the least. We're using Kotlin Multiplatform to simplify cross-platform development.

Kotlin Multiplatform is a game-changer for building cross-platform projects. It allows us to share code between different platforms, reducing development time and increasing efficiency.

We're also leveraging Compose Multiplatform, a modern UI framework that makes building beautiful and performant user interfaces easy and enjoyable. Compose Components Resources provide additional resources for working with Compose Multiplatform.

SQLDelight is another key library we're using, developed by Cash App for working with SQL databases in Kotlin-based Android and multi-platform applications.

Interior of modern kitchen with white and wooden minimalist cabinets and built in appliances in apartment
Credit: pexels.com, Interior of modern kitchen with white and wooden minimalist cabinets and built in appliances in apartment

Here are some of the other libraries we're using:

  • Koin for Dependency Injection
  • Multiplatform Settings for saving simple key-value data
  • AndroidX ViewModel for storing and managing UI-related data
  • Kotlinx-datetime for working with dates and times
  • Kotlinx-serialization for serialization
  • Koala Plot for charting and plotting
  • Spotless for code formatting
  • Github Actions and Renovate for automating workflows and updating dependencies

Prerequisites and Requirements

To start with Android Studio Kotlin Multiplatform development, you'll need to install the necessary tools. Install the latest stable version of Android Studio for compatibility and better performance.

Ensure your system meets the requirements for cross-platform app development. This includes installing the latest version of Java Development Kit (JDK) from the official Oracle website, as Android Studio usually includes this.

Here's a quick rundown of the prerequisites for Kotlin Multiplatform Development:

  • Download and Install Android Studio (Latest Stable Version)
  • Install the latest version of Java Development Kit (JDK) from the official Oracle website
  • Ensure your system meets the requirements for cross-platform app development

Defining Apple Main Source Set

The Apple Main Source Set is a crucial concept in iOS development, and it's essential to understand what it is and how it works. Apple defines it as the base SDK that includes the operating system, frameworks, and libraries required to build apps for a specific iOS version.

You can find the Main Source Set in the Xcode project navigator under the "Derived Data" folder. It's a large directory that contains all the necessary files for building and debugging your app.

Consider reading: Looker Studio Open Source

A Black Android Smartphone with Google Apps on Screen
Credit: pexels.com, A Black Android Smartphone with Google Apps on Screen

The Main Source Set includes the operating system, frameworks, and libraries, which are all necessary for building apps that interact with the iOS operating system. This is why it's so important to use the correct Main Source Set when building your app.

For example, if you're building an app for iOS 14, you'll need to use the Main Source Set for iOS 14. Using the wrong Main Source Set can cause your app to crash or behave unexpectedly.

The Main Source Set is updated by Xcode whenever you make changes to your project settings or import new frameworks. This ensures that your app always uses the latest and greatest versions of the necessary files.

Always make sure to use the correct Main Source Set for your iOS version to avoid any issues with your app.

Prerequisites for Development

To start development, you'll need to meet some basic prerequisites. Install the latest stable version of Android Studio for compatibility and better performance.

Credit: youtube.com, Course Goals and Prerequisites - Developing Android Apps

For Kotlin Multiplatform Development, you'll need to download and install Android Studio, which usually includes the Java Development Kit (JDK). You can also download the JDK from the official Oracle website if you prefer.

Ensure your system meets the requirements for cross-platform app development. This might involve checking your computer's specs, such as processor speed, memory, and storage space.

Here's a quick rundown of the prerequisites for Kotlin Multiplatform Development:

  • Download and Install Android Studio (Latest Stable Version)
  • Install the latest version of Java Development Kit (JDK) from the official Oracle website
  • Ensure your system meets the requirements for cross-platform app development

Background Context

To build a KMM app, it's essential to understand the data flow between the Data Transaction Layer and the UI Rendering Layer. The UI Rendering Layer is a dumb layer that renders data on the respective platform, using Jetpack Compose and Swift.

The Data Transaction Layer focuses on accessing required data, from API, local database, or common business logic, and handles platform-specific implementation. This includes determining which implementation of a function to use based on the user's platform.

We can divide the app into two phases: the Data Transaction Layer and the UI Rendering Layer. This approach helps keep the code organized and maintainable.

To handle platform-specific implementation, we'll use the expect and actual keywords in a scenario where there are different implementations based on the platform.

Project Configuration and Setup

Credit: youtube.com, Get started with Kotlin Multiplatform

To set up a Kotlin Multiplatform project in Android Studio, you'll need to import the shared module as a dependency in both build.gradle.kts and Podfile. This is a crucial step in getting your project up and running.

Kotlin Multiplatform has made it possible to share code across multiple platforms, and setting up a project is relatively straightforward. You can do this by using tools like TouchLab SKIE and SQLDelight for local development.

The shared module is imported as a dependency in both build.gradle.kts and Podfile, which allows you to access its functionality across all platforms. This is a key benefit of Kotlin Multiplatform, enabling you to write code once and deploy it across multiple platforms.

Running and Testing the App

Running and testing your Android Studio Kotlin Multiplatform app is a crucial step in ensuring it works as expected. You can run the application on an Android device or emulator by opening the project in Android Studio and running the imported Android run configuration.

For more insights, see: Looker Studio Running Count

Credit: youtube.com, Kotlin Multiplatform App Development in Android Studio

To run the application on an iPhone device or simulator, you have two options. You can open the ios/iosApp.xcworkspace in Xcode and run the standard configuration, or use the Kotlin Multiplatform Mobile plugin for Android Studio.

To run the desktop application, simply use the command ./gradlew :desktop:run in your terminal. This will launch the app on your desktop.

Here's a quick rundown of how to run the app on different platforms:

Frequently Asked Questions

Which IDE is best for Kotlin Multiplatform?

For Kotlin Multiplatform development, IntelliJ IDEA and Android Studio are top choices, offering full support and stability. If basic support is sufficient, any IDE with Kotlin support will suffice.

Is KMP replacing Flutter?

No, KMP and Flutter have different philosophies and are not replacing each other, but rather complementing each other with distinct approaches to app development

Is Kotlin Multiplatform worth it?

Kotlin Multiplatform offers versatility and wide platform support, making it a worthwhile choice for developers seeking to build cross-platform applications. Its extensive reach across Android, iOS, desktop, web, and server platforms is a significant advantage.

Ann Predovic

Lead Writer

Ann Predovic is a seasoned writer with a passion for crafting informative and engaging content. With a keen eye for detail and a knack for research, she has established herself as a go-to expert in various fields, including technology and software. Her writing career has taken her down a path of exploring complex topics, making them accessible to a broad audience.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.