Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Kotlin DSL can't find java.io package

I'm trying to convert the build.gradle file of an Android app to a Kotlin DSL. This file has a function like this:

def getLastCommitHash() {
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'rev-parse', '--short', 'HEAD'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

which I converted to this:

fun getLastCommitHash() {
    val stdout = ByteArrayOutputStream()
    exec {
        commandLine("git", "rev-parse", "--short", "HEAD")
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

I get an Unresolved reference: ByteArrayOutputStream error and applying the import which changes it to java.io.ByteArrayOutputStream() shows an Unresolved reference: io error.

Is there something I'm doing wrong? Thanks in advance.

like image 774
Henrique Rocha Avatar asked Feb 06 '26 18:02

Henrique Rocha


2 Answers

Importing from java.io works when doing it before the plugins block. I could successfully run builds for both Gradle 7.3.2 and 6.9.2 with the following build.gradle.kts:

import java.io.ByteArrayOutputStream

plugins {
    `java-library`
}

println(ByteArrayOutputStream::class)

If you escape the full package name, then you can also make it work without importing the class (tested as above):

plugins {
    `java-library`
}

println(`java.security`.MessageDigest::class)
like image 140
Chriki Avatar answered Feb 09 '26 11:02

Chriki


I had this issue in backend project in intellij idea, I added this import at the top of build.gradle file:

import java.io.ByteArrayOutputStream

And now it works.

like image 44
estn Avatar answered Feb 09 '26 11:02

estn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!