As a bonus there is no need to use undocumented Gradle API any more. (cherry picked from commit 3598165c4d)
3598165c4d