Automatic Update and build of Android code using crontab

Source: Internet
Author: User

Introduction

The recent work is a project developed on the Android platform, I personally basically do not change the android part of the code, but my project needs to use the Android compiled many of the target files. On the other hand, I'm not developing a generic Android platform-based project like APK, which is the android part of the code that is being maintained by other colleagues. Then there is this scenario: I need to keep the android part of the code updated and built.

People who have compiled the entire Android project know that it takes about 30 minutes to compile a time (if you are an exclusive server and the memory is oversized, walk away silently), and if the entire project is re-compiled (plus the-b parameter), it will take at least 2 hours. If you want to speed up the process of compiling and use the-JN parameter, then you are waiting to be scolded by other colleagues (because it is impossible for others to do anything at all).

For my work, ask to keep the android part of the code updates, but the timeliness is not too strong, that is, as long as the Android part of the code has changed, my side of the time to update the download and compile it. Yes, just take the time to complete the operation, but as a programmer must know, if you have to take the time to complete one thing, then this thing is basically far from the horizon.

So I just wanted to make a script that's automatically updated and built, so that the work is done silently at night, and this is the script and this post.

crontab_android-4.2.1_r1.sh

Let's take a look at the specifics of this script:

The script is decomposed as follows.

The first part

#! /bin/bash

The reason for this is that the default SH has been replaced with dash, and Dash is a very POSIX-compliant Unix shell that has the effect of "a shell that was originally run under the bash shell." Script, there will be some unexpected problems, not 100% of the concurrent use. "。 While bash is familiar to us, its syntax is relatively broad.

The underlying reason for using bash here is that before compiling Android, you need to configure the environment with the following commands:

SOURCE build/envsetup.sh

And I found that if you use the default SH to parse and execute the script, then this line of command can not be executed in the script, the error message is as follows:

./crontab_android-4.2.1_r1.sh:20:./crontab_android-4.2.1_r1.sh:source:not found

After modifying into #!/bin/bash , it can be executed smoothly. The difference between bash and Dash can be referred to as the difference between bash and dash

Part II

ANDROID_4_2=~/ANDROID-4.2.1_R1CD $ANDROID _4_2head_ver= ' svn log-r HEAD | awk ' {if (nr==2) print $} ' | Cut-b 2-' base_ver= ' svn log-r BASE | awk ' {if (nr==2) print $} ' | Cut-b 2-'

First you need to go to the Android source directory and get the latest version number and the local version number on the server. Note that the head and base are used here to represent the latest version of the server and the version of the current working copy. The explanation for head and base is described in SVN log--help:

-R [--revision] Arg:arg (some commands also take arg1:arg2 range)
A revision argument can be one of:
' HEAD ' latest in Repository
' Base ' base rev of item ' s working copy

The version Information format obtained by SVN log-r head or SVN log-r base is as follows:

------------------------------------------------------------------------R100 | Nferzhuang | 2014-12-26 15:06:44 +0800 (Fri, Dec 2014) | 2 Linesthis is a test version------------------------------------------------------------------------

Therefore, you need to read the first field of the second row through awk, and use cut to intercept the 2nd character to the end of the section. (PS: This small piece of processing is not very good, follow-up needs to be optimized)

Part III

If [$HEAD _ver-eq $BASE _ver];  Then     Echo ' $DATE: Current version ($BASE _ver) are the same with SVN server ($HEAD _ver), no need to build system ">> $CRONTAB _log_folder/crontab_log.txtelse    echo "$DATE: Current version ($BASE _ver) was different with SVN server ($HEAD _ver), need to build system ">> $CRONTAB _log_folder/crontab_log.txt    build_log= $CRONTAB _log_folder/${ Android_4_2##*/}_build_$date.log    svn up--force > $BUILD _log    svn st-q | grep ^m | grep-v $NONEED _to_revert | awk ' {print $} ' | Xargs-i svn revert {} >> $BUILD _log    source build/envsetup.sh >> $BUILD _log make    >> $BUILD _log 2 >& 1fi

