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

Brandon Saunders

06/11/2020, 7:42 PM
Is there an example anywhere of using source annotationRetentions in a multiplatform project? I'm trying to follow this, https://medium.com/@elye.project/annotation-tutorial-for-dummies-in-kotlin-1da864acc442, but it presumes a non-multiplatform project and i'm running into a wall where my annotation module needs to be multiplatform but the processor module can likely not be. When you do that though you get the warning stating that the java processor module can't depend on the android annotation one.
h

Hyun

06/14/2020, 12:33 PM
@Brandon Saunders

https://www.youtube.com/watch?v=w-GMlaziIyo

I think this may help.
b

Brandon Saunders

06/15/2020, 6:07 AM
This is awesome, thanks @Hyun! I've gotten KotlinPoet to ALMOST work on multiplatform and it seems like it might be a viable option, but if I can't get it working in a day or so I think I might investigate this option since it's definitely working on multiplatform. Writing in bytecode strings is going to be a fun brain teaser but I think our problem is small enough that it wouldn't be too terrible. Thanks again for finding this!
h

Hyun

06/15/2020, 6:59 AM
@Brandon Saunders welcome :) may I ask one thing that how you achieve annotation processor with KotlinPoet? If I’m correct, you are saying that kapt + kotlin poet. but, as far as I experience(it may be wrong). kapt is working on JVM only. so, It may not work on IOS or JS. so, I suggested the link above as I’m developing my module with it. I agree that the link is complicated comparing to what we want to achieve and it need a lot of investigation(my approach uses similar way with the link but create kt file instead of byte code) so, It would be appreciated if there is simpler way. would you mind if I ask your approach?
b

Brandon Saunders

06/16/2020, 9:44 AM
I may be completely off, but I believe I can make a multiplatform module for the annotation, then use the JVM only processor module to generate the added source code and then compile the original source + the generated source in a multiplatform project. Does that not seem feasible? You may be right on the only working on JVM part though. I could have sworn I saw that source retentions work on multiplatform, but I may be wrong.
h

Hyun

06/16/2020, 9:56 AM
Thanks to explain detail. I just couldn't find simple way. So, I asked it. When you have completed it by that way. I'll appreciate if you share it. I would like to follow the approach.🙏
y

yan

07/07/2020, 7:09 AM
Hello Brandon, Support for source retention in annotations and annotation processor support is two different features (the first one is a language feature, the second one is about tooling). Currently Kotlin supports annotation processors only for JVM modules. In theory, you can write a JVM annotation processor that generates common Kotlin code. However, in this case you will need to configure dependencies between modules and tasks manually.
h

Hyun

07/07/2020, 8:13 AM
@yan can we use annotation processor for kotlin multiplatform? is there some link to learn? I would like to make my code simpler with that.
y

yan

07/07/2020, 12:51 PM
You can use annotation processor in Kotlin/JVM modules with
withJava()
: https://kotlinlang.org/docs/reference/mpp-dsl-reference.html#jvm-targets. There you can configure kapt as usual.
h

Hyun

07/07/2020, 1:01 PM
@yan sorry for asking again. is it working for kotlin/ios as well? please review if what I understood is correct. if there is A multiplatform module B annotation processor module A depends on B A add code below with kapt
Copy code
jvm {
        withJava()
    }
is it correct or something?
y

yan

07/07/2020, 2:36 PM
Code being processed doesn’t typically depend on annotation processors. An annotation processor is a separate program with its own dependencies. As long as you have the JVM target with
withJava()
, kapt should work. @h0tk3y, please correct me if I’m wrong.
h

h0tk3y

07/07/2020, 3:35 PM
Yes, Kapt should work just fine with MPP JVM targets that apply
withJava()
– this is required because most annotation processors emit Java, and JVM targets ignore Java sources unless
withJava()
is applied. The
android
target should work as well. It won’t work for any other MPP targets, as we can’t compile Java for them. If you face any issues with Kapt in this setup, please let us know.
h

Hyun

07/07/2020, 4:50 PM
thanks for detailed explanation 🙏🙏🙏 I’m working on android and ios annotation processor and want to generate code on both target. in this case, do you mean that kapt won’t work? if won’t, I would like to know if Kotlin complier plugin is proper approach or not.