3

I want to build project in Android Studio 3.1.3 on MacOS High Sierra 10.13 from terminal with command:

./gradlew clean build

output: bash:\r: No such file or directory.

or

/gradlew clean build

bash:No such file or directory.

or

gradlew clean build

bash: gradlew command not found.

How can I execute build from command line ? I have looked on stackoverflow answers, but they didn't help me. Thanks.

enter image description here enter image description here

yozhik
  • 3,276
  • 13
  • 50
  • 88

2 Answers2

6

I suppose you have imported the project from a Windows machine and now it contains some carriage returns '\r'.

try to remove carriage returns by executing following commands in your terminal:

brew install dos2unix # Installs dos2unix Mac
find . -type f -exec dos2unix {} \; # recursively removes windows related stuff

after that you can use gradle wrapper as usual:

./gradlew clean build
Andre
  • 335
  • 2
  • 10
  • Yes, I moved this project from Windows machine. And yes, your script deleted some files (a lot of files). But still, after execution of your script - I have the same issue on executing ./gradlew clean build (and other variants as well). – yozhik Aug 06 '18 at 12:41
  • it should not have deleted files, but only the carriage characters generated by windows toolchain. which files got removed? – Andre Aug 06 '18 at 13:06
  • you could also try to reinstall gradle wrapper (gradlew) by executing following code: `gradle wrapper` if you dont have gradle installed on your macos you can install it by `brew install gradle` – Andre Aug 06 '18 at 13:12
  • I spent two days trying to figure this out. Thank you so very much. – SaundersB Jun 22 '19 at 14:14
  • I have the same problem, but I did not import the project from Windows. I just started a new project in Android studio with Kotlin?! – Hillcow Mar 02 '20 at 14:59
  • Thank you! Helped a lot. Took about 40 mins. And I see some changes in VCS, but if I look inside, it doesn't show which lines, just some changes; – Akbolat SSS Mar 25 '20 at 06:11
2

Your problem is incorrectly cloned and converted project - see here: env: bash\r: No such file or directory

  • `ls -lah` - this will list all the files in the current directory, you can also type `pwd` to check what directory you're in – Krzysztof Kubicki Aug 06 '18 at 12:39
  • pwd returned: /Users/myUserName/Documents/myProjectFolderName – yozhik Aug 06 '18 at 12:42
  • so do you have the `gradlew` file? what does `ls -lah` return? – Krzysztof Kubicki Aug 06 '18 at 12:43
  • ls -lah returned: (.gradle, .git, .gitignore, .build.gradle, buildsystem, gradlew, gradle, gradle.properties, .idea, gradlew.bat, settings.gradle, local.properties, data, domain, presentation) – yozhik Aug 06 '18 at 12:46
  • 1
    does it have something like this: `-rwxr-xr-x` – Krzysztof Kubicki Aug 06 '18 at 12:48
  • try `sudo ./gradlew clean install` – Krzysztof Kubicki Aug 06 '18 at 12:48
  • sudo ./gradlew clean install asked me for password and after that wrote that: No such file or directory – yozhik Aug 06 '18 at 12:53
  • This is super weird as the file exist in the directory you're in, it is executable and it seems that you have the permission to execute it. You might be missing some lib. Try reading this : https://askubuntu.com/questions/133389/no-such-file-or-directory-but-the-file-exists. Do you have Android Studio installed OR android SDK? – Krzysztof Kubicki Aug 06 '18 at 13:09
  • I have added photos – yozhik Aug 06 '18 at 13:15
  • Yes, Android studio and SDK installed. The project is successfully builded from Android studio UI buttons. But it don't want to build from command line. – yozhik Aug 06 '18 at 13:17
  • This is your problem: https://stackoverflow.com/questions/29045140/env-bash-r-no-such-file-or-directory – Krzysztof Kubicki Aug 06 '18 at 13:17
  • I was working on Windows and my college on Ubuntu. We both use the same git. But he does not have such problems with command line as I have on Mac. – yozhik Aug 06 '18 at 13:37
  • What do you mean you use the same git? When you clone a project you can "tell" git to convert all windows-like line endings to UNIX-like. Generally it's a good practice to keep them Unix-like on git repository, and (if you work on Windows) convert them to Windows, then when you commit/push your changes convert them back to UNIX. So it all depends on how you colleague configured his git client. Has the solution I gave you worked for you? – Krzysztof Kubicki Aug 06 '18 at 13:40
  • Can you please help me to modify this command from that link you gave me? sed $'s/\r$//' ./install.sh > ./install.Unix.sh, because I wrote like this, and it's not working: sed $'s/\r$//' ./gradlew > ./gradlew.Unix – yozhik Aug 06 '18 at 14:00
  • 1
    I think you're missing `sh` -> `sed $'s/\r$//' ./gradlew.sh > ./gradlew.Unix.sh` but this solution isn't pretty. Try setting up proper line endings and cloning the repo again. I think you need to call `git config --global core.autocrlf true` and the clone the repo again. If that doesn't work try with `false` ;/ This will create a new file. check with `ls- lah` if this new file has `-rwxr-xr-x` those x-es mean you can execute this file. – Krzysztof Kubicki Aug 06 '18 at 14:06
  • I will try to do this with my git, because this command also failed. And I have tried and created new sample android project and tried to run gradlew command - and it works there good: ./gradlew clean build. So it's the issue in myProject structure of files... – yozhik Aug 06 '18 at 14:28
  • 1
    If you're on Mac install Sourcetree and clone the repo with Sourcetree. This should be much cleaner. It should ask about the line endings while cloning. Good luck. – Krzysztof Kubicki Aug 06 '18 at 14:30
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/20510011) – SRack Aug 06 '18 at 14:35
  • 1
    Unfortunately this answer about missing gradlew is absolutely wrong and probably misleading for the author. Since the author mentioned that `./gradlew clean build` had a bash error logging, this means that the gradlew is already there and its executable (otherwise what would have started bash?). Just open the gradlew file with some text editor (or even Android Studio) and you will see that gradlew is just a bash script. @SRack this answers the answer of the author pretty good. Since the actual issue is wrongly imported project with some windows characters. Proper import would fix it. – Andre Aug 06 '18 at 14:55
  • I changed git config --global core.autocrlf true -- and downloaded project again from git and now it works good. – yozhik Aug 08 '18 at 09:33