上一篇jenkins+svn+android studio自動化構建(持續集成)已經描述了jenkins環境的搭建,這裏就不在繼續描述了,簡單的看一下與svn的差別在哪裏html
郵件主題中,svn版本號:${SVN_REVISION}<br/> 變成了 GIT版本號:${GIT_REVISION}<br/>android
如下給出腳本,原本是想跟svn同樣,版本編譯好了,把編譯結果和版本號修改自動push到git庫中的,可是jenkins獲取源碼好像不是clone的,因此沒有辦法提交,這個之後有空能夠研究一下,代碼先貼出來,權當是gradle腳本中使用git命令的方法吧git
project的build.gradle,dependencies中增長github
classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.8.11' classpath 'org.ajoberstar:gradle-git:1.2.0'
如下是app的build.gradle網絡
apply plugin: 'com.android.application' import org.ajoberstar.grgit.* import org.ajoberstar.grgit.auth.* import org.ajoberstar.grgit.operation.* android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { // 包名 applicationId "com.zhb.studiotest" minSdkVersion 19 targetSdkVersion 22 versionCode 1 versionName "1.0.0.1" manifestPlaceholders = [ CHANNEL_NAME:"Unspecified",APPLICATION_LABLE:"StudioTest"] } sourceSets.main.jni.srcDirs = [] sourceSets.main.jniLibs.srcDir 'src/main/libs' def versionPropsFile = file('version.properties') if (versionPropsFile.canRead()) { def Properties versionProps = new Properties() versionProps.load(new FileInputStream(versionPropsFile)) def prename = versionProps['VERSION_NAME_MAJOR'] def name = versionProps['VERSION_NAME_BUILD'].toInteger() def runTasks = gradle.startParameter.taskNames if ('buildAll' in runTasks) { name++ } versionProps['VERSION_NAME_BUILD']=name.toString() versionProps.store(versionPropsFile.newWriter(), null) defaultConfig { versionName prename + name } } signingConfigs { releaseConfig { // 寫死簽名密碼 keyAlias 'haibo.zhou' keyPassword 'hbzhou0622' storeFile file("keystore.jks") storePassword 'hbzhou0622' } } productFlavors { wandoujia {} baidu {} //c360 {} //uc {} productFlavors.all { flavor -> applicationId = "com.zhb."+name flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name,APPLICATION_LABLE:name] } } buildTypes { debug { buildConfigField "boolean", "LOG_DEBUG", "true" versionNameSuffix "-debug" minifyEnabled false zipAlignEnabled false shrinkResources false } release { buildConfigField "boolean", "LOG_DEBUG", "false" minifyEnabled true zipAlignEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' signingConfig signingConfigs.releaseConfig applicationVariants.all { variant -> // 修改APK名稱 variant.outputs.each { output -> def file = output.outputFile def fileName = file.name fileName = fileName.replace(".apk", "-V${defaultConfig.versionName}.apk") fileName = fileName.replace("app", "StudioTest") fileName = fileName.replace("debug-unaligned", "debug") output.outputFile = new File(file.parent, fileName) } // 修改values.xml variant.mergeResources.doLast(){ File valuesFile = file("${buildDir}/intermediates/res/merged/${variant.dirName}/values/values.xml") String content = valuesFile.getText('UTF-8') content = content.replaceAll("CHANNEL_NAME","${variant.productFlavors[0].name}") valuesFile.write(content,'UTF-8') } } } } } task generateZip(type: Zip){ println 'adfadsfasda123213d' def versionPropsFile = file('version.properties') def Properties versionProps = new Properties() versionProps.load(new FileInputStream(versionPropsFile)) def prename = versionProps['VERSION_NAME_MAJOR'] def name = versionProps['VERSION_NAME_BUILD'].toInteger() def version = "V" + prename + name + "("+getGitVersion()+")" from 'build/outputs' archiveName "StudioTest_" + version + ".zip" destinationDir file("build/release") doLast(){ copy{ from ("build/release/"+archiveName) into ("release") } if (!project.hasProperty("commitMsg")){ ext.commitMsg = "//upload compile result" } } } // build script for jenkins only task buildAll(){ println 'start build' } /*task gitOpera() { doLast { def grgit = Grgit.open(dir: '.', creds: ['root', 'hbzhou0622']) grgit.add(patterns: ['*.zip', 'app/release']) grgit.add(patterns: ['version.properties', 'app']) grgit.commit(message: '提交版本號變化') grgit.push(force: true) } }*/ def getGitVersion() { return 'git rev-parse --short HEAD'.execute().text.trim() } //generateZip.dependsOn build //gitOpera.dependsOn generateZip //buildAll.dependsOn gitOpera generateZip.dependsOn build buildAll.dependsOn generateZip tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn ndkBuild } task ndkBuild(type: Exec) { workingDir file('src/main/jni') commandLine getNdkBuildCmd() } task cleanNative(type: Exec){ workingDir file('src/main/jni') commandLine getNdkBuildCmd(), 'clean' } clean.dependsOn cleanNative def getNdkDir() { if (System.env.ANDROID_NDK_ROOT != null) return System.env.ANDROID_NDK_ROOT Properties properties = new Properties() properties.load(project.rootProject.file('local.properties').newDataInputStream()) def ndkdir = properties.getProperty('ndk.dir', null) if (ndkdir == null) throw new GradleException("NDK location not found. Define location with ndk.dir in the local.properties file or with an ANDROID_NDK_ROOT environment variable.") return ndkdir } def getNdkBuildCmd() { def ndkbuild = getNdkDir() + "/ndk-build" ndkbuild += ".cmd" return ndkbuild } dependencies { // 工程目錄裏面的libs文件夾下全部的jar包 compile fileTree(dir: 'libs', include: ['*.jar']) // 網絡倉庫裏面的工程 //compile 'com.github.chrisbanes.photoview:library:1.2.4' // 本地的工程 compile project(':PhotoView-master') }
郵件通知我就不發了,跟svn的大同小異,app
2016.01.21svn
------End------gradle