My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
OpenProject  
開啟現有專案
tw, open, import
Updated Aug 4, 2010 by gasolin

開啟專案

我們回到 Eclipse 環境來。在螢幕上方的選單列上,選擇「File->New->Project」,會彈出「New Project」對話視窗。 Eclipse 是通用的編輯環境,可根據你所安裝的不同擴充套件而支援許多種類的專案。 點擊 「Android」 資料夾下的「Android Project」,會開啟「New Android Project」對話視窗。

我們將開啟「SDK 組件」中提供的 ApiDemos 範例。在「New Android Project」對話視窗中,點選 "Browse..."按鈕以選擇「開啟已經存在的專案」(Create project from existing source)。我們在此選擇位於「android_sdk/platforms/android-2.x/samples」目錄中的 Android 應用程式專案 (android_sdk/platforms/android-2.x/samples/ApiDemos)。

當我們選擇了現存的範例程式專案時,「New Android Project」對話視窗中的諸如專案名稱(Project Name)與屬性等內容都將被自動填寫好。這時我們可以按下 「Finish」按鈕,完成從現存專案新增專案到 Eclipse 環境的動作。

匯入專案

如果你的程式專案已位於工作環境(WorkSpace)資料夾下,想使用上述方法開啟專案時,會得到欲開啟的資料夾已在工作目錄下的警告。因此我們得用另一個方法:匯入專案。

在螢幕上方的選單列上,選擇「File->Import」選項,會跳出「Import」視窗。選擇「General->Existing Projects into WorkSpace」項目,然後按下「Next」按鈕帶到新一個畫面。在「Select Root Directory」欄位旁,按下右方的「Browse...」按鈕,選擇對應的專案。選擇好後,按下「Finish」按鈕完成從現存在工作環境(WorkSpace)資料夾下的專案匯入到 Eclipse 環境的動作。

不同 SDK 版本的範例專案會放在「android_sdk/platforms/android-版本/samples」目錄中,請自行根據要開發的版本來選擇範例。

修復專案

完成新增程式專案到 Eclipse 後,我們可以在左側的「Package Explorer」中找到我們新增的專案。

如果發現開啟後的資料夾圖示上有個小小的黃色驚嘆號,表示這個專案匯入後還有些問題,我們可以使用ADT內建的功能來試著修復專案屬性。 在「Package Explorer」的 「ApiDemos」 專案檔案夾圖示上點選右鍵,從「Android Tools」選單中選擇「修復專案屬性」(Fix Project Properties)。 (Android Tools->Fix Project Properties)

如果發現開啟後的資料夾圖示上有個小小的紅色叉號,表示這個專案開啟/匯入後遇到了無法編譯的問題。最常見的也是與無法正常生成「gen」目錄相關的問題。一般簡單的解決方式是打開專案中任一 XML 檔案(如AndroidManifest.xml 或是「res」目錄下附檔名為 .xml 的檔案),改變一下內容(如在檔案中多按一個空格)後存檔,這時開發工具會自動編譯生成「gen」目錄中新的內容。這樣無法編譯的問題往往就解決了。如何修改 XML 檔案在後面章節中會提到。

切換 SDK 版本

Android 在 SDK 1.5 版之後引入了支援多個版本 SDK 與模擬器的新特性,讓我們得以透過修改屬性設定畫面的設定,來切換用來編譯與運行這些專案的目標 SDK 版本。

在「Navigator」的「ApiDemos」專案檔案夾圖示上點選右鍵,選擇「properties」選項,會開啟專案屬性設定畫面。

在設定畫面中先選擇左方的 Android 標籤,選擇後會出現可勾選的「Project Build Target」選單。

在選單中選擇適當的目標版本,選擇好之後按下 OK 結束設定畫面,這時專案就已經切換成可使用目標版本編譯的狀態了。

參考資料

< 管理 SDK | 回目錄 | 操作虛擬機器 >


對於本章,您還期望知道什麼樣的內容呢?請在下方提出建議!

Comment by han.chun...@gmail.com, Jul 11, 2009

發現開啟後的資料夾圖示上有個小小的黃色驚嘆號,從「Android Tools」選單中選擇「修復專案屬性」(Fix Project Properties)並無法完成修復 錯誤訊息為: [2009-07-12 11:38:24 - ApiDemos?] Unknown error: SDK is not loaded yet [2009-07-12 11:38:48 - ApiDemos?] libpng warning: Ignoring attempt to set cHRM RGB triangle with zero area [2009-07-12 11:39:02 - ApiDemos?] libpng warning: Ignoring attempt to set cHRM RGB triangle with zero area

Comment by project member gasolin, Oct 21, 2009

Build-in App 有的用到 internal API, 最好是連著 Source Code 一起編. 這超過入門難度, 書上沒有教到

Comment by project member gasolin, Oct 21, 2009
Comment by bill.fu6...@gmail.com, Aug 3, 2010

請問 我按照前面幾個步驟安裝好SDK後 我的android_sdk/platforms/底下 只有android-3 android-4 android-7 android-8 並沒有2.0 這樣會照成我之後操作上有問題嗎? 因為我在開啟3的sample後 出現紅色小叉叉無法編譯 我也按照你的步驟去改變xml檔裡面加一些空格 但是還是無法成功編譯

Comment by project member gasolin, Aug 4, 2010

版本不會照成操作上問題, 其他編譯問題請查看參考資料

Comment by mobe...@gmail.com, Sep 8, 2010

請問一下:

我載入1.5跟1.6的ApiDemos? 都會出現錯誤訊息: [2010-09-09 00:18:50 - ApiDemos?] libpng warning: Ignoring attempt to set cHRM RGB triangle with zero area [2010-09-09 00:18:50 - ApiDemos?] C:\eclipse\android-sdk-windows\platforms\android-3\samples\ApiDemos?\res\values\strings.xml:365: error: Apostrophe not preceded by \ (in I'm on! :)) .... 然後gen內都無法產生新內容,用了上面改.xml的方發也是一樣,可是載入2.0以上的demo就都沒問題,請問這到底是怎麼一回事??

Comment by project member gasolin, Sep 8, 2010

記得裝對應版本的SDK

Comment by sow...@gmail.com, Oct 11, 2010

問一下大大 我開新專案 出現這個錯誤碼耶 是啥意思阿>< [2010-10-12 00:39:05 - zBMI] ERROR: Unable to open class file C:\Users\tku\workspace\zBMI\gen\com\kaulug\test\R.java: No such file or directory

Comment by leizi...@yahoo.cn, May 4, 2012

作者,开启后的资料夹图示上有个小小的红色叉号。整个专案有32个类似的errors,其中一个是:“Description Resource Path Location Type The method compare(LoaderCustom?.AppEntry?, LoaderCustom?.AppEntry?) of type new Comparator<LoaderCustom?.AppEntry?>(){} must override a superclass method LoaderCustom?.java /ApiDemos?/src/com/example/android/apis/app line 152 Java Problem”。 请问作者,这是什么原因造成的,又该如何消除呢?期待着您能解答,谢谢!


Sign in to add a comment
Powered by Google Project Hosting