獲取Android的Java源代碼並在Eclipse中關聯查看的最新方法

在Google搜索“Android Eclipse 源代碼”得到的結果並不合適,因爲Google的Eclipse Android開發插件已經更新到了ADT 8.0.1.相應的lib關聯java源代碼的方式也變了。
以下是獲取AndroidJava源代碼並在Eclipse中關聯的最新操作方法和步驟:
1.首先下載AndroidSDK安裝文件:http://dl.google.com/android/android-sdk_r08-windows.zip
然後解壓到:E:\Java\android\android-sdk-windows,運行目錄中的SDK Manager.exe,下載好Android不同版本的API包。

2.然後下載或在線安裝Eclipse插件ADT,
安裝包地址爲:http://dl.google.com/android/ADT-8.0.1.zip
在線安裝地址:https://dl-ssl.google.com/android/eclipse/
來源:ttp://developer.android.com/sdk/eclipse-adt.html

2.ADT插件安裝好之後,下載我已經打包好的Android 2.3(gingerbread)的java源代碼壓縮包:
下載地址:http://lizongbo.googlecode.com/files/android_gingerbread_javasrc.zip
來源:http://code.google.com/p/lizongbo/downloads/list
下載後複製到E:\Java\android\javasrc。

3.AndroidJava源代碼壓縮包下載好之後,在Eclipse中設置好SDK配置:
操作步驟爲:主菜單->Window–>Preferences–>選擇AndroidSDK Location設置爲:E:\Java\android\android-sdk-windows,然後點右下角的Apply來生效。

4.開始進行關聯Android的源代碼,在Eclipse中新建一個Android工程,選中工程,主菜單 –>Project–>Properties–>選擇Android,Project Build Target選中Android 2.3。
然後再點左邊的“Java Build Path”,切換到 Libraries 面板,展開 Android2.3 這個lib,點擊android.jar的Source attachment,再點Edit.在對話框中點“External File”,選擇E:/Java/android/javasrc/android_gingerbread_javasrc.zip,點OK進行確認。這樣就把Androidjava代碼關聯好了。

5.在同一個工程中,選中工程,主菜單–>Project–>Properties–>選擇 Android,Project Build Target分別選擇Android1.5,Android1.6,Android2.01,Android2.1- update1,Android2.2,按上面第四步的操作重複設置關聯。
以後在任何Android工程裏按住Ctrl用鼠標點擊Java類名都可以瀏覽到對應的java源代碼。
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

同時記錄Androidjava源代碼壓縮包製作方法如下:
1.先在遠程的Ubuntu Linux服務器(是Linode VPS)上安裝以下組件(JDK1.6已經安裝好了):
sudo apt-get install git-core gnupg flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl libncurses5-dev zlib1g-dev
信息來自Android官方:http://source.android.com/source/download.html
2.運行 sudo apt-get install lib32readline5-dev
3.安裝Repo腳本:
我懶得配PATH了,於是直接安裝到了/usr/local/bin。
[email protected]:~/bin# cd /usr/local/bin
[email protected]:/usr/local/bin# ls
geoip-lookup
[email protected]:/usr/local/bin# curl http://android.git.kernel.org/repo >/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
[email protected]:/usr/local/bin# chmod a+x /usr/local/bin/repo
注意在下載該腳本的時候,可能會遇到服務器出錯,下載的內容不對,運行命令的時候會提示錯誤“syntax error near unexpected token `newline’”。
[email protected]:/usr/local/bin# repo init -u git://android.git.kernel.org/platform/manifest.git -b cupcake
/usr/local/bin/repo: line 1: syntax error near unexpected token `newline’
/usr/local/bin/repo: line 1: `<!DOCTYPE HTML PUBLIC “-//IETF//DTD HTML 2.0//EN”>’
[email protected]:/usr/local/bin# more repo
<!DOCTYPE HTML PUBLIC “-//IETF//DTD HTML 2.0//EN”>
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator,
[email protected] and inform them of the time the error occurred,
and anything you might have done that may have
caused the error.</p>
<p>More information about this error may be available
in the server error log.</p>
</body></html>
從內容看是服務器出錯了,因此需要重新下載腳本.
4.建立mydroid目錄,按代碼分支下載對應版本的Android完整的源代碼,比如下載Android2.3的代碼:
[email protected]:~/mydroid# mkdir gingerbread
[email protected]:~/mydroid# cd gingerbread/
[email protected]:~/mydroid/gingerbread# repo init -u git://android.git.kernel.org/platform/manifest.git -b gingerbread
[email protected]:~/mydroid/gingerbread# repo sync
Android1.5的代碼分支:
[email protected]:~/mydroid/cupcake# repo init -u git://android.git.kernel.org/platform/manifest.git -b cupcake
[email protected]:~/mydroid/cupcake# repo sync
Android1.6的代碼分支:
[email protected]:~/mydroid/donut# repo init -u git://android.git.kernel.org/platform/manifest.git -b donut
[email protected]:~/mydroid/donut# repo sync
Android2.0.1和2.1的代碼分支:
[email protected]:~/mydroid/eclair# repo init -u git://android.git.kernel.org/platform/manifest.git -b eclair
[email protected]:~/mydroid/eclair# repo sync
Android2.2的代碼分支:
[email protected]:~/mydroid/froyo# repo init -u git://android.git.kernel.org/platform/manifest.git -b froyo
[email protected]:~/mydroid/froyo# repo sync

如果遇到代碼下載速度太慢,可以指定hosts,hosts配置不住爲:
[email protected]:~/mydroid/gingerbread/.repo/projects# nslookup
> android.git.kernel.org
Server:         72.14.188.5
Address:        72.14.188.5#53
Non-authoritative answer:
android.git.kernel.org  canonical name = android.git.geo.kernel.org.
android.git.geo.kernel.org      canonical name = android.git.us.kernel.org.
Name:   android.git.us.kernel.org
Address: 149.20.20.141
Name:   android.git.us.kernel.org
Address: 204.152.191.45
> exit

[email protected]:/etc# vi hosts
204.152.191.45 android.git.us.kernel.org
204.152.191.45 android.git.kernel.org

網上有文章介紹使用提取java文件並打包的pytho腳本可以生成源代碼包,參考:

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

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
但是在Android2.2和Android2.3的代碼裏,Android寫一了一些mock的類,比如android.webkit.WebView,使用python腳本打包的話,會把mock的源代碼打進去,而不是真正的源代碼,
因此我根據python腳本寫了個java程序來提取java文件過濾冗餘的java代碼並打包。
生成的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 java source 下載地址:

Android2.3: http://lizongbo.googlecode.com/files/android_gingerbread_javasrc.zip

Android1.5: http://lizongbo.googlecode.com/files/android_cupcake_javasrc.zip
Android1.6: http://lizongbo.googlecode.com/files/android_donut_javasrc.zip
Android2.1: http://lizongbo.googlecode.com/files/android_eclair_javasrc.zip
Android2.2: http://lizongbo.googlecode.com/files/android_froyo_javasrc.zip

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章