Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 908
pzulw Avatar asked Jan 20 '15 22:01

pzulw


People also ask

What is the use of multidex in Android?

By adding this library, your app can manage the access of additional DEX files. In other words, if you are having more than 64K methods, then you will be having more than one DEX file and these DEX files will be managed by using this multidex support library. Then, modify the module-level build.

What is multidex enabled?

Android applications by default have SingleDex support which limits your application to have only 65536 methods(references). So multidexEnabled = true simply means that now you can write more than 65536 methods(references) in your application.

What is AndroidX test runner?

AndroidX Test is a collection of Jetpack libraries that lets you run tests against Android apps. It also provides a series of tools to help you write these tests. For example, AndroidX Test provides JUnit4 rules to start activities and interact with them in JUnit4 tests.


2 Answers

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);
    }
}
like image 117
mbmc Avatar answered Oct 18 '22 18:10

mbmc


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

like image 33
Patrick Favre Avatar answered Oct 18 '22 19:10

Patrick Favre