Android Studio:测试:已使用java 8或更高版本编译的库依赖项 [英] Android Studio: Testing: Library dependencies that have been compiled using java 8 or above

查看:2508
本文介绍了Android Studio:测试:已使用java 8或更高版本编译的库依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙。我有一个非常可怕的时间设置我的测试为android studio。



我已经下载计算器示例来练习cumcumber代码测试从黄瓜github。 https://github.com/cucumber/cucumber-jvm/tree/master/android (其中某些品牌的btw是非常扼杀命名的)



我试着在Android studio中使用它。程序运行完美(yay!)。然而测试不是。我有一个非常可怕的消息,只是困扰我每次我运行它。

  *要运行dex进程中,Gradle守护进程需要更大的堆。 
目前有大约910 MB。
为了更快的构建,将Gradle守护程序的最大堆大小增加到超过2048 MB。
为此,在项目gradle.properties中设置org.gradle.jvmargs = -Xmx2048M。
有关更多信息,请参阅https://docs.gradle.org/current/userguide/build_environment.html
错误:将字节码转换为dex时出错:
原因:Dex无法解析52版字节码。
这是由使用Java 8或更高版本编译的库依赖性引起的。
如果在库子模块中使用'java'gradle插件,请将
targetCompatibility ='1.7'
sourceCompatibility ='1.7'
添加到该子模块的build.gradle文件中。
错误:1错误; aborting
:app:transformClassesWithDexForDebugAndroidTest FAILED
错误:任务':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:进程'命令'C:\Program Files\Java\jdk1.8.0_74\bin\java.exe''完成非零退出值1 *

它的目标兼容性和源兼容性我遇到了麻烦(还没有到剩下的) p>

这里是gradle版本:你可以看到我已经改变了兼容性1.7

  apply plugin:'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion23.0.3

defaultConfig {
applicationIdboo.thefoodhunt
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName1.0

testInstrumentationRunnerandroid.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'c​​om.android.support:appcompat-v7:23.3.0'

//测试
androidTestCompile'c​​om.android.support .test:testing-support-lib:0.1'
androidTestCompile'c​​om.android.support:support-annotations:23.3.0'

// Espresso
androidTestCompile'c​​om。 android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile'c​​om.android.support.test:runner:0.5'
androidTestCompile'c​​om.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 ){
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(^我按([+ -x\\ / =])$)
public void I_press_op char op){
switch(op){
case'+':
onView(withId(R.id.btn_op_add))。
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(^我应该看到(\\S +)在显示$)
public void I_should_see_s_on_the_display(final String s ){
onView(withId(R.id.txt_calc_display))。check(matches(withText(s)));
}
}



现在ive尝试过:



在我使用jar时出错项目



和这:
是否可以使用Java 8进行Android开发?



$ b Gradle sourceCompatibility对子项目没有任何影响



在项目gradle和应用程序gradle中。但我正在考虑它唯一的测试...这些不会帮助和它的东西与依赖关系,我很好的卡住了。任何人都可以帮助!提前感谢

解决方案

对任何有同样问题的人确定。我I了! (我认为!)它的导致其他问题的程序,但至少我有测试运行和它与设备和阅读通过测试,没有稻田。然而,测试出现了一个全新的问题(对于另一个问题,另一天)如果任何人感兴趣的新问题:

  Caused by:java.lang.RuntimeException:将不执行操作,因为目标视图不匹配以下一个或多个约束:
至少90%的视图区域显示给用户。

我估计它的东西与不是所有的按钮不在视图)



https://github.com/cucumber/cucumber-jvm/issues/893rel =nofollow> https://github.com/cucumber/cucumber-jvm/issues/893 :来自programmerdave的评论2015年11月18日帮助gradle设置。



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

 错误:任务':app:transformResourcesWithMergeJavaResForDebug'的执行失败。 
> com.android.build.api.transform.TransformException:com.android.builder.packaging.DuplicateFileException:在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
buildToolsVersion21.1.2

defaultConfig {
applicationIdcucumber.cukeulator
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName 1.0

testApplicationIdcucumber.cukeulator.test
testInstrumentationRunnercucumber.cukeulator.test.Instrumentation
}

packagingOptions {

//这些是修复它的那些
exclude'META-INF / maven / com.google.guava / guava / pom.properties'
exclude'META-INF / maven / com.google.guava / guava / pom.xml'

//但是我不会删除我的第一次尝试,以防万一;)
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'
}
}
}

依赖关系{
androidTestCompile'c​​om.android.support.test.espresso:espresso-core:2.0'
androidTestCompile'c​​om。 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'c​​om.android.support:support-annotations:23.1.1'
// androidTestCompile'c​​om.android.support.test.espresso:espresso-core: 2.2.2'

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

}
pre>

所有的一切似乎是jvm-deps ......很好谁知道


please help. I am having a really horrible time with setting up my testing for android studio.

I have downloaded the calculator example to practice cumcumber code testing from the cucumber github. https://github.com/cucumber/cucumber-jvm/tree/master/android (some of these brands btw are very strangley named)

I tried to use it with Android studio. The program runs perfect (yay!). The test however does not. I have a really horrible message that just haunts me every time i run it.

*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*

Its the target compatibility and source compatibility i am having trouble with (have not got to the rest yet)

Here's the gradle build: as you can see i have changed the compatibility to 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'
}

The test that is failing to run:

 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)));
    }
}

Now ive tried this:

Error when using a jar in my project

and this: Is it possible to use Java 8 for Android development?

and this: Gradle sourceCompatibility has no effect to subprojects

in both the project gradle and the app gradle. But i'm am thinking as its only coming with testing...these will not help and its something to do with the dependencies and for that i am pretty stuck. Can anyone help! thanks in advance

解决方案

Ok for any one that is having the same problem. I sussed it! (I think!) its lead on to other problems with the program but at least i have the test running and its communicating with the device and reading through the tests without having paddy. The tests however are coming up with a brand new problem (for another question, another day) New problem if anyone is interested:

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.

im guessing that its something to do with not all the buttons not in view (not my design! FYI) so we adjust this tomorrow.

Now on to the solution(ish)

https://github.com/cucumber/cucumber-jvm/issues/893: comment from programmerdave on 18 Nov 2015 helped with the gradle setup.

This brought a whole new 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

But managed to fix this quite quickly. thanks to these guys https://code.google.com/p/android/issues/detail?id=194980

Anyway apparently two different issues that created havoc (a day and half).

Heres the gradle for the Calkulator demo (link in question) - it all feels a bit messy and if anyone has a nice cleaner solution please let me know

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'

}

So all in all it seems to be that jvm-deps......well who knew

这篇关于Android Studio:测试:已使用java 8或更高版本编译的库依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