Many of Jenkins’ Android builds were basically built using Gradle. However, after modularization, different modules will exist in different projects, that is, different Git addresses. Gradle. properties, build.gradle, local.properties, settings.gradle, etc.

Here’s how we used Jenkins to automate building when we split modules into modules. It is divided into one and two chapters, respectively about the construction of library and APP.

Prerequisites:

1. Install Jenkins, JDK, Gradle and Android SDK on the host; 2. Build your own Maven private server on the host

First, this article will talk about the construction of sub-module Library.


First, the construction of sub-module Library

1. First, give the submodulebuild.gradleExample:

apply plugin: 'com.android.library' version = '1.1.5' Description = 'shop chat Library 'ext.pom_packaging = 'aar' ext.pom_groupid = 'com.showjoy.shop' ext.pom_artifactId = 'chat' ext.pom_name = 'shopandroid_chat' ext.pom_snapshot = false android { useLibrary 'org.apache.http.legacy' } dependencies { compile fileTree(dir: 'libs', include: [' *. Jar ']) the compile (' com. Showjoy. Shop: common: 1.7.7 '), the compile (' com. Showjoy. Shop: the webview: 1.2.1 '), the compile 'com. Android. Support: appcompat - v7:24.0.0'} the apply the from: System. The properties [' libraryBaseGradle]Copy the code

With the above configuration, the packaged result will generate shopAndroid_chat-release-1.1.5. aar, which will then be automatically uploaded to the Maven repository. Build. Gradle depends on libraryBaseGradle. Add your own dependencies and the module name and version information.

After the build is complete, dependencies can be used in other modules:

The compile 'com. Showjoy. Shop: chat: 1.1.5'Copy the code

Build. Gradle configuration in the project root directory:

Allprojects {repositories {maven {url 'http://192.168.0.62:8081/repository/maven-releases/'} maven {url 'http://192.168.0.62:8081/repository/maven-snapshots/'} jcenter mavenCentral () ()}}Copy the code

2, libraryBaseGradle. Gradle

Properties [‘libraryBaseGradle’] is the gradle file configured with the gradle.properties parameter in the project root directory: example:

systemProp.compileSdkVersion=23 android.useDeprecatedNdk=true org.gradle.daemon=true org.gradle.parallel=true SystemProp. BuildToolsVersion = 23.0.2 systemProp. TargetSdkVersion = 21 org. Gradle. Jvmargs = - XX \ : MaxPermSize \ = 4096 m org.gradle.configureondemand=true systemProp.minSdkVersion=14 systemProp.scheme=showjoyshop systemProp.libraryBaseGradle=http://git.xxxxxx.com/android/mvn-repo/raw/master/library.gradleCopy the code

The content of http://git.xxxxxx.com/android/mvn-repo/raw/master/library.gradle is as follows:

apply plugin: 'com.android.library' apply plugin: 'maven' android { compileSdkVersion Integer.parseInt(System.properties['compileSdkVersion']) buildToolsVersion System.properties['buildToolsVersion'] defaultConfig { minSdkVersion Integer.parseInt(System.properties['minSdkVersion']) targetSdkVersion Integer.parseInt(System.properties['targetSdkVersion']) } lintOptions { abortOnError false } libraryVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile ! = null && outputFile.name.endsWith('.aar')) { def fileName = outputFile.name.replace(".aar", "-" + version + ".aar") output.outputFile = new File(outputFile.parent, fileName) } } } uploadArchives { repositories { mavenDeployer { if (pom_snapshot) { repository (url: 'http://192.168.0.62:8081/repository/maven-snapshots/') {authentication (userName: "admin" and password: 'xxxxxx') } version = version + '-SNAPSHOT' } else { repository (url: 'http://192.168.0.62:8081/repository/maven-releases/') {authentication (userName: "admin" and password: 'xxxxxx') } } pom.version = version pom.artifactId = pom_artifactId pom.groupId = pom_groupId pom.name = pom_name pom.packaging = pom_packaging pom.project { description description dependencies { project.configurations.collectMany { it.allDependencies } .findAll { it instanceof ProjectDependency || it instanceof ExternalModuleDependency} .each { dep -> dependency { groupId dep.getGroup() artifactId dep.getName() version dep.getVersion() scope 'compile' } } } } } } } }Copy the code

3,Jenkins configuration

In Jenkins, the new item is normal. The basic information configuration is nothing special. You can fill it according to your own needs.

  • Git address configuration

