View android source code in Eclipse

 

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) , 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. o 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  (-b release-1.1)
  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('.'):
  if dir=='.'
  if subdirs.count('out')>0:
    subdirs.remove('out')
 
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()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值