NoClassDefFoundError for DefaultConnectingIOReactor when using Unirest with Android for API integration

I’m working on an Android app and trying to connect to a food recipe API using the Unirest library. I’ve followed the instructions for setting up Unirest without Maven, and I’ve added the jar file to my project.

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'
        exclude 'META-INF/ASL2.0'
    }
}

dependencies {
    implementation 'com.rapidapi:rapidconnect-android:0.1'
    implementation files('libs/unirest-java-1.4.9.jar')
    implementation 'org.json:json:20171018'
    implementation('org.apache.httpcomponents:httpmime:4.3.6') {
        exclude module: 'httpclient'
    }
    implementation 'org.apache.httpcomponents:httpclient-android:4.3.5'
}

And here’s my API call code:

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

However, I’m encountering a crash when the app attempts to make the request. The error indicates that the DefaultConnectingIOReactor class cannot be found. Has anyone experienced this before? What could I be missing in my configuration?

Check your proguard rules - they might be stripping out reactor classes during build. I had the same crash and fixed it by adding -keep class org.apache.http.nio.** to proguard-rules.pro. Also, unirest’s tricky with newer Android versions. I’d switch to okhttp or retrofit - they play much better with Android.

Had the same issue with Unirest in an Android project. That NoClassDefFoundError means you’re missing dependencies for async HTTP requests. Add these to your gradle file: implementation 'org.apache.httpcomponents:httpasyncclient:4.1.3' and implementation 'org.apache.httpcomponents:httpcore-nio:4.4.10'. Unirest needs these for async stuff to work. Clean and rebuild after adding them.

This happens because Unirest needs HttpAsyncClient components that aren’t in the main jar. I hit the same issue migrating an old Android project. DefaultConnectingIOReactor is part of the NIO components for non-blocking I/O operations. Besides adding the missing dependencies others mentioned, you might need to explicitly include httpcore:4.4.10 since version conflicts pop up. Also check if you’re running this on the main thread - Android blocks async operations there. Try wrapping your API call in an AsyncTask or use a background thread. Your legacy HTTP library declaration should help, but packaging options sometimes mess with class loading.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.