Android Jenkins自动化构建

390 阅读1分钟

安装

安装建议使用brew安装,jenkins官网下载会遇到各种问题,使用这种方式安装后因版本过低部分插件无法使用,需升级jenkins版本,进入官网下载.war格式的包。在finder中找到Jenkins的位置~/.jenkins,将.war进行替换,升级成功。

brew install jenkins //安装

环境配置

安装完成启动jenkins,更具指导安装官网默认插件或者自定义安装插件

jenkins //启动

Global Tool Configuration目录下进行插件环境变量配置

  • jdk配置
/usr/libexec/java_home  //获取本地jdk路径

jdk配置.png

  • Gradle配置
    Gradle配置.png
  • git配置
    image.png

  • Manage Jenkins - Configure System目录下配置Android SDK
    image.png

创建项目及参数化配置

1.新建Item下创建建一个Freestyle project

image.png
2.项目配置

  • 进入项目配置列表
    image.png
  • General参数化配置 图例为创建打包类型及多渠道大打包名,后续构建可自动选择,空默认为全选
    General参数化配置.png
  • 源码地址配置
    源码地址配置.png
  • 构建及构建后操作
    image.png
    选择Use Gradle Wrapper选项 添加构建命令:clean assemble${}PRODUCT_FLAVORS{BUILD_TYPE} --stacktrace --debug 构建后操作便于找到构建后的apk
  • 构建成功
    image.png

build.gradle配置

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.example.autobuild"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        flavorDimensions "versionCode"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    signingConfigs {
        release {
            keyAlias 'eminem'
            keyPassword 'eminem'
            storeFile file('./keystore/test.jks')
            storePassword 'eminem'
        }
    }

        buildTypes {
            debug {
                signingConfig signingConfigs.release
            }
            release {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                signingConfig signingConfigs.release
            }
        }
        productFlavors {
            web {}
            baidu {}
        }
        productFlavors.all {
            flavor -> flavor.manifestPlaceholders = [CHANNEL_VALUE: name, JPUSH_CHANNEL: name]
        }

        //自定义APK名称,多渠道打包
        android.applicationVariants.all { variant ->
            variant.outputs.all { output ->
                variant.productFlavors.each { flavor ->
                    def channel = variant.productFlavors[0].name.startsWith("_") ? variant.productFlavors[0].name.substring(1) : variant.productFlavors[0].name
                    def fileName = "v${defaultConfig.versionName}_${releaseTime()}_${channel}.apk"
                    if (buildType == "release") {
                        fileName = "v${defaultConfig.versionName}_${releaseTime()}_${channel}.apk"
                    }
                    output.outputFileName = fileName
                }
            }
        }

        lintOptions {

            checkReleaseBuilds false

            abortOnError false

        }
    }


def releaseTime() {
    return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}