Build android project using jenkins

Say a few words to yourself

As an operation and maintenance person, everyone must have this idea. Never do manual operations if they can be automated. Never do things that can be completed in one step in multiple steps. Being lazy when thinking about the Dharma is the source of our progress.

This is not our Android development. Come and complain to me that testers always ask for apk packages from them recently. Sometimes when they are busy and pause in the middle, the feeling of writing code is gone. Maybe development and writing also require inspired 

In the eyes of operations and maintenance, programmers are omnipotent, and in the eyes of programmers, operations and maintenance are also omnipotent. Now that we have said that, we must do everything we can to solve this problem.

On Jenkins, we give the permission to compile and build to the test, and then the test can be packaged however we want.

Start it, start it

I still have to complain a few words before I officially start the project. Although I can see a lot of things by Baidu (Jenkins builds android project), but I really want to say what the fuck, there is really no way I can finish it just by reading one article. This. At that time, you can try it on Baidu

Fortunately, with my unremitting efforts, I finally achieved it. Let’s take a look.

Environmental preparation

Install and configure gradle and android_sdk required to compile Android

Configure gradle

First go to the official website to download the gradle we need. The version should be the same as the version currently used for development.

Official download address: http://services.gradle.org/distributions/

Currently we use Gradle 4.6. I will use this version for examples.

wget http://downloads.gradle.org/distributions/gradle-4.6-all.zip
unzip gradle-4.6-all.zip 
mv  gradle-4.6-all  /usr/local/gradle
 
vim  /etc/profile
#在最后添加下面这两句
export GRADLE_HOME=/usr/local/gradle  
export PATH=$PATH:$GRADLE_HOME/bin
 
source /etc/profile
gradle -v

Gradle has been installed

Configure android_sdk

Reliable download address  http://tools.android-studio.org/index.php/sdk

Watch me operating on the server

wget https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
tar -xvzf  android-sdk_r24.4.1-linux.tgz
mv android-sdk-linux /usr/local/android-sdk
 
vim  /etc/profile
#在最后添加下面这两句
export ANDROID_HOME=/usr/local/android-sdk
export PATH=$PATH:$ANDROID_HOME/tools
 
source /etc/profile

Jenkins configuration

Next, go to Jenkins to do a wave of work. First, install a wave of plug-ins.

System Management --> Plug-in Management

Install the following plugins

 The picture may not be clear, the details are as follows

Build Name and Description Setter

description setter plugin

Project Description Setter

It is best to install another AnsiColor plug-in

After the plug-in is installed, go to Jenkins to configure the Gradle installed on the server.

System Management --> All Tool Configuration

Just reference the gradle installed on the system, configure it, save and exit

 

 Then set the environment variables of android_sdk

System Management --> System Settings

 

Create a new project

 

Then the configuration is as shown in the figure

Configuration parameters

 

 

Configure source code address

 

Configure build

 

 

Debug configuration

After saving and exiting, start building the project. Select the appropriate parameters to build.

 

Unfortunately, an error is reported during compilation and packaging. We can open the console output to view the error message.

 

 Looking at the error report, it says that some sdk versions are missing (about which versions are needed, you can ask the developer in advance (you can show this screenshot to the developer, they will know), and after installing it, you can avoid this error report)

Next we will install these missing sdk versions

Watch my server operation

#列出所有的 sdk 版本及其 tag
android list sdk --all   
#我们缺少的是   android-28  和 build-tools;28.0.2 ,选择其对应的  tag 进行安装
android update sdk -u --all --filter 8,6      #都选择 y 

When you see the result, the installation is complete.

 Next, we will build it again. There may be errors in the future, but our strategy is to look at the errors and fix whatever is missing.

There are still errors in my follow-up, android-26  and android-27  are still missing.  Repeat the above command to make up for it.

After the compilation is completed and no errors are reported, we go to the workspace to view the compiled APK package (if you don’t know the path, you can ask the developer)

 

QR code production

Next, we need to consider whether this apk package can be downloaded using a link and generate a downloadable QR code for it.

Here we use nginx to implement available connection downloads. Just move the apk package to the corresponding directory configured by nginx.

Modify the nginx configuration file and add the following two servers:

server {
        listen       60080;
        server_name download.apk.com;    #没有使用域名,只是为了好看区分,懂 nginx 的应该知道
        charset utf-8;
        location / {
                root   /opt/android_apk/apk;
                autoindex       on;
                autoindex_exact_size    off;
                autoindex_localtime     on;
        }
    }
 
    server {
        listen      60081;
        server_name download.img.com;
        charset utf-8;
        location / {
                root   /opt/android_apk/img_er;
                autoindex       on;
                autoindex_exact_size    off;
                autoindex_localtime     on;
        }
    }

Generate QR code for apk

The information I saw on the Internet was basically completed by borrowing from a third party. I was wondering if I could make a small tool to complete this operation. Then I thought of the all-purpose Python. Let’s do it and let’s try it together.

The system comes with python2.7, so I used python2.7 to do it.

First pip install the two libraries we need

pip install pillow
pip install qrcode
vim get_img.py
#!/usr/bin/python
# encoding=utf8
 
import qrcode
import sys
 
def main(url,imgpath):
    qr = qrcode.QRCode(
        version=5,                
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4
    )
   #参数 version 表示生成二维码的尺寸大小,取值范围是 1 至 40,最小尺寸 1 会生成 21 * 21 的二维码,version 每增加 1,生成的二维码就会添加 4 尺寸,例如 version 是 2,则生成 25 * 25 的二维码,这里我们需要适当的调整大小
    qr.add_data(url)
    try:
        qr.make(fit=True)
        img = qr.make_image()
        img.save(imgpath)
    except:
        print "输入的保存二维码的路径有问题,情检查"
 
 
if __name__ == '__main__':
    if len(sys.argv) == 3:
        main(sys.argv[1],sys.argv[2])
    else:
        print "输入的参数个数不对,请输入正确的 url 链接 和 二维码保存的地址"

The python script for making QR codes is completed

Continue configuring the Jenkins build

Add a new built shell script (if you want to use it, you should make some appropriate modifications), as shown in the figure

 

Finally, add a  post-build operation , as shown in the figure

 

Here we can adjust the size of the image appropriately

<img width='300px' height='300px' src="http://ip:800/customer_${BUILD_NUMBER}.jpg"

Then save and exit, and start a new round of project construction. When I happily thought that there was no problem, I was slapped in the face again. The QR code we imagined did not appear in the place in the picture, which was very embarrassing.

This still took a while, but I finally found the solution. It turned out that there was a small configuration that needed to be changed.

System Management—>Global Security Configuration

 

But after the change here, another small problem appeared. The global description was changed, as shown in the picture. The description is no longer displayed in separate lines.

 

But for students who know a little bit about html, it’s just a small case, it depends on the operation.

 

Finally, let’s take a look at the final renderings of the project, perfect! ! ! !

 

If you encounter any problems during the process, you can contact the blogger in time

Guess you like

Origin blog.csdn.net/qq_30029665/article/details/130624393