Configuring Debian Stretch for Android Development

Configuring Debian Stretch for Android Development

When it comes to operating systems, I’m consistently amazed at how bad newer versions of both Windows and macOS/osX are. Ultimately, both Microsoft and Apple can really afford to be as bad as they want, because at the end of the day, what can you do? Are you going to stop what you’re doing and invest several years of your life into learning how to use Linux?

I did, and it’s been one of the best decisions that I’ve ever made. My OS is built entirely by other people like myself who are passionate about open source software, it costs me zero dollars to upgrade and I never have any real problems with it.
On top of that, the Portable Operating System Interface (POSIX) guarantees that time invested in learning how to use Linux will never be wasted time. A Linux command line is for the most part a Linux command line.

I want to make it easier for everyone to use GNU/Linux and break free of bad OSs. In reality, most people rely on Windows to do their job, and this isn’t a realistic option. For aspiring Android developers, or Android developers in general, this is not the case. An Android developer would actually be able to do slightly more with Debian, and on a far less expensive machine. Learning how Linux works teaches you a lot about how Android works, as there’s a powerful synergy between the two operating systems.

I’ve personally been focused on developing Android apps for the last two years, and in my opinion, for Android development Debian works better than OSX or Windows. Any Android developer can use Debian for every step of Android development, generally with less problems than they would face if they were using either macOS or Windows.

Debian is legendary for it’s stability, and it turns out that it’s actually relatively easy for just about anyone to use. On the flip-side, installing Debian and getting it working is difficult enough to prevent a lot of people from using it.

If you follow these instructions you’ll end up with a fully working Debian system with
i3 Dynamic Window Manager(DWM),
openjdk-7, openjdk-8,
Android Studio,
gradle,
fastboot,
adb,
Chromium,
vim,
xfe,
libreoffice and inkscape.

Minimum Requirements

  1. PC laptop or desktop
  2. At least 4GB of RAM (If you’re interested in Android Development, otherwise any RAM is enough RAM)
  3. A hard drive (HDD) or solid state drive (SSD) with at least 100GB of storage.
  4. An Ethernet cable with an Internet connection

Preferred Requirements

If you want the easiest possible install with the best possible results for the lowest price:

  1. Lenovo ThinkPad model T400, T500 or X220.
  2. 8GB or 16GB RAM memory. More is better.
  3. 100 GB or bigger SSD.
  4. An Ethernet cable with an Internet connection

Step-by-Step HOWTO

1. Insert a Debian Stretch USB installer into an open USB port on your PC.
2. Setup a minimum Debian system.
3. Configure your minimum Debian install for Android development.

Boot your PC and log in.

localhost login: developer
Password: _

There won’t be any visible change as you type in your password.
Hit Enter.
And check out your new prompt:

~$

At this point, we have a very minimalist Debian setup.
This is ideal.
There is nothing that we don’t want, and we only need to install packages that we want.
Debian apps are called packages. Debian gets packages from online repositories and offline repositories that come in sets of DVDs, BluRay disks or CDs. You don’t need an Internet connection to install Debian, and in fact Debian’s Securing Debian Manual explains that Debian is not safe to connect to the Internet until after installation is complete. I’ve read a lot of technical blogs that explain how to install Debian, and I’ve never seen this detail about installing Debian offline mentioned in any of them. Debian needs to be installed offline, configured offline and then rebooted and connected to the Internet when it’s able to better defend itself.

I mentioned that this is a minimalist Debian install, and that means we don’t have sudo installed yet, so in order to make changes and install packages you’ll need to become root.

$ su
  Password: _  

Enter your root user (superuser = su) password and hit Enter.

root@localhost:/home/developer# _

Notice the change, you’re presented with the root prompt ‘#’ instead of the normal user prompt ‘$’. We need to install a bunch of packages before we can safely connect to the Internet, so we’ll mount the USB installer and use apt-cdrom. Since we’re not using a physical Blu-ray disk there is the issue of mounting the USB drive so that it works like a Blu-ray disk.

