Build the Android automated build tool stream with Jenkins

Every time I packaged an application for testing, I basically just stared at my computer, because compiling Andorid APK was very performance intensive. How could a senior engineer be stumped like that, and so I began a long journey.

Tool selection

Travis- Ci, Jenkins, Gitlab-Ci, etc.

  1. Our repO is on Github and Gitlab-CI is out
  2. Private repO, Travis- CI needs to be phased out for a fee

So I ended up with Jenkins

Jenkins installation & Configuration

Install Jenkins

SSH tool to connect to the server, according to the official Wiki operation

1. Add the repo

wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
Copy the code

2. Add sources

Add the following to /etc/apt/sources.list

deb https://pkg.jenkins.io/debian-stable binary/
Copy the code

3. The installation

Don’t say anything, just do it

sudo apt-get update
sudo apt-get install jenkins
Copy the code

Configuration Jenkins

After the above operation, we have actually installed, and then we need to configure

Actually, my port 8080 is already used, so I need to change the port.

And then, if you’re using VPS, be careful to change the security policy and open the ports to the outside world.

vim /etc/default/jenkins
Copy the code

Find the HTTP_PORT configuration that you want

Then run the following command to copy the initial password for later use

cat /var/lib/jenkins/secrets/initialAdminPassword

*******************************
Copy the code

Ha ha my password certainly won’t show you ~~

Start the

/etc/init.d/jenkins start
Copy the code

Other optional command: {start | stop | status | restart | force – reload}

Then we open the browser :{your server IP address}:{the port number you changed}

Enter the initial password you just copied and you’re done.

Select the recommended plug-in to install, then set up the account password, and you are OK.

Install plug-in package

The proposed plug-in package may miss some things, let’s install it again to see if it is complete

  • Git plugin
  • Gradle Plugin
  • Email Extension Plugin
  • description setter plugin
  • build-name-setter
  • user build vars plugin
  • Post-Build Script Plug-in
  • Branch API Plugin
  • SSH plugin
  • Scriptler
  • Dynamic Parameter Plug-in
  • Git Parameter Plug-In

Fir. Im plug-in

The fir. Im plugin needs to be installed by yourself. See how to use the FIR

Set the tool

Before we do that, let’s check that we have all the dependencies we need to build the Android project

JDK

I installed 1.8

sudo add-apt-repository ppa:webupd8team/java

sudo apt-get update
sudo apt-get install oracle-java8-installer
Copy the code

If you can’t find the add-apt-repository command, install the following two packages

sudo apt-get install software-properties-common python-software-properties
Copy the code

The directory is /usr/lib/jvm/java-8-oracle

Gradle

Now we don’t need to install Gradle ourselves. Android projects now have Gradle-Wrapper enabled by default and will download Gradle versions that are consistent with the project

Android-Sdk

This step is a little bit more difficult, first we go to the Android official website to find the SDK: SDK-tools-linux-3859397.zip

Then download it on the server:

# sudo apt-get install wget https://dl.google.com/android/repository/sdk-tools-linux-3859397.zipCopy the code

Unzip and move to /usr/local/android-sdk

# sudo apt-get install unzip unzip sdK-tools-linux-3859397.zip sudo mkdir /usr/local/android-sdk sudo mv tools /usr/local/android-sdkCopy the code

Install dependencies

Note: If you are running the 64-bit version of Ubuntu, you will need to install some 32-bit libraries using the following commands:

Sudo apt-get install lib32z1 lib32ncurses5 lib32bz2-1.0 lib32stdc++6

If you are running the 64-bit version of Fedora, the command is:

sudo yum install zlib.i686 ncurses-libs.i686 bzip2-libs.i686

Update the SDK

cd tools
sudo ./android update sdk
Copy the code

After the update completes, run the following command

cd ..
cd tools/bin
sudo ./sdkmanager --update
sudo ./sdkmanager --list
Copy the code

Then install the corresponding package as required

