【问题标题】:Errors while configuring groovy in libgdx game在 libgdx 游戏中配置 groovy 时出错
【发布时间】:2016-07-09 20:09:59
【问题描述】:

我前段时间开始学习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。

【问题讨论】:

  • 分享你的build.gradle
  • 我贴出来了。请检查一下

标签: android gradle groovy libgdx


【解决方案1】:

最后我设法让它工作了。 首先必须配置全局build.gradle

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'

    }
}

我只在核心项目中添加了 groovy,它似乎工作正常。

然后到我添加的核心项目的gradle构建:

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

有关源集的“魔法”的更多信息可以在this 答案中找到。

顺便说一句,我为 groovy 类创建了单独的文件夹,因此项目结构如下:

src   
 --main
    --java
    --groovy

【讨论】:

  • 当我在我的项目中添加一些代码并编译它时,我得到了这个异常:错误:Android Gradle 构建目标:java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-92
  • 这是否与当前问题有关? '因为也许值得再问一个)
猜你喜欢
  • 2015-05-08
  • 1970-01-01
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多