问题
I'm struggling getting a unit test in Android Studio to work on some existing code. I'm kepp getting the IllegalAccessError and can't seem to find a solution for it. Any help is very needed.
I'm using Android Studio 1.1.0, gradle 1.1.0 and Espresso 2.0.
The stacktrace:
java.lang.NoClassDefFoundError: com/hoganas/eclino/activities/BikeActivity
at com.hoganas.eclino.activities.ScanActivity$1.onItemClick(ScanActivity.java:49)
at android.widget.AdapterView.performItemClick(AdapterView.java:308)
at android.widget.AbsListView.performItemClick(AbsListView.java:1478)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3480)
at android.widget.AbsListView$3.run(AbsListView.java:4823)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.support.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:461)
at android.support.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:402)
at android.support.test.espresso.base.UiControllerImpl.loopMainThreadForAtLeast(UiControllerImpl.java:387)
at android.support.test.espresso.action.Tap$1.sendTap(Tap.java:44)
at android.support.test.espresso.action.GeneralClickAction.perform(GeneralClickAction.java:98)
at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:144)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
at dalvik.system.DexFile.defineClassNative(Native Method)
at dalvik.system.DexFile.defineClass(DexFile.java:222)
at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:215)
at dalvik.system.DexPathList.findClass(DexPathList.java:322)
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
... 24 more
This is the test case, it seems to fail on the call to perform(click()).
public class BluetoothConnectionTest extends ActivityInstrumentationTestCase2<ScanActivity> {
public BluetoothConnectionTest() {
super(ScanActivity.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
getActivity();
}
private final long SLEEP = 1000;
private final long SLEEP_LONG = 2000;
private final long SLEEP_LONGER = SLEEP_LONG * 2;
public void testScanActivity() {
try {
Espresso.onView(withId(R.id.lvScanResult));
ViewInteraction vi = Espresso.onData(is(instanceOf(LeDeviceListAdapter.BluetoothItem.class)))
.inAdapterView(withId(R.id.lvScanResult)).onChildView(withId(R.id.tvName)).check(matches(withText("Bike 2")));
Thread.sleep(SLEEP_LONG);
vi.perform(click());
Espresso.onView(withId(R.id.tvSpeedUnit)).check(matches(withText("km/h")));
Thread.sleep(SLEEP_LONGER);
} catch (Exception e) {
e.printStackTrace();
}
}
...and yes I know I shouldn't use Thread.sleep in the test but that's another question... I'm also struggling to get the CountingIdlingResource to work here...
And here is the gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId 'com.hoganas.eclino'
minSdkVersion 18
targetSdkVersion 21
versionCode 2
versionName "2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
zipAlignEnabled true
}
debug {
zipAlignEnabled true
}
}
productFlavors {
}
packagingOptions {
exclude('LICENSE.txt')
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
provided 'com.google.guava:guava:18.0'
compile 'com.android.support:support-v13:21.0.3'
androidTestProvided 'com.google.guava:guava:18.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
androidTestCompile 'com.android.support.test.espresso:espresso-idling-resource:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}
The stack trace also points out that the IllegalStateError causes a NoClassDefFoundError when the BikeActivity class is accessed in the activity under test:
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
if (device == null) return;
final Intent intent = new Intent(getApplicationContext(), BikeActivity.class);
intent.putExtra(BikeActivity.EXTRAS_DEVICE_NAME, device.getName());
intent.putExtra(BikeActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());
if (mScanning) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
mScanning = false;
}
startActivity(intent);
}
EDIT: Now I changed the gradle dependency to this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
androidTestCompile ('com.android.support.test.espresso:espresso-core:2.0') {
exclude group: "javax.inject"
exclude group: "com.google.guava"
}
androidTestCompile ('com.android.support.test:testing-support-lib:0.1') {
exclude group: "javax.inject"
exclude group: "com.google.guava"
}
}
When I run the test now I get the following error instead:
java.lang.NoClassDefFoundError:
android.support.test.espresso.base.BaseLayerModule$$ModuleAdapter$ProvideLifecycleMonitorProvidesAdapter
at android.support.test.espresso.base.BaseLayerModule$$ModuleAdapter.getBindings(BaseLayerModule$$ModuleAdapter.java:36)
at android.support.test.espresso.base.BaseLayerModule$$ModuleAdapter.getBindings(BaseLayerModule$$ModuleAdapter.java:16)
at com.android.support.test.deps.dagger.ObjectGraph$DaggerObjectGraph.makeGraph(ObjectGraph.java:185)
at com.android.support.test.deps.dagger.ObjectGraph$DaggerObjectGraph.access$000(ObjectGraph.java:138)
at com.android.support.test.deps.dagger.ObjectGraph.create(ObjectGraph.java:129)
at android.support.test.espresso.GraphHolder.graph(GraphHolder.java:48)
at android.support.test.espresso.Espresso.espressoGraph(Espresso.java:55)
at android.support.test.espresso.Espresso.onView(Espresso.java:70)
at android.support.test.espresso.DataInteraction.load(DataInteraction.java:151)
at android.support.test.espresso.DataInteraction.check(DataInteraction.java:141)
at com.hoganas.eclino.BluetoothConnectionTest.testScanActivity(BluetoothConnectionTest.java:40)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:115)
at junit.framework.TestResult.runProtected(TestResult.java:133)
at android.support.test.internal.runner.junit3.DelegatingTestResult.runProtected(DelegatingTestResult.java:90)
at junit.framework.TestResult.run(TestResult.java:118)
at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:49)
at junit.framework.TestCase.run(TestCase.java:124)
at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103)
at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:63)
at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:270)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1741)
Any ideas what to do?
EDIT 2: I use the AndroidJUnitRunner, not the GoogleInstrumentationTestRunner. The docs for Espresso 2.0 tells you to use the AndroidJUnitRunner even though lots of poeple here suggest the GITR. I tried to switch to GITR but didn't even get the test to run.
回答1:
In your build.gradle dependencies
:
androidTestCompile ("com.android.support.test.espresso:espresso-contrib:2.2.1") {
exclude group: 'com.android.support', module: 'support-v4'
}
回答2:
You can try to add following exclude:
configurations {
androidTestCompile.exclude group: 'com.android.support', module: 'support-v4'
}
This solved java.lang.NoClassDefFoundError
in my case.
回答3:
you must exclude javax.inject
androidTestCompile('com.android.support.test.espresso:espresso-core:2.0') {
exclude group: "javax.inject"
}
Note you may have to do this for all the espresso dependencies.
See:
bug report
Related question 1
Related question 2
来源:https://stackoverflow.com/questions/28820419/illegalaccesserror-class-ref-in-pre-verified-class-resolved-to-unexpected-imple