I'm trying to add an external variant to an Androi...
# gradle
j
I'm trying to add an external variant to an Android module using
AdhocComponentWithVariants.addVariantsFromConfiguration()
as documented here. I'm able to do this in a Java module by casting
components["java"] as AdhocComponentWithVariants
. But in the Android module, the component cast fails. The component is this anonymous type instead:
org.jetbrains.kotlin.gradle.plugin.mpp.AbstractKotlinTarget$buildAdhocComponentsFromKotlinVariants$1$2
. I tested with AGP versions back to 7.0.4 and found the cast succeeds in this version, but with AGP 7.1-8.2 it fails. I'm assuming AGP switched to Kotlin or something changed at that time. Would it make sense for the component to simply implement by delegation
AdhocComponentWithVariants by adhocVariant
to provide access to this API as documented by Gradle?
v
The docs you linked to clearly state
To utilise these methods, you must make sure that the SoftwareComponent you work with is itself an AdhocComponentWithVariants, which is the case for the components created by the Java plugins (Java, Java Library, Java Platform).
So yeah, it seems AGP is not compatible to this anymore. You would need to ask the AGP developers whether they could support it again or not. And in the meantime I guess you just need to declare an own component like described lower on that page.
j
I'm asking if there's a reason for the Kotlin gradle plugin to hide this interface, or if it could be exposed in this way, which should enable what I'm doing without the need for me to write a custom plugin and manually recreate the Android component's variants just to access this API. It seems the reason the AGP doesn't support it is because it's using this Kotlin function to create the component.
v
@tapchicoma maybe you can shed some light?
Might need time until he answers though. If you currently report something in YouTrack, it says that people are unavailable and answer will be delayed, so I guess JetBrains has some team events this week.
j
Thanks, this isn't a high priority. I'm looking to help an upstream project add variants as gradle module metadata to their published builds. Currently I have a workaround in my own project. I just spent the weekend doing a gradle deep dive figuring out how this could be done in their case and ran into this blocker.
t
Such approach was added in this commit, but I am not sure explanation in the commit message is still actual. cc @Anton Lakotka [JB]
j
I read that commit as well and from how I interpreted it, the motivation seemed mostly to expose the other APIs, not necessarily to hide the new adhoc one.
If it helps, accessing the wrapped
AdhocComponentWithVariants
captured variable reference via reflection does seem to work to do exactly what I need.
Copy code
def adhocField = components.release.class.getDeclaredFields()
        .find { it.getType() == AdhocComponentWithVariants }
adhocField.setAccessible(true)
AdhocComponentWithVariants adhocComponent = adhocField.get(components.release)

adhocComponent.addVariantsFromConfiguration(...) {
    ...
I created a YouTrack issue.
h
Another question, is there any overall Youtrack/Google issue talking about the pain points of AGP with Kotlin? (But mostly AGP) Even with the latest version of AGP (8.2.0-alpha0x), as a plugin author you still need to use
afterEvaluate
a lot, and configuring kotlin compile tasks is almost impossible, at least I failed. And docs are rare.
t
AGP starting from 8.2 (if I am not mistaken) will configure Kotlin compilations within itself, so you don't need to apply "org.jetbrains.kotlin.android" plugin anymore. Regarding need to use
afterEvaluate
- better to open issues in Google issuetracker