设为首页 收藏本站
查看: 863|回复: 0

[经验分享] jenkins+svn+android studio自动化构建(持续集成)

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2018-1-7 09:47:18 | 显示全部楼层 |阅读模式
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 = [UMENG_CHANNEL_VALUE: name,APPLICATION_LABLE:name]
  
}
  
baidu {
  
applicationId = "com.zhb.baidu"
  
manifestPlaceholders = [UMENG_CHANNEL_VALUE: name,APPLICATION_LABLE:name]
  
}
  
wandoujia {
  
applicationId = "com.zhb.wandoujia"
  
manifestPlaceholders = [UMENG_CHANNEL_VALUE: name,APPLICATION_LABLE:name]
  
}
  
}*/
  
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
  
// 在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[0].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(new  File("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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-432490-1-1.html 上篇帖子: Jenkins常用插件之Deploy Plugin 下篇帖子: jenkins+maven+svn构建项目,及远程部署war包到tomcat上
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表