1. 程式人生 > >【Android應用開發技術:基礎構建】命令行下的Android應用開發

【Android應用開發技術:基礎構建】命令行下的Android應用開發

star andro all class文件 abs 2.3 pil data 資源

作者:郭孝星
微博:郭孝星的新浪微博
郵箱:[email protected]
博客:http://blog.csdn.net/allenwells
github:https://github.com/AllenWell

一 AVD管理

AVD管理相關命令例如以下所看到的:

  • android avd : 打開AVD管理器
  • android list : 列舉已經安裝的Android版本號和Android模擬器
  • android list avd : 列舉已經安裝的Android模擬器
  • android list device : 列舉已經連接的Android設備
  • android create avd : 創建Android模擬器
  • android move avd : 移動或重命名Android模擬器
  • android delete avd : 刪除Android模擬器
  • android update avd : 更新Android模擬器

二 SDK管理

SDK管理相關命令例如以下所看到的:

  • android sdk : 打開SDK管理器
  • android list sdk : 列舉遠程SDK版本號
  • android list target : 列舉已經安裝的Android版本號
  • android update sdk : 更新SDK

三 SD卡管理

當我們創建一個AVD時。系統會自己主動創建虛擬存儲卡,假設我們想單獨創建虛擬存儲卡,能夠使用命令mksdcard。

命令mksdcard語法格式例如以下所看到的:

mksdcard [-l label] <size> <file>
  • size:執行虛擬SD卡大小
  • file:執行保存虛擬SDka的文件鏡像

舉例

創建一個60MB的SD卡,並將其鏡像保存到D:\sdcard.img。

mksdcard 64M D:\sdcard.img

四 設備調試

ADB(Android Debug Bridge)是有Google官方推出的調試工具,借助該工具能夠管理Android設備或模擬器的狀態。進行系統升級、應用管理等操作。

ADB的工作原理是監聽Socket TCP5544端口的方式讓IDE或DOS和Qemu進行通信。進而控制設備的狀態。

4.1 AVD操作命令

1 顯示系統中所有Android平臺

android list targets

2 顯示系統中所有AVD

android list avd

3 創建AVD

android create avd --name 名稱 --target 平臺編號

4 啟動AVD

emulator -avd 名稱 -sdcard ~/名稱.img (-skin 1280x800)

5 刪除AVD

android delete avd --name 名稱

6 搜索模擬器/設備的實例。取得當前執行的模擬器/設備的實例的列表及每一個實例的狀態

adb devices

7 獲取設備的ID和序列號

adb get-product 
adb get-serialno

4.2 APK操作命令

1 安裝APK

adb install -r 應用程序.apk

2 缷載apk包

adb shell
cd data/app
rm apk包
exit
adb uninstall apk包的主包名
adb install -r apk包

3 刪除系統應用

adb remount (又一次掛載系統分區,使系統分區又一次可寫)
adb shell
cd system/app
rm *.apk

4 啟動Activity

adb shell am start -n 包名/包名+類名(-n 類名,-a action,-d date,-m MIME-TYPE,-c category,-e 擴展數據等)。

5 啟動SDK,文檔,實例下載管理器

android

2.3 系統操作命令

1 獲取模擬器中的文件

adb pull <remote> <local>

從設備或模擬器上復制一個文件或文件夾:

adb pull <source> <destination></destination></source>

比如:

adb pull /addroid/lib/libwebcore.so

2 向模擬器中寫文件

adb push <local> <remote>

一個設備或從一個設備中拷貝文件, 復制一個文件或文件夾到設備或模擬器上:

adb push <source> <destination></destination></source>

比如:

adb push test.txt /tmp/test.txt

3 進入模擬器的shell模式

adb shell

4 查看adb命令幫助信息

adb help

5 在命令行中查看LOG信息

adb logcat -s 標簽名

6 獲取管理員權限

adb root

7 公布端口

