玩龙天子 发表于 2018-1-7 09:47:18

jenkins+svn+android studio自动化构建(持续集成)

import org.tmatesoft.svn.core.wc.*  
import org.tmatesoft.svn.core.wc2.
*  
import org.tmatesoft.svn.core.
*  

  
apply plugin:
'com.android.application'  

  
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 'xxx'
  
keyPassword 'xxxx'
  
storeFile file("keystore.jks")
  
storePassword 'xxxx'
  
// 要求输入签名密码
  
//            storeFile file("keystore.jks")
  
//            keyAlias System.console().readLine("\nkeyAlias: ")
  
//            storePassword System.console().readLine("\nKeystore password: ")
  
//            keyPassword System.console().readLine("\nKey password: ")
  
      }
  
}
  

  
/*productFlavors {
  
xiaomi {
  
applicationId = "com.zhb.xiaomi"
  
manifestPlaceholders =
  
}
  
baidu {
  
applicationId = "com.zhb.baidu"
  
manifestPlaceholders =
  
}
  
wandoujia {
  
applicationId = "com.zhb.wandoujia"
  
manifestPlaceholders =
  
}
  
}*/
  
productFlavors {
  
wandoujia {}
  
baidu {}
  
//c360 {}
  
//uc {}
  
productFlavors.all { flavor ->
  
applicationId = "com.zhb."+name
  
flavor.manifestPlaceholders =
  
}
  

  
}
  

  
buildTypes {
  
debug {
  
buildConfigField "boolean", "LOG_DEBUG", "true"
  
versionNameSuffix "-debug"
  
// 混淆开关
  
minifyEnabled false
  
// 在Android中,每个应用程序中储存的数据文件都会被多个进程访问:
  
// 安装程序会读取应用程序的manifest文件来处理与之相关的权限问题;
  
// Home应用程序会读取资源文件来获取应用程序的名和图标;
  
// 系统服务会因为很多种原因读取资源(例如,显示应用程序的Notification);
  
// 此外,就是应用程序自身用到资源文件。
  
// 当资源文件通过内存映射对齐到4字节边界时,访问资源文件的代码才是有效率的。
  
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.name}")
  
valuesFile.write(content,'UTF-8')
  
}
  
}
  
}
  
}
  
}
  

  
def getSvnRevision(){
  
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
  
SVNClientManager clientManager = SVNClientManager.newInstance(options);
  
SVNStatusClient statusClient = clientManager.getStatusClient();
  
SVNStatus status = statusClient.doStatus(project.rootDir, false);
  
SVNRevision revision = status.getRevision();
  
return revision.getNumber();
  
}
  

  
task svnCommitVersionFile(){
  
description = "Commits a single file to an SVN repository"
  
doLast{
  
if (!project.hasProperty("commitMsg")){
  
ext.commitMsg = "//change version"
  
}
  
SvnOperationFactory svnOperationFactory = new SvnOperationFactory()
  
def authentication = SVNWCUtil.createDefaultAuthenticationManager("haibo.zhou", "hbzhou0622")
  
svnOperationFactory.setAuthenticationManager(authentication)
  
try {
  
SvnCommit commit = svnOperationFactory.createCommit()
  
commit.setSingleTarget(SvnTarget.fromFile(new File('app/version.properties')))
  
commit.setCommitMessage(commitMsg)
  
SVNCommitInfo commitInfo = commit.run()
  
println "Commit info: " + commitInfo
  
println "Commit message: " + commitMsg
  
} finally{
  
svnOperationFactory.dispose()
  
}
  
}
  
}
  

  
task generateZip(type: Zip){
  
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 + "_(" + getSvnRevision() + ")"
  

  
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"
  
}
  
SvnOperationFactory svnOperationFactory = new SvnOperationFactory()
  
def authentication = SVNWCUtil.createDefaultAuthenticationManager("haibo.zhou", "hbzhou0622")
  
svnOperationFactory.setAuthenticationManager(authentication)
  
try {
  
SvnScheduleForAddition add = svnOperationFactory.createScheduleForAddition();
  
SvnTarget target = SvnTarget.fromFile(newFile("app/release/"+archiveName));
  
add.addTarget(target);
  
add.setAddParents(true);
  
add.setForce(true);
  
add.run();
  

  
SvnCommit commit = svnOperationFactory.createCommit()
  
commit.setSingleTarget(SvnTarget.fromFile(new File("app/release/"+archiveName)))
  
commit.setCommitMessage(commitMsg)
  
SVNCommitInfo commitInfo = commit.run()
  
println "Commit info: " + commitInfo
  
println "Commit message: " + commitMsg
  
} finally{
  
svnOperationFactory.dispose()
  
}
  
}
  
}
  

  
// build script for jenkins only
  
task buildAll(){
  
println 'start build'
  
}
  

  
svnCommitVersionFile.dependsOn build
  
generateZip.dependsOn svnCommitVersionFile
  
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')
  
}
页: [1]
查看完整版本: jenkins+svn+android studio自动化构建(持续集成)