The default scala plugin task flow compiles Java before Scala so importing Scala sources within Java causes "error: cannot find symbol".
From inside the new project directory, run the init task using the following command in a terminal: gradle init . When prompted, select the 2: application project type and 5: Scala as implementation language. Next you can choose the DSL for writing buildscripts - 1 : Groovy or 2: Kotlin .
Gradle supports version 1.6. 0 of Zinc and above. The Zinc compiler itself needs a compatible version of scala-library that may be different from the version required by your application. Gradle takes care of specifying a compatible version of scala-library for you.
Scala is compiled to Java bytecodes, and you can use tools like javap (Java class file disassembler) to disassemble bytecodes generated by the Scala compiler. In most cases, Scala features are translated to Java features so that Scala can easily integrate with Java.
This is because of slow internet connection or you haven't configure proxy settings correctly. Gradle needs to download some dependencies , if it cant access the repository it fires this error. All you have to do is check your internet connection and make sure gradle can access the maven repository.
I found the following sourceSet config to fix the problem:
sourceSets { main { scala { srcDirs = ['src/main/scala', 'src/main/java'] } java { srcDirs = [] } }
This because the scala source set can include both java and scala sources.
If your java code use some external libraries like Lombok, using scala compiler to build java class will failed, as scala compiler don't know annotations.
My solution is to change the task dependencies, make compiling Scala before Java.
tasks.compileJava.dependsOn compileScala tasks.compileScala.dependsOn.remove("compileJava")
Now the task compileScala
runs before compileJava
, that's it.
If your java code depends on scala code, you need to do two more steps,
Separate the output folder of scala and java,
sourceSets { main { scala { outputDir = file("$buildDir/classes/scala/main") } java { outputDir = file("$buildDir/classes/java/main") } }
Add the scala output as a dependency for compileJava
,
dependencies { compile files("$sourceSets.main.scala.outputDir") }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With