---
title: "获取Android的Java源代码并在Eclipse中关联查看的最新方法"
author: "sailqi"
date: "2011-06-21T01:04:00Z"
tags: ["Android", "Eclipse", "Java"]
categories: ["移动开发"]
slug: "android-java-eclipse"
draft: false
lastmod: "2012-08-20T15:12:58Z"
id: 1638222
url: "https://sailqi.iteye.com/blog/1638222"
---

在Google搜索“[Android](http://618119.com/tag/android "Posts tagged with Android")Eclipse 源代码”得到的结果并不合适，因为Google的Eclipse[Android](http://618119.com/tag/android "Posts tagged with Android")开发插件已经更新到了ADT 8.0.1.相应的lib关联[java](http://618119.com/tag/java "Posts tagged with Java")源代码的方式也变了。  
以下是获取[Android](http://618119.com/tag/android "Posts tagged with Android")的[Java](http://618119.com/tag/java "Posts tagged with Java")源代码并在Eclipse中关联的最新操作方法和步骤：  
1.首先下载[Android](http://618119.com/tag/android "Posts tagged with Android")的[SDK](http://618119.com/tag/sdk "Posts tagged with SDK")安装文件：[http://dl.google.com/android/android-sdk\_r08-windows.zip](http://dl.google.com/android/android-sdk_r08-windows.zip)  
然后解压到：E:/[Java](http://618119.com/tag/java "Posts tagged with Java")/[android](http://618119.com/tag/android "Posts tagged with Android")/[android](http://618119.com/tag/android "Posts tagged with Android")-[sdk](http://618119.com/tag/sdk "Posts tagged with SDK")-windows，运行目录中的[SDK](http://618119.com/tag/sdk "Posts tagged with SDK")Manager.exe,下载好[Android](http://618119.com/tag/android "Posts tagged with Android")不同版本的API包。

2.然后下载或在线安装Eclipse插件ADT,  
安装包地址为：[http://dl.google.com/android/ADT-8.0.1.zip](http://dl.google.com/android/ADT-8.0.1.zip)  
在线安装地址：[https://dl-ssl.google.com/android/eclipse/](https://dl-ssl.google.com/android/eclipse/)  
来源：[ttp://developer.android.com/sdk/eclipse-adt.html](http://blog.163.com/cp7618@yeah/blog/static/7023477720110299746103/)

2.ADT插件安装好之后，下载我已经打包好的[Android](http://618119.com/tag/android "Posts tagged with Android")2.3（gingerbread）的[java](http://618119.com/tag/java "Posts tagged with Java")源代码压缩包：  
下载地址：[http://lizongbo.googlecode.com/files/android\_gingerbread\_javasrc.zip](http://lizongbo.googlecode.com/files/android_gingerbread_javasrc.zip)  
来源：[http://code.google.com/p/lizongbo/downloads/list](http://code.google.com/p/lizongbo/downloads/list)  
下载后复制到E:/[Java](http://618119.com/tag/java "Posts tagged with Java")/[android](http://618119.com/tag/android "Posts tagged with Android")/javasrc。

3.[Android](http://618119.com/tag/android "Posts tagged with Android")的[Java](http://618119.com/tag/java "Posts tagged with Java")源代码压缩包下载好之后，在Eclipse中设置好[SDK](http://618119.com/tag/sdk "Posts tagged with SDK")配置：  
操作步骤为：主菜单-&gt;Window–&gt;Preferences–&gt;选择[Android](http://618119.com/tag/android "Posts tagged with Android")，[SDK](http://618119.com/tag/sdk "Posts tagged with SDK")Location设置为：E:/[Java](http://618119.com/tag/java "Posts tagged with Java")/[android](http://618119.com/tag/android "Posts tagged with Android")/[android](http://618119.com/tag/android "Posts tagged with Android")-[sdk](http://618119.com/tag/sdk "Posts tagged with SDK")-windows,然后点右下角的Apply来生效。

4.开始进行关联[Android](http://618119.com/tag/android "Posts tagged with Android")的源代码，在Eclipse中新建一个[Android](http://618119.com/tag/android "Posts tagged with Android")工程，选中工程，主菜单 –&gt;Project–&gt;Properties–&gt;选择[Android](http://618119.com/tag/android "Posts tagged with Android")，Project Build Target选中[Android](http://618119.com/tag/android "Posts tagged with Android")2.3。  
然后再点左边的“[Java](http://618119.com/tag/java "Posts tagged with Java")Build Path”，切换到 Libraries 面板，展开 Android2.3 这个lib，点击[android](http://618119.com/tag/android "Posts tagged with Android").jar的[Source](http://618119.com/tag/source "Posts tagged with Source")attachment，再点Edit.在对话框中点“External File”,选择E:/[Java](http://618119.com/tag/java "Posts tagged with Java")/[android](http://618119.com/tag/android "Posts tagged with Android")/javasrc/android\_gingerbread\_javasrc.zip,点OK进行确认。这样就把[Android](http://618119.com/tag/android "Posts tagged with Android")的[java](http://618119.com/tag/java "Posts tagged with Java")代码关联好了。

5.在同一个工程中，选中工程，主菜单–&gt;Project–&gt;Properties–&gt;选择[Android](http://618119.com/tag/android "Posts tagged with Android")，Project Build Target分别选择Android1.5，Android1.6，Android2.01,Android2.1- update1，Android2.2，按上面第四步的操作重复设置关联。  
以后在任何[Android](http://618119.com/tag/android "Posts tagged with Android")工程里按住Ctrl用鼠标点击[Java](http://618119.com/tag/java "Posts tagged with Java")类名都可以浏览到对应的[java](http://618119.com/tag/java "Posts tagged with Java")源代码。  
[Android](http://618119.com/tag/android "Posts tagged with Android")各个版本对应的源代码为：  
Android1.5对应android\_cupcake\_javasrc.zip  
Android1.6对应android\_donut\_javasrc.zip  
Android2.01和Android2.1-update1对应的是android\_eclair\_javasrc.zip  
Android2.2对应android\_froyo\_javasrc.zip  
Android2.3对应android\_gingerbread\_javasrc.zip

6.在Eclipse配置好源代码关联之后之后的信息会存储在：  
E:/lizongbo/workspacenew/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.index  
E:/lizongbo/workspacenew/.metadata/.plugins/org.eclipse.jdt.core/variablesAndContainers.dat

同时记录[Android](http://618119.com/tag/android "Posts tagged with Android")的[java](http://618119.com/tag/java "Posts tagged with Java")源代码压缩包制作方法如下：  
1.先在远程的Ubuntu Linux服务器(是Linode VPS)上安装以下组件(JDK1.6已经安装好了)：  
sudo apt-get install[git](http://618119.com/tag/git "Posts tagged with git")-core gnupg flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl libncurses5-dev zlib1g-dev  
信息来自[Android](http://618119.com/tag/android "Posts tagged with Android")官方：[http://source.android.com/source/download.html](http://source.android.com/source/download.html)  
2.运行 sudo apt-get install lib32readline5-dev  
3.安装Repo脚本：  
我懒得配PATH了，于是直接安装到了/usr/local/bin。  
[root@lizongbo.com](mailto:root@lizongbo.com):~/bin# cd /usr/local/bin  
[root@lizongbo.com](mailto:root@lizongbo.com):/usr/local/bin# ls  
geoip-lookup  
[root@lizongbo.com](mailto:root@lizongbo.com):/usr/local/bin# curl[http://android.git.kernel.org/repo](http://android.git.kernel.org/repo)&gt;/usr/local/bin/repo  
% Total % Received % Xferd Average Speed Time Time Time Current  
Dload Upload Total Spent Left Speed  
100 17412 100 17412 0 0 27517 0 –:–:– –:–:– –:–:– 31148  
[root@lizongbo.com](mailto:root@lizongbo.com):/usr/local/bin# chmod a+x /usr/local/bin/repo  
注意在下载该脚本的时候，可能会遇到服务器出错，下载的内容不对，运行命令的时候会提示错误“syntax error near unexpected token \`newline’”。  
[root@lizongbo.com](mailto:root@lizongbo.com):/usr/local/bin# repo init -u[git://android.git.kernel.org/platform/manifest.git](http://blog.163.com/cp7618@yeah/blog/static/7023477720110299746103/)-b cupcake  
/usr/local/bin/repo: line 1: syntax error near unexpected token \`newline’  
/usr/local/bin/repo: line 1: \`&lt;!DOCTYPE HTML PUBLIC “-//IETF//DTD HTML 2.0//EN”&gt;’  
[root@lizongbo.com](mailto:root@lizongbo.com):/usr/local/bin# more repo  
&lt;!DOCTYPE HTML PUBLIC “-//IETF//DTD HTML 2.0//EN”&gt;  
&lt;html&gt;&lt;head&gt;  
&lt;title&gt;500 Internal Server Error&lt;/title&gt;  
&lt;/head&gt;&lt;body&gt;  
&lt;h1&gt;Internal Server Error&lt;/h1&gt;  
&lt;p&gt;The server encountered an internal error or  
misconfiguration and was unable to complete  
your request.&lt;/p&gt;  
&lt;p&gt;Please contact the server administrator,  
[webmaster@kernel.org](mailto:webmaster@kernel.org)and inform them of the time the error occurred,  
and anything you might have done that may have  
caused the error.&lt;/p&gt;  
&lt;p&gt;More information about this error may be available  
in the server error log.&lt;/p&gt;  
&lt;/body&gt;&lt;/html&gt;  
从内容看是服务器出错了，因此需要重新下载脚本.  
4.建立mydroid目录，按代码分支下载对应版本的[Android](http://618119.com/tag/android "Posts tagged with Android")完整的源代码，比如下载Android2.3的代码：  
[root@lizongbo.com](mailto:root@lizongbo.com):~/mydroid# mkdir gingerbread  
[root@lizongbo.com](mailto:root@lizongbo.com):~/mydroid# cd gingerbread/  
[root@lizongbo.com](mailto:root@lizongbo.com):~/mydroid/gingerbread# repo init -u[git://android.git.kernel.org/platform/manifest.git](http://blog.163.com/cp7618@yeah/blog/static/7023477720110299746103/)-b gingerbread  
[root@lizongbo.com](mailto:root@lizongbo.com):~/mydroid/gingerbread# repo sync  
Android1.5的代码分支：  
[root@lizongbo.com](mailto:root@lizongbo.com):~/mydroid/cupcake# repo init -u[git://android.git.kernel.org/platform/manifest.git](http://blog.163.com/cp7618@yeah/blog/static/7023477720110299746103/)-b cupcake  
[root@lizongbo.com](mailto:root@lizongbo.com):~/mydroid/cupcake# repo sync  
Android1.6的代码分支：  
[root@lizongbo.com](mailto:root@lizongbo.com):~/mydroid/donut# repo init -u[git://android.git.kernel.org/platform/manifest.git](http://blog.163.com/cp7618@yeah/blog/static/7023477720110299746103/)-b donut  
[root@lizongbo.com](mailto:root@lizongbo.com):~/mydroid/donut# repo sync  
Android2.0.1和2.1的代码分支：  
[root@lizongbo.com](mailto:root@lizongbo.com):~/mydroid/eclair# repo init -u[git://android.git.kernel.org/platform/manifest.git](http://blog.163.com/cp7618@yeah/blog/static/7023477720110299746103/)-b eclair  
[root@lizongbo.com](mailto:root@lizongbo.com):~/mydroid/eclair# repo sync  
Android2.2的代码分支：  
[root@lizongbo.com](mailto:root@lizongbo.com):~/mydroid/froyo# repo init -u[git://android.git.kernel.org/platform/manifest.git](http://blog.163.com/cp7618@yeah/blog/static/7023477720110299746103/)-b froyo  
[root@lizongbo.com](mailto:root@lizongbo.com):~/mydroid/froyo# repo sync

如果遇到代码下载速度太慢，可以指定hosts，hosts配置不住为：  
[root@lizongbo.com](mailto:root@lizongbo.com):~/mydroid/gingerbread/.repo/projects# nslookup  
&gt;[android](http://618119.com/tag/android "Posts tagged with Android").[git](http://618119.com/tag/git "Posts tagged with git").kernel.org  
Server: 72.14.188.5  
Address: 72.14.188.5#53  
Non-authoritative answer:  
[android](http://618119.com/tag/android "Posts tagged with Android").[git](http://618119.com/tag/git "Posts tagged with git").kernel.org canonical name =[android](http://618119.com/tag/android "Posts tagged with Android").[git](http://618119.com/tag/git "Posts tagged with git").geo.kernel.org.  
[android](http://618119.com/tag/android "Posts tagged with Android").[git](http://618119.com/tag/git "Posts tagged with git").geo.kernel.org canonical name =[android](http://618119.com/tag/android "Posts tagged with Android").[git](http://618119.com/tag/git "Posts tagged with git").us.kernel.org.  
Name:[android](http://618119.com/tag/android "Posts tagged with Android").[git](http://618119.com/tag/git "Posts tagged with git").us.kernel.org  
Address: 149.20.20.141  
Name:[android](http://618119.com/tag/android "Posts tagged with Android").[git](http://618119.com/tag/git "Posts tagged with git").us.kernel.org  
Address: 204.152.191.45  
&gt; exit

[root@lizongbo.com](mailto:root@lizongbo.com):/etc# vi hosts  
204.152.191.45[android](http://618119.com/tag/android "Posts tagged with Android").[git](http://618119.com/tag/git "Posts tagged with git").us.kernel.org  
204.152.191.45[android](http://618119.com/tag/android "Posts tagged with Android").[git](http://618119.com/tag/git "Posts tagged with git").kernel.org

网上有文章介绍使用提取[java](http://618119.com/tag/java "Posts tagged with Java")文件并打包的pytho脚本可以生成源代码包，参考：

[http://android.opensourceror.org/2010/01/18/android-source/](http://android.opensourceror.org/2010/01/18/android-source/)

和[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)  
Python下载地址：[http://www.python.org/ftp/python/2.6.6/python-2.6.6.msi](http://www.python.org/ftp/python/2.6.6/python-2.6.6.msi)  
但是在Android2.2和Android2.3的代码里，[Android](http://618119.com/tag/android "Posts tagged with Android")写一了一些mock的类，比如[android](http://618119.com/tag/android "Posts tagged with Android").webkit.WebView，使用python脚本打包的话，会把mock的源代码打进去，而不是真正的源代码，  
因此我根据python脚本写了个[java](http://618119.com/tag/java "Posts tagged with Java")程序来提取[java](http://618119.com/tag/java "Posts tagged with Java")文件过滤冗余的[java](http://618119.com/tag/java "Posts tagged with Java")代码并打包。  
生成的[java](http://618119.com/tag/java "Posts tagged with Java")源代码压缩：android\_cupcake\_javasrc.zip，android\_donut\_javasrc.zip，android\_eclair\_javasrc.zip，android\_froyo\_javasrc.zip，android\_gingerbread\_javasrc.zip  
目前在code.google.com上传了android\_gingerbread\_javasrc.zip，欢迎下载。

以下是各版本的[Android](http://618119.com/tag/android "Posts tagged with Android")[java](http://618119.com/tag/java "Posts tagged with Java")[source](http://618119.com/tag/source "Posts tagged with Source")下载地址：

Android2.3:[http://lizongbo.googlecode.com/files/android\_gingerbread\_javasrc.zip](http://lizongbo.googlecode.com/files/android_gingerbread_javasrc.zip)

Android1.5:[http://lizongbo.googlecode.com/files/android\_cupcake\_javasrc.zip](http://lizongbo.googlecode.com/files/android_cupcake_javasrc.zip)  
Android1.6:[http://lizongbo.googlecode.com/files/android\_donut\_javasrc.zip](http://lizongbo.googlecode.com/files/android_donut_javasrc.zip)  
Android2.1:[http://lizongbo.googlecode.com/files/android\_eclair\_javasrc.zip](http://lizongbo.googlecode.com/files/android_eclair_javasrc.zip)  
Android2.2:[http://lizongbo.googlecode.com/files/android\_froyo\_javasrc.zip](http://lizongbo.googlecode.com/files/android_froyo_javasrc.zip)

 

 

本文转自：http://blog.163.com/cp7618@yeah/blog/static/7023477720110299746103/