Android SDK提供了一系列命令行工具,用來進行Android應用的開發、調試、打包等工作!Eclipse能夠進行Android應用開發的相關工作,也是調用了這些工具而已.
只有掌握了命令下的工作方式,工作起來纔會更加的得心應手.下面就拋開Eclipse集成開發工具,徒手走一遍Android開發、編譯、打包的流程!
注:本文只作參考之用,因爲LBSDemo工程中有本人自己加入的代碼,有第三方jar包,so文件等等!故一步一步按照本文操作是行不通的!
1:創建Android應用程序
Android SDK提供了一個android.bat批處理工具,它可以用來處理以下工作,這裏只介紹它的子命令create project來進行程序的創建.
-
D:\Android>android --help
-
Usage:
-
android [global options] action [action options]
-
-
Global options:
-
-v --verbose Verbose mode: errors, warnings and informational messages are pr
-
inted.
-
-h --help Help on a specific command.
-
-s --silent Silent mode: only errors are printed out.
-
-
Valid actions are composed of a verb and an optional direct object:
-
- list : Lists existing targets or virtual devices.
-
- list avd : Lists existing Android Virtual Devices.
-
- list target : Lists existing targets.
-
- create avd : Creates a new Android Virtual Device.
-
- move avd : Moves or renames an Android Virtual Device.
-
- delete avd : Deletes an Android Virtual Device.
-
- update avd : Updates an Android Virtual Device to match the folders
-
of a new SDK.
-
- create project : Creates a new Android project.
-
- update project : Updates an Android project (must already have an Androi
-
dManifest.xml).
-
- create test-project : Creates a new Android project for a test package.
-
- update test-project : Updates the Android project for a test package (must al
-
ready have an AndroidManifest.xml).
-
- create lib-project : Creates a new Android library project.
-
- update lib-project : Updates an Android library project (must already have a
-
n AndroidManifest.xml).
-
- update adb : Updates adb to support the USB devices declared in the
-
SDK add-ons.
-
- update sdk : Updates the SDK by suggesting new platforms to install
- if available.
-
Action "create project":
-
Creates a new Android project.
-
Options:
-
-n --name 應用程序的名字
-
-t --target SDK Target ID
-
-p --path 應用程序的工作目錄
-
-k --package 應用程序的包名
- -a --activity 默認創建的Activity的名字
2:編譯Android應用程序 A:生成R.java文件Android程序代碼中會大量地引用各類資源文件,這些資源文件不是通過手動進行引用的,AndroidSDK提供了一個R.java類來進行資源的管理,方便程序員對資源文件的引用及管理,所以如果要讓程序編譯通過,首先就需要生成R.java類。 編譯R.java類需要用到AndroidSDK提供的aapt工具,aapt參數衆多,以下是主要參數:
-
-d one or more device assets to include, separated by commas
-
-f force overwrite of existing files
-
-g specify a pixel tolerance to force images to grayscale, default 0
-
-j specify a jar or zip file containing classes to include
-
-k junk path of file(s) added
-
-m make package directories under location specified by -J
-
-u update existing packages (add new, replace older, remove deleted files)
-
-v verbose output
-
-x create extending (non-application) resource IDs
-
-z require localization of resource attributes marked with
-
localization="suggested"
-
-A additional directory in which to find raw asset files
-
-G A file to output proguard options into.
-
-F specify the apk file to output
-
-I add an existing package to base include set
-
-J specify where to output R.java resource constant definitions
-
-M specify full path to AndroidManifest.xml to include in zip
-
-P specify where to output public resource definitions
-
-S directory in which to find resources. Multiple directories will be scann
- d
參數說明
-
-f -m -J gen :以覆蓋的形式在gen目錄下生成帶包路徑的R.java
-
-
-S res:指定資源文件
-
-
-I E:\Android\SDK\android-sdk_r09-windows\platforms\android-7\android.jar:使用指定版本的android jar包
-
- -M AndroidManifest.xml:指定程序的配置文件
-
-I<DIR> search path for import statements.
-
-d<FILE> generate dependency file.
-
-p<FILE> file created by --preprocess to import.
-
-o<FOLDER> base output folder for generated files.
- -b fail when trying to compile a parcelable.
C:用JAVASDK提供的javac命令工具將.java源文件編譯爲.class文件javac命令用法如下:
-
其中,可能的選項包括:
-
-g 生成所有調試信息
-
-g:none 不生成任何調試信息
-
-g:{lines,vars,source} 只生成某些調試信息
-
-nowarn 不生成任何警告
-
-verbose 輸出有關編譯器正在執行的操作的消息
-
-deprecation 輸出使用已過時的 API 的源位置
-
-classpath <路徑> 指定查找用戶類文件和註釋處理程序的位置
-
-cp <路徑> 指定查找用戶類文件和註釋處理程序的位置
-
-sourcepath <路徑> 指定查找輸入源文件的位置
-
-bootclasspath <路徑> 覆蓋引導類文件的位置
-
-extdirs <目錄> 覆蓋安裝的擴展目錄的位置
-
-endorseddirs <目錄> 覆蓋簽名的標準路徑的位置
-
-proc:{none,only} 控制是否執行註釋處理和/或編譯。
-
-processor <class1>[,<class2>,<class3>...]要運行的註釋處理程序的名稱;繞過默認
-
的搜索進程
-
-processorpath <路徑> 指定查找註釋處理程序的位置
-
-d <目錄> 指定存放生成的類文件的位置
-
-s <目錄> 指定存放生成的源文件的位置
-
-implicit:{none,class} 指定是否爲隱式引用文件生成類文件
-
-encoding <編碼> 指定源文件使用的字符編碼
-
-source <版本> 提供與指定版本的源兼容性
-
-target <版本> 生成特定 VM 版本的類文件
-
-version 版本信息
-
-help 輸出標準選項的提要
-
-Akey[=value] 傳遞給註釋處理程序的選項
-
-X 輸出非標準選項的提要
- -J<標誌> 直接將 <標誌> 傳遞給運行時系統
D:使用Android SDK提供的dx.bat命令行腳本生成classes.dex文件將工程bin目錄下的class文件編譯成classes.dex,Android虛擬機只能執行dex文件!
命令生成的.dex文件如下
E:使用Android SDK提供的aapt.exe生成資源包文件(包括res、assets、androidmanifest.xml等):aapt用法參考上面!
命令生成的.ap_文件如下:
F:利用AndroidSDK提供的apkbuilder工具生成未簽名的APK文件重要參數如下:
-
-v Verbose.
-
-d Debug Mode: Includes debug files in the APK file.
-
-u Creates an unsigned package.
-
-storetype Forces the KeyStore type. If ommited the default is used.
-
-
-z Followed by the path to a zip archive.
-
Adds the content of the application package.
-
-
-f Followed by the path to a file.
-
Adds the file to the application package.
-
-
-rf Followed by the path to a source folder.
-
Adds the java resources found in that folder to the application
-
package, while keeping their path relative to the source folder.
-
-
-rj Followed by the path to a jar file or a folder containing
-
jar files.
-
Adds the java resources found in the jar file(s) to the application
-
package.
-
-
-nf Followed by the root folder containing native libraries to
- include in the application package.
命令生成的.apk文件
H:利用JAVASDK提供的keytools工具生成證書文件!上面只是生成了一個沒有證書的apk文件,Android是不會去安裝一個沒有數字證書籤名的apk包的,所以需要安裝apk的話,必須用一個數字證書對其簽名後,纔可!
此處輸入命令後,此命令會與用戶產生交互,如果你不想產生交互的情況,那麼就要提前把命令想問的都告訴它! keytool -genkey -alias HelloWorld.keystore -keyalg RSA -validity 1000 -keystore HelloWorld.keystore -dname "CN=w,OU=w,O=localhost,L=w,ST=w,C=CN" -keypass 123456 -storepass 123456命令生成的.keystore文件如下:
I:最後一步,通過jarsigner命令用證書文件對未簽名的APK文件進行簽名重要參數如下:
-
[-keystore <url>] 密鑰庫位置
-
-
[-storepass <口令>] 用於密鑰庫完整性的口令
-
-
[-storetype <類型>] 密鑰庫類型
-
-
[-keypass <口令>] 專用密鑰的口令(如果不同)
-
-
[-sigfile <文件>] .SF/.DSA 文件的名稱
-
-
[-signedjar <文件>] 已簽名的 JAR 文件的名稱
-
-
[-digestalg <算法>] 摘要算法的名稱
-
-
[-sigalg <算法>] 簽名算法的名稱
-
-
[-verify] 驗證已簽名的 JAR 文件
-
-
[-verbose] 簽名/驗證時輸出詳細信息
-
-
[-certs] 輸出詳細信息和驗證時顯示證書
-
-
[-tsa <url>] 時間戳機構的位置
-
-
[-tsacert <別名>] 時間戳機構的公共密鑰證書
-
-
[-altsigner <類>] 替代的簽名機制的類名
-
-
[-altsignerpath <路徑列表>] 替代的簽名機制的位置
-
-
[-internalsf] 在簽名塊內包含 .SF 文件
-
-
[-sectionsonly] 不計算整個清單的散列
-
-
[-protected] 密鑰庫已保護驗證路徑
-
-
[-providerName <名稱>] 提供者名稱
-
-
[-providerClass <類> 加密服務提供者的名稱
- [-providerArg <參數>]] ... 主類文件和構造函數參數
大功告成,終於完成了!
總結:通過對這些命令的運用,你才能清楚地知道程序開發、編譯、打包的每一個細節、才知道Eclipse是如何工作的、就算換了一個集成開發工具、也能很快得心應手!甚至能開發一套與Eclipse裏打包一樣功能的工具呢!下面的就是本人對上面命令的一個總結!如果公司需要,你也可以動手寫一個!