Android开发、测试持续集成环境Jenkins搭建

传统java项目开发都会利用maven、ant在hudson、jenkins中持续集成,开发、测试、每日构建,保证代码质量。maven默认不支持android,但是我们可以利用maven-android-plugin插件来让maven支持android。

环境

  • jdk 1.6及以上
  • Android sdk(r21及以上),最好是最高版本,最好也安装一下ndk
  • Maven 3.0.3及以上,貌似3.1.0不好使,最好使用3.0.5版

    设置ANDROID_HOME环境变量,将$ANDROID_HOME/tools、$ANDROID_HOME/platform-tools加入到环境变量中

测试环境

下载示例程序,通过跑这些程序,可以检测环境是否OK,并且下载maven需要的jar包。示例程序

使用Maven进行Android开发

新建Maven Android项目

具体命令参考此处

手动创建Maven Android项目

有些是历史项目,也可以手动将其改成maven项目,方法如下:

  1. 新建pom.xml文件,拷贝其他maven项目的也可。
  2. 修改<groupId>,<artifactId>,<name>
  3. 修改<version>,默认是<version>0.1.0-SNAPSHOT</version>
  4. 增加android jar包依赖:
    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android</artifactId>
        <version>1.5_r4</version>
        <scope>provided</scope>
    </dependency>
    

    可在此处查看android所有版本

    5. 在android-maven-plugin配置中,配置android platform的版本,如<platform>7</platform>
    6. 可以删除build.xml、build.properties、bin和libs。
    7. 打apk包mvn clean install,将apk包安装到设备中mvn android:deploy

示例pom.xml如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
       <modelVersion>4.0.0</modelVersion>

       <parent>
          <groupId>com.yeetrack</groupId>
          <artifactId>parent</artifactId>
          <version>1.0-SNAPSHOT</version>
       </parent>

       <properties>     
           <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>       
           <maven.compiler.encoding>utf-8</maven.compiler.encoding>
          <platform.version>4.1.1.4</platform.version>
          <android.plugin.version>3.6.0</android.plugin.version>
       </properties>

       <groupId>com.yeetrack</groupId>
       <artifactId>app</artifactId>
       <version>1.0-SNAPSHOT</version>
       <packaging>apk</packaging>
       <name>app</name>

       <dependencies>
          <dependency>
             <groupId>com.google.android</groupId>
             <artifactId>android</artifactId>
             <version>4.1.1.4</version>
             <scope>provided</scope>
          </dependency>

          <dependency>
               <groupId>com.google.android</groupId>
               <artifactId>support-v4</artifactId>
               <version>r7</version>
        </dependency>

     </dependencies>

<!-- 配置pmd、findbugs -->
<reporting>
<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>findbugs-maven-plugin</artifactId>
    <version>2.5</version>
    <configuration>  
                <xmlOutput>true</xmlOutput>  
                <effort>Max</effort>  
            </configuration>  
  </plugin>
</plugins>

 

<build>
    <finalName>${project.artifactId}</finalName>
    <sourceDirectory>src</sourceDirectory>

    <plugins>
        <plugin>
                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                    <artifactId>android-maven-plugin</artifactId>
                    <version>3.6.1</version>
                    <configuration>
                            <sdk><platform>16</platform></sdk>
                            <manifest>
                                <debuggable>true</debuggable>
                            </manifest>
                    </configuration>
                    <!--<executions>
                            <execution>
                                <goals>
                                        <goal>ndk-build</goal>
                                </goals>
                    <configuration>

                                        <attachNativeArtifacts>true</attachNativeArtifacts>
                                </configuration>
                            </execution>
                    </executions>-->
                    <extensions>true</extensions>
                </plugin>

        </plugins>
    </build>

 

使用Maven进行Android单元测试

使用下面命令可以直接新建一个带有单元测试的Android项目:

    mvn archetype:generate \
    -DarchetypeArtifactId=android-with-test \
    -DarchetypeGroupId=de.akquinet.android.archetypes \
    -DarchetypeVersion=1.0.11 \
    -DgroupId=com.foo.bar \
    -DartifactId=my-android-project \
    -Dpackage=com.foo.bar.android

我们只需要修改其中的pom.xml文件即可。示例文件如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
       <modelVersion>4.0.0</modelVersion>

       <parent>
          <groupId>com.yeetrack</groupId>
          <artifactId>parent</artifactId>
          <version>1.0-SNAPSHOT</version>
       </parent>

       <groupId>com.yeetrack</groupId>
       <artifactId>test</artifactId>
       <version>1.0-SNAPSHOT</version>
       <packaging>apk</packaging>
       <name>Tests</name>

