在Groovy中,只需将变量放在里面就可以测试null和empty的集合,如下所示:
def collection = [ 'test' ]
if(!collection) {
//Collection is either null or empty, handle exceptional business here
}
但是,在将@CompileStatic
放在包含此类代码的类上时,它会停止工作(但仅限于Android)并出现错误:
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: []
运行桌面版时似乎没有这种情况。
提供更多背景信息。这是一个生成的LibGDX项目,包含三个项目(-core,-desktop,-android),其中-core项目已转换为groovy项目。 -core项目被引用,-desktop和-android项目的依赖项
无论是否使用@CompileStatic
注释注释类,并且正确识别Groovy Truth,桌面版本都可以正常运行。
另一方面,在Android上,出现上述错误。
我没有使用grooid库,因为转换为groovy的项目在桌面和android之间共享。
如果有任何价值,以下是项目级build.gradle
的内容:
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"
}
答案 0 :(得分:4)
您需要为所有模块使用Groovy的“grooid”版本,否则您将生成使用目标JVM正常运行时的代码。对我的所有模块使用'2.4.1-grooid'应该是好的。