1

I'm developing a Kotlin library for Android. I want to provide a great developer experience for both Kotlin and Java developers, but because I don't have infinite resources I need to prioritize between those.

The library is interacting with a server I control, so I can add metadata to network calls that would help me know the split of my users.
But is there a way I can know when my library is called from Java VS Kotlin?

I see in this answer that there might be a way to inspect a given class, but as the library owner I don't think I can know which class is calling my own without using reflection which would add some runtime cost. Isn't there a way to get at runtime metadata from the JVM, that would indicate if the code was compiled from Kotlin (even at the cost of false-negatives, e.g. checking if KAPT was used at any point which would at least report some Kotlin users?)

PLNech
  • 3,087
  • 1
  • 23
  • 52
  • 1
    Look at https://stackoverflow.com/a/39806722/1159507 – anber May 14 '19 at 14:29
  • In general, on the JVM -- which both Java and Kotlin use -- you can't really detect anything about who is calling your method. – Louis Wasserman May 14 '19 at 14:34
  • @anber: thanks, but this is about inspecting a given class to know if it is in Kotlin; I don't think having my library trigger reflection calls on each callers would be a good approach. Isn't there any metadata that I can find in the JVM context? – PLNech May 14 '19 at 15:15
  • 1
    I believe you can try to execute `Class.forName( "kotlin.Metadata" );` if it works than its Kotlin, if it failed with `ClassNotFoundException` than its Java – anber May 15 '19 at 05:51

1 Answers1

1

Short answer: NO.

When your code is running, it's compiled in Java bytcode in both cases.

Said
  • 689
  • 6
  • 20