看這篇文章之前,假設您已經具備下列條件,否則閱讀這篇文章對您協助不會太大。
<1> ubuntu下,成功安裝JDK1.6並配置環境變數
<2> ubuntu下,成功下載、配置好 ant 環境
<3> ubuntu下,成功安裝android-sdk,並且配置好tools、platform-tools環境變數
好吧,開始ant開發android之旅!/home/mark/android/android-sdk-linux_x86是android_sdk安裝路徑。
1.android 命令
開啟終端,敲入命令
android -h
可以列出關於該命令的協助及其用法,其中下面命令是這篇文章的重點
create project: Creates a new Android projectupdate project: Updates an Android project (must already have an AndroidManifest.xml)
接下來,我們看看這兩個命令的參數及其用法。開啟終端,敲入命令
android -h create project
可以看到,輸出協助資訊:
Usage: android [global options] create project [action options]Global options: -v --verbose Verbose mode: errors, warnings and informational messages are printed. -h --help Help on a specific command. -s --silent Silent mode: only errors are printed out.Action "create project": Creates a new Android project.Options: -n --name Project name -t --target Target ID of the new project [required] -p --path The new project's directory [required] -k --package Android package name for the application [required] -a --activity Name of the default Activity that is created [required]
同理,可以看看另一個命令的用法。
Usage: android [global options] update project [action options]Global options: -v --verbose Verbose mode: errors, warnings and informational messages are printed. -h --help Help on a specific command. -s --silent Silent mode: only errors are printed out.Action "update project": Updates an Android project (must already have an AndroidManifest.xml).Options: -p --path The project's directory [required] -l --library Directory of an Android library to add, relative to this project's directory -n --name Project name -t --target Target ID to set for the project -s --subprojects Also updates any projects in sub-folders, such as test projects.
2. 建立項目
在/home/mark路徑下,建立android項目,詳情如下:
工程名稱 :TestAntAndroidActivity
名稱 :TestActivity
包名稱 :mark.zhangandroid 版本 :4,即 android1.5
那麼,在終端只需要:
android create project -k mark.zhang -n TestAntAndroid -a TestActivity -t 4 -p /home/mark/TestAntAndroid
ok,在/home/mark/下面就會建立TestAntAndroid工程目錄,其結構如下,與使用Eclipse/ADT建立項目是一樣的效果。
修改res/layout/main.xml檔案
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="this is my ant compile android app" android:textSize="20sp" android:textColor="#aa000a" /></LinearLayout>
提示:執行 androidlist target 可以查看安裝的sdk版本
3.編譯項目
只需要兩條簡單命令,呵呵!
cd /home/mark/TestAntAndroid/ant debug
進入目錄/home/mark/TestAntAndroid/bin,可以看到 ak 檔案:
4. 安裝 apk
將上面的 apk 檔案安裝到模擬器,驗證是否可行。
cd /home/mark/TestAntAndroid/binadb install TestAntAndroid-debug.apk
5. 更新已有工程
如果 android 工程已經存在,可以 update project(修改平台的版本),這樣會自動修改 build.xml 等 ant 的設定檔
android update project -n TestAntAndroid -t 11 -p /home/mark/TestAntAndroid/
注意: -t 11 表示使用 android-11,當然你可以使用其他版本來更新工程。但是,有時候更新一個工程不使用 android 高版本來更新的話,項目會報錯。
控制台顯示資訊:
Updated default.propertiesUpdated local.propertiesFile build.xml is too old and needs to be updated.Updated file /home/mark/TestAntAndroid/build.xmlUpdated file /home/mark/TestAntAndroid/proguard.cfg