Although this configuration is not used in future builds, it is useful to check for changes with each build

  • Build part, as shown in the figure:

    Here, a Python script is used for the build

python ${JENKINS_HOME}/workspace/publish/jenkins/library/build.py ${JOB_NAME} "[email protected]:shopandroid/shopandroid_chat.git"Copy the code

Among them, the ${JENKINS_HOME} / workspace/publish/Jenkins/library/build py is the path of the build scripts placed on the host, ${JENKINS_HOME} said Jenkins installation directory. ${JOB_NAME} job name ${JOB_NAME} git address

  • Construction result processing

The purpose of this configuration is to know whether the build was successful and the build version after each build, such as:

Build script build.py

  • The whole idea is to create a new directory sourcecode, then will need to file the project root directory (setting. Gradle, build gradle, gradle. Properties, Gradlew,local.properties) copies to sourcecode from the specified directory. Note The SDK path configured in local.properties must be the SDK installation directory on the host.

  • Gradle contains only the module name of the current library. Clone the library code to sourcecode and build with Gradle Clean build uploadArchives.

The code is as follows:

# coding: utf-8 OUTPUTS_PATH = "/ var/lib/Jenkins/workspace/" GRADL_HOME ="/root/gradle - 2.10 / bin/" SOURCE_CODE = "sourcecode" MODULE_NAME = "shopandroid" BRANCH = "master" def copyFiles(sourceDir, targetDir): For file in os.listdir(sourceDir): sourceFile = os.path.join(sourceDir, file) targetFile = os.path.join(targetDir, file) if os.path.isfile(sourceFile): if not os.path.exists(targetDir): os.makedirs(targetDir) if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) ! = os.path.getsize(sourceFile))): open(targetFile, "wb").write(open(sourceFile, "rb").read()) if os.path.isdir(sourceFile): First_Directory = False copyFiles(sourceFile, targetFile) def chdir(dir): Os. chdir(dir) print (' current directory: '+ os.getcwd()) def getVersionName(path): import re versionName = "" f = open(path) for line in f: searchObj = re.search( r'version(\s*)=(\s*)\'(.*)\'', line, re.M|re.I) if searchObj: versionName = searchObj.group(3) break return versionName if __name__ == "__main__": import shutil import sys import os content = "n" print sys.path[0] chdir(sys.path[0]) import time start_time = Time.time () print ('start time %f' %start_time) Print "Build script parameter not 2, 0 is the script path, Outputs "if (os.path.exists(OUTPUTS_PATH)): < outputs > < outputs > < outputs > < outputs > Shutil. rmtree(OUTPUTS_PATH) # code git_path = sys.argv[2] # The second branch can be if len(sys.argv) >= 4: Argv [3] pass print (' git_path + ') BRANCH = sys.argv[3] pass print (' git_path + ') "+ BRANCH) import re MODULE_NAME = re.search(r'/(.*)\.git', git_path). Group (1) print (' # clone code SOURCE_CODE = MODULE_NAME; if os.path.exists(SOURCE_CODE): shutil.rmtree(SOURCE_CODE) os.mkdir(SOURCE_CODE) chdir(SOURCE_CODE) print ('start to clone codes') clone_command = "/usr/bin/git clone -b "+ BRANCH +" "+ git_path print clone_command print os.system(clone_command chdir(os.path.dirname(os.getcwd())) print ('copy files from gradle_config to ' + SOURCE_CODE) copyFiles('.. /gradle_config', SOURCE_CODE) Chdir (SOURCE_CODE) # modify settingFile = open('settings.gradle', "wb") print settingFile.write("include '" + MODULE_NAME + "'") print settingFile.close() chdir(MODULE_NAME) print ('start to build library') output = os.popen(GRADL_HOME + "gradle clean build uploadArchives" GetVersionName ("build.gradle") print (" " + versionName) buildResult = False outputLog = output.read() copyFiles("build/outputs", OUTPUTS_PATH) chdir(OUTPUTS_PATH) if "BUILD SUCCESSFUL" in outputLog: f = open( versionName + '.txt','w') f.write(outputLog) f.close() buildResult = True pass print outputLog end_time = time.time() cost_time = (end_time - start_time) minutes = cost_time/60 seconds = cost_time - 60 * minutes print ('cost time : %d mins %d secs' %(minutes, seconds)) import sys if False == buildResult: sys.exit(-1) passCopy the code

Fifty cents for the code word


Good life safe thank you!

WeChat pay
Alipay

Scan the QR code with wechat to tip

Scan the QR code with Alipay