将生成的第三方许可证复制到资产

时间:2015-08-03 11:18:37

标签: gradle licensing android-gradle

我想在Gradle中自动包含最新生成的第三方许可证。

配置:

  • Gradle 2.x
  • license-gradle-plugin 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很新,所以也许有一个简单的解释?

4 个答案:

答案 0 :(得分:2)

首先是一些观察。最终的解决方案是:

  • 你仍然在程序上思考:告诉Gradle什么时候做什么(即你在任务beforeAssemble中做了什么)。使用任务图:指定任务的依赖关系(有关某些可能性,请参见下文)。当您需要输出任务时,只需要让Gradle执行该任务,Gradle将找出该任务所依赖的传递任务。它将执行所有这些任务:只执行一次,并按正确顺序执行。
  • 此声明:task beforeAssemble(dependsOn: assemble),没有意义。您希望任务在assemble之前运行,但依赖于assemble您实现了相反的目标:assemble将在beforeAssemble之前运行。

如何指定依赖项?有多种选择:

  • 使用直接依赖:dependsOn
  • 参考任务的inputs中的其他任务。
  • 设置任务的选项,这将自动创建依赖关系。例如:如果from任务的Copy属性设置为其他任务的输出,则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']
}

答案 1 :(得分:1)

在我的项目(gradle 3.4.1)中,它在Android“Module:app”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'

答案 2 :(得分: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'
    }
}

因为现在,您的复制是在配置阶段完成的。

答案 3 :(得分:0)

这个解决方案对我有用,是Johan Stuyts发布的答案的略微修改版本。

但有一个限制:生成的报告最终会出现在根资产文件夹中而不是预期的子文件夹中#34; legal&#34;。

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