2015-08-03 52 views
1

我想在Gradle中自動包含最新生成的第三方許可證。將生成的第三方許可證複製到資產

配置:

  • 搖籃2.x的
  • 許可的gradle-插件0.11.0

代碼:

task beforeAssemble(dependsOn: assemble) << { 
    downloadLicenses.execute() 
    CopyToAssetsTask.execute() 
} 

task CopyToAssetsTask(type: Copy) { 
    description = 'Copies generated Third Party Licenses into assets' 
    from('$buildDir/reports/license') { 
     include('dependency-license.html') 
    } 
    into '$buildDir/intermediates/assets/debug/legal' 
} 

apply plugin: 'license' 

downloadLicenses { 
    includeProjectDependencies = true 
    reportByDependency = true 
    reportByLicenseType = true 
    dependencyConfiguration = "compile" 
} 

在命令行: gradlew clean assembleDebug

由此產生的APK不包括生成的文件,因爲我希望。我對Gradle很陌生,所以也許有一個簡單的解釋?

+0

的問題是,該任務將不會運行。 – Alix

回答

0

該解決方案爲我工作是答案略加修改的版本,約翰Stuyts公佈。

儘管存在一個限制:生成的報告將最終位於根資產文件夾中,而不是預期的子文件夾「legal」。

buildscript { 
     // ... 
     dependencies { 
      // ... 
      classpath 'nl.javadude.gradle.plugins:license-gradle-plugin:0.11.0' 
     } 
    } 

    android { 
    // ... 

    // Add the output folder of the license plug-in as 
    // a source folder for assets. 
     sourceSets { 
      main { 
       // ... 
       assets.srcDirs = ['src/main/assets', 'build/reports/license'] 
      } 
     } 
    } 

    downloadLicenses { 
     includeProjectDependencies = true 

     reportByDependency = true 
     reportByLicenseType = false 

     report.html.enabled = true 
     report.xml.enabled = false 

     dependencyConfiguration = "compile" 
    } 

    // Add a dependency after the project has been evaluated, because 
    // the Android plug-in does not add tasks "merge*Assets" to 
    // the project immediately. 
    project.afterEvaluate { 
     // Download the licenses when (actually: just before) the 
     // assets are merged (for both the "debug" and the 
     // "release" build types). 

     // If you add/change build types, you have to add to/change 
     // these task names. 
     mergeDebugAssets.dependsOn project.tasks.getByName('downloadLicenses') 
     mergeReleaseAssets.dependsOn project.tasks.getByName('downloadLicenses') 
    } 
0

嘗試改變CopyToAssetsTask到:

task CopyToAssetsTask << { 
    copy { 
     description = 'Copies generated Third Party Licenses into assets' 
     from('$buildDir/reports/license') { 
      include('dependency-license.html') 
     } 
     into '$buildDir/intermediates/assets/debug/legal' 
    } 
} 

,因爲現在,你複製在配置階段完成。

+1

隨着更改,您建議在執行階段仍會進行復制。這就是它應該發生的地方。不要嘗試使用配置階段進行構建步驟。 –

+0

啊,我的錯。我想寫「複製在配置階段完成」。 – mate00

+0

不是。 「副本」任務的實例僅在配置階段使用'from','into'等來配置。實際的複製將在執行階段發生。 –

2

首先一些觀察。解決方案在最後:

  • 你還在思考程序化:告訴Gradle在什麼時候做什麼(即你在做什麼beforeAssemble)。使用任務圖:指定任務的依賴關係(參見下面的一些可能性)。當你需要輸出一個任務時,只需要問Gradle來執行那個任務,Gradle就會找出那個任務依賴的傳遞任務。它將執行所有這些任務:只執行一次,並按正確的順序執行。
  • 此聲明:task beforeAssemble(dependsOn: assemble),沒有意義。您希望任務在assemble之前運行,但取決於assemble您取得了相反的結果:assemble將在beforeAssemble之前運行。

如何指定依賴關係?有多種選擇:

  • 使用直接依賴關係:dependsOn
  • 參考任務inputs中的其他任務。
  • 設置任務的選項,該選項將自動創建依賴關係。例如:如果Copy任務的from屬性設置爲其他任務的輸出,則Copy任務將自動將該其他任務添加爲輸入。

