Skip to content

update: IntelliJ IDEA update for the make your app multiplatform tutorial #429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 54 additions & 43 deletions topics/multiplatform-integrate-in-existing-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
<secondary-label ref="Android Studio"/>

<tldr>
<p>This tutorial uses Android Studio, but you can also follow it in IntelliJ IDEA – both IDEs share the same core functionality and Kotlin Multiplatform support.</p>
<p>This tutorial uses Android Studio, but you can also follow it in IntelliJ IDEA. When <a href="quickstart.md">set up properly</a>,
both IDEs share the same core functionality and Kotlin Multiplatform support.</p>
</tldr>

Learn how to make your existing Android application cross-platform so that it works both on Android and iOS.
You'll be able to write code and test it for both Android and iOS only once, in one place.
This tutorial shows how to make an existing Android application cross-platform so that it works both on Android and iOS.
You'll be able to write code for both Android and iOS all at once, in the same place.

This tutorial uses a [sample Android application](https://github.com/Kotlin/kmp-integration-sample) with a single screen
for entering a username and password. The credentials are validated and saved to an in-memory database.
Expand All @@ -24,11 +25,10 @@ After that you'll use your cross-platform code in the Android application, and t

## Prepare an environment for development

1. In the [quickstart](quickstart.md), complete the instructions to [set up your environment for Kotlin Multiplatform development](quickstart.md#set-up-the-environment).
1. In the quickstart, complete the instructions to [set up your environment for Kotlin Multiplatform development](quickstart.md#set-up-the-environment).

> You will need a Mac with macOS to complete certain steps in this tutorial, which include writing iOS-specific code
> and running an iOS application. These steps can't be performed on other operating systems, such as Microsoft Windows.
> This is due to an Apple requirement.
> You need a Mac with macOS to complete certain steps in this tutorial, such as running an iOS application.
> This is due to an Apple requirement.
>
{style="note"}

Expand All @@ -38,12 +38,12 @@ After that you'll use your cross-platform code in the Android application, and t
https://github.com/Kotlin/kmp-integration-sample
```

> The `master` branch contains the project's initial state – a simple Android application. To see the final state
> with the iOS application and the shared module, switch to the `final` branch.
> The `master` branch contains the project's initial state – a simple Android application.
> To see the final state with the iOS application and the shared module, switch to the `final` branch.
>
{style="tip"}

3. Switch from the **Android** view to the **Project** view:
3. Switch to the **Project** view:

![Project view](switch-to-project.png){width="513"}

Expand Down Expand Up @@ -72,23 +72,25 @@ Your future iOS application will use the same logic, so you should make it cross
### Create a shared module for cross-platform code

The cross-platform code used for both iOS and Android will be stored in a shared module.
Starting with the Meerkat version, Android Studio provides a wizard for creating such shared modules.
Both Android Studio and IntelliJ IDEA provide a wizard for creating shared modules for Kotlin Multiplatform.

Create a shared module and connect it to both the existing Android application and your future iOS application:
Create a shared module to connect to both the existing Android application and your future iOS application:

1. In Android Studio, select **File** | **New** | **New Module** from the main menu.
2. In the list of templates, select **Kotlin Multiplatform Shared Module**.
Leave the library name `shared` and enter the package name:
```

```text
com.jetbrains.simplelogin.shared
```

3. Click **Finish**. The wizard creates a shared module, changes the build script accordingly, and starts a Gradle sync.
4. When the setup is complete, you will see the following file structure in the `shared` directory:

![Final file structure inside the shared directory](shared-directory-structure.png){width="341"}

5. Make sure that the `kotlin.androidLibrary.minSdk` property in the `shared/build.gradle.kts` file matches the value of the same
property in the `app/build.gradle.kts` file.
property in the `app/build.gradle.kts` file.

### Add code to the shared module

Expand Down Expand Up @@ -182,7 +184,7 @@ there, and make this code cross-platform.
// ...
}
```
5. Follow Android Studio's suggestions to import missing classes.
5. Follow the IDE's suggestions to import missing classes.
6. In the toolbar, click the `app` dropdown, then click the debug icon:

![App from list to debug](app-list-android.png){width="300"}
Expand Down Expand Up @@ -217,27 +219,8 @@ This is necessary for reusing the code for both Android and iOS.

To make your code work well on both Android and iOS, replace all JVM dependencies with Kotlin dependencies in the
moved `data` directory wherever possible.

1. In the `LoginDataSource` class, replace `IOException` in the `login()` function with `RuntimeException`.
`IOException` is not available in Kotlin/JVM.

```kotlin
// Before
return Result.Error(IOException("Error logging in", e))
```

```kotlin
// After
return Result.Error(RuntimeException("Error logging in", e))
```

2. Remove the import directive for `IOException` as well:

```kotlin
import java.io.IOException
```

3. In the `LoginDataValidator` class, replace the `Patterns` class from the `android.utils` package with a Kotlin

1. In the `LoginDataValidator` class, replace the `Patterns` class from the `android.utils` package with a Kotlin
regular expression matching the pattern for email validation:

```kotlin
Expand All @@ -261,12 +244,32 @@ This is necessary for reusing the code for both Android and iOS.
}
```

4. Remove the import directive for the `Patterns` class:
2. Remove the import directive for the `Patterns` class:

```kotlin
import android.util.Patterns
```


3. In the `LoginDataSource` class, replace `IOException` in the `login()` function with `RuntimeException`.
`IOException` is not available in Kotlin/JVM.

```kotlin
// Before
return Result.Error(IOException("Error logging in", e))
```

```kotlin
// After
return Result.Error(RuntimeException("Error logging in", e))
```

4. Remove the import directive for `IOException` as well:

```kotlin
import java.io.IOException
```


#### Connect to platform-specific APIs from the cross-platform code {initial-collapse-state="collapsed" collapsible="true"}

In the `LoginDataSource` class, a universally unique identifier (UUID) for `fakeUser` is generated using
Expand Down Expand Up @@ -332,7 +335,7 @@ Now, Kotlin will use platform-specific implementations of UUID for Android and i

### Run your cross-platform application on Android

Run your cross-platform application for Android to make sure it works.
Run your cross-platform application for Android to make sure it works as it did before.

![Android login application](android-login.png){width=300}

Expand Down Expand Up @@ -377,9 +380,13 @@ You can set up integration between the iOS app and the framework built by Kotlin
Alternatives to this method are covered in the [iOS integration methods overview](multiplatform-ios-integration-overview.md),
but they are beyond the scope of this tutorial.

1. In Xcode, open the iOS project settings by double-clicking the project name.
2. In the **Targets** section on the left, select **simpleLoginIOS**, then navigate to the **Build Phases** tab.
3. Click the **+** icon and select **New Run Script Phase**.
1. In Android Studio, right-click the `iosApp/simpleLoginIOS.xcodeproj` directory and select
**Open In** | **Open In Associated Application** to open the iOS project in Xcode.
2. In Xcode, open the iOS project settings by double-clicking the project name in the **Project** navigator.

3. In the **Targets** section on the left, select **simpleLoginIOS**, then click the **Build Phases** tab.

4. Click the **+** icon and select **New Run Script Phase**.

![Add a run script phase](xcode-run-script-phase-1.png){width=700}

Expand Down Expand Up @@ -430,7 +437,11 @@ Once you've made sure that Xcode is set up correctly, return to Android Studio:
1. Select **File | Sync Project with Gradle Files** in the main menu. Android Studio automatically generates a run
configuration called **simpleLoginIOS**.

2. In the list of run configurations, select **simpleLoginIOS**. Choose an iOS emulator and then click **Run**.
Android Studio automatically generates a run configuration called **simpleLoginIOS** and marks the `iosApp`
directory as a linked Xcode project.

2. In the list of run configurations, select **simpleLoginIOS**.
Choose an iOS emulator and then click **Run** to check that the iOS app runs correctly.

![The iOS run configuration in the list of run configurations](ios-run-configuration-simplelogin.png){width=400}

Expand Down
2 changes: 2 additions & 0 deletions topics/quickstart.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[//]: # (title: Kotlin Multiplatform quickstart)

<web-summary>JetBrains provides official Kotlin IDE support for IntelliJ IDEA and Android Studio.</web-summary>

With this tutorial, you can get a simple Kotlin Multiplatform app up and running.

## Set up the environment
Expand Down