如何修复Visual Studio+LibGDX项目中‘core不存在导致无法构建’错误
问题描述
我使用Visual Studio、JDK 17和LibGDX进行桌面开发,代码可正常运行,但无法使用自动补全功能,也看不到错误下划线提示。通过GitHub跨设备协作开发,最初构建项目时无此问题,经过几次提交后,两台设备出现该问题,另一台设备正常。目前还遇到「因core不存在导致项目无法构建」的错误,求修复方案。
项目配置文件信息
core模块 build.gradle
sourceCompatibility = 1.8 [compileJava, compileTestJava]*.options*.encoding = 'UTF-8' sourceSets.main.java.srcDirs = [ "src/" ] eclipse.project.name = appName + "-core"
desktop模块 build.gradle
sourceCompatibility = 1.8 sourceSets.main.java.srcDirs = [ "src/" ] sourceSets.main.resources.srcDirs = ["../assets"] project.ext.mainClassName = "com.mygdx.game.DesktopLauncher" project.ext.assetsDir = new File("../assets") import org.gradle.internal.os.OperatingSystem tasks.register('run', JavaExec) { dependsOn classes mainClass = project.mainClassName classpath = sourceSets.main.runtimeClasspath standardInput = System.in workingDir = project.assetsDir ignoreExitValue = true if (OperatingSystem.current() == OperatingSystem.MAC_OS) { // Required to run on macOS jvmArgs += "-XstartOnFirstThread" } } tasks.register('debug', JavaExec) { dependsOn classes mainClass = project.mainClassName classpath = sourceSets.main.runtimeClasspath standardInput = System.in workingDir = project.assetsDir ignoreExitValue = true debug = true } tasks.register('dist', Jar) { duplicatesStrategy(DuplicatesStrategy.EXCLUDE) manifest { attributes 'Main-Class': project.mainClassName } dependsOn configurations.runtimeClasspath from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } with jar } dist.dependsOn classes eclipse.project.name = appName + "-desktop"
根目录 build.gradle
buildscript { repositories { mavenLocal() mavenCentral() gradlePluginPortal() maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } google() } dependencies { } } allprojects { apply plugin: "eclipse" version = '1.0' ext { appName = "Mario" gdxVersion = '1.12.1' roboVMVersion = '2.3.20' box2DLightsVersion = '1.5' ashleyVersion = '1.7.4' aiVersion = '1.8.2' gdxControllersVersion = '2.2.1' } repositories { mavenLocal() mavenCentral() google() gradlePluginPortal() maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } maven { url "https://oss.sonatype.org/content/repositories/releases/" } maven { url "https://jitpack.io" } } } project(":desktop") { apply plugin: "java-library" dependencies { implementation project(":core") api "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion" api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop" } } project(":core") { apply plugin: "java-library" dependencies { api "com.badlogicgames.gdx:gdx:$gdxVersion" } }
settings.gradle
include 'desktop', 'core'
修复方案
同步Gradle并清理缓存
- 在Visual Studio中执行
Gradle: Refresh Gradle Project命令,强制IDE重新识别所有模块 - 删除项目根目录下的
.gradle、.vs(Visual Studio缓存目录)文件夹,以及core/desktop模块下的build目录 - 打开终端执行
gradle clean build,重新构建项目
- 在Visual Studio中执行
修正版本兼容问题
将core和desktop模块build.gradle中的sourceCompatibility = 1.8改为sourceCompatibility = 17,与使用的JDK 17版本保持一致,避免编译版本不匹配导致的IDE识别异常检查模块与目录结构
- 确认settings.gradle中
include 'desktop', 'core'的模块都存在于项目根目录下,core文件夹与desktop同级 - 检查core模块的
src目录是否存在且包含Java源文件,确保sourceSets.main.java.srcDirs = [ "src/" ]指向正确路径
- 确认settings.gradle中
修复IDE语言服务
- 在Visual Studio的项目设置中,确认core模块被标记为Java源模块,未被排除在语言服务扫描范围外
- 重启Visual Studio,清除IDE的缓存,确保语言服务重新加载项目配置
检查Git提交完整性
- 查看
.gitignore文件,确认没有误忽略core模块的src目录或build.gradle文件 - 同步GitHub仓库时,确保core模块的所有必要文件都已提交到远程仓库,本地拉取完整的项目文件
- 查看
内容的提问来源于stack exchange,提问作者Jack Lander
相关产品推荐
相关产品推荐

