Sunday, April 19, 2015

using curl and awk to get the latest android sdk

Here is another of those short posts for me to remember how I did something. So I want to retrieve the latest version of the Android SDK. The reason is not relevant to this article, but since I need something to fill it up and make it sound more important than it is, I will tell you: I want to build a docker container to build Android packages and want to make sure the Dockerfile will grab the latest SDK.

As some of you know, the SDK is available at https://developer.android.com/sdk/index.htm. Since I want to automate this, I would love to be able to have the url for the latest release without having to know which one is that. Asterisk, for instance, allows you to grab the -latest one which would as the name says get the latest asterisk file without any thinking required. If you look at the Android SDK page, you will know what is the latest version number and can get it by clicking on it, but doing it command line seems to be not as fun as getting the -latest one.

One of my objectives is to get the file using curl instead of wget. don't get me wrong, I like and use wget a lot. And, try to forget about the usual wget vs curl wars; the reason is much more pedestrian: OSX comes with curl. Since I wanted to run this not only on Linux but also on OSX without having to use brew or macports, curl it is.

Let's see if we can make a quick script to do the deed. I was going to see if I could come up with a clever script, but someone cut me to it. I really like how they used gsub, so I just had to see if I could apply that idea. Here is my first attempt

#!/bin/sh
# getdroid.sh

site="https://developer.android.com/sdk/index.html"
os="linux"

filename=`curl -s --insecure ${site} | \
awk '/id=\"'${os}'-tools\"/{
    gsub(/.*href=\"/,"")
    gsub(/\">.*/,"");
    if ($0) print
}'`

curl -O ${filename}

The short version of what I am doing:

  1. for now we only care about a linux SDK, hence the os="linux". That said, you can see i am leaving it open to look for the SDK for another OS; you just need to know which pattern to look for.
  2. Get a copy of the page source.
  3. Look for the line that contains the link to the linux package.
  4. Once that is found, grab the link and fetch the file.

The awk line looks rather busy because the search (the id= thingie) part of the statement escapes out to grab ${os}. You could make it easier to understand by using the normal garden-veriety search -- grep -- command to get the line that matches the id="linux-tools pattern, which then is piped into awk:

#!/bin/sh
# getdroid.sh

site="https://developer.android.com/sdk/index.html"
os="linux"

filename=`curl -s --insecure ${site} | \
grep "id=\"${os}-tools" | \
awk '{
    gsub(/.*href=\"/,"")
    gsub(/\">.*/,"");
    if ($0) print
}'`

curl -O ${filename}

Of course, as I mentioned I want to also use the same script on OSX; as it it, it will run on a Mac, but I will leave fetching the OSX version of the SDK as an exercise to the reader.

How about the NDK? It is found at https://developer.android.com/tools/sdk/ndk/index.html. You can do the same thing as I did above, but have to account for having a 32 and a 64bit version; just provide that to the script or make it aware of the OS you are running. Personally I would not do the later because I might want to, say, download the Windows 64bit files from a Mac. I will post my final script on github as soon as I have time; for now, what I put here should be enough to get you started.

1 comment:

Dalek said...

Corrected the script; missed two '. While I was there I added another version.