NoClassDefFoundError for DefaultConnectingIOReactor when setting up Unirest library in Android project

I’m working on an Android app and trying to use the Unirest HTTP library to connect to a food recipe API. However, I keep running into a class not found error.

Here’s my gradle configuration:

apply plugin: 'com.android.application'

android {
    useLibrary 'org.apache.http.legacy'
    
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }
}

dependencies {
    implementation files('libs/unirest-java-1.4.9.jar')
    implementation 'org.apache.httpcomponents:httpmime:4.5.2'
    implementation 'org.apache.httpcomponents:httpclient-android:4.3.5'
}

And here’s the Java code that’s causing issues:

HttpResponse<JsonNode> apiResponse = null;
try {
    apiResponse = Unirest.get("https://api.example.com/food/search?query=chicken&limit=10")
            .header("API-Key", "my-secret-key")
            .header("Host", "api.example.com")
            .asJson();
} catch (UnirestException ex) {
    ex.printStackTrace();
}

The error I’m getting is:

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/impl/nio/reactor/DefaultConnectingIOReactor;
    at com.mashape.unirest.http.options.Options.refresh(Options.java:85)

It seems like some Apache HTTP component classes are missing. Has anyone dealt with this before? What dependencies am I missing to make Unirest work properly on Android?

I ran into this exact same problem about six months ago when trying to integrate Unirest into my Android project. The root cause is that Unirest expects the full Apache HttpComponents suite including the NIO reactor classes, but Android only provides a subset of these libraries even with the legacy HTTP support enabled. What worked for me was adding the httpcore-nio dependency explicitly to my gradle file, but even then I encountered runtime issues because Android’s Dalvik/ART runtime doesn’t play nicely with these desktop Java networking libraries. After spending way too much time debugging, I ended up replacing Unirest with Volley for simple requests and OkHttp for more complex scenarios. The migration wasn’t too painful since the API patterns are similar, and both libraries are specifically designed for Android’s constraints. If you’re set on keeping Unirest, you might have better luck with an older version that has fewer NIO dependencies, but I’d recommend switching to a more Android-friendly HTTP client.

honestly, unirest can be a pain on android. i had similar issues with the nio reactor too. might wanna check out okhttp or retrofit, they’re way more friendly for mobile dev and super easy to implement. helped me out a lot!

The issue stems from Android’s limited support for Apache HttpComponents NIO classes. DefaultConnectingIOReactor is part of the httpcore-nio library which isn’t compatible with Android’s runtime environment. Even with the legacy HTTP library enabled, these NIO classes remain unavailable. I encountered this exact problem when migrating a desktop application to Android. The solution was switching to HttpURLConnection or implementing a custom HTTP client using standard Android networking APIs. Unirest relies heavily on desktop Java HTTP libraries that simply don’t translate well to mobile environments. If you must use Unirest, consider running your HTTP requests through a separate service or using the synchronous HTTP components instead, though this defeats much of Unirest’s purpose.