I know that asking this type of question without specifics is very difficult, however; the project that is having the problem is proprietary and I have not been able to reproduce the issue in any other project.
So I've added the following to my build.gradle file:
androidTestCompile('com.jakewharton.espresso:espresso:1.1-r3') {
exclude group: 'com.square.dagger'
}
androidTestCompile('com.jakewharton.espresso:espresso-support-v4:1.1-r3') {
exclude group: 'com.android.support'
}
I do have two product flavors defined as well, neither are working. The test class I have is:
public class MainActivityInstrumentationTest extends ActivityInstrumentationTestCase2<MainActivity> {
private MainActivity myActivity;
private Spinner spinner;
public MainActivityInstrumentationTest() {
super(MainActivity.class);
}
protected void setUp() throws Exception {
super.setUp();
myActivity = getActivity();
spinner = (Spinner)myActivity.findViewById(R.id.watch_category);
}
public void testTextView() {
assertOnScreen(myActivity.getWindow().getDecorView(), spinner);
}
}
Now in studio it finds MainActivity.class
just fine and adds the correct import. However; when I run the test the constructor throws an exception that fully qualified path to the .class is not found. I've tried running gradle
with --debug
and I don't see anything out of the ordinary. The apk file that is generated has a classes.dex and using dex2jar
I see the MainActivity.class in there. So at this point at a bit of a loss as to why the class file is not found during the test execution. Does anyone have any other methods that I can try to debug this situation, like I said I have other projects that work just fine and I'm not able to modify them to exhibit this behavior.
Any assistance in this would be greatly appreciated.
EDIT: Full trace from logcat
W/dalvikvm( 4371): Class resolved by unexpected DEX: Landroid/support/v7/app/ActionBarActivity;(0xa501f5e8):0x94c6a000 ref [Landroid/support/v4/app/FragmentActivity;] Landroid/support/v4/app/FragmentActivity;(0xa501f5e8):0x9528b000
W/dalvikvm( 4371): (Landroid/support/v7/app/ActionBarActivity; had used a different Landroid/support/v4/app/FragmentActivity; during pre-verification)
W/dalvikvm( 4371): Unable to resolve superclass of Landroid/support/v7/app/ActionBarActivity; (282)
W/dalvikvm( 4371): Link of class 'Landroid/support/v7/app/ActionBarActivity;' failed
W/dalvikvm( 4371): Unable to resolve superclass of Lcom/mycompany/mypackage/MainActivity; (763)
W/dalvikvm( 4371): Link of class 'Lcom/mycompany/mypackage/MainActivity;' failed
E/dalvikvm( 4371): Could not find class 'com.mycompany.myapp.MainActivity', referenced from method com.mycompany.myapp.tests.MainActivityInstrumentationTest.<init>
W/dalvikvm( 4371): VFY: unable to resolve const-class 2880 (Lcom/mycompany/mypackage/MainActivity;) in Lcom/mycompany/mypackage/tests/MainActivityInstrumentationTest;
D/dalvikvm( 4371): VFY: replacing opcode 0x1c at 0x0000
W/dalvikvm( 4371): Class resolved by unexpected DEX: Landroid/support/v7/app/ActionBarActivity;(0xa501f5e8):0x94c6a000 ref [Landroid/support/v4/app/FragmentActivity;] Landroid/support/v4/app/FragmentActivity;(0xa501f5e8):0x9528b000
W/dalvikvm( 4371): (Landroid/support/v7/app/ActionBarActivity; had used a different Landroid/support/v4/app/FragmentActivity; during pre-verification)
W/dalvikvm( 4371): Unable to resolve superclass of Landroid/support/v7/app/ActionBarActivity; (282)
W/dalvikvm( 4371): Link of class 'Landroid/support/v7/app/ActionBarActivity;' failed
W/dalvikvm( 4371): Unable to resolve superclass of Lcom/mycompany/mypackage/MainActivity; (763)
W/dalvikvm( 4371): Link of class 'Lcom/mycompany/mypackage/MainActivity;' failed
E/dalvikvm( 4371): Could not find class 'com.mycompany.myapp.MainActivity', referenced from method com.mycompany.myapp.tests.MainActivityInstrumentationTest.setUp
W/dalvikvm( 4371): VFY: unable to resolve check-cast 2880 (Lcom/mycompany/mypackage/MainActivity;) in Lcom/mycompany/mypackage/tests/MainActivityInstrumentationTest;
D/dalvikvm( 4371): VFY: replacing opcode 0x1f at 0x0007
D/dalvikvm( 4371): GC_FOR_ALLOC freed 261K, 10% free 3376K/3712K, paused 2ms, total 2ms
W/dalvikvm( 4371): Class resolved by unexpected DEX: Landroid/support/v7/app/ActionBarActivity;(0xa501f5e8):0x94c6a000 ref [Landroid/support/v4/app/FragmentActivity;] Landroid/support/v4/app/FragmentActivity;(0xa501f5e8):0x9528b000
W/dalvikvm( 4371): (Landroid/support/v7/app/ActionBarActivity; had used a different Landroid/support/v4/app/FragmentActivity; during pre-verification)
W/dalvikvm( 4371): Unable to resolve superclass of Landroid/support/v7/app/ActionBarActivity; (282)
W/dalvikvm( 4371): Link of class 'Landroid/support/v7/app/ActionBarActivity;' failed
W/dalvikvm( 4371): Unable to resolve superclass of Lcom/mycompany/mypackage/MainActivity; (763)
W/dalvikvm( 4371): Link of class 'Lcom/mycompany/mypackage/MainActivity;' failed
W/dalvikvm( 4371): Class resolved by unexpected DEX: Landroid/support/v7/app/ActionBarActivity;(0xa501f5e8):0x94c6a000 ref [Landroid/support/v4/app/FragmentActivity;] Landroid/support/v4/app/FragmentActivity;(0xa501f5e8):0x9528b000
W/dalvikvm( 4371): (Landroid/support/v7/app/ActionBarActivity; had used a different Landroid/support/v4/app/FragmentActivity; during pre-verification)
W/dalvikvm( 4371): Unable to resolve superclass of Landroid/support/v7/app/ActionBarActivity; (282)
W/dalvikvm( 4371): Link of class 'Landroid/support/v7/app/ActionBarActivity;' failed
W/dalvikvm( 4371): Unable to resolve superclass of Lcom/mycompany/mypackage/MainActivity; (763)
W/dalvikvm( 4371): Link of class 'Lcom/mycompany/mypackage/MainActivity;' failed
I/dalvikvm( 4371): Could not find method com.mycompany.myapp.MainActivity.getWindow, referenced from method com.mycompany.myapp.tests.MainActivityInstrumentationTest.testTextView
W/dalvikvm( 4371): VFY: unable to resolve virtual method 21012: Lcom/mycompany/mypackage/MainActivity;.getWindow ()Landroid/view/Window;
D/dalvikvm( 4371): VFY: replacing opcode 0x6e at 0x0002
D/dalvikvm( 4371): GC_FOR_ALLOC freed 348K, 11% free 3541K/3964K, paused 2ms, total 2ms
I/System.out( 4371): Sending WAIT chunk
I/dalvikvm( 4371): Debugger is active
I/System.out( 4371): Debugger has connected
I/System.out( 4371): waiting for debugger to settle...
I/System.out( 4371): waiting for debugger to settle...
I/System.out( 4371): waiting for debugger to settle...
I/System.out( 4371): waiting for debugger to settle...
I/System.out( 4371): waiting for debugger to settle...
I/System.out( 4371): waiting for debugger to settle...
I/System.out( 4371): waiting for debugger to settle...
I/System.out( 4371): debugger has settled (1483)
I/TestRunner( 4371): started: warning(junit.framework.TestSuite$1)
I/GoogleInstr( 4371): Activities that are still in CREATED to PAUSED: 0
I/TestRunner( 4371): failed: warning(junit.framework.TestSuite$1)
I/TestRunner( 4371): ----- begin exception -----
I/TestRunner( 4371):
I/TestRunner( 4371): junit.framework.AssertionFailedError: Exception in constructor: testTextView (java.lang.NoClassDefFoundError: com.mycompany.myapp.MainActivity
I/TestRunner( 4371): at com.mycompany.myapp.tests.MainActivityInstrumentationTest.<init>(MainActivityInstrumentationTest.java:19)
I/TestRunner( 4371): at java.lang.reflect.Constructor.constructNative(Native Method)
I/TestRunner( 4371): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
I/TestRunner( 4371): at junit.framework.TestSuite.createTest(TestSuite.java:61)
I/TestRunner( 4371): at junit.framework.TestSuite.addTestMethod(TestSuite.java:294)
I/TestRunner( 4371): at junit.framework.TestSuite.addTestsFromTestCase(TestSuite.java:150)
I/TestRunner( 4371): at junit.framework.TestSuite.<init>(TestSuite.java:129)
I/TestRunner( 4371): at junit.runner.BaseTestRunner.getTest(BaseTestRunner.java:118)
I/TestRunner( 4371): at android.test.AndroidTestRunner.getTest(AndroidTestRunner.java:149)
I/TestRunner( 4371): at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:57)
I/TestRunner( 4371): at android.test.suitebuilder.TestSuiteBuilder.addTestClassByName(TestSuiteBuilder.java:80)
I/TestRunner( 4371): at android.test.InstrumentationTestRunner.parseTestClass(InstrumentationTestRunner.java:443)
I/TestRunner( 4371): at android.test.InstrumentationTestRunner.parseTestClasses(InstrumentationTestRunner.java:424)
I/TestRunner( 4371): at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:370)
I/TestRunner( 4371): at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onCreate(GoogleInstrumentationTestRunner.java:114)
I/TestRunner( 4371): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4335)
I/TestRunner( 4371): at android.app.ActivityThread.access$1500(ActivityThread.java:135)
I/TestRunner( 4371): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
I/TestRunner( 4371): at android.os.Handler.dispatchMessage(Handler.java:102)
I/TestRunner( 4371): at android.os.Looper.loop(Looper.java:136)
I/TestRunner( 4371): at android.app.ActivityThread.main(ActivityThread.java:5017)
I/TestRunner( 4371): at java.lang.reflect.Method.invokeNative(Native Method)
I/TestRunner( 4371): at java.lang.reflect.Method.invoke(Method.java:515)
I/TestRunner( 4371): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
I/TestRunner( 4371): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
I/TestRunner( 4371): at dalvik.system.NativeStart.main(Native Method)
I/TestRunner( 4371): )
I/TestRunner( 4371): at junit.framework.Assert.fail(Assert.java:50)
I/TestRunner( 4371): at junit.framework.TestSuite$1.runTest(TestSuite.java:97)
I/TestRunner( 4371): at junit.framework.TestCase.runBare(TestCase.java:134)
I/TestRunner( 4371): at junit.framework.TestResult$1.protect(TestResult.java:115)
I/TestRunner( 4371): at junit.framework.TestResult.runProtected(TestResult.java:133)
I/TestRunner( 4371): at junit.framework.TestResult.run(TestResult.java:118)
I/TestRunner( 4371): at junit.framework.TestCase.run(TestCase.java:124)
I/TestRunner( 4371): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
I/TestRunner( 4371): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
I/TestRunner( 4371): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
I/TestRunner( 4371): at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onStart(GoogleInstrumentationTestRunner.java:167)
I/TestRunner( 4371): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
I/TestRunner( 4371): ----- end exception -----
I/TestRunner( 4371): finished: warning(junit.framework.TestSuite$1)
I/GoogleInstr( 4371): Activities that are still in CREATED to PAUSED: 0
I/GoogleInstr( 4371): waitForActivitiesToComplete() took: 0ms
D/AndroidRuntime( 4361): Shutting down VM
Edit: build.gradle dependencies
dependencies {
compile 'com.mcxiaoke.volley:library-aar:1.0.+'
compile 'com.android.support:support-v4:19.0.+'
compile 'com.android.support:appcompat-v7:19.0.+'
compile 'com.google.android.gms:play-services:4.2.42'
compile 'com.arcbees:facebook:1.1.1'
compile 'com.facebook:facebook-android-sdk:3.5.2@aar'
compile 'com.j256.ormlite:ormlite-android:4.47'
compile 'com.j256.ormlite:ormlite-core:4.47'
compile 'com.google.code.gson:gson:2.2.4'
compile 'org.slf4j:slf4j-api:1.7.5'
compile 'com.github.tony19:logback-android-classic:1.1.1-3'
compile 'com.crashlytics.android:crashlytics:1.+'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.squareup.dagger:dagger:1.0.1'
compile 'com.squareup.dagger:dagger-compiler:1.0.1'
compile 'com.jakewharton:butterknife:1.3.2'
compile 'com.actionbarsherlock:viewpagerindicator:2.4.1'
compile 'com.squareup:otto:1.3.4'
compile 'com.squareup.picasso:picasso:1.1.1'
compile 'com.squareup.retrofit:retrofit:1.2.2'
compile 'com.github.machinarius:preferencefragment:0.1.1'
compile files('libs/cwac-wakefulintentservice.jar')
compile files('libs/libGoogleAnalyticsServices.jar')
compile files('libs/android-switch-backport.jar')
compile files('libs/google-api-client-googleapis-1.4.1-beta.jar')
androidTestCompile 'com.squareup:fest-android:1.0.+'
androidTestCompile('com.jakewharton.espresso:espresso:1.1-r3') {
exclude group: 'com.square.dagger'
}
androidTestCompile('com.jakewharton.espresso:espresso-support-v4:1.1-r3') {
exclude group:'com.android.support', module:'support-v4'
exclude group:'com.android.support', module:'appcompat-v7'
}
}
Update: After making updating the dependecies I still get the NoClassDefFoundError and in logcat I see
W/dalvikvm( 3373): Class resolved by unexpected DEX: Lcom/mycompany/mypackage/MainActivity;(0xa50a6d18):0x94ba7000 ref [Landroid/support/v7/app/ActionBarActivity;] Landroid/support/v7/app/ActionBarActivity;(0xa50a6d18):0x95318000
W/dalvikvm( 3373): (Lcom/mycompany/mypackage/MainActivity; had used a different Landroid/support/v7/app/ActionBarActivity; during pre-verification)
W/dalvikvm( 3373): Unable to resolve superclass of Lcom/mycompany/mypackage/MainActivity; (786)
W/dalvikvm( 3373): Link of class 'Lcom/mycompany/mypackage/MainActivity;' failed
E/dalvikvm( 3373): Could not find class 'com.ping4.ping4alerts.MainActivity', referenced from method com.ping4.ping4alerts.tests.MainActivityInstrumentationTest.<init>
W/dalvikvm( 3373): VFY: unable to resolve const-class 3298 (Lcom/mycompany/mypackage/MainActivity;) in Lcom/mycompany/mypackage/tests/MainActivityInstrumentationTest;