Use business code for Android and iOS written in Kotlin in a multiplatform Xamarin application?

Hartmut Neubauer 1 Reputation point
2022-06-03T13:37:21.137+00:00

I have some code that is written in Java and Kotlin for Android; the Java part can be translated into Kotlin using the Android Studio. Most of this code is business; that means, independent on any hardware or platform specifics; some Android specific classes (like "Bitmap") can be replaced by abstract or general self-defined classes.

As already known, Kotlin business code can be used in multiplatform applications for Android and iOS. Description here : https://kotlinlang.org/docs/multiplatform-mobile-integrate-in-existing-app.html .
Xamarin is used for multiplatform apps, too.

On the other hand, there is a way to include Kotlin code in Xamarin projects. For this purpose, the Xamarin.Kotlin.StdLib is used : https://libraries.io/nuget/Xamarin.Kotlin.StdLib .

My question: Is it possible to develop a Xamarin project (maybe with Xamarin Forms) that includes the Kotlin business code and will work in both Android and iOS environments?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,813 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 15,741 Reputation points Microsoft Employee
    2022-07-13T01:58:13.58+00:00

    Hello @Hartmut Neubauer ,

    After discussing with the team, the only supporting Frameworks available are Android & .Net 6. Therefore, it cannot work for iOS at this time. It can work for Xamarin Android in a single project. And there is no built-in support for Kotlin in iOS.

    Only a single LibraryProjectZip can be included in a Xamarin.Android binding project. This limitation will be removed in .NET 6. See : Build Items - Xamarin | Microsoft Learn.

    (1) create and compile a Kotlin Multiplatform Mobile (KMM) project in Android Studio

    For the Android Studio issue, you can send the feedback to them, see: Feedback - Android Studio Project Site

    (2) import the business code into iOS with XCode to create a native library or framework, (until here it works)

    This needs to be discussed with Apple, you can create a new thread on Apple forums and get help from Apple at https://developer.apple.com/forums/

    (3) bind this with MS VS into a Xamarin project (maybe Xam Forms).

    It's recommend to submit feed back through the VS2022 Feedback channel, be sure to provide detailed information of what you want to achieve. About how to report the feedback to the product team, see : Report a problem with Visual Studio - Visual Studio (Windows) | Microsoft Learn

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments

  2. Hartmut Neubauer 1 Reputation point
    2022-07-19T07:08:28.637+00:00

    Hello @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) ,

    yesterday I have in fact succeeded to write a little test application with Xamarin (Xamarin Forms) that runs on iOS and uses my original Kotlin code. I ran it at least on an iOS simulator running on a Mac mini. It is complicated to describe it and there are many obstacles, but it is possible.

    I already had succeeded to complete the first two steps as described earlier:
    (1) create and compile a Kotlin Multiplatform Mobile (KMM) project in Android Studio,
    (2) import the business code into iOS with XCode to create a static native library and/or framework. It is also possible to use the tool "lipo" to create a "fat" library that runs on a couple of architectures (arm64, x86_64 and so on).
    (3) The next step is to apply "Objective Sharpie" to create the "ApiDefinitions.cs".
    (4) Afterwards, create a solution within Visual Studio 2019 or 2022 and a project containing this API definitions file that also uses the framework and/or static library created in (2). Compiling may first yield a couple of errors, so the ApiDefinitions.cs has to be adapted manually. The changes that are necessary to do this may be discussed.
    (5) The solution should contain at least three more projects: "MyApp", "MyApp.Android", "MyApp.iOS". (Android is not discussed here) The MyApp.iOS should depend on "MyApp" and the project containing the ApiDefinitons and library/framework. Still then errors may happen, and further adaptations of ApiDefinitions.cs might be necessary.
    (6) Finally, the app can be written. in "MyApp" project, interfaces may be defined that correspond on the classes and functions defined in the library and originally written in the Kotlin project. These interfaces may facilitate the usage of those classes.

    Perhaps you can discuss these outcomings in your team. If there is interest, I also can supply some more details.

    No comments

  3. Hartmut Neubauer 1 Reputation point
    2022-08-08T12:30:47.967+00:00

    I have written some instructions here:

    https://stackoverflow.com/a/73276611/2576374

    No comments