Gradle Kotlin DSL 无法找到 java.io 包。

huangapple go评论71阅读模式
英文:

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:

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.

英文:

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.

答案1

得分: 9

java.io 导入在 plugins 块之前进行时是有效的。我可以成功运行 Gradle 7.3.2 和 6.9.2 的构建,使用以下的 build.gradle.kts

import java.io.ByteArrayOutputStream

plugins {
    `java-library`
}

println(ByteArrayOutputStream::class)

如果你转义全包名,那么你也可以使其在不导入该类的情况下工作(如上所测试):

plugins {
    `java-library`
}

println(`java.security`.MessageDigest::class)
英文:

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)

答案2

得分: 6

在IntelliJ IDEA的后端项目中,我在build.gradle文件的顶部添加了以下导入语句:

import java.io.ByteArrayOutputStream

现在它可以工作了。

英文:

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.

答案3

得分: 2

我的观察:

  • 关闭项目
  • 删除.idea.gradle文件夹
  • 通过选择根目录的build.gradle.kts文件并选择"以项目方式打开"来打开项目

有所帮助。

英文:

My observation:

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

did help.

答案4

得分: 1

这是与Android Studio 3.5.6相关的问题。升级到Android Studio 3.6后,一切都正常工作。

英文:

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

huangapple
  • 本文由 发表于 2020年1月7日 00:29:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定