【问题标题】:Bad gradle code from LibGdx book?LibGdx 书中的错误 gradle 代码?
【发布时间】:2015-10-04 22:29:27
【问题描述】:

我正在使用 Learning LibGDX 游戏开发,并且正在尝试遵循 Gradle 方式。在第 144 页底部的第 4 章中,它说添加:

compile "com.badlogic.gdx:gdx-tools:$gdxVersion"

下面的代码部分:

project(":desktop")

我从本书网站继承的 build.gradle 发布在下面。我在“-desktop”部分添加了代码,但我从 Eclipse 中得到了一个错误。

这是我的错误:

Could not find method compile() for arguments [com.badlogic.gdx:gdx-
tools:1.6.4] on org.gradle.plugins.ide.eclipse.model.EclipseProject_Decorated@54d80175.

我是 Gradle 的新手,如果我想通过 Gradle 添加 gdx-tools.jar,有人知道我应该怎么做吗?

桌面项目的Build.gradle:

apply plugin: "java"

sourceCompatibility = 1.6
sourceSets.main.java.srcDirs = [ "src/" ]

project.ext.mainClassName = "com.packetpub.libgdx.canyonbunny.desktop.DesktopLauncher"
project.ext.assetsDir = new File("../android/assets");

task run(dependsOn: classes, type: JavaExec) {
    main = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
}

task dist(type: Jar) {
    from files(sourceSets.main.output.classesDir)
    from files(sourceSets.main.output.resourcesDir)
    from {configurations.compile.collect {zipTree(it)}}
    from files(project.assetsDir);

    manifest {
        attributes 'Main-Class': project.mainClassName
    }
}

dist.dependsOn classes

eclipse {
    project {
        name = appName + "-desktop"
        linkedResource name: 'assets', type: '2', location: 'PARENT-1-PROJECT_LOC/android/assets'
        compile "com.badlogic.gdx:gdx-tools:$gdxVersion"
    }
}

task afterEclipseImport(description: "Post processing after project generation", group: "IDE") {
  doLast {
    def classpath = new XmlParser().parse(file(".classpath"))
    new Node(classpath, "classpathentry", [ kind: 'src', path: 'assets' ]);
    def writer = new FileWriter(file(".classpath"))
    def printer = new XmlNodePrinter(new PrintWriter(writer))
    printer.setPreserveWhitespace(true)
    printer.print(classpath)
  }
}

【问题讨论】:

  • 应该是“com.badlogicgames.gdx:gdx-tools:$gdxVersion”,见:github.com/libgdx/libgdx/wiki/…
  • 我不再收到错误消息,但是当我转到 Gradle->Refresh Dependencies 时,gdx-tools jar 不会出现在我的 Gradle Dependencies 文件夹中。我还是做错了吗?
  • 每个子项目(桌面、android、ios 等)都有一个根 build.gradle 和不同的 gradle 文件,对吧?

标签: java eclipse gradle libgdx


【解决方案1】:

您使用的是旧域名,这是错误的:

编译“com.badlogic.gdx:gdx-tools:$gdxVersion”

请把它换成这个新的:

编译“com.badlogicgames.gdx:gdx-tools:$gdxVersion”

确保放置在正确的位置:

Tools Gradle

Core Dependency: Dont put me in core!
Desktop Dependency:  compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
Android Dependency: Not compatible!
iOS Dependency: Not compatible!
HTML Dependency: Not compatible!

【讨论】:

    【解决方案2】:

    它应该在 dependencies 分支下。但是这个 gradle 文件中也没有定义 gdxVersion

    我假设您可以通过将此代码添加到您共享的 gradle 文件的末尾来使用解决方法来定义变量并在依赖项下使用。

    dependencies {
        def gdxVersion = '1.6.4'
        compile "com.badlogic.gdx:gdx-tools:$gdxVersion"
        //you can add another libraries as well
    }
    

    但我建议您在项目的 gradle 文件中管理 libGDX 依赖项,以保持模块 libGDX 版本的一致性。默认情况下,libGDX 安装程序会在 项目的根目录 中创建 build.gradle 文件。 gdxVersion 变量和 libGDX 依赖项在此文件中定义和管理。比如:

    //defines common variables and settings that all projects (modules like desktop, ios, android) use
    allprojects {
        apply plugin: "eclipse"
        apply plugin: "idea"
    
        version = '1.0'
        ext {
            appName = 'YOUR_PROJECT_NAME'
            gdxVersion = '1.7.0'
            roboVMVersion = '1.8.0'
            gwtVersion = '2.6.0'
        }
    
        repositories {
            mavenCentral()
            maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
            maven { url "https://oss.sonatype.org/content/repositories/releases/" }
        }
    }
    
    //and here desktop module specific settings
    project(":desktop") {
        apply plugin: "java"
    
        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-tools:$gdxVersion"
        }
    }
    

    通过这种用法,您可以看到所有模块的 libGDX 依赖项都可以在桌面模块的依赖项中以相同的方式处理。您可以通过对上面的 gdxVersion 进行一次更改来更新您的 libGDX 版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多