Android CLI 的開發環境安裝完成後(註: 可參考: Android CLI 開發環境安裝、建置), 再來來寫個簡單的 App 測試看看.
Android CLI 開發 App 範例 (Hello World)
在此先記錄些常用指令, Android CLI 常用開發基本指令記錄:
- android list targets
- android update project
- android update project --target your-target-id --name your-project-name --path /path/to/your/project
- android create project
- android create project --target android-19 --name MyFirstApp --path /home/user/dev/android/MyFirstApp --activity MainActivity --package com.example.myfirstapp
- ant compile: will compile your code and re-generate R.java.
- ant debug: will do the above, plus build and sign a package (apk) file.
- adb install bin/MyFirstApp-debug.apk # install
- adb logcat # 查看 Log
- ctags -R src gen # 想要有自動完成, 可以使用 Ctags for Autocompletion
寫 Android App 需要下述幾個步驟:
- 建立 Android Project (~/dev/android/PROJECT_NAME)
- 編譯、產生 APK
- 將 APK 安裝到 Android 模擬器
建立 Android Project
- 到此要確定有安裝 Android SDK 和 ADT Plugin 22.6.2 以上
- 參考:
- Building Your First App
- Creating an Android Project (文件下方有 Create a Project with Command Line Tools)
- cd ~/android/android-sdk-linux/tools
- android list targets
- ex: android create project --target <target-id> --name MyFirstApp \
--path <path-to-workspace>/MyFirstApp --activity MainActivity \
--package com.example.myfirstapp - # Project 開發目錄 我是選擇 ~/dev/android/PROJECT_NAME
- android create project --target android-19 --name MyFirstApp \
--path /home/user/dev/android/MyFirstApp --activity MainActivity \
--package com.example.myfirstappCreated project directory: /home/user/dev/android/MyFirstApp
/home/user/dev/android/MyFirstApp/src/com/example/myfirstapp/MainActivity.java
/home/user/dev/android/MyFirstApp/res/values/strings.xml
/home/user/dev/android/MyFirstApp/res/layout/main.xml
/home/user/dev/android/MyFirstApp/res/values/strings.xml
/home/user/dev/android/MyFirstApp/AndroidManifest.xml
/home/user/dev/android/MyFirstApp/build.xml
/home/user/dev/android/MyFirstApp/proguard-project.txt - 註1: 建議將 platform-tools、tools 目錄加入 PATH 的環境變數中, ex: export PATH=$PATH:~/android/android-sdk-linux/platform-tools:~/android/android-sdk-linux/tools
- 註2: 預設已經將 Hello World 都寫好了, 下述兩個檔案是文字、套件名稱位置
/home/user/dev/android/MyFirstApp/res/layout/main.xml: android:text="Hello World, MainActivity"
/home/user/dev/android/MyFirstApp/res/values/strings.xml: <string name="app_name">MainActivity</string>
編譯、產生 APK
- 參考: Running Your App
- vim /home/user/dev/android/MyFirstApp/AndroidManifest.xml # 看要不要加入下述, 限制版本
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> - cd /home/user/dev/android/MyFirstApp
- ant debug # 註1: 若有發生此 aapt 的問題, 解法於下述.
- 會產出 bin/MyFirstApp-debug.apk
- adb install bin/MyFirstApp-debug.apk # 安裝 adb install -r 可以 re-install 覆蓋
- 註1: 若出現下述兩個問題:
- Q1: android-sdk-linux/build-tools/20.0.0/aapt = no such file or director
- Q2: android-sdk-linux/build-tools/20.0.0/aapt: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory
- 解法參考自此篇: Issue 67155 - android - request for solution: unable to run aapt - Android Open Source Project - Issue Tracker
- 因為 aapt (file aapt) 是 32 bits 的, 所以於 64 bits 無法跑
- 所以安裝: apt-get install lib32z1 lib32stdc++6
將 APK 安裝到 Android 模擬器
- 註: 此前記得先 android avd 來啟動 Android 模擬器
- cd dev/android/MyFirstApp
- ant debug # 或 ant compile
- adb install bin/MyFirstApp-debug.apk # install
- adb install -r bin/MyFirstApp-debug.apk # install 覆蓋掉, 才不用一直移除、安裝
到此 App Hello World 就已經完成囉~ 再來就可以調整 UI, 作其他事情了, ex: Building a Simple User Interface