2

I have a gradle 4.1 multiproject containing a "projectA" containing 2 subfolders "api" and "implementation".

The multiproject uses kotlin and java-library plugins defined in the subprojects section of the main build.gradle. The implementation project avec a API dependency to :projectA:api

In the api folder I have kotlin and java files inside 'src/main/java' and in the implementation project I'm creating a new instance of a kotlin class from the API.

Inside Intellij Idea, I don't have any compilation errors ; but when I compile the whole project using gradle I have an error: cannot find symbol. It is as if the compileJava doesn't have access to the folder kotlin-classes.

Inside the build/kotlin-classes, I see my file.class The class file is on build/classes dir also

Details of the error :

Task :projectA:api:compileKotlin Using kotlin incremental compilation

Task :projectA:implementation:compileJava (...) error: cannot find symbol (the import fails)

Update 1 : removing java-library solved my problem

1 Answer 1

3

This is a known issue of the java-library plugin: when used in a project with another JVM language (Kotlin, Scala, Groovy etc.) , it does not register the classes of the other language so that the dependent projects get them as they consume the classes.

Fortunately, it has a workaround as well. Adapted to Kotlin, it would look like:

configurations {
    apiElements {
        outgoing.variants.getByName('classes').artifact(
            file: compileKotlin.destinationDir,
            type: ArtifactTypeDefinition.JVM_CLASS_DIRECTORY,
            builtBy: compileKotlin)
    }
}

If you use Kapt1, it's file: compileKotlinAfterJava.destinationDir, and for Gradle versions lower than 4.0 use builtBy: copyMainKotlinClasses instead.

This issue is also tracked in the Kotlin issue tracker: KT-18497, follow that issue to see when it's fixed on the Kotlin Gradle plugin side, so that the above workaround will be no more necessary.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.