创建第一个跨平台应用
This is the second part of the Getting started with Kotlin Multiplatform Mobile tutorial. Before proceeding, make sure you've completed the previous step.
Set up an environment
Create your first cross-platform app
Add dependencies
Upgrade your app
Wrap up your project
Here you will learn how to create and run your first Kotlin Multiplatform Mobile application using Android Studio.
Create the project from a template
You can also watch the video version of this tutorial created by Ekaterina Petrova, Kotlin Product Marketing Manager.
- In Android Studio, select File | New | New Project.
Select Kotlin Multiplatform App in the list of project templates, and click Next.
Specify a name for your first application, and click Next.
In the iOS framework distribution list, select the Regular framework option.
We recommend using the regular framework for your first project, as this option doesn't require third-party tools and has less installation issues.
For more complex projects, you might need the CocoaPods dependency manager that helps handle library dependencies. To learn more about CocoaPods and how to set up an environment for them, see CocoaPods overview and setup.
Keep the default names for the application and shared folders. Click Finish.
The project will be set up automatically. It may take some time to download and set up the required components when you do this for the first time.
Examine the project structure
To view the full structure of your mobile multiplatform project, switch the view from Android to Project.
Each Kotlin Multiplatform Mobile project includes three modules:
- shared is a Kotlin module that contains the logic common for both Android and iOS applications – the code you share between platforms. It uses Gradle as the build system that helps you automate your build process. The shared module builds into an Android library and an iOS framework.
- androidApp is a Kotlin module that builds into an Android application. It uses Gradle as the build system. The androidApp module depends on and uses the shared module as a regular Android library.
- iOSApp is an Xcode project that builds into an iOS application. It depends on and uses the shared module as an iOS framework. The shared module can be used as a regular framework or as a CocoaPods dependency, based on what you've chosen in the previous step in iOS framework distribution. In this tutorial, it's a regular framework dependency.
The shared module consists of three source sets: androidMain
, commonMain
, and iosMain
. Source set is a Gradle
concept for a number of files logically grouped together where each group has its own dependencies. In Kotlin Multiplatform,
different source sets in a shared module can target different platforms.
This is an example structure of a Multiplatform Mobile project that you create with the project wizard in IntelliJ IDEA or Android Studio. Real-life projects can have more complex structures.
Run your application
You can run your multiplatform application on Android or iOS.
Run your application on Android
- Create an Android virtual device.
- In the list of run configurations, select androidApp.
Choose your Android virtual device and click Run.
Run on a different Android simulated device
Learn how to configure the Android Emulator and run your application on a different simulated device.
Run on a real Android device
Learn how to configure and connect a hardware device and run your application on it.
Run your application on iOS
- Launch Xcode in a separate window. The first time you may also need to accept its license terms and allow it to perform some necessary initial tasks.
In Android Studio, select iosApp in the list of run configurations and click Run.
If you don't have an available iOS configuration in the list, add a new iOS simulated device.
Run on a new iOS simulated device
If you want to run your application on a simulated device, you can add a new run configuration.
In the list of run configurations, click Edit Configurations.
Click the + button above the list of configurations and select iOS Application.
Name your configuration.
Select the Xcode project file. For that, navigate to your project, for example KotlinMultiplatformSandbox, open the
iosApp
folder and select the.xcodeproj
file.In the Execution target list, select a simulated device and click OK.
Click Run to run your application on the new simulated device.
Run on a real iOS device
- Connect a real iPhone device to Xcode.
- Make sure to code sign your app. For more information, see the official Apple documentation.
- Create a run configuration by selecting iPhone in the Execution target list.
- Click Run to run your application on the iPhone device.
If your build fails, follow the workaround described in this issue.
Update your application
Open the
Greeting.kt
file inshared/src/commonMain/kotlin
. This directory stores the shared code for both Android and iOS. If you make changes to the shared code, you will see them reflected in both applications.Update the shared code by using
[reversed()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/reversed.html)
, the Kotlin standard library function for reversing text that works on all platforms:class Greeting { private val platform: Platform = getPlatform() fun greeting(): String { return "Guess what it is! > ${platform.name.reversed()}!" } }
Re-run the androidApp configuration to see the updated application in the Android simulated device.
In Android Studio, switch to iosApp and re-run it to see the updated application in the iOS simulated device.
Next step
In the next part of the tutorial, you'll learn about dependencies and add a third-party library to expand the functionality of your project.
See also
- See how to create and run multiplatform tests to check that the code works correctly.
- Learn more about the project structure, the shared module's artifacts, and how the Android and iOS apps are produced.
Get help
- Kotlin Slack. Get an invite and join the #multiplatform channel.
- Kotlin issue tracker. Report a new issue.