https://kotlinlang.org logo
Title
m

Matthew Laser

09/29/2021, 2:47 PM
hey gang - in doing some searching I haven't seen this topic broached in a bit. is there any straightforward recipe for combining KSP with an android library? I've been trying to cram the
com.android.library
plugin into the KSP Quickstart's
test-processor
module without much luck
e

elihart

09/29/2021, 5:56 PM
Generally processor libraries shouldn’t/can’t also be android libraries. At least that was the story with java annotation processors and I imagine KSP is similar
if you need an Android type you can look it up by fully qualified name in KSP at runtime
m

Matthew Laser

09/29/2021, 6:00 PM
yeah, that makes sense - I don't necessarily want to make the processor lib an Android lib, but was hoping to access the android libs from my KSP processor to generate code that adds a certain decorator to android library classes, then sticks that resulting code into a different module that is an actual library
and it occured to me I didn't know how to access the Android classes in my project without using either the
android
or
com.android.library
gradle plugins
w

William Reed

09/29/2021, 6:06 PM
do you have a specific example of what you are trying to access?
m

Matthew Laser

09/29/2021, 6:10 PM
I'd like to use KotlinPoet to generate some convenience methods around `DevicePolicyManager`'s member functions and stick them inside a different module that can be compile as a jar or aar. Somewhat similar to what the KSP Quickstart does, just concerned with specific Android based classes I'm interested in
w

William Reed

09/29/2021, 6:13 PM
not sure if this is helpful, but in my annotation processor, i create
android.os.Bundle
extension functions with the following:
private val bundleClassName = ClassName("android.os", "Bundle")
val funBuilder = FunSpec.builder("someExtensionMethod")
    .receiver(bundleClassName) // bundle is the receiver (extension function)
👍 1
this is getting more into ktpoet, but the example here is that we don’t need to be an android library to do that
m

Matthew Laser

09/29/2021, 6:14 PM
this is helpful - part of what I'm trying to figure out what the right way to do this is 😄
but I'm trying to inspect the parameters, etc, that the individual functions are invoked with
w

William Reed

09/29/2021, 6:23 PM
i don’t know that an annotation processor is aware of those things
👌 1
m

Matthew Laser

09/29/2021, 6:26 PM
thanks for the advice william, I appreciate it
🇳🇵 1
all this said while it's not really leveraging KSP apart form being it's own gradle plugin, was able to get what I was after by adding the dependency of :)
implementation(files("/Users/matthew.laser/Library/Android/sdk/platforms/android-30/android.jar"))