Which package for MultiDexTestRunner? android.support.multidex or com.android.test.runner

The page http://developer.android.com/tools/building/multidex.html#testing
advises

   dependencies {
     compile 'com.android.support:multidex:1.0.1'
     androidTestCompile 'com.android.support:multidex-instrumentation:1.0.1'
   }
   android {
     defaultConfig {
        multiDexEnabled true
        testInstrumentationRunner "android.support.multidex.MultiDexTestRunner"
     }
  }

But that produces a ClassNotFoundException when the tests are run.

  • How do you implement context menu in a ListActivity on Android?
  • RxJava with Presenter and retained fragment for configuration changes
  • Display virtual keyboard in android TabLayout
  • Android Emulator, cover screen with palm to dim
  • LocationClient doesn't give callback when screen light goes off but my WakefulThread is running flawlessly as expected
  • android studio not downloading sources and javadoc
  • The API documentation and dexdump show that there is com.android.test.runner.MultiDexTestRunner.

    So if I disbelieve the documentation and instead specify

       dependencies {
         compile 'com.android.support:multidex:1.0.1'
         androidTestCompile 'com.android.support:multidex-instrumentation:1.0.1'
       }
      android {
         defaultConfig {
            multiDexEnabled true
            testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
         }
       }
    

    Then I get

    com/company/myapp/MyApp; had used a different Landroid/support/multidex/MultiDexApplication; during pre-verification 
    ...
    IllegalAccessExceptionIllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
    

    I suspect that the doc page is wrong and the correct path is com.android.test.runner.MultiDexTestRunner … plus I have some other issue.

    Note the multidex application works fine. Somehow a second MultiDexApplication is included in the test apk.

    Questions:
    Which is the correct path for MultiDexTestRunner?
    Why am I getting a second MultiDexApplication in the test apk?

    Related posts:

    Share library project's manifest services and permissions
    Auto import not working for Android classes in Android studio
    How does android compare pending intents
    How to play native camera sound on Android
    Android regular expression - return matched string
    Patch Request Android Volley
  • Eclipse DDMS not showing Emulator on restart
  • Make Android app show up as designed for tablets
  • How to add limit clause using content provider
  • Android: Vertical ListView with overlaped rows
  • Adding a color background and border radius to a Layout
  • How do I draw a curve through tree points in Android?
  • 3 Solutions collect form web for “Which package for MultiDexTestRunner? android.support.multidex or com.android.test.runner”

    UPDATE: here’s the fix. That’s a common pattern, when you see such error message had used a different L<package>; during pre-verification, you need to exclude the package when running the test.

    build.gradle

    android {
        // ...
        defaultConfig {
            // ...
            multiDexEnabled true
            testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
        }
    }
    
    dependencies {
        // ...
        // Need to exclude this when running test
        androidTestCompile('com.android.support:multidex-instrumentation:1.0.1') {
            exclude group: 'com.android.support', module: 'multidex'
        }
    }
    

    Application.java

    public class Application extends android.app.Application {
        @Override
        protected void attachBaseContext(Context base) {
            super.attachBaseContext(base);
            MultiDex.install(this);
        }
    }
    

    Note: When you write instrumentation tests for multidex apps, no additional configuration is required if you use a MonitoringInstrumentation (or an AndroidJUnitRunner) instrumentation.

    Thus, don’t use MultiDexTestRunner, which is deprecated; use AndroidJUnitRunner instead. (This applies to multidex support library v1.0.2+)

    android {
        // ...
        defaultConfig {
            // ...
            multiDexEnabled true
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    }
    

    Only if you have a custom implementation of the test runner bootstrap with:

    public void onCreate(Bundle arguments) {
      MultiDex.install(getTargetContext());
      super.onCreate(arguments);
      ...
    }
    

    See: https://developer.android.com/studio/build/multidex.html#testing

    The exact problems described in the original question may be related to older versions. I’m adding my experience here, because this is one of the first Google hits when researching the problem.

    With recent versions of tools and android plugin you don’t need anything else than this in build.gradle:

    ...
    android {
        ...
        defaultConfig {
            ...
            testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
        }
    }
    

    (I have com.android.tools.build:gradle:1.5.0)

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.