Friday, July 17, 2015

How to use an external project as a library project in Android Studio

We have 2 Android projects:
MyApplication is the project that produces apk and to be run on mobile devices;
Dependency is a library project that contains useful classes such as Fragments, Services, Util classes to be used by MyApplication.

The structure of these 2 projects are like this:
MyApplication
  |--build.gradle
  |--settings.gradle
  |--app
  |    |--build.gradle
Dependency
  |--build.gradle
  |--settings.gradle
  |--app
  |    |--build.gradle

First of all, we need to make sure that the gradle plugin of Dependency/app/build.gradle is library instead of application. The first line of the build.gradle file should be this:


apply plugin: 'com.android.library'

Then we go back to the MyApplication project, find the settings.gradle file under the project directory(MyApplication/settings.gradle), edit it to include our Dependency project by adding the lines like this:


include ':library-project-name'
project(':library-project-name').projectDir = new File(settingsDir, '../Dependency/app/')

the second parameter of File(.., ..) is a directory path pointing to the build.gradle file of the app module of the library project.


The last step is to configure the build.gradle file of the app module of the MyApplication project in order to add the library project to its dependencies.

We go to MyApplication/app/build.gradle file and add a line to the dependencies block.


dependencies{
   ........
   compile project(':library-project-name')
}

All done, feel free to use any classes in the Dependency library project in your MyApplication project from this point on.

Wednesday, July 8, 2015

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'

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


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