Encountering NoClassDefFoundError for: Lorg/apache/http/impl/nio/reactor/DefaultConnectingIOReactor When Implementing Spoonacular API via Unirest on Android

Issue Overview

I am attempting to integrate the Spoonacular API from RapidAPI into my Android application using Unirest for API requests. Following the configuration instructions provided in the Without Maven section of their official documentation, I downloaded the Unirest JAR file and added the necessary configurations in my build.gradle file:

apply plugin: 'com.android.application'

android {
    ...
    useLibrary 'org.apache.http.legacy'
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        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'
}

apply plugin: 'com.google.gms.google-services'

Java Code Snippet

Here is the relevant Java code I am using:

HttpResponse<JsonNode> result = null;
try {
    result = Unirest.get("https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/findByIngredients?ingredients=egg%2Cyogurt&number=5&ranking=1")
            .header("X-Mashape-Key", "your_api_key")
            .header("X-Mashape-Host", "spoonacular-recipe-food-nutrition-v1.p.mashape.com")
            .asJson();
} catch (UnirestException ex) {
    ex.printStackTrace();
}

Error Encountered

I am running into the following error on execution:

09-19 11:53:19.674 6789-6789/com.example.hassen.androidpim E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.hassen.androidpim, PID: 6789
    java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/impl/nio/reactor/DefaultConnectingIOReactor;
        at com.mashape.unirest.http.options.Options.refresh(Options.java:85)
        at com.mashape.unirest.http.options.Options.<clinit>(Options.java:46)
        at com.mashape.unirest.http.options.Options.getOption(Options.java:42)

Request for Assistance

Could you provide guidance on how to resolve this issue? Thank you!

Hey Alice, it looks like your issue stems from the missing class in the HttpComponents library. The NoClassDefFoundError usually means there's a problem with your library setup.

To resolve this, try updating your build.gradle dependencies to use a compatible HttpComponents version. Here's a simple fix:

dependencies {
    implementation 'org.apache.httpcomponents:httpcore:4.4.14'
    implementation 'org.apache.httpcomponents:httpclient:4.5.13'
    implementation('org.apache.httpcomponents:httpmime:4.5.13') {
        exclude module: 'httpclient'
    }
    implementation 'org.apache.httpcomponents:httpclient-android:4.3.5'
    ...
}

Ensure all libraries are up to date and match the required versions. Sync the project and rebuild.