Hey guys, can you use KSP in an Android library to...
# ksp
h
Hey guys, can you use KSP in an Android library to generate Android Kotlin code?
oh, you can’t, issue
g
You can use KSP to generate Android Kotlin code, but the KSP module should not be an Android library (it doesn't make any sense) but a Java one (as it's used at compilation time on the gradle JVM).
m
That seems strange.. I have used KSP with an Android library module (as part of a multiplatform module). This seems unlikely. If this is true: it would mean that you couldn't use Room database (Google's primary recommended persistence framework) in an Android module (as Room uses KSP)
h
As @glureau said @Mike Dawson, that's what I ended up doing, adding the imports and the Android code by hand, lots of boilerplate and not type safe as mistakes can be made quite easily
g
I believe you can import Android classes tho (at least the Android test stubs to have references and signature). I don't remember the exact setup
m
Ah I think I misunderstood - I guess you mean accessing the Android classes within the annotation processor module. My main workaround there has been to use Kotlin Poet which has "ClassName" - so I can define the required Android classes once as a 'static' constant
Not really typesafe
h
Yes you can't access the Android classes inside the annotation processor module
m
One thing that comes to mind is Robolectric - the testing framework
That provides most Android classes and targets the JVM to run Android code as a JVM unit test.
Not sure it will be worth the effort
g
You may want to try something like
compileOnly("org.robolectric:android-all:13-robolectric-9030017")
https://mvnrepository.com/artifact/org.robolectric/android-all
e
pulling that in at "runtime" seems like extreme overkill to me…
👍 2
the Android SDK already ships with a stub android.jar
110 Views