Gradle构建错误:无法访问ITest坏类文件:在版本50.0 classfile中找到ITest.class默认方法

sil*_*der 16 java junit android android-testing android-gradle-plugin

我有奇怪的问题,不知道如何解决它.

我有一个默认方法的接口,如下所示:

public interface ITest{
    default String getText(){
       return "ITest";
    }
}
Run Code Online (Sandbox Code Playgroud)

和实现此接口的类,如下所示:

public class TestClasssss implements ITest{
    private String text;
}
Run Code Online (Sandbox Code Playgroud)

我试图在我的app单元测试项目中使用这个类.

所以,如果我在我的android的单元测试项目中复制这个类,它编译好了,并且所有工作都按预期工作,但是如果这个类和接口在app源文件夹中声明,则应用程序不会编译并崩溃

Error:(30, 10) error: cannot access ITest bad class file: ~\ITest.class
default method found in version 50.0 classfile
Please remove or make sure it appears in the correct subdirectory of the classpath.
Run Code Online (Sandbox Code Playgroud)

那么,我怎么能解决这个奇怪的行为呢?


我的graddle配置看起来像这样:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'me.tatarka:gradle-retrolambda:3.2.5'
    }
}

repositories {
    mavenCentral()
    mavenLocal()
    maven {
        url = "https://oss.sonatype.org/content/repositories/snapshots/"
    }
    jcenter()
}

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'realm-android'
apply plugin: 'com.neenbedankt.android-apt'

apt {
    arguments {
        androidManifestFile variant.outputs[0]?.processResources?.manifestFile
    }
}

android {
    compileSdkVersion 24
    buildToolsVersion '24.0.1'

    defaultConfig {
        applicationId "io.projectname"
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        debug {
            debuggable true
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt')
        }
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    dataBinding {
        enabled = true
    }

    sourceSets {
        main {
            res.srcDirs =
                    [
                            'src/main/res/controls',
                            'src/main/res/fragments',
                            'src/main/res/activities',
                            'src/main/res/views',
                            'src/main/res'
                    ]
        }
    }
}

ext {
    JUNIT_VERSION = '4.12'
    DAGGER_VERSION = '2.2'
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')

    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:2.0.111-beta'
    testCompile "org.robolectric:robolectric:3.1.1"

    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:design:24.2.0'
    compile 'com.roughike:bottom-bar:1.3.4'
    compile 'com.aurelhubert:ahbottomnavigation:1.2.3'
    compile 'joda-time:joda-time:2.9.4'
    compile 'com.annimon:stream:1.0.9'
    compile 'com.kyleduo.switchbutton:library:1.4.1'
    compile 'io.reactivex:rxandroid:1.2.0'
    compile 'io.reactivex:rxjava:1.1.5'
    compile 'com.jakewharton.rxbinding:rxbinding:0.4.0'
    compile('eu.davidea:flexible-adapter:5.0.0-SNAPSHOT') {
        changing = true
    }
    compile 'com.github.aakira:expandable-layout:1.5.1@aar'
    compile "cn.aigestudio.wheelpicker:WheelPicker:1.1.2"
    compile 'net.sf.biweekly:biweekly:0.5.0'

    //Dagger dependencies started
    compile "com.google.dagger:dagger:$DAGGER_VERSION"
    apt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
    provided 'javax.annotation:jsr250-api:1.0'
    compile 'javax.inject:javax.inject:1'
    testCompile "junit:junit:$JUNIT_VERSION"
    testApt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
    //Dagger dependencies finished

    provided 'org.projectlombok:lombok:1.16.10'
}
Run Code Online (Sandbox Code Playgroud)

big*_*C28 -1

您确定您的 gradle 构建使用的是 jdk1.8+ 吗?看起来构建正在尝试利用使用 1.8 构建但在构建期间使用较低版本进行测试的类。

尝试:

gradle clean build
Run Code Online (Sandbox Code Playgroud)

或者

gradlew clean build
Run Code Online (Sandbox Code Playgroud)