androidandroid-gradle-pluginandroid-testingrobotium

Upgrading compileOptions to java8 causes connectedDebugAndroidTest to not run on Kitkat devices


As we have upgraded our android project to java8, the connectedAndroidTest stopped running the Robotium test on Kitkat devices. However the same code runs fine on Lollipop and higher android versions, or even on kitkat it runs fine when executing from Android Studio. But as soon as we switch to ./gradlew cAT it stops execution on kitkat devices with below logs:

If execution is done using AndroidJUnitRunner below error is shown:

18:29:56.902 [LIFECYCLE] [org.gradle.api.Task] Starting 0 tests on Titanium Octane Plus - 4.4.2 18:29:56.903 [WARN] [org.gradle.api.Task] Tests on Titanium Octane Plus - 4.4.2 failed: Instrumentation run failed due to 'java.lang.IllegalAccessError' 18:29:56.939 [INFO] [org.gradle.api.Task] [XmlResultReporter]: XML test result file generated at /Users/sil/Workspace/Android/LrAndroid/dev-eng/Thio/android/build/outputs/androidTest-results/connected/TEST-Titanium Octane Plus - 4.4.2-android-.xml. Total tests 0, 18:29:56.939 [INFO] [com.android.build.gradle.internal.ApplicationTaskManager] [XmlResultReporter]: XML test result file generated at /Users/sil/Workspace/Android/LrAndroid/dev-eng/Thio/android/build/outputs/androidTest-results/connected/TEST-Titanium Octane Plus - 4.4.2-android-.xml. Total tests 0, 18:29:57.108 [WARN] [org.gradle.api.Task] com.android.builder.testing.ConnectedDevice > No tests found.[Titanium Octane Plus - 4.4.2] FAILED 18:29:57.109 [WARN] [org.gradle.api.Task] No tests found. This usually means that your test classes are not in the form that your test runner expects (e.g. don't inherit from TestCase or lack @Test annotations).

But if orchestrator is used, below log appears:

12:13:48.953 [LIFECYCLE] [org.gradle.api.Task] Starting 0 tests on Titanium Octane Plus - 4.4.2 12:13:48.963 [INFO] [org.gradle.api.Task] [XmlResultReporter]: XML test result file generated at /Users/sil/Workspace/Android/LrAndroid/dev-eng/Thio/android/build/outputs/androidTest-results/connected/TEST-Titanium Octane Plus - 4.4.2-android-.xml. Total tests 0, 12:13:48.963 [INFO] [org.gradle.api.Task] [XmlResultReporter]: XML test result file generated at /Users/sil/Workspace/Android/LrAndroid/dev-eng/Thio/android/build/outputs/androidTest-results/connected/TEST-Titanium Octane Plus - 4.4.2-android-.xml. Total tests 0, 12:13:48.963 [INFO] [org.gradle.api.Task] [XmlResultReporter]: XML test result file generated at /Users/sil/Workspace/Android/LrAndroid/dev-eng/Thio/android/build/outputs/androidTest-results/connected/TEST-Titanium Octane Plus - 4.4.2-android-.xml. Total tests 0, 12:13:48.963 [INFO] [org.gradle.api.Task] [XmlResultReporter]: XML test result file generated at /Users/sil/Workspace/Android/LrAndroid/dev-eng/Thio/android/build/outputs/androidTest-results/connected/TEST-Titanium Octane Plus - 4.4.2-android-.xml. Total tests 0, 12:13:48.963 [INFO] [com.android.build.gradle.internal.ApplicationTaskManager] [XmlResultReporter]: XML test result file generated at /Users/sil/Workspace/Android/LrAndroid/dev-eng/Thio/android/build/outputs/androidTest-results/connected/TEST-Titanium Octane Plus - 4.4.2-android-.xml. Total tests 0, 12:13:48.995 [WARN] [org.gradle.api.Task] com.android.builder.testing.ConnectedDevice > No tests found.[Titanium Octane Plus - 4.4.2] FAILED 12:13:48.995 [WARN] [org.gradle.api.Task] No tests found. This usually means that your test classes are not in the form that your test runner expects (e.g. don't inherit from TestCase or lack @Test annotations).

Changes made in build.gradle to use java8 are:

compileOptions { targetCompatibility JavaVersion.VERSION_1_8 sourceCompatibility JavaVersion.VERSION_1_8 }

Looking forward to workarounds/explanation for why the issue is occurring only on Kitkat and lower devices (although i am not much bothered about devices having android version lower than kitkat).


Solution

  • The problem was due to not defining the java source directories for testing code in the build.gradle. So previously the code was like

    instrumentTest.setRoot('tests')
    

    or

    androidTest.setRoot('tests')
    

    which when updated to below, solved the problem.

    androidTest {
            setRoot('tests')
            java.srcDirs = ['src/androidTest/java']
            manifest.srcFile file('src/androidTest/AndroidManifest.xml')
        }