2017-08-14 48 views
2

我有一個簡單的腳本的Gradle(運行我的搖籃任務時只需登錄)Gradle 3.0.0(在Android Studio 3.0中)對任務序列進行重新排序(使用Kotlin模塊進行編譯時)?

class TestScriptTask extends DefaultTask { 
     @TaskAction 
     def testScript() { 
      logger.quiet("My Gradle Task Here") 
     } 
    } 

    project(":app") { 
     task testScript(type: TestScriptTask) { 
     } 
    } 

而且我有一個簡單的Android項目,「應用程序」,這是依賴於科特林模塊「remotelib」。

我的應用程序的依賴關係的build.gradle如下

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile project(':remotelib') 
    // and others 
} 

所以,當我跑我的任務的gradle如下

./gradlew :app:assembleRelease :app:testScript 

運行腳本如下(注意這裏的任務首先是執行)

> Task :app:testScript 
My Gradle Task Here 

> Task :remotelib:compileReleaseKotlin 
Using kotlin incremental compilation 

這隻發生在開始使用'com.android.tools.build:gradle:3.0.0-beta1'(以及beta2)。

當我上'com.android.tools.build:gradle:2.3.2',序列沒關係,即預期

> Task :remotelib:compileReleaseKotlin 
Using kotlin incremental compilation 

> Task :app:testScript 
My Gradle Task Here 

這是一個gradle這個3.0.0的bug,或處理任務進行新的方式?

更新

我的項目範圍的build.gradle如下

buildscript { 
    ext.kotlin_version = '1.1.2-4' 
    repositories { 
     google() 
     jcenter() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:3.0.0-beta1' 
     classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 
     } 
} 

apply from: 'test_script.gradle' 


allprojects { 
    repositories { 
     google() 
     jcenter() 
    } 
} 

task clean(type: Delete) { 
    delete rootProject.buildDir 

} 
+0

Android Gradle插件3.0中有很多更改。0以及它與Kotlin插件的互操作,並且這些更改還可能包括一些影響任務排序的部分。然而,在你的例子中,你不指定(至少在這裏發佈的代碼中)你的':app:testScript'任務依賴'remotelib'中的Kotlin編譯。它可以通過'dependsOn'關係完成,也可以通過'app'的'compile'配置作爲'testScript'任務的輸入。否則,這兩個任務之間的執行順序是未定義的,並且它們可以以任意順序運行。 – hotkey

+0

感謝@hotkey。你能告訴我如何使':app:testScript任務取決於在remotelib中的Kotlin編譯嗎?謝謝。 – Elye

+0

不客氣。我已經描述瞭如何在答案中做到這一點。 – hotkey

回答

1

有很多在Android搖籃插件3.0.0的變化及其與科特林插件互操作,以及改變也可能包括一些影響任務排序的部分。

但是,在您的示例中,您不指定(至少在此處發佈的代碼中):app:testScript任務取決於:remotelib中的Kotlin編譯。因此這兩個任務之間的執行順序是未定義的,並且它們可以以任意順序運行。

這可能是由以下原因之一進行:

  • testScript.dependsOn configurations.compile

    既然你已經添加compile project(':remotelib')app的依賴性,使其testScript取決於configurations.compile也將觸發違約的構建在項目:remotelib中配置,因此也在其中編譯Kotlin。

  • testScript.dependsOn project(':remotelib').compileKotlin

    這明確地指定任務取決於:remotelib:compileKotlin,但它需要:remotelib進行評估之前:app(您可能需要將evaluationDependsOn ':remotelib'行添加到您的:app的構建腳本)。

+0

上面的'TestScriptTask'全部駐留在'test_script.gradle'文件中。在'build.gradle'這個項目中,我調用''test_script.gradle''。我嘗試在應用程序'build.gradle'中,甚至在'test_script.gradle'中應用你提出的'testScript.dependsOn configurations.compile' ...但是不斷收到錯誤。我應該在哪裏放置你的建議腳本? – Elye

+0

@Elye,你得到的錯誤是什麼?基本上,我在回答中提出的解決方案應該在* testScript任務創建後應用。也就是說,在'build.gradle'中應用':'後,或在'test_script.gradle'中的任務定義之後。 – hotkey

+0

我得到這個錯誤'無法得到類型爲org.gradle.api.Project.'的根項目'MyApplication'的未知屬性'testScript''我將它添加到''test_script.gradle''的行後面:顯示在我上面的更新中。 – Elye

相關問題