13

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.

Henrique Rocha
  • 149
  • 1
  • 9
  • I ran into the same issue after finding a nearly identical function in [this answer](https://stackoverflow.com/a/35041457) and converting it. Upvoted for asking the exact question I had. – Pilot_51 Sep 20 '21 at 14:16
  • 3
    in gradle/kotlin the java value is shadowing the package name, making it nigh impossible to import anything from `java.*` The answer to this question will answer "how do you reference the java package name while it's being shadowed by a variable in kotlin?" – Ryan Leach Dec 28 '21 at 18:06

4 Answers4

9

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)
Chriki
  • 15,638
  • 3
  • 51
  • 66
  • Something strange was happening for me initially, in that IntelliJ seemed reluctant to add imports to a build.gradle.kts, and that it was highlighting the import with a warning. Granted I can't seem to reproduce it now. (my issue was with java.security.MessageDigest) This was the solution I had ended up coming to as well, but it still left me wondering if there was any way of escaping the shadowing when referring to the long name. – Ryan Leach Dec 30 '21 at 16:56
  • I believe I’ve found a way, please see my updated answer. – Chriki Jan 02 '22 at 20:05
6

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.

estn
  • 1,203
  • 1
  • 12
  • 25
2

My observation:

  • close project
  • delete .idea and .gradle folders
  • open project by selecting root-build.gradle.kts with "open as project"

did help.

sschrass
  • 7,014
  • 6
  • 43
  • 62
1

This was an issue with Android Studio 3.5.6. After upgrading to Android Studio 3.6 everything is working.

Henrique Rocha
  • 149
  • 1
  • 9