Kotlin DSL in gradle - how do I use Base64

HI there, I am using kotlin dsl in my gradle build scripts by preference. I recently needed to do a Base64 encode… but its failed to resolve Base64.Encoder even though it resolved Base64:

Reproduce with a gradle init, choose library, kotlin, kotlin. Add the following to lib:build.gradle.kts

import java.util.*

...............................

fun getBase64EncodedCredentials(): String {
    // This works in a gradle.build file
    // return "the quality of mercy is not strained".bytes.encodeBase64().toString()
    val bytes = "it dropeth as the gentle rain fro heaven to that place beneath".toByteArray()
    val encoder = Base64.getEncoder()
    return encoder.encode(bytes)
}

In the .kts file I get wiggly red underlines on the references to the encoder.

Am I doing something wrong?

1 Like

Are you sure there is a problem with resolving Base64.Encoder? Because you have a different bug in this code: encode() returns ByteArray, but you declared your function to return String.

You are right - the return type does not match. I corrected that, but the compiler still fails to resolve Base64.Encoder:

1 Like

FYI, I followed your instructions and it works without problems (Java 14). Does this problem occur when using gradle from the command line?

Doh! I usually remember to try that. Good shout - it seems to be associated with the ide (intellij). But the warning does not stop the build from running - either on the command line or in the ide.

Thanks

Phill