歡迎您光臨本站 註冊首頁

Ant入門教程

←手機掃碼閱讀     火星人 @ 2014-03-12 , reply:0
  

看這篇文章之前,假設您已經具備下列條件,否則閱讀這篇文章對您幫助不會太大。
<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 命令
打開終端,敲入命令

  1. Android -h  
可以列出關於該命令的幫助及其用法,其中下面命令是這篇文章的重點
  1. create project: Creates a new Android project  
  2. update project: Updates an Android project (must already have an AndroidManifest.xml)  
接下來,我們看看這兩個命令的參數及其用法。打開終端,敲入命令
  1. Android -h create project  
可以看到,輸出幫助信息:
  1. Usage:  
  2.   Android [global options] create project [action options]  
  3.   
  4.   
  5. Global options:  
  6.   -v --verbose  Verbose mode: errors, warnings and informational messages are printed.  
  7.   -h --help     Help on a specific command.  
  8.   -s --silent   Silent mode: only errors are printed out.  
  9.   
  10.   
  11. Action "create project":  
  12.   Creates a new Android project.  
  13. Options:  
  14.   -n --name     Project name  
  15.   -t --target   Target ID of the new project [required]  
  16.   -p --path     The new project's directory [required]  
  17.   -k --package  Android package name for the application [required]  
  18.   -a --activity Name of the default Activity that is created [required]  
同理,可以看看另一個命令的用法。
  1. Usage:  
  2.   Android [global options] update project [action options]  
  3.   
  4.   
  5. Global options:  
  6.   -v --verbose  Verbose mode: errors, warnings and informational messages are printed.  
  7.   -h --help     Help on a specific command.  
  8.   -s --silent   Silent mode: only errors are printed out.  
  9.   
  10.   
  11. Action "update project":  
  12.   Updates an Android project (must already have an AndroidManifest.xml).  
  13. Options:  
  14.   -p --path        The project's directory [required]  
  15.   -l --library     Directory of an Android library to add, relative to this project's directory  
  16.   -n --name        Project name  
  17.   -t --target      Target ID to set for the project  
  18.   -s --subprojects Also updates any projects in sub-folders, such as test projects.  
2. 創建項目
在/home/mark路徑下,創建Android項目,詳情如下:
工程名稱        :TestAntAndroidActivity
名稱                :TestActivity
包名稱            :mark.zhangAndroid  版本    :4,即 android1.5
那麼,在終端只需要:
  1. 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文件
  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     Android:orientation="vertical"  
  4.     Android:layout_width="fill_parent"  
  5.     Android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     Android:layout_width="fill_parent"   
  9.     Android:layout_height="wrap_content"   
  10.     Android:text="this is my ant compile android app"  
  11.     Android:textSize="20sp"  
  12.     Android:textColor="#aa000a"  
  13.     />  
  14. LinearLayout>  
提示:執行 Androidlist target 可以查看安裝的sdk版本
3.編譯項目
只需要兩條簡單命令,呵呵!
  1. cd /home/mark/TestAntAndroid/  
  2. ant debug  
進入目錄/home/mark/TestAntAndroid/bin,可以看到 ak 文件:
4. 安裝 apk
將上面的 apk 文件安裝到模擬器,驗證是否可行。
  1. cd /home/mark/TestAntAndroid/bin  
  2.   
  3.   
  4. adb install TestAntAndroid-debug.apk  
5. 更新已有工程
如果 Android 工程已經存在,可以 update project(修改平台的版本),這樣會自動修改 build.xml 等 ant 的配置文件
  1. android update project -n TestAntAndroid -t 11 -p /home/mark/TestAntAndroid/  

注意: -t 11 表示使用 Android-11,當然你可以使用其他版本來更新工程。但是,有時候更新一個工程不使用 android 高版本來更新的話,項目會報錯。

控制台顯示信息:

  1. Updated default.properties  
  2. Updated local.properties  
  3. File build.xml is too old and needs to be updated.  
  4. Updated file /home/mark/TestAntAndroid/build.xml  
  5. Updated file /home/mark/TestAntAndroid/proguard.cfg  


[火星人 ] Ant入門教程已經有370次圍觀

http://coctec.com/docs/program/show-post-71452.html