歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Ant入門教程

Ant入門教程

日期:2017/3/1 11:08:21   编辑:Linux編程

看這篇文章之前,假設您已經具備下列條件,否則閱讀這篇文章對您幫助不會太大。
<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. Global options:
  4. -v --verbose Verbose mode: errors, warnings and informational messages are printed.
  5. -h --help Help on a specific command.
  6. -s --silent Silent mode: only errors are printed out.
  7. Action "create project":
  8. Creates a new Android project.
  9. Options:
  10. -n --name Project name
  11. -t --target Target ID of the new project [required]
  12. -p --path The new project's directory [required]
  13. -k --package Android package name for the application [required]
  14. -a --activity Name of the default Activity that is created [required]
同理,可以看看另一個命令的用法。
  1. Usage:
  2. android [global options] update project [action options]
  3. Global options:
  4. -v --verbose Verbose mode: errors, warnings and informational messages are printed.
  5. -h --help Help on a specific command.
  6. -s --silent Silent mode: only errors are printed out.
  7. Action "update project":
  8. Updates an Android project (must already have an AndroidManifest.xml).
  9. Options:
  10. -p --path The project's directory [required]
  11. -l --library Directory of an Android library to add, relative to this project's directory
  12. -n --name Project name
  13. -t --target Target ID to set for the project
  14. -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. 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
Copyright © Linux教程網 All Rights Reserved