最近想要研究一下Google放出來的Android平台,他是個開放源碼的作業系統
個人很看好這塊市場,趁現在還燙著趕緊來研究一下
-------------------------
這邊限定1.5版,因為方法與1.1版有異,以後搞不好會再更改
在開始之前先簡單透視一下Android SDK的架構

這張圖是『旗標:Android程式設計與應用』上的一章示意圖
Android是用Linux 2.6X版的kernel為基礎發展出來的平台,也有人說他就是Google版的Linux
但他用的是
ASL(apache software license)授權方式而非Linux常用的GPL授權
往上在架構了lib層跟他自己的run-time環境,最後在搭上一套Framework就成了Android平台
而Android SDK開發要有幾個要件
- Android Linux系統核心
- Android模擬器
- Eclipse
- Dalvik虛擬機器
- Android除錯工具
Android目前總共有幾個系統上的SDK Windows,Mac OS,跟Linux
在此先介紹Windows版的安裝法
首先呢請先去他的網站下載SDK
http://developer.android.com/sdk/1.5_r1/index.html
請選擇相對應的版本把他下載下來

直接在
I agree to the terms of the Android SDK License Agreement.上打勾
然後按Download就可以下載了
之後把他解壓到適當的目錄下,在此假設我解壓到E:\Android之下
隨後會產生android-sdk-windows-1.5_r1這個子目錄

再來我們需要Eclipse,但是我們還會需要Eclipse的JDT跟WTP兩個開發工具
不過沒關係我們去下載Eclipse的的Classic這個版本他就會替我們包好了
http://www.eclipse.org/downloads/
目前的版本為3.4.2 詳細版本號可能會變,請下載最新那版,然後同樣解壓到E:\Android那個目錄下

如果之前沒有安裝過JAVA JDK的話請先去下載安裝
http://java.sun.com/javase/downloads/index.jsp版本挑1.5以上的
最後請安裝用Eclipse請安裝Android ADT開發工具
首先請先去剛才解壓的eclipse目錄下點選eclipse.exe開啟他
一開始會要我們選workspace,請照自己喜歡的創造
再來要安裝Android ADT了,先點選Help->Software Updates...

跳出視窗後先後點選Available Software->Add Site
之後輸入http://dl-ssl.google.com/android/eclipse/
按下OK(因為我之前已經輸入過了 所以圖片出現了重覆的訊息)

然後他會出現Android的選項,全部勾一勾按install

之後只要等待安裝完成
接下來呢必須設定SDK路徑
從Eclipse上方的選單中 Windows->Preferences會跳出一個視窗
點選Android,在SDK Location的地方設定剛剛Android SDK的位置
之後按下Apply在按下OK及設定完成

然後呢 要開始新增第一個Android的部分了
這邊開始就跟『旗標:Android程式設計與應用』有些不同了,因為這本書上提到的是1.1的安裝法
如果照書上直接去啟動Android的模擬器(在E:\Android\android-sdk-windows-1.5_r1\tool下的emulator.exe)會出現錯誤訊息
emulator: ERROR: You did not provide the name of an Android Virtual Device
with the -avd <name> option. Read -help-avd for more information.
If you *really* want to *NOT* run an AVD, consider using -data <file>
to specify a data partition image file (I hope you know what you're doing).
在這邊首先要用命令提示字元(開始->執行 輸入cmd)去創建一個avd
進入命令提示字元之後移動到SDK tool的目錄下(E:\Android\android-sdk-windows-1.5_r1\tool)輸入
android create avd -n test -t 2
他會出現訊息
Android 1.5 is a basic Android platform.
Do you wish to create a custom hardware profile [no]
直接用他默認的選項,按下Enter,就會出現創建成功的訊息
Created AVD test based on Android 1.5

在這邊因為我已經創建過了,所以我用test2取代
再來就可以創造一個Android Project了
選擇File->new->Project ,跳出一個視窗後選擇Android->Android Project

再來會跳出輸入相關參數的視窗,裡面有幾個一定要選的

- Project name:隨便打,主要是在eclipse裡面的專案名稱
- Build Target:SDK版號 選1.5版
- Application name:隨便打
- Package name:隨便打,但是要是網址的形式,主要是創建package用
- Create Activity:隨便打,創建出來class的名子
下面是創建出來後的對照

先用一段測試的程式碼
public class Test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setText("Hello World!");
setContentView(tv);
}
}
然後要執行的話,從上面的Bar裡面選擇Run->Run Configutrations或是點選start那個綠色按鈕旁邊的小三角形選Run Configutrations

跳出選單後先點選右上的小圖示(New Launch configuration)創建一個新的config

接著輸入Config的名子,按下Brower選取剛剛我們創建出來的專案按下Apply

之後點選Target,之後選擇剛剛創建出來的avd,按下Apply之後就可以按下Run來執行了

模擬器出來之後,一開始是Android的畫面,用滑鼠按一下

接下來會進入開機的畫面

之後要執行程式,先點選被框起來那個小圖

再來跳出的選單中可以看到已經安裝我們剛剛創造的Project,點下去就可以執行了


------------------
這裡打個小廣告,Eclipse真的是很好用的開發工具,從Java、C\C++甚至PHP等等都可以用他來開發,pulg-in,一直在增加,想要開發Web Service還是J2ME、UML架構都可以包辦
這次Google選擇他當主力開發環境實在令人高興,Eclipse可以幫程式設計師提升寫程式的速度