To build Android version earlier than Lollipop from source code, you need the Sun's 1.6 SDK to be installed for ubuntu as the link Initializing a Build Environment | Android Developers.
You may still cannot get the Sun's JDK with below instruction:
$ sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner"
$ sudo apt-get update
$ sudo apt-get install sun-java6-jdk
There are below options to help install the Sun's JDK if you cannot find a valid source through apt-get commands:
$ wget --no-cookies --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F" http://download.oracle.com/otn-pub/java/jdk/6u45-b06/jdk-6u45-linux-x64.bin
$ chmod u+x jdk-6u45-linux-x64.bin
$ ./jdk-6u45-linux-x64.bin
$ sudo mv jdk1.6.0_45 /opt
$ sudo update-alternatives --install /usr/bin/java java /opt/java/64/jdk1.6.0_45/bin/java 1
$ sudo update-alternatives --install /usr/bin/javac javac /opt/java/64/jdk1.6.0_45/bin/javac 1
$ sudo update-alternatives --install /usr/bin/jar jar /opt/java/64/jdk1.6.0_45/bin/jar 1
# if you have already install some other version of JDK, please export the JAVA_HOME env before your android build every time
$ export JAVA_HOME=/opt/jdk1.6.0_45/
#or you can directly link the java binary to the sdk version you need as below:
sudo ln -s /opt/java/64/jdk1.6.0_45/bin/jar /bin/jar
sudo ln -s/opt/java/64/jdk1.6.0_45/java /bin/java
sudo ln -s/opt/java/64/jdk1.6.0_45/javac /bin/javac
sudo ln -s/opt/java/64/jdk1.6.0_45/javah /bin/javah
sudo ln -s/opt/java/64/jdk1.6.0_45/javadoc /bin/javadoc
sudo ln -s/opt/java/64/jdk1.6.0_45/javaws /bin/javaws
To built the Android version Lollipop and Marshmallow from source code, you need the OpenJDK 7 to be installed for ubuntu as the link Initializing a Build Environment | Android Developers.
$ sudo apt-get update
$ sudo apt-get install openjdk-7-jdk
You may have both openjdk7 and SUN JDK 1.6 intalled in your ubuntu to build different Android version. If you have default java SDK to be Sun's JDK 1.6, you can just use below commands to make android build system use the openjdk7 for Lollipop built
$ export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/
$ cd myandroid
$ . ./build/envsetup.sh //be sure to resetup the envsetup, and pick the platform to be built
$ lunch