3

I'm trying to connect my Android Studio - app to an exchange webservice, but i keep getting the following error:

java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/message/BasicLineFormatter; in class Lorg/apache/http/message/BasicLineFormatter; or its superclasses (declaration of 'org.apache.http.message.BasicLineFormatter' appears in /system/framework/ext.jar)

Here are the.jars that ive added to /libs/ in my android project: Here are the .jars i've added to my project

Here is the onCreate method were the error occurs:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ExchangeService service = new ExchangeService(); // this is where the error is hitting
ExchangeCredentials credentials = new WebCredentials("user", "pw");
service.setCredentials(credentials);

try {
    service.setUrl(new URI("uri"));
} catch (URISyntaxException e) {
    e.printStackTrace();
}

EmailMessage msg= null;
try {
    msg = new EmailMessage(service);
    msg.setSubject("Hello world!");
    msg.setBody(MessageBody.getMessageBodyFromText("Sent using the EWS Java API."));
    msg.getToRecipients().add("email");
    msg.send();
} catch (Exception e) {
    e.printStackTrace();
}

}

And my dependencies:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.0.0'
//compile 'org.apache.httpcomponents:httpclient:4.3'
//compile 'org.apache.httpcomponents:httpcore:4.3.3'
//compile 'commons-logging:commons-logging:1.2'
// compile 'joda-time:joda-time:2.7'
     compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
}

I'm currently working on a mac (if the screenshot didnt give it away), and as noted in Android Studio on a Android app.

mathletics
  • 271
  • 1
  • 4
  • 15
  • Consider looking at: http://stackoverflow.com/questions/23830077/httpclient-4-3-3-on-android – dhke Mar 27 '15 at 13:02
  • @dhke Tried importing the one in the comment section there, and am now getting: `Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2` – mathletics Mar 27 '15 at 13:08
  • 1
    The error message with the interesting information is most likely a bit above that. – dhke Mar 27 '15 at 13:13
  • @dhke `com.android.dex.DexException: Multiple dex files define Lorg/apache/http/Consts;` getting closer! – mathletics Mar 27 '15 at 13:14
  • See here: http://stackoverflow.com/questions/15537731/httpclient-from-apache-in-android ;-) – dhke Mar 27 '15 at 13:19

1 Answers1

2

The problem stems from trying to use a different (usually newer) version of apache httpcomponents on android than what the operating system ships with.

Those versions are often API incompatible but use the same class names resulting in the dreaded

Multiple dex files define Lorg/apache/http/Consts;

or similar compile-time error messages.

In general, you will get a load of problems trying to use a different version of the httpcomponents on android than the one stock android provides.

The stock android version seems not to be specified, but most sources seem to agree that it is based on a modified version of httpcomponents-4.0.1. The android SDK, however, includes version 4.1.1 of httpclient (in tools/lib/httpclient-4.1.1.jar).

I think the most common solution is to avoid newer versions of the httpcomponents.

Apache now also provides a custom version of the httpclient. However, as you note yourself, this still can cause problems.

The problem becomes worse when you need other subparts of the httpcomponents, like httpmime which pull in their respective versions of httpcomponent jars.

dhke
  • 15,008
  • 2
  • 39
  • 56