I'm trying to run unit tests with Robolectric 2.1.1 and I cannot get it to inflate custom layouts (e.g. ViewPagerIndicator classes).
Suppose this is my layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="test"
android:id="#+id/test_test"/>
<com.viewpagerindicator.CirclePageIndicator
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Consider this my test class:
#RunWith(RobolectricTestRunner.class)
public class TestRoboActivityTest {
private TestRoboActivity mActivity;
#Before
public void setUp() throws Exception {
mActivity = Robolectric.buildActivity(TestRoboActivity.class).create().get();
}
#After
public void tearDown() throws Exception {
mActivity = null;
}
#Test
public void testSanity() throws Exception {
Assert.assertNotNull(mActivity);
}
}
Executing 'mvn clean test' results in
Tests in error:
testSanity(TestRoboActivityTest): XML file .\res\layout\test.xml line #-1 (sorry, not yet implemented): Error inflating class com.viewpagerindicator.CirclePageIndicator
Cool, so it seems like custom views aren't supported yet. Checking the sample Robolectric project on their website,
one solution could be to inflate the layout from LayoutInflater:
#RunWith(RobolectricTestRunner.class)
public class TestRoboActivityTest {
private View mTestRoboActivityView;
#Before
public void setUp() throws Exception {
mTestRoboActivityView = LayoutInflater.from(new Activity()).inflate(R.layout.test, null);
}
#After
public void tearDown() throws Exception {
mTestRoboActivityView = null;
}
#Test
public void testSanity() throws Exception {
Assert.assertNotNull(mTestRoboActivityView);
}
}
which results in:
Tests in error:
testSanity(TestRoboActivityTest): XML file .\res\layout\test.xml line #-1 (sorry, not yet implemented): Error inflating class com.viewpagerindicator.CirclePageIndicator
My last resort was trying to use shadow classes:
#Implements(CirclePageIndicator.class)
public class CirclePageIndicatorShadow implements PageIndicator {
#Override
#Implementation
public void setViewPager(ViewPager view) {
// Stub
}
// etc.
}
and using #Config(shadows = {CirclePageIndicatorShadow.class}). This again resulted in
Tests in error:
testSanity(TestRoboActivityTest): XML file .\res\layout\test.xml line #-1 (sorry, not yet implemented): Error inflating class com.viewpagerindicator.CirclePageIndicator
Edit (December 2014)
Please note that the following stracktrace was added later by David Rabinowitz. While related, it is not the issue I was facing at the time.
Here is the stack trace:
android.view.InflateException: XML file .\res\layout\activity_home.xml line #-1 (sorry, not yet implemented): Error inflating class com.test.custom.RobotoTextView
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at org.robolectric.tester.android.view.RoboWindow.setContentView(RoboWindow.java:82)
at org.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:273)
at android.app.Activity.setContentView(Activity.java)
at com.example.testrobocustomfont.MainActivity.onCreate(MainActivity.java:12)
at com.example.testrobocustomfont.MainActivityTest.setUp(MainActivityTest.java:28)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:241)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createView(LayoutInflater.java:587)
at android.view.LayoutInflater.createView(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.rInflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:352)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at org.robolectric.tester.android.view.RoboWindow.setContentView(RoboWindow.java:82)
at org.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:273)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.robolectric.bytecode.ShadowWrangler$ShadowMethodPlan.run(ShadowWrangler.java:455)
at android.app.Activity.setContentView(Activity.java)
at com.example.testrobocustomfont.MainActivity.onCreate(MainActivity.java:12)
at com.example.testrobocustomfont.MainActivityTest.setUp(MainActivityTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
... 22 more
Caused by: java.lang.RuntimeException: error converting RobotoMedium.ttf using EnumConverter
at org.robolectric.shadows.Converter.convertAndFill(Converter.java:150)
at org.robolectric.shadows.Converter.convertAndFill(Converter.java:50)
at org.robolectric.shadows.ShadowResources.createTypedArray(ShadowResources.java:228)
at org.robolectric.shadows.ShadowResources.attrsToTypedArray(ShadowResources.java:203)
at org.robolectric.shadows.ShadowResources.access$000(ShadowResources.java:51)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:460)
at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java)
at android.widget.TextView.__constructor__(TextView.java:561)
at android.widget.TextView.<init>(TextView.java:447)
at android.widget.TextView.<init>(TextView.java:442)
at com.test.custom.RobotoTextView.<init>(RobotoTextView.java:16)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at org.robolectric.tester.android.view.RoboWindow.setContentView(RoboWindow.java:82)
at org.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:273)
at android.app.Activity.setContentView(Activity.java)
at com.example.testrobocustomfont.MainActivity.onCreate(MainActivity.java:12)
at com.example.testrobocustomfont.MainActivityTest.setUp(MainActivityTest.java:28)
... 22 more
Caused by: java.lang.RuntimeException: no value found for RobotoMedium.ttf
at org.robolectric.shadows.Converter$EnumOrFlagConverter.findValueFor(Converter.java:375)
at org.robolectric.shadows.Converter$EnumConverter.fillTypedValue(Converter.java:343)
at org.robolectric.shadows.Converter$EnumConverter.fillTypedValue(Converter.java:336)
at org.robolectric.shadows.Converter.convertAndFill(Converter.java:148)
at org.robolectric.shadows.Converter.convertAndFill(Converter.java:50)
at org.robolectric.shadows.ShadowResources.createTypedArray(ShadowResources.java:228)
at org.robolectric.shadows.ShadowResources.attrsToTypedArray(ShadowResources.java:203)
at org.robolectric.shadows.ShadowResources.access$000(ShadowResources.java:51)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:460)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.robolectric.bytecode.ShadowWrangler$ShadowMethodPlan.run(ShadowWrangler.java:455)
at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java)
at android.widget.TextView.$$robo$$TextView_347d___constructor__(TextView.java:561)
at android.widget.TextView.<init>(TextView.java:447)
at android.widget.TextView.<init>(TextView.java:442)
at com.test.custom.RobotoTextView.<init>(RobotoTextView.java:16)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createView(LayoutInflater.java:587)
at android.view.LayoutInflater.createView(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.rInflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:352)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at org.robolectric.tester.android.view.RoboWindow.setContentView(RoboWindow.java:82)
at org.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:273)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.robolectric.bytecode.ShadowWrangler$ShadowMethodPlan.run(ShadowWrangler.java:455)
at android.app.Activity.setContentView(Activity.java)
at com.example.testrobocustomfont.MainActivity.onCreate(MainActivity.java:12)
at com.example.testrobocustomfont.MainActivityTest.setUp(MainActivityTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
... 22 more
Could you guys please point me in the right direction? I'm out of ideas.
Thanks.
I test views in the same test class with the Activity that uses them. In this case I tell Robolectric to give an instance of that Activity and from that I get an instance of the inflated view:
#Before
public void setup(){
activity = Robolectric.buildActivity(MyActivity.class).create().get();
View view = LayoutInflater.from(activity).inflate(R.layout.myView, null);
}
#Test
public void allElementsInViewProduct(){
assertNotNull(view.findViewById(R.id.view1));
assertNotNull(view.findViewById(R.id.view2));
assertNotNull(view.findViewById(R.id.view3));
}
LE: I use Robolectric 3.0 so I am not sure if this applies to you.
Problem:
This issue happens, because gradle merges project dependencies (ex: compile project(':lib-custom')) and external dependencies (ex: compile 'lib.package:name:1.1.0') in different way. After dependencies were merged app has R.java file with all resources fields (colors, ids, drawables, ...). But generated R.java file looks different after merging submodules and external dependencies.
This problem exists only with projects, which have custom views in submodules. In case of external dependencies there is another issues, which can be easily fixed. Read about dependencies types here.
For project dependencies result R.java file contains all resource identifiers, but identifiers from submodule doesn't equals to their original integer identifiers:
com.lib.custom.R.color.primary != com.main.project.R.color.primary
For external dependencies merged R.java file just a merge result of R.java files from all external dependencies
com.lib.custom.R.color.primary == com.main.project.R.color.primary
Solution:
I've found two possible solutions:
Convert your dependencies from submodule to external where possible. For example for viepager indicator has an item in maven.org repository - fr.avianey.com.viewpagerindicator:library.
But this is still not enough - you need to add related item to project.properties file to your main sourceSet. More info here
Example:
// add this dependency to your gradle file instead of project dependency
compile 'fr.avianey.com.viewpagerindicator:library:2.4.1#aar'
// add library dependencies for robolectric (now robolectric knows
// about additional libraries to load resources)
android.library.reference.1=../../../app/build/intermediates/exploded-aar/fr.avianey.com.viewpagerindicator/library/2.4.1
You can check diff for this solution here
Move all your custom views under your main app. It is not good approach to move Custom views to app only because of unit testing, but this will also fix issue with Error inflating class.
I prefer first solution but it is not possible sometimes change project dependency to external.
I am also going to report about this issue to Robolectric team.
P.S. I have project on github related to this issue.
mTestRoboActivityView = LayoutInflater.from(new Activity()).inflate(R.layout.test, null);
In this line of code you used 'new Activity()' means instance of new Activity, that not for your current Activity.
You can resolve this issue by passing instance on current Activity.
Use like this-
public class TestRoboActivityTest {
private View mTestRoboActivityView;
private Context mContext;
public TestRoboActivityTest(Context mContext){
this.mContext=mContext;
}
#Before
public void setUp() throws Exception {
mTestRoboActivityView = (LayoutInflater.from(mContext)).inflate(R.layout.test, null);
}
#After
public void tearDown() throws Exception {
mTestRoboActivityView = null;
}
#Test
public void testSanity() throws Exception {
Assert.assertNotNull(mTestRoboActivityView);
}}
I am not sure that above code working fine but use for reference, instance of current Activity.
Refer it may be help you.
You cannot inflate views in Roboelectric since it does not use the complete android framework but instead mocks out all the Android API's.
You should not use roboelectric to test actual view display behavior.
It is to be used for unit tests and just to test your business logic and not view drawing/display etc. To achieve that You can programmatically create view objects and mock out certain parts which need the android system (Use something like Mockito or Powermock).
eg of simple view testing in roboelectic:
MyCustomView view = new MyCustomView();
assertNotNull(view.setSomeNo(2);
assertTrue(2, view.getSomeNo());
Also if you want to test rendering of how your view looks or renders etc, you should use functional testing frameworks such as Espresso or Robotium which run on an actual device.
Related
I'm using androidx.test.runner.AndroidJUnitRunner as my testInstrumentationRunner from androidTestImplementation 'androidx.test:runner:1.2.0'. Also, I annotated my test with androidx.test.ext.junit.runners.AndroidJUnit4 using the syntax #RunWith(AndroidJUnit4.class)
Suddenly I started getting the following error when I try to run ./gradlew connectedAndroidTest across all device types and emulators that I could try.
java.lang.RuntimeException: Delegate runner 'androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner' for AndroidJUnit4 could not be loaded.
at androidx.test.ext.junit.runners.AndroidJUnit4.throwInitializationError(AndroidJUnit4.java:92)
at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:82)
at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:51)
at androidx.test.ext.junit.runners.AndroidJUnit4.<init>(AndroidJUnit4.java:46)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at androidx.test.internal.runner.junit4.AndroidAnnotatedBuilder.runnerForClass(AndroidAnnotatedBuilder.java:63)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at androidx.test.internal.runner.AndroidRunnerBuilder.runnerForClass(AndroidRunnerBuilder.java:153)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at androidx.test.internal.runner.TestLoader.doCreateRunner(TestLoader.java:73)
at androidx.test.internal.runner.TestLoader.getRunnersFor(TestLoader.java:104)
at androidx.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:793)
at androidx.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:547)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:390)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2074)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:72)
... 17 more
Caused by: org.junit.runners.model.InitializationError
at org.junit.runners.ParentRunner.validate(ParentRunner.java:418)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner.<init>(AndroidJUnit4ClassRunner.java:43)
at androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner.<init>(AndroidJUnit4ClassRunner.java:48)
... 20 more
A similar stack trace is in Delegate runner 'androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner' for AndroidJUnit4 could not be loaded but the author seems to be asking about converting Java to Kotlin, which is not relevant to my question.
I figured out the cause was I mistakenly introduced a parameter to one of the test methods.
To fix the problem, I changed from this:
#Test
public void testTapReservationButton(List<String> settings) throws Exception {
//...
}
To this:
#Test
public void testTapReservationButton() throws Exception {
//...
}
The change was the removal of List<String> settings from the parameters list. It was a typo that I even added that, it a result of bad search and replace logic. Unfortunately the error is not helpful at all.
Note #Test is org.junit.Test
I'm running into problems when editing layouts that contain a ConstraintLayout element in Android Studio 3.0.
My dependencies are as follows:
project.ext.support_library_version = '26.0.0-beta2'
dependencies {
compile 'com.android.support.constraint:constraint-layout:1.1.0-beta1'
compile "com.android.support:palette-v7:${project.ext.support_library_version}"
compile "com.android.support:support-v13:${project.ext.support_library_version}"
compile "com.android.support:customtabs:${project.ext.support_library_version}"
compile "com.android.support:cardview-v7:${project.ext.support_library_version}"
...
}
The error showing in the UI editor is:
The following classes could not be instantiated:
- android.support.constraint.ConstraintLayout (Open Class, Show Exception, Clear Cache)
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE. If this is an unexpected error you can also try to build the project, then manually refresh the layout.
The stacktrace showing is the following:
java.lang.AssertionError: Entries for a declare-styleable should be after the array declaration.
at com.android.tools.idea.res.AppResourceRepository.getDeclaredArrayValues(AppResourceRepository.java:634)
at com.android.tools.idea.res.ResourceClassGenerator.generateStyleable(ResourceClassGenerator.java:249)
at com.android.tools.idea.res.ResourceClassGenerator.generate(ResourceClassGenerator.java:120)
at com.android.tools.idea.res.ResourceClassRegistry.findClassDefinition(ResourceClassRegistry.java:102)
at org.jetbrains.android.uipreview.ModuleClassLoader.findClass(ModuleClassLoader.java:101)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.jetbrains.android.uipreview.ModuleClassLoader.loadClass(ModuleClassLoader.java:215)
at android.support.constraint.ConstraintLayout.init(ConstraintLayout.java:494)
at android.support.constraint.ConstraintLayout.<init>(ConstraintLayout.java:466)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:481)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:264)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:222)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:206)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:337)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:348)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:248)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:334)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:382)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:193)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:591)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$3(RenderTask.java:739)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Cleaning/building/invalidate caches etc. Does not help. Note that the app is functioning perfectly, it's only the UI Editor that is not working anymore.
Who faces the same issue, or has a solution for this?
There's a problem with support library 26.0.0-beta2 -- you need to switch back to beta 1 in order for things to work in Android Studio.
I have updated the AppCompat libraries to 24.2.1 and the SDK to Android 7 in my Eclipse install.
Since that, I'm not capable to run any of my apps.
I appreciate if you can help a bit with that...
E/AndroidRuntime(17555): java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/os/BuildCompat;
E/AndroidRuntime(17555): at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:196)
E/AndroidRuntime(17555): at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)
E/AndroidRuntime(17555): at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)
E/AndroidRuntime(17555): at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
E/AndroidRuntime(17555): at com.ryosoftware.calendareventsnotifier.MainActivity.onCreate(MainActivity.java:844)
E/AndroidRuntime(17555): at android.app.Activity.performCreate(Activity.java:5990)
E/AndroidRuntime(17555): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
E/AndroidRuntime(17555): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
E/AndroidRuntime(17555): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
E/AndroidRuntime(17555): at android.app.ActivityThread.access$900(ActivityThread.java:154)
E/AndroidRuntime(17555): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
E/AndroidRuntime(17555): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(17555): at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(17555): at android.app.ActivityThread.main(ActivityThread.java:5294)
E/AndroidRuntime(17555): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(17555): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(17555): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
E/AndroidRuntime(17555): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
E/AndroidRuntime(17555): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.os.BuildCompat" on path: DexPathList[[zip file "/data/app/com.ryosoftware.calendareventsnotifier-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
E/AndroidRuntime(17555): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
E/AndroidRuntime(17555): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
E/AndroidRuntime(17555): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
E/AndroidRuntime(17555): ... 18 more
E/AndroidRuntime(17555): Suppressed: java.lang.ClassNotFoundException: android.support.v4.os.BuildCompat
E/AndroidRuntime(17555): at java.lang.Class.classForName(Native Method)
E/AndroidRuntime(17555): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
E/AndroidRuntime(17555): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
E/AndroidRuntime(17555): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
E/AndroidRuntime(17555): ... 19 more
E/AndroidRuntime(17555): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
You are getting NoClassDefFoundError & ClassNotFoundException
NoClassDefFoundError in Java comes when Java Virtual Machine is not
able to find a particular class at runtime which was available at
compile time.
FYI
You are using Eclipse. Android Studio is a far simpler way to develop for Android if you manage to get the hang of it. For developers who have been using Eclipse, migrating to Studio is a nightmare for them. Eclipse is dead (My personal opinion).
For your NoClassDefFoundError problem goto rebuild option under Project > Clean and then select the project you want to clean up .Then Restart your Eclipse and run again .
Solutions
Check your classpath contains that jar (AppCompat), if your classpath
doesn't contain the jar then just add that class in your classpath.
You should Use Android Studio instead of Eclipse . Read
Support Library Features
The Gradle build script dependency identifier for this library is as follows:
com.android.support:appcompat-v7:24.2.1
Then Clean-Rebuild-Restart IDE
In my case
clean project
invalidate and restart
it's work
Another ugly reason for this to be caused is if you're trying to attach a debugger with breakpoint on something that happens during Activity creation.
I added two classes in my java package and its working fine, keep both class as showing in below image:
AsyncTaskCompat.java
public class AsyncTaskCompat {
/**
* Executes the task with the specified parameters, allowing multiple tasks to run in parallel
* on a pool of threads managed by {#link android.os.AsyncTask}.
*
* #param task The {#link android.os.AsyncTask} to execute.
* #param params The parameters of the task.
* #return the instance of AsyncTask.
*/
public static <Params, Progress, Result> AsyncTask<Params, Progress, Result> executeParallel(
AsyncTask<Params, Progress, Result> task, Params... params) {
if (task == null) {
throw new IllegalArgumentException("task can not be null");
}
if (Build.VERSION.SDK_INT >= 11) {
// From API 11 onwards, we need to manually select the THREAD_POOL_EXECUTOR
AsyncTaskCompatHoneycomb.executeParallel(task, params);
} else {
// Before API 11, all tasks were run in parallel
task.execute(params);
}
return task;
}
}
AsyncTaskCompatHoneycomb.java
class AsyncTaskCompatHoneycomb {
static <Params, Progress, Result> void executeParallel(
AsyncTask<Params, Progress, Result> task, Params... params) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
}
}
In my case the error was caused due to the use of the entire gms:play-services library. Some components of gms:play-services refer to versions other than explicitly specified by you.
Use only the required play-services which are required e.g: 'com.google.android.gms:play-services-analytics:10.2.0' instead of 'com.google.android.gms:play-services:10.2.0'.
Also see: All com.android.support libraries must use the exact same version specification
Good read: https://blog.mindorks.com/avoiding-conflicts-in-android-gradle-dependencies-28e4200ca235
public class MApplication extends Application {
#Override
protected void attachBaseContext(Context paramContext) {
super.attachBaseContext(paramContext);
Helper.install(MApplication.this);
}
}
Here we override the attachBaseContext() method to add the Helper.install(MApplication.this); line of code.
Note: Since some of SDK classes now need to be loaded before using, the loading process is done by Helper.install(). Developer needs to invoke this method before using any SDK functionality. Failing to do so will result in unexpected crashes.
reference
Unless the app crashes with java.lang.NoClassDefFoundError: Failed resolution of:....
Update implementation com.google.android.gms:play-services-maps in gradle to a newer version.
That worked for me.
I had a very similar issue, the problem was using an old (deprecated) package, more that 4 years without maintenance. Check your reciently added packages and check if they're compatible with you Android SDK.
In my case, just cleaning and rebuilding the project worked for me.
When inflating a layout in a Fragment, with the LayoutInflater, i am getting this exception:
./res/layout/locations_list.xml line #-1 (sorry, not yet implemented): Error inflating class com.costum.android.widget.LoadMoreListView
I figured out that this is happening when inflating a custom layout in
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.locations_list, container, false);
}
Edit This is the locations_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<com.costum.android.widget.LoadMoreListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp" />
</LinearLayout>
any other layout inflator works, like in this test:
#Test
public void testInflator() {
ActivityController<SherlockFragmentActivity> activityController = CustomTestRunner
.startActivity();
SherlockFragmentActivity activity = activityController.get();
LayoutInflater from = LayoutInflater.from(activity);
View view = from
.inflate(com.twisper.R.layout.locations_list_item, null);
assertNotNull(view);
}
I am using Robolectric with the 2.2-SNAPSHOTs , Now my question is how could I work around this issue or how could I implement the missing functionality, the robolectric documentation is very sparse, hence I had trouble to find any starting point.
Full Stack Trace
android.view.InflateException: XML file ./res/layout/locations_list.xml line #-1 (sorry, not yet implemented): Error inflating class com.costum.android.widget.LoadMoreListView
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
...
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createView(LayoutInflater.java:587)
at android.view.LayoutInflater.createView(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.rInflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
...
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -9
at java.lang.String.substring(String.java:1911)
at org.robolectric.res.ResName.qualifyResName(ResName.java:50)
at org.robolectric.res.Attribute.getResourceReference(Attribute.java:138)
at org.robolectric.res.Attribute.qualifiedValue(Attribute.java:127)
at org.robolectric.res.builder.XmlFileBuilder$XmlResourceParserImpl.qualify(XmlFileBuilder.java:316)
at org.robolectric.res.builder.XmlFileBuilder$XmlResourceParserImpl.getAttributeValue(XmlFileBuilder.java:340)
at org.robolectric.shadows.ShadowResources.findAttributeValue(ShadowResources.java:252)
at org.robolectric.shadows.ShadowResources.attrsToTypedArray(ShadowResources.java:188)
at org.robolectric.shadows.ShadowResources.access$000(ShadowResources.java:51)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:460)
I also posted the issue on robolectrics issue tracker.
I figured it out, and if you got stock to such an situation just look for your IDs in the XML, even though
android:id="#+id/android:list"
is often seen in some example code it must be:
android:id="#android:id/list"
The last version of Robolectric (3.0-Snapshot) has some problems with customized views.
To fix that, do the follow:
In the module app where your code is, create a file called project.properties, at the same level as AndroidManifest.xml
Fill the content with reference to the class folder of build. e.g. for StaggeredGridView:
android.library.reference.1=../../build/intermediates/exploded-aar/com.etsy.android.grid/library/1.0.5
Here you must check three things:
There should be one line per reference
Each reference has one number (1, 2, 3), which should be increase by one each time
The version number folder at the end must match with the version number in your build.gradle file.
You have an example of project working here:
https://github.com/jiahaoliuliu/RobolectricSample/blob/master/app/src/main/project.properties
In my case I got this problem when I had this in my app:
<dependency>
<groupId>com.google.android</groupId>
<artifactId>support-v4</artifactId>
<version>r13</version>
</dependency>
but this in my unittest project:
<dependency>
<groupId>com.google.android</groupId>
<artifactId>support-v4</artifactId>
<version>r7</version>
</dependency>
The older version of the support library I used in the tests didn't contain the class I was trying to use (android.support.v4.widget.DrawerLayout).
In my case, my FragmentActivity XML was inflating a Fragment that was expecting a Bundle. I swapped the XML for a FrameLayout holder and added the fragment with the proper bundle in the activity.
i want to test a class which extends from org.opencv.highgui.VideoCapture
public class FilterCamera extends VideoCapture { .. }
.. in my main project i have no problem to create an instance of FilterCamera:
FilterCamera filterCamera = new FilterCamera(..);
but when i try it in my test project i always get:
java.lang.NoClassDefFoundError: valkyrie.filter.FilterCamera
at valkyrie.filter.test.FilterCameraTest.testGetFilterList(FilterCameraTest.java:33)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
i get no errors when i test other classes in the same package or when i remove the "extends VideoCapture"
any idea?