I want to automate inclusion of the latest generated third party licenses in Gradle.
Config:
- Gradle 2.x
- license-gradle-plugin 0.11.0
Code:
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"
}
In command line:
gradlew clean assembleDebug
The resulting APK does not include the generated file as I was hoping. I'm quite new to Gradle so maybe there is a simple explanation?
Try on changing
CopyToAssetsTask
into:because right now, your copying is done in configuration phase.
First some observations. The solution is at the end:
beforeAssemble
). Make use of the task graph: Specify what the dependencies of a task are (see below for some of the possibilities). When you need the output of a task, just ask Gradle to execute that task, and Gradle will figure out the transitive tasks that that task depends on. It will execute all of those tasks: only once, and in the correct order.task beforeAssemble(dependsOn: assemble)
, does not make sense. You want the task to run beforeassemble
, but by depending onassemble
you achieved the reverse:assemble
will be run beforebeforeAssemble
.How to specify dependencies? There are multiple options:
dependsOn
.inputs
of a task.from
attribute of theCopy
task is set to the outputs of another task, theCopy
task will automatically add that other task as an input.Here is the solution. You want two achieve two things:
Add this to your build script:
This solution worked for me and is a slightly modified version of the answer that Johan Stuyts posted.
One limitation though: The generated report will end up in the root assets folder instead of the intended subfolder "legal".
In my project (gradle 3.4.1), it works like this in the Android "Module:app" gradle file: