4

I want to include Apache HttpClient 4.1 to my project.

So, I added to my build.gradle:

dependencies {
    ..
    compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.1'
}

Running the the sanity test:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        try {
            EntityUtils.consume(null);
        } catch (Exception e){
            System.out.print(e.getMessage());
        }
    }

gives the Error:

java.lang.NoSuchMethodError: No static method consume(Lorg/apache/http/HttpEntity;)V in class Lorg/apache/http/util/EntityUtils; or its super classes (declaration of 'org.apache.http.util.EntityUtils' appears in /system/framework/org.apache.http.legacy.boot.jar)

So why is Android calling org.apache.http.legacy.boot.jar, instead of calling the added Apache HttpClient 4.1 library? And how could I force to call it?

1
  • Any solution on this? Commented Apr 10, 2020 at 22:02

2 Answers 2

1

Android 6.0 release removes support for the Apache HTTP client.

Read more details here: https://developer.android.com/about/versions/marshmallow/android-6.0-changes#behavior-apache-http-client

If you are set on using 4.1 instead of latest version try adding this dependency:

 compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.1.3'

After you build, you may want to restart your Android Studio just to make sure its not some strange glitch.

Sign up to request clarification or add additional context in comments.

1 Comment

I know but somehow Android is connecting all the org.apache calls to the not existing Appache client. Its weird! But I tried what you said, added the dependency you suggested and did a "Invalidate Caches & Restart" of Android Studio and also a Clean Project. But sill the error remains.
1

Try add to your build.gradle

android {
    useLibrary 'org.apache.http.legacy'
}

1 Comment

This would activate org.apache.http.legacy.boot.jar which includes an Apache HttpClient but not the HttpClient version I would like to have. E.g. the method EntityUtils.consume() is not available in this version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.