---
title: "自制android1.5的源码包"
author: "houniao"
date: "2009-08-16T17:25:23Z"
tags: []
categories: ["移动开发"]
slug: "android1-5"
draft: false
lastmod: "2009-08-16T17:30:41Z"
id: 450680
url: "https://houniao.iteye.com/blog/450680"
---

最近打算玩下android，听hong老大说最好看看源码。而android官方并没有提供直接可用的。

具体参考如下：  
[http://www.williamhua.com/2009/04/29/git-and-repo-for-dummies/](http://www.williamhua.com/2009/04/29/git-and-repo-for-dummies/)  
[http://blog.michael-forster.de/2008/12/view-android-source-code-in-eclipse.html](http://blog.michael-forster.de/2008/12/view-android-source-code-in-eclipse.html)

注意：在做sources.zip的时候不能是用主干来做，要切换到origin/android-sdk-1.5\_r3分支下，我是这么做的

$ git checkout origin/android-sdk-1.5\_r3 -b sdk1.5r3

否则在debug的时候会定位错误; 今天刚接触这东西，不熟瞎试的，好像还要在.repo/manifest/目录下能执行上面的命令

另外用mike's blog里的python抽取java源文件好像太多了，我稍微修改一下，把android.jar包里面没有的java类都去了。

在自己建的源代码根目录建get\_source.py这么个文件：

```
from __future__ import with_statement  # for Python < 2.6

import os
import re
import zipfile

# open a zip file
DST_FILE = 'sources.zip'
CLASS_FILE_PATH = '/home/don/desktop/android/'
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 and os.path.exists(CLASS_FILE_PATH + match.group(1).replace('.', '/') + '/' + file[0:-4] + 'class'):
            # 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()
```

对python不熟，不知道怎么读jar文件，我把android.jar解压到了桌面。运行上面代码就能生成干净的sources.zip了。

将sources.zip解压到 sdk目录下platforms/android-1.5/下即可

android-1.5下会多出一个sources文件，源文件就在里面了，重启eclipse就可以用了。

 

我把android1.5的源码包放附件里了，省得大家再做了。