Other SDkManager operations are available here

FFFFFFFF; # install sudo./sdkmanager "FFFFFFFF; android-26"Copy the code

Configuration tools path

System Management > Global Tool Configuration

JDK

The new JDK

  • The JDK alias: JAVA_8
  • JAVA_HOME: /usr/lib/jvm/java-8-oracle
  • Automatic installation: Deselect this option

Git

  • Name: Native-Git
  • Path to Git executable: /usr/bin/git
  • Automatic installation: Deselect this option

Apply > Save

System Management > System Settings

Global properties

  • Environment variables: check
  • List of key-value pairs: ANDROID_HOME:/usr/local/android-sdk

At this point, all of Jenkins’ attributes are configured.

project

Our whole idealized process is

  1. Local code pushes to Github’s master branch
  2. Jenkins listens to Github’s Webhook and pulls project updates
  3. Gradle build
  4. Output APK to FIR.im

Test it out with a project on Github

New project

1. Create projects

Enter a project name and choose to build a free-style software project

2. Fill in the project information

general

Since I am a Github project, I checked the Github Project and filled in the project link

Source code management

Source management select Git, fill in the Repository URL on Github, add your Github account and select Master

Build trigger

My requirement is that the build will be triggered every time I push the master branch, so I checked GitHub hook trigger for GITScm polling

For details, please refer to the Github Plugin Wiki

Build environment

You can set it as you want, I didn’t ask for it so I skipped it

build

Add build steps > Invoke Gradle Script > Use Gradle Wrapper

Check Make Gradlew Executable

For Tasks, fill in clean assembleDebug

For formal packages, fill in the Clean assembleRelease Task after configuring the signature

Post-build operation

1. Upload fir. Im

Here we need to upload the built Android APK to fir. Im and send an email to inform the developer.

Add post-build operation steps > Upload to fir.im

Fill in the application Token and other contents

2. Email notification

We use the Email Extension Plugin here

Forget about the installation and initial setup and just add the final content

  • Project Recipient List: To add the email recipient’s email address, use.separated
  • Content Type: HTML
  • Default Subject: $PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!
  • Default Content:
$PROJECT_NAME<br/> Git revision: $GIT_REVISION<br/> # $BUILD_NUMBER < br / > build status: $BUILD_STATUS < br / > build address: < a href = "$BUILD_URL" > $BUILD_URL < / a > < br / >Copy the code
  • Attach Build Log: Compress and Attach Build Log

Trigger a build

Now that we’re all set up, it’s time to trigger the build.

Our default is to build Github Webhook, but I want to build directly now, so go into the project, hit Build Now and wait for everything to run.

A little extra help

If the gradle wrapper download process is particularly slow, you can download the gradle executable file on the local computer and upload it to the server using SFTP

Then move you downloaded zip file/var/lib/Jenkins /. Gradle/wrapper/dists/gradle – {version_number} – all / {strange_strings}, cancel the ongoing build, again

conclusion

I went back and forth for a few days to get the Jenkins auto-build tool flow. First bought a single-core 1 gb of memory on the Aliyun server, and then has been stuck in transformDexArchiveWithExternalLibsDexMergerForDebug task failed here

Don’t build your project with a non-stable version of Android Studio. This seems to be a compatibility issue between the latest non-stable version of Gradle and Ubuntu.

Jenkins could not connect, SSH could not connect, ali Cloud monitoring could not find any reason. Finally, I guessed that the memory was burst, so I upgraded to single-core 2G memory. In order to reduce the impact of the project, I created a new project to Build, and it passed.

Check ali cloud memory monitoring found that the peak memory of a newly created project was already approaching the limit of 2G when it was built. There was no choice but to choose a machine with a higher configuration. But Ali Cloud pit is no 1 core 4G optional, had to refund to Tencent cloud, and then done.

Warning: Gradle is building projects with too much memory. You should buy a machine with enough memory, otherwise it will freeze.