Android Studio:测试:已使用 java 8 或更高版本编译的库依赖项

Android Studio: Testing: Library dependencies that have been compiled using java 8 or above

请帮忙。我在为 android studio 设置测试时遇到了非常糟糕的时间。

我已经从黄瓜github下载了计算器示例来练习黄瓜代码测试。 https://github.com/cucumber/cucumber-jvm/tree/master/android(其中一些品牌的名字非常奇怪)

我尝试将它与 Android studio 一起使用。该程序 运行 非常完美(耶!)。然而,测试没有。我有一个非常可怕的消息,每次我 运行 它都会困扰我。

*To run dex in process, the Gradle daemon needs a larger heap.
It currently has approximately 910 MB.
For faster builds, increase the maximum heap size for the Gradle daemon to more than 2048 MB.
To do this set org.gradle.jvmargs=-Xmx2048M in the project gradle.properties.
For more information see https://docs.gradle.org/current/userguide/build_environment.html
Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add 
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
Error:1 error; aborting
:app:transformClassesWithDexForDebugAndroidTest FAILED
Error:Execution failed for task ':app:transformClassesWithDexForDebugAndroidTest'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_74\bin\java.exe'' finished with non-zero exit value 1*

它的目标兼容性和源兼容性我遇到了麻烦(还没有得到休息)

这是 gradle 版本:如您所见,我已将兼容性更改为 1.7

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "boo.thefoodhunt"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        debug {
            assets.srcDirs = ['src/debug/assets', 'src/androidTest/assets/']
            res.srcDirs = ['src/debug/res', 'src/androidTest/assets/features']
        }
        main { res.srcDirs = ['src/main/res', 'src/androidTest/assets'] }
    }
    dexOptions {
        incremental true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'

    //TESTING
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    androidTestCompile 'com.android.support:support-annotations:23.3.0'

    //Espresso
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test:rules:0.5'

    //Cucumber
    androidTestCompile 'info.cukes:cucumber-android:1.2.4'
    androidTestCompile 'info.cukes:cucumber-picocontainer:1.2.4'
}

未通过 运行 的测试:

 package boo.thefoodhunt;

import android.test.ActivityInstrumentationTestCase2;

import cucumber.api.CucumberOptions;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

@CucumberOptions(features = "features")
public class CalculatorActivitySteps extends ActivityInstrumentationTestCase2<CalculatorActivity> {

    public CalculatorActivitySteps(SomeDependency dependency) {
        super(CalculatorActivity.class);
        assertNotNull(dependency);
    }

    @Given("^I have a CalculatorActivity$")
    public void I_have_a_CalculatorActivity() {
        assertNotNull(getActivity());
    }

    @When("^I press (\d)$")
    public void I_press_d(final int d) {
        switch (d) {
            case 0:
                onView(withId(R.id.btn_d_0)).perform(click());
                break;
            case 1:
                onView(withId(R.id.btn_d_1)).perform(click());
                break;
            case 2:
                onView(withId(R.id.btn_d_2)).perform(click());
                break;
            case 3:
                onView(withId(R.id.btn_d_3)).perform(click());
                break;
            case 4:
                onView(withId(R.id.btn_d_4)).perform(click());
                break;
            case 5:
                onView(withId(R.id.btn_d_5)).perform(click());
                break;
            case 6:
                onView(withId(R.id.btn_d_6)).perform(click());
                break;
            case 7:
                onView(withId(R.id.btn_d_7)).perform(click());
                break;
            case 8:
                onView(withId(R.id.btn_d_8)).perform(click());
                break;
            case 9:
                onView(withId(R.id.btn_d_9)).perform(click());
                break;
        }
    }

    @When("^I press ([+–x\/=])$")
    public void I_press_op(final char op) {
        switch (op) {
            case '+':
                onView(withId(R.id.btn_op_add)).perform(click());
                break;
            case '–':
                onView(withId(R.id.btn_op_subtract)).perform(click());
                break;
            case 'x':
                onView(withId(R.id.btn_op_multiply)).perform(click());
                break;
            case '/':
                onView(withId(R.id.btn_op_divide)).perform(click());
                break;
            case '=':
                onView(withId(R.id.btn_op_equals)).perform(click());
                break;
        }
    }

