Search code examples
androidgradlerobotiumandroid-multidexandroid-instrumentation

Instrumentation test fail randomly with multidexing enabled


In my android app I have multidexing enabled. The app runs fine on emulators. I am using robotium for testing the app. But when I execute instrumentation test cases, sometimes the test passes, but mostly they also fail after system reboot. There is no code change between the time it passes and fails.

Default gradle configuration:

android {
        defaultConfig {
        applicationId "com.example.androidapp"
        minSdkVersion 16
        targetSdkVersion 23
        multiDexEnabled true
        testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
        testProguardFile "proguard-test.txt"
    }
}

Also adding dependencies for test:

androidTestCompile fileTree(dir: 'libs', include:'robotium-solo-5.3.0.jar') 

androidTestCompile ('com.android.support:multidex-instrumentation:1.0.1') {
         exclude group: 'com.android.support', module: 'multidex' }

In AndroidManifest.xml I have mentioned the application tag as:

<application
        android:name="StartupActivity"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" ...../>

I have extended "android.support.multidex.MultiDexApplication" in StartupActivity. The times when the instrumentation test cases fall I get the following error:

INSTRUMENTATION_RESULT: shortMsg=java.lang.IllegalAccessError
INSTRUMENTATION_RESULT: longMsg=java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
INSTRUMENTATION_CODE: 0

The error message in logcat is:

W/dalvikvm﹕ Class resolved by unexpected DEX: Lcom/example/androidapp/StartupActivity;(0xa695df08):0x9910e000 ref [Landroid/support/multidex/MultiDexApplication;] Landroid/support/multidex/MultiDexApplication;(0xa695df08):0x99a2c000
W/dalvikvm﹕ (Lcom/example/androidapp/StartupActivity; had used a different Landroid/support/multidex/MultiDexApplication; during pre-verification)
W/dalvikvm﹕ Unable to resolve superclass of Lcom/example/androidapp/StartupActivity; (540)
W/dalvikvm﹕ Link of class 'Lcom/example/androidapp/StartupActivity;' failed
D/AndroidRuntime﹕ Shutting down VM
W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa628c288)

The test class looks somewhat like:

public class HelloActivityTest extends ActivityInstrumentationTestCase2<HelloActivity> {
private Solo solo;
public HelloActivityTest() {
    super(HelloActivityTest.class);
}
  @Override
  public void setUp() throws Exception {
    setActivityInitialTouchMode(false);
    solo = new Solo(getInstrumentation(), getActivity());
  }

  public void test1() {}

  public void test2() {}

}

I am running the test case as an android test. I am unable to understand which dependency is messing up the code. Besides this, random failures of code are skeptical. Please help.


Solution

  • Found a solution for the same, that is setting dex verification and optimization parameters. You could also set dalvik.vm.dexopt-flags to v=n to have the framework pass -Xverify:none -Xdexopt:verified to disable verification.

    Execute:

    adb shell setprop dalvik.vm.dexopt-flags v=n,o=v
    adb shell stop installd
    adb shell start installd
    

    Had to wait for a few seconds after executing the commands. Instrumentation test with multidexing run smoothly post that.