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

Cristián Arenas

12/10/2019, 7:19 PM
How can I add a special extension to a class only for Android? In iOS, this worked:
Copy code
fun MyType.newMethod(x: NSSomething) {}
I could call it from iOS with the appropriate type. But if I add the equivalent in Android, it isn’t callable.
k

Kris Wong

12/10/2019, 7:25 PM
that sounds odd. i've added symbols to my android target and not had any issues calling them from an app
not this case specifically, though
k

kpgalligan

12/10/2019, 7:26 PM
If the goal is adding it so you can call it from common, then you need to do an expect/actual for the extension, which you can do. Expects in each platform on their own should work, though.
Also, as a maybe problem, I’ve seen naming issues in the jvm. So, if the file is
MyType.kt
on all 3 platforms, you may have an issue there, but I forget the exact trigger sequence
c

Cristián Arenas

12/10/2019, 7:45 PM
@Kris Wong how did you add something to only the android target then?
k

Kris Wong

12/10/2019, 7:46 PM
i right clicked on the source folder and selected new Kotlin class 😛
c

Cristián Arenas

12/10/2019, 7:46 PM
oh, so a new type, not an extension to an already existing one
k

Kris Wong

12/10/2019, 7:47 PM
correct
c

Cristián Arenas

12/10/2019, 7:47 PM
It’s interesting that it did work on iOS, so I guess this is a bug
@kpgalligan the idea is not to have a common API to call from commonMain, but rather, to expose different APIs to iOS and Android code
k

kpgalligan

12/10/2019, 7:51 PM
I’d be pretty surprised if you couldn’t add extension functions on Android. If you’ve got a very small repro in a repo I’d take a look.
c

Cristián Arenas

12/10/2019, 8:09 PM
Damn, it’s really hard to make a new repo 😅
I had forgotten how long it took us to get the environment “right”
👆 2
1
k

Kris Wong

12/10/2019, 8:29 PM
if you only need a JVM target and not android specifically, the new project wizard does a pretty good job for me