Hi folks, I encountered an issue which is a bit we...
# gradle
e
Hi folks, I encountered an issue which is a bit weird: 1. This is quite a common scenario when you write a Gradle Plugin working with Android Gradle Plugin, it basically traverses all variants so that we can add some tasks based on variant settings:
Copy code
val appClassicExt = project.extensions.findByType(AppExtension::class.java)!!
appClassicExt.applicationVariants.all { variant -> ... }
2. The issues is the
all
function: • When I wrote it normally, it works perfectly, it calls the right
all
function which is
org.gradle.api.DomainObjectCollection # void all(Action<? super T> action);
• However when I worked with Kotlin std, it calls this one from Kotlin `_Collections.kt`:
Copy code
public inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean {
    if (this is Collection && isEmpty()) return true
    for (element in this) if (!predicate(element)) return false
    return true
}
So the traversal is not working anymore, may I know how to specifically make it call the one I want (The Gradle API)? Thanks.
p
Note that AGP4.2/7.0 changes variants api. You would likely want to use new one in future
Copy code
androidComponents {
    onVariants { variant ->
e
Yep, the new Artifact API is awesome, however it exported api is rare.. So the old API still the best solution for current usage if the project requires some more extra data rather than Artifact API provides
for example, versions info, AGP task providers, etc
v
Without seeing the code I'd guess you have at least these three options: • explicitly type the argument as
Action
• Make sure it does not have as last statement an expression evaluating to a boolean, so that it cannot match
(T) -> Boolean
• Don't use
all
but
configureEach
👍 1
e
@Vampire Really helpful advice, I have taken solution3 and it works perfectly. (1 should work as well, 2 I haven 't tried). Thanks.😁
👌 1