First we need to make a new directory

# mkdir /media/usb0

Next we need to determine the path to your USB installer.

# fdisk -l

You will get a response, and the information on the left-hand side will look something like this:

Device
/dev/sda1 /dev/sda2
/dev/sda5

Device
/dev/sdb1

The second device is the USB installer.
In my case, the path listed /dev/sdb1
Will need to be mounted as /dev/sdb

You will generally get a path like one of these:
/dev/sdx1

/dev/mmcblk0p1

These paths would be mounted as:
/dev/sdx

/dev/mmcblk0

We need to mount the USB drive (in my case dev/sdb) as a read-only loop device (-o loop,ro) at /media/usb0

# mount -o loop,ro /dev/sdb /media/usb0

Next we need to tell apt-cdrom not to mount anything (-m) and to use the CD-ROM mount point(-d) of /media/usb0 and add a CD-ROM.

# apt-cdrom -m -d=/media/usb0 add

Now we need to use vi to edit /etc/apt/sources.list

# vi /etc/apt/sources.list

There will be a decent number of lines, which you need to delete all of.
Hit the Escape key and then hit the ‘d’ key repeatedly until every line has been deleted.
Next, hit the Escape key and then hit the ‘i’ key once.
Now type in the following, and make it look exactly like this:

deb [trusted=yes] file:/media/usb0 stretch  main  

If you make a mistake, hit the Escape key, and then use ‘x’ to delete the typo.
Hit the Escape key and then ‘i’, and then type in the correct letters.
When you have the line exactly, hit the Escape key, then type in the following:

:wq  

And hit the Enter key.
Now update APT’s package list to reflect all of the packages available via the USB installer, following any instructions given:

# apt update  

Then upgrade the packages

# apt upgrade  

To install packages vim and curl:

  # apt install sudo vim curl

Add your user ‘developer’ to group sudo:

# adduser developer sudo  

Next, install ufw (Universal Firewall) so that we can connect to the Internet without being immediately compromised:

# apt update  
# apt install ufw  

Configure ufw:

# ufw enable

Unmount the USB installer with ‘umount’:

# umount /dev/sdb /media/usb0

After unmounting, ‘sync’ to flush any unwritten changes:

# sync /dev/sdb /media/usb0

Make sure that the sync is complete, then remove the USB installer from the USB port.
You won’t need the USB installer again for this project.

ufw won’t work until you reboot, so reboot:

# reboot

Login. Check to make sure that ufw is active and set correctly:

$ sudo ufw status verbose

If ufw is set correctly, it should read:

Status: active Logging: on (low) Default: deny (incoming), allow (outgoing) New profiles: skip

Before we can take full advantage of the Internet and install the rest of the packages that we need to install, we need to reconfigure our sources.list file. This will require vim, which is easy to use and extremely useful.

vimtutor

vim comes with its own interactive tutorial, so learning how to use it is a straightforward process that doesn’t take a lot of time.

$ vimtutor

Work through the interactive tutorial, then configure your sources.list file.

If you want to shortcut this vim editing challenge of mine, use curl to download the already completed sources.list file from my GitHub:

$ curl -o sources.list https://raw.githubusercontent.com/andDevW/debiandroid/master/CONFIGURATION_FILES/SOURCES/MAIN/sources.list

then move the file you just downloaded to /etc/apt/sources.list

$ sudo mv ~/home/developer/sources.list /etc/apt/sources.list

If you don’t live in the United States of America you might want to change the country to a country that’s closer to where you’re located.
US deb http://ftp.us.debian.org/debian/ jessie-backports main
IN deb http://ftp.in.debian.org/debian/ jessie-backports main
DE deb http://ftp.de.debian.org/debian/ jessie-backports main
XY deb http://ftp.xy.debian.org/debian/ jessie-backports main

The following explains how to use the ‘#’ to either enable or disable a line:

ENABLED

