testCompile with aar dependency

239 views
Skip to first unread message

Jordan Hamel

unread,
Nov 6, 2015, 6:57:44 PM11/6/15
to Robolectric
When testing with Robolectric, I am getting a NoClassDefFound error for an aar dependency. Here is the error and my build.gradle file:

java.lang.NoClassDefFoundError: Could not initialize class com.mapbox.mapboxsdk.views.NativeMapView


dependencies {
 compile fileTree
(dir: 'libs', include: ['*.jar'])
 compile
'com.android.support:appcompat-v7:23.1.0'
 compile
'com.android.support:design:23.1.0'
 compile
'com.mcxiaoke.volley:library:1.0.19'
 compile
('com.crashlytics.sdk.android:crashlytics:2.5.2@aar') {
 transitive
= true;
 
}
 compile
'com.mixpanel.android:mixpanel-android:4.6.4'
 compile
'com.google.android.gms:play-services:6.5.87'
 compile
'commons-io:commons-io:2.0.1'
 compile
('com.mapbox.mapboxsdk:mapbox-android-sdk:2.2.0@aar'){
 transitive
=true
 
}

 testCompile
'junit:junit:4.12'
 testCompile
'org.hamcrest:hamcrest-library:1.3'
 testCompile
'org.apache.maven:maven-ant-tasks:2.1.3' // fixes issue on linux/mac
 testCompile
'org.robolectric:robolectric:3.0'
 testCompile
'org.robolectric:shadows-play-services:3.0'
 testCompile
'org.robolectric:shadows-support-v4:3.0'
 testCompile
('com.mapbox.mapboxsdk:mapbox-android-sdk:2.2.0@aar'){
 transitive
=true
 
}
}

I've tried the solutions from the following Stack Overflow questions, but they aren't working:

Has anybody had luck with getting this to work?

Nico Küchler

unread,
Nov 8, 2015, 2:14:01 AM11/8/15
to Robolectric
AAR dependencies works with robolectric. Often its just a configuration issue.

Potential issues are:
  • You should use RobolectricGradleTestRunner instead of RobolectricTestRunner
  • You should not specify the AndroidManifest.xml location by self

Best would be if you post your test class and the base class if you have one. 
Message has been deleted

Jordan Hamel

unread,
Nov 9, 2015, 11:52:09 AM11/9/15
to Robolectric
Here is my test class. I do not have a base class. I also tried specifying the manifest in the configs, but that did not work.

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class MyActivityUnitTest {

    private MyActivity activity;

    @Before
    public void setUp() {
        activity = Robolectric.setupActivity(MyActivity.class);

Jordan Hamel

unread,
Nov 10, 2015, 5:57:23 PM11/10/15
to Robolectric
I think it might be specific to the Mapbox library. I cloned this sample project that has an aar dependency https://github.com/nenick/AndroidStudioAndRobolectric/tree/library-with-aar, and the tests run fine. When I add the Mapbox dependency it throws an error when it tries to inflate a map view. 

compile ('com.mapbox.mapboxsdk:mapbox-android-sdk:2.2.0@aar'){
 transitive
=true
}


Nico Küchler

unread,
Nov 10, 2015, 11:56:04 PM11/10/15
to Robolectric
The same error from your first post or a different error?

Jordan Hamel

unread,
Nov 11, 2015, 12:04:30 PM11/11/15
to Robolectric
It's the same error.

On Tue, Nov 10, 2015 at 8:56 PM Nico Küchler <kuchl...@gmail.com> wrote:
The same error from your first post or a different error?

--
You received this message because you are subscribed to a topic in the Google Groups "Robolectric" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/robolectric/6roTw-50et4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jordan Hamel

unread,
Nov 12, 2015, 6:04:15 PM11/12/15
to Robolectric
Just as a follow up to anyone who might be having the same error - the problem was with the Mapbox dependency. Since Mapbox runs on OpenGL, which requires active management of the activity lifecycle, you can't inflate a Mapview in your Robolectric tests. 

On Wednesday, November 11, 2015 at 9:04:30 AM UTC-8, Jordan Hamel wrote:
It's the same error.

On Tue, Nov 10, 2015 at 8:56 PM Nico Küchler <kuchl...@gmail.com> wrote:
The same error from your first post or a different error?

--
You received this message because you are subscribed to a topic in the Google Groups "Robolectric" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/robolectric/6roTw-50et4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to robolectric+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages