Since the project needs to compile APK and publish it to dandelion platform for testers to download, before it needed to manually compile APK file and then open dandelion website and manually upload and publish it to the platform, so it would be troublesome to upload several different versions and channel packages. Therefore, A one-click automated compilation of the desired channel and version of APK and automatic publishing to the Dandelion platform is necessary. The complete process can be divided into two parts: compiling APK through Gradle task and uploading APK to dandelion platform by shell command.

task debugTask(a) {
	// This task is to clean up the apK files to ensure that the generated APK is up to date and unique
    dependsOn 'clearBacApkDir'
     // This task is to compile channel package with Walle. If not, use assembleDebug directly
    dependsOn 'walleDebug'
// dependsOn 'assembleDebug
}

task clearBacApkDir(type: Delete) {
    String bakPath = "./channels"
    file(bakPath).list().each {
        f ->
            delete "${bakPath}/${f}"
    }
    println "Delete Channels file"
}

task walleDebug(type: Exec) {
	The executable file we execute gradlew is located in the upper directory of APP so ".. /"
    workingDir ".. /"
    Gradlew Clean Assemblereleasechannels-pchannellist =meituan
    commandLine "./gradlew"."clean"."assembleDebugChannels"."-PchannelList=huawei"
}

// The task of uploading dandelion can be automatically packaged and published to dandelion platform only after executing this task
task uploadDebugApkTask(type: Exec) {
    dependsOn 'debugTask'
    def mode = "debug"
    def remark = "Debug Environment Package"
    // uploadapk. sh is responsible for getting compiled APK for shell file and uploading it to dandelion platform
    commandLine 'sh'.'-c'."./sh/uploadApk.sh $mode $remark"
}

// Walle compile channel file name configuration
walle {
    // Specify the output path of the default channel package
    apkOutputFolder = new File("${project.getProjectDir()}/channels")
    // The APK file name of the custom channel package
    apkFileNameFormat = 'doctor_${channel}_v${versionName}_${buildType}.apk';
    // Channel configuration file
    channelFile = new File("${project.getProjectDir()}/channel")
}

afterEvaluate {
    project.android.applicationVariants.all { BaseVariant variant ->
        variant.assemble.doFirst {
        	// Actually the output path of the channel package
            project.walle.apkOutputFolder = new File("${project.getProjectDir()}/channels/${variant.getBuildType().name}")
            project.walle.channelFile = new File("${project.getProjectDir()}/channel")}}}Copy the code

Sh file content is mainly obtained compiled APK file and uploaded to dandelion platform

#! /bin/sh
#MAIN_MODULE refers to the main module under the AndroidStudio project directory
MAIN_MODULE="."

#The two arguments passed in from build.gradle are MODE for the type of compilation REMARK for description
MODE=$1
REMARK=$2

#Dandelion API account Check the API information of dandelion platform and enter it
PGYER_API_KEY=""
PGYER_USER_KEY=""
#APK path// If packaged using assembleDebug, use APK_PATH annotated below#APK_PATH="${MAIN_MODULE}/build/outputs/apk/${MODE}"
APK_PATH="${MAIN_MODULE}/channels/${MODE}"
echo "current path: $APK_PATH"
#Traverse the apkfor APK_FILE in ${APK_PATH}/*; do APK_NAME=`basename $APK_FILE` if [[ "$APK_NAME" =~ "${MODE}.apk" ]]; then echo "Upload apk:$APK_NAME" break fi done#Curl uploads to dandelion and is published directly by default
curl -F "file=@${APK_PATH}/${APK_NAME}" -F "userKey=${PGYER_USER_KEY}" -F "_api_key=${PGYER_API_KEY}" -F "buildUpdateDescription=${REMARK}"  http://www.pgyer.com/apiv2/app/upload
Copy the code

At this point, a set of Mac environment Android automatic package upload to dandelion function is complete, if you need to compile pre-release and production environment, just copy the above tasks, change the Debug to Preissue and Release respectively. Run./gradlew uploadDebugApkTask in Terminal or double-click uploadDebugApkTask in Gradle on the right of AS. Conditional students can through the Jenkins, of course, configure the remote package, to make students themselves can compile a test package they want, from then on, the test students can compile environment and the version of the package, they want to develop the students also need not always break the code under test on the way students happy drives packaging various versions and the environment.