libGdx 和 Gradle 的 Scala 实例化错误

Scala instantiation error with libGdx and Gradle

我只想在我的 libGdx 项目中为桌面编译 .scala 文件,但出现此错误

Executing task 'DesktopLauncher.main()'...

Executing tasks: [DesktopLauncher.main()]

Task :core:compileJava UP-TO-DATE Task :core:processResources NO-SOURCE Task :core:classes UP-TO-DATE Task :core:jar UP-TO-DATE

Task :desktop:compileJava FAILED warning: [options] bootstrap class path not set in conjunction with -source 1.6 C:\Users\cpepi001\Desktop\myGdxGame\desktop\src\com\mygdx\game\desktop\DesktopLauncher.java:11: error: cannot find symbol new HelloScala(); ^ symbol: class HelloScala location: class DesktopLauncher 1 error 1 warning

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':desktop:compileJava'. Compilation failed; see the compiler error output for details.

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

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

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/5.4.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 0s 3 actionable tasks: 1 executed, 2 up-to-date 2:10:42 PM: Task execution finished 'DesktopLauncher.main()'.

重现步骤:

  1. 创建一个新的 LibGDX 项目
  2. 右键单击桌面模块 -> 添加框架支持 -> Scala (v2.10.4)
  3. 在桌面模块中创建一个新的 .scala 文件
  4. 在 DesktopLauncher.java 中实例化 class(即 new HelloScala);

我也尝试 this guide 但没有成功

我设法找到了解决方案

  1. build.gradle(项目<project_name>

    • 将 Scala 插件添加到 project(":core") 部分:apply plugin: "scala"
    • 在依赖项中包含 scala 库:implementation "org.scala-lang:scala-library:$scalaLibraryVersion"
  2. build.gradle(模块:桌面)

    • 将 sourceSets.main.java.srcDirs = [ "src/" ] 更改为 sourceSets.main.scala.srcDirs = [ "src/" ]