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?
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()
...
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.
Related
MacOS Monterey version 12.4
I'm trying to run a simple pipeline script on my jenkins job
pipeline {
agent any
stages {
stage('Build') {
steps {
sh('emulator -list-avds')
}
}
}
But it throws an error:
/Users/<my_username>/.jenkins/workspace/<my_job_name>#tmp/durable-22217e91/script.sh: line 1: emulator: command not found
My question is: why is it executing commands in the tmp folder? Anything "emulator" related does work when I run commands via terminal.
Following this answer, I've confirmed I'm in the correct dir
Why Jenkins mounts a temporary volume in addition to the workspace?
You are getting this error because the emulator executable is not set in the PATH. Try something like the below.
Try setting your PATH variable to add emulator executable.
environment {
PATH = "/PATH_EMULATOR/bin:${env.PATH}"
}
or something like the below.
withEnv(["PATH+EMULATOR=/PATH_EMULATOR/bin"]) {
sh('emulator -list-avds')
}
or you can also use the full qualified path to the executable
sh('/PATH_TO_EMULATOR/bin/emulator -list-avds')
I start a shell script using process builder. This has been working fine for days now but today, when I uploaded my files to bitbucket and messed around with them, everything seems to work again except that the ProcessBuilder which can't find the file which is already there:
val processBuilder = ProcessBuilder().command(
filesDir.absolutePath + File.separator + "start.sh").start()
The start.sh script is already in the app's files directory but I'm receiving:
java.io.IOException: Cannot run program "/data/user/0/com.example.project/files/start.sh": error=2, No such file or directory
I've tried to run it like this:
val processBuilder = ProcessBuilder().command("start.sh", filesDir.absolutePath + File.separator).start()
and then I'm receiving access denied although all files have chmod 777 and this file has even a+rx flags.
Well this will not likely help anyone but here is what happened:
I tried to put my code on bitbucket and in the process, I managed to delete everything from my local folder. Luckily my code was still on Bitbucket so I just downloaded a tar file with it and started from scratch. On my second attempt, I managed to do everything correctly but I used the downloaded sources from Bitbucket. Everything was fine except that apparently, when I imported the project directly from Bitbucket, the EOL sequence of the synced/downloaded shell script file changed to CRLF instead of LF... I resaved the file with LF and the problem was solved afterwards.
As I'm running an old Python version on android which gives incorrect file sizes for files > 4 GB I tried writing a workaround to get the correct sizes, code:
def getsize_workaround( filename ):
import subprocess as s
output = s.Popen("ls -l " + filename, shell=True, executable="/system/bin/sh", stdout=s.PIPE).communicate()[0]
size = long(re.split(r'\s+', output)[3])
return size
This works well when I try to call it using a simple python script:
print(getsize_workaround(path))
However, when I try to use it in my NZBGet VideoSort script it can't find ls and pops this error at: output = s.Popen("ls -l " + filename, shell=True, executable="/system/bin/sh", stdout=s.PIPE).communicate()[0]-> : /bin/sh: ls: not found. (function is called at line 824, see dropbox link below).
Haven't got a clue why it can't find ls anymore, anyone help is much appreciated. You can find the VideoSort script here: https://db.tt/oM3U5gZR.
PATH variable didn't include the correct directories when run from NZBGet. Fixed by setting os.environ['PATH'] manually. Thanks to abernert for the tip.
also try adding an alias in your bash script for the python version you want to use
alias python=<python version you want to use>
I have added a suite() method to order my tests the way I want them and thus when I run it through Android JUnit they are executed accordingly. But then I noticed that when I use the Spoon execution, the one using cmd, my test cases are executed alphabetically which is the default order.
Why does this happen and how would you counter it without renaming my test cases?
I have the same issue as you; I require a specific order that my test need to be ran in. The app I am testing is too complicated to run in an unpredictable order. My solution was this:
Add this to your build.gradle:
spoon {
if (project.hasProperty('spoonClassName')){
className = project.spoonClassName
}
}
Now, you can execute a specific class with a command like so:
gradle spoon -PspoonClassName=< com.your.pakage.ClassName>
Next, create a file at the root of your Android project: runAllTests.sh
Edit your .sh to look like this:
#!/bin/sh
date +%b-%dT%H.%M > timestamp.out
sites="$HOME"/path/to/project/root
timestamp="$(cat "$sites"/timestamp.out)"
result_folder="$sites"/results
destdir="$result_folder/Results-$timestamp"
mkdir -p "$destdir"
echo "Directory created: ${destdir##*/}"
<---------- Here you start running the test --------------->
echo "Starting Master Setup"
gradle spoon -PspoonClassName=com.espresso.test.MasterSetup
cp -r "$sites"/app/build/spoon "$destdir"/MasterSetup
echo "Results saved to MasterSetup"
echo "Starting WorkoutSchedule"
gradle spoon -PspoonClassName=com.espresso.test.WorkoutSchedule
cp -f "$sites"/app/build/spoon "$destdir"/WorkoutSchedule
echo "Results saved to WorkoutSchedule"
echo "Starting Setting.test"
gradle spoon -PspoonClassName=com.espresso.test.Settings
cp -r "$sites"/app/build/spoon "$destdir"/Settings
echo "Results saved to Settings"
Then, give the script permissions
cd to the script
type chmod u+x runAllTest.sh
You're set. Now just cd to your root, then to execute your test, type . runAllTest.sh.
So, what this does:
First, it creates a timestamp.out. I use this so I can save my results to a file over and over without previous results being overwritten. You do not need this part.
Next, it creates a result folder in the root of your project if it is not already there.
Then, it will make a folder inside the results folder named Results-SOME-DATE.
Lastly, each test will run, saving the results to the normal spot on your project. (Inside build/spoon) Once test are complete it will copy the results to the results folder, and name each test result appropriately so it is easy to see all your tests ran.
NOTE: This script was wrote for MAC. If your on windows or anything else, this script may need modifications.
Additionally: You will find it is inconvenient to open in to each folder to get the index.html opened. So I wrote this script to add to your bash_profile:
function open-results () {
# the browser to open up `index.html' in.
browser='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
# let the user know what directory we're looking in
printf "looking in %s" "$(pwd)"
echo ...
for paths in $(find ./ -name 'debug' -type d); do
for files in $(find "$paths" -name 'index.html'); do
open -a "$browser" "$files"
done
done
echo done
}
Now, cd in terminal to the Results-SOME-DATE, and type open-results. Again, this was written for terminal. You may need to modify depending on your OS. But the structure should be the same
I hope this helps.
The jUnit testing philosophy is that test cases should not depend on each other so order shouldn't be important. That's why you're finding it hard to do. You might want to consider using the "setUp" method to create initial conditions for your test cases rather than having them build on each other.
I have an Android activity where I'm executing NDK compiled code (command line program) with:
Runtime.getRuntime().exec(myCommand);
and load the needed shared libraries with:
static {
System.loadLibrary(myLib);
}
but when running my project and printing the output from error stream I get the following error:
link_image[1963]: 7520 could not load needed library 'libmyLib.so' for './myCommand'
(load_library[1105]: Library 'libmyLib.so' not found)CANNOT LINK EXECUTABLE
I assure libmyLib.so does exist in my project under libs/armeabi/ directory and it's copied to my Android device under /data/data/myProject.path.package/lib/ directory. Owner and group of both executable and library are system:system and permissions are ok as well.
When executing the command from adb shell in the beginning I get the same error but then I can set LD_LIBRARY_PATH and it runs ok:
./adb shell
export LD_LIBRARY_PATH=/data/data/myProject.path.package/lib:$LD_LIBRARY_PATH
/data/data/myProject.path.package/myCommand
So the question is, how to do that from java Android project?
Note: I'm using Linux, Eclipse + Sequoyah, NDK-r5b, Android 2.3.6 (API 10) on GT-P1010.
Solved: you can use exec(String prog, String[] envp). In my case this was:
String[] envp = {"LD_LIBRARY_PATH=/data/data/cse.ecg.dcmtk/lib:$LD_LIBRARY_PATH"};
Runtime.getRuntime().exec(myCommand, envp);
The issue now is that with exec() the process executed can't resolve hostnames (it does from adb shell; Internet permission is set). Any hint about that?