zoukankan      html  css  js  c++  java
  • 使用ant编译Android APK

    ANT —— Apache Ant is a Java library and command-line tool that help building software.

    1. 部署ANT的使用环境

    ① 登录ant的官方网站http://ant.apache.org/bindownload.cgi,下载apache-ant-1.10.1-bin.zip。这是ant要使用的类库。

    ② 在windows环境中,主要配置两个环境ant_home和path

        ANT_HOME :apache-ant-1.10.1-bin.zip的解压文件(apache-ant-1.10.1)所在的位置;

        path路径设置为:PATH = %ANT_HOME%/bin; %ANT_HOME%/lib

    ③ 这两个路径的设置方法:

        右键点击“我的电脑”---属性---高级系统设置---高级---环境变量---在"系统变量“中点击”新建“---

        变量名:ANT_HOME   变量值:D:apache-ant-1.10.1      (可以用同样的方法设置path路径)

    ④  在dos窗口中输入 ant  -version,检查ant是否已经配置完成。

    2. Android的环境配置

          platform-tools —— 执行adb命令的环境变量;

          sdk目录下的tool —— 执行android命令的环境变量,利用ant构建apk所必须的;

          

    到目前为止,ANT和Android的环境配置成功

    3. Ant的简单命令及创建Android工程

          参数说明:

          -n --name :      Project name.

          -a --activity :    Name of the default Activity that is created.   [required]

          -k --package :  Android package name for the application. [required]

          -v --gradle-version: Gradle Android plugin version.

          -t --target :       Target ID of the new project. [required]

          -g --gradle :     Use gradle template.

          -p --path :        The new project's directory. [required]

    3.1 列出不同版本平台对应的ID编号:

    C:Users
    icole>android.bat list targets  
    
    id: 1 or "android-15"
    Name: Android 4.0.3
    Type: Platform
    API level: 15
    Revision: 3
    Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800
    Tag/ABIs : no ABIs.
    ----------
    
    id: 8 or "android-21"
    Name: Android 5.0
    Type: Platform
    API level: 21
    Revision: 1
    Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in
    Tag/ABIs : no ABIs.
    ----------
    id: 9 or "android-23"
    Name: Android 6.0
    Type: Platform
    API level: 23
    Revision: 1
    Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in
    Tag/ABIs : no ABIs.

    3.2 创建android项目,举例: 

    C:Users
    icole>android create project -k com.android.nicole -a AntDemo -t 8 -p D:projectAnt_Demo

    Created project directory: D:projectAnt_Demo
    Created directory D:projectAnt_Demosrccomandroid icole
    Added file D:projectAnt_Demosrccomandroid icoleAntDemo.java
    Created directory D:projectAnt_Demo es
    Created directory D:projectAnt_Demoin
    Created directory D:projectAnt_Demolibs
    Created directory D:projectAnt_Demo esvalues
    Added file D:projectAnt_Demo esvaluesstrings.xml
    Created directory D:projectAnt_Demo eslayout
    Added file D:projectAnt_Demo eslayoutmain.xml
    Created directory D:projectAnt_Demo esdrawable-xhdpi
    Created directory D:projectAnt_Demo esdrawable-hdpi
    Created directory D:projectAnt_Demo esdrawable-mdpi
    Created directory D:projectAnt_Demo esdrawable-ldpi
    Added file D:projectAnt_DemoAndroidManifest.xml
    Added file D:projectAnt_Demouild.xml
    Added file D:projectAnt_Demoproguard-project.txt

     运行成功后会在D盘的projectAnt_Demo目录下生成一个Android工程,里面有我们需要的build.xml文件

     3.3 Undate已有的Android工程

    对已经存在的android工程,我们可以update下(修改平台的版本),这样会自动添加build.xml 等ant 的配置文件

    C:Users
    icole>android update project --name MainActivity -t 9 -p D:projectAndroid_demo
    Updated project.properties
    Updated local.properties
    Added file D:projectAndroid_demouild.xml
    Updated file D:projectAndroid_demoproguard-project.txt
    It seems that there are sub-projects. If you want to update them
    please use the --subprojects parameter.

    编译:直接使用ant命令 是不会有文件输出的,所以后面一定要带上参数

    4.  使用ant打包时,添加第三方jar包

     4.1  libs 库中的第三方jar包

    如果项目只是引用了第三方jar包,只要将jar包放在libs文件夹下就ok了,ant会在编译打包过程中会自动将第三方jar加进去的。

    但是当我们的android 项目参考了其他library项目,这时候我们最初在输入android update 命令的时候应该多一个参数 --subprojects。

    例: C:Users icole>android update project --name SecontActivity -t 9 -p D:projectAndroid_demo

            若有报错,是因为那个library 还不支持ant自动编译,需要先让它也支持:

            进入到library项目所在的目录,输入命令 android update lib-project -p ./  (注意是 lib-project);

       再回到原项目,输入命令” android update project --name MenudrawSample -p ./ --subprojects ",这样就OK了。

    4.2   user library库中第三方jar包

    https://my.oschina.net/yunfound/blog/169288

    4. 利用Ant打包

    如果是利用测试签名打包:cmd命令进入项目根目录,如:E:antAntTest ,执行 ant debug,会在项目的bin目录下生成使用debug签名的apk

    如果是自定义签名:则在项目根目录下添加ant.properties(也有会自动生成local.properties)文件,配置密钥的路径和别名

    具体配置如下:

    key.store=路径
    key.store.password=
    key.alias=
    key.alias.password=

    同样进入根目录下,运行 ant release就可以打包了

    5. 编译

         参数说明:

         debug:  带调试用签名的构建

         release:生成的apk必须签名才可以发布

         install:  安装调试构建的包到运行着的模拟器或者设备

         reinstall 

         uninstall:  卸载安装包

    ant debug:   生成一个测试版apk,默认使用 debug key 进行签名,生成的apk(your_project_name-debug.apk)在bin目录下。

    ant release: 生成一个未签名和未aligned的apk包, project_name-release-unsigned.ap和project_name-release-unaligned.apk 在bin目录下。

    例:进入到项目根目录中使用ant命令时,或者ant debug Buildfile: D:projectAnt_Demouild.xml

    D:projectAnt_Demo>ant debug
    Buildfile: D:projectAnt_Demouild.xml
    
    -set-mode-check:
    
    -set-debug-files:
    
    -check-env:
     [checkenv] Android SDK Tools Revision 23.0.2
     [checkenv] Installed at D:androidadt-bundle-windows-x86-20140702sdk
    
    -setup:
         [echo] Project Name: AntDemo
      [gettype] Project Type: Application
    
    -set-debug-mode:
    
    -debug-obfuscation-check:
    
    -pre-build:
    
    -build-setup:
    [getbuildtools] Using latest Build Tools: 20.0.0
         [echo] Resolving Build Target for AntDemo...
    [gettarget] Project Target:   Android 5.0
    [gettarget] API level:        21
    [gettarget] WARNING: No minSdkVersion value set. Application will install on all Android versions.
         [echo] ----------
         [echo] Creating output directories if needed...
        [mkdir] Created dir: D:projectAnt_Demoin
    es
        [mkdir] Created dir: D:projectAnt_Demoin
    sObj
        [mkdir] Created dir: D:projectAnt_Demoin
    sLibs
        [mkdir] Created dir: D:projectAnt_Demogen
        [mkdir] Created dir: D:projectAnt_Demoinclasses
        [mkdir] Created dir: D:projectAnt_DemoindexedLibs
         [echo] ----------
         [echo] Resolving Dependencies for AntDemo...
    [dependency] Library dependencies:
    [dependency] No Libraries
    [dependency]
    [dependency] ------------------
         [echo] ----------
         [echo] Building Libraries with 'debug'...
       [subant] No sub-builds to iterate on
    
    -code-gen:
    [mergemanifest] Merging AndroidManifest files into one.
    [mergemanifest] Manifest merger disabled. Using project manifest only.

    此处省略......

    -post-package:

    
    

    -do-debug:
    [zipalign] Running zip align on final apk...
    [echo] Debug Package: D:projectAnt_DemoinAntDemo-debug.apk
    [propertyfile] Creating new property file: D:projectAnt_Demoinuild.prop
    [propertyfile] Updating property file: D:projectAnt_Demoinuild.prop
    [propertyfile] Updating property file: D:projectAnt_Demoinuild.prop
    [propertyfile] Updating property file: D:projectAnt_Demoinuild.prop

    
    

    -post-build:

    
    

    debug:

    
    

    BUILD SUCCESSFUL
    Total time: 8 seconds

     6. 安装apk

    D:projectAnt_Demo>adb install binAntDemo-debug.apk
    1408 KB/s (37509 bytes in 0.026s)
            pkg: /data/local/tmp/AntDemo-debug.apk
    Success
  • 相关阅读:
    苹果全球营销高级副总裁Phil Schiller曾考虑炒掉长期创意代理商Media Arts Lab
    Amazon Seller Central is Temporarily Unavailable
    三星高管:我们手机卖的好是因为营销成功
    欧洲跨境电商增速将达21% 德国力压群雄
    苹果挖走了亚马逊搜索技术副总裁,或为提升应用商店搜索功能
    亚马逊CEO贝索斯致股东信:阐述公司未来计划
    Amazon.com 购物 信用卡预售期
    佛论婆媳关系
    Focalprice李培亮:梦想让人在我店里排队
    DX孟虎点评新兴市场:巴西俄罗斯火爆背后
  • 原文地址:https://www.cnblogs.com/nicoleTeng/p/7421536.html
Copyright © 2011-2022 走看看