You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复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并清理缓存

    1. 在Visual Studio中执行Gradle: Refresh Gradle Project命令,强制IDE重新识别所有模块
    2. 删除项目根目录下的.gradle、.vs(Visual Studio缓存目录)文件夹,以及core/desktop模块下的build目录
    3. 打开终端执行gradle clean build,重新构建项目
  • 修正版本兼容问题
    将core和desktop模块build.gradle中的sourceCompatibility = 1.8改为sourceCompatibility = 17,与使用的JDK 17版本保持一致,避免编译版本不匹配导致的IDE识别异常

  • 检查模块与目录结构

    1. 确认settings.gradle中include 'desktop', 'core'的模块都存在于项目根目录下,core文件夹与desktop同级
    2. 检查core模块的src目录是否存在且包含Java源文件,确保sourceSets.main.java.srcDirs = [ "src/" ]指向正确路径
  • 修复IDE语言服务

    1. 在Visual Studio的项目设置中,确认core模块被标记为Java源模块,未被排除在语言服务扫描范围外
    2. 重启Visual Studio,清除IDE的缓存,确保语言服务重新加载项目配置
  • 检查Git提交完整性

    1. 查看.gitignore文件,确认没有误忽略core模块的src目录或build.gradle文件
    2. 同步GitHub仓库时,确保core模块的所有必要文件都已提交到远程仓库,本地拉取完整的项目文件

内容的提问来源于stack exchange,提问作者Jack Lander

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 17:37:36