4

I have android sdk installed, and I want the binaries in build-tools/android-VERSION to be available in PATH, so I add few lines:

ANDROID_SDK=/Application/Binaries/adt-bundle-linux-x86_64-20140321/sdk/
ANDROID_PATH=$ANDROID_SDK/tools:$ANDROID_SDK/platform-tools:$ANDROID_SDK/build-tools/android-4.4.2/

Here's the problem, I might update the SDK at any time, so build-tools/android-4.4.2/ might change upon that. If so I would have to edit the file again and update the android-version part in $PATH

So I changed the profile to something like this,

ANDROID_SDK=/Application/Binaries/adt-bundle-linux-x86_64-20140321/sdk/
ANDROID_PATH=$ANDROID_SDK/tools:$ANDROID_SDK/platform-tools
for x in $ANDROID_SDK/build-tools/android-*/; do
    export PATH=$PATH:$x
done

That looks dumb, any better way to write it?

daisy
  • 54,555

3 Answers3

3

The ideal way is creating a soft link directory called adt, and re-creating it after SDK changes.

Thus, your PATH will stay the same.

Deer Hunter
  • 1,866
0
PATH="$PATH:$(printf '%s:' $ANDROID_SDK/build-tools/android-[0-9.]*/)"

Should work.

cd ~ ; mkdir dir1 dir2 dir3
( PATH=$(printf %s: $HOME/dir[12])
echo "$PATH" )

/home/mikeserv/dir1:/home/mikeserv/dir2:

Another method:

PATH="$PATH:$( set -- `printf '%s\n' "$ANDROID_SDK"*/android[0-9.]*/ |
    sort -rV` ; [ -d "$1" ] && echo "$1" )"

That takes advantage of sort's -Version handling, which should ensure that the positional parameter "$1" is always set to the latest version of your Android SDK. For example:

WITHOUT SORT:

% printf %s\\n ~/dir[0-9]*
/home/mikeserv/dir1
/home/mikeserv/dir2
/home/mikeserv/dir23
/home/mikeserv/dir3
/home/mikeserv/dir45

WITH IT:

%  printf %s\\n ~/dir[0-9]* | sort -V
/home/mikeserv/dir1
/home/mikeserv/dir2
/home/mikeserv/dir3
/home/mikeserv/dir23
/home/mikeserv/dir45

AND REVERSED:

% printf %s\\n ~/dir[0-9]* | sort -rV
/home/mikeserv/dir45
/home/mikeserv/dir23
/home/mikeserv/dir3
/home/mikeserv/dir2
/home/mikeserv/dir1
mikeserv
  • 58,310
0

In this approach, on two occasions the output of ls, sorted on time and for directories only (-td), is piped to the head command which then selects the first line. The result is that the most recent version of both adt-bundle-linux-x86_64-*/sdk and of build-tools/android-*/ should be used for the modification of the PATH environment variable.

ANDROID_SDK=$( ls -td /Application/Binaries/adt-bundle-linux-x86_64-*/sdk/ | head -n 1 )
if ! [ -z $ANDROID_SDK ] ; then
    ANDROID_PATH=$ANDROID_SDK/tools:$ANDROID_SDK/platform-tools
    ANDROID_BUILDTOOLS=$( ls -td $ANDROID_SDK/build-tools/android-*/ | head -n 1 )
    if ! [ -z $ANDROID_BUILDTOOLS ] ; then
        echo "Adding $ANDROID_BUILDTOOLS to PATH"
        export PATH=$PATH:$ANDROID_BUILDTOOLS
    else
        echo "Couldn't find build tools"
    fi
else
    echo "Couldn't find SDK"
fi
brm
  • 1,021