Android Studio项目中的NoSuchFieldError

时间:2015-03-27 12:34:58

标签: java android android-studio apache-httpclient-4.x

我正在尝试将我的Android Studio - 应用程序连接到Exchange Web服务,但我一直收到以下错误:

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)

以下是我在android项目中添加到/ libs /的.jars: Here are the .jars i've added to my project

这是onCreate方法发生错误:

@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();
}

}

我的依赖项:

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'
}

我正在使用Mac(如果屏幕截图没有放弃),并且如Android应用中的Android Studio中所述。

1 个答案:

答案 0 :(得分:1)

问题源于尝试在Android上使用与操作系统附带的不同(通常更新)的apache httpcomponents版本。

这些版本通常是API不兼容的,但使用相同的类名,导致可怕的

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

或类似的编译时错误消息。

一般情况下,尝试在android上使用不同版本的httpcomponents时,会遇到一些问题,而不是android提供的那个版本。

似乎没有指定股票Android版本,但大多数消息来源似乎都同意它基于httpcomponents-4.0.1的修改版本。但是,Android SDK包含4.1.1的版本httpclient(在tools/lib/httpclient-4.1.1.jar中)。

我认为最常见的解决方案是避免使用较新版本的httpcomponents。

Apache现在还提供了httpclient的自定义版本。但是,正如您自己注意到的,这仍然会导致问题。

当您需要httpcomponents的其他子部分时,问题会变得更糟,例如httpmime,它会引入各自版本的httpcomponent jar。