Warning: Use of undefined constant _FILE_ - assumed '_FILE_' (this will throw an Error in a future version of PHP) in /home/bolyarco/www-ikratko/ogrelab/wp-content/plugins/ad-blocker-stats-vip/ad-blocker-stats-vip.php on line 13

Warning: count(): Parameter must be an array or an object that implements Countable in /home/bolyarco/www-ikratko/ogrelab/wp-content/plugins/microkids-related-posts/microkids-related-posts.php on line 645

Using newer version of HttpClient on Android (like 4.1.x)

Posted / Публикувана 2011-08-20 in category / в категория: Android

Warning: count(): Parameter must be an array or an object that implements Countable in /home/bolyarco/www-ikratko/ogrelab/wp-content/plugins/microkids-related-posts/microkids-related-posts.php on line 645

[EDIT] 2015-10-15: Now apache provides jar for android (currently Also if you are targeting Android API 23 and newer you can use packages for Android maintained by Marek Sebera (currently

[EDIT] This publication and method are old and deprecated. Please use http://code.google.com/p/httpclientandroidlib/ instead.

If you ended up here that is probably because you hit a bug  in the built-in HttpClient library which is not just old, it is beta quality (httpclient-4.0-beta1.jar). Strangely enough there is no hope that Android team will update it soon (also this is impossible without breaking backward compatibility…).

Fear not, there is a remedy.

Sadly we cannot replace the build-in library nor just add newer version but we can "trick" the system and move the library in another namespace in order to avoid conflict with the built-in lib.

The quick solution: here is an zip file containing all the required files (JARs converted using "ogrelab-" namespace prefix). Jump directly to step 7 in the "thorough" solution bellow.

The thorough solution:


1. Download JarJar -- this is the tool which we will use to move the classes in new namespace

2. Download latest HttpClientCommonsLogging and Log4j (you will need "binary" packages) and extract them somewhere

3. Create a temporary dir somewhere like tmp_httpclient for example find following Jars and copy them into the temp dir (versions may differ):

  • commons-logging-1.1.1.jar
  • httpclient-4.1.2.jar
  • httpcore-4.1.2.jar
  • httpmime-4.1.2.jar
  • log4j-1.2.16.jar
Please note: there are additional JARs especially in the HttpClient archive but I don't use them, so I've excluded them from the list. Depending on your situation you may need them at some point so you will have to "convert" them too.
4. Copy jarjar-1.1.jar to the temp dir too
5. Download this text file which contains rules for jarjar how to process the jars. Extract it into the temp dir. You may want to edit it and replace "ogrelab" with some other identifier like your or your company's name.
6. "Convert" all the jars in the temp dir like this:
java -jar jarjar-1.1.jar process rules.txt LIBNAME.jar ogrelab-LIBNAME.jar
replacing LIBNAME appropriately.
7. Copy the new jars in the lib/ directory of your android project (you may need to create it first)
8. In Eclipse:
  • select your project
  • right button -> Properties OR from the main menu Project -> Properties
  • in the left pane select Java Build Path
  • in the right pane select Libraries tab and use the "Add JARs…" button to add the jars from the lib/ dir.
  • go to Order and Export tab and move the new jars to be on top, i.e. above Android API
At this stage you will be able to use the new version ot the HttpClient but it will produce warnings in the LogCat about log4j not being initialized properly. You will need just one more thing:
9. Download log4j.properties file, extract it and put it in src/ directory of your project.

You are done. Be careful not to mix imports from the built-in and the new version.

Happy Http-ing :-)


7 Responses to “Using newer version of HttpClient on Android (like 4.1.x)”

  1. Moritz Post Says:

    That is a very nice write up. Thanks for sharing.

    I experienced a bug in the httpclient on a 2.3.6 device that didn't occur on 4.0. Bundling the latest httpclient with my application fixes the issue on all devices.

    The issue was related to overwriting request params on a per request basis.

    Thanks again. :)

  2. hus buy Says:

    Thanks a lot!
    This is exactly what I was looking for! Great job!

    Thanks again and again!

  3. hus buy Says:

    Hi again,

    I have just tested it, it works well.

    i am wondering if it is necessary to add log4j jar?


  4. Ogi Bankov / Огнян Банков Says:

    If you don't include log4j.jar you will get warnings at build time. It is not fatal but is annoying.
    All in all, please note the first sentence in this publication. It is recommended to use http://code.google.com/p/httpclientandroidlib/ instead of the method described here.

  5. hus buy Says:

    Thanks for your quick response and your advise!

    Best regards,

  6. Ogi Bankov / Огнян Банков Says:

    YW :-)

  7. Gabry Says:

    Thanks a lot, this is exactly what i was looking for. Your blog was a godsend. I can't believe google refuses to give us a support library for Apache HttpClient.