使用Ant自动签名、打包Android apk并且自动安装到手机

一、建立Ant打包Apk

  1. 新建一个TestAnt项目 
    这里写图片描述
  2. 创建App的签名密钥

    参考我的这篇github,欢迎Star|点击这里

  3. 取到密钥后,在项目中创建一个keystore的文件夹,复制密钥到此 
    这里写图片描述

  4. 给项目添加Ant

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//到项目的上级目录</span>
cd /Users/.../Documents/workspace/
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//给相聚添加ant,-p 项目 -t 是target 我这里是android-21</span>
android update project -p TestAnt -t android-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span> --subprojects
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//这条命令执行完毕会创建build.xml 和proguard-project.txt 文件,会更新local.properties</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

5.新建ant.properties ,并添加密钥

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//密钥的路径</span>
key.store=<keystore>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//别名</span>
key.alias=<key>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//store 的密码</span>
key.store.password=<keystore pwd>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//alias 的密码</span>
key.alias.password=<key pwd></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

这里写图片描述 
这里写图片描述

6.运行ant

依次选择[build.xml][Run AS] [AntBuild…]

这里写图片描述

勾选clean 和 debug

这里写图片描述

查看运行日志

这里写图片描述

二、安装到手机并且编写脚本,实现全自动

1.安装到手机

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//复制上图中```[echo] Debug Package:```的路径</span>
adb install 路径</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
  • 如果你想学到更多adb push adb install adb install -r 三个命令的区别的话,请点击这里

2.编写脚本,放到项目更目录

  • run.sh
<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">#使用ant编译为debug版本
ant clean debug
#安装apk到手机
adb install -r bin/TestAnt-debug.apk</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
  • 在mac中是run.sh在win中是run.bat

3.运行脚本

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">cd 项目路径
./run.sh</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

4.错误

  • 运行./run.sh 提示Permission denied没有权限
<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">chmod <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">777</span> run.sh</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
  • 运行完成了,但是提示Invalid APK file: bin/TestAnt-debug.apk无效的apk
<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">修改build.xml文件中的project 的name为项目的名称TestAnt</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

这里写图片描述

阅读更多
文章标签: ant
个人分类: development tools
上一篇Android Studio 打包、生成jks密钥、签名Apk、多渠道打包
下一篇Android Material Design之Toolbar与Palette实践
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