在libgdx游戏中配置groovy时出错

时间:2016-07-09 20:09:59

标签: android gradle groovy libgdx

我刚刚开始学习ligdx。我试图将groovy配置为在游戏的核心项目中工作,但每次运行应用程序时我都会遇到以下异常:

  

错误:Gradle:任务执行失败:core:compileJava。

     

错误:无法编译Groovy文件:没有为模块'core'定义Groovy库

     

编译失败;有关详细信息,请参阅编译器错误输出。

     

错误:(16,23)Gradle:错误:找不到符号类testClass

     

错误:(16,23)Gradle:错误:找不到符号类testClass

我的代码是:

public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Texture img;

@Override
public void create () {
    batch = new SpriteBatch();
    img = new Texture("badlogic.jpg");
    testClass test = new testClass();
}

@Override
public void render () {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.draw(img, 0, 0);
    batch.end();
}

@Override
public void dispose () {
    batch.dispose();
    img.dispose();


}
}

class testClass {

def scddsds(){
    println('scddsds')
}
}

全局gradle文件是:

    buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'

    }
}

allprojects {
    apply plugin: "eclipse"
    apply plugin: "idea"

    version = '1.0'
    ext {
        appName = "groovy-test"
        gdxVersion = '1.9.3'
        roboVMVersion = '2.1.0'
        box2DLightsVersion = '1.4'
        ashleyVersion = '1.7.0'
        aiVersion = '1.8.0'
    }

    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://oss.sonatype.org/content/repositories/releases/" }
    }
}

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"
    }
}

project(":android") {
    apply plugin: "android"

    configurations { natives }

    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
    }
}

project(":core") {
    apply plugin: "java"


    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile 'org.codehaus.groovy:groovy:2.4.6:grooid'
    }
}

tasks.eclipse.doLast {
    delete ".project"
}

核心项目的gradle构建是:

   apply plugin: "java"

sourceCompatibility = 1.6
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

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


eclipse.project {
    name = appName + "-core"
}

我正在使用Android Studio。

1 个答案:

答案 0 :(得分:0)

Finally I managed to get it working. Firstly global build.gradle must be configured:

project(":core") {
    apply plugin: "java"
    apply plugin: "groovy"

    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.7'

    }
}

I added groovy to the core project only and it seems to work fine.

Then to the gradle build of the core project I added:

apply plugin: "java"
apply plugin: "groovy"
sourceCompatibility = 1.6
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets {
    main {
        java { srcDirs = [] } 
        groovy { srcDir "src" }
    }
}
eclipse.project {
    name = appName + "-core"
}
dependencies {
}

More info about that "magic" with source sets could be found in this answer.

BTW I created separate folder for groovy classes, so the project structure is like:

src   
 --main
    --java
    --groovy