Exception when using named regex groups in gradle script

Hi there,

When I try to use named groups in a kotlin.text.Regex in my build.gradle, I get the following exception:
“Retrieving groups by name is not supported on this platform.”

Example Code:

buildscript {
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-stdlib-jre8:1.1.3")
    }
}

import kotlin.text.Regex
import kotlin.text.MatchNamedGroupCollection

def regex = new Regex("(?<bla>.*)")
MatchNamedGroupCollection groups = regex.matchEntire("abc").groups
println groups.get("bla").value

Of course I use JDK 8 and when I try this in an ordinary kotlin file it works perfectly well.

Help would be appreciated!

I just noticed, you don’t even need the classpath-dependency… gradle seems to have kotlin as default on the classpath.
Maybe thats the problem, that gradle has a non-jre8 kotlin on the default classpath?

I suppose the problem is that Gradle loads its bundled kotlin-stdlib in a separate classloader than the other classpath dependencies. So when stdlib tries to discover the extensions provided by kotlin-stdlib-jre8, it fails to find them in the same classloader.

The obvious solution is to bundle kotlin-stdlib-jre8 into Gradle, but it isn’t possible while Gradle targets Java 7 as minimal supported JRE.

Seems I have to stick to the old way then, until Gradle 5 is released which drops Java 7 support (as mentioned in the current 4.2.1 release notes).

Better to report that in the gradle issue tracker Issues · gradle/gradle · GitHub, perhaps they could advise something here.

I opened issue #3194.

Thanks for your answers so far!

Hi again,
the gradle team thinks they can include kotlin-stdlib-jre8 in their shipment and as long as they don’t explicitely use something declared therein, it will still be compatible to java 7 (see Ship `kotlin-stdlib-jre8` · Issue #558 · gradle/kotlin-dsl-samples · GitHub).

However, as I understand it, the internal implementation will make use of jre8 if the library is there.
This also matches your post, Ilya:

Can you confirm my assumption? If so, I will inform the gradle team, that they should not proceed.

The implementation selector checks the current java version first, before trying to use the extension library.
I believe Gradle codebase is covered with tests extensively, that run on all supported Java versions, so if they don’t fail, it may be fine then to bundle extension libraries.
By the way, running these tests on JDK 9 has revealed a problem that getting named groups from regex doesn’t work there: https://youtrack.jetbrains.com/issue/KT-20865

I see, thanks again for the infos. I will follow the java 9 problem… Seems named regex is quite a complex feature :grinning: