Groovy CompileStatic在Android上扰乱了Groovy的真相

在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批注对类进行批注,都可以@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"
}

您需要为所有模块使用Groovy的“grooid”版本,否则将生成使用运行时目标在正常JVM上的代码。 对于所有模块使用'2.4.1-grooid'应该不错,我认为。

链接地址: http://www.djcxy.com/p/83761.html

上一篇: Groovy CompileStatic on Android messes up Groovy Truth

下一篇: Getting Android SDK Tools to work on Raspberry PI