为Android项目搭建GitLab-CI持续构建服务(ANT非Gradle,可不看)

这两天腾出时间来折腾一下Linux系统,甚是欢喜,于是乎,找了公司一台机器装了Ubuntu 14.04 LTS 64bit版本折腾。

装好以后第一反应是先折腾一个GitLab上去,以满足我管理代码的虚荣之心。OK,开始动手。

1、安装GitLab.

首先,安装Gitlab之前先安装一下SSH服务。

sudo apt-get install openssh-server 

安装完成后,我们就可以开始安装Gitlab了。

话说其实GitLab照着网上安装方法实在在麻烦了,我这个只是在公司内网环境搭建一个作为代码版本控制系统的服务器,不想那样折腾

于是乎,又找来了BitNami的一键安装包装上了。整个过程轻松加愉快,但是有一点,安装过程是下一步下一步式的向导,其中有一步骤

是填写访问域名,这就要求你的Ubuntu系统当前是使用固定IP,在这里最好只填写IP地址,当然,主机名也可。

什么?尔等不知BitNami的一键安装包为何物?没事,有任意门:GitLab CE Cloud Hosting, GitLab CE Installer, Docker Container and VM 各位看官如若与在下一样不喜安装Ruby、Redis等

此类折腾,即可移步这里去下载一键安装包也。

这是题外话。回到主线上来,我们安装好了Gitlab以后,我们就可以在本机上打开Gitlab的本机地址了。

现在,这台服务器上有Gitlab了,我们可以正常的将它作为一台Git服务器来使用了。

但是我们如何使用他的CI功能为Android项目进行持续集成呢?

2、安装Android SDK和Eclipse

既然我们要为android项目进行持续集成的话,那么我们必须要有Android的构建环境。Android的应用构建环境 所需要的工具是Android SDK最新版本的

平台包、ANT、JDK当然必不可少。

我很懒,在这里我就用OpenJDK了。实际上最好使用Oracle JDK的发布版本。

于是乎,一句话

sudo apt-get install Eclipse-platform


直接安装了Eclipse。在安装过程中会自动安装好ANT,OpenJDK。啊~ 我真佩服我自己的懒啊~

Eclipse有了以后,我们要从官网上去下载ADT-bundle,也就是Eclipse和android-sdk混合打包的版本,又一次省事~

省掉了下载adt-plugin和下载sdk平台的过程了~

我把adt-bundle解压到了 /usr/local/目录下,重新命名为adt目录,如此一来,下载的adt就有两个目录:

  /usr/local/adt/eclipse 和 /usr/local/sdk。很明显一个是Eclise,另一个是android-sdk。最好去执行一下 sdk/tools/android 这个脚本文件

来打开sdk manager,下载一下Android 4.4的platform和system-image,可以用于打包和运行虚拟机。

在这里可能发生的问题:

找不到adb,或者aapt执行不了此类问题。此类问题发生的原因有两个,

第一:Eclipse没有执行SDK目录下应用程序的权限;这个问题的解决方式一般需要让目录权限为777或者执行

sudo chmod -R a+x  * 或 sudo chmod -R 777  *


有句名言说得好:----永远不要忘记配置好目录和用户访问权限。

让每个目录下的应用程序都允许其他用户执行

第二:adb和aapt等二进制应用程序文件依然是32位版本,不存在32位兼容库导致的。

sudo apt-get install lib32ncurses5 lib32stdc++6  

