我正在开发 libGDX 项目,我想使用 Lombok。我关注了this short tutorial关于在 libGDX 项目中使用 lombok,但不幸的是它不起作用。
情况看起来像这样 - IntelliJ 正在识别 lombok 并且不会提示 get/set 方法,但是当我想运行项目(使用 gradle)时,会出现 java 编译器错误,如
error: cannot find symbol static builder
error: cannot find symbol method setBoardSize(float a, float b)
所以基本上 Lombok 没有被识别。
通常建议是在 IntelliJ 中设置“启用注释处理”,但我尝试过但没有成功 - 我还尝试重新安装 Lombok 插件,将 Lombok 版本更改为 1.18.8,添加到 build.gradle android 部分
annotationProcessor "org.projectlombok:lombok:1.16.6"
但问题没有得到解决。我还相信这不是 IntelliJ 的问题,因为当我尝试使用
从 gradle 命令行运行项目时gradlew desktop:run
它不工作,我有相同的堆栈跟踪
有人遇到同样的问题吗?如何解决这个问题?
我将我的build.gradle附在下面
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
jcenter()
google()
}
dependencies {
classpath 'org.wisepersist:gwt-gradle-plugin:1.0.6'
classpath 'com.android.tools.build:gradle:3.1.0'
}
}
allprojects {
apply plugin: "eclipse"
apply plugin: "idea"
version = '1.0'
ext {
appName = "light-game"
gdxVersion = '1.9.8'
roboVMVersion = '2.3.3'
box2DLightsVersion = '1.4'
ashleyVersion = '1.7.0'
aiVersion = '1.8.0'
}
repositories {
mavenLocal()
mavenCentral()
google()
jcenter()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
project(":desktop") {
apply plugin: "java"
apply plugin: "war"
dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
providedCompile "org.projectlombok:lombok:1.16.6"
}
}
project(":android") {
apply plugin: "android"
configurations { natives }
dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-arm64-v8a"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86_64"
compile "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion"
provided "org.projectlombok:lombok:1.16.6"
}
}
project(":core") {
apply plugin: "java"
apply plugin: "war"
dependencies {
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
compile "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion"
providedCompile "org.projectlombok:lombok:1.16.6"
}
}
project(":html") {
apply plugin: "gwt"
apply plugin: "war"
dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion"
compile "com.badlogicgames.gdx:gdx:$gdxVersion:sources"
compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion:sources"
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion:sources"
compile "com.badlogicgames.gdx:gdx-box2d-gwt:$gdxVersion:sources"
compile "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion:sources"
}
}
tasks.eclipse.doLast {
delete ".project"
}
最佳答案
呃 - 所以最后我发现了这个问题,这非常令人惊讶。事实证明,我的配置没问题,IntelliJ 没问题,但 Lombok 本身有一个我不知道的问题 - 这个问题是 静态导入 builder() 方法,它完全搞乱了编译类的字节码并导致像我这样的问题
基本上我所要做的就是删除这样的导入
import static com.antkowicz.project.MyClass.builder;
public MyClass getMyClass() {
return builder().build();
}
而不是使用这个
import static com.antkowicz.project.MyClass;
public MyClass getMyClass() {
return MyClass.builder().build();
}
似乎这个问题是众所周知的,但不会很快(或永远)得到解决,而且这并不是什么小事 - 所以基本上我们唯一能做的就是省略 Lombok 的静态导入(至少 builder()):)
下面有一些关于此主题的链接:
关于java - LibGDX 项目未识别 Lombok,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56251597/