Home > Mobile >  Shell script in Gradle build: java.io.IOException
Shell script in Gradle build: java.io.IOException

Time:05-07

I have the following lines in my build.gradle file of my Android project:

"./prebuild.sh".execute()

But during the build I get this error:

java.io.IOException: Cannot run program "./prebuild.sh": error=2, No such file or directory

The prebuild.sh script is in the root directory of the app and executable. What's weird is that this exact build works for everyone on the team, just not on my machine (M1). I also remember that this used to work months ago.

This happens on a fresh clone of the repository and a fresh install of Android Studio.

I think I've narrowed it down to a problem with the working directory. If I try to print it like this:

println new File(".").absolutePath

I get the following:

/Users/ale/.gradle/daemon/6.5/.

Which is obviously not my project directory.

Any hints on what I could do to fix it?

CodePudding user response:

Check if the file has DOS line endings (\r\n). This can lead to a confusing "no such file or directory", because it searches for a file called /bin/sh\r (ending with an actual carriage return), which does not exist.

CodePudding user response:

Assuming a functional shell prompt; pass the absolute path instead of . current working directory:

if(rootProject.file('prebuild.sh').exists()) {
    commandLine 'sh', rootProject.file('prebuild.sh').absolutePath
} else {
    println "missing: prebuild.sh"
}

Or how you start it as process, one can also pass the current working directory as second argument:

def proc = "./prebuild.sh".execute([], rootProject.absolutePath)
proc.waitForProcessOutput(System.out, System.err)

I'd run cd first, then pwd should return the expected value:

def proc = "cd ${rootProject.absolutePath} && pwd".execute()
...
  • Related