你能夠設置隨意的端口號,做為主機向模擬器或設備的請求端口。

如: adb forward tcp:5555 tcp:8000。

8 查看bug報告

adb bugreport

9 記錄無線通訊日誌

一般來說。無線通訊的日誌許多,在執行時不是必需去記錄,但我們還是能夠通過命令,設置記錄:

adb shell 
logcat -b radio

10 訪問數據庫SQLite3

adb shell 
sqlite3

五 項目管理

5.1 項目創建

Android項目創建相關命令例如以下所看到的:

  • android create project : 創建項目
  • android update project : 更細項目
  • android create test-project : 創建測試項目
  • android update test-project : 更新測試項目
  • android create lib-project : 創建庫項目
  • android update lib-project : 更新庫項目
  • android create uitest-project: 創建UI測試項目
  • android update adb : 更新ADB以支持在SDK add-ons定義的USB設

舉例

創建Android工程

註意:把SDK_HOME/tools和SDK_HOME/platfrom-tools加入到系統Path裏,方便開發。下面兩個工具在tools文件夾下。

執行命令:

android create project --target <target-id> --name MyApp \
--path <path-to-workspace>/MyApp --activity MyActivity \
--package com.allenwells.myapp

替換 ,替換 為你想要保存項目的路徑。例如以下所看到的:

android create project --target android-22 --name MyApp --path D:\workplace_studio\MyApp --activity MyActivity --package com.allenwells.myapp

命令行顯示工程創建流程,例如以下圖所看到的:

技術分享

創建後的項目工程文件夾結構例如以下圖所看到的:

技術分享

5.2 資源打包

Android的資源打包工具是aapt。

經常使用語法格式例如以下所看到的:

aapt -A <附件資源路徑> -S <資源路徑> -M <Android應用清單文件> -I <額外附加的包> -F <目標文件路徑>
  • aapt l[ist] [-v] [-a] file.{zip,jar,apk}

列出資源壓縮包裏的內容。

  • aapt d[ump] [–values] WHAT file.{apk} [asset [asset …]]

查看APK裏的內容

badging          Print the label and icon for the app declared in APK.
permissions      Print the permissions from the APK.
resources        Print the resource table from the APK.
configurations   Print the configurations in the APK.
xmltree          Print the compiled xmls in the given assets.
xmlstrings       Print the strings of the given compiled xml assets.
  • aapt p[ackage] [-d][-f][-m][-u][-v][-x][-z][-M AndroidManifest.xml] \
    [-0 extension [-0 extension …]] [-g tolerance] [-j jarfile] \
    [–debug-mode] [–min-sdk-version VAL] [–target-sdk-version VAL] \
    [–app-version VAL] [–app-version-name TEXT] [–custom-package VAL] \
    [–rename-manifest-package PACKAGE] \
    [–rename-instrumentation-target-package PACKAGE] \
    [–utf16] [–auto-add-overlay] \
    [–max-res-version VAL] \
    [-I base-package [-I base-package …]] \
    [-A asset-source-dir] [-G class-list-file] [-P public-definitions-file]\
    [-S resource-sources [-S resource-sources …]] \
    [-F apk-file] [-J R-file-dir] \
    [–product product1,product2,…] \
    [-c CONFIGS] [–preferred-configurations CONFIGS] \
    [raw-files-dir [raw-files-dir] …] \
    [–output-text-symbols DIR]

打包生成資源壓縮包

  • aapt r[emove] [-v] file.{zip,jar,apk} file1 [file2 …]

從壓縮包中刪除指定文件。

  • aapt a[dd] [-v] file.{zip,jar,apk} file1 [file2 …]

向壓縮包加入指定文件。

  • aapt c[runch] [-v] -S resource-sources … -C output-folder …

處理PNG資源並把處理結果保存到外部文件夾中。

  • aapt v[ersion]

打印aapt版本號。

