Jenkins集成Android apk编译的条件如下:
1、服务器上安装好jenkins,配置好Android sdk,build tools,gradle,Android sdk需要配置ANDROID_HOME环境变量,gradle也需要配置环境变量;
2、复制代码路径可以到app层级,将project目录下的build.gradle里面内容直接复制粘贴到app目录下的build.gradle文件前面即可。由于服务器的环境(keystore存放路径)可能和本地不一致,所以一般新建一个jenkins.gradle文件,这个文件专门用于服务器构建app使用,本地使用还是app目录下的build.gradle
3、编译apk的命令如下(已配置环境变量):
gradle -b jenkins.gradle build
4、jenkins.gradle示例如下:
import org.tmatesoft.svn.core.wc.*
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.2"
classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.8.11'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }//这就是添加的
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
minSdkVersion 22
targetSdkVersion 22
versionCode 1
versionName "V1.0.0"
}
// applicationVariants.all { variant ->
// variant.outputs.each { output ->
// if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) {
// output.outputFile = file("${output.outputFile.parent}/AndroidPlugin" +
// "-${variant.buildType.name.toLowerCase()}" +
// "_R${getSvnRevision()}_${releaseTime()}" +
// ".apk")
// }
// }
// }
applicationVariants.all { variant ->
variant.outputs.all { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "AndroidPlugin" + "-${variant.buildType.name.toLowerCase()}" +
"_R${getSvnRevision()}_${releaseTime()}_${getVersionName()}" +
".apk"
outputFileName = fileName
}
}
}
lintOptions {
checkReleaseBuilds false
abortOnError false
}
signingConfigs {
release {
storeFile file("/home/jenkins/android/application.keystore")
storePassword 'xxx'
keyAlias 'xxx'
keyPassword 'xxx'
}
debug {
storeFile file("/home/jenkins/android/application.keystore")
storePassword 'xxx'
keyAlias 'xxx'
keyPassword 'xxx'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:25.3.1-alpha1'
implementation 'com.android.support:design:25.3.1'
configurations.all {//不加这配置会出现Resolved versions for app (25.1.0) and test app (27.1.1) differ
resolutionStrategy.force 'com.android.support:support-annotations:26.1.0'
}
testImplementation 'junit:junit:4.12'
testImplementation 'androidx.test.ext:junit:1.0.0-beta02'
testImplementation 'androidx.test:runner:1.1.0-beta02'
//butterknife
implementation 'com.jakewharton:butterknife:10.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
//rxjava
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'io.reactivex.rxjava2:rxjava:2.2.0'
//retrofit
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.4.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
//exoplayer
implementation 'com.google.android.exoplayer:exoplayer:2.8.1'
implementation 'com.google.android.exoplayer:exoplayer-core:2.8.1'
//分页
compile 'com.github.Aspsine:SwipeToLoadLayout:1.0.4'
}
def releaseTime() {
return new Date().format("yyyyMMddHHmmss")
}
def getVersionName(){
return android.defaultConfig.versionName
}
def getSvnRevision() {
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
SVNClientManager clientManager = SVNClientManager.newInstance(options);
SVNStatusClient statusClient = clientManager.getStatusClient();
SVNStatus status = statusClient.doStatus(projectDir, false);
SVNRevision revision = status.getCommittedRevision();
return revision.getNumber();
}
5、如果编译过程中,报错,提示jenkins.gradle不是项目一部分,请修改settings.gradle文件,则可以把project下的settings.gradle放到app目录下;
6、如果编译过程中,报错,提示找不到androidx,可以把project下的gradle.properties放到app目录下
7、以下是gradle3.3版本使用的设置文件名的方式,如果使用高版本gradle,请参考jenkins.gradle的配置
// applicationVariants.all { variant ->
// variant.outputs.each { output ->
// if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) {
// output.outputFile = file("${output.outputFile.parent}/AndroidPlugin" +
// "-${variant.buildType.name.toLowerCase()}" +
// "_R${getSvnRevision()}_${releaseTime()}" +
// ".apk")
// }
// }
// }