2016-07-13 81 views
1

我需要一些任務將在最後運行。可以說一些println。我看到了多個如何在最後運行任務的例子,他們總是依賴於其他任務,所以我真的不知道該怎麼做,也沒有任務。現在我有一些代碼段:運行任務一次在gradle結束

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

那一塊代碼運行兩次,一次用於實驗的味道,一個用於PROD風味的情況下,我運行:

MyApp:assembleRelease -PRELEASE=1 

我需要這個代碼和平只運行一次,所以爲此我需要在最後運行一次。

我的全gradle這個:

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') 
} 

它只是幫助我,如果你可以將某些println添加到此gradle這個將在年底運行一次。

回答

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' 
    } 
} 
+0

謝謝,這是我問的......但我需要把函數BA,如果(releaseBol){}和releaseBol沒有達到那裏。我看到BA在「BUILD SUCCESSFUL」之後運行,但我在 – Dim

+0

@Dim之前需要它,您可以在項目範圍中定義'releaseBol',然後通過'project'變量引用它。我想在不使用任務終結器的情況下,無法在「BUILD SUCCESSFUL」之前運行它。 – Opal

+0

@Dim,看看編輯 - 最後一個可能是你要找的。 – Opal

相關問題