[expand title="發表日期:2013/12 最後更新:2015/01/31" alt="點選觀看詳細更新記錄"]
- 2013/12, 新增文章
- 2014/10/07, 增加JAVA_HOME設定連結
- 2015/01/31, 更新ADT下載站點網址
[/expand]
0.目錄
1. 安裝環境
2.下載Eclipse 4.3.1 並安裝
3.安裝ADT –Eclipse plugin
4.安裝Android SDK
5.開啟新專案
6.在手機上運行
7.執行專案
- 需要準備的檔案:
- Android SDK : http://developer.android.com/sdk/index.html
- (PS. 20131030版的Android sdk已經附上Eclipse,可直接下載Android SDK壓縮檔解壓縮即可)
- 最新版Eclipse : http://www.eclipse.org/downloads/
- 如果無法執行Eclipse請先安裝並設定 Java 環境 http://www.oracle.com/technetwork/java/index.html
- JAVA_HOME設定請參考這篇:[Windows]設定JAVA_HOME(No JVM installation found)
1. 安裝環境
- Windows 7 X64
- Eclipse 4.3.1
- 要執行Eclipse需要JAVA環境,請自 http://www.oracle.com/technetwork/java/index.html 下載(安裝環境是java1.6.3)
- 設備:手機HTC The New One (SDK : 4.3)
2.下載Eclipse 4.3.1 並安裝
- http://www.eclipse.org/downloads/
- 下載後解壓縮放到指定的資料夾。如:D:⁄eclipse
- 執行eclipse.exe,這個版本代號是KEPLER(開普勒,日耳曼天文學家,提出行星運動三大定律。終結傳統的周轉圓理論,開創天文的新紀元。)
[caption id="attachment_257" align="aligncenter" width="451"] 版本代號是KEPLER[/caption]
- 第一次執行會詢問workspace要放在哪裡,設定好自己偏好路徑。可以點選 "Use this as the default and do not ask again." 下次就不會再詢問[caption id="attachment_258" align="aligncenter" width="905"]
第一次開啟畫面[/caption]
3.安裝ADT –Eclipse plugin
- 開始之前可以先 Check for Update 確定Eclipse環境為最新版。
- 點選Help > Install New Software
[caption id="attachment_259" align="aligncenter" width="276"] 安裝ADT –Eclipse plugin[/caption]
- 先新增一個站台
- 點選Add新增(開啟Add Repository畫面)
- Name自定
- Location輸入:https://dl-ssl.google.com/android/eclipse/
[caption id="attachment_260" align="aligncenter" width="881"] 新增一個站台[/caption]
- OK後等待Pending取得資料,結束後就可以看到Developer Tools 跟 Plugins
[caption id="attachment_261" align="aligncenter" width="876"] 等待Pending取得資料[/caption]
- 全選打勾點選 Next ,預覽要下載的清單,點 Next 繼續
[caption id="attachment_262" align="aligncenter" width="878"] 預覽要下載的清單[/caption]
- 版權聲明,點選 "I accept the terms of the license agreements" 後點Finish開始安裝
[caption id="attachment_263" align="aligncenter" width="876"] 版權聲明[/caption]
- 接下來就會開始下載並自動安裝
[caption id="attachment_264" align="aligncenter" width="530"] 下載並自動安裝[/caption]
- 如果出現安全警告畫面點 OK 繼續,他只是跟你說安裝的軟體沒有簽證,可能會有安全性問題
[caption id="attachment_265" align="aligncenter" width="545"] 安全警告畫面[/caption]
- 全部完成後會出現要求重新啟動 Eclipse,點選 Yes 重新啟動(套用)
[caption id="attachment_266" align="aligncenter" width="521"] 重新啟動 Eclipse[/caption]
4.安裝Android SDK
- 重新開啟後可能出現以下錯誤,因為Android SDK沒有被設定(當然,因為還沒安裝)
[caption id="attachment_267" align="aligncenter" width="615"] 重新開啟後可能出現錯誤[/caption]
- 下載 Android SDK
- http://developer.android.com/sdk/index.html
- 點選Download the SDK
[caption id="attachment_268" align="aligncenter" width="731"] 下載 Android SDK[/caption]
- 接受使用者授權合約、選擇版本後開始下載(檔案大小約480M)
[caption id="attachment_269" align="aligncenter" width="553"] 下載 Android SDK[/caption]
- 下載完成後解壓縮 adt-bundle-windows-x86-20131030.zip 到自己喜好資料夾 (我解壓縮到 D:/_ProgramBackup/SDK/Android-sdk-20131030)
- 點選 Open Preferences 或是點選 Window > Preferences 開啟設定畫面
[caption id="attachment_270" align="aligncenter" width="270"] 開啟設定畫面[/caption]
- 到Android > 輸入SDK Location ( 我是放到 D:/_ProgramBackup/SDK/Android-sdk-20131030 ),點選Apply 成功的話就會出現可選的版本
[caption id="attachment_271" align="aligncenter" width="843"] 輸入SDK Location[/caption]
- SDK 壓縮檔中附有 SDK Manager.exe。點兩下可以開啟管理程式,可以下載需要的API版本
[caption id="attachment_272" align="aligncenter" width="681"] 開啟管理程式[/caption]
- 選擇需要的版本按Install開始下載並安裝
[caption id="attachment_273" align="aligncenter" width="736"] 開始下載並安裝[/caption]
- 安裝完成自己需要的SDK後就可以回到Eclipse開始一個新的專案
5.開啟新專案
- 點選 File > Android Project
- 如果沒有則點選 File > Other
[caption id="attachment_274" align="aligncenter" width="538"] 開啟新專案[/caption]
- 選擇 Android > Android Application Project > Next
[caption id="attachment_275" align="aligncenter" width="528"] 開啟新專案[/caption]
- 接著輸入應用程式名稱(Application Name)、專案名稱(Project Name)、包裝名稱(Package Name),完成後設定Finish會產生一個Hello World的範例程式。
- 特別注意,Application Name是用於APP識別(如放上Play Store時使用),通常建議以大寫開頭(否則會出現黃色驚嘆號),後面不管大小寫或數字都可接受
- 特別注意,Project Name是用於Eclipse辨識,可以跟Application Name不同,可以是英文大小寫或數字
- 特別注意,Package Name是用於辨識用的唯一名稱,預設是"com.example.xxx",因為"com.example"是預設值可能與其他人設計的應用程式名稱重複所以會出現黃色驚嘆號表示警告。可以改成自己的公司或名稱作為識別就不會與他人衝突。可以有大小寫、底線或數字,不可以有空白。
- 底下分別是設定最小能安裝的SDK版本,如果APP有用到版本不相容的程式碼建議設定最小要求。往下是目標SDK、編譯使用的SDK版本以及主題。(在此都用預設值就好。PS. 筆者手機版本為4.3)
[caption id="attachment_276" align="aligncenter" width="634"] 開啟新專案[/caption]
6.在手機上運行
- 開啟專案後就可以直接執行,執行的方式有兩種,一種是使用模擬器,但這速度比較慢也可能與實際手機有不同,另一種就是使用實體設備。有鑑於現今智慧型手機的普及(應該不太有人沒有智慧型手機吧xd)所以接下來的範例都直接使用實機做介紹。
- 首先設定手機部分,開啟開發者模式
- 進入手機設定 > 開發人員選項 > 打勾"USB除錯中"
[caption id="attachment_277" align="aligncenter" width="222"] 設定開發人員選項[/caption]
- 有些手機會詢問是否允許USB除錯,點選允許後完成
- 電腦部分,開始安裝之前先確定Eclipse能不能抓到目標手機
- 首先把手機用USB傳輸線插到電腦上。
- 開啟 Window > Other
[caption id="attachment_278" align="aligncenter" width="539"] 開啟Device檢視視窗[/caption]
- 增加要顯示的視窗,選擇 Android > Devices
[caption id="attachment_279" align="aligncenter" width="324"] 開啟Device檢視視窗[/caption]
- 如果有抓到就會在 Devices 視窗中看到連接的設備名稱(或設備號碼),如果無法抓到請再次確認是否開啟"偵錯模式",並且安裝該手機最新版驅動程式(新手機通常會附在USB插入時出現install,例如HTC安裝完HTC Sync程式後就可以成功抓到設備)
[caption id="attachment_280" align="aligncenter" width="364"] 開啟Device檢視視窗[/caption]
7.執行專案
- 在左側 Package Explorer 找到目前的專案,點擊 右鍵 > Run as > 1. Android Application
[caption id="attachment_281" align="aligncenter" width="628"] 執行專案[/caption]
- 選擇要執行的設備,當然是選擇我們連接的設備,選擇後點OK
[caption id="attachment_282" align="aligncenter" width="706"] 選擇要執行的設備[/caption]
- 有可能出現是否要出現Logcat,點選Yes,這個是Run-time的log,對於手機開發的除錯非常有幫助
[caption id="attachment_283" align="aligncenter" width="628"] 有可能出現是否要出現Logcat[/caption]
- 等待應用程式傳輸到設備上,他會自動安裝並開啟,成功後就可以在設備上看到如下圖畫面
[caption id="attachment_284" align="aligncenter" width="222"] 成功畫面[/caption]
- 到這步就代表成功建置好環境可以開始應用程式開發了。
[caption id="attachment_287" align="aligncenter" width="259"] LIKE[/caption]