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

问题描述 投票:16回答:6

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

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

public interface ITest{
    default String getText(){
       return "ITest";
    }
}

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

public class TestClasssss implements ITest{
    private String text;
}

我试图在我的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.

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


我的gradle配置如下所示:

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'
}
java android junit android-gradle android-testing
6个回答
1
投票

确保正确设置source and target兼容性

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

1
投票

我正在处理同样的问题,它似乎是在android上接受的编译的java源代码与用retrolambda编译的代码产生的输出之间的某种冲突。当您使用default表示法进行功能接口方法时,问题就会出现。在retrolambda插件中有一些有限的支持,引用了repo的自述文件(orfjackal/retrolambda):

retrolambda.defaultMethods是否在接口上反向移植默认方法和静态方法。限制:所有后向移植的接口和实现它们或调用其静态方法的所有类必须一起向后移植,一次执行Retrolambda。默认情况下禁用。通过设置为“true”启用

所以你可以尝试在你的android模块的build.gradle文件中使用它:

...
android {
...
}
retrolambda {
    defaultMethods = true
}

在我的情况下,这不起作用,但我的情况与你的情况完全不同。我在一个库项目中使用了retrolambda代码,然后尝试在另一个应用程序项目中使用它。

此外,我无法让Jack&Jill编译器工作(启用了Jack的某种java8支持,请参阅android参考中的Enable Java 8 Features and the Jack Toolchain),一个神秘的“NullPointer”在./gradlew assembleDebug引发,所以我建议现在避免使用jackOptions

祝好运!


0
投票

我认为您可能需要启用Jack编译器。您需要以下内容:

  • Android Studio> 2.1
  • 默认方法仅在您定位API级别24时可用

你的gradle需要看起来像这样:

android {
  ...
  defaultConfig {
    ...
    jackOptions {
      enabled true
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

这是Java 8 Language Feature Page in the Android Documentation


0
投票

使用dex2jar后我遇到了这个问题。根据https://gitlab.ow2.org/asm/asm/blob/master/asm/src/main/java/org/objectweb/asm/Opcodes.java#L264,JVM字节码的第50版表示Java 1.6,而我的dex文件设计为在第52版(1.8)上运行。我使用了另一个dex2jar实用程序(它可以在任何jar上工作,而不仅仅是dex)将版本更改为1.8。来源是https://github.com/pxb1988/dex2jar/blob/2.x/dex-tools/src/main/java/com/googlecode/dex2jar/tools/ClassVersionSwitch.java,命令是d2j-class-version-switch.sh 8 <source jar> <destination jar>


-1
投票

你确定你的gradle构建使用的是jdk1.8 +吗?似乎构建试图利用使用1.8构建的类,但在构建期间使用较低版本进行测试。

尝试:

gradle clean build

要么

gradlew clean build

-1
投票

如果您使用的是Android Studio,请尝试清理项目,然后编译项目。如果它也没有用,那么单击文件 - >“使高速缓存/重新启动无效”。它必须有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.