    @Then("^I should see (\S+) on the display$")
    public void I_should_see_s_on_the_display(final String s) {
        onView(withId(R.id.txt_calc_display)).check(matches(withText(s)));
    }
}

现在我试过了:

Error when using a jar in my project

还有这个: Is it possible to use Java 8 for Android development?

还有这个: Gradle sourceCompatibility has no effect to subprojects

在项目 gradle 和应用程序 gradle 中。但我在想,因为它只是通过测试来的……这些无济于事,而且它与依赖关系有关,为此我很困惑。谁能帮忙!提前致谢

您有一个依赖项,不清楚是哪个,它是为 Java 8 编译的,并且您在构建中指定了 Java 7。

错误消息 This is caused by library dependencies that have been compiled using Java 8 or above. 是他们在此处输入的关键字。

我的方法是将 Java 版本更改为 8。如果这样做不起作用,请减少问题。也就是说,从一个没有依赖项和代码的新项目开始,并添加非常小的部分,直到找到导致上述错误的原因。

即应用加尔斯法则:

"A complex system that works is invariably found to have evolved from a simple system that worked. A complex system designed from scratch never works and cannot be patched up to make it work. You have to start over with a working simple system. – John Gall (1975, p.71)"

https://en.wikipedia.org/wiki/John_Gall_%28author%29

好的,对于遇到相同问题的任何人。我怀疑它! (我认为!)它会导致程序出现其他问题,但至少我有测试 运行 并且它与设备通信并在没有 paddy 的情况下阅读测试。然而,测试提出了一个全新的问题(另一个问题,另一天)新问题如果有人感兴趣:

Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
at least 90 percent of the view's area is displayed to the user.

我猜这与并非所有不可见的按钮有关(不是我的设计!仅供参考)所以我们明天调整它。

现在开始解决(差不多)

https://github.com/cucumber/cucumber-jvm/issues/893:programmerdave 于 2015 年 11 月 18 日发表的评论有助于 gradle 设置。

这带来了一个全新的错误!

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/maven/com.squareup.okhttp/okhttp/pom.properties
    File1: C:\Users\kyleparker\.gradle\caches\modules-2\files-2.1\com.crashlytics.android\crashlytics.1.13\e821eafa1bf489a26bdb71f95078c26785b37a1\crashlytics-1.1.13.jar
    File2: C:\Users\kyleparker\.gradle\caches\modules-2\files-2.1\com.squareup.okhttp\okhttp.4.0340c0748190fe897baf7bffbc1b282734294e5\okhttp-2.4.0.jar

但很快就解决了这个问题。感谢这些人 https://code.google.com/p/android/issues/detail?id=194980

无论如何,显然是两个不同的问题造成了严重破坏(一天半)。

这是计算器演示的 gradle(有问题的 link)- 感觉有点乱,如果有人有更简洁的解决方案,请告诉我

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "cucumber.cukeulator"
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"

        testApplicationId "cucumber.cukeulator.test"
        testInstrumentationRunner "cucumber.cukeulator.test.Instrumentation"
    }

    packagingOptions {

        //these are the ones that fixed it for me
        exclude 'META-INF/maven/com.google.guava/guava/pom.properties'
        exclude 'META-INF/maven/com.google.guava/guava/pom.xml'

        //but im not going to  remove my first attempt just in case ;)
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

    sourceSets {
        androidTest {
            assets.srcDirs = ['src/androidTest/assets']
        }
    }

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

dependencies {
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'

    androidTestCompile('info.cukes:cucumber-android:1.2.4') {
        exclude module: 'cucumber-jvm-deps'
    }

    androidTestCompile('info.cukes:cucumber-picocontainer:1.2.4') {
        exclude module: 'cucumber-jvm-deps'
    }

    // androidTestCompile 'com.android.support:support-annotations:23.1.1'
    // androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'

    androidTestCompile 'info.cukes:cucumber-jvm-deps:1.0.3'

}

所以总而言之,它似乎是 jvm-deps ......谁知道呢