打造一个全命令行的Android构建系统

"IDE都是给小白程序员的,大牛级别的程序员一定是命令行控,终端控,你看大牛都是使用vim,emacs 就一切搞定"

这话说的虽然有些绝对,但是也不无道理,做开发这行要想效率高,自动化还真是缺少不了命令行工具,因为只有命令行才是最佳的人机交互工具。其实IDE也是底层也是调用命令行工具而已,只不过给普通开发者呈现一个更友好的开发界面。这里可不是宣扬让大家放弃IDE都改命令行,只是每种事物都有他存在的理由,无论是编程语言还是工具都是一个原则 “没有最好的,只有最合适的”。

打造一个全命令行的Android构建系统

前一段时间做一个人产品 xbrowser.me ,发布产品的时候为了统计各渠道流量免不了要构建不通的渠道包,你懂得国内渠道上百个,靠IDE编译打包非吐血不可。这些重复劳动最适合交个程序来做,很多程序员想不明白这个问题,宁愿把大量的精力时间花在业务上,却不知道用工具提高工作效率。在这里写一篇简单的教程,告诉大家怎么脱离IDE环境完成一个Android项目的编译构建,有了这基础开发什么自动化构建工具都不是什么难事了, 前一阵子做的一个打包html5应用的在线工具AppBuilder就是基于命令行构建完成的。

说到命令行自然是不需要图形界面,所以Android SDK的安装下载自然都是在终端上进行。下面是本文中使用的一些SDK和基本环境。

  • Ubuntu Server 14.04 (64位)
  • JDK 1.7
  • android-sdk_r24.0
  • gradle-2.2.1

进入正题,接下来一步一步介绍如何安装配置一个命令行下的编译构建系统.

 

step 1 安装 JDK 环境

配合android的JDK最好选用JDK官方版本而不是Open JDK,下面是在unbuntu下安装JDK 1.7的方法。

  1. <span class="pln">sudo add</span><span class="pun">-</span><span class="pln">apt</span><span class="pun">-</span><span class="pln">repository ppa</span><span class="pun">:</span><span class="pln">webupd8team</span><span class="pun">/</span><span class="pln">java</span>
  2. <span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> update</span>
  3. <span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install Oracle</span><span class="pun">-</span><span class="pln">java7</span><span class="pun">-</span><span class="pln">installer</span>

 

step 2 安装 Android SDK

android sdk 工具包的一些命令行工具是基于32位系统的,在64为平台运行32程序必须安装 i386 的一些依赖库,方法如下:

  1. <span class="pln">sudo dpkg </span><span class="pun">--</span><span class="pln">add</span><span class="pun">-</span><span class="pln">architecture i386</span>
  2. <span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> update</span>
  3. <span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install libc6</span><span class="pun">:</span><span class="pln">i386 libncurses5</span><span class="pun">:</span><span class="pln">i386 libstdc</span><span class="pun">++</span><span class="lit">6</span><span class="pun">:</span><span class="pln">i386 lib32z1</span>

安装完成32位的依赖库后,我们使用wget 去官方下载最新的linux下android SDK包。

  1. <span class="pln">cd </span><span class="pun">~</span>
  2. <span class="pln">wget http</span><span class="pun">:</span><span class="com">//dl.google.com/android/android-sdk_r24.0.1-linux.tgz</span>
  3. <span class="pln">tar xvzf android</span><span class="pun">-</span><span class="pln">sdk_r24</span><span class="pun">.</span><span class="lit">0.1</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">.</span><span class="pln">tgz</span>

编辑 .profile 或者 .bash_profile 把下面的目录增加到 path的搜索路径中,确保android SDK的的一些命令工具可以直接在终端使用,比如 adb 命令。

  1. <span class="pln">ANDROID_HOME</span><span class="pun">=</span><span class="pln">$HOME</span><span class="pun">/</span><span class="pln">android</span><span class="pun">-</span><span class="pln">sdk</span><span class="pun">-</span><span class="pln">linux</span>
  2. <span class="kwd">export</span><span class="pln"> PATH</span><span class="pun">=</span><span class="str">"$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools"</span>
  3. <span class="pln">exprot ANDROID_HOME</span>

使环境变量生效

  1. <span class="pln">source </span><span class="pun">~/.</span><span class="pln">profile</span>

环境变量生效后,你可以使用android命令 列出sdk相关的列表,以便我们选择和自己项目匹配的SDK版本。(刚才只是安装了最基础的SDK,要完全满足你的开发环境需要还得从下面的列表中选择你需要的SDK和工具更新下载)

  1. <span class="pln">android list sdk </span><span class="pun">--</span><span class="pln">all</span>

输出如下所示:

  1. <span class="lit">1</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">24.0</span><span class="pun">.</span><span class="lit">1</span>
  2. <span class="lit">2</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Platform</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">21</span>
  3. <span class="lit">3</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Build</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">21.1</span><span class="pun">.</span><span class="lit">2</span>
  4. <span class="lit">4</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Build</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">21.1</span><span class="pun">.</span><span class="lit">1</span>
  5. <span class="lit">5</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Build</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">21.1</span>
  6. <span class="lit">6</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Build</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">21.0</span><span class="pun">.</span><span class="lit">2</span>
  7. <span class="lit">7</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Build</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">21.0</span><span class="pun">.</span><span class="lit">1</span>
  8. <span class="lit">8</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Build</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">21</span>
  9. <span class="lit">9</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Build</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">20</span>
  10. <span class="lit">10</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Build</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">19.1</span>
  11. <span class="lit">11</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Build</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">19.0</span><span class="pun">.</span><span class="lit">3</span>
  12. <span class="lit">12</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Build</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">19.0</span><span class="pun">.</span><span class="lit">2</span>
  13. <span class="lit">13</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Build</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">19.0</span><span class="pun">.</span><span class="lit">1</span>
  14. <span class="lit">14</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Build</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">19</span>
  15. <span class="lit">15</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Build</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">18.1</span><span class="pun">.</span><span class="lit">1</span>
  16. <span class="lit">16</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Build</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">18.1</span>
  17. <span class="lit">17</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Build</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">18.0</span><span class="pun">.</span><span class="lit">1</span>
  18. <span class="lit">18</span><span class="pun">-</span><span class="typ">Android</span><span class="pln"> SDK </span><span class="typ">Build</span><span class="pun">-</span><span class="pln">tools</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">17</span>
  19. <span class="lit">19</span><span class="pun">-</span><span class="typ">Documentation</span><span class="kwd">for</span><span class="typ">Android</span><span class="pln"> SDK</span><span class="pun">,</span><span class="pln"> API </span><span class="lit">21</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">1</span>
  20. <span class="lit">20</span><span class="pun">-</span><span class="pln"> SDK </span><span class="typ">Platform</span><span class="typ">Android</span><span class="lit">5.0</span><span class="pun">.</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> API </span><span class="lit">21</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">2</span>
  21. <span class="lit">21</span><span class="pun">-</span><span class="pln"> SDK </span><span class="typ">Platform</span><span class="typ">Android</span><span class="lit">4.4W</span><span class="pun">.</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> API </span><span class="lit">20</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">2</span>
  22. <span class="lit">22</span><span class="pun">-</span><span class="pln"> SDK </span><span class="typ">Platform</span><span class="typ">Android</span><span class="lit">4.4</span><span class="pun">.</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> API </span><span class="lit">19</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">4</span>
  23. <span class="lit">23</span><span class="pun">-</span><span class="pln"> SDK </span><span class="typ">Platform</span><span class="typ">Android</span><span class="lit">4.3</span><span class="pun">.</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> API </span><span class="lit">18</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">3</span>
  24. <span class="lit">24</span><span class="pun">-</span><span class="pln"> SDK </span><span class="typ">Platform</span><span class="typ">Android</span><span class="lit">4.2</span><span class="pun">.</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> API </span><span class="lit">17</span><span class="pun">,</span><span class="pln"> revision </span><span class="lit">3</span>
  25. <span class="pun">....</span>

