Android Continuous Integration, androidcontinuous

Source: Internet
Author: User

Android Continuous Integration, androidcontinuous

With the gradual maturity of the Android platform and the launch of a series of Android testing frameworks, developers can finally perform unit testing, integration testing, and functional testing on mobile development. From development, testing, and acceptance in Agile processes to user-oriented Release versions, the entire lifecycle of Story is experienced. CI (Continuous Integration) also plays a very important role in Agile practices.

If we say that the continuous integration of Web and various testing frameworks have accumulated some history. The continuous integration of Android can be said to be a new thing. Most IT companies know how to perform a series of automated tests on the server or Web to ensure the correctness of their functions. For mobile products, a large number of tests are made up of testers. Mobile-end manual testing is costly for testers, developers who often package and fix various bugs.

I have been an Android developer since Android 2.3.3. From the developer's point of view, we have witnessed the step-by-step upgrade of Android and the dominance of Android in the middle and low-end markets from the perspective of ordinary users. Although I am very satisfied with Apple products, my feelings for Android cannot be left alone. I am glad that I have finally been able to witness the test-driven development practices on Android, and I am glad that I have the opportunity to learn about the continuous integration of Android from scratch.

Preface

This article explores how to build an available Android CI environment based on the role of a learner from scratch. The final goal is to compile the QA version available for QA testing through one-click deployment on Jenkins from build unit testing to function testing, and use the HockeyApp to generate downloadable links.

1. Build an Android basic project and run the test locally

The basic project here mainly refers to the basic functions that can run the test. I chose to use Robolectric for unit testing and espresso for inheritance testing (which will be discussed later ). The basic project mainly refers to robolectric/decard-gradle. Through some gradlew configurations, our project can directly run unit test and integration test.

Run the unit test:

./gradlew test

Run the espresso test:

./gradlew connectedAndroidTest

Once your instance runs successfully, this step has actually been completed. Later, we only need to run these commands through jenkins and then display the results.

2. Install Jenkins and Android on a virtual machine

In order to start from scratch, I chose a clean Ubuntu/trusty machine. In order to make the build start from scratch, we can better simulate the actual situation on EC2 machines.
. From the test perspective, I chose to use Vagrant to start the virtual machine.

XiaoMing:ci minggong$ vagrant init ubuntu/trusty64A `Vagrantfile` has been placed in this directory. You are nowready to `vagrant up` your first virtual environment! Please readthe comments in the Vagrantfile as well as documentation on`vagrantup.com` for more information on using Vagrant.XiaoMing:ci minggong$ vagrant upBringing machine 'default' up with 'virtualbox' provider...[default] Importing base box 'ubuntu/trusty64'...[default] Matching MAC address for NAT networking...[default] Setting the name of the VM...[default] Clearing any previously set forwarded ports...[default] Fixed port collision for 22 => 2222. Now on port 2200.[default] Creating shared folders metadata...[default] Clearing any previously set network interfaces...[default] Preparing network interfaces based on configuration...[default] Forwarding ports...[default] -- 22 => 2200 (adapter 1)[default] Booting VM...[default] Waiting for machine to boot. This may take a few minutes...[default] Machine booted and ready![default] Mounting shared folders...[default] -- /vagrantXiaoMing:ci minggong$ vagrant sshWelcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64) * Documentation:  https://help.ubuntu.com/Last login: Tue Apr 22 19:47:09 2014 from 10.0.2.2

If a static IP address has been set in Vagrantfile, you can directly log in through the IP address. The default vagrant user name isvagrantThe password isvagrant.

Usevagrant sshAfter logging on to the virtual machine, install the following environments:

  • Java
  • Jenkins
  • Android Environment
2.1 install Java

Install JDK directly with apt-get:

vagrant@ubuntu-14:~$ sudo apt-get updatevagrant@ubuntu-14:~$ sudo apt-get install openjdk-7-jdk -yvagrant@ubuntu-14:~$ java -versionjava version "1.7.0_75"OpenJDK Runtime Environment (IcedTea 2.5.4) (7u75-2.5.4-1~trusty1)OpenJDK 64-Bit Server VM (build 24.75-b04, mixed mode)
2.2 install Jenkins

Refer to the official Jenkins Tutorial:

wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'sudo apt-get updatesudo apt-get install jenkins -y

After Jenkins is installed, verify that Jenkins has been installed,curl http://localhost:8080If a bunch of HTML tags can be output, it proves that Jenkins has been installed successfully and started. Common Jenkins commands are as follows, which are enabled on port 8080 by default:

vagrant@ubuntu-14:~$ sudo /etc/init.d/jenkins -helpUsage: /etc/init.d/jenkins {start|stop|status|restart|force-reload}

The default de configuration file for Jenkins is/etc/default/jenkinsYou can see the path defined by Jenkins_home:

 #jenkins home location JENKINS_HOME=/var/lib/jenkins

To change the default Jenkins home path/etc/default/jenkinsAnd then restart jenkins.

2.3 install the Android Runtime Environment

In the last step, we need to install the Android runtime environment so that Jenkins can run Android-related tests. How to Build Apps with Jenkins

  • Android SDK official website to find the latest SDK installation package address: http://dl.google.com/android/android-sdk_r24.1.2-linux.tgz, download and unzip:
$ cd /opt$ sudo wget http://dl.google.com/android/android-sdk_r24.1.2-linux.tgz$ sudo tar zxvf android-sdk_r24.1.2-linux.tgz$ sudo rm android-sdk_r24.1.2-linux.tgz
  • Set Android environment variables:
$ vi /etc/profile.d/android.sh

Add the following to android. sh file

export ANDROID_HOME="/opt/android-sdk-linux"export PATH="$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$PATH"

Thensource /etc/profileTo make the file take effect.

  • Configure the Android SDK

The simplest and most crude method is to download all the sdks and related tools. Of course, you can download them one by one. For details, refer to the article above. I directly update all the methods:

android update sdk --no-ui

After a long wait, the download is complete. (This step is generally left for midnight download)

2.4 install Git

To enable Jenkins to directly download code from Git Repository, install Git in Ubuntu:

sudo apt-get updatesudo apt-get install git -y
2.5 configure Jenkins

After Jenkins is started, register the login user and create a new Build Project. The build is basically complete. Of course, you can also find some plug-ins in the previous blog Set up Jenkins for Android integration using Docker. These plug-ins can better achieve continuous Android integration, including continuous release of new versions through the Hockey App.

For installation details, refer to Setup Android CI using Docker

Roughly speaking, install the plug-in:Git plugin, Android emulator plugin, Copy artifact plugin, Android lint plugin, Hockey app plugin, Build monitor plugin

During running, jenkins users do not have operation permissions on the jenkins-home directory. You must use chmod to add permissions. Under the current vagrant user,su jenkinsYou can switch to the jenkins directory, but you need to provide a password. You can modify the password by yourself:

vagrant@ubuntu-14:/var/lib/jenkins$ sudo passwd jenkinsEnter new UNIX password:Retype new UNIX password:passwd: password updated successfullyvagrant@ubuntu-14:/var/lib/jenkins$ su jenkinsPassword:jenkins@ubuntu-14:~$

After the permission is set successfully, run the test again and prompt that the Android Build Tools has not been installed successfully:

+ ./gradlew clean buildFAILURE: Build failed with an exception.* What went wrong:A problem occurred configuring project ':app'.> failed to find Build Tools revision 21.1.2

The Android Build Tools version is 21.1.2 specified in app/build. gradle. Passandroid list sdk --allYou can view the list of all sdks that can be downloaded.

Packages available for installation or update: 138   1- Android SDK Tools, revision 24.1.2   2- Android SDK Platform-tools, revision 22   3- Android SDK Build-tools, revision 22.0.1   4- Android SDK Build-tools, revision 22 (Obsolete)   5- Android SDK Build-tools, revision 21.1.2   6- Android SDK Build-tools, revision 21.1.1 (Obsolete)   7- Android SDK Build-tools, revision 21.1 (Obsolete)   8- Android SDK Build-tools, revision 21.0.2 (Obsolete)   9- Android SDK Build-tools, revision 21.0.1 (Obsolete)

You can see that the index value of 21.1.2 is 5, so you can useandroid update sdk -u --all --filter <number>Update to detect permission issues.

Installing Archives:  Preparing to install archives  Downloading Android SDK Build-tools, revision 21.1.2  URL not found: /opt/android-sdk-linux/temp/build-tools_r21.1.2-linux.zip (Permission denied)  Done. Nothing was installed.

You can directly add 777 permissions to/opt/android-sdk-linux and its sub-folders under the current user.

sudo chmod -R 777 /opt/android-sdk-linux

Then install Android build tools 21.1.2,android update sdk -u --all --filter 5. After the installation is complete, an error may occur:

:app:mergeDevDebugResources FAILEDFAILURE: Build failed with an exception.* What went wrong:Execution failed for task ':app:mergeDevDebugResources'.> Error: org.gradle.process.internal.ExecException: A problem occurred starting process 'command '/opt/android-sdk-linux/build-tools/22.0.1/aapt''

Google only found that Android runs 64 on a Linux machine and requires installationia32-libsBut via apt installation is indeed told that the ia32-libs does not exist and cannot be installed. Stackoverflow solution:

sudo -icd /etc/apt/sources.list.decho "deb http://old-releases.ubuntu.com/ubuntu/ raring main restricted universe multiverse" >ia32-libs-raring.listapt-get updateapt-get install ia32-libs

After the installation is successful, the Android unit test runs successfully.

After the unit test, we often need to run the function test. During the function test, we need to open the simulator. That is, headless android emulator. Create an avd in the virtual machine, and then start the running simulator.

Before running, you need to install the API-21 corresponding to the armeabi-v7a before you can create a virtual machine. Viewandroid list sdk --allIt is known that its serial number is 68, so you can install it through android update:

android update sdk -u --all --filter 68

Create a API-21 virtual machine after installation:

android create avd -f -a -s 1080x1920 -n Nexus-21 -t android-21 --abi

After the virtual machine is created successfully, you need to open the virtual machine:

emulator -avd Nexus-21 -no-skin -no-audio -no-window

If it succeeds, the function test should be able to run here. However, android emulator cannot be started in my virtual machine. It seems that it is more reliable to build a real android function test using a Mac mini with a display.

3. Other

How to configure different Flavor in Android and upload the constructed App to the HockeyApp. Check out my blog.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.