简体   繁体   English

尝试使用Gradle,Robolectric和Travis CI测试Android应用时出现NoClassDefFoundError

[英]NoClassDefFoundError when trying to test Android app with Gradle, Robolectric and Travis CI

I am always running into NoClassDefFoundError when using Travis CI to test an Android application with Robolectric and Gradle. 使用Travis CI使用Robolectric和Gradle测试Android应用程序时,我总是遇到NoClassDefFoundError

All tests pass in Android Studio. 所有测试均通过Android Studio。 So I guess it's a Travis specific configuration issue. 所以我想这是Travis特定的配置问题。 Any experience in running Robolectric tests with Gradle and Travis? 在使用Gradle和Travis进行Robolectric测试方面有任何经验吗?

I've the following .travis.yml: 我有以下.travis.yml:

language: android
android:
  components:
  - platform-tools
  - tools
  - build-tools-21.1.2
  - android-21
  - extra-android-m2repository

licenses:
  - android-sdk-license-.+

notifications:
  email: false

script: ./gradlew clean test

And the following Robolectric specific build.gradle: 以及以下Robolectric特定的build.gradle:

apply plugin: 'robolectric'

robolectric {
    include '**/*Test.class'
}

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

    androidTestCompile 'junit:junit:4.8.1'
    androidTestCompile 'org.robolectric:robolectric:3.0-SNAPSHOT'
    androidTestCompile 'com.squareup.assertj:assertj-android:1.0.0'
}

This is my GitHub repo: https://github.com/a11n/android-travis-test/tree/travis-configuration-test 这是我的GitHub存储库: https : //github.com/a11n/android-travis-test/tree/travis-configuration-test

And this is the Travis log: https://travis-ci.org/a11n/android-travis-test/builds/49715336 这是Travis日志: https : //travis-ci.org/a11n/android-travis-test/builds/49715336

de.ad.android.app.MainActivityTest > testLifecycle FAILED
    java.lang.NoClassDefFoundError at MainActivityTest.java:19
        Caused by: java.lang.ClassNotFoundException at MainActivityTest.java:19

It seems the Android dependency cannot be resolved. 似乎无法解决Android依赖性。

Any ideas? 有任何想法吗?

Update 1: 更新1:

With --debug flag the log discovers which class could not be found: 使用--debug标志,日志会发现找不到哪个类:

15:45:33.082 [DEBUG] [TestEventLogger]         Caused by:
15:45:33.082 [DEBUG] [TestEventLogger]         java.lang.ClassNotFoundException: android.support.v4.view.OnApplyWindowInsetsListener
15:45:33.083 [DEBUG] [TestEventLogger]             at org.robolectric.internal.bytecode.InstrumentingClassLoader.getByteCode(InstrumentingClassLoader.java:166)
15:45:33.083 [DEBUG] [TestEventLogger]             at org.robolectric.internal.bytecode.InstrumentingClassLoader.findClass(InstrumentingClassLoader.java:123)
15:45:33.083 [DEBUG] [TestEventLogger]             at org.robolectric.internal.bytecode.InstrumentingClassLoader.loadClass(InstrumentingClassLoader.java:95)
15:45:33.083 [DEBUG] [TestEventLogger]             at android.support.v7.app.ActionBarActivityDelegate.$$robo$$ActionBarActivityDelegate_7c55_createDelegate(ActionBarActivityDelegate.java:51)
15:45:33.083 [DEBUG] [TestEventLogger]             at android.support.v7.app.ActionBarActivityDelegate.createDelegate(ActionBarActivityDelegate.java)
15:45:33.084 [DEBUG] [TestEventLogger]             at android.support.v7.app.ActionBarActivity.$$robo$$ActionBarActivity_eab0_getDelegate(ActionBarActivity.java:557)
15:45:33.084 [DEBUG] [TestEventLogger]             at android.support.v7.app.ActionBarActivity.getDelegate(ActionBarActivity.java)
15:45:33.084 [DEBUG] [TestEventLogger]             at android.support.v7.app.ActionBarActivity.$$robo$$ActionBarActivity_eab0_onCreate(ActionBarActivity.java:123)
15:45:33.084 [DEBUG] [TestEventLogger]             at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java)

I'll investigate a little bit further in this direction, maybe it's a known Robolectric issue. 我会朝这个方向做进一步研究,也许这是一个已知的Robolectric问题。

Update 2: 更新2:

There seems to be an issue with AppCompat v7. AppCompat v7似乎有问题。 https://github.com/robolectric/robolectric-samples/commit/85e21dcde99bc860ad91bcf2b4e3605bc267fcaa https://github.com/robolectric/robolectric-samples/commit/85e21dcde99bc860ad91bcf2b4e3605bc267fcaa

However, I try to remove this dependency and check if Travis build passes then. 但是,我尝试删除此依赖项,然后检查Travis构建是否通过。

The issue was caused by AppCompat v7. 该问题是由AppCompat v7引起的。 dependency which seems to be a known issue with Travis CI. 依赖关系,这似乎是Travis CI的一个已知问题。

For above's use-case, removing the dependency solved the problem. 对于上述用例,删除依赖项解决了该问题。

I just checked. 我刚刚检查。 Can you add next to you app build.gradle : 您可以在应用程序build.gradle旁边添加:

android {
    ...
    sourceSets {
      androidTest.setRoot( 'src/androidTest' )
    }
}

dependencies {
  compile 'com.android.support:support-v4:21.0.3'
  androidTestCompile 'com.android.support:support-v4:21.0.3'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM