当前位置: 首页 > 知识库问答 >
问题:

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

燕靖
2023-03-14

请帮助。我有一个非常可怕的时间设置我的测试android工作室。

我已经从cucumber github下载了计算器示例,以练习cucumber代码测试。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)));
    }
}

现在我试过这个:

在我的项目中使用jar时出错

这就是:是否可以将Java 8用于Android开发

这个:Gradle源兼容性对子项目没有影响

在项目gradle和应用程序gradle中。但我认为这只是测试的结果。。。这些都不会有帮助,这与依赖性有关,为此,我非常困惑。有人能帮忙吗!提前感谢

共有2个答案

江智
2023-03-14

对于任何有相同问题的人来说都可以。我成功了!(我想!)这会导致程序出现其他问题,但至少我可以运行测试,并与设备通信,在没有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.

我猜这与不是所有的按钮都不在视图中有关(不是我的设计!仅供参考),所以我们明天调整。

现在进入解决方案(ish)

https://github.com/cucumber/cucumber-jvm/issues/893:2015年11月18日的程序员戴夫的评论有助于Gradle设置。

这带来了一个全新的bug!

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.1.13\e821eafa1bf489a26bdb71f95078c26785b37a1\crashlytics-1.1.13.jar
    File2: C:\Users\kyleparker\.gradle\caches\modules-2\files-2.1\com.squareup.okhttp\okhttp\2.4.0\40340c0748190fe897baf7bffbc1b282734294e5\okhttp-2.4.0.jar

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

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

这是Calkulator演示的渐变(链接有问题)-感觉有点凌乱,如果有人有一个很好的清洁解决方案,请告诉我

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/html" target="_blank">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......谁知道呢

汪鸿志
2023-03-14

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

错误消息是由使用Java 8或更高版本编译的库依赖项引起的 此处是否为键。

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

也就是说,应用Galls定律:

“人们总是会发现,一个有效的复杂系统是从一个有效的简单系统演变而来的。一个从无到有设计的复杂系统永远不会起作用,也无法通过修补使其起作用。你必须从一个有效的简单系统重新开始。”——约翰·加尔(1975,第71页)

https://en.wikipedia.org/wiki/John_Gall_(作者)

 类似资料: