zoukankan      html  css  js  c++  java
  • 【Android应用开发技术:基础构建】命令行下的Android应用开发

    作者:郭孝星
    微博:郭孝星的新浪微博
    邮箱:allenwells@163.com
    博客: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_studioMyApp --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 esapk资源包中

    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:myprojectin文件夹下的所有二进制文件转换为D:文件夹下的my.dex文件。

    dex -dex --dump-to=D:my.dex --core-library D:myprojectin

    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和数字证书的别名。

    签名流程例如以下图所看到的:

    这里写图片描写叙述

  • 相关阅读:
    《自己动手写操作系统》:开发环境配置心得
    sip.conf配置详情
    MySQL字符串中数字排序的问题
    Asterisk iax错误提示
    Python 快速入门
    C# winfrom 导出word
    SetWindowsHookEx函数参数详解
    Ubuntu Linux系统下轻松架设nginx+php服务器应用
    TShockwaveFlash的使用
    检讨
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7053724.html
Copyright © 2011-2022 走看看