具體選項參見:

 Modifiers:
   -a  print Android-specific data (resources, manifest) when listing
   -c  specify which configurations to include.  The default is all
       configurations.  The value of the parameter should be a comma
       separated list of configuration values.  Locales should be specified
       as either a language or language-region pair.  Some examples:
            en
            port,en
            port,land,en_US
       If you put the special locale, zz_ZZ on the list, it will perform
       pseudolocalization on the default locale, modifying all of the
       strings so you can look for strings that missed the
       internationalization process.  For example:
            port,land,zz_ZZ
   -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
ed
       and the first match found (left to right) will take precedence.
   -0  specifies an additional extension for which such files will not
       be stored compressed in the .apk.  An empty string means to not
       compress any files at all.
   --debug-mode
       inserts android:debuggable="true" in to the application node of the
       manifest, making the application debuggable even on production devices.
   --min-sdk-version
       inserts android:minSdkVersion in to manifest.  If the version is 7 or
       higher, the default encoding for resources will be in UTF-8.
   --target-sdk-version
       inserts android:targetSdkVersion in to manifest.
   --max-res-version
       ignores versioned resource directories above the given value.
   --values
       when used with "dump resources" also includes resource values.
   --version-code
       inserts android:versionCode in to manifest.
   --version-name
       inserts android:versionName in to manifest.
   --custom-package
       generates R.java into a different package.
   --extra-packages
       generate R.java for libraries. Separate libraries with ‘:‘.
   --generate-dependencies
       generate dependency files in the same directories for R.java and resource
 package
   --auto-add-overlay
       Automatically add resources that are only in overlays.
   --preferred-configurations
       Like the -c option for filtering out unneeded configurations, but
       only expresses a preference.  If there is no resource available with
       the preferred configuration then it will not be stripped.
   --rename-manifest-package
       Rewrite the manifest so that its package name is the package name
       given here.  Relative class names (for example .Foo) will be
       changed to absolute names with the old package so that the code
       does not need to change.
   --rename-instrumentation-target-package
       Rewrite the manifest so that all of its instrumentation
       components target the given package.  Useful when used in
       conjunction with --rename-manifest-package to fix tests against
       a package that has been renamed.
   --product
       Specifies which variant to choose for strings that have
       product variants
   --utf16
       changes default encoding for resources to UTF-16.  Only useful when API
       level is set to 7 or higher where the default encoding is UTF-8.
   --non-constant-id
       Make the resources ID non constant. This is required to make an R java cl
ass
       that does not contain the final value but is used to make reusable compil
ed
       libraries that need to access resources.
   --error-on-failed-insert
       Forces aapt to return an error if it fails to insert values into the mani
fest
       with --debug-mode, --min-sdk-version, --target-sdk-version --version-code

       and --version-name.
       Insertion typically fails if the manifest already defines the attribute.
   --output-text-symbols
       Generates a text file containing the resource symbols of the R class in t
he
       specified folder.
   --ignore-assets
       Assets to be ignored. Default pattern is:
       !.svn:!.git:!.ds_store:!*.scc:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~

舉例

將當前文件夾下assets子文件夾、res子文件夾、AndroidManifest.xml文件都打包到bin\resapk資源包中

aapt -A assets -S res -M AndroidManifest.xml -I D:\my.jar -F bin/resapk

5.3 項目編譯

Android虛擬機並不直接執行Java的.class文件,它通過DX工具打包.class文件後執行。

DX工具用於將Android應用的.class文件轉換為.dex文件。

dx命令格式例如以下所看到的:

dx --dex [--dump-to=<file>] [--core-library] [<file>.class | <file>.{zip,jar,apk} | <directory>}

舉例

將D:\myproject\bin文件夾下的所有二進制文件轉換為D:\文件夾下的my.dex文件。

dex -dex --dump-to=D:\my.dex --core-library D:\myproject\bin

5.4 應用簽名

Android應用的簽名工具是jarsigner。簽名之前我們須要創建一個keystore庫。

