New File().length() always return 0

Hi, I am using jenkins to build Android game with Unity.
I want to get the build size with File.length() but it always return 0.
I researched and people say that it’s because that the file is opening for writing.
How can I get the build size now?

the file object always operates on the jenkins controller, not any build agents. Its why its not recommended to use raw java classes inside your pipeline.

There’s no utility to get the size of a file, but you could do something like (untested)

script {
  def filesize
  if (!isUnix()) {
    filesize = powershell(returnStdout: true, script: "Write-Host((Get-Item $filename).length)).trim()
  } else {
    filesize = sh(returnStdout: true, script: 'du $filename | awk '{print $1}').trim() * 1024
  }
}

and turn it into a shared library so it can be used anywhere you want

1 Like

The findFiles step is what you can use for this. It returns file info objects that contain also the file length.

1 Like