deb http://ftp.us.debian.org/debian/ jessie-backports main

DISABLED (commented out)

#deb http://ftp.us.debian.org/debian/ jessie-backports main

You’ll need to comment out the line that we edited earlier, so that apt will only check online repositories, and not the local USB installer. Here’s what your sources.list needs to look like:

# /etc/apt/sources.list

#stretch apt-cdrom via usb0
#deb [trusted=yes] file:/media/usb0 stretch  main  

#stretch security updates main
deb http://security.debian.org/ stretch/updates main
deb-src http://security.debian.org/ stretch/updates main

#stretch main
deb http://ftp.us.debian.org/debian/ stretch main
deb-src http://ftp.us.debian.org/debian/ stretch main

#jessie-backports main
deb http://ftp.us.debian.org/debian/ stretch-backports  main
deb-src http://ftp.us.debian.org/debian/ stretch-backports main

Ignore the top three lines that are commented out, and look at the rest of the file.
Notice that the sources.list file has three different repositories, with two lines each.
The first line begins with deb …
The second line begins with deb-src …
Then everything that comes after that prefix is exactly the same.

An easy way to enter these lines into your sources.list is to only type out the first lines starting with ‘deb’.
Next hit the Escape key, then move the cursor to the line you want to copy and hit the ‘Y’ key to copy the line.
Hit the Escape key and then hit the ‘P’ key to paste.
Vi will copy the first line onto the line below it, so now you only need to insert ‘-src’ after ‘deb’.

Before updating apt we might as well add the i386 architecture that Android Studio requires:

$ sudo dpkg –add-architecture i386

Now to update and upgrade apt:

$ sudo apt update && sudo apt upgrade

**If you make any mistakes, apt will inform you of where they were made. One way to make identifying the mistake much easier is to set vi to show number lines by default. Here’s how:

$ echo “set number” >> ~/.vimrc

This is a method of creating new files and amending existing files, if you were to issue the above command two times your ~/.vimrc file would look like this:

set number
  set number

Here is how issuing the command works:
echo “YOUR TEXT” » YOUR_FILENAME

As apt updates you will be presented with messages that you need to scan through and ultimately hit the Enter key to proceed. At the end of the message you will have to enter ‘q’ to quit. Now your Debian OS is up-to-date.
Right now you don’t have Xorg installed, so you don’t have the ability to display graphics (at least not graphics as we know them now), and as a result you won’t be able to run Android Studio or the Chromium browser. For Android development we need to have graphics, and we also need easy access to a terminal window, and on top of that, we want to keep everything as lightweight as possible with regards to resource
need to install a Dynamic Window Manager (DWM), and since i3 works beautifully with Android Studio right out of the box, we’re going to use i3.

These commands are available via GitHub, to simplify things: https://raw.githubusercontent.com/andDevW/debiandroid/master/resources/commands.txt

$ sudo apt install xorg openjdk-7-jdk i3 xfe chromium android-tools-adb android-tools-fastboot gradle git git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib  libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev ccache libgl1-mesa-dev  libxml2-utils xsltproc unzip inkscape

You’ll get a prompt asking you for a Y/n
The uppercase Y means the default is YES, so hit the Enter key.

When the installation is complete you’ll need to reboot to get everything working:

$ sudo reboot

Now, log in and launch Xserver:

$ startx

i3 will prompt you to configure the ~/.i3/config, so follow the prompt or just hit Enter.
Now you should be looking at a new type of homescreen, and this is i3. i3 is in my opinion the best DWM that there is, because it will allow you to rely almost entirely on the keyboard for navigation that would generally require a mouse or touchpad. This ends up being a massive improvement, and saving a ton of time.

Here is a basic i3 tutorial, to show you how to get around in i3: Your mod key (Mod) will generally be the Windows key on your keyboard. Hit Mod + Enter and open a new terminal window.
Hit Mod + Enter and open another terminal window. Hit Mod + Enter and open another terminal window. Hit Mod + Enter and open one last terminal window.
Now hold Mod and hit the left arrow key <– While holding Mod hit the left arrow key again. While still holding Mod hit the right arrow key –> While holding Mod hit the left arrow key again. Now hit Mod + Shift + ‘Q’ and close the window that you have selected.

