Monday, March 30, 2015

Mastering Diff and patch

Diff and patch are essential if you are working on a large project with multiple contributors. They are a boon for programmers distributed across the globe and wanting to work on a single project.

These tools are present by default on a linux or Unix based operating system.

Diffing individual file


$diff -u originalFile modifiedFile > output.patch
This creates a file `output.patch` containing the diff of the two files, i.e. the original file and the modified file.

Diffing entire directories


$ diff -ur originalDirectory modifiedDirectory > output.patch
This creates a patch file containing all the modifications

Applying a patch to an individual file


When you say you are applying a patch to a file, you are basically applying a diff to an original file.
$ patch -p < patchFile
The diff file and the original file should be in the same directory.

Patching entire directories


cd to the parent directory of the directory to which patch has to be applied.
$patch -p1 < patchFile
The number 1 basically says that the number of directories to be ignored while applying the patch is 1.

Sunday, March 29, 2015

Archives using TAR

If you are running a Linux Distribution, a Mac OS X or other Unix-like operating system, your system certainly has the tar program on it already.You can see the tar manual by typing '$man tar'.

Unpacking a tarball


To unpack a tarball, use
$ tar xvf <tarball>

The x stands for extract.
The v stands for verbose output
The f stands for filename of archive

To uncompress a gzip compressed tar, use option z in addition to the above
$ tar xvzf <tarball>

The z stands for uncompress a gzip archive

To uncompress a bzip2 compressed tar, use option j instead of z
$ tar xvjf <tarball>

Creating a tarball


To create a tar archive,use
$ tar cvf <tarball name> <file/directory to be archived>

The c stands for create archive
The v stands for verbose list files which have been processed
The f stands for filename of archive

To create a compressed archive, use
$ tar cvzf <tarball name> <file/directory to be archived>

This creates a gzip compressed archive

In order to create a bzip2 compressed archive, use option j.
$ tar cvjf <tarball name> <file/directory to be archived>

In order to add a file or directory to an existing archive, use the following:
$ tar rvf <tarball name> <file/directory to be added>

** however, you cannot add a file or directory to a compressed archive.

Listing an archive

The tar content can be viewed before extracting by using the following command
$ tar tvf <tarball>


Similar to above two cases, for a compressed archive you can use the options 'j' or 'z' depending on the type of compression.

Extract a single file

To extract a single file from the archive, you can specify the name of the file at the end of extract command
tar xvf <tarball> <path to file>

Instead of a file, if we specify a directory, the directory will be extracted from the path.
We can also specify multiple files/directories for extraction.
Wildcards can also be used for extracting files matching a specific pattern


Reference: Open Hatch Tar quick reference

Monday, March 23, 2015

Prepending a set of lines with Vim

Have you ever faced a situation where you just wanted to have a set of characters prepended to a set of lines in one of your files, and wished that there was an easy way to do it. Well Vim comes to your rescue. It gives you a very simple way of doing this.

1. Use Ctrl+V to select the first column of text in the lines you want to comment.
2. Then hit Shift+i and type the text you want to insert.
3. Then hit Esc, wait 1 second and the inserted text will appear on every line.

This is one of the requirements for which I use Vim extensively. Hope, it helps others out there.

Reference: http://stackoverflow.com/questions/1174274/how-can-i-prepend-text-to-multiple-lines-in-vim

Saturday, March 21, 2015

Say Hello to Android - Your very first Android App

For installation instruction follow my post on Android App development on a Mac. This post is written with an android studio version 1.0.1.

Open Android Studio. Create a New Project.


In the project Wizard, set up the App name to be HelloWorld. Then provide a package name. Remember that the package name should be globally unique across all packages installed on Android. Next, choose your project location and hit next.

Now you have to choose the minimum SDK version.

There is also something known as Target SDK. The Android Studio automatically sets the target SDK for you to the latest Android version. However, in order to develop an Android App, its important for you to know the difference between the two.
The Min SDK acts as your low pass filter. The Google Play wont show your app on a device running a lower version than your Min SDK. However, the target SDK does not act as a high pass filter. This only declares which platform, your app has been tested on.

Next, we have to choose an Activity. We choose a Blank Activity with Fragment. An activity serves as a presentation layer for the UI and a fragment represents a behavior or portion of the screen.

Next, we have the option to modify the names of the files auto-generated from the Wizard.

The layout will come from activity_main, which is an XML file. Similary, the fragments layout will come from fragment_main which is also an XMl file. When you click on Finish, you will see the project structure as seen in the screenshot below.

Make sure that your Android device is connected via USB and USB debugging is enabled on device. Hit run for the project in Android Studio

Congratulations!!! You have your HelloWorld App running on your device.

Sunday, March 1, 2015

Android App development on a Mac

I started with Android development yesterday, and getting to run the Android App on my phone took some struggle. I hope that this post helps the naive android developers to get started with building their first App.
OSx version : Yosemite 10.10.1

For developing an Android App, you need the following to be installed.

Install Java


First check whether Java is installed or not. On your terminal, type
$java -version
If you have Java 6.0 or above, you can skip to setting JAVA_HOME, else please download JAVA SDK from Java install page

Now set JAVA_HOME in your ~/.bash_profile to following:
export JAVA_HOME=$(/usr/libexec/java_home)
export JDK_HOME=$(/usr/libexec/java_home)

On the terminal window, type the following command:
$ source ~/.bash_profile

Install Android Studio


Get started by downloading Android Developer Studio. You can get this from the Android Developers Site.
After setting up Android Studio as per the instruction given on the site, you can get started by developing your first Android project.

Setting up USB debugging on your Android device


Plug in your device to your Mac and then enable USB debugging on your phone. This can be found in Developer Options on your android phone. Note that, the developer options is hidden by default on Android devices with version 4.2 and above.

To find the secret menu, click on settings App. Scroll down to About Phone.

Then go down to Build Number and tap on that 7 times.

Then when you go back to settings, you will see the developer options menu appear.

Enable USB debugging on device.

Getting you Mac to detect your Android device


Ideally, your Mac should detect your phone by default. However, for some reason, this may not happen. Follow the following steps to make your Mac detect your phone.

1. Make sure that adb(Android Developer Bridge) is included in your path. The default location for adb is:
~/Library/Android/sdk/platform-tools/

2. Include the following line in your .bash_profile.
export PATH=/usr/local/sbin:$PATH:~/Library/Android/sdk/platform-tools/

3. On the terminal window, type the following command:
$ source ~/.bash_profile

4. Next add the Vendor ID to ~/.android/adb_usb.ini. But first you have to find the Vendor ID value. You can do this from the System Information Application. Fortunately on Mac this is pretty easy. Launch the System Information application.
From the Hardware Menu in the left pane -> Select USB -> In the right pane, you will see the list of USB devices, select your phone from it -> In the lower pane on the right, you will see the vendor id. Copy this vendor id to the file ~/.android/adb_usb.ini

5. Restart adb
$ adb kill-server
$ adb devices

The device should be listed.

Thats it, you are all set for developing android apps and testing them on your mobile