Useful Commands For Android Developers on Mac

Check jdk path

/usr/libexec/java_home -v 1.7

Add JDK path to environment variables

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home

Add gradle to path

GRADLE_HOME=/Users/ericliu/Developer/Android/gradle-2.5;
export GRADLE_HOME
export PATH=$PATH:$GRADLE_HOME/bin

Add adb to path

export PATH=$PATH:/Users/ericliu/Library/Android/sdk/platform-tools

Open a file with an Application

open Application.app

Or:

open -a Application filename

Close an Application from Terminal

osascript -e 'quit app "APPLICATIONNAME"'

For example, to quit Calendar from the command line, replace APPLICATIONNAME with “Calendar”

osascript -e 'quit app "Calendar"'

Enable Git auto-completion and add the current branch, and the status of the working directory info to the prompt
First, download some script files from github and rename them to start with “.” so they will become hidden files.

curl -OL https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
mv git-completion.bash .git-completion.bash
curl -OL https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
mv git-prompt.sh .git-prompt.sh

Then edit the .bash_profile file or .profile or .bashrc

# Git config: add the current branch, and the status of the working directory info to the # prompt
. ~/.git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
export PS1='\w$(__git_ps1 " (%s)")\$ '

# Git config: load the .git-completion.bash file if it exists
if [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
fi

Now the auto-completion for git is enabled and the prompt line will now display the current branch name. If you go to a git repository directory, the prompt line will now look like this:

~/Developer/Work/MyApplication (master)$

View Android Task Stack from command line

adb shell dumpsys activity

To be more concise:

adb shell dumpsys activity activities | sed -En -e '/Stack #/p' -e '/Running activities/,/Run #0/p'

We can create an alias in the .profile file for this long command:

alias dumpsys="adb shell dumpsys activity activities | sed -En -e '/Stack #/p' -e '/Running activities/,/Run #0/p'"

Re-start the Terminal and we will able to just type ‘dumpsys’ to display activity stack.

Start an Activity from adb shell

am start -n "com.ericliu.uat/com.ericliu.MainActivity"

Start a debug session

am start -D -n "com.ericliu.uat/com.ericliu.MainActivity"

In addition, if you only want to see the activities’ name in the stack you can do this:

adb shell <enter> dumpsys activity | grep -i run

Output logcat output from adb to a file

adb logcat -v time > ~/logcat.txt

Compile a C program file.

gcc HelloWorld.c

And run the compiled result.

./a.out

References:

http://osxdaily.com/2014/09/05/gracefully-quit-application-command-line/

http://stackoverflow.com/questions/2442713/view-the-tasks-activity-stack

Show your support

Clapping shows how much you appreciated ericliu’s story.