Once you get the latest version number and the local version number on the server, you'll know if you need to update the code by comparing them, and if not, log the log directly to the specified file. Otherwise, the code needs to be updated and compiled.

Let's start with Build_log, which is how the name of the LOG file is composed.

    Android_4_2=~/android-4.2.1_r1        date= ' DATE "+%y-%m-%d" '    build_log= $CRONTAB _log_folder/${android_4_2##*/} _build_$date.log

Date acquisition can refer to the man Manual of the date command, which mainly describes the method of shell interception string in Linux, please refer to the article "Summary of Shell interception string method in Linux"

Use the # # operator. The purpose is to delete the last occurrence of the substring, which is the left character, from the left side, leaving the right character. Usage is ##*substr, for example:
Str= ' http://www.baidu.com/cut-string.html '
Echo ${str##*/}
The result is cut-string.html, which deletes the last "/" and all of its left characters.
Note: If it is echo ${str##*//}, the output is the same as Echo ${str#*//} because there is only one "//" string in str

What am I doing here? The main function is to intercept the last field of the $android_4_2 string and then assemble it into a file name with _build_$date.log.

One of the following commands also needs to be told separately:

    SVN st-q | grep ^m | Grep-v $NONEED _to_revert | awk ' {print $} ' | Xargs-i svn revert {} >> $BUILD _log

Let's talk about what this line of code does: it restores all the files (except the $noneed_to_revert file) that were changed in the current folder. To break it down is:

    • SVN st-q: Get a summary of the current SVN changes, note that using the-Q parameter here does not show files that are not in version management
    • grep ^m: Filters out all lines starting with M, i.e. files with changes
    • Grep-v $NONEED _to_revert: The specified $noneed_to_revert file is not included in the filtered file
    • awk ' {print $} ': Print out the second field for each row
    • Xargs-i svn revert {}: Perform SVN revert operations on the results of each row, refer to the article "Xargs I parameter " for the use of xargs-i parameters

The last code to be explained is:

    Make >> $BUILD _log 2>& 1

The purpose of using 2>& 1 here is not only to redirect the results of the standard output to a file, but also to redirect the result of the error output to a file. For details, please refer to the article: "2>&1 use"

Crontab Brief Introduction

The content of the crontab_android-4.2.1_r1.sh script is introduced, and if you let the script run at the scheduled time it is crontab work. First look at the Baidu encyclopedia in the definition of crontab:

The crontab command is commonly used in Unix-and Unix-like operating systems to set instructions that are executed periodically. The command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution. The word derives from the Greek language Chronos (χρνο), which was originally meant to be time. Typically, crontab stored instructions are activated by the daemon, run in the background, and check every minute for scheduled jobs to be executed. This type of work is generally called cron jobs.

The format of the crontab job is as follows:
* * * * * command
Time-sharing Weekly command
The 1th column represents minutes 1~59 per minute with * or */1
The 2nd column represents the hour 1~23 (0 means 0 points)
The 3rd column represents the date 1~31
The 4th column represents the month 1~12
5th Column Identification Number Week 0~6 (0 = Sunday)
6th List of commands to run

The crontab command is used in the following ways:

Usage:crontab [-u user] File
-E (Edit user ' s crontab)
-L (list user ' s crontab)

In simple terms, it is edited with the-e parameter and is viewed with the-l parameter. Specific editing methods do not speak, the following look at my crontab job table:

#00 XX * * * Date >> ~/bak/date.txt00 * * * SH ~/bin/crontab_android-4.2.1_r1.sh &

The above line is for testing to make sure that you can start a task every morning, and the following line adds a crontab_android-4.2.1_r1.sh script that executes 00:00 every day.

Here's why & is used to force a background run, mainly because the script is a blocking job and should not monopolize the execution of the CRONTAB task table.

Summarize

This blog is actually not very good, it involves several aspects of crontab, SVN, Shell and so on, each one is a thick book, where I use their combination to complete an automated action.

The final sentence is: the ability to manufacture and use tools is the essential difference between humans and animals .

Automatic Update and build of Android code using crontab

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.