Chinaunix首页 | 论坛 | 博客
  • 博客访问: 903990
  • 博文数量: 75
  • 博客积分: 6236
  • 博客等级: 准将
  • 技术积分: 726
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-03 14:43
文章分类

全部博文(75)

文章存档

2019年(1)

2018年(6)

2017年(4)

2015年(1)

2014年(9)

2013年(1)

2012年(3)

2011年(9)

2010年(3)

2009年(2)

2008年(4)

2007年(24)

2006年(8)

我的朋友

分类: Android平台

2014-04-23 15:45:23

Android SDK提供了一系列命令行工具,用来进行Android应用的开发、调试、打包等工作!Eclipse能够进行Android应用开发的相关工作,也是调用了这些工具而已.

只有掌握了命令下的工作方式,工作起来才会更加的得心应手.下面就抛开Eclipse集成开发工具,徒手走一遍Android开发、编译、打包的流程!

 

注:本文只作参考之用,因为LBSDemo工程中有本人自己加入的代码,有第三方jar包,so文件等等!故一步一步按照本文操作是行不通的!

 

1:创建Android应用程序

Android SDK提供了一个android.bat批处理工具,它可以用来处理以下工作,这里只介绍它的子命令create project来进行程序的创建.

android --help
Usage:
       android [global options] action [action options]
Global options:
  -h --help       : Help on a specific command.
  -v --verbose    : Verbose mode, shows errors, warnings and all messages.
     --clear-cache: Clear the SDK Manager repository manifest cache.
  -s --silent     : Silent mode, shows errors only.

Valid actions are composed of a verb and an optional direct object:
-    sdk              : Displays the SDK Manager window.
-    avd              : Displays the AVD Manager window.
-   list              : Lists existing targets or virtual devices.
-   list avd          : Lists existing Android Virtual Devices.
-   list target       : Lists existing targets.
-   list sdk          : Lists remote SDK repository.
- 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
                        AndroidManifest.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
                        already have an AndroidManifest.xml).
- create lib-project  : Creates a new Android library project.
- update lib-project  : Updates an Android library project (must already have
                        an AndroidManifest.xml).
- create uitest-project: Creates a new UI test project.
- 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.

 

--create project子命令的用法如下:

android -h create project
Usage:
       android [global options] create project [action options]

Action "create project":
  Creates a new Android project.
Options:
  -n --name          : Project name.
  -v --gradle-version: Gradle Android plugin version.
  -t --target        : Target ID of the new project. [required]
  -p --path          : The new project's directory. [required]
  -g --gradle        : Use gradle template.
  -k --package       : Android package name for the application. [required]
  -a --activity      : Name of the default Activity that is created.
                       [required]

 

了解了相关命令及其用法,现在就可以用之来创建应用程序:在D:\Android工作目录下创建LBSDemo程序

image

命令自动创建了如下内容:

image7

 

2:编译Android应用程序

 

A:生成R.java文件

Android程序代码中会大量地引用各类资源文件,这些资源文件不是通过手动进行引用的,AndroidSDK提供了一个R.java类来进行资源的管理,方便程序员对资源文件的引用及管理,所以如果要让程序编译通过,首先就需要生成R.java类。

 

编译R.java类需要用到AndroidSDK提供的aapt工具,aapt参数众多,以下是主要参数:

Usage:
 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]

   Package the android resources.  It will read assets and resources that are
   supplied with the -M -A -S or raw-files-dir arguments.  The -J -P -F and -R
   options control which files are output.

 Modifiers:
   -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 scanned
       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.

 

aapt编译R.java文件具体如下:

需要进入LBSDemo应用程序目录,新建一个gen目录,没有gen目录,命令将会出现找不到文件的错误!

命令成功执行后将会在gen目录下生成成包结构的目录树,及R.java文件!

image

 

参数说明

-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:指定程序的配置文件


B:根据.aidl定义文件生成java文件 

将.aidl文件生成.java文件需要用到AndroidSDK自带的aidl工具,此工具具体参数如下:
    