<dependencies>
    <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android-test</artifactId>
            <scope>provided</scope>
        </dependency>
        <!--需要依赖开发代码打出的apk包-->
    <dependency>
        <groupId>com.yeetrack</groupId>
        <artifactId>app</artifactId>
        <type>apk</type>
        <version>1.0-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <!--需要依赖开发代码打出的jar包-->
    <dependency>
        <groupId>com.yeetrack</groupId>
        <artifactId>app</artifactId>
        <type>jar</type>
        <version>1.0-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <finalName>${project.artifactId}</finalName>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <configuration>
                <test>
                    <!--<skip>true|false|auto</skip> -->
                    <!--<instrumentationPackage>packageName</instrumentationPackage> -->
                    `
                    <!--<instrumentationRunner>className</instrumentationRunner> -->
                    <!--<debug>true|false</debug> -->
                    <!--<coverage>true|false</coverage> -->
                    <!--<logonly>true|false</logonly> avd -->
                    <!--<testsize>small|medium|large</testsize> -->
                    <createReport>true</createReport>
                    <!--<classes> -->
                    <!--<class>your.package.name.YourTestClass</class> -->
                    <!--</classes> -->
                    <!--<packages> -->
                    <!--<package>your.package.name</package> -->
                    <!--</packages> -->
                </test>
            </configuration>
            <extensions>true</extensions>
        </plugin>
    </plugins>
</build>

 

Google官方提供Android单元测试的方法,需要继承ActivityInstrumentationTestCase2这个类,

单元测试代码示例,如下(因为我们引用了app构造的apk和jar包,所以我们能够在测试代码中直接引用开发代码):

    package com.yeetrack.test;

    import com.yeetrack.app.LoginActivity;
    import com.yeetrack.app.R;

    import android.test.ActivityInstrumentationTestCase2;
    import android.test.UiThreadTest;
    import android.widget.Button;
    import android.widget.EditText;

    /**
    * @author victor
    * app UI登陆界面测试
    */
    public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity>
    {
       /**
       * 登陆界面的activity
       */
       private LoginActivity loginActivity;
       /**
        * 用户账号输入框
        */
       private EditText usernameEditText;
       /**
        * 用户密码输入框
        */
       private EditText passwordEditText;
       /**
        * 提交按钮
        */
       private Button submitButton;

       public LoginActivityTest()
       {
           super(LoginActivity.class);
       }

       @Override
       protected void setUp() throws Exception
       {
           // TODO Auto-generated method stub
           super.setUp();
           loginActivity = getActivity();
           usernameEditText = (EditText) loginActivity.findViewById(R.id.username);
           passwordEditText = (EditText) loginActivity.findViewById(R.id.password);
           submitButton = (Button) loginActivity.findViewById(R.id.loginButton);
        }
       //需要在主线程中执行,android禁止在其他线程中操作主线程的控件
       @UiThreadTest
       public void testAppActivity()
       {
          assertNotNull(loginActivity);
          assertNotNull(usernameEditText);
          assertNotNull(passwordEditText);
          assertNotNull(submitButton);

          usernameEditText.setText("Victor Wang");
          passwordEditText.setText("password");
          submitButton.performClick();

          try
           {
               Thread.sleep(10000);
            } catch (InterruptedException e)
            {
               e.printStackTrace();
            }
       }

       @Override
        protected void tearDown() throws Exception
        {
           // TODO Auto-generated method stub
           super.tearDown();
        }
    }

使用Maven进行Android功能测试

Android功能测试工具很多,这里用Robotium,pom.xml如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.yeetrack</groupId>
        <artifactId>Robotium</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>apk</packaging>
        <name>Baidutx Robotium</name>
       <description>https://www.yeetrack.com</description>

        <parent>
          <groupId>com.yeetrack</groupId>
          <artifactId>parent</artifactId>
          <version>1.0-SNAPSHOT</version>
        </parent>

        <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <platform.version>4.1.1.4</platform.version>
        </properties>

        <dependencies>
          <dependency>
             <groupId>com.jayway.android.robotium</groupId>
             <artifactId>robotium-solo</artifactId>
             <version>4.3</version>
          </dependency>
          <dependency>
            <groupId>com.google</groupId>
            <artifactId>android</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/android.jar</systemPath>
        </dependency>
        <dependency>
         <groupId>com.yeetrack</groupId>
         <artifactId>app</artifactId>
         <type>apk</type>
         <version>1.0-SNAPSHOT</version>
         <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>com.yeetrack</groupId>
        <artifactId>app</artifactId>
        <type>jar</type>
        <version>1.0-SNAPSHOT</version>
        <scope>provided</scope>
      </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
        <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>

                <assetsDirectory>${project.basedir}/assets</assetsDirectory>
                <resourceDirectory>${project.basedir}/res</resourceDirectory>
                <nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
                <sdk>
                    <platform>17</platform>
                </sdk>
                <undeployBeforeDeploy>true</undeployBeforeDeploy>
            </configuration>
            <extensions>true</extensions>
        </plugin>

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <encoding>UTF-8</encoding>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        </plugins>
        </build>
    </project>

添加Parent项目

将上述项目放在parent文件夹中,并在parent创建pom.xml,这样运行parent的pom.xml即可。

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
       <modelVersion>4.0.0</modelVersion>
       <groupId>com.yeetrack</groupId>
       <artifactId>parent</artifactId>
       <version>1.0-SNAPSHOT</version>
       <packaging>pom</packaging>
       <name>Parent</name>

       <modules>
          <module>app</module>
          <module>test</module>
          <module>robotium</module>
       </modules>

       <properties>
          <platform.version> 4.1.1.4</platform.version>
          <android.plugin.version>3.6.0</android.plugin.version>
       </properties>

       <dependencyManagement>
          <dependencies>
             <dependency>
                    <groupId>com.google.android</groupId>
                    <artifactId>android</artifactId>
                    <version>${platform.version}</version>
                    <scope>provided</scope>
             </dependency>
             <dependency>
                    <groupId>com.google.android</groupId>
                    <artifactId>android-test</artifactId>
                    <version>${platform.version}</version>
                    <scope>provided</scope>
             </dependency>
             <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>4.11</version>
                    <scope>provided</scope>
             </dependency>
          </dependencies>
       </dependencyManagement>

       <build>
          <pluginManagement>
             <plugins>
                    <plugin>
                       <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                       <artifactId>android-maven-plugin</artifactId>
                       <version>${android.plugin.version}</version>
                       <configuration>
                          <sdk>
                             <platform>16</platform>
                          </sdk> 
                       </configuration>
                    </plugin>
                    <plugin>
                       <artifactId>maven-compiler-plugin</artifactId>
                       <version>3.1</version>
                    </plugin>
             </plugins>
          </pluginManagement>
       </build>
    </project>

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

(Spamcheck Enabled)