我有一个使用SlidingUpPanelLayout
的项目,但我需要开始使用Robolectric编写自动化测试。当我向项目介绍Robolectric时,我的Espresso测试无法启动。当系统尝试膨胀SlidingUpPanelLayout时,我会得到一个java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.marcthomas.testrobolectric/com.marcthomas.testrobolectric.MyActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class com.sothree.slidinguppanel.SlidingUpPanelLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.sothree.slidinguppanel.SlidingUpPanelLayout
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1929)
at com.marcthomas.testrobolectric.MyActivity.onCreate(MyActivity.java:14)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentation.callActivityOnCreate(GoogleInstrumentation.java:428)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
... 23 more
Caused by: java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
at com.sothree.slidinguppanel.ViewDragHelper.<init>(ViewDragHelper.java:392)
at com.sothree.slidinguppanel.ViewDragHelper.create(ViewDragHelper.java:348)
at com.sothree.slidinguppanel.ViewDragHelper.create(ViewDragHelper.java:361)
at com.sothree.slidinguppanel.SlidingUpPanelLayout.<init>(SlidingUpPanelLayout.java:334)
at com.sothree.slidinguppanel.SlidingUpPanelLayout.<init>(SlidingUpPanelLayout.java:263)
... 26 more
如果我从项目中的activity_my.xml
布局文件中删除SlidingUpPanelLayout,测试将再次成功运行,所以它似乎与这两个组件有某种关系。
我在github上创建了一个演示项目,演示了这种行为。克隆代码并运行./gradlew connectedAndroidTest
将重现此问题。
答案 0 :(得分:0)
要解决此问题,我需要添加
exclude module: 'support-v4'
到app模块gradle文件中的排除列表,其中声明了用于robolectric的androidTestCompile。见下面的解释。
此错误归结为Gradle配置错误。原因是在构建中多次声明了依赖项,其中一个需要被排除。
要解决这个问题的最后一部分。
ViewDragHelper是罪魁祸首,因此如果在您的IDE(Android Studio)中打开搜索类名(OS X中的Command-O)并输入ViewDragHelper。您将看到返回了多个Jars。您需要排除您不想要的那个。
在这种情况下,我们需要android-19
sdk,support-v4-19
和support-v4-20
以及library-2.0.1
(slideuppanellayout库)来解决我需要exclude support-v4
的问题。
因此,在我包含robolectric的项目的构建gradle文件中,我需要将以下内容添加到排除列表中。
exclude module: 'support-v4'