来自LibGdx的书糟糕的代码?

时间:2015-10-04 22:29:28

标签: java eclipse gradle libgdx

我正在使用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)
  }
}

2 个答案:

答案 0 :(得分:1)

它应该在依赖分支下。但是这个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版本。

答案 1 :(得分: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!