1

I am working on android open source project. I have downloaded the latest release from android.googlesource.com.

Now, the git repository synching is complete. But, when I try to setup the environment for compiling android source code,the command: source 'build/envsetup.sh' is throwing this error:

bash: build/envsetup.sh: line 1: syntax error near unexpected token $'{\r'' 'ash: build/envsetup.sh: line 1:function hmm() {

I have tried online solutions available but no success. If anyone knows how to fix this error, that would be helpful.

Asad Ali
  • 454
  • 2
  • 13
  • 1
    I have tried to follow the guidelines in your link. I hope it will help now to understand the problem. – Asad Ali Feb 02 '15 at 20:31
  • 1
    looks like MS WIndows was involved creating file that is now being used in Linux environment. (`\r`).. `dos2unix file1 file2 file3 file.... filen` to convert/fix them all at once. Good luck. – shellter Feb 03 '15 at 01:33

2 Answers2

4

EDIT: the root cause of the problem was due to Git autocrlf set to true. Under Linux, it should be set to input:

git config --global core.autocrlf input

(original answer)

The problem comes from Windows end-of-lines (EOL) so you'll have to convert all scripts to unix-style EOL through dos2unix (run apt-get install dos2unix on Ubuntu) and then convert your scripts:

dos2unix build/envsetup.sh sdk/bash_completion/adb.bash

Then all vendorsetup.sh (that will prevent the "command not found" error you get):

find device/ -name vendorsetup.sh -exec dos2unix {} \;

And one last to run the choosecombo script:

dos2unix build/core/find-jdk-tools-jar.sh

EDIT: In order to finish the overall compilation, the exhaustive conversion:

find . -name '*.sh' -exec dos2unix {} \;
find . -name '*.py' -exec dos2unix {} \;
find . -name '*.c' -exec dos2unix {} \;
find . -name '*.h' -exec dos2unix {} \;
find . -name '*.cpp' -exec dos2unix {} \;
find . -name '*.hpp' -exec dos2unix {} \;
find . -name '*.txt' -exec dos2unix {} \;
find . -name 'Config.in' -exec dos2unix {} \;
find . -name 'Config.src' -exec dos2unix {} \;
find . -name 'Makefile' -exec dos2unix {} \;
find . -name 'mkmakefile' -exec dos2unix {} \;
find . -name 'Kconfig*' -exec dos2unix {} \;
find . -name rmtypedefs -exec dos2unix {} \;
find . -name apicheck -exec dos2unix {} \;
find . -name seapp_contexts -exec dos2unix {} \;
dos2unix external/busybox/scripts/* external/busybox/applets/* kernel/scripts/* dalvik/dx/etc/* prebuilts/sdk/tools/*

The *.sh for all shell scripts, and *.py for all python scripts (used during make compilation), as well as .c and .cpp file (obviously) and other files used by makefiles.

Of course you could go the over-overkill find . -type f -exec dos2unix -s -k -o {} \; and let dos2unix decide which files are text and which are binary.

There might be other. I'll edit this answer as I find new ones...

Matthieu
  • 2,736
  • 4
  • 57
  • 87
0

Easy way to convert example.sh file to unix is use NotePad++ (Edit>EOL Conversion>UNIX/OSX Format)

You can also set the default EOL in notepad++ (Settings>Preferences>New Document/Default Directory>select Unix/OSX under the Format box)

Reference- syntax error near unexpected token `$'in\r''

It also solves this problem: android_build.sh gives error while building ffmpeg library

I used this one. It solves my problem earlier.

Community
  • 1
  • 1
Bee
  • 104
  • 1
  • 13