How to create a Zip file of dependencies used in an Android application?


(Jon Ball) #1

How can a Zip file be created that contains dependencies used in an Android application?

Context

The Zip file that contains all the the application dependencies is used by Nexus IQ server. This product can analyze dependencies to determine if there are any security vulnerabilities or licensing issues.

Problem

In earlier versions of Gradle (e.g. 3.3 and below), the following Gradle task was used to create a Zip file of dependencies.

task dependenciesZip(type: Zip) {
    from configurations.compile
}

After upgrading to Gradle 4.1, the task above stopped working.

One issue with the task above is the configuration that is used. When migrating to Gradle 4.1, all the application dependencies were changed from compile to implementation. As a result, the compile configuration did not contain any dependencies to include in the Zip file.

In an attempt to fix this problem, the task above was updated to the following:

task dependenciesZip(type: Zip) {
    from configurations.implementation
}

However, the above task fails to run with the following error:

./gradlew :app:dependenciesZip

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:dependenciesZip'.
> Resolving configuration 'implementation' directly is not allowed

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 0s

This failure is caused by an IllegalStateException that is thrown when Gradle attempts to resolve the implementation configuration. The isCanBeResolved() method returns false for the implementation configuration.

Questions

  1. What configuration (or set of configurations) should be used to capture the dependencies used in an Android application?
  2. How can a task resolve the dependencies in a configuration?

This question has been cross-posted on StackOverflow at https://stackoverflow.com/questions/48195162/how-to-create-a-zip-file-of-dependencies-used-in-an-android-application.