Saturday, December 20, 2008

View Android Source Code in Eclipse

Recently, I have been hacking around with Android a lot, and I think it's awesome.

Unfortunately, the Android SDK does not come with source code, and sometimes the docs are not so great, yet. Being an open source project, the Android sources are available online, but they are not very easy to access from Eclipse. For one, the source files are sometimes difficult to find as they are distributed over many individual Git repositories, and then I am not always online.

Integrating source files into Eclipse is usually quite easy, but the Android Eclipse plugin (ADT) does not allow to modify the Java Source Attachment of android.jar in the Android Library. Fortunately, you only have to find out what the default location for source code is. Eric Burke nicely describes how to find that out.

Long story short: Android source code needs to be placed in a "sources" subdirectory of the Android SDK. Here is what you need to do:

  1. Get the Android source code (install repo, repo init, repo sync)
  2. Move all Java sources into a "sources" subdirectory of the Android SDK

Step 2 sounds easier than it is. There are a lot of Java files in the Android sources, and they are sprinkled all over the place. Eclipse, however, needs the source files in a standard Java source directory structure: The path of a source file needs to match its package name. To simplify this task, I have written a short Python script that recursively searches for Java files and packs them into a ZIP file. Unpack that source file into a "sources" subdirectory of the Android SDK. Enjoy:

from __future__ import with_statement  # for Python < 2.6

import os
import re
import zipfile

# open a zip file
DST_FILE = 'sources.zip'
if os.path.exists(DST_FILE):
  print DST_FILE, "already exists"
  exit(1)
zip = zipfile.ZipFile(DST_FILE, 'w', zipfile.ZIP_DEFLATED)

# some files are duplicated, copy them only once
written = {}

# iterate over all Java files
for dir, subdirs, files in os.walk('.'):
  for file in files:
    if file.endswith('.java'):
      # search package name
      path = os.path.join(dir, file)
      with open(path) as f:
        for line in f:
          match = re.match(r'\s*package\s+([a-zA-Z0-9\._]+);', line)
          if match:
            # copy source into the zip file using the package as path
            zippath = match.group(1).replace('.', '/') + '/' + file
            if zippath not in written:
              written[zippath] = 1
              zip.write(path, zippath)
            break;
         
zip.close()

12 comments:

NealMcB said...

Excellent! Thanks.
The discussion at this link was also helpful.

http://stuffthathappens.com/blog/2008/11/01/browsing-android-source-in-eclipse/

Peter said...

On windows, An easy way to just get a copy of the framework source is to install a windows version of git & ignore the google repo instructions.
I used "Git Gui" and used their "Git Bash" shell program to navigate to a directory of my choice and then executed:

"git clone git://android.git.kernel.org/platform/frameworks/base.git"

You still need to move everything around to get into a nice format for eclipse...

edam said...

It might be worth noting that if you follow the instructions on the google site to get the Android source code, you will be getting the latest source code. This will not match the code that comes with the SDK.

To get the correct source code, when you init the repo, you should do it like this:

$ repo init -u git://android.git.kernel.org/platform/manifest.git -b release-1.0

stanb said...

To browse just a single file, go to http://www.google.co.in/codesearch and search for: android calendar
for example.

Gil Michlin said...

you must not include OUT directory
or you will have SRC which is stub
so add the following after os.walk:

if dir=='.'
  if subdirs.count('out')>0:
    subdirs.remove('out')

secoif said...

Could someone please provide a link to download the 1.1 source files... poor internet connection rules 2gb downloads out for me so this would be really really helpful :) :)

poohtbear said...

Great post! great link as well, found it very useful.
However i was unable to use repo init command, not that i got error message, it just takes too long! i got "receiving objects" to 91% and stopped there, and i left the process overnight...
trying with git directly didn't do the trick most of the time either.One time (that was enough ) i've made it to pull out just the java sources and put them in sources.

Anyone knows why it's soooooo slow ? is there another official mirror to the git ?

ssaidwho said...

Thank you! The referenced blog and this script worked great for me.

id said...

perfect. thank you!

sosiouxme said...

Thanks, incredibly useful.

To edam's point - now there are a few more releases out, and they're not logically named release-1.1, release-1.5, etc.; the *tags* are but you can't get those with -b. Looking at the output of repo I'm thinking the branch names you want are cupcake, donut, or eclair, to debug with for your 1.5, 1.6, or 2.0 platforms respectively. Or do we want the -release versions of those?

To secoif's point - I don't see any reason why, after I get the source all nicely located for a particular release, I couldn't zip it up and make it available on the web... will make a post if I get it all set...

ntraft said...

Does anyone know if Gil Michlin is correct about the OUT directories? I'm not familiar with Python and I'm not sure exactly where his code should be placed.

sosiouxme said...

Those who don't have the connection to pull down 2GB of repo, check out my article for a summary and to download platform-specific 22MB source zips :-)

http://android.opensourceror.org/2010/01/18/android-source/

Let me know how it goes...

Post a Comment