https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

adam-mcneilly

09/19/2019, 6:09 PM
Hi! I just walked through this hands on example, which was super helpful. Thanks to whoever wrote this:
<https://play.kotlinlang.org/hands-on/Targeting%20iOS%20and%20Android%20with%20Kotlin%20Multiplatform/01_Introduction>
I noticed that in this example we create a SharedCode module right inside the same folder as the Android app. Is this approach common, or is it normal to have your shared code as its own project that could be kept in its own git repository? If I do that, I'm not sure how difficult it becomes to link it to my Android app since it's no longer as simple as
implementation project(':SharedCode')
. Just curious what others have done about this.
k

Kris Wong

09/19/2019, 6:13 PM
both ways are common. if you pull it out as a library, it's the same as integrating any other artifact into your app.
it could be a submodule/subproject. it could be built separately and hosted in artifactory, whatever works for you.
a

Alejandro Rios

09/19/2019, 6:14 PM
Hey @adam-mcneilly 😄, i've played with multiplatform doing this course https://www.raywenderlich.com/1304635-kotlin-native-and-multiplatform And what's @Kris Wong says makes a lot of sense.
j

JL

09/19/2019, 6:15 PM
We use svn extnerals for linking externally managed subprojects, which is the equivalent of git submodules.
k

Kris Wong

09/19/2019, 6:15 PM
svn!
i've commented on this in a blog post i wrote. unfortunately I have t o play the big company waiting game for it to get published
a

adam-mcneilly

09/19/2019, 6:18 PM
Thanks @Alejandro Rios! And others - all good ideas to look into. I somehow have not encountered submodules yet but after looking that up that sounds really helpful I'll try to learn more about them. 🙂
If I were starting from scratch keeping it all together would sound nice, but if I'm looking to integrate this to existing apps I'd prefer to keep it separate. I'd hate for the iOS team to have to clone the entire Android repo just to get the shared code. 😂
k

Kris Wong

09/19/2019, 6:30 PM
yep
use the IDEA template Mobile Shared Library to create your project
🤯 1
a

adam-mcneilly

09/19/2019, 6:35 PM
Wow! Thanks for sharing that! Really helpful
m

Miguel Fermin

09/19/2019, 6:57 PM
I just started evaluating Kotlin Native to move the common business logic into a SharedLib, and consume from android and iOS. I found this thread really helpful, thanks!
r

russhwolf

09/20/2019, 2:38 AM
Note that the mobile shared library template builds JVM and not Android. You’ll need to adjust it some if you want to access Android APIs in the platform-specific implementations of the shared module.
b

Benjamin Charais

09/20/2019, 3:40 AM
For my project I have a module for each target, and my SharedCode contains an android library implementation that is as bare bones and makes it very easy to build everything multiplatform very stand alone
a

adam-mcneilly

09/20/2019, 3:55 AM
@russhwolf What kind of adjustments?
r

russhwolf

09/20/2019, 11:43 AM
@adam-mcneilly Adding
com.android.library
plugin and
android
block. Some publishing stuff too if you need to import from another project. Details on android-specific multiplatform gradle config at https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#android-support
👍🏼 2
a

adam-mcneilly

09/20/2019, 12:45 PM
Y'all are the best, thanks so much! Company hack week next week, I think I wanna peak at this and try implementing a shared network call or model class or something.
k

Kris Wong

09/20/2019, 12:59 PM
i also have a blog post on converting to an Android target within an MPP. there are several steps. if you need it PM me and I can link you to the draft.
a

adam-mcneilly

09/20/2019, 1:14 PM
I may do so next week if I get stuck. Thanks. 🙂
k

kpgalligan

09/20/2019, 3:37 PM
There’s been a lot of feedback here. I’d say most projects want to have a separate module for shared code rather than having it mixed in to the Android app module, but obviously it’ll work either way. Mentally it’s sort of easier to manage if split out, and it seems like that’s the preferred config, but whatever.
k

Kris Wong

09/20/2019, 3:45 PM
if a project will see a lot of development, it's easy to use a gradle flag to flip between a subproject (for dev), and an artifact in Artifactory or whatever
a

adam-mcneilly

09/23/2019, 1:38 AM
Yeah I'd like to take up Kris' offer (or anyone else who knows of one of these) on the blog post for adding an Android target within the "Mobile Shared Library" template. The information in the link from Russell is helpful but not quite there and I'm not sure what I'm missing.
r

russhwolf

09/23/2019, 1:55 AM
What’s the issue you’re hitting?
a

adam-mcneilly

09/23/2019, 2:02 AM
Well I couldn't figure out how to rename everything from
jvmMain
to
androidMain
but after looking at another project, I tried this magical trick in my build.gradle and it seemed to work:
Copy code
kotlin {
    jvm("android")
}
Then I realized it already includes a maven-publish plugin, and there's a gradle task to publish it to my local maven repo, which is everything I want for right now! I'm still not sure why the thing above fixed anything. Previously it was just
jvm()
. Is the parameter like specifying a name for that module? I also haven't tried referencing any Android specific APIs but I don't think I'll need to. I just want to build a proof of concept with like a shared networking layer.
r

russhwolf

09/23/2019, 2:44 AM
try
android()
or
android("xyz")
. The string is just a name. The function call determines the platform it’s building for.
The name will then correlate to the sourceset. So
android("xyz")
would look for sources in
src/xyzMain/
and
src/xyzTest/
a

adam-mcneilly

09/23/2019, 2:56 AM
Originally doing that I kept getting something like
Extension with name 'android' does not exist.
. I've put the code away for now, but I can check again tomorrow and see what I did wrong. Thanks for checking though. 🙂 If I'm not using platform libraries, does it really matter then? If I can use jvm on Android then this might not be worth sinking time into.
r

russhwolf

09/23/2019, 3:07 AM
yeah if you don’t need android APIs you should be fine with JVM.
That error means you need to apply the
com.android.library
plugin
a

adam-mcneilly

09/23/2019, 3:11 AM
Yes, but then it will tell you that the plugin can't be found. 😂 I tried adding google's maven to the repositories block and no luck. I'm probably missing something extremely obvious.
l

louiscad

09/23/2019, 5:43 AM
@adam-mcneilly This module supports Android, iOS and macOS. It relies on helpers put in
buildSrc
of the root project that allow smooth multiplatform dev in Android Studio or IntelliJ: https://github.com/LouisCAD/Splitties/blob/241a91f582352952813afc5ca0b06555361f74a1/modules/preferences/build.gradle.kts
k

Kris Wong

09/23/2019, 1:08 PM
@adam-mcneilly let me know if you still need that blog post
a

Alejandro Rios

09/23/2019, 1:09 PM
@Kris Wong share the link please
k

Kris Wong

09/23/2019, 1:09 PM
it's not published yet, so I am just offering to share the draft privately to those who need it
my employer controls when it gets published
a

Alejandro Rios

09/23/2019, 1:12 PM
oh, ok
4 Views