Now hit Mod + ‘E’. Hit Mod + ‘E’ again.

Hit Mod + Shift + ‘E’ to Exit i3.

In i3, start the Chromium browser using the command line:

$ chromium

But there is a shortcut that starts Chromium without taking up a terminal window in the process. Hold Mod(Windows key) and press ‘D’ , bringing up XFE.
Now type in the word chromium
When you see chromium selected hit the Enter key.

Now, inside Chromium navigate to:
https://developer.android.com/studio/index.html#downloads
Download Android Studio for Linux, or:

$ cd ~/Downloads  
$ wget https://dl.google.com/dl/android/studio/ide-zips/2.3.3.0/android-studio-ide-162.4069837-linux.zip

Then verify the integrity of the download:

$ echo “1383cfd47441e5f820b6257a1bdd683e0e980bc76c7f2027ef84dc2e6ad2f17f android-studio-ide-162.4069837-linux.zip” | sha256sum -c

If the file downloaded correctly it will say
android-studio-ide-162.4069837-linux.zip:OK Now change into your home directory and unzip the file that you just downloaded:

$ cd ~
$ unzip ~/Downloads/android-studio-ide-162.4069837-linux.zip

When the file is finished unzipping delete the zip file:

$ rm -rf ~/Downloads/android-studio-ide-162.4069837-linux.zip

Now you’ll need to install openjdk-8-jdk:

 $ sudo apt install openjdk-8-jdk

Next, you’ll need to configure the JAVA_HOME and ANDROID_HOME environments in your ~/.bashrc
*IT’S IMPORTANT TO MAKE SURE THAT YOU ARE NOT ROOT WHEN EDITING .bashrc

$ vi ~/.bashrc

Next hit ESC then SHIFT + ‘G’ to jump to the bottom of the file.
Hit ESC, and then SHIFT + ‘O’ to skip to a new line.
Add the following lines, substituting your USERNAME for ‘developer’ if you didn’t use ‘developer’ as your USERNAME.
Some of which are commented out, but will be useful for building AOSP.
ADD THESE LINES:

#Configure the JAVA_HOME environment
#Configuration for openjdk-7-jdk
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64

#Configuration for openjdk-8-jdk:
#export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64

#Configure the ANDROID_HOME environment  
export ANDROID_HOME=/home/developer/Android/Sdk
export PATH=$PATH:/home/developer/Android/Sdk/platform-tools
export PATH=$ANDROID_HOME/tools:$PATH/

If you want to skip manually configuring your bashrc file, use curl to download the complete file from GitHub:

  $ curl -o ~/.bashrc https://raw.githubusercontent.com/andDevW/debiandroid/master/CONFIGURATION_FILES/.bashrc

Now source .bashrc so that the changes you made will take effect:

    $ source ~/.bashrc or
    $ . ~/.bashrc

You will quickly know that your .bashrc file either works or doesn’t work.
If it worked your command prompt will now be colored.
If your .bashrc file doesn’t work you won’t see a colored command line prompt, and you might get an error about where it’s broken.

Now create a symbolic link to the android studio script. This will allow you to launch Android Studio from dmenu by selecting ‘astudio’ :

$ sudo ln -s  ~/android-studio/bin/studio.sh /usr/local/bin/astudio

Now select astudio in XFE, or run the script manually:

$ cd ~/android-studio/bin/
$ ./studio.sh

In the Android Studio Wizard select Next, then use the default configuration and wait for everything to download, and then hit the Finish button. Now you have a fully working installation of Android Studio v2.3.3 on a fully working Debian Stretch OS.
Remember to update your system by running the command

$ sudo apt update && sudo apt upgrade

Enjoy Android development with GNU/Linux!