Monday, July 23, 2012

Eclipse EGIT, Download Code, Attach Framework code & Debug

This article explains procedure to download Android source (few important Apps and Framework base code) using Eclipse EGit plugin and then attach framework code to debug important framework classes (e.g. Activity etc.).
  1. Install EGit
  2. Download Source from GIT Repository
  3. Attach Framework code
  4. Debug
Download EGit Plug-in
EGit is a GIT plugin for Eclipse which helps to mange GIT clone, Check-ins, Sync etc. from your Eclipse workspace.
Eclipse (Version: 3.7.x) -> Help -> Install New Software -> "Add" - "http://download.eclipse.org/egit/updates".
Once the plug-in installation is successful, you'll find a new Eclipse View perspective- "Git Repository Exploring"

  
Download Android source
To download code from Android GIT repository, we need to create "local Git clone". Each local clone is associated with Remote Clone URL.  
https://android.googlesource.com/ lists Git Repository URLs for different sections of Android source code (platform, kernel etc.).
Kindly note:
The repository URL for Platform (https://android.googlesource.com/platform/manifest), doesn't recursively download complete platform code (e.g. /platform/frameworks/base, /platform/packages/app), rather it downloads default.xml which contains Git URL for individual platform components.
If you are using EGit, you need to separately download code for each components of platform (e.g. framework/base, packages/app/Contact etc.)

Following section describes procedure to download code for Calendar application and Framework (/framework/base)-

Open the Git Repository Perspective-
  

There are 3 important buttons- "Add an Existing Repo", "Clone a Remote Repo" and "Create a New Repo".
Click on the Git Icon with blue arrow ("Clone a Remote Repo") to create a local clone of Android Jelly Bean source.
 

Press "Next". It will communicate with Google Server and download the list of available branches. You need to select relevant Android source branch, in this case "jb-release"-
 

Press "Next". In the successive screen, you need to Select the location where you want to create Local clone of this remote repository. Make sure you checked "Import all existing projects after clone finishes", this will instruct Egit to download the source code.
 

Click "Finish". If all above steps works fine, it will show progress bar and download source code-
 

Follow the same steps to clone Framework base repository. Framework code base will of around 1 GB and you need to increase Eclipse max Heap size to 1GB, otherwise Git Cloning process will fail.

Following screenshot shows both the clones-

Attach Framework base
"Import" the Calendar Application code as Android Project (ignore compilation errors).

To attach framework code, Right-click on "android.jar" and mention the framework source location (location of Framework code that we downloaded in the above step).
 
Once you mention the source location, Eclipse automatically does the indexing based on Package and Java file name. Now, if you click on "Activity.class", it will open the attached Jelly Bean Activity.java file-
 
We are going good. We have downloaded latest Android 4.1 Calendar application and Framework Base source code, we have attached the framework source with android.jar and Eclipse has done necessary indexing to find out respective Java file. So, all set to do the Framework Level Debugging.

Debugging
Lets set a break-point at the entry of Calendar application and then we'll debug files (both Calendar and Framework files).
For Calendar application, entry point or Main Launcher is com.android.calendar.AllInOneActivity. I'll set the break-point at the very beginning i.e. onCreate()-
Now, launch the Emulator, once Emulator is boot-up, you can find a list of already running processes and "Calendar" is one of them. To start the debugging, we need to attach Eclipse Remote debugger with Calendar process ID.
Select the Calendar process ID on DDMS and then click on Debug button (green color bug icon). It will attach Java remote debugger framework to Android Calendar process 8614.

We can also follow Debug Configuration option to attach Java Remote debugger with Android process-
  

As we have set the break-point at onCreate() method of Calendar application, if we launch Calendar from emulator, this break-point will hit and Eclipse will open "Debug Perspective". Press F5 to move into implementation of getSharedPreference() method.
     
I guess there is still one more question, how do I put break-point on Framework class e.g. Activity.java, I'll cover this topic in my next post.