数据绑定因NoSuchMethodError而失败

时间:2016-04-20 16:23:35

标签: android android-gradle android-databinding

每当我尝试assemble调试应用程序的构建时,更新到gradle 2.10后,我得到NoSuchMethodError异常。以下是构建日志的相关部分:

java.lang.RuntimeException: failure, see logs for details.
  cannot generate view binders java.lang.NoSuchMethodError: com.google.common.base.Strings.isNullOrEmpty(Ljava/lang/String;)Z
    at android.databinding.tool.util.StringUtils.capitalize(StringUtils.java:57)
    at android.databinding.tool.util.ParserHelper.toClassName(ParserHelper.java:23)
    at android.databinding.tool.store.ResourceBundle$LayoutFileBundle.getFullBindingClass(ResourceBundle.java:551)
    at android.databinding.tool.store.ResourceBundle$LayoutFileBundle.getBindingClassPackage(ResourceBundle.java:541)
    at android.databinding.tool.CompilerChef.pushClassesToAnalyzer(CompilerChef.java:124)
    at android.databinding.tool.CompilerChef.createChef(CompilerChef.java:73)
    at android.databinding.annotationprocessor.ProcessExpressions.writeResourceBundle(ProcessExpressions.java:148)
    at android.databinding.annotationprocessor.ProcessExpressions.onHandleStep(ProcessExpressions.java:82)
    at android.databinding.annotationprocessor.ProcessDataBinding$ProcessingStep.runStep(ProcessDataBinding.java:154)
    at android.databinding.annotationprocessor.ProcessDataBinding$ProcessingStep.access$000(ProcessDataBinding.java:139)
    at android.databinding.annotationprocessor.ProcessDataBinding.process(ProcessDataBinding.java:66)

如您所见,无法找到方法com.google.common.base.Strings.isNullOrEmpty

某些细节

我使用了Retrolambda 3.2.5和Java 8.没有其他额外的插件。

构建插件版本:com.android.tools.build:gradle:2.0.0

构建工具版本:23.0.3

操作系统:OS X

build.gradle看起来像这样。我稍微改了一下,不露出一些私密的东西,但问题仍然存在。

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'
        classpath 'me.tatarka:gradle-retrolambda:3.2.3'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'

project.version = '1.0.0'

afterEvaluate {
    tasks.matching {
        it.name.startsWith('dex')
    }.each { dx ->
        if (dx.additionalParameters == null) {
            dx.additionalParameters = []
        }
        dx.additionalParameters += "--set-max-idx-number=50000" //    default 60000
    }
}

def googleApiKey = "key goes here"
def appVersionCode = 1
def appVersionName = project.version + "." + appVersionCode

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        manifestPlaceholders = [googleApiKey  : googleApiKey,
                                appVersionCode: appVersionCode,
                                appVersionName: appVersionName]
        multiDexEnabled true

        ndk {
            abiFilters "armeabi", "armeabi-v7a"
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.cfg'

            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    output.outputFile = new File(
                            output.outputFile.parent,
                            "App-${project.version}-${appVersionCode}.apk"
                    )
                }
            }
        }

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }

    flavorDimensions "multidex", "leakcanary"

    productFlavors {
        withLeakCanary {
            dimension "leakcanary"
        }

        withoutLeakCanary {
            dimension "leakcanary"
        }

        develDex {
            dimension "multidex"
            minSdkVersion 21
            targetSdkVersion 23
        }

        prodDex {
            dimension "multidex"
            minSdkVersion 15
            targetSdkVersion 23
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/DEPENDENCIES'
    }
    lintOptions {
        abortOnError false
    }
    sourceSets {
        main {
            jniLibs.srcDir 'build/jniLibs'
        }
    }

    dexOptions {
        javaMaxHeapSize "4g"
    }

    dataBinding {
        enabled = true
    }
}

task copyNativeLibs(type: Copy) {
    from(new File(buildDir, 'intermediates/exploded-aar/')) {
        include '**/*.so'
        exclude '**/lib-detector.so'
    }
    into new File(buildDir, 'jniLibs')
    eachFile { details ->
        def pathSplit = details.path.split('/')
        details.path = pathSplit[pathSplit.length - 2] + '/' +    pathSplit[pathSplit.length - 1]
    }

    includeEmptyDirs = false
}

tasks.withType(JavaCompile) { javaCompileTask ->    javaCompileTask.dependsOn copyNativeLibs }

clean.dependsOn 'cleanCopyNativeLibs'

dependencies {
    testCompile 'junit:junit:4.11'
    testCompile 'org.robolectric:robolectric:3.0'
    testCompile 'org.robolectric:shadows-multidex:3.0'
    testCompile('org.robolectric:shadows-httpclient:3.0') {
        exclude module: 'httpcore'
        exclude module: 'commons-codec'
    }
    testCompile 'org.powermock:powermock-module-junit4:1.5.2'
    testCompile 'org.powermock:powermock-api-mockito:1.5.2'
    testCompile 'org.roboguice:roboguice:3.0.1'

    compile 'com.parse.bolts:bolts-android:1.2.1'
    compile fileTree(dir: 'libs', include: 'Parse-*.jar')

    compile 'com.google.android.gms:play-services-drive:7.8.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:support-annotations:23.0.1'
    compile 'com.android.support:support-v4:23.0.1'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:recyclerview-v7:23.0.1'
    compile 'com.android.support:design:23.0.1'
    compile 'org.roboguice:roboguice:3.0.1'
    provided 'org.roboguice:roboblender:3.0.1'
    compile('com.google.inject.extensions:guice-assistedinject:3.0') {
        exclude group: 'com.google.inject', module: 'guice'
    }
    compile 'commons-io:commons-io:2.4'
    compile 'commons-lang:commons-lang:2.6'
    compile 'com.intellij:annotations:12.0'

    compile 'com.google.zxing:core:3.2.1'
    compile 'com.google.zxing:android-core:3.2.1'
    compile 'com.google.android.gms:play-services-base:7.8.0'
    compile 'com.google.android.gms:play-services-location:7.8.0'
    compile 'com.google.android.gms:play-services-maps:7.8.0'
    compile 'com.google.android.gms:play-services-analytics:7.8.0'
    compile 'com.amazon:in-app-purchasing:2.0.1'

    compile 'com.googlecode.libphonenumber:libphonenumber:7.0.7'

    withLeakCanaryCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'

    compile 'com.google.android.gms:play-services-ads:7.8.0'

    compile 'io.reactivex:rxandroid:1.0.1'
    compile 'io.reactivex:rxjava:1.0.14'
}

问题

其他人有同样的问题吗?怎么解决?如果您需要一些额外的信息,请在评论中告诉我。

2 个答案:

答案 0 :(得分:1)

您是否尝试过类似问题#134 clean-build的jitpack上的新补丁,gradle导入订单似乎有问题,您可以尝试使用:

repositories {
    maven { url "https://jitpack.io" }
}
dependencies {
    classpath 'com.github.denis-itskovich:gradle-retrolambda:3.2.3-fix-134'
}

答案 1 :(得分:0)

升级Android Studio后,看起来插件出现了错误。

如果您进入:<Android Studio Dir>/plugins/android/lib/builder-model-x.x.x.jar,您可能会找到2个.jars。尝试删除旧版本.jar并保留新版本并清理并重建项目。

如果上述方法不起作用,请尝试以下方法:

将后端的build.gradle文件中的Objectify库版本更改为4.0b或更高版本(如果存在)。

这可能听起来无关紧要,但是客体化4.0b库具有相同的包名,这些类包含在com.google.common.base.Strings.isNullOrEmpty等appengine sdk中。

当你部署app后端时,appengine类会被objectify类覆盖,因此当你尝试调用某个方法时会抛出错误。

这是在客观化5.0。+

中解决的

希望它有所帮助,因为它帮助我解决了这个问题。