在gradle结束时运行一次任务

时间:2016-07-13 12:02:16

标签: android gradle android-gradle

我需要一些将在最后运行的任务。让我们说一些println。我看到了多个如何在最后运行任务的例子,他们总是依赖于其他任务,所以我真的不知道怎么做,而且我没有任务。现在我有一些代码:

if (releaseBol) 
{
 // Some of my code
 // Run twice, one for lab and other for prod
 println    fileName
}

这段代码运行两次,一次用于实验室味道,另一次用于生产的味道:

MyApp:assembleRelease -PRELEASE=1

我需要这段代码才能运行一次,所以为此我需要在最后运行一次。

我全力以赴:

import org.tmatesoft.svn.core.wc.*

apply plugin: 'com.android.application'

def BuildNumberFile = new File('./BuildNumber.txt')
def BuildNumber = 'BRND'
def _versionName = '1.0.1'
def _applicationId = 'com.myapp.android.pic'
def _versionCode = 17
def fileName = ''
def obfuscated = false
def releaseBol = false


android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    lintOptions {
        abortOnError false
    }

    def lines = BuildNumberFile.readLines()
    BuildNumber = 'S'+lines.get(0)

    defaultConfig {
        applicationId _applicationId
        minSdkVersion 15
        targetSdkVersion 23
        versionCode _versionCode
        versionName _versionName
        multiDexEnabled true
        resValue 'string', 'BUILD_NUMBER_RES', BuildNumber


    }

    if (project.hasProperty('RELEASE') && project.ext.RELEASE == '1')
        releaseBol = true

    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.outputFile = new File(
                    output.outputFile.parent,
                    output.outputFile.name.replace(".apk", "-${variant.versionName}." + BuildNumber + "-" + getSvnRevision() + ".apk"))
            fileName=output.outputFile.name

        }


        variant.assemble.doLast {
            variant.outputs.each { output ->
                println "aligned " + output.outputFile
                println "unaligned " + output.packageApplication.outputFile

                File unaligned = output.packageApplication.outputFile;
                File aligned = output.outputFile
                if (!unaligned.getName().equalsIgnoreCase(aligned.getName())) {
                    println "deleting " + unaligned.getName()
                    unaligned.delete()
                }

            }

            if (releaseBol) {

                // Some of my code
                // Run twice, one for lab and other for prod
                println fileName
            }
        }
    }
    signingConfigs {
        release {
            storeFile file('myapp')
            storePassword "myapp123"
            keyAlias 'myapp'
            keyPassword "myappmobile"
        }
    }

    productFlavors {
        prod {


        }

        lab {

        }

    }

    buildTypes {
        release {
            minifyEnabled obfuscated
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-android.txt'
            signingConfig signingConfigs.release
        }


    }


    dexOptions {
        preDexLibraries = false
        javaMaxHeapSize "4g"
        jumboMode true
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':trunk')
}

如果您可以在此gradle中添加一些println,那么它只会帮助我。

1 个答案:

答案 0 :(得分:2)

您需要添加BuildListener的实例:

class BA extends BuildAdapter {
    void buildFinished(BuildResult result) {
       println "finished"
    }
}

gradle.addListener(new BA())

修改

说到releaseBol,可以通过以下方式完成:

project.ext.releaseBol = false

class BA extends BuildAdapter {

    Project project

    BA(Project project) {
      this.project = project
    }

    void buildFinished(BuildResult result) {
       println "finished $project.releaseBol"
    }
}

gradle.addListener(new BA(project))

编辑2

您还可以使用gradle TaskExecutionGraph。通过whenReady关闭,您可以获得图表已准备好并填充任务的信息。此时没有选项可以修改图表(添加/删除任务,更改订单),但您可以(通过getAllTasks)获取最后一个并添加操作。这是它的样子:

task someTask << {
  println "run"
}

gradle.taskGraph.whenReady {
  gradle.taskGraph.allTasks[-1] << {
    println 'finished'
  }
}