0

I currently have an app on both Android and iOS, with lots of shared business logic. The business logic is in Java and developed in Android Studio on Windows (alongside the Android-specific code). I currently copy the Java files to XCode on a Mac, and use J2ObjC to incorporate them in my XCode project. Edit: I'm already using Kotlin in Android Studio for a few classes that are not shared with iOS.

What's the easiest way to do the same with Kotlin classes/files? I'd like to continue to develop first in Windows/AndroidStudio, then get the files/framework/pod to XCode/Mac to use there.

All the tutorials I've come across appear to assume that all the development is being done on a Mac, with Android Studio running there. There are blogs talking about Gradle Plugins, and I don't understand how they apply in this case. There are also pages like this one here on StackOverflow, but again I don't see how that applies. I'm not aware that I'm using Gradle in XCode, so I guess things have to happen on the Windows/AndroidStudio end.

How do I either copy the source to Mac/XCode and compile it or (I suspect) create a framework or Pod on Windows/AndroidStudio that I can copy to Mac/XCode. Or have I misunderstood altogether!

Kiwi
  • 109
  • 8
  • I think you're looking for KMM: https://kotlinlang.org/docs/multiplatform-mobile-getting-started.html – dominicoder Apr 16 '23 at 17:49
  • @dominicoder Sure, but that appears to be for developing everything on a Mac. I want to continue developing on Windows/AndroidStudio, and export/copy something across to the Mac. I'll look again, in case I'm missing something. Perhaps the first part of that tutorial is relevant for the PC too – Kiwi Apr 16 '23 at 18:58
  • I've not actually used KMM myself, but a cursory search on Google and SO would indicate it does work on Windows. – dominicoder Apr 16 '23 at 21:13

1 Answers1

0

To answer my own question, as it may be useful to others.

You need to have Android Studio installed on your Mac, even if you're not developing there. Android Studio on Mac generates the framework, which is then imported to XCode. You can't generate it from Android Studio on Windows.

The framework is generated from a Build Script in XCode, which appears to invoke Android Studio in the background to generate the framework.

After installing AS, use this link to install the Kotlin Plugin, and this link to install KMM.

Then follow this tutorial to set up your new Android Studio project. The important thing is naming your "iOS app" the same as your existing iOS app, so that it still works without lots of refactoring (as described in the tutorial).

Move (or copy) your whole XCode project folder to the Android Studio project's "iOS app" folder, replacing the existing content. You can then point XCode at that folder instead of the original.

However, the suggested new Build Phase in that tutorial is now out of date, as it uses packForXCode. The newer way is to replace that whole script with embedAndSignAppleFrameworkForXcode:

cd "$SRCROOT/.."
./gradlew :<sharedcodefolder>:embedAndSignAppleFrameworkForXcode

This new Build Phase runs every time you compile, even when nothing has changed. By adding Input Files pointing to your Kotlin source folder and a dummy output file (created with Echo in my case) it should run only when required.

You'll need JDK version 17 installed to make KMM work. I got it from here

If you're already using J2ObjC for cross-platform code, there could be a conflict in JDK versions. KMM requires v17, but J2ObjC requires v1.8 or 11. This can be resolved in XCode by adding a User-Defined Build Setting for JAVA_HOME, and pointing it to the older version. J2ObjC will use this.

And now you're ready to go! Develop your Android app and shared Kotlin on Windows. Copy the shared Kotlin to the shared code folder on your Mac (or use Git for this). Build in XCode. Enjoy.

Kiwi
  • 109
  • 8