Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 學習筆記(1):Windows 開發環境建置與第一支 Hello World

編輯:Android 開發實例

從零開始的環境準備

要寫 Android,得先把三塊拼圖湊齊:JDK、Android SDK,以及一個稱手的 IDE。本文以 Windows 為示範平台,把環境、專案結構到第一支程式完整走一遍。

  • Windows 底下從零架起 Android 開發環境
  • Android 專案的目錄結構與各檔案職責
  • 動手寫一支最小可運行的 Hello World

一、環境建置十三步

1. 安裝 JDK

Android 的語言基礎是 Java,先安裝 Java Development Kit:https://download.java.net/jdk6/

2. 安裝 Android SDK

下載點:https://developer.android.com/sdk

3. 安裝 Eclipse

下載點:https://www.eclipse.org/

4. 為 Eclipse 掛上 ADT 插件

開啟 Eclipse,走 "Help" → "Install New Software",在 "Available Software" 填入 https://dl-ssl.google.com/android/eclipse/,接著安裝 ADT(Android Development Tools)。裝完重啟 Eclipse,記得回偏好設定把 SDK 路徑指對。

5. 建立 Android 專案

"New" → Android Project,各欄位意義如下:

  • Project Name:專案名稱
  • Build Target:編譯時採用的 SDK 版本
  • Application name:對外顯示的程式名稱
  • Package name:Java 套件名稱
  • Min SDK Version:支援的最低 SDK 代號(2 對應 1.1、3 對應 1.5、4 對應 1.6)

6. 執行專案

"Run" → "Run Configurations" → New launch configuration,設定啟動專案名稱,在 Android 頁籤挑選要跑的專案,再到 Target 頁籤指定模擬器。

7. 建立並掛載模擬 SD 卡

建立映像檔,例如:

mksdcard -l sdcard 512M d:\android\sdcard.img

要讓模擬器讀到它,在專案設定的 Target 頁籤「Additional Emulator Command Line Options」欄位填入:

-sdcard d:\android\sdcard.img

8. 設定模擬器(AVD)

可以用指令建立:

android create avd --name android15 --target 2

或者直接走 "Window" → "Android AVD Manager" 用圖形介面設定,後者對新手更友善。

9. 瀏覽模擬 SD 卡內容

除錯時開啟 DDMS 的 "File Explorer",其中的 sdcard 目錄就是模擬卡的檔案樹。

10. 看日誌 LogCat

Window → Show View → Other → Android → LogCat。這支視窗幾乎是日常除錯的主戰場。

11. 安裝與移除 apk

adb install name.apk adb uninstall packagename

移除時的參數要填「套件名稱」,不是 apk 檔名,這點常有人搞錯。

12. 反編譯既有程式

apk 本質上是 zip,解壓後取出 classes.dex,再執行:

dexdump.exe -d classes.dex > dump.txt

結果會被導出到指定的文字檔。

13. 常見錯誤排除

  • 出現 no classfiles specified 或 Conversion to Dalvik format failed with error 1:執行 Project → Clean 重建一次
  • 卡在 Android SDK Content Loader 60%:到 Project 選單取消勾選 Build Automatically

另外,想看 SDK 原始碼的話,取得 source 後解壓到 SDK 根目錄下的 sources 資料夾,IDE 就能關聯跳轉了。

二、專案目錄結構速覽

  • src:放自己的程式碼
  • gen:自動產生的 R.java,用來以常數方式引用 res 底下的資源,這個檔案不要手動改
  • assets:放原始檔案,系統完全不處理,這是它與 res 最大的差別
  • res/drawable 放圖片、res/layout 放佈局 XML、res/values 放常數與字串
  • AndroidManifest.xml:等於程式的設定檔,應用程式名稱、圖示、Activity、Service、Receiver 都在這裡宣告

三、Hello World 實作

佈局檔 res/layout/main.xml。設定 ID 用 @ id/ 前綴,引用字串資源用 @string/:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@ id/layout">
    <TextView
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello" />
    <TextView
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@ id/txt" />
</LinearLayout>

字串資源 res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">layout 直接調用 values 中的字符串</string>
    <string name="hello2">編程方式調用 values 中的字符串</string>
    <string name="app_name">webabcd_hello</string>
</resources>

再準備一張 icon.png 放進 res/drawable,然後是清單檔 AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
      package="com.webabcd.hello"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Main"
                  android:label="@string/app_name">
                <intent-filter>
                     <action android:name="android.intent.action.MAIN" />
                     <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
           </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
</manifest>

最後是 Main.java,示範三件事:載入佈局、動態加元件、以程式碼讀取字串資源。

package com.webabcd.hello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 把指定的佈局檔設為 Activity 的顯示內容
        setContentView(R.layout.main);

        // 動態建立元件並塞進既有容器
        TextView txt = new TextView(this);
        txt.setText("動態添加控件");
        // setContentView(txt); // 也可以直接把它當成整個畫面
        ((LinearLayout)this.findViewById(R.id.layout)).addView(txt);

        // 以程式碼讀取字串資源
        TextView txt1 = (TextView)this.findViewById(R.id.txt);
        txt1.setText(this.getString(R.string.hello2));
    }
}

跑起來之後你會看到畫面上出現兩段文字:一段來自 XML 的靜態引用,一段來自 Java 的動態寫入。理解這兩條路徑的差異,之後所有 Android 開發都建立在這個基礎上。

[源碼下載]

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved