简体   繁体   中英

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.

All tests pass in Android Studio. So I guess it's a Travis specific configuration issue. Any experience in running Robolectric tests with Gradle and Travis?

I've the following .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:

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

And this is the Travis log: 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.

Any ideas?

Update 1:

With --debug flag the log discovers which class could not be found:

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.

Update 2:

There seems to be an issue with AppCompat v7. https://github.com/robolectric/robolectric-samples/commit/85e21dcde99bc860ad91bcf2b4e3605bc267fcaa

However, I try to remove this dependency and check if Travis build passes then.

The issue was caused by AppCompat v7. dependency which seems to be a known issue with 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 :

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'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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