Rationale

Each revision of Android is meant to be compiled with a specific set of dependencies. No effort is made upstream to support multiple build environments.
Building with different environments often yields build errors due to untested dependencies versions.
So we document here how to recreate these recommended build environments.

Recommended environment history

https://source.android.com/setup/build/initializing#setting-up-a-linux-build-environment
https://source.android.com/setup/build/older-versions#jdk-linux

AOSP also provides a Dockerfile now:
https://android.googlesource.com/platform/build/+/master/tools/docker

Ubuntu support:

          ,2011-02-07 - Dapper  (06.06) Up to Froyo (included), preferrably 32-bit - <https://web.archive.org/web/20100523002816/http://source.android.com/source/download.html>
2011-02-07,2013-10-15 - Lucid   (10.04) Gingerbread-? - <https://web.archive.org/web/20110207230238/http://source.android.com:80/source/download.html>
2013-10-15,2015-03-16 - Precise (12.04) probably with 4.4 KitKat - https://web.archive.org/web/20131015123913/http://source.android.com/source/initializing.html
2015-03-16,           - Trusty  (14.04) https://web.archive.org/web/20150316053136/https://source.android.com/source/initializing.html

Java support:

          ,2011-02-07 - Sun JDK 5 - Up to Froyo (included), "JDK 5.0, update 12 or higher" - <https://web.archive.org/web/20100523002816/http://source.android.com/source/download.html>
2011-02-07,2014-03-31 - Sun JDK 6 - Gingerbread-? - <https://web.archive.org/web/20110207230238/http://source.android.com:80/source/download.html>
2014-03-31,2016-02-12 - OpenJDK 7 - https://web.archive.org/web/20140331004436/https://source.android.com/source/initializing.html
2016-02-12,           - OpenJDK 8 - https://web.archive.org/web/20160212195103/http://source.android.com/source/initializing.html

Ubuntu 14.04

With LXC:

lxc-create -n android-14.04 -t download -- -d ubuntu -r trusty -a amd64

lxc-start -n android-14.04 -d
lxc-attach -n android-14.04

# clean-up non-free sources
sed -i -e 's/ restricted//' -e 's/ multiverse//' /etc/apt/sources.list
apt-get update

With Docker: see https://hub.docker.com/r/library/ubuntu/tags/ for date-frozen ubuntu:trusty-20YYMMDD snapshots (rather than 14.04 which changes over time)

docker run --name tmp -ti ubuntu:trusty-20170330 bash -c 'sed -i -e "s/ restricted//" /etc/apt/sources.list && apt-get update'
docker commit tmp ubuntu:trusty-20170330

Ubuntu 10.04

With LXC:

apt-get install ubuntu-archive-keyring rsync
lxc-create -n android-10.04 -t ubuntu -- -r lucid -a amd64

lxc-start -n android-10.04 -d
lxc-attach -n android-10.04

# clean-up non-free sources
sed -i -e 's/ restricted//' -e 's/ multiverse//' /etc/apt/sources.list
apt-get update

You'll need git > 1.7.2 for repo:

cd /usr/src/
apt-get install wget gcc libssl-dev zlib1g-dev libcurl4-gnutls-dev libexpat-dev gettext
wget https://www.kernel.org/pub/software/scm/git/git-2.4.6.tar.gz
tar xf git-2.4.6.tar.gz 
cd git-2.4.6/
make -j4  # ~2mn
make install prefix=/usr/local

If network access is necessary:

# 'lxcguest' fixes a number of issues but seem to disable init..
dhclient
/etc/init.d/ssh restart

Ubuntu 8.04

For compilation only (repo's dependencies, as well as 2019's SSL/TLS world, require a more recent version).

With Docker:

# Get GPG keyring from http://old-releases.ubuntu.com/ubuntu/pool/main/u/ubuntu-keyring/ubuntu-keyring_2008.03.04.tar.gz / ubuntu-keyring-2008.03.04/keyrings/ubuntu-archive-keyring.gpg
debootstrap --arch i386 --keyring ./ubuntu-archive-keyring-2008.03.04.gpg hardy 8.04/ http://old-releases.ubuntu.com/ubuntu/
tar -czf 8.04-template.tar.gz -C 8.04/ .
rm -rf 8.04-template/
docker image import 8.04-template.tar.gz ubuntu:8.04

Post-install

Some shell scripts assume /bin/sh is bash:

echo 'dash dash/sh boolean false' | debconf-set-selections
dpkg-reconfigure --pri=high dash

Let's avoid Recommended packages for better dependencies control:

echo 'APT::Install-Recommends "false";' > /etc/apt/apt.conf.d/00InstallRecommends