因此,我有一个使用詹金斯构建调试apk的android应用程序。我正在为apk使用自定义文件名。要为apk使用自定义文件名,我在应用级gradle文件中使用以下代码来重命名内置的apk:
def gitBranch() {
def branch = ""
def proc = "git rev-parse --abbrev-ref HEAD".execute()
proc.in.eachLine { line -> branch = line }
proc.err.eachLine { line -> println line }
proc.waitFor()
branch
}
android {
compileSdkVersion 29
buildToolsVersion '28.0.3'
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
// Redirect your apks to new defined location to outputDirPath
def outputDirPath = new File("${project.rootDir.absolutePath}/apks/${variant.flavorName}/${variant.buildType.name}")
variant.packageApplicationProvider.get().outputDirectory = outputDirPath
def apkFileName = "${rootProject.name}_${gitBranch()}_${android.defaultConfig.versionName}.apk"
output.outputFileName = apkFileName // directly assign the new name back to outputFileName
}
}
defaultConfig {
applicationid "com.example.androidbubble"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
resValue "string","gitBranch",gitBranch()
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
}
}
compileOptions {
targetcompatibility = 1.6
sourceCompatibility = 1.6
}
}
现在,当我使用android studio来构建apk时,我会在所需位置找到具有所需文件名的apk(例如AndroidBubble_master_1.0.apk,其中“ master”是git分支)
但是当我尝试从jenkins执行构建时,构建完成后jenkins生成的工件文件名不是我想要的。我输入了错误的分支名称(例如AndroidBubble_HEAD_1.0.apk)。在用詹金斯构建应用程序之前,我有一个branch_selector,它允许我选择要从中构建应用程序的git分支。而不是像AndroidBubble_master_1.0这样命名分支,它在jenkins中始终为“ HEAD”。在AndroidStudio上一切正常,但在jenkins上则无法正常工作。我从链接中遵循了以下教程:https://www.serverkaka.com/2019/03/generate-android-apk-from-source-code-jenkins.html
仅供参考:我是詹金斯的新朋友。