Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle cannot find module's output jar when executing custom task

I have a Gradle project with three modules, each with nested dependency on the next:

android app module, which depends on...
   android lib module, which depends on ...
      java lib module (called core)

Inside my lib module I'm trying to create a custom task (to generate javadocs, but that's prob not relevant)

library/build.gradle:

apply plugin: 'com.android.library'

android {
    ...
}

android.libraryVariants.all { variant ->
    task("${variant.name}Docs", type: Javadoc) {
        failOnError true
        source = variant.javaCompiler.source
        classpath = files(((Object) android.bootClasspath.join(File.pathSeparator)))
        classpath += files(variant.javaCompiler.classpath.files) // THIS LINE IS CAUSING ERROR
    }
}

dependencies {
    implementation project(':core')
    ...
}

The problem is that when I run any Gradle task (even Gradle clean), I get an error about not finding core.jar

org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':library'.

Caused by: org.gradle.api.artifacts.transform.ArtifactTransformException: Failed to transform file 'core.jar' to match attributes {artifactType=android-classes} using transform JarTransform

Caused by: org.gradle.api.InvalidUserDataException: Transform output file ******/core/build/libs/core.jar does not exist.

I guess this is happening because Gradle is looking for all files needed library's classpath (including core.jar) during the configuration phase. But why?

  1. Why doesn't gradle only resolve dependencies during execution? (i.e. Why do I see this error for every gradle task?)

  2. How to I tell gradle to build core.jar before executing this custom task?

like image 897
tir38 Avatar asked Nov 07 '17 02:11

tir38


People also ask

Where is the JAR file after Gradle build?

The Jar is created under the $project/build/libs/ folder.

How do I pass environment variables in Gradle task?

If you are using an IDE, go to run, edit configurations, gradle, select gradle task and update the environment variables. See the picture below. Alternatively, if you are executing gradle commands using terminal, just type 'export KEY=VALUE', and your job is done. Save this answer.


1 Answers

Because variant.javaCompiler.classpath.files is already files type. You can try both statements:

classpath += variant.javaCompiler.classpath.files

Or as you mention before:

classpath += files(variant.javaCompiler.classpath)
like image 128
Ivan Bryzzhin Avatar answered Oct 14 '22 10:10

Ivan Bryzzhin