這是解決方案。您希望兩個實現兩件事:

  • 生成期間(在某個特定點)生成許可證信息。
  • 將許可證信息包含在檔案中。

添加到您的構建腳本:

android { 
    // ... 

    // Add the output folder of the license plug-in as 
    // a source folder for resources. 
    sourceSets { 
     main { 
      resources { 
       srcDir 'build/reports/license' 
      } 
     } 
    } 
} 

downloadLicenses { 
    includeProjectDependencies = true 
    reportByDependency = true 
    reportByLicenseType = true 
    dependencyConfiguration = "compile" 
} 

// Add a dependency after the project has been evaluated, because 
// the Android plug-in does not add tasks "generate*Resources" to 
// the project immediately. 
project.afterEvaluate { 
    // Download the licenses when (actually: just before) the 
    // resources are generated (for both the "debug" and the 
    // "release" build types). 

    // If you add/change build types, you have to add to/change 
    // these task names. 
    generateDebugResources.dependsOn tasks['downloadLicenses'] 
    generateReleaseResources.dependsOn tasks['downloadLicenses'] 
} 
+0

這是非常有用的,它現在幾乎可以工作,但文件不會放入資產文件夾,而是將它們編譯到apk文件的根目錄中。 此外:'sourceSets.main.resources.srcDir'應該是'sourceSets.main.resources.srcDirs' – Alix

+0

一個小小的調整解決了它。不過,我想將其放入資產內的「合法」文件夾中(生成的文件現在放置在根資產文件夾中)。 我的TWEAK: 'assets.srcDirs = [ '的src /主/資產', '建立/報告/許可證']'' mergeDebugAssets.dependsOn project.tasks.getByName( 'downloadLicenses')'' mergeReleaseAssets.dependsOn project.tasks.getByName('downloadLicenses')' – Alix

+0

我有一個類似的問題,我正在努力獲得這個工作,你可以粘貼一個完整的例子@Alix你如何解決它? – tskulbru

1

在我的項目(3.4.1的gradle),它是這樣工作在Android的 「模塊:應用程序」 文件的gradle:

plugins { 
    id "com.github.hierynomus.license" version "0.14.0" 
} 

license { 
    include "**/*.java" 
    strictCheck = false 
    ignoreFailures = true 
    skipExistingHeaders = true 
} 

downloadLicenses { 
    includeProjectDependencies = true 
    reportByDependency = true 
    reportByLicenseType = false 
    dependencyConfiguration = "compile" 
    licenses = [ 
     (group('com.android.support')) : license('Apache License, Version 2.0', 'http://www.apache.org/licenses/LICENSE-2.0'), 
     (group('com.android.support.constraint')) : license('Apache License, Version 2.0', 'http://www.apache.org/licenses/LICENSE-2.0'), 
     (group('com.google.firebase')) : license('Apache License, Version 2.0', 'http://www.apache.org/licenses/LICENSE-2.0'), 
     (group('com.google.android')) : license('Apache License, Version 2.0', 'http://www.apache.org/licenses/LICENSE-2.0'), 
     (group('com.google.android.gms')) : license('Apache License, Version 2.0', 'http://www.apache.org/licenses/LICENSE-2.0') 
] 
} 

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.2" 

    defaultConfig { 
    ... 
    } 

    sourceSets { 
     main { 
      assets.srcDirs = ['src/main/assets', 'build/licenses/'] 
     } 
    } 

    buildTypes { 
    ... 
    } 
} 

dependencies { 
    compile 'com.android.support:appcompat-v7:25.3.1' 
    compile 'com.android.support:multidex:1.0.1' 
    compile 'com.android.support:support-v4:25.3.1' 
    compile 'com.android.support:design:25.3.1' 
    ... 
} 

task copyLicenseReport(type: Copy) { 
    from('build/reports/license') { 
    include '**/*.html' 
    include '**/*.xml' 
    } 
    into 'build/licenses/thirdPartyLicenses' 
} 

project.afterEvaluate { 
    copyLicenseReport.dependsOn project.tasks.getByName('downloadLicenses') 
    preBuild.dependsOn copyLicenseReport 
} 

apply plugin: 'com.google.gms.google-services'