执行上面的命令安装32位兼容库解决兼容问题(参考:How-To: Install Google's Android Eclipse Plugin (And/or Adb) on 64-bit Debian/Ubuntu - Tim Mattison

现在,我们的Android开发编译环境已经搭建好了。接下来要开始搭建我们的持续集成环境了。

3、配置GitLab-CI

我们安装好的GitLab一体包当中已经为我们安装好了GitLab-CI这个东东。那我们怎么使用它呢?

首先,我们先进入他的web界面,不要忘记之前你设定好的用户名密码哦~

我这里的主机地址是ubuntu1,你的主机地址根据你的主机设定来

http://ubuntu1/gitlabci/

登录之后会看到你已经通过Git版本控制的项目。

神马?你没有看到的你的项目?

那请你先把你的项目上传到Gitlab中进行版本管理

然后上传即可

这一步操作以后你就可以在Gitlab-ci中同步显示你的项目了。

慢!大侠们莫急~~ 我们少了一步,很重要的一步。

3.5 配置Android项目自动编译脚本

对对对,就是少了这一步。

我们既然要做自动持续集成,那么我们一定要有一个自动编译的程序来为我们执行集成命令。

OK,我们Android项目要做自动编译那毫无疑问就是Ant配合build.xml脚本来进行自动编译了,反正我懒,刚才安装Eclipse的时候已经安装好Ant,

那我们就要告诉Ant如何编译,这就取决于build.xml文件了。

我们刚才已经草莽的就把项目提交到Gitlab了,那我们看一下目录结构:

TestContent
│  AndroidManifest.xml
│  ic_launcher-web.png
│  proguard-project.txt
│  project.properties
│  
├─libs
│      android-support-v4.jar
│      universal-image-loader-1.9.1.jar
│      
├─res

......

根本没有build.xml嘛!真是浪费感情。而接下来我们要弄到这个文件。

我们要在提交项目的电脑上或者这台机器上的项目目录下,执行一下

android update project -p ./

看好了,斜杠前面不要少了 小数点

命令,此时生成了两个文件(有时候是三个):

│  AndroidManifest.xml
│  build.xml             (这个文件就是Ant执行的xml脚本文件,至于这个xml里面怎么写不在今天的讨论之列,但是一定要放到版本管理系统中)
│  ic_launcher-web.png
│  local.properties      (这个文件里面一般只有一个sdk.dir属性,记住,这个文件千万不要提交到版本管理系统上去)
│  proguard-project.txt  (这个文件有时候是被更新而不是被添加,这个是用于做代码混淆时使用的文件)
│  project.properties
│  
├─libs
......


红字部分的文件就是执行了这个命令以后生成的文件

我们简单看一下build.xml的内容:

<?xml version="1.0" encoding="UTF-8"?>
<project name="AccActivity" default="help">

    <!-- The local.properties file is created and updated by the 'android' tool.
         It contains the path to the SDK. It should *NOT* be checked into
         Version Control Systems. 
-->
    <property file="local.properties" />

    <!-- The ant.properties file can be created by you. It is only edited by the
         'android' tool to add properties to it.
         This is the place to change some Ant specific build properties.
         Here are some properties you may want to change/update:

         source.dir
             The name of the source directory. Default is 'src'.
         out.dir
             The name of the output directory. Default is 'bin'.

         For other overridable properties, look at the beginning of the rules
         files in the SDK, at tools/ant/build.xml

         Properties related to the SDK location or the project target should
         be updated using the 'android' tool with the 'update' action.

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems.

         -->
    <property file="ant.properties" />

    <!-- if sdk.dir was not set from one of the property file, then
         get it from the ANDROID_HOME env var.
         This must be done before we load project.properties since
         the proguard config can use sdk.dir -->
    <property environment="env" />
    <condition property="sdk.dir" value="${env.ANDROID_HOME}">
        <isset property="env.ANDROID_HOME" />
    </condition>

    <!-- The project.properties file is created and updated by the 'android'
         tool, as well as ADT.

         This contains project specific properties such as project target, and library
         dependencies. Lower level build properties are stored in ant.properties
         (or in .classpath for Eclipse projects).

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems. -->
    <loadproperties srcFile="project.properties" />

    <!-- quick check on sdk.dir -->
    <fail
            message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
            unless="sdk.dir"
    />

  <!-- ....省省篇幅......-->

 <!-- version-tag: 1 -->
    <import file="${sdk.dir}/tools/ant/build.xml" />

</project>


请大家着重看上面的注释部分我建议多看看这些注释,我补充一点,local.properties文件只能用于“本地”,根据不同的电脑配置的环境不同是有差别的,所以不用版本控制系统去管理。

文件中提到了一个

ant.properties

文件,没错,这个文件是我们需要的手动建立的文件,用于存储keystore文件(这个也要自己创建一个,放到项目目录下,并用Git版本管理)的路径、别名和密码的文件。而这个文件也需要我们放在项目的根目录下

我手动创建好了一个:

# This file is used to override default values used by the Ant build system.
#
# This file must be checked into Version Control Systems, as it is
# integral to the build system of your project.

# This file is only used by the Ant script.

# You can use this to override default values such as
#  'source.dir' for the location of your java source folder and
#  'out.dir' for the location of your output folder.

# You can also use it define how the release builds are signed by declaring
# the following properties:
#  'key.store' for the location of your keystore and
#  'key.alias' for the name of the key to use.
# The password will be asked during the build when you use the 'release' target.

key.store=key  <------(这个是我之前创建好的keystore文件)
key.alias=demo6x1 <---------(这个是创建keystore命名的别名)
key.alias.password=11111111  <----------(别名密码)
key.store.password=11111111  <----------(keystore密码)

jar.libs.dir=libs   <-----(库目录)
java.encoding=utf-8 <-----(编译用字符集)

现在我们的目录结构就变成了:

│  AndroidManifest.xml
│  ant.properties
│  build.xml
│  ic_launcher-web.png
│  key
│  local.properties <----- 这个文件请不要加入Git版本管理当中,请把文件名放到 .ignore当中忽略这个文件。
│  proguard-project.txt
│  project.properties
│  
├─libs
│      android-support-v4.jar
│      universal-image-loader-1.9.1.jar
│      
├─res
│  ├─drawable
......

其他的文件都加入版本管理提交上去。

4、继续配置GitLab-CI

这里有一个前提,忘了提一下,安装完BitNami提供的GitLab安装一体包以后,会生成git,mysql,gitlab-ci三个新用户,建议把自己当前用户还有这三个用户加入到root组里,

防止在运行的时候出现各种权限不够和找不到文件的问题。(为什么会找不到文件?如果文件在一个无权限的目录下的话,那么访问该目录时就没有权限读取文件清单,就成了找不到文件)。权限问题是个大问题,但是真的不太想花篇幅去解释这个问题。

我们现在可以到 http://ubuntu1/gitlabci/ 去配置了。


在这里看到了新添加的项目,还是Not added to Ci的状态,那我们Add一下吧

然后点击项目进入构建设定界面

点击上图here的链接,或者左边的settings进入构建设定

上图的build step中,实际上就是相对于编译时候临时检出的git工作目录的执行的shell命令

我们可以在这里直接写上ant release命令,我们要在这台Ubuntu机器上,通过android update project -p命令来生成一个local.properties文件放在某个目录当中

每次进行构建时复制到项目目录下,这就是为什么不要local.properties进行版本管理的原因。我们要保证local.properties文件中sdk.dir=指向的目录永远是本机

正确的sdk所在目录,否则会找不到平台包和编译工具。我把生成的local.properties文件放在了/opt/gitlab-7.1.1-0/android/目录下了。那么每次构建的时候就把

这个文件复制到项目目录中。

于是乎,我们的脚本就改成了这样子:

构建的时候可以到构建页面上监视输出的控制文本流

以下是我构建成功的页面:

中间省掉一大段日志.....

然后就是build successful.

当然,我尝试过一个规模比较大的项目会打印超长的日志,构建完成了他也是running状态,我不明白为什么,但是如果

在ant release 这个命令上把日志的文本流重导向到 /dev/null的话,则是可以正常successful的。

如果你已经配置成功的话,并且测试百分之百可以构建成功的话,就可以替换命令为

ant release >>/dev/null 

了。

5、如果出错怎么办?

1、这种情况:

这个毫无疑问,是去/sdk/tools/ant/的路上遇到目录权限问题,这肯定是哪一层目录上的访问权限没有rwx或者r-x

2、还有这种情况

如果有人在构建脚本中加了这两句话

ANDROID_HOME=/usr/local/adt/sdk  (这个是你解压了adt的路径)
$ANDROID_HOME/tools/android update project -p ./

会出现这个状况:

毫无疑问,还是权限问题,没有对

/usr/local/adtbundle/sdk/tools/android

这个脚本文件的执行权限。

还是要改权限。

3、还有这么个情况

这种情况是忘记复制local.properties文件或者local.properties文件中的sdk.dir指向了一个无效的目录。

怎么解决?其实本文中已经说明了。

=====================================================================

OK, 为Android项目搭建一个GitLab的CI持续构建环境就是这样。请大家不要多多交流,也不要互相帮助,也不要加QQ群,更不要百度等等,阿嚏!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值