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()

Posted by Dr. Michael Forster at 4:50 PM

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. XML文件结构和基本语法
  2. 了解Xml格式
  3. XML轻松学习手册
  4. 深入SQLite多线程的使用总结详解
  5. XML入门的常见问题(四)
  6. java中枚举的详细使用介绍
  7. XML入门的常见问题(三)
  8. J2ME Mobile 3D入门教程系列文章之一
  9. XML入门的常见问题(二)
  10. XML入门的常见问题(一)