这里包括不同的Android API 版本和不同的构建工具,选择你想要安装项目的序号,这里我想安装 build tools 19.1 ,build tools 21 及 android 4.2.2以上的SDK所以选择序号 “1,2,3,20,21,22,23”

  1. <span class="pln">android update sdk </span><span class="pun">-</span><span class="pln">u </span><span class="pun">-</span><span class="pln">a </span><span class="pun">-</span><span class="pln">t </span><span class="lit">1</span><span class="pun">,</span><span class="lit">2</span><span class="pun">,</span><span class="lit">3</span><span class="pun">,</span><span class="lit">10</span><span class="pun">,</span><span class="lit">20</span><span class="pun">,</span><span class="lit">21</span><span class="pun">,</span><span class="lit">22</span><span class="pun">,</span><span class="lit">23</span>

 

step 3 安装gradle构建环境

使用Ant构建项目已经是过去式了,这里我们选用更加强悍和方便的构建工具gradle 。

下载 grdle 二进制包

  1. <span class="pln">cd </span><span class="pun">~</span>
  2. <span class="pln">wget https</span><span class="pun">:</span><span class="com">//services.gradle.org/distributions/gradle-2.2.1-bin.zip</span>

释放到本地Home目录,创建名字为"gradle"的符号链接,符号连接的好处是方便版本更新,有了新的版本直接修改符号链接即可。

  1. <span class="pln">unzip gradle</span><span class="pun">-</span><span class="lit">2.2</span><span class="pun">.</span><span class="lit">1</span><span class="pun">-</span><span class="pln">bin</span><span class="pun">.</span><span class="pln">zip </span>
  2. <span class="pln">ln </span><span class="pun">-</span><span class="pln">s gradle</span><span class="pun">-</span><span class="lit">2.2</span><span class="pun">.</span><span class="lit">1</span><span class="pln"> gradle</span>

配置gradle环境变量并使其生效,编辑 ~/.profje 文件增加下面内容

  1. <span class="pln">GRADLE_HOME</span><span class="pun">=</span><span class="pln">$HOME</span><span class="pun">/</span><span class="pln">gradle</span>
  2. <span class="kwd">export</span><span class="pln"> PATH</span><span class="pun">=</span><span class="pln">$PATH</span><span class="pun">:</span><span class="pln">$GRADLE_HOME</span><span class="pun">/</span><span class="pln">bin</span>

保存后使环境变量使其生效

  1. <span class="pln">source </span><span class="pun">~/.</span><span class="pln">profile</span>

环境变量生效后你可以在终端敲入'gradle'命令并运行用以检测gradle是否安装成功。

  1. <span class="pln">gradle</span>

如果安装配置的没有问题将会提示类似下面的信息

  1. <span class="pun">:</span><span class="pln">help</span>
  2. <span class="typ">Welcome</span><span class="pln"> to </span><span class="typ">Gradle</span><span class="lit">2.2</span><span class="pun">.</span><span class="lit">1</span>
  3. <span class="typ">To</span><span class="pln"> run a build</span><span class="pun">,</span><span class="pln"> run gradle </span><span class="str"><task></span><span class="pun">...</span>
  4. <span class="typ">To</span><span class="pln"> see a list of available tasks</span><span class="pun">,</span><span class="pln"> run gradle tasks</span>
  5. <span class="typ">To</span><span class="pln"> see a list of command</span><span class="pun">-</span><span class="pln">line options</span><span class="pun">,</span><span class="pln"> run gradle </span><span class="pun">--</span><span class="pln">help</span>
  6. <span class="pln">BUILD SUCCESSFUL</span>

 

验证是否可以编译android 应用

完成以上的环境配置,我们的一个Android下的基础构建环境就全部配置好了,接下来做的事情就是尝试使用gradle 编译一个android 应用出来验证我的编译环境是否OK,下载我写的一个gadle demo 例子进行测试。

  1. <span class="pln">git clone https</span><span class="pun">:</span><span class="com">//github.com/examplecode/gradle_demo</span>
  2. <span class="pln">cd gradle_demo</span><span class="pun">/</span><span class="pln">hello</span><span class="pun">-</span><span class="pln">apk</span><span class="pun">-</span><span class="kwd">with</span><span class="pun">-</span><span class="pln">gradle</span>
  3. <span class="pln">gradle build</span>

一切顺利的话,编译完成后,你会在 “hello-apk-with-gradle/build/outputs/apk” 目录下找到编译好的apk包。至于如何整合到你自己的项目中去,只需要仿照例子给你的项目提供一个"gradle.build" 脚本即可。

相关推荐