5.4.1 生成keystore庫

keytool是密鑰和證書管理工具。

keytool命令選項例如以下所看到的:

  • -alias: 生成證書的別名
  • -keyalg: 生成證書的算法
  • -validity: 生成證書的有效期
  • -keystore: 生成證書的具體路徑
  • -certreq: 生成證書請求
  • -changealias: 更改條目的別名
  • -delete: 刪除條目
  • -exportcert: 導出證書
  • -genkeypair: 生成密鑰對
  • -genseckey: 生成密鑰
  • -gencert: 依據證書請求生成證書
  • -importcert: 導入證書或證書鏈
  • -importpass: 導入口令
  • -importkeystore: 從其它密鑰庫導入一個或所有條目
  • -keypasswd: 更改條目的密鑰口令
  • -list: 列出密鑰庫中的條目
  • -printcert: 打印證書內容
  • -printcertreq: 打印證書請求的內容
  • -printcrl: 打印 CRL 文件的內容
  • -storepasswd: 更改密鑰庫的存儲口令

舉例

創建一個名為my.keystore的keystore庫

keytool -genkeypair -alias my.keystore -keyalg RSA -validity 400 -keystore my.keystore

命令執行後會提示我們填寫一系列的東西,例如以下圖所看到的:

技術分享

填寫完畢後。能夠發如今當前文件夾下生成了my.keystore文件,例如以下圖所看到的:

技術分享

5.4.2 用keystore庫給應用簽名

jarsigner:應用簽名工具

jarsigner命令使用方法例如以下所看到的:

jarsigner [選項] jar-file 別名
jarsigner -verify [選項] jar-file [別名...]

jarsigner命令相關選項例如以下所看到的:

  • [-keystore ]: 密鑰庫位置
  • [-storepass <口令>]: 用於密鑰庫完整性的口令
  • [-storetype <類型>]: 密鑰庫類型
  • [-keypass <口令>]: 私有密鑰的口令 (假設不同)
  • [-certchain <文件>]: 替代證書鏈文件的名稱
  • [-sigfile <文件>]: .SF/.DSA 文件的名稱
  • [-signedjar <文件>]: 已簽名的 JAR 文件的名稱
  • [-digestalg <算法>]: 摘要算法的名稱
  • [-sigalg <算法>]: 簽名算法的名稱
  • [-verify]: 驗證已簽名的 JAR 文件
  • [-verbose[:suboptions]]: 簽名/驗證時輸出具體信息。

    選項能夠是 all, grouped 或 summary

  • [-certs]: 輸出具體信息和驗證時顯示證書
  • [-tsa ]: 時間戳頒發機構的位置
  • [-tsacert <別名>]: 時間戳頒發機構的公共密鑰證書
  • [-tsapolicyid ]: 時間戳頒發機構的 TSAPolicyID
  • [-altsigner <類>]: 替代的簽名機制的類名
  • [-altsignerpath <路徑列表>]: 替代的簽名機制的位置
  • [-internalsf]: 在簽名塊內包括 .SF 文件
  • [-sectionsonly]: 不計算整個清單的散列
  • [-protected]: 密鑰庫具有受保護驗證路徑
  • [-providerName <名稱>]: 提供方名稱
  • [-providerClass <類>: 加密服務提供方的名稱
  • [-providerArg <參數>]]… 主類文件和構造器參數
  • [-strict]: 將警告視為錯誤

舉例

用my.keystore給my.apk簽名

jarsigner -verbose -keystore my.keystore -signedjar my_signed.apk  my.apk my.keystore
  • -verbose:執定生成具體輸出
  • -keystore:指定生成數字證書的存儲路徑
  • -signedjar:該選項後的三個參數分別為:簽名後的APK、未簽名的APK和數字證書的別名。

簽名流程例如以下圖所看到的:

技術分享

【Android應用開發技術:基礎構建】命令行下的Android應用開發