-I

    search path for import statements.
    -d   generate dependency file.
    -p   file created by --preprocess to import.
    -o base output folder for generated files.
    -b         fail when trying to compile a parcelable.
    值得注意的是:这个工具的参数与参数值之间不能有空格,Google也有对工资不满意的工程师!

  image

 

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 [,,...]要运行的注释处理程序的名称;绕过默认
    的搜索进程
      -processorpath <路径>        指定查找注释处理程序的位置
      -d <目录>                    指定存放生成的类文件的位置
      -s <目录>                    指定存放生成的源文件的位置
      -implicit:{none,class}     指定是否为隐式引用文件生成类文件
      -encoding <编码>             指定源文件使用的字符编码
      -source <版本>               提供与指定版本的源兼容性
      -target <版本>               生成特定 VM 版本的类文件
      -version                   版本信息
      -help                      输出标准选项的提要
      -Akey[=value]              传递给注释处理程序的选项
      -X                         输出非标准选项的提要
      -J<标志>                     直接将 <标志> 传递给运行时系统

具体如下[因为需要编译的.java文件分布在不同的文件夹下,故写了多个*.java文件路径.引用了第三方Jar包,故要将jar包指定入classpath]:

image

 

 

D:使用Android SDK提供的dx.bat命令行脚本生成classes.dex文件

将工程bin目录下的class文件编译成classes.dex,Android虚拟机只能执行dex文件!

image

命令生成的.dex文件如下

image

 

 

E:使用Android SDK提供的aapt.exe生成资源包文件(包括res、assets、androidmanifest.xml等):

aapt用法参考上面!

image

命令生成的.ap_文件如下:

image

 

 

 

 

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.

注:1,如果需要将so文件打包进apk,一定要加上-nf参数

  2,如果第三方jar包里含有图片资源,一定要加上-rj参数,不然jar包里资源文件解不出来,程序会因为无法引用资源而报错!

image

image

命令生成的.apk文件

image

【* 目前Android SDK已经删除了 apkbuilder 工具,可以先考虑简单的 aapt add LbsDemo.ap_ classes.dex 方法】

 

H:利用JAVASDK提供的keytools工具生成证书文件!

上面只是生成了一个没有证书的apk文件,Android是不会去安装一个没有数字证书签名的apk包的,所以需要安装apk的话,必须用一个数字证书对其签名后,才可!

image

此处输入命令后,此命令会与用户产生交互,如果你不想产生交互的情况,那么就要提前把命令想问的都告诉它!

 

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文件如下:

image

 

 

I:最后一步,通过jarsigner命令用证书文件对未签名的APK文件进行签名

重要参数如下:
    [-keystore ]           密钥库位置
    [-storepass <口令>]         用于密钥库完整性的口令
    [-storetype <类型>]         密钥库类型
    [-keypass <口令>]           专用密钥的口令(如果不同)
    [-sigfile <文件>]           .SF/.DSA 文件的名称
    [-signedjar <文件>]         已签名的 JAR 文件的名称
    [-digestalg <算法>]    摘要算法的名称
    [-sigalg <算法>]       签名算法的名称
    [-verify]                   验证已签名的 JAR 文件
    [-verbose]                  签名/验证时输出详细信息
    [-certs]                    输出详细信息和验证时显示证书
    [-tsa ]                时间戳机构的位置
    [-tsacert <别名>]           时间戳机构的公共密钥证书
    [-altsigner <类>]           替代的签名机制的类名
    [-altsignerpath <路径列表>] 替代的签名机制的位置
    [-internalsf]               在签名块内包含 .SF 文件
    [-sectionsonly]             不计算整个清单的散列
    [-protected]                密钥库已保护验证路径
    [-providerName <名称>]      提供者名称
    [-providerClass <类>        加密服务提供者的名称
    [-providerArg <参数>]] ... 主类文件和构造函数参数

具体用法如下:

image

image

 

大功告成,终于完成了!

image

 

总结:

通过对这些命令的运用,你才能清楚地知道程序开发、编译、打包的每一个细节、才知道 Eclipse是如何工作的、就算换了一个集成开发工具、也能很快得心应手!甚至能开发一套与Eclipse里打包一样功能的工具呢!下面的就是本人对上 面命令的一个总结!如果公司需要,你也可以动手写一个!

阅读(6306) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~