lundi 2 mars 2015

IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation in Android unit test

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);
}

Aucun commentaire:

Enregistrer un commentaire