2015-10-04 22 views
0

我正在使用學習LibGDX遊戲開發,我試圖遵循Gradle的方式。在144頁的底部第4章,它說添加:來自LibGdx書籍的不良gradle代碼?

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 [email protected]d80175. 

我嶄新的品牌,以搖籃,沒有任何人知道,如果我想通過搖籃添加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) 
    } 
} 
+1

它應該是「com.badlogicgames.gdx:gdx-tools:$ gdxVersion」,請參閱:https://github.com/libgdx/libgdx/wiki/Dependency-management-with-Gradle#tools-gradle – Xoppa

+0

我再也沒有遇到這個錯誤,但是當我進入Gradle-> Refresh Dependencies時,gdx-tools jar不會出現在我的Gradle Dependencies文件夾中。我仍然做錯了嗎? – smuggledPancakes

+0

你有一個root build.gradle和不同的gradle文件,用於每個子項目(桌面,android,ios等),對不對? – yigiter

回答

1

您使用的是舊域,這是錯誤的:

編譯 「COM badlogic .gdx:GDX-工具:$ gdxVersion」

請更換爲這個新:

編譯 「COM badlogicgames .gdx:GDX-工具:$ 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! 
1

它應該是下依賴分支。但是在這個gradle文件中也沒有定義gdxVersion的

我假設您可以使用變通方法來定義變量,並在依賴關係下使用,方法是將此代碼添加到您共享的gradle文件的末尾。

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

但我建議你在模塊的libGDX版本的一致性,項目的gradle這個文件管理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依賴可以通過在桌面模塊的depencendies以同樣的方式來處理。你可以更新你的libGDX版本,只需要修改上面的gdxVersion。