Kotlin ios and android

Share code on your terms and for different platforms

Kotlin Multiplatform technology simplifies the development of cross-platform projects. It reduces time spent writing and maintaining the same code for different platforms while retaining the flexibility and benefits of native programming.

Kotlin applications will work on different operating systems, such as iOS, Android, macOS, Windows, Linux, watchOS, and others.

It is trusted in production by many of the world’s leading companies, including Philips, Netflix, Leroy Merlin, and VMWare.

Single codebase for application logic

Maintain a single codebase for networking, data storage and data validation, analytics, computations, and the other logic of your applications.

Share as much as you want – in a new project or in an existing one

Use Kotlin Multiplatform when you start a new project and implement data, business, and even presentation layers just once.

Kotlin Multiplatform can also simplify the development of existing projects. Choose a piece of logic (for example, data validation, filtering, or sorting), which changes frequently and usually goes out-of-sync, make it cross-platform, and connect it to your project as a micro-library.

Go 100% sharing with common UI

Want to go beyond sharing app logic and reuse the whole app code? Sharing the UI is possible with Compose Multiplatform – you can already experiment with sharing the UI between Android and iOS.

Where other technologies abstract away or completely replace platform specific app development, Kotlin Multiplatform is complementary to existing platform specific technologies and is geared towards replacing platform agnostic business logic. It’s a new tool in the toolbox as opposed to replacing the toolbox.

We loved the “shared business, native UI” idea that Kotlin Multiplatform promoted, and it meant that our teams did not have to give up using their preferred toolchains.

The advent of Kotlin Multiplatform provided a serious opportunity to not only become faster at implementing new features, but also to get more interaction in the team between Android and iOS developers.

Performance on both Android and iOS in key areas using shared code was drastically improved when compared Kotlin Multiplatform to our previous Shared Javascript approach. On iOS, we had over 25x faster grading performance than using JavascriptCore. On Android, we had an average improvement of 1.5s off of J2V8 initialization time and 5x faster runtime of shared code itself.

Читайте также:  Python file input readline

Now, with Kotlin Multiplatform Mobile, the speed of development across the three client platforms is pretty much unmatched. A single engineer is able to do all of the UI implementations, now does all three clients and has re-skinned or otherwise changed the screens in Down Dog in just a few months.

Источник

Create your first cross-platform app

Here you will learn how to create and run your first Kotlin Multiplatform 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.

  1. In Android Studio, select File | New | New Project.
  2. Select Kotlin Multiplatform App in the list of project templates, and click Next. Mobile Multiplatform project template
  3. Specify a name for your first application, and click Next. Mobile Multiplatform project - general settings
  4. In the iOS framework distribution list, select the Regular framework option. Mobile Multiplatform project - additional settingsWe recommend using the regular framework for your first project, as this option doesn’t require third-party tools and has fewer 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.
  5. 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.

Select the Project view

Each Kotlin Multiplatform project includes three modules:

Basic Multiplatform Mobile project structure

  • 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.

Читайте также:  Php time if statement

Source sets and modules structure

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

  1. Create an Android virtual device.
  2. In the list of run configurations, select androidApp.
  3. Choose your Android virtual device and click Run. Run multiplatform app on AndroidFirst mobile multiplatform app on Android

Источник

Make your Android application work on iOS – tutorial

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 uses a sample Android application with a single screen for entering a username and password. The credentials are validated and saved to an in-memory database.

If you aren’t familiar with Kotlin Multiplatform for mobile, learn how to set up environment and create a cross-platform application from scratch first.

Prepare an environment for development

  1. Install all the necessary tools and update them to the latest versions. 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.
  2. In Android Studio, create a new project from version control:

Project view

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.

  • Switch to the Project view.
  • Make your code cross-platform

    To make your application work on iOS, you’ll first make your code cross-platform, and then you’ll reuse your cross-platform code in a new iOS application.

    To make your code cross-platform:

    Decide what code to make cross-platform

    Decide which code of your Android application is better to share for iOS and which to keep native. A simple rule is: share what you want to reuse as much as possible. The business logic is often the same for both Android and iOS, so it’s a great candidate for reuse.

    In your sample Android application, the business logic is stored in the package com.jetbrains.simplelogin.androidapp.data . Your future iOS application will use the same logic, so you should make it cross-platform, as well.

    Business logic to share

    Create a shared module for cross-platform code

    The cross-platform code that is used for both iOS and Android is stored in the shared module. The Kotlin Multiplatform plugin provides a special wizard for creating such modules.

    In your Android project, create a Kotlin Multiplatform shared module for your cross-platform code. Later you’ll connect it to your existing Android application and your future iOS application.

    Читайте также:  Примеры на c sharp

    Kotlin Multiplatform shared module

    1. In Android Studio, click File | New | New Module.
    2. In the list of templates, select Kotlin Multiplatform Shared Module, enter the module name shared , and select the Regular framework in the list of iOS framework distribution options.
      This is required for connecting the shared module to the iOS application.
    3. Click Finish.

    The wizard will create the Kotlin Multiplatform shared module, update the configuration files, and create files with classes that demonstrate the benefits of Kotlin Multiplatform. You can learn more about the project structure.

    Add a dependency on the shared module to your Android application

    To use cross-platform code in your Android application, connect the shared module to it, move the business logic code there, and make this code cross-platform.

    1. In the build.gradle.kts file of the shared module, ensure that compileSdk and minSdk are the same as those in the build.gradle.kts of your Android application in the app module. If they’re different, update them in the build.gradle.kts of the shared module. Otherwise, you’ll encounter a compile error.
    2. Add a dependency on the shared module to the build.gradle.kts of your Android application.

    Synchronize the Gradle files

  • Synchronize the Gradle files by clicking Sync Now in the notification.
  • In the app/src/main/java/ directory, open the LoginActivity class in the com.jetbrains.simplelogin.androidapp.ui.login package.
  • To make sure that the shared module is successfully connected to your application, dump the greet() function result to the log by updating the onCreate() method:

    Greeting from the shared module

  • Follow Android Studio suggestions to import missing classes.
  • Debug the app . On the Logcat tab, search for Hello in the log, and you’ll find the greeting from the shared module.
  • Make the business logic cross-platform

    You can now extract the business logic code to the Kotlin Multiplatform shared module and make it platform-independent. This is necessary for reusing the code for both Android and iOS.

    1. Move the business logic code com.jetbrains.simplelogin.androidapp.data from the app directory to the com.jetbrains.simplelogin.shared package in the shared/src/commonMain directory. You can drag and drop the package or refactor it by moving everything from one directory to another. Drag and drop the package with the business logic code
    2. When Android Studio asks what you’d like to do, select to move the package, and then approve the refactoring. Refactor the business logic package
    3. Ignore all warnings about platform-dependent code and click Continue. Warnings about platform-dependent code
    4. Remove Android-specific code by replacing it with cross-platform Kotlin code or connecting to Android-specific APIs using expect and actual declarations. See the following sections for details:

    Replace Android-specific code with cross-platform code

    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.

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

    Источник

    Оцените статью