[Miscellaneous Android Development] Deleted member 5186178:
First open a terminal window and type this:

sudo apt-get update

If you have installed the latest Ubuntu 12.04.5 x64 with latest HWE stack, instead of the resources listed on Android guide, use these:

sudo apt-get install git gnupg flex bison gperf build-essential zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev libx11-dev libreadline6-dev libgl1-mesa-dri-lts-trusty:i386 libgl1-mesa-dev-lts-trusty g++-multilib mingw32 tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386 lzop ccache gnupg python gcc g++ cpp

and:

sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so

Then install this:

sudo apt-get install ia32-libs

If you have installed the latest Ubuntu 14.04.1 x64, instead of the resources listed on Android guide, use these:

sudo apt-get install bison g++-multilib git gperf libxml2-utils

then

sudo apt-get install gnupg ccache lzop flex build-essential zip curl zlib1g-dev zlib1g-dev:i386 libc6-dev lib32bz2-1.0 lib32ncurses5-dev x11proto-core-dev libx11-dev:i386 libreadline6-dev:i386 lib32z1-dev libgl1-mesa-glx:i386 libgl1-mesa-dev mingw32 tofrodos python-markdown xsltproc libreadline6-dev lib32readline-gplv2-dev libncurses5-dev bzip2 libbz2-dev libbz2-1.0 libghc-bzlib-dev lib32bz2-dev squashfs-tools pngcrush pngquant schedtool dpkg-dev

and:

sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so

For 51-android.rules:

For USB rules check here to see how they look for google devices and here to see a list with vendor Ids for different brands and here for a cyanogenmod template.

MTK

- if you use a MTK device add these lines to that file too:

#MTK
SUBSYSTEM=="usb", ATTR{idVendor}=="0e8d", MODE="0666", GROUP="plugdev", OWNER="your_username"
#MTK adb and mass storage
SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", [COLOR="Red"]ATTR{idProduct}=="0c03"[/COLOR], MODE="0666", GROUP="plugdev", OWNER="your_username"

Why you need to add idProduct as well? Because idVendor "0bb4" is already used by HTC, and that will make the difference between the two.

Attached is my 51-android.rules file.

Next, download Android-sdk and unpack it in your home directory:

tar xzf $HOME/Downloads/android-sdk_[COLOR="Red"]rxx[/COLOR]-linux.tgz

where "rxx" is the release name (current is r24.0.2, but check the number when you download it, because it will be changed probably), and rename android-sdk-linux folder as android-sdk, or what name you want (keep in mind that this will be added in bashrc file in path), then in tools subfolder double click "android" script to start it (select run, not run in terminal) and install at least one android platform, to get sdk tools like adb, fastboot, etc.

For java I have my way. Because of the new instructions for openjdk in Android source, and because the new cyanogenmod 12 requires that too, I will present a new way of setting java.

Old way dual java:

MTK:

In official guide is recommended openjdk-7, but if you want to build for MTK phones and you have access to full building sources, you will notice that openjdk is not supported; in fact you will see exact this error message:

"openjdk is not supported" and "FAIL"

First uninstall in synaptic package manager the libreoffice completely, because if you don't you will not be able to uninstall openjdk (libreoffice requires java, and if you uninstall openjdk7 it will be a prompt to install openjdk6 and viceversa). This is a computer for building not office, but if you need an office solution try WPS Office for linux, or install abiword and gnumeric. After that remove completely openjdk and icedtea plugins:

sudo apt-get purge openjdk-\* icedtea-\* icedtea-6-\* icedtea-7-\*

and check if there is any trace left:

sudo dpkg --list | grep -i jdk

Next go to this page and download jdk-7u67-linux-x64.tar.gz and jdk-6u45-linux-x64.bin.

Move both in your home folder (/home/username), or use

mv $HOME/Downloads/jdk-7u67-linux-x64.tar.gz $HOME && mv $HOME/Downloads/jdk-6u45-linux-x64.bin $HOME

After that use these commands to install java:

sudo mkdir -p /opt/java

sudo tar -zxf jdk-7u67-linux-x64.tar.gz -C /opt/java

chmod u+x jdk-6u45-linux-x64.bin

./jdk-6u45-linux-x64.bin

sudo mv jdk1.6.0_45 /opt/java

sudo rm jdk-6u45-linux-x64.bin jdk-7u67-linux-x64.tar.gz

At this point java is installed in /opt/java folder, but you need to setup java alternatives.

Type these one by one:

sudo update-alternatives --install "/usr/bin/java" "java" "/opt/java/jdk1.7.0_67/bin/java" 1

sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/java/jdk1.7.0_67/bin/javac" 1

sudo update-alternatives --install "/usr/bin/javadoc" "javadoc" "/opt/java/jdk1.7.0_67/bin/javadoc" 1

sudo update-alternatives --install "/usr/bin/javah" "javah" "/opt/java/jdk1.7.0_67/bin/javah" 1

sudo update-alternatives --install "/usr/bin/javap" "javap" "/opt/java/jdk1.7.0_67/bin/javap" 1

sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/opt/java/jdk1.7.0_67/bin/javaws" 1

That will setup jdk 1.7 as your main java alternative, but if you need to work with jdk 1.6, type these too:

sudo update-alternatives --install "/usr/bin/java" "java" "/opt/java/jdk1.6.0_45/bin/java" 2

sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/java/jdk1.6.0_45/bin/javac" 2

sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/opt/java/jdk1.6.0_45/bin/javaws" 2

For building with jdk 1.6, these three are all you need.

Check again the java alternatives:

ls -la /etc/alternatives/java*

If they are not all with jdk 1.7, which will be your main java jdk (required by kitkat, or latest apktool), use these to select jdk 1.7:

sudo update-alternatives --config java

sudo update-alternatives --config javac

sudo update-alternatives --config javaws

Other thing you can do is to install java plugin for your browsers (not building related, but useful):

sudo ln -s /opt/java/jdk1.7.0_67/jre/lib/amd64/libnpjp2.so /usr/lib/mozilla/plugins/libnpjp2.so

sudo ln -s /opt/java/jdk1.7.0_67/jre/lib/amd64/libnpjp2.so /usr/lib/chromium-browser/plugins/libnpjp2.so

sudo ln -s /opt/java/jdk1.7.0_67/jre/lib/amd64/libnpjp2.so /usr/lib/opera/plugins/libnpjp2.so

That depends on what browser you have installed.

So, if you have to build with jdk 1.6, update those three alternatives and also in bashrc file comment "export JAVA_HOME" for jdk 1.7, and uncomment for jdk 1.6:

export JAVA_HOME=/opt/java/jdk1.6.0_45
[COLOR="Red"]#[/COLOR] export JAVA_HOME=/opt/java/jdk1.7.0_67

Everything else remain the same. Then reboot and you will be using jdk 1.6 for building. Same to revert.

Ok, you have dual java on your computer.

Ah, if you see in terminal an error like this:

(gedit:7129): Gtk-CRITICAL **: gtk_tree_selection_get_selected: assertion `GTK_IS_TREE_SELECTION (selection)' failed

when you close the gedit with bashrc file, then open again gedit with this command in terminal:

sudo gedit

in Edit menu, open Preferences > Plugins and deselect File Browser Panel plugin. Next time you open gedit in terminal you won't see that error anymore.

For removing these manual java installations, type these commands one by one:

sudo rm -rf /opt/java/jdk1.6.0_45
sudo rm -rf /opt/java/jdk1.7.0_67

sudo update-alternatives --remove "java" "/usr/bin/java"
sudo update-alternatives --remove "javac" "/usr/bin/javac"
sudo update-alternatives --remove "javadoc" "/usr/bin/javadoc"
sudo update-alternatives --remove "javah" "/usr/bin/javah"
sudo update-alternatives --remove "javap" "/usr/bin/javap"
sudo update-alternatives --remove "javaws" "/usr/bin/javaws"

sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config javadoc
sudo update-alternatives --config javah
sudo update-alternatives --config javap
sudo update-alternatives --config javaws

sudo rm /usr/lib/mozilla/plugins/libnpjp2.so
sudo rm /usr/lib/chromium-browser/plugins/libnpjp2.so
sudo rm /usr/lib/opera/plugins/libnpjp2.so

and delete from bashrc file

#export JAVA_HOME=/opt/java/jdk1.6.0_45
export JAVA_HOME=/opt/java/jdk1.7.0_67

Then reinstall openjdk-7 and icedtea plugin:

sudo apt-get update
sudo apt-get install openjdk-7-jdk icedtea-netx

You can use OpenJDK 7 now, if you are not building for MTK phones.

For those of you that need to use either sun jdk 1.6 or 1.7, you have to install it manually as I show above. Then, after installation, you don't have to add it to java alternatives, since the MTK building source doesn't care about your path. Instead you have to edit "mbldenv.sh" file and to replace the java path with yours.

Although, if you want you can use jdk 1.7 or 1.6 in dual setup with openjdk-7. Just install these two alternatives:

sudo update-alternatives --install "/usr/bin/java" "java" "/opt/java/jdk1.7.0_67/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/java/jdk1.7.0_67/bin/javac" 1

or

sudo update-alternatives --install "/usr/bin/java" "java" "/opt/java/jdk1.6.0_45/bin/java" 2
sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/java/jdk1.6.0_45/bin/javac" 2

and ad it to bashrc file in path:

#export JAVA_HOME=/opt/java/jdk1.7.0_67
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64

or

#export JAVA_HOME=/opt/java/jdk1.6.0_45
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64

switching between the two with these:

sudo update-alternatives --config java
sudo update-alternatives --config javac

and editing the path in bashrc, then restart the computer. Same to revert.

Next you need to add java in your path. In Android guide for setup building environment you can see how to create the bin folder for repo file. Then how to add that folder in path. We will complete that now:

sudo gedit ~/.bashrc

And at the end add these:

export PATH=~/bin:$PATH

#export JAVA_HOME=/opt/java/jdk1.7.0_67
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
export JRE_HOME=JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

export PATH=$HOME/bin:$HOME/android-sdk/platform-tools/:$HOME/android-sdk/tools:$PATH

export USE_CCACHE=1

To check if java is installed and is in path type these in terminal:

java -version
which java
echo $JAVA_HOME

You can see here also the android sdk path, which I named android-sdk/, usually is android-sdk-linux/. You need sdk for adb and fastboot to work. And, if you have only OpenJDK, remove the jdk 1.7 line.

This is a complete building environment set up.