7

In Groovy it is possible to test collections for null and empty simply by placing the variable by itself inside if like:

def collection = [ 'test' ]
if(!collection) {
  //Collection is either null or empty, handle exceptional business here
}

However upon placing @CompileStatic on the class which contains code like this, it stops working (but only on Android) with an error:

02-16 20:49:03.837: E/AndroidRuntime(9013): org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack: No signature of method: java.util.ArrayList.asBoolean() is applicable for argument types: () values: []

This does not seem to occur when running the desktop version.

To give more context. This is a generated LibGDX project with three projects (-core, -desktop, -android) where the -core project has been converted to a groovy project. The -core project is referenced, and a dependency of both -desktop and -android projects

Desktop version works without any issues regardless whether the classes are annotated with @CompileStaticannotation, and Groovy Truth is properly recognized.

On android on the other hand, the aforementioned error occurs.

I am not using the grooid library because the project which is converted to groovy is shared between both desktop and android.

If it's of any worth, here are the contents of build.gradle on the project level:

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
        classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.5'        
    }
}

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

    version = '1.0'
    ext {
        appName = 'CastleShuffle'
        gdxVersion = '1.5.4'
        roboVMVersion = '1.0.0-beta-04'
        box2DLightsVersion = '1.3'
        ashleyVersion = '1.3.1'
        aiVersion = '1.5.0'
    }

    repositories {
        mavenCentral()
        jcenter()
        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"
        compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
        compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
    }
}

project(":android") {
    apply plugin: "android" 
    //apply plugin: "groovyx.grooid.groovy-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-x86"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
        compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
    //    compile 'org.codehaus.groovy:groovy:2.4.0:grooid' //Adding this causes a Dex exception where groovy class Bindable is referenced multiple times
    //    compile 'org.codehaus.groovy:groovy-all:2.4.0'
    }
}

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

    dependencies {
        compile 'org.codehaus.groovy:groovy-all:2.4.0'
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
    }
}

tasks.eclipse.doLast {
    delete ".project"
}
durron597
  • 31,968
  • 17
  • 99
  • 158
MrPlow
  • 1,295
  • 3
  • 26
  • 45

1 Answers1

4

You need to use the "grooid" version of Groovy for all your modules, otherwise you will have code generated that uses a runtime targetted at normal JVMs. Using '2.4.1-grooid' for all your modules should be good I think.

melix
  • 1,510
  • 7
  • 6
  • There seem to be issues with grooid repository paths, `compile 'org.codehaus.groovy:groovy:2.4.1:grooid'` in gradle produces a different path than the real dependency path so I can't really test it. When i figure out what's wrong and how to fix it I'll try your solution and distribute the bounty appropriately. Thanks for your help. – MrPlow Feb 23 '15 at 17:19
  • You are including the groovy-all dependency from core into your android project. This needs to be excluded since groovy-all and grooid jars can't be compiled together. – Pieces Aug 19 '16 at 12:51